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