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