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