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