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