2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * High level api to nfs filesystems
25 #include "aros_compat.h"
29 #include "win32_compat.h"
37 #define statvfs statfs
50 #ifdef HAVE_SYS_STATVFS_H
51 #include <sys/statvfs.h>
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
68 #include <sys/types.h>
71 #include "libnfs-zdr.h"
73 #include "libnfs-raw.h"
74 #include "libnfs-raw-mount.h"
75 #include "libnfs-raw-nfs.h"
76 #include "libnfs-raw-portmap.h"
77 #include "libnfs-private.h"
80 struct nfsdirent
*entries
;
81 struct nfsdirent
*current
;
92 struct rpc_context
*rpc
;
95 struct nfs_fh3 rootfh
;
101 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
103 while (nfsdir
->entries
) {
104 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
105 if (nfsdir
->entries
->name
!= NULL
) {
106 free(nfsdir
->entries
->name
);
108 free(nfsdir
->entries
);
109 nfsdir
->entries
= dirent
;
115 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
118 struct nfs_context
*nfs
;
120 char *saved_path
, *path
;
125 continue_func continue_cb
;
127 void (*free_continue_data
)(void *);
132 /* for multi-read/write calls. */
137 uint64_t start_offset
, max_offset
, count
;
143 struct nfs_mcb_data
{
144 struct nfs_cb_data
*data
;
150 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
152 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
154 rpc_set_auth(nfs
->rpc
, auth
);
157 int nfs_get_fd(struct nfs_context
*nfs
)
159 return rpc_get_fd(nfs
->rpc
);
162 int nfs_queue_length(struct nfs_context
*nfs
)
164 return rpc_queue_length(nfs
->rpc
);
167 int nfs_which_events(struct nfs_context
*nfs
)
169 return rpc_which_events(nfs
->rpc
);
172 int nfs_service(struct nfs_context
*nfs
, int revents
)
174 return rpc_service(nfs
->rpc
, revents
);
177 char *nfs_get_error(struct nfs_context
*nfs
)
179 return rpc_get_error(nfs
->rpc
);
182 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
184 if (!strncmp(arg
, "tcp-syncnt", 10)) {
185 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
186 } else if (!strncmp(arg
, "uid", 3)) {
187 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
188 } else if (!strncmp(arg
, "gid", 3)) {
189 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
194 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
196 struct nfs_url
*urls
;
197 char *strp
, *flagsp
, *strp2
;
199 if (strncmp(url
, "nfs://", 6)) {
200 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
204 urls
= malloc(sizeof(struct nfs_url
));
206 rpc_set_error(nfs
->rpc
, "Out of memory");
210 memset(urls
, 0x00, sizeof(struct nfs_url
));
211 urls
->server
= strdup(url
+ 6);
212 if (urls
->server
== NULL
) {
213 nfs_destroy_url(urls
);
214 rpc_set_error(nfs
->rpc
, "Out of memory");
218 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
219 urls
->server
[0] == '?') {
221 flagsp
= strchr(urls
->server
, '?');
224 nfs_destroy_url(urls
);
225 rpc_set_error(nfs
->rpc
, "Invalid server string");
229 strp
= strchr(urls
->server
, '/');
232 flagsp
= strchr(urls
->server
, '?');
235 nfs_destroy_url(urls
);
236 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
240 urls
->path
= strdup(strp
);
241 if (urls
->path
== NULL
) {
242 nfs_destroy_url(urls
);
243 rpc_set_error(nfs
->rpc
, "Out of memory");
249 flagsp
= strchr(urls
->path
, '?');
253 strp
= strrchr(urls
->path
, '/');
256 flagsp
= strchr(urls
->path
, '?');
259 nfs_destroy_url(urls
);
260 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
263 urls
->file
= strdup(strp
);
264 if (urls
->path
== NULL
) {
265 nfs_destroy_url(urls
);
266 rpc_set_error(nfs
->rpc
, "Out of memory");
270 flagsp
= strchr(urls
->file
, '?');
277 if (urls
->file
&& !strlen(urls
->file
)) {
281 nfs_destroy_url(urls
);
282 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
287 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
289 flagsp
= strchr(strp
, '&');
293 strp2
= strchr(strp
, '=');
297 nfs_set_context_args(nfs
, strp
, strp2
);
301 if (urls
->server
&& strlen(urls
->server
) <= 1) {
309 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
311 return nfs_parse_url(nfs
, url
, 0, 0);
314 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
316 return nfs_parse_url(nfs
, url
, 1, 0);
319 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
321 return nfs_parse_url(nfs
, url
, 0, 1);
325 void nfs_destroy_url(struct nfs_url
*url
)
335 struct nfs_context
*nfs_init_context(void)
337 struct nfs_context
*nfs
;
339 nfs
= malloc(sizeof(struct nfs_context
));
343 memset(nfs
, 0, sizeof(struct nfs_context
));
345 nfs
->rpc
= rpc_init_context();
346 if (nfs
->rpc
== NULL
) {
351 nfs
->cwd
= strdup("/");
356 void nfs_destroy_context(struct nfs_context
*nfs
)
358 rpc_destroy_context(nfs
->rpc
);
376 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
377 free(nfs
->rootfh
.data
.data_val
);
378 nfs
->rootfh
.data
.data_val
= NULL
;
393 void free_rpc_cb_data(struct rpc_cb_data
*data
)
400 static void rpc_connect_program_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
402 struct rpc_cb_data
*data
= private_data
;
404 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
406 /* Dont want any more callbacks even if the socket is closed */
407 rpc
->connect_cb
= NULL
;
409 if (status
== RPC_STATUS_ERROR
) {
410 data
->cb(rpc
, status
, command_data
, data
->private_data
);
411 free_rpc_cb_data(data
);
414 if (status
== RPC_STATUS_CANCEL
) {
415 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
416 free_rpc_cb_data(data
);
420 data
->cb(rpc
, status
, NULL
, data
->private_data
);
421 free_rpc_cb_data(data
);
424 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
426 struct rpc_cb_data
*data
= private_data
;
428 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
430 /* Dont want any more callbacks even if the socket is closed */
431 rpc
->connect_cb
= NULL
;
433 if (status
== RPC_STATUS_ERROR
) {
434 data
->cb(rpc
, status
, command_data
, data
->private_data
);
435 free_rpc_cb_data(data
);
438 if (status
== RPC_STATUS_CANCEL
) {
439 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
440 free_rpc_cb_data(data
);
444 switch (data
->program
) {
446 if (rpc_mount3_null_async(rpc
, rpc_connect_program_5_cb
,
448 data
->cb(rpc
, status
, command_data
, data
->private_data
);
449 free_rpc_cb_data(data
);
454 if (rpc_nfs3_null_async(rpc
, rpc_connect_program_5_cb
,
456 data
->cb(rpc
, status
, command_data
, data
->private_data
);
457 free_rpc_cb_data(data
);
463 data
->cb(rpc
, status
, NULL
, data
->private_data
);
464 free_rpc_cb_data(data
);
467 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
469 struct rpc_cb_data
*data
= private_data
;
470 struct pmap3_string_result
*gar
;
471 uint32_t rpc_port
= 0;
474 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
476 if (status
== RPC_STATUS_ERROR
) {
477 data
->cb(rpc
, status
, command_data
, data
->private_data
);
478 free_rpc_cb_data(data
);
481 if (status
== RPC_STATUS_CANCEL
) {
482 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
483 free_rpc_cb_data(data
);
487 switch (rpc
->s
.ss_family
) {
489 rpc_port
= *(uint32_t *)command_data
;
492 /* ouch. portmapper and ipv6 are not great */
494 if (gar
->addr
== NULL
) {
497 ptr
= strrchr(gar
->addr
, '.');
501 rpc_port
= atoi(ptr
+ 1);
503 ptr
= strrchr(gar
->addr
, '.');
507 rpc_port
+= 256 * atoi(ptr
+ 1);
511 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
512 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
513 free_rpc_cb_data(data
);
517 rpc_disconnect(rpc
, "normal disconnect");
518 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
519 data
->cb(rpc
, status
, command_data
, data
->private_data
);
520 free_rpc_cb_data(data
);
525 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
527 struct rpc_cb_data
*data
= private_data
;
528 struct pmap3_mapping map
;
530 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
532 if (status
== RPC_STATUS_ERROR
) {
533 data
->cb(rpc
, status
, command_data
, data
->private_data
);
534 free_rpc_cb_data(data
);
537 if (status
== RPC_STATUS_CANCEL
) {
538 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
539 free_rpc_cb_data(data
);
543 switch (rpc
->s
.ss_family
) {
545 if (rpc_pmap2_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
546 data
->cb(rpc
, status
, command_data
, data
->private_data
);
547 free_rpc_cb_data(data
);
552 map
.prog
=data
->program
;
553 map
.vers
=data
->version
;
557 if (rpc_pmap3_getaddr_async(rpc
, &map
, rpc_connect_program_3_cb
, private_data
) != 0) {
558 data
->cb(rpc
, status
, command_data
, data
->private_data
);
559 free_rpc_cb_data(data
);
566 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
568 struct rpc_cb_data
*data
= private_data
;
570 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
572 /* Dont want any more callbacks even if the socket is closed */
573 rpc
->connect_cb
= NULL
;
575 if (status
== RPC_STATUS_ERROR
) {
576 data
->cb(rpc
, status
, command_data
, data
->private_data
);
577 free_rpc_cb_data(data
);
580 if (status
== RPC_STATUS_CANCEL
) {
581 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
582 free_rpc_cb_data(data
);
586 switch (rpc
->s
.ss_family
) {
588 if (rpc_pmap2_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
589 data
->cb(rpc
, status
, command_data
, data
->private_data
);
590 free_rpc_cb_data(data
);
595 if (rpc_pmap3_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
596 data
->cb(rpc
, status
, command_data
, data
->private_data
);
597 free_rpc_cb_data(data
);
604 int rpc_connect_program_async(struct rpc_context
*rpc
, const char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
606 struct rpc_cb_data
*data
;
608 data
= malloc(sizeof(struct rpc_cb_data
));
612 memset(data
, 0, sizeof(struct rpc_cb_data
));
613 data
->server
= strdup(server
);
614 data
->program
= program
;
615 data
->version
= version
;
618 data
->private_data
= private_data
;
620 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
621 rpc_set_error(rpc
, "Failed to start connection");
622 free_rpc_cb_data(data
);
628 static void free_nfs_cb_data(struct nfs_cb_data
*data
)
630 if (data
->continue_data
!= NULL
) {
631 assert(data
->free_continue_data
);
632 data
->free_continue_data(data
->continue_data
);
635 free(data
->saved_path
);
636 free(data
->fh
.data
.data_val
);
646 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
648 struct nfs_cb_data
*data
= private_data
;
649 struct nfs_context
*nfs
= data
->nfs
;
651 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
653 if (status
== RPC_STATUS_ERROR
) {
654 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
655 free_nfs_cb_data(data
);
658 if (status
== RPC_STATUS_CANCEL
) {
659 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
660 free_nfs_cb_data(data
);
664 data
->cb(0, nfs
, NULL
, data
->private_data
);
665 free_nfs_cb_data(data
);
668 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
670 struct nfs_cb_data
*data
= private_data
;
671 struct nfs_context
*nfs
= data
->nfs
;
672 FSINFO3res
*res
= command_data
;
673 struct GETATTR3args args
;
675 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
677 if (status
== RPC_STATUS_ERROR
) {
678 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
679 free_nfs_cb_data(data
);
682 if (status
== RPC_STATUS_CANCEL
) {
683 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
684 free_nfs_cb_data(data
);
688 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
689 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
691 memset(&args
, 0, sizeof(GETATTR3args
));
692 args
.object
= nfs
->rootfh
;
694 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
695 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
696 free_nfs_cb_data(data
);
701 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
703 struct nfs_cb_data
*data
= private_data
;
704 struct nfs_context
*nfs
= data
->nfs
;
705 struct FSINFO3args args
;
707 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
709 if (status
== RPC_STATUS_ERROR
) {
710 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
711 free_nfs_cb_data(data
);
714 if (status
== RPC_STATUS_CANCEL
) {
715 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
716 free_nfs_cb_data(data
);
720 args
.fsroot
= nfs
->rootfh
;
721 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
722 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
723 free_nfs_cb_data(data
);
728 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
730 struct nfs_cb_data
*data
= private_data
;
731 struct nfs_context
*nfs
= data
->nfs
;
734 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
736 if (status
== RPC_STATUS_ERROR
) {
737 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
738 free_nfs_cb_data(data
);
741 if (status
== RPC_STATUS_CANCEL
) {
742 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
743 free_nfs_cb_data(data
);
748 if (res
->fhs_status
!= MNT3_OK
) {
749 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
));
750 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
751 free_nfs_cb_data(data
);
755 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
756 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
757 if (nfs
->rootfh
.data
.data_val
== NULL
) {
758 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
759 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
760 free_nfs_cb_data(data
);
763 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
765 rpc_disconnect(rpc
, "normal disconnect");
767 if (rpc_connect_program_async(nfs
->rpc
, nfs
->server
, NFS_PROGRAM
, NFS_V3
, nfs_mount_8_cb
, data
) != 0) {
768 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
769 free_nfs_cb_data(data
);
773 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
774 rpc_set_autoreconnect(rpc
);
778 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
780 struct nfs_cb_data
*data
= private_data
;
781 struct nfs_context
*nfs
= data
->nfs
;
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 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
797 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
798 free_nfs_cb_data(data
);
804 * Async call for mounting an nfs share and geting the root filehandle
806 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
808 struct nfs_cb_data
*data
;
809 char *new_server
, *new_export
;
811 data
= malloc(sizeof(struct nfs_cb_data
));
813 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
816 memset(data
, 0, sizeof(struct nfs_cb_data
));
817 new_server
= strdup(server
);
818 new_export
= strdup(export
);
819 if (nfs
->server
!= NULL
) {
822 nfs
->server
= new_server
;
823 if (nfs
->export
!= NULL
) {
826 nfs
->export
= new_export
;
829 data
->private_data
= private_data
;
831 if (rpc_connect_program_async(nfs
->rpc
, server
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_5_cb
, data
) != 0) {
832 rpc_set_error(nfs
->rpc
, "Failed to start connection");
833 free_nfs_cb_data(data
);
843 * Functions to first look up a path, component by component, and then finally call a specific function once
844 * the filehandle for the final component is found.
846 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
848 struct nfs_cb_data
*data
= private_data
;
849 struct nfs_context
*nfs
= data
->nfs
;
852 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
854 if (status
== RPC_STATUS_ERROR
) {
855 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
856 free_nfs_cb_data(data
);
859 if (status
== RPC_STATUS_CANCEL
) {
860 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
861 free_nfs_cb_data(data
);
866 if (res
->status
!= NFS3_OK
) {
867 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
));
868 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
869 free_nfs_cb_data(data
);
873 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
874 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
875 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
876 free_nfs_cb_data(data
);
881 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
886 while (*data
->path
== '/') {
891 slash
= strchr(path
, '/');
893 /* Clear slash so that path is a zero terminated string for
894 * the current path component. Set it back to '/' again later
895 * when we are finished referencing this component so that
896 * data->saved_path will still point to the full
900 data
->path
= slash
+1;
902 while (*data
->path
!= 0) {
908 data
->fh
.data
.data_len
= fh
->data
.data_len
;
909 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
910 if (data
->fh
.data
.data_val
== NULL
) {
911 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
912 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
913 free_nfs_cb_data(data
);
916 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
920 data
->continue_cb(nfs
, data
);
925 memset(&args
, 0, sizeof(LOOKUP3args
));
927 args
.what
.name
= path
;
929 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
930 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
931 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
932 free_nfs_cb_data(data
);
941 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
947 while ((str
= strstr(path
, "//"))) {
955 while ((str
= strstr(path
, "/./"))) {
963 if (!strncmp(path
, "/../", 4)) {
964 rpc_set_error(nfs
->rpc
,
965 "Absolute path starts with '/../' "
966 "during normalization");
971 if (path
[0] != '/') {
972 rpc_set_error(nfs
->rpc
,
973 "Absolute path does not start with '/'");
977 /* /string/../ -> / */
978 while ((str
= strstr(path
, "/../"))) {
981 if (!strncmp(path
, "/../", 4)) {
982 rpc_set_error(nfs
->rpc
,
983 "Absolute path starts with '/../' "
984 "during normalization");
989 while (*tmp
!= '/') {
993 while((*(tmp
++) = *(str
++)) != '\0')
1000 if (path
[len
- 1] == '/') {
1001 path
[len
- 1] = '\0';
1005 if (path
[0] == '\0') {
1006 rpc_set_error(nfs
->rpc
,
1007 "Absolute path became '' "
1008 "during normalization");
1014 if (!strcmp(&path
[len
- 2], "/.")) {
1015 path
[len
- 2] = '\0';
1020 /* ^/..$ -> error */
1021 if (!strcmp(path
, "/..")) {
1022 rpc_set_error(nfs
->rpc
,
1023 "Absolute path is '/..' "
1024 "during normalization");
1028 /* /string/..$ -> / */
1030 if (!strcmp(&path
[len
- 3], "/..")) {
1031 char *tmp
= &path
[len
- 3];
1032 while (*--tmp
!= '/')
1041 static int nfs_lookuppath_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
, continue_func continue_cb
, void *continue_data
, void (*free_continue_data
)(void *), int continue_int
)
1043 struct nfs_cb_data
*data
;
1045 if (path
[0] == '\0') {
1049 data
= malloc(sizeof(struct nfs_cb_data
));
1051 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1052 "nfs_cb_data structure");
1053 if (free_continue_data
)
1054 free_continue_data(continue_data
);
1057 memset(data
, 0, sizeof(struct nfs_cb_data
));
1060 data
->continue_cb
= continue_cb
;
1061 data
->continue_data
= continue_data
;
1062 data
->free_continue_data
= free_continue_data
;
1063 data
->continue_int
= continue_int
;
1064 data
->private_data
= private_data
;
1065 if (path
[0] == '/') {
1066 data
->saved_path
= strdup(path
);
1068 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1069 if (data
->saved_path
== NULL
) {
1070 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1071 "malloc path string");
1072 free_nfs_cb_data(data
);
1075 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1078 if (data
->saved_path
== NULL
) {
1079 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1080 free_nfs_cb_data(data
);
1083 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1084 free_nfs_cb_data(data
);
1088 data
->path
= data
->saved_path
;
1090 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1091 /* return 0 here since the callback will be invoked if there is a failure */
1104 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1107 struct nfs_cb_data
*data
= private_data
;
1108 struct nfs_context
*nfs
= data
->nfs
;
1115 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1117 if (status
== RPC_STATUS_ERROR
) {
1118 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1119 free_nfs_cb_data(data
);
1122 if (status
== RPC_STATUS_CANCEL
) {
1123 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1124 free_nfs_cb_data(data
);
1129 if (res
->status
!= NFS3_OK
) {
1130 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
));
1131 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1132 free_nfs_cb_data(data
);
1137 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1138 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1139 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1140 st
.st_mode
|= S_IFDIR
;
1142 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1143 st
.st_mode
|= S_IFREG
;
1145 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1146 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1147 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1149 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1151 st
.st_blksize
= 4096;
1152 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1154 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1155 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1156 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1158 data
->cb(0, nfs
, &st
, data
->private_data
);
1159 free_nfs_cb_data(data
);
1162 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1164 struct GETATTR3args args
;
1166 memset(&args
, 0, sizeof(GETATTR3args
));
1167 args
.object
= data
->fh
;
1169 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1170 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1171 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1172 free_nfs_cb_data(data
);
1178 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1180 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1181 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1190 * Async nfs_stat64()
1192 static void nfs_stat64_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1195 struct nfs_cb_data
*data
= private_data
;
1196 struct nfs_context
*nfs
= data
->nfs
;
1197 struct nfs_stat_64 st
;
1199 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1201 if (status
== RPC_STATUS_ERROR
) {
1202 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1203 free_nfs_cb_data(data
);
1206 if (status
== RPC_STATUS_CANCEL
) {
1207 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1208 free_nfs_cb_data(data
);
1213 if (res
->status
!= NFS3_OK
) {
1214 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
));
1215 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1216 free_nfs_cb_data(data
);
1221 st
.nfs_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1222 st
.nfs_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1223 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1224 st
.nfs_mode
|= S_IFDIR
;
1226 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1227 st
.nfs_mode
|= S_IFREG
;
1229 st
.nfs_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1230 st
.nfs_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1231 st
.nfs_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1233 st
.nfs_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1234 st
.nfs_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1235 st
.nfs_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1236 st
.nfs_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1238 data
->cb(0, nfs
, &st
, data
->private_data
);
1239 free_nfs_cb_data(data
);
1242 static int nfs_stat64_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1244 struct GETATTR3args args
;
1246 memset(&args
, 0, sizeof(GETATTR3args
));
1247 args
.object
= data
->fh
;
1249 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
1250 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1251 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1252 free_nfs_cb_data(data
);
1258 int nfs_stat64_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1260 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat64_continue_internal
, NULL
, NULL
, 0) != 0) {
1261 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1271 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1273 struct nfs_cb_data
*data
= private_data
;
1274 struct nfs_context
*nfs
= data
->nfs
;
1275 struct nfsfh
*nfsfh
;
1278 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1280 if (status
== RPC_STATUS_ERROR
) {
1281 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1282 free_nfs_cb_data(data
);
1285 if (status
== RPC_STATUS_CANCEL
) {
1286 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1287 free_nfs_cb_data(data
);
1292 if (res
->status
!= NFS3_OK
) {
1293 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1294 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1295 free_nfs_cb_data(data
);
1299 nfsfh
= malloc(sizeof(struct nfsfh
));
1300 if (nfsfh
== NULL
) {
1301 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1302 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1303 free_nfs_cb_data(data
);
1306 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1308 if (data
->continue_int
& O_SYNC
) {
1311 if (data
->continue_int
& O_APPEND
) {
1312 nfsfh
->is_append
= 1;
1315 /* steal the filehandle */
1316 nfsfh
->fh
= data
->fh
;
1317 data
->fh
.data
.data_val
= NULL
;
1319 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1320 free_nfs_cb_data(data
);
1323 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1326 struct nfs_cb_data
*data
= private_data
;
1327 struct nfs_context
*nfs
= data
->nfs
;
1328 struct nfsfh
*nfsfh
;
1329 unsigned int nfsmode
= 0;
1331 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1333 if (status
== RPC_STATUS_ERROR
) {
1334 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1335 free_nfs_cb_data(data
);
1338 if (status
== RPC_STATUS_CANCEL
) {
1339 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1340 free_nfs_cb_data(data
);
1345 if (res
->status
!= NFS3_OK
) {
1346 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
));
1347 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1348 free_nfs_cb_data(data
);
1352 if (data
->continue_int
& O_WRONLY
) {
1353 nfsmode
|= ACCESS3_MODIFY
;
1355 if (data
->continue_int
& O_RDWR
) {
1356 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1358 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1359 nfsmode
|= ACCESS3_READ
;
1363 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1364 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1365 nfsmode
&ACCESS3_READ
?'r':'-',
1366 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1367 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1368 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1369 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1370 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1371 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1372 free_nfs_cb_data(data
);
1376 /* Try to truncate it if we were requested to */
1377 if ((data
->continue_int
& O_TRUNC
) &&
1378 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1381 memset(&args
, 0, sizeof(SETATTR3args
));
1382 args
.object
= data
->fh
;
1383 args
.new_attributes
.size
.set_it
= 1;
1384 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1386 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1388 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1389 "SETATTR call for %s", data
->path
);
1390 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1391 data
->private_data
);
1392 free_nfs_cb_data(data
);
1398 nfsfh
= malloc(sizeof(struct nfsfh
));
1399 if (nfsfh
== NULL
) {
1400 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1401 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1402 free_nfs_cb_data(data
);
1405 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1407 if (data
->continue_int
& O_SYNC
) {
1410 if (data
->continue_int
& O_APPEND
) {
1411 nfsfh
->is_append
= 1;
1414 /* steal the filehandle */
1415 nfsfh
->fh
= data
->fh
;
1416 data
->fh
.data
.data_val
= NULL
;
1418 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1419 free_nfs_cb_data(data
);
1422 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1427 if (data
->continue_int
& O_WRONLY
) {
1428 nfsmode
|= ACCESS3_MODIFY
;
1430 if (data
->continue_int
& O_RDWR
) {
1431 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1433 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1434 nfsmode
|= ACCESS3_READ
;
1437 memset(&args
, 0, sizeof(ACCESS3args
));
1438 args
.object
= data
->fh
;
1439 args
.access
= nfsmode
;
1441 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1442 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1443 "call for %s", data
->path
);
1444 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1445 data
->private_data
);
1446 free_nfs_cb_data(data
);
1452 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1454 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1455 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1466 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1468 /* steal saved_path */
1470 nfs
->cwd
= data
->saved_path
;
1471 data
->saved_path
= NULL
;
1473 data
->cb(0, nfs
, NULL
, data
->private_data
);
1474 free_nfs_cb_data(data
);
1479 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1481 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1482 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1493 static void nfs_fill_READ3args(READ3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
)
1495 memset(args
, 0, sizeof(READ3args
));
1496 args
->file
= fh
->fh
;
1497 args
->offset
= offset
;
1498 args
->count
= count
;
1501 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1503 struct nfs_mcb_data
*mdata
= private_data
;
1504 struct nfs_cb_data
*data
= mdata
->data
;
1505 struct nfs_context
*nfs
= data
->nfs
;
1508 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1512 if (status
== RPC_STATUS_ERROR
) {
1513 /* flag the failure but do not invoke callback until we have received all responses */
1516 if (status
== RPC_STATUS_CANCEL
) {
1517 /* flag the cancellation but do not invoke callback until we have received all responses */
1521 if (status
== RPC_STATUS_SUCCESS
) {
1523 if (res
->status
!= NFS3_OK
) {
1524 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1527 uint64_t count
= res
->READ3res_u
.resok
.count
;
1529 if (mdata
->update_pos
)
1530 data
->nfsfh
->offset
+= count
;
1532 /* if we have more than one call or we have received a short read we need a reassembly buffer */
1533 if (data
->num_calls
|| (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
)) {
1534 if (data
->buffer
== NULL
) {
1535 data
->buffer
= malloc(data
->request_size
);
1536 if (data
->buffer
== NULL
) {
1537 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data
->request_size
);
1543 if (count
<= mdata
->count
) {
1544 /* copy data into reassembly buffer if we have one */
1545 if (data
->buffer
!= NULL
) {
1546 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, count
);
1548 if (data
->max_offset
< mdata
->offset
+ count
) {
1549 data
->max_offset
= mdata
->offset
+ count
;
1552 rpc_set_error(nfs
->rpc
, "NFS: Read overflow. Server has sent more data than requested!");
1556 /* check if we have received a short read */
1557 if (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
) {
1559 rpc_set_error(nfs
->rpc
, "NFS: Read failed. No bytes read and not at EOF!");
1562 /* reissue reminder of this read request */
1564 mdata
->offset
+= count
;
1565 mdata
->count
-= count
;
1566 nfs_fill_READ3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
);
1567 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) == 0) {
1571 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1581 if (data
->num_calls
> 0) {
1582 /* still waiting for more replies */
1585 if (data
->oom
!= 0) {
1586 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1587 free_nfs_cb_data(data
);
1590 if (data
->error
!= 0) {
1591 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1592 free_nfs_cb_data(data
);
1595 if (data
->cancel
!= 0) {
1596 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1597 free_nfs_cb_data(data
);
1602 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1604 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1607 free_nfs_cb_data(data
);
1610 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
)
1612 struct nfs_cb_data
*data
;
1614 data
= malloc(sizeof(struct nfs_cb_data
));
1616 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1619 memset(data
, 0, sizeof(struct nfs_cb_data
));
1622 data
->private_data
= private_data
;
1623 data
->nfsfh
= nfsfh
;
1624 data
->request_size
= count
;
1626 assert(data
->num_calls
== 0);
1628 /* chop requests into chunks of at most READMAX bytes if necessary.
1629 * we send all reads in parallel so that performance is still good.
1631 data
->max_offset
= offset
;
1632 data
->start_offset
= offset
;
1635 uint64_t readcount
= count
;
1636 struct nfs_mcb_data
*mdata
;
1639 if (readcount
> nfs_get_readmax(nfs
)) {
1640 readcount
= nfs_get_readmax(nfs
);
1643 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1644 if (mdata
== NULL
) {
1645 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1646 if (data
->num_calls
== 0) {
1647 free_nfs_cb_data(data
);
1653 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1655 mdata
->offset
= offset
;
1656 mdata
->count
= readcount
;
1657 mdata
->update_pos
= update_pos
;
1659 nfs_fill_READ3args(&args
, nfsfh
, offset
, readcount
);
1661 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1662 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1664 if (data
->num_calls
== 0) {
1665 free_nfs_cb_data(data
);
1673 offset
+= readcount
;
1675 } while (count
> 0);
1680 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1682 return nfs_pread_async_internal(nfs
, nfsfh
, offset
, count
, cb
, private_data
, 0);
1688 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1690 return nfs_pread_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
, 1);
1698 static void nfs_fill_WRITE3args (WRITE3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
,
1701 memset(args
, 0, sizeof(WRITE3args
));
1702 args
->file
= fh
->fh
;
1703 args
->offset
= offset
;
1704 args
->count
= count
;
1705 args
->stable
= fh
->is_sync
? FILE_SYNC
: UNSTABLE
;
1706 args
->data
.data_len
= count
;
1707 args
->data
.data_val
= buf
;
1710 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1712 struct nfs_mcb_data
*mdata
= private_data
;
1713 struct nfs_cb_data
*data
= mdata
->data
;
1714 struct nfs_context
*nfs
= data
->nfs
;
1717 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1721 if (status
== RPC_STATUS_ERROR
) {
1722 /* flag the failure but do not invoke callback until we have received all responses */
1725 if (status
== RPC_STATUS_CANCEL
) {
1726 /* flag the cancellation but do not invoke callback until we have received all responses */
1730 if (status
== RPC_STATUS_SUCCESS
) {
1732 if (res
->status
!= NFS3_OK
) {
1733 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1736 uint64_t count
= res
->WRITE3res_u
.resok
.count
;
1738 if (mdata
->update_pos
)
1739 data
->nfsfh
->offset
+= count
;
1741 if (count
< mdata
->count
) {
1743 rpc_set_error(nfs
->rpc
, "NFS: Write failed. No bytes written!");
1746 /* reissue reminder of this write request */
1748 mdata
->offset
+= count
;
1749 mdata
->count
-= count
;
1751 nfs_fill_WRITE3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
,
1752 &data
->usrbuf
[mdata
->offset
- data
->start_offset
]);
1753 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) == 0) {
1757 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1763 if (data
->max_offset
< mdata
->offset
+ count
) {
1764 data
->max_offset
= mdata
->offset
+ count
;
1772 if (data
->num_calls
> 0) {
1773 /* still waiting for more replies */
1776 if (data
->oom
!= 0) {
1777 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1778 free_nfs_cb_data(data
);
1781 if (data
->error
!= 0) {
1782 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1783 free_nfs_cb_data(data
);
1786 if (data
->cancel
!= 0) {
1787 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1788 free_nfs_cb_data(data
);
1792 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1794 free_nfs_cb_data(data
);
1798 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
)
1800 struct nfs_cb_data
*data
;
1802 data
= malloc(sizeof(struct nfs_cb_data
));
1804 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1807 memset(data
, 0, sizeof(struct nfs_cb_data
));
1810 data
->private_data
= private_data
;
1811 data
->nfsfh
= nfsfh
;
1814 /* hello, clang-analyzer */
1815 assert(data
->num_calls
== 0);
1817 /* chop requests into chunks of at most WRITEMAX bytes if necessary.
1818 * we send all writes in parallel so that performance is still good.
1820 data
->max_offset
= offset
;
1821 data
->start_offset
= offset
;
1824 uint64_t writecount
= count
;
1825 struct nfs_mcb_data
*mdata
;
1828 if (writecount
> nfs_get_writemax(nfs
)) {
1829 writecount
= nfs_get_writemax(nfs
);
1832 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1833 if (mdata
== NULL
) {
1834 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1835 if (data
->num_calls
== 0) {
1836 free_nfs_cb_data(data
);
1842 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1844 mdata
->offset
= offset
;
1845 mdata
->count
= writecount
;
1846 mdata
->update_pos
= update_pos
;
1848 nfs_fill_WRITE3args(&args
, nfsfh
, offset
, writecount
, &buf
[offset
- data
->start_offset
]);
1850 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1851 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1853 if (data
->num_calls
== 0) {
1854 free_nfs_cb_data(data
);
1861 count
-= writecount
;
1862 offset
+= writecount
;
1864 } while (count
> 0);
1869 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
)
1871 return nfs_pwrite_async_internal(nfs
, nfsfh
, offset
, count
, buf
, cb
, private_data
, 0);
1877 static void nfs_write_append_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1879 struct nfs_cb_data
*data
= private_data
;
1880 struct nfs_context
*nfs
= data
->nfs
;
1883 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1885 if (status
== RPC_STATUS_ERROR
) {
1886 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1887 free_nfs_cb_data(data
);
1890 if (status
== RPC_STATUS_CANCEL
) {
1891 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1892 free_nfs_cb_data(data
);
1897 if (res
->status
!= NFS3_OK
) {
1898 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1899 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1900 free_nfs_cb_data(data
);
1904 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) {
1905 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1906 free_nfs_cb_data(data
);
1909 free_nfs_cb_data(data
);
1912 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1914 if (nfsfh
->is_append
) {
1915 struct GETATTR3args args
;
1916 struct nfs_cb_data
*data
;
1918 data
= malloc(sizeof(struct nfs_cb_data
));
1920 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1923 memset(data
, 0, sizeof(struct nfs_cb_data
));
1926 data
->private_data
= private_data
;
1927 data
->nfsfh
= nfsfh
;
1929 data
->count
= count
;
1931 memset(&args
, 0, sizeof(GETATTR3args
));
1932 args
.object
= nfsfh
->fh
;
1934 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_write_append_cb
, &args
, data
) != 0) {
1935 rpc_set_error(nfs
->rpc
, "out of memory: failed to send GETATTR");
1936 free_nfs_cb_data(data
);
1941 return nfs_pwrite_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
, 1);
1951 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1953 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1954 free(nfsfh
->fh
.data
.data_val
);
1955 nfsfh
->fh
.data
.data_val
= NULL
;
1959 cb(0, nfs
, NULL
, private_data
);
1970 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1972 struct nfs_cb_data
*data
;
1973 struct GETATTR3args args
;
1975 data
= malloc(sizeof(struct nfs_cb_data
));
1977 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1980 memset(data
, 0, sizeof(struct nfs_cb_data
));
1983 data
->private_data
= private_data
;
1985 memset(&args
, 0, sizeof(GETATTR3args
));
1986 args
.object
= nfsfh
->fh
;
1988 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1989 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1990 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1991 free_nfs_cb_data(data
);
2002 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2004 struct nfs_cb_data
*data
= private_data
;
2005 struct nfs_context
*nfs
= data
->nfs
;
2008 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2010 if (status
== RPC_STATUS_ERROR
) {
2011 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2012 free_nfs_cb_data(data
);
2015 if (status
== RPC_STATUS_CANCEL
) {
2016 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2017 free_nfs_cb_data(data
);
2022 if (res
->status
!= NFS3_OK
) {
2023 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2024 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2025 free_nfs_cb_data(data
);
2029 data
->cb(0, nfs
, NULL
, data
->private_data
);
2030 free_nfs_cb_data(data
);
2033 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2035 struct nfs_cb_data
*data
;
2036 struct COMMIT3args args
;
2038 data
= malloc(sizeof(struct nfs_cb_data
));
2040 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2043 memset(data
, 0, sizeof(struct nfs_cb_data
));
2046 data
->private_data
= private_data
;
2048 args
.file
= nfsfh
->fh
;
2051 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
2052 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
2053 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2054 free_nfs_cb_data(data
);
2066 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2068 struct nfs_cb_data
*data
= private_data
;
2069 struct nfs_context
*nfs
= data
->nfs
;
2072 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2074 if (status
== RPC_STATUS_ERROR
) {
2075 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2076 free_nfs_cb_data(data
);
2079 if (status
== RPC_STATUS_CANCEL
) {
2080 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2081 free_nfs_cb_data(data
);
2086 if (res
->status
!= NFS3_OK
) {
2087 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2088 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2089 free_nfs_cb_data(data
);
2093 data
->cb(0, nfs
, NULL
, data
->private_data
);
2094 free_nfs_cb_data(data
);
2097 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
2099 struct nfs_cb_data
*data
;
2102 data
= malloc(sizeof(struct nfs_cb_data
));
2104 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2107 memset(data
, 0, sizeof(struct nfs_cb_data
));
2110 data
->private_data
= private_data
;
2112 memset(&args
, 0, sizeof(SETATTR3args
));
2113 args
.object
= nfsfh
->fh
;
2114 args
.new_attributes
.size
.set_it
= 1;
2115 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2117 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2118 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2119 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2120 free_nfs_cb_data(data
);
2130 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2132 uint64_t offset
= data
->continue_int
;
2135 nfsfh
.fh
= data
->fh
;
2137 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2138 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2139 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2140 free_nfs_cb_data(data
);
2143 free_nfs_cb_data(data
);
2147 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2153 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2154 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2167 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2170 struct nfs_cb_data
*data
= private_data
;
2171 struct nfs_context
*nfs
= data
->nfs
;
2172 char *str
= data
->continue_data
;
2174 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2176 str
= &str
[strlen(str
) + 1];
2178 if (status
== RPC_STATUS_ERROR
) {
2179 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2180 free_nfs_cb_data(data
);
2183 if (status
== RPC_STATUS_CANCEL
) {
2184 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2185 free_nfs_cb_data(data
);
2190 if (res
->status
!= NFS3_OK
) {
2191 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
));
2192 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2193 free_nfs_cb_data(data
);
2197 data
->cb(0, nfs
, NULL
, data
->private_data
);
2198 free_nfs_cb_data(data
);
2201 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2203 char *str
= data
->continue_data
;
2206 str
= &str
[strlen(str
) + 1];
2208 memset(&args
, 0, sizeof(MKDIR3args
));
2209 args
.where
.dir
= data
->fh
;
2210 args
.where
.name
= str
;
2211 args
.attributes
.mode
.set_it
= 1;
2212 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2214 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2215 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2216 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2217 free_nfs_cb_data(data
);
2223 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2228 new_path
= strdup(path
);
2229 if (new_path
== NULL
) {
2230 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2234 ptr
= strrchr(new_path
, '/');
2237 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2242 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2243 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2244 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2258 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2261 struct nfs_cb_data
*data
= private_data
;
2262 struct nfs_context
*nfs
= data
->nfs
;
2263 char *str
= data
->continue_data
;
2265 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2267 str
= &str
[strlen(str
) + 1];
2269 if (status
== RPC_STATUS_ERROR
) {
2270 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2271 free_nfs_cb_data(data
);
2274 if (status
== RPC_STATUS_CANCEL
) {
2275 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2276 free_nfs_cb_data(data
);
2281 if (res
->status
!= NFS3_OK
) {
2282 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
));
2283 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2284 free_nfs_cb_data(data
);
2288 data
->cb(0, nfs
, NULL
, data
->private_data
);
2289 free_nfs_cb_data(data
);
2292 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2294 char *str
= data
->continue_data
;
2297 str
= &str
[strlen(str
) + 1];
2299 args
.object
.dir
= data
->fh
;
2300 args
.object
.name
= str
;
2301 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2302 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2303 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2304 free_nfs_cb_data(data
);
2310 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2315 new_path
= strdup(path
);
2316 if (new_path
== NULL
) {
2317 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2321 ptr
= strrchr(new_path
, '/');
2324 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2329 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2330 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2331 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2344 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2347 struct nfs_cb_data
*data
= private_data
;
2348 struct nfs_context
*nfs
= data
->nfs
;
2349 struct nfsfh
*nfsfh
;
2350 char *str
= data
->continue_data
;
2352 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2354 if (status
== RPC_STATUS_ERROR
) {
2355 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2356 free_nfs_cb_data(data
);
2359 if (status
== RPC_STATUS_CANCEL
) {
2360 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2361 free_nfs_cb_data(data
);
2365 str
= &str
[strlen(str
) + 1];
2367 if (res
->status
!= NFS3_OK
) {
2368 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
));
2369 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2374 nfsfh
= malloc(sizeof(struct nfsfh
));
2375 if (nfsfh
== NULL
) {
2376 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2377 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2378 free_nfs_cb_data(data
);
2381 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2383 /* copy the filehandle */
2384 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2385 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2386 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2388 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2389 free_nfs_cb_data(data
);
2394 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2397 struct nfs_cb_data
*data
= private_data
;
2398 struct nfs_context
*nfs
= data
->nfs
;
2399 char *str
= data
->continue_data
;
2402 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2404 if (status
== RPC_STATUS_ERROR
) {
2405 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2406 free_nfs_cb_data(data
);
2409 if (status
== RPC_STATUS_CANCEL
) {
2410 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2411 free_nfs_cb_data(data
);
2415 str
= &str
[strlen(str
) + 1];
2417 if (res
->status
!= NFS3_OK
) {
2418 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
));
2419 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2420 free_nfs_cb_data(data
);
2424 memset(&args
, 0, sizeof(LOOKUP3args
));
2425 args
.what
.dir
= data
->fh
;
2426 args
.what
.name
= str
;
2428 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2429 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2430 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2431 free_nfs_cb_data(data
);
2437 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2439 char *str
= data
->continue_data
;
2442 str
= &str
[strlen(str
) + 1];
2444 memset(&args
, 0, sizeof(CREATE3args
));
2445 args
.where
.dir
= data
->fh
;
2446 args
.where
.name
= str
;
2447 args
.how
.mode
= UNCHECKED
;
2448 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2449 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2451 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2452 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2453 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2454 free_nfs_cb_data(data
);
2460 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2465 new_path
= strdup(path
);
2466 if (new_path
== NULL
) {
2467 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2471 ptr
= strrchr(new_path
, '/');
2473 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2479 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2480 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2481 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2494 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2497 struct nfs_cb_data
*data
= private_data
;
2498 struct nfs_context
*nfs
= data
->nfs
;
2499 char *str
= data
->continue_data
;
2501 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2503 str
= &str
[strlen(str
) + 1];
2505 if (status
== RPC_STATUS_ERROR
) {
2506 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2507 free_nfs_cb_data(data
);
2510 if (status
== RPC_STATUS_CANCEL
) {
2511 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2512 free_nfs_cb_data(data
);
2517 if (res
->status
!= NFS3_OK
) {
2518 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
));
2519 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2520 free_nfs_cb_data(data
);
2524 data
->cb(0, nfs
, NULL
, data
->private_data
);
2525 free_nfs_cb_data(data
);
2528 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2530 char *str
= data
->continue_data
;
2531 struct REMOVE3args args
;
2533 str
= &str
[strlen(str
) + 1];
2535 args
.object
.dir
= data
->fh
;
2536 args
.object
.name
= str
;
2537 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2538 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2539 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2540 free_nfs_cb_data(data
);
2546 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2551 new_path
= strdup(path
);
2552 if (new_path
== NULL
) {
2553 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2557 ptr
= strrchr(new_path
, '/');
2560 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2565 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2566 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2567 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2578 struct mknod_cb_data
{
2585 static void free_mknod_cb_data(void *ptr
)
2587 struct mknod_cb_data
*data
= ptr
;
2593 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2596 struct nfs_cb_data
*data
= private_data
;
2597 struct nfs_context
*nfs
= data
->nfs
;
2598 char *str
= data
->continue_data
;
2600 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2602 str
= &str
[strlen(str
) + 1];
2604 if (status
== RPC_STATUS_ERROR
) {
2605 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2606 free_nfs_cb_data(data
);
2609 if (status
== RPC_STATUS_CANCEL
) {
2610 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2611 free_nfs_cb_data(data
);
2616 if (res
->status
!= NFS3_OK
) {
2617 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
));
2618 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2619 free_nfs_cb_data(data
);
2623 data
->cb(0, nfs
, NULL
, data
->private_data
);
2624 free_nfs_cb_data(data
);
2627 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2629 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2630 char *str
= cb_data
->path
;
2633 str
= &str
[strlen(str
) + 1];
2635 args
.where
.dir
= data
->fh
;
2636 args
.where
.name
= str
;
2637 switch (cb_data
->mode
& S_IFMT
) {
2639 args
.what
.type
= NF3CHR
;
2640 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2641 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
);
2642 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2643 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2646 args
.what
.type
= NF3BLK
;
2647 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2648 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
);
2649 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2650 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2652 args
.what
.type
= NF3SOCK
;
2653 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2654 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
);
2657 args
.what
.type
= NF3FIFO
;
2658 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2659 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
);
2662 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2663 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2664 free_nfs_cb_data(data
);
2668 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2669 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2670 free_nfs_cb_data(data
);
2676 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2679 struct mknod_cb_data
*cb_data
;
2681 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2682 if (cb_data
== NULL
) {
2683 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2687 cb_data
->path
= strdup(path
);
2688 if (cb_data
->path
== NULL
) {
2689 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2694 ptr
= strrchr(cb_data
->path
, '/');
2696 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2697 free_mknod_cb_data(cb_data
);
2702 cb_data
->mode
= mode
;
2703 cb_data
->major
= major(dev
);
2704 cb_data
->minor
= minor(dev
);
2706 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2707 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2708 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2719 /* ReadDirPlus Emulation Callback data */
2720 struct rdpe_cb_data
{
2723 struct nfs_cb_data
*data
;
2726 /* ReadDirPlus Emulation LOOKUP Callback data */
2727 struct rdpe_lookup_cb_data
{
2728 struct rdpe_cb_data
*rdpe_cb_data
;
2729 struct nfsdirent
*nfsdirent
;
2732 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2733 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2735 LOOKUP3res
*res
= command_data
;
2736 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2737 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2738 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2739 struct nfsdir
*nfsdir
= data
->continue_data
;
2740 struct nfs_context
*nfs
= data
->nfs
;
2741 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2743 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2745 free(rdpe_lookup_cb_data
);
2747 rdpe_cb_data
->getattrcount
--;
2749 if (status
== RPC_STATUS_ERROR
) {
2750 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2751 "failed with RPC_STATUS_ERROR");
2752 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2754 if (status
== RPC_STATUS_CANCEL
) {
2755 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2756 "failed with RPC_STATUS_CANCEL");
2757 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2759 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2760 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2761 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2763 nfsdirent
->type
= attributes
->type
;
2764 nfsdirent
->mode
= attributes
->mode
;
2765 nfsdirent
->size
= attributes
->size
;
2767 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2768 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2769 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2770 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2771 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2772 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2773 nfsdirent
->uid
= attributes
->uid
;
2774 nfsdirent
->gid
= attributes
->gid
;
2778 if (rdpe_cb_data
->getattrcount
== 0) {
2779 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2780 rpc_set_error(nfs
->rpc
, "READDIRPLUS emulation "
2781 "failed: %s", rpc_get_error(rpc
));
2782 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
2783 data
->private_data
);
2784 nfs_free_nfsdir(nfsdir
);
2786 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2790 data
->continue_data
= NULL
;
2791 free_nfs_cb_data(data
);
2795 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2797 READDIR3res
*res
= command_data
;
2798 struct nfs_cb_data
*data
= private_data
;
2799 struct nfs_context
*nfs
= data
->nfs
;
2800 struct nfsdir
*nfsdir
= data
->continue_data
;
2801 struct nfsdirent
*nfsdirent
;
2802 struct entry3
*entry
;
2803 uint64_t cookie
= 0;
2804 struct rdpe_cb_data
*rdpe_cb_data
;
2806 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2808 if (status
== RPC_STATUS_ERROR
) {
2809 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2810 nfs_free_nfsdir(nfsdir
);
2811 data
->continue_data
= NULL
;
2812 free_nfs_cb_data(data
);
2816 if (status
== RPC_STATUS_CANCEL
) {
2817 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2818 nfs_free_nfsdir(nfsdir
);
2819 data
->continue_data
= NULL
;
2820 free_nfs_cb_data(data
);
2824 if (res
->status
!= NFS3_OK
) {
2825 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
));
2826 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2827 nfs_free_nfsdir(nfsdir
);
2828 data
->continue_data
= NULL
;
2829 free_nfs_cb_data(data
);
2833 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2834 while (entry
!= NULL
) {
2835 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2836 if (nfsdirent
== NULL
) {
2837 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2838 nfs_free_nfsdir(nfsdir
);
2839 data
->continue_data
= NULL
;
2840 free_nfs_cb_data(data
);
2843 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2844 nfsdirent
->name
= strdup(entry
->name
);
2845 if (nfsdirent
->name
== NULL
) {
2846 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2848 nfs_free_nfsdir(nfsdir
);
2849 data
->continue_data
= NULL
;
2850 free_nfs_cb_data(data
);
2853 nfsdirent
->inode
= entry
->fileid
;
2855 nfsdirent
->next
= nfsdir
->entries
;
2856 nfsdir
->entries
= nfsdirent
;
2858 cookie
= entry
->cookie
;
2859 entry
= entry
->nextentry
;
2862 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2865 args
.dir
= data
->fh
;
2866 args
.cookie
= cookie
;
2867 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2870 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2871 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2872 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2873 nfs_free_nfsdir(nfsdir
);
2874 data
->continue_data
= NULL
;
2875 free_nfs_cb_data(data
);
2881 /* steal the dirhandle */
2882 nfsdir
->current
= nfsdir
->entries
;
2884 if (nfsdir
->entries
) {
2885 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2886 rdpe_cb_data
->getattrcount
= 0;
2887 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2888 rdpe_cb_data
->data
= data
;
2889 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2890 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2893 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2894 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2895 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2897 memset(&args
, 0, sizeof(LOOKUP3args
));
2898 args
.what
.dir
= data
->fh
;
2899 args
.what
.name
= nfsdirent
->name
;
2901 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2902 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2904 /* if we have already commands in flight, we cant just stop, we have to wait for the
2905 * commands in flight to complete
2907 if (rdpe_cb_data
->getattrcount
> 0) {
2908 nfs_free_nfsdir(nfsdir
);
2909 data
->continue_data
= NULL
;
2910 free_nfs_cb_data(data
);
2911 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2912 free(rdpe_lookup_cb_data
);
2916 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2917 nfs_free_nfsdir(nfsdir
);
2918 data
->continue_data
= NULL
;
2919 free_nfs_cb_data(data
);
2920 free(rdpe_lookup_cb_data
);
2924 rdpe_cb_data
->getattrcount
++;
2929 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2931 READDIRPLUS3res
*res
= command_data
;
2932 struct nfs_cb_data
*data
= private_data
;
2933 struct nfs_context
*nfs
= data
->nfs
;
2934 struct nfsdir
*nfsdir
= data
->continue_data
;
2935 struct entryplus3
*entry
;
2936 uint64_t cookie
= 0;
2938 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2940 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2943 args
.dir
= data
->fh
;
2944 args
.cookie
= cookie
;
2945 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2948 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2949 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2950 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2951 nfs_free_nfsdir(nfsdir
);
2952 data
->continue_data
= NULL
;
2953 free_nfs_cb_data(data
);
2959 if (status
== RPC_STATUS_CANCEL
) {
2960 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2961 nfs_free_nfsdir(nfsdir
);
2962 data
->continue_data
= NULL
;
2963 free_nfs_cb_data(data
);
2967 if (res
->status
!= NFS3_OK
) {
2968 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
));
2969 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2970 nfs_free_nfsdir(nfsdir
);
2971 data
->continue_data
= NULL
;
2972 free_nfs_cb_data(data
);
2976 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2977 while (entry
!= NULL
) {
2978 struct nfsdirent
*nfsdirent
;
2980 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2981 if (nfsdirent
== NULL
) {
2982 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2983 nfs_free_nfsdir(nfsdir
);
2984 data
->continue_data
= NULL
;
2985 free_nfs_cb_data(data
);
2988 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2989 nfsdirent
->name
= strdup(entry
->name
);
2990 if (nfsdirent
->name
== NULL
) {
2991 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2993 nfs_free_nfsdir(nfsdir
);
2994 data
->continue_data
= NULL
;
2995 free_nfs_cb_data(data
);
2998 nfsdirent
->inode
= entry
->fileid
;
2999 if (entry
->name_attributes
.attributes_follow
) {
3000 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
3001 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
3002 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
3004 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
3005 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
3006 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
3007 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
3008 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
3009 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
3010 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
3011 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
3014 nfsdirent
->next
= nfsdir
->entries
;
3015 nfsdir
->entries
= nfsdirent
;
3017 cookie
= entry
->cookie
;
3018 entry
= entry
->nextentry
;
3021 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
3022 READDIRPLUS3args args
;
3024 args
.dir
= data
->fh
;
3025 args
.cookie
= cookie
;
3026 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
3027 args
.dircount
= 8192;
3028 args
.maxcount
= 8192;
3030 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
3031 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
3032 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3033 nfs_free_nfsdir(nfsdir
);
3034 data
->continue_data
= NULL
;
3035 free_nfs_cb_data(data
);
3041 /* steal the dirhandle */
3042 data
->continue_data
= NULL
;
3043 nfsdir
->current
= nfsdir
->entries
;
3045 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
3046 free_nfs_cb_data(data
);
3049 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3051 READDIRPLUS3args args
;
3053 args
.dir
= data
->fh
;
3055 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
3056 args
.dircount
= 8192;
3057 args
.maxcount
= 8192;
3058 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
3059 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
3060 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3061 free_nfs_cb_data(data
);
3067 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3069 struct nfsdir
*nfsdir
;
3071 nfsdir
= malloc(sizeof(struct nfsdir
));
3072 if (nfsdir
== NULL
) {
3073 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
3076 memset(nfsdir
, 0, sizeof(struct nfsdir
));
3078 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
3079 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3087 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3089 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
3091 if (nfsdir
->current
!= NULL
) {
3092 nfsdir
->current
= nfsdir
->current
->next
;
3101 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3103 nfs_free_nfsdir(nfsdir
);
3110 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3121 struct lseek_cb_data
{
3122 struct nfs_context
*nfs
;
3123 struct nfsfh
*nfsfh
;
3129 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3132 struct lseek_cb_data
*data
= private_data
;
3133 struct nfs_context
*nfs
= data
->nfs
;
3136 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3138 if (status
== RPC_STATUS_ERROR
) {
3139 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3143 if (status
== RPC_STATUS_CANCEL
) {
3144 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3150 if (res
->status
!= NFS3_OK
) {
3151 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3152 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3157 size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3159 if (data
->offset
< 0 &&
3160 (uint64_t)(-data
->offset
) > size
) {
3161 data
->cb(-EINVAL
, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3163 data
->nfsfh
->offset
= data
->offset
+ size
;
3164 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3170 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3172 struct lseek_cb_data
*data
;
3173 struct GETATTR3args args
;
3175 if (whence
== SEEK_SET
) {
3177 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3179 nfsfh
->offset
= offset
;
3180 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3184 if (whence
== SEEK_CUR
) {
3186 nfsfh
->offset
< (uint64_t)(-offset
)) {
3187 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3189 nfsfh
->offset
+= offset
;
3190 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3195 data
= malloc(sizeof(struct lseek_cb_data
));
3197 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3202 data
->nfsfh
= nfsfh
;
3203 data
->offset
= offset
;
3205 data
->private_data
= private_data
;
3207 memset(&args
, 0, sizeof(GETATTR3args
));
3208 args
.object
= nfsfh
->fh
;
3210 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3211 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3224 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3227 struct nfs_cb_data
*data
= private_data
;
3228 struct nfs_context
*nfs
= data
->nfs
;
3229 struct statvfs svfs
;
3231 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3233 if (status
== RPC_STATUS_ERROR
) {
3234 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3235 free_nfs_cb_data(data
);
3238 if (status
== RPC_STATUS_CANCEL
) {
3239 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3240 free_nfs_cb_data(data
);
3245 if (res
->status
!= NFS3_OK
) {
3246 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
));
3247 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3248 free_nfs_cb_data(data
);
3252 svfs
.f_bsize
= 4096;
3253 svfs
.f_frsize
= 4096;
3254 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3255 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3256 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3257 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3258 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3259 #if !defined(ANDROID)
3260 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3263 svfs
.f_namemax
= 256;
3266 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3267 free_nfs_cb_data(data
);
3270 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3274 args
.fsroot
= data
->fh
;
3275 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3276 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3277 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3278 free_nfs_cb_data(data
);
3284 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3286 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3287 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3300 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3303 struct nfs_cb_data
*data
= private_data
;
3304 struct nfs_context
*nfs
= data
->nfs
;
3306 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3308 if (status
== RPC_STATUS_ERROR
) {
3309 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3310 free_nfs_cb_data(data
);
3313 if (status
== RPC_STATUS_CANCEL
) {
3314 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3315 free_nfs_cb_data(data
);
3320 if (res
->status
!= NFS3_OK
) {
3321 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
));
3322 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3323 free_nfs_cb_data(data
);
3328 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3329 free_nfs_cb_data(data
);
3332 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3336 args
.symlink
= data
->fh
;
3338 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3339 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3340 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3341 free_nfs_cb_data(data
);
3347 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3349 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3350 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3363 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3365 struct nfs_cb_data
*data
= private_data
;
3366 struct nfs_context
*nfs
= data
->nfs
;
3369 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3371 if (status
== RPC_STATUS_ERROR
) {
3372 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3373 free_nfs_cb_data(data
);
3376 if (status
== RPC_STATUS_CANCEL
) {
3377 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3378 free_nfs_cb_data(data
);
3383 if (res
->status
!= NFS3_OK
) {
3384 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3385 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3386 free_nfs_cb_data(data
);
3390 data
->cb(0, nfs
, NULL
, data
->private_data
);
3391 free_nfs_cb_data(data
);
3394 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3398 memset(&args
, 0, sizeof(SETATTR3args
));
3399 args
.object
= data
->fh
;
3400 args
.new_attributes
.mode
.set_it
= 1;
3401 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3403 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3404 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3405 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3406 free_nfs_cb_data(data
);
3413 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3415 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3416 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3426 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3428 struct nfs_cb_data
*data
;
3430 data
= malloc(sizeof(struct nfs_cb_data
));
3432 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3435 memset(data
, 0, sizeof(struct nfs_cb_data
));
3438 data
->private_data
= private_data
;
3439 data
->continue_int
= mode
;
3440 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3441 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3442 if (data
->fh
.data
.data_val
== NULL
) {
3443 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3444 free_nfs_cb_data(data
);
3447 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3449 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3461 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3463 struct nfs_cb_data
*data
= private_data
;
3464 struct nfs_context
*nfs
= data
->nfs
;
3467 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3469 if (status
== RPC_STATUS_ERROR
) {
3470 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3471 free_nfs_cb_data(data
);
3474 if (status
== RPC_STATUS_CANCEL
) {
3475 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3476 free_nfs_cb_data(data
);
3481 if (res
->status
!= NFS3_OK
) {
3482 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3483 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3484 free_nfs_cb_data(data
);
3488 data
->cb(0, nfs
, NULL
, data
->private_data
);
3489 free_nfs_cb_data(data
);
3492 struct nfs_chown_data
{
3497 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3500 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3502 memset(&args
, 0, sizeof(SETATTR3args
));
3503 args
.object
= data
->fh
;
3504 if (chown_data
->uid
!= (uid_t
)-1) {
3505 args
.new_attributes
.uid
.set_it
= 1;
3506 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3508 if (chown_data
->gid
!= (gid_t
)-1) {
3509 args
.new_attributes
.gid
.set_it
= 1;
3510 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3513 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3514 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3515 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3516 free_nfs_cb_data(data
);
3523 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3525 struct nfs_chown_data
*chown_data
;
3527 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3528 if (chown_data
== NULL
) {
3529 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3533 chown_data
->uid
= uid
;
3534 chown_data
->gid
= gid
;
3536 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3537 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3548 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3550 struct nfs_cb_data
*data
;
3551 struct nfs_chown_data
*chown_data
;
3553 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3554 if (chown_data
== NULL
) {
3555 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3559 chown_data
->uid
= uid
;
3560 chown_data
->gid
= gid
;
3562 data
= malloc(sizeof(struct nfs_cb_data
));
3564 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3568 memset(data
, 0, sizeof(struct nfs_cb_data
));
3571 data
->private_data
= private_data
;
3572 data
->continue_data
= chown_data
;
3573 data
->free_continue_data
= free
;
3574 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3575 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3576 if (data
->fh
.data
.data_val
== NULL
) {
3577 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3578 free_nfs_cb_data(data
);
3581 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3583 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3597 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3599 struct nfs_cb_data
*data
= private_data
;
3600 struct nfs_context
*nfs
= data
->nfs
;
3603 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3605 if (status
== RPC_STATUS_ERROR
) {
3606 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3607 free_nfs_cb_data(data
);
3610 if (status
== RPC_STATUS_CANCEL
) {
3611 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3612 free_nfs_cb_data(data
);
3617 if (res
->status
!= NFS3_OK
) {
3618 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3619 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3620 free_nfs_cb_data(data
);
3624 data
->cb(0, nfs
, NULL
, data
->private_data
);
3625 free_nfs_cb_data(data
);
3628 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3631 struct timeval
*utimes_data
= data
->continue_data
;
3633 memset(&args
, 0, sizeof(SETATTR3args
));
3634 args
.object
= data
->fh
;
3635 if (utimes_data
!= NULL
) {
3636 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3637 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3638 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3639 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3640 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3641 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3643 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3644 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3647 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3648 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3649 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3650 free_nfs_cb_data(data
);
3657 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3659 struct timeval
*new_times
= NULL
;
3661 if (times
!= NULL
) {
3662 new_times
= malloc(sizeof(struct timeval
)*2);
3663 if (new_times
== NULL
) {
3664 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3668 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3671 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3672 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3682 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3684 struct timeval
*new_times
= NULL
;
3686 if (times
!= NULL
) {
3687 new_times
= malloc(sizeof(struct timeval
)*2);
3688 if (new_times
== NULL
) {
3689 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3693 new_times
[0].tv_sec
= times
->actime
;
3694 new_times
[0].tv_usec
= 0;
3695 new_times
[1].tv_sec
= times
->modtime
;
3696 new_times
[1].tv_usec
= 0;
3699 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3700 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3711 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3714 struct nfs_cb_data
*data
= private_data
;
3715 struct nfs_context
*nfs
= data
->nfs
;
3716 unsigned int nfsmode
= 0;
3718 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3720 if (status
== RPC_STATUS_ERROR
) {
3721 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3722 free_nfs_cb_data(data
);
3725 if (status
== RPC_STATUS_CANCEL
) {
3726 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3727 free_nfs_cb_data(data
);
3732 if (res
->status
!= NFS3_OK
) {
3733 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
));
3734 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3735 free_nfs_cb_data(data
);
3739 if (data
->continue_int
& R_OK
) {
3740 nfsmode
|= ACCESS3_READ
;
3742 if (data
->continue_int
& W_OK
) {
3743 nfsmode
|= ACCESS3_MODIFY
;
3745 if (data
->continue_int
& X_OK
) {
3746 nfsmode
|= ACCESS3_EXECUTE
;
3749 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3750 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3751 nfsmode
&ACCESS3_READ
?'r':'-',
3752 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3753 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3754 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3755 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3756 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3757 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3758 free_nfs_cb_data(data
);
3762 data
->cb(0, nfs
, NULL
, data
->private_data
);
3763 free_nfs_cb_data(data
);
3766 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3771 if (data
->continue_int
& R_OK
) {
3772 nfsmode
|= ACCESS3_READ
;
3774 if (data
->continue_int
& W_OK
) {
3775 nfsmode
|= ACCESS3_MODIFY
;
3777 if (data
->continue_int
& X_OK
) {
3778 nfsmode
|= ACCESS3_EXECUTE
;
3781 memset(&args
, 0, sizeof(ACCESS3args
));
3782 args
.object
= data
->fh
;
3783 args
.access
= nfsmode
;
3785 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3786 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3787 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3788 free_nfs_cb_data(data
);
3794 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3796 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3797 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3809 struct nfs_symlink_data
{
3811 char *newpathparent
;
3812 char *newpathobject
;
3815 static void free_nfs_symlink_data(void *mem
)
3817 struct nfs_symlink_data
*data
= mem
;
3819 if (data
->oldpath
!= NULL
) {
3820 free(data
->oldpath
);
3822 if (data
->newpathparent
!= NULL
) {
3823 free(data
->newpathparent
);
3825 if (data
->newpathobject
!= NULL
) {
3826 free(data
->newpathobject
);
3831 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3834 struct nfs_cb_data
*data
= private_data
;
3835 struct nfs_context
*nfs
= data
->nfs
;
3836 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3838 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3840 if (status
== RPC_STATUS_ERROR
) {
3841 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3842 free_nfs_cb_data(data
);
3845 if (status
== RPC_STATUS_CANCEL
) {
3846 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3847 free_nfs_cb_data(data
);
3852 if (res
->status
!= NFS3_OK
) {
3853 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
));
3854 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3855 free_nfs_cb_data(data
);
3859 data
->cb(0, nfs
, NULL
, data
->private_data
);
3860 free_nfs_cb_data(data
);
3863 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3865 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3868 memset(&args
, 0, sizeof(SYMLINK3args
));
3869 args
.where
.dir
= data
->fh
;
3870 args
.where
.name
= symlink_data
->newpathobject
;
3871 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3872 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
;
3873 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3875 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3876 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3877 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3878 free_nfs_cb_data(data
);
3884 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3887 struct nfs_symlink_data
*symlink_data
;
3889 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3890 if (symlink_data
== NULL
) {
3891 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3894 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3896 symlink_data
->oldpath
= strdup(oldpath
);
3897 if (symlink_data
->oldpath
== NULL
) {
3898 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3899 free_nfs_symlink_data(symlink_data
);
3903 symlink_data
->newpathparent
= strdup(newpath
);
3904 if (symlink_data
->newpathparent
== NULL
) {
3905 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3906 free_nfs_symlink_data(symlink_data
);
3910 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3912 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3913 free_nfs_symlink_data(symlink_data
);
3919 symlink_data
->newpathobject
= strdup(ptr
);
3920 if (symlink_data
->newpathobject
== NULL
) {
3921 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3922 free_nfs_symlink_data(symlink_data
);
3926 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3927 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3939 struct nfs_rename_data
{
3942 struct nfs_fh3 olddir
;
3945 struct nfs_fh3 newdir
;
3948 static void free_nfs_rename_data(void *mem
)
3950 struct nfs_rename_data
*data
= mem
;
3952 if (data
->oldpath
!= NULL
) {
3953 free(data
->oldpath
);
3955 if (data
->olddir
.data
.data_val
!= NULL
) {
3956 free(data
->olddir
.data
.data_val
);
3958 if (data
->newpath
!= NULL
) {
3959 free(data
->newpath
);
3961 if (data
->newdir
.data
.data_val
!= NULL
) {
3962 free(data
->newdir
.data
.data_val
);
3967 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3970 struct nfs_cb_data
*data
= private_data
;
3971 struct nfs_context
*nfs
= data
->nfs
;
3972 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3974 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3976 if (status
== RPC_STATUS_ERROR
) {
3977 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3978 free_nfs_cb_data(data
);
3981 if (status
== RPC_STATUS_CANCEL
) {
3982 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3983 free_nfs_cb_data(data
);
3988 if (res
->status
!= NFS3_OK
) {
3989 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
));
3990 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3991 free_nfs_cb_data(data
);
3995 data
->cb(0, nfs
, NULL
, data
->private_data
);
3996 free_nfs_cb_data(data
);
3999 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4001 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4004 /* steal the filehandle */
4005 rename_data
->newdir
= data
->fh
;
4006 data
->fh
.data
.data_val
= NULL
;
4008 args
.from
.dir
= rename_data
->olddir
;
4009 args
.from
.name
= rename_data
->oldobject
;
4010 args
.to
.dir
= rename_data
->newdir
;
4011 args
.to
.name
= rename_data
->newobject
;
4012 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
4013 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
4014 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4015 free_nfs_cb_data(data
);
4022 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4024 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4025 char* newpath
= strdup(rename_data
->newpath
);
4027 rpc_set_error(nfs
->rpc
, "Out of memory. Could not allocate memory to store target path for rename");
4028 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4029 free_nfs_cb_data(data
);
4033 /* steal the filehandle */
4034 rename_data
->olddir
= data
->fh
;
4035 data
->fh
.data
.data_val
= NULL
;
4037 if (nfs_lookuppath_async(nfs
, rename_data
->newpath
, data
->cb
, data
->private_data
, nfs_rename_continue_2_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
4038 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", newpath
);
4039 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4040 free_nfs_cb_data(data
);
4044 data
->continue_data
= NULL
;
4045 free_nfs_cb_data(data
);
4052 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4055 struct nfs_rename_data
*rename_data
;
4057 rename_data
= malloc(sizeof(struct nfs_rename_data
));
4058 if (rename_data
== NULL
) {
4059 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
4062 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
4064 rename_data
->oldpath
= strdup(oldpath
);
4065 if (rename_data
->oldpath
== NULL
) {
4066 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4067 free_nfs_rename_data(rename_data
);
4070 ptr
= strrchr(rename_data
->oldpath
, '/');
4072 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
4073 free_nfs_rename_data(rename_data
);
4078 rename_data
->oldobject
= ptr
;
4081 rename_data
->newpath
= strdup(newpath
);
4082 if (rename_data
->newpath
== NULL
) {
4083 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4084 free_nfs_rename_data(rename_data
);
4087 ptr
= strrchr(rename_data
->newpath
, '/');
4089 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4090 free_nfs_rename_data(rename_data
);
4095 rename_data
->newobject
= ptr
;
4098 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
4099 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4110 struct nfs_link_data
{
4112 struct nfs_fh3 oldfh
;
4115 struct nfs_fh3 newdir
;
4118 static void free_nfs_link_data(void *mem
)
4120 struct nfs_link_data
*data
= mem
;
4122 if (data
->oldpath
!= NULL
) {
4123 free(data
->oldpath
);
4125 if (data
->oldfh
.data
.data_val
!= NULL
) {
4126 free(data
->oldfh
.data
.data_val
);
4128 if (data
->newpath
!= NULL
) {
4129 free(data
->newpath
);
4131 if (data
->newdir
.data
.data_val
!= NULL
) {
4132 free(data
->newdir
.data
.data_val
);
4137 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4140 struct nfs_cb_data
*data
= private_data
;
4141 struct nfs_context
*nfs
= data
->nfs
;
4142 struct nfs_link_data
*link_data
= data
->continue_data
;
4144 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4146 if (status
== RPC_STATUS_ERROR
) {
4147 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4148 free_nfs_cb_data(data
);
4151 if (status
== RPC_STATUS_CANCEL
) {
4152 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4153 free_nfs_cb_data(data
);
4158 if (res
->status
!= NFS3_OK
) {
4159 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
));
4160 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4161 free_nfs_cb_data(data
);
4165 data
->cb(0, nfs
, NULL
, data
->private_data
);
4166 free_nfs_cb_data(data
);
4169 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4171 struct nfs_link_data
*link_data
= data
->continue_data
;
4174 /* steal the filehandle */
4175 link_data
->newdir
= data
->fh
;
4176 data
->fh
.data
.data_val
= NULL
;
4178 memset(&args
, 0, sizeof(LINK3args
));
4179 args
.file
= link_data
->oldfh
;
4180 args
.link
.dir
= link_data
->newdir
;
4181 args
.link
.name
= link_data
->newobject
;
4182 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4183 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4184 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4185 free_nfs_cb_data(data
);
4192 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4194 struct nfs_link_data
*link_data
= data
->continue_data
;
4196 /* steal the filehandle */
4197 link_data
->oldfh
= data
->fh
;
4198 data
->fh
.data
.data_val
= NULL
;
4200 if (nfs_lookuppath_async(nfs
, link_data
->newpath
, data
->cb
, data
->private_data
, nfs_link_continue_2_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4201 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4202 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4203 free_nfs_cb_data(data
);
4206 data
->continue_data
= NULL
;
4207 free_nfs_cb_data(data
);
4213 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4216 struct nfs_link_data
*link_data
;
4218 link_data
= malloc(sizeof(struct nfs_link_data
));
4219 if (link_data
== NULL
) {
4220 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4223 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4225 link_data
->oldpath
= strdup(oldpath
);
4226 if (link_data
->oldpath
== NULL
) {
4227 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4228 free_nfs_link_data(link_data
);
4232 link_data
->newpath
= strdup(newpath
);
4233 if (link_data
->newpath
== NULL
) {
4234 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4235 free_nfs_link_data(link_data
);
4238 ptr
= strrchr(link_data
->newpath
, '/');
4240 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4241 free_nfs_link_data(link_data
);
4246 link_data
->newobject
= ptr
;
4249 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4250 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4258 //qqq replace later with lseek()
4259 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4261 return nfsfh
->offset
;
4267 * Get the maximum supported READ3 size by the server
4269 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4271 return nfs
->readmax
;
4275 * Get the maximum supported WRITE3 size by the server
4277 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4279 return nfs
->writemax
;
4282 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4283 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4286 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4287 rpc_set_uid(nfs
->rpc
, uid
);
4290 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4291 rpc_set_gid(nfs
->rpc
, gid
);
4294 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4299 va_start(ap
, error_string
);
4301 vsnprintf(str
, 1024, error_string
, ap
);
4302 if (nfs
->rpc
->error_string
!= NULL
) {
4303 free(nfs
->rpc
->error_string
);
4305 nfs
->rpc
->error_string
= str
;
4311 struct mount_cb_data
{
4317 static void free_mount_cb_data(struct mount_cb_data
*data
)
4319 if (data
->server
!= NULL
) {
4321 data
->server
= NULL
;
4327 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4329 struct mount_cb_data
*data
= private_data
;
4331 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4333 if (status
== RPC_STATUS_ERROR
) {
4334 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4335 free_mount_cb_data(data
);
4338 if (status
== RPC_STATUS_CANCEL
) {
4339 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4340 free_mount_cb_data(data
);
4344 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4345 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4346 rpc_set_error(rpc
, "Failed to disconnect\n");
4348 free_mount_cb_data(data
);
4351 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4353 struct mount_cb_data
*data
= private_data
;
4355 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4357 /* Dont want any more callbacks even if the socket is closed */
4358 rpc
->connect_cb
= NULL
;
4360 if (status
== RPC_STATUS_ERROR
) {
4361 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4362 free_mount_cb_data(data
);
4365 if (status
== RPC_STATUS_CANCEL
) {
4366 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4367 free_mount_cb_data(data
);
4371 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4372 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4373 free_mount_cb_data(data
);
4378 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4380 struct mount_cb_data
*data
;
4382 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4384 data
= malloc(sizeof(struct mount_cb_data
));
4388 memset(data
, 0, sizeof(struct mount_cb_data
));
4390 data
->private_data
= private_data
;
4391 data
->server
= strdup(server
);
4392 if (data
->server
== NULL
) {
4393 free_mount_cb_data(data
);
4396 if (rpc_connect_program_async(rpc
, data
->server
, MOUNT_PROGRAM
, MOUNT_V3
, mount_export_4_cb
, data
) != 0) {
4397 rpc_set_error(rpc
, "Failed to start connection");
4398 free_mount_cb_data(data
);
4405 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4407 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4411 const char *nfs_get_server(struct nfs_context
*nfs
) {
4415 const char *nfs_get_export(struct nfs_context
*nfs
) {
4419 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4420 return &nfs
->rootfh
;
4423 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {