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