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