Add uid/gid to the stat data returned in the readdir directoryentry
[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;
7bda8bad
RS
2437 nfsdirent->uid = attributes->uid;
2438 nfsdirent->gid = attributes->gid;
fb69c64c
RS
2439 }
2440 }
2441
2442 if (rdpe_cb_data->getattrcount == 0) {
2443 if (rdpe_cb_data->status != RPC_STATUS_SUCCESS) {
2444 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2445 nfs_free_nfsdir(nfsdir);
2446 } else {
2447 data->cb(0, nfs, nfsdir, data->private_data);
2448 }
2449 free(rdpe_cb_data);
2450
2451 data->continue_data = NULL;
2452 free_nfs_cb_data(data);
2453 }
2454}
2455
f3a75078 2456static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
fb69c64c
RS
2457{
2458 READDIR3res *res = command_data;
84004dbf
RS
2459 struct nfs_cb_data *data = private_data;
2460 struct nfs_context *nfs = data->nfs;
fb6510bb 2461 struct nfsdir *nfsdir = data->continue_data;
fb69c64c
RS
2462 struct nfsdirent *nfsdirent;
2463 struct entry3 *entry;
84004dbf 2464 uint64_t cookie;
fb69c64c
RS
2465 struct rdpe_cb_data *rdpe_cb_data;
2466
f3a75078
RS
2467 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2468
84004dbf
RS
2469 if (status == RPC_STATUS_ERROR) {
2470 data->cb(-EFAULT, nfs, command_data, data->private_data);
2471 nfs_free_nfsdir(nfsdir);
2472 data->continue_data = NULL;
2473 free_nfs_cb_data(data);
2474 return;
2475 }
fb69c64c 2476
84004dbf
RS
2477 if (status == RPC_STATUS_CANCEL) {
2478 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2479 nfs_free_nfsdir(nfsdir);
2480 data->continue_data = NULL;
2481 free_nfs_cb_data(data);
2482 return;
2483 }
2484
84004dbf
RS
2485 if (res->status != NFS3_OK) {
2486 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));
2487 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2488 nfs_free_nfsdir(nfsdir);
2489 data->continue_data = NULL;
2490 free_nfs_cb_data(data);
2491 return;
fb69c64c
RS
2492 }
2493
2494 entry =res->READDIR3res_u.resok.reply.entries;
2495 while (entry != NULL) {
2496 nfsdirent = malloc(sizeof(struct nfsdirent));
2497 if (nfsdirent == NULL) {
2498 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
2499 nfs_free_nfsdir(nfsdir);
2500 data->continue_data = NULL;
2501 free_nfs_cb_data(data);
2502 return;
2503 }
2504 memset(nfsdirent, 0, sizeof(struct nfsdirent));
2505 nfsdirent->name = strdup(entry->name);
2506 if (nfsdirent->name == NULL) {
2507 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
2508 nfs_free_nfsdir(nfsdir);
2509 data->continue_data = NULL;
2510 free_nfs_cb_data(data);
2511 return;
2512 }
2513 nfsdirent->inode = entry->fileid;
2514
2515 nfsdirent->next = nfsdir->entries;
2516 nfsdir->entries = nfsdirent;
2517
2518 cookie = entry->cookie;
2519 entry = entry->nextentry;
2520 }
2521
2522 if (res->READDIR3res_u.resok.reply.eof == 0) {
a64a16a2
RS
2523 READDIR3args args;
2524
2525 args.dir = data->fh;
2526 args.cookie = cookie;
2452c57f 2527 memcpy(&args.cookieverf, res->READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
a64a16a2
RS
2528 args.count = 8192;
2529
2530 if (rpc_nfs3_readdir_async(nfs->rpc, nfs_opendir2_cb, &args, data) != 0) {
fb69c64c
RS
2531 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
2532 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2533 nfs_free_nfsdir(nfsdir);
2534 data->continue_data = NULL;
2535 free_nfs_cb_data(data);
2536 return;
2537 }
2538 return;
2539 }
2540
2541 /* steal the dirhandle */
2542 nfsdir->current = nfsdir->entries;
2543
2544 rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data));
2545 rdpe_cb_data->getattrcount = 0;
2546 rdpe_cb_data->status = RPC_STATUS_SUCCESS;
2547 rdpe_cb_data->data = data;
2548 for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) {
2549 struct rdpe_lookup_cb_data *rdpe_lookup_cb_data;
463d59bf 2550 LOOKUP3args args;
fb69c64c
RS
2551
2552 rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data));
2553 rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data;
2554 rdpe_lookup_cb_data->nfsdirent = nfsdirent;
2555
463d59bf 2556 memset(&args, 0, sizeof(LOOKUP3args));
2452c57f
RS
2557 args.what.dir = data->fh;
2558 args.what.name = nfsdirent->name;
463d59bf
RS
2559
2560 if (rpc_nfs3_lookup_async(nfs->rpc, nfs_opendir3_cb, &args, rdpe_lookup_cb_data) != 0) {
fb69c64c
RS
2561 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call");
2562
2563 /* if we have already commands in flight, we cant just stop, we have to wait for the
2564 * commands in flight to complete
2565 */
2566 if (rdpe_cb_data->getattrcount > 0) {
2567 rdpe_cb_data->status = RPC_STATUS_ERROR;
2568 free(rdpe_lookup_cb_data);
2569 return;
2570 }
2571
2572 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2573 nfs_free_nfsdir(nfsdir);
2574 data->continue_data = NULL;
2575 free_nfs_cb_data(data);
2576 free(rdpe_lookup_cb_data);
2577 free(rdpe_cb_data);
2578 return;
2579 }
2580 rdpe_cb_data->getattrcount++;
2581 }
2582}
2583
2584
f3a75078 2585static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
fb69c64c
RS
2586{
2587 READDIRPLUS3res *res = command_data;
2588 struct nfs_cb_data *data = private_data;
2589 struct nfs_context *nfs = data->nfs;
2590 struct nfsdir *nfsdir = data->continue_data;
2591 struct entryplus3 *entry;
2592 uint64_t cookie;
2593
f3a75078 2594 assert(rpc->magic == RPC_CONTEXT_MAGIC);
fb69c64c
RS
2595
2596 if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){
a64a16a2
RS
2597 READDIR3args args;
2598
2599 args.dir = data->fh;
2600 args.cookie = cookie;
2452c57f 2601 memset(&args.cookieverf, 0, sizeof(cookieverf3));
a64a16a2 2602 args.count = 8192;
fb69c64c 2603
a64a16a2 2604 if (rpc_nfs3_readdir_async(nfs->rpc, nfs_opendir2_cb, &args, data) != 0) {
fb69c64c
RS
2605 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
2606 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2607 nfs_free_nfsdir(nfsdir);
2608 data->continue_data = NULL;
2609 free_nfs_cb_data(data);
2610 return;
2611 }
2612 return;
2613 }
2614
2615 if (status == RPC_STATUS_CANCEL) {
2616 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2617 nfs_free_nfsdir(nfsdir);
2618 data->continue_data = NULL;
2619 free_nfs_cb_data(data);
2620 return;
2621 }
2622
2623 if (res->status != NFS3_OK) {
2624 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));
2625 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2626 nfs_free_nfsdir(nfsdir);
2627 data->continue_data = NULL;
2628 free_nfs_cb_data(data);
2629 return;
84004dbf
RS
2630 }
2631
f390f181 2632 entry =res->READDIRPLUS3res_u.resok.reply.entries;
84004dbf
RS
2633 while (entry != NULL) {
2634 struct nfsdirent *nfsdirent;
2635
2636 nfsdirent = malloc(sizeof(struct nfsdirent));
2637 if (nfsdirent == NULL) {
2638 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
2639 nfs_free_nfsdir(nfsdir);
2640 data->continue_data = NULL;
2641 free_nfs_cb_data(data);
2642 return;
2643 }
ea98629a 2644 memset(nfsdirent, 0, sizeof(struct nfsdirent));
84004dbf
RS
2645 nfsdirent->name = strdup(entry->name);
2646 if (nfsdirent->name == NULL) {
2647 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
2648 nfs_free_nfsdir(nfsdir);
2649 data->continue_data = NULL;
2650 free_nfs_cb_data(data);
2651 return;
2652 }
2653 nfsdirent->inode = entry->fileid;
0804e67d
RS
2654 if (entry->name_attributes.attributes_follow) {
2655 nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type;
2656 nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode;
2657 nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size;
2658
2659 nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds;
2660 nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000;
2661 nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds;
2662 nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000;
2663 nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds;
2664 nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000;
7bda8bad
RS
2665 nfsdirent->uid = entry->name_attributes.post_op_attr_u.attributes.uid;
2666 nfsdirent->gid = entry->name_attributes.post_op_attr_u.attributes.gid;
0804e67d
RS
2667 }
2668
84004dbf
RS
2669 nfsdirent->next = nfsdir->entries;
2670 nfsdir->entries = nfsdirent;
2671
2672 cookie = entry->cookie;
2673 entry = entry->nextentry;
2674 }
2675
f390f181 2676 if (res->READDIRPLUS3res_u.resok.reply.eof == 0) {
1a5b83c9
RS
2677 READDIRPLUS3args args;
2678
2679 args.dir = data->fh;
2680 args.cookie = cookie;
2681 memcpy(&args.cookieverf, res->READDIRPLUS3res_u.resok.cookieverf, sizeof(cookieverf3));
2682 args.dircount = 8192;
2683 args.maxcount = 8192;
2684
2685 if (rpc_nfs3_readdirplus_async(nfs->rpc, nfs_opendir_cb, &args, data) != 0) {
f390f181 2686 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
2687 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2688 nfs_free_nfsdir(nfsdir);
2689 data->continue_data = NULL;
2690 free_nfs_cb_data(data);
2691 return;
2692 }
2693 return;
2694 }
2695
2696 /* steal the dirhandle */
2697 data->continue_data = NULL;
2698 nfsdir->current = nfsdir->entries;
2699
2700 data->cb(0, nfs, nfsdir, data->private_data);
2701 free_nfs_cb_data(data);
2702}
2703
2704static int nfs_opendir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2705{
1a5b83c9 2706 READDIRPLUS3args args;
84004dbf 2707
1a5b83c9
RS
2708 args.dir = data->fh;
2709 args.cookie = 0;
2710 memset(&args.cookieverf, 0, sizeof(cookieverf3));
2711 args.dircount = 8192;
2712 args.maxcount = 8192;
2713 if (rpc_nfs3_readdirplus_async(nfs->rpc, nfs_opendir_cb, &args, data) != 0) {
f390f181 2714 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
2715 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2716 free_nfs_cb_data(data);
2717 return -1;
2718 }
2719 return 0;
2720}
2721
2722int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2723{
2724 struct nfsdir *nfsdir;
2725
2726 nfsdir = malloc(sizeof(struct nfsdir));
2727 if (nfsdir == NULL) {
cbbf9d3e 2728 rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir");
84004dbf
RS
2729 return -1;
2730 }
ea98629a 2731 memset(nfsdir, 0, sizeof(struct nfsdir));
84004dbf
RS
2732
2733 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) {
cbbf9d3e
RS
2734 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2735 return -1;
84004dbf
RS
2736 }
2737
2738 return 0;
2739}
2740
2741
2742struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
2743{
2744 struct nfsdirent *nfsdirent = nfsdir->current;
2745
2746 if (nfsdir->current != NULL) {
2747 nfsdir->current = nfsdir->current->next;
2748 }
2749 return nfsdirent;
2750}
2751
2752
2753void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
2754{
2755 nfs_free_nfsdir(nfsdir);
2756}
2757
2758
2759
2760
2761
2762
2763
2764/*
2765 * Async lseek()
2766 */
2767struct lseek_cb_data {
2768 struct nfs_context *nfs;
2769 struct nfsfh *nfsfh;
183451cf 2770 uint64_t offset;
84004dbf
RS
2771 nfs_cb cb;
2772 void *private_data;
2773};
2774
f3a75078 2775static void nfs_lseek_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2776{
2777 GETATTR3res *res;
2778 struct lseek_cb_data *data = private_data;
2779 struct nfs_context *nfs = data->nfs;
2780
f3a75078
RS
2781 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2782
84004dbf
RS
2783 if (status == RPC_STATUS_ERROR) {
2784 data->cb(-EFAULT, nfs, command_data, data->private_data);
2785 free(data);
2786 return;
2787 }
2788 if (status == RPC_STATUS_CANCEL) {
2789 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2790 free(data);
2791 return;
2792 }
2793
2794 res = command_data;
2795 if (res->status != NFS3_OK) {
2796 rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2797 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2798 free(data);
2799 return;
2800 }
2801
2802 data->nfsfh->offset = data->offset + res->GETATTR3res_u.resok.obj_attributes.size;
2803 data->cb(0, nfs, &data->nfsfh->offset, data->private_data);
2804 free(data);
2805}
2806
183451cf 2807int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, int whence, nfs_cb cb, void *private_data)
84004dbf
RS
2808{
2809 struct lseek_cb_data *data;
463d59bf 2810 struct GETATTR3args args;
84004dbf
RS
2811
2812 if (whence == SEEK_SET) {
2813 nfsfh->offset = offset;
2814 cb(0, nfs, &nfsfh->offset, private_data);
2815 return 0;
2816 }
2817 if (whence == SEEK_CUR) {
2818 nfsfh->offset += offset;
2819 cb(0, nfs, &nfsfh->offset, private_data);
2820 return 0;
2821 }
2822
2823 data = malloc(sizeof(struct lseek_cb_data));
2824 if (data == NULL) {
2825 rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data");
2826 return -1;
2827 }
2828
2829 data->nfs = nfs;
2830 data->nfsfh = nfsfh;
2831 data->offset = offset;
2832 data->cb = cb;
2833 data->private_data = private_data;
2834
463d59bf 2835 memset(&args, 0, sizeof(GETATTR3args));
2452c57f 2836 args.object = nfsfh->fh;
463d59bf
RS
2837
2838 if (rpc_nfs3_getattr_async(nfs->rpc, nfs_lseek_1_cb, &args, data) != 0) {
84004dbf
RS
2839 rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call");
2840 free(data);
cbbf9d3e 2841 return -1;
84004dbf
RS
2842 }
2843 return 0;
2844}
2845
2846
2847
2848
2849/*
2850 * Async statvfs()
2851 */
f3a75078 2852static void nfs_statvfs_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2853{
2854 FSSTAT3res *res;
2855 struct nfs_cb_data *data = private_data;
2856 struct nfs_context *nfs = data->nfs;
2857 struct statvfs svfs;
2858
f3a75078
RS
2859 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2860
84004dbf
RS
2861 if (status == RPC_STATUS_ERROR) {
2862 data->cb(-EFAULT, nfs, command_data, data->private_data);
2863 free_nfs_cb_data(data);
2864 return;
2865 }
2866 if (status == RPC_STATUS_CANCEL) {
2867 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2868 free_nfs_cb_data(data);
2869 return;
2870 }
2871
2872 res = command_data;
2873 if (res->status != NFS3_OK) {
2874 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));
2875 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2876 free_nfs_cb_data(data);
2877 return;
2878 }
2879
2880 svfs.f_bsize = 4096;
2881 svfs.f_frsize = 4096;
2882 svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/4096;
2883 svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/4096;
2884 svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/4096;
2885 svfs.f_files = res->FSSTAT3res_u.resok.tfiles;
2886 svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles;
252aa90d 2887#if !defined(ANDROID)
84004dbf
RS
2888 svfs.f_favail = res->FSSTAT3res_u.resok.afiles;
2889 svfs.f_fsid = 0;
2890 svfs.f_flag = 0;
2891 svfs.f_namemax = 256;
252aa90d 2892#endif
84004dbf
RS
2893
2894 data->cb(0, nfs, &svfs, data->private_data);
2895 free_nfs_cb_data(data);
2896}
2897
2898static int nfs_statvfs_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2899{
0249c17b
RS
2900 FSSTAT3args args;
2901
2902 args.fsroot = data->fh;
2903 if (rpc_nfs3_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &args, data) != 0) {
84004dbf
RS
2904 rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT call for %s", data->path);
2905 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2906 free_nfs_cb_data(data);
2907 return -1;
2908 }
2909 return 0;
2910}
2911
2912int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2913{
2914 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2915 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2916 return -1;
2917 }
2918
2919 return 0;
2920}
2921
2922
2923
2924
2925/*
2926 * Async readlink()
2927 */
f3a75078 2928static void nfs_readlink_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2929{
2930 READLINK3res *res;
2931 struct nfs_cb_data *data = private_data;
2932 struct nfs_context *nfs = data->nfs;
2933
f3a75078
RS
2934 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2935
84004dbf
RS
2936 if (status == RPC_STATUS_ERROR) {
2937 data->cb(-EFAULT, nfs, command_data, data->private_data);
2938 free_nfs_cb_data(data);
2939 return;
2940 }
2941 if (status == RPC_STATUS_CANCEL) {
2942 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2943 free_nfs_cb_data(data);
2944 return;
2945 }
2946
2947 res = command_data;
2948 if (res->status != NFS3_OK) {
2949 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));
2950 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2951 free_nfs_cb_data(data);
2952 return;
2953 }
2954
2955
2956 data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data);
2957 free_nfs_cb_data(data);
2958}
2959
2960static int nfs_readlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2961{
16104b27
RS
2962 READLINK3args args;
2963
2452c57f 2964 args.symlink = data->fh;
16104b27 2965
7cd23ccc 2966 if (rpc_nfs3_readlink_async(nfs->rpc, nfs_readlink_1_cb, &args, data) != 0) {
84004dbf
RS
2967 rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path);
2968 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2969 free_nfs_cb_data(data);
2970 return -1;
2971 }
2972 return 0;
2973}
2974
2975int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2976{
2977 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2978 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2979 return -1;
2980 }
2981
2982 return 0;
2983}
2984
2985
2986
2987
2988/*
2989 * Async chmod()
2990 */
f3a75078 2991static void nfs_chmod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2992{
2993 struct nfs_cb_data *data = private_data;
2994 struct nfs_context *nfs = data->nfs;
2995 SETATTR3res *res;
2996
f3a75078
RS
2997 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2998
84004dbf
RS
2999 if (status == RPC_STATUS_ERROR) {
3000 data->cb(-EFAULT, nfs, command_data, data->private_data);
3001 free_nfs_cb_data(data);
3002 return;
3003 }
3004 if (status == RPC_STATUS_CANCEL) {
3005 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3006 free_nfs_cb_data(data);
3007 return;
3008 }
3009
3010 res = command_data;
3011 if (res->status != NFS3_OK) {
3012 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
3013 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3014 free_nfs_cb_data(data);
3015 return;
3016 }
3017
3018 data->cb(0, nfs, NULL, data->private_data);
3019 free_nfs_cb_data(data);
3020}
3021
3022static int nfs_chmod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3023{
3024 SETATTR3args args;
3025
ea98629a 3026 memset(&args, 0, sizeof(SETATTR3args));
2452c57f 3027 args.object = data->fh;
84004dbf
RS
3028 args.new_attributes.mode.set_it = 1;
3029 args.new_attributes.mode.set_mode3_u.mode = data->continue_int;
3030
b701254d 3031 if (rpc_nfs3_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) {
84004dbf
RS
3032 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
3033 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3034 free_nfs_cb_data(data);
3035 return -1;
3036 }
3037 return 0;
3038}
3039
3040
3041int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
3042{
3043 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e 3044 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
3045 return -1;
3046 }
3047
3048 return 0;
3049}
3050
3051/*
3052 * Async fchmod()
3053 */
3054int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data)
3055{
3056 struct nfs_cb_data *data;
3057
3058 data = malloc(sizeof(struct nfs_cb_data));
3059 if (data == NULL) {
cbbf9d3e 3060 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data");
84004dbf
RS
3061 return -1;
3062 }
ea98629a 3063 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
3064 data->nfs = nfs;
3065 data->cb = cb;
3066 data->private_data = private_data;
3067 data->continue_int = mode;
3068 data->fh.data.data_len = nfsfh->fh.data.data_len;
3069 data->fh.data.data_val = malloc(data->fh.data.data_len);
3070 if (data->fh.data.data_val == NULL) {
3071 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
3072 free_nfs_cb_data(data);
3073 return -1;
3074 }
3075 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
3076
3077 if (nfs_chmod_continue_internal(nfs, data) != 0) {
3078 free_nfs_cb_data(data);
3079 return -1;
3080 }
3081
3082 return 0;
3083}
3084
3085
3086
3087/*
3088 * Async chown()
3089 */
f3a75078 3090static void nfs_chown_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3091{
3092 struct nfs_cb_data *data = private_data;
3093 struct nfs_context *nfs = data->nfs;
3094 SETATTR3res *res;
3095
f3a75078
RS
3096 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3097
84004dbf
RS
3098 if (status == RPC_STATUS_ERROR) {
3099 data->cb(-EFAULT, nfs, command_data, data->private_data);
3100 free_nfs_cb_data(data);
3101 return;
3102 }
3103 if (status == RPC_STATUS_CANCEL) {
3104 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3105 free_nfs_cb_data(data);
3106 return;
3107 }
3108
3109 res = command_data;
3110 if (res->status != NFS3_OK) {
3111 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
3112 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3113 free_nfs_cb_data(data);
3114 return;
3115 }
3116
3117 data->cb(0, nfs, NULL, data->private_data);
3118 free_nfs_cb_data(data);
3119}
3120
3121struct nfs_chown_data {
3122 uid_t uid;
3123 gid_t gid;
3124};
3125
3126static int nfs_chown_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3127{
3128 SETATTR3args args;
3129 struct nfs_chown_data *chown_data = data->continue_data;
3130
ea98629a 3131 memset(&args, 0, sizeof(SETATTR3args));
2452c57f 3132 args.object = data->fh;
84004dbf
RS
3133 if (chown_data->uid != (uid_t)-1) {
3134 args.new_attributes.uid.set_it = 1;
3135 args.new_attributes.uid.set_uid3_u.uid = chown_data->uid;
3136 }
3137 if (chown_data->gid != (gid_t)-1) {
3138 args.new_attributes.gid.set_it = 1;
3139 args.new_attributes.gid.set_gid3_u.gid = chown_data->gid;
3140 }
3141
b701254d 3142 if (rpc_nfs3_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) {
84004dbf
RS
3143 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
3144 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3145 free_nfs_cb_data(data);
3146 return -1;
3147 }
3148 return 0;
3149}
3150
3151
3152int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data)
3153{
3154 struct nfs_chown_data *chown_data;
3155
3156 chown_data = malloc(sizeof(struct nfs_chown_data));
3157 if (chown_data == NULL) {
cbbf9d3e 3158 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
3159 return -1;
3160 }
3161
3162 chown_data->uid = uid;
3163 chown_data->gid = gid;
3164
3165 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) {
cbbf9d3e 3166 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
3167 return -1;
3168 }
3169
3170 return 0;
3171}
3172
3173
3174/*
3175 * Async fchown()
3176 */
3177int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data)
3178{
3179 struct nfs_cb_data *data;
3180 struct nfs_chown_data *chown_data;
3181
3182 chown_data = malloc(sizeof(struct nfs_chown_data));
3183 if (chown_data == NULL) {
cbbf9d3e 3184 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
3185 return -1;
3186 }
3187
3188 chown_data->uid = uid;
3189 chown_data->gid = gid;
3190
3191
3192 data = malloc(sizeof(struct nfs_cb_data));
3193 if (data == NULL) {
cbbf9d3e 3194 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data");
84004dbf
RS
3195 return -1;
3196 }
ea98629a 3197 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
3198 data->nfs = nfs;
3199 data->cb = cb;
3200 data->private_data = private_data;
3201 data->continue_data = chown_data;
3202 data->fh.data.data_len = nfsfh->fh.data.data_len;
3203 data->fh.data.data_val = malloc(data->fh.data.data_len);
3204 if (data->fh.data.data_val == NULL) {
3205 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
3206 free_nfs_cb_data(data);
3207 return -1;
3208 }
3209 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
3210
3211
3212 if (nfs_chown_continue_internal(nfs, data) != 0) {
3213 free_nfs_cb_data(data);
3214 return -1;
3215 }
3216
3217 return 0;
3218}
3219
3220
3221
3222
3223
3224/*
3225 * Async utimes()
3226 */
f3a75078 3227static void nfs_utimes_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3228{
3229 struct nfs_cb_data *data = private_data;
3230 struct nfs_context *nfs = data->nfs;
3231 SETATTR3res *res;
3232
f3a75078
RS
3233 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3234
84004dbf
RS
3235 if (status == RPC_STATUS_ERROR) {
3236 data->cb(-EFAULT, nfs, command_data, data->private_data);
3237 free_nfs_cb_data(data);
3238 return;
3239 }
3240 if (status == RPC_STATUS_CANCEL) {
3241 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3242 free_nfs_cb_data(data);
3243 return;
3244 }
3245
3246 res = command_data;
3247 if (res->status != NFS3_OK) {
3248 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
3249 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3250 free_nfs_cb_data(data);
3251 return;
3252 }
3253
3254 data->cb(0, nfs, NULL, data->private_data);
3255 free_nfs_cb_data(data);
3256}
3257
3258static int nfs_utimes_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3259{
3260 SETATTR3args args;
3261 struct timeval *utimes_data = data->continue_data;
3262
ea98629a 3263 memset(&args, 0, sizeof(SETATTR3args));
2452c57f 3264 args.object = data->fh;
84004dbf
RS
3265 if (utimes_data != NULL) {
3266 args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME;
3267 args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec;
3268 args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000;
3269 args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME;
3270 args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec;
3271 args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000;
3272 } else {
3273 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
3274 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
3275 }
3276
b701254d 3277 if (rpc_nfs3_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) {
84004dbf
RS
3278 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
3279 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3280 free_nfs_cb_data(data);
3281 return -1;
3282 }
3283 return 0;
3284}
3285
3286
3287int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data)
3288{
3289 struct timeval *new_times = NULL;
3290
3291 if (times != NULL) {
3292 new_times = malloc(sizeof(struct timeval)*2);
3293 if (new_times == NULL) {
cbbf9d3e 3294 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
3295 return -1;
3296 }
3297
3298 memcpy(new_times, times, sizeof(struct timeval)*2);
3299 }
3300
3301 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 3302 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
3303 return -1;
3304 }
3305
3306 return 0;
3307}
3308
3309/*
3310 * Async utime()
3311 */
3312int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data)
3313{
3314 struct timeval *new_times = NULL;
3315
3316 if (times != NULL) {
3317 new_times = malloc(sizeof(struct timeval)*2);
3318 if (new_times == NULL) {
cbbf9d3e 3319 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
3320 return -1;
3321 }
3322
3323 new_times[0].tv_sec = times->actime;
3324 new_times[0].tv_usec = 0;
3325 new_times[1].tv_sec = times->modtime;
3326 new_times[1].tv_usec = 0;
3327 }
3328
3329 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 3330 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
3331 return -1;
3332 }
3333
3334 return 0;
3335}
84004dbf
RS
3336
3337
3338/*
3339 * Async access()
3340 */
f3a75078 3341static void nfs_access_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3342{
3343 ACCESS3res *res;
3344 struct nfs_cb_data *data = private_data;
3345 struct nfs_context *nfs = data->nfs;
3346 unsigned int nfsmode = 0;
3347
f3a75078
RS
3348 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3349
84004dbf
RS
3350 if (status == RPC_STATUS_ERROR) {
3351 data->cb(-EFAULT, nfs, command_data, data->private_data);
3352 free_nfs_cb_data(data);
3353 return;
3354 }
3355 if (status == RPC_STATUS_CANCEL) {
3356 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3357 free_nfs_cb_data(data);
3358 return;
3359 }
3360
3361 res = command_data;
3362 if (res->status != NFS3_OK) {
3363 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));
3364 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3365 free_nfs_cb_data(data);
3366 return;
3367 }
3368
3369 if (data->continue_int & R_OK) {
3370 nfsmode |= ACCESS3_READ;
3371 }
3372 if (data->continue_int & W_OK) {
3373 nfsmode |= ACCESS3_MODIFY;
3374 }
3375 if (data->continue_int & X_OK) {
3376 nfsmode |= ACCESS3_EXECUTE;
3377 }
3378
3379 if (res->ACCESS3res_u.resok.access != nfsmode) {
3380 rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3381 nfsmode&ACCESS3_READ?'r':'-',
3382 nfsmode&ACCESS3_MODIFY?'w':'-',
3383 nfsmode&ACCESS3_EXECUTE?'x':'-',
3384 res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-',
3385 res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-',
3386 res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-');
3387 data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data);
3388 free_nfs_cb_data(data);
3389 return;
3390 }
3391
3392 data->cb(0, nfs, NULL, data->private_data);
3393 free_nfs_cb_data(data);
3394}
3395
3396static int nfs_access_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3397{
3398 int nfsmode = 0;
463d59bf 3399 ACCESS3args args;
84004dbf
RS
3400
3401 if (data->continue_int & R_OK) {
3402 nfsmode |= ACCESS3_READ;
3403 }
3404 if (data->continue_int & W_OK) {
3405 nfsmode |= ACCESS3_MODIFY;
3406 }
3407 if (data->continue_int & X_OK) {
3408 nfsmode |= ACCESS3_EXECUTE;
3409 }
3410
463d59bf 3411 memset(&args, 0, sizeof(ACCESS3args));
2452c57f 3412 args.object = data->fh;
463d59bf
RS
3413 args.access = nfsmode;
3414
3415 if (rpc_nfs3_access_async(nfs->rpc, nfs_access_cb, &args, data) != 0) {
84004dbf
RS
3416 rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
3417 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3418 free_nfs_cb_data(data);
3419 return -1;
3420 }
3421 return 0;
3422}
3423
3424int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
3425{
3426 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e
RS
3427 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3428 return -1;
84004dbf
RS
3429 }
3430
3431 return 0;
3432}
3433
3434
3435
3436/*
3437 * Async symlink()
3438 */
3439struct nfs_symlink_data {
3440 char *oldpath;
3441 char *newpathparent;
3442 char *newpathobject;
3443};
3444
3445static void free_nfs_symlink_data(void *mem)
3446{
3447 struct nfs_symlink_data *data = mem;
3448
3449 if (data->oldpath != NULL) {
3450 free(data->oldpath);
3451 }
3452 if (data->newpathparent != NULL) {
3453 free(data->newpathparent);
3454 }
3455 if (data->newpathobject != NULL) {
3456 free(data->newpathobject);
3457 }
3458 free(data);
3459}
3460
f3a75078 3461static void nfs_symlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3462{
3463 SYMLINK3res *res;
3464 struct nfs_cb_data *data = private_data;
3465 struct nfs_context *nfs = data->nfs;
3466 struct nfs_symlink_data *symlink_data = data->continue_data;
3467
f3a75078
RS
3468 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3469
84004dbf
RS
3470 if (status == RPC_STATUS_ERROR) {
3471 data->cb(-EFAULT, nfs, command_data, data->private_data);
3472 free_nfs_cb_data(data);
3473 return;
3474 }
3475 if (status == RPC_STATUS_CANCEL) {
3476 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3477 free_nfs_cb_data(data);
3478 return;
3479 }
3480
3481 res = command_data;
3482 if (res->status != NFS3_OK) {
3483 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));
3484 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3485 free_nfs_cb_data(data);
3486 return;
3487 }
3488
3489 data->cb(0, nfs, NULL, data->private_data);
3490 free_nfs_cb_data(data);
3491}
3492
3493static int nfs_symlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3494{
3495 struct nfs_symlink_data *symlink_data = data->continue_data;
87f81c85 3496 SYMLINK3args args;
84004dbf 3497
87f81c85 3498 memset(&args, 0, sizeof(SYMLINK3args));
2452c57f 3499 args.where.dir = data->fh;
87f81c85
RS
3500 args.where.name = symlink_data->newpathobject;
3501 args.symlink.symlink_attributes.mode.set_it = 1;
3502 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;
3503 args.symlink.symlink_data = symlink_data->oldpath;
8e255816 3504
87f81c85 3505 if (rpc_nfs3_symlink_async(nfs->rpc, nfs_symlink_cb, &args, data) != 0) {
84004dbf
RS
3506 rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path);
3507 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3508 free_nfs_cb_data(data);
3509 return -1;
3510 }
3511 return 0;
3512}
3513
3514int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3515{
3516 char *ptr;
3517 struct nfs_symlink_data *symlink_data;
3518
3519 symlink_data = malloc(sizeof(struct nfs_symlink_data));
3520 if (symlink_data == NULL) {
cbbf9d3e 3521 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data");
84004dbf
RS
3522 return -1;
3523 }
ea98629a 3524 memset(symlink_data, 0, sizeof(struct nfs_symlink_data));
84004dbf
RS
3525
3526 symlink_data->oldpath = strdup(oldpath);
3527 if (symlink_data->oldpath == NULL) {
cbbf9d3e 3528 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3529 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3530 return -1;
84004dbf
RS
3531 }
3532
3533 symlink_data->newpathparent = strdup(newpath);
3534 if (symlink_data->newpathparent == NULL) {
cbbf9d3e 3535 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 3536 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3537 return -1;
84004dbf
RS
3538 }
3539
93e9306f 3540 ptr = strrchr(symlink_data->newpathparent, '/');
84004dbf 3541 if (ptr == NULL) {
cbbf9d3e 3542 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 3543 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3544 return -1;
84004dbf
RS
3545 }
3546 *ptr = 0;
3547 ptr++;
3548
3549 symlink_data->newpathobject = strdup(ptr);
3550 if (symlink_data->newpathobject == NULL) {
cbbf9d3e 3551 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 3552 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3553 return -1;
84004dbf
RS
3554 }
3555
3556 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
3557 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3558 return -1;
84004dbf
RS
3559 }
3560
3561 return 0;
3562}
3563
3564
3565
3566/*
3567 * Async rename()
3568 */
3569struct nfs_rename_data {
3570 char *oldpath;
3571 char *oldobject;
3572 struct nfs_fh3 olddir;
3573 char *newpath;
3574 char *newobject;
3575 struct nfs_fh3 newdir;
3576};
3577
3578static void free_nfs_rename_data(void *mem)
3579{
3580 struct nfs_rename_data *data = mem;
3581
3582 if (data->oldpath != NULL) {
3583 free(data->oldpath);
3584 }
3585 if (data->olddir.data.data_val != NULL) {
3586 free(data->olddir.data.data_val);
3587 }
3588 if (data->newpath != NULL) {
3589 free(data->newpath);
3590 }
3591 if (data->newdir.data.data_val != NULL) {
3592 free(data->newdir.data.data_val);
3593 }
3594 free(data);
3595}
3596
f3a75078 3597static void nfs_rename_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3598{
3599 RENAME3res *res;
3600 struct nfs_cb_data *data = private_data;
3601 struct nfs_context *nfs = data->nfs;
3602 struct nfs_rename_data *rename_data = data->continue_data;
3603
f3a75078
RS
3604 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3605
84004dbf
RS
3606 if (status == RPC_STATUS_ERROR) {
3607 data->cb(-EFAULT, nfs, command_data, data->private_data);
3608 free_nfs_cb_data(data);
3609 return;
3610 }
3611 if (status == RPC_STATUS_CANCEL) {
3612 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3613 free_nfs_cb_data(data);
3614 return;
3615 }
3616
3617 res = command_data;
3618 if (res->status != NFS3_OK) {
3619 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));
3620 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3621 free_nfs_cb_data(data);
3622 return;
3623 }
3624
3625 data->cb(0, nfs, NULL, data->private_data);
3626 free_nfs_cb_data(data);
3627}
3628
3629static int nfs_rename_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3630{
3631 struct nfs_rename_data *rename_data = data->continue_data;
5940c705 3632 RENAME3args args;
84004dbf
RS
3633
3634 /* steal the filehandle */
2452c57f 3635 rename_data->newdir = data->fh;
84004dbf
RS
3636 data->fh.data.data_val = NULL;
3637
5940c705
RS
3638 args.from.dir = rename_data->olddir;
3639 args.from.name = rename_data->oldobject;
3640 args.to.dir = rename_data->newdir;
3641 args.to.name = rename_data->newobject;
3642 if (rpc_nfs3_rename_async(nfs->rpc, nfs_rename_cb, &args, data) != 0) {
84004dbf
RS
3643 rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path);
3644 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3645 free_nfs_cb_data(data);
3646 return -1;
3647 }
3648 return 0;
3649}
3650
3651
3652static int nfs_rename_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3653{
3654 struct nfs_rename_data *rename_data = data->continue_data;
3655
3656 /* steal the filehandle */
2452c57f 3657 rename_data->olddir = data->fh;
84004dbf
RS
3658 data->fh.data.data_val = NULL;
3659
3660 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) {
3661 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", rename_data->newpath);
3662 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3663 free_nfs_cb_data(data);
3664 return -1;
3665 }
3666 data->continue_data = NULL;
3667 free_nfs_cb_data(data);
3668
3669 return 0;
3670}
3671
3672
3673int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3674{
3675 char *ptr;
3676 struct nfs_rename_data *rename_data;
3677
3678 rename_data = malloc(sizeof(struct nfs_rename_data));
3679 if (rename_data == NULL) {
cbbf9d3e 3680 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data");
84004dbf
RS
3681 return -1;
3682 }
ea98629a 3683 memset(rename_data, 0, sizeof(struct nfs_rename_data));
84004dbf
RS
3684
3685 rename_data->oldpath = strdup(oldpath);
3686 if (rename_data->oldpath == NULL) {
cbbf9d3e 3687 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3688 free_nfs_rename_data(rename_data);
cbbf9d3e 3689 return -1;
84004dbf 3690 }
93e9306f 3691 ptr = strrchr(rename_data->oldpath, '/');
84004dbf 3692 if (ptr == NULL) {
cbbf9d3e 3693 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 3694 free_nfs_rename_data(rename_data);
cbbf9d3e 3695 return -1;
84004dbf
RS
3696 }
3697 *ptr = 0;
3698 ptr++;
3699 rename_data->oldobject = ptr;
3700
3701
3702 rename_data->newpath = strdup(newpath);
3703 if (rename_data->newpath == NULL) {
cbbf9d3e 3704 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 3705 free_nfs_rename_data(rename_data);
cbbf9d3e 3706 return -1;
84004dbf 3707 }
93e9306f 3708 ptr = strrchr(rename_data->newpath, '/');
84004dbf 3709 if (ptr == NULL) {
cbbf9d3e 3710 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 3711 free_nfs_rename_data(rename_data);
cbbf9d3e 3712 return -1;
84004dbf
RS
3713 }
3714 *ptr = 0;
3715 ptr++;
3716 rename_data->newobject = ptr;
3717
3718
3719 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
3720 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3721 return -1;
84004dbf
RS
3722 }
3723
3724 return 0;
3725}
3726
3727
3728/*
3729 * Async link()
3730 */
3731struct nfs_link_data {
3732 char *oldpath;
3733 struct nfs_fh3 oldfh;
3734 char *newpath;
3735 char *newobject;
3736 struct nfs_fh3 newdir;
3737};
3738
3739static void free_nfs_link_data(void *mem)
3740{
3741 struct nfs_link_data *data = mem;
3742
3743 if (data->oldpath != NULL) {
3744 free(data->oldpath);
3745 }
3746 if (data->oldfh.data.data_val != NULL) {
3747 free(data->oldfh.data.data_val);
3748 }
3749 if (data->newpath != NULL) {
3750 free(data->newpath);
3751 }
3752 if (data->newdir.data.data_val != NULL) {
3753 free(data->newdir.data.data_val);
3754 }
3755 free(data);
3756}
3757
f3a75078 3758static void nfs_link_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3759{
3760 LINK3res *res;
3761 struct nfs_cb_data *data = private_data;
3762 struct nfs_context *nfs = data->nfs;
3763 struct nfs_link_data *link_data = data->continue_data;
3764
f3a75078
RS
3765 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3766
84004dbf
RS
3767 if (status == RPC_STATUS_ERROR) {
3768 data->cb(-EFAULT, nfs, command_data, data->private_data);
3769 free_nfs_cb_data(data);
3770 return;
3771 }
3772 if (status == RPC_STATUS_CANCEL) {
3773 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3774 free_nfs_cb_data(data);
3775 return;
3776 }
3777
3778 res = command_data;
3779 if (res->status != NFS3_OK) {
3780 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));
3781 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3782 free_nfs_cb_data(data);
3783 return;
3784 }
3785
3786 data->cb(0, nfs, NULL, data->private_data);
3787 free_nfs_cb_data(data);
3788}
3789
3790static int nfs_link_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3791{
3792 struct nfs_link_data *link_data = data->continue_data;
560f9434 3793 LINK3args args;
84004dbf
RS
3794
3795 /* steal the filehandle */
2452c57f 3796 link_data->newdir = data->fh;
84004dbf
RS
3797 data->fh.data.data_val = NULL;
3798
560f9434
RS
3799 memset(&args, 0, sizeof(LINK3args));
3800 args.file = link_data->oldfh;
3801 args.link.dir = link_data->newdir;
3802 args.link.name = link_data->newobject;
3803 if (rpc_nfs3_link_async(nfs->rpc, nfs_link_cb, &args, data) != 0) {
84004dbf
RS
3804 rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path);
3805 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3806 free_nfs_cb_data(data);
3807 return -1;
3808 }
3809 return 0;
3810}
3811
3812
3813static int nfs_link_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3814{
3815 struct nfs_link_data *link_data = data->continue_data;
3816
3817 /* steal the filehandle */
2452c57f 3818 link_data->oldfh = data->fh;
84004dbf
RS
3819 data->fh.data.data_val = NULL;
3820
3821 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) {
3822 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath);
3823 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3824 free_nfs_cb_data(data);
3825 return -1;
3826 }
3827 data->continue_data = NULL;
3828 free_nfs_cb_data(data);
3829
3830 return 0;
3831}
3832
3833
3834int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3835{
3836 char *ptr;
3837 struct nfs_link_data *link_data;
3838
3839 link_data = malloc(sizeof(struct nfs_link_data));
3840 if (link_data == NULL) {
cbbf9d3e 3841 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data");
84004dbf
RS
3842 return -1;
3843 }
ea98629a 3844 memset(link_data, 0, sizeof(struct nfs_link_data));
84004dbf
RS
3845
3846 link_data->oldpath = strdup(oldpath);
3847 if (link_data->oldpath == NULL) {
cbbf9d3e 3848 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3849 free_nfs_link_data(link_data);
cbbf9d3e 3850 return -1;
84004dbf
RS
3851 }
3852
3853 link_data->newpath = strdup(newpath);
3854 if (link_data->newpath == NULL) {
cbbf9d3e 3855 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 3856 free_nfs_link_data(link_data);
cbbf9d3e 3857 return -1;
84004dbf 3858 }
93e9306f 3859 ptr = strrchr(link_data->newpath, '/');
84004dbf 3860 if (ptr == NULL) {
cbbf9d3e 3861 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 3862 free_nfs_link_data(link_data);
cbbf9d3e 3863 return -1;
84004dbf
RS
3864 }
3865 *ptr = 0;
3866 ptr++;
3867 link_data->newobject = ptr;
3868
3869
3870 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
3871 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3872 return -1;
84004dbf
RS
3873 }
3874
3875 return 0;
3876}
3877
3878
3879//qqq replace later with lseek()
183451cf 3880uint64_t nfs_get_current_offset(struct nfsfh *nfsfh)
84004dbf
RS
3881{
3882 return nfsfh->offset;
3883}
3884
17ef62fa
RS
3885
3886
3887/*
3888 * Get the maximum supported READ3 size by the server
3889 */
183451cf 3890uint64_t nfs_get_readmax(struct nfs_context *nfs)
17ef62fa
RS
3891{
3892 return nfs->readmax;
3893}
3894
3895/*
3896 * Get the maximum supported WRITE3 size by the server
3897 */
183451cf 3898uint64_t nfs_get_writemax(struct nfs_context *nfs)
17ef62fa 3899{
e7f3a781 3900 return nfs->writemax;
17ef62fa 3901}
1896d37b
RS
3902
3903void nfs_set_error(struct nfs_context *nfs, char *error_string, ...)
3904{
3905 va_list ap;
1e8994af 3906 char *str = NULL;
1896d37b 3907
1e8994af 3908 va_start(ap, error_string);
2606f9bb
RS
3909 str = malloc(1024);
3910 vsnprintf(str, 1024, error_string, ap);
1896d37b
RS
3911 if (nfs->rpc->error_string != NULL) {
3912 free(nfs->rpc->error_string);
3913 }
1896d37b 3914 nfs->rpc->error_string = str;
a8a1b858 3915 va_end(ap);
1896d37b 3916}
7f0242ca
RS
3917
3918
3919
3920struct mount_cb_data {
3921 rpc_cb cb;
3922 void *private_data;
3923 char *server;
3924};
3925
3926static void free_mount_cb_data(struct mount_cb_data *data)
3927{
3928 if (data->server != NULL) {
3929 free(data->server);
3930 data->server = NULL;
3931 }
3932
3933 free(data);
3934}
3935
3936static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3937{
3938 struct mount_cb_data *data = private_data;
3939
f3a75078
RS
3940 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3941
3942 if (status == RPC_STATUS_ERROR) {
7f0242ca
RS
3943 data->cb(rpc, -EFAULT, command_data, data->private_data);
3944 free_mount_cb_data(data);
3945 return;
3946 }
3947 if (status == RPC_STATUS_CANCEL) {
3948 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3949 free_mount_cb_data(data);
3950 return;
3951 }
3952
3953 data->cb(rpc, 0, command_data, data->private_data);
3954 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
3955 rpc_set_error(rpc, "Failed to disconnect\n");
3956 }
3957 free_mount_cb_data(data);
3958}
3959
3960static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3961{
3962 struct mount_cb_data *data = private_data;
3963
f3a75078
RS
3964 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3965
14a062eb
RS
3966 /* Dont want any more callbacks even if the socket is closed */
3967 rpc->connect_cb = NULL;
3968
7f0242ca
RS
3969 if (status == RPC_STATUS_ERROR) {
3970 data->cb(rpc, -EFAULT, command_data, data->private_data);
3971 free_mount_cb_data(data);
3972 return;
3973 }
3974 if (status == RPC_STATUS_CANCEL) {
3975 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3976 free_mount_cb_data(data);
3977 return;
3978 }
3979
eda77ec3 3980 if (rpc_mount3_export_async(rpc, mount_export_5_cb, data) != 0) {
7f0242ca
RS
3981 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3982 free_mount_cb_data(data);
3983 return;
3984 }
3985}
3986
3987static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3988{
3989 struct mount_cb_data *data = private_data;
3990 uint32_t mount_port;
3991
f3a75078
RS
3992 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3993
3994 if (status == RPC_STATUS_ERROR) {
7f0242ca
RS
3995 data->cb(rpc, -EFAULT, command_data, data->private_data);
3996 free_mount_cb_data(data);
3997 return;
3998 }
3999 if (status == RPC_STATUS_CANCEL) {
4000 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
4001 free_mount_cb_data(data);
4002 return;
4003 }
4004
4005 mount_port = *(uint32_t *)command_data;
4006 if (mount_port == 0) {
4007 rpc_set_error(rpc, "RPC error. Mount program is not available");
4008 data->cb(rpc, -ENOENT, command_data, data->private_data);
4009 free_mount_cb_data(data);
4010 return;
4011 }
4012
4013 rpc_disconnect(rpc, "normal disconnect");
4014 if (rpc_connect_async(rpc, data->server, mount_port, mount_export_4_cb, data) != 0) {
4015 data->cb(rpc, -ENOMEM, command_data, data->private_data);
4016 free_mount_cb_data(data);
4017 return;
4018 }
4019}
4020
4021static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
4022{
4023 struct mount_cb_data *data = private_data;
4024
f3a75078
RS
4025 assert(rpc->magic == RPC_CONTEXT_MAGIC);
4026
7f0242ca
RS
4027 if (status == RPC_STATUS_ERROR) {
4028 data->cb(rpc, -EFAULT, command_data, data->private_data);
4029 free_mount_cb_data(data);
4030 return;
4031 }
4032 if (status == RPC_STATUS_CANCEL) {
4033 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
4034 free_mount_cb_data(data);
4035 return;
4036 }
4037
5c6b1176 4038 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, mount_export_3_cb, private_data) != 0) {
7f0242ca
RS
4039 data->cb(rpc, -ENOMEM, command_data, data->private_data);
4040 free_mount_cb_data(data);
4041 return;
4042 }
4043}
4044
4045static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
4046{
4047 struct mount_cb_data *data = private_data;
4048
f3a75078
RS
4049 assert(rpc->magic == RPC_CONTEXT_MAGIC);
4050
14a062eb
RS
4051 /* Dont want any more callbacks even if the socket is closed */
4052 rpc->connect_cb = NULL;
4053
7f0242ca
RS
4054 if (status == RPC_STATUS_ERROR) {
4055 data->cb(rpc, -EFAULT, command_data, data->private_data);
4056 free_mount_cb_data(data);
4057 return;
4058 }
4059 if (status == RPC_STATUS_CANCEL) {
4060 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
4061 free_mount_cb_data(data);
4062 return;
4063 }
4064
4065 if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) {
4066 data->cb(rpc, -ENOMEM, command_data, data->private_data);
4067 free_mount_cb_data(data);
4068 return;
4069 }
4070}
4071
4072int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data)
4073{
4074 struct mount_cb_data *data;
4075
f3a75078
RS
4076 assert(rpc->magic == RPC_CONTEXT_MAGIC);
4077
7f0242ca
RS
4078 data = malloc(sizeof(struct mount_cb_data));
4079 if (data == NULL) {
4080 return -1;
4081 }
ea98629a 4082 memset(data, 0, sizeof(struct mount_cb_data));
7f0242ca
RS
4083 data->cb = cb;
4084 data->private_data = private_data;
4085 data->server = strdup(server);
4086 if (data->server == NULL) {
4087 free_mount_cb_data(data);
4088 return -1;
4089 }
4090 if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) {
4091 free_mount_cb_data(data);
4092 return -1;
4093 }
4094
4095 return 0;
4096}
4097
df5af25f
RS
4098struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs)
4099{
f3a75078 4100 assert(nfs->rpc->magic == RPC_CONTEXT_MAGIC);
df5af25f
RS
4101 return nfs->rpc;
4102}
4103
b077fdeb
RS
4104const char *nfs_get_server(struct nfs_context *nfs) {
4105 return nfs->server;
4106}
4107
4108const char *nfs_get_export(struct nfs_context *nfs) {
4109 return nfs->export;
4110}
0ad9a1f1 4111
8724c833
RS
4112const struct nfs_fh3 *nfs_get_rootfh(struct nfs_context *nfs) {
4113 return &nfs->rootfh;
4114}
fa3c25be
RS
4115
4116struct nfs_fh3 *nfs_get_fh(struct nfsfh *nfsfh) {
4117 return &nfsfh->fh;
4118}