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