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