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