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