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