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