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