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 static void free_nfs_cb_data(struct nfs_cb_data
*data
)
529 if (data
->continue_data
!= NULL
) {
530 assert(data
->free_continue_data
);
531 data
->free_continue_data(data
->continue_data
);
534 free(data
->saved_path
);
535 free(data
->fh
.data
.data_val
);
545 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
547 struct nfs_cb_data
*data
= private_data
;
548 struct nfs_context
*nfs
= data
->nfs
;
550 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
552 if (status
== RPC_STATUS_ERROR
) {
553 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
554 free_nfs_cb_data(data
);
557 if (status
== RPC_STATUS_CANCEL
) {
558 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
559 free_nfs_cb_data(data
);
563 data
->cb(0, nfs
, NULL
, data
->private_data
);
564 free_nfs_cb_data(data
);
567 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
569 struct nfs_cb_data
*data
= private_data
;
570 struct nfs_context
*nfs
= data
->nfs
;
571 FSINFO3res
*res
= command_data
;
572 struct GETATTR3args args
;
574 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
576 if (status
== RPC_STATUS_ERROR
) {
577 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
578 free_nfs_cb_data(data
);
581 if (status
== RPC_STATUS_CANCEL
) {
582 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
583 free_nfs_cb_data(data
);
587 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
588 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
590 memset(&args
, 0, sizeof(GETATTR3args
));
591 args
.object
= nfs
->rootfh
;
593 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
594 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
595 free_nfs_cb_data(data
);
600 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
602 struct nfs_cb_data
*data
= private_data
;
603 struct nfs_context
*nfs
= data
->nfs
;
604 struct FSINFO3args args
;
606 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
608 if (status
== RPC_STATUS_ERROR
) {
609 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
610 free_nfs_cb_data(data
);
613 if (status
== RPC_STATUS_CANCEL
) {
614 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
615 free_nfs_cb_data(data
);
619 args
.fsroot
= nfs
->rootfh
;
620 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
621 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
622 free_nfs_cb_data(data
);
628 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
630 struct nfs_cb_data
*data
= private_data
;
631 struct nfs_context
*nfs
= data
->nfs
;
633 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
635 /* Dont want any more callbacks even if the socket is closed */
636 rpc
->connect_cb
= NULL
;
638 if (status
== RPC_STATUS_ERROR
) {
639 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
640 free_nfs_cb_data(data
);
643 if (status
== RPC_STATUS_CANCEL
) {
644 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
645 free_nfs_cb_data(data
);
649 if (rpc_nfs3_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
650 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
651 free_nfs_cb_data(data
);
657 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
659 struct nfs_cb_data
*data
= private_data
;
660 struct nfs_context
*nfs
= data
->nfs
;
663 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
665 if (status
== RPC_STATUS_ERROR
) {
666 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
667 free_nfs_cb_data(data
);
670 if (status
== RPC_STATUS_CANCEL
) {
671 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
672 free_nfs_cb_data(data
);
677 if (res
->fhs_status
!= MNT3_OK
) {
678 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
));
679 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
680 free_nfs_cb_data(data
);
684 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
685 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
686 if (nfs
->rootfh
.data
.data_val
== NULL
) {
687 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
688 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
689 free_nfs_cb_data(data
);
692 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
694 rpc_disconnect(rpc
, "normal disconnect");
695 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
696 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
697 free_nfs_cb_data(data
);
700 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
701 rpc_set_autoreconnect(rpc
);
705 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
707 struct nfs_cb_data
*data
= private_data
;
708 struct nfs_context
*nfs
= data
->nfs
;
710 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
712 if (status
== RPC_STATUS_ERROR
) {
713 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
714 free_nfs_cb_data(data
);
717 if (status
== RPC_STATUS_CANCEL
) {
718 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
719 free_nfs_cb_data(data
);
723 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
724 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
725 free_nfs_cb_data(data
);
730 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
732 struct nfs_cb_data
*data
= private_data
;
733 struct nfs_context
*nfs
= data
->nfs
;
735 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
737 /* Dont want any more callbacks even if the socket is closed */
738 rpc
->connect_cb
= NULL
;
740 if (status
== RPC_STATUS_ERROR
) {
741 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
742 free_nfs_cb_data(data
);
745 if (status
== RPC_STATUS_CANCEL
) {
746 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
747 free_nfs_cb_data(data
);
751 if (rpc_mount3_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
752 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
753 free_nfs_cb_data(data
);
758 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
760 struct nfs_cb_data
*data
= private_data
;
761 struct nfs_context
*nfs
= data
->nfs
;
764 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
766 if (status
== RPC_STATUS_ERROR
) {
767 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
768 free_nfs_cb_data(data
);
771 if (status
== RPC_STATUS_CANCEL
) {
772 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
773 free_nfs_cb_data(data
);
777 mount_port
= *(uint32_t *)command_data
;
778 if (mount_port
== 0) {
779 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
780 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
781 free_nfs_cb_data(data
);
785 rpc_disconnect(rpc
, "normal disconnect");
786 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
787 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
788 free_nfs_cb_data(data
);
794 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
796 struct nfs_cb_data
*data
= private_data
;
797 struct nfs_context
*nfs
= data
->nfs
;
799 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
801 if (status
== RPC_STATUS_ERROR
) {
802 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
803 free_nfs_cb_data(data
);
806 if (status
== RPC_STATUS_CANCEL
) {
807 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
808 free_nfs_cb_data(data
);
812 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
813 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
814 free_nfs_cb_data(data
);
819 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
821 struct nfs_cb_data
*data
= private_data
;
822 struct nfs_context
*nfs
= data
->nfs
;
824 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
826 /* Dont want any more callbacks even if the socket is closed */
827 rpc
->connect_cb
= NULL
;
829 if (status
== RPC_STATUS_ERROR
) {
830 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
831 free_nfs_cb_data(data
);
834 if (status
== RPC_STATUS_CANCEL
) {
835 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
836 free_nfs_cb_data(data
);
840 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
841 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
842 free_nfs_cb_data(data
);
848 * Async call for mounting an nfs share and geting the root filehandle
850 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
852 struct nfs_cb_data
*data
;
853 char *new_server
, *new_export
;
855 data
= malloc(sizeof(struct nfs_cb_data
));
857 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
860 memset(data
, 0, sizeof(struct nfs_cb_data
));
861 new_server
= strdup(server
);
862 new_export
= strdup(export
);
863 if (nfs
->server
!= NULL
) {
866 nfs
->server
= new_server
;
867 if (nfs
->export
!= NULL
) {
870 nfs
->export
= new_export
;
873 data
->private_data
= private_data
;
875 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
876 rpc_set_error(nfs
->rpc
, "Failed to start connection");
877 free_nfs_cb_data(data
);
887 * Functions to first look up a path, component by component, and then finally call a specific function once
888 * the filehandle for the final component is found.
890 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
892 struct nfs_cb_data
*data
= private_data
;
893 struct nfs_context
*nfs
= data
->nfs
;
896 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
898 if (status
== RPC_STATUS_ERROR
) {
899 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
900 free_nfs_cb_data(data
);
903 if (status
== RPC_STATUS_CANCEL
) {
904 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
905 free_nfs_cb_data(data
);
910 if (res
->status
!= NFS3_OK
) {
911 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
));
912 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
913 free_nfs_cb_data(data
);
917 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
918 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
919 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
920 free_nfs_cb_data(data
);
925 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
930 while (*data
->path
== '/') {
935 slash
= strchr(path
, '/');
937 /* Clear slash so that path is a zero terminated string for
938 * the current path component. Set it back to '/' again later
939 * when we are finished referencing this component so that
940 * data->saved_path will still point to the full
944 data
->path
= slash
+1;
946 while (*data
->path
!= 0) {
952 data
->fh
.data
.data_len
= fh
->data
.data_len
;
953 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
954 if (data
->fh
.data
.data_val
== NULL
) {
955 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
956 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
957 free_nfs_cb_data(data
);
960 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
964 data
->continue_cb(nfs
, data
);
969 memset(&args
, 0, sizeof(LOOKUP3args
));
971 args
.what
.name
= path
;
973 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
974 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
975 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
976 free_nfs_cb_data(data
);
985 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
991 while ((str
= strstr(path
, "//"))) {
999 while ((str
= strstr(path
, "/./"))) {
1006 /* ^/../ -> error */
1007 if (!strncmp(path
, "/../", 4)) {
1008 rpc_set_error(nfs
->rpc
,
1009 "Absolute path starts with '/../' "
1010 "during normalization");
1014 /* ^[^/] -> error */
1015 if (path
[0] != '/') {
1016 rpc_set_error(nfs
->rpc
,
1017 "Absolute path does not start with '/'");
1021 /* /string/../ -> / */
1022 while ((str
= strstr(path
, "/../"))) {
1025 if (!strncmp(path
, "/../", 4)) {
1026 rpc_set_error(nfs
->rpc
,
1027 "Absolute path starts with '/../' "
1028 "during normalization");
1033 while (*tmp
!= '/') {
1037 while((*(tmp
++) = *(str
++)) != '\0')
1044 if (path
[len
- 1] == '/') {
1045 path
[len
- 1] = '\0';
1049 if (path
[0] == '\0') {
1050 rpc_set_error(nfs
->rpc
,
1051 "Absolute path became '' "
1052 "during normalization");
1058 if (!strcmp(&path
[len
- 2], "/.")) {
1059 path
[len
- 2] = '\0';
1064 /* ^/..$ -> error */
1065 if (!strcmp(path
, "/..")) {
1066 rpc_set_error(nfs
->rpc
,
1067 "Absolute path is '/..' "
1068 "during normalization");
1072 /* /string/..$ -> / */
1074 if (!strcmp(&path
[len
- 3], "/..")) {
1075 char *tmp
= &path
[len
- 3];
1076 while (*--tmp
!= '/')
1085 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
)
1087 struct nfs_cb_data
*data
;
1089 if (path
[0] == '\0') {
1093 data
= malloc(sizeof(struct nfs_cb_data
));
1095 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1096 "nfs_cb_data structure");
1097 if (free_continue_data
)
1098 free_continue_data(continue_data
);
1101 memset(data
, 0, sizeof(struct nfs_cb_data
));
1104 data
->continue_cb
= continue_cb
;
1105 data
->continue_data
= continue_data
;
1106 data
->free_continue_data
= free_continue_data
;
1107 data
->continue_int
= continue_int
;
1108 data
->private_data
= private_data
;
1109 if (path
[0] == '/') {
1110 data
->saved_path
= strdup(path
);
1112 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1113 if (data
->saved_path
== NULL
) {
1114 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1115 "malloc path string");
1116 free_nfs_cb_data(data
);
1119 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1122 if (data
->saved_path
== NULL
) {
1123 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1124 free_nfs_cb_data(data
);
1127 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1128 free_nfs_cb_data(data
);
1132 data
->path
= data
->saved_path
;
1134 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1135 /* return 0 here since the callback will be invoked if there is a failure */
1148 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1151 struct nfs_cb_data
*data
= private_data
;
1152 struct nfs_context
*nfs
= data
->nfs
;
1159 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1161 if (status
== RPC_STATUS_ERROR
) {
1162 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1163 free_nfs_cb_data(data
);
1166 if (status
== RPC_STATUS_CANCEL
) {
1167 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1168 free_nfs_cb_data(data
);
1173 if (res
->status
!= NFS3_OK
) {
1174 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
));
1175 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1176 free_nfs_cb_data(data
);
1181 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1182 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1183 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1184 st
.st_mode
|= S_IFDIR
;
1186 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1187 st
.st_mode
|= S_IFREG
;
1189 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1190 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1191 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1193 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1195 st
.st_blksize
= 4096;
1196 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1198 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1199 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1200 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1202 data
->cb(0, nfs
, &st
, data
->private_data
);
1203 free_nfs_cb_data(data
);
1206 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1208 struct GETATTR3args args
;
1210 memset(&args
, 0, sizeof(GETATTR3args
));
1211 args
.object
= data
->fh
;
1213 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1214 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1215 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1216 free_nfs_cb_data(data
);
1222 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1224 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1225 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1237 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1239 struct nfs_cb_data
*data
= private_data
;
1240 struct nfs_context
*nfs
= data
->nfs
;
1241 struct nfsfh
*nfsfh
;
1244 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1246 if (status
== RPC_STATUS_ERROR
) {
1247 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1248 free_nfs_cb_data(data
);
1251 if (status
== RPC_STATUS_CANCEL
) {
1252 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1253 free_nfs_cb_data(data
);
1258 if (res
->status
!= NFS3_OK
) {
1259 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1260 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1261 free_nfs_cb_data(data
);
1265 nfsfh
= malloc(sizeof(struct nfsfh
));
1266 if (nfsfh
== NULL
) {
1267 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1268 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1269 free_nfs_cb_data(data
);
1272 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1274 if (data
->continue_int
& O_SYNC
) {
1278 /* steal the filehandle */
1279 nfsfh
->fh
= data
->fh
;
1280 data
->fh
.data
.data_val
= NULL
;
1282 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1283 free_nfs_cb_data(data
);
1286 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1289 struct nfs_cb_data
*data
= private_data
;
1290 struct nfs_context
*nfs
= data
->nfs
;
1291 struct nfsfh
*nfsfh
;
1292 unsigned int nfsmode
= 0;
1294 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1296 if (status
== RPC_STATUS_ERROR
) {
1297 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1298 free_nfs_cb_data(data
);
1301 if (status
== RPC_STATUS_CANCEL
) {
1302 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1303 free_nfs_cb_data(data
);
1308 if (res
->status
!= NFS3_OK
) {
1309 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
));
1310 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1311 free_nfs_cb_data(data
);
1315 if (data
->continue_int
& O_WRONLY
) {
1316 nfsmode
|= ACCESS3_MODIFY
;
1318 if (data
->continue_int
& O_RDWR
) {
1319 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1321 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1322 nfsmode
|= ACCESS3_READ
;
1326 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1327 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1328 nfsmode
&ACCESS3_READ
?'r':'-',
1329 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1330 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1331 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1332 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1333 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1334 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1335 free_nfs_cb_data(data
);
1339 /* Try to truncate it if we were requested to */
1340 if ((data
->continue_int
& O_TRUNC
) &&
1341 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1344 memset(&args
, 0, sizeof(SETATTR3args
));
1345 args
.object
= data
->fh
;
1346 args
.new_attributes
.size
.set_it
= 1;
1347 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1349 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1351 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1352 "SETATTR call for %s", data
->path
);
1353 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1354 data
->private_data
);
1355 free_nfs_cb_data(data
);
1361 nfsfh
= malloc(sizeof(struct nfsfh
));
1362 if (nfsfh
== NULL
) {
1363 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1364 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1365 free_nfs_cb_data(data
);
1368 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1370 if (data
->continue_int
& O_SYNC
) {
1374 /* steal the filehandle */
1375 nfsfh
->fh
= data
->fh
;
1376 data
->fh
.data
.data_val
= NULL
;
1378 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1379 free_nfs_cb_data(data
);
1382 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1387 if (data
->continue_int
& O_WRONLY
) {
1388 nfsmode
|= ACCESS3_MODIFY
;
1390 if (data
->continue_int
& O_RDWR
) {
1391 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1393 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1394 nfsmode
|= ACCESS3_READ
;
1397 memset(&args
, 0, sizeof(ACCESS3args
));
1398 args
.object
= data
->fh
;
1399 args
.access
= nfsmode
;
1401 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1402 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1403 "call for %s", data
->path
);
1404 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1405 data
->private_data
);
1406 free_nfs_cb_data(data
);
1412 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1414 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1415 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1426 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1428 /* steal saved_path */
1430 nfs
->cwd
= data
->saved_path
;
1431 data
->saved_path
= NULL
;
1433 data
->cb(0, nfs
, NULL
, data
->private_data
);
1434 free_nfs_cb_data(data
);
1439 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1441 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1442 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1453 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1455 struct nfs_cb_data
*data
= private_data
;
1456 struct nfs_context
*nfs
= data
->nfs
;
1459 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1461 if (status
== RPC_STATUS_ERROR
) {
1462 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1463 free_nfs_cb_data(data
);
1466 if (status
== RPC_STATUS_CANCEL
) {
1467 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1468 free_nfs_cb_data(data
);
1473 if (res
->status
!= NFS3_OK
) {
1474 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1475 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1476 free_nfs_cb_data(data
);
1480 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1481 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1482 free_nfs_cb_data(data
);
1485 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1487 struct nfs_mcb_data
*mdata
= private_data
;
1488 struct nfs_cb_data
*data
= mdata
->data
;
1489 struct nfs_context
*nfs
= data
->nfs
;
1492 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1496 if (status
== RPC_STATUS_ERROR
) {
1497 /* flag the failure but do not invoke callback until we have received all responses */
1500 if (status
== RPC_STATUS_CANCEL
) {
1501 /* flag the cancellation but do not invoke callback until we have received all responses */
1505 /* reassemble the data into the buffer */
1506 if (status
== RPC_STATUS_SUCCESS
) {
1508 if (res
->status
!= NFS3_OK
) {
1509 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1512 if (res
->READ3res_u
.resok
.count
> 0) {
1513 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1514 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1515 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1521 if (data
->num_calls
> 0) {
1522 /* still waiting for more replies */
1527 if (data
->error
!= 0) {
1528 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1529 free_nfs_cb_data(data
);
1533 if (data
->cancel
!= 0) {
1534 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1535 free_nfs_cb_data(data
);
1540 data
->nfsfh
->offset
= data
->max_offset
;
1541 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1543 free_nfs_cb_data(data
);
1547 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1549 struct nfs_cb_data
*data
;
1551 data
= malloc(sizeof(struct nfs_cb_data
));
1553 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1556 memset(data
, 0, sizeof(struct nfs_cb_data
));
1559 data
->private_data
= private_data
;
1560 data
->nfsfh
= nfsfh
;
1562 nfsfh
->offset
= offset
;
1564 if (count
<= nfs_get_readmax(nfs
)) {
1567 memset(&args
, 0, sizeof(READ3args
));
1568 args
.file
= nfsfh
->fh
;
1569 args
.offset
= offset
;
1572 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_cb
, &args
, data
) != 0) {
1573 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1574 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1575 free_nfs_cb_data(data
);
1581 /* trying to read more than maximum server read size, we has to chop it up into smaller
1582 * reads and collect into a reassembly buffer.
1583 * we send all reads in parallel so that performance is still good.
1585 data
->max_offset
= offset
;
1586 data
->start_offset
= offset
;
1588 data
->buffer
= malloc(count
);
1589 if (data
->buffer
== NULL
) {
1590 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1591 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1592 free_nfs_cb_data(data
);
1597 uint64_t readcount
= count
;
1598 struct nfs_mcb_data
*mdata
;
1601 if (readcount
> nfs_get_readmax(nfs
)) {
1602 readcount
= nfs_get_readmax(nfs
);
1605 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1606 if (mdata
== NULL
) {
1607 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1610 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1612 mdata
->offset
= offset
;
1613 mdata
->count
= readcount
;
1615 memset(&args
, 0, sizeof(READ3args
));
1616 args
.file
= nfsfh
->fh
;
1617 args
.offset
= offset
;
1618 args
.count
= readcount
;
1620 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1621 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1622 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1628 offset
+= readcount
;
1638 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1640 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1648 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1650 struct nfs_cb_data
*data
= private_data
;
1651 struct nfs_context
*nfs
= data
->nfs
;
1654 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1656 if (status
== RPC_STATUS_ERROR
) {
1657 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1658 free_nfs_cb_data(data
);
1661 if (status
== RPC_STATUS_CANCEL
) {
1662 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1663 free_nfs_cb_data(data
);
1668 if (res
->status
!= NFS3_OK
) {
1669 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1670 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1671 free_nfs_cb_data(data
);
1675 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1676 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1677 free_nfs_cb_data(data
);
1680 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1682 struct nfs_mcb_data
*mdata
= private_data
;
1683 struct nfs_cb_data
*data
= mdata
->data
;
1684 struct nfs_context
*nfs
= data
->nfs
;
1687 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1691 if (status
== RPC_STATUS_ERROR
) {
1692 /* flag the failure but do not invoke callback until we have received all responses */
1695 if (status
== RPC_STATUS_CANCEL
) {
1696 /* flag the cancellation but do not invoke callback until we have received all responses */
1700 if (status
== RPC_STATUS_SUCCESS
) {
1702 if (res
->status
!= NFS3_OK
) {
1703 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1706 if (res
->WRITE3res_u
.resok
.count
> 0) {
1707 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1708 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1714 if (data
->num_calls
> 0) {
1715 /* still waiting for more replies */
1720 if (data
->error
!= 0) {
1721 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1722 free_nfs_cb_data(data
);
1726 if (data
->cancel
!= 0) {
1727 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1728 free_nfs_cb_data(data
);
1733 data
->nfsfh
->offset
= data
->max_offset
;
1734 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1736 free_nfs_cb_data(data
);
1741 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
)
1743 struct nfs_cb_data
*data
;
1745 data
= malloc(sizeof(struct nfs_cb_data
));
1747 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1750 memset(data
, 0, sizeof(struct nfs_cb_data
));
1753 data
->private_data
= private_data
;
1754 data
->nfsfh
= nfsfh
;
1756 nfsfh
->offset
= offset
;
1758 if (count
<= nfs_get_writemax(nfs
)) {
1761 memset(&args
, 0, sizeof(WRITE3args
));
1762 args
.file
= nfsfh
->fh
;
1763 args
.offset
= offset
;
1765 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1766 args
.data
.data_len
= count
;
1767 args
.data
.data_val
= buf
;
1769 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_cb
, &args
, data
) != 0) {
1770 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1771 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1772 free_nfs_cb_data(data
);
1778 /* trying to write more than maximum server write size, we has to chop it up into smaller
1780 * we send all writes in parallel so that performance is still good.
1782 data
->max_offset
= offset
;
1783 data
->start_offset
= offset
;
1786 uint64_t writecount
= count
;
1787 struct nfs_mcb_data
*mdata
;
1790 if (writecount
> nfs_get_writemax(nfs
)) {
1791 writecount
= nfs_get_writemax(nfs
);
1794 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1795 if (mdata
== NULL
) {
1796 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1799 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1801 mdata
->offset
= offset
;
1802 mdata
->count
= writecount
;
1804 memset(&args
, 0, sizeof(WRITE3args
));
1805 args
.file
= nfsfh
->fh
;
1806 args
.offset
= offset
;
1807 args
.count
= writecount
;
1808 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1809 args
.data
.data_len
= writecount
;
1810 args
.data
.data_val
= &buf
[offset
- data
->start_offset
];
1812 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1813 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1814 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1819 count
-= writecount
;
1820 offset
+= writecount
;
1830 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1832 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1842 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1844 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1845 free(nfsfh
->fh
.data
.data_val
);
1846 nfsfh
->fh
.data
.data_val
= NULL
;
1850 cb(0, nfs
, NULL
, private_data
);
1861 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1863 struct nfs_cb_data
*data
;
1864 struct GETATTR3args args
;
1866 data
= malloc(sizeof(struct nfs_cb_data
));
1868 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1871 memset(data
, 0, sizeof(struct nfs_cb_data
));
1874 data
->private_data
= private_data
;
1876 memset(&args
, 0, sizeof(GETATTR3args
));
1877 args
.object
= nfsfh
->fh
;
1879 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1880 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1881 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1882 free_nfs_cb_data(data
);
1893 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1895 struct nfs_cb_data
*data
= private_data
;
1896 struct nfs_context
*nfs
= data
->nfs
;
1899 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1901 if (status
== RPC_STATUS_ERROR
) {
1902 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1903 free_nfs_cb_data(data
);
1906 if (status
== RPC_STATUS_CANCEL
) {
1907 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1908 free_nfs_cb_data(data
);
1913 if (res
->status
!= NFS3_OK
) {
1914 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1915 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1916 free_nfs_cb_data(data
);
1920 data
->cb(0, nfs
, NULL
, data
->private_data
);
1921 free_nfs_cb_data(data
);
1924 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1926 struct nfs_cb_data
*data
;
1927 struct COMMIT3args args
;
1929 data
= malloc(sizeof(struct nfs_cb_data
));
1931 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1934 memset(data
, 0, sizeof(struct nfs_cb_data
));
1937 data
->private_data
= private_data
;
1939 args
.file
= nfsfh
->fh
;
1942 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1943 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1944 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1945 free_nfs_cb_data(data
);
1957 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1959 struct nfs_cb_data
*data
= private_data
;
1960 struct nfs_context
*nfs
= data
->nfs
;
1963 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1965 if (status
== RPC_STATUS_ERROR
) {
1966 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1967 free_nfs_cb_data(data
);
1970 if (status
== RPC_STATUS_CANCEL
) {
1971 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1972 free_nfs_cb_data(data
);
1977 if (res
->status
!= NFS3_OK
) {
1978 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1979 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1980 free_nfs_cb_data(data
);
1984 data
->cb(0, nfs
, NULL
, data
->private_data
);
1985 free_nfs_cb_data(data
);
1988 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1990 struct nfs_cb_data
*data
;
1993 data
= malloc(sizeof(struct nfs_cb_data
));
1995 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1998 memset(data
, 0, sizeof(struct nfs_cb_data
));
2001 data
->private_data
= private_data
;
2003 memset(&args
, 0, sizeof(SETATTR3args
));
2004 args
.object
= nfsfh
->fh
;
2005 args
.new_attributes
.size
.set_it
= 1;
2006 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2008 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2009 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2010 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2011 free_nfs_cb_data(data
);
2021 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2023 uint64_t offset
= data
->continue_int
;
2026 nfsfh
.fh
= data
->fh
;
2028 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2029 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2030 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2031 free_nfs_cb_data(data
);
2034 free_nfs_cb_data(data
);
2038 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2044 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2045 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2058 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2061 struct nfs_cb_data
*data
= private_data
;
2062 struct nfs_context
*nfs
= data
->nfs
;
2063 char *str
= data
->continue_data
;
2065 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2067 str
= &str
[strlen(str
) + 1];
2069 if (status
== RPC_STATUS_ERROR
) {
2070 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2071 free_nfs_cb_data(data
);
2074 if (status
== RPC_STATUS_CANCEL
) {
2075 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2076 free_nfs_cb_data(data
);
2081 if (res
->status
!= NFS3_OK
) {
2082 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
));
2083 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2084 free_nfs_cb_data(data
);
2088 data
->cb(0, nfs
, NULL
, data
->private_data
);
2089 free_nfs_cb_data(data
);
2092 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2094 char *str
= data
->continue_data
;
2097 str
= &str
[strlen(str
) + 1];
2099 memset(&args
, 0, sizeof(MKDIR3args
));
2100 args
.where
.dir
= data
->fh
;
2101 args
.where
.name
= str
;
2102 args
.attributes
.mode
.set_it
= 1;
2103 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2105 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2106 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2107 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2108 free_nfs_cb_data(data
);
2114 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2119 new_path
= strdup(path
);
2120 if (new_path
== NULL
) {
2121 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2125 ptr
= strrchr(new_path
, '/');
2128 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2133 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2134 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2135 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2149 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2152 struct nfs_cb_data
*data
= private_data
;
2153 struct nfs_context
*nfs
= data
->nfs
;
2154 char *str
= data
->continue_data
;
2156 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2158 str
= &str
[strlen(str
) + 1];
2160 if (status
== RPC_STATUS_ERROR
) {
2161 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2162 free_nfs_cb_data(data
);
2165 if (status
== RPC_STATUS_CANCEL
) {
2166 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2167 free_nfs_cb_data(data
);
2172 if (res
->status
!= NFS3_OK
) {
2173 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
));
2174 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2175 free_nfs_cb_data(data
);
2179 data
->cb(0, nfs
, NULL
, data
->private_data
);
2180 free_nfs_cb_data(data
);
2183 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2185 char *str
= data
->continue_data
;
2188 str
= &str
[strlen(str
) + 1];
2190 args
.object
.dir
= data
->fh
;
2191 args
.object
.name
= str
;
2192 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2193 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2194 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2195 free_nfs_cb_data(data
);
2201 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2206 new_path
= strdup(path
);
2207 if (new_path
== NULL
) {
2208 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2212 ptr
= strrchr(new_path
, '/');
2215 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2220 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2221 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2222 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2235 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2238 struct nfs_cb_data
*data
= private_data
;
2239 struct nfs_context
*nfs
= data
->nfs
;
2240 struct nfsfh
*nfsfh
;
2241 char *str
= data
->continue_data
;
2243 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2245 if (status
== RPC_STATUS_ERROR
) {
2246 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2247 free_nfs_cb_data(data
);
2250 if (status
== RPC_STATUS_CANCEL
) {
2251 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2252 free_nfs_cb_data(data
);
2256 str
= &str
[strlen(str
) + 1];
2258 if (res
->status
!= NFS3_OK
) {
2259 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
));
2260 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2265 nfsfh
= malloc(sizeof(struct nfsfh
));
2266 if (nfsfh
== NULL
) {
2267 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2268 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2269 free_nfs_cb_data(data
);
2272 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2274 /* copy the filehandle */
2275 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2276 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2277 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2279 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2280 free_nfs_cb_data(data
);
2285 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2288 struct nfs_cb_data
*data
= private_data
;
2289 struct nfs_context
*nfs
= data
->nfs
;
2290 char *str
= data
->continue_data
;
2293 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2295 if (status
== RPC_STATUS_ERROR
) {
2296 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2297 free_nfs_cb_data(data
);
2300 if (status
== RPC_STATUS_CANCEL
) {
2301 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2302 free_nfs_cb_data(data
);
2306 str
= &str
[strlen(str
) + 1];
2308 if (res
->status
!= NFS3_OK
) {
2309 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
));
2310 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2311 free_nfs_cb_data(data
);
2315 memset(&args
, 0, sizeof(LOOKUP3args
));
2316 args
.what
.dir
= data
->fh
;
2317 args
.what
.name
= str
;
2319 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2320 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2321 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2322 free_nfs_cb_data(data
);
2328 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2330 char *str
= data
->continue_data
;
2333 str
= &str
[strlen(str
) + 1];
2335 memset(&args
, 0, sizeof(CREATE3args
));
2336 args
.where
.dir
= data
->fh
;
2337 args
.where
.name
= str
;
2338 args
.how
.mode
= UNCHECKED
;
2339 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2340 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2342 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2343 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2344 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2345 free_nfs_cb_data(data
);
2351 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2356 new_path
= strdup(path
);
2357 if (new_path
== NULL
) {
2358 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2362 ptr
= strrchr(new_path
, '/');
2364 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2370 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2371 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2372 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2385 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2388 struct nfs_cb_data
*data
= private_data
;
2389 struct nfs_context
*nfs
= data
->nfs
;
2390 char *str
= data
->continue_data
;
2392 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2394 str
= &str
[strlen(str
) + 1];
2396 if (status
== RPC_STATUS_ERROR
) {
2397 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2398 free_nfs_cb_data(data
);
2401 if (status
== RPC_STATUS_CANCEL
) {
2402 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2403 free_nfs_cb_data(data
);
2408 if (res
->status
!= NFS3_OK
) {
2409 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
));
2410 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2411 free_nfs_cb_data(data
);
2415 data
->cb(0, nfs
, NULL
, data
->private_data
);
2416 free_nfs_cb_data(data
);
2419 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2421 char *str
= data
->continue_data
;
2422 struct REMOVE3args args
;
2424 str
= &str
[strlen(str
) + 1];
2426 args
.object
.dir
= data
->fh
;
2427 args
.object
.name
= str
;
2428 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2429 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2430 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2431 free_nfs_cb_data(data
);
2437 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2442 new_path
= strdup(path
);
2443 if (new_path
== NULL
) {
2444 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2448 ptr
= strrchr(new_path
, '/');
2450 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2455 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2456 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2457 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2468 struct mknod_cb_data
{
2475 static void free_mknod_cb_data(void *ptr
)
2477 struct mknod_cb_data
*data
= ptr
;
2483 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2486 struct nfs_cb_data
*data
= private_data
;
2487 struct nfs_context
*nfs
= data
->nfs
;
2488 char *str
= data
->continue_data
;
2490 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2492 str
= &str
[strlen(str
) + 1];
2494 if (status
== RPC_STATUS_ERROR
) {
2495 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2496 free_nfs_cb_data(data
);
2499 if (status
== RPC_STATUS_CANCEL
) {
2500 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2501 free_nfs_cb_data(data
);
2506 if (res
->status
!= NFS3_OK
) {
2507 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
));
2508 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2509 free_nfs_cb_data(data
);
2513 data
->cb(0, nfs
, NULL
, data
->private_data
);
2514 free_nfs_cb_data(data
);
2517 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2519 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2520 char *str
= cb_data
->path
;
2523 str
= &str
[strlen(str
) + 1];
2525 args
.where
.dir
= data
->fh
;
2526 args
.where
.name
= str
;
2527 switch (cb_data
->mode
& S_IFMT
) {
2529 args
.what
.type
= NF3CHR
;
2530 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2531 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
);
2532 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2533 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2536 args
.what
.type
= NF3BLK
;
2537 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2538 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
);
2539 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2540 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2542 args
.what
.type
= NF3SOCK
;
2543 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2544 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
);
2547 args
.what
.type
= NF3FIFO
;
2548 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2549 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
);
2552 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2553 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2554 free_nfs_cb_data(data
);
2558 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2559 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2560 free_nfs_cb_data(data
);
2566 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2569 struct mknod_cb_data
*cb_data
;
2571 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2572 if (cb_data
== NULL
) {
2573 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2577 cb_data
->path
= strdup(path
);
2578 if (cb_data
->path
== NULL
) {
2579 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2584 ptr
= strrchr(cb_data
->path
, '/');
2586 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2587 free_mknod_cb_data(cb_data
);
2592 cb_data
->mode
= mode
;
2593 cb_data
->major
= major(dev
);
2594 cb_data
->minor
= minor(dev
);
2596 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2597 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2598 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2609 /* ReadDirPlus Emulation Callback data */
2610 struct rdpe_cb_data
{
2613 struct nfs_cb_data
*data
;
2616 /* ReadDirPlus Emulation LOOKUP Callback data */
2617 struct rdpe_lookup_cb_data
{
2618 struct rdpe_cb_data
*rdpe_cb_data
;
2619 struct nfsdirent
*nfsdirent
;
2622 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2623 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2625 LOOKUP3res
*res
= command_data
;
2626 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2627 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2628 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2629 struct nfsdir
*nfsdir
= data
->continue_data
;
2630 struct nfs_context
*nfs
= data
->nfs
;
2631 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2633 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2635 free(rdpe_lookup_cb_data
);
2637 rdpe_cb_data
->getattrcount
--;
2639 if (status
== RPC_STATUS_ERROR
) {
2640 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2642 if (status
== RPC_STATUS_CANCEL
) {
2643 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2645 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2646 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2648 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2649 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2650 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2652 nfsdirent
->type
= attributes
->type
;
2653 nfsdirent
->mode
= attributes
->mode
;
2654 nfsdirent
->size
= attributes
->size
;
2656 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2657 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2658 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2659 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2660 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2661 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2662 nfsdirent
->uid
= attributes
->uid
;
2663 nfsdirent
->gid
= attributes
->gid
;
2667 if (rdpe_cb_data
->getattrcount
== 0) {
2668 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2669 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2670 nfs_free_nfsdir(nfsdir
);
2672 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2676 data
->continue_data
= NULL
;
2677 free_nfs_cb_data(data
);
2681 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2683 READDIR3res
*res
= command_data
;
2684 struct nfs_cb_data
*data
= private_data
;
2685 struct nfs_context
*nfs
= data
->nfs
;
2686 struct nfsdir
*nfsdir
= data
->continue_data
;
2687 struct nfsdirent
*nfsdirent
;
2688 struct entry3
*entry
;
2689 uint64_t cookie
= 0;
2690 struct rdpe_cb_data
*rdpe_cb_data
;
2692 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2694 if (status
== RPC_STATUS_ERROR
) {
2695 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2696 nfs_free_nfsdir(nfsdir
);
2697 data
->continue_data
= NULL
;
2698 free_nfs_cb_data(data
);
2702 if (status
== RPC_STATUS_CANCEL
) {
2703 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2704 nfs_free_nfsdir(nfsdir
);
2705 data
->continue_data
= NULL
;
2706 free_nfs_cb_data(data
);
2710 if (res
->status
!= NFS3_OK
) {
2711 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
));
2712 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2713 nfs_free_nfsdir(nfsdir
);
2714 data
->continue_data
= NULL
;
2715 free_nfs_cb_data(data
);
2719 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2720 while (entry
!= NULL
) {
2721 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2722 if (nfsdirent
== NULL
) {
2723 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2724 nfs_free_nfsdir(nfsdir
);
2725 data
->continue_data
= NULL
;
2726 free_nfs_cb_data(data
);
2729 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2730 nfsdirent
->name
= strdup(entry
->name
);
2731 if (nfsdirent
->name
== NULL
) {
2732 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2734 nfs_free_nfsdir(nfsdir
);
2735 data
->continue_data
= NULL
;
2736 free_nfs_cb_data(data
);
2739 nfsdirent
->inode
= entry
->fileid
;
2741 nfsdirent
->next
= nfsdir
->entries
;
2742 nfsdir
->entries
= nfsdirent
;
2744 cookie
= entry
->cookie
;
2745 entry
= entry
->nextentry
;
2748 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2751 args
.dir
= data
->fh
;
2752 args
.cookie
= cookie
;
2753 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2756 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2757 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2758 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2759 nfs_free_nfsdir(nfsdir
);
2760 data
->continue_data
= NULL
;
2761 free_nfs_cb_data(data
);
2767 /* steal the dirhandle */
2768 nfsdir
->current
= nfsdir
->entries
;
2770 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2771 rdpe_cb_data
->getattrcount
= 0;
2772 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2773 rdpe_cb_data
->data
= data
;
2774 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2775 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2778 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2779 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2780 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2782 memset(&args
, 0, sizeof(LOOKUP3args
));
2783 args
.what
.dir
= data
->fh
;
2784 args
.what
.name
= nfsdirent
->name
;
2786 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2787 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2789 /* if we have already commands in flight, we cant just stop, we have to wait for the
2790 * commands in flight to complete
2792 if (rdpe_cb_data
->getattrcount
> 0) {
2793 nfs_free_nfsdir(nfsdir
);
2794 data
->continue_data
= NULL
;
2795 free_nfs_cb_data(data
);
2796 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2797 free(rdpe_lookup_cb_data
);
2801 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2802 nfs_free_nfsdir(nfsdir
);
2803 data
->continue_data
= NULL
;
2804 free_nfs_cb_data(data
);
2805 free(rdpe_lookup_cb_data
);
2809 rdpe_cb_data
->getattrcount
++;
2813 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2815 READDIRPLUS3res
*res
= command_data
;
2816 struct nfs_cb_data
*data
= private_data
;
2817 struct nfs_context
*nfs
= data
->nfs
;
2818 struct nfsdir
*nfsdir
= data
->continue_data
;
2819 struct entryplus3
*entry
;
2820 uint64_t cookie
= 0;
2822 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2824 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2827 args
.dir
= data
->fh
;
2828 args
.cookie
= cookie
;
2829 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2832 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2833 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2834 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2835 nfs_free_nfsdir(nfsdir
);
2836 data
->continue_data
= NULL
;
2837 free_nfs_cb_data(data
);
2843 if (status
== RPC_STATUS_CANCEL
) {
2844 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2845 nfs_free_nfsdir(nfsdir
);
2846 data
->continue_data
= NULL
;
2847 free_nfs_cb_data(data
);
2851 if (res
->status
!= NFS3_OK
) {
2852 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
));
2853 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2854 nfs_free_nfsdir(nfsdir
);
2855 data
->continue_data
= NULL
;
2856 free_nfs_cb_data(data
);
2860 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2861 while (entry
!= NULL
) {
2862 struct nfsdirent
*nfsdirent
;
2864 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2865 if (nfsdirent
== NULL
) {
2866 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2867 nfs_free_nfsdir(nfsdir
);
2868 data
->continue_data
= NULL
;
2869 free_nfs_cb_data(data
);
2872 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2873 nfsdirent
->name
= strdup(entry
->name
);
2874 if (nfsdirent
->name
== NULL
) {
2875 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2877 nfs_free_nfsdir(nfsdir
);
2878 data
->continue_data
= NULL
;
2879 free_nfs_cb_data(data
);
2882 nfsdirent
->inode
= entry
->fileid
;
2883 if (entry
->name_attributes
.attributes_follow
) {
2884 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2885 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2886 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2888 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2889 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2890 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2891 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2892 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2893 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2894 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2895 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2898 nfsdirent
->next
= nfsdir
->entries
;
2899 nfsdir
->entries
= nfsdirent
;
2901 cookie
= entry
->cookie
;
2902 entry
= entry
->nextentry
;
2905 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2906 READDIRPLUS3args args
;
2908 args
.dir
= data
->fh
;
2909 args
.cookie
= cookie
;
2910 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2911 args
.dircount
= 8192;
2912 args
.maxcount
= 8192;
2914 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2915 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2916 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2917 nfs_free_nfsdir(nfsdir
);
2918 data
->continue_data
= NULL
;
2919 free_nfs_cb_data(data
);
2925 /* steal the dirhandle */
2926 data
->continue_data
= NULL
;
2927 nfsdir
->current
= nfsdir
->entries
;
2929 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2930 free_nfs_cb_data(data
);
2933 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2935 READDIRPLUS3args args
;
2937 args
.dir
= data
->fh
;
2939 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2940 args
.dircount
= 8192;
2941 args
.maxcount
= 8192;
2942 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2943 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2944 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2945 free_nfs_cb_data(data
);
2951 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2953 struct nfsdir
*nfsdir
;
2955 nfsdir
= malloc(sizeof(struct nfsdir
));
2956 if (nfsdir
== NULL
) {
2957 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2960 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2962 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2963 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2971 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2973 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2975 if (nfsdir
->current
!= NULL
) {
2976 nfsdir
->current
= nfsdir
->current
->next
;
2985 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2987 nfs_free_nfsdir(nfsdir
);
2994 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3005 struct lseek_cb_data
{
3006 struct nfs_context
*nfs
;
3007 struct nfsfh
*nfsfh
;
3013 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3016 struct lseek_cb_data
*data
= private_data
;
3017 struct nfs_context
*nfs
= data
->nfs
;
3019 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3021 if (status
== RPC_STATUS_ERROR
) {
3022 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3026 if (status
== RPC_STATUS_CANCEL
) {
3027 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3033 if (res
->status
!= NFS3_OK
) {
3034 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3035 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3040 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3041 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3045 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3047 struct lseek_cb_data
*data
;
3048 struct GETATTR3args args
;
3050 if (whence
== SEEK_SET
) {
3051 nfsfh
->offset
= offset
;
3052 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3055 if (whence
== SEEK_CUR
) {
3056 nfsfh
->offset
+= offset
;
3057 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3061 data
= malloc(sizeof(struct lseek_cb_data
));
3063 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3068 data
->nfsfh
= nfsfh
;
3069 data
->offset
= offset
;
3071 data
->private_data
= private_data
;
3073 memset(&args
, 0, sizeof(GETATTR3args
));
3074 args
.object
= nfsfh
->fh
;
3076 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3077 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3090 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3093 struct nfs_cb_data
*data
= private_data
;
3094 struct nfs_context
*nfs
= data
->nfs
;
3095 struct statvfs svfs
;
3097 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3099 if (status
== RPC_STATUS_ERROR
) {
3100 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3101 free_nfs_cb_data(data
);
3104 if (status
== RPC_STATUS_CANCEL
) {
3105 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3106 free_nfs_cb_data(data
);
3111 if (res
->status
!= NFS3_OK
) {
3112 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
));
3113 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3114 free_nfs_cb_data(data
);
3118 svfs
.f_bsize
= 4096;
3119 svfs
.f_frsize
= 4096;
3120 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3121 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3122 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3123 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3124 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3125 #if !defined(ANDROID)
3126 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3129 svfs
.f_namemax
= 256;
3132 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3133 free_nfs_cb_data(data
);
3136 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3140 args
.fsroot
= data
->fh
;
3141 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3142 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3143 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3144 free_nfs_cb_data(data
);
3150 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3152 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3153 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3166 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3169 struct nfs_cb_data
*data
= private_data
;
3170 struct nfs_context
*nfs
= data
->nfs
;
3172 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3174 if (status
== RPC_STATUS_ERROR
) {
3175 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3176 free_nfs_cb_data(data
);
3179 if (status
== RPC_STATUS_CANCEL
) {
3180 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3181 free_nfs_cb_data(data
);
3186 if (res
->status
!= NFS3_OK
) {
3187 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
));
3188 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3189 free_nfs_cb_data(data
);
3194 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3195 free_nfs_cb_data(data
);
3198 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3202 args
.symlink
= data
->fh
;
3204 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3205 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3206 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3207 free_nfs_cb_data(data
);
3213 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3215 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3216 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3229 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3231 struct nfs_cb_data
*data
= private_data
;
3232 struct nfs_context
*nfs
= data
->nfs
;
3235 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3237 if (status
== RPC_STATUS_ERROR
) {
3238 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3239 free_nfs_cb_data(data
);
3242 if (status
== RPC_STATUS_CANCEL
) {
3243 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3244 free_nfs_cb_data(data
);
3249 if (res
->status
!= NFS3_OK
) {
3250 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3251 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3252 free_nfs_cb_data(data
);
3256 data
->cb(0, nfs
, NULL
, data
->private_data
);
3257 free_nfs_cb_data(data
);
3260 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3264 memset(&args
, 0, sizeof(SETATTR3args
));
3265 args
.object
= data
->fh
;
3266 args
.new_attributes
.mode
.set_it
= 1;
3267 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3269 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3270 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3271 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3272 free_nfs_cb_data(data
);
3279 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3281 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3282 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3292 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3294 struct nfs_cb_data
*data
;
3296 data
= malloc(sizeof(struct nfs_cb_data
));
3298 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3301 memset(data
, 0, sizeof(struct nfs_cb_data
));
3304 data
->private_data
= private_data
;
3305 data
->continue_int
= mode
;
3306 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3307 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3308 if (data
->fh
.data
.data_val
== NULL
) {
3309 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3310 free_nfs_cb_data(data
);
3313 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3315 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3327 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3329 struct nfs_cb_data
*data
= private_data
;
3330 struct nfs_context
*nfs
= data
->nfs
;
3333 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3335 if (status
== RPC_STATUS_ERROR
) {
3336 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3337 free_nfs_cb_data(data
);
3340 if (status
== RPC_STATUS_CANCEL
) {
3341 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3342 free_nfs_cb_data(data
);
3347 if (res
->status
!= NFS3_OK
) {
3348 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3349 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3350 free_nfs_cb_data(data
);
3354 data
->cb(0, nfs
, NULL
, data
->private_data
);
3355 free_nfs_cb_data(data
);
3358 struct nfs_chown_data
{
3363 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3366 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3368 memset(&args
, 0, sizeof(SETATTR3args
));
3369 args
.object
= data
->fh
;
3370 if (chown_data
->uid
!= (uid_t
)-1) {
3371 args
.new_attributes
.uid
.set_it
= 1;
3372 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3374 if (chown_data
->gid
!= (gid_t
)-1) {
3375 args
.new_attributes
.gid
.set_it
= 1;
3376 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3379 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3380 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3381 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3382 free_nfs_cb_data(data
);
3389 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3391 struct nfs_chown_data
*chown_data
;
3393 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3394 if (chown_data
== NULL
) {
3395 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3399 chown_data
->uid
= uid
;
3400 chown_data
->gid
= gid
;
3402 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3403 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3414 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3416 struct nfs_cb_data
*data
;
3417 struct nfs_chown_data
*chown_data
;
3419 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3420 if (chown_data
== NULL
) {
3421 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3425 chown_data
->uid
= uid
;
3426 chown_data
->gid
= gid
;
3428 data
= malloc(sizeof(struct nfs_cb_data
));
3430 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3434 memset(data
, 0, sizeof(struct nfs_cb_data
));
3437 data
->private_data
= private_data
;
3438 data
->continue_data
= chown_data
;
3439 data
->free_continue_data
= free
;
3440 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3441 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3442 if (data
->fh
.data
.data_val
== NULL
) {
3443 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3444 free_nfs_cb_data(data
);
3447 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3449 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3463 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3465 struct nfs_cb_data
*data
= private_data
;
3466 struct nfs_context
*nfs
= data
->nfs
;
3469 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3471 if (status
== RPC_STATUS_ERROR
) {
3472 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3473 free_nfs_cb_data(data
);
3476 if (status
== RPC_STATUS_CANCEL
) {
3477 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3478 free_nfs_cb_data(data
);
3483 if (res
->status
!= NFS3_OK
) {
3484 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3485 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3486 free_nfs_cb_data(data
);
3490 data
->cb(0, nfs
, NULL
, data
->private_data
);
3491 free_nfs_cb_data(data
);
3494 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3497 struct timeval
*utimes_data
= data
->continue_data
;
3499 memset(&args
, 0, sizeof(SETATTR3args
));
3500 args
.object
= data
->fh
;
3501 if (utimes_data
!= NULL
) {
3502 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3503 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3504 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3505 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3506 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3507 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3509 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3510 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3513 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3514 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3515 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3516 free_nfs_cb_data(data
);
3523 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3525 struct timeval
*new_times
= NULL
;
3527 if (times
!= NULL
) {
3528 new_times
= malloc(sizeof(struct timeval
)*2);
3529 if (new_times
== NULL
) {
3530 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3534 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3537 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3538 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3548 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3550 struct timeval
*new_times
= NULL
;
3552 if (times
!= NULL
) {
3553 new_times
= malloc(sizeof(struct timeval
)*2);
3554 if (new_times
== NULL
) {
3555 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3559 new_times
[0].tv_sec
= times
->actime
;
3560 new_times
[0].tv_usec
= 0;
3561 new_times
[1].tv_sec
= times
->modtime
;
3562 new_times
[1].tv_usec
= 0;
3565 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3566 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3577 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3580 struct nfs_cb_data
*data
= private_data
;
3581 struct nfs_context
*nfs
= data
->nfs
;
3582 unsigned int nfsmode
= 0;
3584 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3586 if (status
== RPC_STATUS_ERROR
) {
3587 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3588 free_nfs_cb_data(data
);
3591 if (status
== RPC_STATUS_CANCEL
) {
3592 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3593 free_nfs_cb_data(data
);
3598 if (res
->status
!= NFS3_OK
) {
3599 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
));
3600 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3601 free_nfs_cb_data(data
);
3605 if (data
->continue_int
& R_OK
) {
3606 nfsmode
|= ACCESS3_READ
;
3608 if (data
->continue_int
& W_OK
) {
3609 nfsmode
|= ACCESS3_MODIFY
;
3611 if (data
->continue_int
& X_OK
) {
3612 nfsmode
|= ACCESS3_EXECUTE
;
3615 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3616 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3617 nfsmode
&ACCESS3_READ
?'r':'-',
3618 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3619 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3620 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3621 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3622 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3623 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3624 free_nfs_cb_data(data
);
3628 data
->cb(0, nfs
, NULL
, data
->private_data
);
3629 free_nfs_cb_data(data
);
3632 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3637 if (data
->continue_int
& R_OK
) {
3638 nfsmode
|= ACCESS3_READ
;
3640 if (data
->continue_int
& W_OK
) {
3641 nfsmode
|= ACCESS3_MODIFY
;
3643 if (data
->continue_int
& X_OK
) {
3644 nfsmode
|= ACCESS3_EXECUTE
;
3647 memset(&args
, 0, sizeof(ACCESS3args
));
3648 args
.object
= data
->fh
;
3649 args
.access
= nfsmode
;
3651 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3652 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3653 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3654 free_nfs_cb_data(data
);
3660 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3662 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3663 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3675 struct nfs_symlink_data
{
3677 char *newpathparent
;
3678 char *newpathobject
;
3681 static void free_nfs_symlink_data(void *mem
)
3683 struct nfs_symlink_data
*data
= mem
;
3685 if (data
->oldpath
!= NULL
) {
3686 free(data
->oldpath
);
3688 if (data
->newpathparent
!= NULL
) {
3689 free(data
->newpathparent
);
3691 if (data
->newpathobject
!= NULL
) {
3692 free(data
->newpathobject
);
3697 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3700 struct nfs_cb_data
*data
= private_data
;
3701 struct nfs_context
*nfs
= data
->nfs
;
3702 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3704 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3706 if (status
== RPC_STATUS_ERROR
) {
3707 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3708 free_nfs_cb_data(data
);
3711 if (status
== RPC_STATUS_CANCEL
) {
3712 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3713 free_nfs_cb_data(data
);
3718 if (res
->status
!= NFS3_OK
) {
3719 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
));
3720 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3721 free_nfs_cb_data(data
);
3725 data
->cb(0, nfs
, NULL
, data
->private_data
);
3726 free_nfs_cb_data(data
);
3729 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3731 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3734 memset(&args
, 0, sizeof(SYMLINK3args
));
3735 args
.where
.dir
= data
->fh
;
3736 args
.where
.name
= symlink_data
->newpathobject
;
3737 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3738 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
;
3739 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3741 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3742 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3743 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3744 free_nfs_cb_data(data
);
3750 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3753 struct nfs_symlink_data
*symlink_data
;
3755 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3756 if (symlink_data
== NULL
) {
3757 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3760 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3762 symlink_data
->oldpath
= strdup(oldpath
);
3763 if (symlink_data
->oldpath
== NULL
) {
3764 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3765 free_nfs_symlink_data(symlink_data
);
3769 symlink_data
->newpathparent
= strdup(newpath
);
3770 if (symlink_data
->newpathparent
== NULL
) {
3771 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3772 free_nfs_symlink_data(symlink_data
);
3776 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3778 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3779 free_nfs_symlink_data(symlink_data
);
3785 symlink_data
->newpathobject
= strdup(ptr
);
3786 if (symlink_data
->newpathobject
== NULL
) {
3787 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3788 free_nfs_symlink_data(symlink_data
);
3792 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3793 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3805 struct nfs_rename_data
{
3808 struct nfs_fh3 olddir
;
3811 struct nfs_fh3 newdir
;
3814 static void free_nfs_rename_data(void *mem
)
3816 struct nfs_rename_data
*data
= mem
;
3818 if (data
->oldpath
!= NULL
) {
3819 free(data
->oldpath
);
3821 if (data
->olddir
.data
.data_val
!= NULL
) {
3822 free(data
->olddir
.data
.data_val
);
3824 if (data
->newpath
!= NULL
) {
3825 free(data
->newpath
);
3827 if (data
->newdir
.data
.data_val
!= NULL
) {
3828 free(data
->newdir
.data
.data_val
);
3833 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3836 struct nfs_cb_data
*data
= private_data
;
3837 struct nfs_context
*nfs
= data
->nfs
;
3838 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3840 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3842 if (status
== RPC_STATUS_ERROR
) {
3843 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3844 free_nfs_cb_data(data
);
3847 if (status
== RPC_STATUS_CANCEL
) {
3848 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3849 free_nfs_cb_data(data
);
3854 if (res
->status
!= NFS3_OK
) {
3855 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
));
3856 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3857 free_nfs_cb_data(data
);
3861 data
->cb(0, nfs
, NULL
, data
->private_data
);
3862 free_nfs_cb_data(data
);
3865 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3867 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3870 /* steal the filehandle */
3871 rename_data
->newdir
= data
->fh
;
3872 data
->fh
.data
.data_val
= NULL
;
3874 args
.from
.dir
= rename_data
->olddir
;
3875 args
.from
.name
= rename_data
->oldobject
;
3876 args
.to
.dir
= rename_data
->newdir
;
3877 args
.to
.name
= rename_data
->newobject
;
3878 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3879 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3880 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3881 free_nfs_cb_data(data
);
3888 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3890 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3892 /* steal the filehandle */
3893 rename_data
->olddir
= data
->fh
;
3894 data
->fh
.data
.data_val
= NULL
;
3896 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) {
3897 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3898 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3899 free_nfs_cb_data(data
);
3902 data
->continue_data
= NULL
;
3903 free_nfs_cb_data(data
);
3909 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3912 struct nfs_rename_data
*rename_data
;
3914 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3915 if (rename_data
== NULL
) {
3916 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3919 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3921 rename_data
->oldpath
= strdup(oldpath
);
3922 if (rename_data
->oldpath
== NULL
) {
3923 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3924 free_nfs_rename_data(rename_data
);
3927 ptr
= strrchr(rename_data
->oldpath
, '/');
3929 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3930 free_nfs_rename_data(rename_data
);
3935 rename_data
->oldobject
= ptr
;
3938 rename_data
->newpath
= strdup(newpath
);
3939 if (rename_data
->newpath
== NULL
) {
3940 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3941 free_nfs_rename_data(rename_data
);
3944 ptr
= strrchr(rename_data
->newpath
, '/');
3946 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3947 free_nfs_rename_data(rename_data
);
3952 rename_data
->newobject
= ptr
;
3955 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3956 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3967 struct nfs_link_data
{
3969 struct nfs_fh3 oldfh
;
3972 struct nfs_fh3 newdir
;
3975 static void free_nfs_link_data(void *mem
)
3977 struct nfs_link_data
*data
= mem
;
3979 if (data
->oldpath
!= NULL
) {
3980 free(data
->oldpath
);
3982 if (data
->oldfh
.data
.data_val
!= NULL
) {
3983 free(data
->oldfh
.data
.data_val
);
3985 if (data
->newpath
!= NULL
) {
3986 free(data
->newpath
);
3988 if (data
->newdir
.data
.data_val
!= NULL
) {
3989 free(data
->newdir
.data
.data_val
);
3994 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3997 struct nfs_cb_data
*data
= private_data
;
3998 struct nfs_context
*nfs
= data
->nfs
;
3999 struct nfs_link_data
*link_data
= data
->continue_data
;
4001 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4003 if (status
== RPC_STATUS_ERROR
) {
4004 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4005 free_nfs_cb_data(data
);
4008 if (status
== RPC_STATUS_CANCEL
) {
4009 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4010 free_nfs_cb_data(data
);
4015 if (res
->status
!= NFS3_OK
) {
4016 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
));
4017 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4018 free_nfs_cb_data(data
);
4022 data
->cb(0, nfs
, NULL
, data
->private_data
);
4023 free_nfs_cb_data(data
);
4026 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4028 struct nfs_link_data
*link_data
= data
->continue_data
;
4031 /* steal the filehandle */
4032 link_data
->newdir
= data
->fh
;
4033 data
->fh
.data
.data_val
= NULL
;
4035 memset(&args
, 0, sizeof(LINK3args
));
4036 args
.file
= link_data
->oldfh
;
4037 args
.link
.dir
= link_data
->newdir
;
4038 args
.link
.name
= link_data
->newobject
;
4039 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4040 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4041 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4042 free_nfs_cb_data(data
);
4049 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4051 struct nfs_link_data
*link_data
= data
->continue_data
;
4053 /* steal the filehandle */
4054 link_data
->oldfh
= data
->fh
;
4055 data
->fh
.data
.data_val
= NULL
;
4057 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) {
4058 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4059 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4060 free_nfs_cb_data(data
);
4063 data
->continue_data
= NULL
;
4064 free_nfs_cb_data(data
);
4070 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4073 struct nfs_link_data
*link_data
;
4075 link_data
= malloc(sizeof(struct nfs_link_data
));
4076 if (link_data
== NULL
) {
4077 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4080 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4082 link_data
->oldpath
= strdup(oldpath
);
4083 if (link_data
->oldpath
== NULL
) {
4084 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4085 free_nfs_link_data(link_data
);
4089 link_data
->newpath
= strdup(newpath
);
4090 if (link_data
->newpath
== NULL
) {
4091 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4092 free_nfs_link_data(link_data
);
4095 ptr
= strrchr(link_data
->newpath
, '/');
4097 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4098 free_nfs_link_data(link_data
);
4103 link_data
->newobject
= ptr
;
4106 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4107 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4115 //qqq replace later with lseek()
4116 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4118 return nfsfh
->offset
;
4124 * Get the maximum supported READ3 size by the server
4126 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4128 return nfs
->readmax
;
4132 * Get the maximum supported WRITE3 size by the server
4134 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4136 return nfs
->writemax
;
4139 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4140 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4143 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4144 rpc_set_uid(nfs
->rpc
, uid
);
4147 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4148 rpc_set_gid(nfs
->rpc
, gid
);
4151 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4156 va_start(ap
, error_string
);
4158 vsnprintf(str
, 1024, error_string
, ap
);
4159 if (nfs
->rpc
->error_string
!= NULL
) {
4160 free(nfs
->rpc
->error_string
);
4162 nfs
->rpc
->error_string
= str
;
4168 struct mount_cb_data
{
4174 static void free_mount_cb_data(struct mount_cb_data
*data
)
4176 if (data
->server
!= NULL
) {
4178 data
->server
= NULL
;
4184 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4186 struct mount_cb_data
*data
= private_data
;
4188 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4190 if (status
== RPC_STATUS_ERROR
) {
4191 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4192 free_mount_cb_data(data
);
4195 if (status
== RPC_STATUS_CANCEL
) {
4196 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4197 free_mount_cb_data(data
);
4201 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4202 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4203 rpc_set_error(rpc
, "Failed to disconnect\n");
4205 free_mount_cb_data(data
);
4208 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4210 struct mount_cb_data
*data
= private_data
;
4212 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4214 /* Dont want any more callbacks even if the socket is closed */
4215 rpc
->connect_cb
= NULL
;
4217 if (status
== RPC_STATUS_ERROR
) {
4218 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4219 free_mount_cb_data(data
);
4222 if (status
== RPC_STATUS_CANCEL
) {
4223 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4224 free_mount_cb_data(data
);
4228 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4229 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4230 free_mount_cb_data(data
);
4235 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4237 struct mount_cb_data
*data
= private_data
;
4238 uint32_t mount_port
;
4240 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4242 if (status
== RPC_STATUS_ERROR
) {
4243 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4244 free_mount_cb_data(data
);
4247 if (status
== RPC_STATUS_CANCEL
) {
4248 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4249 free_mount_cb_data(data
);
4253 mount_port
= *(uint32_t *)command_data
;
4254 if (mount_port
== 0) {
4255 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4256 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4257 free_mount_cb_data(data
);
4261 rpc_disconnect(rpc
, "normal disconnect");
4262 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4263 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4264 free_mount_cb_data(data
);
4269 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4271 struct mount_cb_data
*data
= private_data
;
4273 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4275 if (status
== RPC_STATUS_ERROR
) {
4276 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4277 free_mount_cb_data(data
);
4280 if (status
== RPC_STATUS_CANCEL
) {
4281 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4282 free_mount_cb_data(data
);
4286 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4287 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4288 free_mount_cb_data(data
);
4293 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4295 struct mount_cb_data
*data
= private_data
;
4297 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4299 /* Dont want any more callbacks even if the socket is closed */
4300 rpc
->connect_cb
= NULL
;
4302 if (status
== RPC_STATUS_ERROR
) {
4303 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4304 free_mount_cb_data(data
);
4307 if (status
== RPC_STATUS_CANCEL
) {
4308 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4309 free_mount_cb_data(data
);
4313 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4314 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4315 free_mount_cb_data(data
);
4320 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4322 struct mount_cb_data
*data
;
4324 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4326 data
= malloc(sizeof(struct mount_cb_data
));
4330 memset(data
, 0, sizeof(struct mount_cb_data
));
4332 data
->private_data
= private_data
;
4333 data
->server
= strdup(server
);
4334 if (data
->server
== NULL
) {
4335 free_mount_cb_data(data
);
4338 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4339 free_mount_cb_data(data
);
4346 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4348 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4352 const char *nfs_get_server(struct nfs_context
*nfs
) {
4356 const char *nfs_get_export(struct nfs_context
*nfs
) {
4360 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4361 return &nfs
->rootfh
;
4364 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {