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-raw-portmap.h"
77 #include "libnfs-private.h"
80 struct nfsdirent
*entries
;
81 struct nfsdirent
*current
;
91 struct rpc_context
*rpc
;
94 struct nfs_fh3 rootfh
;
100 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
102 while (nfsdir
->entries
) {
103 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
104 if (nfsdir
->entries
->name
!= NULL
) {
105 free(nfsdir
->entries
->name
);
107 free(nfsdir
->entries
);
108 nfsdir
->entries
= dirent
;
114 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
117 struct nfs_context
*nfs
;
119 char *saved_path
, *path
;
124 continue_func continue_cb
;
126 void (*free_continue_data
)(void *);
131 /* for multi-read/write calls. */
136 uint64_t start_offset
, max_offset
;
142 struct nfs_mcb_data
{
143 struct nfs_cb_data
*data
;
149 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
151 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
153 rpc_set_auth(nfs
->rpc
, auth
);
156 int nfs_get_fd(struct nfs_context
*nfs
)
158 return rpc_get_fd(nfs
->rpc
);
161 int nfs_queue_length(struct nfs_context
*nfs
)
163 return rpc_queue_length(nfs
->rpc
);
166 int nfs_which_events(struct nfs_context
*nfs
)
168 return rpc_which_events(nfs
->rpc
);
171 int nfs_service(struct nfs_context
*nfs
, int revents
)
173 return rpc_service(nfs
->rpc
, revents
);
176 char *nfs_get_error(struct nfs_context
*nfs
)
178 return rpc_get_error(nfs
->rpc
);
181 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
183 if (!strncmp(arg
, "tcp-syncnt", 10)) {
184 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
185 } else if (!strncmp(arg
, "uid", 3)) {
186 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
187 } else if (!strncmp(arg
, "gid", 3)) {
188 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
193 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
195 struct nfs_url
*urls
;
196 char *strp
, *flagsp
, *strp2
;
198 if (strncmp(url
, "nfs://", 6)) {
199 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
203 urls
= malloc(sizeof(struct nfs_url
));
205 rpc_set_error(nfs
->rpc
, "Out of memory");
209 memset(urls
, 0x00, sizeof(struct nfs_url
));
210 urls
->server
= strdup(url
+ 6);
211 if (urls
->server
== NULL
) {
212 nfs_destroy_url(urls
);
213 rpc_set_error(nfs
->rpc
, "Out of memory");
217 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
218 urls
->server
[0] == '?') {
220 flagsp
= strchr(urls
->server
, '?');
223 nfs_destroy_url(urls
);
224 rpc_set_error(nfs
->rpc
, "Invalid server string");
228 strp
= strchr(urls
->server
, '/');
231 flagsp
= strchr(urls
->server
, '?');
234 nfs_destroy_url(urls
);
235 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
239 urls
->path
= strdup(strp
);
240 if (urls
->path
== NULL
) {
241 nfs_destroy_url(urls
);
242 rpc_set_error(nfs
->rpc
, "Out of memory");
248 flagsp
= strchr(urls
->path
, '?');
252 strp
= strrchr(urls
->path
, '/');
255 flagsp
= strchr(urls
->path
, '?');
258 nfs_destroy_url(urls
);
259 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
262 urls
->file
= strdup(strp
);
263 if (urls
->path
== NULL
) {
264 nfs_destroy_url(urls
);
265 rpc_set_error(nfs
->rpc
, "Out of memory");
269 flagsp
= strchr(urls
->file
, '?');
276 if (urls
->file
&& !strlen(urls
->file
)) {
280 nfs_destroy_url(urls
);
281 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
286 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
288 flagsp
= strchr(strp
, '&');
292 strp2
= strchr(strp
, '=');
296 nfs_set_context_args(nfs
, strp
, strp2
);
300 if (urls
->server
&& strlen(urls
->server
) <= 1) {
308 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
310 return nfs_parse_url(nfs
, url
, 0, 0);
313 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
315 return nfs_parse_url(nfs
, url
, 1, 0);
318 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
320 return nfs_parse_url(nfs
, url
, 0, 1);
324 void nfs_destroy_url(struct nfs_url
*url
)
334 struct nfs_context
*nfs_init_context(void)
336 struct nfs_context
*nfs
;
338 nfs
= malloc(sizeof(struct nfs_context
));
342 memset(nfs
, 0, sizeof(struct nfs_context
));
344 nfs
->rpc
= rpc_init_context();
345 if (nfs
->rpc
== NULL
) {
350 nfs
->cwd
= strdup("/");
355 void nfs_destroy_context(struct nfs_context
*nfs
)
357 rpc_destroy_context(nfs
->rpc
);
375 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
376 free(nfs
->rootfh
.data
.data_val
);
377 nfs
->rootfh
.data
.data_val
= NULL
;
392 void free_rpc_cb_data(struct rpc_cb_data
*data
)
399 static void rpc_connect_program_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
401 struct rpc_cb_data
*data
= private_data
;
403 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
405 /* Dont want any more callbacks even if the socket is closed */
406 rpc
->connect_cb
= NULL
;
408 if (status
== RPC_STATUS_ERROR
) {
409 data
->cb(rpc
, status
, command_data
, data
->private_data
);
410 free_rpc_cb_data(data
);
413 if (status
== RPC_STATUS_CANCEL
) {
414 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
415 free_rpc_cb_data(data
);
419 data
->cb(rpc
, status
, NULL
, data
->private_data
);
420 free_rpc_cb_data(data
);
423 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
425 struct rpc_cb_data
*data
= private_data
;
427 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
429 /* Dont want any more callbacks even if the socket is closed */
430 rpc
->connect_cb
= NULL
;
432 if (status
== RPC_STATUS_ERROR
) {
433 data
->cb(rpc
, status
, command_data
, data
->private_data
);
434 free_rpc_cb_data(data
);
437 if (status
== RPC_STATUS_CANCEL
) {
438 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
439 free_rpc_cb_data(data
);
443 switch (data
->program
) {
445 if (rpc_mount3_null_async(rpc
, rpc_connect_program_5_cb
,
447 data
->cb(rpc
, status
, command_data
, data
->private_data
);
448 free_rpc_cb_data(data
);
453 if (rpc_nfs3_null_async(rpc
, rpc_connect_program_5_cb
,
455 data
->cb(rpc
, status
, command_data
, data
->private_data
);
456 free_rpc_cb_data(data
);
462 data
->cb(rpc
, status
, NULL
, data
->private_data
);
463 free_rpc_cb_data(data
);
466 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
468 struct rpc_cb_data
*data
= private_data
;
469 struct pmap3_string_result
*gar
;
470 uint32_t rpc_port
= 0;
473 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
475 if (status
== RPC_STATUS_ERROR
) {
476 data
->cb(rpc
, status
, command_data
, data
->private_data
);
477 free_rpc_cb_data(data
);
480 if (status
== RPC_STATUS_CANCEL
) {
481 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
482 free_rpc_cb_data(data
);
486 switch (rpc
->s
.ss_family
) {
488 rpc_port
= *(uint32_t *)command_data
;
491 /* ouch. portmapper and ipv6 are not great */
493 if (gar
->addr
== NULL
) {
496 ptr
= strrchr(gar
->addr
, '.');
500 rpc_port
= atoi(ptr
+ 1);
502 ptr
= strrchr(gar
->addr
, '.');
506 rpc_port
+= 256 * atoi(ptr
+ 1);
510 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
511 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
512 free_rpc_cb_data(data
);
516 rpc_disconnect(rpc
, "normal disconnect");
517 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
518 data
->cb(rpc
, status
, command_data
, data
->private_data
);
519 free_rpc_cb_data(data
);
524 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
526 struct rpc_cb_data
*data
= private_data
;
527 struct pmap3_mapping map
;
529 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
531 if (status
== RPC_STATUS_ERROR
) {
532 data
->cb(rpc
, status
, command_data
, data
->private_data
);
533 free_rpc_cb_data(data
);
536 if (status
== RPC_STATUS_CANCEL
) {
537 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
538 free_rpc_cb_data(data
);
542 switch (rpc
->s
.ss_family
) {
544 if (rpc_pmap2_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
545 data
->cb(rpc
, status
, command_data
, data
->private_data
);
546 free_rpc_cb_data(data
);
551 map
.prog
=data
->program
;
552 map
.vers
=data
->version
;
556 if (rpc_pmap3_getaddr_async(rpc
, &map
, rpc_connect_program_3_cb
, private_data
) != 0) {
557 data
->cb(rpc
, status
, command_data
, data
->private_data
);
558 free_rpc_cb_data(data
);
565 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
567 struct rpc_cb_data
*data
= private_data
;
569 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
571 /* Dont want any more callbacks even if the socket is closed */
572 rpc
->connect_cb
= NULL
;
574 if (status
== RPC_STATUS_ERROR
) {
575 data
->cb(rpc
, status
, command_data
, data
->private_data
);
576 free_rpc_cb_data(data
);
579 if (status
== RPC_STATUS_CANCEL
) {
580 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
581 free_rpc_cb_data(data
);
585 switch (rpc
->s
.ss_family
) {
587 if (rpc_pmap2_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
588 data
->cb(rpc
, status
, command_data
, data
->private_data
);
589 free_rpc_cb_data(data
);
594 if (rpc_pmap3_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
595 data
->cb(rpc
, status
, command_data
, data
->private_data
);
596 free_rpc_cb_data(data
);
603 int rpc_connect_program_async(struct rpc_context
*rpc
, const char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
605 struct rpc_cb_data
*data
;
607 data
= malloc(sizeof(struct rpc_cb_data
));
611 memset(data
, 0, sizeof(struct rpc_cb_data
));
612 data
->server
= strdup(server
);
613 data
->program
= program
;
614 data
->version
= version
;
617 data
->private_data
= private_data
;
619 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
620 rpc_set_error(rpc
, "Failed to start connection");
621 free_rpc_cb_data(data
);
627 static void free_nfs_cb_data(struct nfs_cb_data
*data
)
629 if (data
->continue_data
!= NULL
) {
630 assert(data
->free_continue_data
);
631 data
->free_continue_data(data
->continue_data
);
634 free(data
->saved_path
);
635 free(data
->fh
.data
.data_val
);
645 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
647 struct nfs_cb_data
*data
= private_data
;
648 struct nfs_context
*nfs
= data
->nfs
;
650 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
652 if (status
== RPC_STATUS_ERROR
) {
653 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
654 free_nfs_cb_data(data
);
657 if (status
== RPC_STATUS_CANCEL
) {
658 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
659 free_nfs_cb_data(data
);
663 data
->cb(0, nfs
, NULL
, data
->private_data
);
664 free_nfs_cb_data(data
);
667 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
669 struct nfs_cb_data
*data
= private_data
;
670 struct nfs_context
*nfs
= data
->nfs
;
671 FSINFO3res
*res
= command_data
;
672 struct GETATTR3args args
;
674 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
676 if (status
== RPC_STATUS_ERROR
) {
677 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
678 free_nfs_cb_data(data
);
681 if (status
== RPC_STATUS_CANCEL
) {
682 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
683 free_nfs_cb_data(data
);
687 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
688 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
690 memset(&args
, 0, sizeof(GETATTR3args
));
691 args
.object
= nfs
->rootfh
;
693 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
694 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
695 free_nfs_cb_data(data
);
700 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
702 struct nfs_cb_data
*data
= private_data
;
703 struct nfs_context
*nfs
= data
->nfs
;
704 struct FSINFO3args args
;
706 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
708 if (status
== RPC_STATUS_ERROR
) {
709 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
710 free_nfs_cb_data(data
);
713 if (status
== RPC_STATUS_CANCEL
) {
714 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
715 free_nfs_cb_data(data
);
719 args
.fsroot
= nfs
->rootfh
;
720 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
721 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
722 free_nfs_cb_data(data
);
727 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
729 struct nfs_cb_data
*data
= private_data
;
730 struct nfs_context
*nfs
= data
->nfs
;
733 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
735 if (status
== RPC_STATUS_ERROR
) {
736 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
737 free_nfs_cb_data(data
);
740 if (status
== RPC_STATUS_CANCEL
) {
741 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
742 free_nfs_cb_data(data
);
747 if (res
->fhs_status
!= MNT3_OK
) {
748 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
));
749 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
750 free_nfs_cb_data(data
);
754 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
755 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
756 if (nfs
->rootfh
.data
.data_val
== NULL
) {
757 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
758 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
759 free_nfs_cb_data(data
);
762 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
764 rpc_disconnect(rpc
, "normal disconnect");
766 if (rpc_connect_program_async(nfs
->rpc
, nfs
->server
, NFS_PROGRAM
, NFS_V3
, nfs_mount_8_cb
, data
) != 0) {
767 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
768 free_nfs_cb_data(data
);
772 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
773 rpc_set_autoreconnect(rpc
);
777 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
779 struct nfs_cb_data
*data
= private_data
;
780 struct nfs_context
*nfs
= data
->nfs
;
782 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
784 if (status
== RPC_STATUS_ERROR
) {
785 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
786 free_nfs_cb_data(data
);
789 if (status
== RPC_STATUS_CANCEL
) {
790 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
791 free_nfs_cb_data(data
);
795 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
796 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
797 free_nfs_cb_data(data
);
803 * Async call for mounting an nfs share and geting the root filehandle
805 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
807 struct nfs_cb_data
*data
;
808 char *new_server
, *new_export
;
810 data
= malloc(sizeof(struct nfs_cb_data
));
812 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
815 memset(data
, 0, sizeof(struct nfs_cb_data
));
816 new_server
= strdup(server
);
817 new_export
= strdup(export
);
818 if (nfs
->server
!= NULL
) {
821 nfs
->server
= new_server
;
822 if (nfs
->export
!= NULL
) {
825 nfs
->export
= new_export
;
828 data
->private_data
= private_data
;
830 if (rpc_connect_program_async(nfs
->rpc
, server
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_5_cb
, data
) != 0) {
831 rpc_set_error(nfs
->rpc
, "Failed to start connection");
832 free_nfs_cb_data(data
);
842 * Functions to first look up a path, component by component, and then finally call a specific function once
843 * the filehandle for the final component is found.
845 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
847 struct nfs_cb_data
*data
= private_data
;
848 struct nfs_context
*nfs
= data
->nfs
;
851 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
853 if (status
== RPC_STATUS_ERROR
) {
854 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
855 free_nfs_cb_data(data
);
858 if (status
== RPC_STATUS_CANCEL
) {
859 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
860 free_nfs_cb_data(data
);
865 if (res
->status
!= NFS3_OK
) {
866 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
));
867 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
868 free_nfs_cb_data(data
);
872 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
873 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
874 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
875 free_nfs_cb_data(data
);
880 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
885 while (*data
->path
== '/') {
890 slash
= strchr(path
, '/');
892 /* Clear slash so that path is a zero terminated string for
893 * the current path component. Set it back to '/' again later
894 * when we are finished referencing this component so that
895 * data->saved_path will still point to the full
899 data
->path
= slash
+1;
901 while (*data
->path
!= 0) {
907 data
->fh
.data
.data_len
= fh
->data
.data_len
;
908 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
909 if (data
->fh
.data
.data_val
== NULL
) {
910 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
911 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
912 free_nfs_cb_data(data
);
915 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
919 data
->continue_cb(nfs
, data
);
924 memset(&args
, 0, sizeof(LOOKUP3args
));
926 args
.what
.name
= path
;
928 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
929 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
930 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
931 free_nfs_cb_data(data
);
940 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
946 while ((str
= strstr(path
, "//"))) {
954 while ((str
= strstr(path
, "/./"))) {
962 if (!strncmp(path
, "/../", 4)) {
963 rpc_set_error(nfs
->rpc
,
964 "Absolute path starts with '/../' "
965 "during normalization");
970 if (path
[0] != '/') {
971 rpc_set_error(nfs
->rpc
,
972 "Absolute path does not start with '/'");
976 /* /string/../ -> / */
977 while ((str
= strstr(path
, "/../"))) {
980 if (!strncmp(path
, "/../", 4)) {
981 rpc_set_error(nfs
->rpc
,
982 "Absolute path starts with '/../' "
983 "during normalization");
988 while (*tmp
!= '/') {
992 while((*(tmp
++) = *(str
++)) != '\0')
999 if (path
[len
- 1] == '/') {
1000 path
[len
- 1] = '\0';
1004 if (path
[0] == '\0') {
1005 rpc_set_error(nfs
->rpc
,
1006 "Absolute path became '' "
1007 "during normalization");
1013 if (!strcmp(&path
[len
- 2], "/.")) {
1014 path
[len
- 2] = '\0';
1019 /* ^/..$ -> error */
1020 if (!strcmp(path
, "/..")) {
1021 rpc_set_error(nfs
->rpc
,
1022 "Absolute path is '/..' "
1023 "during normalization");
1027 /* /string/..$ -> / */
1029 if (!strcmp(&path
[len
- 3], "/..")) {
1030 char *tmp
= &path
[len
- 3];
1031 while (*--tmp
!= '/')
1040 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
)
1042 struct nfs_cb_data
*data
;
1044 if (path
[0] == '\0') {
1048 data
= malloc(sizeof(struct nfs_cb_data
));
1050 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1051 "nfs_cb_data structure");
1052 if (free_continue_data
)
1053 free_continue_data(continue_data
);
1056 memset(data
, 0, sizeof(struct nfs_cb_data
));
1059 data
->continue_cb
= continue_cb
;
1060 data
->continue_data
= continue_data
;
1061 data
->free_continue_data
= free_continue_data
;
1062 data
->continue_int
= continue_int
;
1063 data
->private_data
= private_data
;
1064 if (path
[0] == '/') {
1065 data
->saved_path
= strdup(path
);
1067 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1068 if (data
->saved_path
== NULL
) {
1069 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1070 "malloc path string");
1071 free_nfs_cb_data(data
);
1074 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1077 if (data
->saved_path
== NULL
) {
1078 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1079 free_nfs_cb_data(data
);
1082 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1083 free_nfs_cb_data(data
);
1087 data
->path
= data
->saved_path
;
1089 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1090 /* return 0 here since the callback will be invoked if there is a failure */
1103 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1106 struct nfs_cb_data
*data
= private_data
;
1107 struct nfs_context
*nfs
= data
->nfs
;
1114 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1116 if (status
== RPC_STATUS_ERROR
) {
1117 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1118 free_nfs_cb_data(data
);
1121 if (status
== RPC_STATUS_CANCEL
) {
1122 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1123 free_nfs_cb_data(data
);
1128 if (res
->status
!= NFS3_OK
) {
1129 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
));
1130 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1131 free_nfs_cb_data(data
);
1136 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1137 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1138 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1139 st
.st_mode
|= S_IFDIR
;
1141 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1142 st
.st_mode
|= S_IFREG
;
1144 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1145 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1146 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1148 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1150 st
.st_blksize
= 4096;
1151 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1153 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1154 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1155 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1157 data
->cb(0, nfs
, &st
, data
->private_data
);
1158 free_nfs_cb_data(data
);
1161 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1163 struct GETATTR3args args
;
1165 memset(&args
, 0, sizeof(GETATTR3args
));
1166 args
.object
= data
->fh
;
1168 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1169 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1170 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1171 free_nfs_cb_data(data
);
1177 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1179 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1180 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1189 * Async nfs_stat64()
1191 static void nfs_stat64_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1194 struct nfs_cb_data
*data
= private_data
;
1195 struct nfs_context
*nfs
= data
->nfs
;
1196 struct nfs_stat_64 st
;
1198 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1200 if (status
== RPC_STATUS_ERROR
) {
1201 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1202 free_nfs_cb_data(data
);
1205 if (status
== RPC_STATUS_CANCEL
) {
1206 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1207 free_nfs_cb_data(data
);
1212 if (res
->status
!= NFS3_OK
) {
1213 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
));
1214 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1215 free_nfs_cb_data(data
);
1220 st
.nfs_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1221 st
.nfs_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1222 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1223 st
.nfs_mode
|= S_IFDIR
;
1225 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1226 st
.nfs_mode
|= S_IFREG
;
1228 st
.nfs_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1229 st
.nfs_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1230 st
.nfs_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1232 st
.nfs_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1233 st
.nfs_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1234 st
.nfs_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1235 st
.nfs_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1237 data
->cb(0, nfs
, &st
, data
->private_data
);
1238 free_nfs_cb_data(data
);
1241 static int nfs_stat64_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1243 struct GETATTR3args args
;
1245 memset(&args
, 0, sizeof(GETATTR3args
));
1246 args
.object
= data
->fh
;
1248 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
1249 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1250 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1251 free_nfs_cb_data(data
);
1257 int nfs_stat64_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1259 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat64_continue_internal
, NULL
, NULL
, 0) != 0) {
1260 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1270 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1272 struct nfs_cb_data
*data
= private_data
;
1273 struct nfs_context
*nfs
= data
->nfs
;
1274 struct nfsfh
*nfsfh
;
1277 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1279 if (status
== RPC_STATUS_ERROR
) {
1280 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1281 free_nfs_cb_data(data
);
1284 if (status
== RPC_STATUS_CANCEL
) {
1285 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1286 free_nfs_cb_data(data
);
1291 if (res
->status
!= NFS3_OK
) {
1292 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1293 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1294 free_nfs_cb_data(data
);
1298 nfsfh
= malloc(sizeof(struct nfsfh
));
1299 if (nfsfh
== NULL
) {
1300 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1301 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1302 free_nfs_cb_data(data
);
1305 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1307 if (data
->continue_int
& O_SYNC
) {
1311 /* steal the filehandle */
1312 nfsfh
->fh
= data
->fh
;
1313 data
->fh
.data
.data_val
= NULL
;
1315 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1316 free_nfs_cb_data(data
);
1319 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1322 struct nfs_cb_data
*data
= private_data
;
1323 struct nfs_context
*nfs
= data
->nfs
;
1324 struct nfsfh
*nfsfh
;
1325 unsigned int nfsmode
= 0;
1327 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1329 if (status
== RPC_STATUS_ERROR
) {
1330 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1331 free_nfs_cb_data(data
);
1334 if (status
== RPC_STATUS_CANCEL
) {
1335 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1336 free_nfs_cb_data(data
);
1341 if (res
->status
!= NFS3_OK
) {
1342 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
));
1343 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1344 free_nfs_cb_data(data
);
1348 if (data
->continue_int
& O_WRONLY
) {
1349 nfsmode
|= ACCESS3_MODIFY
;
1351 if (data
->continue_int
& O_RDWR
) {
1352 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1354 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1355 nfsmode
|= ACCESS3_READ
;
1359 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1360 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1361 nfsmode
&ACCESS3_READ
?'r':'-',
1362 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1363 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1364 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1365 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1366 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1367 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1368 free_nfs_cb_data(data
);
1372 /* Try to truncate it if we were requested to */
1373 if ((data
->continue_int
& O_TRUNC
) &&
1374 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1377 memset(&args
, 0, sizeof(SETATTR3args
));
1378 args
.object
= data
->fh
;
1379 args
.new_attributes
.size
.set_it
= 1;
1380 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1382 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1384 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1385 "SETATTR call for %s", data
->path
);
1386 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1387 data
->private_data
);
1388 free_nfs_cb_data(data
);
1394 nfsfh
= malloc(sizeof(struct nfsfh
));
1395 if (nfsfh
== NULL
) {
1396 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1397 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1398 free_nfs_cb_data(data
);
1401 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1403 if (data
->continue_int
& O_SYNC
) {
1407 /* steal the filehandle */
1408 nfsfh
->fh
= data
->fh
;
1409 data
->fh
.data
.data_val
= NULL
;
1411 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1412 free_nfs_cb_data(data
);
1415 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1420 if (data
->continue_int
& O_WRONLY
) {
1421 nfsmode
|= ACCESS3_MODIFY
;
1423 if (data
->continue_int
& O_RDWR
) {
1424 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1426 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1427 nfsmode
|= ACCESS3_READ
;
1430 memset(&args
, 0, sizeof(ACCESS3args
));
1431 args
.object
= data
->fh
;
1432 args
.access
= nfsmode
;
1434 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1435 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1436 "call for %s", data
->path
);
1437 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1438 data
->private_data
);
1439 free_nfs_cb_data(data
);
1445 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1447 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1448 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1459 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1461 /* steal saved_path */
1463 nfs
->cwd
= data
->saved_path
;
1464 data
->saved_path
= NULL
;
1466 data
->cb(0, nfs
, NULL
, data
->private_data
);
1467 free_nfs_cb_data(data
);
1472 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1474 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1475 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1486 static void nfs_fill_READ3args(READ3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
)
1488 memset(args
, 0, sizeof(READ3args
));
1489 args
->file
= fh
->fh
;
1490 args
->offset
= offset
;
1491 args
->count
= count
;
1494 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1496 struct nfs_mcb_data
*mdata
= private_data
;
1497 struct nfs_cb_data
*data
= mdata
->data
;
1498 struct nfs_context
*nfs
= data
->nfs
;
1501 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1505 if (status
== RPC_STATUS_ERROR
) {
1506 /* flag the failure but do not invoke callback until we have received all responses */
1509 if (status
== RPC_STATUS_CANCEL
) {
1510 /* flag the cancellation but do not invoke callback until we have received all responses */
1514 if (status
== RPC_STATUS_SUCCESS
) {
1516 if (res
->status
!= NFS3_OK
) {
1517 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1520 uint64_t count
= res
->READ3res_u
.resok
.count
;
1522 if (mdata
->update_pos
)
1523 data
->nfsfh
->offset
+= count
;
1525 /* if we have more than one call or we have received a short read we need a reassembly buffer */
1526 if (data
->num_calls
|| (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
)) {
1527 if (data
->buffer
== NULL
) {
1528 data
->buffer
= malloc(data
->request_size
);
1529 if (data
->buffer
== NULL
) {
1530 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data
->request_size
);
1536 if (count
<= mdata
->count
) {
1537 /* copy data into reassembly buffer if we have one */
1538 if (data
->buffer
!= NULL
) {
1539 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, count
);
1541 if (data
->max_offset
< mdata
->offset
+ count
) {
1542 data
->max_offset
= mdata
->offset
+ count
;
1545 rpc_set_error(nfs
->rpc
, "NFS: Read overflow. Server has sent more data than requested!");
1549 /* check if we have received a short read */
1550 if (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
) {
1552 rpc_set_error(nfs
->rpc
, "NFS: Read failed. No bytes read and not at EOF!");
1555 /* reissue reminder of this read request */
1557 mdata
->offset
+= count
;
1558 mdata
->count
-= count
;
1559 nfs_fill_READ3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
);
1560 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) == 0) {
1564 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1574 if (data
->num_calls
> 0) {
1575 /* still waiting for more replies */
1578 if (data
->oom
!= 0) {
1579 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1580 free_nfs_cb_data(data
);
1583 if (data
->error
!= 0) {
1584 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1585 free_nfs_cb_data(data
);
1588 if (data
->cancel
!= 0) {
1589 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1590 free_nfs_cb_data(data
);
1595 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1597 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1600 free_nfs_cb_data(data
);
1603 static int nfs_pread_async_internal(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
, int update_pos
)
1605 struct nfs_cb_data
*data
;
1607 data
= malloc(sizeof(struct nfs_cb_data
));
1609 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1612 memset(data
, 0, sizeof(struct nfs_cb_data
));
1615 data
->private_data
= private_data
;
1616 data
->nfsfh
= nfsfh
;
1617 data
->request_size
= count
;
1619 assert(data
->num_calls
== 0);
1621 /* chop requests into chunks of at most READMAX bytes if necessary.
1622 * we send all reads in parallel so that performance is still good.
1624 data
->max_offset
= offset
;
1625 data
->start_offset
= offset
;
1628 uint64_t readcount
= count
;
1629 struct nfs_mcb_data
*mdata
;
1632 if (readcount
> nfs_get_readmax(nfs
)) {
1633 readcount
= nfs_get_readmax(nfs
);
1636 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1637 if (mdata
== NULL
) {
1638 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1639 if (data
->num_calls
== 0) {
1640 free_nfs_cb_data(data
);
1646 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1648 mdata
->offset
= offset
;
1649 mdata
->count
= readcount
;
1650 mdata
->update_pos
= update_pos
;
1652 nfs_fill_READ3args(&args
, nfsfh
, offset
, readcount
);
1654 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1655 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1657 if (data
->num_calls
== 0) {
1658 free_nfs_cb_data(data
);
1666 offset
+= readcount
;
1668 } while (count
> 0);
1673 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1675 return nfs_pread_async_internal(nfs
, nfsfh
, offset
, count
, cb
, private_data
, 0);
1681 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1683 return nfs_pread_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
, 1);
1691 static void nfs_fill_WRITE3args (WRITE3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
,
1694 memset(args
, 0, sizeof(WRITE3args
));
1695 args
->file
= fh
->fh
;
1696 args
->offset
= offset
;
1697 args
->count
= count
;
1698 args
->stable
= fh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1699 args
->data
.data_len
= count
;
1700 args
->data
.data_val
= buf
;
1703 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1705 struct nfs_mcb_data
*mdata
= private_data
;
1706 struct nfs_cb_data
*data
= mdata
->data
;
1707 struct nfs_context
*nfs
= data
->nfs
;
1710 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1714 if (status
== RPC_STATUS_ERROR
) {
1715 /* flag the failure but do not invoke callback until we have received all responses */
1718 if (status
== RPC_STATUS_CANCEL
) {
1719 /* flag the cancellation but do not invoke callback until we have received all responses */
1723 if (status
== RPC_STATUS_SUCCESS
) {
1725 if (res
->status
!= NFS3_OK
) {
1726 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1729 uint64_t count
= res
->WRITE3res_u
.resok
.count
;
1731 if (mdata
->update_pos
)
1732 data
->nfsfh
->offset
+= count
;
1734 if (count
< mdata
->count
) {
1736 rpc_set_error(nfs
->rpc
, "NFS: Write failed. No bytes written!");
1739 /* reissue reminder of this write request */
1741 mdata
->offset
+= count
;
1742 mdata
->count
-= count
;
1744 nfs_fill_WRITE3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
,
1745 &data
->usrbuf
[mdata
->offset
- data
->start_offset
]);
1746 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) == 0) {
1750 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1756 if (data
->max_offset
< mdata
->offset
+ count
) {
1757 data
->max_offset
= mdata
->offset
+ count
;
1765 if (data
->num_calls
> 0) {
1766 /* still waiting for more replies */
1769 if (data
->oom
!= 0) {
1770 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1771 free_nfs_cb_data(data
);
1774 if (data
->error
!= 0) {
1775 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1776 free_nfs_cb_data(data
);
1779 if (data
->cancel
!= 0) {
1780 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1781 free_nfs_cb_data(data
);
1785 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1787 free_nfs_cb_data(data
);
1791 static int nfs_pwrite_async_internal(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
, int update_pos
)
1793 struct nfs_cb_data
*data
;
1795 data
= malloc(sizeof(struct nfs_cb_data
));
1797 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1800 memset(data
, 0, sizeof(struct nfs_cb_data
));
1803 data
->private_data
= private_data
;
1804 data
->nfsfh
= nfsfh
;
1807 /* hello, clang-analyzer */
1808 assert(data
->num_calls
== 0);
1810 /* chop requests into chunks of at most WRITEMAX bytes if necessary.
1811 * we send all writes in parallel so that performance is still good.
1813 data
->max_offset
= offset
;
1814 data
->start_offset
= offset
;
1817 uint64_t writecount
= count
;
1818 struct nfs_mcb_data
*mdata
;
1821 if (writecount
> nfs_get_writemax(nfs
)) {
1822 writecount
= nfs_get_writemax(nfs
);
1825 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1826 if (mdata
== NULL
) {
1827 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1828 if (data
->num_calls
== 0) {
1829 free_nfs_cb_data(data
);
1835 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1837 mdata
->offset
= offset
;
1838 mdata
->count
= writecount
;
1839 mdata
->update_pos
= update_pos
;
1841 nfs_fill_WRITE3args(&args
, nfsfh
, offset
, writecount
, &buf
[offset
- data
->start_offset
]);
1843 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1844 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1846 if (data
->num_calls
== 0) {
1847 free_nfs_cb_data(data
);
1854 count
-= writecount
;
1855 offset
+= writecount
;
1857 } while (count
> 0);
1862 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
)
1864 return nfs_pwrite_async_internal(nfs
, nfsfh
, offset
, count
, buf
, cb
, private_data
, 0);
1870 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1872 return nfs_pwrite_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
, 1);
1882 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1884 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1885 free(nfsfh
->fh
.data
.data_val
);
1886 nfsfh
->fh
.data
.data_val
= NULL
;
1890 cb(0, nfs
, NULL
, private_data
);
1901 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1903 struct nfs_cb_data
*data
;
1904 struct GETATTR3args args
;
1906 data
= malloc(sizeof(struct nfs_cb_data
));
1908 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1911 memset(data
, 0, sizeof(struct nfs_cb_data
));
1914 data
->private_data
= private_data
;
1916 memset(&args
, 0, sizeof(GETATTR3args
));
1917 args
.object
= nfsfh
->fh
;
1919 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1920 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1921 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1922 free_nfs_cb_data(data
);
1933 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1935 struct nfs_cb_data
*data
= private_data
;
1936 struct nfs_context
*nfs
= data
->nfs
;
1939 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1941 if (status
== RPC_STATUS_ERROR
) {
1942 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1943 free_nfs_cb_data(data
);
1946 if (status
== RPC_STATUS_CANCEL
) {
1947 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1948 free_nfs_cb_data(data
);
1953 if (res
->status
!= NFS3_OK
) {
1954 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1955 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1956 free_nfs_cb_data(data
);
1960 data
->cb(0, nfs
, NULL
, data
->private_data
);
1961 free_nfs_cb_data(data
);
1964 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1966 struct nfs_cb_data
*data
;
1967 struct COMMIT3args args
;
1969 data
= malloc(sizeof(struct nfs_cb_data
));
1971 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1974 memset(data
, 0, sizeof(struct nfs_cb_data
));
1977 data
->private_data
= private_data
;
1979 args
.file
= nfsfh
->fh
;
1982 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1983 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1984 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1985 free_nfs_cb_data(data
);
1997 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1999 struct nfs_cb_data
*data
= private_data
;
2000 struct nfs_context
*nfs
= data
->nfs
;
2003 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2005 if (status
== RPC_STATUS_ERROR
) {
2006 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2007 free_nfs_cb_data(data
);
2010 if (status
== RPC_STATUS_CANCEL
) {
2011 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2012 free_nfs_cb_data(data
);
2017 if (res
->status
!= NFS3_OK
) {
2018 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2019 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2020 free_nfs_cb_data(data
);
2024 data
->cb(0, nfs
, NULL
, data
->private_data
);
2025 free_nfs_cb_data(data
);
2028 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
2030 struct nfs_cb_data
*data
;
2033 data
= malloc(sizeof(struct nfs_cb_data
));
2035 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2038 memset(data
, 0, sizeof(struct nfs_cb_data
));
2041 data
->private_data
= private_data
;
2043 memset(&args
, 0, sizeof(SETATTR3args
));
2044 args
.object
= nfsfh
->fh
;
2045 args
.new_attributes
.size
.set_it
= 1;
2046 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2048 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2049 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2050 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2051 free_nfs_cb_data(data
);
2061 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2063 uint64_t offset
= data
->continue_int
;
2066 nfsfh
.fh
= data
->fh
;
2068 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2069 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2070 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2071 free_nfs_cb_data(data
);
2074 free_nfs_cb_data(data
);
2078 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2084 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2085 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2098 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2101 struct nfs_cb_data
*data
= private_data
;
2102 struct nfs_context
*nfs
= data
->nfs
;
2103 char *str
= data
->continue_data
;
2105 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2107 str
= &str
[strlen(str
) + 1];
2109 if (status
== RPC_STATUS_ERROR
) {
2110 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2111 free_nfs_cb_data(data
);
2114 if (status
== RPC_STATUS_CANCEL
) {
2115 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2116 free_nfs_cb_data(data
);
2121 if (res
->status
!= NFS3_OK
) {
2122 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
));
2123 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2124 free_nfs_cb_data(data
);
2128 data
->cb(0, nfs
, NULL
, data
->private_data
);
2129 free_nfs_cb_data(data
);
2132 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2134 char *str
= data
->continue_data
;
2137 str
= &str
[strlen(str
) + 1];
2139 memset(&args
, 0, sizeof(MKDIR3args
));
2140 args
.where
.dir
= data
->fh
;
2141 args
.where
.name
= str
;
2142 args
.attributes
.mode
.set_it
= 1;
2143 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2145 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2146 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2147 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2148 free_nfs_cb_data(data
);
2154 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2159 new_path
= strdup(path
);
2160 if (new_path
== NULL
) {
2161 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2165 ptr
= strrchr(new_path
, '/');
2168 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2173 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2174 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2175 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2189 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2192 struct nfs_cb_data
*data
= private_data
;
2193 struct nfs_context
*nfs
= data
->nfs
;
2194 char *str
= data
->continue_data
;
2196 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2198 str
= &str
[strlen(str
) + 1];
2200 if (status
== RPC_STATUS_ERROR
) {
2201 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2202 free_nfs_cb_data(data
);
2205 if (status
== RPC_STATUS_CANCEL
) {
2206 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2207 free_nfs_cb_data(data
);
2212 if (res
->status
!= NFS3_OK
) {
2213 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
));
2214 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2215 free_nfs_cb_data(data
);
2219 data
->cb(0, nfs
, NULL
, data
->private_data
);
2220 free_nfs_cb_data(data
);
2223 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2225 char *str
= data
->continue_data
;
2228 str
= &str
[strlen(str
) + 1];
2230 args
.object
.dir
= data
->fh
;
2231 args
.object
.name
= str
;
2232 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2233 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2234 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2235 free_nfs_cb_data(data
);
2241 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2246 new_path
= strdup(path
);
2247 if (new_path
== NULL
) {
2248 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2252 ptr
= strrchr(new_path
, '/');
2255 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2260 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2261 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2262 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2275 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2278 struct nfs_cb_data
*data
= private_data
;
2279 struct nfs_context
*nfs
= data
->nfs
;
2280 struct nfsfh
*nfsfh
;
2281 char *str
= data
->continue_data
;
2283 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2285 if (status
== RPC_STATUS_ERROR
) {
2286 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2287 free_nfs_cb_data(data
);
2290 if (status
== RPC_STATUS_CANCEL
) {
2291 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2292 free_nfs_cb_data(data
);
2296 str
= &str
[strlen(str
) + 1];
2298 if (res
->status
!= NFS3_OK
) {
2299 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
));
2300 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2305 nfsfh
= malloc(sizeof(struct nfsfh
));
2306 if (nfsfh
== NULL
) {
2307 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2308 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2309 free_nfs_cb_data(data
);
2312 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2314 /* copy the filehandle */
2315 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2316 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2317 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2319 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2320 free_nfs_cb_data(data
);
2325 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2328 struct nfs_cb_data
*data
= private_data
;
2329 struct nfs_context
*nfs
= data
->nfs
;
2330 char *str
= data
->continue_data
;
2333 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2335 if (status
== RPC_STATUS_ERROR
) {
2336 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2337 free_nfs_cb_data(data
);
2340 if (status
== RPC_STATUS_CANCEL
) {
2341 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2342 free_nfs_cb_data(data
);
2346 str
= &str
[strlen(str
) + 1];
2348 if (res
->status
!= NFS3_OK
) {
2349 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
));
2350 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2351 free_nfs_cb_data(data
);
2355 memset(&args
, 0, sizeof(LOOKUP3args
));
2356 args
.what
.dir
= data
->fh
;
2357 args
.what
.name
= str
;
2359 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2360 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2361 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2362 free_nfs_cb_data(data
);
2368 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2370 char *str
= data
->continue_data
;
2373 str
= &str
[strlen(str
) + 1];
2375 memset(&args
, 0, sizeof(CREATE3args
));
2376 args
.where
.dir
= data
->fh
;
2377 args
.where
.name
= str
;
2378 args
.how
.mode
= UNCHECKED
;
2379 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2380 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2382 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2383 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2384 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2385 free_nfs_cb_data(data
);
2391 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2396 new_path
= strdup(path
);
2397 if (new_path
== NULL
) {
2398 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2402 ptr
= strrchr(new_path
, '/');
2404 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2410 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2411 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2412 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2425 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2428 struct nfs_cb_data
*data
= private_data
;
2429 struct nfs_context
*nfs
= data
->nfs
;
2430 char *str
= data
->continue_data
;
2432 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2434 str
= &str
[strlen(str
) + 1];
2436 if (status
== RPC_STATUS_ERROR
) {
2437 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2438 free_nfs_cb_data(data
);
2441 if (status
== RPC_STATUS_CANCEL
) {
2442 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2443 free_nfs_cb_data(data
);
2448 if (res
->status
!= NFS3_OK
) {
2449 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
));
2450 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2451 free_nfs_cb_data(data
);
2455 data
->cb(0, nfs
, NULL
, data
->private_data
);
2456 free_nfs_cb_data(data
);
2459 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2461 char *str
= data
->continue_data
;
2462 struct REMOVE3args args
;
2464 str
= &str
[strlen(str
) + 1];
2466 args
.object
.dir
= data
->fh
;
2467 args
.object
.name
= str
;
2468 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2469 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2470 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2471 free_nfs_cb_data(data
);
2477 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2482 new_path
= strdup(path
);
2483 if (new_path
== NULL
) {
2484 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2488 ptr
= strrchr(new_path
, '/');
2491 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2496 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2497 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2498 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2509 struct mknod_cb_data
{
2516 static void free_mknod_cb_data(void *ptr
)
2518 struct mknod_cb_data
*data
= ptr
;
2524 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2527 struct nfs_cb_data
*data
= private_data
;
2528 struct nfs_context
*nfs
= data
->nfs
;
2529 char *str
= data
->continue_data
;
2531 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2533 str
= &str
[strlen(str
) + 1];
2535 if (status
== RPC_STATUS_ERROR
) {
2536 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2537 free_nfs_cb_data(data
);
2540 if (status
== RPC_STATUS_CANCEL
) {
2541 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2542 free_nfs_cb_data(data
);
2547 if (res
->status
!= NFS3_OK
) {
2548 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
));
2549 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2550 free_nfs_cb_data(data
);
2554 data
->cb(0, nfs
, NULL
, data
->private_data
);
2555 free_nfs_cb_data(data
);
2558 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2560 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2561 char *str
= cb_data
->path
;
2564 str
= &str
[strlen(str
) + 1];
2566 args
.where
.dir
= data
->fh
;
2567 args
.where
.name
= str
;
2568 switch (cb_data
->mode
& S_IFMT
) {
2570 args
.what
.type
= NF3CHR
;
2571 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2572 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
);
2573 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2574 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2577 args
.what
.type
= NF3BLK
;
2578 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2579 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
);
2580 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2581 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2583 args
.what
.type
= NF3SOCK
;
2584 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2585 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
);
2588 args
.what
.type
= NF3FIFO
;
2589 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2590 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
);
2593 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2594 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2595 free_nfs_cb_data(data
);
2599 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2600 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2601 free_nfs_cb_data(data
);
2607 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2610 struct mknod_cb_data
*cb_data
;
2612 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2613 if (cb_data
== NULL
) {
2614 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2618 cb_data
->path
= strdup(path
);
2619 if (cb_data
->path
== NULL
) {
2620 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2625 ptr
= strrchr(cb_data
->path
, '/');
2627 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2628 free_mknod_cb_data(cb_data
);
2633 cb_data
->mode
= mode
;
2634 cb_data
->major
= major(dev
);
2635 cb_data
->minor
= minor(dev
);
2637 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2638 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2639 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2650 /* ReadDirPlus Emulation Callback data */
2651 struct rdpe_cb_data
{
2654 struct nfs_cb_data
*data
;
2657 /* ReadDirPlus Emulation LOOKUP Callback data */
2658 struct rdpe_lookup_cb_data
{
2659 struct rdpe_cb_data
*rdpe_cb_data
;
2660 struct nfsdirent
*nfsdirent
;
2663 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2664 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2666 LOOKUP3res
*res
= command_data
;
2667 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2668 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2669 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2670 struct nfsdir
*nfsdir
= data
->continue_data
;
2671 struct nfs_context
*nfs
= data
->nfs
;
2672 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2674 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2676 free(rdpe_lookup_cb_data
);
2678 rdpe_cb_data
->getattrcount
--;
2680 if (status
== RPC_STATUS_ERROR
) {
2681 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2682 "failed with RPC_STATUS_ERROR");
2683 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2685 if (status
== RPC_STATUS_CANCEL
) {
2686 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2687 "failed with RPC_STATUS_CANCEL");
2688 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2690 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2691 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2692 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2694 nfsdirent
->type
= attributes
->type
;
2695 nfsdirent
->mode
= attributes
->mode
;
2696 nfsdirent
->size
= attributes
->size
;
2698 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2699 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2700 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2701 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2702 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2703 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2704 nfsdirent
->uid
= attributes
->uid
;
2705 nfsdirent
->gid
= attributes
->gid
;
2709 if (rdpe_cb_data
->getattrcount
== 0) {
2710 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2711 rpc_set_error(nfs
->rpc
, "READDIRPLUS emulation "
2712 "failed: %s", rpc_get_error(rpc
));
2713 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
2714 data
->private_data
);
2715 nfs_free_nfsdir(nfsdir
);
2717 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2721 data
->continue_data
= NULL
;
2722 free_nfs_cb_data(data
);
2726 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2728 READDIR3res
*res
= command_data
;
2729 struct nfs_cb_data
*data
= private_data
;
2730 struct nfs_context
*nfs
= data
->nfs
;
2731 struct nfsdir
*nfsdir
= data
->continue_data
;
2732 struct nfsdirent
*nfsdirent
;
2733 struct entry3
*entry
;
2734 uint64_t cookie
= 0;
2735 struct rdpe_cb_data
*rdpe_cb_data
;
2737 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2739 if (status
== RPC_STATUS_ERROR
) {
2740 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2741 nfs_free_nfsdir(nfsdir
);
2742 data
->continue_data
= NULL
;
2743 free_nfs_cb_data(data
);
2747 if (status
== RPC_STATUS_CANCEL
) {
2748 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2749 nfs_free_nfsdir(nfsdir
);
2750 data
->continue_data
= NULL
;
2751 free_nfs_cb_data(data
);
2755 if (res
->status
!= NFS3_OK
) {
2756 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
));
2757 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2758 nfs_free_nfsdir(nfsdir
);
2759 data
->continue_data
= NULL
;
2760 free_nfs_cb_data(data
);
2764 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2765 while (entry
!= NULL
) {
2766 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2767 if (nfsdirent
== NULL
) {
2768 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2769 nfs_free_nfsdir(nfsdir
);
2770 data
->continue_data
= NULL
;
2771 free_nfs_cb_data(data
);
2774 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2775 nfsdirent
->name
= strdup(entry
->name
);
2776 if (nfsdirent
->name
== NULL
) {
2777 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2779 nfs_free_nfsdir(nfsdir
);
2780 data
->continue_data
= NULL
;
2781 free_nfs_cb_data(data
);
2784 nfsdirent
->inode
= entry
->fileid
;
2786 nfsdirent
->next
= nfsdir
->entries
;
2787 nfsdir
->entries
= nfsdirent
;
2789 cookie
= entry
->cookie
;
2790 entry
= entry
->nextentry
;
2793 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2796 args
.dir
= data
->fh
;
2797 args
.cookie
= cookie
;
2798 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2801 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2802 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2803 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2804 nfs_free_nfsdir(nfsdir
);
2805 data
->continue_data
= NULL
;
2806 free_nfs_cb_data(data
);
2812 /* steal the dirhandle */
2813 nfsdir
->current
= nfsdir
->entries
;
2815 if (nfsdir
->entries
) {
2816 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2817 rdpe_cb_data
->getattrcount
= 0;
2818 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2819 rdpe_cb_data
->data
= data
;
2820 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2821 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2824 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2825 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2826 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2828 memset(&args
, 0, sizeof(LOOKUP3args
));
2829 args
.what
.dir
= data
->fh
;
2830 args
.what
.name
= nfsdirent
->name
;
2832 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2833 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2835 /* if we have already commands in flight, we cant just stop, we have to wait for the
2836 * commands in flight to complete
2838 if (rdpe_cb_data
->getattrcount
> 0) {
2839 nfs_free_nfsdir(nfsdir
);
2840 data
->continue_data
= NULL
;
2841 free_nfs_cb_data(data
);
2842 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2843 free(rdpe_lookup_cb_data
);
2847 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2848 nfs_free_nfsdir(nfsdir
);
2849 data
->continue_data
= NULL
;
2850 free_nfs_cb_data(data
);
2851 free(rdpe_lookup_cb_data
);
2855 rdpe_cb_data
->getattrcount
++;
2860 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2862 READDIRPLUS3res
*res
= command_data
;
2863 struct nfs_cb_data
*data
= private_data
;
2864 struct nfs_context
*nfs
= data
->nfs
;
2865 struct nfsdir
*nfsdir
= data
->continue_data
;
2866 struct entryplus3
*entry
;
2867 uint64_t cookie
= 0;
2869 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2871 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2874 args
.dir
= data
->fh
;
2875 args
.cookie
= cookie
;
2876 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2879 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2880 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2881 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2882 nfs_free_nfsdir(nfsdir
);
2883 data
->continue_data
= NULL
;
2884 free_nfs_cb_data(data
);
2890 if (status
== RPC_STATUS_CANCEL
) {
2891 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2892 nfs_free_nfsdir(nfsdir
);
2893 data
->continue_data
= NULL
;
2894 free_nfs_cb_data(data
);
2898 if (res
->status
!= NFS3_OK
) {
2899 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
));
2900 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2901 nfs_free_nfsdir(nfsdir
);
2902 data
->continue_data
= NULL
;
2903 free_nfs_cb_data(data
);
2907 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2908 while (entry
!= NULL
) {
2909 struct nfsdirent
*nfsdirent
;
2911 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2912 if (nfsdirent
== NULL
) {
2913 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2914 nfs_free_nfsdir(nfsdir
);
2915 data
->continue_data
= NULL
;
2916 free_nfs_cb_data(data
);
2919 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2920 nfsdirent
->name
= strdup(entry
->name
);
2921 if (nfsdirent
->name
== NULL
) {
2922 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2924 nfs_free_nfsdir(nfsdir
);
2925 data
->continue_data
= NULL
;
2926 free_nfs_cb_data(data
);
2929 nfsdirent
->inode
= entry
->fileid
;
2930 if (entry
->name_attributes
.attributes_follow
) {
2931 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2932 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2933 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2935 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2936 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2937 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2938 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2939 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2940 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2941 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2942 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2945 nfsdirent
->next
= nfsdir
->entries
;
2946 nfsdir
->entries
= nfsdirent
;
2948 cookie
= entry
->cookie
;
2949 entry
= entry
->nextentry
;
2952 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2953 READDIRPLUS3args args
;
2955 args
.dir
= data
->fh
;
2956 args
.cookie
= cookie
;
2957 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2958 args
.dircount
= 8192;
2959 args
.maxcount
= 8192;
2961 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2962 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2963 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2964 nfs_free_nfsdir(nfsdir
);
2965 data
->continue_data
= NULL
;
2966 free_nfs_cb_data(data
);
2972 /* steal the dirhandle */
2973 data
->continue_data
= NULL
;
2974 nfsdir
->current
= nfsdir
->entries
;
2976 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2977 free_nfs_cb_data(data
);
2980 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2982 READDIRPLUS3args args
;
2984 args
.dir
= data
->fh
;
2986 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2987 args
.dircount
= 8192;
2988 args
.maxcount
= 8192;
2989 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2990 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2991 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2992 free_nfs_cb_data(data
);
2998 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3000 struct nfsdir
*nfsdir
;
3002 nfsdir
= malloc(sizeof(struct nfsdir
));
3003 if (nfsdir
== NULL
) {
3004 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
3007 memset(nfsdir
, 0, sizeof(struct nfsdir
));
3009 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
3010 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3018 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3020 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
3022 if (nfsdir
->current
!= NULL
) {
3023 nfsdir
->current
= nfsdir
->current
->next
;
3032 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3034 nfs_free_nfsdir(nfsdir
);
3041 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3052 struct lseek_cb_data
{
3053 struct nfs_context
*nfs
;
3054 struct nfsfh
*nfsfh
;
3060 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3063 struct lseek_cb_data
*data
= private_data
;
3064 struct nfs_context
*nfs
= data
->nfs
;
3067 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3069 if (status
== RPC_STATUS_ERROR
) {
3070 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3074 if (status
== RPC_STATUS_CANCEL
) {
3075 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3081 if (res
->status
!= NFS3_OK
) {
3082 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3083 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3088 size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3090 if (data
->offset
< 0 &&
3091 (uint64_t)(-data
->offset
) > size
) {
3092 data
->cb(-EINVAL
, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3094 data
->nfsfh
->offset
= data
->offset
+ size
;
3095 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3101 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3103 struct lseek_cb_data
*data
;
3104 struct GETATTR3args args
;
3106 if (whence
== SEEK_SET
) {
3108 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3110 nfsfh
->offset
= offset
;
3111 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3115 if (whence
== SEEK_CUR
) {
3117 nfsfh
->offset
< (uint64_t)(-offset
)) {
3118 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3120 nfsfh
->offset
+= offset
;
3121 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3126 data
= malloc(sizeof(struct lseek_cb_data
));
3128 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3133 data
->nfsfh
= nfsfh
;
3134 data
->offset
= offset
;
3136 data
->private_data
= private_data
;
3138 memset(&args
, 0, sizeof(GETATTR3args
));
3139 args
.object
= nfsfh
->fh
;
3141 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3142 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3155 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3158 struct nfs_cb_data
*data
= private_data
;
3159 struct nfs_context
*nfs
= data
->nfs
;
3160 struct statvfs svfs
;
3162 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3164 if (status
== RPC_STATUS_ERROR
) {
3165 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3166 free_nfs_cb_data(data
);
3169 if (status
== RPC_STATUS_CANCEL
) {
3170 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3171 free_nfs_cb_data(data
);
3176 if (res
->status
!= NFS3_OK
) {
3177 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
));
3178 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3179 free_nfs_cb_data(data
);
3183 svfs
.f_bsize
= 4096;
3184 svfs
.f_frsize
= 4096;
3185 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3186 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3187 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3188 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3189 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3190 #if !defined(ANDROID)
3191 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3194 svfs
.f_namemax
= 256;
3197 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3198 free_nfs_cb_data(data
);
3201 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3205 args
.fsroot
= data
->fh
;
3206 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3207 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3208 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3209 free_nfs_cb_data(data
);
3215 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3217 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3218 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3231 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3234 struct nfs_cb_data
*data
= private_data
;
3235 struct nfs_context
*nfs
= data
->nfs
;
3237 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3239 if (status
== RPC_STATUS_ERROR
) {
3240 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3241 free_nfs_cb_data(data
);
3244 if (status
== RPC_STATUS_CANCEL
) {
3245 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3246 free_nfs_cb_data(data
);
3251 if (res
->status
!= NFS3_OK
) {
3252 rpc_set_error(nfs
->rpc
, "NFS: READLINK of %s failed with %s(%d)", data
->saved_path
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3253 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3254 free_nfs_cb_data(data
);
3259 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3260 free_nfs_cb_data(data
);
3263 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3267 args
.symlink
= data
->fh
;
3269 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3270 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3271 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3272 free_nfs_cb_data(data
);
3278 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3280 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3281 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3294 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3296 struct nfs_cb_data
*data
= private_data
;
3297 struct nfs_context
*nfs
= data
->nfs
;
3300 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3302 if (status
== RPC_STATUS_ERROR
) {
3303 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3304 free_nfs_cb_data(data
);
3307 if (status
== RPC_STATUS_CANCEL
) {
3308 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3309 free_nfs_cb_data(data
);
3314 if (res
->status
!= NFS3_OK
) {
3315 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3316 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3317 free_nfs_cb_data(data
);
3321 data
->cb(0, nfs
, NULL
, data
->private_data
);
3322 free_nfs_cb_data(data
);
3325 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3329 memset(&args
, 0, sizeof(SETATTR3args
));
3330 args
.object
= data
->fh
;
3331 args
.new_attributes
.mode
.set_it
= 1;
3332 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3334 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3335 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3336 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3337 free_nfs_cb_data(data
);
3344 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3346 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3347 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3357 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3359 struct nfs_cb_data
*data
;
3361 data
= malloc(sizeof(struct nfs_cb_data
));
3363 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3366 memset(data
, 0, sizeof(struct nfs_cb_data
));
3369 data
->private_data
= private_data
;
3370 data
->continue_int
= mode
;
3371 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3372 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3373 if (data
->fh
.data
.data_val
== NULL
) {
3374 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3375 free_nfs_cb_data(data
);
3378 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3380 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3392 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3394 struct nfs_cb_data
*data
= private_data
;
3395 struct nfs_context
*nfs
= data
->nfs
;
3398 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3400 if (status
== RPC_STATUS_ERROR
) {
3401 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3402 free_nfs_cb_data(data
);
3405 if (status
== RPC_STATUS_CANCEL
) {
3406 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3407 free_nfs_cb_data(data
);
3412 if (res
->status
!= NFS3_OK
) {
3413 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3414 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3415 free_nfs_cb_data(data
);
3419 data
->cb(0, nfs
, NULL
, data
->private_data
);
3420 free_nfs_cb_data(data
);
3423 struct nfs_chown_data
{
3428 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3431 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3433 memset(&args
, 0, sizeof(SETATTR3args
));
3434 args
.object
= data
->fh
;
3435 if (chown_data
->uid
!= (uid_t
)-1) {
3436 args
.new_attributes
.uid
.set_it
= 1;
3437 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3439 if (chown_data
->gid
!= (gid_t
)-1) {
3440 args
.new_attributes
.gid
.set_it
= 1;
3441 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3444 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3445 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3446 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3447 free_nfs_cb_data(data
);
3454 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3456 struct nfs_chown_data
*chown_data
;
3458 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3459 if (chown_data
== NULL
) {
3460 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3464 chown_data
->uid
= uid
;
3465 chown_data
->gid
= gid
;
3467 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3468 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3479 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3481 struct nfs_cb_data
*data
;
3482 struct nfs_chown_data
*chown_data
;
3484 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3485 if (chown_data
== NULL
) {
3486 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3490 chown_data
->uid
= uid
;
3491 chown_data
->gid
= gid
;
3493 data
= malloc(sizeof(struct nfs_cb_data
));
3495 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3499 memset(data
, 0, sizeof(struct nfs_cb_data
));
3502 data
->private_data
= private_data
;
3503 data
->continue_data
= chown_data
;
3504 data
->free_continue_data
= free
;
3505 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3506 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3507 if (data
->fh
.data
.data_val
== NULL
) {
3508 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3509 free_nfs_cb_data(data
);
3512 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3514 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3528 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3530 struct nfs_cb_data
*data
= private_data
;
3531 struct nfs_context
*nfs
= data
->nfs
;
3534 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3536 if (status
== RPC_STATUS_ERROR
) {
3537 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3538 free_nfs_cb_data(data
);
3541 if (status
== RPC_STATUS_CANCEL
) {
3542 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3543 free_nfs_cb_data(data
);
3548 if (res
->status
!= NFS3_OK
) {
3549 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3550 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3551 free_nfs_cb_data(data
);
3555 data
->cb(0, nfs
, NULL
, data
->private_data
);
3556 free_nfs_cb_data(data
);
3559 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3562 struct timeval
*utimes_data
= data
->continue_data
;
3564 memset(&args
, 0, sizeof(SETATTR3args
));
3565 args
.object
= data
->fh
;
3566 if (utimes_data
!= NULL
) {
3567 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3568 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3569 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3570 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3571 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3572 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3574 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3575 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3578 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3579 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3580 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3581 free_nfs_cb_data(data
);
3588 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3590 struct timeval
*new_times
= NULL
;
3592 if (times
!= NULL
) {
3593 new_times
= malloc(sizeof(struct timeval
)*2);
3594 if (new_times
== NULL
) {
3595 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3599 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3602 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3603 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3613 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3615 struct timeval
*new_times
= NULL
;
3617 if (times
!= NULL
) {
3618 new_times
= malloc(sizeof(struct timeval
)*2);
3619 if (new_times
== NULL
) {
3620 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3624 new_times
[0].tv_sec
= times
->actime
;
3625 new_times
[0].tv_usec
= 0;
3626 new_times
[1].tv_sec
= times
->modtime
;
3627 new_times
[1].tv_usec
= 0;
3630 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3631 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3642 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3645 struct nfs_cb_data
*data
= private_data
;
3646 struct nfs_context
*nfs
= data
->nfs
;
3647 unsigned int nfsmode
= 0;
3649 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3651 if (status
== RPC_STATUS_ERROR
) {
3652 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3653 free_nfs_cb_data(data
);
3656 if (status
== RPC_STATUS_CANCEL
) {
3657 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3658 free_nfs_cb_data(data
);
3663 if (res
->status
!= NFS3_OK
) {
3664 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
));
3665 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3666 free_nfs_cb_data(data
);
3670 if (data
->continue_int
& R_OK
) {
3671 nfsmode
|= ACCESS3_READ
;
3673 if (data
->continue_int
& W_OK
) {
3674 nfsmode
|= ACCESS3_MODIFY
;
3676 if (data
->continue_int
& X_OK
) {
3677 nfsmode
|= ACCESS3_EXECUTE
;
3680 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3681 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3682 nfsmode
&ACCESS3_READ
?'r':'-',
3683 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3684 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3685 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3686 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3687 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3688 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3689 free_nfs_cb_data(data
);
3693 data
->cb(0, nfs
, NULL
, data
->private_data
);
3694 free_nfs_cb_data(data
);
3697 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3702 if (data
->continue_int
& R_OK
) {
3703 nfsmode
|= ACCESS3_READ
;
3705 if (data
->continue_int
& W_OK
) {
3706 nfsmode
|= ACCESS3_MODIFY
;
3708 if (data
->continue_int
& X_OK
) {
3709 nfsmode
|= ACCESS3_EXECUTE
;
3712 memset(&args
, 0, sizeof(ACCESS3args
));
3713 args
.object
= data
->fh
;
3714 args
.access
= nfsmode
;
3716 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3717 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3718 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3719 free_nfs_cb_data(data
);
3725 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3727 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3728 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3740 struct nfs_symlink_data
{
3742 char *newpathparent
;
3743 char *newpathobject
;
3746 static void free_nfs_symlink_data(void *mem
)
3748 struct nfs_symlink_data
*data
= mem
;
3750 if (data
->oldpath
!= NULL
) {
3751 free(data
->oldpath
);
3753 if (data
->newpathparent
!= NULL
) {
3754 free(data
->newpathparent
);
3756 if (data
->newpathobject
!= NULL
) {
3757 free(data
->newpathobject
);
3762 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3765 struct nfs_cb_data
*data
= private_data
;
3766 struct nfs_context
*nfs
= data
->nfs
;
3767 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3769 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3771 if (status
== RPC_STATUS_ERROR
) {
3772 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3773 free_nfs_cb_data(data
);
3776 if (status
== RPC_STATUS_CANCEL
) {
3777 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3778 free_nfs_cb_data(data
);
3783 if (res
->status
!= NFS3_OK
) {
3784 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
));
3785 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3786 free_nfs_cb_data(data
);
3790 data
->cb(0, nfs
, NULL
, data
->private_data
);
3791 free_nfs_cb_data(data
);
3794 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3796 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3799 memset(&args
, 0, sizeof(SYMLINK3args
));
3800 args
.where
.dir
= data
->fh
;
3801 args
.where
.name
= symlink_data
->newpathobject
;
3802 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3803 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
;
3804 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3806 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3807 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3808 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3809 free_nfs_cb_data(data
);
3815 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3818 struct nfs_symlink_data
*symlink_data
;
3820 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3821 if (symlink_data
== NULL
) {
3822 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3825 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3827 symlink_data
->oldpath
= strdup(oldpath
);
3828 if (symlink_data
->oldpath
== NULL
) {
3829 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3830 free_nfs_symlink_data(symlink_data
);
3834 symlink_data
->newpathparent
= strdup(newpath
);
3835 if (symlink_data
->newpathparent
== NULL
) {
3836 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3837 free_nfs_symlink_data(symlink_data
);
3841 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3843 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3844 free_nfs_symlink_data(symlink_data
);
3850 symlink_data
->newpathobject
= strdup(ptr
);
3851 if (symlink_data
->newpathobject
== NULL
) {
3852 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3853 free_nfs_symlink_data(symlink_data
);
3857 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3858 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3870 struct nfs_rename_data
{
3873 struct nfs_fh3 olddir
;
3876 struct nfs_fh3 newdir
;
3879 static void free_nfs_rename_data(void *mem
)
3881 struct nfs_rename_data
*data
= mem
;
3883 if (data
->oldpath
!= NULL
) {
3884 free(data
->oldpath
);
3886 if (data
->olddir
.data
.data_val
!= NULL
) {
3887 free(data
->olddir
.data
.data_val
);
3889 if (data
->newpath
!= NULL
) {
3890 free(data
->newpath
);
3892 if (data
->newdir
.data
.data_val
!= NULL
) {
3893 free(data
->newdir
.data
.data_val
);
3898 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3901 struct nfs_cb_data
*data
= private_data
;
3902 struct nfs_context
*nfs
= data
->nfs
;
3903 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3905 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3907 if (status
== RPC_STATUS_ERROR
) {
3908 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3909 free_nfs_cb_data(data
);
3912 if (status
== RPC_STATUS_CANCEL
) {
3913 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3914 free_nfs_cb_data(data
);
3919 if (res
->status
!= NFS3_OK
) {
3920 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
));
3921 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3922 free_nfs_cb_data(data
);
3926 data
->cb(0, nfs
, NULL
, data
->private_data
);
3927 free_nfs_cb_data(data
);
3930 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3932 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3935 /* steal the filehandle */
3936 rename_data
->newdir
= data
->fh
;
3937 data
->fh
.data
.data_val
= NULL
;
3939 args
.from
.dir
= rename_data
->olddir
;
3940 args
.from
.name
= rename_data
->oldobject
;
3941 args
.to
.dir
= rename_data
->newdir
;
3942 args
.to
.name
= rename_data
->newobject
;
3943 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3944 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3945 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3946 free_nfs_cb_data(data
);
3953 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3955 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3956 char* newpath
= strdup(rename_data
->newpath
);
3958 rpc_set_error(nfs
->rpc
, "Out of memory. Could not allocate memory to store target path for rename");
3959 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3960 free_nfs_cb_data(data
);
3964 /* steal the filehandle */
3965 rename_data
->olddir
= data
->fh
;
3966 data
->fh
.data
.data_val
= NULL
;
3968 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) {
3969 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", newpath
);
3970 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3971 free_nfs_cb_data(data
);
3975 data
->continue_data
= NULL
;
3976 free_nfs_cb_data(data
);
3983 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3986 struct nfs_rename_data
*rename_data
;
3988 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3989 if (rename_data
== NULL
) {
3990 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3993 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3995 rename_data
->oldpath
= strdup(oldpath
);
3996 if (rename_data
->oldpath
== NULL
) {
3997 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3998 free_nfs_rename_data(rename_data
);
4001 ptr
= strrchr(rename_data
->oldpath
, '/');
4003 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
4004 free_nfs_rename_data(rename_data
);
4009 rename_data
->oldobject
= ptr
;
4012 rename_data
->newpath
= strdup(newpath
);
4013 if (rename_data
->newpath
== NULL
) {
4014 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4015 free_nfs_rename_data(rename_data
);
4018 ptr
= strrchr(rename_data
->newpath
, '/');
4020 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4021 free_nfs_rename_data(rename_data
);
4026 rename_data
->newobject
= ptr
;
4029 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
4030 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4041 struct nfs_link_data
{
4043 struct nfs_fh3 oldfh
;
4046 struct nfs_fh3 newdir
;
4049 static void free_nfs_link_data(void *mem
)
4051 struct nfs_link_data
*data
= mem
;
4053 if (data
->oldpath
!= NULL
) {
4054 free(data
->oldpath
);
4056 if (data
->oldfh
.data
.data_val
!= NULL
) {
4057 free(data
->oldfh
.data
.data_val
);
4059 if (data
->newpath
!= NULL
) {
4060 free(data
->newpath
);
4062 if (data
->newdir
.data
.data_val
!= NULL
) {
4063 free(data
->newdir
.data
.data_val
);
4068 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4071 struct nfs_cb_data
*data
= private_data
;
4072 struct nfs_context
*nfs
= data
->nfs
;
4073 struct nfs_link_data
*link_data
= data
->continue_data
;
4075 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4077 if (status
== RPC_STATUS_ERROR
) {
4078 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4079 free_nfs_cb_data(data
);
4082 if (status
== RPC_STATUS_CANCEL
) {
4083 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4084 free_nfs_cb_data(data
);
4089 if (res
->status
!= NFS3_OK
) {
4090 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
));
4091 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4092 free_nfs_cb_data(data
);
4096 data
->cb(0, nfs
, NULL
, data
->private_data
);
4097 free_nfs_cb_data(data
);
4100 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4102 struct nfs_link_data
*link_data
= data
->continue_data
;
4105 /* steal the filehandle */
4106 link_data
->newdir
= data
->fh
;
4107 data
->fh
.data
.data_val
= NULL
;
4109 memset(&args
, 0, sizeof(LINK3args
));
4110 args
.file
= link_data
->oldfh
;
4111 args
.link
.dir
= link_data
->newdir
;
4112 args
.link
.name
= link_data
->newobject
;
4113 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4114 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4115 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4116 free_nfs_cb_data(data
);
4123 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4125 struct nfs_link_data
*link_data
= data
->continue_data
;
4127 /* steal the filehandle */
4128 link_data
->oldfh
= data
->fh
;
4129 data
->fh
.data
.data_val
= NULL
;
4131 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) {
4132 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4133 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4134 free_nfs_cb_data(data
);
4137 data
->continue_data
= NULL
;
4138 free_nfs_cb_data(data
);
4144 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4147 struct nfs_link_data
*link_data
;
4149 link_data
= malloc(sizeof(struct nfs_link_data
));
4150 if (link_data
== NULL
) {
4151 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4154 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4156 link_data
->oldpath
= strdup(oldpath
);
4157 if (link_data
->oldpath
== NULL
) {
4158 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4159 free_nfs_link_data(link_data
);
4163 link_data
->newpath
= strdup(newpath
);
4164 if (link_data
->newpath
== NULL
) {
4165 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4166 free_nfs_link_data(link_data
);
4169 ptr
= strrchr(link_data
->newpath
, '/');
4171 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4172 free_nfs_link_data(link_data
);
4177 link_data
->newobject
= ptr
;
4180 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4181 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4189 //qqq replace later with lseek()
4190 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4192 return nfsfh
->offset
;
4198 * Get the maximum supported READ3 size by the server
4200 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4202 return nfs
->readmax
;
4206 * Get the maximum supported WRITE3 size by the server
4208 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4210 return nfs
->writemax
;
4213 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4214 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4217 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4218 rpc_set_uid(nfs
->rpc
, uid
);
4221 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4222 rpc_set_gid(nfs
->rpc
, gid
);
4225 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4230 va_start(ap
, error_string
);
4232 vsnprintf(str
, 1024, error_string
, ap
);
4233 if (nfs
->rpc
->error_string
!= NULL
) {
4234 free(nfs
->rpc
->error_string
);
4236 nfs
->rpc
->error_string
= str
;
4242 struct mount_cb_data
{
4248 static void free_mount_cb_data(struct mount_cb_data
*data
)
4250 if (data
->server
!= NULL
) {
4252 data
->server
= NULL
;
4258 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4260 struct mount_cb_data
*data
= private_data
;
4262 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4264 if (status
== RPC_STATUS_ERROR
) {
4265 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4266 free_mount_cb_data(data
);
4269 if (status
== RPC_STATUS_CANCEL
) {
4270 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4271 free_mount_cb_data(data
);
4275 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4276 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4277 rpc_set_error(rpc
, "Failed to disconnect\n");
4279 free_mount_cb_data(data
);
4282 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4284 struct mount_cb_data
*data
= private_data
;
4286 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4288 /* Dont want any more callbacks even if the socket is closed */
4289 rpc
->connect_cb
= NULL
;
4291 if (status
== RPC_STATUS_ERROR
) {
4292 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4293 free_mount_cb_data(data
);
4296 if (status
== RPC_STATUS_CANCEL
) {
4297 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4298 free_mount_cb_data(data
);
4302 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4303 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4304 free_mount_cb_data(data
);
4309 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4311 struct mount_cb_data
*data
;
4313 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4315 data
= malloc(sizeof(struct mount_cb_data
));
4319 memset(data
, 0, sizeof(struct mount_cb_data
));
4321 data
->private_data
= private_data
;
4322 data
->server
= strdup(server
);
4323 if (data
->server
== NULL
) {
4324 free_mount_cb_data(data
);
4327 if (rpc_connect_program_async(rpc
, data
->server
, MOUNT_PROGRAM
, MOUNT_V3
, mount_export_4_cb
, data
) != 0) {
4328 rpc_set_error(rpc
, "Failed to start connection");
4329 free_mount_cb_data(data
);
4336 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4338 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4342 const char *nfs_get_server(struct nfs_context
*nfs
) {
4346 const char *nfs_get_export(struct nfs_context
*nfs
) {
4350 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4351 return &nfs
->rootfh
;
4354 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {