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