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