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