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