| 1 | /* |
| 2 | Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com> |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | /* |
| 18 | * High level api to nfs filesystems |
| 19 | */ |
| 20 | #ifdef WIN32 |
| 21 | #include "win32_compat.h" |
| 22 | #define DllExport |
| 23 | #else |
| 24 | #include <strings.h> |
| 25 | #include <sys/statvfs.h> |
| 26 | #include <utime.h> |
| 27 | #include <unistd.h> |
| 28 | #endif/*WIN32*/ |
| 29 | |
| 30 | #define _GNU_SOURCE |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <assert.h> |
| 37 | #include <errno.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <fcntl.h> |
| 41 | #include "libnfs.h" |
| 42 | #include "libnfs-raw.h" |
| 43 | #include "libnfs-raw-mount.h" |
| 44 | #include "libnfs-raw-nfs.h" |
| 45 | #include "libnfs-private.h" |
| 46 | |
| 47 | struct nfsdir { |
| 48 | struct nfsdirent *entries; |
| 49 | struct nfsdirent *current; |
| 50 | }; |
| 51 | |
| 52 | struct nfsfh { |
| 53 | struct nfs_fh3 fh; |
| 54 | int is_sync; |
| 55 | uint64_t offset; |
| 56 | }; |
| 57 | |
| 58 | struct nfs_context { |
| 59 | struct rpc_context *rpc; |
| 60 | char *server; |
| 61 | char *export; |
| 62 | struct nfs_fh3 rootfh; |
| 63 | uint64_t readmax; |
| 64 | uint64_t writemax; |
| 65 | }; |
| 66 | |
| 67 | void nfs_free_nfsdir(struct nfsdir *nfsdir) |
| 68 | { |
| 69 | while (nfsdir->entries) { |
| 70 | struct nfsdirent *dirent = nfsdir->entries->next; |
| 71 | if (nfsdir->entries->name != NULL) { |
| 72 | free(nfsdir->entries->name); |
| 73 | } |
| 74 | free(nfsdir->entries); |
| 75 | nfsdir->entries = dirent; |
| 76 | } |
| 77 | free(nfsdir); |
| 78 | } |
| 79 | |
| 80 | struct nfs_cb_data; |
| 81 | typedef int (*continue_func)(struct nfs_context *nfs, struct nfs_cb_data *data); |
| 82 | |
| 83 | struct nfs_cb_data { |
| 84 | struct nfs_context *nfs; |
| 85 | struct nfsfh *nfsfh; |
| 86 | char *saved_path, *path; |
| 87 | |
| 88 | nfs_cb cb; |
| 89 | void *private_data; |
| 90 | |
| 91 | continue_func continue_cb; |
| 92 | void *continue_data; |
| 93 | void (*free_continue_data)(void *); |
| 94 | int continue_int; |
| 95 | |
| 96 | struct nfs_fh3 fh; |
| 97 | |
| 98 | /* for multi-read/write calls. */ |
| 99 | int error; |
| 100 | int cancel; |
| 101 | int num_calls; |
| 102 | uint64_t start_offset, max_offset; |
| 103 | char *buffer; |
| 104 | }; |
| 105 | |
| 106 | struct nfs_mcb_data { |
| 107 | struct nfs_cb_data *data; |
| 108 | uint64_t offset; |
| 109 | uint64_t count; |
| 110 | }; |
| 111 | |
| 112 | static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh); |
| 113 | |
| 114 | |
| 115 | void nfs_set_auth(struct nfs_context *nfs, AUTH *auth) |
| 116 | { |
| 117 | rpc_set_auth(nfs->rpc, auth); |
| 118 | } |
| 119 | |
| 120 | int nfs_get_fd(struct nfs_context *nfs) |
| 121 | { |
| 122 | return rpc_get_fd(nfs->rpc); |
| 123 | } |
| 124 | |
| 125 | int nfs_queue_length(struct nfs_context *nfs) |
| 126 | { |
| 127 | return rpc_queue_length(nfs->rpc); |
| 128 | } |
| 129 | |
| 130 | int nfs_which_events(struct nfs_context *nfs) |
| 131 | { |
| 132 | return rpc_which_events(nfs->rpc); |
| 133 | } |
| 134 | |
| 135 | int nfs_service(struct nfs_context *nfs, int revents) |
| 136 | { |
| 137 | return rpc_service(nfs->rpc, revents); |
| 138 | } |
| 139 | |
| 140 | char *nfs_get_error(struct nfs_context *nfs) |
| 141 | { |
| 142 | return rpc_get_error(nfs->rpc); |
| 143 | }; |
| 144 | |
| 145 | struct nfs_context *nfs_init_context(void) |
| 146 | { |
| 147 | struct nfs_context *nfs; |
| 148 | |
| 149 | nfs = malloc(sizeof(struct nfs_context)); |
| 150 | if (nfs == NULL) { |
| 151 | return NULL; |
| 152 | } |
| 153 | nfs->rpc = rpc_init_context(); |
| 154 | if (nfs->rpc == NULL) { |
| 155 | free(nfs); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | nfs->server = NULL; |
| 160 | nfs->export = NULL; |
| 161 | |
| 162 | nfs->rootfh.data.data_len = 0; |
| 163 | nfs->rootfh.data.data_val = NULL; |
| 164 | |
| 165 | return nfs; |
| 166 | } |
| 167 | |
| 168 | void nfs_destroy_context(struct nfs_context *nfs) |
| 169 | { |
| 170 | rpc_destroy_context(nfs->rpc); |
| 171 | nfs->rpc = NULL; |
| 172 | |
| 173 | if (nfs->server) { |
| 174 | free(nfs->server); |
| 175 | nfs->server = NULL; |
| 176 | } |
| 177 | |
| 178 | if (nfs->export) { |
| 179 | free(nfs->export); |
| 180 | nfs->export = NULL; |
| 181 | } |
| 182 | |
| 183 | if (nfs->rootfh.data.data_val != NULL) { |
| 184 | free(nfs->rootfh.data.data_val); |
| 185 | nfs->rootfh.data.data_val = NULL; |
| 186 | } |
| 187 | |
| 188 | free(nfs); |
| 189 | } |
| 190 | |
| 191 | void free_nfs_cb_data(struct nfs_cb_data *data) |
| 192 | { |
| 193 | if (data->saved_path != NULL) { |
| 194 | free(data->saved_path); |
| 195 | data->saved_path = NULL; |
| 196 | } |
| 197 | |
| 198 | if (data->continue_data != NULL) { |
| 199 | data->free_continue_data(data->continue_data); |
| 200 | data->continue_data = NULL; |
| 201 | } |
| 202 | |
| 203 | if (data->fh.data.data_val != NULL) { |
| 204 | free(data->fh.data.data_val); |
| 205 | data->fh.data.data_val = NULL; |
| 206 | } |
| 207 | |
| 208 | if (data->buffer != NULL) { |
| 209 | free(data->buffer); |
| 210 | data->buffer = NULL; |
| 211 | } |
| 212 | |
| 213 | free(data); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | static void nfs_mount_10_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 221 | { |
| 222 | struct nfs_cb_data *data = private_data; |
| 223 | struct nfs_context *nfs = data->nfs; |
| 224 | |
| 225 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 226 | |
| 227 | if (status == RPC_STATUS_ERROR) { |
| 228 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 229 | free_nfs_cb_data(data); |
| 230 | return; |
| 231 | } |
| 232 | if (status == RPC_STATUS_CANCEL) { |
| 233 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 234 | free_nfs_cb_data(data); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | data->cb(0, nfs, NULL, data->private_data); |
| 239 | free_nfs_cb_data(data); |
| 240 | } |
| 241 | |
| 242 | static void nfs_mount_9_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 243 | { |
| 244 | struct nfs_cb_data *data = private_data; |
| 245 | struct nfs_context *nfs = data->nfs; |
| 246 | FSINFO3res *res = command_data; |
| 247 | |
| 248 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 249 | |
| 250 | if (status == RPC_STATUS_ERROR) { |
| 251 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 252 | free_nfs_cb_data(data); |
| 253 | return; |
| 254 | } |
| 255 | if (status == RPC_STATUS_CANCEL) { |
| 256 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 257 | free_nfs_cb_data(data); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | nfs->readmax = res->FSINFO3res_u.resok.rtmax; |
| 262 | nfs->writemax = res->FSINFO3res_u.resok.wtmax; |
| 263 | |
| 264 | if (rpc_nfs_getattr_async(rpc, nfs_mount_10_cb, &nfs->rootfh, data) != 0) { |
| 265 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 266 | free_nfs_cb_data(data); |
| 267 | return; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static void nfs_mount_8_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 272 | { |
| 273 | struct nfs_cb_data *data = private_data; |
| 274 | struct nfs_context *nfs = data->nfs; |
| 275 | |
| 276 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 277 | |
| 278 | if (status == RPC_STATUS_ERROR) { |
| 279 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 280 | free_nfs_cb_data(data); |
| 281 | return; |
| 282 | } |
| 283 | if (status == RPC_STATUS_CANCEL) { |
| 284 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 285 | free_nfs_cb_data(data); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | if (rpc_nfs_fsinfo_async(rpc, nfs_mount_9_cb, &nfs->rootfh, data) != 0) { |
| 290 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 291 | free_nfs_cb_data(data); |
| 292 | return; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static void nfs_mount_7_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 298 | { |
| 299 | struct nfs_cb_data *data = private_data; |
| 300 | struct nfs_context *nfs = data->nfs; |
| 301 | |
| 302 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 303 | |
| 304 | /* Dont want any more callbacks even if the socket is closed */ |
| 305 | rpc->connect_cb = NULL; |
| 306 | |
| 307 | if (status == RPC_STATUS_ERROR) { |
| 308 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 309 | free_nfs_cb_data(data); |
| 310 | return; |
| 311 | } |
| 312 | if (status == RPC_STATUS_CANCEL) { |
| 313 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 314 | free_nfs_cb_data(data); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | if (rpc_nfs_null_async(rpc, nfs_mount_8_cb, data) != 0) { |
| 319 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 320 | free_nfs_cb_data(data); |
| 321 | return; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | static void nfs_mount_6_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 327 | { |
| 328 | struct nfs_cb_data *data = private_data; |
| 329 | struct nfs_context *nfs = data->nfs; |
| 330 | mountres3 *res; |
| 331 | |
| 332 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 333 | |
| 334 | if (status == RPC_STATUS_ERROR) { |
| 335 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 336 | free_nfs_cb_data(data); |
| 337 | return; |
| 338 | } |
| 339 | if (status == RPC_STATUS_CANCEL) { |
| 340 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 341 | free_nfs_cb_data(data); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | res = command_data; |
| 346 | if (res->fhs_status != MNT3_OK) { |
| 347 | rpc_set_error(rpc, "RPC error: Mount failed with error %s(%d) %s(%d)", mountstat3_to_str(res->fhs_status), res->fhs_status, strerror(-mountstat3_to_errno(res->fhs_status)), -mountstat3_to_errno(res->fhs_status)); |
| 348 | data->cb(mountstat3_to_errno(res->fhs_status), nfs, rpc_get_error(rpc), data->private_data); |
| 349 | free_nfs_cb_data(data); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | nfs->rootfh.data.data_len = res->mountres3_u.mountinfo.fhandle.fhandle3_len; |
| 354 | nfs->rootfh.data.data_val = malloc(nfs->rootfh.data.data_len); |
| 355 | if (nfs->rootfh.data.data_val == NULL) { |
| 356 | rpc_set_error(rpc, "Out of memory. Could not allocate memory to store root filehandle"); |
| 357 | data->cb(-ENOMEM, nfs, rpc_get_error(rpc), data->private_data); |
| 358 | free_nfs_cb_data(data); |
| 359 | return; |
| 360 | } |
| 361 | memcpy(nfs->rootfh.data.data_val, res->mountres3_u.mountinfo.fhandle.fhandle3_val, nfs->rootfh.data.data_len); |
| 362 | |
| 363 | rpc_disconnect(rpc, "normal disconnect"); |
| 364 | if (rpc_connect_async(rpc, nfs->server, 2049, nfs_mount_7_cb, data) != 0) { |
| 365 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 366 | free_nfs_cb_data(data); |
| 367 | return; |
| 368 | } |
| 369 | /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */ |
| 370 | rpc_set_autoreconnect(rpc); |
| 371 | } |
| 372 | |
| 373 | |
| 374 | static void nfs_mount_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 375 | { |
| 376 | struct nfs_cb_data *data = private_data; |
| 377 | struct nfs_context *nfs = data->nfs; |
| 378 | |
| 379 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 380 | |
| 381 | if (status == RPC_STATUS_ERROR) { |
| 382 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 383 | free_nfs_cb_data(data); |
| 384 | return; |
| 385 | } |
| 386 | if (status == RPC_STATUS_CANCEL) { |
| 387 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 388 | free_nfs_cb_data(data); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | if (rpc_mount_mnt_async(rpc, nfs_mount_6_cb, nfs->export, data) != 0) { |
| 393 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 394 | free_nfs_cb_data(data); |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static void nfs_mount_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 400 | { |
| 401 | struct nfs_cb_data *data = private_data; |
| 402 | struct nfs_context *nfs = data->nfs; |
| 403 | |
| 404 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 405 | |
| 406 | /* Dont want any more callbacks even if the socket is closed */ |
| 407 | rpc->connect_cb = NULL; |
| 408 | |
| 409 | if (status == RPC_STATUS_ERROR) { |
| 410 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 411 | free_nfs_cb_data(data); |
| 412 | return; |
| 413 | } |
| 414 | if (status == RPC_STATUS_CANCEL) { |
| 415 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 416 | free_nfs_cb_data(data); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | if (rpc_mount_null_async(rpc, nfs_mount_5_cb, data) != 0) { |
| 421 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 422 | free_nfs_cb_data(data); |
| 423 | return; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static void nfs_mount_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 428 | { |
| 429 | struct nfs_cb_data *data = private_data; |
| 430 | struct nfs_context *nfs = data->nfs; |
| 431 | uint32_t mount_port; |
| 432 | |
| 433 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 434 | |
| 435 | if (status == RPC_STATUS_ERROR) { |
| 436 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 437 | free_nfs_cb_data(data); |
| 438 | return; |
| 439 | } |
| 440 | if (status == RPC_STATUS_CANCEL) { |
| 441 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 442 | free_nfs_cb_data(data); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | mount_port = *(uint32_t *)command_data; |
| 447 | if (mount_port == 0) { |
| 448 | rpc_set_error(rpc, "RPC error. Mount program is not available on %s", nfs->server); |
| 449 | data->cb(-ENOENT, nfs, command_data, data->private_data); |
| 450 | free_nfs_cb_data(data); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | rpc_disconnect(rpc, "normal disconnect"); |
| 455 | if (rpc_connect_async(rpc, nfs->server, mount_port, nfs_mount_4_cb, data) != 0) { |
| 456 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 457 | free_nfs_cb_data(data); |
| 458 | return; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | static void nfs_mount_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 464 | { |
| 465 | struct nfs_cb_data *data = private_data; |
| 466 | struct nfs_context *nfs = data->nfs; |
| 467 | |
| 468 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 469 | |
| 470 | if (status == RPC_STATUS_ERROR) { |
| 471 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 472 | free_nfs_cb_data(data); |
| 473 | return; |
| 474 | } |
| 475 | if (status == RPC_STATUS_CANCEL) { |
| 476 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 477 | free_nfs_cb_data(data); |
| 478 | return; |
| 479 | } |
| 480 | |
| 481 | if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, nfs_mount_3_cb, private_data) != 0) { |
| 482 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 483 | free_nfs_cb_data(data); |
| 484 | return; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | static void nfs_mount_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 489 | { |
| 490 | struct nfs_cb_data *data = private_data; |
| 491 | struct nfs_context *nfs = data->nfs; |
| 492 | |
| 493 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 494 | |
| 495 | /* Dont want any more callbacks even if the socket is closed */ |
| 496 | rpc->connect_cb = NULL; |
| 497 | |
| 498 | if (status == RPC_STATUS_ERROR) { |
| 499 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 500 | free_nfs_cb_data(data); |
| 501 | return; |
| 502 | } |
| 503 | if (status == RPC_STATUS_CANCEL) { |
| 504 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 505 | free_nfs_cb_data(data); |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | if (rpc_pmap_null_async(rpc, nfs_mount_2_cb, data) != 0) { |
| 510 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 511 | free_nfs_cb_data(data); |
| 512 | return; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Async call for mounting an nfs share and geting the root filehandle |
| 518 | */ |
| 519 | int nfs_mount_async(struct nfs_context *nfs, const char *server, const char *export, nfs_cb cb, void *private_data) |
| 520 | { |
| 521 | struct nfs_cb_data *data; |
| 522 | char *new_server, *new_export; |
| 523 | |
| 524 | data = malloc(sizeof(struct nfs_cb_data)); |
| 525 | if (data == NULL) { |
| 526 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data"); |
| 527 | return -1; |
| 528 | } |
| 529 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 530 | new_server = strdup(server); |
| 531 | new_export = strdup(export); |
| 532 | if (nfs->server != NULL) { |
| 533 | free(nfs->server); |
| 534 | } |
| 535 | nfs->server = new_server; |
| 536 | if (nfs->export != NULL) { |
| 537 | free(nfs->export); |
| 538 | } |
| 539 | nfs->export = new_export; |
| 540 | data->nfs = nfs; |
| 541 | data->cb = cb; |
| 542 | data->private_data = private_data; |
| 543 | |
| 544 | if (rpc_connect_async(nfs->rpc, server, 111, nfs_mount_1_cb, data) != 0) { |
| 545 | rpc_set_error(nfs->rpc, "Failed to start connection"); |
| 546 | free_nfs_cb_data(data); |
| 547 | return -1; |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | |
| 554 | |
| 555 | /* |
| 556 | * Functions to first look up a path, component by component, and then finally call a specific function once |
| 557 | * the filehandle for the final component is found. |
| 558 | */ |
| 559 | static void nfs_lookup_path_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 560 | { |
| 561 | struct nfs_cb_data *data = private_data; |
| 562 | struct nfs_context *nfs = data->nfs; |
| 563 | LOOKUP3res *res; |
| 564 | |
| 565 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 566 | |
| 567 | if (status == RPC_STATUS_ERROR) { |
| 568 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 569 | free_nfs_cb_data(data); |
| 570 | return; |
| 571 | } |
| 572 | if (status == RPC_STATUS_CANCEL) { |
| 573 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 574 | free_nfs_cb_data(data); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | res = command_data; |
| 579 | if (res->status != NFS3_OK) { |
| 580 | rpc_set_error(nfs->rpc, "NFS: Lookup of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 581 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 582 | free_nfs_cb_data(data); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | if (nfs_lookup_path_async_internal(nfs, data, &res->LOOKUP3res_u.resok.object) != 0) { |
| 587 | rpc_set_error(nfs->rpc, "Failed to create lookup pdu"); |
| 588 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 589 | free_nfs_cb_data(data); |
| 590 | return; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh) |
| 595 | { |
| 596 | char *path, *str; |
| 597 | |
| 598 | while (*data->path == '/') { |
| 599 | data->path++; |
| 600 | } |
| 601 | |
| 602 | path = data->path; |
| 603 | str = strchr(path, '/'); |
| 604 | if (str != NULL) { |
| 605 | *str = 0; |
| 606 | data->path = str+1; |
| 607 | } else { |
| 608 | while (*data->path != 0) { |
| 609 | data->path++; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (*path == 0) { |
| 614 | data->fh.data.data_len = fh->data.data_len; |
| 615 | data->fh.data.data_val = malloc(data->fh.data.data_len); |
| 616 | if (data->fh.data.data_val == NULL) { |
| 617 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh for %s", data->path); |
| 618 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 619 | free_nfs_cb_data(data); |
| 620 | return -1; |
| 621 | } |
| 622 | memcpy(data->fh.data.data_val, fh->data.data_val, data->fh.data.data_len); |
| 623 | data->continue_cb(nfs, data); |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | if (rpc_nfs_lookup_async(nfs->rpc, nfs_lookup_path_1_cb, fh, path, data) != 0) { |
| 628 | rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s", data->path); |
| 629 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 630 | free_nfs_cb_data(data); |
| 631 | return -1; |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static int nfs_lookuppath_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data, continue_func continue_cb, void *continue_data, void (*free_continue_data)(void *), int continue_int) |
| 637 | { |
| 638 | struct nfs_cb_data *data; |
| 639 | |
| 640 | if (path[0] != '/') { |
| 641 | rpc_set_error(nfs->rpc, "Pathname is not absulute %s", path); |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | data = malloc(sizeof(struct nfs_cb_data)); |
| 646 | if (data == NULL) { |
| 647 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 648 | return -1; |
| 649 | } |
| 650 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 651 | data->nfs = nfs; |
| 652 | data->cb = cb; |
| 653 | data->continue_cb = continue_cb; |
| 654 | data->continue_data = continue_data; |
| 655 | data->free_continue_data = free_continue_data; |
| 656 | data->continue_int = continue_int; |
| 657 | data->private_data = private_data; |
| 658 | data->saved_path = strdup(path); |
| 659 | if (data->saved_path == NULL) { |
| 660 | rpc_set_error(nfs->rpc, "out of memory: failed to copy path string"); |
| 661 | free_nfs_cb_data(data); |
| 662 | return -1; |
| 663 | } |
| 664 | data->path = data->saved_path; |
| 665 | |
| 666 | if (nfs_lookup_path_async_internal(nfs, data, &nfs->rootfh) != 0) { |
| 667 | /* return 0 here since the callback will be invoked if there is a failure */ |
| 668 | return 0; |
| 669 | } |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | /* |
| 678 | * Async stat() |
| 679 | */ |
| 680 | static void nfs_stat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 681 | { |
| 682 | GETATTR3res *res; |
| 683 | struct nfs_cb_data *data = private_data; |
| 684 | struct nfs_context *nfs = data->nfs; |
| 685 | struct stat st; |
| 686 | |
| 687 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 688 | |
| 689 | if (status == RPC_STATUS_ERROR) { |
| 690 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 691 | free_nfs_cb_data(data); |
| 692 | return; |
| 693 | } |
| 694 | if (status == RPC_STATUS_CANCEL) { |
| 695 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 696 | free_nfs_cb_data(data); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | res = command_data; |
| 701 | if (res->status != NFS3_OK) { |
| 702 | rpc_set_error(nfs->rpc, "NFS: GETATTR of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 703 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 704 | free_nfs_cb_data(data); |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | st.st_dev = -1; |
| 709 | st.st_ino = res->GETATTR3res_u.resok.obj_attributes.fileid; |
| 710 | st.st_mode = res->GETATTR3res_u.resok.obj_attributes.mode; |
| 711 | if (res->GETATTR3res_u.resok.obj_attributes.type == NF3DIR) { |
| 712 | st.st_mode |= S_IFDIR ; |
| 713 | } |
| 714 | if (res->GETATTR3res_u.resok.obj_attributes.type == NF3REG) { |
| 715 | st.st_mode |= S_IFREG ; |
| 716 | } |
| 717 | st.st_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink; |
| 718 | st.st_uid = res->GETATTR3res_u.resok.obj_attributes.uid; |
| 719 | st.st_gid = res->GETATTR3res_u.resok.obj_attributes.gid; |
| 720 | st.st_rdev = 0; |
| 721 | st.st_size = res->GETATTR3res_u.resok.obj_attributes.size; |
| 722 | #ifndef WIN32 |
| 723 | st.st_blksize = 4096; |
| 724 | st.st_blocks = res->GETATTR3res_u.resok.obj_attributes.size / 4096; |
| 725 | #endif//WIN32 |
| 726 | st.st_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds; |
| 727 | st.st_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds; |
| 728 | st.st_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds; |
| 729 | |
| 730 | data->cb(0, nfs, &st, data->private_data); |
| 731 | free_nfs_cb_data(data); |
| 732 | } |
| 733 | |
| 734 | static int nfs_stat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 735 | { |
| 736 | if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &data->fh, data) != 0) { |
| 737 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); |
| 738 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 739 | free_nfs_cb_data(data); |
| 740 | return -1; |
| 741 | } |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 746 | { |
| 747 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_stat_continue_internal, NULL, NULL, 0) != 0) { |
| 748 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 749 | return -1; |
| 750 | } |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | /* |
| 760 | * Async open() |
| 761 | */ |
| 762 | static void nfs_open_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 763 | { |
| 764 | ACCESS3res *res; |
| 765 | struct nfs_cb_data *data = private_data; |
| 766 | struct nfs_context *nfs = data->nfs; |
| 767 | struct nfsfh *nfsfh; |
| 768 | unsigned int nfsmode = 0; |
| 769 | |
| 770 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 771 | |
| 772 | if (status == RPC_STATUS_ERROR) { |
| 773 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 774 | free_nfs_cb_data(data); |
| 775 | return; |
| 776 | } |
| 777 | if (status == RPC_STATUS_CANCEL) { |
| 778 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 779 | free_nfs_cb_data(data); |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | res = command_data; |
| 784 | if (res->status != NFS3_OK) { |
| 785 | rpc_set_error(nfs->rpc, "NFS: ACCESS of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 786 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 787 | free_nfs_cb_data(data); |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | if (data->continue_int & O_WRONLY) { |
| 792 | nfsmode |= ACCESS3_MODIFY; |
| 793 | } |
| 794 | if (data->continue_int & O_RDWR) { |
| 795 | nfsmode |= ACCESS3_READ|ACCESS3_MODIFY; |
| 796 | } |
| 797 | if (!(data->continue_int & (O_WRONLY|O_RDWR))) { |
| 798 | nfsmode |= ACCESS3_READ; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | if (res->ACCESS3res_u.resok.access != nfsmode) { |
| 803 | rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c", |
| 804 | nfsmode&ACCESS3_READ?'r':'-', |
| 805 | nfsmode&ACCESS3_MODIFY?'w':'-', |
| 806 | nfsmode&ACCESS3_EXECUTE?'x':'-', |
| 807 | res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-', |
| 808 | res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-', |
| 809 | res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-'); |
| 810 | data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 811 | free_nfs_cb_data(data); |
| 812 | return; |
| 813 | } |
| 814 | |
| 815 | nfsfh = malloc(sizeof(struct nfsfh)); |
| 816 | if (nfsfh == NULL) { |
| 817 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); |
| 818 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 819 | free_nfs_cb_data(data); |
| 820 | return; |
| 821 | } |
| 822 | memset(nfsfh, 0, sizeof(struct nfsfh)); |
| 823 | |
| 824 | if (data->continue_int & O_SYNC) { |
| 825 | nfsfh->is_sync = 1; |
| 826 | } |
| 827 | |
| 828 | /* steal the filehandle */ |
| 829 | nfsfh->fh.data.data_len = data->fh.data.data_len; |
| 830 | nfsfh->fh.data.data_val = data->fh.data.data_val; |
| 831 | data->fh.data.data_val = NULL; |
| 832 | |
| 833 | data->cb(0, nfs, nfsfh, data->private_data); |
| 834 | free_nfs_cb_data(data); |
| 835 | } |
| 836 | |
| 837 | static int nfs_open_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 838 | { |
| 839 | int nfsmode = 0; |
| 840 | |
| 841 | if (data->continue_int & O_WRONLY) { |
| 842 | nfsmode |= ACCESS3_MODIFY; |
| 843 | } |
| 844 | if (data->continue_int & O_RDWR) { |
| 845 | nfsmode |= ACCESS3_READ|ACCESS3_MODIFY; |
| 846 | } |
| 847 | if (!(data->continue_int & (O_WRONLY|O_RDWR))) { |
| 848 | nfsmode |= ACCESS3_READ; |
| 849 | } |
| 850 | |
| 851 | if (rpc_nfs_access_async(nfs->rpc, nfs_open_cb, &data->fh, nfsmode, data) != 0) { |
| 852 | rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path); |
| 853 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 854 | free_nfs_cb_data(data); |
| 855 | return -1; |
| 856 | } |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | int nfs_open_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) |
| 861 | { |
| 862 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_open_continue_internal, NULL, NULL, mode) != 0) { |
| 863 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | /* |
| 875 | * Async pread() |
| 876 | */ |
| 877 | static void nfs_pread_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 878 | { |
| 879 | struct nfs_cb_data *data = private_data; |
| 880 | struct nfs_context *nfs = data->nfs; |
| 881 | READ3res *res; |
| 882 | |
| 883 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 884 | |
| 885 | if (status == RPC_STATUS_ERROR) { |
| 886 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 887 | free_nfs_cb_data(data); |
| 888 | return; |
| 889 | } |
| 890 | if (status == RPC_STATUS_CANCEL) { |
| 891 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 892 | free_nfs_cb_data(data); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | res = command_data; |
| 897 | if (res->status != NFS3_OK) { |
| 898 | rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 899 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 900 | free_nfs_cb_data(data); |
| 901 | return; |
| 902 | } |
| 903 | |
| 904 | data->nfsfh->offset += res->READ3res_u.resok.count; |
| 905 | data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_data); |
| 906 | free_nfs_cb_data(data); |
| 907 | } |
| 908 | |
| 909 | static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 910 | { |
| 911 | struct nfs_mcb_data *mdata = private_data; |
| 912 | struct nfs_cb_data *data = mdata->data; |
| 913 | struct nfs_context *nfs = data->nfs; |
| 914 | READ3res *res; |
| 915 | |
| 916 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 917 | |
| 918 | data->num_calls--; |
| 919 | |
| 920 | if (status == RPC_STATUS_ERROR) { |
| 921 | /* flag the failure but do not invoke callback until we have received all responses */ |
| 922 | data->error = 1; |
| 923 | } |
| 924 | if (status == RPC_STATUS_CANCEL) { |
| 925 | /* flag the cancellation but do not invoke callback until we have received all responses */ |
| 926 | data->cancel = 1; |
| 927 | } |
| 928 | |
| 929 | /* reassemble the data into the buffer */ |
| 930 | if (status == RPC_STATUS_SUCCESS) { |
| 931 | res = command_data; |
| 932 | if (res->status != NFS3_OK) { |
| 933 | rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 934 | data->error = 1; |
| 935 | } else { |
| 936 | if (res->READ3res_u.resok.count > 0) { |
| 937 | memcpy(&data->buffer[mdata->offset - data->start_offset], res->READ3res_u.resok.data.data_val, res->READ3res_u.resok.count); |
| 938 | if ((unsigned)data->max_offset < mdata->offset + res->READ3res_u.resok.count) { |
| 939 | data->max_offset = mdata->offset + res->READ3res_u.resok.count; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (data->num_calls > 0) { |
| 946 | /* still waiting for more replies */ |
| 947 | free(mdata); |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | if (data->error != 0) { |
| 952 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 953 | free_nfs_cb_data(data); |
| 954 | free(mdata); |
| 955 | return; |
| 956 | } |
| 957 | if (data->cancel != 0) { |
| 958 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 959 | free_nfs_cb_data(data); |
| 960 | free(mdata); |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | data->nfsfh->offset = data->max_offset; |
| 965 | data->cb(data->max_offset - data->start_offset, nfs, data->buffer, data->private_data); |
| 966 | |
| 967 | free_nfs_cb_data(data); |
| 968 | free(mdata); |
| 969 | } |
| 970 | |
| 971 | int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, nfs_cb cb, void *private_data) |
| 972 | { |
| 973 | struct nfs_cb_data *data; |
| 974 | |
| 975 | data = malloc(sizeof(struct nfs_cb_data)); |
| 976 | if (data == NULL) { |
| 977 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 978 | return -1; |
| 979 | } |
| 980 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 981 | data->nfs = nfs; |
| 982 | data->cb = cb; |
| 983 | data->private_data = private_data; |
| 984 | data->nfsfh = nfsfh; |
| 985 | |
| 986 | nfsfh->offset = offset; |
| 987 | |
| 988 | if (count <= nfs_get_readmax(nfs)) { |
| 989 | if (rpc_nfs_read_async(nfs->rpc, nfs_pread_cb, &nfsfh->fh, offset, count, data) != 0) { |
| 990 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path); |
| 991 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 992 | free_nfs_cb_data(data); |
| 993 | return -1; |
| 994 | } |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | /* trying to read more than maximum server read size, we has to chop it up into smaller |
| 999 | * reads and collect into a reassembly buffer. |
| 1000 | * we send all reads in parallell so that performance is still good. |
| 1001 | */ |
| 1002 | data->max_offset = offset; |
| 1003 | data->start_offset = offset; |
| 1004 | |
| 1005 | data->buffer = malloc(count); |
| 1006 | if (data->buffer == NULL) { |
| 1007 | rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count); |
| 1008 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1009 | free_nfs_cb_data(data); |
| 1010 | return -1; |
| 1011 | } |
| 1012 | |
| 1013 | while (count > 0) { |
| 1014 | uint64_t readcount = count; |
| 1015 | struct nfs_mcb_data *mdata; |
| 1016 | |
| 1017 | if (readcount > nfs_get_readmax(nfs)) { |
| 1018 | readcount = nfs_get_readmax(nfs); |
| 1019 | } |
| 1020 | |
| 1021 | mdata = malloc(sizeof(struct nfs_mcb_data)); |
| 1022 | if (mdata == NULL) { |
| 1023 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure"); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | memset(mdata, 0, sizeof(struct nfs_mcb_data)); |
| 1027 | mdata->data = data; |
| 1028 | mdata->offset = offset; |
| 1029 | mdata->count = readcount; |
| 1030 | if (rpc_nfs_read_async(nfs->rpc, nfs_pread_mcb, &nfsfh->fh, offset, readcount, mdata) != 0) { |
| 1031 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path); |
| 1032 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1033 | free(mdata); |
| 1034 | return -1; |
| 1035 | } |
| 1036 | |
| 1037 | count -= readcount; |
| 1038 | offset += readcount; |
| 1039 | data->num_calls++; |
| 1040 | } |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * Async read() |
| 1047 | */ |
| 1048 | int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, nfs_cb cb, void *private_data) |
| 1049 | { |
| 1050 | return nfs_pread_async(nfs, nfsfh, nfsfh->offset, count, cb, private_data); |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | /* |
| 1056 | * Async pwrite() |
| 1057 | */ |
| 1058 | static void nfs_pwrite_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1059 | { |
| 1060 | struct nfs_cb_data *data = private_data; |
| 1061 | struct nfs_context *nfs = data->nfs; |
| 1062 | WRITE3res *res; |
| 1063 | |
| 1064 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1065 | |
| 1066 | if (status == RPC_STATUS_ERROR) { |
| 1067 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1068 | free_nfs_cb_data(data); |
| 1069 | return; |
| 1070 | } |
| 1071 | if (status == RPC_STATUS_CANCEL) { |
| 1072 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1073 | free_nfs_cb_data(data); |
| 1074 | return; |
| 1075 | } |
| 1076 | |
| 1077 | res = command_data; |
| 1078 | if (res->status != NFS3_OK) { |
| 1079 | rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1080 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1081 | free_nfs_cb_data(data); |
| 1082 | return; |
| 1083 | } |
| 1084 | |
| 1085 | data->nfsfh->offset += res->WRITE3res_u.resok.count; |
| 1086 | data->cb(res->WRITE3res_u.resok.count, nfs, NULL, data->private_data); |
| 1087 | free_nfs_cb_data(data); |
| 1088 | } |
| 1089 | |
| 1090 | static void nfs_pwrite_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1091 | { |
| 1092 | struct nfs_mcb_data *mdata = private_data; |
| 1093 | struct nfs_cb_data *data = mdata->data; |
| 1094 | struct nfs_context *nfs = data->nfs; |
| 1095 | WRITE3res *res; |
| 1096 | |
| 1097 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1098 | |
| 1099 | data->num_calls--; |
| 1100 | |
| 1101 | if (status == RPC_STATUS_ERROR) { |
| 1102 | /* flag the failure but do not invoke callback until we have received all responses */ |
| 1103 | data->error = 1; |
| 1104 | } |
| 1105 | if (status == RPC_STATUS_CANCEL) { |
| 1106 | /* flag the cancellation but do not invoke callback until we have received all responses */ |
| 1107 | data->cancel = 1; |
| 1108 | } |
| 1109 | |
| 1110 | if (status == RPC_STATUS_SUCCESS) { |
| 1111 | res = command_data; |
| 1112 | if (res->status != NFS3_OK) { |
| 1113 | rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1114 | data->error = 1; |
| 1115 | } else { |
| 1116 | if (res->WRITE3res_u.resok.count > 0) { |
| 1117 | if ((unsigned)data->max_offset < mdata->offset + res->WRITE3res_u.resok.count) { |
| 1118 | data->max_offset = mdata->offset + res->WRITE3res_u.resok.count; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | if (data->num_calls > 0) { |
| 1125 | /* still waiting for more replies */ |
| 1126 | free(mdata); |
| 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | if (data->error != 0) { |
| 1131 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1132 | free_nfs_cb_data(data); |
| 1133 | free(mdata); |
| 1134 | return; |
| 1135 | } |
| 1136 | if (data->cancel != 0) { |
| 1137 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1138 | free_nfs_cb_data(data); |
| 1139 | free(mdata); |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | data->nfsfh->offset = data->max_offset; |
| 1144 | data->cb(data->max_offset - data->start_offset, nfs, NULL, data->private_data); |
| 1145 | |
| 1146 | free_nfs_cb_data(data); |
| 1147 | free(mdata); |
| 1148 | } |
| 1149 | |
| 1150 | |
| 1151 | int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, char *buf, nfs_cb cb, void *private_data) |
| 1152 | { |
| 1153 | struct nfs_cb_data *data; |
| 1154 | |
| 1155 | data = malloc(sizeof(struct nfs_cb_data)); |
| 1156 | if (data == NULL) { |
| 1157 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 1158 | return -1; |
| 1159 | } |
| 1160 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 1161 | data->nfs = nfs; |
| 1162 | data->cb = cb; |
| 1163 | data->private_data = private_data; |
| 1164 | data->nfsfh = nfsfh; |
| 1165 | |
| 1166 | nfsfh->offset = offset; |
| 1167 | |
| 1168 | if (count <= nfs_get_writemax(nfs)) { |
| 1169 | if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_cb, &nfsfh->fh, buf, offset, count, nfsfh->is_sync?FILE_SYNC:UNSTABLE, data) != 0) { |
| 1170 | rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path); |
| 1171 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1172 | free_nfs_cb_data(data); |
| 1173 | return -1; |
| 1174 | } |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | /* trying to write more than maximum server write size, we has to chop it up into smaller |
| 1179 | * chunks. |
| 1180 | * we send all writes in parallell so that performance is still good. |
| 1181 | */ |
| 1182 | data->max_offset = offset; |
| 1183 | data->start_offset = offset; |
| 1184 | |
| 1185 | while (count > 0) { |
| 1186 | uint64_t writecount = count; |
| 1187 | struct nfs_mcb_data *mdata; |
| 1188 | |
| 1189 | if (writecount > nfs_get_writemax(nfs)) { |
| 1190 | writecount = nfs_get_writemax(nfs); |
| 1191 | } |
| 1192 | |
| 1193 | mdata = malloc(sizeof(struct nfs_mcb_data)); |
| 1194 | if (mdata == NULL) { |
| 1195 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure"); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | memset(mdata, 0, sizeof(struct nfs_mcb_data)); |
| 1199 | mdata->data = data; |
| 1200 | mdata->offset = offset; |
| 1201 | mdata->count = writecount; |
| 1202 | |
| 1203 | if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_mcb, &nfsfh->fh, &buf[offset - data->start_offset], offset, writecount, nfsfh->is_sync?FILE_SYNC:UNSTABLE, mdata) != 0) { |
| 1204 | rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path); |
| 1205 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1206 | free(mdata); |
| 1207 | return -1; |
| 1208 | } |
| 1209 | |
| 1210 | count -= writecount; |
| 1211 | offset += writecount; |
| 1212 | data->num_calls++; |
| 1213 | } |
| 1214 | |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Async write() |
| 1220 | */ |
| 1221 | int nfs_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, char *buf, nfs_cb cb, void *private_data) |
| 1222 | { |
| 1223 | return nfs_pwrite_async(nfs, nfsfh, nfsfh->offset, count, buf, cb, private_data); |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | /* |
| 1230 | * close |
| 1231 | */ |
| 1232 | |
| 1233 | int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) |
| 1234 | { |
| 1235 | if (nfsfh->fh.data.data_val != NULL){ |
| 1236 | free(nfsfh->fh.data.data_val); |
| 1237 | nfsfh->fh.data.data_val = NULL; |
| 1238 | } |
| 1239 | free(nfsfh); |
| 1240 | |
| 1241 | cb(0, nfs, NULL, private_data); |
| 1242 | return 0; |
| 1243 | }; |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | /* |
| 1250 | * Async fstat() |
| 1251 | */ |
| 1252 | int nfs_fstat_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) |
| 1253 | { |
| 1254 | struct nfs_cb_data *data; |
| 1255 | |
| 1256 | data = malloc(sizeof(struct nfs_cb_data)); |
| 1257 | if (data == NULL) { |
| 1258 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 1259 | return -1; |
| 1260 | } |
| 1261 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 1262 | data->nfs = nfs; |
| 1263 | data->cb = cb; |
| 1264 | data->private_data = private_data; |
| 1265 | |
| 1266 | if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &nfsfh->fh, data) != 0) { |
| 1267 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); |
| 1268 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1269 | free_nfs_cb_data(data); |
| 1270 | return -1; |
| 1271 | } |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | /* |
| 1278 | * Async fsync() |
| 1279 | */ |
| 1280 | static void nfs_fsync_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1281 | { |
| 1282 | struct nfs_cb_data *data = private_data; |
| 1283 | struct nfs_context *nfs = data->nfs; |
| 1284 | COMMIT3res *res; |
| 1285 | |
| 1286 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1287 | |
| 1288 | if (status == RPC_STATUS_ERROR) { |
| 1289 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1290 | free_nfs_cb_data(data); |
| 1291 | return; |
| 1292 | } |
| 1293 | if (status == RPC_STATUS_CANCEL) { |
| 1294 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1295 | free_nfs_cb_data(data); |
| 1296 | return; |
| 1297 | } |
| 1298 | |
| 1299 | res = command_data; |
| 1300 | if (res->status != NFS3_OK) { |
| 1301 | rpc_set_error(nfs->rpc, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1302 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1303 | free_nfs_cb_data(data); |
| 1304 | return; |
| 1305 | } |
| 1306 | |
| 1307 | data->cb(0, nfs, NULL, data->private_data); |
| 1308 | free_nfs_cb_data(data); |
| 1309 | } |
| 1310 | |
| 1311 | int nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) |
| 1312 | { |
| 1313 | struct nfs_cb_data *data; |
| 1314 | |
| 1315 | data = malloc(sizeof(struct nfs_cb_data)); |
| 1316 | if (data == NULL) { |
| 1317 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 1318 | return -1; |
| 1319 | } |
| 1320 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 1321 | data->nfs = nfs; |
| 1322 | data->cb = cb; |
| 1323 | data->private_data = private_data; |
| 1324 | |
| 1325 | if (rpc_nfs_commit_async(nfs->rpc, nfs_fsync_cb, &nfsfh->fh, data) != 0) { |
| 1326 | rpc_set_error(nfs->rpc, "RPC error: Failed to send COMMIT call for %s", data->path); |
| 1327 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1328 | free_nfs_cb_data(data); |
| 1329 | return -1; |
| 1330 | } |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | /* |
| 1338 | * Async ftruncate() |
| 1339 | */ |
| 1340 | static void nfs_ftruncate_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1341 | { |
| 1342 | struct nfs_cb_data *data = private_data; |
| 1343 | struct nfs_context *nfs = data->nfs; |
| 1344 | SETATTR3res *res; |
| 1345 | |
| 1346 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1347 | |
| 1348 | if (status == RPC_STATUS_ERROR) { |
| 1349 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1350 | free_nfs_cb_data(data); |
| 1351 | return; |
| 1352 | } |
| 1353 | if (status == RPC_STATUS_CANCEL) { |
| 1354 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1355 | free_nfs_cb_data(data); |
| 1356 | return; |
| 1357 | } |
| 1358 | |
| 1359 | res = command_data; |
| 1360 | if (res->status != NFS3_OK) { |
| 1361 | rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1362 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1363 | free_nfs_cb_data(data); |
| 1364 | return; |
| 1365 | } |
| 1366 | |
| 1367 | data->cb(0, nfs, NULL, data->private_data); |
| 1368 | free_nfs_cb_data(data); |
| 1369 | } |
| 1370 | |
| 1371 | int nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t length, nfs_cb cb, void *private_data) |
| 1372 | { |
| 1373 | struct nfs_cb_data *data; |
| 1374 | SETATTR3args args; |
| 1375 | |
| 1376 | data = malloc(sizeof(struct nfs_cb_data)); |
| 1377 | if (data == NULL) { |
| 1378 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 1379 | return -1; |
| 1380 | } |
| 1381 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 1382 | data->nfs = nfs; |
| 1383 | data->cb = cb; |
| 1384 | data->private_data = private_data; |
| 1385 | |
| 1386 | memset(&args, 0, sizeof(SETATTR3args)); |
| 1387 | args.object.data.data_len = nfsfh->fh.data.data_len; |
| 1388 | args.object.data.data_val = nfsfh->fh.data.data_val; |
| 1389 | args.new_attributes.size.set_it = 1; |
| 1390 | args.new_attributes.size.set_size3_u.size = length; |
| 1391 | |
| 1392 | if (rpc_nfs_setattr_async(nfs->rpc, nfs_ftruncate_cb, &args, data) != 0) { |
| 1393 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 1394 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1395 | free_nfs_cb_data(data); |
| 1396 | return -1; |
| 1397 | } |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | /* |
| 1403 | * Async truncate() |
| 1404 | */ |
| 1405 | static int nfs_truncate_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 1406 | { |
| 1407 | uint64_t offset = data->continue_int; |
| 1408 | struct nfsfh nfsfh; |
| 1409 | |
| 1410 | nfsfh.fh.data.data_val = data->fh.data.data_val; |
| 1411 | nfsfh.fh.data.data_len = data->fh.data.data_len; |
| 1412 | |
| 1413 | if (nfs_ftruncate_async(nfs, &nfsfh, offset, data->cb, data->private_data) != 0) { |
| 1414 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 1415 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1416 | free_nfs_cb_data(data); |
| 1417 | return -1; |
| 1418 | } |
| 1419 | free_nfs_cb_data(data); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | int nfs_truncate_async(struct nfs_context *nfs, const char *path, uint64_t length, nfs_cb cb, void *private_data) |
| 1424 | { |
| 1425 | uint64_t offset; |
| 1426 | |
| 1427 | offset = length; |
| 1428 | |
| 1429 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_truncate_continue_internal, NULL, NULL, offset) != 0) { |
| 1430 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1431 | return -1; |
| 1432 | } |
| 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | |
| 1440 | /* |
| 1441 | * Async mkdir() |
| 1442 | */ |
| 1443 | static void nfs_mkdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1444 | { |
| 1445 | MKDIR3res *res; |
| 1446 | struct nfs_cb_data *data = private_data; |
| 1447 | struct nfs_context *nfs = data->nfs; |
| 1448 | char *str = data->continue_data; |
| 1449 | |
| 1450 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1451 | |
| 1452 | str = &str[strlen(str) + 1]; |
| 1453 | |
| 1454 | if (status == RPC_STATUS_ERROR) { |
| 1455 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1456 | free_nfs_cb_data(data); |
| 1457 | return; |
| 1458 | } |
| 1459 | if (status == RPC_STATUS_CANCEL) { |
| 1460 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1461 | free_nfs_cb_data(data); |
| 1462 | return; |
| 1463 | } |
| 1464 | |
| 1465 | res = command_data; |
| 1466 | if (res->status != NFS3_OK) { |
| 1467 | rpc_set_error(nfs->rpc, "NFS: MKDIR of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1468 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1469 | free_nfs_cb_data(data); |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | data->cb(0, nfs, NULL, data->private_data); |
| 1474 | free_nfs_cb_data(data); |
| 1475 | } |
| 1476 | |
| 1477 | static int nfs_mkdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 1478 | { |
| 1479 | char *str = data->continue_data; |
| 1480 | MKDIR3args args; |
| 1481 | |
| 1482 | str = &str[strlen(str) + 1]; |
| 1483 | |
| 1484 | memset(&args, 0, sizeof(MKDIR3args)); |
| 1485 | args.where.dir.data.data_len = data->fh.data.data_len; |
| 1486 | args.where.dir.data.data_val = data->fh.data.data_val; |
| 1487 | args.where.name = str; |
| 1488 | args.attributes.mode.set_it = 1; |
| 1489 | args.attributes.mode.set_mode3_u.mode = 0755; |
| 1490 | |
| 1491 | if (rpc_nfs_mkdir_async(nfs->rpc, nfs_mkdir_cb, &args, data) != 0) { |
| 1492 | rpc_set_error(nfs->rpc, "RPC error: Failed to send MKDIR call for %s", data->path); |
| 1493 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1494 | free_nfs_cb_data(data); |
| 1495 | return -1; |
| 1496 | } |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | int nfs_mkdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 1501 | { |
| 1502 | char *new_path; |
| 1503 | char *ptr; |
| 1504 | |
| 1505 | new_path = strdup(path); |
| 1506 | if (new_path == NULL) { |
| 1507 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 1508 | return -1; |
| 1509 | } |
| 1510 | |
| 1511 | ptr = strrchr(new_path, '/'); |
| 1512 | if (ptr == NULL) { |
| 1513 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 1514 | return -1; |
| 1515 | } |
| 1516 | *ptr = 0; |
| 1517 | |
| 1518 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 1519 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_mkdir_continue_internal, new_path, free, 0) != 0) { |
| 1520 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path component"); |
| 1521 | return -1; |
| 1522 | } |
| 1523 | |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | /* |
| 1532 | * Async rmdir() |
| 1533 | */ |
| 1534 | static void nfs_rmdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1535 | { |
| 1536 | RMDIR3res *res; |
| 1537 | struct nfs_cb_data *data = private_data; |
| 1538 | struct nfs_context *nfs = data->nfs; |
| 1539 | char *str = data->continue_data; |
| 1540 | |
| 1541 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1542 | |
| 1543 | str = &str[strlen(str) + 1]; |
| 1544 | |
| 1545 | if (status == RPC_STATUS_ERROR) { |
| 1546 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1547 | free_nfs_cb_data(data); |
| 1548 | return; |
| 1549 | } |
| 1550 | if (status == RPC_STATUS_CANCEL) { |
| 1551 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1552 | free_nfs_cb_data(data); |
| 1553 | return; |
| 1554 | } |
| 1555 | |
| 1556 | res = command_data; |
| 1557 | if (res->status != NFS3_OK) { |
| 1558 | rpc_set_error(nfs->rpc, "NFS: RMDIR of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1559 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1560 | free_nfs_cb_data(data); |
| 1561 | return; |
| 1562 | } |
| 1563 | |
| 1564 | data->cb(0, nfs, NULL, data->private_data); |
| 1565 | free_nfs_cb_data(data); |
| 1566 | } |
| 1567 | |
| 1568 | static int nfs_rmdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 1569 | { |
| 1570 | char *str = data->continue_data; |
| 1571 | |
| 1572 | str = &str[strlen(str) + 1]; |
| 1573 | |
| 1574 | if (rpc_nfs_rmdir_async(nfs->rpc, nfs_rmdir_cb, &data->fh, str, data) != 0) { |
| 1575 | rpc_set_error(nfs->rpc, "RPC error: Failed to send RMDIR call for %s", data->path); |
| 1576 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1577 | free_nfs_cb_data(data); |
| 1578 | return -1; |
| 1579 | } |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | int nfs_rmdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 1584 | { |
| 1585 | char *new_path; |
| 1586 | char *ptr; |
| 1587 | |
| 1588 | new_path = strdup(path); |
| 1589 | if (new_path == NULL) { |
| 1590 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 1591 | return -1; |
| 1592 | } |
| 1593 | |
| 1594 | ptr = strrchr(new_path, '/'); |
| 1595 | if (ptr == NULL) { |
| 1596 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 1597 | return -1; |
| 1598 | } |
| 1599 | *ptr = 0; |
| 1600 | |
| 1601 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 1602 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_rmdir_continue_internal, new_path, free, 0) != 0) { |
| 1603 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
| 1610 | |
| 1611 | |
| 1612 | |
| 1613 | /* |
| 1614 | * Async creat() |
| 1615 | */ |
| 1616 | static void nfs_create_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1617 | { |
| 1618 | LOOKUP3res *res; |
| 1619 | struct nfs_cb_data *data = private_data; |
| 1620 | struct nfs_context *nfs = data->nfs; |
| 1621 | struct nfsfh *nfsfh; |
| 1622 | char *str = data->continue_data; |
| 1623 | |
| 1624 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1625 | |
| 1626 | if (status == RPC_STATUS_ERROR) { |
| 1627 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1628 | free_nfs_cb_data(data); |
| 1629 | return; |
| 1630 | } |
| 1631 | if (status == RPC_STATUS_CANCEL) { |
| 1632 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1633 | free_nfs_cb_data(data); |
| 1634 | return; |
| 1635 | } |
| 1636 | |
| 1637 | str = &str[strlen(str) + 1]; |
| 1638 | res = command_data; |
| 1639 | if (res->status != NFS3_OK) { |
| 1640 | rpc_set_error(nfs->rpc, "NFS: CREATE of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1641 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1642 | |
| 1643 | return; |
| 1644 | } |
| 1645 | |
| 1646 | nfsfh = malloc(sizeof(struct nfsfh)); |
| 1647 | if (nfsfh == NULL) { |
| 1648 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); |
| 1649 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1650 | free_nfs_cb_data(data); |
| 1651 | return; |
| 1652 | } |
| 1653 | memset(nfsfh, 0, sizeof(struct nfsfh)); |
| 1654 | |
| 1655 | /* steal the filehandle */ |
| 1656 | nfsfh->fh.data.data_len = data->fh.data.data_len; |
| 1657 | nfsfh->fh.data.data_val = data->fh.data.data_val; |
| 1658 | data->fh.data.data_val = NULL; |
| 1659 | |
| 1660 | data->cb(0, nfs, nfsfh, data->private_data); |
| 1661 | free_nfs_cb_data(data); |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | |
| 1666 | static void nfs_creat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1667 | { |
| 1668 | CREATE3res *res; |
| 1669 | struct nfs_cb_data *data = private_data; |
| 1670 | struct nfs_context *nfs = data->nfs; |
| 1671 | char *str = data->continue_data; |
| 1672 | |
| 1673 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1674 | |
| 1675 | if (status == RPC_STATUS_ERROR) { |
| 1676 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1677 | free_nfs_cb_data(data); |
| 1678 | return; |
| 1679 | } |
| 1680 | if (status == RPC_STATUS_CANCEL) { |
| 1681 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1682 | free_nfs_cb_data(data); |
| 1683 | return; |
| 1684 | } |
| 1685 | |
| 1686 | str = &str[strlen(str) + 1]; |
| 1687 | res = command_data; |
| 1688 | if (res->status != NFS3_OK) { |
| 1689 | rpc_set_error(nfs->rpc, "NFS: CREATE of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1690 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1691 | |
| 1692 | return; |
| 1693 | } |
| 1694 | |
| 1695 | if (rpc_nfs_lookup_async(nfs->rpc, nfs_create_2_cb, &data->fh, str, data) != 0) { |
| 1696 | rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s/%s", data->saved_path, str); |
| 1697 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1698 | free_nfs_cb_data(data); |
| 1699 | return; |
| 1700 | } |
| 1701 | return; |
| 1702 | } |
| 1703 | |
| 1704 | static int nfs_creat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 1705 | { |
| 1706 | char *str = data->continue_data; |
| 1707 | CREATE3args args; |
| 1708 | |
| 1709 | str = &str[strlen(str) + 1]; |
| 1710 | |
| 1711 | |
| 1712 | memset(&args, 0, sizeof(CREATE3args)); |
| 1713 | args.where.dir.data.data_len = data->fh.data.data_len; |
| 1714 | args.where.dir.data.data_val = data->fh.data.data_val; |
| 1715 | args.where.name = str; |
| 1716 | args.how.mode = UNCHECKED; |
| 1717 | args.how.createhow3_u.obj_attributes.mode.set_it = 1; |
| 1718 | args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = data->continue_int; |
| 1719 | |
| 1720 | if (rpc_nfs_create_async(nfs->rpc, nfs_creat_1_cb, &args, data) != 0) { |
| 1721 | rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str); |
| 1722 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1723 | free_nfs_cb_data(data); |
| 1724 | return -1; |
| 1725 | } |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) |
| 1730 | { |
| 1731 | char *new_path; |
| 1732 | char *ptr; |
| 1733 | |
| 1734 | new_path = strdup(path); |
| 1735 | if (new_path == NULL) { |
| 1736 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 1737 | return -1; |
| 1738 | } |
| 1739 | |
| 1740 | ptr = strrchr(new_path, '/'); |
| 1741 | if (ptr == NULL) { |
| 1742 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 1743 | return -1; |
| 1744 | } |
| 1745 | *ptr = 0; |
| 1746 | |
| 1747 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 1748 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_creat_continue_internal, new_path, free, mode) != 0) { |
| 1749 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1750 | return -1; |
| 1751 | } |
| 1752 | |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | |
| 1757 | |
| 1758 | |
| 1759 | /* |
| 1760 | * Async unlink() |
| 1761 | */ |
| 1762 | static void nfs_unlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1763 | { |
| 1764 | REMOVE3res *res; |
| 1765 | struct nfs_cb_data *data = private_data; |
| 1766 | struct nfs_context *nfs = data->nfs; |
| 1767 | char *str = data->continue_data; |
| 1768 | |
| 1769 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1770 | |
| 1771 | str = &str[strlen(str) + 1]; |
| 1772 | |
| 1773 | if (status == RPC_STATUS_ERROR) { |
| 1774 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1775 | free_nfs_cb_data(data); |
| 1776 | return; |
| 1777 | } |
| 1778 | if (status == RPC_STATUS_CANCEL) { |
| 1779 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1780 | free_nfs_cb_data(data); |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | res = command_data; |
| 1785 | if (res->status != NFS3_OK) { |
| 1786 | rpc_set_error(nfs->rpc, "NFS: REMOVE of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1787 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1788 | free_nfs_cb_data(data); |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | data->cb(0, nfs, NULL, data->private_data); |
| 1793 | free_nfs_cb_data(data); |
| 1794 | } |
| 1795 | |
| 1796 | static int nfs_unlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 1797 | { |
| 1798 | char *str = data->continue_data; |
| 1799 | |
| 1800 | str = &str[strlen(str) + 1]; |
| 1801 | |
| 1802 | if (rpc_nfs_remove_async(nfs->rpc, nfs_unlink_cb, &data->fh, str, data) != 0) { |
| 1803 | rpc_set_error(nfs->rpc, "RPC error: Failed to send REMOVE call for %s", data->path); |
| 1804 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1805 | free_nfs_cb_data(data); |
| 1806 | return -1; |
| 1807 | } |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
| 1811 | int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 1812 | { |
| 1813 | char *new_path; |
| 1814 | char *ptr; |
| 1815 | |
| 1816 | new_path = strdup(path); |
| 1817 | if (new_path == NULL) { |
| 1818 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 1819 | return -1; |
| 1820 | } |
| 1821 | |
| 1822 | ptr = strrchr(new_path, '/'); |
| 1823 | if (ptr == NULL) { |
| 1824 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 1825 | return -1; |
| 1826 | } |
| 1827 | *ptr = 0; |
| 1828 | |
| 1829 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 1830 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_unlink_continue_internal, new_path, free, 0) != 0) { |
| 1831 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1832 | return -1; |
| 1833 | } |
| 1834 | |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | |
| 1839 | /* |
| 1840 | * Async mknod() |
| 1841 | */ |
| 1842 | struct mknod_cb_data { |
| 1843 | char *path; |
| 1844 | int mode; |
| 1845 | int major; |
| 1846 | int minor; |
| 1847 | }; |
| 1848 | |
| 1849 | static void free_mknod_cb_data(void *ptr) |
| 1850 | { |
| 1851 | struct mknod_cb_data *data = ptr; |
| 1852 | |
| 1853 | free(data->path); |
| 1854 | free(data); |
| 1855 | } |
| 1856 | |
| 1857 | static void nfs_mknod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1858 | { |
| 1859 | MKNOD3res *res; |
| 1860 | struct nfs_cb_data *data = private_data; |
| 1861 | struct nfs_context *nfs = data->nfs; |
| 1862 | char *str = data->continue_data; |
| 1863 | |
| 1864 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1865 | |
| 1866 | str = &str[strlen(str) + 1]; |
| 1867 | |
| 1868 | if (status == RPC_STATUS_ERROR) { |
| 1869 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1870 | free_nfs_cb_data(data); |
| 1871 | return; |
| 1872 | } |
| 1873 | if (status == RPC_STATUS_CANCEL) { |
| 1874 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1875 | free_nfs_cb_data(data); |
| 1876 | return; |
| 1877 | } |
| 1878 | |
| 1879 | res = command_data; |
| 1880 | if (res->status != NFS3_OK) { |
| 1881 | rpc_set_error(nfs->rpc, "NFS: MKNOD of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1882 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1883 | free_nfs_cb_data(data); |
| 1884 | return; |
| 1885 | } |
| 1886 | |
| 1887 | data->cb(0, nfs, NULL, data->private_data); |
| 1888 | free_nfs_cb_data(data); |
| 1889 | } |
| 1890 | |
| 1891 | static int nfs_mknod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 1892 | { |
| 1893 | struct mknod_cb_data *cb_data = data->continue_data; |
| 1894 | char *str = cb_data->path; |
| 1895 | |
| 1896 | str = &str[strlen(str) + 1]; |
| 1897 | |
| 1898 | if (rpc_nfs_mknod_async(nfs->rpc, nfs_mknod_cb, &data->fh, str, cb_data->mode, cb_data->major, cb_data->minor, data) != 0) { |
| 1899 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1900 | free_nfs_cb_data(data); |
| 1901 | return -1; |
| 1902 | } |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | int nfs_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev, nfs_cb cb, void *private_data) |
| 1907 | { |
| 1908 | char *ptr; |
| 1909 | struct mknod_cb_data *cb_data; |
| 1910 | |
| 1911 | cb_data = malloc(sizeof(struct mknod_cb_data)); |
| 1912 | if (cb_data == NULL) { |
| 1913 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for cb data"); |
| 1914 | return -1; |
| 1915 | } |
| 1916 | |
| 1917 | cb_data->path = strdup(path); |
| 1918 | if (cb_data->path == NULL) { |
| 1919 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 1920 | free(cb_data); |
| 1921 | return -1; |
| 1922 | } |
| 1923 | |
| 1924 | ptr = strrchr(cb_data->path, '/'); |
| 1925 | if (ptr == NULL) { |
| 1926 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 1927 | return -1; |
| 1928 | } |
| 1929 | *ptr = 0; |
| 1930 | |
| 1931 | cb_data->mode = mode; |
| 1932 | cb_data->major = major(dev); |
| 1933 | cb_data->minor = minor(dev); |
| 1934 | |
| 1935 | /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 1936 | if (nfs_lookuppath_async(nfs, cb_data->path, cb, private_data, nfs_mknod_continue_internal, cb_data, free_mknod_cb_data, 0) != 0) { |
| 1937 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1938 | free_mknod_cb_data(cb_data); |
| 1939 | return -1; |
| 1940 | } |
| 1941 | |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Async opendir() |
| 1947 | */ |
| 1948 | |
| 1949 | /* ReadDirPlus Emulation Callback data */ |
| 1950 | struct rdpe_cb_data { |
| 1951 | int getattrcount; |
| 1952 | int status; |
| 1953 | struct nfs_cb_data *data; |
| 1954 | }; |
| 1955 | |
| 1956 | /* ReadDirPlus Emulation LOOKUP Callback data */ |
| 1957 | struct rdpe_lookup_cb_data { |
| 1958 | struct rdpe_cb_data *rdpe_cb_data; |
| 1959 | struct nfsdirent *nfsdirent; |
| 1960 | }; |
| 1961 | |
| 1962 | /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */ |
| 1963 | static void nfs_opendir3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1964 | { |
| 1965 | LOOKUP3res *res = command_data; |
| 1966 | struct rdpe_lookup_cb_data *rdpe_lookup_cb_data = private_data; |
| 1967 | struct rdpe_cb_data *rdpe_cb_data = rdpe_lookup_cb_data->rdpe_cb_data; |
| 1968 | struct nfs_cb_data *data = rdpe_cb_data->data; |
| 1969 | struct nfsdir *nfsdir = data->continue_data; |
| 1970 | struct nfs_context *nfs = data->nfs; |
| 1971 | struct nfsdirent *nfsdirent = rdpe_lookup_cb_data->nfsdirent; |
| 1972 | |
| 1973 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1974 | |
| 1975 | free(rdpe_lookup_cb_data); |
| 1976 | |
| 1977 | rdpe_cb_data->getattrcount--; |
| 1978 | |
| 1979 | if (status == RPC_STATUS_ERROR) { |
| 1980 | rdpe_cb_data->status = RPC_STATUS_ERROR; |
| 1981 | } |
| 1982 | if (status == RPC_STATUS_CANCEL) { |
| 1983 | rdpe_cb_data->status = RPC_STATUS_CANCEL; |
| 1984 | } |
| 1985 | if (status == RPC_STATUS_SUCCESS && res->status != NFS3_OK) { |
| 1986 | rdpe_cb_data->status = RPC_STATUS_ERROR; |
| 1987 | } |
| 1988 | if (status == RPC_STATUS_SUCCESS && res->status == NFS3_OK) { |
| 1989 | if (res->LOOKUP3res_u.resok.obj_attributes.attributes_follow) { |
| 1990 | fattr3 *attributes = &res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes; |
| 1991 | |
| 1992 | nfsdirent->type = attributes->type; |
| 1993 | nfsdirent->mode = attributes->mode; |
| 1994 | nfsdirent->size = attributes->size; |
| 1995 | |
| 1996 | nfsdirent->atime.tv_sec = attributes->atime.seconds; |
| 1997 | nfsdirent->atime.tv_usec = attributes->atime.nseconds/1000; |
| 1998 | nfsdirent->mtime.tv_sec = attributes->mtime.seconds; |
| 1999 | nfsdirent->mtime.tv_usec = attributes->mtime.nseconds/1000; |
| 2000 | nfsdirent->ctime.tv_sec = attributes->ctime.seconds; |
| 2001 | nfsdirent->ctime.tv_usec = attributes->ctime.nseconds/1000; |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | if (rdpe_cb_data->getattrcount == 0) { |
| 2006 | if (rdpe_cb_data->status != RPC_STATUS_SUCCESS) { |
| 2007 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2008 | nfs_free_nfsdir(nfsdir); |
| 2009 | } else { |
| 2010 | data->cb(0, nfs, nfsdir, data->private_data); |
| 2011 | } |
| 2012 | free(rdpe_cb_data); |
| 2013 | |
| 2014 | data->continue_data = NULL; |
| 2015 | free_nfs_cb_data(data); |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2020 | { |
| 2021 | READDIR3res *res = command_data; |
| 2022 | struct nfs_cb_data *data = private_data; |
| 2023 | struct nfs_context *nfs = data->nfs; |
| 2024 | struct nfsdir *nfsdir = data->continue_data; |
| 2025 | struct nfsdirent *nfsdirent; |
| 2026 | struct entry3 *entry; |
| 2027 | uint64_t cookie; |
| 2028 | struct rdpe_cb_data *rdpe_cb_data; |
| 2029 | |
| 2030 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2031 | |
| 2032 | if (status == RPC_STATUS_ERROR) { |
| 2033 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2034 | nfs_free_nfsdir(nfsdir); |
| 2035 | data->continue_data = NULL; |
| 2036 | free_nfs_cb_data(data); |
| 2037 | return; |
| 2038 | } |
| 2039 | |
| 2040 | if (status == RPC_STATUS_CANCEL) { |
| 2041 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2042 | nfs_free_nfsdir(nfsdir); |
| 2043 | data->continue_data = NULL; |
| 2044 | free_nfs_cb_data(data); |
| 2045 | return; |
| 2046 | } |
| 2047 | |
| 2048 | if (res->status != NFS3_OK) { |
| 2049 | rpc_set_error(nfs->rpc, "NFS: READDIR of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2050 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2051 | nfs_free_nfsdir(nfsdir); |
| 2052 | data->continue_data = NULL; |
| 2053 | free_nfs_cb_data(data); |
| 2054 | return; |
| 2055 | } |
| 2056 | |
| 2057 | entry =res->READDIR3res_u.resok.reply.entries; |
| 2058 | while (entry != NULL) { |
| 2059 | nfsdirent = malloc(sizeof(struct nfsdirent)); |
| 2060 | if (nfsdirent == NULL) { |
| 2061 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data); |
| 2062 | nfs_free_nfsdir(nfsdir); |
| 2063 | data->continue_data = NULL; |
| 2064 | free_nfs_cb_data(data); |
| 2065 | return; |
| 2066 | } |
| 2067 | memset(nfsdirent, 0, sizeof(struct nfsdirent)); |
| 2068 | nfsdirent->name = strdup(entry->name); |
| 2069 | if (nfsdirent->name == NULL) { |
| 2070 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data); |
| 2071 | nfs_free_nfsdir(nfsdir); |
| 2072 | data->continue_data = NULL; |
| 2073 | free_nfs_cb_data(data); |
| 2074 | return; |
| 2075 | } |
| 2076 | nfsdirent->inode = entry->fileid; |
| 2077 | |
| 2078 | nfsdirent->next = nfsdir->entries; |
| 2079 | nfsdir->entries = nfsdirent; |
| 2080 | |
| 2081 | cookie = entry->cookie; |
| 2082 | entry = entry->nextentry; |
| 2083 | } |
| 2084 | |
| 2085 | if (res->READDIR3res_u.resok.reply.eof == 0) { |
| 2086 | if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, cookie, res->READDIR3res_u.resok.cookieverf, 8192, data) != 0) { |
| 2087 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path); |
| 2088 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2089 | nfs_free_nfsdir(nfsdir); |
| 2090 | data->continue_data = NULL; |
| 2091 | free_nfs_cb_data(data); |
| 2092 | return; |
| 2093 | } |
| 2094 | return; |
| 2095 | } |
| 2096 | |
| 2097 | /* steal the dirhandle */ |
| 2098 | nfsdir->current = nfsdir->entries; |
| 2099 | |
| 2100 | rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data)); |
| 2101 | rdpe_cb_data->getattrcount = 0; |
| 2102 | rdpe_cb_data->status = RPC_STATUS_SUCCESS; |
| 2103 | rdpe_cb_data->data = data; |
| 2104 | for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) { |
| 2105 | struct rdpe_lookup_cb_data *rdpe_lookup_cb_data; |
| 2106 | |
| 2107 | rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data)); |
| 2108 | rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data; |
| 2109 | rdpe_lookup_cb_data->nfsdirent = nfsdirent; |
| 2110 | |
| 2111 | if (rpc_nfs_lookup_async(nfs->rpc, nfs_opendir3_cb, &data->fh, nfsdirent->name, rdpe_lookup_cb_data) != 0) { |
| 2112 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call"); |
| 2113 | |
| 2114 | /* if we have already commands in flight, we cant just stop, we have to wait for the |
| 2115 | * commands in flight to complete |
| 2116 | */ |
| 2117 | if (rdpe_cb_data->getattrcount > 0) { |
| 2118 | rdpe_cb_data->status = RPC_STATUS_ERROR; |
| 2119 | free(rdpe_lookup_cb_data); |
| 2120 | return; |
| 2121 | } |
| 2122 | |
| 2123 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2124 | nfs_free_nfsdir(nfsdir); |
| 2125 | data->continue_data = NULL; |
| 2126 | free_nfs_cb_data(data); |
| 2127 | free(rdpe_lookup_cb_data); |
| 2128 | free(rdpe_cb_data); |
| 2129 | return; |
| 2130 | } |
| 2131 | rdpe_cb_data->getattrcount++; |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | |
| 2136 | static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2137 | { |
| 2138 | READDIRPLUS3res *res = command_data; |
| 2139 | struct nfs_cb_data *data = private_data; |
| 2140 | struct nfs_context *nfs = data->nfs; |
| 2141 | struct nfsdir *nfsdir = data->continue_data; |
| 2142 | struct entryplus3 *entry; |
| 2143 | uint64_t cookie; |
| 2144 | |
| 2145 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2146 | |
| 2147 | if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){ |
| 2148 | cookieverf3 cv; |
| 2149 | |
| 2150 | if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) { |
| 2151 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path); |
| 2152 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2153 | nfs_free_nfsdir(nfsdir); |
| 2154 | data->continue_data = NULL; |
| 2155 | free_nfs_cb_data(data); |
| 2156 | return; |
| 2157 | } |
| 2158 | return; |
| 2159 | } |
| 2160 | |
| 2161 | if (status == RPC_STATUS_CANCEL) { |
| 2162 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2163 | nfs_free_nfsdir(nfsdir); |
| 2164 | data->continue_data = NULL; |
| 2165 | free_nfs_cb_data(data); |
| 2166 | return; |
| 2167 | } |
| 2168 | |
| 2169 | if (res->status != NFS3_OK) { |
| 2170 | rpc_set_error(nfs->rpc, "NFS: READDIRPLUS of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2171 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2172 | nfs_free_nfsdir(nfsdir); |
| 2173 | data->continue_data = NULL; |
| 2174 | free_nfs_cb_data(data); |
| 2175 | return; |
| 2176 | } |
| 2177 | |
| 2178 | entry =res->READDIRPLUS3res_u.resok.reply.entries; |
| 2179 | while (entry != NULL) { |
| 2180 | struct nfsdirent *nfsdirent; |
| 2181 | |
| 2182 | nfsdirent = malloc(sizeof(struct nfsdirent)); |
| 2183 | if (nfsdirent == NULL) { |
| 2184 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data); |
| 2185 | nfs_free_nfsdir(nfsdir); |
| 2186 | data->continue_data = NULL; |
| 2187 | free_nfs_cb_data(data); |
| 2188 | return; |
| 2189 | } |
| 2190 | memset(nfsdirent, 0, sizeof(struct nfsdirent)); |
| 2191 | nfsdirent->name = strdup(entry->name); |
| 2192 | if (nfsdirent->name == NULL) { |
| 2193 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data); |
| 2194 | nfs_free_nfsdir(nfsdir); |
| 2195 | data->continue_data = NULL; |
| 2196 | free_nfs_cb_data(data); |
| 2197 | return; |
| 2198 | } |
| 2199 | nfsdirent->inode = entry->fileid; |
| 2200 | if (entry->name_attributes.attributes_follow) { |
| 2201 | nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type; |
| 2202 | nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode; |
| 2203 | nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size; |
| 2204 | |
| 2205 | nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds; |
| 2206 | nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000; |
| 2207 | nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds; |
| 2208 | nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000; |
| 2209 | nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds; |
| 2210 | nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000; |
| 2211 | } |
| 2212 | |
| 2213 | nfsdirent->next = nfsdir->entries; |
| 2214 | nfsdir->entries = nfsdirent; |
| 2215 | |
| 2216 | cookie = entry->cookie; |
| 2217 | entry = entry->nextentry; |
| 2218 | } |
| 2219 | |
| 2220 | if (res->READDIRPLUS3res_u.resok.reply.eof == 0) { |
| 2221 | if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, cookie, res->READDIRPLUS3res_u.resok.cookieverf, 8192, data) != 0) { |
| 2222 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path); |
| 2223 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2224 | nfs_free_nfsdir(nfsdir); |
| 2225 | data->continue_data = NULL; |
| 2226 | free_nfs_cb_data(data); |
| 2227 | return; |
| 2228 | } |
| 2229 | return; |
| 2230 | } |
| 2231 | |
| 2232 | /* steal the dirhandle */ |
| 2233 | data->continue_data = NULL; |
| 2234 | nfsdir->current = nfsdir->entries; |
| 2235 | |
| 2236 | data->cb(0, nfs, nfsdir, data->private_data); |
| 2237 | free_nfs_cb_data(data); |
| 2238 | } |
| 2239 | |
| 2240 | static int nfs_opendir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 2241 | { |
| 2242 | cookieverf3 cv; |
| 2243 | |
| 2244 | memset(cv, 0, sizeof(cookieverf3)); |
| 2245 | if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) { |
| 2246 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path); |
| 2247 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2248 | free_nfs_cb_data(data); |
| 2249 | return -1; |
| 2250 | } |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 2255 | { |
| 2256 | struct nfsdir *nfsdir; |
| 2257 | |
| 2258 | nfsdir = malloc(sizeof(struct nfsdir)); |
| 2259 | if (nfsdir == NULL) { |
| 2260 | rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir"); |
| 2261 | return -1; |
| 2262 | } |
| 2263 | memset(nfsdir, 0, sizeof(struct nfsdir)); |
| 2264 | |
| 2265 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) { |
| 2266 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2267 | return -1; |
| 2268 | } |
| 2269 | |
| 2270 | return 0; |
| 2271 | } |
| 2272 | |
| 2273 | |
| 2274 | struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir) |
| 2275 | { |
| 2276 | struct nfsdirent *nfsdirent = nfsdir->current; |
| 2277 | |
| 2278 | if (nfsdir->current != NULL) { |
| 2279 | nfsdir->current = nfsdir->current->next; |
| 2280 | } |
| 2281 | return nfsdirent; |
| 2282 | } |
| 2283 | |
| 2284 | |
| 2285 | void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir) |
| 2286 | { |
| 2287 | nfs_free_nfsdir(nfsdir); |
| 2288 | } |
| 2289 | |
| 2290 | |
| 2291 | |
| 2292 | |
| 2293 | |
| 2294 | |
| 2295 | |
| 2296 | /* |
| 2297 | * Async lseek() |
| 2298 | */ |
| 2299 | struct lseek_cb_data { |
| 2300 | struct nfs_context *nfs; |
| 2301 | struct nfsfh *nfsfh; |
| 2302 | uint64_t offset; |
| 2303 | nfs_cb cb; |
| 2304 | void *private_data; |
| 2305 | }; |
| 2306 | |
| 2307 | static void nfs_lseek_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2308 | { |
| 2309 | GETATTR3res *res; |
| 2310 | struct lseek_cb_data *data = private_data; |
| 2311 | struct nfs_context *nfs = data->nfs; |
| 2312 | |
| 2313 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2314 | |
| 2315 | if (status == RPC_STATUS_ERROR) { |
| 2316 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2317 | free(data); |
| 2318 | return; |
| 2319 | } |
| 2320 | if (status == RPC_STATUS_CANCEL) { |
| 2321 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2322 | free(data); |
| 2323 | return; |
| 2324 | } |
| 2325 | |
| 2326 | res = command_data; |
| 2327 | if (res->status != NFS3_OK) { |
| 2328 | rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2329 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2330 | free(data); |
| 2331 | return; |
| 2332 | } |
| 2333 | |
| 2334 | data->nfsfh->offset = data->offset + res->GETATTR3res_u.resok.obj_attributes.size; |
| 2335 | data->cb(0, nfs, &data->nfsfh->offset, data->private_data); |
| 2336 | free(data); |
| 2337 | } |
| 2338 | |
| 2339 | int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, int whence, nfs_cb cb, void *private_data) |
| 2340 | { |
| 2341 | struct lseek_cb_data *data; |
| 2342 | |
| 2343 | if (whence == SEEK_SET) { |
| 2344 | nfsfh->offset = offset; |
| 2345 | cb(0, nfs, &nfsfh->offset, private_data); |
| 2346 | return 0; |
| 2347 | } |
| 2348 | if (whence == SEEK_CUR) { |
| 2349 | nfsfh->offset += offset; |
| 2350 | cb(0, nfs, &nfsfh->offset, private_data); |
| 2351 | return 0; |
| 2352 | } |
| 2353 | |
| 2354 | data = malloc(sizeof(struct lseek_cb_data)); |
| 2355 | if (data == NULL) { |
| 2356 | rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data"); |
| 2357 | return -1; |
| 2358 | } |
| 2359 | |
| 2360 | data->nfs = nfs; |
| 2361 | data->nfsfh = nfsfh; |
| 2362 | data->offset = offset; |
| 2363 | data->cb = cb; |
| 2364 | data->private_data = private_data; |
| 2365 | |
| 2366 | if (rpc_nfs_getattr_async(nfs->rpc, nfs_lseek_1_cb, &nfsfh->fh, data) != 0) { |
| 2367 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call"); |
| 2368 | free(data); |
| 2369 | return -1; |
| 2370 | } |
| 2371 | return 0; |
| 2372 | } |
| 2373 | |
| 2374 | |
| 2375 | |
| 2376 | |
| 2377 | /* |
| 2378 | * Async statvfs() |
| 2379 | */ |
| 2380 | static void nfs_statvfs_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2381 | { |
| 2382 | FSSTAT3res *res; |
| 2383 | struct nfs_cb_data *data = private_data; |
| 2384 | struct nfs_context *nfs = data->nfs; |
| 2385 | struct statvfs svfs; |
| 2386 | |
| 2387 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2388 | |
| 2389 | if (status == RPC_STATUS_ERROR) { |
| 2390 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2391 | free_nfs_cb_data(data); |
| 2392 | return; |
| 2393 | } |
| 2394 | if (status == RPC_STATUS_CANCEL) { |
| 2395 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2396 | free_nfs_cb_data(data); |
| 2397 | return; |
| 2398 | } |
| 2399 | |
| 2400 | res = command_data; |
| 2401 | if (res->status != NFS3_OK) { |
| 2402 | rpc_set_error(nfs->rpc, "NFS: FSSTAT of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2403 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2404 | free_nfs_cb_data(data); |
| 2405 | return; |
| 2406 | } |
| 2407 | |
| 2408 | svfs.f_bsize = 4096; |
| 2409 | svfs.f_frsize = 4096; |
| 2410 | svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/4096; |
| 2411 | svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/4096; |
| 2412 | svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/4096; |
| 2413 | svfs.f_files = res->FSSTAT3res_u.resok.tfiles; |
| 2414 | svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles; |
| 2415 | svfs.f_favail = res->FSSTAT3res_u.resok.afiles; |
| 2416 | svfs.f_fsid = 0; |
| 2417 | svfs.f_flag = 0; |
| 2418 | svfs.f_namemax = 256; |
| 2419 | |
| 2420 | data->cb(0, nfs, &svfs, data->private_data); |
| 2421 | free_nfs_cb_data(data); |
| 2422 | } |
| 2423 | |
| 2424 | static int nfs_statvfs_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 2425 | { |
| 2426 | if (rpc_nfs_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &data->fh, data) != 0) { |
| 2427 | rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT call for %s", data->path); |
| 2428 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2429 | free_nfs_cb_data(data); |
| 2430 | return -1; |
| 2431 | } |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
| 2435 | int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 2436 | { |
| 2437 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) { |
| 2438 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2439 | return -1; |
| 2440 | } |
| 2441 | |
| 2442 | return 0; |
| 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | |
| 2447 | |
| 2448 | /* |
| 2449 | * Async readlink() |
| 2450 | */ |
| 2451 | static void nfs_readlink_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2452 | { |
| 2453 | READLINK3res *res; |
| 2454 | struct nfs_cb_data *data = private_data; |
| 2455 | struct nfs_context *nfs = data->nfs; |
| 2456 | |
| 2457 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2458 | |
| 2459 | if (status == RPC_STATUS_ERROR) { |
| 2460 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2461 | free_nfs_cb_data(data); |
| 2462 | return; |
| 2463 | } |
| 2464 | if (status == RPC_STATUS_CANCEL) { |
| 2465 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2466 | free_nfs_cb_data(data); |
| 2467 | return; |
| 2468 | } |
| 2469 | |
| 2470 | res = command_data; |
| 2471 | if (res->status != NFS3_OK) { |
| 2472 | rpc_set_error(nfs->rpc, "NFS: READLINK of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2473 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2474 | free_nfs_cb_data(data); |
| 2475 | return; |
| 2476 | } |
| 2477 | |
| 2478 | |
| 2479 | data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data); |
| 2480 | free_nfs_cb_data(data); |
| 2481 | } |
| 2482 | |
| 2483 | static int nfs_readlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 2484 | { |
| 2485 | READLINK3args args; |
| 2486 | |
| 2487 | args.symlink.data.data_len = data->fh.data.data_len; |
| 2488 | args.symlink.data.data_val = data->fh.data.data_val; |
| 2489 | |
| 2490 | if (rpc_nfs_readlink_async(nfs->rpc, nfs_readlink_1_cb, &args, data) != 0) { |
| 2491 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path); |
| 2492 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2493 | free_nfs_cb_data(data); |
| 2494 | return -1; |
| 2495 | } |
| 2496 | return 0; |
| 2497 | } |
| 2498 | |
| 2499 | int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 2500 | { |
| 2501 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) { |
| 2502 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2503 | return -1; |
| 2504 | } |
| 2505 | |
| 2506 | return 0; |
| 2507 | } |
| 2508 | |
| 2509 | |
| 2510 | |
| 2511 | |
| 2512 | /* |
| 2513 | * Async chmod() |
| 2514 | */ |
| 2515 | static void nfs_chmod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2516 | { |
| 2517 | struct nfs_cb_data *data = private_data; |
| 2518 | struct nfs_context *nfs = data->nfs; |
| 2519 | SETATTR3res *res; |
| 2520 | |
| 2521 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2522 | |
| 2523 | if (status == RPC_STATUS_ERROR) { |
| 2524 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2525 | free_nfs_cb_data(data); |
| 2526 | return; |
| 2527 | } |
| 2528 | if (status == RPC_STATUS_CANCEL) { |
| 2529 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2530 | free_nfs_cb_data(data); |
| 2531 | return; |
| 2532 | } |
| 2533 | |
| 2534 | res = command_data; |
| 2535 | if (res->status != NFS3_OK) { |
| 2536 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2537 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2538 | free_nfs_cb_data(data); |
| 2539 | return; |
| 2540 | } |
| 2541 | |
| 2542 | data->cb(0, nfs, NULL, data->private_data); |
| 2543 | free_nfs_cb_data(data); |
| 2544 | } |
| 2545 | |
| 2546 | static int nfs_chmod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 2547 | { |
| 2548 | SETATTR3args args; |
| 2549 | |
| 2550 | memset(&args, 0, sizeof(SETATTR3args)); |
| 2551 | args.object.data.data_len = data->fh.data.data_len; |
| 2552 | args.object.data.data_val = data->fh.data.data_val; |
| 2553 | args.new_attributes.mode.set_it = 1; |
| 2554 | args.new_attributes.mode.set_mode3_u.mode = data->continue_int; |
| 2555 | |
| 2556 | if (rpc_nfs_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) { |
| 2557 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 2558 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2559 | free_nfs_cb_data(data); |
| 2560 | return -1; |
| 2561 | } |
| 2562 | return 0; |
| 2563 | } |
| 2564 | |
| 2565 | |
| 2566 | int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) |
| 2567 | { |
| 2568 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) { |
| 2569 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2570 | return -1; |
| 2571 | } |
| 2572 | |
| 2573 | return 0; |
| 2574 | } |
| 2575 | |
| 2576 | /* |
| 2577 | * Async fchmod() |
| 2578 | */ |
| 2579 | int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data) |
| 2580 | { |
| 2581 | struct nfs_cb_data *data; |
| 2582 | |
| 2583 | data = malloc(sizeof(struct nfs_cb_data)); |
| 2584 | if (data == NULL) { |
| 2585 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data"); |
| 2586 | return -1; |
| 2587 | } |
| 2588 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 2589 | data->nfs = nfs; |
| 2590 | data->cb = cb; |
| 2591 | data->private_data = private_data; |
| 2592 | data->continue_int = mode; |
| 2593 | data->fh.data.data_len = nfsfh->fh.data.data_len; |
| 2594 | data->fh.data.data_val = malloc(data->fh.data.data_len); |
| 2595 | if (data->fh.data.data_val == NULL) { |
| 2596 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh"); |
| 2597 | free_nfs_cb_data(data); |
| 2598 | return -1; |
| 2599 | } |
| 2600 | memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len); |
| 2601 | |
| 2602 | if (nfs_chmod_continue_internal(nfs, data) != 0) { |
| 2603 | free_nfs_cb_data(data); |
| 2604 | return -1; |
| 2605 | } |
| 2606 | |
| 2607 | return 0; |
| 2608 | } |
| 2609 | |
| 2610 | |
| 2611 | |
| 2612 | /* |
| 2613 | * Async chown() |
| 2614 | */ |
| 2615 | static void nfs_chown_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2616 | { |
| 2617 | struct nfs_cb_data *data = private_data; |
| 2618 | struct nfs_context *nfs = data->nfs; |
| 2619 | SETATTR3res *res; |
| 2620 | |
| 2621 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2622 | |
| 2623 | if (status == RPC_STATUS_ERROR) { |
| 2624 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2625 | free_nfs_cb_data(data); |
| 2626 | return; |
| 2627 | } |
| 2628 | if (status == RPC_STATUS_CANCEL) { |
| 2629 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2630 | free_nfs_cb_data(data); |
| 2631 | return; |
| 2632 | } |
| 2633 | |
| 2634 | res = command_data; |
| 2635 | if (res->status != NFS3_OK) { |
| 2636 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2637 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2638 | free_nfs_cb_data(data); |
| 2639 | return; |
| 2640 | } |
| 2641 | |
| 2642 | data->cb(0, nfs, NULL, data->private_data); |
| 2643 | free_nfs_cb_data(data); |
| 2644 | } |
| 2645 | |
| 2646 | struct nfs_chown_data { |
| 2647 | uid_t uid; |
| 2648 | gid_t gid; |
| 2649 | }; |
| 2650 | |
| 2651 | static int nfs_chown_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 2652 | { |
| 2653 | SETATTR3args args; |
| 2654 | struct nfs_chown_data *chown_data = data->continue_data; |
| 2655 | |
| 2656 | memset(&args, 0, sizeof(SETATTR3args)); |
| 2657 | args.object.data.data_len = data->fh.data.data_len; |
| 2658 | args.object.data.data_val = data->fh.data.data_val; |
| 2659 | if (chown_data->uid != (uid_t)-1) { |
| 2660 | args.new_attributes.uid.set_it = 1; |
| 2661 | args.new_attributes.uid.set_uid3_u.uid = chown_data->uid; |
| 2662 | } |
| 2663 | if (chown_data->gid != (gid_t)-1) { |
| 2664 | args.new_attributes.gid.set_it = 1; |
| 2665 | args.new_attributes.gid.set_gid3_u.gid = chown_data->gid; |
| 2666 | } |
| 2667 | |
| 2668 | if (rpc_nfs_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) { |
| 2669 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 2670 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2671 | free_nfs_cb_data(data); |
| 2672 | return -1; |
| 2673 | } |
| 2674 | return 0; |
| 2675 | } |
| 2676 | |
| 2677 | |
| 2678 | int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data) |
| 2679 | { |
| 2680 | struct nfs_chown_data *chown_data; |
| 2681 | |
| 2682 | chown_data = malloc(sizeof(struct nfs_chown_data)); |
| 2683 | if (chown_data == NULL) { |
| 2684 | rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure"); |
| 2685 | return -1; |
| 2686 | } |
| 2687 | |
| 2688 | chown_data->uid = uid; |
| 2689 | chown_data->gid = gid; |
| 2690 | |
| 2691 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) { |
| 2692 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2693 | return -1; |
| 2694 | } |
| 2695 | |
| 2696 | return 0; |
| 2697 | } |
| 2698 | |
| 2699 | |
| 2700 | /* |
| 2701 | * Async fchown() |
| 2702 | */ |
| 2703 | int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data) |
| 2704 | { |
| 2705 | struct nfs_cb_data *data; |
| 2706 | struct nfs_chown_data *chown_data; |
| 2707 | |
| 2708 | chown_data = malloc(sizeof(struct nfs_chown_data)); |
| 2709 | if (chown_data == NULL) { |
| 2710 | rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure"); |
| 2711 | return -1; |
| 2712 | } |
| 2713 | |
| 2714 | chown_data->uid = uid; |
| 2715 | chown_data->gid = gid; |
| 2716 | |
| 2717 | |
| 2718 | data = malloc(sizeof(struct nfs_cb_data)); |
| 2719 | if (data == NULL) { |
| 2720 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data"); |
| 2721 | return -1; |
| 2722 | } |
| 2723 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 2724 | data->nfs = nfs; |
| 2725 | data->cb = cb; |
| 2726 | data->private_data = private_data; |
| 2727 | data->continue_data = chown_data; |
| 2728 | data->fh.data.data_len = nfsfh->fh.data.data_len; |
| 2729 | data->fh.data.data_val = malloc(data->fh.data.data_len); |
| 2730 | if (data->fh.data.data_val == NULL) { |
| 2731 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh"); |
| 2732 | free_nfs_cb_data(data); |
| 2733 | return -1; |
| 2734 | } |
| 2735 | memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len); |
| 2736 | |
| 2737 | |
| 2738 | if (nfs_chown_continue_internal(nfs, data) != 0) { |
| 2739 | free_nfs_cb_data(data); |
| 2740 | return -1; |
| 2741 | } |
| 2742 | |
| 2743 | return 0; |
| 2744 | } |
| 2745 | |
| 2746 | |
| 2747 | |
| 2748 | |
| 2749 | |
| 2750 | /* |
| 2751 | * Async utimes() |
| 2752 | */ |
| 2753 | static void nfs_utimes_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2754 | { |
| 2755 | struct nfs_cb_data *data = private_data; |
| 2756 | struct nfs_context *nfs = data->nfs; |
| 2757 | SETATTR3res *res; |
| 2758 | |
| 2759 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2760 | |
| 2761 | if (status == RPC_STATUS_ERROR) { |
| 2762 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2763 | free_nfs_cb_data(data); |
| 2764 | return; |
| 2765 | } |
| 2766 | if (status == RPC_STATUS_CANCEL) { |
| 2767 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2768 | free_nfs_cb_data(data); |
| 2769 | return; |
| 2770 | } |
| 2771 | |
| 2772 | res = command_data; |
| 2773 | if (res->status != NFS3_OK) { |
| 2774 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2775 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2776 | free_nfs_cb_data(data); |
| 2777 | return; |
| 2778 | } |
| 2779 | |
| 2780 | data->cb(0, nfs, NULL, data->private_data); |
| 2781 | free_nfs_cb_data(data); |
| 2782 | } |
| 2783 | |
| 2784 | static int nfs_utimes_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 2785 | { |
| 2786 | SETATTR3args args; |
| 2787 | struct timeval *utimes_data = data->continue_data; |
| 2788 | |
| 2789 | memset(&args, 0, sizeof(SETATTR3args)); |
| 2790 | args.object.data.data_len = data->fh.data.data_len; |
| 2791 | args.object.data.data_val = data->fh.data.data_val; |
| 2792 | if (utimes_data != NULL) { |
| 2793 | args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME; |
| 2794 | args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec; |
| 2795 | args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000; |
| 2796 | args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME; |
| 2797 | args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec; |
| 2798 | args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000; |
| 2799 | } else { |
| 2800 | args.new_attributes.atime.set_it = SET_TO_SERVER_TIME; |
| 2801 | args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME; |
| 2802 | } |
| 2803 | |
| 2804 | if (rpc_nfs_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) { |
| 2805 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 2806 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2807 | free_nfs_cb_data(data); |
| 2808 | return -1; |
| 2809 | } |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
| 2813 | |
| 2814 | int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data) |
| 2815 | { |
| 2816 | struct timeval *new_times = NULL; |
| 2817 | |
| 2818 | if (times != NULL) { |
| 2819 | new_times = malloc(sizeof(struct timeval)*2); |
| 2820 | if (new_times == NULL) { |
| 2821 | rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure"); |
| 2822 | return -1; |
| 2823 | } |
| 2824 | |
| 2825 | memcpy(new_times, times, sizeof(struct timeval)*2); |
| 2826 | } |
| 2827 | |
| 2828 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) { |
| 2829 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2830 | return -1; |
| 2831 | } |
| 2832 | |
| 2833 | return 0; |
| 2834 | } |
| 2835 | |
| 2836 | /* |
| 2837 | * Async utime() |
| 2838 | */ |
| 2839 | int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data) |
| 2840 | { |
| 2841 | struct timeval *new_times = NULL; |
| 2842 | |
| 2843 | if (times != NULL) { |
| 2844 | new_times = malloc(sizeof(struct timeval)*2); |
| 2845 | if (new_times == NULL) { |
| 2846 | rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure"); |
| 2847 | return -1; |
| 2848 | } |
| 2849 | |
| 2850 | new_times[0].tv_sec = times->actime; |
| 2851 | new_times[0].tv_usec = 0; |
| 2852 | new_times[1].tv_sec = times->modtime; |
| 2853 | new_times[1].tv_usec = 0; |
| 2854 | } |
| 2855 | |
| 2856 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) { |
| 2857 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2858 | return -1; |
| 2859 | } |
| 2860 | |
| 2861 | return 0; |
| 2862 | } |
| 2863 | |
| 2864 | |
| 2865 | |
| 2866 | |
| 2867 | |
| 2868 | /* |
| 2869 | * Async access() |
| 2870 | */ |
| 2871 | static void nfs_access_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2872 | { |
| 2873 | ACCESS3res *res; |
| 2874 | struct nfs_cb_data *data = private_data; |
| 2875 | struct nfs_context *nfs = data->nfs; |
| 2876 | unsigned int nfsmode = 0; |
| 2877 | |
| 2878 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2879 | |
| 2880 | if (status == RPC_STATUS_ERROR) { |
| 2881 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2882 | free_nfs_cb_data(data); |
| 2883 | return; |
| 2884 | } |
| 2885 | if (status == RPC_STATUS_CANCEL) { |
| 2886 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2887 | free_nfs_cb_data(data); |
| 2888 | return; |
| 2889 | } |
| 2890 | |
| 2891 | res = command_data; |
| 2892 | if (res->status != NFS3_OK) { |
| 2893 | rpc_set_error(nfs->rpc, "NFS: ACCESS of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2894 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2895 | free_nfs_cb_data(data); |
| 2896 | return; |
| 2897 | } |
| 2898 | |
| 2899 | if (data->continue_int & R_OK) { |
| 2900 | nfsmode |= ACCESS3_READ; |
| 2901 | } |
| 2902 | if (data->continue_int & W_OK) { |
| 2903 | nfsmode |= ACCESS3_MODIFY; |
| 2904 | } |
| 2905 | if (data->continue_int & X_OK) { |
| 2906 | nfsmode |= ACCESS3_EXECUTE; |
| 2907 | } |
| 2908 | |
| 2909 | if (res->ACCESS3res_u.resok.access != nfsmode) { |
| 2910 | rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c", |
| 2911 | nfsmode&ACCESS3_READ?'r':'-', |
| 2912 | nfsmode&ACCESS3_MODIFY?'w':'-', |
| 2913 | nfsmode&ACCESS3_EXECUTE?'x':'-', |
| 2914 | res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-', |
| 2915 | res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-', |
| 2916 | res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-'); |
| 2917 | data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2918 | free_nfs_cb_data(data); |
| 2919 | return; |
| 2920 | } |
| 2921 | |
| 2922 | data->cb(0, nfs, NULL, data->private_data); |
| 2923 | free_nfs_cb_data(data); |
| 2924 | } |
| 2925 | |
| 2926 | static int nfs_access_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 2927 | { |
| 2928 | int nfsmode = 0; |
| 2929 | |
| 2930 | if (data->continue_int & R_OK) { |
| 2931 | nfsmode |= ACCESS3_READ; |
| 2932 | } |
| 2933 | if (data->continue_int & W_OK) { |
| 2934 | nfsmode |= ACCESS3_MODIFY; |
| 2935 | } |
| 2936 | if (data->continue_int & X_OK) { |
| 2937 | nfsmode |= ACCESS3_EXECUTE; |
| 2938 | } |
| 2939 | |
| 2940 | if (rpc_nfs_access_async(nfs->rpc, nfs_access_cb, &data->fh, nfsmode, data) != 0) { |
| 2941 | rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path); |
| 2942 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2943 | free_nfs_cb_data(data); |
| 2944 | return -1; |
| 2945 | } |
| 2946 | return 0; |
| 2947 | } |
| 2948 | |
| 2949 | int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) |
| 2950 | { |
| 2951 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) { |
| 2952 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2953 | return -1; |
| 2954 | } |
| 2955 | |
| 2956 | return 0; |
| 2957 | } |
| 2958 | |
| 2959 | |
| 2960 | |
| 2961 | /* |
| 2962 | * Async symlink() |
| 2963 | */ |
| 2964 | struct nfs_symlink_data { |
| 2965 | char *oldpath; |
| 2966 | char *newpathparent; |
| 2967 | char *newpathobject; |
| 2968 | }; |
| 2969 | |
| 2970 | static void free_nfs_symlink_data(void *mem) |
| 2971 | { |
| 2972 | struct nfs_symlink_data *data = mem; |
| 2973 | |
| 2974 | if (data->oldpath != NULL) { |
| 2975 | free(data->oldpath); |
| 2976 | } |
| 2977 | if (data->newpathparent != NULL) { |
| 2978 | free(data->newpathparent); |
| 2979 | } |
| 2980 | if (data->newpathobject != NULL) { |
| 2981 | free(data->newpathobject); |
| 2982 | } |
| 2983 | free(data); |
| 2984 | } |
| 2985 | |
| 2986 | static void nfs_symlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2987 | { |
| 2988 | SYMLINK3res *res; |
| 2989 | struct nfs_cb_data *data = private_data; |
| 2990 | struct nfs_context *nfs = data->nfs; |
| 2991 | struct nfs_symlink_data *symlink_data = data->continue_data; |
| 2992 | |
| 2993 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2994 | |
| 2995 | if (status == RPC_STATUS_ERROR) { |
| 2996 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2997 | free_nfs_cb_data(data); |
| 2998 | return; |
| 2999 | } |
| 3000 | if (status == RPC_STATUS_CANCEL) { |
| 3001 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3002 | free_nfs_cb_data(data); |
| 3003 | return; |
| 3004 | } |
| 3005 | |
| 3006 | res = command_data; |
| 3007 | if (res->status != NFS3_OK) { |
| 3008 | rpc_set_error(nfs->rpc, "NFS: SYMLINK %s/%s -> %s failed with %s(%d)", symlink_data->newpathparent, symlink_data->newpathobject, symlink_data->oldpath, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 3009 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3010 | free_nfs_cb_data(data); |
| 3011 | return; |
| 3012 | } |
| 3013 | |
| 3014 | data->cb(0, nfs, NULL, data->private_data); |
| 3015 | free_nfs_cb_data(data); |
| 3016 | } |
| 3017 | |
| 3018 | static int nfs_symlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 3019 | { |
| 3020 | struct nfs_symlink_data *symlink_data = data->continue_data; |
| 3021 | SYMLINK3args sa; |
| 3022 | |
| 3023 | memset(&sa, 0, sizeof(SYMLINK3args)); |
| 3024 | sa.where.dir.data.data_len = data->fh.data.data_len; |
| 3025 | sa.where.dir.data.data_val = data->fh.data.data_val; |
| 3026 | sa.where.name = symlink_data->newpathobject; |
| 3027 | sa.symlink.symlink_attributes.mode.set_it = 1; |
| 3028 | sa.symlink.symlink_attributes.mode.set_mode3_u.mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH; |
| 3029 | sa.symlink.symlink_data = symlink_data->oldpath; |
| 3030 | |
| 3031 | if (rpc_nfs_symlink_async(nfs->rpc, nfs_symlink_cb, &sa, data) != 0) { |
| 3032 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path); |
| 3033 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3034 | free_nfs_cb_data(data); |
| 3035 | return -1; |
| 3036 | } |
| 3037 | return 0; |
| 3038 | } |
| 3039 | |
| 3040 | int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) |
| 3041 | { |
| 3042 | char *ptr; |
| 3043 | struct nfs_symlink_data *symlink_data; |
| 3044 | |
| 3045 | symlink_data = malloc(sizeof(struct nfs_symlink_data)); |
| 3046 | if (symlink_data == NULL) { |
| 3047 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data"); |
| 3048 | return -1; |
| 3049 | } |
| 3050 | memset(symlink_data, 0, sizeof(struct nfs_symlink_data)); |
| 3051 | |
| 3052 | symlink_data->oldpath = strdup(oldpath); |
| 3053 | if (symlink_data->oldpath == NULL) { |
| 3054 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
| 3055 | free_nfs_symlink_data(symlink_data); |
| 3056 | return -1; |
| 3057 | } |
| 3058 | |
| 3059 | symlink_data->newpathparent = strdup(newpath); |
| 3060 | if (symlink_data->newpathparent == NULL) { |
| 3061 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path"); |
| 3062 | free_nfs_symlink_data(symlink_data); |
| 3063 | return -1; |
| 3064 | } |
| 3065 | |
| 3066 | ptr = strrchr(symlink_data->newpathparent, '/'); |
| 3067 | if (ptr == NULL) { |
| 3068 | rpc_set_error(nfs->rpc, "Invalid path %s", oldpath); |
| 3069 | free_nfs_symlink_data(symlink_data); |
| 3070 | return -1; |
| 3071 | } |
| 3072 | *ptr = 0; |
| 3073 | ptr++; |
| 3074 | |
| 3075 | symlink_data->newpathobject = strdup(ptr); |
| 3076 | if (symlink_data->newpathobject == NULL) { |
| 3077 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path"); |
| 3078 | free_nfs_symlink_data(symlink_data); |
| 3079 | return -1; |
| 3080 | } |
| 3081 | |
| 3082 | if (nfs_lookuppath_async(nfs, symlink_data->newpathparent, cb, private_data, nfs_symlink_continue_internal, symlink_data, free_nfs_symlink_data, 0) != 0) { |
| 3083 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3084 | return -1; |
| 3085 | } |
| 3086 | |
| 3087 | return 0; |
| 3088 | } |
| 3089 | |
| 3090 | |
| 3091 | |
| 3092 | /* |
| 3093 | * Async rename() |
| 3094 | */ |
| 3095 | struct nfs_rename_data { |
| 3096 | char *oldpath; |
| 3097 | char *oldobject; |
| 3098 | struct nfs_fh3 olddir; |
| 3099 | char *newpath; |
| 3100 | char *newobject; |
| 3101 | struct nfs_fh3 newdir; |
| 3102 | }; |
| 3103 | |
| 3104 | static void free_nfs_rename_data(void *mem) |
| 3105 | { |
| 3106 | struct nfs_rename_data *data = mem; |
| 3107 | |
| 3108 | if (data->oldpath != NULL) { |
| 3109 | free(data->oldpath); |
| 3110 | } |
| 3111 | if (data->olddir.data.data_val != NULL) { |
| 3112 | free(data->olddir.data.data_val); |
| 3113 | } |
| 3114 | if (data->newpath != NULL) { |
| 3115 | free(data->newpath); |
| 3116 | } |
| 3117 | if (data->newdir.data.data_val != NULL) { |
| 3118 | free(data->newdir.data.data_val); |
| 3119 | } |
| 3120 | free(data); |
| 3121 | } |
| 3122 | |
| 3123 | static void nfs_rename_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3124 | { |
| 3125 | RENAME3res *res; |
| 3126 | struct nfs_cb_data *data = private_data; |
| 3127 | struct nfs_context *nfs = data->nfs; |
| 3128 | struct nfs_rename_data *rename_data = data->continue_data; |
| 3129 | |
| 3130 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3131 | |
| 3132 | if (status == RPC_STATUS_ERROR) { |
| 3133 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3134 | free_nfs_cb_data(data); |
| 3135 | return; |
| 3136 | } |
| 3137 | if (status == RPC_STATUS_CANCEL) { |
| 3138 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3139 | free_nfs_cb_data(data); |
| 3140 | return; |
| 3141 | } |
| 3142 | |
| 3143 | res = command_data; |
| 3144 | if (res->status != NFS3_OK) { |
| 3145 | rpc_set_error(nfs->rpc, "NFS: RENAME %s/%s -> %s/%s failed with %s(%d)", rename_data->oldpath, rename_data->oldobject, rename_data->newpath, rename_data->newobject, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 3146 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3147 | free_nfs_cb_data(data); |
| 3148 | return; |
| 3149 | } |
| 3150 | |
| 3151 | data->cb(0, nfs, NULL, data->private_data); |
| 3152 | free_nfs_cb_data(data); |
| 3153 | } |
| 3154 | |
| 3155 | static int nfs_rename_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 3156 | { |
| 3157 | struct nfs_rename_data *rename_data = data->continue_data; |
| 3158 | |
| 3159 | /* steal the filehandle */ |
| 3160 | rename_data->newdir.data.data_len = data->fh.data.data_len; |
| 3161 | rename_data->newdir.data.data_val = data->fh.data.data_val; |
| 3162 | data->fh.data.data_val = NULL; |
| 3163 | |
| 3164 | if (rpc_nfs_rename_async(nfs->rpc, nfs_rename_cb, &rename_data->olddir, rename_data->oldobject, &rename_data->newdir, rename_data->newobject, data) != 0) { |
| 3165 | rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path); |
| 3166 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3167 | free_nfs_cb_data(data); |
| 3168 | return -1; |
| 3169 | } |
| 3170 | return 0; |
| 3171 | } |
| 3172 | |
| 3173 | |
| 3174 | static int nfs_rename_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 3175 | { |
| 3176 | struct nfs_rename_data *rename_data = data->continue_data; |
| 3177 | |
| 3178 | /* steal the filehandle */ |
| 3179 | rename_data->olddir.data.data_len = data->fh.data.data_len; |
| 3180 | rename_data->olddir.data.data_val = data->fh.data.data_val; |
| 3181 | data->fh.data.data_val = NULL; |
| 3182 | |
| 3183 | if (nfs_lookuppath_async(nfs, rename_data->newpath, data->cb, data->private_data, nfs_rename_continue_2_internal, rename_data, free_nfs_rename_data, 0) != 0) { |
| 3184 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", rename_data->newpath); |
| 3185 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3186 | free_nfs_cb_data(data); |
| 3187 | return -1; |
| 3188 | } |
| 3189 | data->continue_data = NULL; |
| 3190 | free_nfs_cb_data(data); |
| 3191 | |
| 3192 | return 0; |
| 3193 | } |
| 3194 | |
| 3195 | |
| 3196 | int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) |
| 3197 | { |
| 3198 | char *ptr; |
| 3199 | struct nfs_rename_data *rename_data; |
| 3200 | |
| 3201 | rename_data = malloc(sizeof(struct nfs_rename_data)); |
| 3202 | if (rename_data == NULL) { |
| 3203 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data"); |
| 3204 | return -1; |
| 3205 | } |
| 3206 | memset(rename_data, 0, sizeof(struct nfs_rename_data)); |
| 3207 | |
| 3208 | rename_data->oldpath = strdup(oldpath); |
| 3209 | if (rename_data->oldpath == NULL) { |
| 3210 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
| 3211 | free_nfs_rename_data(rename_data); |
| 3212 | return -1; |
| 3213 | } |
| 3214 | ptr = strrchr(rename_data->oldpath, '/'); |
| 3215 | if (ptr == NULL) { |
| 3216 | rpc_set_error(nfs->rpc, "Invalid path %s", oldpath); |
| 3217 | free_nfs_rename_data(rename_data); |
| 3218 | return -1; |
| 3219 | } |
| 3220 | *ptr = 0; |
| 3221 | ptr++; |
| 3222 | rename_data->oldobject = ptr; |
| 3223 | |
| 3224 | |
| 3225 | rename_data->newpath = strdup(newpath); |
| 3226 | if (rename_data->newpath == NULL) { |
| 3227 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath"); |
| 3228 | free_nfs_rename_data(rename_data); |
| 3229 | return -1; |
| 3230 | } |
| 3231 | ptr = strrchr(rename_data->newpath, '/'); |
| 3232 | if (ptr == NULL) { |
| 3233 | rpc_set_error(nfs->rpc, "Invalid path %s", newpath); |
| 3234 | free_nfs_rename_data(rename_data); |
| 3235 | return -1; |
| 3236 | } |
| 3237 | *ptr = 0; |
| 3238 | ptr++; |
| 3239 | rename_data->newobject = ptr; |
| 3240 | |
| 3241 | |
| 3242 | if (nfs_lookuppath_async(nfs, rename_data->oldpath, cb, private_data, nfs_rename_continue_1_internal, rename_data, free_nfs_rename_data, 0) != 0) { |
| 3243 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3244 | return -1; |
| 3245 | } |
| 3246 | |
| 3247 | return 0; |
| 3248 | } |
| 3249 | |
| 3250 | |
| 3251 | /* |
| 3252 | * Async link() |
| 3253 | */ |
| 3254 | struct nfs_link_data { |
| 3255 | char *oldpath; |
| 3256 | struct nfs_fh3 oldfh; |
| 3257 | char *newpath; |
| 3258 | char *newobject; |
| 3259 | struct nfs_fh3 newdir; |
| 3260 | }; |
| 3261 | |
| 3262 | static void free_nfs_link_data(void *mem) |
| 3263 | { |
| 3264 | struct nfs_link_data *data = mem; |
| 3265 | |
| 3266 | if (data->oldpath != NULL) { |
| 3267 | free(data->oldpath); |
| 3268 | } |
| 3269 | if (data->oldfh.data.data_val != NULL) { |
| 3270 | free(data->oldfh.data.data_val); |
| 3271 | } |
| 3272 | if (data->newpath != NULL) { |
| 3273 | free(data->newpath); |
| 3274 | } |
| 3275 | if (data->newdir.data.data_val != NULL) { |
| 3276 | free(data->newdir.data.data_val); |
| 3277 | } |
| 3278 | free(data); |
| 3279 | } |
| 3280 | |
| 3281 | static void nfs_link_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3282 | { |
| 3283 | LINK3res *res; |
| 3284 | struct nfs_cb_data *data = private_data; |
| 3285 | struct nfs_context *nfs = data->nfs; |
| 3286 | struct nfs_link_data *link_data = data->continue_data; |
| 3287 | |
| 3288 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3289 | |
| 3290 | if (status == RPC_STATUS_ERROR) { |
| 3291 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3292 | free_nfs_cb_data(data); |
| 3293 | return; |
| 3294 | } |
| 3295 | if (status == RPC_STATUS_CANCEL) { |
| 3296 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3297 | free_nfs_cb_data(data); |
| 3298 | return; |
| 3299 | } |
| 3300 | |
| 3301 | res = command_data; |
| 3302 | if (res->status != NFS3_OK) { |
| 3303 | rpc_set_error(nfs->rpc, "NFS: LINK %s -> %s/%s failed with %s(%d)", link_data->oldpath, link_data->newpath, link_data->newobject, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 3304 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3305 | free_nfs_cb_data(data); |
| 3306 | return; |
| 3307 | } |
| 3308 | |
| 3309 | data->cb(0, nfs, NULL, data->private_data); |
| 3310 | free_nfs_cb_data(data); |
| 3311 | } |
| 3312 | |
| 3313 | static int nfs_link_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 3314 | { |
| 3315 | struct nfs_link_data *link_data = data->continue_data; |
| 3316 | |
| 3317 | /* steal the filehandle */ |
| 3318 | link_data->newdir.data.data_len = data->fh.data.data_len; |
| 3319 | link_data->newdir.data.data_val = data->fh.data.data_val; |
| 3320 | data->fh.data.data_val = NULL; |
| 3321 | |
| 3322 | if (rpc_nfs_link_async(nfs->rpc, nfs_link_cb, &link_data->oldfh, &link_data->newdir, link_data->newobject, data) != 0) { |
| 3323 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path); |
| 3324 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3325 | free_nfs_cb_data(data); |
| 3326 | return -1; |
| 3327 | } |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
| 3331 | |
| 3332 | static int nfs_link_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data) |
| 3333 | { |
| 3334 | struct nfs_link_data *link_data = data->continue_data; |
| 3335 | |
| 3336 | /* steal the filehandle */ |
| 3337 | link_data->oldfh.data.data_len = data->fh.data.data_len; |
| 3338 | link_data->oldfh.data.data_val = data->fh.data.data_val; |
| 3339 | data->fh.data.data_val = NULL; |
| 3340 | |
| 3341 | if (nfs_lookuppath_async(nfs, link_data->newpath, data->cb, data->private_data, nfs_link_continue_2_internal, link_data, free_nfs_link_data, 0) != 0) { |
| 3342 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath); |
| 3343 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3344 | free_nfs_cb_data(data); |
| 3345 | return -1; |
| 3346 | } |
| 3347 | data->continue_data = NULL; |
| 3348 | free_nfs_cb_data(data); |
| 3349 | |
| 3350 | return 0; |
| 3351 | } |
| 3352 | |
| 3353 | |
| 3354 | int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) |
| 3355 | { |
| 3356 | char *ptr; |
| 3357 | struct nfs_link_data *link_data; |
| 3358 | |
| 3359 | link_data = malloc(sizeof(struct nfs_link_data)); |
| 3360 | if (link_data == NULL) { |
| 3361 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data"); |
| 3362 | return -1; |
| 3363 | } |
| 3364 | memset(link_data, 0, sizeof(struct nfs_link_data)); |
| 3365 | |
| 3366 | link_data->oldpath = strdup(oldpath); |
| 3367 | if (link_data->oldpath == NULL) { |
| 3368 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
| 3369 | free_nfs_link_data(link_data); |
| 3370 | return -1; |
| 3371 | } |
| 3372 | |
| 3373 | link_data->newpath = strdup(newpath); |
| 3374 | if (link_data->newpath == NULL) { |
| 3375 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath"); |
| 3376 | free_nfs_link_data(link_data); |
| 3377 | return -1; |
| 3378 | } |
| 3379 | ptr = strrchr(link_data->newpath, '/'); |
| 3380 | if (ptr == NULL) { |
| 3381 | rpc_set_error(nfs->rpc, "Invalid path %s", newpath); |
| 3382 | free_nfs_link_data(link_data); |
| 3383 | return -1; |
| 3384 | } |
| 3385 | *ptr = 0; |
| 3386 | ptr++; |
| 3387 | link_data->newobject = ptr; |
| 3388 | |
| 3389 | |
| 3390 | if (nfs_lookuppath_async(nfs, link_data->oldpath, cb, private_data, nfs_link_continue_1_internal, link_data, free_nfs_link_data, 0) != 0) { |
| 3391 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3392 | return -1; |
| 3393 | } |
| 3394 | |
| 3395 | return 0; |
| 3396 | } |
| 3397 | |
| 3398 | |
| 3399 | //qqq replace later with lseek() |
| 3400 | uint64_t nfs_get_current_offset(struct nfsfh *nfsfh) |
| 3401 | { |
| 3402 | return nfsfh->offset; |
| 3403 | } |
| 3404 | |
| 3405 | |
| 3406 | |
| 3407 | /* |
| 3408 | * Get the maximum supported READ3 size by the server |
| 3409 | */ |
| 3410 | uint64_t nfs_get_readmax(struct nfs_context *nfs) |
| 3411 | { |
| 3412 | return nfs->readmax; |
| 3413 | } |
| 3414 | |
| 3415 | /* |
| 3416 | * Get the maximum supported WRITE3 size by the server |
| 3417 | */ |
| 3418 | uint64_t nfs_get_writemax(struct nfs_context *nfs) |
| 3419 | { |
| 3420 | /* Some XDR libraries can not marshall PDUs bigger than this */ |
| 3421 | if (nfs->writemax < 32768) { |
| 3422 | return nfs->writemax; |
| 3423 | } |
| 3424 | return 32768; |
| 3425 | } |
| 3426 | |
| 3427 | void nfs_set_error(struct nfs_context *nfs, char *error_string, ...) |
| 3428 | { |
| 3429 | va_list ap; |
| 3430 | char *str = NULL; |
| 3431 | |
| 3432 | va_start(ap, error_string); |
| 3433 | str = malloc(1024); |
| 3434 | vsnprintf(str, 1024, error_string, ap); |
| 3435 | if (nfs->rpc->error_string != NULL) { |
| 3436 | free(nfs->rpc->error_string); |
| 3437 | } |
| 3438 | nfs->rpc->error_string = str; |
| 3439 | va_end(ap); |
| 3440 | } |
| 3441 | |
| 3442 | |
| 3443 | |
| 3444 | struct mount_cb_data { |
| 3445 | rpc_cb cb; |
| 3446 | void *private_data; |
| 3447 | char *server; |
| 3448 | }; |
| 3449 | |
| 3450 | static void free_mount_cb_data(struct mount_cb_data *data) |
| 3451 | { |
| 3452 | if (data->server != NULL) { |
| 3453 | free(data->server); |
| 3454 | data->server = NULL; |
| 3455 | } |
| 3456 | |
| 3457 | free(data); |
| 3458 | } |
| 3459 | |
| 3460 | static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3461 | { |
| 3462 | struct mount_cb_data *data = private_data; |
| 3463 | |
| 3464 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3465 | |
| 3466 | if (status == RPC_STATUS_ERROR) { |
| 3467 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
| 3468 | free_mount_cb_data(data); |
| 3469 | return; |
| 3470 | } |
| 3471 | if (status == RPC_STATUS_CANCEL) { |
| 3472 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); |
| 3473 | free_mount_cb_data(data); |
| 3474 | return; |
| 3475 | } |
| 3476 | |
| 3477 | data->cb(rpc, 0, command_data, data->private_data); |
| 3478 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { |
| 3479 | rpc_set_error(rpc, "Failed to disconnect\n"); |
| 3480 | } |
| 3481 | free_mount_cb_data(data); |
| 3482 | } |
| 3483 | |
| 3484 | static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3485 | { |
| 3486 | struct mount_cb_data *data = private_data; |
| 3487 | |
| 3488 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3489 | |
| 3490 | /* Dont want any more callbacks even if the socket is closed */ |
| 3491 | rpc->connect_cb = NULL; |
| 3492 | |
| 3493 | if (status == RPC_STATUS_ERROR) { |
| 3494 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
| 3495 | free_mount_cb_data(data); |
| 3496 | return; |
| 3497 | } |
| 3498 | if (status == RPC_STATUS_CANCEL) { |
| 3499 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); |
| 3500 | free_mount_cb_data(data); |
| 3501 | return; |
| 3502 | } |
| 3503 | |
| 3504 | if (rpc_mount_export_async(rpc, mount_export_5_cb, data) != 0) { |
| 3505 | data->cb(rpc, -ENOMEM, command_data, data->private_data); |
| 3506 | free_mount_cb_data(data); |
| 3507 | return; |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3512 | { |
| 3513 | struct mount_cb_data *data = private_data; |
| 3514 | uint32_t mount_port; |
| 3515 | |
| 3516 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3517 | |
| 3518 | if (status == RPC_STATUS_ERROR) { |
| 3519 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
| 3520 | free_mount_cb_data(data); |
| 3521 | return; |
| 3522 | } |
| 3523 | if (status == RPC_STATUS_CANCEL) { |
| 3524 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); |
| 3525 | free_mount_cb_data(data); |
| 3526 | return; |
| 3527 | } |
| 3528 | |
| 3529 | mount_port = *(uint32_t *)command_data; |
| 3530 | if (mount_port == 0) { |
| 3531 | rpc_set_error(rpc, "RPC error. Mount program is not available"); |
| 3532 | data->cb(rpc, -ENOENT, command_data, data->private_data); |
| 3533 | free_mount_cb_data(data); |
| 3534 | return; |
| 3535 | } |
| 3536 | |
| 3537 | rpc_disconnect(rpc, "normal disconnect"); |
| 3538 | if (rpc_connect_async(rpc, data->server, mount_port, mount_export_4_cb, data) != 0) { |
| 3539 | data->cb(rpc, -ENOMEM, command_data, data->private_data); |
| 3540 | free_mount_cb_data(data); |
| 3541 | return; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3546 | { |
| 3547 | struct mount_cb_data *data = private_data; |
| 3548 | |
| 3549 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3550 | |
| 3551 | if (status == RPC_STATUS_ERROR) { |
| 3552 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
| 3553 | free_mount_cb_data(data); |
| 3554 | return; |
| 3555 | } |
| 3556 | if (status == RPC_STATUS_CANCEL) { |
| 3557 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); |
| 3558 | free_mount_cb_data(data); |
| 3559 | return; |
| 3560 | } |
| 3561 | |
| 3562 | if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, mount_export_3_cb, private_data) != 0) { |
| 3563 | data->cb(rpc, -ENOMEM, command_data, data->private_data); |
| 3564 | free_mount_cb_data(data); |
| 3565 | return; |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3570 | { |
| 3571 | struct mount_cb_data *data = private_data; |
| 3572 | |
| 3573 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3574 | |
| 3575 | /* Dont want any more callbacks even if the socket is closed */ |
| 3576 | rpc->connect_cb = NULL; |
| 3577 | |
| 3578 | if (status == RPC_STATUS_ERROR) { |
| 3579 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
| 3580 | free_mount_cb_data(data); |
| 3581 | return; |
| 3582 | } |
| 3583 | if (status == RPC_STATUS_CANCEL) { |
| 3584 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); |
| 3585 | free_mount_cb_data(data); |
| 3586 | return; |
| 3587 | } |
| 3588 | |
| 3589 | if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) { |
| 3590 | data->cb(rpc, -ENOMEM, command_data, data->private_data); |
| 3591 | free_mount_cb_data(data); |
| 3592 | return; |
| 3593 | } |
| 3594 | } |
| 3595 | |
| 3596 | int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data) |
| 3597 | { |
| 3598 | struct mount_cb_data *data; |
| 3599 | |
| 3600 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3601 | |
| 3602 | data = malloc(sizeof(struct mount_cb_data)); |
| 3603 | if (data == NULL) { |
| 3604 | return -1; |
| 3605 | } |
| 3606 | memset(data, 0, sizeof(struct mount_cb_data)); |
| 3607 | data->cb = cb; |
| 3608 | data->private_data = private_data; |
| 3609 | data->server = strdup(server); |
| 3610 | if (data->server == NULL) { |
| 3611 | free_mount_cb_data(data); |
| 3612 | return -1; |
| 3613 | } |
| 3614 | if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) { |
| 3615 | free_mount_cb_data(data); |
| 3616 | return -1; |
| 3617 | } |
| 3618 | |
| 3619 | return 0; |
| 3620 | } |
| 3621 | |
| 3622 | struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs) |
| 3623 | { |
| 3624 | assert(nfs->rpc->magic == RPC_CONTEXT_MAGIC); |
| 3625 | return nfs->rpc; |
| 3626 | } |
| 3627 | |
| 3628 | const char *nfs_get_server(struct nfs_context *nfs) { |
| 3629 | return nfs->server; |
| 3630 | } |
| 3631 | |
| 3632 | const char *nfs_get_export(struct nfs_context *nfs) { |
| 3633 | return nfs->export; |
| 3634 | } |
| 3635 | |
| 3636 | const struct nfs_fh3 *nfs_get_rootfh(struct nfs_context *nfs) { |
| 3637 | return &nfs->rootfh; |
| 3638 | } |
| 3639 | |
| 3640 | struct nfs_fh3 *nfs_get_fh(struct nfsfh *nfsfh) { |
| 3641 | return &nfsfh->fh; |
| 3642 | } |