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