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>
77 #include <sys/types.h>
80 #include "libnfs-zdr.h"
83 #include "libnfs-raw.h"
84 #include "libnfs-raw-mount.h"
85 #include "libnfs-raw-nfs.h"
86 #include "libnfs-raw-portmap.h"
87 #include "libnfs-private.h"
89 #define MAX_DIR_CACHE 128
90 #define MAX_LINK_COUNT 40
97 struct nfsdirent
*entries
;
98 struct nfsdirent
*current
;
101 struct nfs_readahead
{
103 uint64_t last_offset
;
116 struct nfs_readahead ra
;
120 struct rpc_context
*rpc
;
123 struct nfs_fh3 rootfh
;
127 struct nfsdir
*dircache
;
130 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
132 while (nfsdir
->entries
) {
133 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
134 if (nfsdir
->entries
->name
!= NULL
) {
135 free(nfsdir
->entries
->name
);
137 free(nfsdir
->entries
);
138 nfsdir
->entries
= dirent
;
140 free(nfsdir
->fh
.data
.data_val
);
144 static void nfs_dircache_add(struct nfs_context
*nfs
, struct nfsdir
*nfsdir
)
147 LIBNFS_LIST_ADD(&nfs
->dircache
, nfsdir
);
149 for (nfsdir
= nfs
->dircache
, i
= 0; nfsdir
; nfsdir
= nfsdir
->next
, i
++) {
150 if (i
> MAX_DIR_CACHE
) {
151 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
152 nfs_free_nfsdir(nfsdir
);
158 static struct nfsdir
*nfs_dircache_find(struct nfs_context
*nfs
, struct nfs_fh3
*fh
)
160 struct nfsdir
*nfsdir
;
162 for (nfsdir
= nfs
->dircache
; nfsdir
; nfsdir
= nfsdir
->next
) {
163 if (nfsdir
->fh
.data
.data_len
== fh
->data
.data_len
&&
164 !memcmp(nfsdir
->fh
.data
.data_val
, fh
->data
.data_val
, fh
->data
.data_len
)) {
165 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
174 typedef int (*continue_func
)(struct nfs_context
*nfs
, fattr3
*attr
,
175 struct nfs_cb_data
*data
);
178 struct nfs_context
*nfs
;
180 char *saved_path
, *path
;
181 int link_count
, no_follow
;
186 continue_func continue_cb
;
188 void (*free_continue_data
)(void *);
193 /* for multi-read/write calls. */
198 uint64_t offset
, count
, max_offset
, org_offset
, org_count
;
203 struct nfs_mcb_data
{
204 struct nfs_cb_data
*data
;
210 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, fattr3
*attr
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
211 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
);
213 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
215 rpc_set_auth(nfs
->rpc
, auth
);
218 int nfs_get_fd(struct nfs_context
*nfs
)
220 return rpc_get_fd(nfs
->rpc
);
223 int nfs_queue_length(struct nfs_context
*nfs
)
225 return rpc_queue_length(nfs
->rpc
);
228 int nfs_which_events(struct nfs_context
*nfs
)
230 return rpc_which_events(nfs
->rpc
);
233 int nfs_service(struct nfs_context
*nfs
, int revents
)
235 return rpc_service(nfs
->rpc
, revents
);
238 char *nfs_get_error(struct nfs_context
*nfs
)
240 return rpc_get_error(nfs
->rpc
);
243 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
245 if (!strcmp(arg
, "tcp-syncnt")) {
246 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
247 } else if (!strcmp(arg
, "uid")) {
248 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
249 } else if (!strcmp(arg
, "gid")) {
250 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
251 } else if (!strcmp(arg
, "readahaed")) {
252 rpc_set_readahead(nfs_get_rpc_context(nfs
), atoi(val
));
257 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
259 struct nfs_url
*urls
;
260 char *strp
, *flagsp
, *strp2
;
262 if (strncmp(url
, "nfs://", 6)) {
263 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
267 urls
= malloc(sizeof(struct nfs_url
));
269 rpc_set_error(nfs
->rpc
, "Out of memory");
273 memset(urls
, 0x00, sizeof(struct nfs_url
));
274 urls
->server
= strdup(url
+ 6);
275 if (urls
->server
== NULL
) {
276 nfs_destroy_url(urls
);
277 rpc_set_error(nfs
->rpc
, "Out of memory");
281 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
282 urls
->server
[0] == '?') {
284 flagsp
= strchr(urls
->server
, '?');
287 nfs_destroy_url(urls
);
288 rpc_set_error(nfs
->rpc
, "Invalid server string");
292 strp
= strchr(urls
->server
, '/');
295 flagsp
= strchr(urls
->server
, '?');
298 nfs_destroy_url(urls
);
299 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
303 urls
->path
= strdup(strp
);
304 if (urls
->path
== NULL
) {
305 nfs_destroy_url(urls
);
306 rpc_set_error(nfs
->rpc
, "Out of memory");
312 flagsp
= strchr(urls
->path
, '?');
316 strp
= strrchr(urls
->path
, '/');
319 flagsp
= strchr(urls
->path
, '?');
322 nfs_destroy_url(urls
);
323 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
326 urls
->file
= strdup(strp
);
327 if (urls
->path
== NULL
) {
328 nfs_destroy_url(urls
);
329 rpc_set_error(nfs
->rpc
, "Out of memory");
333 flagsp
= strchr(urls
->file
, '?');
340 if (urls
->file
&& !strlen(urls
->file
)) {
344 nfs_destroy_url(urls
);
345 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
350 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
352 flagsp
= strchr(strp
, '&');
356 strp2
= strchr(strp
, '=');
360 nfs_set_context_args(nfs
, strp
, strp2
);
364 if (urls
->server
&& strlen(urls
->server
) <= 1) {
372 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
374 return nfs_parse_url(nfs
, url
, 0, 0);
377 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
379 return nfs_parse_url(nfs
, url
, 1, 0);
382 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
384 return nfs_parse_url(nfs
, url
, 0, 1);
388 void nfs_destroy_url(struct nfs_url
*url
)
398 struct nfs_context
*nfs_init_context(void)
400 struct nfs_context
*nfs
;
402 nfs
= malloc(sizeof(struct nfs_context
));
406 memset(nfs
, 0, sizeof(struct nfs_context
));
408 nfs
->rpc
= rpc_init_context();
409 if (nfs
->rpc
== NULL
) {
414 nfs
->cwd
= strdup("/");
419 void nfs_destroy_context(struct nfs_context
*nfs
)
421 rpc_destroy_context(nfs
->rpc
);
439 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
440 free(nfs
->rootfh
.data
.data_val
);
441 nfs
->rootfh
.data
.data_val
= NULL
;
444 while (nfs
->dircache
) {
445 struct nfsdir
*nfsdir
= nfs
->dircache
;
446 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
447 nfs_free_nfsdir(nfsdir
);
462 void free_rpc_cb_data(struct rpc_cb_data
*data
)
469 static void rpc_connect_program_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
471 struct rpc_cb_data
*data
= private_data
;
473 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
475 /* Dont want any more callbacks even if the socket is closed */
476 rpc
->connect_cb
= NULL
;
478 if (status
== RPC_STATUS_ERROR
) {
479 data
->cb(rpc
, status
, command_data
, data
->private_data
);
480 free_rpc_cb_data(data
);
483 if (status
== RPC_STATUS_CANCEL
) {
484 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
485 free_rpc_cb_data(data
);
489 data
->cb(rpc
, status
, NULL
, data
->private_data
);
490 free_rpc_cb_data(data
);
493 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
495 struct rpc_cb_data
*data
= private_data
;
497 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
499 /* Dont want any more callbacks even if the socket is closed */
500 rpc
->connect_cb
= NULL
;
502 if (status
== RPC_STATUS_ERROR
) {
503 data
->cb(rpc
, status
, command_data
, data
->private_data
);
504 free_rpc_cb_data(data
);
507 if (status
== RPC_STATUS_CANCEL
) {
508 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
509 free_rpc_cb_data(data
);
513 switch (data
->program
) {
515 if (rpc_mount3_null_async(rpc
, rpc_connect_program_5_cb
,
517 data
->cb(rpc
, status
, command_data
, data
->private_data
);
518 free_rpc_cb_data(data
);
523 if (rpc_nfs3_null_async(rpc
, rpc_connect_program_5_cb
,
525 data
->cb(rpc
, status
, command_data
, data
->private_data
);
526 free_rpc_cb_data(data
);
532 data
->cb(rpc
, status
, NULL
, data
->private_data
);
533 free_rpc_cb_data(data
);
536 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
538 struct rpc_cb_data
*data
= private_data
;
539 struct pmap3_string_result
*gar
;
540 uint32_t rpc_port
= 0;
543 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
545 if (status
== RPC_STATUS_ERROR
) {
546 data
->cb(rpc
, status
, command_data
, data
->private_data
);
547 free_rpc_cb_data(data
);
550 if (status
== RPC_STATUS_CANCEL
) {
551 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
552 free_rpc_cb_data(data
);
556 switch (rpc
->s
.ss_family
) {
558 rpc_port
= *(uint32_t *)command_data
;
561 /* ouch. portmapper and ipv6 are not great */
563 if (gar
->addr
== NULL
) {
566 ptr
= strrchr(gar
->addr
, '.');
570 rpc_port
= atoi(ptr
+ 1);
572 ptr
= strrchr(gar
->addr
, '.');
576 rpc_port
+= 256 * atoi(ptr
+ 1);
580 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
581 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
582 free_rpc_cb_data(data
);
586 rpc_disconnect(rpc
, "normal disconnect");
587 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
588 data
->cb(rpc
, status
, command_data
, data
->private_data
);
589 free_rpc_cb_data(data
);
594 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
596 struct rpc_cb_data
*data
= private_data
;
597 struct pmap3_mapping map
;
599 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
601 if (status
== RPC_STATUS_ERROR
) {
602 data
->cb(rpc
, status
, command_data
, data
->private_data
);
603 free_rpc_cb_data(data
);
606 if (status
== RPC_STATUS_CANCEL
) {
607 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
608 free_rpc_cb_data(data
);
612 switch (rpc
->s
.ss_family
) {
614 if (rpc_pmap2_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
615 data
->cb(rpc
, status
, command_data
, data
->private_data
);
616 free_rpc_cb_data(data
);
621 map
.prog
=data
->program
;
622 map
.vers
=data
->version
;
626 if (rpc_pmap3_getaddr_async(rpc
, &map
, rpc_connect_program_3_cb
, private_data
) != 0) {
627 data
->cb(rpc
, status
, command_data
, data
->private_data
);
628 free_rpc_cb_data(data
);
635 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
637 struct rpc_cb_data
*data
= private_data
;
639 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
641 /* Dont want any more callbacks even if the socket is closed */
642 rpc
->connect_cb
= NULL
;
644 if (status
== RPC_STATUS_ERROR
) {
645 data
->cb(rpc
, status
, command_data
, data
->private_data
);
646 free_rpc_cb_data(data
);
649 if (status
== RPC_STATUS_CANCEL
) {
650 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
651 free_rpc_cb_data(data
);
655 switch (rpc
->s
.ss_family
) {
657 if (rpc_pmap2_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
658 data
->cb(rpc
, status
, command_data
, data
->private_data
);
659 free_rpc_cb_data(data
);
664 if (rpc_pmap3_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
665 data
->cb(rpc
, status
, command_data
, data
->private_data
);
666 free_rpc_cb_data(data
);
673 int rpc_connect_program_async(struct rpc_context
*rpc
, const char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
675 struct rpc_cb_data
*data
;
677 data
= malloc(sizeof(struct rpc_cb_data
));
681 memset(data
, 0, sizeof(struct rpc_cb_data
));
682 data
->server
= strdup(server
);
683 data
->program
= program
;
684 data
->version
= version
;
687 data
->private_data
= private_data
;
689 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
690 rpc_set_error(rpc
, "Failed to start connection");
691 free_rpc_cb_data(data
);
697 static void free_nfs_cb_data(struct nfs_cb_data
*data
)
699 if (data
->continue_data
!= NULL
) {
700 assert(data
->free_continue_data
);
701 data
->free_continue_data(data
->continue_data
);
704 free(data
->saved_path
);
705 free(data
->fh
.data
.data_val
);
711 static void free_nfsfh(struct nfsfh
*nfsfh
)
713 if (nfsfh
->fh
.data
.data_val
!= NULL
) {
714 free(nfsfh
->fh
.data
.data_val
);
715 nfsfh
->fh
.data
.data_val
= NULL
;
722 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
724 struct nfs_cb_data
*data
= private_data
;
725 struct nfs_context
*nfs
= data
->nfs
;
727 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
729 if (status
== RPC_STATUS_ERROR
) {
730 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
731 free_nfs_cb_data(data
);
734 if (status
== RPC_STATUS_CANCEL
) {
735 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
736 free_nfs_cb_data(data
);
740 data
->cb(0, nfs
, NULL
, data
->private_data
);
741 free_nfs_cb_data(data
);
744 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
746 struct nfs_cb_data
*data
= private_data
;
747 struct nfs_context
*nfs
= data
->nfs
;
748 FSINFO3res
*res
= command_data
;
749 struct GETATTR3args args
;
751 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
753 if (status
== RPC_STATUS_ERROR
) {
754 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
755 free_nfs_cb_data(data
);
758 if (status
== RPC_STATUS_CANCEL
) {
759 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
760 free_nfs_cb_data(data
);
764 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
765 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
767 memset(&args
, 0, sizeof(GETATTR3args
));
768 args
.object
= nfs
->rootfh
;
770 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
771 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
772 free_nfs_cb_data(data
);
777 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
779 struct nfs_cb_data
*data
= private_data
;
780 struct nfs_context
*nfs
= data
->nfs
;
781 struct FSINFO3args args
;
783 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
785 if (status
== RPC_STATUS_ERROR
) {
786 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
787 free_nfs_cb_data(data
);
790 if (status
== RPC_STATUS_CANCEL
) {
791 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
792 free_nfs_cb_data(data
);
796 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
797 rpc_set_autoreconnect(rpc
);
799 args
.fsroot
= nfs
->rootfh
;
800 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
801 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
802 free_nfs_cb_data(data
);
807 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
809 struct nfs_cb_data
*data
= private_data
;
810 struct nfs_context
*nfs
= data
->nfs
;
813 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
815 if (status
== RPC_STATUS_ERROR
) {
816 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
817 free_nfs_cb_data(data
);
820 if (status
== RPC_STATUS_CANCEL
) {
821 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
822 free_nfs_cb_data(data
);
827 if (res
->fhs_status
!= MNT3_OK
) {
828 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
));
829 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
830 free_nfs_cb_data(data
);
834 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
835 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
836 if (nfs
->rootfh
.data
.data_val
== NULL
) {
837 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
838 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
839 free_nfs_cb_data(data
);
842 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
844 rpc_disconnect(rpc
, "normal disconnect");
846 if (rpc_connect_program_async(nfs
->rpc
, nfs
->server
, NFS_PROGRAM
, NFS_V3
, nfs_mount_8_cb
, data
) != 0) {
847 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
848 free_nfs_cb_data(data
);
854 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
856 struct nfs_cb_data
*data
= private_data
;
857 struct nfs_context
*nfs
= data
->nfs
;
859 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
861 if (status
== RPC_STATUS_ERROR
) {
862 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
863 free_nfs_cb_data(data
);
866 if (status
== RPC_STATUS_CANCEL
) {
867 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
868 free_nfs_cb_data(data
);
872 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
873 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
874 free_nfs_cb_data(data
);
880 * Async call for mounting an nfs share and geting the root filehandle
882 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
884 struct nfs_cb_data
*data
;
885 char *new_server
, *new_export
;
887 data
= malloc(sizeof(struct nfs_cb_data
));
889 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
892 memset(data
, 0, sizeof(struct nfs_cb_data
));
893 new_server
= strdup(server
);
894 new_export
= strdup(export
);
895 if (nfs
->server
!= NULL
) {
898 nfs
->server
= new_server
;
899 if (nfs
->export
!= NULL
) {
902 nfs
->export
= new_export
;
905 data
->private_data
= private_data
;
907 if (rpc_connect_program_async(nfs
->rpc
, server
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_5_cb
, data
) != 0) {
908 rpc_set_error(nfs
->rpc
, "Failed to start connection");
909 free_nfs_cb_data(data
);
919 * Functions to first look up a path, component by component, and then finally call a specific function once
920 * the filehandle for the final component is found.
922 static void nfs_lookup_path_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
924 struct nfs_cb_data
*data
= private_data
;
925 struct nfs_context
*nfs
= data
->nfs
;
927 char *path
, *newpath
;
929 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
931 if (status
== RPC_STATUS_ERROR
) {
932 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
933 free_nfs_cb_data(data
);
936 if (status
== RPC_STATUS_CANCEL
) {
937 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
938 free_nfs_cb_data(data
);
943 if (res
->status
!= NFS3_OK
) {
944 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
));
945 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
946 free_nfs_cb_data(data
);
950 path
= res
->READLINK3res_u
.resok
.data
;
952 /* Handle absolute paths, ensuring that the path lies within the
954 if (path
[0] == '/') {
955 if (strstr(path
, nfs
->export
) == path
) {
956 char *ptr
= path
+ strlen(nfs
->export
);
958 newpath
= strdup(ptr
);
959 } else if (*ptr
== '\0') {
960 newpath
= strdup("/");
962 data
->cb(-ENOENT
, nfs
, "Symbolic link points outside export", data
->private_data
);
963 free_nfs_cb_data(data
);
967 data
->cb(-ENOENT
, nfs
, "Symbolic link points outside export", data
->private_data
);
968 free_nfs_cb_data(data
);
975 /* Handle relative paths, both the case where the current
976 * component is an intermediate component and when it is the
977 * final component. */
979 /* Since path points to a component and saved_path
980 * always starts with '/', path[-1] is valid. */
981 data
->path
[-1] = '\0';
982 newpath
= malloc(strlen(data
->saved_path
) + strlen(path
) + strlen(data
->path
) + 6);
986 sprintf(newpath
, "%s/../%s/%s", data
->saved_path
, path
, data
->path
);
988 newpath
= malloc(strlen(data
->saved_path
) + strlen(path
) + 5);
992 sprintf(newpath
, "%s/../%s", data
->saved_path
, path
);
995 free(data
->saved_path
);
996 data
->saved_path
= newpath
;
998 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
999 data
->cb(-ENOENT
, nfs
, "Symbolic link resolves to invalid path", data
->private_data
);
1000 free_nfs_cb_data(data
);
1004 data
->path
= data
->saved_path
;
1005 nfs_lookup_path_async_internal(nfs
, NULL
, data
, &nfs
->rootfh
);
1009 data
->cb(-ENOMEM
, nfs
, "Failed to allocate memory for path", data
->private_data
);
1010 free_nfs_cb_data(data
);
1013 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1015 struct nfs_cb_data
*data
= private_data
;
1016 struct nfs_context
*nfs
= data
->nfs
;
1020 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1022 if (status
== RPC_STATUS_ERROR
) {
1023 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1024 free_nfs_cb_data(data
);
1027 if (status
== RPC_STATUS_CANCEL
) {
1028 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1029 free_nfs_cb_data(data
);
1034 if (res
->status
!= NFS3_OK
) {
1035 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
));
1036 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1037 free_nfs_cb_data(data
);
1041 attr
= res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
?
1042 &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
:
1045 /* This function will always invoke the callback and cleanup
1046 * for failures. So no need to check the return value.
1048 nfs_lookup_path_async_internal(nfs
, attr
, data
, &res
->LOOKUP3res_u
.resok
.object
);
1051 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, fattr3
*attr
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
1056 while (*data
->path
== '/') {
1061 slash
= strchr(path
, '/');
1063 if (attr
&& attr
->type
== NF3LNK
&& (!data
->no_follow
|| *path
!= '\0')) {
1066 if (data
->link_count
++ >= MAX_LINK_COUNT
) {
1067 data
->cb(-ELOOP
, nfs
, "Too many levels of symbolic links", data
->private_data
);
1068 free_nfs_cb_data(data
);
1074 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_lookup_path_2_cb
, &args
, data
) != 0) {
1075 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
1076 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1077 free_nfs_cb_data(data
);
1081 if (slash
!= NULL
) {
1087 if (slash
!= NULL
) {
1088 /* Clear slash so that path is a zero terminated string for
1089 * the current path component. Set it back to '/' again later
1090 * when we are finished referencing this component so that
1091 * data->saved_path will still point to the full
1095 data
->path
= slash
+1;
1097 while (*data
->path
!= 0) {
1103 data
->fh
.data
.data_len
= fh
->data
.data_len
;
1104 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
1105 if (data
->fh
.data
.data_val
== NULL
) {
1106 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
1107 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1108 free_nfs_cb_data(data
);
1111 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
1112 if (slash
!= NULL
) {
1115 data
->continue_cb(nfs
, attr
, data
);
1119 memset(&args
, 0, sizeof(LOOKUP3args
));
1120 args
.what
.dir
= *fh
;
1121 args
.what
.name
= path
;
1123 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
1124 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
1125 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1126 free_nfs_cb_data(data
);
1129 if (slash
!= NULL
) {
1135 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
1141 while ((str
= strstr(path
, "//"))) {
1149 while ((str
= strstr(path
, "/./"))) {
1156 /* ^/../ -> error */
1157 if (!strncmp(path
, "/../", 4)) {
1158 rpc_set_error(nfs
->rpc
,
1159 "Absolute path starts with '/../' "
1160 "during normalization");
1164 /* ^[^/] -> error */
1165 if (path
[0] != '/') {
1166 rpc_set_error(nfs
->rpc
,
1167 "Absolute path does not start with '/'");
1171 /* /string/../ -> / */
1172 while ((str
= strstr(path
, "/../"))) {
1175 if (!strncmp(path
, "/../", 4)) {
1176 rpc_set_error(nfs
->rpc
,
1177 "Absolute path starts with '/../' "
1178 "during normalization");
1183 while (*tmp
!= '/') {
1187 while((*(tmp
++) = *(str
++)) != '\0')
1194 if (path
[len
- 1] == '/') {
1195 path
[len
- 1] = '\0';
1199 if (path
[0] == '\0') {
1200 rpc_set_error(nfs
->rpc
,
1201 "Absolute path became '' "
1202 "during normalization");
1208 if (!strcmp(&path
[len
- 2], "/.")) {
1209 path
[len
- 2] = '\0';
1214 /* ^/..$ -> error */
1215 if (!strcmp(path
, "/..")) {
1216 rpc_set_error(nfs
->rpc
,
1217 "Absolute path is '/..' "
1218 "during normalization");
1222 /* /string/..$ -> / */
1224 if (!strcmp(&path
[len
- 3], "/..")) {
1225 char *tmp
= &path
[len
- 3];
1226 while (*--tmp
!= '/')
1235 static void nfs_lookup_path_getattr_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1237 struct nfs_cb_data
*data
= private_data
;
1238 struct nfs_context
*nfs
= data
->nfs
;
1242 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1244 if (status
== RPC_STATUS_ERROR
) {
1245 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1246 free_nfs_cb_data(data
);
1249 if (status
== RPC_STATUS_CANCEL
) {
1250 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1251 free_nfs_cb_data(data
);
1256 if (res
->status
!= NFS3_OK
) {
1257 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
));
1258 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1259 free_nfs_cb_data(data
);
1263 attr
= &res
->GETATTR3res_u
.resok
.obj_attributes
;
1264 /* This function will always invoke the callback and cleanup
1265 * for failures. So no need to check the return value.
1267 nfs_lookup_path_async_internal(nfs
, attr
, data
, &nfs
->rootfh
);
1270 static int nfs_lookuppath_async(struct nfs_context
*nfs
, const char *path
, int no_follow
, nfs_cb cb
, void *private_data
, continue_func continue_cb
, void *continue_data
, void (*free_continue_data
)(void *), int continue_int
)
1272 struct nfs_cb_data
*data
;
1273 struct GETATTR3args args
;
1275 if (path
[0] == '\0') {
1279 data
= malloc(sizeof(struct nfs_cb_data
));
1281 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1282 "nfs_cb_data structure");
1283 if (free_continue_data
)
1284 free_continue_data(continue_data
);
1287 memset(data
, 0, sizeof(struct nfs_cb_data
));
1290 data
->continue_cb
= continue_cb
;
1291 data
->continue_data
= continue_data
;
1292 data
->free_continue_data
= free_continue_data
;
1293 data
->continue_int
= continue_int
;
1294 data
->private_data
= private_data
;
1295 data
->no_follow
= no_follow
;
1296 if (path
[0] == '/') {
1297 data
->saved_path
= strdup(path
);
1299 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1300 if (data
->saved_path
== NULL
) {
1301 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1302 "malloc path string");
1303 free_nfs_cb_data(data
);
1306 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1309 if (data
->saved_path
== NULL
) {
1310 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1311 free_nfs_cb_data(data
);
1314 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1315 free_nfs_cb_data(data
);
1319 data
->path
= data
->saved_path
;
1320 if (data
->path
[0]) {
1321 /* This function will always invoke the callback and cleanup
1322 * for failures. So no need to check the return value.
1324 nfs_lookup_path_async_internal(nfs
, NULL
, data
, &nfs
->rootfh
);
1328 /* We have a request for "", so just perform a GETATTR3 so we can
1329 * return the attributes to the caller.
1331 memset(&args
, 0, sizeof(GETATTR3args
));
1332 args
.object
= nfs
->rootfh
;
1333 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lookup_path_getattr_cb
, &args
, data
) != 0) {
1334 free_nfs_cb_data(data
);
1344 static dev_t
specdata3_to_rdev(struct specdata3
*rdev
)
1347 return makedev(rdev
->specdata1
, rdev
->specdata2
);
1353 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1356 struct nfs_cb_data
*data
= private_data
;
1357 struct nfs_context
*nfs
= data
->nfs
;
1364 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1366 if (status
== RPC_STATUS_ERROR
) {
1367 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1368 free_nfs_cb_data(data
);
1371 if (status
== RPC_STATUS_CANCEL
) {
1372 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1373 free_nfs_cb_data(data
);
1378 if (res
->status
!= NFS3_OK
) {
1379 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
));
1380 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1381 free_nfs_cb_data(data
);
1385 st
.st_dev
= res
->GETATTR3res_u
.resok
.obj_attributes
.fsid
;
1386 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1387 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1388 switch (res
->GETATTR3res_u
.resok
.obj_attributes
.type
) {
1390 st
.st_mode
|= S_IFREG
;
1393 st
.st_mode
|= S_IFDIR
;
1396 st
.st_mode
|= S_IFBLK
;
1399 st
.st_mode
|= S_IFCHR
;
1402 st
.st_mode
|= S_IFLNK
;
1405 st
.st_mode
|= S_IFSOCK
;
1408 st
.st_mode
|= S_IFIFO
;
1411 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1412 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1413 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1414 st
.st_rdev
= specdata3_to_rdev(&res
->GETATTR3res_u
.resok
.obj_attributes
.rdev
);
1415 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1417 st
.st_blksize
= NFS_BLKSIZE
;
1418 st
.st_blocks
= (res
->GETATTR3res_u
.resok
.obj_attributes
.used
+ 512 - 1) / 512;
1420 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1421 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1422 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1423 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1424 st
.st_atim
.tv_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.nseconds
;
1425 st
.st_mtim
.tv_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.nseconds
;
1426 st
.st_ctim
.tv_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.nseconds
;
1429 data
->cb(0, nfs
, &st
, data
->private_data
);
1430 free_nfs_cb_data(data
);
1433 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1435 struct GETATTR3args args
;
1437 memset(&args
, 0, sizeof(GETATTR3args
));
1438 args
.object
= data
->fh
;
1440 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1441 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1442 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1443 free_nfs_cb_data(data
);
1449 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1451 if (nfs_lookuppath_async(nfs
, path
, 0, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1452 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1461 * Async nfs_stat64()
1463 static void nfs_stat64_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1466 struct nfs_cb_data
*data
= private_data
;
1467 struct nfs_context
*nfs
= data
->nfs
;
1468 struct nfs_stat_64 st
;
1470 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1472 if (status
== RPC_STATUS_ERROR
) {
1473 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1474 free_nfs_cb_data(data
);
1477 if (status
== RPC_STATUS_CANCEL
) {
1478 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1479 free_nfs_cb_data(data
);
1484 if (res
->status
!= NFS3_OK
) {
1485 rpc_set_error(nfs
->rpc
, "NFS: GETATTR of %s failed with %s(%d)", data
->saved_path
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1486 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1487 free_nfs_cb_data(data
);
1491 st
.nfs_dev
= res
->GETATTR3res_u
.resok
.obj_attributes
.fsid
;
1492 st
.nfs_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1493 st
.nfs_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1494 switch (res
->GETATTR3res_u
.resok
.obj_attributes
.type
) {
1496 st
.nfs_mode
|= S_IFREG
;
1499 st
.nfs_mode
|= S_IFDIR
;
1502 st
.nfs_mode
|= S_IFBLK
;
1505 st
.nfs_mode
|= S_IFCHR
;
1508 st
.nfs_mode
|= S_IFLNK
;
1511 st
.nfs_mode
|= S_IFSOCK
;
1514 st
.nfs_mode
|= S_IFIFO
;
1517 st
.nfs_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1518 st
.nfs_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1519 st
.nfs_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1520 st
.nfs_rdev
= specdata3_to_rdev(&res
->GETATTR3res_u
.resok
.obj_attributes
.rdev
);
1521 st
.nfs_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1522 st
.nfs_blksize
= NFS_BLKSIZE
;
1523 st
.nfs_blocks
= (res
->GETATTR3res_u
.resok
.obj_attributes
.used
+ 512 - 1) / 512;
1524 st
.nfs_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1525 st
.nfs_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1526 st
.nfs_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1527 st
.nfs_atime_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.nseconds
;
1528 st
.nfs_mtime_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.nseconds
;
1529 st
.nfs_ctime_nsec
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.nseconds
;
1530 st
.nfs_used
= res
->GETATTR3res_u
.resok
.obj_attributes
.used
;
1532 data
->cb(0, nfs
, &st
, data
->private_data
);
1533 free_nfs_cb_data(data
);
1536 static int nfs_stat64_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1538 struct GETATTR3args args
;
1540 memset(&args
, 0, sizeof(GETATTR3args
));
1541 args
.object
= data
->fh
;
1543 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
1544 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1545 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1546 free_nfs_cb_data(data
);
1552 int nfs_stat64_async_internal(struct nfs_context
*nfs
, const char *path
, int no_follow
, nfs_cb cb
, void *private_data
)
1554 if (nfs_lookuppath_async(nfs
, path
, no_follow
, cb
, private_data
, nfs_stat64_continue_internal
, NULL
, NULL
, 0) != 0) {
1555 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1562 int nfs_stat64_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1564 return nfs_stat64_async_internal(nfs
, path
, 0, cb
, private_data
);
1567 int nfs_lstat64_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1569 return nfs_stat64_async_internal(nfs
, path
, 1, cb
, private_data
);
1575 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1577 struct nfs_cb_data
*data
= private_data
;
1578 struct nfs_context
*nfs
= data
->nfs
;
1579 struct nfsfh
*nfsfh
;
1582 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1584 if (status
== RPC_STATUS_ERROR
) {
1585 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1586 free_nfs_cb_data(data
);
1589 if (status
== RPC_STATUS_CANCEL
) {
1590 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1591 free_nfs_cb_data(data
);
1596 if (res
->status
!= NFS3_OK
) {
1597 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1598 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1599 free_nfs_cb_data(data
);
1603 nfsfh
= malloc(sizeof(struct nfsfh
));
1604 if (nfsfh
== NULL
) {
1605 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1606 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1607 free_nfs_cb_data(data
);
1610 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1612 if (data
->continue_int
& O_SYNC
) {
1615 if (data
->continue_int
& O_APPEND
) {
1616 nfsfh
->is_append
= 1;
1619 /* steal the filehandle */
1620 nfsfh
->fh
= data
->fh
;
1621 data
->fh
.data
.data_val
= NULL
;
1623 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1624 free_nfs_cb_data(data
);
1627 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1630 struct nfs_cb_data
*data
= private_data
;
1631 struct nfs_context
*nfs
= data
->nfs
;
1632 struct nfsfh
*nfsfh
;
1633 unsigned int nfsmode
= 0;
1635 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1637 if (status
== RPC_STATUS_ERROR
) {
1638 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1639 free_nfs_cb_data(data
);
1642 if (status
== RPC_STATUS_CANCEL
) {
1643 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1644 free_nfs_cb_data(data
);
1649 if (res
->status
!= NFS3_OK
) {
1650 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
));
1651 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1652 free_nfs_cb_data(data
);
1656 if (data
->continue_int
& O_WRONLY
) {
1657 nfsmode
|= ACCESS3_MODIFY
;
1659 if (data
->continue_int
& O_RDWR
) {
1660 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1662 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1663 nfsmode
|= ACCESS3_READ
;
1667 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1668 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1669 nfsmode
&ACCESS3_READ
?'r':'-',
1670 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1671 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1672 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1673 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1674 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1675 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1676 free_nfs_cb_data(data
);
1680 /* Try to truncate it if we were requested to */
1681 if ((data
->continue_int
& O_TRUNC
) &&
1682 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1685 memset(&args
, 0, sizeof(SETATTR3args
));
1686 args
.object
= data
->fh
;
1687 args
.new_attributes
.size
.set_it
= 1;
1688 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1690 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1692 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1693 "SETATTR call for %s", data
->path
);
1694 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1695 data
->private_data
);
1696 free_nfs_cb_data(data
);
1702 nfsfh
= malloc(sizeof(struct nfsfh
));
1703 if (nfsfh
== NULL
) {
1704 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1705 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1706 free_nfs_cb_data(data
);
1709 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1711 if (data
->continue_int
& O_SYNC
) {
1714 if (data
->continue_int
& O_APPEND
) {
1715 nfsfh
->is_append
= 1;
1718 /* steal the filehandle */
1719 nfsfh
->fh
= data
->fh
;
1720 data
->fh
.data
.data_val
= NULL
;
1722 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1723 free_nfs_cb_data(data
);
1726 static int nfs_open_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1731 if (data
->continue_int
& O_WRONLY
) {
1732 nfsmode
|= ACCESS3_MODIFY
;
1734 if (data
->continue_int
& O_RDWR
) {
1735 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1737 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1738 nfsmode
|= ACCESS3_READ
;
1741 memset(&args
, 0, sizeof(ACCESS3args
));
1742 args
.object
= data
->fh
;
1743 args
.access
= nfsmode
;
1745 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1746 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1747 "call for %s", data
->path
);
1748 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1749 data
->private_data
);
1750 free_nfs_cb_data(data
);
1756 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1758 if (nfs_lookuppath_async(nfs
, path
, 0, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1759 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1770 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1772 /* steal saved_path */
1774 nfs
->cwd
= data
->saved_path
;
1775 data
->saved_path
= NULL
;
1777 data
->cb(0, nfs
, NULL
, data
->private_data
);
1778 free_nfs_cb_data(data
);
1783 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1785 if (nfs_lookuppath_async(nfs
, path
, 0, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1786 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1797 static void nfs_fill_READ3args(READ3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
)
1799 memset(args
, 0, sizeof(READ3args
));
1800 args
->file
= fh
->fh
;
1801 args
->offset
= offset
;
1802 args
->count
= count
;
1805 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1807 struct nfs_mcb_data
*mdata
= private_data
;
1808 struct nfs_cb_data
*data
= mdata
->data
;
1809 struct nfs_context
*nfs
= data
->nfs
;
1814 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1818 if (status
== RPC_STATUS_ERROR
) {
1819 /* flag the failure but do not invoke callback until we have received all responses */
1822 if (status
== RPC_STATUS_CANCEL
) {
1823 /* flag the cancellation but do not invoke callback until we have received all responses */
1827 if (status
== RPC_STATUS_SUCCESS
) {
1829 if (res
->status
!= NFS3_OK
) {
1830 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1833 uint64_t count
= res
->READ3res_u
.resok
.count
;
1835 if (mdata
->update_pos
)
1836 data
->nfsfh
->offset
+= count
;
1838 /* if we have more than one call or we have received a short read we need a reassembly buffer */
1839 if (data
->num_calls
|| (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
)) {
1840 if (data
->buffer
== NULL
) {
1841 data
->buffer
= malloc(data
->count
);
1842 if (data
->buffer
== NULL
) {
1843 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data
->count
);
1849 if (count
<= mdata
->count
) {
1850 /* copy data into reassembly buffer if we have one */
1851 if (data
->buffer
!= NULL
) {
1852 memcpy(&data
->buffer
[mdata
->offset
- data
->offset
], res
->READ3res_u
.resok
.data
.data_val
, count
);
1854 if (data
->max_offset
< mdata
->offset
+ count
) {
1855 data
->max_offset
= mdata
->offset
+ count
;
1858 rpc_set_error(nfs
->rpc
, "NFS: Read overflow. Server has sent more data than requested!");
1862 /* check if we have received a short read */
1863 if (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
) {
1865 rpc_set_error(nfs
->rpc
, "NFS: Read failed. No bytes read and not at EOF!");
1868 /* reissue reminder of this read request */
1870 mdata
->offset
+= count
;
1871 mdata
->count
-= count
;
1872 nfs_fill_READ3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
);
1873 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) == 0) {
1877 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1887 if (data
->num_calls
> 0) {
1888 /* still waiting for more replies */
1891 if (data
->oom
!= 0) {
1892 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1893 free_nfs_cb_data(data
);
1896 if (data
->error
!= 0) {
1897 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1898 free_nfs_cb_data(data
);
1901 if (data
->cancel
!= 0) {
1902 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1903 free_nfs_cb_data(data
);
1908 if (data
->max_offset
> data
->org_offset
+ data
->org_count
) {
1909 data
->max_offset
= data
->org_offset
+ data
->org_count
;
1911 cb_err
= data
->max_offset
- data
->org_offset
;
1912 cb_data
= data
->buffer
+ (data
->org_offset
- data
->offset
);
1914 cb_err
= res
->READ3res_u
.resok
.count
;
1915 cb_data
= res
->READ3res_u
.resok
.data
.data_val
;
1918 data
->nfsfh
->ra
.fh_offset
= data
->max_offset
;
1919 if (data
->nfsfh
->ra
.cur_ra
) {
1920 free(data
->nfsfh
->ra
.buf
);
1921 data
->nfsfh
->ra
.buf
= data
->buffer
;
1922 data
->nfsfh
->ra
.buf_offset
= data
->offset
;
1923 data
->nfsfh
->ra
.buf_count
= data
->count
;
1924 data
->nfsfh
->ra
.buf_ts
= time(NULL
);
1925 data
->buffer
= NULL
;
1928 data
->cb(cb_err
, nfs
, cb_data
, data
->private_data
);
1929 free_nfs_cb_data(data
);
1932 static void nfs_ra_invalidate(struct nfsfh
*nfsfh
) {
1933 free(nfsfh
->ra
.buf
);
1934 nfsfh
->ra
.buf
= NULL
;
1935 nfsfh
->ra
.buf_offset
= 0;
1936 nfsfh
->ra
.buf_count
= 0;
1937 nfsfh
->ra
.buf_ts
= time(NULL
);
1938 nfsfh
->ra
.cur_ra
= NFS_BLKSIZE
;
1941 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
)
1943 struct nfs_cb_data
*data
;
1945 data
= malloc(sizeof(struct nfs_cb_data
));
1947 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1950 memset(data
, 0, sizeof(struct nfs_cb_data
));
1953 data
->private_data
= private_data
;
1954 data
->nfsfh
= nfsfh
;
1955 data
->org_offset
= offset
;
1956 data
->org_count
= count
;
1958 assert(data
->num_calls
== 0);
1960 if (nfs
->rpc
->readahead
&& time(NULL
) - nfsfh
->ra
.buf_ts
> NFS_RA_TIMEOUT
) {
1961 /* readahead cache timeout */
1962 nfs_ra_invalidate(nfsfh
);
1965 if (nfs
->rpc
->readahead
) {
1966 if (offset
>= nfsfh
->ra
.last_offset
&&
1967 offset
- NFS_BLKSIZE
<= nfsfh
->ra
.fh_offset
+ nfsfh
->ra
.cur_ra
) {
1968 if (nfs
->rpc
->readahead
> nfsfh
->ra
.cur_ra
) {
1969 nfsfh
->ra
.cur_ra
<<= 1;
1972 nfsfh
->ra
.cur_ra
= NFS_BLKSIZE
;
1975 nfsfh
->ra
.last_offset
= offset
;
1977 if (nfsfh
->ra
.buf_offset
<= offset
&&
1978 nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
>= offset
+ count
) {
1979 /* serve request completely from cache */
1980 data
->buffer
= malloc(count
);
1981 if (data
->buffer
== NULL
) {
1982 free_nfs_cb_data(data
);
1985 memcpy(data
->buffer
, nfsfh
->ra
.buf
+ (offset
- nfsfh
->ra
.buf_offset
), count
);
1986 data
->cb(count
, nfs
, data
->buffer
, data
->private_data
);
1987 nfsfh
->ra
.fh_offset
= offset
+ count
;
1988 free_nfs_cb_data(data
);
1992 /* align start offset to blocksize */
1993 count
+= offset
& (NFS_BLKSIZE
- 1);
1994 offset
&= ~(NFS_BLKSIZE
- 1);
1996 /* align end offset to blocksize and add readahead */
1997 count
+= nfsfh
->ra
.cur_ra
- 1;
1998 count
&= ~(NFS_BLKSIZE
- 1);
2000 data
->buffer
= malloc(count
);
2001 if (data
->buffer
== NULL
) {
2002 free_nfs_cb_data(data
);
2005 data
->offset
= offset
;
2006 data
->count
= count
;
2008 if (nfsfh
->ra
.buf_count
&& nfsfh
->ra
.buf_offset
<= offset
&&
2009 nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
>= offset
) {
2010 /* serve request partially from cache */
2011 size_t overlap
= (nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
) - offset
;
2012 if (overlap
> count
) count
= overlap
;
2013 memcpy(data
->buffer
, nfsfh
->ra
.buf
+ (offset
- nfsfh
->ra
.buf_offset
), overlap
);
2018 data
->offset
= offset
;
2019 data
->count
= count
;
2022 data
->max_offset
= offset
;
2024 /* chop requests into chunks of at most READMAX bytes if necessary.
2025 * we send all reads in parallel so that performance is still good.
2028 uint64_t readcount
= count
;
2029 struct nfs_mcb_data
*mdata
;
2032 if (readcount
> nfs_get_readmax(nfs
)) {
2033 readcount
= nfs_get_readmax(nfs
);
2036 mdata
= malloc(sizeof(struct nfs_mcb_data
));
2037 if (mdata
== NULL
) {
2038 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
2039 if (data
->num_calls
== 0) {
2040 free_nfs_cb_data(data
);
2046 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
2048 mdata
->offset
= offset
;
2049 mdata
->count
= readcount
;
2050 mdata
->update_pos
= update_pos
;
2052 nfs_fill_READ3args(&args
, nfsfh
, offset
, readcount
);
2054 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
2055 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
2057 if (data
->num_calls
== 0) {
2058 free_nfs_cb_data(data
);
2066 offset
+= readcount
;
2068 } while (count
> 0);
2073 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
2075 return nfs_pread_async_internal(nfs
, nfsfh
, offset
, count
, cb
, private_data
, 0);
2081 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
2083 return nfs_pread_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
, 1);
2091 static void nfs_fill_WRITE3args (WRITE3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
,
2094 memset(args
, 0, sizeof(WRITE3args
));
2095 args
->file
= fh
->fh
;
2096 args
->offset
= offset
;
2097 args
->count
= count
;
2098 args
->stable
= fh
->is_sync
? FILE_SYNC
: UNSTABLE
;
2099 args
->data
.data_len
= count
;
2100 args
->data
.data_val
= buf
;
2103 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2105 struct nfs_mcb_data
*mdata
= private_data
;
2106 struct nfs_cb_data
*data
= mdata
->data
;
2107 struct nfs_context
*nfs
= data
->nfs
;
2110 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2114 if (status
== RPC_STATUS_ERROR
) {
2115 /* flag the failure but do not invoke callback until we have received all responses */
2118 if (status
== RPC_STATUS_CANCEL
) {
2119 /* flag the cancellation but do not invoke callback until we have received all responses */
2123 if (status
== RPC_STATUS_SUCCESS
) {
2125 if (res
->status
!= NFS3_OK
) {
2126 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2129 uint64_t count
= res
->WRITE3res_u
.resok
.count
;
2131 if (mdata
->update_pos
)
2132 data
->nfsfh
->offset
+= count
;
2134 if (count
< mdata
->count
) {
2136 rpc_set_error(nfs
->rpc
, "NFS: Write failed. No bytes written!");
2139 /* reissue reminder of this write request */
2141 mdata
->offset
+= count
;
2142 mdata
->count
-= count
;
2144 nfs_fill_WRITE3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
,
2145 &data
->usrbuf
[mdata
->offset
- data
->offset
]);
2146 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) == 0) {
2150 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
2156 if (data
->max_offset
< mdata
->offset
+ count
) {
2157 data
->max_offset
= mdata
->offset
+ count
;
2165 if (data
->num_calls
> 0) {
2166 /* still waiting for more replies */
2169 if (data
->oom
!= 0) {
2170 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
2171 free_nfs_cb_data(data
);
2174 if (data
->error
!= 0) {
2175 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2176 free_nfs_cb_data(data
);
2179 if (data
->cancel
!= 0) {
2180 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2181 free_nfs_cb_data(data
);
2185 data
->cb(data
->max_offset
- data
->offset
, nfs
, NULL
, data
->private_data
);
2187 free_nfs_cb_data(data
);
2191 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
)
2193 struct nfs_cb_data
*data
;
2195 data
= malloc(sizeof(struct nfs_cb_data
));
2197 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2200 memset(data
, 0, sizeof(struct nfs_cb_data
));
2203 data
->private_data
= private_data
;
2204 data
->nfsfh
= nfsfh
;
2207 /* hello, clang-analyzer */
2208 assert(data
->num_calls
== 0);
2210 /* chop requests into chunks of at most WRITEMAX bytes if necessary.
2211 * we send all writes in parallel so that performance is still good.
2213 data
->max_offset
= offset
;
2214 data
->offset
= offset
;
2217 uint64_t writecount
= count
;
2218 struct nfs_mcb_data
*mdata
;
2221 if (writecount
> nfs_get_writemax(nfs
)) {
2222 writecount
= nfs_get_writemax(nfs
);
2225 mdata
= malloc(sizeof(struct nfs_mcb_data
));
2226 if (mdata
== NULL
) {
2227 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
2228 if (data
->num_calls
== 0) {
2229 free_nfs_cb_data(data
);
2235 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
2237 mdata
->offset
= offset
;
2238 mdata
->count
= writecount
;
2239 mdata
->update_pos
= update_pos
;
2241 nfs_fill_WRITE3args(&args
, nfsfh
, offset
, writecount
, &buf
[offset
- data
->offset
]);
2243 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
2244 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
2246 if (data
->num_calls
== 0) {
2247 free_nfs_cb_data(data
);
2254 count
-= writecount
;
2255 offset
+= writecount
;
2257 } while (count
> 0);
2262 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
)
2264 return nfs_pwrite_async_internal(nfs
, nfsfh
, offset
, count
, buf
, cb
, private_data
, 0);
2270 static void nfs_write_append_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2272 struct nfs_cb_data
*data
= private_data
;
2273 struct nfs_context
*nfs
= data
->nfs
;
2276 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2278 if (status
== RPC_STATUS_ERROR
) {
2279 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2280 free_nfs_cb_data(data
);
2283 if (status
== RPC_STATUS_CANCEL
) {
2284 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2285 free_nfs_cb_data(data
);
2290 if (res
->status
!= NFS3_OK
) {
2291 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2292 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2293 free_nfs_cb_data(data
);
2297 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) {
2298 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2299 free_nfs_cb_data(data
);
2302 free_nfs_cb_data(data
);
2305 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
2307 nfs_ra_invalidate(nfsfh
);
2308 if (nfsfh
->is_append
) {
2309 struct GETATTR3args args
;
2310 struct nfs_cb_data
*data
;
2312 data
= malloc(sizeof(struct nfs_cb_data
));
2314 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2317 memset(data
, 0, sizeof(struct nfs_cb_data
));
2320 data
->private_data
= private_data
;
2321 data
->nfsfh
= nfsfh
;
2323 data
->count
= count
;
2325 memset(&args
, 0, sizeof(GETATTR3args
));
2326 args
.object
= nfsfh
->fh
;
2328 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_write_append_cb
, &args
, data
) != 0) {
2329 rpc_set_error(nfs
->rpc
, "out of memory: failed to send GETATTR");
2330 free_nfs_cb_data(data
);
2335 return nfs_pwrite_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
, 1);
2345 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2348 cb(0, nfs
, NULL
, private_data
);
2359 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2361 struct nfs_cb_data
*data
;
2362 struct GETATTR3args args
;
2364 data
= malloc(sizeof(struct nfs_cb_data
));
2366 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2369 memset(data
, 0, sizeof(struct nfs_cb_data
));
2372 data
->private_data
= private_data
;
2374 memset(&args
, 0, sizeof(GETATTR3args
));
2375 args
.object
= nfsfh
->fh
;
2377 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
2378 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
2379 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2380 free_nfs_cb_data(data
);
2389 int nfs_fstat64_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2391 struct nfs_cb_data
*data
;
2392 struct GETATTR3args args
;
2394 data
= malloc(sizeof(struct nfs_cb_data
));
2396 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2399 memset(data
, 0, sizeof(struct nfs_cb_data
));
2402 data
->private_data
= private_data
;
2404 memset(&args
, 0, sizeof(GETATTR3args
));
2405 args
.object
= nfsfh
->fh
;
2407 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
2408 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
2409 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2410 free_nfs_cb_data(data
);
2421 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2423 struct nfs_cb_data
*data
= private_data
;
2424 struct nfs_context
*nfs
= data
->nfs
;
2427 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2429 if (status
== RPC_STATUS_ERROR
) {
2430 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2431 free_nfs_cb_data(data
);
2434 if (status
== RPC_STATUS_CANCEL
) {
2435 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2436 free_nfs_cb_data(data
);
2441 if (res
->status
!= NFS3_OK
) {
2442 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2443 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2444 free_nfs_cb_data(data
);
2448 data
->cb(0, nfs
, NULL
, data
->private_data
);
2449 free_nfs_cb_data(data
);
2452 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2454 struct nfs_cb_data
*data
;
2455 struct COMMIT3args args
;
2457 data
= malloc(sizeof(struct nfs_cb_data
));
2459 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2462 memset(data
, 0, sizeof(struct nfs_cb_data
));
2465 data
->private_data
= private_data
;
2467 args
.file
= nfsfh
->fh
;
2470 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
2471 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
2472 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2473 free_nfs_cb_data(data
);
2485 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2487 struct nfs_cb_data
*data
= private_data
;
2488 struct nfs_context
*nfs
= data
->nfs
;
2491 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2493 if (status
== RPC_STATUS_ERROR
) {
2494 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2495 free_nfs_cb_data(data
);
2498 if (status
== RPC_STATUS_CANCEL
) {
2499 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2500 free_nfs_cb_data(data
);
2505 if (res
->status
!= NFS3_OK
) {
2506 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2507 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2508 free_nfs_cb_data(data
);
2512 data
->cb(0, nfs
, NULL
, data
->private_data
);
2513 free_nfs_cb_data(data
);
2516 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
2518 struct nfs_cb_data
*data
;
2521 data
= malloc(sizeof(struct nfs_cb_data
));
2523 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2526 memset(data
, 0, sizeof(struct nfs_cb_data
));
2529 data
->private_data
= private_data
;
2531 memset(&args
, 0, sizeof(SETATTR3args
));
2532 args
.object
= nfsfh
->fh
;
2533 args
.new_attributes
.size
.set_it
= 1;
2534 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2536 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2537 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2538 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2539 free_nfs_cb_data(data
);
2549 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2551 uint64_t offset
= data
->continue_int
;
2554 nfsfh
.fh
= data
->fh
;
2556 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2557 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2558 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2559 free_nfs_cb_data(data
);
2562 free_nfs_cb_data(data
);
2566 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2572 if (nfs_lookuppath_async(nfs
, path
, 0, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2573 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2586 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2589 struct nfs_cb_data
*data
= private_data
;
2590 struct nfs_context
*nfs
= data
->nfs
;
2591 char *str
= data
->continue_data
;
2593 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2595 str
= &str
[strlen(str
) + 1];
2597 if (status
== RPC_STATUS_ERROR
) {
2598 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2599 free_nfs_cb_data(data
);
2602 if (status
== RPC_STATUS_CANCEL
) {
2603 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2604 free_nfs_cb_data(data
);
2609 if (res
->status
!= NFS3_OK
) {
2610 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
));
2611 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2612 free_nfs_cb_data(data
);
2616 data
->cb(0, nfs
, NULL
, data
->private_data
);
2617 free_nfs_cb_data(data
);
2620 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2622 char *str
= data
->continue_data
;
2625 str
= &str
[strlen(str
) + 1];
2627 memset(&args
, 0, sizeof(MKDIR3args
));
2628 args
.where
.dir
= data
->fh
;
2629 args
.where
.name
= str
;
2630 args
.attributes
.mode
.set_it
= 1;
2631 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2633 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2634 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2635 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2636 free_nfs_cb_data(data
);
2642 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2647 new_path
= strdup(path
);
2648 if (new_path
== NULL
) {
2649 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2653 ptr
= strrchr(new_path
, '/');
2656 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2661 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2662 if (nfs_lookuppath_async(nfs
, new_path
, 0, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2663 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2677 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2680 struct nfs_cb_data
*data
= private_data
;
2681 struct nfs_context
*nfs
= data
->nfs
;
2682 char *str
= data
->continue_data
;
2684 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2686 str
= &str
[strlen(str
) + 1];
2688 if (status
== RPC_STATUS_ERROR
) {
2689 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2690 free_nfs_cb_data(data
);
2693 if (status
== RPC_STATUS_CANCEL
) {
2694 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2695 free_nfs_cb_data(data
);
2700 if (res
->status
!= NFS3_OK
) {
2701 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
));
2702 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2703 free_nfs_cb_data(data
);
2707 data
->cb(0, nfs
, NULL
, data
->private_data
);
2708 free_nfs_cb_data(data
);
2711 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2713 char *str
= data
->continue_data
;
2716 str
= &str
[strlen(str
) + 1];
2718 args
.object
.dir
= data
->fh
;
2719 args
.object
.name
= str
;
2720 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2721 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2722 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2723 free_nfs_cb_data(data
);
2729 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2734 new_path
= strdup(path
);
2735 if (new_path
== NULL
) {
2736 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2740 ptr
= strrchr(new_path
, '/');
2743 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2748 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2749 if (nfs_lookuppath_async(nfs
, new_path
, 0, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2750 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2763 struct create_cb_data
{
2769 static void free_create_cb_data(void *ptr
)
2771 struct create_cb_data
*data
= ptr
;
2777 static void nfs_create_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2779 struct nfs_cb_data
*data
= private_data
;
2780 struct nfs_context
*nfs
= data
->nfs
;
2781 struct nfsfh
*nfsfh
= data
->nfsfh
;
2784 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2786 if (status
== RPC_STATUS_ERROR
) {
2787 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2788 free_nfs_cb_data(data
);
2792 if (status
== RPC_STATUS_CANCEL
) {
2793 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2794 free_nfs_cb_data(data
);
2800 if (res
->status
!= NFS3_OK
) {
2801 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2802 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2803 free_nfs_cb_data(data
);
2808 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2809 free_nfs_cb_data(data
);
2812 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2815 struct nfs_cb_data
*data
= private_data
;
2816 struct nfs_context
*nfs
= data
->nfs
;
2817 struct nfsfh
*nfsfh
;
2818 struct create_cb_data
*cb_data
= data
->continue_data
;
2819 char *str
= cb_data
->path
;
2821 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2823 if (status
== RPC_STATUS_ERROR
) {
2824 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2825 free_nfs_cb_data(data
);
2828 if (status
== RPC_STATUS_CANCEL
) {
2829 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2830 free_nfs_cb_data(data
);
2834 str
= &str
[strlen(str
) + 1];
2836 if (res
->status
!= NFS3_OK
) {
2837 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
));
2838 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2843 nfsfh
= malloc(sizeof(struct nfsfh
));
2844 if (nfsfh
== NULL
) {
2845 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2846 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2847 free_nfs_cb_data(data
);
2850 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2852 if (cb_data
->flags
& O_SYNC
) {
2855 if (cb_data
->flags
& O_APPEND
) {
2856 nfsfh
->is_append
= 1;
2859 /* copy the filehandle */
2860 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2861 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2862 if (nfsfh
->fh
.data
.data_val
== NULL
) {
2863 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh structure");
2864 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2865 free_nfs_cb_data(data
);
2869 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2871 /* Try to truncate it if we were requested to */
2872 if (cb_data
->flags
& O_TRUNC
) {
2875 data
->nfsfh
= nfsfh
;
2877 memset(&args
, 0, sizeof(SETATTR3args
));
2878 args
.object
= nfsfh
->fh
;
2879 args
.new_attributes
.size
.set_it
= 1;
2880 args
.new_attributes
.size
.set_size3_u
.size
= 0;
2882 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_create_trunc_cb
,
2883 &args
, data
) != 0) {
2884 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
2885 "SETATTR call for %s", data
->path
);
2886 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
2887 data
->private_data
);
2888 free_nfs_cb_data(data
);
2895 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2896 free_nfs_cb_data(data
);
2901 static void nfs_create_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2904 struct nfs_cb_data
*data
= private_data
;
2905 struct nfs_context
*nfs
= data
->nfs
;
2906 struct create_cb_data
*cb_data
= data
->continue_data
;
2907 char *str
= cb_data
->path
;
2910 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2912 if (status
== RPC_STATUS_ERROR
) {
2913 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2914 free_nfs_cb_data(data
);
2917 if (status
== RPC_STATUS_CANCEL
) {
2918 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2919 free_nfs_cb_data(data
);
2923 str
= &str
[strlen(str
) + 1];
2925 if (res
->status
!= NFS3_OK
) {
2926 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
));
2927 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2928 free_nfs_cb_data(data
);
2932 memset(&args
, 0, sizeof(LOOKUP3args
));
2933 args
.what
.dir
= data
->fh
;
2934 args
.what
.name
= str
;
2936 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2937 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2938 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2939 free_nfs_cb_data(data
);
2945 static int nfs_create_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2947 struct create_cb_data
*cb_data
= data
->continue_data
;
2948 char *str
= cb_data
->path
;
2951 str
= &str
[strlen(str
) + 1];
2953 memset(&args
, 0, sizeof(CREATE3args
));
2954 args
.where
.dir
= data
->fh
;
2955 args
.where
.name
= str
;
2956 args
.how
.mode
= (cb_data
->flags
& O_EXCL
) ? GUARDED
: UNCHECKED
;
2957 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2958 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
;
2960 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_create_1_cb
, &args
, data
) != 0) {
2961 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2962 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2963 free_nfs_cb_data(data
);
2969 int nfs_create_async(struct nfs_context
*nfs
, const char *path
, int flags
, int mode
, nfs_cb cb
, void *private_data
)
2971 struct create_cb_data
*cb_data
;
2974 cb_data
= malloc(sizeof(struct create_cb_data
));
2975 if (cb_data
== NULL
) {
2976 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2980 cb_data
->path
= strdup(path
);
2981 if (cb_data
->path
== NULL
) {
2982 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2987 ptr
= strrchr(cb_data
->path
, '/');
2989 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2990 free_create_cb_data(cb_data
);
2995 cb_data
->flags
= flags
;
2996 cb_data
->mode
= mode
;
2998 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2999 if (nfs_lookuppath_async(nfs
, cb_data
->path
, 0, cb
, private_data
, nfs_create_continue_internal
, cb_data
, free_create_cb_data
, 0) != 0) {
3000 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3007 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3009 return nfs_create_async(nfs
, path
, 0, mode
, cb
, private_data
);
3018 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3021 struct nfs_cb_data
*data
= private_data
;
3022 struct nfs_context
*nfs
= data
->nfs
;
3023 char *str
= data
->continue_data
;
3025 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3027 str
= &str
[strlen(str
) + 1];
3029 if (status
== RPC_STATUS_ERROR
) {
3030 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3031 free_nfs_cb_data(data
);
3034 if (status
== RPC_STATUS_CANCEL
) {
3035 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3036 free_nfs_cb_data(data
);
3041 if (res
->status
!= NFS3_OK
) {
3042 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
));
3043 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3044 free_nfs_cb_data(data
);
3048 data
->cb(0, nfs
, NULL
, data
->private_data
);
3049 free_nfs_cb_data(data
);
3052 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3054 char *str
= data
->continue_data
;
3055 struct REMOVE3args args
;
3057 str
= &str
[strlen(str
) + 1];
3059 args
.object
.dir
= data
->fh
;
3060 args
.object
.name
= str
;
3061 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
3062 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
3063 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3064 free_nfs_cb_data(data
);
3070 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3075 new_path
= strdup(path
);
3076 if (new_path
== NULL
) {
3077 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
3081 ptr
= strrchr(new_path
, '/');
3084 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
3089 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
3090 if (nfs_lookuppath_async(nfs
, new_path
, 0, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
3091 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3102 struct mknod_cb_data
{
3109 static void free_mknod_cb_data(void *ptr
)
3111 struct mknod_cb_data
*data
= ptr
;
3117 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3120 struct nfs_cb_data
*data
= private_data
;
3121 struct nfs_context
*nfs
= data
->nfs
;
3122 char *str
= data
->continue_data
;
3124 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3126 str
= &str
[strlen(str
) + 1];
3128 if (status
== RPC_STATUS_ERROR
) {
3129 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3130 free_nfs_cb_data(data
);
3133 if (status
== RPC_STATUS_CANCEL
) {
3134 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3135 free_nfs_cb_data(data
);
3140 if (res
->status
!= NFS3_OK
) {
3141 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
));
3142 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3143 free_nfs_cb_data(data
);
3147 data
->cb(0, nfs
, NULL
, data
->private_data
);
3148 free_nfs_cb_data(data
);
3151 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3153 struct mknod_cb_data
*cb_data
= data
->continue_data
;
3154 char *str
= cb_data
->path
;
3157 str
= &str
[strlen(str
) + 1];
3159 args
.where
.dir
= data
->fh
;
3160 args
.where
.name
= str
;
3161 switch (cb_data
->mode
& S_IFMT
) {
3163 args
.what
.type
= NF3CHR
;
3164 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
3165 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
);
3166 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
3167 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
3170 args
.what
.type
= NF3BLK
;
3171 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
3172 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
);
3173 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
3174 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
3176 args
.what
.type
= NF3SOCK
;
3177 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
3178 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
);
3181 args
.what
.type
= NF3FIFO
;
3182 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
3183 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
);
3186 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
3187 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3188 free_nfs_cb_data(data
);
3192 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
3193 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3194 free_nfs_cb_data(data
);
3200 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
3203 struct mknod_cb_data
*cb_data
;
3205 cb_data
= malloc(sizeof(struct mknod_cb_data
));
3206 if (cb_data
== NULL
) {
3207 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
3211 cb_data
->path
= strdup(path
);
3212 if (cb_data
->path
== NULL
) {
3213 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
3218 ptr
= strrchr(cb_data
->path
, '/');
3220 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
3221 free_mknod_cb_data(cb_data
);
3226 cb_data
->mode
= mode
;
3227 cb_data
->major
= major(dev
);
3228 cb_data
->minor
= minor(dev
);
3230 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
3231 if (nfs_lookuppath_async(nfs
, cb_data
->path
, 0, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
3232 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3243 /* ReadDirPlus Emulation Callback data */
3244 struct rdpe_cb_data
{
3247 struct nfs_cb_data
*data
;
3250 /* ReadDirPlus Emulation LOOKUP Callback data */
3251 struct rdpe_lookup_cb_data
{
3252 struct rdpe_cb_data
*rdpe_cb_data
;
3253 struct nfsdirent
*nfsdirent
;
3256 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
3257 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3259 LOOKUP3res
*res
= command_data
;
3260 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
3261 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
3262 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
3263 struct nfsdir
*nfsdir
= data
->continue_data
;
3264 struct nfs_context
*nfs
= data
->nfs
;
3265 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
3267 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3269 free(rdpe_lookup_cb_data
);
3271 rdpe_cb_data
->getattrcount
--;
3273 if (status
== RPC_STATUS_ERROR
) {
3274 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
3275 "failed with RPC_STATUS_ERROR");
3276 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
3278 if (status
== RPC_STATUS_CANCEL
) {
3279 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
3280 "failed with RPC_STATUS_CANCEL");
3281 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
3283 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
3284 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
3285 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
3287 nfsdirent
->type
= attributes
->type
;
3288 nfsdirent
->mode
= attributes
->mode
;
3289 nfsdirent
->size
= attributes
->size
;
3291 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
3292 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
3293 nfsdirent
->atime_nsec
= attributes
->atime
.nseconds
;
3294 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
3295 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
3296 nfsdirent
->mtime_nsec
= attributes
->mtime
.nseconds
;
3297 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
3298 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
3299 nfsdirent
->ctime_nsec
= attributes
->ctime
.nseconds
;
3300 nfsdirent
->uid
= attributes
->uid
;
3301 nfsdirent
->gid
= attributes
->gid
;
3302 nfsdirent
->nlink
= attributes
->nlink
;
3303 nfsdirent
->dev
= attributes
->fsid
;
3304 nfsdirent
->rdev
= specdata3_to_rdev(&attributes
->rdev
);
3305 nfsdirent
->blksize
= NFS_BLKSIZE
;
3306 nfsdirent
->blocks
= (attributes
->used
+ 512 - 1) / 512;
3307 nfsdirent
->used
= attributes
->used
;
3311 if (rdpe_cb_data
->getattrcount
== 0) {
3312 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
3313 rpc_set_error(nfs
->rpc
, "READDIRPLUS emulation "
3314 "failed: %s", rpc_get_error(rpc
));
3315 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
3316 data
->private_data
);
3317 nfs_free_nfsdir(nfsdir
);
3319 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
3323 data
->continue_data
= NULL
;
3324 free_nfs_cb_data(data
);
3328 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3330 READDIR3res
*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 nfsdirent
*nfsdirent
;
3335 struct entry3
*entry
;
3336 uint64_t cookie
= 0;
3337 struct rdpe_cb_data
*rdpe_cb_data
;
3339 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3341 if (status
== RPC_STATUS_ERROR
) {
3342 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3343 nfs_free_nfsdir(nfsdir
);
3344 data
->continue_data
= NULL
;
3345 free_nfs_cb_data(data
);
3349 if (status
== RPC_STATUS_CANCEL
) {
3350 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3351 nfs_free_nfsdir(nfsdir
);
3352 data
->continue_data
= NULL
;
3353 free_nfs_cb_data(data
);
3357 if (res
->status
!= NFS3_OK
) {
3358 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
));
3359 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3360 nfs_free_nfsdir(nfsdir
);
3361 data
->continue_data
= NULL
;
3362 free_nfs_cb_data(data
);
3366 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
3367 while (entry
!= NULL
) {
3368 nfsdirent
= malloc(sizeof(struct nfsdirent
));
3369 if (nfsdirent
== NULL
) {
3370 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
3371 nfs_free_nfsdir(nfsdir
);
3372 data
->continue_data
= NULL
;
3373 free_nfs_cb_data(data
);
3376 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
3377 nfsdirent
->name
= strdup(entry
->name
);
3378 if (nfsdirent
->name
== NULL
) {
3379 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
3381 nfs_free_nfsdir(nfsdir
);
3382 data
->continue_data
= NULL
;
3383 free_nfs_cb_data(data
);
3386 nfsdirent
->inode
= entry
->fileid
;
3388 nfsdirent
->next
= nfsdir
->entries
;
3389 nfsdir
->entries
= nfsdirent
;
3391 cookie
= entry
->cookie
;
3392 entry
= entry
->nextentry
;
3395 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
3398 args
.dir
= data
->fh
;
3399 args
.cookie
= cookie
;
3400 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
3403 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
3404 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
3405 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3406 nfs_free_nfsdir(nfsdir
);
3407 data
->continue_data
= NULL
;
3408 free_nfs_cb_data(data
);
3414 if (res
->READDIR3res_u
.resok
.dir_attributes
.attributes_follow
)
3415 nfsdir
->attr
= res
->READDIR3res_u
.resok
.dir_attributes
.post_op_attr_u
.attributes
;
3417 /* steal the dirhandle */
3418 nfsdir
->current
= nfsdir
->entries
;
3420 if (nfsdir
->entries
) {
3421 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
3422 rdpe_cb_data
->getattrcount
= 0;
3423 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
3424 rdpe_cb_data
->data
= data
;
3425 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
3426 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
3429 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
3430 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
3431 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
3433 memset(&args
, 0, sizeof(LOOKUP3args
));
3434 args
.what
.dir
= data
->fh
;
3435 args
.what
.name
= nfsdirent
->name
;
3437 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
3438 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
3440 /* if we have already commands in flight, we cant just stop, we have to wait for the
3441 * commands in flight to complete
3443 if (rdpe_cb_data
->getattrcount
> 0) {
3444 nfs_free_nfsdir(nfsdir
);
3445 data
->continue_data
= NULL
;
3446 free_nfs_cb_data(data
);
3447 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
3448 free(rdpe_lookup_cb_data
);
3452 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3453 nfs_free_nfsdir(nfsdir
);
3454 data
->continue_data
= NULL
;
3455 free_nfs_cb_data(data
);
3456 free(rdpe_lookup_cb_data
);
3460 rdpe_cb_data
->getattrcount
++;
3465 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3467 READDIRPLUS3res
*res
= command_data
;
3468 struct nfs_cb_data
*data
= private_data
;
3469 struct nfs_context
*nfs
= data
->nfs
;
3470 struct nfsdir
*nfsdir
= data
->continue_data
;
3471 struct entryplus3
*entry
;
3472 uint64_t cookie
= 0;
3474 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3476 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
3479 args
.dir
= data
->fh
;
3480 args
.cookie
= cookie
;
3481 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
3484 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
3485 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
3486 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3487 nfs_free_nfsdir(nfsdir
);
3488 data
->continue_data
= NULL
;
3489 free_nfs_cb_data(data
);
3495 if (status
== RPC_STATUS_CANCEL
) {
3496 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3497 nfs_free_nfsdir(nfsdir
);
3498 data
->continue_data
= NULL
;
3499 free_nfs_cb_data(data
);
3503 if (res
->status
!= NFS3_OK
) {
3504 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
));
3505 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3506 nfs_free_nfsdir(nfsdir
);
3507 data
->continue_data
= NULL
;
3508 free_nfs_cb_data(data
);
3512 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
3513 while (entry
!= NULL
) {
3514 struct nfsdirent
*nfsdirent
;
3516 nfsdirent
= malloc(sizeof(struct nfsdirent
));
3517 if (nfsdirent
== NULL
) {
3518 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
3519 nfs_free_nfsdir(nfsdir
);
3520 data
->continue_data
= NULL
;
3521 free_nfs_cb_data(data
);
3524 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
3525 nfsdirent
->name
= strdup(entry
->name
);
3526 if (nfsdirent
->name
== NULL
) {
3527 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
3529 nfs_free_nfsdir(nfsdir
);
3530 data
->continue_data
= NULL
;
3531 free_nfs_cb_data(data
);
3534 nfsdirent
->inode
= entry
->fileid
;
3535 if (entry
->name_attributes
.attributes_follow
) {
3536 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
3537 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
3538 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
3540 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
3541 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
3542 nfsdirent
->atime_nsec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
;
3543 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
3544 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
3545 nfsdirent
->mtime_nsec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
;
3546 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
3547 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
3548 nfsdirent
->ctime_nsec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
;
3549 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
3550 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
3551 nfsdirent
->nlink
= entry
->name_attributes
.post_op_attr_u
.attributes
.nlink
;
3552 nfsdirent
->dev
= entry
->name_attributes
.post_op_attr_u
.attributes
.fsid
;
3553 nfsdirent
->rdev
= specdata3_to_rdev(&entry
->name_attributes
.post_op_attr_u
.attributes
.rdev
);
3554 nfsdirent
->blksize
= NFS_BLKSIZE
;
3555 nfsdirent
->blocks
= (entry
->name_attributes
.post_op_attr_u
.attributes
.used
+ 512 - 1) / 512;
3556 nfsdirent
->used
= entry
->name_attributes
.post_op_attr_u
.attributes
.used
;
3559 nfsdirent
->next
= nfsdir
->entries
;
3560 nfsdir
->entries
= nfsdirent
;
3562 cookie
= entry
->cookie
;
3563 entry
= entry
->nextentry
;
3566 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
3567 READDIRPLUS3args args
;
3569 args
.dir
= data
->fh
;
3570 args
.cookie
= cookie
;
3571 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
3572 args
.dircount
= 8192;
3573 args
.maxcount
= 8192;
3575 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
3576 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
3577 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3578 nfs_free_nfsdir(nfsdir
);
3579 data
->continue_data
= NULL
;
3580 free_nfs_cb_data(data
);
3586 if (res
->READDIRPLUS3res_u
.resok
.dir_attributes
.attributes_follow
)
3587 nfsdir
->attr
= res
->READDIRPLUS3res_u
.resok
.dir_attributes
.post_op_attr_u
.attributes
;
3589 /* steal the dirhandle */
3590 data
->continue_data
= NULL
;
3591 nfsdir
->current
= nfsdir
->entries
;
3593 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
3594 free_nfs_cb_data(data
);
3597 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3599 READDIRPLUS3args args
;
3600 struct nfsdir
*nfsdir
= data
->continue_data
;;
3601 struct nfsdir
*cached
;
3603 cached
= nfs_dircache_find(nfs
, &data
->fh
);
3605 if (attr
&& attr
->mtime
.seconds
== cached
->attr
.mtime
.seconds
) {
3606 cached
->current
= cached
->entries
;
3607 data
->cb(0, nfs
, cached
, data
->private_data
);
3608 free_nfs_cb_data(data
);
3611 /* cache must be stale */
3612 nfs_free_nfsdir(cached
);
3616 nfsdir
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
3617 nfsdir
->fh
.data
.data_val
= malloc(nfsdir
->fh
.data
.data_len
);
3618 if (nfsdir
->fh
.data
.data_val
== NULL
) {
3619 rpc_set_error(nfs
->rpc
, "OOM when allocating fh for nfsdir");
3620 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3621 free_nfs_cb_data(data
);
3624 memcpy(nfsdir
->fh
.data
.data_val
, data
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3626 args
.dir
= data
->fh
;
3628 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
3629 args
.dircount
= 8192;
3630 args
.maxcount
= 8192;
3631 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
3632 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
3633 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3634 free_nfs_cb_data(data
);
3640 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3642 struct nfsdir
*nfsdir
;
3644 nfsdir
= malloc(sizeof(struct nfsdir
));
3645 if (nfsdir
== NULL
) {
3646 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
3649 memset(nfsdir
, 0, sizeof(struct nfsdir
));
3651 if (nfs_lookuppath_async(nfs
, path
, 0, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
3652 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3660 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3662 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
3664 if (nfsdir
->current
!= NULL
) {
3665 nfsdir
->current
= nfsdir
->current
->next
;
3674 void nfs_closedir(struct nfs_context
*nfs
, struct nfsdir
*nfsdir
)
3676 nfs_dircache_add(nfs
, nfsdir
);
3683 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3694 struct lseek_cb_data
{
3695 struct nfs_context
*nfs
;
3696 struct nfsfh
*nfsfh
;
3702 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3705 struct lseek_cb_data
*data
= private_data
;
3706 struct nfs_context
*nfs
= data
->nfs
;
3709 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3711 if (status
== RPC_STATUS_ERROR
) {
3712 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3716 if (status
== RPC_STATUS_CANCEL
) {
3717 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3723 if (res
->status
!= NFS3_OK
) {
3724 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3725 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3730 size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3732 if (data
->offset
< 0 &&
3733 (uint64_t)(-data
->offset
) > size
) {
3734 data
->cb(-EINVAL
, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3736 data
->nfsfh
->offset
= data
->offset
+ size
;
3737 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3743 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3745 struct lseek_cb_data
*data
;
3746 struct GETATTR3args args
;
3748 if (whence
== SEEK_SET
) {
3750 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3752 nfsfh
->offset
= offset
;
3753 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3757 if (whence
== SEEK_CUR
) {
3759 nfsfh
->offset
< (uint64_t)(-offset
)) {
3760 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3762 nfsfh
->offset
+= offset
;
3763 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3768 data
= malloc(sizeof(struct lseek_cb_data
));
3770 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3775 data
->nfsfh
= nfsfh
;
3776 data
->offset
= offset
;
3778 data
->private_data
= private_data
;
3780 memset(&args
, 0, sizeof(GETATTR3args
));
3781 args
.object
= nfsfh
->fh
;
3783 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3784 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3797 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3800 struct nfs_cb_data
*data
= private_data
;
3801 struct nfs_context
*nfs
= data
->nfs
;
3802 struct statvfs svfs
;
3804 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3806 if (status
== RPC_STATUS_ERROR
) {
3807 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3808 free_nfs_cb_data(data
);
3811 if (status
== RPC_STATUS_CANCEL
) {
3812 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3813 free_nfs_cb_data(data
);
3818 if (res
->status
!= NFS3_OK
) {
3819 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
));
3820 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3821 free_nfs_cb_data(data
);
3825 svfs
.f_bsize
= NFS_BLKSIZE
;
3826 svfs
.f_frsize
= NFS_BLKSIZE
;
3827 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/NFS_BLKSIZE
;
3828 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/NFS_BLKSIZE
;
3829 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/NFS_BLKSIZE
;
3830 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3831 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3832 #if !defined(ANDROID)
3833 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3836 svfs
.f_namemax
= 256;
3839 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3840 free_nfs_cb_data(data
);
3843 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3847 args
.fsroot
= data
->fh
;
3848 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3849 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3850 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3851 free_nfs_cb_data(data
);
3857 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3859 if (nfs_lookuppath_async(nfs
, path
, 0, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3860 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3873 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3876 struct nfs_cb_data
*data
= private_data
;
3877 struct nfs_context
*nfs
= data
->nfs
;
3879 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3881 if (status
== RPC_STATUS_ERROR
) {
3882 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3883 free_nfs_cb_data(data
);
3886 if (status
== RPC_STATUS_CANCEL
) {
3887 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3888 free_nfs_cb_data(data
);
3893 if (res
->status
!= NFS3_OK
) {
3894 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
));
3895 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3896 free_nfs_cb_data(data
);
3901 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3902 free_nfs_cb_data(data
);
3905 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3909 args
.symlink
= data
->fh
;
3911 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3912 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3913 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3914 free_nfs_cb_data(data
);
3920 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3922 if (nfs_lookuppath_async(nfs
, path
, 1, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3923 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3936 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3938 struct nfs_cb_data
*data
= private_data
;
3939 struct nfs_context
*nfs
= data
->nfs
;
3942 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3944 if (status
== RPC_STATUS_ERROR
) {
3945 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3946 free_nfs_cb_data(data
);
3949 if (status
== RPC_STATUS_CANCEL
) {
3950 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3951 free_nfs_cb_data(data
);
3956 if (res
->status
!= NFS3_OK
) {
3957 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3958 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3959 free_nfs_cb_data(data
);
3963 data
->cb(0, nfs
, NULL
, data
->private_data
);
3964 free_nfs_cb_data(data
);
3967 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3971 memset(&args
, 0, sizeof(SETATTR3args
));
3972 args
.object
= data
->fh
;
3973 args
.new_attributes
.mode
.set_it
= 1;
3974 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3976 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3977 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3978 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3979 free_nfs_cb_data(data
);
3986 int nfs_chmod_async_internal(struct nfs_context
*nfs
, const char *path
, int no_follow
, int mode
, nfs_cb cb
, void *private_data
)
3988 if (nfs_lookuppath_async(nfs
, path
, no_follow
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3989 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3996 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3998 return nfs_chmod_async_internal(nfs
, path
, 0, mode
, cb
, private_data
);
4001 int nfs_lchmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
4003 return nfs_chmod_async_internal(nfs
, path
, 1, mode
, cb
, private_data
);
4009 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
4011 struct nfs_cb_data
*data
;
4013 data
= malloc(sizeof(struct nfs_cb_data
));
4015 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
4018 memset(data
, 0, sizeof(struct nfs_cb_data
));
4021 data
->private_data
= private_data
;
4022 data
->continue_int
= mode
;
4023 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
4024 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
4025 if (data
->fh
.data
.data_val
== NULL
) {
4026 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
4027 free_nfs_cb_data(data
);
4030 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
4032 if (nfs_chmod_continue_internal(nfs
, NULL
, data
) != 0) {
4044 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4046 struct nfs_cb_data
*data
= private_data
;
4047 struct nfs_context
*nfs
= data
->nfs
;
4050 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4052 if (status
== RPC_STATUS_ERROR
) {
4053 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4054 free_nfs_cb_data(data
);
4057 if (status
== RPC_STATUS_CANCEL
) {
4058 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4059 free_nfs_cb_data(data
);
4064 if (res
->status
!= NFS3_OK
) {
4065 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
4066 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4067 free_nfs_cb_data(data
);
4071 data
->cb(0, nfs
, NULL
, data
->private_data
);
4072 free_nfs_cb_data(data
);
4075 struct nfs_chown_data
{
4080 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4083 struct nfs_chown_data
*chown_data
= data
->continue_data
;
4085 memset(&args
, 0, sizeof(SETATTR3args
));
4086 args
.object
= data
->fh
;
4087 if (chown_data
->uid
!= (uid_t
)-1) {
4088 args
.new_attributes
.uid
.set_it
= 1;
4089 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
4091 if (chown_data
->gid
!= (gid_t
)-1) {
4092 args
.new_attributes
.gid
.set_it
= 1;
4093 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
4096 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
4097 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
4098 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4099 free_nfs_cb_data(data
);
4106 int nfs_chown_async_internal(struct nfs_context
*nfs
, const char *path
, int no_follow
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
4108 struct nfs_chown_data
*chown_data
;
4110 chown_data
= malloc(sizeof(struct nfs_chown_data
));
4111 if (chown_data
== NULL
) {
4112 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
4116 chown_data
->uid
= uid
;
4117 chown_data
->gid
= gid
;
4119 if (nfs_lookuppath_async(nfs
, path
, no_follow
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
4120 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4127 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
4129 return nfs_chown_async_internal(nfs
, path
, 0, uid
, gid
, cb
, private_data
);
4132 int nfs_lchown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
4134 return nfs_chown_async_internal(nfs
, path
, 1, uid
, gid
, cb
, private_data
);
4140 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
4142 struct nfs_cb_data
*data
;
4143 struct nfs_chown_data
*chown_data
;
4145 chown_data
= malloc(sizeof(struct nfs_chown_data
));
4146 if (chown_data
== NULL
) {
4147 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
4151 chown_data
->uid
= uid
;
4152 chown_data
->gid
= gid
;
4154 data
= malloc(sizeof(struct nfs_cb_data
));
4156 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
4160 memset(data
, 0, sizeof(struct nfs_cb_data
));
4163 data
->private_data
= private_data
;
4164 data
->continue_data
= chown_data
;
4165 data
->free_continue_data
= free
;
4166 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
4167 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
4168 if (data
->fh
.data
.data_val
== NULL
) {
4169 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
4170 free_nfs_cb_data(data
);
4173 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
4175 if (nfs_chown_continue_internal(nfs
, NULL
, data
) != 0) {
4189 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4191 struct nfs_cb_data
*data
= private_data
;
4192 struct nfs_context
*nfs
= data
->nfs
;
4195 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4197 if (status
== RPC_STATUS_ERROR
) {
4198 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4199 free_nfs_cb_data(data
);
4202 if (status
== RPC_STATUS_CANCEL
) {
4203 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4204 free_nfs_cb_data(data
);
4209 if (res
->status
!= NFS3_OK
) {
4210 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
4211 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4212 free_nfs_cb_data(data
);
4216 data
->cb(0, nfs
, NULL
, data
->private_data
);
4217 free_nfs_cb_data(data
);
4220 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4223 struct timeval
*utimes_data
= data
->continue_data
;
4225 memset(&args
, 0, sizeof(SETATTR3args
));
4226 args
.object
= data
->fh
;
4227 if (utimes_data
!= NULL
) {
4228 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
4229 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
4230 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
4231 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
4232 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
4233 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
4235 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
4236 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
4239 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
4240 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
4241 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4242 free_nfs_cb_data(data
);
4248 int nfs_utimes_async_internal(struct nfs_context
*nfs
, const char *path
, int no_follow
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
4250 struct timeval
*new_times
= NULL
;
4252 if (times
!= NULL
) {
4253 new_times
= malloc(sizeof(struct timeval
)*2);
4254 if (new_times
== NULL
) {
4255 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
4259 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
4262 if (nfs_lookuppath_async(nfs
, path
, no_follow
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
4263 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4270 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
4272 return nfs_utimes_async_internal(nfs
, path
, 0, times
, cb
, private_data
);
4275 int nfs_lutimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
4277 return nfs_utimes_async_internal(nfs
, path
, 1, times
, cb
, private_data
);
4283 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
4285 struct timeval
*new_times
= NULL
;
4287 if (times
!= NULL
) {
4288 new_times
= malloc(sizeof(struct timeval
)*2);
4289 if (new_times
== NULL
) {
4290 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
4294 new_times
[0].tv_sec
= times
->actime
;
4295 new_times
[0].tv_usec
= 0;
4296 new_times
[1].tv_sec
= times
->modtime
;
4297 new_times
[1].tv_usec
= 0;
4300 if (nfs_lookuppath_async(nfs
, path
, 0, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
4301 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4312 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4315 struct nfs_cb_data
*data
= private_data
;
4316 struct nfs_context
*nfs
= data
->nfs
;
4317 unsigned int nfsmode
= 0;
4319 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4321 if (status
== RPC_STATUS_ERROR
) {
4322 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4323 free_nfs_cb_data(data
);
4326 if (status
== RPC_STATUS_CANCEL
) {
4327 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4328 free_nfs_cb_data(data
);
4333 if (res
->status
!= NFS3_OK
) {
4334 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
));
4335 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4336 free_nfs_cb_data(data
);
4340 if (data
->continue_int
& R_OK
) {
4341 nfsmode
|= ACCESS3_READ
;
4343 if (data
->continue_int
& W_OK
) {
4344 nfsmode
|= ACCESS3_MODIFY
;
4346 if (data
->continue_int
& X_OK
) {
4347 nfsmode
|= ACCESS3_EXECUTE
;
4350 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
4351 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
4352 nfsmode
&ACCESS3_READ
?'r':'-',
4353 nfsmode
&ACCESS3_MODIFY
?'w':'-',
4354 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
4355 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
4356 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
4357 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
4358 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4359 free_nfs_cb_data(data
);
4363 data
->cb(0, nfs
, NULL
, data
->private_data
);
4364 free_nfs_cb_data(data
);
4367 static int nfs_access_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4372 if (data
->continue_int
& R_OK
) {
4373 nfsmode
|= ACCESS3_READ
;
4375 if (data
->continue_int
& W_OK
) {
4376 nfsmode
|= ACCESS3_MODIFY
;
4378 if (data
->continue_int
& X_OK
) {
4379 nfsmode
|= ACCESS3_EXECUTE
;
4382 memset(&args
, 0, sizeof(ACCESS3args
));
4383 args
.object
= data
->fh
;
4384 args
.access
= nfsmode
;
4386 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
4387 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
4388 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4389 free_nfs_cb_data(data
);
4395 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
4397 if (nfs_lookuppath_async(nfs
, path
, 0, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
4398 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4410 struct nfs_symlink_data
{
4412 char *newpathparent
;
4413 char *newpathobject
;
4416 static void free_nfs_symlink_data(void *mem
)
4418 struct nfs_symlink_data
*data
= mem
;
4420 if (data
->oldpath
!= NULL
) {
4421 free(data
->oldpath
);
4423 if (data
->newpathparent
!= NULL
) {
4424 free(data
->newpathparent
);
4426 if (data
->newpathobject
!= NULL
) {
4427 free(data
->newpathobject
);
4432 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4435 struct nfs_cb_data
*data
= private_data
;
4436 struct nfs_context
*nfs
= data
->nfs
;
4437 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
4439 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4441 if (status
== RPC_STATUS_ERROR
) {
4442 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4443 free_nfs_cb_data(data
);
4446 if (status
== RPC_STATUS_CANCEL
) {
4447 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4448 free_nfs_cb_data(data
);
4453 if (res
->status
!= NFS3_OK
) {
4454 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
));
4455 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4456 free_nfs_cb_data(data
);
4460 data
->cb(0, nfs
, NULL
, data
->private_data
);
4461 free_nfs_cb_data(data
);
4464 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4466 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
4469 memset(&args
, 0, sizeof(SYMLINK3args
));
4470 args
.where
.dir
= data
->fh
;
4471 args
.where
.name
= symlink_data
->newpathobject
;
4472 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
4473 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
;
4474 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
4476 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
4477 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
4478 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4479 free_nfs_cb_data(data
);
4485 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4488 struct nfs_symlink_data
*symlink_data
;
4490 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
4491 if (symlink_data
== NULL
) {
4492 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
4495 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
4497 symlink_data
->oldpath
= strdup(oldpath
);
4498 if (symlink_data
->oldpath
== NULL
) {
4499 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4500 free_nfs_symlink_data(symlink_data
);
4504 symlink_data
->newpathparent
= strdup(newpath
);
4505 if (symlink_data
->newpathparent
== NULL
) {
4506 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
4507 free_nfs_symlink_data(symlink_data
);
4511 ptr
= strrchr(symlink_data
->newpathparent
, '/');
4513 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
4514 free_nfs_symlink_data(symlink_data
);
4520 symlink_data
->newpathobject
= strdup(ptr
);
4521 if (symlink_data
->newpathobject
== NULL
) {
4522 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
4523 free_nfs_symlink_data(symlink_data
);
4527 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, 0, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
4528 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4540 struct nfs_rename_data
{
4543 struct nfs_fh3 olddir
;
4546 struct nfs_fh3 newdir
;
4549 static void free_nfs_rename_data(void *mem
)
4551 struct nfs_rename_data
*data
= mem
;
4553 if (data
->oldpath
!= NULL
) {
4554 free(data
->oldpath
);
4556 if (data
->olddir
.data
.data_val
!= NULL
) {
4557 free(data
->olddir
.data
.data_val
);
4559 if (data
->newpath
!= NULL
) {
4560 free(data
->newpath
);
4562 if (data
->newdir
.data
.data_val
!= NULL
) {
4563 free(data
->newdir
.data
.data_val
);
4568 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4571 struct nfs_cb_data
*data
= private_data
;
4572 struct nfs_context
*nfs
= data
->nfs
;
4573 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4575 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4577 if (status
== RPC_STATUS_ERROR
) {
4578 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4579 free_nfs_cb_data(data
);
4582 if (status
== RPC_STATUS_CANCEL
) {
4583 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4584 free_nfs_cb_data(data
);
4589 if (res
->status
!= NFS3_OK
) {
4590 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
));
4591 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4592 free_nfs_cb_data(data
);
4596 data
->cb(0, nfs
, NULL
, data
->private_data
);
4597 free_nfs_cb_data(data
);
4600 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4602 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4605 /* steal the filehandle */
4606 rename_data
->newdir
= data
->fh
;
4607 data
->fh
.data
.data_val
= NULL
;
4609 args
.from
.dir
= rename_data
->olddir
;
4610 args
.from
.name
= rename_data
->oldobject
;
4611 args
.to
.dir
= rename_data
->newdir
;
4612 args
.to
.name
= rename_data
->newobject
;
4613 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
4614 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
4615 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4616 free_nfs_cb_data(data
);
4623 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4625 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4626 char* newpath
= strdup(rename_data
->newpath
);
4628 rpc_set_error(nfs
->rpc
, "Out of memory. Could not allocate memory to store target path for rename");
4629 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4630 free_nfs_cb_data(data
);
4634 /* steal the filehandle */
4635 rename_data
->olddir
= data
->fh
;
4636 data
->fh
.data
.data_val
= NULL
;
4638 if (nfs_lookuppath_async(nfs
, rename_data
->newpath
, 0, data
->cb
, data
->private_data
, nfs_rename_continue_2_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
4639 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", newpath
);
4640 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4641 free_nfs_cb_data(data
);
4645 data
->continue_data
= NULL
;
4646 free_nfs_cb_data(data
);
4653 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4656 struct nfs_rename_data
*rename_data
;
4658 rename_data
= malloc(sizeof(struct nfs_rename_data
));
4659 if (rename_data
== NULL
) {
4660 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
4663 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
4665 rename_data
->oldpath
= strdup(oldpath
);
4666 if (rename_data
->oldpath
== NULL
) {
4667 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4668 free_nfs_rename_data(rename_data
);
4671 ptr
= strrchr(rename_data
->oldpath
, '/');
4673 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
4674 free_nfs_rename_data(rename_data
);
4679 rename_data
->oldobject
= ptr
;
4682 rename_data
->newpath
= strdup(newpath
);
4683 if (rename_data
->newpath
== NULL
) {
4684 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4685 free_nfs_rename_data(rename_data
);
4688 ptr
= strrchr(rename_data
->newpath
, '/');
4690 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4691 free_nfs_rename_data(rename_data
);
4696 rename_data
->newobject
= ptr
;
4699 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, 0, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
4700 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4711 struct nfs_link_data
{
4713 struct nfs_fh3 oldfh
;
4716 struct nfs_fh3 newdir
;
4719 static void free_nfs_link_data(void *mem
)
4721 struct nfs_link_data
*data
= mem
;
4723 if (data
->oldpath
!= NULL
) {
4724 free(data
->oldpath
);
4726 if (data
->oldfh
.data
.data_val
!= NULL
) {
4727 free(data
->oldfh
.data
.data_val
);
4729 if (data
->newpath
!= NULL
) {
4730 free(data
->newpath
);
4732 if (data
->newdir
.data
.data_val
!= NULL
) {
4733 free(data
->newdir
.data
.data_val
);
4738 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4741 struct nfs_cb_data
*data
= private_data
;
4742 struct nfs_context
*nfs
= data
->nfs
;
4743 struct nfs_link_data
*link_data
= data
->continue_data
;
4745 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4747 if (status
== RPC_STATUS_ERROR
) {
4748 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4749 free_nfs_cb_data(data
);
4752 if (status
== RPC_STATUS_CANCEL
) {
4753 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4754 free_nfs_cb_data(data
);
4759 if (res
->status
!= NFS3_OK
) {
4760 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
));
4761 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4762 free_nfs_cb_data(data
);
4766 data
->cb(0, nfs
, NULL
, data
->private_data
);
4767 free_nfs_cb_data(data
);
4770 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4772 struct nfs_link_data
*link_data
= data
->continue_data
;
4775 /* steal the filehandle */
4776 link_data
->newdir
= data
->fh
;
4777 data
->fh
.data
.data_val
= NULL
;
4779 memset(&args
, 0, sizeof(LINK3args
));
4780 args
.file
= link_data
->oldfh
;
4781 args
.link
.dir
= link_data
->newdir
;
4782 args
.link
.name
= link_data
->newobject
;
4783 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4784 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4785 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4786 free_nfs_cb_data(data
);
4793 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4795 struct nfs_link_data
*link_data
= data
->continue_data
;
4797 /* steal the filehandle */
4798 link_data
->oldfh
= data
->fh
;
4799 data
->fh
.data
.data_val
= NULL
;
4801 if (nfs_lookuppath_async(nfs
, link_data
->newpath
, 0, data
->cb
, data
->private_data
, nfs_link_continue_2_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4802 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4803 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4804 free_nfs_cb_data(data
);
4807 data
->continue_data
= NULL
;
4808 free_nfs_cb_data(data
);
4814 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4817 struct nfs_link_data
*link_data
;
4819 link_data
= malloc(sizeof(struct nfs_link_data
));
4820 if (link_data
== NULL
) {
4821 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4824 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4826 link_data
->oldpath
= strdup(oldpath
);
4827 if (link_data
->oldpath
== NULL
) {
4828 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4829 free_nfs_link_data(link_data
);
4833 link_data
->newpath
= strdup(newpath
);
4834 if (link_data
->newpath
== NULL
) {
4835 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4836 free_nfs_link_data(link_data
);
4839 ptr
= strrchr(link_data
->newpath
, '/');
4841 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4842 free_nfs_link_data(link_data
);
4847 link_data
->newobject
= ptr
;
4850 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, 0, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4851 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4859 //qqq replace later with lseek()
4860 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4862 return nfsfh
->offset
;
4868 * Get the maximum supported READ3 size by the server
4870 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4872 return nfs
->readmax
;
4876 * Get the maximum supported WRITE3 size by the server
4878 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4880 return nfs
->writemax
;
4883 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4884 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4887 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4888 rpc_set_uid(nfs
->rpc
, uid
);
4891 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4892 rpc_set_gid(nfs
->rpc
, gid
);
4895 void nfs_set_readahead(struct nfs_context
*nfs
, uint32_t v
) {
4896 rpc_set_readahead(nfs
->rpc
, v
);
4899 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4904 va_start(ap
, error_string
);
4906 vsnprintf(str
, 1024, error_string
, ap
);
4907 if (nfs
->rpc
->error_string
!= NULL
) {
4908 free(nfs
->rpc
->error_string
);
4910 nfs
->rpc
->error_string
= str
;
4916 struct mount_cb_data
{
4922 static void free_mount_cb_data(struct mount_cb_data
*data
)
4924 if (data
->server
!= NULL
) {
4926 data
->server
= NULL
;
4932 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4934 struct mount_cb_data
*data
= private_data
;
4936 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4938 if (status
== RPC_STATUS_ERROR
) {
4939 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4940 free_mount_cb_data(data
);
4943 if (status
== RPC_STATUS_CANCEL
) {
4944 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4945 free_mount_cb_data(data
);
4949 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4950 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4951 rpc_set_error(rpc
, "Failed to disconnect\n");
4953 free_mount_cb_data(data
);
4956 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4958 struct mount_cb_data
*data
= private_data
;
4960 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4962 /* Dont want any more callbacks even if the socket is closed */
4963 rpc
->connect_cb
= NULL
;
4965 if (status
== RPC_STATUS_ERROR
) {
4966 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4967 free_mount_cb_data(data
);
4970 if (status
== RPC_STATUS_CANCEL
) {
4971 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4972 free_mount_cb_data(data
);
4976 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4977 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4978 free_mount_cb_data(data
);
4983 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4985 struct mount_cb_data
*data
;
4987 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4989 data
= malloc(sizeof(struct mount_cb_data
));
4993 memset(data
, 0, sizeof(struct mount_cb_data
));
4995 data
->private_data
= private_data
;
4996 data
->server
= strdup(server
);
4997 if (data
->server
== NULL
) {
4998 free_mount_cb_data(data
);
5001 if (rpc_connect_program_async(rpc
, data
->server
, MOUNT_PROGRAM
, MOUNT_V3
, mount_export_4_cb
, data
) != 0) {
5002 rpc_set_error(rpc
, "Failed to start connection");
5003 free_mount_cb_data(data
);
5010 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
5012 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
5016 const char *nfs_get_server(struct nfs_context
*nfs
) {
5020 const char *nfs_get_export(struct nfs_context
*nfs
) {
5024 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
5025 return &nfs
->rootfh
;
5028 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {