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