| 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 HAVE_CONFIG_H |
| 21 | #include "config.h" |
| 22 | #endif |
| 23 | |
| 24 | #ifdef AROS |
| 25 | #include "aros_compat.h" |
| 26 | #endif |
| 27 | |
| 28 | #ifdef WIN32 |
| 29 | #include "win32_compat.h" |
| 30 | #endif |
| 31 | |
| 32 | #ifdef HAVE_UTIME_H |
| 33 | #include <utime.h> |
| 34 | #endif |
| 35 | |
| 36 | #ifdef ANDROID |
| 37 | #define statvfs statfs |
| 38 | #endif |
| 39 | |
| 40 | #define _GNU_SOURCE |
| 41 | |
| 42 | #ifdef HAVE_UNISTD_H |
| 43 | #include <unistd.h> |
| 44 | #endif |
| 45 | |
| 46 | #ifdef HAVE_SYS_VFS_H |
| 47 | #include <sys/vfs.h> |
| 48 | #endif |
| 49 | |
| 50 | #ifdef HAVE_SYS_STATVFS_H |
| 51 | #include <sys/statvfs.h> |
| 52 | #endif |
| 53 | |
| 54 | #ifdef HAVE_NETINET_IN_H |
| 55 | #include <netinet/in.h> |
| 56 | #endif |
| 57 | |
| 58 | #ifdef HAVE_STRINGS_H |
| 59 | #include <strings.h> |
| 60 | #endif |
| 61 | |
| 62 | #ifdef MAJOR_IN_MKDEV |
| 63 | #include <sys/mkdev.h> |
| 64 | #endif |
| 65 | |
| 66 | #ifdef MAJOR_IN_SYSMACROS |
| 67 | #include <sys/sysmacros.h> |
| 68 | #endif |
| 69 | |
| 70 | #include <stdio.h> |
| 71 | #include <stdarg.h> |
| 72 | #include <stdlib.h> |
| 73 | #include <string.h> |
| 74 | #include <assert.h> |
| 75 | #include <errno.h> |
| 76 | #include <time.h> |
| 77 | #include <sys/types.h> |
| 78 | #include <sys/stat.h> |
| 79 | #include <fcntl.h> |
| 80 | #include "libnfs-zdr.h" |
| 81 | #include "slist.h" |
| 82 | #include "libnfs.h" |
| 83 | #include "libnfs-raw.h" |
| 84 | #include "libnfs-raw-mount.h" |
| 85 | #include "libnfs-raw-nfs.h" |
| 86 | #include "libnfs-raw-portmap.h" |
| 87 | #include "libnfs-private.h" |
| 88 | |
| 89 | #define MAX_DIR_CACHE 128 |
| 90 | #define MAX_LINK_COUNT 40 |
| 91 | |
| 92 | struct nfsdir { |
| 93 | struct nfs_fh3 fh; |
| 94 | fattr3 attr; |
| 95 | struct nfsdir *next; |
| 96 | |
| 97 | struct nfsdirent *entries; |
| 98 | struct nfsdirent *current; |
| 99 | }; |
| 100 | |
| 101 | struct nfs_readahead { |
| 102 | uint64_t fh_offset; |
| 103 | uint64_t last_offset; |
| 104 | uint64_t buf_offset; |
| 105 | uint64_t buf_count; |
| 106 | time_t buf_ts; |
| 107 | void *buf; |
| 108 | uint32_t cur_ra; |
| 109 | }; |
| 110 | |
| 111 | struct nfsfh { |
| 112 | struct nfs_fh3 fh; |
| 113 | int is_sync; |
| 114 | int is_append; |
| 115 | uint64_t offset; |
| 116 | struct nfs_readahead ra; |
| 117 | }; |
| 118 | |
| 119 | struct nfs_context { |
| 120 | struct rpc_context *rpc; |
| 121 | char *server; |
| 122 | char *export; |
| 123 | struct nfs_fh3 rootfh; |
| 124 | uint64_t readmax; |
| 125 | uint64_t writemax; |
| 126 | char *cwd; |
| 127 | struct nfsdir *dircache; |
| 128 | }; |
| 129 | |
| 130 | void nfs_free_nfsdir(struct nfsdir *nfsdir) |
| 131 | { |
| 132 | while (nfsdir->entries) { |
| 133 | struct nfsdirent *dirent = nfsdir->entries->next; |
| 134 | if (nfsdir->entries->name != NULL) { |
| 135 | free(nfsdir->entries->name); |
| 136 | } |
| 137 | free(nfsdir->entries); |
| 138 | nfsdir->entries = dirent; |
| 139 | } |
| 140 | free(nfsdir->fh.data.data_val); |
| 141 | free(nfsdir); |
| 142 | } |
| 143 | |
| 144 | static void nfs_dircache_add(struct nfs_context *nfs, struct nfsdir *nfsdir) |
| 145 | { |
| 146 | int i; |
| 147 | LIBNFS_LIST_ADD(&nfs->dircache, nfsdir); |
| 148 | |
| 149 | for (nfsdir = nfs->dircache, i = 0; nfsdir; nfsdir = nfsdir->next, i++) { |
| 150 | if (i > MAX_DIR_CACHE) { |
| 151 | LIBNFS_LIST_REMOVE(&nfs->dircache, nfsdir); |
| 152 | nfs_free_nfsdir(nfsdir); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static struct nfsdir *nfs_dircache_find(struct nfs_context *nfs, struct nfs_fh3 *fh) |
| 159 | { |
| 160 | struct nfsdir *nfsdir; |
| 161 | |
| 162 | for (nfsdir = nfs->dircache; nfsdir; nfsdir = nfsdir->next) { |
| 163 | if (nfsdir->fh.data.data_len == fh->data.data_len && |
| 164 | !memcmp(nfsdir->fh.data.data_val, fh->data.data_val, fh->data.data_len)) { |
| 165 | LIBNFS_LIST_REMOVE(&nfs->dircache, nfsdir); |
| 166 | return nfsdir; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | struct nfs_cb_data; |
| 174 | typedef int (*continue_func)(struct nfs_context *nfs, fattr3 *attr, |
| 175 | struct nfs_cb_data *data); |
| 176 | |
| 177 | struct nfs_cb_data { |
| 178 | struct nfs_context *nfs; |
| 179 | struct nfsfh *nfsfh; |
| 180 | char *saved_path, *path; |
| 181 | int link_count, no_follow; |
| 182 | |
| 183 | nfs_cb cb; |
| 184 | void *private_data; |
| 185 | |
| 186 | continue_func continue_cb; |
| 187 | void *continue_data; |
| 188 | void (*free_continue_data)(void *); |
| 189 | int continue_int; |
| 190 | |
| 191 | struct nfs_fh3 fh; |
| 192 | |
| 193 | /* for multi-read/write calls. */ |
| 194 | int error; |
| 195 | int cancel; |
| 196 | int oom; |
| 197 | int num_calls; |
| 198 | uint64_t offset, count, max_offset, org_offset, org_count; |
| 199 | char *buffer; |
| 200 | char *usrbuf; |
| 201 | }; |
| 202 | |
| 203 | struct nfs_mcb_data { |
| 204 | struct nfs_cb_data *data; |
| 205 | uint64_t offset; |
| 206 | uint64_t count; |
| 207 | int update_pos; |
| 208 | }; |
| 209 | |
| 210 | static int nfs_lookup_path_async_internal(struct nfs_context *nfs, fattr3 *attr, struct nfs_cb_data *data, struct nfs_fh3 *fh); |
| 211 | static int nfs_normalize_path(struct nfs_context *nfs, char *path); |
| 212 | |
| 213 | void nfs_set_auth(struct nfs_context *nfs, struct AUTH *auth) |
| 214 | { |
| 215 | rpc_set_auth(nfs->rpc, auth); |
| 216 | } |
| 217 | |
| 218 | int nfs_get_fd(struct nfs_context *nfs) |
| 219 | { |
| 220 | return rpc_get_fd(nfs->rpc); |
| 221 | } |
| 222 | |
| 223 | int nfs_queue_length(struct nfs_context *nfs) |
| 224 | { |
| 225 | return rpc_queue_length(nfs->rpc); |
| 226 | } |
| 227 | |
| 228 | int nfs_which_events(struct nfs_context *nfs) |
| 229 | { |
| 230 | return rpc_which_events(nfs->rpc); |
| 231 | } |
| 232 | |
| 233 | int nfs_service(struct nfs_context *nfs, int revents) |
| 234 | { |
| 235 | return rpc_service(nfs->rpc, revents); |
| 236 | } |
| 237 | |
| 238 | char *nfs_get_error(struct nfs_context *nfs) |
| 239 | { |
| 240 | return rpc_get_error(nfs->rpc); |
| 241 | }; |
| 242 | |
| 243 | static int nfs_set_context_args(struct nfs_context *nfs, char *arg, char *val) |
| 244 | { |
| 245 | if (!strcmp(arg, "tcp-syncnt")) { |
| 246 | rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs), atoi(val)); |
| 247 | } else if (!strcmp(arg, "uid")) { |
| 248 | rpc_set_uid(nfs_get_rpc_context(nfs), atoi(val)); |
| 249 | } else if (!strcmp(arg, "gid")) { |
| 250 | rpc_set_gid(nfs_get_rpc_context(nfs), atoi(val)); |
| 251 | } else if (!strcmp(arg, "readahaed")) { |
| 252 | rpc_set_readahead(nfs_get_rpc_context(nfs), atoi(val)); |
| 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static struct nfs_url *nfs_parse_url(struct nfs_context *nfs, const char *url, int dir, int incomplete) |
| 258 | { |
| 259 | struct nfs_url *urls; |
| 260 | char *strp, *flagsp, *strp2; |
| 261 | |
| 262 | if (strncmp(url, "nfs://", 6)) { |
| 263 | rpc_set_error(nfs->rpc, "Invalid URL specified"); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | urls = malloc(sizeof(struct nfs_url)); |
| 268 | if (urls == NULL) { |
| 269 | rpc_set_error(nfs->rpc, "Out of memory"); |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | memset(urls, 0x00, sizeof(struct nfs_url)); |
| 274 | urls->server = strdup(url + 6); |
| 275 | if (urls->server == NULL) { |
| 276 | nfs_destroy_url(urls); |
| 277 | rpc_set_error(nfs->rpc, "Out of memory"); |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | if (urls->server[0] == '/' || urls->server[0] == '\0' || |
| 282 | urls->server[0] == '?') { |
| 283 | if (incomplete) { |
| 284 | flagsp = strchr(urls->server, '?'); |
| 285 | goto flags; |
| 286 | } |
| 287 | nfs_destroy_url(urls); |
| 288 | rpc_set_error(nfs->rpc, "Invalid server string"); |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | strp = strchr(urls->server, '/'); |
| 293 | if (strp == NULL) { |
| 294 | if (incomplete) { |
| 295 | flagsp = strchr(urls->server, '?'); |
| 296 | goto flags; |
| 297 | } |
| 298 | nfs_destroy_url(urls); |
| 299 | rpc_set_error(nfs->rpc, "Incomplete or invalid URL specified."); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | urls->path = strdup(strp); |
| 304 | if (urls->path == NULL) { |
| 305 | nfs_destroy_url(urls); |
| 306 | rpc_set_error(nfs->rpc, "Out of memory"); |
| 307 | return NULL; |
| 308 | } |
| 309 | *strp = 0; |
| 310 | |
| 311 | if (dir) { |
| 312 | flagsp = strchr(urls->path, '?'); |
| 313 | goto flags; |
| 314 | } |
| 315 | |
| 316 | strp = strrchr(urls->path, '/'); |
| 317 | if (strp == NULL) { |
| 318 | if (incomplete) { |
| 319 | flagsp = strchr(urls->path, '?'); |
| 320 | goto flags; |
| 321 | } |
| 322 | nfs_destroy_url(urls); |
| 323 | rpc_set_error(nfs->rpc, "Incomplete or invalid URL specified."); |
| 324 | return NULL; |
| 325 | } |
| 326 | urls->file = strdup(strp); |
| 327 | if (urls->path == NULL) { |
| 328 | nfs_destroy_url(urls); |
| 329 | rpc_set_error(nfs->rpc, "Out of memory"); |
| 330 | return NULL; |
| 331 | } |
| 332 | *strp = 0; |
| 333 | flagsp = strchr(urls->file, '?'); |
| 334 | |
| 335 | flags: |
| 336 | if (flagsp) { |
| 337 | *flagsp = 0; |
| 338 | } |
| 339 | |
| 340 | if (urls->file && !strlen(urls->file)) { |
| 341 | free(urls->file); |
| 342 | urls->file = NULL; |
| 343 | if (!incomplete) { |
| 344 | nfs_destroy_url(urls); |
| 345 | rpc_set_error(nfs->rpc, "Incomplete or invalid URL specified."); |
| 346 | return NULL; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | while (flagsp != NULL && *(flagsp+1) != 0) { |
| 351 | strp = flagsp + 1; |
| 352 | flagsp = strchr(strp, '&'); |
| 353 | if (flagsp) { |
| 354 | *flagsp = 0; |
| 355 | } |
| 356 | strp2 = strchr(strp, '='); |
| 357 | if (strp2) { |
| 358 | *strp2 = 0; |
| 359 | strp2++; |
| 360 | nfs_set_context_args(nfs, strp, strp2); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (urls->server && strlen(urls->server) <= 1) { |
| 365 | free(urls->server); |
| 366 | urls->server = NULL; |
| 367 | } |
| 368 | |
| 369 | return urls; |
| 370 | } |
| 371 | |
| 372 | struct nfs_url *nfs_parse_url_full(struct nfs_context *nfs, const char *url) |
| 373 | { |
| 374 | return nfs_parse_url(nfs, url, 0, 0); |
| 375 | } |
| 376 | |
| 377 | struct nfs_url *nfs_parse_url_dir(struct nfs_context *nfs, const char *url) |
| 378 | { |
| 379 | return nfs_parse_url(nfs, url, 1, 0); |
| 380 | } |
| 381 | |
| 382 | struct nfs_url *nfs_parse_url_incomplete(struct nfs_context *nfs, const char *url) |
| 383 | { |
| 384 | return nfs_parse_url(nfs, url, 0, 1); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | void nfs_destroy_url(struct nfs_url *url) |
| 389 | { |
| 390 | if (url) { |
| 391 | free(url->server); |
| 392 | free(url->path); |
| 393 | free(url->file); |
| 394 | } |
| 395 | free(url); |
| 396 | } |
| 397 | |
| 398 | struct nfs_context *nfs_init_context(void) |
| 399 | { |
| 400 | struct nfs_context *nfs; |
| 401 | |
| 402 | nfs = malloc(sizeof(struct nfs_context)); |
| 403 | if (nfs == NULL) { |
| 404 | return NULL; |
| 405 | } |
| 406 | memset(nfs, 0, sizeof(struct nfs_context)); |
| 407 | |
| 408 | nfs->rpc = rpc_init_context(); |
| 409 | if (nfs->rpc == NULL) { |
| 410 | free(nfs); |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | nfs->cwd = strdup("/"); |
| 415 | |
| 416 | return nfs; |
| 417 | } |
| 418 | |
| 419 | void nfs_destroy_context(struct nfs_context *nfs) |
| 420 | { |
| 421 | rpc_destroy_context(nfs->rpc); |
| 422 | nfs->rpc = NULL; |
| 423 | |
| 424 | if (nfs->server) { |
| 425 | free(nfs->server); |
| 426 | nfs->server = NULL; |
| 427 | } |
| 428 | |
| 429 | if (nfs->export) { |
| 430 | free(nfs->export); |
| 431 | nfs->export = NULL; |
| 432 | } |
| 433 | |
| 434 | if (nfs->cwd) { |
| 435 | free(nfs->cwd); |
| 436 | nfs->cwd = NULL; |
| 437 | } |
| 438 | |
| 439 | if (nfs->rootfh.data.data_val != NULL) { |
| 440 | free(nfs->rootfh.data.data_val); |
| 441 | nfs->rootfh.data.data_val = NULL; |
| 442 | } |
| 443 | |
| 444 | while (nfs->dircache) { |
| 445 | struct nfsdir *nfsdir = nfs->dircache; |
| 446 | LIBNFS_LIST_REMOVE(&nfs->dircache, nfsdir); |
| 447 | nfs_free_nfsdir(nfsdir); |
| 448 | } |
| 449 | |
| 450 | free(nfs); |
| 451 | } |
| 452 | |
| 453 | struct rpc_cb_data { |
| 454 | char *server; |
| 455 | uint32_t program; |
| 456 | uint32_t version; |
| 457 | |
| 458 | rpc_cb cb; |
| 459 | void *private_data; |
| 460 | }; |
| 461 | |
| 462 | void free_rpc_cb_data(struct rpc_cb_data *data) |
| 463 | { |
| 464 | free(data->server); |
| 465 | data->server = NULL; |
| 466 | free(data); |
| 467 | } |
| 468 | |
| 469 | static void rpc_connect_program_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 470 | { |
| 471 | struct rpc_cb_data *data = private_data; |
| 472 | |
| 473 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 474 | |
| 475 | /* Dont want any more callbacks even if the socket is closed */ |
| 476 | rpc->connect_cb = NULL; |
| 477 | |
| 478 | if (status == RPC_STATUS_ERROR) { |
| 479 | data->cb(rpc, status, command_data, data->private_data); |
| 480 | free_rpc_cb_data(data); |
| 481 | return; |
| 482 | } |
| 483 | if (status == RPC_STATUS_CANCEL) { |
| 484 | data->cb(rpc, status, "Command was cancelled", data->private_data); |
| 485 | free_rpc_cb_data(data); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | data->cb(rpc, status, NULL, data->private_data); |
| 490 | free_rpc_cb_data(data); |
| 491 | } |
| 492 | |
| 493 | static void rpc_connect_program_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 494 | { |
| 495 | struct rpc_cb_data *data = private_data; |
| 496 | |
| 497 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 498 | |
| 499 | /* Dont want any more callbacks even if the socket is closed */ |
| 500 | rpc->connect_cb = NULL; |
| 501 | |
| 502 | if (status == RPC_STATUS_ERROR) { |
| 503 | data->cb(rpc, status, command_data, data->private_data); |
| 504 | free_rpc_cb_data(data); |
| 505 | return; |
| 506 | } |
| 507 | if (status == RPC_STATUS_CANCEL) { |
| 508 | data->cb(rpc, status, "Command was cancelled", data->private_data); |
| 509 | free_rpc_cb_data(data); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | switch (data->program) { |
| 514 | case MOUNT_PROGRAM: |
| 515 | if (rpc_mount3_null_async(rpc, rpc_connect_program_5_cb, |
| 516 | data) != 0) { |
| 517 | data->cb(rpc, status, command_data, data->private_data); |
| 518 | free_rpc_cb_data(data); |
| 519 | return; |
| 520 | } |
| 521 | return; |
| 522 | case NFS_PROGRAM: |
| 523 | if (rpc_nfs3_null_async(rpc, rpc_connect_program_5_cb, |
| 524 | data) != 0) { |
| 525 | data->cb(rpc, status, command_data, data->private_data); |
| 526 | free_rpc_cb_data(data); |
| 527 | return; |
| 528 | } |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | data->cb(rpc, status, NULL, data->private_data); |
| 533 | free_rpc_cb_data(data); |
| 534 | } |
| 535 | |
| 536 | static void rpc_connect_program_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 537 | { |
| 538 | struct rpc_cb_data *data = private_data; |
| 539 | struct pmap3_string_result *gar; |
| 540 | uint32_t rpc_port = 0; |
| 541 | unsigned char *ptr; |
| 542 | |
| 543 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 544 | |
| 545 | if (status == RPC_STATUS_ERROR) { |
| 546 | data->cb(rpc, status, command_data, data->private_data); |
| 547 | free_rpc_cb_data(data); |
| 548 | return; |
| 549 | } |
| 550 | if (status == RPC_STATUS_CANCEL) { |
| 551 | data->cb(rpc, status, "Command was cancelled", data->private_data); |
| 552 | free_rpc_cb_data(data); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | switch (rpc->s.ss_family) { |
| 557 | case AF_INET: |
| 558 | rpc_port = *(uint32_t *)command_data; |
| 559 | break; |
| 560 | case AF_INET6: |
| 561 | /* ouch. portmapper and ipv6 are not great */ |
| 562 | gar = command_data; |
| 563 | if (gar->addr == NULL) { |
| 564 | break; |
| 565 | } |
| 566 | ptr = strrchr(gar->addr, '.'); |
| 567 | if (ptr == NULL) { |
| 568 | break; |
| 569 | } |
| 570 | rpc_port = atoi(ptr + 1); |
| 571 | *ptr = 0; |
| 572 | ptr = strrchr(gar->addr, '.'); |
| 573 | if (ptr == NULL) { |
| 574 | break; |
| 575 | } |
| 576 | rpc_port += 256 * atoi(ptr + 1); |
| 577 | break; |
| 578 | } |
| 579 | if (rpc_port == 0) { |
| 580 | rpc_set_error(rpc, "RPC error. Program is not available on %s", data->server); |
| 581 | data->cb(rpc, RPC_STATUS_ERROR, rpc_get_error(rpc), data->private_data); |
| 582 | free_rpc_cb_data(data); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | rpc_disconnect(rpc, "normal disconnect"); |
| 587 | if (rpc_connect_async(rpc, data->server, rpc_port, rpc_connect_program_4_cb, data) != 0) { |
| 588 | data->cb(rpc, status, command_data, data->private_data); |
| 589 | free_rpc_cb_data(data); |
| 590 | return; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | static void rpc_connect_program_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 595 | { |
| 596 | struct rpc_cb_data *data = private_data; |
| 597 | struct pmap3_mapping map; |
| 598 | |
| 599 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 600 | |
| 601 | if (status == RPC_STATUS_ERROR) { |
| 602 | data->cb(rpc, status, command_data, data->private_data); |
| 603 | free_rpc_cb_data(data); |
| 604 | return; |
| 605 | } |
| 606 | if (status == RPC_STATUS_CANCEL) { |
| 607 | data->cb(rpc, status, "Command was cancelled", data->private_data); |
| 608 | free_rpc_cb_data(data); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | switch (rpc->s.ss_family) { |
| 613 | case AF_INET: |
| 614 | if (rpc_pmap2_getport_async(rpc, data->program, data->version, IPPROTO_TCP, rpc_connect_program_3_cb, private_data) != 0) { |
| 615 | data->cb(rpc, status, command_data, data->private_data); |
| 616 | free_rpc_cb_data(data); |
| 617 | return; |
| 618 | } |
| 619 | break; |
| 620 | case AF_INET6: |
| 621 | map.prog=data->program; |
| 622 | map.vers=data->version; |
| 623 | map.netid=""; |
| 624 | map.addr=""; |
| 625 | map.owner=""; |
| 626 | if (rpc_pmap3_getaddr_async(rpc, &map, rpc_connect_program_3_cb, private_data) != 0) { |
| 627 | data->cb(rpc, status, command_data, data->private_data); |
| 628 | free_rpc_cb_data(data); |
| 629 | return; |
| 630 | } |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | static void rpc_connect_program_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 636 | { |
| 637 | struct rpc_cb_data *data = private_data; |
| 638 | |
| 639 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 640 | |
| 641 | /* Dont want any more callbacks even if the socket is closed */ |
| 642 | rpc->connect_cb = NULL; |
| 643 | |
| 644 | if (status == RPC_STATUS_ERROR) { |
| 645 | data->cb(rpc, status, command_data, data->private_data); |
| 646 | free_rpc_cb_data(data); |
| 647 | return; |
| 648 | } |
| 649 | if (status == RPC_STATUS_CANCEL) { |
| 650 | data->cb(rpc, status, "Command was cancelled", data->private_data); |
| 651 | free_rpc_cb_data(data); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | switch (rpc->s.ss_family) { |
| 656 | case AF_INET: |
| 657 | if (rpc_pmap2_null_async(rpc, rpc_connect_program_2_cb, data) != 0) { |
| 658 | data->cb(rpc, status, command_data, data->private_data); |
| 659 | free_rpc_cb_data(data); |
| 660 | return; |
| 661 | } |
| 662 | break; |
| 663 | case AF_INET6: |
| 664 | if (rpc_pmap3_null_async(rpc, rpc_connect_program_2_cb, data) != 0) { |
| 665 | data->cb(rpc, status, command_data, data->private_data); |
| 666 | free_rpc_cb_data(data); |
| 667 | return; |
| 668 | } |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | int rpc_connect_program_async(struct rpc_context *rpc, const char *server, int program, int version, rpc_cb cb, void *private_data) |
| 674 | { |
| 675 | struct rpc_cb_data *data; |
| 676 | |
| 677 | data = malloc(sizeof(struct rpc_cb_data)); |
| 678 | if (data == NULL) { |
| 679 | return -1; |
| 680 | } |
| 681 | memset(data, 0, sizeof(struct rpc_cb_data)); |
| 682 | data->server = strdup(server); |
| 683 | data->program = program; |
| 684 | data->version = version; |
| 685 | |
| 686 | data->cb = cb; |
| 687 | data->private_data = private_data; |
| 688 | |
| 689 | if (rpc_connect_async(rpc, server, 111, rpc_connect_program_1_cb, data) != 0) { |
| 690 | rpc_set_error(rpc, "Failed to start connection"); |
| 691 | free_rpc_cb_data(data); |
| 692 | return -1; |
| 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static void free_nfs_cb_data(struct nfs_cb_data *data) |
| 698 | { |
| 699 | if (data->continue_data != NULL) { |
| 700 | assert(data->free_continue_data); |
| 701 | data->free_continue_data(data->continue_data); |
| 702 | } |
| 703 | |
| 704 | free(data->saved_path); |
| 705 | free(data->fh.data.data_val); |
| 706 | free(data->buffer); |
| 707 | |
| 708 | free(data); |
| 709 | } |
| 710 | |
| 711 | static void free_nfsfh(struct nfsfh *nfsfh) |
| 712 | { |
| 713 | if (nfsfh->fh.data.data_val != NULL) { |
| 714 | free(nfsfh->fh.data.data_val); |
| 715 | nfsfh->fh.data.data_val = NULL; |
| 716 | } |
| 717 | free(nfsfh->ra.buf); |
| 718 | free(nfsfh); |
| 719 | } |
| 720 | |
| 721 | |
| 722 | static void nfs_mount_10_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 723 | { |
| 724 | struct nfs_cb_data *data = private_data; |
| 725 | struct nfs_context *nfs = data->nfs; |
| 726 | |
| 727 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 728 | |
| 729 | if (status == RPC_STATUS_ERROR) { |
| 730 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 731 | free_nfs_cb_data(data); |
| 732 | return; |
| 733 | } |
| 734 | if (status == RPC_STATUS_CANCEL) { |
| 735 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 736 | free_nfs_cb_data(data); |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | data->cb(0, nfs, NULL, data->private_data); |
| 741 | free_nfs_cb_data(data); |
| 742 | } |
| 743 | |
| 744 | static void nfs_mount_9_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 745 | { |
| 746 | struct nfs_cb_data *data = private_data; |
| 747 | struct nfs_context *nfs = data->nfs; |
| 748 | FSINFO3res *res = command_data; |
| 749 | struct GETATTR3args args; |
| 750 | |
| 751 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 752 | |
| 753 | if (status == RPC_STATUS_ERROR) { |
| 754 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 755 | free_nfs_cb_data(data); |
| 756 | return; |
| 757 | } |
| 758 | if (status == RPC_STATUS_CANCEL) { |
| 759 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 760 | free_nfs_cb_data(data); |
| 761 | return; |
| 762 | } |
| 763 | |
| 764 | nfs->readmax = res->FSINFO3res_u.resok.rtmax; |
| 765 | nfs->writemax = res->FSINFO3res_u.resok.wtmax; |
| 766 | |
| 767 | memset(&args, 0, sizeof(GETATTR3args)); |
| 768 | args.object = nfs->rootfh; |
| 769 | |
| 770 | if (rpc_nfs3_getattr_async(rpc, nfs_mount_10_cb, &args, data) != 0) { |
| 771 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 772 | free_nfs_cb_data(data); |
| 773 | return; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | static void nfs_mount_8_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 778 | { |
| 779 | struct nfs_cb_data *data = private_data; |
| 780 | struct nfs_context *nfs = data->nfs; |
| 781 | struct FSINFO3args args; |
| 782 | |
| 783 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 784 | |
| 785 | if (status == RPC_STATUS_ERROR) { |
| 786 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 787 | free_nfs_cb_data(data); |
| 788 | return; |
| 789 | } |
| 790 | if (status == RPC_STATUS_CANCEL) { |
| 791 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 792 | free_nfs_cb_data(data); |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */ |
| 797 | rpc_set_autoreconnect(rpc); |
| 798 | |
| 799 | args.fsroot = nfs->rootfh; |
| 800 | if (rpc_nfs3_fsinfo_async(rpc, nfs_mount_9_cb, &args, data) != 0) { |
| 801 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 802 | free_nfs_cb_data(data); |
| 803 | return; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | static void nfs_mount_6_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 808 | { |
| 809 | struct nfs_cb_data *data = private_data; |
| 810 | struct nfs_context *nfs = data->nfs; |
| 811 | mountres3 *res; |
| 812 | |
| 813 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 814 | |
| 815 | if (status == RPC_STATUS_ERROR) { |
| 816 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 817 | free_nfs_cb_data(data); |
| 818 | return; |
| 819 | } |
| 820 | if (status == RPC_STATUS_CANCEL) { |
| 821 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 822 | free_nfs_cb_data(data); |
| 823 | return; |
| 824 | } |
| 825 | |
| 826 | res = command_data; |
| 827 | if (res->fhs_status != MNT3_OK) { |
| 828 | 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)); |
| 829 | data->cb(mountstat3_to_errno(res->fhs_status), nfs, rpc_get_error(rpc), data->private_data); |
| 830 | free_nfs_cb_data(data); |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | nfs->rootfh.data.data_len = res->mountres3_u.mountinfo.fhandle.fhandle3_len; |
| 835 | nfs->rootfh.data.data_val = malloc(nfs->rootfh.data.data_len); |
| 836 | if (nfs->rootfh.data.data_val == NULL) { |
| 837 | rpc_set_error(rpc, "Out of memory. Could not allocate memory to store root filehandle"); |
| 838 | data->cb(-ENOMEM, nfs, rpc_get_error(rpc), data->private_data); |
| 839 | free_nfs_cb_data(data); |
| 840 | return; |
| 841 | } |
| 842 | memcpy(nfs->rootfh.data.data_val, res->mountres3_u.mountinfo.fhandle.fhandle3_val, nfs->rootfh.data.data_len); |
| 843 | |
| 844 | rpc_disconnect(rpc, "normal disconnect"); |
| 845 | |
| 846 | if (rpc_connect_program_async(nfs->rpc, nfs->server, NFS_PROGRAM, NFS_V3, nfs_mount_8_cb, data) != 0) { |
| 847 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 848 | free_nfs_cb_data(data); |
| 849 | return; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | |
| 854 | static void nfs_mount_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 855 | { |
| 856 | struct nfs_cb_data *data = private_data; |
| 857 | struct nfs_context *nfs = data->nfs; |
| 858 | |
| 859 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 860 | |
| 861 | if (status == RPC_STATUS_ERROR) { |
| 862 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 863 | free_nfs_cb_data(data); |
| 864 | return; |
| 865 | } |
| 866 | if (status == RPC_STATUS_CANCEL) { |
| 867 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 868 | free_nfs_cb_data(data); |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | if (rpc_mount3_mnt_async(rpc, nfs_mount_6_cb, nfs->export, data) != 0) { |
| 873 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 874 | free_nfs_cb_data(data); |
| 875 | return; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * Async call for mounting an nfs share and geting the root filehandle |
| 881 | */ |
| 882 | int nfs_mount_async(struct nfs_context *nfs, const char *server, const char *export, nfs_cb cb, void *private_data) |
| 883 | { |
| 884 | struct nfs_cb_data *data; |
| 885 | char *new_server, *new_export; |
| 886 | |
| 887 | data = malloc(sizeof(struct nfs_cb_data)); |
| 888 | if (data == NULL) { |
| 889 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data"); |
| 890 | return -1; |
| 891 | } |
| 892 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 893 | new_server = strdup(server); |
| 894 | new_export = strdup(export); |
| 895 | if (nfs->server != NULL) { |
| 896 | free(nfs->server); |
| 897 | } |
| 898 | nfs->server = new_server; |
| 899 | if (nfs->export != NULL) { |
| 900 | free(nfs->export); |
| 901 | } |
| 902 | nfs->export = new_export; |
| 903 | data->nfs = nfs; |
| 904 | data->cb = cb; |
| 905 | data->private_data = private_data; |
| 906 | |
| 907 | if (rpc_connect_program_async(nfs->rpc, server, MOUNT_PROGRAM, MOUNT_V3, nfs_mount_5_cb, data) != 0) { |
| 908 | rpc_set_error(nfs->rpc, "Failed to start connection"); |
| 909 | free_nfs_cb_data(data); |
| 910 | return -1; |
| 911 | } |
| 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | |
| 917 | |
| 918 | /* |
| 919 | * Functions to first look up a path, component by component, and then finally call a specific function once |
| 920 | * the filehandle for the final component is found. |
| 921 | */ |
| 922 | static void nfs_lookup_path_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 923 | { |
| 924 | struct nfs_cb_data *data = private_data; |
| 925 | struct nfs_context *nfs = data->nfs; |
| 926 | READLINK3res *res; |
| 927 | char *path, *newpath; |
| 928 | |
| 929 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 930 | |
| 931 | if (status == RPC_STATUS_ERROR) { |
| 932 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 933 | free_nfs_cb_data(data); |
| 934 | return; |
| 935 | } |
| 936 | if (status == RPC_STATUS_CANCEL) { |
| 937 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 938 | free_nfs_cb_data(data); |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | res = command_data; |
| 943 | if (res->status != NFS3_OK) { |
| 944 | 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)); |
| 945 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 946 | free_nfs_cb_data(data); |
| 947 | return; |
| 948 | } |
| 949 | |
| 950 | path = res->READLINK3res_u.resok.data; |
| 951 | |
| 952 | /* Handle absolute paths, ensuring that the path lies within the |
| 953 | * export. */ |
| 954 | if (path[0] == '/') { |
| 955 | if (strstr(path, nfs->export) == path) { |
| 956 | char *ptr = path + strlen(nfs->export); |
| 957 | if (*ptr == '/') { |
| 958 | newpath = strdup(ptr); |
| 959 | } else if (*ptr == '\0') { |
| 960 | newpath = strdup("/"); |
| 961 | } else { |
| 962 | data->cb(-ENOENT, nfs, "Symbolic link points outside export", data->private_data); |
| 963 | free_nfs_cb_data(data); |
| 964 | return; |
| 965 | } |
| 966 | } else { |
| 967 | data->cb(-ENOENT, nfs, "Symbolic link points outside export", data->private_data); |
| 968 | free_nfs_cb_data(data); |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | if (!newpath) |
| 973 | goto nomem; |
| 974 | } else { |
| 975 | /* Handle relative paths, both the case where the current |
| 976 | * component is an intermediate component and when it is the |
| 977 | * final component. */ |
| 978 | if (data->path[0]) { |
| 979 | /* Since path points to a component and saved_path |
| 980 | * always starts with '/', path[-1] is valid. */ |
| 981 | data->path[-1] = '\0'; |
| 982 | newpath = malloc(strlen(data->saved_path) + strlen(path) + strlen(data->path) + 6); |
| 983 | if (!newpath) |
| 984 | goto nomem; |
| 985 | |
| 986 | sprintf(newpath, "%s/../%s/%s", data->saved_path, path, data->path); |
| 987 | } else { |
| 988 | newpath = malloc(strlen(data->saved_path) + strlen(path) + 5); |
| 989 | if (!newpath) |
| 990 | goto nomem; |
| 991 | |
| 992 | sprintf(newpath, "%s/../%s", data->saved_path, path); |
| 993 | } |
| 994 | } |
| 995 | free(data->saved_path); |
| 996 | data->saved_path = newpath; |
| 997 | |
| 998 | if (nfs_normalize_path(nfs, data->saved_path) != 0) { |
| 999 | data->cb(-ENOENT, nfs, "Symbolic link resolves to invalid path", data->private_data); |
| 1000 | free_nfs_cb_data(data); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | data->path = data->saved_path; |
| 1005 | nfs_lookup_path_async_internal(nfs, NULL, data, &nfs->rootfh); |
| 1006 | return; |
| 1007 | |
| 1008 | nomem: |
| 1009 | data->cb(-ENOMEM, nfs, "Failed to allocate memory for path", data->private_data); |
| 1010 | free_nfs_cb_data(data); |
| 1011 | } |
| 1012 | |
| 1013 | static void nfs_lookup_path_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1014 | { |
| 1015 | struct nfs_cb_data *data = private_data; |
| 1016 | struct nfs_context *nfs = data->nfs; |
| 1017 | LOOKUP3res *res; |
| 1018 | fattr3 *attr; |
| 1019 | |
| 1020 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1021 | |
| 1022 | if (status == RPC_STATUS_ERROR) { |
| 1023 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1024 | free_nfs_cb_data(data); |
| 1025 | return; |
| 1026 | } |
| 1027 | if (status == RPC_STATUS_CANCEL) { |
| 1028 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1029 | free_nfs_cb_data(data); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | res = command_data; |
| 1034 | if (res->status != NFS3_OK) { |
| 1035 | 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)); |
| 1036 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1037 | free_nfs_cb_data(data); |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | attr = res->LOOKUP3res_u.resok.obj_attributes.attributes_follow ? |
| 1042 | &res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes : |
| 1043 | NULL; |
| 1044 | |
| 1045 | /* This function will always invoke the callback and cleanup |
| 1046 | * for failures. So no need to check the return value. |
| 1047 | */ |
| 1048 | nfs_lookup_path_async_internal(nfs, attr, data, &res->LOOKUP3res_u.resok.object); |
| 1049 | } |
| 1050 | |
| 1051 | static int nfs_lookup_path_async_internal(struct nfs_context *nfs, fattr3 *attr, struct nfs_cb_data *data, struct nfs_fh3 *fh) |
| 1052 | { |
| 1053 | char *path, *slash; |
| 1054 | LOOKUP3args args; |
| 1055 | |
| 1056 | while (*data->path == '/') { |
| 1057 | data->path++; |
| 1058 | } |
| 1059 | |
| 1060 | path = data->path; |
| 1061 | slash = strchr(path, '/'); |
| 1062 | |
| 1063 | if (attr && attr->type == NF3LNK && (!data->no_follow || *path != '\0')) { |
| 1064 | READLINK3args args; |
| 1065 | |
| 1066 | if (data->link_count++ >= MAX_LINK_COUNT) { |
| 1067 | data->cb(-ELOOP, nfs, "Too many levels of symbolic links", data->private_data); |
| 1068 | free_nfs_cb_data(data); |
| 1069 | return -1; |
| 1070 | } |
| 1071 | |
| 1072 | args.symlink = *fh; |
| 1073 | |
| 1074 | if (rpc_nfs3_readlink_async(nfs->rpc, nfs_lookup_path_2_cb, &args, data) != 0) { |
| 1075 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path); |
| 1076 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1077 | free_nfs_cb_data(data); |
| 1078 | return -1; |
| 1079 | } |
| 1080 | |
| 1081 | if (slash != NULL) { |
| 1082 | *slash = '/'; |
| 1083 | } |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | if (slash != NULL) { |
| 1088 | /* Clear slash so that path is a zero terminated string for |
| 1089 | * the current path component. Set it back to '/' again later |
| 1090 | * when we are finished referencing this component so that |
| 1091 | * data->saved_path will still point to the full |
| 1092 | * normalized path. |
| 1093 | */ |
| 1094 | *slash = 0; |
| 1095 | data->path = slash+1; |
| 1096 | } else { |
| 1097 | while (*data->path != 0) { |
| 1098 | data->path++; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | if (*path == 0) { |
| 1103 | data->fh.data.data_len = fh->data.data_len; |
| 1104 | data->fh.data.data_val = malloc(data->fh.data.data_len); |
| 1105 | if (data->fh.data.data_val == NULL) { |
| 1106 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh for %s", data->path); |
| 1107 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1108 | free_nfs_cb_data(data); |
| 1109 | return -1; |
| 1110 | } |
| 1111 | memcpy(data->fh.data.data_val, fh->data.data_val, data->fh.data.data_len); |
| 1112 | if (slash != NULL) { |
| 1113 | *slash = '/'; |
| 1114 | } |
| 1115 | data->continue_cb(nfs, attr, data); |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | memset(&args, 0, sizeof(LOOKUP3args)); |
| 1120 | args.what.dir = *fh; |
| 1121 | args.what.name = path; |
| 1122 | |
| 1123 | if (rpc_nfs3_lookup_async(nfs->rpc, nfs_lookup_path_1_cb, &args, data) != 0) { |
| 1124 | rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s", data->path); |
| 1125 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1126 | free_nfs_cb_data(data); |
| 1127 | return -1; |
| 1128 | } |
| 1129 | if (slash != NULL) { |
| 1130 | *slash = '/'; |
| 1131 | } |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | static int nfs_normalize_path(struct nfs_context *nfs, char *path) |
| 1136 | { |
| 1137 | char *str; |
| 1138 | int len; |
| 1139 | |
| 1140 | /* // -> / */ |
| 1141 | while ((str = strstr(path, "//"))) { |
| 1142 | while(*str) { |
| 1143 | *str = *(str + 1); |
| 1144 | str++; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | /* /./ -> / */ |
| 1149 | while ((str = strstr(path, "/./"))) { |
| 1150 | while(*(str + 1)) { |
| 1151 | *str = *(str + 2); |
| 1152 | str++; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | /* ^/../ -> error */ |
| 1157 | if (!strncmp(path, "/../", 4)) { |
| 1158 | rpc_set_error(nfs->rpc, |
| 1159 | "Absolute path starts with '/../' " |
| 1160 | "during normalization"); |
| 1161 | return -1; |
| 1162 | } |
| 1163 | |
| 1164 | /* ^[^/] -> error */ |
| 1165 | if (path[0] != '/') { |
| 1166 | rpc_set_error(nfs->rpc, |
| 1167 | "Absolute path does not start with '/'"); |
| 1168 | return -1; |
| 1169 | } |
| 1170 | |
| 1171 | /* /string/../ -> / */ |
| 1172 | while ((str = strstr(path, "/../"))) { |
| 1173 | char *tmp; |
| 1174 | |
| 1175 | if (!strncmp(path, "/../", 4)) { |
| 1176 | rpc_set_error(nfs->rpc, |
| 1177 | "Absolute path starts with '/../' " |
| 1178 | "during normalization"); |
| 1179 | return -1; |
| 1180 | } |
| 1181 | |
| 1182 | tmp = str - 1; |
| 1183 | while (*tmp != '/') { |
| 1184 | tmp--; |
| 1185 | } |
| 1186 | str += 3; |
| 1187 | while((*(tmp++) = *(str++)) != '\0') |
| 1188 | ; |
| 1189 | } |
| 1190 | |
| 1191 | /* /$ -> \0 */ |
| 1192 | len = strlen(path); |
| 1193 | if (len > 1) { |
| 1194 | if (path[len - 1] == '/') { |
| 1195 | path[len - 1] = '\0'; |
| 1196 | len--; |
| 1197 | } |
| 1198 | } |
| 1199 | if (path[0] == '\0') { |
| 1200 | rpc_set_error(nfs->rpc, |
| 1201 | "Absolute path became '' " |
| 1202 | "during normalization"); |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
| 1206 | /* /.$ -> \0 */ |
| 1207 | if (len >= 2) { |
| 1208 | if (!strcmp(&path[len - 2], "/.")) { |
| 1209 | path[len - 2] = '\0'; |
| 1210 | len -= 2; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | /* ^/..$ -> error */ |
| 1215 | if (!strcmp(path, "/..")) { |
| 1216 | rpc_set_error(nfs->rpc, |
| 1217 | "Absolute path is '/..' " |
| 1218 | "during normalization"); |
| 1219 | return -1; |
| 1220 | } |
| 1221 | |
| 1222 | /* /string/..$ -> / */ |
| 1223 | if (len >= 3) { |
| 1224 | if (!strcmp(&path[len - 3], "/..")) { |
| 1225 | char *tmp = &path[len - 3]; |
| 1226 | while (*--tmp != '/') |
| 1227 | ; |
| 1228 | *tmp = '\0'; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | static void nfs_lookup_path_getattr_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1236 | { |
| 1237 | struct nfs_cb_data *data = private_data; |
| 1238 | struct nfs_context *nfs = data->nfs; |
| 1239 | GETATTR3res *res; |
| 1240 | fattr3 *attr; |
| 1241 | |
| 1242 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1243 | |
| 1244 | if (status == RPC_STATUS_ERROR) { |
| 1245 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1246 | free_nfs_cb_data(data); |
| 1247 | return; |
| 1248 | } |
| 1249 | if (status == RPC_STATUS_CANCEL) { |
| 1250 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1251 | free_nfs_cb_data(data); |
| 1252 | return; |
| 1253 | } |
| 1254 | |
| 1255 | res = command_data; |
| 1256 | if (res->status != NFS3_OK) { |
| 1257 | 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)); |
| 1258 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1259 | free_nfs_cb_data(data); |
| 1260 | return; |
| 1261 | } |
| 1262 | |
| 1263 | attr = &res->GETATTR3res_u.resok.obj_attributes; |
| 1264 | /* This function will always invoke the callback and cleanup |
| 1265 | * for failures. So no need to check the return value. |
| 1266 | */ |
| 1267 | nfs_lookup_path_async_internal(nfs, attr, data, &nfs->rootfh); |
| 1268 | } |
| 1269 | |
| 1270 | static int nfs_lookuppath_async(struct nfs_context *nfs, const char *path, int no_follow, nfs_cb cb, void *private_data, continue_func continue_cb, void *continue_data, void (*free_continue_data)(void *), int continue_int) |
| 1271 | { |
| 1272 | struct nfs_cb_data *data; |
| 1273 | struct GETATTR3args args; |
| 1274 | |
| 1275 | if (path[0] == '\0') { |
| 1276 | path = "."; |
| 1277 | } |
| 1278 | |
| 1279 | data = malloc(sizeof(struct nfs_cb_data)); |
| 1280 | if (data == NULL) { |
| 1281 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate " |
| 1282 | "nfs_cb_data structure"); |
| 1283 | if (free_continue_data) |
| 1284 | free_continue_data(continue_data); |
| 1285 | return -1; |
| 1286 | } |
| 1287 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 1288 | data->nfs = nfs; |
| 1289 | data->cb = cb; |
| 1290 | data->continue_cb = continue_cb; |
| 1291 | data->continue_data = continue_data; |
| 1292 | data->free_continue_data = free_continue_data; |
| 1293 | data->continue_int = continue_int; |
| 1294 | data->private_data = private_data; |
| 1295 | data->no_follow = no_follow; |
| 1296 | if (path[0] == '/') { |
| 1297 | data->saved_path = strdup(path); |
| 1298 | } else { |
| 1299 | data->saved_path = malloc(strlen(path) + strlen(nfs->cwd) + 2); |
| 1300 | if (data->saved_path == NULL) { |
| 1301 | rpc_set_error(nfs->rpc, "out of memory: failed to " |
| 1302 | "malloc path string"); |
| 1303 | free_nfs_cb_data(data); |
| 1304 | return -1; |
| 1305 | } |
| 1306 | sprintf(data->saved_path, "%s/%s", nfs->cwd, path); |
| 1307 | } |
| 1308 | |
| 1309 | if (data->saved_path == NULL) { |
| 1310 | rpc_set_error(nfs->rpc, "out of memory: failed to copy path string"); |
| 1311 | free_nfs_cb_data(data); |
| 1312 | return -1; |
| 1313 | } |
| 1314 | if (nfs_normalize_path(nfs, data->saved_path) != 0) { |
| 1315 | free_nfs_cb_data(data); |
| 1316 | return -1; |
| 1317 | } |
| 1318 | |
| 1319 | data->path = data->saved_path; |
| 1320 | if (data->path[0]) { |
| 1321 | /* This function will always invoke the callback and cleanup |
| 1322 | * for failures. So no need to check the return value. |
| 1323 | */ |
| 1324 | nfs_lookup_path_async_internal(nfs, NULL, data, &nfs->rootfh); |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | /* We have a request for "", so just perform a GETATTR3 so we can |
| 1329 | * return the attributes to the caller. |
| 1330 | */ |
| 1331 | memset(&args, 0, sizeof(GETATTR3args)); |
| 1332 | args.object = nfs->rootfh; |
| 1333 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_lookup_path_getattr_cb, &args, data) != 0) { |
| 1334 | free_nfs_cb_data(data); |
| 1335 | return -1; |
| 1336 | } |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | /* |
| 1342 | * Async stat() |
| 1343 | */ |
| 1344 | static dev_t specdata3_to_rdev(struct specdata3 *rdev) |
| 1345 | { |
| 1346 | #ifdef makedev |
| 1347 | return makedev(rdev->specdata1, rdev->specdata2); |
| 1348 | #else |
| 1349 | return 0; |
| 1350 | #endif |
| 1351 | } |
| 1352 | |
| 1353 | static void nfs_stat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1354 | { |
| 1355 | GETATTR3res *res; |
| 1356 | struct nfs_cb_data *data = private_data; |
| 1357 | struct nfs_context *nfs = data->nfs; |
| 1358 | #ifdef WIN32 |
| 1359 | struct __stat64 st; |
| 1360 | #else |
| 1361 | struct stat st; |
| 1362 | #endif |
| 1363 | |
| 1364 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1365 | |
| 1366 | if (status == RPC_STATUS_ERROR) { |
| 1367 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1368 | free_nfs_cb_data(data); |
| 1369 | return; |
| 1370 | } |
| 1371 | if (status == RPC_STATUS_CANCEL) { |
| 1372 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1373 | free_nfs_cb_data(data); |
| 1374 | return; |
| 1375 | } |
| 1376 | |
| 1377 | res = command_data; |
| 1378 | if (res->status != NFS3_OK) { |
| 1379 | 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)); |
| 1380 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1381 | free_nfs_cb_data(data); |
| 1382 | return; |
| 1383 | } |
| 1384 | |
| 1385 | st.st_dev = res->GETATTR3res_u.resok.obj_attributes.fsid; |
| 1386 | st.st_ino = res->GETATTR3res_u.resok.obj_attributes.fileid; |
| 1387 | st.st_mode = res->GETATTR3res_u.resok.obj_attributes.mode; |
| 1388 | switch (res->GETATTR3res_u.resok.obj_attributes.type) { |
| 1389 | case NF3REG: |
| 1390 | st.st_mode |= S_IFREG; |
| 1391 | break; |
| 1392 | case NF3DIR: |
| 1393 | st.st_mode |= S_IFDIR; |
| 1394 | break; |
| 1395 | case NF3BLK: |
| 1396 | st.st_mode |= S_IFBLK; |
| 1397 | break; |
| 1398 | case NF3CHR: |
| 1399 | st.st_mode |= S_IFCHR; |
| 1400 | break; |
| 1401 | case NF3LNK: |
| 1402 | st.st_mode |= S_IFLNK; |
| 1403 | break; |
| 1404 | case NF3SOCK: |
| 1405 | st.st_mode |= S_IFSOCK; |
| 1406 | break; |
| 1407 | case NF3FIFO: |
| 1408 | st.st_mode |= S_IFIFO; |
| 1409 | break; |
| 1410 | } |
| 1411 | st.st_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink; |
| 1412 | st.st_uid = res->GETATTR3res_u.resok.obj_attributes.uid; |
| 1413 | st.st_gid = res->GETATTR3res_u.resok.obj_attributes.gid; |
| 1414 | st.st_rdev = specdata3_to_rdev(&res->GETATTR3res_u.resok.obj_attributes.rdev); |
| 1415 | st.st_size = res->GETATTR3res_u.resok.obj_attributes.size; |
| 1416 | #ifndef WIN32 |
| 1417 | st.st_blksize = NFS_BLKSIZE; |
| 1418 | st.st_blocks = (res->GETATTR3res_u.resok.obj_attributes.used + 512 - 1) / 512; |
| 1419 | #endif//WIN32 |
| 1420 | st.st_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds; |
| 1421 | st.st_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds; |
| 1422 | st.st_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds; |
| 1423 | #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC |
| 1424 | st.st_atim.tv_nsec = res->GETATTR3res_u.resok.obj_attributes.atime.nseconds; |
| 1425 | st.st_mtim.tv_nsec = res->GETATTR3res_u.resok.obj_attributes.mtime.nseconds; |
| 1426 | st.st_ctim.tv_nsec = res->GETATTR3res_u.resok.obj_attributes.ctime.nseconds; |
| 1427 | #endif |
| 1428 | |
| 1429 | data->cb(0, nfs, &st, data->private_data); |
| 1430 | free_nfs_cb_data(data); |
| 1431 | } |
| 1432 | |
| 1433 | static int nfs_stat_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 1434 | { |
| 1435 | struct GETATTR3args args; |
| 1436 | |
| 1437 | memset(&args, 0, sizeof(GETATTR3args)); |
| 1438 | args.object = data->fh; |
| 1439 | |
| 1440 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_stat_1_cb, &args, data) != 0) { |
| 1441 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); |
| 1442 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1443 | free_nfs_cb_data(data); |
| 1444 | return -1; |
| 1445 | } |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 1450 | { |
| 1451 | if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_stat_continue_internal, NULL, NULL, 0) != 0) { |
| 1452 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1453 | return -1; |
| 1454 | } |
| 1455 | |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | /* |
| 1461 | * Async nfs_stat64() |
| 1462 | */ |
| 1463 | static void nfs_stat64_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1464 | { |
| 1465 | GETATTR3res *res; |
| 1466 | struct nfs_cb_data *data = private_data; |
| 1467 | struct nfs_context *nfs = data->nfs; |
| 1468 | struct nfs_stat_64 st; |
| 1469 | |
| 1470 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1471 | |
| 1472 | if (status == RPC_STATUS_ERROR) { |
| 1473 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1474 | free_nfs_cb_data(data); |
| 1475 | return; |
| 1476 | } |
| 1477 | if (status == RPC_STATUS_CANCEL) { |
| 1478 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1479 | free_nfs_cb_data(data); |
| 1480 | return; |
| 1481 | } |
| 1482 | |
| 1483 | res = command_data; |
| 1484 | if (res->status != NFS3_OK) { |
| 1485 | 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)); |
| 1486 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1487 | free_nfs_cb_data(data); |
| 1488 | return; |
| 1489 | } |
| 1490 | |
| 1491 | st.nfs_dev = res->GETATTR3res_u.resok.obj_attributes.fsid; |
| 1492 | st.nfs_ino = res->GETATTR3res_u.resok.obj_attributes.fileid; |
| 1493 | st.nfs_mode = res->GETATTR3res_u.resok.obj_attributes.mode; |
| 1494 | switch (res->GETATTR3res_u.resok.obj_attributes.type) { |
| 1495 | case NF3REG: |
| 1496 | st.nfs_mode |= S_IFREG; |
| 1497 | break; |
| 1498 | case NF3DIR: |
| 1499 | st.nfs_mode |= S_IFDIR; |
| 1500 | break; |
| 1501 | case NF3BLK: |
| 1502 | st.nfs_mode |= S_IFBLK; |
| 1503 | break; |
| 1504 | case NF3CHR: |
| 1505 | st.nfs_mode |= S_IFCHR; |
| 1506 | break; |
| 1507 | case NF3LNK: |
| 1508 | st.nfs_mode |= S_IFLNK; |
| 1509 | break; |
| 1510 | case NF3SOCK: |
| 1511 | st.nfs_mode |= S_IFSOCK; |
| 1512 | break; |
| 1513 | case NF3FIFO: |
| 1514 | st.nfs_mode |= S_IFIFO; |
| 1515 | break; |
| 1516 | } |
| 1517 | st.nfs_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink; |
| 1518 | st.nfs_uid = res->GETATTR3res_u.resok.obj_attributes.uid; |
| 1519 | st.nfs_gid = res->GETATTR3res_u.resok.obj_attributes.gid; |
| 1520 | st.nfs_rdev = specdata3_to_rdev(&res->GETATTR3res_u.resok.obj_attributes.rdev); |
| 1521 | st.nfs_size = res->GETATTR3res_u.resok.obj_attributes.size; |
| 1522 | st.nfs_blksize = NFS_BLKSIZE; |
| 1523 | st.nfs_blocks = (res->GETATTR3res_u.resok.obj_attributes.used + 512 - 1) / 512; |
| 1524 | st.nfs_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds; |
| 1525 | st.nfs_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds; |
| 1526 | st.nfs_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds; |
| 1527 | st.nfs_atime_nsec = res->GETATTR3res_u.resok.obj_attributes.atime.nseconds; |
| 1528 | st.nfs_mtime_nsec = res->GETATTR3res_u.resok.obj_attributes.mtime.nseconds; |
| 1529 | st.nfs_ctime_nsec = res->GETATTR3res_u.resok.obj_attributes.ctime.nseconds; |
| 1530 | st.nfs_used = res->GETATTR3res_u.resok.obj_attributes.used; |
| 1531 | |
| 1532 | data->cb(0, nfs, &st, data->private_data); |
| 1533 | free_nfs_cb_data(data); |
| 1534 | } |
| 1535 | |
| 1536 | static int nfs_stat64_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 1537 | { |
| 1538 | struct GETATTR3args args; |
| 1539 | |
| 1540 | memset(&args, 0, sizeof(GETATTR3args)); |
| 1541 | args.object = data->fh; |
| 1542 | |
| 1543 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_stat64_1_cb, &args, data) != 0) { |
| 1544 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); |
| 1545 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1546 | free_nfs_cb_data(data); |
| 1547 | return -1; |
| 1548 | } |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | int nfs_stat64_async_internal(struct nfs_context *nfs, const char *path, int no_follow, nfs_cb cb, void *private_data) |
| 1553 | { |
| 1554 | if (nfs_lookuppath_async(nfs, path, no_follow, cb, private_data, nfs_stat64_continue_internal, NULL, NULL, 0) != 0) { |
| 1555 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1556 | return -1; |
| 1557 | } |
| 1558 | |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | int nfs_stat64_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 1563 | { |
| 1564 | return nfs_stat64_async_internal(nfs, path, 0, cb, private_data); |
| 1565 | } |
| 1566 | |
| 1567 | int nfs_lstat64_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 1568 | { |
| 1569 | return nfs_stat64_async_internal(nfs, path, 1, cb, private_data); |
| 1570 | } |
| 1571 | |
| 1572 | /* |
| 1573 | * Async open() |
| 1574 | */ |
| 1575 | static void nfs_open_trunc_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1576 | { |
| 1577 | struct nfs_cb_data *data = private_data; |
| 1578 | struct nfs_context *nfs = data->nfs; |
| 1579 | struct nfsfh *nfsfh; |
| 1580 | SETATTR3res *res; |
| 1581 | |
| 1582 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1583 | |
| 1584 | if (status == RPC_STATUS_ERROR) { |
| 1585 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1586 | free_nfs_cb_data(data); |
| 1587 | return; |
| 1588 | } |
| 1589 | if (status == RPC_STATUS_CANCEL) { |
| 1590 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1591 | free_nfs_cb_data(data); |
| 1592 | return; |
| 1593 | } |
| 1594 | |
| 1595 | res = command_data; |
| 1596 | if (res->status != NFS3_OK) { |
| 1597 | rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1598 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1599 | free_nfs_cb_data(data); |
| 1600 | return; |
| 1601 | } |
| 1602 | |
| 1603 | nfsfh = malloc(sizeof(struct nfsfh)); |
| 1604 | if (nfsfh == NULL) { |
| 1605 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); |
| 1606 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1607 | free_nfs_cb_data(data); |
| 1608 | return; |
| 1609 | } |
| 1610 | memset(nfsfh, 0, sizeof(struct nfsfh)); |
| 1611 | |
| 1612 | if (data->continue_int & O_SYNC) { |
| 1613 | nfsfh->is_sync = 1; |
| 1614 | } |
| 1615 | if (data->continue_int & O_APPEND) { |
| 1616 | nfsfh->is_append = 1; |
| 1617 | } |
| 1618 | |
| 1619 | /* steal the filehandle */ |
| 1620 | nfsfh->fh = data->fh; |
| 1621 | data->fh.data.data_val = NULL; |
| 1622 | |
| 1623 | data->cb(0, nfs, nfsfh, data->private_data); |
| 1624 | free_nfs_cb_data(data); |
| 1625 | } |
| 1626 | |
| 1627 | static void nfs_open_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1628 | { |
| 1629 | ACCESS3res *res; |
| 1630 | struct nfs_cb_data *data = private_data; |
| 1631 | struct nfs_context *nfs = data->nfs; |
| 1632 | struct nfsfh *nfsfh; |
| 1633 | unsigned int nfsmode = 0; |
| 1634 | |
| 1635 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1636 | |
| 1637 | if (status == RPC_STATUS_ERROR) { |
| 1638 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1639 | free_nfs_cb_data(data); |
| 1640 | return; |
| 1641 | } |
| 1642 | if (status == RPC_STATUS_CANCEL) { |
| 1643 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1644 | free_nfs_cb_data(data); |
| 1645 | return; |
| 1646 | } |
| 1647 | |
| 1648 | res = command_data; |
| 1649 | if (res->status != NFS3_OK) { |
| 1650 | 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)); |
| 1651 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1652 | free_nfs_cb_data(data); |
| 1653 | return; |
| 1654 | } |
| 1655 | |
| 1656 | if (data->continue_int & O_WRONLY) { |
| 1657 | nfsmode |= ACCESS3_MODIFY; |
| 1658 | } |
| 1659 | if (data->continue_int & O_RDWR) { |
| 1660 | nfsmode |= ACCESS3_READ|ACCESS3_MODIFY; |
| 1661 | } |
| 1662 | if (!(data->continue_int & (O_WRONLY|O_RDWR))) { |
| 1663 | nfsmode |= ACCESS3_READ; |
| 1664 | } |
| 1665 | |
| 1666 | |
| 1667 | if (res->ACCESS3res_u.resok.access != nfsmode) { |
| 1668 | rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c", |
| 1669 | nfsmode&ACCESS3_READ?'r':'-', |
| 1670 | nfsmode&ACCESS3_MODIFY?'w':'-', |
| 1671 | nfsmode&ACCESS3_EXECUTE?'x':'-', |
| 1672 | res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-', |
| 1673 | res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-', |
| 1674 | res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-'); |
| 1675 | data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1676 | free_nfs_cb_data(data); |
| 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | /* Try to truncate it if we were requested to */ |
| 1681 | if ((data->continue_int & O_TRUNC) && |
| 1682 | (data->continue_int & (O_RDWR|O_WRONLY))) { |
| 1683 | SETATTR3args args; |
| 1684 | |
| 1685 | memset(&args, 0, sizeof(SETATTR3args)); |
| 1686 | args.object = data->fh; |
| 1687 | args.new_attributes.size.set_it = 1; |
| 1688 | args.new_attributes.size.set_size3_u.size = 0; |
| 1689 | |
| 1690 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_open_trunc_cb, &args, |
| 1691 | data) != 0) { |
| 1692 | rpc_set_error(nfs->rpc, "RPC error: Failed to send " |
| 1693 | "SETATTR call for %s", data->path); |
| 1694 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), |
| 1695 | data->private_data); |
| 1696 | free_nfs_cb_data(data); |
| 1697 | return; |
| 1698 | } |
| 1699 | return; |
| 1700 | } |
| 1701 | |
| 1702 | nfsfh = malloc(sizeof(struct nfsfh)); |
| 1703 | if (nfsfh == NULL) { |
| 1704 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); |
| 1705 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 1706 | free_nfs_cb_data(data); |
| 1707 | return; |
| 1708 | } |
| 1709 | memset(nfsfh, 0, sizeof(struct nfsfh)); |
| 1710 | |
| 1711 | if (data->continue_int & O_SYNC) { |
| 1712 | nfsfh->is_sync = 1; |
| 1713 | } |
| 1714 | if (data->continue_int & O_APPEND) { |
| 1715 | nfsfh->is_append = 1; |
| 1716 | } |
| 1717 | |
| 1718 | /* steal the filehandle */ |
| 1719 | nfsfh->fh = data->fh; |
| 1720 | data->fh.data.data_val = NULL; |
| 1721 | |
| 1722 | data->cb(0, nfs, nfsfh, data->private_data); |
| 1723 | free_nfs_cb_data(data); |
| 1724 | } |
| 1725 | |
| 1726 | static int nfs_open_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 1727 | { |
| 1728 | int nfsmode = 0; |
| 1729 | ACCESS3args args; |
| 1730 | |
| 1731 | if (data->continue_int & O_WRONLY) { |
| 1732 | nfsmode |= ACCESS3_MODIFY; |
| 1733 | } |
| 1734 | if (data->continue_int & O_RDWR) { |
| 1735 | nfsmode |= ACCESS3_READ|ACCESS3_MODIFY; |
| 1736 | } |
| 1737 | if (!(data->continue_int & (O_WRONLY|O_RDWR))) { |
| 1738 | nfsmode |= ACCESS3_READ; |
| 1739 | } |
| 1740 | |
| 1741 | memset(&args, 0, sizeof(ACCESS3args)); |
| 1742 | args.object = data->fh; |
| 1743 | args.access = nfsmode; |
| 1744 | |
| 1745 | if (rpc_nfs3_access_async(nfs->rpc, nfs_open_cb, &args, data) != 0) { |
| 1746 | rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS " |
| 1747 | "call for %s", data->path); |
| 1748 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), |
| 1749 | data->private_data); |
| 1750 | free_nfs_cb_data(data); |
| 1751 | return -1; |
| 1752 | } |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | int nfs_open_async(struct nfs_context *nfs, const char *path, int flags, nfs_cb cb, void *private_data) |
| 1757 | { |
| 1758 | if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_open_continue_internal, NULL, NULL, flags) != 0) { |
| 1759 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1760 | return -1; |
| 1761 | } |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | |
| 1767 | /* |
| 1768 | * Async chdir() |
| 1769 | */ |
| 1770 | static int nfs_chdir_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 1771 | { |
| 1772 | /* steal saved_path */ |
| 1773 | free(nfs->cwd); |
| 1774 | nfs->cwd = data->saved_path; |
| 1775 | data->saved_path = NULL; |
| 1776 | |
| 1777 | data->cb(0, nfs, NULL, data->private_data); |
| 1778 | free_nfs_cb_data(data); |
| 1779 | |
| 1780 | return 0; |
| 1781 | } |
| 1782 | |
| 1783 | int nfs_chdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 1784 | { |
| 1785 | if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_chdir_continue_internal, NULL, NULL, 0) != 0) { |
| 1786 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 1787 | return -1; |
| 1788 | } |
| 1789 | |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | |
| 1794 | /* |
| 1795 | * Async pread() |
| 1796 | */ |
| 1797 | static void nfs_fill_READ3args(READ3args *args, struct nfsfh *fh, uint64_t offset, uint64_t count) |
| 1798 | { |
| 1799 | memset(args, 0, sizeof(READ3args)); |
| 1800 | args->file = fh->fh; |
| 1801 | args->offset = offset; |
| 1802 | args->count = count; |
| 1803 | } |
| 1804 | |
| 1805 | static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 1806 | { |
| 1807 | struct nfs_mcb_data *mdata = private_data; |
| 1808 | struct nfs_cb_data *data = mdata->data; |
| 1809 | struct nfs_context *nfs = data->nfs; |
| 1810 | READ3res *res; |
| 1811 | |
| 1812 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 1813 | |
| 1814 | data->num_calls--; |
| 1815 | |
| 1816 | if (status == RPC_STATUS_ERROR) { |
| 1817 | /* flag the failure but do not invoke callback until we have received all responses */ |
| 1818 | data->error = 1; |
| 1819 | } |
| 1820 | if (status == RPC_STATUS_CANCEL) { |
| 1821 | /* flag the cancellation but do not invoke callback until we have received all responses */ |
| 1822 | data->cancel = 1; |
| 1823 | } |
| 1824 | |
| 1825 | if (status == RPC_STATUS_SUCCESS) { |
| 1826 | res = command_data; |
| 1827 | if (res->status != NFS3_OK) { |
| 1828 | rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 1829 | data->error = 1; |
| 1830 | } else { |
| 1831 | uint64_t count = res->READ3res_u.resok.count; |
| 1832 | |
| 1833 | if (mdata->update_pos) |
| 1834 | data->nfsfh->offset += count; |
| 1835 | |
| 1836 | /* if we have more than one call or we have received a short read we need a reassembly buffer */ |
| 1837 | if (data->num_calls || (count < mdata->count && !res->READ3res_u.resok.eof)) { |
| 1838 | if (data->buffer == NULL) { |
| 1839 | data->buffer = malloc(data->count); |
| 1840 | if (data->buffer == NULL) { |
| 1841 | rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data->count); |
| 1842 | data->oom = 1; |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | if (count > 0) { |
| 1847 | if (count <= mdata->count) { |
| 1848 | /* copy data into reassembly buffer if we have one */ |
| 1849 | if (data->buffer != NULL) { |
| 1850 | memcpy(&data->buffer[mdata->offset - data->offset], res->READ3res_u.resok.data.data_val, count); |
| 1851 | } |
| 1852 | if (data->max_offset < mdata->offset + count) { |
| 1853 | data->max_offset = mdata->offset + count; |
| 1854 | } |
| 1855 | } else { |
| 1856 | rpc_set_error(nfs->rpc, "NFS: Read overflow. Server has sent more data than requested!"); |
| 1857 | data->error = 1; |
| 1858 | } |
| 1859 | } |
| 1860 | /* check if we have received a short read */ |
| 1861 | if (count < mdata->count && !res->READ3res_u.resok.eof) { |
| 1862 | if (count == 0) { |
| 1863 | rpc_set_error(nfs->rpc, "NFS: Read failed. No bytes read and not at EOF!"); |
| 1864 | data->error = 1; |
| 1865 | } else { |
| 1866 | /* reissue reminder of this read request */ |
| 1867 | READ3args args; |
| 1868 | mdata->offset += count; |
| 1869 | mdata->count -= count; |
| 1870 | nfs_fill_READ3args(&args, data->nfsfh, mdata->offset, mdata->count); |
| 1871 | if (rpc_nfs3_read_async(nfs->rpc, nfs_pread_mcb, &args, mdata) == 0) { |
| 1872 | data->num_calls++; |
| 1873 | return; |
| 1874 | } else { |
| 1875 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path); |
| 1876 | data->oom = 1; |
| 1877 | } |
| 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | free(mdata); |
| 1884 | |
| 1885 | if (data->num_calls > 0) { |
| 1886 | /* still waiting for more replies */ |
| 1887 | return; |
| 1888 | } |
| 1889 | if (data->oom != 0) { |
| 1890 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 1891 | free_nfs_cb_data(data); |
| 1892 | return; |
| 1893 | } |
| 1894 | if (data->error != 0) { |
| 1895 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 1896 | free_nfs_cb_data(data); |
| 1897 | return; |
| 1898 | } |
| 1899 | if (data->cancel != 0) { |
| 1900 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 1901 | free_nfs_cb_data(data); |
| 1902 | return; |
| 1903 | } |
| 1904 | |
| 1905 | if (data->buffer) { |
| 1906 | if (data->max_offset > data->org_offset + data->org_count) { |
| 1907 | data->max_offset = data->org_offset + data->org_count; |
| 1908 | } |
| 1909 | data->cb(data->max_offset - data->org_offset, nfs, data->buffer + (data->org_offset - data->offset), data->private_data); |
| 1910 | } else { |
| 1911 | data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_data); |
| 1912 | } |
| 1913 | |
| 1914 | data->nfsfh->ra.fh_offset = data->max_offset; |
| 1915 | if (data->nfsfh->ra.cur_ra) { |
| 1916 | free(data->nfsfh->ra.buf); |
| 1917 | data->nfsfh->ra.buf = data->buffer; |
| 1918 | data->nfsfh->ra.buf_offset = data->offset; |
| 1919 | data->nfsfh->ra.buf_count = data->count; |
| 1920 | data->nfsfh->ra.buf_ts = time(NULL); |
| 1921 | data->buffer = NULL; |
| 1922 | } |
| 1923 | free_nfs_cb_data(data); |
| 1924 | } |
| 1925 | |
| 1926 | static void nfs_ra_invalidate(struct nfsfh *nfsfh) { |
| 1927 | free(nfsfh->ra.buf); |
| 1928 | nfsfh->ra.buf = NULL; |
| 1929 | nfsfh->ra.buf_offset = 0; |
| 1930 | nfsfh->ra.buf_count = 0; |
| 1931 | nfsfh->ra.buf_ts = time(NULL); |
| 1932 | nfsfh->ra.cur_ra = NFS_BLKSIZE; |
| 1933 | } |
| 1934 | |
| 1935 | static int nfs_pread_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, nfs_cb cb, void *private_data, int update_pos) |
| 1936 | { |
| 1937 | struct nfs_cb_data *data; |
| 1938 | |
| 1939 | data = malloc(sizeof(struct nfs_cb_data)); |
| 1940 | if (data == NULL) { |
| 1941 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 1942 | return -1; |
| 1943 | } |
| 1944 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 1945 | data->nfs = nfs; |
| 1946 | data->cb = cb; |
| 1947 | data->private_data = private_data; |
| 1948 | data->nfsfh = nfsfh; |
| 1949 | data->org_offset = offset; |
| 1950 | data->org_count = count; |
| 1951 | |
| 1952 | assert(data->num_calls == 0); |
| 1953 | |
| 1954 | if (nfs->rpc->readahead && time(NULL) - nfsfh->ra.buf_ts > NFS_RA_TIMEOUT) { |
| 1955 | /* readahead cache timeout */ |
| 1956 | nfs_ra_invalidate(nfsfh); |
| 1957 | } |
| 1958 | |
| 1959 | if (nfs->rpc->readahead) { |
| 1960 | if (offset >= nfsfh->ra.last_offset && |
| 1961 | offset - NFS_BLKSIZE <= nfsfh->ra.fh_offset + nfsfh->ra.cur_ra) { |
| 1962 | if (nfs->rpc->readahead > nfsfh->ra.cur_ra) { |
| 1963 | nfsfh->ra.cur_ra <<= 1; |
| 1964 | } |
| 1965 | } else { |
| 1966 | nfsfh->ra.cur_ra = NFS_BLKSIZE; |
| 1967 | } |
| 1968 | |
| 1969 | nfsfh->ra.last_offset = offset; |
| 1970 | |
| 1971 | if (nfsfh->ra.buf_offset <= offset && |
| 1972 | nfsfh->ra.buf_offset + nfsfh->ra.buf_count >= offset + count) { |
| 1973 | /* serve request completely from cache */ |
| 1974 | data->buffer = malloc(count); |
| 1975 | if (data->buffer == NULL) { |
| 1976 | free_nfs_cb_data(data); |
| 1977 | return -ENOMEM; |
| 1978 | } |
| 1979 | memcpy(data->buffer, nfsfh->ra.buf + (offset - nfsfh->ra.buf_offset), count); |
| 1980 | data->cb(count, nfs, data->buffer, data->private_data); |
| 1981 | nfsfh->ra.fh_offset = offset + count; |
| 1982 | free_nfs_cb_data(data); |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
| 1986 | /* align start offset to blocksize */ |
| 1987 | count += offset & (NFS_BLKSIZE - 1); |
| 1988 | offset &= ~(NFS_BLKSIZE - 1); |
| 1989 | |
| 1990 | /* align end offset to blocksize and add readahead */ |
| 1991 | count += nfsfh->ra.cur_ra - 1; |
| 1992 | count &= ~(NFS_BLKSIZE - 1); |
| 1993 | |
| 1994 | data->buffer = malloc(count); |
| 1995 | if (data->buffer == NULL) { |
| 1996 | free_nfs_cb_data(data); |
| 1997 | return -ENOMEM; |
| 1998 | } |
| 1999 | data->offset = offset; |
| 2000 | data->count = count; |
| 2001 | |
| 2002 | if (nfsfh->ra.buf_count && nfsfh->ra.buf_offset <= offset && |
| 2003 | nfsfh->ra.buf_offset + nfsfh->ra.buf_count >= offset) { |
| 2004 | /* serve request partially from cache */ |
| 2005 | size_t overlap = (nfsfh->ra.buf_offset + nfsfh->ra.buf_count) - offset; |
| 2006 | if (overlap > count) count = overlap; |
| 2007 | memcpy(data->buffer, nfsfh->ra.buf + (offset - nfsfh->ra.buf_offset), overlap); |
| 2008 | offset += overlap; |
| 2009 | count -= overlap; |
| 2010 | } |
| 2011 | } else { |
| 2012 | data->offset = offset; |
| 2013 | data->count = count; |
| 2014 | } |
| 2015 | |
| 2016 | data->max_offset = offset; |
| 2017 | |
| 2018 | /* chop requests into chunks of at most READMAX bytes if necessary. |
| 2019 | * we send all reads in parallel so that performance is still good. |
| 2020 | */ |
| 2021 | do { |
| 2022 | uint64_t readcount = count; |
| 2023 | struct nfs_mcb_data *mdata; |
| 2024 | READ3args args; |
| 2025 | |
| 2026 | if (readcount > nfs_get_readmax(nfs)) { |
| 2027 | readcount = nfs_get_readmax(nfs); |
| 2028 | } |
| 2029 | |
| 2030 | mdata = malloc(sizeof(struct nfs_mcb_data)); |
| 2031 | if (mdata == NULL) { |
| 2032 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure"); |
| 2033 | if (data->num_calls == 0) { |
| 2034 | free_nfs_cb_data(data); |
| 2035 | return -1; |
| 2036 | } |
| 2037 | data->oom = 1; |
| 2038 | break; |
| 2039 | } |
| 2040 | memset(mdata, 0, sizeof(struct nfs_mcb_data)); |
| 2041 | mdata->data = data; |
| 2042 | mdata->offset = offset; |
| 2043 | mdata->count = readcount; |
| 2044 | mdata->update_pos = update_pos; |
| 2045 | |
| 2046 | nfs_fill_READ3args(&args, nfsfh, offset, readcount); |
| 2047 | |
| 2048 | if (rpc_nfs3_read_async(nfs->rpc, nfs_pread_mcb, &args, mdata) != 0) { |
| 2049 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path); |
| 2050 | free(mdata); |
| 2051 | if (data->num_calls == 0) { |
| 2052 | free_nfs_cb_data(data); |
| 2053 | return -1; |
| 2054 | } |
| 2055 | data->oom = 1; |
| 2056 | break; |
| 2057 | } |
| 2058 | |
| 2059 | count -= readcount; |
| 2060 | offset += readcount; |
| 2061 | data->num_calls++; |
| 2062 | } while (count > 0); |
| 2063 | |
| 2064 | return 0; |
| 2065 | } |
| 2066 | |
| 2067 | int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, nfs_cb cb, void *private_data) |
| 2068 | { |
| 2069 | return nfs_pread_async_internal(nfs, nfsfh, offset, count, cb, private_data, 0); |
| 2070 | } |
| 2071 | |
| 2072 | /* |
| 2073 | * Async read() |
| 2074 | */ |
| 2075 | int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, nfs_cb cb, void *private_data) |
| 2076 | { |
| 2077 | return nfs_pread_async_internal(nfs, nfsfh, nfsfh->offset, count, cb, private_data, 1); |
| 2078 | } |
| 2079 | |
| 2080 | |
| 2081 | |
| 2082 | /* |
| 2083 | * Async pwrite() |
| 2084 | */ |
| 2085 | static void nfs_fill_WRITE3args (WRITE3args *args, struct nfsfh *fh, uint64_t offset, uint64_t count, |
| 2086 | void *buf) |
| 2087 | { |
| 2088 | memset(args, 0, sizeof(WRITE3args)); |
| 2089 | args->file = fh->fh; |
| 2090 | args->offset = offset; |
| 2091 | args->count = count; |
| 2092 | args->stable = fh->is_sync ? FILE_SYNC : UNSTABLE; |
| 2093 | args->data.data_len = count; |
| 2094 | args->data.data_val = buf; |
| 2095 | } |
| 2096 | |
| 2097 | static void nfs_pwrite_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2098 | { |
| 2099 | struct nfs_mcb_data *mdata = private_data; |
| 2100 | struct nfs_cb_data *data = mdata->data; |
| 2101 | struct nfs_context *nfs = data->nfs; |
| 2102 | WRITE3res *res; |
| 2103 | |
| 2104 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2105 | |
| 2106 | data->num_calls--; |
| 2107 | |
| 2108 | if (status == RPC_STATUS_ERROR) { |
| 2109 | /* flag the failure but do not invoke callback until we have received all responses */ |
| 2110 | data->error = 1; |
| 2111 | } |
| 2112 | if (status == RPC_STATUS_CANCEL) { |
| 2113 | /* flag the cancellation but do not invoke callback until we have received all responses */ |
| 2114 | data->cancel = 1; |
| 2115 | } |
| 2116 | |
| 2117 | if (status == RPC_STATUS_SUCCESS) { |
| 2118 | res = command_data; |
| 2119 | if (res->status != NFS3_OK) { |
| 2120 | rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2121 | data->error = 1; |
| 2122 | } else { |
| 2123 | uint64_t count = res->WRITE3res_u.resok.count; |
| 2124 | |
| 2125 | if (mdata->update_pos) |
| 2126 | data->nfsfh->offset += count; |
| 2127 | |
| 2128 | if (count < mdata->count) { |
| 2129 | if (count == 0) { |
| 2130 | rpc_set_error(nfs->rpc, "NFS: Write failed. No bytes written!"); |
| 2131 | data->error = 1; |
| 2132 | } else { |
| 2133 | /* reissue reminder of this write request */ |
| 2134 | WRITE3args args; |
| 2135 | mdata->offset += count; |
| 2136 | mdata->count -= count; |
| 2137 | |
| 2138 | nfs_fill_WRITE3args(&args, data->nfsfh, mdata->offset, mdata->count, |
| 2139 | &data->usrbuf[mdata->offset - data->offset]); |
| 2140 | if (rpc_nfs3_write_async(nfs->rpc, nfs_pwrite_mcb, &args, mdata) == 0) { |
| 2141 | data->num_calls++; |
| 2142 | return; |
| 2143 | } else { |
| 2144 | rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path); |
| 2145 | data->oom = 1; |
| 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | if (count > 0) { |
| 2150 | if (data->max_offset < mdata->offset + count) { |
| 2151 | data->max_offset = mdata->offset + count; |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | free(mdata); |
| 2158 | |
| 2159 | if (data->num_calls > 0) { |
| 2160 | /* still waiting for more replies */ |
| 2161 | return; |
| 2162 | } |
| 2163 | if (data->oom != 0) { |
| 2164 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
| 2165 | free_nfs_cb_data(data); |
| 2166 | return; |
| 2167 | } |
| 2168 | if (data->error != 0) { |
| 2169 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2170 | free_nfs_cb_data(data); |
| 2171 | return; |
| 2172 | } |
| 2173 | if (data->cancel != 0) { |
| 2174 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2175 | free_nfs_cb_data(data); |
| 2176 | return; |
| 2177 | } |
| 2178 | |
| 2179 | data->cb(data->max_offset - data->offset, nfs, NULL, data->private_data); |
| 2180 | |
| 2181 | free_nfs_cb_data(data); |
| 2182 | } |
| 2183 | |
| 2184 | |
| 2185 | static int nfs_pwrite_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, char *buf, nfs_cb cb, void *private_data, int update_pos) |
| 2186 | { |
| 2187 | struct nfs_cb_data *data; |
| 2188 | |
| 2189 | data = malloc(sizeof(struct nfs_cb_data)); |
| 2190 | if (data == NULL) { |
| 2191 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 2192 | return -1; |
| 2193 | } |
| 2194 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 2195 | data->nfs = nfs; |
| 2196 | data->cb = cb; |
| 2197 | data->private_data = private_data; |
| 2198 | data->nfsfh = nfsfh; |
| 2199 | data->usrbuf = buf; |
| 2200 | |
| 2201 | /* hello, clang-analyzer */ |
| 2202 | assert(data->num_calls == 0); |
| 2203 | |
| 2204 | /* chop requests into chunks of at most WRITEMAX bytes if necessary. |
| 2205 | * we send all writes in parallel so that performance is still good. |
| 2206 | */ |
| 2207 | data->max_offset = offset; |
| 2208 | data->offset = offset; |
| 2209 | |
| 2210 | do { |
| 2211 | uint64_t writecount = count; |
| 2212 | struct nfs_mcb_data *mdata; |
| 2213 | WRITE3args args; |
| 2214 | |
| 2215 | if (writecount > nfs_get_writemax(nfs)) { |
| 2216 | writecount = nfs_get_writemax(nfs); |
| 2217 | } |
| 2218 | |
| 2219 | mdata = malloc(sizeof(struct nfs_mcb_data)); |
| 2220 | if (mdata == NULL) { |
| 2221 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure"); |
| 2222 | if (data->num_calls == 0) { |
| 2223 | free_nfs_cb_data(data); |
| 2224 | return -1; |
| 2225 | } |
| 2226 | data->oom = 1; |
| 2227 | break; |
| 2228 | } |
| 2229 | memset(mdata, 0, sizeof(struct nfs_mcb_data)); |
| 2230 | mdata->data = data; |
| 2231 | mdata->offset = offset; |
| 2232 | mdata->count = writecount; |
| 2233 | mdata->update_pos = update_pos; |
| 2234 | |
| 2235 | nfs_fill_WRITE3args(&args, nfsfh, offset, writecount, &buf[offset - data->offset]); |
| 2236 | |
| 2237 | if (rpc_nfs3_write_async(nfs->rpc, nfs_pwrite_mcb, &args, mdata) != 0) { |
| 2238 | rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path); |
| 2239 | free(mdata); |
| 2240 | if (data->num_calls == 0) { |
| 2241 | free_nfs_cb_data(data); |
| 2242 | return -1; |
| 2243 | } |
| 2244 | data->oom = 1; |
| 2245 | break; |
| 2246 | } |
| 2247 | |
| 2248 | count -= writecount; |
| 2249 | offset += writecount; |
| 2250 | data->num_calls++; |
| 2251 | } while (count > 0); |
| 2252 | |
| 2253 | return 0; |
| 2254 | } |
| 2255 | |
| 2256 | 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) |
| 2257 | { |
| 2258 | return nfs_pwrite_async_internal(nfs, nfsfh, offset, count, buf, cb, private_data, 0); |
| 2259 | } |
| 2260 | |
| 2261 | /* |
| 2262 | * Async write() |
| 2263 | */ |
| 2264 | static void nfs_write_append_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2265 | { |
| 2266 | struct nfs_cb_data *data = private_data; |
| 2267 | struct nfs_context *nfs = data->nfs; |
| 2268 | GETATTR3res *res; |
| 2269 | |
| 2270 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2271 | |
| 2272 | if (status == RPC_STATUS_ERROR) { |
| 2273 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2274 | free_nfs_cb_data(data); |
| 2275 | return; |
| 2276 | } |
| 2277 | if (status == RPC_STATUS_CANCEL) { |
| 2278 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2279 | free_nfs_cb_data(data); |
| 2280 | return; |
| 2281 | } |
| 2282 | |
| 2283 | res = command_data; |
| 2284 | if (res->status != NFS3_OK) { |
| 2285 | rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2286 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2287 | free_nfs_cb_data(data); |
| 2288 | return; |
| 2289 | } |
| 2290 | |
| 2291 | if (nfs_pwrite_async_internal(nfs, data->nfsfh, res->GETATTR3res_u.resok.obj_attributes.size, data->count, data->usrbuf, data->cb, data->private_data, 1) != 0) { |
| 2292 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2293 | free_nfs_cb_data(data); |
| 2294 | return; |
| 2295 | } |
| 2296 | free_nfs_cb_data(data); |
| 2297 | } |
| 2298 | |
| 2299 | int nfs_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, char *buf, nfs_cb cb, void *private_data) |
| 2300 | { |
| 2301 | nfs_ra_invalidate(nfsfh); |
| 2302 | if (nfsfh->is_append) { |
| 2303 | struct GETATTR3args args; |
| 2304 | struct nfs_cb_data *data; |
| 2305 | |
| 2306 | data = malloc(sizeof(struct nfs_cb_data)); |
| 2307 | if (data == NULL) { |
| 2308 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 2309 | return -1; |
| 2310 | } |
| 2311 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 2312 | data->nfs = nfs; |
| 2313 | data->cb = cb; |
| 2314 | data->private_data = private_data; |
| 2315 | data->nfsfh = nfsfh; |
| 2316 | data->usrbuf = buf; |
| 2317 | data->count = count; |
| 2318 | |
| 2319 | memset(&args, 0, sizeof(GETATTR3args)); |
| 2320 | args.object = nfsfh->fh; |
| 2321 | |
| 2322 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_write_append_cb, &args, data) != 0) { |
| 2323 | rpc_set_error(nfs->rpc, "out of memory: failed to send GETATTR"); |
| 2324 | free_nfs_cb_data(data); |
| 2325 | return -1; |
| 2326 | } |
| 2327 | return 0; |
| 2328 | } |
| 2329 | return nfs_pwrite_async_internal(nfs, nfsfh, nfsfh->offset, count, buf, cb, private_data, 1); |
| 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | |
| 2334 | |
| 2335 | /* |
| 2336 | * close |
| 2337 | */ |
| 2338 | |
| 2339 | int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) |
| 2340 | { |
| 2341 | free_nfsfh(nfsfh); |
| 2342 | cb(0, nfs, NULL, private_data); |
| 2343 | return 0; |
| 2344 | }; |
| 2345 | |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | |
| 2350 | /* |
| 2351 | * Async fstat() |
| 2352 | */ |
| 2353 | int nfs_fstat_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) |
| 2354 | { |
| 2355 | struct nfs_cb_data *data; |
| 2356 | struct GETATTR3args args; |
| 2357 | |
| 2358 | data = malloc(sizeof(struct nfs_cb_data)); |
| 2359 | if (data == NULL) { |
| 2360 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 2361 | return -1; |
| 2362 | } |
| 2363 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 2364 | data->nfs = nfs; |
| 2365 | data->cb = cb; |
| 2366 | data->private_data = private_data; |
| 2367 | |
| 2368 | memset(&args, 0, sizeof(GETATTR3args)); |
| 2369 | args.object = nfsfh->fh; |
| 2370 | |
| 2371 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_stat_1_cb, &args, data) != 0) { |
| 2372 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); |
| 2373 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2374 | free_nfs_cb_data(data); |
| 2375 | return -1; |
| 2376 | } |
| 2377 | return 0; |
| 2378 | } |
| 2379 | |
| 2380 | /* |
| 2381 | * Async fstat64() |
| 2382 | */ |
| 2383 | int nfs_fstat64_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) |
| 2384 | { |
| 2385 | struct nfs_cb_data *data; |
| 2386 | struct GETATTR3args args; |
| 2387 | |
| 2388 | data = malloc(sizeof(struct nfs_cb_data)); |
| 2389 | if (data == NULL) { |
| 2390 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 2391 | return -1; |
| 2392 | } |
| 2393 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 2394 | data->nfs = nfs; |
| 2395 | data->cb = cb; |
| 2396 | data->private_data = private_data; |
| 2397 | |
| 2398 | memset(&args, 0, sizeof(GETATTR3args)); |
| 2399 | args.object = nfsfh->fh; |
| 2400 | |
| 2401 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_stat64_1_cb, &args, data) != 0) { |
| 2402 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); |
| 2403 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2404 | free_nfs_cb_data(data); |
| 2405 | return -1; |
| 2406 | } |
| 2407 | return 0; |
| 2408 | } |
| 2409 | |
| 2410 | |
| 2411 | |
| 2412 | /* |
| 2413 | * Async fsync() |
| 2414 | */ |
| 2415 | static void nfs_fsync_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2416 | { |
| 2417 | struct nfs_cb_data *data = private_data; |
| 2418 | struct nfs_context *nfs = data->nfs; |
| 2419 | COMMIT3res *res; |
| 2420 | |
| 2421 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2422 | |
| 2423 | if (status == RPC_STATUS_ERROR) { |
| 2424 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2425 | free_nfs_cb_data(data); |
| 2426 | return; |
| 2427 | } |
| 2428 | if (status == RPC_STATUS_CANCEL) { |
| 2429 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2430 | free_nfs_cb_data(data); |
| 2431 | return; |
| 2432 | } |
| 2433 | |
| 2434 | res = command_data; |
| 2435 | if (res->status != NFS3_OK) { |
| 2436 | rpc_set_error(nfs->rpc, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2437 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2438 | free_nfs_cb_data(data); |
| 2439 | return; |
| 2440 | } |
| 2441 | |
| 2442 | data->cb(0, nfs, NULL, data->private_data); |
| 2443 | free_nfs_cb_data(data); |
| 2444 | } |
| 2445 | |
| 2446 | int nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) |
| 2447 | { |
| 2448 | struct nfs_cb_data *data; |
| 2449 | struct COMMIT3args args; |
| 2450 | |
| 2451 | data = malloc(sizeof(struct nfs_cb_data)); |
| 2452 | if (data == NULL) { |
| 2453 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 2454 | return -1; |
| 2455 | } |
| 2456 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 2457 | data->nfs = nfs; |
| 2458 | data->cb = cb; |
| 2459 | data->private_data = private_data; |
| 2460 | |
| 2461 | args.file = nfsfh->fh; |
| 2462 | args.offset = 0; |
| 2463 | args.count = 0; |
| 2464 | if (rpc_nfs3_commit_async(nfs->rpc, nfs_fsync_cb, &args, data) != 0) { |
| 2465 | rpc_set_error(nfs->rpc, "RPC error: Failed to send COMMIT call for %s", data->path); |
| 2466 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2467 | free_nfs_cb_data(data); |
| 2468 | return -1; |
| 2469 | } |
| 2470 | return 0; |
| 2471 | } |
| 2472 | |
| 2473 | |
| 2474 | |
| 2475 | |
| 2476 | /* |
| 2477 | * Async ftruncate() |
| 2478 | */ |
| 2479 | static void nfs_ftruncate_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2480 | { |
| 2481 | struct nfs_cb_data *data = private_data; |
| 2482 | struct nfs_context *nfs = data->nfs; |
| 2483 | SETATTR3res *res; |
| 2484 | |
| 2485 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2486 | |
| 2487 | if (status == RPC_STATUS_ERROR) { |
| 2488 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2489 | free_nfs_cb_data(data); |
| 2490 | return; |
| 2491 | } |
| 2492 | if (status == RPC_STATUS_CANCEL) { |
| 2493 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2494 | free_nfs_cb_data(data); |
| 2495 | return; |
| 2496 | } |
| 2497 | |
| 2498 | res = command_data; |
| 2499 | if (res->status != NFS3_OK) { |
| 2500 | rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2501 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2502 | free_nfs_cb_data(data); |
| 2503 | return; |
| 2504 | } |
| 2505 | |
| 2506 | data->cb(0, nfs, NULL, data->private_data); |
| 2507 | free_nfs_cb_data(data); |
| 2508 | } |
| 2509 | |
| 2510 | int nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t length, nfs_cb cb, void *private_data) |
| 2511 | { |
| 2512 | struct nfs_cb_data *data; |
| 2513 | SETATTR3args args; |
| 2514 | |
| 2515 | data = malloc(sizeof(struct nfs_cb_data)); |
| 2516 | if (data == NULL) { |
| 2517 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); |
| 2518 | return -1; |
| 2519 | } |
| 2520 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 2521 | data->nfs = nfs; |
| 2522 | data->cb = cb; |
| 2523 | data->private_data = private_data; |
| 2524 | |
| 2525 | memset(&args, 0, sizeof(SETATTR3args)); |
| 2526 | args.object = nfsfh->fh; |
| 2527 | args.new_attributes.size.set_it = 1; |
| 2528 | args.new_attributes.size.set_size3_u.size = length; |
| 2529 | |
| 2530 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_ftruncate_cb, &args, data) != 0) { |
| 2531 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 2532 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2533 | free_nfs_cb_data(data); |
| 2534 | return -1; |
| 2535 | } |
| 2536 | return 0; |
| 2537 | } |
| 2538 | |
| 2539 | |
| 2540 | /* |
| 2541 | * Async truncate() |
| 2542 | */ |
| 2543 | static int nfs_truncate_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 2544 | { |
| 2545 | uint64_t offset = data->continue_int; |
| 2546 | struct nfsfh nfsfh; |
| 2547 | |
| 2548 | nfsfh.fh = data->fh; |
| 2549 | |
| 2550 | if (nfs_ftruncate_async(nfs, &nfsfh, offset, data->cb, data->private_data) != 0) { |
| 2551 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 2552 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2553 | free_nfs_cb_data(data); |
| 2554 | return -1; |
| 2555 | } |
| 2556 | free_nfs_cb_data(data); |
| 2557 | return 0; |
| 2558 | } |
| 2559 | |
| 2560 | int nfs_truncate_async(struct nfs_context *nfs, const char *path, uint64_t length, nfs_cb cb, void *private_data) |
| 2561 | { |
| 2562 | uint64_t offset; |
| 2563 | |
| 2564 | offset = length; |
| 2565 | |
| 2566 | if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_truncate_continue_internal, NULL, NULL, offset) != 0) { |
| 2567 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2568 | return -1; |
| 2569 | } |
| 2570 | |
| 2571 | return 0; |
| 2572 | } |
| 2573 | |
| 2574 | |
| 2575 | |
| 2576 | |
| 2577 | /* |
| 2578 | * Async mkdir() |
| 2579 | */ |
| 2580 | static void nfs_mkdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2581 | { |
| 2582 | MKDIR3res *res; |
| 2583 | struct nfs_cb_data *data = private_data; |
| 2584 | struct nfs_context *nfs = data->nfs; |
| 2585 | char *str = data->continue_data; |
| 2586 | |
| 2587 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2588 | |
| 2589 | str = &str[strlen(str) + 1]; |
| 2590 | |
| 2591 | if (status == RPC_STATUS_ERROR) { |
| 2592 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2593 | free_nfs_cb_data(data); |
| 2594 | return; |
| 2595 | } |
| 2596 | if (status == RPC_STATUS_CANCEL) { |
| 2597 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2598 | free_nfs_cb_data(data); |
| 2599 | return; |
| 2600 | } |
| 2601 | |
| 2602 | res = command_data; |
| 2603 | if (res->status != NFS3_OK) { |
| 2604 | 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)); |
| 2605 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2606 | free_nfs_cb_data(data); |
| 2607 | return; |
| 2608 | } |
| 2609 | |
| 2610 | data->cb(0, nfs, NULL, data->private_data); |
| 2611 | free_nfs_cb_data(data); |
| 2612 | } |
| 2613 | |
| 2614 | static int nfs_mkdir_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 2615 | { |
| 2616 | char *str = data->continue_data; |
| 2617 | MKDIR3args args; |
| 2618 | |
| 2619 | str = &str[strlen(str) + 1]; |
| 2620 | |
| 2621 | memset(&args, 0, sizeof(MKDIR3args)); |
| 2622 | args.where.dir = data->fh; |
| 2623 | args.where.name = str; |
| 2624 | args.attributes.mode.set_it = 1; |
| 2625 | args.attributes.mode.set_mode3_u.mode = 0755; |
| 2626 | |
| 2627 | if (rpc_nfs3_mkdir_async(nfs->rpc, nfs_mkdir_cb, &args, data) != 0) { |
| 2628 | rpc_set_error(nfs->rpc, "RPC error: Failed to send MKDIR call for %s", data->path); |
| 2629 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2630 | free_nfs_cb_data(data); |
| 2631 | return -1; |
| 2632 | } |
| 2633 | return 0; |
| 2634 | } |
| 2635 | |
| 2636 | int nfs_mkdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 2637 | { |
| 2638 | char *new_path; |
| 2639 | char *ptr; |
| 2640 | |
| 2641 | new_path = strdup(path); |
| 2642 | if (new_path == NULL) { |
| 2643 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 2644 | return -1; |
| 2645 | } |
| 2646 | |
| 2647 | ptr = strrchr(new_path, '/'); |
| 2648 | if (ptr == NULL) { |
| 2649 | free(new_path); |
| 2650 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 2651 | return -1; |
| 2652 | } |
| 2653 | *ptr = 0; |
| 2654 | |
| 2655 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 2656 | if (nfs_lookuppath_async(nfs, new_path, 0, cb, private_data, nfs_mkdir_continue_internal, new_path, free, 0) != 0) { |
| 2657 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path component"); |
| 2658 | return -1; |
| 2659 | } |
| 2660 | |
| 2661 | return 0; |
| 2662 | } |
| 2663 | |
| 2664 | |
| 2665 | |
| 2666 | |
| 2667 | |
| 2668 | /* |
| 2669 | * Async rmdir() |
| 2670 | */ |
| 2671 | static void nfs_rmdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2672 | { |
| 2673 | RMDIR3res *res; |
| 2674 | struct nfs_cb_data *data = private_data; |
| 2675 | struct nfs_context *nfs = data->nfs; |
| 2676 | char *str = data->continue_data; |
| 2677 | |
| 2678 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2679 | |
| 2680 | str = &str[strlen(str) + 1]; |
| 2681 | |
| 2682 | if (status == RPC_STATUS_ERROR) { |
| 2683 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2684 | free_nfs_cb_data(data); |
| 2685 | return; |
| 2686 | } |
| 2687 | if (status == RPC_STATUS_CANCEL) { |
| 2688 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2689 | free_nfs_cb_data(data); |
| 2690 | return; |
| 2691 | } |
| 2692 | |
| 2693 | res = command_data; |
| 2694 | if (res->status != NFS3_OK) { |
| 2695 | 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)); |
| 2696 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2697 | free_nfs_cb_data(data); |
| 2698 | return; |
| 2699 | } |
| 2700 | |
| 2701 | data->cb(0, nfs, NULL, data->private_data); |
| 2702 | free_nfs_cb_data(data); |
| 2703 | } |
| 2704 | |
| 2705 | static int nfs_rmdir_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 2706 | { |
| 2707 | char *str = data->continue_data; |
| 2708 | RMDIR3args args; |
| 2709 | |
| 2710 | str = &str[strlen(str) + 1]; |
| 2711 | |
| 2712 | args.object.dir = data->fh; |
| 2713 | args.object.name = str; |
| 2714 | if (rpc_nfs3_rmdir_async(nfs->rpc, nfs_rmdir_cb, &args, data) != 0) { |
| 2715 | rpc_set_error(nfs->rpc, "RPC error: Failed to send RMDIR call for %s", data->path); |
| 2716 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2717 | free_nfs_cb_data(data); |
| 2718 | return -1; |
| 2719 | } |
| 2720 | return 0; |
| 2721 | } |
| 2722 | |
| 2723 | int nfs_rmdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 2724 | { |
| 2725 | char *new_path; |
| 2726 | char *ptr; |
| 2727 | |
| 2728 | new_path = strdup(path); |
| 2729 | if (new_path == NULL) { |
| 2730 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 2731 | return -1; |
| 2732 | } |
| 2733 | |
| 2734 | ptr = strrchr(new_path, '/'); |
| 2735 | if (ptr == NULL) { |
| 2736 | free(new_path); |
| 2737 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 2738 | return -1; |
| 2739 | } |
| 2740 | *ptr = 0; |
| 2741 | |
| 2742 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 2743 | if (nfs_lookuppath_async(nfs, new_path, 0, cb, private_data, nfs_rmdir_continue_internal, new_path, free, 0) != 0) { |
| 2744 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2745 | return -1; |
| 2746 | } |
| 2747 | |
| 2748 | return 0; |
| 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | |
| 2753 | |
| 2754 | /* |
| 2755 | * Async creat() |
| 2756 | */ |
| 2757 | struct create_cb_data { |
| 2758 | char *path; |
| 2759 | int flags; |
| 2760 | int mode; |
| 2761 | }; |
| 2762 | |
| 2763 | static void free_create_cb_data(void *ptr) |
| 2764 | { |
| 2765 | struct create_cb_data *data = ptr; |
| 2766 | |
| 2767 | free(data->path); |
| 2768 | free(data); |
| 2769 | } |
| 2770 | |
| 2771 | static void nfs_create_trunc_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2772 | { |
| 2773 | struct nfs_cb_data *data = private_data; |
| 2774 | struct nfs_context *nfs = data->nfs; |
| 2775 | struct nfsfh *nfsfh = data->nfsfh; |
| 2776 | SETATTR3res *res; |
| 2777 | |
| 2778 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2779 | |
| 2780 | if (status == RPC_STATUS_ERROR) { |
| 2781 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2782 | free_nfs_cb_data(data); |
| 2783 | free_nfsfh(nfsfh); |
| 2784 | return; |
| 2785 | } |
| 2786 | if (status == RPC_STATUS_CANCEL) { |
| 2787 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2788 | free_nfs_cb_data(data); |
| 2789 | free_nfsfh(nfsfh); |
| 2790 | return; |
| 2791 | } |
| 2792 | |
| 2793 | res = command_data; |
| 2794 | if (res->status != NFS3_OK) { |
| 2795 | rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 2796 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2797 | free_nfs_cb_data(data); |
| 2798 | free_nfsfh(nfsfh); |
| 2799 | return; |
| 2800 | } |
| 2801 | |
| 2802 | data->cb(0, nfs, nfsfh, data->private_data); |
| 2803 | free_nfs_cb_data(data); |
| 2804 | } |
| 2805 | |
| 2806 | static void nfs_create_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2807 | { |
| 2808 | LOOKUP3res *res; |
| 2809 | struct nfs_cb_data *data = private_data; |
| 2810 | struct nfs_context *nfs = data->nfs; |
| 2811 | struct nfsfh *nfsfh; |
| 2812 | struct create_cb_data *cb_data = data->continue_data; |
| 2813 | char *str = cb_data->path; |
| 2814 | |
| 2815 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2816 | |
| 2817 | if (status == RPC_STATUS_ERROR) { |
| 2818 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2819 | free_nfs_cb_data(data); |
| 2820 | return; |
| 2821 | } |
| 2822 | if (status == RPC_STATUS_CANCEL) { |
| 2823 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2824 | free_nfs_cb_data(data); |
| 2825 | return; |
| 2826 | } |
| 2827 | |
| 2828 | str = &str[strlen(str) + 1]; |
| 2829 | res = command_data; |
| 2830 | if (res->status != NFS3_OK) { |
| 2831 | 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)); |
| 2832 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2833 | |
| 2834 | return; |
| 2835 | } |
| 2836 | |
| 2837 | nfsfh = malloc(sizeof(struct nfsfh)); |
| 2838 | if (nfsfh == NULL) { |
| 2839 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); |
| 2840 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2841 | free_nfs_cb_data(data); |
| 2842 | return; |
| 2843 | } |
| 2844 | memset(nfsfh, 0, sizeof(struct nfsfh)); |
| 2845 | |
| 2846 | if (cb_data->flags & O_SYNC) { |
| 2847 | nfsfh->is_sync = 1; |
| 2848 | } |
| 2849 | if (cb_data->flags & O_APPEND) { |
| 2850 | nfsfh->is_append = 1; |
| 2851 | } |
| 2852 | |
| 2853 | /* copy the filehandle */ |
| 2854 | nfsfh->fh.data.data_len = res->LOOKUP3res_u.resok.object.data.data_len; |
| 2855 | nfsfh->fh.data.data_val = malloc(nfsfh->fh.data.data_len); |
| 2856 | if (nfsfh->fh.data.data_val == NULL) { |
| 2857 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh structure"); |
| 2858 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2859 | free_nfs_cb_data(data); |
| 2860 | free(nfsfh); |
| 2861 | return; |
| 2862 | } |
| 2863 | memcpy(nfsfh->fh.data.data_val, res->LOOKUP3res_u.resok.object.data.data_val, nfsfh->fh.data.data_len); |
| 2864 | |
| 2865 | /* Try to truncate it if we were requested to */ |
| 2866 | if (cb_data->flags & O_TRUNC) { |
| 2867 | SETATTR3args args; |
| 2868 | |
| 2869 | data->nfsfh = nfsfh; |
| 2870 | |
| 2871 | memset(&args, 0, sizeof(SETATTR3args)); |
| 2872 | args.object = nfsfh->fh; |
| 2873 | args.new_attributes.size.set_it = 1; |
| 2874 | args.new_attributes.size.set_size3_u.size = 0; |
| 2875 | |
| 2876 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_create_trunc_cb, |
| 2877 | &args, data) != 0) { |
| 2878 | rpc_set_error(nfs->rpc, "RPC error: Failed to send " |
| 2879 | "SETATTR call for %s", data->path); |
| 2880 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), |
| 2881 | data->private_data); |
| 2882 | free_nfs_cb_data(data); |
| 2883 | free_nfsfh(nfsfh); |
| 2884 | return; |
| 2885 | } |
| 2886 | return; |
| 2887 | } |
| 2888 | |
| 2889 | data->cb(0, nfs, nfsfh, data->private_data); |
| 2890 | free_nfs_cb_data(data); |
| 2891 | } |
| 2892 | |
| 2893 | |
| 2894 | |
| 2895 | static void nfs_create_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 2896 | { |
| 2897 | CREATE3res *res; |
| 2898 | struct nfs_cb_data *data = private_data; |
| 2899 | struct nfs_context *nfs = data->nfs; |
| 2900 | struct create_cb_data *cb_data = data->continue_data; |
| 2901 | char *str = cb_data->path; |
| 2902 | LOOKUP3args args; |
| 2903 | |
| 2904 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 2905 | |
| 2906 | if (status == RPC_STATUS_ERROR) { |
| 2907 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 2908 | free_nfs_cb_data(data); |
| 2909 | return; |
| 2910 | } |
| 2911 | if (status == RPC_STATUS_CANCEL) { |
| 2912 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 2913 | free_nfs_cb_data(data); |
| 2914 | return; |
| 2915 | } |
| 2916 | |
| 2917 | str = &str[strlen(str) + 1]; |
| 2918 | res = command_data; |
| 2919 | if (res->status != NFS3_OK) { |
| 2920 | 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)); |
| 2921 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2922 | free_nfs_cb_data(data); |
| 2923 | return; |
| 2924 | } |
| 2925 | |
| 2926 | memset(&args, 0, sizeof(LOOKUP3args)); |
| 2927 | args.what.dir = data->fh; |
| 2928 | args.what.name = str; |
| 2929 | |
| 2930 | if (rpc_nfs3_lookup_async(nfs->rpc, nfs_create_2_cb, &args, data) != 0) { |
| 2931 | rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s/%s", data->saved_path, str); |
| 2932 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2933 | free_nfs_cb_data(data); |
| 2934 | return; |
| 2935 | } |
| 2936 | return; |
| 2937 | } |
| 2938 | |
| 2939 | static int nfs_create_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 2940 | { |
| 2941 | struct create_cb_data *cb_data = data->continue_data; |
| 2942 | char *str = cb_data->path; |
| 2943 | CREATE3args args; |
| 2944 | |
| 2945 | str = &str[strlen(str) + 1]; |
| 2946 | |
| 2947 | memset(&args, 0, sizeof(CREATE3args)); |
| 2948 | args.where.dir = data->fh; |
| 2949 | args.where.name = str; |
| 2950 | args.how.mode = (cb_data->flags & O_EXCL) ? GUARDED : UNCHECKED; |
| 2951 | args.how.createhow3_u.obj_attributes.mode.set_it = 1; |
| 2952 | args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = cb_data->mode; |
| 2953 | |
| 2954 | if (rpc_nfs3_create_async(nfs->rpc, nfs_create_1_cb, &args, data) != 0) { |
| 2955 | rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str); |
| 2956 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 2957 | free_nfs_cb_data(data); |
| 2958 | return -1; |
| 2959 | } |
| 2960 | return 0; |
| 2961 | } |
| 2962 | |
| 2963 | int nfs_create_async(struct nfs_context *nfs, const char *path, int flags, int mode, nfs_cb cb, void *private_data) |
| 2964 | { |
| 2965 | struct create_cb_data *cb_data; |
| 2966 | char *ptr; |
| 2967 | |
| 2968 | cb_data = malloc(sizeof(struct create_cb_data)); |
| 2969 | if (cb_data == NULL) { |
| 2970 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for cb data"); |
| 2971 | return -1; |
| 2972 | } |
| 2973 | |
| 2974 | cb_data->path = strdup(path); |
| 2975 | if (cb_data->path == NULL) { |
| 2976 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 2977 | free(cb_data); |
| 2978 | return -1; |
| 2979 | } |
| 2980 | |
| 2981 | ptr = strrchr(cb_data->path, '/'); |
| 2982 | if (ptr == NULL) { |
| 2983 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 2984 | free_create_cb_data(cb_data); |
| 2985 | return -1; |
| 2986 | } |
| 2987 | *ptr = 0; |
| 2988 | |
| 2989 | cb_data->flags = flags; |
| 2990 | cb_data->mode = mode; |
| 2991 | |
| 2992 | /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */ |
| 2993 | if (nfs_lookuppath_async(nfs, cb_data->path, 0, cb, private_data, nfs_create_continue_internal, cb_data, free_create_cb_data, 0) != 0) { |
| 2994 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 2995 | return -1; |
| 2996 | } |
| 2997 | |
| 2998 | return 0; |
| 2999 | } |
| 3000 | |
| 3001 | int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) |
| 3002 | { |
| 3003 | return nfs_create_async(nfs, path, 0, mode, cb, private_data); |
| 3004 | } |
| 3005 | |
| 3006 | |
| 3007 | |
| 3008 | |
| 3009 | /* |
| 3010 | * Async unlink() |
| 3011 | */ |
| 3012 | static void nfs_unlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3013 | { |
| 3014 | REMOVE3res *res; |
| 3015 | struct nfs_cb_data *data = private_data; |
| 3016 | struct nfs_context *nfs = data->nfs; |
| 3017 | char *str = data->continue_data; |
| 3018 | |
| 3019 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3020 | |
| 3021 | str = &str[strlen(str) + 1]; |
| 3022 | |
| 3023 | if (status == RPC_STATUS_ERROR) { |
| 3024 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3025 | free_nfs_cb_data(data); |
| 3026 | return; |
| 3027 | } |
| 3028 | if (status == RPC_STATUS_CANCEL) { |
| 3029 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3030 | free_nfs_cb_data(data); |
| 3031 | return; |
| 3032 | } |
| 3033 | |
| 3034 | res = command_data; |
| 3035 | if (res->status != NFS3_OK) { |
| 3036 | 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)); |
| 3037 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3038 | free_nfs_cb_data(data); |
| 3039 | return; |
| 3040 | } |
| 3041 | |
| 3042 | data->cb(0, nfs, NULL, data->private_data); |
| 3043 | free_nfs_cb_data(data); |
| 3044 | } |
| 3045 | |
| 3046 | static int nfs_unlink_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 3047 | { |
| 3048 | char *str = data->continue_data; |
| 3049 | struct REMOVE3args args; |
| 3050 | |
| 3051 | str = &str[strlen(str) + 1]; |
| 3052 | |
| 3053 | args.object.dir = data->fh; |
| 3054 | args.object.name = str; |
| 3055 | if (rpc_nfs3_remove_async(nfs->rpc, nfs_unlink_cb, &args, data) != 0) { |
| 3056 | rpc_set_error(nfs->rpc, "RPC error: Failed to send REMOVE call for %s", data->path); |
| 3057 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3058 | free_nfs_cb_data(data); |
| 3059 | return -1; |
| 3060 | } |
| 3061 | return 0; |
| 3062 | } |
| 3063 | |
| 3064 | int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 3065 | { |
| 3066 | char *new_path; |
| 3067 | char *ptr; |
| 3068 | |
| 3069 | new_path = strdup(path); |
| 3070 | if (new_path == NULL) { |
| 3071 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 3072 | return -1; |
| 3073 | } |
| 3074 | |
| 3075 | ptr = strrchr(new_path, '/'); |
| 3076 | if (ptr == NULL) { |
| 3077 | free(new_path); |
| 3078 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 3079 | return -1; |
| 3080 | } |
| 3081 | *ptr = 0; |
| 3082 | |
| 3083 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 3084 | if (nfs_lookuppath_async(nfs, new_path, 0, cb, private_data, nfs_unlink_continue_internal, new_path, free, 0) != 0) { |
| 3085 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3086 | return -1; |
| 3087 | } |
| 3088 | |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | /* |
| 3094 | * Async mknod() |
| 3095 | */ |
| 3096 | struct mknod_cb_data { |
| 3097 | char *path; |
| 3098 | int mode; |
| 3099 | int major; |
| 3100 | int minor; |
| 3101 | }; |
| 3102 | |
| 3103 | static void free_mknod_cb_data(void *ptr) |
| 3104 | { |
| 3105 | struct mknod_cb_data *data = ptr; |
| 3106 | |
| 3107 | free(data->path); |
| 3108 | free(data); |
| 3109 | } |
| 3110 | |
| 3111 | static void nfs_mknod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3112 | { |
| 3113 | MKNOD3res *res; |
| 3114 | struct nfs_cb_data *data = private_data; |
| 3115 | struct nfs_context *nfs = data->nfs; |
| 3116 | char *str = data->continue_data; |
| 3117 | |
| 3118 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3119 | |
| 3120 | str = &str[strlen(str) + 1]; |
| 3121 | |
| 3122 | if (status == RPC_STATUS_ERROR) { |
| 3123 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3124 | free_nfs_cb_data(data); |
| 3125 | return; |
| 3126 | } |
| 3127 | if (status == RPC_STATUS_CANCEL) { |
| 3128 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3129 | free_nfs_cb_data(data); |
| 3130 | return; |
| 3131 | } |
| 3132 | |
| 3133 | res = command_data; |
| 3134 | if (res->status != NFS3_OK) { |
| 3135 | 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)); |
| 3136 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3137 | free_nfs_cb_data(data); |
| 3138 | return; |
| 3139 | } |
| 3140 | |
| 3141 | data->cb(0, nfs, NULL, data->private_data); |
| 3142 | free_nfs_cb_data(data); |
| 3143 | } |
| 3144 | |
| 3145 | static int nfs_mknod_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 3146 | { |
| 3147 | struct mknod_cb_data *cb_data = data->continue_data; |
| 3148 | char *str = cb_data->path; |
| 3149 | MKNOD3args args; |
| 3150 | |
| 3151 | str = &str[strlen(str) + 1]; |
| 3152 | |
| 3153 | args.where.dir = data->fh; |
| 3154 | args.where.name = str; |
| 3155 | switch (cb_data->mode & S_IFMT) { |
| 3156 | case S_IFCHR: |
| 3157 | args.what.type = NF3CHR; |
| 3158 | args.what.mknoddata3_u.chr_device.dev_attributes.mode.set_it = 1; |
| 3159 | args.what.mknoddata3_u.chr_device.dev_attributes.mode.set_mode3_u.mode = cb_data->mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); |
| 3160 | args.what.mknoddata3_u.chr_device.spec.specdata1 = cb_data->major; |
| 3161 | args.what.mknoddata3_u.chr_device.spec.specdata2 = cb_data->minor; |
| 3162 | break; |
| 3163 | case S_IFBLK: |
| 3164 | args.what.type = NF3BLK; |
| 3165 | args.what.mknoddata3_u.blk_device.dev_attributes.mode.set_it = 1; |
| 3166 | args.what.mknoddata3_u.blk_device.dev_attributes.mode.set_mode3_u.mode = cb_data->mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); |
| 3167 | args.what.mknoddata3_u.blk_device.spec.specdata1 = cb_data->major; |
| 3168 | args.what.mknoddata3_u.blk_device.spec.specdata2 = cb_data->minor; |
| 3169 | case S_IFSOCK: |
| 3170 | args.what.type = NF3SOCK; |
| 3171 | args.what.mknoddata3_u.sock_attributes.mode.set_it = 1; |
| 3172 | args.what.mknoddata3_u.sock_attributes.mode.set_mode3_u.mode = cb_data->mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); |
| 3173 | break; |
| 3174 | case S_IFIFO: |
| 3175 | args.what.type = NF3FIFO; |
| 3176 | args.what.mknoddata3_u.pipe_attributes.mode.set_it = 1; |
| 3177 | args.what.mknoddata3_u.pipe_attributes.mode.set_mode3_u.mode = cb_data->mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); |
| 3178 | break; |
| 3179 | default: |
| 3180 | rpc_set_error(nfs->rpc, "Invalid file type for NFS3/MKNOD call"); |
| 3181 | data->cb(-EINVAL, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3182 | free_nfs_cb_data(data); |
| 3183 | return -1; |
| 3184 | } |
| 3185 | |
| 3186 | if (rpc_nfs3_mknod_async(nfs->rpc, nfs_mknod_cb, &args, data) != 0) { |
| 3187 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3188 | free_nfs_cb_data(data); |
| 3189 | return -1; |
| 3190 | } |
| 3191 | return 0; |
| 3192 | } |
| 3193 | |
| 3194 | int nfs_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev, nfs_cb cb, void *private_data) |
| 3195 | { |
| 3196 | char *ptr; |
| 3197 | struct mknod_cb_data *cb_data; |
| 3198 | |
| 3199 | cb_data = malloc(sizeof(struct mknod_cb_data)); |
| 3200 | if (cb_data == NULL) { |
| 3201 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for cb data"); |
| 3202 | return -1; |
| 3203 | } |
| 3204 | |
| 3205 | cb_data->path = strdup(path); |
| 3206 | if (cb_data->path == NULL) { |
| 3207 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
| 3208 | free(cb_data); |
| 3209 | return -1; |
| 3210 | } |
| 3211 | |
| 3212 | ptr = strrchr(cb_data->path, '/'); |
| 3213 | if (ptr == NULL) { |
| 3214 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
| 3215 | free_mknod_cb_data(cb_data); |
| 3216 | return -1; |
| 3217 | } |
| 3218 | *ptr = 0; |
| 3219 | |
| 3220 | cb_data->mode = mode; |
| 3221 | cb_data->major = major(dev); |
| 3222 | cb_data->minor = minor(dev); |
| 3223 | |
| 3224 | /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ |
| 3225 | if (nfs_lookuppath_async(nfs, cb_data->path, 0, cb, private_data, nfs_mknod_continue_internal, cb_data, free_mknod_cb_data, 0) != 0) { |
| 3226 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3227 | return -1; |
| 3228 | } |
| 3229 | |
| 3230 | return 0; |
| 3231 | } |
| 3232 | |
| 3233 | /* |
| 3234 | * Async opendir() |
| 3235 | */ |
| 3236 | |
| 3237 | /* ReadDirPlus Emulation Callback data */ |
| 3238 | struct rdpe_cb_data { |
| 3239 | int getattrcount; |
| 3240 | int status; |
| 3241 | struct nfs_cb_data *data; |
| 3242 | }; |
| 3243 | |
| 3244 | /* ReadDirPlus Emulation LOOKUP Callback data */ |
| 3245 | struct rdpe_lookup_cb_data { |
| 3246 | struct rdpe_cb_data *rdpe_cb_data; |
| 3247 | struct nfsdirent *nfsdirent; |
| 3248 | }; |
| 3249 | |
| 3250 | /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */ |
| 3251 | static void nfs_opendir3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3252 | { |
| 3253 | LOOKUP3res *res = command_data; |
| 3254 | struct rdpe_lookup_cb_data *rdpe_lookup_cb_data = private_data; |
| 3255 | struct rdpe_cb_data *rdpe_cb_data = rdpe_lookup_cb_data->rdpe_cb_data; |
| 3256 | struct nfs_cb_data *data = rdpe_cb_data->data; |
| 3257 | struct nfsdir *nfsdir = data->continue_data; |
| 3258 | struct nfs_context *nfs = data->nfs; |
| 3259 | struct nfsdirent *nfsdirent = rdpe_lookup_cb_data->nfsdirent; |
| 3260 | |
| 3261 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3262 | |
| 3263 | free(rdpe_lookup_cb_data); |
| 3264 | |
| 3265 | rdpe_cb_data->getattrcount--; |
| 3266 | |
| 3267 | if (status == RPC_STATUS_ERROR) { |
| 3268 | rpc_set_error(nfs->rpc, "LOOKUP during READDIRPLUS emulation " |
| 3269 | "failed with RPC_STATUS_ERROR"); |
| 3270 | rdpe_cb_data->status = RPC_STATUS_ERROR; |
| 3271 | } |
| 3272 | if (status == RPC_STATUS_CANCEL) { |
| 3273 | rpc_set_error(nfs->rpc, "LOOKUP during READDIRPLUS emulation " |
| 3274 | "failed with RPC_STATUS_CANCEL"); |
| 3275 | rdpe_cb_data->status = RPC_STATUS_CANCEL; |
| 3276 | } |
| 3277 | if (status == RPC_STATUS_SUCCESS && res->status == NFS3_OK) { |
| 3278 | if (res->LOOKUP3res_u.resok.obj_attributes.attributes_follow) { |
| 3279 | fattr3 *attributes = &res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes; |
| 3280 | |
| 3281 | nfsdirent->type = attributes->type; |
| 3282 | nfsdirent->mode = attributes->mode; |
| 3283 | nfsdirent->size = attributes->size; |
| 3284 | |
| 3285 | nfsdirent->atime.tv_sec = attributes->atime.seconds; |
| 3286 | nfsdirent->atime.tv_usec = attributes->atime.nseconds/1000; |
| 3287 | nfsdirent->atime_nsec = attributes->atime.nseconds; |
| 3288 | nfsdirent->mtime.tv_sec = attributes->mtime.seconds; |
| 3289 | nfsdirent->mtime.tv_usec = attributes->mtime.nseconds/1000; |
| 3290 | nfsdirent->mtime_nsec = attributes->mtime.nseconds; |
| 3291 | nfsdirent->ctime.tv_sec = attributes->ctime.seconds; |
| 3292 | nfsdirent->ctime.tv_usec = attributes->ctime.nseconds/1000; |
| 3293 | nfsdirent->ctime_nsec = attributes->ctime.nseconds; |
| 3294 | nfsdirent->uid = attributes->uid; |
| 3295 | nfsdirent->gid = attributes->gid; |
| 3296 | nfsdirent->nlink = attributes->nlink; |
| 3297 | nfsdirent->dev = attributes->fsid; |
| 3298 | nfsdirent->rdev = specdata3_to_rdev(&attributes->rdev); |
| 3299 | nfsdirent->blksize = NFS_BLKSIZE; |
| 3300 | nfsdirent->blocks = (attributes->used + 512 - 1) / 512; |
| 3301 | nfsdirent->used = attributes->used; |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | if (rdpe_cb_data->getattrcount == 0) { |
| 3306 | if (rdpe_cb_data->status != RPC_STATUS_SUCCESS) { |
| 3307 | rpc_set_error(nfs->rpc, "READDIRPLUS emulation " |
| 3308 | "failed: %s", rpc_get_error(rpc)); |
| 3309 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), |
| 3310 | data->private_data); |
| 3311 | nfs_free_nfsdir(nfsdir); |
| 3312 | } else { |
| 3313 | data->cb(0, nfs, nfsdir, data->private_data); |
| 3314 | } |
| 3315 | free(rdpe_cb_data); |
| 3316 | |
| 3317 | data->continue_data = NULL; |
| 3318 | free_nfs_cb_data(data); |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3323 | { |
| 3324 | READDIR3res *res = command_data; |
| 3325 | struct nfs_cb_data *data = private_data; |
| 3326 | struct nfs_context *nfs = data->nfs; |
| 3327 | struct nfsdir *nfsdir = data->continue_data; |
| 3328 | struct nfsdirent *nfsdirent; |
| 3329 | struct entry3 *entry; |
| 3330 | uint64_t cookie = 0; |
| 3331 | struct rdpe_cb_data *rdpe_cb_data; |
| 3332 | |
| 3333 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3334 | |
| 3335 | if (status == RPC_STATUS_ERROR) { |
| 3336 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3337 | nfs_free_nfsdir(nfsdir); |
| 3338 | data->continue_data = NULL; |
| 3339 | free_nfs_cb_data(data); |
| 3340 | return; |
| 3341 | } |
| 3342 | |
| 3343 | if (status == RPC_STATUS_CANCEL) { |
| 3344 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3345 | nfs_free_nfsdir(nfsdir); |
| 3346 | data->continue_data = NULL; |
| 3347 | free_nfs_cb_data(data); |
| 3348 | return; |
| 3349 | } |
| 3350 | |
| 3351 | if (res->status != NFS3_OK) { |
| 3352 | 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)); |
| 3353 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3354 | nfs_free_nfsdir(nfsdir); |
| 3355 | data->continue_data = NULL; |
| 3356 | free_nfs_cb_data(data); |
| 3357 | return; |
| 3358 | } |
| 3359 | |
| 3360 | entry =res->READDIR3res_u.resok.reply.entries; |
| 3361 | while (entry != NULL) { |
| 3362 | nfsdirent = malloc(sizeof(struct nfsdirent)); |
| 3363 | if (nfsdirent == NULL) { |
| 3364 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data); |
| 3365 | nfs_free_nfsdir(nfsdir); |
| 3366 | data->continue_data = NULL; |
| 3367 | free_nfs_cb_data(data); |
| 3368 | return; |
| 3369 | } |
| 3370 | memset(nfsdirent, 0, sizeof(struct nfsdirent)); |
| 3371 | nfsdirent->name = strdup(entry->name); |
| 3372 | if (nfsdirent->name == NULL) { |
| 3373 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data); |
| 3374 | free(nfsdirent); |
| 3375 | nfs_free_nfsdir(nfsdir); |
| 3376 | data->continue_data = NULL; |
| 3377 | free_nfs_cb_data(data); |
| 3378 | return; |
| 3379 | } |
| 3380 | nfsdirent->inode = entry->fileid; |
| 3381 | |
| 3382 | nfsdirent->next = nfsdir->entries; |
| 3383 | nfsdir->entries = nfsdirent; |
| 3384 | |
| 3385 | cookie = entry->cookie; |
| 3386 | entry = entry->nextentry; |
| 3387 | } |
| 3388 | |
| 3389 | if (res->READDIR3res_u.resok.reply.eof == 0) { |
| 3390 | READDIR3args args; |
| 3391 | |
| 3392 | args.dir = data->fh; |
| 3393 | args.cookie = cookie; |
| 3394 | memcpy(&args.cookieverf, res->READDIR3res_u.resok.cookieverf, sizeof(cookieverf3)); |
| 3395 | args.count = 8192; |
| 3396 | |
| 3397 | if (rpc_nfs3_readdir_async(nfs->rpc, nfs_opendir2_cb, &args, data) != 0) { |
| 3398 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path); |
| 3399 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3400 | nfs_free_nfsdir(nfsdir); |
| 3401 | data->continue_data = NULL; |
| 3402 | free_nfs_cb_data(data); |
| 3403 | return; |
| 3404 | } |
| 3405 | return; |
| 3406 | } |
| 3407 | |
| 3408 | if (res->READDIR3res_u.resok.dir_attributes.attributes_follow) |
| 3409 | nfsdir->attr = res->READDIR3res_u.resok.dir_attributes.post_op_attr_u.attributes; |
| 3410 | |
| 3411 | /* steal the dirhandle */ |
| 3412 | nfsdir->current = nfsdir->entries; |
| 3413 | |
| 3414 | if (nfsdir->entries) { |
| 3415 | rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data)); |
| 3416 | rdpe_cb_data->getattrcount = 0; |
| 3417 | rdpe_cb_data->status = RPC_STATUS_SUCCESS; |
| 3418 | rdpe_cb_data->data = data; |
| 3419 | for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) { |
| 3420 | struct rdpe_lookup_cb_data *rdpe_lookup_cb_data; |
| 3421 | LOOKUP3args args; |
| 3422 | |
| 3423 | rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data)); |
| 3424 | rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data; |
| 3425 | rdpe_lookup_cb_data->nfsdirent = nfsdirent; |
| 3426 | |
| 3427 | memset(&args, 0, sizeof(LOOKUP3args)); |
| 3428 | args.what.dir = data->fh; |
| 3429 | args.what.name = nfsdirent->name; |
| 3430 | |
| 3431 | if (rpc_nfs3_lookup_async(nfs->rpc, nfs_opendir3_cb, &args, rdpe_lookup_cb_data) != 0) { |
| 3432 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call"); |
| 3433 | |
| 3434 | /* if we have already commands in flight, we cant just stop, we have to wait for the |
| 3435 | * commands in flight to complete |
| 3436 | */ |
| 3437 | if (rdpe_cb_data->getattrcount > 0) { |
| 3438 | nfs_free_nfsdir(nfsdir); |
| 3439 | data->continue_data = NULL; |
| 3440 | free_nfs_cb_data(data); |
| 3441 | rdpe_cb_data->status = RPC_STATUS_ERROR; |
| 3442 | free(rdpe_lookup_cb_data); |
| 3443 | return; |
| 3444 | } |
| 3445 | |
| 3446 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3447 | nfs_free_nfsdir(nfsdir); |
| 3448 | data->continue_data = NULL; |
| 3449 | free_nfs_cb_data(data); |
| 3450 | free(rdpe_lookup_cb_data); |
| 3451 | free(rdpe_cb_data); |
| 3452 | return; |
| 3453 | } |
| 3454 | rdpe_cb_data->getattrcount++; |
| 3455 | } |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3460 | { |
| 3461 | READDIRPLUS3res *res = command_data; |
| 3462 | struct nfs_cb_data *data = private_data; |
| 3463 | struct nfs_context *nfs = data->nfs; |
| 3464 | struct nfsdir *nfsdir = data->continue_data; |
| 3465 | struct entryplus3 *entry; |
| 3466 | uint64_t cookie = 0; |
| 3467 | |
| 3468 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3469 | |
| 3470 | if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){ |
| 3471 | READDIR3args args; |
| 3472 | |
| 3473 | args.dir = data->fh; |
| 3474 | args.cookie = cookie; |
| 3475 | memset(&args.cookieverf, 0, sizeof(cookieverf3)); |
| 3476 | args.count = 8192; |
| 3477 | |
| 3478 | if (rpc_nfs3_readdir_async(nfs->rpc, nfs_opendir2_cb, &args, data) != 0) { |
| 3479 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path); |
| 3480 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3481 | nfs_free_nfsdir(nfsdir); |
| 3482 | data->continue_data = NULL; |
| 3483 | free_nfs_cb_data(data); |
| 3484 | return; |
| 3485 | } |
| 3486 | return; |
| 3487 | } |
| 3488 | |
| 3489 | if (status == RPC_STATUS_CANCEL) { |
| 3490 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3491 | nfs_free_nfsdir(nfsdir); |
| 3492 | data->continue_data = NULL; |
| 3493 | free_nfs_cb_data(data); |
| 3494 | return; |
| 3495 | } |
| 3496 | |
| 3497 | if (res->status != NFS3_OK) { |
| 3498 | 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)); |
| 3499 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3500 | nfs_free_nfsdir(nfsdir); |
| 3501 | data->continue_data = NULL; |
| 3502 | free_nfs_cb_data(data); |
| 3503 | return; |
| 3504 | } |
| 3505 | |
| 3506 | entry =res->READDIRPLUS3res_u.resok.reply.entries; |
| 3507 | while (entry != NULL) { |
| 3508 | struct nfsdirent *nfsdirent; |
| 3509 | |
| 3510 | nfsdirent = malloc(sizeof(struct nfsdirent)); |
| 3511 | if (nfsdirent == NULL) { |
| 3512 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data); |
| 3513 | nfs_free_nfsdir(nfsdir); |
| 3514 | data->continue_data = NULL; |
| 3515 | free_nfs_cb_data(data); |
| 3516 | return; |
| 3517 | } |
| 3518 | memset(nfsdirent, 0, sizeof(struct nfsdirent)); |
| 3519 | nfsdirent->name = strdup(entry->name); |
| 3520 | if (nfsdirent->name == NULL) { |
| 3521 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data); |
| 3522 | free(nfsdirent); |
| 3523 | nfs_free_nfsdir(nfsdir); |
| 3524 | data->continue_data = NULL; |
| 3525 | free_nfs_cb_data(data); |
| 3526 | return; |
| 3527 | } |
| 3528 | nfsdirent->inode = entry->fileid; |
| 3529 | if (entry->name_attributes.attributes_follow) { |
| 3530 | nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type; |
| 3531 | nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode; |
| 3532 | nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size; |
| 3533 | |
| 3534 | nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds; |
| 3535 | nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000; |
| 3536 | nfsdirent->atime_nsec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds; |
| 3537 | nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds; |
| 3538 | nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000; |
| 3539 | nfsdirent->mtime_nsec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds; |
| 3540 | nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds; |
| 3541 | nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000; |
| 3542 | nfsdirent->ctime_nsec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds; |
| 3543 | nfsdirent->uid = entry->name_attributes.post_op_attr_u.attributes.uid; |
| 3544 | nfsdirent->gid = entry->name_attributes.post_op_attr_u.attributes.gid; |
| 3545 | nfsdirent->nlink = entry->name_attributes.post_op_attr_u.attributes.nlink; |
| 3546 | nfsdirent->dev = entry->name_attributes.post_op_attr_u.attributes.fsid; |
| 3547 | nfsdirent->rdev = specdata3_to_rdev(&entry->name_attributes.post_op_attr_u.attributes.rdev); |
| 3548 | nfsdirent->blksize = NFS_BLKSIZE; |
| 3549 | nfsdirent->blocks = (entry->name_attributes.post_op_attr_u.attributes.used + 512 - 1) / 512; |
| 3550 | nfsdirent->used = entry->name_attributes.post_op_attr_u.attributes.used; |
| 3551 | } |
| 3552 | |
| 3553 | nfsdirent->next = nfsdir->entries; |
| 3554 | nfsdir->entries = nfsdirent; |
| 3555 | |
| 3556 | cookie = entry->cookie; |
| 3557 | entry = entry->nextentry; |
| 3558 | } |
| 3559 | |
| 3560 | if (res->READDIRPLUS3res_u.resok.reply.eof == 0) { |
| 3561 | READDIRPLUS3args args; |
| 3562 | |
| 3563 | args.dir = data->fh; |
| 3564 | args.cookie = cookie; |
| 3565 | memcpy(&args.cookieverf, res->READDIRPLUS3res_u.resok.cookieverf, sizeof(cookieverf3)); |
| 3566 | args.dircount = 8192; |
| 3567 | args.maxcount = 8192; |
| 3568 | |
| 3569 | if (rpc_nfs3_readdirplus_async(nfs->rpc, nfs_opendir_cb, &args, data) != 0) { |
| 3570 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path); |
| 3571 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3572 | nfs_free_nfsdir(nfsdir); |
| 3573 | data->continue_data = NULL; |
| 3574 | free_nfs_cb_data(data); |
| 3575 | return; |
| 3576 | } |
| 3577 | return; |
| 3578 | } |
| 3579 | |
| 3580 | if (res->READDIRPLUS3res_u.resok.dir_attributes.attributes_follow) |
| 3581 | nfsdir->attr = res->READDIRPLUS3res_u.resok.dir_attributes.post_op_attr_u.attributes; |
| 3582 | |
| 3583 | /* steal the dirhandle */ |
| 3584 | data->continue_data = NULL; |
| 3585 | nfsdir->current = nfsdir->entries; |
| 3586 | |
| 3587 | data->cb(0, nfs, nfsdir, data->private_data); |
| 3588 | free_nfs_cb_data(data); |
| 3589 | } |
| 3590 | |
| 3591 | static int nfs_opendir_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 3592 | { |
| 3593 | READDIRPLUS3args args; |
| 3594 | struct nfsdir *nfsdir = data->continue_data;; |
| 3595 | struct nfsdir *cached; |
| 3596 | |
| 3597 | cached = nfs_dircache_find(nfs, &data->fh); |
| 3598 | if (cached) { |
| 3599 | if (attr && attr->mtime.seconds == cached->attr.mtime.seconds) { |
| 3600 | cached->current = cached->entries; |
| 3601 | data->cb(0, nfs, cached, data->private_data); |
| 3602 | free_nfs_cb_data(data); |
| 3603 | return 0; |
| 3604 | } else { |
| 3605 | /* cache must be stale */ |
| 3606 | nfs_free_nfsdir(cached); |
| 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | nfsdir->fh.data.data_len = data->fh.data.data_len; |
| 3611 | nfsdir->fh.data.data_val = malloc(nfsdir->fh.data.data_len); |
| 3612 | if (nfsdir->fh.data.data_val == NULL) { |
| 3613 | rpc_set_error(nfs->rpc, "OOM when allocating fh for nfsdir"); |
| 3614 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3615 | free_nfs_cb_data(data); |
| 3616 | return -1; |
| 3617 | } |
| 3618 | memcpy(nfsdir->fh.data.data_val, data->fh.data.data_val, data->fh.data.data_len); |
| 3619 | |
| 3620 | args.dir = data->fh; |
| 3621 | args.cookie = 0; |
| 3622 | memset(&args.cookieverf, 0, sizeof(cookieverf3)); |
| 3623 | args.dircount = 8192; |
| 3624 | args.maxcount = 8192; |
| 3625 | if (rpc_nfs3_readdirplus_async(nfs->rpc, nfs_opendir_cb, &args, data) != 0) { |
| 3626 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path); |
| 3627 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3628 | free_nfs_cb_data(data); |
| 3629 | return -1; |
| 3630 | } |
| 3631 | return 0; |
| 3632 | } |
| 3633 | |
| 3634 | int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 3635 | { |
| 3636 | struct nfsdir *nfsdir; |
| 3637 | |
| 3638 | nfsdir = malloc(sizeof(struct nfsdir)); |
| 3639 | if (nfsdir == NULL) { |
| 3640 | rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir"); |
| 3641 | return -1; |
| 3642 | } |
| 3643 | memset(nfsdir, 0, sizeof(struct nfsdir)); |
| 3644 | |
| 3645 | if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) { |
| 3646 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3647 | return -1; |
| 3648 | } |
| 3649 | |
| 3650 | return 0; |
| 3651 | } |
| 3652 | |
| 3653 | |
| 3654 | struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir) |
| 3655 | { |
| 3656 | struct nfsdirent *nfsdirent = nfsdir->current; |
| 3657 | |
| 3658 | if (nfsdir->current != NULL) { |
| 3659 | nfsdir->current = nfsdir->current->next; |
| 3660 | } |
| 3661 | return nfsdirent; |
| 3662 | } |
| 3663 | |
| 3664 | |
| 3665 | /* |
| 3666 | * closedir() |
| 3667 | */ |
| 3668 | void nfs_closedir(struct nfs_context *nfs, struct nfsdir *nfsdir) |
| 3669 | { |
| 3670 | nfs_dircache_add(nfs, nfsdir); |
| 3671 | } |
| 3672 | |
| 3673 | |
| 3674 | /* |
| 3675 | * getcwd() |
| 3676 | */ |
| 3677 | void nfs_getcwd(struct nfs_context *nfs, const char **cwd) |
| 3678 | { |
| 3679 | if (cwd) { |
| 3680 | *cwd = nfs->cwd; |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | |
| 3685 | /* |
| 3686 | * Async lseek() |
| 3687 | */ |
| 3688 | struct lseek_cb_data { |
| 3689 | struct nfs_context *nfs; |
| 3690 | struct nfsfh *nfsfh; |
| 3691 | int64_t offset; |
| 3692 | nfs_cb cb; |
| 3693 | void *private_data; |
| 3694 | }; |
| 3695 | |
| 3696 | static void nfs_lseek_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3697 | { |
| 3698 | GETATTR3res *res; |
| 3699 | struct lseek_cb_data *data = private_data; |
| 3700 | struct nfs_context *nfs = data->nfs; |
| 3701 | uint64_t size = 0; |
| 3702 | |
| 3703 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3704 | |
| 3705 | if (status == RPC_STATUS_ERROR) { |
| 3706 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3707 | free(data); |
| 3708 | return; |
| 3709 | } |
| 3710 | if (status == RPC_STATUS_CANCEL) { |
| 3711 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3712 | free(data); |
| 3713 | return; |
| 3714 | } |
| 3715 | |
| 3716 | res = command_data; |
| 3717 | if (res->status != NFS3_OK) { |
| 3718 | rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 3719 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3720 | free(data); |
| 3721 | return; |
| 3722 | } |
| 3723 | |
| 3724 | size = res->GETATTR3res_u.resok.obj_attributes.size; |
| 3725 | |
| 3726 | if (data->offset < 0 && |
| 3727 | (uint64_t)(-data->offset) > size) { |
| 3728 | data->cb(-EINVAL, nfs, &data->nfsfh->offset, data->private_data); |
| 3729 | } else { |
| 3730 | data->nfsfh->offset = data->offset + size; |
| 3731 | data->cb(0, nfs, &data->nfsfh->offset, data->private_data); |
| 3732 | } |
| 3733 | |
| 3734 | free(data); |
| 3735 | } |
| 3736 | |
| 3737 | int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int64_t offset, int whence, nfs_cb cb, void *private_data) |
| 3738 | { |
| 3739 | struct lseek_cb_data *data; |
| 3740 | struct GETATTR3args args; |
| 3741 | |
| 3742 | if (whence == SEEK_SET) { |
| 3743 | if (offset < 0) { |
| 3744 | cb(-EINVAL, nfs, &nfsfh->offset, private_data); |
| 3745 | } else { |
| 3746 | nfsfh->offset = offset; |
| 3747 | cb(0, nfs, &nfsfh->offset, private_data); |
| 3748 | } |
| 3749 | return 0; |
| 3750 | } |
| 3751 | if (whence == SEEK_CUR) { |
| 3752 | if (offset < 0 && |
| 3753 | nfsfh->offset < (uint64_t)(-offset)) { |
| 3754 | cb(-EINVAL, nfs, &nfsfh->offset, private_data); |
| 3755 | } else { |
| 3756 | nfsfh->offset += offset; |
| 3757 | cb(0, nfs, &nfsfh->offset, private_data); |
| 3758 | } |
| 3759 | return 0; |
| 3760 | } |
| 3761 | |
| 3762 | data = malloc(sizeof(struct lseek_cb_data)); |
| 3763 | if (data == NULL) { |
| 3764 | rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data"); |
| 3765 | return -1; |
| 3766 | } |
| 3767 | |
| 3768 | data->nfs = nfs; |
| 3769 | data->nfsfh = nfsfh; |
| 3770 | data->offset = offset; |
| 3771 | data->cb = cb; |
| 3772 | data->private_data = private_data; |
| 3773 | |
| 3774 | memset(&args, 0, sizeof(GETATTR3args)); |
| 3775 | args.object = nfsfh->fh; |
| 3776 | |
| 3777 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_lseek_1_cb, &args, data) != 0) { |
| 3778 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call"); |
| 3779 | free(data); |
| 3780 | return -1; |
| 3781 | } |
| 3782 | return 0; |
| 3783 | } |
| 3784 | |
| 3785 | |
| 3786 | |
| 3787 | |
| 3788 | /* |
| 3789 | * Async statvfs() |
| 3790 | */ |
| 3791 | static void nfs_statvfs_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3792 | { |
| 3793 | FSSTAT3res *res; |
| 3794 | struct nfs_cb_data *data = private_data; |
| 3795 | struct nfs_context *nfs = data->nfs; |
| 3796 | struct statvfs svfs; |
| 3797 | |
| 3798 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3799 | |
| 3800 | if (status == RPC_STATUS_ERROR) { |
| 3801 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3802 | free_nfs_cb_data(data); |
| 3803 | return; |
| 3804 | } |
| 3805 | if (status == RPC_STATUS_CANCEL) { |
| 3806 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3807 | free_nfs_cb_data(data); |
| 3808 | return; |
| 3809 | } |
| 3810 | |
| 3811 | res = command_data; |
| 3812 | if (res->status != NFS3_OK) { |
| 3813 | 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)); |
| 3814 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3815 | free_nfs_cb_data(data); |
| 3816 | return; |
| 3817 | } |
| 3818 | |
| 3819 | svfs.f_bsize = NFS_BLKSIZE; |
| 3820 | svfs.f_frsize = NFS_BLKSIZE; |
| 3821 | svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/NFS_BLKSIZE; |
| 3822 | svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/NFS_BLKSIZE; |
| 3823 | svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/NFS_BLKSIZE; |
| 3824 | svfs.f_files = res->FSSTAT3res_u.resok.tfiles; |
| 3825 | svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles; |
| 3826 | #if !defined(ANDROID) |
| 3827 | svfs.f_favail = res->FSSTAT3res_u.resok.afiles; |
| 3828 | svfs.f_fsid = 0; |
| 3829 | svfs.f_flag = 0; |
| 3830 | svfs.f_namemax = 256; |
| 3831 | #endif |
| 3832 | |
| 3833 | data->cb(0, nfs, &svfs, data->private_data); |
| 3834 | free_nfs_cb_data(data); |
| 3835 | } |
| 3836 | |
| 3837 | static int nfs_statvfs_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 3838 | { |
| 3839 | FSSTAT3args args; |
| 3840 | |
| 3841 | args.fsroot = data->fh; |
| 3842 | if (rpc_nfs3_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &args, data) != 0) { |
| 3843 | rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT call for %s", data->path); |
| 3844 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3845 | free_nfs_cb_data(data); |
| 3846 | return -1; |
| 3847 | } |
| 3848 | return 0; |
| 3849 | } |
| 3850 | |
| 3851 | int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 3852 | { |
| 3853 | if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) { |
| 3854 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3855 | return -1; |
| 3856 | } |
| 3857 | |
| 3858 | return 0; |
| 3859 | } |
| 3860 | |
| 3861 | |
| 3862 | |
| 3863 | |
| 3864 | /* |
| 3865 | * Async readlink() |
| 3866 | */ |
| 3867 | static void nfs_readlink_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3868 | { |
| 3869 | READLINK3res *res; |
| 3870 | struct nfs_cb_data *data = private_data; |
| 3871 | struct nfs_context *nfs = data->nfs; |
| 3872 | |
| 3873 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3874 | |
| 3875 | if (status == RPC_STATUS_ERROR) { |
| 3876 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3877 | free_nfs_cb_data(data); |
| 3878 | return; |
| 3879 | } |
| 3880 | if (status == RPC_STATUS_CANCEL) { |
| 3881 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3882 | free_nfs_cb_data(data); |
| 3883 | return; |
| 3884 | } |
| 3885 | |
| 3886 | res = command_data; |
| 3887 | if (res->status != NFS3_OK) { |
| 3888 | 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)); |
| 3889 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3890 | free_nfs_cb_data(data); |
| 3891 | return; |
| 3892 | } |
| 3893 | |
| 3894 | |
| 3895 | data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data); |
| 3896 | free_nfs_cb_data(data); |
| 3897 | } |
| 3898 | |
| 3899 | static int nfs_readlink_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 3900 | { |
| 3901 | READLINK3args args; |
| 3902 | |
| 3903 | args.symlink = data->fh; |
| 3904 | |
| 3905 | if (rpc_nfs3_readlink_async(nfs->rpc, nfs_readlink_1_cb, &args, data) != 0) { |
| 3906 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path); |
| 3907 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3908 | free_nfs_cb_data(data); |
| 3909 | return -1; |
| 3910 | } |
| 3911 | return 0; |
| 3912 | } |
| 3913 | |
| 3914 | int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) |
| 3915 | { |
| 3916 | if (nfs_lookuppath_async(nfs, path, 1, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) { |
| 3917 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3918 | return -1; |
| 3919 | } |
| 3920 | |
| 3921 | return 0; |
| 3922 | } |
| 3923 | |
| 3924 | |
| 3925 | |
| 3926 | |
| 3927 | /* |
| 3928 | * Async chmod() |
| 3929 | */ |
| 3930 | static void nfs_chmod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 3931 | { |
| 3932 | struct nfs_cb_data *data = private_data; |
| 3933 | struct nfs_context *nfs = data->nfs; |
| 3934 | SETATTR3res *res; |
| 3935 | |
| 3936 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 3937 | |
| 3938 | if (status == RPC_STATUS_ERROR) { |
| 3939 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 3940 | free_nfs_cb_data(data); |
| 3941 | return; |
| 3942 | } |
| 3943 | if (status == RPC_STATUS_CANCEL) { |
| 3944 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 3945 | free_nfs_cb_data(data); |
| 3946 | return; |
| 3947 | } |
| 3948 | |
| 3949 | res = command_data; |
| 3950 | if (res->status != NFS3_OK) { |
| 3951 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 3952 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3953 | free_nfs_cb_data(data); |
| 3954 | return; |
| 3955 | } |
| 3956 | |
| 3957 | data->cb(0, nfs, NULL, data->private_data); |
| 3958 | free_nfs_cb_data(data); |
| 3959 | } |
| 3960 | |
| 3961 | static int nfs_chmod_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 3962 | { |
| 3963 | SETATTR3args args; |
| 3964 | |
| 3965 | memset(&args, 0, sizeof(SETATTR3args)); |
| 3966 | args.object = data->fh; |
| 3967 | args.new_attributes.mode.set_it = 1; |
| 3968 | args.new_attributes.mode.set_mode3_u.mode = data->continue_int; |
| 3969 | |
| 3970 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) { |
| 3971 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 3972 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 3973 | free_nfs_cb_data(data); |
| 3974 | return -1; |
| 3975 | } |
| 3976 | return 0; |
| 3977 | } |
| 3978 | |
| 3979 | |
| 3980 | int nfs_chmod_async_internal(struct nfs_context *nfs, const char *path, int no_follow, int mode, nfs_cb cb, void *private_data) |
| 3981 | { |
| 3982 | if (nfs_lookuppath_async(nfs, path, no_follow, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) { |
| 3983 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 3984 | return -1; |
| 3985 | } |
| 3986 | |
| 3987 | return 0; |
| 3988 | } |
| 3989 | |
| 3990 | int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) |
| 3991 | { |
| 3992 | return nfs_chmod_async_internal(nfs, path, 0, mode, cb, private_data); |
| 3993 | } |
| 3994 | |
| 3995 | int nfs_lchmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) |
| 3996 | { |
| 3997 | return nfs_chmod_async_internal(nfs, path, 1, mode, cb, private_data); |
| 3998 | } |
| 3999 | |
| 4000 | /* |
| 4001 | * Async fchmod() |
| 4002 | */ |
| 4003 | int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data) |
| 4004 | { |
| 4005 | struct nfs_cb_data *data; |
| 4006 | |
| 4007 | data = malloc(sizeof(struct nfs_cb_data)); |
| 4008 | if (data == NULL) { |
| 4009 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data"); |
| 4010 | return -1; |
| 4011 | } |
| 4012 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 4013 | data->nfs = nfs; |
| 4014 | data->cb = cb; |
| 4015 | data->private_data = private_data; |
| 4016 | data->continue_int = mode; |
| 4017 | data->fh.data.data_len = nfsfh->fh.data.data_len; |
| 4018 | data->fh.data.data_val = malloc(data->fh.data.data_len); |
| 4019 | if (data->fh.data.data_val == NULL) { |
| 4020 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh"); |
| 4021 | free_nfs_cb_data(data); |
| 4022 | return -1; |
| 4023 | } |
| 4024 | memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len); |
| 4025 | |
| 4026 | if (nfs_chmod_continue_internal(nfs, NULL, data) != 0) { |
| 4027 | return -1; |
| 4028 | } |
| 4029 | |
| 4030 | return 0; |
| 4031 | } |
| 4032 | |
| 4033 | |
| 4034 | |
| 4035 | /* |
| 4036 | * Async chown() |
| 4037 | */ |
| 4038 | static void nfs_chown_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 4039 | { |
| 4040 | struct nfs_cb_data *data = private_data; |
| 4041 | struct nfs_context *nfs = data->nfs; |
| 4042 | SETATTR3res *res; |
| 4043 | |
| 4044 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4045 | |
| 4046 | if (status == RPC_STATUS_ERROR) { |
| 4047 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 4048 | free_nfs_cb_data(data); |
| 4049 | return; |
| 4050 | } |
| 4051 | if (status == RPC_STATUS_CANCEL) { |
| 4052 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 4053 | free_nfs_cb_data(data); |
| 4054 | return; |
| 4055 | } |
| 4056 | |
| 4057 | res = command_data; |
| 4058 | if (res->status != NFS3_OK) { |
| 4059 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 4060 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4061 | free_nfs_cb_data(data); |
| 4062 | return; |
| 4063 | } |
| 4064 | |
| 4065 | data->cb(0, nfs, NULL, data->private_data); |
| 4066 | free_nfs_cb_data(data); |
| 4067 | } |
| 4068 | |
| 4069 | struct nfs_chown_data { |
| 4070 | uid_t uid; |
| 4071 | gid_t gid; |
| 4072 | }; |
| 4073 | |
| 4074 | static int nfs_chown_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 4075 | { |
| 4076 | SETATTR3args args; |
| 4077 | struct nfs_chown_data *chown_data = data->continue_data; |
| 4078 | |
| 4079 | memset(&args, 0, sizeof(SETATTR3args)); |
| 4080 | args.object = data->fh; |
| 4081 | if (chown_data->uid != (uid_t)-1) { |
| 4082 | args.new_attributes.uid.set_it = 1; |
| 4083 | args.new_attributes.uid.set_uid3_u.uid = chown_data->uid; |
| 4084 | } |
| 4085 | if (chown_data->gid != (gid_t)-1) { |
| 4086 | args.new_attributes.gid.set_it = 1; |
| 4087 | args.new_attributes.gid.set_gid3_u.gid = chown_data->gid; |
| 4088 | } |
| 4089 | |
| 4090 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) { |
| 4091 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 4092 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4093 | free_nfs_cb_data(data); |
| 4094 | return -1; |
| 4095 | } |
| 4096 | return 0; |
| 4097 | } |
| 4098 | |
| 4099 | |
| 4100 | int nfs_chown_async_internal(struct nfs_context *nfs, const char *path, int no_follow, int uid, int gid, nfs_cb cb, void *private_data) |
| 4101 | { |
| 4102 | struct nfs_chown_data *chown_data; |
| 4103 | |
| 4104 | chown_data = malloc(sizeof(struct nfs_chown_data)); |
| 4105 | if (chown_data == NULL) { |
| 4106 | rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure"); |
| 4107 | return -1; |
| 4108 | } |
| 4109 | |
| 4110 | chown_data->uid = uid; |
| 4111 | chown_data->gid = gid; |
| 4112 | |
| 4113 | if (nfs_lookuppath_async(nfs, path, no_follow, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) { |
| 4114 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 4115 | return -1; |
| 4116 | } |
| 4117 | |
| 4118 | return 0; |
| 4119 | } |
| 4120 | |
| 4121 | int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data) |
| 4122 | { |
| 4123 | return nfs_chown_async_internal(nfs, path, 0, uid, gid, cb, private_data); |
| 4124 | } |
| 4125 | |
| 4126 | int nfs_lchown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data) |
| 4127 | { |
| 4128 | return nfs_chown_async_internal(nfs, path, 1, uid, gid, cb, private_data); |
| 4129 | } |
| 4130 | |
| 4131 | /* |
| 4132 | * Async fchown() |
| 4133 | */ |
| 4134 | int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data) |
| 4135 | { |
| 4136 | struct nfs_cb_data *data; |
| 4137 | struct nfs_chown_data *chown_data; |
| 4138 | |
| 4139 | chown_data = malloc(sizeof(struct nfs_chown_data)); |
| 4140 | if (chown_data == NULL) { |
| 4141 | rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure"); |
| 4142 | return -1; |
| 4143 | } |
| 4144 | |
| 4145 | chown_data->uid = uid; |
| 4146 | chown_data->gid = gid; |
| 4147 | |
| 4148 | data = malloc(sizeof(struct nfs_cb_data)); |
| 4149 | if (data == NULL) { |
| 4150 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data"); |
| 4151 | free(chown_data); |
| 4152 | return -1; |
| 4153 | } |
| 4154 | memset(data, 0, sizeof(struct nfs_cb_data)); |
| 4155 | data->nfs = nfs; |
| 4156 | data->cb = cb; |
| 4157 | data->private_data = private_data; |
| 4158 | data->continue_data = chown_data; |
| 4159 | data->free_continue_data = free; |
| 4160 | data->fh.data.data_len = nfsfh->fh.data.data_len; |
| 4161 | data->fh.data.data_val = malloc(data->fh.data.data_len); |
| 4162 | if (data->fh.data.data_val == NULL) { |
| 4163 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh"); |
| 4164 | free_nfs_cb_data(data); |
| 4165 | return -1; |
| 4166 | } |
| 4167 | memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len); |
| 4168 | |
| 4169 | if (nfs_chown_continue_internal(nfs, NULL, data) != 0) { |
| 4170 | return -1; |
| 4171 | } |
| 4172 | |
| 4173 | return 0; |
| 4174 | } |
| 4175 | |
| 4176 | |
| 4177 | |
| 4178 | |
| 4179 | |
| 4180 | /* |
| 4181 | * Async utimes() |
| 4182 | */ |
| 4183 | static void nfs_utimes_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 4184 | { |
| 4185 | struct nfs_cb_data *data = private_data; |
| 4186 | struct nfs_context *nfs = data->nfs; |
| 4187 | SETATTR3res *res; |
| 4188 | |
| 4189 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4190 | |
| 4191 | if (status == RPC_STATUS_ERROR) { |
| 4192 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 4193 | free_nfs_cb_data(data); |
| 4194 | return; |
| 4195 | } |
| 4196 | if (status == RPC_STATUS_CANCEL) { |
| 4197 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 4198 | free_nfs_cb_data(data); |
| 4199 | return; |
| 4200 | } |
| 4201 | |
| 4202 | res = command_data; |
| 4203 | if (res->status != NFS3_OK) { |
| 4204 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); |
| 4205 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4206 | free_nfs_cb_data(data); |
| 4207 | return; |
| 4208 | } |
| 4209 | |
| 4210 | data->cb(0, nfs, NULL, data->private_data); |
| 4211 | free_nfs_cb_data(data); |
| 4212 | } |
| 4213 | |
| 4214 | static int nfs_utimes_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 4215 | { |
| 4216 | SETATTR3args args; |
| 4217 | struct timeval *utimes_data = data->continue_data; |
| 4218 | |
| 4219 | memset(&args, 0, sizeof(SETATTR3args)); |
| 4220 | args.object = data->fh; |
| 4221 | if (utimes_data != NULL) { |
| 4222 | args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME; |
| 4223 | args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec; |
| 4224 | args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000; |
| 4225 | args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME; |
| 4226 | args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec; |
| 4227 | args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000; |
| 4228 | } else { |
| 4229 | args.new_attributes.atime.set_it = SET_TO_SERVER_TIME; |
| 4230 | args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME; |
| 4231 | } |
| 4232 | |
| 4233 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) { |
| 4234 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
| 4235 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4236 | free_nfs_cb_data(data); |
| 4237 | return -1; |
| 4238 | } |
| 4239 | return 0; |
| 4240 | } |
| 4241 | |
| 4242 | int nfs_utimes_async_internal(struct nfs_context *nfs, const char *path, int no_follow, struct timeval *times, nfs_cb cb, void *private_data) |
| 4243 | { |
| 4244 | struct timeval *new_times = NULL; |
| 4245 | |
| 4246 | if (times != NULL) { |
| 4247 | new_times = malloc(sizeof(struct timeval)*2); |
| 4248 | if (new_times == NULL) { |
| 4249 | rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure"); |
| 4250 | return -1; |
| 4251 | } |
| 4252 | |
| 4253 | memcpy(new_times, times, sizeof(struct timeval)*2); |
| 4254 | } |
| 4255 | |
| 4256 | if (nfs_lookuppath_async(nfs, path, no_follow, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) { |
| 4257 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 4258 | return -1; |
| 4259 | } |
| 4260 | |
| 4261 | return 0; |
| 4262 | } |
| 4263 | |
| 4264 | int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data) |
| 4265 | { |
| 4266 | return nfs_utimes_async_internal(nfs, path, 0, times, cb, private_data); |
| 4267 | } |
| 4268 | |
| 4269 | int nfs_lutimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data) |
| 4270 | { |
| 4271 | return nfs_utimes_async_internal(nfs, path, 1, times, cb, private_data); |
| 4272 | } |
| 4273 | |
| 4274 | /* |
| 4275 | * Async utime() |
| 4276 | */ |
| 4277 | int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data) |
| 4278 | { |
| 4279 | struct timeval *new_times = NULL; |
| 4280 | |
| 4281 | if (times != NULL) { |
| 4282 | new_times = malloc(sizeof(struct timeval)*2); |
| 4283 | if (new_times == NULL) { |
| 4284 | rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure"); |
| 4285 | return -1; |
| 4286 | } |
| 4287 | |
| 4288 | new_times[0].tv_sec = times->actime; |
| 4289 | new_times[0].tv_usec = 0; |
| 4290 | new_times[1].tv_sec = times->modtime; |
| 4291 | new_times[1].tv_usec = 0; |
| 4292 | } |
| 4293 | |
| 4294 | if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) { |
| 4295 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 4296 | return -1; |
| 4297 | } |
| 4298 | |
| 4299 | return 0; |
| 4300 | } |
| 4301 | |
| 4302 | |
| 4303 | /* |
| 4304 | * Async access() |
| 4305 | */ |
| 4306 | static void nfs_access_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 4307 | { |
| 4308 | ACCESS3res *res; |
| 4309 | struct nfs_cb_data *data = private_data; |
| 4310 | struct nfs_context *nfs = data->nfs; |
| 4311 | unsigned int nfsmode = 0; |
| 4312 | |
| 4313 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4314 | |
| 4315 | if (status == RPC_STATUS_ERROR) { |
| 4316 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 4317 | free_nfs_cb_data(data); |
| 4318 | return; |
| 4319 | } |
| 4320 | if (status == RPC_STATUS_CANCEL) { |
| 4321 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 4322 | free_nfs_cb_data(data); |
| 4323 | return; |
| 4324 | } |
| 4325 | |
| 4326 | res = command_data; |
| 4327 | if (res->status != NFS3_OK) { |
| 4328 | 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)); |
| 4329 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4330 | free_nfs_cb_data(data); |
| 4331 | return; |
| 4332 | } |
| 4333 | |
| 4334 | if (data->continue_int & R_OK) { |
| 4335 | nfsmode |= ACCESS3_READ; |
| 4336 | } |
| 4337 | if (data->continue_int & W_OK) { |
| 4338 | nfsmode |= ACCESS3_MODIFY; |
| 4339 | } |
| 4340 | if (data->continue_int & X_OK) { |
| 4341 | nfsmode |= ACCESS3_EXECUTE; |
| 4342 | } |
| 4343 | |
| 4344 | if (res->ACCESS3res_u.resok.access != nfsmode) { |
| 4345 | rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c", |
| 4346 | nfsmode&ACCESS3_READ?'r':'-', |
| 4347 | nfsmode&ACCESS3_MODIFY?'w':'-', |
| 4348 | nfsmode&ACCESS3_EXECUTE?'x':'-', |
| 4349 | res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-', |
| 4350 | res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-', |
| 4351 | res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-'); |
| 4352 | data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4353 | free_nfs_cb_data(data); |
| 4354 | return; |
| 4355 | } |
| 4356 | |
| 4357 | data->cb(0, nfs, NULL, data->private_data); |
| 4358 | free_nfs_cb_data(data); |
| 4359 | } |
| 4360 | |
| 4361 | static int nfs_access_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 4362 | { |
| 4363 | int nfsmode = 0; |
| 4364 | ACCESS3args args; |
| 4365 | |
| 4366 | if (data->continue_int & R_OK) { |
| 4367 | nfsmode |= ACCESS3_READ; |
| 4368 | } |
| 4369 | if (data->continue_int & W_OK) { |
| 4370 | nfsmode |= ACCESS3_MODIFY; |
| 4371 | } |
| 4372 | if (data->continue_int & X_OK) { |
| 4373 | nfsmode |= ACCESS3_EXECUTE; |
| 4374 | } |
| 4375 | |
| 4376 | memset(&args, 0, sizeof(ACCESS3args)); |
| 4377 | args.object = data->fh; |
| 4378 | args.access = nfsmode; |
| 4379 | |
| 4380 | if (rpc_nfs3_access_async(nfs->rpc, nfs_access_cb, &args, data) != 0) { |
| 4381 | rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path); |
| 4382 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4383 | free_nfs_cb_data(data); |
| 4384 | return -1; |
| 4385 | } |
| 4386 | return 0; |
| 4387 | } |
| 4388 | |
| 4389 | int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) |
| 4390 | { |
| 4391 | if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) { |
| 4392 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 4393 | return -1; |
| 4394 | } |
| 4395 | |
| 4396 | return 0; |
| 4397 | } |
| 4398 | |
| 4399 | |
| 4400 | |
| 4401 | /* |
| 4402 | * Async symlink() |
| 4403 | */ |
| 4404 | struct nfs_symlink_data { |
| 4405 | char *oldpath; |
| 4406 | char *newpathparent; |
| 4407 | char *newpathobject; |
| 4408 | }; |
| 4409 | |
| 4410 | static void free_nfs_symlink_data(void *mem) |
| 4411 | { |
| 4412 | struct nfs_symlink_data *data = mem; |
| 4413 | |
| 4414 | if (data->oldpath != NULL) { |
| 4415 | free(data->oldpath); |
| 4416 | } |
| 4417 | if (data->newpathparent != NULL) { |
| 4418 | free(data->newpathparent); |
| 4419 | } |
| 4420 | if (data->newpathobject != NULL) { |
| 4421 | free(data->newpathobject); |
| 4422 | } |
| 4423 | free(data); |
| 4424 | } |
| 4425 | |
| 4426 | static void nfs_symlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 4427 | { |
| 4428 | SYMLINK3res *res; |
| 4429 | struct nfs_cb_data *data = private_data; |
| 4430 | struct nfs_context *nfs = data->nfs; |
| 4431 | struct nfs_symlink_data *symlink_data = data->continue_data; |
| 4432 | |
| 4433 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4434 | |
| 4435 | if (status == RPC_STATUS_ERROR) { |
| 4436 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 4437 | free_nfs_cb_data(data); |
| 4438 | return; |
| 4439 | } |
| 4440 | if (status == RPC_STATUS_CANCEL) { |
| 4441 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 4442 | free_nfs_cb_data(data); |
| 4443 | return; |
| 4444 | } |
| 4445 | |
| 4446 | res = command_data; |
| 4447 | if (res->status != NFS3_OK) { |
| 4448 | 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)); |
| 4449 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4450 | free_nfs_cb_data(data); |
| 4451 | return; |
| 4452 | } |
| 4453 | |
| 4454 | data->cb(0, nfs, NULL, data->private_data); |
| 4455 | free_nfs_cb_data(data); |
| 4456 | } |
| 4457 | |
| 4458 | static int nfs_symlink_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 4459 | { |
| 4460 | struct nfs_symlink_data *symlink_data = data->continue_data; |
| 4461 | SYMLINK3args args; |
| 4462 | |
| 4463 | memset(&args, 0, sizeof(SYMLINK3args)); |
| 4464 | args.where.dir = data->fh; |
| 4465 | args.where.name = symlink_data->newpathobject; |
| 4466 | args.symlink.symlink_attributes.mode.set_it = 1; |
| 4467 | args.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; |
| 4468 | args.symlink.symlink_data = symlink_data->oldpath; |
| 4469 | |
| 4470 | if (rpc_nfs3_symlink_async(nfs->rpc, nfs_symlink_cb, &args, data) != 0) { |
| 4471 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path); |
| 4472 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4473 | free_nfs_cb_data(data); |
| 4474 | return -1; |
| 4475 | } |
| 4476 | return 0; |
| 4477 | } |
| 4478 | |
| 4479 | int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) |
| 4480 | { |
| 4481 | char *ptr; |
| 4482 | struct nfs_symlink_data *symlink_data; |
| 4483 | |
| 4484 | symlink_data = malloc(sizeof(struct nfs_symlink_data)); |
| 4485 | if (symlink_data == NULL) { |
| 4486 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data"); |
| 4487 | return -1; |
| 4488 | } |
| 4489 | memset(symlink_data, 0, sizeof(struct nfs_symlink_data)); |
| 4490 | |
| 4491 | symlink_data->oldpath = strdup(oldpath); |
| 4492 | if (symlink_data->oldpath == NULL) { |
| 4493 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
| 4494 | free_nfs_symlink_data(symlink_data); |
| 4495 | return -1; |
| 4496 | } |
| 4497 | |
| 4498 | symlink_data->newpathparent = strdup(newpath); |
| 4499 | if (symlink_data->newpathparent == NULL) { |
| 4500 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path"); |
| 4501 | free_nfs_symlink_data(symlink_data); |
| 4502 | return -1; |
| 4503 | } |
| 4504 | |
| 4505 | ptr = strrchr(symlink_data->newpathparent, '/'); |
| 4506 | if (ptr == NULL) { |
| 4507 | rpc_set_error(nfs->rpc, "Invalid path %s", oldpath); |
| 4508 | free_nfs_symlink_data(symlink_data); |
| 4509 | return -1; |
| 4510 | } |
| 4511 | *ptr = 0; |
| 4512 | ptr++; |
| 4513 | |
| 4514 | symlink_data->newpathobject = strdup(ptr); |
| 4515 | if (symlink_data->newpathobject == NULL) { |
| 4516 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path"); |
| 4517 | free_nfs_symlink_data(symlink_data); |
| 4518 | return -1; |
| 4519 | } |
| 4520 | |
| 4521 | if (nfs_lookuppath_async(nfs, symlink_data->newpathparent, 0, cb, private_data, nfs_symlink_continue_internal, symlink_data, free_nfs_symlink_data, 0) != 0) { |
| 4522 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 4523 | return -1; |
| 4524 | } |
| 4525 | |
| 4526 | return 0; |
| 4527 | } |
| 4528 | |
| 4529 | |
| 4530 | |
| 4531 | /* |
| 4532 | * Async rename() |
| 4533 | */ |
| 4534 | struct nfs_rename_data { |
| 4535 | char *oldpath; |
| 4536 | char *oldobject; |
| 4537 | struct nfs_fh3 olddir; |
| 4538 | char *newpath; |
| 4539 | char *newobject; |
| 4540 | struct nfs_fh3 newdir; |
| 4541 | }; |
| 4542 | |
| 4543 | static void free_nfs_rename_data(void *mem) |
| 4544 | { |
| 4545 | struct nfs_rename_data *data = mem; |
| 4546 | |
| 4547 | if (data->oldpath != NULL) { |
| 4548 | free(data->oldpath); |
| 4549 | } |
| 4550 | if (data->olddir.data.data_val != NULL) { |
| 4551 | free(data->olddir.data.data_val); |
| 4552 | } |
| 4553 | if (data->newpath != NULL) { |
| 4554 | free(data->newpath); |
| 4555 | } |
| 4556 | if (data->newdir.data.data_val != NULL) { |
| 4557 | free(data->newdir.data.data_val); |
| 4558 | } |
| 4559 | free(data); |
| 4560 | } |
| 4561 | |
| 4562 | static void nfs_rename_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 4563 | { |
| 4564 | RENAME3res *res; |
| 4565 | struct nfs_cb_data *data = private_data; |
| 4566 | struct nfs_context *nfs = data->nfs; |
| 4567 | struct nfs_rename_data *rename_data = data->continue_data; |
| 4568 | |
| 4569 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4570 | |
| 4571 | if (status == RPC_STATUS_ERROR) { |
| 4572 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 4573 | free_nfs_cb_data(data); |
| 4574 | return; |
| 4575 | } |
| 4576 | if (status == RPC_STATUS_CANCEL) { |
| 4577 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 4578 | free_nfs_cb_data(data); |
| 4579 | return; |
| 4580 | } |
| 4581 | |
| 4582 | res = command_data; |
| 4583 | if (res->status != NFS3_OK) { |
| 4584 | 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)); |
| 4585 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4586 | free_nfs_cb_data(data); |
| 4587 | return; |
| 4588 | } |
| 4589 | |
| 4590 | data->cb(0, nfs, NULL, data->private_data); |
| 4591 | free_nfs_cb_data(data); |
| 4592 | } |
| 4593 | |
| 4594 | static int nfs_rename_continue_2_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 4595 | { |
| 4596 | struct nfs_rename_data *rename_data = data->continue_data; |
| 4597 | RENAME3args args; |
| 4598 | |
| 4599 | /* steal the filehandle */ |
| 4600 | rename_data->newdir = data->fh; |
| 4601 | data->fh.data.data_val = NULL; |
| 4602 | |
| 4603 | args.from.dir = rename_data->olddir; |
| 4604 | args.from.name = rename_data->oldobject; |
| 4605 | args.to.dir = rename_data->newdir; |
| 4606 | args.to.name = rename_data->newobject; |
| 4607 | if (rpc_nfs3_rename_async(nfs->rpc, nfs_rename_cb, &args, data) != 0) { |
| 4608 | rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path); |
| 4609 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4610 | free_nfs_cb_data(data); |
| 4611 | return -1; |
| 4612 | } |
| 4613 | return 0; |
| 4614 | } |
| 4615 | |
| 4616 | |
| 4617 | static int nfs_rename_continue_1_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 4618 | { |
| 4619 | struct nfs_rename_data *rename_data = data->continue_data; |
| 4620 | char* newpath = strdup(rename_data->newpath); |
| 4621 | if (!newpath) { |
| 4622 | rpc_set_error(nfs->rpc, "Out of memory. Could not allocate memory to store target path for rename"); |
| 4623 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4624 | free_nfs_cb_data(data); |
| 4625 | return -1; |
| 4626 | } |
| 4627 | |
| 4628 | /* steal the filehandle */ |
| 4629 | rename_data->olddir = data->fh; |
| 4630 | data->fh.data.data_val = NULL; |
| 4631 | |
| 4632 | if (nfs_lookuppath_async(nfs, rename_data->newpath, 0, data->cb, data->private_data, nfs_rename_continue_2_internal, rename_data, free_nfs_rename_data, 0) != 0) { |
| 4633 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", newpath); |
| 4634 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4635 | free_nfs_cb_data(data); |
| 4636 | free(newpath); |
| 4637 | return -1; |
| 4638 | } |
| 4639 | data->continue_data = NULL; |
| 4640 | free_nfs_cb_data(data); |
| 4641 | free(newpath); |
| 4642 | |
| 4643 | return 0; |
| 4644 | } |
| 4645 | |
| 4646 | |
| 4647 | int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) |
| 4648 | { |
| 4649 | char *ptr; |
| 4650 | struct nfs_rename_data *rename_data; |
| 4651 | |
| 4652 | rename_data = malloc(sizeof(struct nfs_rename_data)); |
| 4653 | if (rename_data == NULL) { |
| 4654 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data"); |
| 4655 | return -1; |
| 4656 | } |
| 4657 | memset(rename_data, 0, sizeof(struct nfs_rename_data)); |
| 4658 | |
| 4659 | rename_data->oldpath = strdup(oldpath); |
| 4660 | if (rename_data->oldpath == NULL) { |
| 4661 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
| 4662 | free_nfs_rename_data(rename_data); |
| 4663 | return -1; |
| 4664 | } |
| 4665 | ptr = strrchr(rename_data->oldpath, '/'); |
| 4666 | if (ptr == NULL) { |
| 4667 | rpc_set_error(nfs->rpc, "Invalid path %s", oldpath); |
| 4668 | free_nfs_rename_data(rename_data); |
| 4669 | return -1; |
| 4670 | } |
| 4671 | *ptr = 0; |
| 4672 | ptr++; |
| 4673 | rename_data->oldobject = ptr; |
| 4674 | |
| 4675 | |
| 4676 | rename_data->newpath = strdup(newpath); |
| 4677 | if (rename_data->newpath == NULL) { |
| 4678 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath"); |
| 4679 | free_nfs_rename_data(rename_data); |
| 4680 | return -1; |
| 4681 | } |
| 4682 | ptr = strrchr(rename_data->newpath, '/'); |
| 4683 | if (ptr == NULL) { |
| 4684 | rpc_set_error(nfs->rpc, "Invalid path %s", newpath); |
| 4685 | free_nfs_rename_data(rename_data); |
| 4686 | return -1; |
| 4687 | } |
| 4688 | *ptr = 0; |
| 4689 | ptr++; |
| 4690 | rename_data->newobject = ptr; |
| 4691 | |
| 4692 | |
| 4693 | if (nfs_lookuppath_async(nfs, rename_data->oldpath, 0, cb, private_data, nfs_rename_continue_1_internal, rename_data, free_nfs_rename_data, 0) != 0) { |
| 4694 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 4695 | return -1; |
| 4696 | } |
| 4697 | |
| 4698 | return 0; |
| 4699 | } |
| 4700 | |
| 4701 | |
| 4702 | /* |
| 4703 | * Async link() |
| 4704 | */ |
| 4705 | struct nfs_link_data { |
| 4706 | char *oldpath; |
| 4707 | struct nfs_fh3 oldfh; |
| 4708 | char *newpath; |
| 4709 | char *newobject; |
| 4710 | struct nfs_fh3 newdir; |
| 4711 | }; |
| 4712 | |
| 4713 | static void free_nfs_link_data(void *mem) |
| 4714 | { |
| 4715 | struct nfs_link_data *data = mem; |
| 4716 | |
| 4717 | if (data->oldpath != NULL) { |
| 4718 | free(data->oldpath); |
| 4719 | } |
| 4720 | if (data->oldfh.data.data_val != NULL) { |
| 4721 | free(data->oldfh.data.data_val); |
| 4722 | } |
| 4723 | if (data->newpath != NULL) { |
| 4724 | free(data->newpath); |
| 4725 | } |
| 4726 | if (data->newdir.data.data_val != NULL) { |
| 4727 | free(data->newdir.data.data_val); |
| 4728 | } |
| 4729 | free(data); |
| 4730 | } |
| 4731 | |
| 4732 | static void nfs_link_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 4733 | { |
| 4734 | LINK3res *res; |
| 4735 | struct nfs_cb_data *data = private_data; |
| 4736 | struct nfs_context *nfs = data->nfs; |
| 4737 | struct nfs_link_data *link_data = data->continue_data; |
| 4738 | |
| 4739 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4740 | |
| 4741 | if (status == RPC_STATUS_ERROR) { |
| 4742 | data->cb(-EFAULT, nfs, command_data, data->private_data); |
| 4743 | free_nfs_cb_data(data); |
| 4744 | return; |
| 4745 | } |
| 4746 | if (status == RPC_STATUS_CANCEL) { |
| 4747 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); |
| 4748 | free_nfs_cb_data(data); |
| 4749 | return; |
| 4750 | } |
| 4751 | |
| 4752 | res = command_data; |
| 4753 | if (res->status != NFS3_OK) { |
| 4754 | 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)); |
| 4755 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4756 | free_nfs_cb_data(data); |
| 4757 | return; |
| 4758 | } |
| 4759 | |
| 4760 | data->cb(0, nfs, NULL, data->private_data); |
| 4761 | free_nfs_cb_data(data); |
| 4762 | } |
| 4763 | |
| 4764 | static int nfs_link_continue_2_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 4765 | { |
| 4766 | struct nfs_link_data *link_data = data->continue_data; |
| 4767 | LINK3args args; |
| 4768 | |
| 4769 | /* steal the filehandle */ |
| 4770 | link_data->newdir = data->fh; |
| 4771 | data->fh.data.data_val = NULL; |
| 4772 | |
| 4773 | memset(&args, 0, sizeof(LINK3args)); |
| 4774 | args.file = link_data->oldfh; |
| 4775 | args.link.dir = link_data->newdir; |
| 4776 | args.link.name = link_data->newobject; |
| 4777 | if (rpc_nfs3_link_async(nfs->rpc, nfs_link_cb, &args, data) != 0) { |
| 4778 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path); |
| 4779 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4780 | free_nfs_cb_data(data); |
| 4781 | return -1; |
| 4782 | } |
| 4783 | return 0; |
| 4784 | } |
| 4785 | |
| 4786 | |
| 4787 | static int nfs_link_continue_1_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) |
| 4788 | { |
| 4789 | struct nfs_link_data *link_data = data->continue_data; |
| 4790 | |
| 4791 | /* steal the filehandle */ |
| 4792 | link_data->oldfh = data->fh; |
| 4793 | data->fh.data.data_val = NULL; |
| 4794 | |
| 4795 | if (nfs_lookuppath_async(nfs, link_data->newpath, 0, data->cb, data->private_data, nfs_link_continue_2_internal, link_data, free_nfs_link_data, 0) != 0) { |
| 4796 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath); |
| 4797 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
| 4798 | free_nfs_cb_data(data); |
| 4799 | return -1; |
| 4800 | } |
| 4801 | data->continue_data = NULL; |
| 4802 | free_nfs_cb_data(data); |
| 4803 | |
| 4804 | return 0; |
| 4805 | } |
| 4806 | |
| 4807 | |
| 4808 | int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) |
| 4809 | { |
| 4810 | char *ptr; |
| 4811 | struct nfs_link_data *link_data; |
| 4812 | |
| 4813 | link_data = malloc(sizeof(struct nfs_link_data)); |
| 4814 | if (link_data == NULL) { |
| 4815 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data"); |
| 4816 | return -1; |
| 4817 | } |
| 4818 | memset(link_data, 0, sizeof(struct nfs_link_data)); |
| 4819 | |
| 4820 | link_data->oldpath = strdup(oldpath); |
| 4821 | if (link_data->oldpath == NULL) { |
| 4822 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
| 4823 | free_nfs_link_data(link_data); |
| 4824 | return -1; |
| 4825 | } |
| 4826 | |
| 4827 | link_data->newpath = strdup(newpath); |
| 4828 | if (link_data->newpath == NULL) { |
| 4829 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath"); |
| 4830 | free_nfs_link_data(link_data); |
| 4831 | return -1; |
| 4832 | } |
| 4833 | ptr = strrchr(link_data->newpath, '/'); |
| 4834 | if (ptr == NULL) { |
| 4835 | rpc_set_error(nfs->rpc, "Invalid path %s", newpath); |
| 4836 | free_nfs_link_data(link_data); |
| 4837 | return -1; |
| 4838 | } |
| 4839 | *ptr = 0; |
| 4840 | ptr++; |
| 4841 | link_data->newobject = ptr; |
| 4842 | |
| 4843 | |
| 4844 | if (nfs_lookuppath_async(nfs, link_data->oldpath, 0, cb, private_data, nfs_link_continue_1_internal, link_data, free_nfs_link_data, 0) != 0) { |
| 4845 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
| 4846 | return -1; |
| 4847 | } |
| 4848 | |
| 4849 | return 0; |
| 4850 | } |
| 4851 | |
| 4852 | |
| 4853 | //qqq replace later with lseek() |
| 4854 | uint64_t nfs_get_current_offset(struct nfsfh *nfsfh) |
| 4855 | { |
| 4856 | return nfsfh->offset; |
| 4857 | } |
| 4858 | |
| 4859 | |
| 4860 | |
| 4861 | /* |
| 4862 | * Get the maximum supported READ3 size by the server |
| 4863 | */ |
| 4864 | uint64_t nfs_get_readmax(struct nfs_context *nfs) |
| 4865 | { |
| 4866 | return nfs->readmax; |
| 4867 | } |
| 4868 | |
| 4869 | /* |
| 4870 | * Get the maximum supported WRITE3 size by the server |
| 4871 | */ |
| 4872 | uint64_t nfs_get_writemax(struct nfs_context *nfs) |
| 4873 | { |
| 4874 | return nfs->writemax; |
| 4875 | } |
| 4876 | |
| 4877 | void nfs_set_tcp_syncnt(struct nfs_context *nfs, int v) { |
| 4878 | rpc_set_tcp_syncnt(nfs->rpc, v); |
| 4879 | } |
| 4880 | |
| 4881 | void nfs_set_uid(struct nfs_context *nfs, int uid) { |
| 4882 | rpc_set_uid(nfs->rpc, uid); |
| 4883 | } |
| 4884 | |
| 4885 | void nfs_set_gid(struct nfs_context *nfs, int gid) { |
| 4886 | rpc_set_gid(nfs->rpc, gid); |
| 4887 | } |
| 4888 | |
| 4889 | void nfs_set_readahead(struct nfs_context *nfs, uint32_t v) { |
| 4890 | rpc_set_readahead(nfs->rpc, v); |
| 4891 | } |
| 4892 | |
| 4893 | void nfs_set_error(struct nfs_context *nfs, char *error_string, ...) |
| 4894 | { |
| 4895 | va_list ap; |
| 4896 | char *str = NULL; |
| 4897 | |
| 4898 | va_start(ap, error_string); |
| 4899 | str = malloc(1024); |
| 4900 | vsnprintf(str, 1024, error_string, ap); |
| 4901 | if (nfs->rpc->error_string != NULL) { |
| 4902 | free(nfs->rpc->error_string); |
| 4903 | } |
| 4904 | nfs->rpc->error_string = str; |
| 4905 | va_end(ap); |
| 4906 | } |
| 4907 | |
| 4908 | |
| 4909 | |
| 4910 | struct mount_cb_data { |
| 4911 | rpc_cb cb; |
| 4912 | void *private_data; |
| 4913 | char *server; |
| 4914 | }; |
| 4915 | |
| 4916 | static void free_mount_cb_data(struct mount_cb_data *data) |
| 4917 | { |
| 4918 | if (data->server != NULL) { |
| 4919 | free(data->server); |
| 4920 | data->server = NULL; |
| 4921 | } |
| 4922 | |
| 4923 | free(data); |
| 4924 | } |
| 4925 | |
| 4926 | static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 4927 | { |
| 4928 | struct mount_cb_data *data = private_data; |
| 4929 | |
| 4930 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4931 | |
| 4932 | if (status == RPC_STATUS_ERROR) { |
| 4933 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
| 4934 | free_mount_cb_data(data); |
| 4935 | return; |
| 4936 | } |
| 4937 | if (status == RPC_STATUS_CANCEL) { |
| 4938 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); |
| 4939 | free_mount_cb_data(data); |
| 4940 | return; |
| 4941 | } |
| 4942 | |
| 4943 | data->cb(rpc, 0, command_data, data->private_data); |
| 4944 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { |
| 4945 | rpc_set_error(rpc, "Failed to disconnect\n"); |
| 4946 | } |
| 4947 | free_mount_cb_data(data); |
| 4948 | } |
| 4949 | |
| 4950 | static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
| 4951 | { |
| 4952 | struct mount_cb_data *data = private_data; |
| 4953 | |
| 4954 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4955 | |
| 4956 | /* Dont want any more callbacks even if the socket is closed */ |
| 4957 | rpc->connect_cb = NULL; |
| 4958 | |
| 4959 | if (status == RPC_STATUS_ERROR) { |
| 4960 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
| 4961 | free_mount_cb_data(data); |
| 4962 | return; |
| 4963 | } |
| 4964 | if (status == RPC_STATUS_CANCEL) { |
| 4965 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); |
| 4966 | free_mount_cb_data(data); |
| 4967 | return; |
| 4968 | } |
| 4969 | |
| 4970 | if (rpc_mount3_export_async(rpc, mount_export_5_cb, data) != 0) { |
| 4971 | data->cb(rpc, -ENOMEM, command_data, data->private_data); |
| 4972 | free_mount_cb_data(data); |
| 4973 | return; |
| 4974 | } |
| 4975 | } |
| 4976 | |
| 4977 | int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data) |
| 4978 | { |
| 4979 | struct mount_cb_data *data; |
| 4980 | |
| 4981 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 4982 | |
| 4983 | data = malloc(sizeof(struct mount_cb_data)); |
| 4984 | if (data == NULL) { |
| 4985 | return -1; |
| 4986 | } |
| 4987 | memset(data, 0, sizeof(struct mount_cb_data)); |
| 4988 | data->cb = cb; |
| 4989 | data->private_data = private_data; |
| 4990 | data->server = strdup(server); |
| 4991 | if (data->server == NULL) { |
| 4992 | free_mount_cb_data(data); |
| 4993 | return -1; |
| 4994 | } |
| 4995 | if (rpc_connect_program_async(rpc, data->server, MOUNT_PROGRAM, MOUNT_V3, mount_export_4_cb, data) != 0) { |
| 4996 | rpc_set_error(rpc, "Failed to start connection"); |
| 4997 | free_mount_cb_data(data); |
| 4998 | return -1; |
| 4999 | } |
| 5000 | |
| 5001 | return 0; |
| 5002 | } |
| 5003 | |
| 5004 | struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs) |
| 5005 | { |
| 5006 | assert(nfs->rpc->magic == RPC_CONTEXT_MAGIC); |
| 5007 | return nfs->rpc; |
| 5008 | } |
| 5009 | |
| 5010 | const char *nfs_get_server(struct nfs_context *nfs) { |
| 5011 | return nfs->server; |
| 5012 | } |
| 5013 | |
| 5014 | const char *nfs_get_export(struct nfs_context *nfs) { |
| 5015 | return nfs->export; |
| 5016 | } |
| 5017 | |
| 5018 | const struct nfs_fh3 *nfs_get_rootfh(struct nfs_context *nfs) { |
| 5019 | return &nfs->rootfh; |
| 5020 | } |
| 5021 | |
| 5022 | struct nfs_fh3 *nfs_get_fh(struct nfsfh *nfsfh) { |
| 5023 | return &nfsfh->fh; |
| 5024 | } |