2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
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.
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.
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/>.
18 * High level api to nfs filesystems
25 #include "aros_compat.h"
29 #include "win32_compat.h"
37 #define statvfs statfs
50 #ifdef HAVE_SYS_STATVFS_H
51 #include <sys/statvfs.h>
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
68 #include <sys/types.h>
71 #include "libnfs-zdr.h"
73 #include "libnfs-raw.h"
74 #include "libnfs-raw-mount.h"
75 #include "libnfs-raw-nfs.h"
76 #include "libnfs-private.h"
79 struct nfsdirent
*entries
;
80 struct nfsdirent
*current
;
90 struct rpc_context
*rpc
;
93 struct nfs_fh3 rootfh
;
99 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
101 while (nfsdir
->entries
) {
102 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
103 if (nfsdir
->entries
->name
!= NULL
) {
104 free(nfsdir
->entries
->name
);
106 free(nfsdir
->entries
);
107 nfsdir
->entries
= dirent
;
113 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
116 struct nfs_context
*nfs
;
118 char *saved_path
, *path
;
123 continue_func continue_cb
;
125 void (*free_continue_data
)(void *);
130 /* for multi-read/write calls. */
134 uint64_t start_offset
, max_offset
;
138 struct nfs_mcb_data
{
139 struct nfs_cb_data
*data
;
144 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
146 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
148 rpc_set_auth(nfs
->rpc
, auth
);
151 int nfs_get_fd(struct nfs_context
*nfs
)
153 return rpc_get_fd(nfs
->rpc
);
156 int nfs_queue_length(struct nfs_context
*nfs
)
158 return rpc_queue_length(nfs
->rpc
);
161 int nfs_which_events(struct nfs_context
*nfs
)
163 return rpc_which_events(nfs
->rpc
);
166 int nfs_service(struct nfs_context
*nfs
, int revents
)
168 return rpc_service(nfs
->rpc
, revents
);
171 char *nfs_get_error(struct nfs_context
*nfs
)
173 return rpc_get_error(nfs
->rpc
);
176 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
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
));
188 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
190 struct nfs_url
*urls
;
191 char *strp
, *flagsp
, *strp2
;
193 if (strncmp(url
, "nfs://", 6)) {
194 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
198 urls
= malloc(sizeof(struct nfs_url
));
200 rpc_set_error(nfs
->rpc
, "Out of memory");
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");
212 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
213 urls
->server
[0] == '?') {
215 flagsp
= strchr(urls
->server
, '?');
218 nfs_destroy_url(urls
);
219 rpc_set_error(nfs
->rpc
, "Invalid server string");
223 strp
= strchr(urls
->server
, '/');
226 flagsp
= strchr(urls
->server
, '?');
229 nfs_destroy_url(urls
);
230 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
234 urls
->path
= strdup(strp
);
235 if (urls
->path
== NULL
) {
236 nfs_destroy_url(urls
);
237 rpc_set_error(nfs
->rpc
, "Out of memory");
243 flagsp
= strchr(urls
->path
, '?');
247 strp
= strrchr(urls
->path
, '/');
250 flagsp
= strchr(urls
->path
, '?');
253 nfs_destroy_url(urls
);
254 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
257 urls
->file
= strdup(strp
);
258 if (urls
->path
== NULL
) {
259 nfs_destroy_url(urls
);
260 rpc_set_error(nfs
->rpc
, "Out of memory");
264 flagsp
= strchr(urls
->file
, '?');
271 if (urls
->file
&& !strlen(urls
->file
)) {
275 nfs_destroy_url(urls
);
276 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
281 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
283 flagsp
= strchr(strp
, '&');
287 strp2
= strchr(strp
, '=');
291 nfs_set_context_args(nfs
, strp
, strp2
);
295 if (urls
->server
&& strlen(urls
->server
) <= 1) {
303 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
305 return nfs_parse_url(nfs
, url
, 0, 0);
308 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
310 return nfs_parse_url(nfs
, url
, 1, 0);
313 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
315 return nfs_parse_url(nfs
, url
, 0, 1);
319 void nfs_destroy_url(struct nfs_url
*url
)
329 struct nfs_context
*nfs_init_context(void)
331 struct nfs_context
*nfs
;
333 nfs
= malloc(sizeof(struct nfs_context
));
337 memset(nfs
, 0, sizeof(struct nfs_context
));
339 nfs
->rpc
= rpc_init_context();
340 if (nfs
->rpc
== NULL
) {
345 nfs
->cwd
= strdup("/");
350 void nfs_destroy_context(struct nfs_context
*nfs
)
352 rpc_destroy_context(nfs
->rpc
);
370 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
371 free(nfs
->rootfh
.data
.data_val
);
372 nfs
->rootfh
.data
.data_val
= NULL
;
387 void free_rpc_cb_data(struct rpc_cb_data
*data
)
394 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
396 struct rpc_cb_data
*data
= private_data
;
398 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
400 /* Dont want any more callbacks even if the socket is closed */
401 rpc
->connect_cb
= NULL
;
403 if (status
== RPC_STATUS_ERROR
) {
404 data
->cb(rpc
, status
, command_data
, data
->private_data
);
405 free_rpc_cb_data(data
);
408 if (status
== RPC_STATUS_CANCEL
) {
409 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
410 free_rpc_cb_data(data
);
414 data
->cb(rpc
, status
, NULL
, data
->private_data
);
415 free_rpc_cb_data(data
);
418 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
420 struct rpc_cb_data
*data
= private_data
;
423 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
425 if (status
== RPC_STATUS_ERROR
) {
426 data
->cb(rpc
, status
, command_data
, data
->private_data
);
427 free_rpc_cb_data(data
);
430 if (status
== RPC_STATUS_CANCEL
) {
431 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
432 free_rpc_cb_data(data
);
436 rpc_port
= *(uint32_t *)command_data
;
438 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
439 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
440 free_rpc_cb_data(data
);
444 rpc_disconnect(rpc
, "normal disconnect");
445 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
446 data
->cb(rpc
, status
, command_data
, data
->private_data
);
447 free_rpc_cb_data(data
);
452 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
454 struct rpc_cb_data
*data
= private_data
;
456 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
458 if (status
== RPC_STATUS_ERROR
) {
459 data
->cb(rpc
, status
, command_data
, data
->private_data
);
460 free_rpc_cb_data(data
);
463 if (status
== RPC_STATUS_CANCEL
) {
464 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
465 free_rpc_cb_data(data
);
469 if (rpc_pmap_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
470 data
->cb(rpc
, status
, command_data
, data
->private_data
);
471 free_rpc_cb_data(data
);
476 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
478 struct rpc_cb_data
*data
= private_data
;
480 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
482 /* Dont want any more callbacks even if the socket is closed */
483 rpc
->connect_cb
= NULL
;
485 if (status
== RPC_STATUS_ERROR
) {
486 data
->cb(rpc
, status
, command_data
, data
->private_data
);
487 free_rpc_cb_data(data
);
490 if (status
== RPC_STATUS_CANCEL
) {
491 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
492 free_rpc_cb_data(data
);
496 if (rpc_pmap_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
497 data
->cb(rpc
, status
, command_data
, data
->private_data
);
498 free_rpc_cb_data(data
);
503 int rpc_connect_program_async(struct rpc_context
*rpc
, char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
505 struct rpc_cb_data
*data
;
507 data
= malloc(sizeof(struct rpc_cb_data
));
511 memset(data
, 0, sizeof(struct rpc_cb_data
));
512 data
->server
= strdup(server
);
513 data
->program
= program
;
514 data
->version
= version
;
517 data
->private_data
= private_data
;
519 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
520 rpc_set_error(rpc
, "Failed to start connection");
521 free_rpc_cb_data(data
);
527 void free_nfs_cb_data(struct nfs_cb_data
*data
)
529 if (data
->saved_path
!= NULL
) {
530 free(data
->saved_path
);
531 data
->saved_path
= NULL
;
534 if (data
->continue_data
!= NULL
) {
535 data
->free_continue_data(data
->continue_data
);
536 data
->continue_data
= NULL
;
539 if (data
->fh
.data
.data_val
!= NULL
) {
540 free(data
->fh
.data
.data_val
);
541 data
->fh
.data
.data_val
= NULL
;
544 if (data
->buffer
!= NULL
) {
556 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
558 struct nfs_cb_data
*data
= private_data
;
559 struct nfs_context
*nfs
= data
->nfs
;
561 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
563 if (status
== RPC_STATUS_ERROR
) {
564 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
565 free_nfs_cb_data(data
);
568 if (status
== RPC_STATUS_CANCEL
) {
569 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
570 free_nfs_cb_data(data
);
574 data
->cb(0, nfs
, NULL
, data
->private_data
);
575 free_nfs_cb_data(data
);
578 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
580 struct nfs_cb_data
*data
= private_data
;
581 struct nfs_context
*nfs
= data
->nfs
;
582 FSINFO3res
*res
= command_data
;
583 struct GETATTR3args args
;
585 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
587 if (status
== RPC_STATUS_ERROR
) {
588 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
589 free_nfs_cb_data(data
);
592 if (status
== RPC_STATUS_CANCEL
) {
593 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
594 free_nfs_cb_data(data
);
598 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
599 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
601 memset(&args
, 0, sizeof(GETATTR3args
));
602 args
.object
= nfs
->rootfh
;
604 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
605 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
606 free_nfs_cb_data(data
);
611 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
613 struct nfs_cb_data
*data
= private_data
;
614 struct nfs_context
*nfs
= data
->nfs
;
615 struct FSINFO3args args
;
617 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
619 if (status
== RPC_STATUS_ERROR
) {
620 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
621 free_nfs_cb_data(data
);
624 if (status
== RPC_STATUS_CANCEL
) {
625 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
626 free_nfs_cb_data(data
);
630 args
.fsroot
= nfs
->rootfh
;
631 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
632 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
633 free_nfs_cb_data(data
);
639 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
641 struct nfs_cb_data
*data
= private_data
;
642 struct nfs_context
*nfs
= data
->nfs
;
644 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
646 /* Dont want any more callbacks even if the socket is closed */
647 rpc
->connect_cb
= NULL
;
649 if (status
== RPC_STATUS_ERROR
) {
650 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
651 free_nfs_cb_data(data
);
654 if (status
== RPC_STATUS_CANCEL
) {
655 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
656 free_nfs_cb_data(data
);
660 if (rpc_nfs3_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
661 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
662 free_nfs_cb_data(data
);
668 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
670 struct nfs_cb_data
*data
= private_data
;
671 struct nfs_context
*nfs
= data
->nfs
;
674 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
676 if (status
== RPC_STATUS_ERROR
) {
677 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
678 free_nfs_cb_data(data
);
681 if (status
== RPC_STATUS_CANCEL
) {
682 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
683 free_nfs_cb_data(data
);
688 if (res
->fhs_status
!= MNT3_OK
) {
689 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
));
690 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
691 free_nfs_cb_data(data
);
695 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
696 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
697 if (nfs
->rootfh
.data
.data_val
== NULL
) {
698 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
699 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
700 free_nfs_cb_data(data
);
703 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
705 rpc_disconnect(rpc
, "normal disconnect");
706 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
707 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
708 free_nfs_cb_data(data
);
711 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
712 rpc_set_autoreconnect(rpc
);
716 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
718 struct nfs_cb_data
*data
= private_data
;
719 struct nfs_context
*nfs
= data
->nfs
;
721 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
723 if (status
== RPC_STATUS_ERROR
) {
724 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
725 free_nfs_cb_data(data
);
728 if (status
== RPC_STATUS_CANCEL
) {
729 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
730 free_nfs_cb_data(data
);
734 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
735 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
736 free_nfs_cb_data(data
);
741 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
743 struct nfs_cb_data
*data
= private_data
;
744 struct nfs_context
*nfs
= data
->nfs
;
746 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
748 /* Dont want any more callbacks even if the socket is closed */
749 rpc
->connect_cb
= NULL
;
751 if (status
== RPC_STATUS_ERROR
) {
752 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
753 free_nfs_cb_data(data
);
756 if (status
== RPC_STATUS_CANCEL
) {
757 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
758 free_nfs_cb_data(data
);
762 if (rpc_mount3_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
763 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
764 free_nfs_cb_data(data
);
769 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
771 struct nfs_cb_data
*data
= private_data
;
772 struct nfs_context
*nfs
= data
->nfs
;
775 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
777 if (status
== RPC_STATUS_ERROR
) {
778 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
779 free_nfs_cb_data(data
);
782 if (status
== RPC_STATUS_CANCEL
) {
783 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
784 free_nfs_cb_data(data
);
788 mount_port
= *(uint32_t *)command_data
;
789 if (mount_port
== 0) {
790 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
791 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
792 free_nfs_cb_data(data
);
796 rpc_disconnect(rpc
, "normal disconnect");
797 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
798 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
799 free_nfs_cb_data(data
);
805 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
807 struct nfs_cb_data
*data
= private_data
;
808 struct nfs_context
*nfs
= data
->nfs
;
810 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
812 if (status
== RPC_STATUS_ERROR
) {
813 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
814 free_nfs_cb_data(data
);
817 if (status
== RPC_STATUS_CANCEL
) {
818 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
819 free_nfs_cb_data(data
);
823 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
824 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
825 free_nfs_cb_data(data
);
830 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
832 struct nfs_cb_data
*data
= private_data
;
833 struct nfs_context
*nfs
= data
->nfs
;
835 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
837 /* Dont want any more callbacks even if the socket is closed */
838 rpc
->connect_cb
= NULL
;
840 if (status
== RPC_STATUS_ERROR
) {
841 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
842 free_nfs_cb_data(data
);
845 if (status
== RPC_STATUS_CANCEL
) {
846 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
847 free_nfs_cb_data(data
);
851 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
852 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
853 free_nfs_cb_data(data
);
859 * Async call for mounting an nfs share and geting the root filehandle
861 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
863 struct nfs_cb_data
*data
;
864 char *new_server
, *new_export
;
866 data
= malloc(sizeof(struct nfs_cb_data
));
868 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
871 memset(data
, 0, sizeof(struct nfs_cb_data
));
872 new_server
= strdup(server
);
873 new_export
= strdup(export
);
874 if (nfs
->server
!= NULL
) {
877 nfs
->server
= new_server
;
878 if (nfs
->export
!= NULL
) {
881 nfs
->export
= new_export
;
884 data
->private_data
= private_data
;
886 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
887 rpc_set_error(nfs
->rpc
, "Failed to start connection");
888 free_nfs_cb_data(data
);
898 * Functions to first look up a path, component by component, and then finally call a specific function once
899 * the filehandle for the final component is found.
901 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
903 struct nfs_cb_data
*data
= private_data
;
904 struct nfs_context
*nfs
= data
->nfs
;
907 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
909 if (status
== RPC_STATUS_ERROR
) {
910 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
911 free_nfs_cb_data(data
);
914 if (status
== RPC_STATUS_CANCEL
) {
915 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
916 free_nfs_cb_data(data
);
921 if (res
->status
!= NFS3_OK
) {
922 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
));
923 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
924 free_nfs_cb_data(data
);
928 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
929 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
930 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
931 free_nfs_cb_data(data
);
936 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
941 while (*data
->path
== '/') {
946 slash
= strchr(path
, '/');
948 /* Clear slash so that path is a zero terminated string for
949 * the current path component. Set it back to '/' again later
950 * when we are finished referencing this component so that
951 * data->saved_path will still point to the full
955 data
->path
= slash
+1;
957 while (*data
->path
!= 0) {
963 data
->fh
.data
.data_len
= fh
->data
.data_len
;
964 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
965 if (data
->fh
.data
.data_val
== NULL
) {
966 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
967 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
968 free_nfs_cb_data(data
);
971 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
975 data
->continue_cb(nfs
, data
);
980 memset(&args
, 0, sizeof(LOOKUP3args
));
982 args
.what
.name
= path
;
984 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
985 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
986 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
987 free_nfs_cb_data(data
);
996 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
1002 while ((str
= strstr(path
, "//"))) {
1010 while ((str
= strstr(path
, "/./"))) {
1017 /* ^/../ -> error */
1018 if (!strncmp(path
, "/../", 4)) {
1019 rpc_set_error(nfs
->rpc
,
1020 "Absolute path starts with '/../' "
1021 "during normalization");
1025 /* ^[^/] -> error */
1026 if (path
[0] != '/') {
1027 rpc_set_error(nfs
->rpc
,
1028 "Absolute path does not start with '/'");
1032 /* /string/../ -> / */
1033 while ((str
= strstr(path
, "/../"))) {
1036 if (!strncmp(path
, "/../", 4)) {
1037 rpc_set_error(nfs
->rpc
,
1038 "Absolute path starts with '/../' "
1039 "during normalization");
1044 while (*tmp
!= '/') {
1048 while((*(tmp
++) = *(str
++)) != '\0')
1055 if (path
[len
- 1] == '/') {
1056 path
[len
- 1] = '\0';
1060 if (path
[0] == '\0') {
1061 rpc_set_error(nfs
->rpc
,
1062 "Absolute path became '' "
1063 "during normalization");
1069 if (!strcmp(&path
[len
- 2], "/.")) {
1070 path
[len
- 2] = '\0';
1075 /* ^/..$ -> error */
1076 if (!strcmp(path
, "/..")) {
1077 rpc_set_error(nfs
->rpc
,
1078 "Absolute path is '/..' "
1079 "during normalization");
1083 /* /string/..$ -> / */
1085 if (!strcmp(&path
[len
- 3], "/..")) {
1086 char *tmp
= &path
[len
- 3];
1087 while (*--tmp
!= '/')
1096 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
)
1098 struct nfs_cb_data
*data
;
1100 if (path
[0] == '\0') {
1104 data
= malloc(sizeof(struct nfs_cb_data
));
1106 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1107 "nfs_cb_data structure");
1108 if (free_continue_data
)
1109 free_continue_data(continue_data
);
1112 memset(data
, 0, sizeof(struct nfs_cb_data
));
1115 data
->continue_cb
= continue_cb
;
1116 data
->continue_data
= continue_data
;
1117 data
->free_continue_data
= free_continue_data
;
1118 data
->continue_int
= continue_int
;
1119 data
->private_data
= private_data
;
1120 if (path
[0] == '/') {
1121 data
->saved_path
= strdup(path
);
1123 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1124 if (data
->saved_path
== NULL
) {
1125 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1126 "malloc path string");
1127 free_nfs_cb_data(data
);
1130 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1133 if (data
->saved_path
== NULL
) {
1134 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1135 free_nfs_cb_data(data
);
1138 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1139 free_nfs_cb_data(data
);
1143 data
->path
= data
->saved_path
;
1145 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1146 /* return 0 here since the callback will be invoked if there is a failure */
1159 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1162 struct nfs_cb_data
*data
= private_data
;
1163 struct nfs_context
*nfs
= data
->nfs
;
1170 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1172 if (status
== RPC_STATUS_ERROR
) {
1173 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1174 free_nfs_cb_data(data
);
1177 if (status
== RPC_STATUS_CANCEL
) {
1178 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1179 free_nfs_cb_data(data
);
1184 if (res
->status
!= NFS3_OK
) {
1185 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
));
1186 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1187 free_nfs_cb_data(data
);
1192 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1193 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1194 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1195 st
.st_mode
|= S_IFDIR
;
1197 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1198 st
.st_mode
|= S_IFREG
;
1200 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1201 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1202 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1204 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1206 st
.st_blksize
= 4096;
1207 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1209 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1210 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1211 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1213 data
->cb(0, nfs
, &st
, data
->private_data
);
1214 free_nfs_cb_data(data
);
1217 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1219 struct GETATTR3args args
;
1221 memset(&args
, 0, sizeof(GETATTR3args
));
1222 args
.object
= data
->fh
;
1224 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1225 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1226 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1227 free_nfs_cb_data(data
);
1233 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1235 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1236 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1248 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1250 struct nfs_cb_data
*data
= private_data
;
1251 struct nfs_context
*nfs
= data
->nfs
;
1252 struct nfsfh
*nfsfh
;
1255 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1257 if (status
== RPC_STATUS_ERROR
) {
1258 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1259 free_nfs_cb_data(data
);
1262 if (status
== RPC_STATUS_CANCEL
) {
1263 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1264 free_nfs_cb_data(data
);
1269 if (res
->status
!= NFS3_OK
) {
1270 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1271 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1272 free_nfs_cb_data(data
);
1276 nfsfh
= malloc(sizeof(struct nfsfh
));
1277 if (nfsfh
== NULL
) {
1278 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1279 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1280 free_nfs_cb_data(data
);
1283 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1285 if (data
->continue_int
& O_SYNC
) {
1289 /* steal the filehandle */
1290 nfsfh
->fh
= data
->fh
;
1291 data
->fh
.data
.data_val
= NULL
;
1293 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1294 free_nfs_cb_data(data
);
1297 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1300 struct nfs_cb_data
*data
= private_data
;
1301 struct nfs_context
*nfs
= data
->nfs
;
1302 struct nfsfh
*nfsfh
;
1303 unsigned int nfsmode
= 0;
1305 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1307 if (status
== RPC_STATUS_ERROR
) {
1308 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1309 free_nfs_cb_data(data
);
1312 if (status
== RPC_STATUS_CANCEL
) {
1313 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1314 free_nfs_cb_data(data
);
1319 if (res
->status
!= NFS3_OK
) {
1320 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
));
1321 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1322 free_nfs_cb_data(data
);
1326 if (data
->continue_int
& O_WRONLY
) {
1327 nfsmode
|= ACCESS3_MODIFY
;
1329 if (data
->continue_int
& O_RDWR
) {
1330 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1332 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1333 nfsmode
|= ACCESS3_READ
;
1337 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1338 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1339 nfsmode
&ACCESS3_READ
?'r':'-',
1340 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1341 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1342 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1343 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1344 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1345 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1346 free_nfs_cb_data(data
);
1350 /* Try to truncate it if we were requested to */
1351 if ((data
->continue_int
& O_TRUNC
) &&
1352 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1355 memset(&args
, 0, sizeof(SETATTR3args
));
1356 args
.object
= data
->fh
;
1357 args
.new_attributes
.size
.set_it
= 1;
1358 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1360 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1362 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1363 "SETATTR call for %s", data
->path
);
1364 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1365 data
->private_data
);
1366 free_nfs_cb_data(data
);
1372 nfsfh
= malloc(sizeof(struct nfsfh
));
1373 if (nfsfh
== NULL
) {
1374 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1375 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1376 free_nfs_cb_data(data
);
1379 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1381 if (data
->continue_int
& O_SYNC
) {
1385 /* steal the filehandle */
1386 nfsfh
->fh
= data
->fh
;
1387 data
->fh
.data
.data_val
= NULL
;
1389 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1390 free_nfs_cb_data(data
);
1393 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1398 if (data
->continue_int
& O_WRONLY
) {
1399 nfsmode
|= ACCESS3_MODIFY
;
1401 if (data
->continue_int
& O_RDWR
) {
1402 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1404 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1405 nfsmode
|= ACCESS3_READ
;
1408 memset(&args
, 0, sizeof(ACCESS3args
));
1409 args
.object
= data
->fh
;
1410 args
.access
= nfsmode
;
1412 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1413 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1414 "call for %s", data
->path
);
1415 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1416 data
->private_data
);
1417 free_nfs_cb_data(data
);
1423 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1425 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1426 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1437 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1439 /* steal saved_path */
1441 nfs
->cwd
= data
->saved_path
;
1442 data
->saved_path
= NULL
;
1444 data
->cb(0, nfs
, NULL
, data
->private_data
);
1445 free_nfs_cb_data(data
);
1450 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1452 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1453 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1464 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1466 struct nfs_cb_data
*data
= private_data
;
1467 struct nfs_context
*nfs
= data
->nfs
;
1470 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1472 if (status
== RPC_STATUS_ERROR
) {
1473 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1474 free_nfs_cb_data(data
);
1477 if (status
== RPC_STATUS_CANCEL
) {
1478 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1479 free_nfs_cb_data(data
);
1484 if (res
->status
!= NFS3_OK
) {
1485 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1486 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1487 free_nfs_cb_data(data
);
1491 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1492 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1493 free_nfs_cb_data(data
);
1496 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1498 struct nfs_mcb_data
*mdata
= private_data
;
1499 struct nfs_cb_data
*data
= mdata
->data
;
1500 struct nfs_context
*nfs
= data
->nfs
;
1503 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1507 if (status
== RPC_STATUS_ERROR
) {
1508 /* flag the failure but do not invoke callback until we have received all responses */
1511 if (status
== RPC_STATUS_CANCEL
) {
1512 /* flag the cancellation but do not invoke callback until we have received all responses */
1516 /* reassemble the data into the buffer */
1517 if (status
== RPC_STATUS_SUCCESS
) {
1519 if (res
->status
!= NFS3_OK
) {
1520 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1523 if (res
->READ3res_u
.resok
.count
> 0) {
1524 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1525 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1526 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1532 if (data
->num_calls
> 0) {
1533 /* still waiting for more replies */
1538 if (data
->error
!= 0) {
1539 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1540 free_nfs_cb_data(data
);
1544 if (data
->cancel
!= 0) {
1545 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1546 free_nfs_cb_data(data
);
1551 data
->nfsfh
->offset
= data
->max_offset
;
1552 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1554 free_nfs_cb_data(data
);
1558 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1560 struct nfs_cb_data
*data
;
1562 data
= malloc(sizeof(struct nfs_cb_data
));
1564 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1567 memset(data
, 0, sizeof(struct nfs_cb_data
));
1570 data
->private_data
= private_data
;
1571 data
->nfsfh
= nfsfh
;
1573 nfsfh
->offset
= offset
;
1575 if (count
<= nfs_get_readmax(nfs
)) {
1578 memset(&args
, 0, sizeof(READ3args
));
1579 args
.file
= nfsfh
->fh
;
1580 args
.offset
= offset
;
1583 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_cb
, &args
, data
) != 0) {
1584 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1585 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1586 free_nfs_cb_data(data
);
1592 /* trying to read more than maximum server read size, we has to chop it up into smaller
1593 * reads and collect into a reassembly buffer.
1594 * we send all reads in parallel so that performance is still good.
1596 data
->max_offset
= offset
;
1597 data
->start_offset
= offset
;
1599 data
->buffer
= malloc(count
);
1600 if (data
->buffer
== NULL
) {
1601 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1602 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1603 free_nfs_cb_data(data
);
1608 uint64_t readcount
= count
;
1609 struct nfs_mcb_data
*mdata
;
1612 if (readcount
> nfs_get_readmax(nfs
)) {
1613 readcount
= nfs_get_readmax(nfs
);
1616 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1617 if (mdata
== NULL
) {
1618 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1621 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1623 mdata
->offset
= offset
;
1624 mdata
->count
= readcount
;
1626 memset(&args
, 0, sizeof(READ3args
));
1627 args
.file
= nfsfh
->fh
;
1628 args
.offset
= offset
;
1629 args
.count
= readcount
;
1631 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1632 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1633 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1639 offset
+= readcount
;
1649 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1651 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1659 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1661 struct nfs_cb_data
*data
= private_data
;
1662 struct nfs_context
*nfs
= data
->nfs
;
1665 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1667 if (status
== RPC_STATUS_ERROR
) {
1668 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1669 free_nfs_cb_data(data
);
1672 if (status
== RPC_STATUS_CANCEL
) {
1673 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1674 free_nfs_cb_data(data
);
1679 if (res
->status
!= NFS3_OK
) {
1680 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1681 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1682 free_nfs_cb_data(data
);
1686 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1687 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1688 free_nfs_cb_data(data
);
1691 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1693 struct nfs_mcb_data
*mdata
= private_data
;
1694 struct nfs_cb_data
*data
= mdata
->data
;
1695 struct nfs_context
*nfs
= data
->nfs
;
1698 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1702 if (status
== RPC_STATUS_ERROR
) {
1703 /* flag the failure but do not invoke callback until we have received all responses */
1706 if (status
== RPC_STATUS_CANCEL
) {
1707 /* flag the cancellation but do not invoke callback until we have received all responses */
1711 if (status
== RPC_STATUS_SUCCESS
) {
1713 if (res
->status
!= NFS3_OK
) {
1714 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1717 if (res
->WRITE3res_u
.resok
.count
> 0) {
1718 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1719 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1725 if (data
->num_calls
> 0) {
1726 /* still waiting for more replies */
1731 if (data
->error
!= 0) {
1732 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1733 free_nfs_cb_data(data
);
1737 if (data
->cancel
!= 0) {
1738 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1739 free_nfs_cb_data(data
);
1744 data
->nfsfh
->offset
= data
->max_offset
;
1745 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1747 free_nfs_cb_data(data
);
1752 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
)
1754 struct nfs_cb_data
*data
;
1756 data
= malloc(sizeof(struct nfs_cb_data
));
1758 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1761 memset(data
, 0, sizeof(struct nfs_cb_data
));
1764 data
->private_data
= private_data
;
1765 data
->nfsfh
= nfsfh
;
1767 nfsfh
->offset
= offset
;
1769 if (count
<= nfs_get_writemax(nfs
)) {
1772 memset(&args
, 0, sizeof(WRITE3args
));
1773 args
.file
= nfsfh
->fh
;
1774 args
.offset
= offset
;
1776 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1777 args
.data
.data_len
= count
;
1778 args
.data
.data_val
= buf
;
1780 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_cb
, &args
, data
) != 0) {
1781 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1782 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1783 free_nfs_cb_data(data
);
1789 /* trying to write more than maximum server write size, we has to chop it up into smaller
1791 * we send all writes in parallel so that performance is still good.
1793 data
->max_offset
= offset
;
1794 data
->start_offset
= offset
;
1797 uint64_t writecount
= count
;
1798 struct nfs_mcb_data
*mdata
;
1801 if (writecount
> nfs_get_writemax(nfs
)) {
1802 writecount
= nfs_get_writemax(nfs
);
1805 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1806 if (mdata
== NULL
) {
1807 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1810 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1812 mdata
->offset
= offset
;
1813 mdata
->count
= writecount
;
1815 memset(&args
, 0, sizeof(WRITE3args
));
1816 args
.file
= nfsfh
->fh
;
1817 args
.offset
= offset
;
1818 args
.count
= writecount
;
1819 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1820 args
.data
.data_len
= writecount
;
1821 args
.data
.data_val
= &buf
[offset
- data
->start_offset
];
1823 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1824 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1825 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1830 count
-= writecount
;
1831 offset
+= writecount
;
1841 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1843 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1853 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1855 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1856 free(nfsfh
->fh
.data
.data_val
);
1857 nfsfh
->fh
.data
.data_val
= NULL
;
1861 cb(0, nfs
, NULL
, private_data
);
1872 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1874 struct nfs_cb_data
*data
;
1875 struct GETATTR3args args
;
1877 data
= malloc(sizeof(struct nfs_cb_data
));
1879 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1882 memset(data
, 0, sizeof(struct nfs_cb_data
));
1885 data
->private_data
= private_data
;
1887 memset(&args
, 0, sizeof(GETATTR3args
));
1888 args
.object
= nfsfh
->fh
;
1890 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1891 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1892 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1893 free_nfs_cb_data(data
);
1904 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1906 struct nfs_cb_data
*data
= private_data
;
1907 struct nfs_context
*nfs
= data
->nfs
;
1910 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1912 if (status
== RPC_STATUS_ERROR
) {
1913 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1914 free_nfs_cb_data(data
);
1917 if (status
== RPC_STATUS_CANCEL
) {
1918 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1919 free_nfs_cb_data(data
);
1924 if (res
->status
!= NFS3_OK
) {
1925 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1926 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1927 free_nfs_cb_data(data
);
1931 data
->cb(0, nfs
, NULL
, data
->private_data
);
1932 free_nfs_cb_data(data
);
1935 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1937 struct nfs_cb_data
*data
;
1938 struct COMMIT3args args
;
1940 data
= malloc(sizeof(struct nfs_cb_data
));
1942 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1945 memset(data
, 0, sizeof(struct nfs_cb_data
));
1948 data
->private_data
= private_data
;
1950 args
.file
= nfsfh
->fh
;
1953 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1954 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1955 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1956 free_nfs_cb_data(data
);
1968 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1970 struct nfs_cb_data
*data
= private_data
;
1971 struct nfs_context
*nfs
= data
->nfs
;
1974 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1976 if (status
== RPC_STATUS_ERROR
) {
1977 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1978 free_nfs_cb_data(data
);
1981 if (status
== RPC_STATUS_CANCEL
) {
1982 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1983 free_nfs_cb_data(data
);
1988 if (res
->status
!= NFS3_OK
) {
1989 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1990 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1991 free_nfs_cb_data(data
);
1995 data
->cb(0, nfs
, NULL
, data
->private_data
);
1996 free_nfs_cb_data(data
);
1999 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
2001 struct nfs_cb_data
*data
;
2004 data
= malloc(sizeof(struct nfs_cb_data
));
2006 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2009 memset(data
, 0, sizeof(struct nfs_cb_data
));
2012 data
->private_data
= private_data
;
2014 memset(&args
, 0, sizeof(SETATTR3args
));
2015 args
.object
= nfsfh
->fh
;
2016 args
.new_attributes
.size
.set_it
= 1;
2017 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2019 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2020 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2021 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2022 free_nfs_cb_data(data
);
2032 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2034 uint64_t offset
= data
->continue_int
;
2037 nfsfh
.fh
= data
->fh
;
2039 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2040 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2041 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2042 free_nfs_cb_data(data
);
2045 free_nfs_cb_data(data
);
2049 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2055 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2056 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2069 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2072 struct nfs_cb_data
*data
= private_data
;
2073 struct nfs_context
*nfs
= data
->nfs
;
2074 char *str
= data
->continue_data
;
2076 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2078 str
= &str
[strlen(str
) + 1];
2080 if (status
== RPC_STATUS_ERROR
) {
2081 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2082 free_nfs_cb_data(data
);
2085 if (status
== RPC_STATUS_CANCEL
) {
2086 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2087 free_nfs_cb_data(data
);
2092 if (res
->status
!= NFS3_OK
) {
2093 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
));
2094 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2095 free_nfs_cb_data(data
);
2099 data
->cb(0, nfs
, NULL
, data
->private_data
);
2100 free_nfs_cb_data(data
);
2103 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2105 char *str
= data
->continue_data
;
2108 str
= &str
[strlen(str
) + 1];
2110 memset(&args
, 0, sizeof(MKDIR3args
));
2111 args
.where
.dir
= data
->fh
;
2112 args
.where
.name
= str
;
2113 args
.attributes
.mode
.set_it
= 1;
2114 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2116 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2117 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2118 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2119 free_nfs_cb_data(data
);
2125 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2130 new_path
= strdup(path
);
2131 if (new_path
== NULL
) {
2132 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2136 ptr
= strrchr(new_path
, '/');
2138 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2143 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2144 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2145 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2159 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2162 struct nfs_cb_data
*data
= private_data
;
2163 struct nfs_context
*nfs
= data
->nfs
;
2164 char *str
= data
->continue_data
;
2166 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2168 str
= &str
[strlen(str
) + 1];
2170 if (status
== RPC_STATUS_ERROR
) {
2171 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2172 free_nfs_cb_data(data
);
2175 if (status
== RPC_STATUS_CANCEL
) {
2176 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2177 free_nfs_cb_data(data
);
2182 if (res
->status
!= NFS3_OK
) {
2183 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
));
2184 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2185 free_nfs_cb_data(data
);
2189 data
->cb(0, nfs
, NULL
, data
->private_data
);
2190 free_nfs_cb_data(data
);
2193 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2195 char *str
= data
->continue_data
;
2198 str
= &str
[strlen(str
) + 1];
2200 args
.object
.dir
= data
->fh
;
2201 args
.object
.name
= str
;
2202 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2203 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2204 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2205 free_nfs_cb_data(data
);
2211 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2216 new_path
= strdup(path
);
2217 if (new_path
== NULL
) {
2218 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2222 ptr
= strrchr(new_path
, '/');
2224 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2229 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2230 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2231 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2244 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2247 struct nfs_cb_data
*data
= private_data
;
2248 struct nfs_context
*nfs
= data
->nfs
;
2249 struct nfsfh
*nfsfh
;
2250 char *str
= data
->continue_data
;
2252 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2254 if (status
== RPC_STATUS_ERROR
) {
2255 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2256 free_nfs_cb_data(data
);
2259 if (status
== RPC_STATUS_CANCEL
) {
2260 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2261 free_nfs_cb_data(data
);
2265 str
= &str
[strlen(str
) + 1];
2267 if (res
->status
!= NFS3_OK
) {
2268 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
));
2269 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2274 nfsfh
= malloc(sizeof(struct nfsfh
));
2275 if (nfsfh
== NULL
) {
2276 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2277 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2278 free_nfs_cb_data(data
);
2281 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2283 /* copy the filehandle */
2284 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2285 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2286 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2288 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2289 free_nfs_cb_data(data
);
2294 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2297 struct nfs_cb_data
*data
= private_data
;
2298 struct nfs_context
*nfs
= data
->nfs
;
2299 char *str
= data
->continue_data
;
2302 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2304 if (status
== RPC_STATUS_ERROR
) {
2305 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2306 free_nfs_cb_data(data
);
2309 if (status
== RPC_STATUS_CANCEL
) {
2310 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2311 free_nfs_cb_data(data
);
2315 str
= &str
[strlen(str
) + 1];
2317 if (res
->status
!= NFS3_OK
) {
2318 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
));
2319 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2320 free_nfs_cb_data(data
);
2324 memset(&args
, 0, sizeof(LOOKUP3args
));
2325 args
.what
.dir
= data
->fh
;
2326 args
.what
.name
= str
;
2328 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2329 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2330 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2331 free_nfs_cb_data(data
);
2337 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2339 char *str
= data
->continue_data
;
2342 str
= &str
[strlen(str
) + 1];
2344 memset(&args
, 0, sizeof(CREATE3args
));
2345 args
.where
.dir
= data
->fh
;
2346 args
.where
.name
= str
;
2347 args
.how
.mode
= UNCHECKED
;
2348 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2349 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2351 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2352 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2353 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2354 free_nfs_cb_data(data
);
2360 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2365 new_path
= strdup(path
);
2366 if (new_path
== NULL
) {
2367 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2371 ptr
= strrchr(new_path
, '/');
2373 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2378 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2379 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2380 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2393 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2396 struct nfs_cb_data
*data
= private_data
;
2397 struct nfs_context
*nfs
= data
->nfs
;
2398 char *str
= data
->continue_data
;
2400 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2402 str
= &str
[strlen(str
) + 1];
2404 if (status
== RPC_STATUS_ERROR
) {
2405 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2406 free_nfs_cb_data(data
);
2409 if (status
== RPC_STATUS_CANCEL
) {
2410 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2411 free_nfs_cb_data(data
);
2416 if (res
->status
!= NFS3_OK
) {
2417 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
));
2418 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2419 free_nfs_cb_data(data
);
2423 data
->cb(0, nfs
, NULL
, data
->private_data
);
2424 free_nfs_cb_data(data
);
2427 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2429 char *str
= data
->continue_data
;
2430 struct REMOVE3args args
;
2432 str
= &str
[strlen(str
) + 1];
2434 args
.object
.dir
= data
->fh
;
2435 args
.object
.name
= str
;
2436 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2437 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2438 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2439 free_nfs_cb_data(data
);
2445 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2450 new_path
= strdup(path
);
2451 if (new_path
== NULL
) {
2452 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2456 ptr
= strrchr(new_path
, '/');
2458 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2463 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2464 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2465 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2476 struct mknod_cb_data
{
2483 static void free_mknod_cb_data(void *ptr
)
2485 struct mknod_cb_data
*data
= ptr
;
2491 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2494 struct nfs_cb_data
*data
= private_data
;
2495 struct nfs_context
*nfs
= data
->nfs
;
2496 char *str
= data
->continue_data
;
2498 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2500 str
= &str
[strlen(str
) + 1];
2502 if (status
== RPC_STATUS_ERROR
) {
2503 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2504 free_nfs_cb_data(data
);
2507 if (status
== RPC_STATUS_CANCEL
) {
2508 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2509 free_nfs_cb_data(data
);
2514 if (res
->status
!= NFS3_OK
) {
2515 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
));
2516 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2517 free_nfs_cb_data(data
);
2521 data
->cb(0, nfs
, NULL
, data
->private_data
);
2522 free_nfs_cb_data(data
);
2525 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2527 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2528 char *str
= cb_data
->path
;
2531 str
= &str
[strlen(str
) + 1];
2533 args
.where
.dir
= data
->fh
;
2534 args
.where
.name
= str
;
2535 switch (cb_data
->mode
& S_IFMT
) {
2537 args
.what
.type
= NF3CHR
;
2538 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2539 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
);
2540 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2541 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2544 args
.what
.type
= NF3BLK
;
2545 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2546 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
);
2547 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2548 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2550 args
.what
.type
= NF3SOCK
;
2551 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2552 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
);
2555 args
.what
.type
= NF3FIFO
;
2556 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2557 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
);
2560 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2561 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2562 free_nfs_cb_data(data
);
2566 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2567 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2568 free_nfs_cb_data(data
);
2574 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2577 struct mknod_cb_data
*cb_data
;
2579 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2580 if (cb_data
== NULL
) {
2581 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2585 cb_data
->path
= strdup(path
);
2586 if (cb_data
->path
== NULL
) {
2587 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2592 ptr
= strrchr(cb_data
->path
, '/');
2594 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2595 free_mknod_cb_data(cb_data
);
2600 cb_data
->mode
= mode
;
2601 cb_data
->major
= major(dev
);
2602 cb_data
->minor
= minor(dev
);
2604 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2605 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2606 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2607 free_mknod_cb_data(cb_data
);
2618 /* ReadDirPlus Emulation Callback data */
2619 struct rdpe_cb_data
{
2622 struct nfs_cb_data
*data
;
2625 /* ReadDirPlus Emulation LOOKUP Callback data */
2626 struct rdpe_lookup_cb_data
{
2627 struct rdpe_cb_data
*rdpe_cb_data
;
2628 struct nfsdirent
*nfsdirent
;
2631 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2632 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2634 LOOKUP3res
*res
= command_data
;
2635 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2636 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2637 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2638 struct nfsdir
*nfsdir
= data
->continue_data
;
2639 struct nfs_context
*nfs
= data
->nfs
;
2640 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2642 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2644 free(rdpe_lookup_cb_data
);
2646 rdpe_cb_data
->getattrcount
--;
2648 if (status
== RPC_STATUS_ERROR
) {
2649 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2651 if (status
== RPC_STATUS_CANCEL
) {
2652 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2654 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2655 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2657 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2658 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2659 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2661 nfsdirent
->type
= attributes
->type
;
2662 nfsdirent
->mode
= attributes
->mode
;
2663 nfsdirent
->size
= attributes
->size
;
2665 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2666 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2667 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2668 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2669 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2670 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2671 nfsdirent
->uid
= attributes
->uid
;
2672 nfsdirent
->gid
= attributes
->gid
;
2676 if (rdpe_cb_data
->getattrcount
== 0) {
2677 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2678 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2679 nfs_free_nfsdir(nfsdir
);
2681 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2685 data
->continue_data
= NULL
;
2686 free_nfs_cb_data(data
);
2690 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2692 READDIR3res
*res
= command_data
;
2693 struct nfs_cb_data
*data
= private_data
;
2694 struct nfs_context
*nfs
= data
->nfs
;
2695 struct nfsdir
*nfsdir
= data
->continue_data
;
2696 struct nfsdirent
*nfsdirent
;
2697 struct entry3
*entry
;
2698 uint64_t cookie
= 0;
2699 struct rdpe_cb_data
*rdpe_cb_data
;
2701 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2703 if (status
== RPC_STATUS_ERROR
) {
2704 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2705 nfs_free_nfsdir(nfsdir
);
2706 data
->continue_data
= NULL
;
2707 free_nfs_cb_data(data
);
2711 if (status
== RPC_STATUS_CANCEL
) {
2712 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2713 nfs_free_nfsdir(nfsdir
);
2714 data
->continue_data
= NULL
;
2715 free_nfs_cb_data(data
);
2719 if (res
->status
!= NFS3_OK
) {
2720 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
));
2721 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2722 nfs_free_nfsdir(nfsdir
);
2723 data
->continue_data
= NULL
;
2724 free_nfs_cb_data(data
);
2728 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2729 while (entry
!= NULL
) {
2730 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2731 if (nfsdirent
== NULL
) {
2732 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2733 nfs_free_nfsdir(nfsdir
);
2734 data
->continue_data
= NULL
;
2735 free_nfs_cb_data(data
);
2738 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2739 nfsdirent
->name
= strdup(entry
->name
);
2740 if (nfsdirent
->name
== NULL
) {
2741 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2742 nfs_free_nfsdir(nfsdir
);
2743 data
->continue_data
= NULL
;
2744 free_nfs_cb_data(data
);
2747 nfsdirent
->inode
= entry
->fileid
;
2749 nfsdirent
->next
= nfsdir
->entries
;
2750 nfsdir
->entries
= nfsdirent
;
2752 cookie
= entry
->cookie
;
2753 entry
= entry
->nextentry
;
2756 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2759 args
.dir
= data
->fh
;
2760 args
.cookie
= cookie
;
2761 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2764 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2765 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2766 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2767 nfs_free_nfsdir(nfsdir
);
2768 data
->continue_data
= NULL
;
2769 free_nfs_cb_data(data
);
2775 /* steal the dirhandle */
2776 nfsdir
->current
= nfsdir
->entries
;
2778 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2779 rdpe_cb_data
->getattrcount
= 0;
2780 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2781 rdpe_cb_data
->data
= data
;
2782 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2783 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2786 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2787 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2788 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2790 memset(&args
, 0, sizeof(LOOKUP3args
));
2791 args
.what
.dir
= data
->fh
;
2792 args
.what
.name
= nfsdirent
->name
;
2794 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2795 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2797 /* if we have already commands in flight, we cant just stop, we have to wait for the
2798 * commands in flight to complete
2800 if (rdpe_cb_data
->getattrcount
> 0) {
2801 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2802 free(rdpe_lookup_cb_data
);
2806 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2807 nfs_free_nfsdir(nfsdir
);
2808 data
->continue_data
= NULL
;
2809 free_nfs_cb_data(data
);
2810 free(rdpe_lookup_cb_data
);
2814 rdpe_cb_data
->getattrcount
++;
2819 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2821 READDIRPLUS3res
*res
= command_data
;
2822 struct nfs_cb_data
*data
= private_data
;
2823 struct nfs_context
*nfs
= data
->nfs
;
2824 struct nfsdir
*nfsdir
= data
->continue_data
;
2825 struct entryplus3
*entry
;
2826 uint64_t cookie
= 0;
2828 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2830 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2833 args
.dir
= data
->fh
;
2834 args
.cookie
= cookie
;
2835 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2838 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2839 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2840 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2841 nfs_free_nfsdir(nfsdir
);
2842 data
->continue_data
= NULL
;
2843 free_nfs_cb_data(data
);
2849 if (status
== RPC_STATUS_CANCEL
) {
2850 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2851 nfs_free_nfsdir(nfsdir
);
2852 data
->continue_data
= NULL
;
2853 free_nfs_cb_data(data
);
2857 if (res
->status
!= NFS3_OK
) {
2858 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
));
2859 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2860 nfs_free_nfsdir(nfsdir
);
2861 data
->continue_data
= NULL
;
2862 free_nfs_cb_data(data
);
2866 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2867 while (entry
!= NULL
) {
2868 struct nfsdirent
*nfsdirent
;
2870 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2871 if (nfsdirent
== NULL
) {
2872 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2873 nfs_free_nfsdir(nfsdir
);
2874 data
->continue_data
= NULL
;
2875 free_nfs_cb_data(data
);
2878 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2879 nfsdirent
->name
= strdup(entry
->name
);
2880 if (nfsdirent
->name
== NULL
) {
2881 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2883 nfs_free_nfsdir(nfsdir
);
2884 data
->continue_data
= NULL
;
2885 free_nfs_cb_data(data
);
2888 nfsdirent
->inode
= entry
->fileid
;
2889 if (entry
->name_attributes
.attributes_follow
) {
2890 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2891 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2892 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2894 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2895 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2896 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2897 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2898 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2899 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2900 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2901 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2904 nfsdirent
->next
= nfsdir
->entries
;
2905 nfsdir
->entries
= nfsdirent
;
2907 cookie
= entry
->cookie
;
2908 entry
= entry
->nextentry
;
2911 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2912 READDIRPLUS3args args
;
2914 args
.dir
= data
->fh
;
2915 args
.cookie
= cookie
;
2916 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2917 args
.dircount
= 8192;
2918 args
.maxcount
= 8192;
2920 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2921 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2922 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2923 nfs_free_nfsdir(nfsdir
);
2924 data
->continue_data
= NULL
;
2925 free_nfs_cb_data(data
);
2931 /* steal the dirhandle */
2932 data
->continue_data
= NULL
;
2933 nfsdir
->current
= nfsdir
->entries
;
2935 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2936 free_nfs_cb_data(data
);
2939 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2941 READDIRPLUS3args args
;
2943 args
.dir
= data
->fh
;
2945 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2946 args
.dircount
= 8192;
2947 args
.maxcount
= 8192;
2948 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2949 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2950 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2951 free_nfs_cb_data(data
);
2957 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2959 struct nfsdir
*nfsdir
;
2961 nfsdir
= malloc(sizeof(struct nfsdir
));
2962 if (nfsdir
== NULL
) {
2963 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2966 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2968 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2969 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2977 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2979 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2981 if (nfsdir
->current
!= NULL
) {
2982 nfsdir
->current
= nfsdir
->current
->next
;
2991 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2993 nfs_free_nfsdir(nfsdir
);
3000 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3011 struct lseek_cb_data
{
3012 struct nfs_context
*nfs
;
3013 struct nfsfh
*nfsfh
;
3019 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3022 struct lseek_cb_data
*data
= private_data
;
3023 struct nfs_context
*nfs
= data
->nfs
;
3025 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3027 if (status
== RPC_STATUS_ERROR
) {
3028 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3032 if (status
== RPC_STATUS_CANCEL
) {
3033 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3039 if (res
->status
!= NFS3_OK
) {
3040 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3041 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3046 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3047 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3051 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3053 struct lseek_cb_data
*data
;
3054 struct GETATTR3args args
;
3056 if (whence
== SEEK_SET
) {
3057 nfsfh
->offset
= offset
;
3058 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3061 if (whence
== SEEK_CUR
) {
3062 nfsfh
->offset
+= offset
;
3063 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3067 data
= malloc(sizeof(struct lseek_cb_data
));
3069 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3074 data
->nfsfh
= nfsfh
;
3075 data
->offset
= offset
;
3077 data
->private_data
= private_data
;
3079 memset(&args
, 0, sizeof(GETATTR3args
));
3080 args
.object
= nfsfh
->fh
;
3082 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3083 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3096 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3099 struct nfs_cb_data
*data
= private_data
;
3100 struct nfs_context
*nfs
= data
->nfs
;
3101 struct statvfs svfs
;
3103 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3105 if (status
== RPC_STATUS_ERROR
) {
3106 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3107 free_nfs_cb_data(data
);
3110 if (status
== RPC_STATUS_CANCEL
) {
3111 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3112 free_nfs_cb_data(data
);
3117 if (res
->status
!= NFS3_OK
) {
3118 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
));
3119 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3120 free_nfs_cb_data(data
);
3124 svfs
.f_bsize
= 4096;
3125 svfs
.f_frsize
= 4096;
3126 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3127 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3128 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3129 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3130 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3131 #if !defined(ANDROID)
3132 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3135 svfs
.f_namemax
= 256;
3138 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3139 free_nfs_cb_data(data
);
3142 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3146 args
.fsroot
= data
->fh
;
3147 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3148 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3149 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3150 free_nfs_cb_data(data
);
3156 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3158 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3159 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3172 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3175 struct nfs_cb_data
*data
= private_data
;
3176 struct nfs_context
*nfs
= data
->nfs
;
3178 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3180 if (status
== RPC_STATUS_ERROR
) {
3181 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3182 free_nfs_cb_data(data
);
3185 if (status
== RPC_STATUS_CANCEL
) {
3186 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3187 free_nfs_cb_data(data
);
3192 if (res
->status
!= NFS3_OK
) {
3193 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
));
3194 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3195 free_nfs_cb_data(data
);
3200 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3201 free_nfs_cb_data(data
);
3204 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3208 args
.symlink
= data
->fh
;
3210 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3211 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3212 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3213 free_nfs_cb_data(data
);
3219 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3221 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3222 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3235 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3237 struct nfs_cb_data
*data
= private_data
;
3238 struct nfs_context
*nfs
= data
->nfs
;
3241 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3243 if (status
== RPC_STATUS_ERROR
) {
3244 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3245 free_nfs_cb_data(data
);
3248 if (status
== RPC_STATUS_CANCEL
) {
3249 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3250 free_nfs_cb_data(data
);
3255 if (res
->status
!= NFS3_OK
) {
3256 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3257 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3258 free_nfs_cb_data(data
);
3262 data
->cb(0, nfs
, NULL
, data
->private_data
);
3263 free_nfs_cb_data(data
);
3266 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3270 memset(&args
, 0, sizeof(SETATTR3args
));
3271 args
.object
= data
->fh
;
3272 args
.new_attributes
.mode
.set_it
= 1;
3273 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3275 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3276 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3277 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3278 free_nfs_cb_data(data
);
3285 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3287 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3288 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3298 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3300 struct nfs_cb_data
*data
;
3302 data
= malloc(sizeof(struct nfs_cb_data
));
3304 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3307 memset(data
, 0, sizeof(struct nfs_cb_data
));
3310 data
->private_data
= private_data
;
3311 data
->continue_int
= mode
;
3312 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3313 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3314 if (data
->fh
.data
.data_val
== NULL
) {
3315 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3316 free_nfs_cb_data(data
);
3319 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3321 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3322 free_nfs_cb_data(data
);
3334 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3336 struct nfs_cb_data
*data
= private_data
;
3337 struct nfs_context
*nfs
= data
->nfs
;
3340 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3342 if (status
== RPC_STATUS_ERROR
) {
3343 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3344 free_nfs_cb_data(data
);
3347 if (status
== RPC_STATUS_CANCEL
) {
3348 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3349 free_nfs_cb_data(data
);
3354 if (res
->status
!= NFS3_OK
) {
3355 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3356 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3357 free_nfs_cb_data(data
);
3361 data
->cb(0, nfs
, NULL
, data
->private_data
);
3362 free_nfs_cb_data(data
);
3365 struct nfs_chown_data
{
3370 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3373 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3375 memset(&args
, 0, sizeof(SETATTR3args
));
3376 args
.object
= data
->fh
;
3377 if (chown_data
->uid
!= (uid_t
)-1) {
3378 args
.new_attributes
.uid
.set_it
= 1;
3379 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3381 if (chown_data
->gid
!= (gid_t
)-1) {
3382 args
.new_attributes
.gid
.set_it
= 1;
3383 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3386 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3387 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3388 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3389 free_nfs_cb_data(data
);
3396 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3398 struct nfs_chown_data
*chown_data
;
3400 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3401 if (chown_data
== NULL
) {
3402 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3406 chown_data
->uid
= uid
;
3407 chown_data
->gid
= gid
;
3409 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3410 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3421 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3423 struct nfs_cb_data
*data
;
3424 struct nfs_chown_data
*chown_data
;
3426 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3427 if (chown_data
== NULL
) {
3428 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3432 chown_data
->uid
= uid
;
3433 chown_data
->gid
= gid
;
3436 data
= malloc(sizeof(struct nfs_cb_data
));
3438 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3441 memset(data
, 0, sizeof(struct nfs_cb_data
));
3444 data
->private_data
= private_data
;
3445 data
->continue_data
= chown_data
;
3446 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3447 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3448 if (data
->fh
.data
.data_val
== NULL
) {
3449 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3450 free_nfs_cb_data(data
);
3453 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3456 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3457 free_nfs_cb_data(data
);
3471 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3473 struct nfs_cb_data
*data
= private_data
;
3474 struct nfs_context
*nfs
= data
->nfs
;
3477 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3479 if (status
== RPC_STATUS_ERROR
) {
3480 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3481 free_nfs_cb_data(data
);
3484 if (status
== RPC_STATUS_CANCEL
) {
3485 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3486 free_nfs_cb_data(data
);
3491 if (res
->status
!= NFS3_OK
) {
3492 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3493 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3494 free_nfs_cb_data(data
);
3498 data
->cb(0, nfs
, NULL
, data
->private_data
);
3499 free_nfs_cb_data(data
);
3502 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3505 struct timeval
*utimes_data
= data
->continue_data
;
3507 memset(&args
, 0, sizeof(SETATTR3args
));
3508 args
.object
= data
->fh
;
3509 if (utimes_data
!= NULL
) {
3510 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3511 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3512 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3513 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3514 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3515 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3517 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3518 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3521 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3522 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3523 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3524 free_nfs_cb_data(data
);
3531 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3533 struct timeval
*new_times
= NULL
;
3535 if (times
!= NULL
) {
3536 new_times
= malloc(sizeof(struct timeval
)*2);
3537 if (new_times
== NULL
) {
3538 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3542 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3545 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3546 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3556 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3558 struct timeval
*new_times
= NULL
;
3560 if (times
!= NULL
) {
3561 new_times
= malloc(sizeof(struct timeval
)*2);
3562 if (new_times
== NULL
) {
3563 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3567 new_times
[0].tv_sec
= times
->actime
;
3568 new_times
[0].tv_usec
= 0;
3569 new_times
[1].tv_sec
= times
->modtime
;
3570 new_times
[1].tv_usec
= 0;
3573 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3574 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3585 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3588 struct nfs_cb_data
*data
= private_data
;
3589 struct nfs_context
*nfs
= data
->nfs
;
3590 unsigned int nfsmode
= 0;
3592 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3594 if (status
== RPC_STATUS_ERROR
) {
3595 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3596 free_nfs_cb_data(data
);
3599 if (status
== RPC_STATUS_CANCEL
) {
3600 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3601 free_nfs_cb_data(data
);
3606 if (res
->status
!= NFS3_OK
) {
3607 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
));
3608 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3609 free_nfs_cb_data(data
);
3613 if (data
->continue_int
& R_OK
) {
3614 nfsmode
|= ACCESS3_READ
;
3616 if (data
->continue_int
& W_OK
) {
3617 nfsmode
|= ACCESS3_MODIFY
;
3619 if (data
->continue_int
& X_OK
) {
3620 nfsmode
|= ACCESS3_EXECUTE
;
3623 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3624 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3625 nfsmode
&ACCESS3_READ
?'r':'-',
3626 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3627 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3628 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3629 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3630 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3631 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3632 free_nfs_cb_data(data
);
3636 data
->cb(0, nfs
, NULL
, data
->private_data
);
3637 free_nfs_cb_data(data
);
3640 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3645 if (data
->continue_int
& R_OK
) {
3646 nfsmode
|= ACCESS3_READ
;
3648 if (data
->continue_int
& W_OK
) {
3649 nfsmode
|= ACCESS3_MODIFY
;
3651 if (data
->continue_int
& X_OK
) {
3652 nfsmode
|= ACCESS3_EXECUTE
;
3655 memset(&args
, 0, sizeof(ACCESS3args
));
3656 args
.object
= data
->fh
;
3657 args
.access
= nfsmode
;
3659 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3660 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3661 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3662 free_nfs_cb_data(data
);
3668 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3670 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3671 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3683 struct nfs_symlink_data
{
3685 char *newpathparent
;
3686 char *newpathobject
;
3689 static void free_nfs_symlink_data(void *mem
)
3691 struct nfs_symlink_data
*data
= mem
;
3693 if (data
->oldpath
!= NULL
) {
3694 free(data
->oldpath
);
3696 if (data
->newpathparent
!= NULL
) {
3697 free(data
->newpathparent
);
3699 if (data
->newpathobject
!= NULL
) {
3700 free(data
->newpathobject
);
3705 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3708 struct nfs_cb_data
*data
= private_data
;
3709 struct nfs_context
*nfs
= data
->nfs
;
3710 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3712 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3714 if (status
== RPC_STATUS_ERROR
) {
3715 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3716 free_nfs_cb_data(data
);
3719 if (status
== RPC_STATUS_CANCEL
) {
3720 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3721 free_nfs_cb_data(data
);
3726 if (res
->status
!= NFS3_OK
) {
3727 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
));
3728 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3729 free_nfs_cb_data(data
);
3733 data
->cb(0, nfs
, NULL
, data
->private_data
);
3734 free_nfs_cb_data(data
);
3737 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3739 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3742 memset(&args
, 0, sizeof(SYMLINK3args
));
3743 args
.where
.dir
= data
->fh
;
3744 args
.where
.name
= symlink_data
->newpathobject
;
3745 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3746 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
;
3747 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3749 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3750 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3751 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3752 free_nfs_cb_data(data
);
3758 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3761 struct nfs_symlink_data
*symlink_data
;
3763 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3764 if (symlink_data
== NULL
) {
3765 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3768 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3770 symlink_data
->oldpath
= strdup(oldpath
);
3771 if (symlink_data
->oldpath
== NULL
) {
3772 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3773 free_nfs_symlink_data(symlink_data
);
3777 symlink_data
->newpathparent
= strdup(newpath
);
3778 if (symlink_data
->newpathparent
== NULL
) {
3779 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3780 free_nfs_symlink_data(symlink_data
);
3784 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3786 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3787 free_nfs_symlink_data(symlink_data
);
3793 symlink_data
->newpathobject
= strdup(ptr
);
3794 if (symlink_data
->newpathobject
== NULL
) {
3795 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3796 free_nfs_symlink_data(symlink_data
);
3800 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3801 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3813 struct nfs_rename_data
{
3816 struct nfs_fh3 olddir
;
3819 struct nfs_fh3 newdir
;
3822 static void free_nfs_rename_data(void *mem
)
3824 struct nfs_rename_data
*data
= mem
;
3826 if (data
->oldpath
!= NULL
) {
3827 free(data
->oldpath
);
3829 if (data
->olddir
.data
.data_val
!= NULL
) {
3830 free(data
->olddir
.data
.data_val
);
3832 if (data
->newpath
!= NULL
) {
3833 free(data
->newpath
);
3835 if (data
->newdir
.data
.data_val
!= NULL
) {
3836 free(data
->newdir
.data
.data_val
);
3841 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3844 struct nfs_cb_data
*data
= private_data
;
3845 struct nfs_context
*nfs
= data
->nfs
;
3846 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3848 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3850 if (status
== RPC_STATUS_ERROR
) {
3851 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3852 free_nfs_cb_data(data
);
3855 if (status
== RPC_STATUS_CANCEL
) {
3856 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3857 free_nfs_cb_data(data
);
3862 if (res
->status
!= NFS3_OK
) {
3863 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
));
3864 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3865 free_nfs_cb_data(data
);
3869 data
->cb(0, nfs
, NULL
, data
->private_data
);
3870 free_nfs_cb_data(data
);
3873 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3875 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3878 /* steal the filehandle */
3879 rename_data
->newdir
= data
->fh
;
3880 data
->fh
.data
.data_val
= NULL
;
3882 args
.from
.dir
= rename_data
->olddir
;
3883 args
.from
.name
= rename_data
->oldobject
;
3884 args
.to
.dir
= rename_data
->newdir
;
3885 args
.to
.name
= rename_data
->newobject
;
3886 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3887 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3888 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3889 free_nfs_cb_data(data
);
3896 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3898 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3900 /* steal the filehandle */
3901 rename_data
->olddir
= data
->fh
;
3902 data
->fh
.data
.data_val
= NULL
;
3904 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) {
3905 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3906 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3907 free_nfs_cb_data(data
);
3910 data
->continue_data
= NULL
;
3911 free_nfs_cb_data(data
);
3917 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3920 struct nfs_rename_data
*rename_data
;
3922 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3923 if (rename_data
== NULL
) {
3924 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3927 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3929 rename_data
->oldpath
= strdup(oldpath
);
3930 if (rename_data
->oldpath
== NULL
) {
3931 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3932 free_nfs_rename_data(rename_data
);
3935 ptr
= strrchr(rename_data
->oldpath
, '/');
3937 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3938 free_nfs_rename_data(rename_data
);
3943 rename_data
->oldobject
= ptr
;
3946 rename_data
->newpath
= strdup(newpath
);
3947 if (rename_data
->newpath
== NULL
) {
3948 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3949 free_nfs_rename_data(rename_data
);
3952 ptr
= strrchr(rename_data
->newpath
, '/');
3954 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3955 free_nfs_rename_data(rename_data
);
3960 rename_data
->newobject
= ptr
;
3963 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3964 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3975 struct nfs_link_data
{
3977 struct nfs_fh3 oldfh
;
3980 struct nfs_fh3 newdir
;
3983 static void free_nfs_link_data(void *mem
)
3985 struct nfs_link_data
*data
= mem
;
3987 if (data
->oldpath
!= NULL
) {
3988 free(data
->oldpath
);
3990 if (data
->oldfh
.data
.data_val
!= NULL
) {
3991 free(data
->oldfh
.data
.data_val
);
3993 if (data
->newpath
!= NULL
) {
3994 free(data
->newpath
);
3996 if (data
->newdir
.data
.data_val
!= NULL
) {
3997 free(data
->newdir
.data
.data_val
);
4002 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4005 struct nfs_cb_data
*data
= private_data
;
4006 struct nfs_context
*nfs
= data
->nfs
;
4007 struct nfs_link_data
*link_data
= data
->continue_data
;
4009 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4011 if (status
== RPC_STATUS_ERROR
) {
4012 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4013 free_nfs_cb_data(data
);
4016 if (status
== RPC_STATUS_CANCEL
) {
4017 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4018 free_nfs_cb_data(data
);
4023 if (res
->status
!= NFS3_OK
) {
4024 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
));
4025 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4026 free_nfs_cb_data(data
);
4030 data
->cb(0, nfs
, NULL
, data
->private_data
);
4031 free_nfs_cb_data(data
);
4034 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4036 struct nfs_link_data
*link_data
= data
->continue_data
;
4039 /* steal the filehandle */
4040 link_data
->newdir
= data
->fh
;
4041 data
->fh
.data
.data_val
= NULL
;
4043 memset(&args
, 0, sizeof(LINK3args
));
4044 args
.file
= link_data
->oldfh
;
4045 args
.link
.dir
= link_data
->newdir
;
4046 args
.link
.name
= link_data
->newobject
;
4047 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4048 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4049 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4050 free_nfs_cb_data(data
);
4057 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4059 struct nfs_link_data
*link_data
= data
->continue_data
;
4061 /* steal the filehandle */
4062 link_data
->oldfh
= data
->fh
;
4063 data
->fh
.data
.data_val
= NULL
;
4065 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) {
4066 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4067 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4068 free_nfs_cb_data(data
);
4071 data
->continue_data
= NULL
;
4072 free_nfs_cb_data(data
);
4078 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4081 struct nfs_link_data
*link_data
;
4083 link_data
= malloc(sizeof(struct nfs_link_data
));
4084 if (link_data
== NULL
) {
4085 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4088 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4090 link_data
->oldpath
= strdup(oldpath
);
4091 if (link_data
->oldpath
== NULL
) {
4092 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4093 free_nfs_link_data(link_data
);
4097 link_data
->newpath
= strdup(newpath
);
4098 if (link_data
->newpath
== NULL
) {
4099 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4100 free_nfs_link_data(link_data
);
4103 ptr
= strrchr(link_data
->newpath
, '/');
4105 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4106 free_nfs_link_data(link_data
);
4111 link_data
->newobject
= ptr
;
4114 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4115 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4123 //qqq replace later with lseek()
4124 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4126 return nfsfh
->offset
;
4132 * Get the maximum supported READ3 size by the server
4134 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4136 return nfs
->readmax
;
4140 * Get the maximum supported WRITE3 size by the server
4142 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4144 return nfs
->writemax
;
4147 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4148 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4151 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4152 rpc_set_uid(nfs
->rpc
, uid
);
4155 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4156 rpc_set_gid(nfs
->rpc
, gid
);
4159 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4164 va_start(ap
, error_string
);
4166 vsnprintf(str
, 1024, error_string
, ap
);
4167 if (nfs
->rpc
->error_string
!= NULL
) {
4168 free(nfs
->rpc
->error_string
);
4170 nfs
->rpc
->error_string
= str
;
4176 struct mount_cb_data
{
4182 static void free_mount_cb_data(struct mount_cb_data
*data
)
4184 if (data
->server
!= NULL
) {
4186 data
->server
= NULL
;
4192 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4194 struct mount_cb_data
*data
= private_data
;
4196 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4198 if (status
== RPC_STATUS_ERROR
) {
4199 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4200 free_mount_cb_data(data
);
4203 if (status
== RPC_STATUS_CANCEL
) {
4204 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4205 free_mount_cb_data(data
);
4209 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4210 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4211 rpc_set_error(rpc
, "Failed to disconnect\n");
4213 free_mount_cb_data(data
);
4216 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4218 struct mount_cb_data
*data
= private_data
;
4220 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4222 /* Dont want any more callbacks even if the socket is closed */
4223 rpc
->connect_cb
= NULL
;
4225 if (status
== RPC_STATUS_ERROR
) {
4226 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4227 free_mount_cb_data(data
);
4230 if (status
== RPC_STATUS_CANCEL
) {
4231 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4232 free_mount_cb_data(data
);
4236 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4237 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4238 free_mount_cb_data(data
);
4243 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4245 struct mount_cb_data
*data
= private_data
;
4246 uint32_t mount_port
;
4248 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4250 if (status
== RPC_STATUS_ERROR
) {
4251 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4252 free_mount_cb_data(data
);
4255 if (status
== RPC_STATUS_CANCEL
) {
4256 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4257 free_mount_cb_data(data
);
4261 mount_port
= *(uint32_t *)command_data
;
4262 if (mount_port
== 0) {
4263 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4264 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4265 free_mount_cb_data(data
);
4269 rpc_disconnect(rpc
, "normal disconnect");
4270 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4271 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4272 free_mount_cb_data(data
);
4277 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4279 struct mount_cb_data
*data
= private_data
;
4281 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4283 if (status
== RPC_STATUS_ERROR
) {
4284 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4285 free_mount_cb_data(data
);
4288 if (status
== RPC_STATUS_CANCEL
) {
4289 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4290 free_mount_cb_data(data
);
4294 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4295 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4296 free_mount_cb_data(data
);
4301 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4303 struct mount_cb_data
*data
= private_data
;
4305 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4307 /* Dont want any more callbacks even if the socket is closed */
4308 rpc
->connect_cb
= NULL
;
4310 if (status
== RPC_STATUS_ERROR
) {
4311 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4312 free_mount_cb_data(data
);
4315 if (status
== RPC_STATUS_CANCEL
) {
4316 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4317 free_mount_cb_data(data
);
4321 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4322 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4323 free_mount_cb_data(data
);
4328 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4330 struct mount_cb_data
*data
;
4332 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4334 data
= malloc(sizeof(struct mount_cb_data
));
4338 memset(data
, 0, sizeof(struct mount_cb_data
));
4340 data
->private_data
= private_data
;
4341 data
->server
= strdup(server
);
4342 if (data
->server
== NULL
) {
4343 free_mount_cb_data(data
);
4346 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4347 free_mount_cb_data(data
);
4354 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4356 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4360 const char *nfs_get_server(struct nfs_context
*nfs
) {
4364 const char *nfs_get_export(struct nfs_context
*nfs
) {
4368 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4369 return &nfs
->rootfh
;
4372 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {