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>
63 #include <sys/mkdev.h>
66 #ifdef MAJOR_IN_SYSMACROS
67 #include <sys/sysmacros.h>
76 #include <sys/types.h>
79 #include "libnfs-zdr.h"
82 #include "libnfs-raw.h"
83 #include "libnfs-raw-mount.h"
84 #include "libnfs-raw-nfs.h"
85 #include "libnfs-raw-portmap.h"
86 #include "libnfs-private.h"
88 #define MAX_DIR_CACHE 128
95 struct nfsdirent
*entries
;
96 struct nfsdirent
*current
;
99 struct nfs_readahead
{
101 uint64_t last_offset
;
114 struct nfs_readahead ra
;
118 struct rpc_context
*rpc
;
121 struct nfs_fh3 rootfh
;
125 struct nfsdir
*dircache
;
128 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
130 while (nfsdir
->entries
) {
131 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
132 if (nfsdir
->entries
->name
!= NULL
) {
133 free(nfsdir
->entries
->name
);
135 free(nfsdir
->entries
);
136 nfsdir
->entries
= dirent
;
138 free(nfsdir
->fh
.data
.data_val
);
142 static void nfs_dircache_add(struct nfs_context
*nfs
, struct nfsdir
*nfsdir
)
145 LIBNFS_LIST_ADD(&nfs
->dircache
, nfsdir
);
147 for (nfsdir
= nfs
->dircache
, i
= 0; nfsdir
; nfsdir
= nfsdir
->next
, i
++) {
148 if (i
> MAX_DIR_CACHE
) {
149 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
150 nfs_free_nfsdir(nfsdir
);
156 static struct nfsdir
*nfs_dircache_find(struct nfs_context
*nfs
, struct nfs_fh3
*fh
)
158 struct nfsdir
*nfsdir
;
160 for (nfsdir
= nfs
->dircache
; nfsdir
; nfsdir
= nfsdir
->next
) {
161 if (nfsdir
->fh
.data
.data_len
== fh
->data
.data_len
&&
162 !memcmp(nfsdir
->fh
.data
.data_val
, fh
->data
.data_val
, fh
->data
.data_len
)) {
163 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
172 typedef int (*continue_func
)(struct nfs_context
*nfs
, fattr3
*attr
,
173 struct nfs_cb_data
*data
);
176 struct nfs_context
*nfs
;
178 char *saved_path
, *path
;
183 continue_func continue_cb
;
185 void (*free_continue_data
)(void *);
190 /* for multi-read/write calls. */
195 uint64_t offset
, count
, max_offset
, org_offset
, org_count
;
200 struct nfs_mcb_data
{
201 struct nfs_cb_data
*data
;
207 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, fattr3
*attr
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
209 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
211 rpc_set_auth(nfs
->rpc
, auth
);
214 int nfs_get_fd(struct nfs_context
*nfs
)
216 return rpc_get_fd(nfs
->rpc
);
219 int nfs_queue_length(struct nfs_context
*nfs
)
221 return rpc_queue_length(nfs
->rpc
);
224 int nfs_which_events(struct nfs_context
*nfs
)
226 return rpc_which_events(nfs
->rpc
);
229 int nfs_service(struct nfs_context
*nfs
, int revents
)
231 return rpc_service(nfs
->rpc
, revents
);
234 char *nfs_get_error(struct nfs_context
*nfs
)
236 return rpc_get_error(nfs
->rpc
);
239 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
241 if (!strcmp(arg
, "tcp-syncnt")) {
242 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
243 } else if (!strcmp(arg
, "uid")) {
244 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
245 } else if (!strcmp(arg
, "gid")) {
246 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
247 } else if (!strcmp(arg
, "readahaed")) {
248 rpc_set_readahead(nfs_get_rpc_context(nfs
), atoi(val
));
253 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
255 struct nfs_url
*urls
;
256 char *strp
, *flagsp
, *strp2
;
258 if (strncmp(url
, "nfs://", 6)) {
259 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
263 urls
= malloc(sizeof(struct nfs_url
));
265 rpc_set_error(nfs
->rpc
, "Out of memory");
269 memset(urls
, 0x00, sizeof(struct nfs_url
));
270 urls
->server
= strdup(url
+ 6);
271 if (urls
->server
== NULL
) {
272 nfs_destroy_url(urls
);
273 rpc_set_error(nfs
->rpc
, "Out of memory");
277 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
278 urls
->server
[0] == '?') {
280 flagsp
= strchr(urls
->server
, '?');
283 nfs_destroy_url(urls
);
284 rpc_set_error(nfs
->rpc
, "Invalid server string");
288 strp
= strchr(urls
->server
, '/');
291 flagsp
= strchr(urls
->server
, '?');
294 nfs_destroy_url(urls
);
295 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
299 urls
->path
= strdup(strp
);
300 if (urls
->path
== NULL
) {
301 nfs_destroy_url(urls
);
302 rpc_set_error(nfs
->rpc
, "Out of memory");
308 flagsp
= strchr(urls
->path
, '?');
312 strp
= strrchr(urls
->path
, '/');
315 flagsp
= strchr(urls
->path
, '?');
318 nfs_destroy_url(urls
);
319 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
322 urls
->file
= strdup(strp
);
323 if (urls
->path
== NULL
) {
324 nfs_destroy_url(urls
);
325 rpc_set_error(nfs
->rpc
, "Out of memory");
329 flagsp
= strchr(urls
->file
, '?');
336 if (urls
->file
&& !strlen(urls
->file
)) {
340 nfs_destroy_url(urls
);
341 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
346 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
348 flagsp
= strchr(strp
, '&');
352 strp2
= strchr(strp
, '=');
356 nfs_set_context_args(nfs
, strp
, strp2
);
360 if (urls
->server
&& strlen(urls
->server
) <= 1) {
368 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
370 return nfs_parse_url(nfs
, url
, 0, 0);
373 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
375 return nfs_parse_url(nfs
, url
, 1, 0);
378 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
380 return nfs_parse_url(nfs
, url
, 0, 1);
384 void nfs_destroy_url(struct nfs_url
*url
)
394 struct nfs_context
*nfs_init_context(void)
396 struct nfs_context
*nfs
;
398 nfs
= malloc(sizeof(struct nfs_context
));
402 memset(nfs
, 0, sizeof(struct nfs_context
));
404 nfs
->rpc
= rpc_init_context();
405 if (nfs
->rpc
== NULL
) {
410 nfs
->cwd
= strdup("/");
415 void nfs_destroy_context(struct nfs_context
*nfs
)
417 rpc_destroy_context(nfs
->rpc
);
435 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
436 free(nfs
->rootfh
.data
.data_val
);
437 nfs
->rootfh
.data
.data_val
= NULL
;
440 while (nfs
->dircache
) {
441 struct nfsdir
*nfsdir
= nfs
->dircache
;
442 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
443 nfs_free_nfsdir(nfsdir
);
458 void free_rpc_cb_data(struct rpc_cb_data
*data
)
465 static void rpc_connect_program_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
467 struct rpc_cb_data
*data
= private_data
;
469 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
471 /* Dont want any more callbacks even if the socket is closed */
472 rpc
->connect_cb
= NULL
;
474 if (status
== RPC_STATUS_ERROR
) {
475 data
->cb(rpc
, status
, command_data
, data
->private_data
);
476 free_rpc_cb_data(data
);
479 if (status
== RPC_STATUS_CANCEL
) {
480 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
481 free_rpc_cb_data(data
);
485 data
->cb(rpc
, status
, NULL
, data
->private_data
);
486 free_rpc_cb_data(data
);
489 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
491 struct rpc_cb_data
*data
= private_data
;
493 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
495 /* Dont want any more callbacks even if the socket is closed */
496 rpc
->connect_cb
= NULL
;
498 if (status
== RPC_STATUS_ERROR
) {
499 data
->cb(rpc
, status
, command_data
, data
->private_data
);
500 free_rpc_cb_data(data
);
503 if (status
== RPC_STATUS_CANCEL
) {
504 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
505 free_rpc_cb_data(data
);
509 switch (data
->program
) {
511 if (rpc_mount3_null_async(rpc
, rpc_connect_program_5_cb
,
513 data
->cb(rpc
, status
, command_data
, data
->private_data
);
514 free_rpc_cb_data(data
);
519 if (rpc_nfs3_null_async(rpc
, rpc_connect_program_5_cb
,
521 data
->cb(rpc
, status
, command_data
, data
->private_data
);
522 free_rpc_cb_data(data
);
528 data
->cb(rpc
, status
, NULL
, data
->private_data
);
529 free_rpc_cb_data(data
);
532 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
534 struct rpc_cb_data
*data
= private_data
;
535 struct pmap3_string_result
*gar
;
536 uint32_t rpc_port
= 0;
539 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
541 if (status
== RPC_STATUS_ERROR
) {
542 data
->cb(rpc
, status
, command_data
, data
->private_data
);
543 free_rpc_cb_data(data
);
546 if (status
== RPC_STATUS_CANCEL
) {
547 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
548 free_rpc_cb_data(data
);
552 switch (rpc
->s
.ss_family
) {
554 rpc_port
= *(uint32_t *)command_data
;
557 /* ouch. portmapper and ipv6 are not great */
559 if (gar
->addr
== NULL
) {
562 ptr
= strrchr(gar
->addr
, '.');
566 rpc_port
= atoi(ptr
+ 1);
568 ptr
= strrchr(gar
->addr
, '.');
572 rpc_port
+= 256 * atoi(ptr
+ 1);
576 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
577 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
578 free_rpc_cb_data(data
);
582 rpc_disconnect(rpc
, "normal disconnect");
583 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
584 data
->cb(rpc
, status
, command_data
, data
->private_data
);
585 free_rpc_cb_data(data
);
590 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
592 struct rpc_cb_data
*data
= private_data
;
593 struct pmap3_mapping map
;
595 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
597 if (status
== RPC_STATUS_ERROR
) {
598 data
->cb(rpc
, status
, command_data
, data
->private_data
);
599 free_rpc_cb_data(data
);
602 if (status
== RPC_STATUS_CANCEL
) {
603 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
604 free_rpc_cb_data(data
);
608 switch (rpc
->s
.ss_family
) {
610 if (rpc_pmap2_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
611 data
->cb(rpc
, status
, command_data
, data
->private_data
);
612 free_rpc_cb_data(data
);
617 map
.prog
=data
->program
;
618 map
.vers
=data
->version
;
622 if (rpc_pmap3_getaddr_async(rpc
, &map
, rpc_connect_program_3_cb
, private_data
) != 0) {
623 data
->cb(rpc
, status
, command_data
, data
->private_data
);
624 free_rpc_cb_data(data
);
631 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
633 struct rpc_cb_data
*data
= private_data
;
635 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
637 /* Dont want any more callbacks even if the socket is closed */
638 rpc
->connect_cb
= NULL
;
640 if (status
== RPC_STATUS_ERROR
) {
641 data
->cb(rpc
, status
, command_data
, data
->private_data
);
642 free_rpc_cb_data(data
);
645 if (status
== RPC_STATUS_CANCEL
) {
646 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
647 free_rpc_cb_data(data
);
651 switch (rpc
->s
.ss_family
) {
653 if (rpc_pmap2_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
654 data
->cb(rpc
, status
, command_data
, data
->private_data
);
655 free_rpc_cb_data(data
);
660 if (rpc_pmap3_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
661 data
->cb(rpc
, status
, command_data
, data
->private_data
);
662 free_rpc_cb_data(data
);
669 int rpc_connect_program_async(struct rpc_context
*rpc
, const char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
671 struct rpc_cb_data
*data
;
673 data
= malloc(sizeof(struct rpc_cb_data
));
677 memset(data
, 0, sizeof(struct rpc_cb_data
));
678 data
->server
= strdup(server
);
679 data
->program
= program
;
680 data
->version
= version
;
683 data
->private_data
= private_data
;
685 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
686 rpc_set_error(rpc
, "Failed to start connection");
687 free_rpc_cb_data(data
);
693 static void free_nfs_cb_data(struct nfs_cb_data
*data
)
695 if (data
->continue_data
!= NULL
) {
696 assert(data
->free_continue_data
);
697 data
->free_continue_data(data
->continue_data
);
700 free(data
->saved_path
);
701 free(data
->fh
.data
.data_val
);
707 static void free_nfsfh(struct nfsfh
*nfsfh
)
709 if (nfsfh
->fh
.data
.data_val
!= NULL
) {
710 free(nfsfh
->fh
.data
.data_val
);
711 nfsfh
->fh
.data
.data_val
= NULL
;
718 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
720 struct nfs_cb_data
*data
= private_data
;
721 struct nfs_context
*nfs
= data
->nfs
;
723 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
725 if (status
== RPC_STATUS_ERROR
) {
726 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
727 free_nfs_cb_data(data
);
730 if (status
== RPC_STATUS_CANCEL
) {
731 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
732 free_nfs_cb_data(data
);
736 data
->cb(0, nfs
, NULL
, data
->private_data
);
737 free_nfs_cb_data(data
);
740 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
742 struct nfs_cb_data
*data
= private_data
;
743 struct nfs_context
*nfs
= data
->nfs
;
744 FSINFO3res
*res
= command_data
;
745 struct GETATTR3args args
;
747 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
749 if (status
== RPC_STATUS_ERROR
) {
750 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
751 free_nfs_cb_data(data
);
754 if (status
== RPC_STATUS_CANCEL
) {
755 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
756 free_nfs_cb_data(data
);
760 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
761 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
763 memset(&args
, 0, sizeof(GETATTR3args
));
764 args
.object
= nfs
->rootfh
;
766 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
767 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
768 free_nfs_cb_data(data
);
773 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
775 struct nfs_cb_data
*data
= private_data
;
776 struct nfs_context
*nfs
= data
->nfs
;
777 struct FSINFO3args args
;
779 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
781 if (status
== RPC_STATUS_ERROR
) {
782 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
783 free_nfs_cb_data(data
);
786 if (status
== RPC_STATUS_CANCEL
) {
787 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
788 free_nfs_cb_data(data
);
792 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
793 rpc_set_autoreconnect(rpc
);
795 args
.fsroot
= nfs
->rootfh
;
796 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
797 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
798 free_nfs_cb_data(data
);
803 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
805 struct nfs_cb_data
*data
= private_data
;
806 struct nfs_context
*nfs
= data
->nfs
;
809 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
811 if (status
== RPC_STATUS_ERROR
) {
812 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
813 free_nfs_cb_data(data
);
816 if (status
== RPC_STATUS_CANCEL
) {
817 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
818 free_nfs_cb_data(data
);
823 if (res
->fhs_status
!= MNT3_OK
) {
824 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
));
825 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
826 free_nfs_cb_data(data
);
830 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
831 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
832 if (nfs
->rootfh
.data
.data_val
== NULL
) {
833 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
834 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
835 free_nfs_cb_data(data
);
838 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
840 rpc_disconnect(rpc
, "normal disconnect");
842 if (rpc_connect_program_async(nfs
->rpc
, nfs
->server
, NFS_PROGRAM
, NFS_V3
, nfs_mount_8_cb
, data
) != 0) {
843 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
844 free_nfs_cb_data(data
);
850 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
852 struct nfs_cb_data
*data
= private_data
;
853 struct nfs_context
*nfs
= data
->nfs
;
855 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
857 if (status
== RPC_STATUS_ERROR
) {
858 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
859 free_nfs_cb_data(data
);
862 if (status
== RPC_STATUS_CANCEL
) {
863 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
864 free_nfs_cb_data(data
);
868 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
869 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
870 free_nfs_cb_data(data
);
876 * Async call for mounting an nfs share and geting the root filehandle
878 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
880 struct nfs_cb_data
*data
;
881 char *new_server
, *new_export
;
883 data
= malloc(sizeof(struct nfs_cb_data
));
885 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
888 memset(data
, 0, sizeof(struct nfs_cb_data
));
889 new_server
= strdup(server
);
890 new_export
= strdup(export
);
891 if (nfs
->server
!= NULL
) {
894 nfs
->server
= new_server
;
895 if (nfs
->export
!= NULL
) {
898 nfs
->export
= new_export
;
901 data
->private_data
= private_data
;
903 if (rpc_connect_program_async(nfs
->rpc
, server
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_5_cb
, data
) != 0) {
904 rpc_set_error(nfs
->rpc
, "Failed to start connection");
905 free_nfs_cb_data(data
);
915 * Functions to first look up a path, component by component, and then finally call a specific function once
916 * the filehandle for the final component is found.
918 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
920 struct nfs_cb_data
*data
= private_data
;
921 struct nfs_context
*nfs
= data
->nfs
;
925 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
927 if (status
== RPC_STATUS_ERROR
) {
928 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
929 free_nfs_cb_data(data
);
932 if (status
== RPC_STATUS_CANCEL
) {
933 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
934 free_nfs_cb_data(data
);
939 if (res
->status
!= NFS3_OK
) {
940 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
));
941 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
942 free_nfs_cb_data(data
);
946 attr
= res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
?
947 &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
:
950 /* This function will always invoke the callback and cleanup
951 * for failures. So no need to check the return value.
953 nfs_lookup_path_async_internal(nfs
, attr
, data
, &res
->LOOKUP3res_u
.resok
.object
);
956 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
961 while (*data
->path
== '/') {
966 slash
= strchr(path
, '/');
968 /* Clear slash so that path is a zero terminated string for
969 * the current path component. Set it back to '/' again later
970 * when we are finished referencing this component so that
971 * data->saved_path will still point to the full
975 data
->path
= slash
+1;
977 while (*data
->path
!= 0) {
983 data
->fh
.data
.data_len
= fh
->data
.data_len
;
984 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
985 if (data
->fh
.data
.data_val
== NULL
) {
986 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
987 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
988 free_nfs_cb_data(data
);
991 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
995 data
->continue_cb(nfs
, attr
, data
);
999 memset(&args
, 0, sizeof(LOOKUP3args
));
1000 args
.what
.dir
= *fh
;
1001 args
.what
.name
= path
;
1003 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
1004 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
1005 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1006 free_nfs_cb_data(data
);
1009 if (slash
!= NULL
) {
1015 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
1021 while ((str
= strstr(path
, "//"))) {
1029 while ((str
= strstr(path
, "/./"))) {
1036 /* ^/../ -> error */
1037 if (!strncmp(path
, "/../", 4)) {
1038 rpc_set_error(nfs
->rpc
,
1039 "Absolute path starts with '/../' "
1040 "during normalization");
1044 /* ^[^/] -> error */
1045 if (path
[0] != '/') {
1046 rpc_set_error(nfs
->rpc
,
1047 "Absolute path does not start with '/'");
1051 /* /string/../ -> / */
1052 while ((str
= strstr(path
, "/../"))) {
1055 if (!strncmp(path
, "/../", 4)) {
1056 rpc_set_error(nfs
->rpc
,
1057 "Absolute path starts with '/../' "
1058 "during normalization");
1063 while (*tmp
!= '/') {
1067 while((*(tmp
++) = *(str
++)) != '\0')
1074 if (path
[len
- 1] == '/') {
1075 path
[len
- 1] = '\0';
1079 if (path
[0] == '\0') {
1080 rpc_set_error(nfs
->rpc
,
1081 "Absolute path became '' "
1082 "during normalization");
1088 if (!strcmp(&path
[len
- 2], "/.")) {
1089 path
[len
- 2] = '\0';
1094 /* ^/..$ -> error */
1095 if (!strcmp(path
, "/..")) {
1096 rpc_set_error(nfs
->rpc
,
1097 "Absolute path is '/..' "
1098 "during normalization");
1102 /* /string/..$ -> / */
1104 if (!strcmp(&path
[len
- 3], "/..")) {
1105 char *tmp
= &path
[len
- 3];
1106 while (*--tmp
!= '/')
1115 static void nfs_lookup_path_getattr_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1117 struct nfs_cb_data
*data
= private_data
;
1118 struct nfs_context
*nfs
= data
->nfs
;
1122 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1124 if (status
== RPC_STATUS_ERROR
) {
1125 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1126 free_nfs_cb_data(data
);
1129 if (status
== RPC_STATUS_CANCEL
) {
1130 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1131 free_nfs_cb_data(data
);
1136 if (res
->status
!= NFS3_OK
) {
1137 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
));
1138 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1139 free_nfs_cb_data(data
);
1143 attr
= &res
->GETATTR3res_u
.resok
.obj_attributes
;
1144 /* This function will always invoke the callback and cleanup
1145 * for failures. So no need to check the return value.
1147 nfs_lookup_path_async_internal(nfs
, attr
, data
, &nfs
->rootfh
);
1150 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
)
1152 struct nfs_cb_data
*data
;
1153 struct GETATTR3args args
;
1155 if (path
[0] == '\0') {
1159 data
= malloc(sizeof(struct nfs_cb_data
));
1161 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1162 "nfs_cb_data structure");
1163 if (free_continue_data
)
1164 free_continue_data(continue_data
);
1167 memset(data
, 0, sizeof(struct nfs_cb_data
));
1170 data
->continue_cb
= continue_cb
;
1171 data
->continue_data
= continue_data
;
1172 data
->free_continue_data
= free_continue_data
;
1173 data
->continue_int
= continue_int
;
1174 data
->private_data
= private_data
;
1175 if (path
[0] == '/') {
1176 data
->saved_path
= strdup(path
);
1178 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1179 if (data
->saved_path
== NULL
) {
1180 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1181 "malloc path string");
1182 free_nfs_cb_data(data
);
1185 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1188 if (data
->saved_path
== NULL
) {
1189 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1190 free_nfs_cb_data(data
);
1193 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1194 free_nfs_cb_data(data
);
1198 data
->path
= data
->saved_path
;
1199 if (data
->path
[0]) {
1200 /* This function will always invoke the callback and cleanup
1201 * for failures. So no need to check the return value.
1203 nfs_lookup_path_async_internal(nfs
, NULL
, data
, &nfs
->rootfh
);
1207 /* We have a request for "", so just perform a GETATTR3 so we can
1208 * return the attributes to the caller.
1210 memset(&args
, 0, sizeof(GETATTR3args
));
1211 args
.object
= nfs
->rootfh
;
1212 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lookup_path_getattr_cb
, &args
, data
) != 0) {
1213 free_nfs_cb_data(data
);
1223 static dev_t
specdata3_to_rdev(struct specdata3
*rdev
)
1226 return makedev(rdev
->specdata1
, rdev
->specdata2
);
1232 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1235 struct nfs_cb_data
*data
= private_data
;
1236 struct nfs_context
*nfs
= data
->nfs
;
1243 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1245 if (status
== RPC_STATUS_ERROR
) {
1246 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1247 free_nfs_cb_data(data
);
1250 if (status
== RPC_STATUS_CANCEL
) {
1251 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1252 free_nfs_cb_data(data
);
1257 if (res
->status
!= NFS3_OK
) {
1258 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
));
1259 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1260 free_nfs_cb_data(data
);
1264 st
.st_dev
= res
->GETATTR3res_u
.resok
.obj_attributes
.fsid
;
1265 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1266 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1267 switch (res
->GETATTR3res_u
.resok
.obj_attributes
.type
) {
1269 st
.st_mode
|= S_IFREG
;
1272 st
.st_mode
|= S_IFDIR
;
1275 st
.st_mode
|= S_IFBLK
;
1278 st
.st_mode
|= S_IFCHR
;
1281 st
.st_mode
|= S_IFLNK
;
1284 st
.st_mode
|= S_IFSOCK
;
1287 st
.st_mode
|= S_IFIFO
;
1290 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1291 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1292 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1293 st
.st_rdev
= specdata3_to_rdev(&res
->GETATTR3res_u
.resok
.obj_attributes
.rdev
);
1294 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1296 st
.st_blksize
= NFS_BLKSIZE
;
1297 st
.st_blocks
= (res
->GETATTR3res_u
.resok
.obj_attributes
.used
+ 512 - 1) / 512;
1299 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1300 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1301 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1302 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1303 st
.st_atim
.tv_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.nseconds
;
1304 st
.st_mtim
.tv_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.nseconds
;
1305 st
.st_ctim
.tv_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.nseconds
;
1308 data
->cb(0, nfs
, &st
, data
->private_data
);
1309 free_nfs_cb_data(data
);
1312 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1314 struct GETATTR3args args
;
1316 memset(&args
, 0, sizeof(GETATTR3args
));
1317 args
.object
= data
->fh
;
1319 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1320 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1321 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1322 free_nfs_cb_data(data
);
1328 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1330 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1331 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1340 * Async nfs_stat64()
1342 static void nfs_stat64_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1345 struct nfs_cb_data
*data
= private_data
;
1346 struct nfs_context
*nfs
= data
->nfs
;
1347 struct nfs_stat_64 st
;
1349 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1351 if (status
== RPC_STATUS_ERROR
) {
1352 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1353 free_nfs_cb_data(data
);
1356 if (status
== RPC_STATUS_CANCEL
) {
1357 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1358 free_nfs_cb_data(data
);
1363 if (res
->status
!= NFS3_OK
) {
1364 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
));
1365 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1366 free_nfs_cb_data(data
);
1370 st
.nfs_dev
= res
->GETATTR3res_u
.resok
.obj_attributes
.fsid
;
1371 st
.nfs_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1372 st
.nfs_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1373 switch (res
->GETATTR3res_u
.resok
.obj_attributes
.type
) {
1375 st
.nfs_mode
|= S_IFREG
;
1378 st
.nfs_mode
|= S_IFDIR
;
1381 st
.nfs_mode
|= S_IFBLK
;
1384 st
.nfs_mode
|= S_IFCHR
;
1387 st
.nfs_mode
|= S_IFLNK
;
1390 st
.nfs_mode
|= S_IFSOCK
;
1393 st
.nfs_mode
|= S_IFIFO
;
1396 st
.nfs_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1397 st
.nfs_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1398 st
.nfs_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1399 st
.nfs_rdev
= specdata3_to_rdev(&res
->GETATTR3res_u
.resok
.obj_attributes
.rdev
);
1400 st
.nfs_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1401 st
.nfs_blksize
= NFS_BLKSIZE
;
1402 st
.nfs_blocks
= (res
->GETATTR3res_u
.resok
.obj_attributes
.used
+ 512 - 1) / 512;
1403 st
.nfs_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1404 st
.nfs_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1405 st
.nfs_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1406 st
.nfs_atime_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.nseconds
;
1407 st
.nfs_mtime_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.nseconds
;
1408 st
.nfs_ctime_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.nseconds
;
1409 st
.nfs_used
= res
->GETATTR3res_u
.resok
.obj_attributes
.used
;
1411 data
->cb(0, nfs
, &st
, data
->private_data
);
1412 free_nfs_cb_data(data
);
1415 static int nfs_stat64_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1417 struct GETATTR3args args
;
1419 memset(&args
, 0, sizeof(GETATTR3args
));
1420 args
.object
= data
->fh
;
1422 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
1423 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1424 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1425 free_nfs_cb_data(data
);
1431 int nfs_stat64_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1433 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat64_continue_internal
, NULL
, NULL
, 0) != 0) {
1434 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1444 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1446 struct nfs_cb_data
*data
= private_data
;
1447 struct nfs_context
*nfs
= data
->nfs
;
1448 struct nfsfh
*nfsfh
;
1451 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1453 if (status
== RPC_STATUS_ERROR
) {
1454 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1455 free_nfs_cb_data(data
);
1458 if (status
== RPC_STATUS_CANCEL
) {
1459 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1460 free_nfs_cb_data(data
);
1465 if (res
->status
!= NFS3_OK
) {
1466 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1467 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1468 free_nfs_cb_data(data
);
1472 nfsfh
= malloc(sizeof(struct nfsfh
));
1473 if (nfsfh
== NULL
) {
1474 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1475 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1476 free_nfs_cb_data(data
);
1479 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1481 if (data
->continue_int
& O_SYNC
) {
1484 if (data
->continue_int
& O_APPEND
) {
1485 nfsfh
->is_append
= 1;
1488 /* steal the filehandle */
1489 nfsfh
->fh
= data
->fh
;
1490 data
->fh
.data
.data_val
= NULL
;
1492 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1493 free_nfs_cb_data(data
);
1496 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1499 struct nfs_cb_data
*data
= private_data
;
1500 struct nfs_context
*nfs
= data
->nfs
;
1501 struct nfsfh
*nfsfh
;
1502 unsigned int nfsmode
= 0;
1504 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1506 if (status
== RPC_STATUS_ERROR
) {
1507 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1508 free_nfs_cb_data(data
);
1511 if (status
== RPC_STATUS_CANCEL
) {
1512 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1513 free_nfs_cb_data(data
);
1518 if (res
->status
!= NFS3_OK
) {
1519 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
));
1520 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1521 free_nfs_cb_data(data
);
1525 if (data
->continue_int
& O_WRONLY
) {
1526 nfsmode
|= ACCESS3_MODIFY
;
1528 if (data
->continue_int
& O_RDWR
) {
1529 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1531 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1532 nfsmode
|= ACCESS3_READ
;
1536 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1537 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1538 nfsmode
&ACCESS3_READ
?'r':'-',
1539 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1540 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1541 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1542 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1543 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1544 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1545 free_nfs_cb_data(data
);
1549 /* Try to truncate it if we were requested to */
1550 if ((data
->continue_int
& O_TRUNC
) &&
1551 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1554 memset(&args
, 0, sizeof(SETATTR3args
));
1555 args
.object
= data
->fh
;
1556 args
.new_attributes
.size
.set_it
= 1;
1557 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1559 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1561 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1562 "SETATTR call for %s", data
->path
);
1563 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1564 data
->private_data
);
1565 free_nfs_cb_data(data
);
1571 nfsfh
= malloc(sizeof(struct nfsfh
));
1572 if (nfsfh
== NULL
) {
1573 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1574 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1575 free_nfs_cb_data(data
);
1578 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1580 if (data
->continue_int
& O_SYNC
) {
1583 if (data
->continue_int
& O_APPEND
) {
1584 nfsfh
->is_append
= 1;
1587 /* steal the filehandle */
1588 nfsfh
->fh
= data
->fh
;
1589 data
->fh
.data
.data_val
= NULL
;
1591 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1592 free_nfs_cb_data(data
);
1595 static int nfs_open_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1600 if (data
->continue_int
& O_WRONLY
) {
1601 nfsmode
|= ACCESS3_MODIFY
;
1603 if (data
->continue_int
& O_RDWR
) {
1604 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1606 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1607 nfsmode
|= ACCESS3_READ
;
1610 memset(&args
, 0, sizeof(ACCESS3args
));
1611 args
.object
= data
->fh
;
1612 args
.access
= nfsmode
;
1614 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1615 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1616 "call for %s", data
->path
);
1617 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1618 data
->private_data
);
1619 free_nfs_cb_data(data
);
1625 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1627 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1628 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1639 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1641 /* steal saved_path */
1643 nfs
->cwd
= data
->saved_path
;
1644 data
->saved_path
= NULL
;
1646 data
->cb(0, nfs
, NULL
, data
->private_data
);
1647 free_nfs_cb_data(data
);
1652 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1654 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1655 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1666 static void nfs_fill_READ3args(READ3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
)
1668 memset(args
, 0, sizeof(READ3args
));
1669 args
->file
= fh
->fh
;
1670 args
->offset
= offset
;
1671 args
->count
= count
;
1674 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1676 struct nfs_mcb_data
*mdata
= private_data
;
1677 struct nfs_cb_data
*data
= mdata
->data
;
1678 struct nfs_context
*nfs
= data
->nfs
;
1681 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1685 if (status
== RPC_STATUS_ERROR
) {
1686 /* flag the failure but do not invoke callback until we have received all responses */
1689 if (status
== RPC_STATUS_CANCEL
) {
1690 /* flag the cancellation but do not invoke callback until we have received all responses */
1694 if (status
== RPC_STATUS_SUCCESS
) {
1696 if (res
->status
!= NFS3_OK
) {
1697 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1700 uint64_t count
= res
->READ3res_u
.resok
.count
;
1702 if (mdata
->update_pos
)
1703 data
->nfsfh
->offset
+= count
;
1705 /* if we have more than one call or we have received a short read we need a reassembly buffer */
1706 if (data
->num_calls
|| (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
)) {
1707 if (data
->buffer
== NULL
) {
1708 data
->buffer
= malloc(data
->count
);
1709 if (data
->buffer
== NULL
) {
1710 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data
->count
);
1716 if (count
<= mdata
->count
) {
1717 /* copy data into reassembly buffer if we have one */
1718 if (data
->buffer
!= NULL
) {
1719 memcpy(&data
->buffer
[mdata
->offset
- data
->offset
], res
->READ3res_u
.resok
.data
.data_val
, count
);
1721 if (data
->max_offset
< mdata
->offset
+ count
) {
1722 data
->max_offset
= mdata
->offset
+ count
;
1725 rpc_set_error(nfs
->rpc
, "NFS: Read overflow. Server has sent more data than requested!");
1729 /* check if we have received a short read */
1730 if (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
) {
1732 rpc_set_error(nfs
->rpc
, "NFS: Read failed. No bytes read and not at EOF!");
1735 /* reissue reminder of this read request */
1737 mdata
->offset
+= count
;
1738 mdata
->count
-= count
;
1739 nfs_fill_READ3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
);
1740 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) == 0) {
1744 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1754 if (data
->num_calls
> 0) {
1755 /* still waiting for more replies */
1758 if (data
->oom
!= 0) {
1759 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1760 free_nfs_cb_data(data
);
1763 if (data
->error
!= 0) {
1764 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1765 free_nfs_cb_data(data
);
1768 if (data
->cancel
!= 0) {
1769 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1770 free_nfs_cb_data(data
);
1775 if (data
->max_offset
> data
->org_offset
+ data
->org_count
) {
1776 data
->max_offset
= data
->org_offset
+ data
->org_count
;
1778 data
->cb(data
->max_offset
- data
->org_offset
, nfs
, data
->buffer
+ (data
->org_offset
- data
->offset
), data
->private_data
);
1780 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1783 data
->nfsfh
->ra
.fh_offset
= data
->max_offset
;
1784 if (data
->nfsfh
->ra
.cur_ra
) {
1785 free(data
->nfsfh
->ra
.buf
);
1786 data
->nfsfh
->ra
.buf
= data
->buffer
;
1787 data
->nfsfh
->ra
.buf_offset
= data
->offset
;
1788 data
->nfsfh
->ra
.buf_count
= data
->count
;
1789 data
->nfsfh
->ra
.buf_ts
= time(NULL
);
1790 data
->buffer
= NULL
;
1792 free_nfs_cb_data(data
);
1795 static void nfs_ra_invalidate(struct nfsfh
*nfsfh
) {
1796 free(nfsfh
->ra
.buf
);
1797 nfsfh
->ra
.buf
= NULL
;
1798 nfsfh
->ra
.buf_offset
= 0;
1799 nfsfh
->ra
.buf_count
= 0;
1800 nfsfh
->ra
.buf_ts
= time(NULL
);
1801 nfsfh
->ra
.cur_ra
= NFS_BLKSIZE
;
1804 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
)
1806 struct nfs_cb_data
*data
;
1808 data
= malloc(sizeof(struct nfs_cb_data
));
1810 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1813 memset(data
, 0, sizeof(struct nfs_cb_data
));
1816 data
->private_data
= private_data
;
1817 data
->nfsfh
= nfsfh
;
1818 data
->org_offset
= offset
;
1819 data
->org_count
= count
;
1821 assert(data
->num_calls
== 0);
1823 if (nfs
->rpc
->readahead
&& time(NULL
) - nfsfh
->ra
.buf_ts
> NFS_RA_TIMEOUT
) {
1824 /* readahead cache timeout */
1825 nfs_ra_invalidate(nfsfh
);
1828 if (nfs
->rpc
->readahead
) {
1829 if (offset
>= nfsfh
->ra
.last_offset
&&
1830 offset
- NFS_BLKSIZE
<= nfsfh
->ra
.fh_offset
+ nfsfh
->ra
.cur_ra
) {
1831 if (nfs
->rpc
->readahead
> nfsfh
->ra
.cur_ra
) {
1832 nfsfh
->ra
.cur_ra
<<= 1;
1835 nfsfh
->ra
.cur_ra
= NFS_BLKSIZE
;
1838 nfsfh
->ra
.last_offset
= offset
;
1840 if (nfsfh
->ra
.buf_offset
<= offset
&&
1841 nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
>= offset
+ count
) {
1842 /* serve request completely from cache */
1843 data
->buffer
= malloc(count
);
1844 if (data
->buffer
== NULL
) {
1845 free_nfs_cb_data(data
);
1848 memcpy(data
->buffer
, nfsfh
->ra
.buf
+ (offset
- nfsfh
->ra
.buf_offset
), count
);
1849 data
->cb(count
, nfs
, data
->buffer
, data
->private_data
);
1850 nfsfh
->ra
.fh_offset
= offset
+ count
;
1851 free_nfs_cb_data(data
);
1855 /* align start offset to blocksize */
1856 count
+= offset
& (NFS_BLKSIZE
- 1);
1857 offset
&= ~(NFS_BLKSIZE
- 1);
1859 /* align end offset to blocksize and add readahead */
1860 count
+= nfsfh
->ra
.cur_ra
- 1;
1861 count
&= ~(NFS_BLKSIZE
- 1);
1863 data
->buffer
= malloc(count
);
1864 if (data
->buffer
== NULL
) {
1865 free_nfs_cb_data(data
);
1868 data
->offset
= offset
;
1869 data
->count
= count
;
1871 if (nfsfh
->ra
.buf_count
&& nfsfh
->ra
.buf_offset
<= offset
&&
1872 nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
>= offset
) {
1873 /* serve request partially from cache */
1874 size_t overlap
= (nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
) - offset
;
1875 if (overlap
> count
) count
= overlap
;
1876 memcpy(data
->buffer
, nfsfh
->ra
.buf
+ (offset
- nfsfh
->ra
.buf_offset
), overlap
);
1881 data
->offset
= offset
;
1882 data
->count
= count
;
1885 data
->max_offset
= offset
;
1887 /* chop requests into chunks of at most READMAX bytes if necessary.
1888 * we send all reads in parallel so that performance is still good.
1891 uint64_t readcount
= count
;
1892 struct nfs_mcb_data
*mdata
;
1895 if (readcount
> nfs_get_readmax(nfs
)) {
1896 readcount
= nfs_get_readmax(nfs
);
1899 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1900 if (mdata
== NULL
) {
1901 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1902 if (data
->num_calls
== 0) {
1903 free_nfs_cb_data(data
);
1909 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1911 mdata
->offset
= offset
;
1912 mdata
->count
= readcount
;
1913 mdata
->update_pos
= update_pos
;
1915 nfs_fill_READ3args(&args
, nfsfh
, offset
, readcount
);
1917 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1918 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1920 if (data
->num_calls
== 0) {
1921 free_nfs_cb_data(data
);
1929 offset
+= readcount
;
1931 } while (count
> 0);
1936 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1938 return nfs_pread_async_internal(nfs
, nfsfh
, offset
, count
, cb
, private_data
, 0);
1944 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1946 return nfs_pread_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
, 1);
1954 static void nfs_fill_WRITE3args (WRITE3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
,
1957 memset(args
, 0, sizeof(WRITE3args
));
1958 args
->file
= fh
->fh
;
1959 args
->offset
= offset
;
1960 args
->count
= count
;
1961 args
->stable
= fh
->is_sync
? FILE_SYNC
: UNSTABLE
;
1962 args
->data
.data_len
= count
;
1963 args
->data
.data_val
= buf
;
1966 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1968 struct nfs_mcb_data
*mdata
= private_data
;
1969 struct nfs_cb_data
*data
= mdata
->data
;
1970 struct nfs_context
*nfs
= data
->nfs
;
1973 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1977 if (status
== RPC_STATUS_ERROR
) {
1978 /* flag the failure but do not invoke callback until we have received all responses */
1981 if (status
== RPC_STATUS_CANCEL
) {
1982 /* flag the cancellation but do not invoke callback until we have received all responses */
1986 if (status
== RPC_STATUS_SUCCESS
) {
1988 if (res
->status
!= NFS3_OK
) {
1989 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1992 uint64_t count
= res
->WRITE3res_u
.resok
.count
;
1994 if (mdata
->update_pos
)
1995 data
->nfsfh
->offset
+= count
;
1997 if (count
< mdata
->count
) {
1999 rpc_set_error(nfs
->rpc
, "NFS: Write failed. No bytes written!");
2002 /* reissue reminder of this write request */
2004 mdata
->offset
+= count
;
2005 mdata
->count
-= count
;
2007 nfs_fill_WRITE3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
,
2008 &data
->usrbuf
[mdata
->offset
- data
->offset
]);
2009 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) == 0) {
2013 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
2019 if (data
->max_offset
< mdata
->offset
+ count
) {
2020 data
->max_offset
= mdata
->offset
+ count
;
2028 if (data
->num_calls
> 0) {
2029 /* still waiting for more replies */
2032 if (data
->oom
!= 0) {
2033 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
2034 free_nfs_cb_data(data
);
2037 if (data
->error
!= 0) {
2038 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2039 free_nfs_cb_data(data
);
2042 if (data
->cancel
!= 0) {
2043 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2044 free_nfs_cb_data(data
);
2048 data
->cb(data
->max_offset
- data
->offset
, nfs
, NULL
, data
->private_data
);
2050 free_nfs_cb_data(data
);
2054 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
)
2056 struct nfs_cb_data
*data
;
2058 data
= malloc(sizeof(struct nfs_cb_data
));
2060 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2063 memset(data
, 0, sizeof(struct nfs_cb_data
));
2066 data
->private_data
= private_data
;
2067 data
->nfsfh
= nfsfh
;
2070 /* hello, clang-analyzer */
2071 assert(data
->num_calls
== 0);
2073 /* chop requests into chunks of at most WRITEMAX bytes if necessary.
2074 * we send all writes in parallel so that performance is still good.
2076 data
->max_offset
= offset
;
2077 data
->offset
= offset
;
2080 uint64_t writecount
= count
;
2081 struct nfs_mcb_data
*mdata
;
2084 if (writecount
> nfs_get_writemax(nfs
)) {
2085 writecount
= nfs_get_writemax(nfs
);
2088 mdata
= malloc(sizeof(struct nfs_mcb_data
));
2089 if (mdata
== NULL
) {
2090 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
2091 if (data
->num_calls
== 0) {
2092 free_nfs_cb_data(data
);
2098 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
2100 mdata
->offset
= offset
;
2101 mdata
->count
= writecount
;
2102 mdata
->update_pos
= update_pos
;
2104 nfs_fill_WRITE3args(&args
, nfsfh
, offset
, writecount
, &buf
[offset
- data
->offset
]);
2106 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
2107 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
2109 if (data
->num_calls
== 0) {
2110 free_nfs_cb_data(data
);
2117 count
-= writecount
;
2118 offset
+= writecount
;
2120 } while (count
> 0);
2125 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
)
2127 return nfs_pwrite_async_internal(nfs
, nfsfh
, offset
, count
, buf
, cb
, private_data
, 0);
2133 static void nfs_write_append_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2135 struct nfs_cb_data
*data
= private_data
;
2136 struct nfs_context
*nfs
= data
->nfs
;
2139 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2141 if (status
== RPC_STATUS_ERROR
) {
2142 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2143 free_nfs_cb_data(data
);
2146 if (status
== RPC_STATUS_CANCEL
) {
2147 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2148 free_nfs_cb_data(data
);
2153 if (res
->status
!= NFS3_OK
) {
2154 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2155 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2156 free_nfs_cb_data(data
);
2160 if (nfs_pwrite_async_internal(nfs
, data
->nfsfh
, res
->GETATTR3res_u
.resok
.obj_attributes
.size
, data
->count
, data
->usrbuf
, data
->cb
, data
->private_data
, 1) != 0) {
2161 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2162 free_nfs_cb_data(data
);
2165 free_nfs_cb_data(data
);
2168 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
2170 nfs_ra_invalidate(nfsfh
);
2171 if (nfsfh
->is_append
) {
2172 struct GETATTR3args args
;
2173 struct nfs_cb_data
*data
;
2175 data
= malloc(sizeof(struct nfs_cb_data
));
2177 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2180 memset(data
, 0, sizeof(struct nfs_cb_data
));
2183 data
->private_data
= private_data
;
2184 data
->nfsfh
= nfsfh
;
2186 data
->count
= count
;
2188 memset(&args
, 0, sizeof(GETATTR3args
));
2189 args
.object
= nfsfh
->fh
;
2191 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_write_append_cb
, &args
, data
) != 0) {
2192 rpc_set_error(nfs
->rpc
, "out of memory: failed to send GETATTR");
2193 free_nfs_cb_data(data
);
2198 return nfs_pwrite_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
, 1);
2208 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2211 cb(0, nfs
, NULL
, private_data
);
2222 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2224 struct nfs_cb_data
*data
;
2225 struct GETATTR3args args
;
2227 data
= malloc(sizeof(struct nfs_cb_data
));
2229 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2232 memset(data
, 0, sizeof(struct nfs_cb_data
));
2235 data
->private_data
= private_data
;
2237 memset(&args
, 0, sizeof(GETATTR3args
));
2238 args
.object
= nfsfh
->fh
;
2240 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
2241 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
2242 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2243 free_nfs_cb_data(data
);
2252 int nfs_fstat64_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2254 struct nfs_cb_data
*data
;
2255 struct GETATTR3args args
;
2257 data
= malloc(sizeof(struct nfs_cb_data
));
2259 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2262 memset(data
, 0, sizeof(struct nfs_cb_data
));
2265 data
->private_data
= private_data
;
2267 memset(&args
, 0, sizeof(GETATTR3args
));
2268 args
.object
= nfsfh
->fh
;
2270 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
2271 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
2272 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2273 free_nfs_cb_data(data
);
2284 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2286 struct nfs_cb_data
*data
= private_data
;
2287 struct nfs_context
*nfs
= data
->nfs
;
2290 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2292 if (status
== RPC_STATUS_ERROR
) {
2293 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2294 free_nfs_cb_data(data
);
2297 if (status
== RPC_STATUS_CANCEL
) {
2298 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2299 free_nfs_cb_data(data
);
2304 if (res
->status
!= NFS3_OK
) {
2305 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2306 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2307 free_nfs_cb_data(data
);
2311 data
->cb(0, nfs
, NULL
, data
->private_data
);
2312 free_nfs_cb_data(data
);
2315 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2317 struct nfs_cb_data
*data
;
2318 struct COMMIT3args args
;
2320 data
= malloc(sizeof(struct nfs_cb_data
));
2322 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2325 memset(data
, 0, sizeof(struct nfs_cb_data
));
2328 data
->private_data
= private_data
;
2330 args
.file
= nfsfh
->fh
;
2333 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
2334 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
2335 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2336 free_nfs_cb_data(data
);
2348 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2350 struct nfs_cb_data
*data
= private_data
;
2351 struct nfs_context
*nfs
= data
->nfs
;
2354 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2356 if (status
== RPC_STATUS_ERROR
) {
2357 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2358 free_nfs_cb_data(data
);
2361 if (status
== RPC_STATUS_CANCEL
) {
2362 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2363 free_nfs_cb_data(data
);
2368 if (res
->status
!= NFS3_OK
) {
2369 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2370 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2371 free_nfs_cb_data(data
);
2375 data
->cb(0, nfs
, NULL
, data
->private_data
);
2376 free_nfs_cb_data(data
);
2379 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
2381 struct nfs_cb_data
*data
;
2384 data
= malloc(sizeof(struct nfs_cb_data
));
2386 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2389 memset(data
, 0, sizeof(struct nfs_cb_data
));
2392 data
->private_data
= private_data
;
2394 memset(&args
, 0, sizeof(SETATTR3args
));
2395 args
.object
= nfsfh
->fh
;
2396 args
.new_attributes
.size
.set_it
= 1;
2397 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2399 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2400 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2401 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2402 free_nfs_cb_data(data
);
2412 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2414 uint64_t offset
= data
->continue_int
;
2417 nfsfh
.fh
= data
->fh
;
2419 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2420 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2421 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2422 free_nfs_cb_data(data
);
2425 free_nfs_cb_data(data
);
2429 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2435 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2436 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2449 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2452 struct nfs_cb_data
*data
= private_data
;
2453 struct nfs_context
*nfs
= data
->nfs
;
2454 char *str
= data
->continue_data
;
2456 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2458 str
= &str
[strlen(str
) + 1];
2460 if (status
== RPC_STATUS_ERROR
) {
2461 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2462 free_nfs_cb_data(data
);
2465 if (status
== RPC_STATUS_CANCEL
) {
2466 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2467 free_nfs_cb_data(data
);
2472 if (res
->status
!= NFS3_OK
) {
2473 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
));
2474 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2475 free_nfs_cb_data(data
);
2479 data
->cb(0, nfs
, NULL
, data
->private_data
);
2480 free_nfs_cb_data(data
);
2483 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2485 char *str
= data
->continue_data
;
2488 str
= &str
[strlen(str
) + 1];
2490 memset(&args
, 0, sizeof(MKDIR3args
));
2491 args
.where
.dir
= data
->fh
;
2492 args
.where
.name
= str
;
2493 args
.attributes
.mode
.set_it
= 1;
2494 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2496 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2497 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2498 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2499 free_nfs_cb_data(data
);
2505 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2510 new_path
= strdup(path
);
2511 if (new_path
== NULL
) {
2512 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2516 ptr
= strrchr(new_path
, '/');
2519 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2524 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2525 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2526 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2540 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2543 struct nfs_cb_data
*data
= private_data
;
2544 struct nfs_context
*nfs
= data
->nfs
;
2545 char *str
= data
->continue_data
;
2547 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2549 str
= &str
[strlen(str
) + 1];
2551 if (status
== RPC_STATUS_ERROR
) {
2552 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2553 free_nfs_cb_data(data
);
2556 if (status
== RPC_STATUS_CANCEL
) {
2557 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2558 free_nfs_cb_data(data
);
2563 if (res
->status
!= NFS3_OK
) {
2564 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
));
2565 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2566 free_nfs_cb_data(data
);
2570 data
->cb(0, nfs
, NULL
, data
->private_data
);
2571 free_nfs_cb_data(data
);
2574 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2576 char *str
= data
->continue_data
;
2579 str
= &str
[strlen(str
) + 1];
2581 args
.object
.dir
= data
->fh
;
2582 args
.object
.name
= str
;
2583 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2584 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2585 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2586 free_nfs_cb_data(data
);
2592 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2597 new_path
= strdup(path
);
2598 if (new_path
== NULL
) {
2599 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2603 ptr
= strrchr(new_path
, '/');
2606 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2611 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2612 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2613 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2626 struct create_cb_data
{
2632 static void free_create_cb_data(void *ptr
)
2634 struct create_cb_data
*data
= ptr
;
2640 static void nfs_create_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2642 struct nfs_cb_data
*data
= private_data
;
2643 struct nfs_context
*nfs
= data
->nfs
;
2644 struct nfsfh
*nfsfh
;
2647 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2649 if (status
== RPC_STATUS_ERROR
) {
2650 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2651 free_nfs_cb_data(data
);
2655 if (status
== RPC_STATUS_CANCEL
) {
2656 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2657 free_nfs_cb_data(data
);
2663 if (res
->status
!= NFS3_OK
) {
2664 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2665 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2666 free_nfs_cb_data(data
);
2671 data
->cb(0, nfs
, data
->nfsfh
, data
->private_data
);
2672 free_nfs_cb_data(data
);
2675 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2678 struct nfs_cb_data
*data
= private_data
;
2679 struct nfs_context
*nfs
= data
->nfs
;
2680 struct nfsfh
*nfsfh
;
2681 struct create_cb_data
*cb_data
= data
->continue_data
;
2682 char *str
= cb_data
->path
;
2684 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2686 if (status
== RPC_STATUS_ERROR
) {
2687 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2688 free_nfs_cb_data(data
);
2691 if (status
== RPC_STATUS_CANCEL
) {
2692 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2693 free_nfs_cb_data(data
);
2697 str
= &str
[strlen(str
) + 1];
2699 if (res
->status
!= NFS3_OK
) {
2700 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
));
2701 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2706 nfsfh
= malloc(sizeof(struct nfsfh
));
2707 if (nfsfh
== NULL
) {
2708 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2709 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2710 free_nfs_cb_data(data
);
2713 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2715 if (cb_data
->flags
& O_SYNC
) {
2718 if (cb_data
->flags
& O_APPEND
) {
2719 nfsfh
->is_append
= 1;
2722 /* copy the filehandle */
2723 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2724 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2725 if (nfsfh
->fh
.data
.data_val
== NULL
) {
2726 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh structure");
2727 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2728 free_nfs_cb_data(data
);
2732 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2734 /* Try to truncate it if we were requested to */
2735 if (cb_data
->flags
& O_TRUNC
) {
2738 data
->nfsfh
= nfsfh
;
2740 memset(&args
, 0, sizeof(SETATTR3args
));
2741 args
.object
= nfsfh
->fh
;
2742 args
.new_attributes
.size
.set_it
= 1;
2743 args
.new_attributes
.size
.set_size3_u
.size
= 0;
2745 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_create_trunc_cb
,
2746 &args
, data
) != 0) {
2747 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
2748 "SETATTR call for %s", data
->path
);
2749 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
2750 data
->private_data
);
2751 free_nfs_cb_data(data
);
2758 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2759 free_nfs_cb_data(data
);
2764 static void nfs_create_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2767 struct nfs_cb_data
*data
= private_data
;
2768 struct nfs_context
*nfs
= data
->nfs
;
2769 struct create_cb_data
*cb_data
= data
->continue_data
;
2770 char *str
= cb_data
->path
;
2773 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2775 if (status
== RPC_STATUS_ERROR
) {
2776 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2777 free_nfs_cb_data(data
);
2780 if (status
== RPC_STATUS_CANCEL
) {
2781 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2782 free_nfs_cb_data(data
);
2786 str
= &str
[strlen(str
) + 1];
2788 if (res
->status
!= NFS3_OK
) {
2789 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
));
2790 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2791 free_nfs_cb_data(data
);
2795 memset(&args
, 0, sizeof(LOOKUP3args
));
2796 args
.what
.dir
= data
->fh
;
2797 args
.what
.name
= str
;
2799 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2800 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2801 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2802 free_nfs_cb_data(data
);
2808 static int nfs_create_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2810 struct create_cb_data
*cb_data
= data
->continue_data
;
2811 char *str
= cb_data
->path
;
2814 str
= &str
[strlen(str
) + 1];
2816 memset(&args
, 0, sizeof(CREATE3args
));
2817 args
.where
.dir
= data
->fh
;
2818 args
.where
.name
= str
;
2819 args
.how
.mode
= (cb_data
->flags
& O_EXCL
) ? GUARDED
: UNCHECKED
;
2820 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2821 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
;
2823 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_create_1_cb
, &args
, data
) != 0) {
2824 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2825 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2826 free_nfs_cb_data(data
);
2832 int nfs_create_async(struct nfs_context
*nfs
, const char *path
, int flags
, int mode
, nfs_cb cb
, void *private_data
)
2834 struct create_cb_data
*cb_data
;
2837 cb_data
= malloc(sizeof(struct create_cb_data
));
2838 if (cb_data
== NULL
) {
2839 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2843 cb_data
->path
= strdup(path
);
2844 if (cb_data
->path
== NULL
) {
2845 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2850 ptr
= strrchr(cb_data
->path
, '/');
2852 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2853 free_create_cb_data(cb_data
);
2858 cb_data
->flags
= flags
;
2859 cb_data
->mode
= mode
;
2861 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2862 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_create_continue_internal
, cb_data
, free_create_cb_data
, 0) != 0) {
2863 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2870 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2872 return nfs_create_async(nfs
, path
, 0, mode
, cb
, private_data
);
2881 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2884 struct nfs_cb_data
*data
= private_data
;
2885 struct nfs_context
*nfs
= data
->nfs
;
2886 char *str
= data
->continue_data
;
2888 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2890 str
= &str
[strlen(str
) + 1];
2892 if (status
== RPC_STATUS_ERROR
) {
2893 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2894 free_nfs_cb_data(data
);
2897 if (status
== RPC_STATUS_CANCEL
) {
2898 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2899 free_nfs_cb_data(data
);
2904 if (res
->status
!= NFS3_OK
) {
2905 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
));
2906 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2907 free_nfs_cb_data(data
);
2911 data
->cb(0, nfs
, NULL
, data
->private_data
);
2912 free_nfs_cb_data(data
);
2915 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2917 char *str
= data
->continue_data
;
2918 struct REMOVE3args args
;
2920 str
= &str
[strlen(str
) + 1];
2922 args
.object
.dir
= data
->fh
;
2923 args
.object
.name
= str
;
2924 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2925 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2926 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2927 free_nfs_cb_data(data
);
2933 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2938 new_path
= strdup(path
);
2939 if (new_path
== NULL
) {
2940 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2944 ptr
= strrchr(new_path
, '/');
2947 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2952 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2953 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2954 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2965 struct mknod_cb_data
{
2972 static void free_mknod_cb_data(void *ptr
)
2974 struct mknod_cb_data
*data
= ptr
;
2980 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2983 struct nfs_cb_data
*data
= private_data
;
2984 struct nfs_context
*nfs
= data
->nfs
;
2985 char *str
= data
->continue_data
;
2987 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2989 str
= &str
[strlen(str
) + 1];
2991 if (status
== RPC_STATUS_ERROR
) {
2992 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2993 free_nfs_cb_data(data
);
2996 if (status
== RPC_STATUS_CANCEL
) {
2997 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2998 free_nfs_cb_data(data
);
3003 if (res
->status
!= NFS3_OK
) {
3004 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
));
3005 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3006 free_nfs_cb_data(data
);
3010 data
->cb(0, nfs
, NULL
, data
->private_data
);
3011 free_nfs_cb_data(data
);
3014 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3016 struct mknod_cb_data
*cb_data
= data
->continue_data
;
3017 char *str
= cb_data
->path
;
3020 str
= &str
[strlen(str
) + 1];
3022 args
.where
.dir
= data
->fh
;
3023 args
.where
.name
= str
;
3024 switch (cb_data
->mode
& S_IFMT
) {
3026 args
.what
.type
= NF3CHR
;
3027 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
3028 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
);
3029 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
3030 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
3033 args
.what
.type
= NF3BLK
;
3034 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
3035 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
);
3036 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
3037 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
3039 args
.what
.type
= NF3SOCK
;
3040 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
3041 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
);
3044 args
.what
.type
= NF3FIFO
;
3045 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
3046 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
);
3049 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
3050 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3051 free_nfs_cb_data(data
);
3055 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
3056 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3057 free_nfs_cb_data(data
);
3063 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
3066 struct mknod_cb_data
*cb_data
;
3068 cb_data
= malloc(sizeof(struct mknod_cb_data
));
3069 if (cb_data
== NULL
) {
3070 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
3074 cb_data
->path
= strdup(path
);
3075 if (cb_data
->path
== NULL
) {
3076 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
3081 ptr
= strrchr(cb_data
->path
, '/');
3083 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
3084 free_mknod_cb_data(cb_data
);
3089 cb_data
->mode
= mode
;
3090 cb_data
->major
= major(dev
);
3091 cb_data
->minor
= minor(dev
);
3093 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
3094 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
3095 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3106 /* ReadDirPlus Emulation Callback data */
3107 struct rdpe_cb_data
{
3110 struct nfs_cb_data
*data
;
3113 /* ReadDirPlus Emulation LOOKUP Callback data */
3114 struct rdpe_lookup_cb_data
{
3115 struct rdpe_cb_data
*rdpe_cb_data
;
3116 struct nfsdirent
*nfsdirent
;
3119 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
3120 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3122 LOOKUP3res
*res
= command_data
;
3123 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
3124 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
3125 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
3126 struct nfsdir
*nfsdir
= data
->continue_data
;
3127 struct nfs_context
*nfs
= data
->nfs
;
3128 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
3130 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3132 free(rdpe_lookup_cb_data
);
3134 rdpe_cb_data
->getattrcount
--;
3136 if (status
== RPC_STATUS_ERROR
) {
3137 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
3138 "failed with RPC_STATUS_ERROR");
3139 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
3141 if (status
== RPC_STATUS_CANCEL
) {
3142 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
3143 "failed with RPC_STATUS_CANCEL");
3144 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
3146 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
3147 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
3148 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
3150 nfsdirent
->type
= attributes
->type
;
3151 nfsdirent
->mode
= attributes
->mode
;
3152 nfsdirent
->size
= attributes
->size
;
3154 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
3155 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
3156 nfsdirent
->atime_nsec
= attributes
->atime
.nseconds
;
3157 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
3158 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
3159 nfsdirent
->mtime_nsec
= attributes
->mtime
.nseconds
;
3160 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
3161 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
3162 nfsdirent
->ctime_nsec
= attributes
->ctime
.nseconds
;
3163 nfsdirent
->uid
= attributes
->uid
;
3164 nfsdirent
->gid
= attributes
->gid
;
3165 nfsdirent
->nlink
= attributes
->nlink
;
3166 nfsdirent
->dev
= attributes
->fsid
;
3167 nfsdirent
->rdev
= specdata3_to_rdev(&attributes
->rdev
);
3168 nfsdirent
->blksize
= NFS_BLKSIZE
;
3169 nfsdirent
->blocks
= (attributes
->used
+ 512 - 1) / 512;
3170 nfsdirent
->used
= attributes
->used
;
3174 if (rdpe_cb_data
->getattrcount
== 0) {
3175 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
3176 rpc_set_error(nfs
->rpc
, "READDIRPLUS emulation "
3177 "failed: %s", rpc_get_error(rpc
));
3178 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
3179 data
->private_data
);
3180 nfs_free_nfsdir(nfsdir
);
3182 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
3186 data
->continue_data
= NULL
;
3187 free_nfs_cb_data(data
);
3191 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3193 READDIR3res
*res
= command_data
;
3194 struct nfs_cb_data
*data
= private_data
;
3195 struct nfs_context
*nfs
= data
->nfs
;
3196 struct nfsdir
*nfsdir
= data
->continue_data
;
3197 struct nfsdirent
*nfsdirent
;
3198 struct entry3
*entry
;
3199 uint64_t cookie
= 0;
3200 struct rdpe_cb_data
*rdpe_cb_data
;
3202 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3204 if (status
== RPC_STATUS_ERROR
) {
3205 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3206 nfs_free_nfsdir(nfsdir
);
3207 data
->continue_data
= NULL
;
3208 free_nfs_cb_data(data
);
3212 if (status
== RPC_STATUS_CANCEL
) {
3213 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3214 nfs_free_nfsdir(nfsdir
);
3215 data
->continue_data
= NULL
;
3216 free_nfs_cb_data(data
);
3220 if (res
->status
!= NFS3_OK
) {
3221 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
));
3222 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3223 nfs_free_nfsdir(nfsdir
);
3224 data
->continue_data
= NULL
;
3225 free_nfs_cb_data(data
);
3229 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
3230 while (entry
!= NULL
) {
3231 nfsdirent
= malloc(sizeof(struct nfsdirent
));
3232 if (nfsdirent
== NULL
) {
3233 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
3234 nfs_free_nfsdir(nfsdir
);
3235 data
->continue_data
= NULL
;
3236 free_nfs_cb_data(data
);
3239 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
3240 nfsdirent
->name
= strdup(entry
->name
);
3241 if (nfsdirent
->name
== NULL
) {
3242 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
3244 nfs_free_nfsdir(nfsdir
);
3245 data
->continue_data
= NULL
;
3246 free_nfs_cb_data(data
);
3249 nfsdirent
->inode
= entry
->fileid
;
3251 nfsdirent
->next
= nfsdir
->entries
;
3252 nfsdir
->entries
= nfsdirent
;
3254 cookie
= entry
->cookie
;
3255 entry
= entry
->nextentry
;
3258 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
3261 args
.dir
= data
->fh
;
3262 args
.cookie
= cookie
;
3263 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
3266 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
3267 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
3268 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3269 nfs_free_nfsdir(nfsdir
);
3270 data
->continue_data
= NULL
;
3271 free_nfs_cb_data(data
);
3277 if (res
->READDIR3res_u
.resok
.dir_attributes
.attributes_follow
)
3278 nfsdir
->attr
= res
->READDIR3res_u
.resok
.dir_attributes
.post_op_attr_u
.attributes
;
3280 /* steal the dirhandle */
3281 nfsdir
->current
= nfsdir
->entries
;
3283 if (nfsdir
->entries
) {
3284 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
3285 rdpe_cb_data
->getattrcount
= 0;
3286 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
3287 rdpe_cb_data
->data
= data
;
3288 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
3289 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
3292 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
3293 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
3294 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
3296 memset(&args
, 0, sizeof(LOOKUP3args
));
3297 args
.what
.dir
= data
->fh
;
3298 args
.what
.name
= nfsdirent
->name
;
3300 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
3301 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
3303 /* if we have already commands in flight, we cant just stop, we have to wait for the
3304 * commands in flight to complete
3306 if (rdpe_cb_data
->getattrcount
> 0) {
3307 nfs_free_nfsdir(nfsdir
);
3308 data
->continue_data
= NULL
;
3309 free_nfs_cb_data(data
);
3310 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
3311 free(rdpe_lookup_cb_data
);
3315 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3316 nfs_free_nfsdir(nfsdir
);
3317 data
->continue_data
= NULL
;
3318 free_nfs_cb_data(data
);
3319 free(rdpe_lookup_cb_data
);
3323 rdpe_cb_data
->getattrcount
++;
3328 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3330 READDIRPLUS3res
*res
= command_data
;
3331 struct nfs_cb_data
*data
= private_data
;
3332 struct nfs_context
*nfs
= data
->nfs
;
3333 struct nfsdir
*nfsdir
= data
->continue_data
;
3334 struct entryplus3
*entry
;
3335 uint64_t cookie
= 0;
3337 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3339 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
3342 args
.dir
= data
->fh
;
3343 args
.cookie
= cookie
;
3344 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
3347 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
3348 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
3349 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3350 nfs_free_nfsdir(nfsdir
);
3351 data
->continue_data
= NULL
;
3352 free_nfs_cb_data(data
);
3358 if (status
== RPC_STATUS_CANCEL
) {
3359 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3360 nfs_free_nfsdir(nfsdir
);
3361 data
->continue_data
= NULL
;
3362 free_nfs_cb_data(data
);
3366 if (res
->status
!= NFS3_OK
) {
3367 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
));
3368 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3369 nfs_free_nfsdir(nfsdir
);
3370 data
->continue_data
= NULL
;
3371 free_nfs_cb_data(data
);
3375 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
3376 while (entry
!= NULL
) {
3377 struct nfsdirent
*nfsdirent
;
3379 nfsdirent
= malloc(sizeof(struct nfsdirent
));
3380 if (nfsdirent
== NULL
) {
3381 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
3382 nfs_free_nfsdir(nfsdir
);
3383 data
->continue_data
= NULL
;
3384 free_nfs_cb_data(data
);
3387 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
3388 nfsdirent
->name
= strdup(entry
->name
);
3389 if (nfsdirent
->name
== NULL
) {
3390 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
3392 nfs_free_nfsdir(nfsdir
);
3393 data
->continue_data
= NULL
;
3394 free_nfs_cb_data(data
);
3397 nfsdirent
->inode
= entry
->fileid
;
3398 if (entry
->name_attributes
.attributes_follow
) {
3399 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
3400 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
3401 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
3403 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
3404 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
3405 nfsdirent
->atime_nsec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
;
3406 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
3407 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
3408 nfsdirent
->mtime_nsec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
;
3409 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
3410 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
3411 nfsdirent
->ctime_nsec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
;
3412 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
3413 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
3414 nfsdirent
->nlink
= entry
->name_attributes
.post_op_attr_u
.attributes
.nlink
;
3415 nfsdirent
->dev
= entry
->name_attributes
.post_op_attr_u
.attributes
.fsid
;
3416 nfsdirent
->rdev
= specdata3_to_rdev(&entry
->name_attributes
.post_op_attr_u
.attributes
.rdev
);
3417 nfsdirent
->blksize
= NFS_BLKSIZE
;
3418 nfsdirent
->blocks
= (entry
->name_attributes
.post_op_attr_u
.attributes
.used
+ 512 - 1) / 512;
3419 nfsdirent
->used
= entry
->name_attributes
.post_op_attr_u
.attributes
.used
;
3422 nfsdirent
->next
= nfsdir
->entries
;
3423 nfsdir
->entries
= nfsdirent
;
3425 cookie
= entry
->cookie
;
3426 entry
= entry
->nextentry
;
3429 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
3430 READDIRPLUS3args args
;
3432 args
.dir
= data
->fh
;
3433 args
.cookie
= cookie
;
3434 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
3435 args
.dircount
= 8192;
3436 args
.maxcount
= 8192;
3438 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
3439 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
3440 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3441 nfs_free_nfsdir(nfsdir
);
3442 data
->continue_data
= NULL
;
3443 free_nfs_cb_data(data
);
3449 if (res
->READDIRPLUS3res_u
.resok
.dir_attributes
.attributes_follow
)
3450 nfsdir
->attr
= res
->READDIRPLUS3res_u
.resok
.dir_attributes
.post_op_attr_u
.attributes
;
3452 /* steal the dirhandle */
3453 data
->continue_data
= NULL
;
3454 nfsdir
->current
= nfsdir
->entries
;
3456 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
3457 free_nfs_cb_data(data
);
3460 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3462 READDIRPLUS3args args
;
3463 struct nfsdir
*nfsdir
= data
->continue_data
;;
3464 struct nfsdir
*cached
;
3466 cached
= nfs_dircache_find(nfs
, &data
->fh
);
3468 if (attr
&& attr
->mtime
.seconds
== cached
->attr
.mtime
.seconds
) {
3469 cached
->current
= cached
->entries
;
3470 data
->cb(0, nfs
, cached
, data
->private_data
);
3471 free_nfs_cb_data(data
);
3474 /* cache must be stale */
3475 nfs_free_nfsdir(cached
);
3479 nfsdir
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
3480 nfsdir
->fh
.data
.data_val
= malloc(nfsdir
->fh
.data
.data_len
);
3481 if (nfsdir
->fh
.data
.data_val
== NULL
) {
3482 rpc_set_error(nfs
->rpc
, "OOM when allocating fh for nfsdir");
3483 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3484 free_nfs_cb_data(data
);
3487 memcpy(nfsdir
->fh
.data
.data_val
, data
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3489 args
.dir
= data
->fh
;
3491 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
3492 args
.dircount
= 8192;
3493 args
.maxcount
= 8192;
3494 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
3495 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
3496 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3497 free_nfs_cb_data(data
);
3503 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3505 struct nfsdir
*nfsdir
;
3507 nfsdir
= malloc(sizeof(struct nfsdir
));
3508 if (nfsdir
== NULL
) {
3509 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
3512 memset(nfsdir
, 0, sizeof(struct nfsdir
));
3514 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
3515 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3523 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3525 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
3527 if (nfsdir
->current
!= NULL
) {
3528 nfsdir
->current
= nfsdir
->current
->next
;
3537 void nfs_closedir(struct nfs_context
*nfs
, struct nfsdir
*nfsdir
)
3539 nfs_dircache_add(nfs
, nfsdir
);
3546 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3557 struct lseek_cb_data
{
3558 struct nfs_context
*nfs
;
3559 struct nfsfh
*nfsfh
;
3565 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3568 struct lseek_cb_data
*data
= private_data
;
3569 struct nfs_context
*nfs
= data
->nfs
;
3572 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3574 if (status
== RPC_STATUS_ERROR
) {
3575 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3579 if (status
== RPC_STATUS_CANCEL
) {
3580 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3586 if (res
->status
!= NFS3_OK
) {
3587 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3588 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3593 size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3595 if (data
->offset
< 0 &&
3596 (uint64_t)(-data
->offset
) > size
) {
3597 data
->cb(-EINVAL
, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3599 data
->nfsfh
->offset
= data
->offset
+ size
;
3600 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3606 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3608 struct lseek_cb_data
*data
;
3609 struct GETATTR3args args
;
3611 if (whence
== SEEK_SET
) {
3613 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3615 nfsfh
->offset
= offset
;
3616 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3620 if (whence
== SEEK_CUR
) {
3622 nfsfh
->offset
< (uint64_t)(-offset
)) {
3623 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3625 nfsfh
->offset
+= offset
;
3626 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3631 data
= malloc(sizeof(struct lseek_cb_data
));
3633 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3638 data
->nfsfh
= nfsfh
;
3639 data
->offset
= offset
;
3641 data
->private_data
= private_data
;
3643 memset(&args
, 0, sizeof(GETATTR3args
));
3644 args
.object
= nfsfh
->fh
;
3646 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3647 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3660 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3663 struct nfs_cb_data
*data
= private_data
;
3664 struct nfs_context
*nfs
= data
->nfs
;
3665 struct statvfs svfs
;
3667 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3669 if (status
== RPC_STATUS_ERROR
) {
3670 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3671 free_nfs_cb_data(data
);
3674 if (status
== RPC_STATUS_CANCEL
) {
3675 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3676 free_nfs_cb_data(data
);
3681 if (res
->status
!= NFS3_OK
) {
3682 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
));
3683 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3684 free_nfs_cb_data(data
);
3688 svfs
.f_bsize
= NFS_BLKSIZE
;
3689 svfs
.f_frsize
= NFS_BLKSIZE
;
3690 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/NFS_BLKSIZE
;
3691 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/NFS_BLKSIZE
;
3692 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/NFS_BLKSIZE
;
3693 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3694 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3695 #if !defined(ANDROID)
3696 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3699 svfs
.f_namemax
= 256;
3702 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3703 free_nfs_cb_data(data
);
3706 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3710 args
.fsroot
= data
->fh
;
3711 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3712 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3713 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3714 free_nfs_cb_data(data
);
3720 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3722 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3723 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3736 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3739 struct nfs_cb_data
*data
= private_data
;
3740 struct nfs_context
*nfs
= data
->nfs
;
3742 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3744 if (status
== RPC_STATUS_ERROR
) {
3745 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3746 free_nfs_cb_data(data
);
3749 if (status
== RPC_STATUS_CANCEL
) {
3750 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3751 free_nfs_cb_data(data
);
3756 if (res
->status
!= NFS3_OK
) {
3757 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
));
3758 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3759 free_nfs_cb_data(data
);
3764 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3765 free_nfs_cb_data(data
);
3768 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3772 args
.symlink
= data
->fh
;
3774 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3775 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3776 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3777 free_nfs_cb_data(data
);
3783 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3785 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3786 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3799 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3801 struct nfs_cb_data
*data
= private_data
;
3802 struct nfs_context
*nfs
= data
->nfs
;
3805 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3807 if (status
== RPC_STATUS_ERROR
) {
3808 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3809 free_nfs_cb_data(data
);
3812 if (status
== RPC_STATUS_CANCEL
) {
3813 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3814 free_nfs_cb_data(data
);
3819 if (res
->status
!= NFS3_OK
) {
3820 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3821 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3822 free_nfs_cb_data(data
);
3826 data
->cb(0, nfs
, NULL
, data
->private_data
);
3827 free_nfs_cb_data(data
);
3830 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3834 memset(&args
, 0, sizeof(SETATTR3args
));
3835 args
.object
= data
->fh
;
3836 args
.new_attributes
.mode
.set_it
= 1;
3837 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3839 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3840 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3841 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3842 free_nfs_cb_data(data
);
3849 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3851 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3852 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3862 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3864 struct nfs_cb_data
*data
;
3866 data
= malloc(sizeof(struct nfs_cb_data
));
3868 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3871 memset(data
, 0, sizeof(struct nfs_cb_data
));
3874 data
->private_data
= private_data
;
3875 data
->continue_int
= mode
;
3876 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3877 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3878 if (data
->fh
.data
.data_val
== NULL
) {
3879 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3880 free_nfs_cb_data(data
);
3883 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3885 if (nfs_chmod_continue_internal(nfs
, NULL
, data
) != 0) {
3897 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3899 struct nfs_cb_data
*data
= private_data
;
3900 struct nfs_context
*nfs
= data
->nfs
;
3903 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3905 if (status
== RPC_STATUS_ERROR
) {
3906 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3907 free_nfs_cb_data(data
);
3910 if (status
== RPC_STATUS_CANCEL
) {
3911 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3912 free_nfs_cb_data(data
);
3917 if (res
->status
!= NFS3_OK
) {
3918 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3919 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3920 free_nfs_cb_data(data
);
3924 data
->cb(0, nfs
, NULL
, data
->private_data
);
3925 free_nfs_cb_data(data
);
3928 struct nfs_chown_data
{
3933 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3936 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3938 memset(&args
, 0, sizeof(SETATTR3args
));
3939 args
.object
= data
->fh
;
3940 if (chown_data
->uid
!= (uid_t
)-1) {
3941 args
.new_attributes
.uid
.set_it
= 1;
3942 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3944 if (chown_data
->gid
!= (gid_t
)-1) {
3945 args
.new_attributes
.gid
.set_it
= 1;
3946 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3949 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3950 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3951 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3952 free_nfs_cb_data(data
);
3959 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3961 struct nfs_chown_data
*chown_data
;
3963 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3964 if (chown_data
== NULL
) {
3965 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3969 chown_data
->uid
= uid
;
3970 chown_data
->gid
= gid
;
3972 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3973 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3984 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3986 struct nfs_cb_data
*data
;
3987 struct nfs_chown_data
*chown_data
;
3989 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3990 if (chown_data
== NULL
) {
3991 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3995 chown_data
->uid
= uid
;
3996 chown_data
->gid
= gid
;
3998 data
= malloc(sizeof(struct nfs_cb_data
));
4000 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
4004 memset(data
, 0, sizeof(struct nfs_cb_data
));
4007 data
->private_data
= private_data
;
4008 data
->continue_data
= chown_data
;
4009 data
->free_continue_data
= free
;
4010 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
4011 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
4012 if (data
->fh
.data
.data_val
== NULL
) {
4013 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
4014 free_nfs_cb_data(data
);
4017 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
4019 if (nfs_chown_continue_internal(nfs
, NULL
, data
) != 0) {
4033 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4035 struct nfs_cb_data
*data
= private_data
;
4036 struct nfs_context
*nfs
= data
->nfs
;
4039 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4041 if (status
== RPC_STATUS_ERROR
) {
4042 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4043 free_nfs_cb_data(data
);
4046 if (status
== RPC_STATUS_CANCEL
) {
4047 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4048 free_nfs_cb_data(data
);
4053 if (res
->status
!= NFS3_OK
) {
4054 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
4055 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4056 free_nfs_cb_data(data
);
4060 data
->cb(0, nfs
, NULL
, data
->private_data
);
4061 free_nfs_cb_data(data
);
4064 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4067 struct timeval
*utimes_data
= data
->continue_data
;
4069 memset(&args
, 0, sizeof(SETATTR3args
));
4070 args
.object
= data
->fh
;
4071 if (utimes_data
!= NULL
) {
4072 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
4073 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
4074 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
4075 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
4076 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
4077 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
4079 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
4080 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
4083 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
4084 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
4085 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4086 free_nfs_cb_data(data
);
4093 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
4095 struct timeval
*new_times
= NULL
;
4097 if (times
!= NULL
) {
4098 new_times
= malloc(sizeof(struct timeval
)*2);
4099 if (new_times
== NULL
) {
4100 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
4104 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
4107 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
4108 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4118 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
4120 struct timeval
*new_times
= NULL
;
4122 if (times
!= NULL
) {
4123 new_times
= malloc(sizeof(struct timeval
)*2);
4124 if (new_times
== NULL
) {
4125 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
4129 new_times
[0].tv_sec
= times
->actime
;
4130 new_times
[0].tv_usec
= 0;
4131 new_times
[1].tv_sec
= times
->modtime
;
4132 new_times
[1].tv_usec
= 0;
4135 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
4136 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4147 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4150 struct nfs_cb_data
*data
= private_data
;
4151 struct nfs_context
*nfs
= data
->nfs
;
4152 unsigned int nfsmode
= 0;
4154 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4156 if (status
== RPC_STATUS_ERROR
) {
4157 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4158 free_nfs_cb_data(data
);
4161 if (status
== RPC_STATUS_CANCEL
) {
4162 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4163 free_nfs_cb_data(data
);
4168 if (res
->status
!= NFS3_OK
) {
4169 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
));
4170 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4171 free_nfs_cb_data(data
);
4175 if (data
->continue_int
& R_OK
) {
4176 nfsmode
|= ACCESS3_READ
;
4178 if (data
->continue_int
& W_OK
) {
4179 nfsmode
|= ACCESS3_MODIFY
;
4181 if (data
->continue_int
& X_OK
) {
4182 nfsmode
|= ACCESS3_EXECUTE
;
4185 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
4186 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
4187 nfsmode
&ACCESS3_READ
?'r':'-',
4188 nfsmode
&ACCESS3_MODIFY
?'w':'-',
4189 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
4190 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
4191 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
4192 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
4193 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4194 free_nfs_cb_data(data
);
4198 data
->cb(0, nfs
, NULL
, data
->private_data
);
4199 free_nfs_cb_data(data
);
4202 static int nfs_access_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4207 if (data
->continue_int
& R_OK
) {
4208 nfsmode
|= ACCESS3_READ
;
4210 if (data
->continue_int
& W_OK
) {
4211 nfsmode
|= ACCESS3_MODIFY
;
4213 if (data
->continue_int
& X_OK
) {
4214 nfsmode
|= ACCESS3_EXECUTE
;
4217 memset(&args
, 0, sizeof(ACCESS3args
));
4218 args
.object
= data
->fh
;
4219 args
.access
= nfsmode
;
4221 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
4222 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
4223 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4224 free_nfs_cb_data(data
);
4230 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
4232 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
4233 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4245 struct nfs_symlink_data
{
4247 char *newpathparent
;
4248 char *newpathobject
;
4251 static void free_nfs_symlink_data(void *mem
)
4253 struct nfs_symlink_data
*data
= mem
;
4255 if (data
->oldpath
!= NULL
) {
4256 free(data
->oldpath
);
4258 if (data
->newpathparent
!= NULL
) {
4259 free(data
->newpathparent
);
4261 if (data
->newpathobject
!= NULL
) {
4262 free(data
->newpathobject
);
4267 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4270 struct nfs_cb_data
*data
= private_data
;
4271 struct nfs_context
*nfs
= data
->nfs
;
4272 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
4274 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4276 if (status
== RPC_STATUS_ERROR
) {
4277 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4278 free_nfs_cb_data(data
);
4281 if (status
== RPC_STATUS_CANCEL
) {
4282 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4283 free_nfs_cb_data(data
);
4288 if (res
->status
!= NFS3_OK
) {
4289 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
));
4290 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4291 free_nfs_cb_data(data
);
4295 data
->cb(0, nfs
, NULL
, data
->private_data
);
4296 free_nfs_cb_data(data
);
4299 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4301 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
4304 memset(&args
, 0, sizeof(SYMLINK3args
));
4305 args
.where
.dir
= data
->fh
;
4306 args
.where
.name
= symlink_data
->newpathobject
;
4307 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
4308 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
;
4309 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
4311 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
4312 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
4313 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4314 free_nfs_cb_data(data
);
4320 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4323 struct nfs_symlink_data
*symlink_data
;
4325 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
4326 if (symlink_data
== NULL
) {
4327 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
4330 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
4332 symlink_data
->oldpath
= strdup(oldpath
);
4333 if (symlink_data
->oldpath
== NULL
) {
4334 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4335 free_nfs_symlink_data(symlink_data
);
4339 symlink_data
->newpathparent
= strdup(newpath
);
4340 if (symlink_data
->newpathparent
== NULL
) {
4341 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
4342 free_nfs_symlink_data(symlink_data
);
4346 ptr
= strrchr(symlink_data
->newpathparent
, '/');
4348 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
4349 free_nfs_symlink_data(symlink_data
);
4355 symlink_data
->newpathobject
= strdup(ptr
);
4356 if (symlink_data
->newpathobject
== NULL
) {
4357 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
4358 free_nfs_symlink_data(symlink_data
);
4362 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
4363 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4375 struct nfs_rename_data
{
4378 struct nfs_fh3 olddir
;
4381 struct nfs_fh3 newdir
;
4384 static void free_nfs_rename_data(void *mem
)
4386 struct nfs_rename_data
*data
= mem
;
4388 if (data
->oldpath
!= NULL
) {
4389 free(data
->oldpath
);
4391 if (data
->olddir
.data
.data_val
!= NULL
) {
4392 free(data
->olddir
.data
.data_val
);
4394 if (data
->newpath
!= NULL
) {
4395 free(data
->newpath
);
4397 if (data
->newdir
.data
.data_val
!= NULL
) {
4398 free(data
->newdir
.data
.data_val
);
4403 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4406 struct nfs_cb_data
*data
= private_data
;
4407 struct nfs_context
*nfs
= data
->nfs
;
4408 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4410 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4412 if (status
== RPC_STATUS_ERROR
) {
4413 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4414 free_nfs_cb_data(data
);
4417 if (status
== RPC_STATUS_CANCEL
) {
4418 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4419 free_nfs_cb_data(data
);
4424 if (res
->status
!= NFS3_OK
) {
4425 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
));
4426 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4427 free_nfs_cb_data(data
);
4431 data
->cb(0, nfs
, NULL
, data
->private_data
);
4432 free_nfs_cb_data(data
);
4435 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4437 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4440 /* steal the filehandle */
4441 rename_data
->newdir
= data
->fh
;
4442 data
->fh
.data
.data_val
= NULL
;
4444 args
.from
.dir
= rename_data
->olddir
;
4445 args
.from
.name
= rename_data
->oldobject
;
4446 args
.to
.dir
= rename_data
->newdir
;
4447 args
.to
.name
= rename_data
->newobject
;
4448 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
4449 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
4450 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4451 free_nfs_cb_data(data
);
4458 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4460 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4461 char* newpath
= strdup(rename_data
->newpath
);
4463 rpc_set_error(nfs
->rpc
, "Out of memory. Could not allocate memory to store target path for rename");
4464 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4465 free_nfs_cb_data(data
);
4469 /* steal the filehandle */
4470 rename_data
->olddir
= data
->fh
;
4471 data
->fh
.data
.data_val
= NULL
;
4473 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) {
4474 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", newpath
);
4475 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4476 free_nfs_cb_data(data
);
4480 data
->continue_data
= NULL
;
4481 free_nfs_cb_data(data
);
4488 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4491 struct nfs_rename_data
*rename_data
;
4493 rename_data
= malloc(sizeof(struct nfs_rename_data
));
4494 if (rename_data
== NULL
) {
4495 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
4498 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
4500 rename_data
->oldpath
= strdup(oldpath
);
4501 if (rename_data
->oldpath
== NULL
) {
4502 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4503 free_nfs_rename_data(rename_data
);
4506 ptr
= strrchr(rename_data
->oldpath
, '/');
4508 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
4509 free_nfs_rename_data(rename_data
);
4514 rename_data
->oldobject
= ptr
;
4517 rename_data
->newpath
= strdup(newpath
);
4518 if (rename_data
->newpath
== NULL
) {
4519 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4520 free_nfs_rename_data(rename_data
);
4523 ptr
= strrchr(rename_data
->newpath
, '/');
4525 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4526 free_nfs_rename_data(rename_data
);
4531 rename_data
->newobject
= ptr
;
4534 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
4535 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4546 struct nfs_link_data
{
4548 struct nfs_fh3 oldfh
;
4551 struct nfs_fh3 newdir
;
4554 static void free_nfs_link_data(void *mem
)
4556 struct nfs_link_data
*data
= mem
;
4558 if (data
->oldpath
!= NULL
) {
4559 free(data
->oldpath
);
4561 if (data
->oldfh
.data
.data_val
!= NULL
) {
4562 free(data
->oldfh
.data
.data_val
);
4564 if (data
->newpath
!= NULL
) {
4565 free(data
->newpath
);
4567 if (data
->newdir
.data
.data_val
!= NULL
) {
4568 free(data
->newdir
.data
.data_val
);
4573 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4576 struct nfs_cb_data
*data
= private_data
;
4577 struct nfs_context
*nfs
= data
->nfs
;
4578 struct nfs_link_data
*link_data
= data
->continue_data
;
4580 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4582 if (status
== RPC_STATUS_ERROR
) {
4583 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4584 free_nfs_cb_data(data
);
4587 if (status
== RPC_STATUS_CANCEL
) {
4588 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4589 free_nfs_cb_data(data
);
4594 if (res
->status
!= NFS3_OK
) {
4595 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
));
4596 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4597 free_nfs_cb_data(data
);
4601 data
->cb(0, nfs
, NULL
, data
->private_data
);
4602 free_nfs_cb_data(data
);
4605 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4607 struct nfs_link_data
*link_data
= data
->continue_data
;
4610 /* steal the filehandle */
4611 link_data
->newdir
= data
->fh
;
4612 data
->fh
.data
.data_val
= NULL
;
4614 memset(&args
, 0, sizeof(LINK3args
));
4615 args
.file
= link_data
->oldfh
;
4616 args
.link
.dir
= link_data
->newdir
;
4617 args
.link
.name
= link_data
->newobject
;
4618 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4619 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4620 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4621 free_nfs_cb_data(data
);
4628 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4630 struct nfs_link_data
*link_data
= data
->continue_data
;
4632 /* steal the filehandle */
4633 link_data
->oldfh
= data
->fh
;
4634 data
->fh
.data
.data_val
= NULL
;
4636 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) {
4637 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4638 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4639 free_nfs_cb_data(data
);
4642 data
->continue_data
= NULL
;
4643 free_nfs_cb_data(data
);
4649 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4652 struct nfs_link_data
*link_data
;
4654 link_data
= malloc(sizeof(struct nfs_link_data
));
4655 if (link_data
== NULL
) {
4656 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4659 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4661 link_data
->oldpath
= strdup(oldpath
);
4662 if (link_data
->oldpath
== NULL
) {
4663 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4664 free_nfs_link_data(link_data
);
4668 link_data
->newpath
= strdup(newpath
);
4669 if (link_data
->newpath
== NULL
) {
4670 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4671 free_nfs_link_data(link_data
);
4674 ptr
= strrchr(link_data
->newpath
, '/');
4676 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4677 free_nfs_link_data(link_data
);
4682 link_data
->newobject
= ptr
;
4685 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4686 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4694 //qqq replace later with lseek()
4695 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4697 return nfsfh
->offset
;
4703 * Get the maximum supported READ3 size by the server
4705 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4707 return nfs
->readmax
;
4711 * Get the maximum supported WRITE3 size by the server
4713 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4715 return nfs
->writemax
;
4718 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4719 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4722 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4723 rpc_set_uid(nfs
->rpc
, uid
);
4726 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4727 rpc_set_gid(nfs
->rpc
, gid
);
4730 void nfs_set_readahead(struct nfs_context
*nfs
, uint32_t v
) {
4731 rpc_set_readahead(nfs
->rpc
, v
);
4734 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4739 va_start(ap
, error_string
);
4741 vsnprintf(str
, 1024, error_string
, ap
);
4742 if (nfs
->rpc
->error_string
!= NULL
) {
4743 free(nfs
->rpc
->error_string
);
4745 nfs
->rpc
->error_string
= str
;
4751 struct mount_cb_data
{
4757 static void free_mount_cb_data(struct mount_cb_data
*data
)
4759 if (data
->server
!= NULL
) {
4761 data
->server
= NULL
;
4767 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4769 struct mount_cb_data
*data
= private_data
;
4771 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4773 if (status
== RPC_STATUS_ERROR
) {
4774 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4775 free_mount_cb_data(data
);
4778 if (status
== RPC_STATUS_CANCEL
) {
4779 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4780 free_mount_cb_data(data
);
4784 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4785 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4786 rpc_set_error(rpc
, "Failed to disconnect\n");
4788 free_mount_cb_data(data
);
4791 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4793 struct mount_cb_data
*data
= private_data
;
4795 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4797 /* Dont want any more callbacks even if the socket is closed */
4798 rpc
->connect_cb
= NULL
;
4800 if (status
== RPC_STATUS_ERROR
) {
4801 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4802 free_mount_cb_data(data
);
4805 if (status
== RPC_STATUS_CANCEL
) {
4806 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4807 free_mount_cb_data(data
);
4811 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4812 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4813 free_mount_cb_data(data
);
4818 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4820 struct mount_cb_data
*data
;
4822 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4824 data
= malloc(sizeof(struct mount_cb_data
));
4828 memset(data
, 0, sizeof(struct mount_cb_data
));
4830 data
->private_data
= private_data
;
4831 data
->server
= strdup(server
);
4832 if (data
->server
== NULL
) {
4833 free_mount_cb_data(data
);
4836 if (rpc_connect_program_async(rpc
, data
->server
, MOUNT_PROGRAM
, MOUNT_V3
, mount_export_4_cb
, data
) != 0) {
4837 rpc_set_error(rpc
, "Failed to start connection");
4838 free_mount_cb_data(data
);
4845 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4847 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4851 const char *nfs_get_server(struct nfs_context
*nfs
) {
4855 const char *nfs_get_export(struct nfs_context
*nfs
) {
4859 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4860 return &nfs
->rootfh
;
4863 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {