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