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