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