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
);
2379 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2380 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2381 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2394 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2397 struct nfs_cb_data
*data
= private_data
;
2398 struct nfs_context
*nfs
= data
->nfs
;
2399 char *str
= data
->continue_data
;
2401 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2403 str
= &str
[strlen(str
) + 1];
2405 if (status
== RPC_STATUS_ERROR
) {
2406 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2407 free_nfs_cb_data(data
);
2410 if (status
== RPC_STATUS_CANCEL
) {
2411 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2412 free_nfs_cb_data(data
);
2417 if (res
->status
!= NFS3_OK
) {
2418 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
));
2419 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2420 free_nfs_cb_data(data
);
2424 data
->cb(0, nfs
, NULL
, data
->private_data
);
2425 free_nfs_cb_data(data
);
2428 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2430 char *str
= data
->continue_data
;
2431 struct REMOVE3args args
;
2433 str
= &str
[strlen(str
) + 1];
2435 args
.object
.dir
= data
->fh
;
2436 args
.object
.name
= str
;
2437 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2438 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2439 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2440 free_nfs_cb_data(data
);
2446 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2451 new_path
= strdup(path
);
2452 if (new_path
== NULL
) {
2453 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2457 ptr
= strrchr(new_path
, '/');
2459 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2464 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2465 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2466 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2477 struct mknod_cb_data
{
2484 static void free_mknod_cb_data(void *ptr
)
2486 struct mknod_cb_data
*data
= ptr
;
2492 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2495 struct nfs_cb_data
*data
= private_data
;
2496 struct nfs_context
*nfs
= data
->nfs
;
2497 char *str
= data
->continue_data
;
2499 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2501 str
= &str
[strlen(str
) + 1];
2503 if (status
== RPC_STATUS_ERROR
) {
2504 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2505 free_nfs_cb_data(data
);
2508 if (status
== RPC_STATUS_CANCEL
) {
2509 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2510 free_nfs_cb_data(data
);
2515 if (res
->status
!= NFS3_OK
) {
2516 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
));
2517 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2518 free_nfs_cb_data(data
);
2522 data
->cb(0, nfs
, NULL
, data
->private_data
);
2523 free_nfs_cb_data(data
);
2526 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2528 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2529 char *str
= cb_data
->path
;
2532 str
= &str
[strlen(str
) + 1];
2534 args
.where
.dir
= data
->fh
;
2535 args
.where
.name
= str
;
2536 switch (cb_data
->mode
& S_IFMT
) {
2538 args
.what
.type
= NF3CHR
;
2539 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2540 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
);
2541 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2542 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2545 args
.what
.type
= NF3BLK
;
2546 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2547 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
);
2548 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2549 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2551 args
.what
.type
= NF3SOCK
;
2552 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2553 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
);
2556 args
.what
.type
= NF3FIFO
;
2557 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2558 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
);
2561 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2562 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2563 free_nfs_cb_data(data
);
2567 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2568 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2569 free_nfs_cb_data(data
);
2575 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2578 struct mknod_cb_data
*cb_data
;
2580 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2581 if (cb_data
== NULL
) {
2582 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2586 cb_data
->path
= strdup(path
);
2587 if (cb_data
->path
== NULL
) {
2588 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2593 ptr
= strrchr(cb_data
->path
, '/');
2595 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2596 free_mknod_cb_data(cb_data
);
2601 cb_data
->mode
= mode
;
2602 cb_data
->major
= major(dev
);
2603 cb_data
->minor
= minor(dev
);
2605 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2606 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2607 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2608 free_mknod_cb_data(cb_data
);
2619 /* ReadDirPlus Emulation Callback data */
2620 struct rdpe_cb_data
{
2623 struct nfs_cb_data
*data
;
2626 /* ReadDirPlus Emulation LOOKUP Callback data */
2627 struct rdpe_lookup_cb_data
{
2628 struct rdpe_cb_data
*rdpe_cb_data
;
2629 struct nfsdirent
*nfsdirent
;
2632 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2633 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2635 LOOKUP3res
*res
= command_data
;
2636 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2637 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2638 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2639 struct nfsdir
*nfsdir
= data
->continue_data
;
2640 struct nfs_context
*nfs
= data
->nfs
;
2641 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2643 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2645 free(rdpe_lookup_cb_data
);
2647 rdpe_cb_data
->getattrcount
--;
2649 if (status
== RPC_STATUS_ERROR
) {
2650 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2652 if (status
== RPC_STATUS_CANCEL
) {
2653 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2655 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2656 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2658 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2659 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2660 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2662 nfsdirent
->type
= attributes
->type
;
2663 nfsdirent
->mode
= attributes
->mode
;
2664 nfsdirent
->size
= attributes
->size
;
2666 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2667 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2668 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2669 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2670 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2671 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2672 nfsdirent
->uid
= attributes
->uid
;
2673 nfsdirent
->gid
= attributes
->gid
;
2677 if (rdpe_cb_data
->getattrcount
== 0) {
2678 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2679 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2680 nfs_free_nfsdir(nfsdir
);
2682 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2686 data
->continue_data
= NULL
;
2687 free_nfs_cb_data(data
);
2691 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2693 READDIR3res
*res
= command_data
;
2694 struct nfs_cb_data
*data
= private_data
;
2695 struct nfs_context
*nfs
= data
->nfs
;
2696 struct nfsdir
*nfsdir
= data
->continue_data
;
2697 struct nfsdirent
*nfsdirent
;
2698 struct entry3
*entry
;
2699 uint64_t cookie
= 0;
2700 struct rdpe_cb_data
*rdpe_cb_data
;
2702 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2704 if (status
== RPC_STATUS_ERROR
) {
2705 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2706 nfs_free_nfsdir(nfsdir
);
2707 data
->continue_data
= NULL
;
2708 free_nfs_cb_data(data
);
2712 if (status
== RPC_STATUS_CANCEL
) {
2713 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2714 nfs_free_nfsdir(nfsdir
);
2715 data
->continue_data
= NULL
;
2716 free_nfs_cb_data(data
);
2720 if (res
->status
!= NFS3_OK
) {
2721 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
));
2722 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2723 nfs_free_nfsdir(nfsdir
);
2724 data
->continue_data
= NULL
;
2725 free_nfs_cb_data(data
);
2729 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2730 while (entry
!= NULL
) {
2731 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2732 if (nfsdirent
== NULL
) {
2733 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2734 nfs_free_nfsdir(nfsdir
);
2735 data
->continue_data
= NULL
;
2736 free_nfs_cb_data(data
);
2739 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2740 nfsdirent
->name
= strdup(entry
->name
);
2741 if (nfsdirent
->name
== NULL
) {
2742 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2743 nfs_free_nfsdir(nfsdir
);
2744 data
->continue_data
= NULL
;
2745 free_nfs_cb_data(data
);
2748 nfsdirent
->inode
= entry
->fileid
;
2750 nfsdirent
->next
= nfsdir
->entries
;
2751 nfsdir
->entries
= nfsdirent
;
2753 cookie
= entry
->cookie
;
2754 entry
= entry
->nextentry
;
2757 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2760 args
.dir
= data
->fh
;
2761 args
.cookie
= cookie
;
2762 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2765 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2766 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2767 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2768 nfs_free_nfsdir(nfsdir
);
2769 data
->continue_data
= NULL
;
2770 free_nfs_cb_data(data
);
2776 /* steal the dirhandle */
2777 nfsdir
->current
= nfsdir
->entries
;
2779 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2780 rdpe_cb_data
->getattrcount
= 0;
2781 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2782 rdpe_cb_data
->data
= data
;
2783 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2784 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2787 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2788 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2789 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2791 memset(&args
, 0, sizeof(LOOKUP3args
));
2792 args
.what
.dir
= data
->fh
;
2793 args
.what
.name
= nfsdirent
->name
;
2795 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2796 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2798 /* if we have already commands in flight, we cant just stop, we have to wait for the
2799 * commands in flight to complete
2801 if (rdpe_cb_data
->getattrcount
> 0) {
2802 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2803 free(rdpe_lookup_cb_data
);
2807 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2808 nfs_free_nfsdir(nfsdir
);
2809 data
->continue_data
= NULL
;
2810 free_nfs_cb_data(data
);
2811 free(rdpe_lookup_cb_data
);
2815 rdpe_cb_data
->getattrcount
++;
2820 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2822 READDIRPLUS3res
*res
= command_data
;
2823 struct nfs_cb_data
*data
= private_data
;
2824 struct nfs_context
*nfs
= data
->nfs
;
2825 struct nfsdir
*nfsdir
= data
->continue_data
;
2826 struct entryplus3
*entry
;
2827 uint64_t cookie
= 0;
2829 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2831 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2834 args
.dir
= data
->fh
;
2835 args
.cookie
= cookie
;
2836 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2839 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2840 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2841 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2842 nfs_free_nfsdir(nfsdir
);
2843 data
->continue_data
= NULL
;
2844 free_nfs_cb_data(data
);
2850 if (status
== RPC_STATUS_CANCEL
) {
2851 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2852 nfs_free_nfsdir(nfsdir
);
2853 data
->continue_data
= NULL
;
2854 free_nfs_cb_data(data
);
2858 if (res
->status
!= NFS3_OK
) {
2859 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
));
2860 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2861 nfs_free_nfsdir(nfsdir
);
2862 data
->continue_data
= NULL
;
2863 free_nfs_cb_data(data
);
2867 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2868 while (entry
!= NULL
) {
2869 struct nfsdirent
*nfsdirent
;
2871 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2872 if (nfsdirent
== NULL
) {
2873 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2874 nfs_free_nfsdir(nfsdir
);
2875 data
->continue_data
= NULL
;
2876 free_nfs_cb_data(data
);
2879 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2880 nfsdirent
->name
= strdup(entry
->name
);
2881 if (nfsdirent
->name
== NULL
) {
2882 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2884 nfs_free_nfsdir(nfsdir
);
2885 data
->continue_data
= NULL
;
2886 free_nfs_cb_data(data
);
2889 nfsdirent
->inode
= entry
->fileid
;
2890 if (entry
->name_attributes
.attributes_follow
) {
2891 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2892 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2893 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2895 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2896 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2897 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2898 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2899 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2900 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2901 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2902 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2905 nfsdirent
->next
= nfsdir
->entries
;
2906 nfsdir
->entries
= nfsdirent
;
2908 cookie
= entry
->cookie
;
2909 entry
= entry
->nextentry
;
2912 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2913 READDIRPLUS3args args
;
2915 args
.dir
= data
->fh
;
2916 args
.cookie
= cookie
;
2917 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2918 args
.dircount
= 8192;
2919 args
.maxcount
= 8192;
2921 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2922 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2923 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2924 nfs_free_nfsdir(nfsdir
);
2925 data
->continue_data
= NULL
;
2926 free_nfs_cb_data(data
);
2932 /* steal the dirhandle */
2933 data
->continue_data
= NULL
;
2934 nfsdir
->current
= nfsdir
->entries
;
2936 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2937 free_nfs_cb_data(data
);
2940 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2942 READDIRPLUS3args args
;
2944 args
.dir
= data
->fh
;
2946 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2947 args
.dircount
= 8192;
2948 args
.maxcount
= 8192;
2949 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2950 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2951 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2952 free_nfs_cb_data(data
);
2958 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2960 struct nfsdir
*nfsdir
;
2962 nfsdir
= malloc(sizeof(struct nfsdir
));
2963 if (nfsdir
== NULL
) {
2964 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2967 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2969 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2970 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2978 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2980 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2982 if (nfsdir
->current
!= NULL
) {
2983 nfsdir
->current
= nfsdir
->current
->next
;
2992 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2994 nfs_free_nfsdir(nfsdir
);
3001 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3012 struct lseek_cb_data
{
3013 struct nfs_context
*nfs
;
3014 struct nfsfh
*nfsfh
;
3020 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3023 struct lseek_cb_data
*data
= private_data
;
3024 struct nfs_context
*nfs
= data
->nfs
;
3026 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3028 if (status
== RPC_STATUS_ERROR
) {
3029 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3033 if (status
== RPC_STATUS_CANCEL
) {
3034 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3040 if (res
->status
!= NFS3_OK
) {
3041 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3042 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3047 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3048 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3052 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3054 struct lseek_cb_data
*data
;
3055 struct GETATTR3args args
;
3057 if (whence
== SEEK_SET
) {
3058 nfsfh
->offset
= offset
;
3059 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3062 if (whence
== SEEK_CUR
) {
3063 nfsfh
->offset
+= offset
;
3064 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3068 data
= malloc(sizeof(struct lseek_cb_data
));
3070 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3075 data
->nfsfh
= nfsfh
;
3076 data
->offset
= offset
;
3078 data
->private_data
= private_data
;
3080 memset(&args
, 0, sizeof(GETATTR3args
));
3081 args
.object
= nfsfh
->fh
;
3083 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3084 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3097 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3100 struct nfs_cb_data
*data
= private_data
;
3101 struct nfs_context
*nfs
= data
->nfs
;
3102 struct statvfs svfs
;
3104 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3106 if (status
== RPC_STATUS_ERROR
) {
3107 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3108 free_nfs_cb_data(data
);
3111 if (status
== RPC_STATUS_CANCEL
) {
3112 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3113 free_nfs_cb_data(data
);
3118 if (res
->status
!= NFS3_OK
) {
3119 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
));
3120 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3121 free_nfs_cb_data(data
);
3125 svfs
.f_bsize
= 4096;
3126 svfs
.f_frsize
= 4096;
3127 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3128 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3129 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3130 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3131 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3132 #if !defined(ANDROID)
3133 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3136 svfs
.f_namemax
= 256;
3139 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3140 free_nfs_cb_data(data
);
3143 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3147 args
.fsroot
= data
->fh
;
3148 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3149 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3150 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3151 free_nfs_cb_data(data
);
3157 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3159 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3160 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3173 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3176 struct nfs_cb_data
*data
= private_data
;
3177 struct nfs_context
*nfs
= data
->nfs
;
3179 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3181 if (status
== RPC_STATUS_ERROR
) {
3182 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3183 free_nfs_cb_data(data
);
3186 if (status
== RPC_STATUS_CANCEL
) {
3187 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3188 free_nfs_cb_data(data
);
3193 if (res
->status
!= NFS3_OK
) {
3194 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
));
3195 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3196 free_nfs_cb_data(data
);
3201 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3202 free_nfs_cb_data(data
);
3205 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3209 args
.symlink
= data
->fh
;
3211 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3212 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3213 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3214 free_nfs_cb_data(data
);
3220 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3222 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3223 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3236 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3238 struct nfs_cb_data
*data
= private_data
;
3239 struct nfs_context
*nfs
= data
->nfs
;
3242 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3244 if (status
== RPC_STATUS_ERROR
) {
3245 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3246 free_nfs_cb_data(data
);
3249 if (status
== RPC_STATUS_CANCEL
) {
3250 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3251 free_nfs_cb_data(data
);
3256 if (res
->status
!= NFS3_OK
) {
3257 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3258 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3259 free_nfs_cb_data(data
);
3263 data
->cb(0, nfs
, NULL
, data
->private_data
);
3264 free_nfs_cb_data(data
);
3267 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3271 memset(&args
, 0, sizeof(SETATTR3args
));
3272 args
.object
= data
->fh
;
3273 args
.new_attributes
.mode
.set_it
= 1;
3274 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3276 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3277 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3278 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3279 free_nfs_cb_data(data
);
3286 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3288 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3289 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3299 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3301 struct nfs_cb_data
*data
;
3303 data
= malloc(sizeof(struct nfs_cb_data
));
3305 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3308 memset(data
, 0, sizeof(struct nfs_cb_data
));
3311 data
->private_data
= private_data
;
3312 data
->continue_int
= mode
;
3313 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3314 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3315 if (data
->fh
.data
.data_val
== NULL
) {
3316 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3317 free_nfs_cb_data(data
);
3320 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3322 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3323 free_nfs_cb_data(data
);
3335 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3337 struct nfs_cb_data
*data
= private_data
;
3338 struct nfs_context
*nfs
= data
->nfs
;
3341 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3343 if (status
== RPC_STATUS_ERROR
) {
3344 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3345 free_nfs_cb_data(data
);
3348 if (status
== RPC_STATUS_CANCEL
) {
3349 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3350 free_nfs_cb_data(data
);
3355 if (res
->status
!= NFS3_OK
) {
3356 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3357 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3358 free_nfs_cb_data(data
);
3362 data
->cb(0, nfs
, NULL
, data
->private_data
);
3363 free_nfs_cb_data(data
);
3366 struct nfs_chown_data
{
3371 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3374 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3376 memset(&args
, 0, sizeof(SETATTR3args
));
3377 args
.object
= data
->fh
;
3378 if (chown_data
->uid
!= (uid_t
)-1) {
3379 args
.new_attributes
.uid
.set_it
= 1;
3380 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3382 if (chown_data
->gid
!= (gid_t
)-1) {
3383 args
.new_attributes
.gid
.set_it
= 1;
3384 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3387 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3388 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3389 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3390 free_nfs_cb_data(data
);
3397 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3399 struct nfs_chown_data
*chown_data
;
3401 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3402 if (chown_data
== NULL
) {
3403 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3407 chown_data
->uid
= uid
;
3408 chown_data
->gid
= gid
;
3410 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3411 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3422 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3424 struct nfs_cb_data
*data
;
3425 struct nfs_chown_data
*chown_data
;
3427 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3428 if (chown_data
== NULL
) {
3429 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3433 chown_data
->uid
= uid
;
3434 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");
3442 memset(data
, 0, sizeof(struct nfs_cb_data
));
3445 data
->private_data
= private_data
;
3446 data
->continue_data
= chown_data
;
3447 data
->free_continue_data
= free
;
3448 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3449 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3450 if (data
->fh
.data
.data_val
== NULL
) {
3451 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3452 free_nfs_cb_data(data
);
3455 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3457 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3458 free_nfs_cb_data(data
);
3472 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3474 struct nfs_cb_data
*data
= private_data
;
3475 struct nfs_context
*nfs
= data
->nfs
;
3478 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3480 if (status
== RPC_STATUS_ERROR
) {
3481 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3482 free_nfs_cb_data(data
);
3485 if (status
== RPC_STATUS_CANCEL
) {
3486 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3487 free_nfs_cb_data(data
);
3492 if (res
->status
!= NFS3_OK
) {
3493 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3494 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3495 free_nfs_cb_data(data
);
3499 data
->cb(0, nfs
, NULL
, data
->private_data
);
3500 free_nfs_cb_data(data
);
3503 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3506 struct timeval
*utimes_data
= data
->continue_data
;
3508 memset(&args
, 0, sizeof(SETATTR3args
));
3509 args
.object
= data
->fh
;
3510 if (utimes_data
!= NULL
) {
3511 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3512 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3513 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3514 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3515 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3516 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3518 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3519 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3522 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3523 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3524 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3525 free_nfs_cb_data(data
);
3532 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3534 struct timeval
*new_times
= NULL
;
3536 if (times
!= NULL
) {
3537 new_times
= malloc(sizeof(struct timeval
)*2);
3538 if (new_times
== NULL
) {
3539 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3543 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3546 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3547 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3557 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3559 struct timeval
*new_times
= NULL
;
3561 if (times
!= NULL
) {
3562 new_times
= malloc(sizeof(struct timeval
)*2);
3563 if (new_times
== NULL
) {
3564 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3568 new_times
[0].tv_sec
= times
->actime
;
3569 new_times
[0].tv_usec
= 0;
3570 new_times
[1].tv_sec
= times
->modtime
;
3571 new_times
[1].tv_usec
= 0;
3574 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3575 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3586 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3589 struct nfs_cb_data
*data
= private_data
;
3590 struct nfs_context
*nfs
= data
->nfs
;
3591 unsigned int nfsmode
= 0;
3593 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3595 if (status
== RPC_STATUS_ERROR
) {
3596 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3597 free_nfs_cb_data(data
);
3600 if (status
== RPC_STATUS_CANCEL
) {
3601 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3602 free_nfs_cb_data(data
);
3607 if (res
->status
!= NFS3_OK
) {
3608 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
));
3609 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3610 free_nfs_cb_data(data
);
3614 if (data
->continue_int
& R_OK
) {
3615 nfsmode
|= ACCESS3_READ
;
3617 if (data
->continue_int
& W_OK
) {
3618 nfsmode
|= ACCESS3_MODIFY
;
3620 if (data
->continue_int
& X_OK
) {
3621 nfsmode
|= ACCESS3_EXECUTE
;
3624 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3625 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3626 nfsmode
&ACCESS3_READ
?'r':'-',
3627 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3628 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3629 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3630 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3631 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3632 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3633 free_nfs_cb_data(data
);
3637 data
->cb(0, nfs
, NULL
, data
->private_data
);
3638 free_nfs_cb_data(data
);
3641 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3646 if (data
->continue_int
& R_OK
) {
3647 nfsmode
|= ACCESS3_READ
;
3649 if (data
->continue_int
& W_OK
) {
3650 nfsmode
|= ACCESS3_MODIFY
;
3652 if (data
->continue_int
& X_OK
) {
3653 nfsmode
|= ACCESS3_EXECUTE
;
3656 memset(&args
, 0, sizeof(ACCESS3args
));
3657 args
.object
= data
->fh
;
3658 args
.access
= nfsmode
;
3660 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3661 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3662 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3663 free_nfs_cb_data(data
);
3669 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3671 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3672 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3684 struct nfs_symlink_data
{
3686 char *newpathparent
;
3687 char *newpathobject
;
3690 static void free_nfs_symlink_data(void *mem
)
3692 struct nfs_symlink_data
*data
= mem
;
3694 if (data
->oldpath
!= NULL
) {
3695 free(data
->oldpath
);
3697 if (data
->newpathparent
!= NULL
) {
3698 free(data
->newpathparent
);
3700 if (data
->newpathobject
!= NULL
) {
3701 free(data
->newpathobject
);
3706 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3709 struct nfs_cb_data
*data
= private_data
;
3710 struct nfs_context
*nfs
= data
->nfs
;
3711 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3713 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3715 if (status
== RPC_STATUS_ERROR
) {
3716 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3717 free_nfs_cb_data(data
);
3720 if (status
== RPC_STATUS_CANCEL
) {
3721 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3722 free_nfs_cb_data(data
);
3727 if (res
->status
!= NFS3_OK
) {
3728 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
));
3729 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3730 free_nfs_cb_data(data
);
3734 data
->cb(0, nfs
, NULL
, data
->private_data
);
3735 free_nfs_cb_data(data
);
3738 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3740 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3743 memset(&args
, 0, sizeof(SYMLINK3args
));
3744 args
.where
.dir
= data
->fh
;
3745 args
.where
.name
= symlink_data
->newpathobject
;
3746 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3747 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
;
3748 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3750 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3751 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3752 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3753 free_nfs_cb_data(data
);
3759 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3762 struct nfs_symlink_data
*symlink_data
;
3764 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3765 if (symlink_data
== NULL
) {
3766 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3769 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3771 symlink_data
->oldpath
= strdup(oldpath
);
3772 if (symlink_data
->oldpath
== NULL
) {
3773 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3774 free_nfs_symlink_data(symlink_data
);
3778 symlink_data
->newpathparent
= strdup(newpath
);
3779 if (symlink_data
->newpathparent
== NULL
) {
3780 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3781 free_nfs_symlink_data(symlink_data
);
3785 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3787 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3788 free_nfs_symlink_data(symlink_data
);
3794 symlink_data
->newpathobject
= strdup(ptr
);
3795 if (symlink_data
->newpathobject
== NULL
) {
3796 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3797 free_nfs_symlink_data(symlink_data
);
3801 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3802 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3814 struct nfs_rename_data
{
3817 struct nfs_fh3 olddir
;
3820 struct nfs_fh3 newdir
;
3823 static void free_nfs_rename_data(void *mem
)
3825 struct nfs_rename_data
*data
= mem
;
3827 if (data
->oldpath
!= NULL
) {
3828 free(data
->oldpath
);
3830 if (data
->olddir
.data
.data_val
!= NULL
) {
3831 free(data
->olddir
.data
.data_val
);
3833 if (data
->newpath
!= NULL
) {
3834 free(data
->newpath
);
3836 if (data
->newdir
.data
.data_val
!= NULL
) {
3837 free(data
->newdir
.data
.data_val
);
3842 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3845 struct nfs_cb_data
*data
= private_data
;
3846 struct nfs_context
*nfs
= data
->nfs
;
3847 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3849 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3851 if (status
== RPC_STATUS_ERROR
) {
3852 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3853 free_nfs_cb_data(data
);
3856 if (status
== RPC_STATUS_CANCEL
) {
3857 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3858 free_nfs_cb_data(data
);
3863 if (res
->status
!= NFS3_OK
) {
3864 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
));
3865 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3866 free_nfs_cb_data(data
);
3870 data
->cb(0, nfs
, NULL
, data
->private_data
);
3871 free_nfs_cb_data(data
);
3874 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3876 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3879 /* steal the filehandle */
3880 rename_data
->newdir
= data
->fh
;
3881 data
->fh
.data
.data_val
= NULL
;
3883 args
.from
.dir
= rename_data
->olddir
;
3884 args
.from
.name
= rename_data
->oldobject
;
3885 args
.to
.dir
= rename_data
->newdir
;
3886 args
.to
.name
= rename_data
->newobject
;
3887 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3888 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3889 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3890 free_nfs_cb_data(data
);
3897 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3899 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3901 /* steal the filehandle */
3902 rename_data
->olddir
= data
->fh
;
3903 data
->fh
.data
.data_val
= NULL
;
3905 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) {
3906 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3907 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3908 free_nfs_cb_data(data
);
3911 data
->continue_data
= NULL
;
3912 free_nfs_cb_data(data
);
3918 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3921 struct nfs_rename_data
*rename_data
;
3923 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3924 if (rename_data
== NULL
) {
3925 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3928 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3930 rename_data
->oldpath
= strdup(oldpath
);
3931 if (rename_data
->oldpath
== NULL
) {
3932 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3933 free_nfs_rename_data(rename_data
);
3936 ptr
= strrchr(rename_data
->oldpath
, '/');
3938 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3939 free_nfs_rename_data(rename_data
);
3944 rename_data
->oldobject
= ptr
;
3947 rename_data
->newpath
= strdup(newpath
);
3948 if (rename_data
->newpath
== NULL
) {
3949 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3950 free_nfs_rename_data(rename_data
);
3953 ptr
= strrchr(rename_data
->newpath
, '/');
3955 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3956 free_nfs_rename_data(rename_data
);
3961 rename_data
->newobject
= ptr
;
3964 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3965 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3976 struct nfs_link_data
{
3978 struct nfs_fh3 oldfh
;
3981 struct nfs_fh3 newdir
;
3984 static void free_nfs_link_data(void *mem
)
3986 struct nfs_link_data
*data
= mem
;
3988 if (data
->oldpath
!= NULL
) {
3989 free(data
->oldpath
);
3991 if (data
->oldfh
.data
.data_val
!= NULL
) {
3992 free(data
->oldfh
.data
.data_val
);
3994 if (data
->newpath
!= NULL
) {
3995 free(data
->newpath
);
3997 if (data
->newdir
.data
.data_val
!= NULL
) {
3998 free(data
->newdir
.data
.data_val
);
4003 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4006 struct nfs_cb_data
*data
= private_data
;
4007 struct nfs_context
*nfs
= data
->nfs
;
4008 struct nfs_link_data
*link_data
= data
->continue_data
;
4010 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4012 if (status
== RPC_STATUS_ERROR
) {
4013 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4014 free_nfs_cb_data(data
);
4017 if (status
== RPC_STATUS_CANCEL
) {
4018 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4019 free_nfs_cb_data(data
);
4024 if (res
->status
!= NFS3_OK
) {
4025 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
));
4026 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4027 free_nfs_cb_data(data
);
4031 data
->cb(0, nfs
, NULL
, data
->private_data
);
4032 free_nfs_cb_data(data
);
4035 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4037 struct nfs_link_data
*link_data
= data
->continue_data
;
4040 /* steal the filehandle */
4041 link_data
->newdir
= data
->fh
;
4042 data
->fh
.data
.data_val
= NULL
;
4044 memset(&args
, 0, sizeof(LINK3args
));
4045 args
.file
= link_data
->oldfh
;
4046 args
.link
.dir
= link_data
->newdir
;
4047 args
.link
.name
= link_data
->newobject
;
4048 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4049 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4050 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4051 free_nfs_cb_data(data
);
4058 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4060 struct nfs_link_data
*link_data
= data
->continue_data
;
4062 /* steal the filehandle */
4063 link_data
->oldfh
= data
->fh
;
4064 data
->fh
.data
.data_val
= NULL
;
4066 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) {
4067 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4068 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4069 free_nfs_cb_data(data
);
4072 data
->continue_data
= NULL
;
4073 free_nfs_cb_data(data
);
4079 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4082 struct nfs_link_data
*link_data
;
4084 link_data
= malloc(sizeof(struct nfs_link_data
));
4085 if (link_data
== NULL
) {
4086 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4089 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4091 link_data
->oldpath
= strdup(oldpath
);
4092 if (link_data
->oldpath
== NULL
) {
4093 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4094 free_nfs_link_data(link_data
);
4098 link_data
->newpath
= strdup(newpath
);
4099 if (link_data
->newpath
== NULL
) {
4100 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4101 free_nfs_link_data(link_data
);
4104 ptr
= strrchr(link_data
->newpath
, '/');
4106 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4107 free_nfs_link_data(link_data
);
4112 link_data
->newobject
= ptr
;
4115 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4116 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4124 //qqq replace later with lseek()
4125 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4127 return nfsfh
->offset
;
4133 * Get the maximum supported READ3 size by the server
4135 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4137 return nfs
->readmax
;
4141 * Get the maximum supported WRITE3 size by the server
4143 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4145 return nfs
->writemax
;
4148 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4149 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4152 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4153 rpc_set_uid(nfs
->rpc
, uid
);
4156 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4157 rpc_set_gid(nfs
->rpc
, gid
);
4160 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4165 va_start(ap
, error_string
);
4167 vsnprintf(str
, 1024, error_string
, ap
);
4168 if (nfs
->rpc
->error_string
!= NULL
) {
4169 free(nfs
->rpc
->error_string
);
4171 nfs
->rpc
->error_string
= str
;
4177 struct mount_cb_data
{
4183 static void free_mount_cb_data(struct mount_cb_data
*data
)
4185 if (data
->server
!= NULL
) {
4187 data
->server
= NULL
;
4193 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4195 struct mount_cb_data
*data
= private_data
;
4197 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4199 if (status
== RPC_STATUS_ERROR
) {
4200 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4201 free_mount_cb_data(data
);
4204 if (status
== RPC_STATUS_CANCEL
) {
4205 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4206 free_mount_cb_data(data
);
4210 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4211 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4212 rpc_set_error(rpc
, "Failed to disconnect\n");
4214 free_mount_cb_data(data
);
4217 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4219 struct mount_cb_data
*data
= private_data
;
4221 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4223 /* Dont want any more callbacks even if the socket is closed */
4224 rpc
->connect_cb
= NULL
;
4226 if (status
== RPC_STATUS_ERROR
) {
4227 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4228 free_mount_cb_data(data
);
4231 if (status
== RPC_STATUS_CANCEL
) {
4232 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4233 free_mount_cb_data(data
);
4237 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4238 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4239 free_mount_cb_data(data
);
4244 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4246 struct mount_cb_data
*data
= private_data
;
4247 uint32_t mount_port
;
4249 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4251 if (status
== RPC_STATUS_ERROR
) {
4252 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4253 free_mount_cb_data(data
);
4256 if (status
== RPC_STATUS_CANCEL
) {
4257 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4258 free_mount_cb_data(data
);
4262 mount_port
= *(uint32_t *)command_data
;
4263 if (mount_port
== 0) {
4264 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4265 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4266 free_mount_cb_data(data
);
4270 rpc_disconnect(rpc
, "normal disconnect");
4271 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4272 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4273 free_mount_cb_data(data
);
4278 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4280 struct mount_cb_data
*data
= private_data
;
4282 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4284 if (status
== RPC_STATUS_ERROR
) {
4285 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4286 free_mount_cb_data(data
);
4289 if (status
== RPC_STATUS_CANCEL
) {
4290 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4291 free_mount_cb_data(data
);
4295 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4296 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4297 free_mount_cb_data(data
);
4302 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4304 struct mount_cb_data
*data
= private_data
;
4306 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4308 /* Dont want any more callbacks even if the socket is closed */
4309 rpc
->connect_cb
= NULL
;
4311 if (status
== RPC_STATUS_ERROR
) {
4312 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4313 free_mount_cb_data(data
);
4316 if (status
== RPC_STATUS_CANCEL
) {
4317 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4318 free_mount_cb_data(data
);
4322 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4323 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4324 free_mount_cb_data(data
);
4329 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4331 struct mount_cb_data
*data
;
4333 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4335 data
= malloc(sizeof(struct mount_cb_data
));
4339 memset(data
, 0, sizeof(struct mount_cb_data
));
4341 data
->private_data
= private_data
;
4342 data
->server
= strdup(server
);
4343 if (data
->server
== NULL
) {
4344 free_mount_cb_data(data
);
4347 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4348 free_mount_cb_data(data
);
4355 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4357 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4361 const char *nfs_get_server(struct nfs_context
*nfs
) {
4365 const char *nfs_get_export(struct nfs_context
*nfs
) {
4369 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4370 return &nfs
->rootfh
;
4373 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {