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");
1110 memset(data
, 0, sizeof(struct nfs_cb_data
));
1113 data
->continue_cb
= continue_cb
;
1114 data
->continue_data
= continue_data
;
1115 data
->free_continue_data
= free_continue_data
;
1116 data
->continue_int
= continue_int
;
1117 data
->private_data
= private_data
;
1118 if (path
[0] == '/') {
1119 data
->saved_path
= strdup(path
);
1121 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1122 if (data
->saved_path
== NULL
) {
1123 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1124 "malloc path string");
1125 free_nfs_cb_data(data
);
1128 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1131 if (data
->saved_path
== NULL
) {
1132 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1133 free_nfs_cb_data(data
);
1136 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1137 free_nfs_cb_data(data
);
1141 data
->path
= data
->saved_path
;
1143 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1144 /* return 0 here since the callback will be invoked if there is a failure */
1157 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1160 struct nfs_cb_data
*data
= private_data
;
1161 struct nfs_context
*nfs
= data
->nfs
;
1168 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1170 if (status
== RPC_STATUS_ERROR
) {
1171 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1172 free_nfs_cb_data(data
);
1175 if (status
== RPC_STATUS_CANCEL
) {
1176 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1177 free_nfs_cb_data(data
);
1182 if (res
->status
!= NFS3_OK
) {
1183 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
));
1184 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1185 free_nfs_cb_data(data
);
1190 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1191 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1192 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1193 st
.st_mode
|= S_IFDIR
;
1195 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1196 st
.st_mode
|= S_IFREG
;
1198 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1199 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1200 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1202 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1204 st
.st_blksize
= 4096;
1205 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1207 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1208 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1209 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1211 data
->cb(0, nfs
, &st
, data
->private_data
);
1212 free_nfs_cb_data(data
);
1215 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1217 struct GETATTR3args args
;
1219 memset(&args
, 0, sizeof(GETATTR3args
));
1220 args
.object
= data
->fh
;
1222 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1223 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1224 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1225 free_nfs_cb_data(data
);
1231 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1233 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1234 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1246 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1248 struct nfs_cb_data
*data
= private_data
;
1249 struct nfs_context
*nfs
= data
->nfs
;
1250 struct nfsfh
*nfsfh
;
1253 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1255 if (status
== RPC_STATUS_ERROR
) {
1256 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1257 free_nfs_cb_data(data
);
1260 if (status
== RPC_STATUS_CANCEL
) {
1261 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1262 free_nfs_cb_data(data
);
1267 if (res
->status
!= NFS3_OK
) {
1268 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1269 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1270 free_nfs_cb_data(data
);
1274 nfsfh
= malloc(sizeof(struct nfsfh
));
1275 if (nfsfh
== NULL
) {
1276 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1277 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1278 free_nfs_cb_data(data
);
1281 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1283 if (data
->continue_int
& O_SYNC
) {
1287 /* steal the filehandle */
1288 nfsfh
->fh
= data
->fh
;
1289 data
->fh
.data
.data_val
= NULL
;
1291 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1292 free_nfs_cb_data(data
);
1295 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1298 struct nfs_cb_data
*data
= private_data
;
1299 struct nfs_context
*nfs
= data
->nfs
;
1300 struct nfsfh
*nfsfh
;
1301 unsigned int nfsmode
= 0;
1303 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1305 if (status
== RPC_STATUS_ERROR
) {
1306 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1307 free_nfs_cb_data(data
);
1310 if (status
== RPC_STATUS_CANCEL
) {
1311 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1312 free_nfs_cb_data(data
);
1317 if (res
->status
!= NFS3_OK
) {
1318 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
));
1319 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1320 free_nfs_cb_data(data
);
1324 if (data
->continue_int
& O_WRONLY
) {
1325 nfsmode
|= ACCESS3_MODIFY
;
1327 if (data
->continue_int
& O_RDWR
) {
1328 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1330 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1331 nfsmode
|= ACCESS3_READ
;
1335 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1336 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1337 nfsmode
&ACCESS3_READ
?'r':'-',
1338 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1339 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1340 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1341 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1342 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1343 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1344 free_nfs_cb_data(data
);
1348 /* Try to truncate it if we were requested to */
1349 if ((data
->continue_int
& O_TRUNC
) &&
1350 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1353 memset(&args
, 0, sizeof(SETATTR3args
));
1354 args
.object
= data
->fh
;
1355 args
.new_attributes
.size
.set_it
= 1;
1356 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1358 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1360 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1361 "SETATTR call for %s", data
->path
);
1362 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1363 data
->private_data
);
1364 free_nfs_cb_data(data
);
1370 nfsfh
= malloc(sizeof(struct nfsfh
));
1371 if (nfsfh
== NULL
) {
1372 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1373 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1374 free_nfs_cb_data(data
);
1377 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1379 if (data
->continue_int
& O_SYNC
) {
1383 /* steal the filehandle */
1384 nfsfh
->fh
= data
->fh
;
1385 data
->fh
.data
.data_val
= NULL
;
1387 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1388 free_nfs_cb_data(data
);
1391 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1396 if (data
->continue_int
& O_WRONLY
) {
1397 nfsmode
|= ACCESS3_MODIFY
;
1399 if (data
->continue_int
& O_RDWR
) {
1400 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1402 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1403 nfsmode
|= ACCESS3_READ
;
1406 memset(&args
, 0, sizeof(ACCESS3args
));
1407 args
.object
= data
->fh
;
1408 args
.access
= nfsmode
;
1410 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1411 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1412 "call for %s", data
->path
);
1413 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1414 data
->private_data
);
1415 free_nfs_cb_data(data
);
1421 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1423 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1424 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1435 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1437 /* steal saved_path */
1439 nfs
->cwd
= data
->saved_path
;
1440 data
->saved_path
= NULL
;
1442 data
->cb(0, nfs
, NULL
, data
->private_data
);
1443 free_nfs_cb_data(data
);
1448 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1450 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1451 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1462 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1464 struct nfs_cb_data
*data
= private_data
;
1465 struct nfs_context
*nfs
= data
->nfs
;
1468 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1470 if (status
== RPC_STATUS_ERROR
) {
1471 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1472 free_nfs_cb_data(data
);
1475 if (status
== RPC_STATUS_CANCEL
) {
1476 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1477 free_nfs_cb_data(data
);
1482 if (res
->status
!= NFS3_OK
) {
1483 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1484 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1485 free_nfs_cb_data(data
);
1489 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1490 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1491 free_nfs_cb_data(data
);
1494 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1496 struct nfs_mcb_data
*mdata
= private_data
;
1497 struct nfs_cb_data
*data
= mdata
->data
;
1498 struct nfs_context
*nfs
= data
->nfs
;
1501 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1505 if (status
== RPC_STATUS_ERROR
) {
1506 /* flag the failure but do not invoke callback until we have received all responses */
1509 if (status
== RPC_STATUS_CANCEL
) {
1510 /* flag the cancellation but do not invoke callback until we have received all responses */
1514 /* reassemble the data into the buffer */
1515 if (status
== RPC_STATUS_SUCCESS
) {
1517 if (res
->status
!= NFS3_OK
) {
1518 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1521 if (res
->READ3res_u
.resok
.count
> 0) {
1522 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1523 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1524 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1530 if (data
->num_calls
> 0) {
1531 /* still waiting for more replies */
1536 if (data
->error
!= 0) {
1537 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1538 free_nfs_cb_data(data
);
1542 if (data
->cancel
!= 0) {
1543 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1544 free_nfs_cb_data(data
);
1549 data
->nfsfh
->offset
= data
->max_offset
;
1550 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1552 free_nfs_cb_data(data
);
1556 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1558 struct nfs_cb_data
*data
;
1560 data
= malloc(sizeof(struct nfs_cb_data
));
1562 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1565 memset(data
, 0, sizeof(struct nfs_cb_data
));
1568 data
->private_data
= private_data
;
1569 data
->nfsfh
= nfsfh
;
1571 nfsfh
->offset
= offset
;
1573 if (count
<= nfs_get_readmax(nfs
)) {
1576 memset(&args
, 0, sizeof(READ3args
));
1577 args
.file
= nfsfh
->fh
;
1578 args
.offset
= offset
;
1581 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_cb
, &args
, data
) != 0) {
1582 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1583 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1584 free_nfs_cb_data(data
);
1590 /* trying to read more than maximum server read size, we has to chop it up into smaller
1591 * reads and collect into a reassembly buffer.
1592 * we send all reads in parallel so that performance is still good.
1594 data
->max_offset
= offset
;
1595 data
->start_offset
= offset
;
1597 data
->buffer
= malloc(count
);
1598 if (data
->buffer
== NULL
) {
1599 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1600 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1601 free_nfs_cb_data(data
);
1606 uint64_t readcount
= count
;
1607 struct nfs_mcb_data
*mdata
;
1610 if (readcount
> nfs_get_readmax(nfs
)) {
1611 readcount
= nfs_get_readmax(nfs
);
1614 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1615 if (mdata
== NULL
) {
1616 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1619 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1621 mdata
->offset
= offset
;
1622 mdata
->count
= readcount
;
1624 memset(&args
, 0, sizeof(READ3args
));
1625 args
.file
= nfsfh
->fh
;
1626 args
.offset
= offset
;
1627 args
.count
= readcount
;
1629 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1630 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1631 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1637 offset
+= readcount
;
1647 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1649 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1657 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1659 struct nfs_cb_data
*data
= private_data
;
1660 struct nfs_context
*nfs
= data
->nfs
;
1663 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1665 if (status
== RPC_STATUS_ERROR
) {
1666 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1667 free_nfs_cb_data(data
);
1670 if (status
== RPC_STATUS_CANCEL
) {
1671 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1672 free_nfs_cb_data(data
);
1677 if (res
->status
!= NFS3_OK
) {
1678 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1679 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1680 free_nfs_cb_data(data
);
1684 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1685 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1686 free_nfs_cb_data(data
);
1689 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1691 struct nfs_mcb_data
*mdata
= private_data
;
1692 struct nfs_cb_data
*data
= mdata
->data
;
1693 struct nfs_context
*nfs
= data
->nfs
;
1696 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1700 if (status
== RPC_STATUS_ERROR
) {
1701 /* flag the failure but do not invoke callback until we have received all responses */
1704 if (status
== RPC_STATUS_CANCEL
) {
1705 /* flag the cancellation but do not invoke callback until we have received all responses */
1709 if (status
== RPC_STATUS_SUCCESS
) {
1711 if (res
->status
!= NFS3_OK
) {
1712 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1715 if (res
->WRITE3res_u
.resok
.count
> 0) {
1716 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1717 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1723 if (data
->num_calls
> 0) {
1724 /* still waiting for more replies */
1729 if (data
->error
!= 0) {
1730 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1731 free_nfs_cb_data(data
);
1735 if (data
->cancel
!= 0) {
1736 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1737 free_nfs_cb_data(data
);
1742 data
->nfsfh
->offset
= data
->max_offset
;
1743 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1745 free_nfs_cb_data(data
);
1750 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
)
1752 struct nfs_cb_data
*data
;
1754 data
= malloc(sizeof(struct nfs_cb_data
));
1756 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1759 memset(data
, 0, sizeof(struct nfs_cb_data
));
1762 data
->private_data
= private_data
;
1763 data
->nfsfh
= nfsfh
;
1765 nfsfh
->offset
= offset
;
1767 if (count
<= nfs_get_writemax(nfs
)) {
1770 memset(&args
, 0, sizeof(WRITE3args
));
1771 args
.file
= nfsfh
->fh
;
1772 args
.offset
= offset
;
1774 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1775 args
.data
.data_len
= count
;
1776 args
.data
.data_val
= buf
;
1778 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_cb
, &args
, data
) != 0) {
1779 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1780 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1781 free_nfs_cb_data(data
);
1787 /* trying to write more than maximum server write size, we has to chop it up into smaller
1789 * we send all writes in parallel so that performance is still good.
1791 data
->max_offset
= offset
;
1792 data
->start_offset
= offset
;
1795 uint64_t writecount
= count
;
1796 struct nfs_mcb_data
*mdata
;
1799 if (writecount
> nfs_get_writemax(nfs
)) {
1800 writecount
= nfs_get_writemax(nfs
);
1803 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1804 if (mdata
== NULL
) {
1805 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1808 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1810 mdata
->offset
= offset
;
1811 mdata
->count
= writecount
;
1813 memset(&args
, 0, sizeof(WRITE3args
));
1814 args
.file
= nfsfh
->fh
;
1815 args
.offset
= offset
;
1816 args
.count
= writecount
;
1817 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1818 args
.data
.data_len
= writecount
;
1819 args
.data
.data_val
= &buf
[offset
- data
->start_offset
];
1821 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1822 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1823 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1828 count
-= writecount
;
1829 offset
+= writecount
;
1839 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1841 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1851 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1853 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1854 free(nfsfh
->fh
.data
.data_val
);
1855 nfsfh
->fh
.data
.data_val
= NULL
;
1859 cb(0, nfs
, NULL
, private_data
);
1870 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1872 struct nfs_cb_data
*data
;
1873 struct GETATTR3args args
;
1875 data
= malloc(sizeof(struct nfs_cb_data
));
1877 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1880 memset(data
, 0, sizeof(struct nfs_cb_data
));
1883 data
->private_data
= private_data
;
1885 memset(&args
, 0, sizeof(GETATTR3args
));
1886 args
.object
= nfsfh
->fh
;
1888 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1889 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1890 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1891 free_nfs_cb_data(data
);
1902 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1904 struct nfs_cb_data
*data
= private_data
;
1905 struct nfs_context
*nfs
= data
->nfs
;
1908 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1910 if (status
== RPC_STATUS_ERROR
) {
1911 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1912 free_nfs_cb_data(data
);
1915 if (status
== RPC_STATUS_CANCEL
) {
1916 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1917 free_nfs_cb_data(data
);
1922 if (res
->status
!= NFS3_OK
) {
1923 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1924 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1925 free_nfs_cb_data(data
);
1929 data
->cb(0, nfs
, NULL
, data
->private_data
);
1930 free_nfs_cb_data(data
);
1933 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1935 struct nfs_cb_data
*data
;
1936 struct COMMIT3args args
;
1938 data
= malloc(sizeof(struct nfs_cb_data
));
1940 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1943 memset(data
, 0, sizeof(struct nfs_cb_data
));
1946 data
->private_data
= private_data
;
1948 args
.file
= nfsfh
->fh
;
1951 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1952 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1953 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1954 free_nfs_cb_data(data
);
1966 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1968 struct nfs_cb_data
*data
= private_data
;
1969 struct nfs_context
*nfs
= data
->nfs
;
1972 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1974 if (status
== RPC_STATUS_ERROR
) {
1975 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1976 free_nfs_cb_data(data
);
1979 if (status
== RPC_STATUS_CANCEL
) {
1980 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1981 free_nfs_cb_data(data
);
1986 if (res
->status
!= NFS3_OK
) {
1987 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1988 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1989 free_nfs_cb_data(data
);
1993 data
->cb(0, nfs
, NULL
, data
->private_data
);
1994 free_nfs_cb_data(data
);
1997 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1999 struct nfs_cb_data
*data
;
2002 data
= malloc(sizeof(struct nfs_cb_data
));
2004 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2007 memset(data
, 0, sizeof(struct nfs_cb_data
));
2010 data
->private_data
= private_data
;
2012 memset(&args
, 0, sizeof(SETATTR3args
));
2013 args
.object
= nfsfh
->fh
;
2014 args
.new_attributes
.size
.set_it
= 1;
2015 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2017 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2018 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2019 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2020 free_nfs_cb_data(data
);
2030 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2032 uint64_t offset
= data
->continue_int
;
2035 nfsfh
.fh
= data
->fh
;
2037 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2038 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2039 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2040 free_nfs_cb_data(data
);
2043 free_nfs_cb_data(data
);
2047 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2053 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2054 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2067 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2070 struct nfs_cb_data
*data
= private_data
;
2071 struct nfs_context
*nfs
= data
->nfs
;
2072 char *str
= data
->continue_data
;
2074 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2076 str
= &str
[strlen(str
) + 1];
2078 if (status
== RPC_STATUS_ERROR
) {
2079 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2080 free_nfs_cb_data(data
);
2083 if (status
== RPC_STATUS_CANCEL
) {
2084 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2085 free_nfs_cb_data(data
);
2090 if (res
->status
!= NFS3_OK
) {
2091 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
));
2092 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2093 free_nfs_cb_data(data
);
2097 data
->cb(0, nfs
, NULL
, data
->private_data
);
2098 free_nfs_cb_data(data
);
2101 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2103 char *str
= data
->continue_data
;
2106 str
= &str
[strlen(str
) + 1];
2108 memset(&args
, 0, sizeof(MKDIR3args
));
2109 args
.where
.dir
= data
->fh
;
2110 args
.where
.name
= str
;
2111 args
.attributes
.mode
.set_it
= 1;
2112 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2114 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2115 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2116 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2117 free_nfs_cb_data(data
);
2123 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2128 new_path
= strdup(path
);
2129 if (new_path
== NULL
) {
2130 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2134 ptr
= strrchr(new_path
, '/');
2136 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2141 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2142 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2143 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2157 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2160 struct nfs_cb_data
*data
= private_data
;
2161 struct nfs_context
*nfs
= data
->nfs
;
2162 char *str
= data
->continue_data
;
2164 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2166 str
= &str
[strlen(str
) + 1];
2168 if (status
== RPC_STATUS_ERROR
) {
2169 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2170 free_nfs_cb_data(data
);
2173 if (status
== RPC_STATUS_CANCEL
) {
2174 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2175 free_nfs_cb_data(data
);
2180 if (res
->status
!= NFS3_OK
) {
2181 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
));
2182 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2183 free_nfs_cb_data(data
);
2187 data
->cb(0, nfs
, NULL
, data
->private_data
);
2188 free_nfs_cb_data(data
);
2191 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2193 char *str
= data
->continue_data
;
2196 str
= &str
[strlen(str
) + 1];
2198 args
.object
.dir
= data
->fh
;
2199 args
.object
.name
= str
;
2200 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2201 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2202 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2203 free_nfs_cb_data(data
);
2209 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2214 new_path
= strdup(path
);
2215 if (new_path
== NULL
) {
2216 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2220 ptr
= strrchr(new_path
, '/');
2222 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2227 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2228 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2229 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2242 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2245 struct nfs_cb_data
*data
= private_data
;
2246 struct nfs_context
*nfs
= data
->nfs
;
2247 struct nfsfh
*nfsfh
;
2248 char *str
= data
->continue_data
;
2250 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2252 if (status
== RPC_STATUS_ERROR
) {
2253 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2254 free_nfs_cb_data(data
);
2257 if (status
== RPC_STATUS_CANCEL
) {
2258 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2259 free_nfs_cb_data(data
);
2263 str
= &str
[strlen(str
) + 1];
2265 if (res
->status
!= NFS3_OK
) {
2266 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
));
2267 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2272 nfsfh
= malloc(sizeof(struct nfsfh
));
2273 if (nfsfh
== NULL
) {
2274 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2275 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2276 free_nfs_cb_data(data
);
2279 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2281 /* copy the filehandle */
2282 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2283 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2284 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2286 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2287 free_nfs_cb_data(data
);
2292 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2295 struct nfs_cb_data
*data
= private_data
;
2296 struct nfs_context
*nfs
= data
->nfs
;
2297 char *str
= data
->continue_data
;
2300 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2302 if (status
== RPC_STATUS_ERROR
) {
2303 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2304 free_nfs_cb_data(data
);
2307 if (status
== RPC_STATUS_CANCEL
) {
2308 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2309 free_nfs_cb_data(data
);
2313 str
= &str
[strlen(str
) + 1];
2315 if (res
->status
!= NFS3_OK
) {
2316 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
));
2317 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2318 free_nfs_cb_data(data
);
2322 memset(&args
, 0, sizeof(LOOKUP3args
));
2323 args
.what
.dir
= data
->fh
;
2324 args
.what
.name
= str
;
2326 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2327 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2328 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2329 free_nfs_cb_data(data
);
2335 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2337 char *str
= data
->continue_data
;
2340 str
= &str
[strlen(str
) + 1];
2342 memset(&args
, 0, sizeof(CREATE3args
));
2343 args
.where
.dir
= data
->fh
;
2344 args
.where
.name
= str
;
2345 args
.how
.mode
= UNCHECKED
;
2346 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2347 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2349 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2350 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2351 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2352 free_nfs_cb_data(data
);
2358 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2363 new_path
= strdup(path
);
2364 if (new_path
== NULL
) {
2365 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2369 ptr
= strrchr(new_path
, '/');
2371 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2376 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2377 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2378 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2391 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2394 struct nfs_cb_data
*data
= private_data
;
2395 struct nfs_context
*nfs
= data
->nfs
;
2396 char *str
= data
->continue_data
;
2398 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2400 str
= &str
[strlen(str
) + 1];
2402 if (status
== RPC_STATUS_ERROR
) {
2403 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2404 free_nfs_cb_data(data
);
2407 if (status
== RPC_STATUS_CANCEL
) {
2408 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2409 free_nfs_cb_data(data
);
2414 if (res
->status
!= NFS3_OK
) {
2415 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
));
2416 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2417 free_nfs_cb_data(data
);
2421 data
->cb(0, nfs
, NULL
, data
->private_data
);
2422 free_nfs_cb_data(data
);
2425 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2427 char *str
= data
->continue_data
;
2428 struct REMOVE3args args
;
2430 str
= &str
[strlen(str
) + 1];
2432 args
.object
.dir
= data
->fh
;
2433 args
.object
.name
= str
;
2434 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2435 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2436 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2437 free_nfs_cb_data(data
);
2443 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2448 new_path
= strdup(path
);
2449 if (new_path
== NULL
) {
2450 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2454 ptr
= strrchr(new_path
, '/');
2456 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2461 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2462 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2463 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2474 struct mknod_cb_data
{
2481 static void free_mknod_cb_data(void *ptr
)
2483 struct mknod_cb_data
*data
= ptr
;
2489 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2492 struct nfs_cb_data
*data
= private_data
;
2493 struct nfs_context
*nfs
= data
->nfs
;
2494 char *str
= data
->continue_data
;
2496 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2498 str
= &str
[strlen(str
) + 1];
2500 if (status
== RPC_STATUS_ERROR
) {
2501 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2502 free_nfs_cb_data(data
);
2505 if (status
== RPC_STATUS_CANCEL
) {
2506 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2507 free_nfs_cb_data(data
);
2512 if (res
->status
!= NFS3_OK
) {
2513 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
));
2514 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2515 free_nfs_cb_data(data
);
2519 data
->cb(0, nfs
, NULL
, data
->private_data
);
2520 free_nfs_cb_data(data
);
2523 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2525 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2526 char *str
= cb_data
->path
;
2529 str
= &str
[strlen(str
) + 1];
2531 args
.where
.dir
= data
->fh
;
2532 args
.where
.name
= str
;
2533 switch (cb_data
->mode
& S_IFMT
) {
2535 args
.what
.type
= NF3CHR
;
2536 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2537 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
);
2538 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2539 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2542 args
.what
.type
= NF3BLK
;
2543 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2544 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
);
2545 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2546 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2548 args
.what
.type
= NF3SOCK
;
2549 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2550 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
);
2553 args
.what
.type
= NF3FIFO
;
2554 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2555 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
);
2558 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2559 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2560 free_nfs_cb_data(data
);
2564 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2565 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2566 free_nfs_cb_data(data
);
2572 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2575 struct mknod_cb_data
*cb_data
;
2577 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2578 if (cb_data
== NULL
) {
2579 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2583 cb_data
->path
= strdup(path
);
2584 if (cb_data
->path
== NULL
) {
2585 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2590 ptr
= strrchr(cb_data
->path
, '/');
2592 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2597 cb_data
->mode
= mode
;
2598 cb_data
->major
= major(dev
);
2599 cb_data
->minor
= minor(dev
);
2601 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2602 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2603 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2604 free_mknod_cb_data(cb_data
);
2615 /* ReadDirPlus Emulation Callback data */
2616 struct rdpe_cb_data
{
2619 struct nfs_cb_data
*data
;
2622 /* ReadDirPlus Emulation LOOKUP Callback data */
2623 struct rdpe_lookup_cb_data
{
2624 struct rdpe_cb_data
*rdpe_cb_data
;
2625 struct nfsdirent
*nfsdirent
;
2628 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2629 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2631 LOOKUP3res
*res
= command_data
;
2632 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2633 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2634 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2635 struct nfsdir
*nfsdir
= data
->continue_data
;
2636 struct nfs_context
*nfs
= data
->nfs
;
2637 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2639 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2641 free(rdpe_lookup_cb_data
);
2643 rdpe_cb_data
->getattrcount
--;
2645 if (status
== RPC_STATUS_ERROR
) {
2646 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2648 if (status
== RPC_STATUS_CANCEL
) {
2649 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2651 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2652 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2654 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2655 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2656 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2658 nfsdirent
->type
= attributes
->type
;
2659 nfsdirent
->mode
= attributes
->mode
;
2660 nfsdirent
->size
= attributes
->size
;
2662 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2663 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2664 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2665 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2666 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2667 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2668 nfsdirent
->uid
= attributes
->uid
;
2669 nfsdirent
->gid
= attributes
->gid
;
2673 if (rdpe_cb_data
->getattrcount
== 0) {
2674 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2675 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2676 nfs_free_nfsdir(nfsdir
);
2678 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2682 data
->continue_data
= NULL
;
2683 free_nfs_cb_data(data
);
2687 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2689 READDIR3res
*res
= command_data
;
2690 struct nfs_cb_data
*data
= private_data
;
2691 struct nfs_context
*nfs
= data
->nfs
;
2692 struct nfsdir
*nfsdir
= data
->continue_data
;
2693 struct nfsdirent
*nfsdirent
;
2694 struct entry3
*entry
;
2695 uint64_t cookie
= 0;
2696 struct rdpe_cb_data
*rdpe_cb_data
;
2698 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2700 if (status
== RPC_STATUS_ERROR
) {
2701 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2702 nfs_free_nfsdir(nfsdir
);
2703 data
->continue_data
= NULL
;
2704 free_nfs_cb_data(data
);
2708 if (status
== RPC_STATUS_CANCEL
) {
2709 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2710 nfs_free_nfsdir(nfsdir
);
2711 data
->continue_data
= NULL
;
2712 free_nfs_cb_data(data
);
2716 if (res
->status
!= NFS3_OK
) {
2717 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
));
2718 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2719 nfs_free_nfsdir(nfsdir
);
2720 data
->continue_data
= NULL
;
2721 free_nfs_cb_data(data
);
2725 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2726 while (entry
!= NULL
) {
2727 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2728 if (nfsdirent
== NULL
) {
2729 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2730 nfs_free_nfsdir(nfsdir
);
2731 data
->continue_data
= NULL
;
2732 free_nfs_cb_data(data
);
2735 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2736 nfsdirent
->name
= strdup(entry
->name
);
2737 if (nfsdirent
->name
== NULL
) {
2738 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2739 nfs_free_nfsdir(nfsdir
);
2740 data
->continue_data
= NULL
;
2741 free_nfs_cb_data(data
);
2744 nfsdirent
->inode
= entry
->fileid
;
2746 nfsdirent
->next
= nfsdir
->entries
;
2747 nfsdir
->entries
= nfsdirent
;
2749 cookie
= entry
->cookie
;
2750 entry
= entry
->nextentry
;
2753 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2756 args
.dir
= data
->fh
;
2757 args
.cookie
= cookie
;
2758 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2761 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2762 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2763 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2764 nfs_free_nfsdir(nfsdir
);
2765 data
->continue_data
= NULL
;
2766 free_nfs_cb_data(data
);
2772 /* steal the dirhandle */
2773 nfsdir
->current
= nfsdir
->entries
;
2775 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2776 rdpe_cb_data
->getattrcount
= 0;
2777 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2778 rdpe_cb_data
->data
= data
;
2779 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2780 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2783 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2784 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2785 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2787 memset(&args
, 0, sizeof(LOOKUP3args
));
2788 args
.what
.dir
= data
->fh
;
2789 args
.what
.name
= nfsdirent
->name
;
2791 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2792 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2794 /* if we have already commands in flight, we cant just stop, we have to wait for the
2795 * commands in flight to complete
2797 if (rdpe_cb_data
->getattrcount
> 0) {
2798 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2799 free(rdpe_lookup_cb_data
);
2803 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2804 nfs_free_nfsdir(nfsdir
);
2805 data
->continue_data
= NULL
;
2806 free_nfs_cb_data(data
);
2807 free(rdpe_lookup_cb_data
);
2811 rdpe_cb_data
->getattrcount
++;
2816 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2818 READDIRPLUS3res
*res
= command_data
;
2819 struct nfs_cb_data
*data
= private_data
;
2820 struct nfs_context
*nfs
= data
->nfs
;
2821 struct nfsdir
*nfsdir
= data
->continue_data
;
2822 struct entryplus3
*entry
;
2823 uint64_t cookie
= 0;
2825 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2827 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2830 args
.dir
= data
->fh
;
2831 args
.cookie
= cookie
;
2832 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2835 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2836 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2837 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2838 nfs_free_nfsdir(nfsdir
);
2839 data
->continue_data
= NULL
;
2840 free_nfs_cb_data(data
);
2846 if (status
== RPC_STATUS_CANCEL
) {
2847 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2848 nfs_free_nfsdir(nfsdir
);
2849 data
->continue_data
= NULL
;
2850 free_nfs_cb_data(data
);
2854 if (res
->status
!= NFS3_OK
) {
2855 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
));
2856 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2857 nfs_free_nfsdir(nfsdir
);
2858 data
->continue_data
= NULL
;
2859 free_nfs_cb_data(data
);
2863 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2864 while (entry
!= NULL
) {
2865 struct nfsdirent
*nfsdirent
;
2867 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2868 if (nfsdirent
== NULL
) {
2869 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2870 nfs_free_nfsdir(nfsdir
);
2871 data
->continue_data
= NULL
;
2872 free_nfs_cb_data(data
);
2875 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2876 nfsdirent
->name
= strdup(entry
->name
);
2877 if (nfsdirent
->name
== NULL
) {
2878 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2879 nfs_free_nfsdir(nfsdir
);
2880 data
->continue_data
= NULL
;
2881 free_nfs_cb_data(data
);
2884 nfsdirent
->inode
= entry
->fileid
;
2885 if (entry
->name_attributes
.attributes_follow
) {
2886 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2887 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2888 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2890 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2891 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2892 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2893 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2894 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2895 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2896 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2897 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2900 nfsdirent
->next
= nfsdir
->entries
;
2901 nfsdir
->entries
= nfsdirent
;
2903 cookie
= entry
->cookie
;
2904 entry
= entry
->nextentry
;
2907 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2908 READDIRPLUS3args args
;
2910 args
.dir
= data
->fh
;
2911 args
.cookie
= cookie
;
2912 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2913 args
.dircount
= 8192;
2914 args
.maxcount
= 8192;
2916 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2917 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2918 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2919 nfs_free_nfsdir(nfsdir
);
2920 data
->continue_data
= NULL
;
2921 free_nfs_cb_data(data
);
2927 /* steal the dirhandle */
2928 data
->continue_data
= NULL
;
2929 nfsdir
->current
= nfsdir
->entries
;
2931 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2932 free_nfs_cb_data(data
);
2935 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2937 READDIRPLUS3args args
;
2939 args
.dir
= data
->fh
;
2941 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2942 args
.dircount
= 8192;
2943 args
.maxcount
= 8192;
2944 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2945 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2946 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2947 free_nfs_cb_data(data
);
2953 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2955 struct nfsdir
*nfsdir
;
2957 nfsdir
= malloc(sizeof(struct nfsdir
));
2958 if (nfsdir
== NULL
) {
2959 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2962 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2964 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2965 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2973 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2975 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2977 if (nfsdir
->current
!= NULL
) {
2978 nfsdir
->current
= nfsdir
->current
->next
;
2987 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2989 nfs_free_nfsdir(nfsdir
);
2996 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3007 struct lseek_cb_data
{
3008 struct nfs_context
*nfs
;
3009 struct nfsfh
*nfsfh
;
3015 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3018 struct lseek_cb_data
*data
= private_data
;
3019 struct nfs_context
*nfs
= data
->nfs
;
3021 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3023 if (status
== RPC_STATUS_ERROR
) {
3024 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3028 if (status
== RPC_STATUS_CANCEL
) {
3029 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3035 if (res
->status
!= NFS3_OK
) {
3036 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3037 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3042 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3043 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3047 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3049 struct lseek_cb_data
*data
;
3050 struct GETATTR3args args
;
3052 if (whence
== SEEK_SET
) {
3053 nfsfh
->offset
= offset
;
3054 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3057 if (whence
== SEEK_CUR
) {
3058 nfsfh
->offset
+= offset
;
3059 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3063 data
= malloc(sizeof(struct lseek_cb_data
));
3065 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3070 data
->nfsfh
= nfsfh
;
3071 data
->offset
= offset
;
3073 data
->private_data
= private_data
;
3075 memset(&args
, 0, sizeof(GETATTR3args
));
3076 args
.object
= nfsfh
->fh
;
3078 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3079 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3092 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3095 struct nfs_cb_data
*data
= private_data
;
3096 struct nfs_context
*nfs
= data
->nfs
;
3097 struct statvfs svfs
;
3099 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3101 if (status
== RPC_STATUS_ERROR
) {
3102 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3103 free_nfs_cb_data(data
);
3106 if (status
== RPC_STATUS_CANCEL
) {
3107 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3108 free_nfs_cb_data(data
);
3113 if (res
->status
!= NFS3_OK
) {
3114 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
));
3115 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3116 free_nfs_cb_data(data
);
3120 svfs
.f_bsize
= 4096;
3121 svfs
.f_frsize
= 4096;
3122 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3123 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3124 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3125 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3126 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3127 #if !defined(ANDROID)
3128 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3131 svfs
.f_namemax
= 256;
3134 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3135 free_nfs_cb_data(data
);
3138 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3142 args
.fsroot
= data
->fh
;
3143 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3144 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3145 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3146 free_nfs_cb_data(data
);
3152 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3154 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3155 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3168 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3171 struct nfs_cb_data
*data
= private_data
;
3172 struct nfs_context
*nfs
= data
->nfs
;
3174 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3176 if (status
== RPC_STATUS_ERROR
) {
3177 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3178 free_nfs_cb_data(data
);
3181 if (status
== RPC_STATUS_CANCEL
) {
3182 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3183 free_nfs_cb_data(data
);
3188 if (res
->status
!= NFS3_OK
) {
3189 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
));
3190 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3191 free_nfs_cb_data(data
);
3196 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3197 free_nfs_cb_data(data
);
3200 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3204 args
.symlink
= data
->fh
;
3206 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3207 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3208 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3209 free_nfs_cb_data(data
);
3215 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3217 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3218 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3231 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3233 struct nfs_cb_data
*data
= private_data
;
3234 struct nfs_context
*nfs
= data
->nfs
;
3237 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3239 if (status
== RPC_STATUS_ERROR
) {
3240 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3241 free_nfs_cb_data(data
);
3244 if (status
== RPC_STATUS_CANCEL
) {
3245 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3246 free_nfs_cb_data(data
);
3251 if (res
->status
!= NFS3_OK
) {
3252 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3253 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3254 free_nfs_cb_data(data
);
3258 data
->cb(0, nfs
, NULL
, data
->private_data
);
3259 free_nfs_cb_data(data
);
3262 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3266 memset(&args
, 0, sizeof(SETATTR3args
));
3267 args
.object
= data
->fh
;
3268 args
.new_attributes
.mode
.set_it
= 1;
3269 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3271 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3272 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3273 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3274 free_nfs_cb_data(data
);
3281 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3283 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3284 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3294 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3296 struct nfs_cb_data
*data
;
3298 data
= malloc(sizeof(struct nfs_cb_data
));
3300 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3303 memset(data
, 0, sizeof(struct nfs_cb_data
));
3306 data
->private_data
= private_data
;
3307 data
->continue_int
= mode
;
3308 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3309 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3310 if (data
->fh
.data
.data_val
== NULL
) {
3311 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3312 free_nfs_cb_data(data
);
3315 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3317 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3318 free_nfs_cb_data(data
);
3330 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3332 struct nfs_cb_data
*data
= private_data
;
3333 struct nfs_context
*nfs
= data
->nfs
;
3336 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3338 if (status
== RPC_STATUS_ERROR
) {
3339 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3340 free_nfs_cb_data(data
);
3343 if (status
== RPC_STATUS_CANCEL
) {
3344 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3345 free_nfs_cb_data(data
);
3350 if (res
->status
!= NFS3_OK
) {
3351 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3352 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3353 free_nfs_cb_data(data
);
3357 data
->cb(0, nfs
, NULL
, data
->private_data
);
3358 free_nfs_cb_data(data
);
3361 struct nfs_chown_data
{
3366 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3369 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3371 memset(&args
, 0, sizeof(SETATTR3args
));
3372 args
.object
= data
->fh
;
3373 if (chown_data
->uid
!= (uid_t
)-1) {
3374 args
.new_attributes
.uid
.set_it
= 1;
3375 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3377 if (chown_data
->gid
!= (gid_t
)-1) {
3378 args
.new_attributes
.gid
.set_it
= 1;
3379 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3382 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3383 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3384 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3385 free_nfs_cb_data(data
);
3392 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3394 struct nfs_chown_data
*chown_data
;
3396 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3397 if (chown_data
== NULL
) {
3398 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3402 chown_data
->uid
= uid
;
3403 chown_data
->gid
= gid
;
3405 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3406 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3417 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3419 struct nfs_cb_data
*data
;
3420 struct nfs_chown_data
*chown_data
;
3422 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3423 if (chown_data
== NULL
) {
3424 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3428 chown_data
->uid
= uid
;
3429 chown_data
->gid
= gid
;
3432 data
= malloc(sizeof(struct nfs_cb_data
));
3434 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3437 memset(data
, 0, sizeof(struct nfs_cb_data
));
3440 data
->private_data
= private_data
;
3441 data
->continue_data
= chown_data
;
3442 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3443 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3444 if (data
->fh
.data
.data_val
== NULL
) {
3445 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3446 free_nfs_cb_data(data
);
3449 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3452 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3453 free_nfs_cb_data(data
);
3467 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3469 struct nfs_cb_data
*data
= private_data
;
3470 struct nfs_context
*nfs
= data
->nfs
;
3473 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3475 if (status
== RPC_STATUS_ERROR
) {
3476 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3477 free_nfs_cb_data(data
);
3480 if (status
== RPC_STATUS_CANCEL
) {
3481 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3482 free_nfs_cb_data(data
);
3487 if (res
->status
!= NFS3_OK
) {
3488 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3489 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3490 free_nfs_cb_data(data
);
3494 data
->cb(0, nfs
, NULL
, data
->private_data
);
3495 free_nfs_cb_data(data
);
3498 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3501 struct timeval
*utimes_data
= data
->continue_data
;
3503 memset(&args
, 0, sizeof(SETATTR3args
));
3504 args
.object
= data
->fh
;
3505 if (utimes_data
!= NULL
) {
3506 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3507 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3508 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3509 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3510 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3511 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3513 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3514 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3517 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3518 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3519 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3520 free_nfs_cb_data(data
);
3527 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3529 struct timeval
*new_times
= NULL
;
3531 if (times
!= NULL
) {
3532 new_times
= malloc(sizeof(struct timeval
)*2);
3533 if (new_times
== NULL
) {
3534 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3538 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3541 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3542 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3552 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3554 struct timeval
*new_times
= NULL
;
3556 if (times
!= NULL
) {
3557 new_times
= malloc(sizeof(struct timeval
)*2);
3558 if (new_times
== NULL
) {
3559 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3563 new_times
[0].tv_sec
= times
->actime
;
3564 new_times
[0].tv_usec
= 0;
3565 new_times
[1].tv_sec
= times
->modtime
;
3566 new_times
[1].tv_usec
= 0;
3569 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3570 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3581 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3584 struct nfs_cb_data
*data
= private_data
;
3585 struct nfs_context
*nfs
= data
->nfs
;
3586 unsigned int nfsmode
= 0;
3588 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3590 if (status
== RPC_STATUS_ERROR
) {
3591 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3592 free_nfs_cb_data(data
);
3595 if (status
== RPC_STATUS_CANCEL
) {
3596 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3597 free_nfs_cb_data(data
);
3602 if (res
->status
!= NFS3_OK
) {
3603 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
));
3604 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3605 free_nfs_cb_data(data
);
3609 if (data
->continue_int
& R_OK
) {
3610 nfsmode
|= ACCESS3_READ
;
3612 if (data
->continue_int
& W_OK
) {
3613 nfsmode
|= ACCESS3_MODIFY
;
3615 if (data
->continue_int
& X_OK
) {
3616 nfsmode
|= ACCESS3_EXECUTE
;
3619 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3620 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3621 nfsmode
&ACCESS3_READ
?'r':'-',
3622 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3623 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3624 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3625 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3626 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3627 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3628 free_nfs_cb_data(data
);
3632 data
->cb(0, nfs
, NULL
, data
->private_data
);
3633 free_nfs_cb_data(data
);
3636 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3641 if (data
->continue_int
& R_OK
) {
3642 nfsmode
|= ACCESS3_READ
;
3644 if (data
->continue_int
& W_OK
) {
3645 nfsmode
|= ACCESS3_MODIFY
;
3647 if (data
->continue_int
& X_OK
) {
3648 nfsmode
|= ACCESS3_EXECUTE
;
3651 memset(&args
, 0, sizeof(ACCESS3args
));
3652 args
.object
= data
->fh
;
3653 args
.access
= nfsmode
;
3655 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3656 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3657 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3658 free_nfs_cb_data(data
);
3664 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3666 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3667 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3679 struct nfs_symlink_data
{
3681 char *newpathparent
;
3682 char *newpathobject
;
3685 static void free_nfs_symlink_data(void *mem
)
3687 struct nfs_symlink_data
*data
= mem
;
3689 if (data
->oldpath
!= NULL
) {
3690 free(data
->oldpath
);
3692 if (data
->newpathparent
!= NULL
) {
3693 free(data
->newpathparent
);
3695 if (data
->newpathobject
!= NULL
) {
3696 free(data
->newpathobject
);
3701 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3704 struct nfs_cb_data
*data
= private_data
;
3705 struct nfs_context
*nfs
= data
->nfs
;
3706 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3708 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3710 if (status
== RPC_STATUS_ERROR
) {
3711 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3712 free_nfs_cb_data(data
);
3715 if (status
== RPC_STATUS_CANCEL
) {
3716 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3717 free_nfs_cb_data(data
);
3722 if (res
->status
!= NFS3_OK
) {
3723 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
));
3724 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3725 free_nfs_cb_data(data
);
3729 data
->cb(0, nfs
, NULL
, data
->private_data
);
3730 free_nfs_cb_data(data
);
3733 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3735 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3738 memset(&args
, 0, sizeof(SYMLINK3args
));
3739 args
.where
.dir
= data
->fh
;
3740 args
.where
.name
= symlink_data
->newpathobject
;
3741 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3742 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
;
3743 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3745 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3746 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3747 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3748 free_nfs_cb_data(data
);
3754 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3757 struct nfs_symlink_data
*symlink_data
;
3759 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3760 if (symlink_data
== NULL
) {
3761 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3764 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3766 symlink_data
->oldpath
= strdup(oldpath
);
3767 if (symlink_data
->oldpath
== NULL
) {
3768 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3769 free_nfs_symlink_data(symlink_data
);
3773 symlink_data
->newpathparent
= strdup(newpath
);
3774 if (symlink_data
->newpathparent
== NULL
) {
3775 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3776 free_nfs_symlink_data(symlink_data
);
3780 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3782 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3783 free_nfs_symlink_data(symlink_data
);
3789 symlink_data
->newpathobject
= strdup(ptr
);
3790 if (symlink_data
->newpathobject
== NULL
) {
3791 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3792 free_nfs_symlink_data(symlink_data
);
3796 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3797 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3809 struct nfs_rename_data
{
3812 struct nfs_fh3 olddir
;
3815 struct nfs_fh3 newdir
;
3818 static void free_nfs_rename_data(void *mem
)
3820 struct nfs_rename_data
*data
= mem
;
3822 if (data
->oldpath
!= NULL
) {
3823 free(data
->oldpath
);
3825 if (data
->olddir
.data
.data_val
!= NULL
) {
3826 free(data
->olddir
.data
.data_val
);
3828 if (data
->newpath
!= NULL
) {
3829 free(data
->newpath
);
3831 if (data
->newdir
.data
.data_val
!= NULL
) {
3832 free(data
->newdir
.data
.data_val
);
3837 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3840 struct nfs_cb_data
*data
= private_data
;
3841 struct nfs_context
*nfs
= data
->nfs
;
3842 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3844 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3846 if (status
== RPC_STATUS_ERROR
) {
3847 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3848 free_nfs_cb_data(data
);
3851 if (status
== RPC_STATUS_CANCEL
) {
3852 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3853 free_nfs_cb_data(data
);
3858 if (res
->status
!= NFS3_OK
) {
3859 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
));
3860 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3861 free_nfs_cb_data(data
);
3865 data
->cb(0, nfs
, NULL
, data
->private_data
);
3866 free_nfs_cb_data(data
);
3869 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3871 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3874 /* steal the filehandle */
3875 rename_data
->newdir
= data
->fh
;
3876 data
->fh
.data
.data_val
= NULL
;
3878 args
.from
.dir
= rename_data
->olddir
;
3879 args
.from
.name
= rename_data
->oldobject
;
3880 args
.to
.dir
= rename_data
->newdir
;
3881 args
.to
.name
= rename_data
->newobject
;
3882 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3883 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3884 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3885 free_nfs_cb_data(data
);
3892 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3894 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3896 /* steal the filehandle */
3897 rename_data
->olddir
= data
->fh
;
3898 data
->fh
.data
.data_val
= NULL
;
3900 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) {
3901 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3902 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3903 free_nfs_cb_data(data
);
3906 data
->continue_data
= NULL
;
3907 free_nfs_cb_data(data
);
3913 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3916 struct nfs_rename_data
*rename_data
;
3918 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3919 if (rename_data
== NULL
) {
3920 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3923 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3925 rename_data
->oldpath
= strdup(oldpath
);
3926 if (rename_data
->oldpath
== NULL
) {
3927 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3928 free_nfs_rename_data(rename_data
);
3931 ptr
= strrchr(rename_data
->oldpath
, '/');
3933 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3934 free_nfs_rename_data(rename_data
);
3939 rename_data
->oldobject
= ptr
;
3942 rename_data
->newpath
= strdup(newpath
);
3943 if (rename_data
->newpath
== NULL
) {
3944 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3945 free_nfs_rename_data(rename_data
);
3948 ptr
= strrchr(rename_data
->newpath
, '/');
3950 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3951 free_nfs_rename_data(rename_data
);
3956 rename_data
->newobject
= ptr
;
3959 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3960 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3971 struct nfs_link_data
{
3973 struct nfs_fh3 oldfh
;
3976 struct nfs_fh3 newdir
;
3979 static void free_nfs_link_data(void *mem
)
3981 struct nfs_link_data
*data
= mem
;
3983 if (data
->oldpath
!= NULL
) {
3984 free(data
->oldpath
);
3986 if (data
->oldfh
.data
.data_val
!= NULL
) {
3987 free(data
->oldfh
.data
.data_val
);
3989 if (data
->newpath
!= NULL
) {
3990 free(data
->newpath
);
3992 if (data
->newdir
.data
.data_val
!= NULL
) {
3993 free(data
->newdir
.data
.data_val
);
3998 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4001 struct nfs_cb_data
*data
= private_data
;
4002 struct nfs_context
*nfs
= data
->nfs
;
4003 struct nfs_link_data
*link_data
= data
->continue_data
;
4005 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4007 if (status
== RPC_STATUS_ERROR
) {
4008 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4009 free_nfs_cb_data(data
);
4012 if (status
== RPC_STATUS_CANCEL
) {
4013 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4014 free_nfs_cb_data(data
);
4019 if (res
->status
!= NFS3_OK
) {
4020 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
));
4021 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4022 free_nfs_cb_data(data
);
4026 data
->cb(0, nfs
, NULL
, data
->private_data
);
4027 free_nfs_cb_data(data
);
4030 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4032 struct nfs_link_data
*link_data
= data
->continue_data
;
4035 /* steal the filehandle */
4036 link_data
->newdir
= data
->fh
;
4037 data
->fh
.data
.data_val
= NULL
;
4039 memset(&args
, 0, sizeof(LINK3args
));
4040 args
.file
= link_data
->oldfh
;
4041 args
.link
.dir
= link_data
->newdir
;
4042 args
.link
.name
= link_data
->newobject
;
4043 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4044 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4045 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4046 free_nfs_cb_data(data
);
4053 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4055 struct nfs_link_data
*link_data
= data
->continue_data
;
4057 /* steal the filehandle */
4058 link_data
->oldfh
= data
->fh
;
4059 data
->fh
.data
.data_val
= NULL
;
4061 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) {
4062 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4063 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4064 free_nfs_cb_data(data
);
4067 data
->continue_data
= NULL
;
4068 free_nfs_cb_data(data
);
4074 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4077 struct nfs_link_data
*link_data
;
4079 link_data
= malloc(sizeof(struct nfs_link_data
));
4080 if (link_data
== NULL
) {
4081 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4084 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4086 link_data
->oldpath
= strdup(oldpath
);
4087 if (link_data
->oldpath
== NULL
) {
4088 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4089 free_nfs_link_data(link_data
);
4093 link_data
->newpath
= strdup(newpath
);
4094 if (link_data
->newpath
== NULL
) {
4095 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4096 free_nfs_link_data(link_data
);
4099 ptr
= strrchr(link_data
->newpath
, '/');
4101 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4102 free_nfs_link_data(link_data
);
4107 link_data
->newobject
= ptr
;
4110 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4111 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4119 //qqq replace later with lseek()
4120 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4122 return nfsfh
->offset
;
4128 * Get the maximum supported READ3 size by the server
4130 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4132 return nfs
->readmax
;
4136 * Get the maximum supported WRITE3 size by the server
4138 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4140 return nfs
->writemax
;
4143 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4144 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4147 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4148 rpc_set_uid(nfs
->rpc
, uid
);
4151 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4152 rpc_set_gid(nfs
->rpc
, gid
);
4155 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4160 va_start(ap
, error_string
);
4162 vsnprintf(str
, 1024, error_string
, ap
);
4163 if (nfs
->rpc
->error_string
!= NULL
) {
4164 free(nfs
->rpc
->error_string
);
4166 nfs
->rpc
->error_string
= str
;
4172 struct mount_cb_data
{
4178 static void free_mount_cb_data(struct mount_cb_data
*data
)
4180 if (data
->server
!= NULL
) {
4182 data
->server
= NULL
;
4188 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4190 struct mount_cb_data
*data
= private_data
;
4192 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4194 if (status
== RPC_STATUS_ERROR
) {
4195 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4196 free_mount_cb_data(data
);
4199 if (status
== RPC_STATUS_CANCEL
) {
4200 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4201 free_mount_cb_data(data
);
4205 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4206 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4207 rpc_set_error(rpc
, "Failed to disconnect\n");
4209 free_mount_cb_data(data
);
4212 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4214 struct mount_cb_data
*data
= private_data
;
4216 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4218 /* Dont want any more callbacks even if the socket is closed */
4219 rpc
->connect_cb
= NULL
;
4221 if (status
== RPC_STATUS_ERROR
) {
4222 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4223 free_mount_cb_data(data
);
4226 if (status
== RPC_STATUS_CANCEL
) {
4227 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4228 free_mount_cb_data(data
);
4232 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4233 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4234 free_mount_cb_data(data
);
4239 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4241 struct mount_cb_data
*data
= private_data
;
4242 uint32_t mount_port
;
4244 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4246 if (status
== RPC_STATUS_ERROR
) {
4247 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4248 free_mount_cb_data(data
);
4251 if (status
== RPC_STATUS_CANCEL
) {
4252 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4253 free_mount_cb_data(data
);
4257 mount_port
= *(uint32_t *)command_data
;
4258 if (mount_port
== 0) {
4259 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4260 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4261 free_mount_cb_data(data
);
4265 rpc_disconnect(rpc
, "normal disconnect");
4266 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4267 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4268 free_mount_cb_data(data
);
4273 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4275 struct mount_cb_data
*data
= private_data
;
4277 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4279 if (status
== RPC_STATUS_ERROR
) {
4280 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4281 free_mount_cb_data(data
);
4284 if (status
== RPC_STATUS_CANCEL
) {
4285 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4286 free_mount_cb_data(data
);
4290 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4291 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4292 free_mount_cb_data(data
);
4297 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4299 struct mount_cb_data
*data
= private_data
;
4301 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4303 /* Dont want any more callbacks even if the socket is closed */
4304 rpc
->connect_cb
= NULL
;
4306 if (status
== RPC_STATUS_ERROR
) {
4307 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4308 free_mount_cb_data(data
);
4311 if (status
== RPC_STATUS_CANCEL
) {
4312 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4313 free_mount_cb_data(data
);
4317 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4318 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4319 free_mount_cb_data(data
);
4324 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4326 struct mount_cb_data
*data
;
4328 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4330 data
= malloc(sizeof(struct mount_cb_data
));
4334 memset(data
, 0, sizeof(struct mount_cb_data
));
4336 data
->private_data
= private_data
;
4337 data
->server
= strdup(server
);
4338 if (data
->server
== NULL
) {
4339 free_mount_cb_data(data
);
4342 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4343 free_mount_cb_data(data
);
4350 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4352 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4356 const char *nfs_get_server(struct nfs_context
*nfs
) {
4360 const char *nfs_get_export(struct nfs_context
*nfs
) {
4364 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4365 return &nfs
->rootfh
;
4368 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {