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"
74 #include "libnfs-raw.h"
75 #include "libnfs-raw-mount.h"
76 #include "libnfs-raw-nfs.h"
77 #include "libnfs-raw-portmap.h"
78 #include "libnfs-private.h"
80 #define MAX_DIR_CACHE 128
87 struct nfsdirent
*entries
;
88 struct nfsdirent
*current
;
91 struct nfs_readahead
{
106 struct nfs_readahead ra
;
110 struct rpc_context
*rpc
;
113 struct nfs_fh3 rootfh
;
117 struct nfsdir
*dircache
;
120 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
122 while (nfsdir
->entries
) {
123 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
124 if (nfsdir
->entries
->name
!= NULL
) {
125 free(nfsdir
->entries
->name
);
127 free(nfsdir
->entries
);
128 nfsdir
->entries
= dirent
;
130 free(nfsdir
->fh
.data
.data_val
);
134 static void nfs_dircache_add(struct nfs_context
*nfs
, struct nfsdir
*nfsdir
)
137 LIBNFS_LIST_ADD(&nfs
->dircache
, nfsdir
);
139 for (nfsdir
= nfs
->dircache
, i
= 0; nfsdir
; nfsdir
= nfsdir
->next
, i
++) {
140 if (i
> MAX_DIR_CACHE
) {
141 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
142 nfs_free_nfsdir(nfsdir
);
148 static struct nfsdir
*nfs_dircache_find(struct nfs_context
*nfs
, struct nfs_fh3
*fh
)
150 struct nfsdir
*nfsdir
;
152 for (nfsdir
= nfs
->dircache
; nfsdir
; nfsdir
= nfsdir
->next
) {
153 if (nfsdir
->fh
.data
.data_len
== fh
->data
.data_len
&&
154 !memcmp(nfsdir
->fh
.data
.data_val
, fh
->data
.data_val
, fh
->data
.data_len
)) {
155 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
164 typedef int (*continue_func
)(struct nfs_context
*nfs
, fattr3
*attr
,
165 struct nfs_cb_data
*data
);
168 struct nfs_context
*nfs
;
170 char *saved_path
, *path
;
175 continue_func continue_cb
;
177 void (*free_continue_data
)(void *);
182 /* for multi-read/write calls. */
187 uint64_t offset
, count
, max_offset
, org_offset
, org_count
;
192 struct nfs_mcb_data
{
193 struct nfs_cb_data
*data
;
199 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, fattr3
*attr
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
201 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
203 rpc_set_auth(nfs
->rpc
, auth
);
206 int nfs_get_fd(struct nfs_context
*nfs
)
208 return rpc_get_fd(nfs
->rpc
);
211 int nfs_queue_length(struct nfs_context
*nfs
)
213 return rpc_queue_length(nfs
->rpc
);
216 int nfs_which_events(struct nfs_context
*nfs
)
218 return rpc_which_events(nfs
->rpc
);
221 int nfs_service(struct nfs_context
*nfs
, int revents
)
223 return rpc_service(nfs
->rpc
, revents
);
226 char *nfs_get_error(struct nfs_context
*nfs
)
228 return rpc_get_error(nfs
->rpc
);
231 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
233 if (!strcmp(arg
, "tcp-syncnt")) {
234 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
235 } else if (!strcmp(arg
, "uid")) {
236 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
237 } else if (!strcmp(arg
, "gid")) {
238 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
239 } else if (!strcmp(arg
, "readahaed")) {
240 rpc_set_readahead(nfs_get_rpc_context(nfs
), atoi(val
));
245 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
247 struct nfs_url
*urls
;
248 char *strp
, *flagsp
, *strp2
;
250 if (strncmp(url
, "nfs://", 6)) {
251 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
255 urls
= malloc(sizeof(struct nfs_url
));
257 rpc_set_error(nfs
->rpc
, "Out of memory");
261 memset(urls
, 0x00, sizeof(struct nfs_url
));
262 urls
->server
= strdup(url
+ 6);
263 if (urls
->server
== NULL
) {
264 nfs_destroy_url(urls
);
265 rpc_set_error(nfs
->rpc
, "Out of memory");
269 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
270 urls
->server
[0] == '?') {
272 flagsp
= strchr(urls
->server
, '?');
275 nfs_destroy_url(urls
);
276 rpc_set_error(nfs
->rpc
, "Invalid server string");
280 strp
= strchr(urls
->server
, '/');
283 flagsp
= strchr(urls
->server
, '?');
286 nfs_destroy_url(urls
);
287 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
291 urls
->path
= strdup(strp
);
292 if (urls
->path
== NULL
) {
293 nfs_destroy_url(urls
);
294 rpc_set_error(nfs
->rpc
, "Out of memory");
300 flagsp
= strchr(urls
->path
, '?');
304 strp
= strrchr(urls
->path
, '/');
307 flagsp
= strchr(urls
->path
, '?');
310 nfs_destroy_url(urls
);
311 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
314 urls
->file
= strdup(strp
);
315 if (urls
->path
== NULL
) {
316 nfs_destroy_url(urls
);
317 rpc_set_error(nfs
->rpc
, "Out of memory");
321 flagsp
= strchr(urls
->file
, '?');
328 if (urls
->file
&& !strlen(urls
->file
)) {
332 nfs_destroy_url(urls
);
333 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
338 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
340 flagsp
= strchr(strp
, '&');
344 strp2
= strchr(strp
, '=');
348 nfs_set_context_args(nfs
, strp
, strp2
);
352 if (urls
->server
&& strlen(urls
->server
) <= 1) {
360 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
362 return nfs_parse_url(nfs
, url
, 0, 0);
365 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
367 return nfs_parse_url(nfs
, url
, 1, 0);
370 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
372 return nfs_parse_url(nfs
, url
, 0, 1);
376 void nfs_destroy_url(struct nfs_url
*url
)
386 struct nfs_context
*nfs_init_context(void)
388 struct nfs_context
*nfs
;
390 nfs
= malloc(sizeof(struct nfs_context
));
394 memset(nfs
, 0, sizeof(struct nfs_context
));
396 nfs
->rpc
= rpc_init_context();
397 if (nfs
->rpc
== NULL
) {
402 nfs
->cwd
= strdup("/");
407 void nfs_destroy_context(struct nfs_context
*nfs
)
409 rpc_destroy_context(nfs
->rpc
);
427 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
428 free(nfs
->rootfh
.data
.data_val
);
429 nfs
->rootfh
.data
.data_val
= NULL
;
432 while (nfs
->dircache
) {
433 struct nfsdir
*nfsdir
= nfs
->dircache
;
434 LIBNFS_LIST_REMOVE(&nfs
->dircache
, nfsdir
);
435 nfs_free_nfsdir(nfsdir
);
450 void free_rpc_cb_data(struct rpc_cb_data
*data
)
457 static void rpc_connect_program_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
459 struct rpc_cb_data
*data
= private_data
;
461 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
463 /* Dont want any more callbacks even if the socket is closed */
464 rpc
->connect_cb
= NULL
;
466 if (status
== RPC_STATUS_ERROR
) {
467 data
->cb(rpc
, status
, command_data
, data
->private_data
);
468 free_rpc_cb_data(data
);
471 if (status
== RPC_STATUS_CANCEL
) {
472 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
473 free_rpc_cb_data(data
);
477 data
->cb(rpc
, status
, NULL
, data
->private_data
);
478 free_rpc_cb_data(data
);
481 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
483 struct rpc_cb_data
*data
= private_data
;
485 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
487 /* Dont want any more callbacks even if the socket is closed */
488 rpc
->connect_cb
= NULL
;
490 if (status
== RPC_STATUS_ERROR
) {
491 data
->cb(rpc
, status
, command_data
, data
->private_data
);
492 free_rpc_cb_data(data
);
495 if (status
== RPC_STATUS_CANCEL
) {
496 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
497 free_rpc_cb_data(data
);
501 switch (data
->program
) {
503 if (rpc_mount3_null_async(rpc
, rpc_connect_program_5_cb
,
505 data
->cb(rpc
, status
, command_data
, data
->private_data
);
506 free_rpc_cb_data(data
);
511 if (rpc_nfs3_null_async(rpc
, rpc_connect_program_5_cb
,
513 data
->cb(rpc
, status
, command_data
, data
->private_data
);
514 free_rpc_cb_data(data
);
520 data
->cb(rpc
, status
, NULL
, data
->private_data
);
521 free_rpc_cb_data(data
);
524 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
526 struct rpc_cb_data
*data
= private_data
;
527 struct pmap3_string_result
*gar
;
528 uint32_t rpc_port
= 0;
531 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
533 if (status
== RPC_STATUS_ERROR
) {
534 data
->cb(rpc
, status
, command_data
, data
->private_data
);
535 free_rpc_cb_data(data
);
538 if (status
== RPC_STATUS_CANCEL
) {
539 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
540 free_rpc_cb_data(data
);
544 switch (rpc
->s
.ss_family
) {
546 rpc_port
= *(uint32_t *)command_data
;
549 /* ouch. portmapper and ipv6 are not great */
551 if (gar
->addr
== NULL
) {
554 ptr
= strrchr(gar
->addr
, '.');
558 rpc_port
= atoi(ptr
+ 1);
560 ptr
= strrchr(gar
->addr
, '.');
564 rpc_port
+= 256 * atoi(ptr
+ 1);
568 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
569 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
570 free_rpc_cb_data(data
);
574 rpc_disconnect(rpc
, "normal disconnect");
575 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
576 data
->cb(rpc
, status
, command_data
, data
->private_data
);
577 free_rpc_cb_data(data
);
582 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
584 struct rpc_cb_data
*data
= private_data
;
585 struct pmap3_mapping map
;
587 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
589 if (status
== RPC_STATUS_ERROR
) {
590 data
->cb(rpc
, status
, command_data
, data
->private_data
);
591 free_rpc_cb_data(data
);
594 if (status
== RPC_STATUS_CANCEL
) {
595 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
596 free_rpc_cb_data(data
);
600 switch (rpc
->s
.ss_family
) {
602 if (rpc_pmap2_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
603 data
->cb(rpc
, status
, command_data
, data
->private_data
);
604 free_rpc_cb_data(data
);
609 map
.prog
=data
->program
;
610 map
.vers
=data
->version
;
614 if (rpc_pmap3_getaddr_async(rpc
, &map
, rpc_connect_program_3_cb
, private_data
) != 0) {
615 data
->cb(rpc
, status
, command_data
, data
->private_data
);
616 free_rpc_cb_data(data
);
623 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
625 struct rpc_cb_data
*data
= private_data
;
627 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
629 /* Dont want any more callbacks even if the socket is closed */
630 rpc
->connect_cb
= NULL
;
632 if (status
== RPC_STATUS_ERROR
) {
633 data
->cb(rpc
, status
, command_data
, data
->private_data
);
634 free_rpc_cb_data(data
);
637 if (status
== RPC_STATUS_CANCEL
) {
638 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
639 free_rpc_cb_data(data
);
643 switch (rpc
->s
.ss_family
) {
645 if (rpc_pmap2_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
646 data
->cb(rpc
, status
, command_data
, data
->private_data
);
647 free_rpc_cb_data(data
);
652 if (rpc_pmap3_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
653 data
->cb(rpc
, status
, command_data
, data
->private_data
);
654 free_rpc_cb_data(data
);
661 int rpc_connect_program_async(struct rpc_context
*rpc
, const char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
663 struct rpc_cb_data
*data
;
665 data
= malloc(sizeof(struct rpc_cb_data
));
669 memset(data
, 0, sizeof(struct rpc_cb_data
));
670 data
->server
= strdup(server
);
671 data
->program
= program
;
672 data
->version
= version
;
675 data
->private_data
= private_data
;
677 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
678 rpc_set_error(rpc
, "Failed to start connection");
679 free_rpc_cb_data(data
);
685 static void free_nfs_cb_data(struct nfs_cb_data
*data
)
687 if (data
->continue_data
!= NULL
) {
688 assert(data
->free_continue_data
);
689 data
->free_continue_data(data
->continue_data
);
692 free(data
->saved_path
);
693 free(data
->fh
.data
.data_val
);
700 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
702 struct nfs_cb_data
*data
= private_data
;
703 struct nfs_context
*nfs
= data
->nfs
;
705 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
707 if (status
== RPC_STATUS_ERROR
) {
708 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
709 free_nfs_cb_data(data
);
712 if (status
== RPC_STATUS_CANCEL
) {
713 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
714 free_nfs_cb_data(data
);
718 data
->cb(0, nfs
, NULL
, data
->private_data
);
719 free_nfs_cb_data(data
);
722 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
724 struct nfs_cb_data
*data
= private_data
;
725 struct nfs_context
*nfs
= data
->nfs
;
726 FSINFO3res
*res
= command_data
;
727 struct GETATTR3args args
;
729 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
731 if (status
== RPC_STATUS_ERROR
) {
732 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
733 free_nfs_cb_data(data
);
736 if (status
== RPC_STATUS_CANCEL
) {
737 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
738 free_nfs_cb_data(data
);
742 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
743 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
745 memset(&args
, 0, sizeof(GETATTR3args
));
746 args
.object
= nfs
->rootfh
;
748 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
749 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
750 free_nfs_cb_data(data
);
755 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
757 struct nfs_cb_data
*data
= private_data
;
758 struct nfs_context
*nfs
= data
->nfs
;
759 struct FSINFO3args args
;
761 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
763 if (status
== RPC_STATUS_ERROR
) {
764 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
765 free_nfs_cb_data(data
);
768 if (status
== RPC_STATUS_CANCEL
) {
769 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
770 free_nfs_cb_data(data
);
774 args
.fsroot
= nfs
->rootfh
;
775 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
776 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
777 free_nfs_cb_data(data
);
782 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
784 struct nfs_cb_data
*data
= private_data
;
785 struct nfs_context
*nfs
= data
->nfs
;
788 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
790 if (status
== RPC_STATUS_ERROR
) {
791 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
792 free_nfs_cb_data(data
);
795 if (status
== RPC_STATUS_CANCEL
) {
796 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
797 free_nfs_cb_data(data
);
802 if (res
->fhs_status
!= MNT3_OK
) {
803 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
));
804 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
805 free_nfs_cb_data(data
);
809 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
810 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
811 if (nfs
->rootfh
.data
.data_val
== NULL
) {
812 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
813 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
814 free_nfs_cb_data(data
);
817 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
819 rpc_disconnect(rpc
, "normal disconnect");
821 if (rpc_connect_program_async(nfs
->rpc
, nfs
->server
, NFS_PROGRAM
, NFS_V3
, nfs_mount_8_cb
, data
) != 0) {
822 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
823 free_nfs_cb_data(data
);
827 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
828 rpc_set_autoreconnect(rpc
);
832 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
834 struct nfs_cb_data
*data
= private_data
;
835 struct nfs_context
*nfs
= data
->nfs
;
837 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
839 if (status
== RPC_STATUS_ERROR
) {
840 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
841 free_nfs_cb_data(data
);
844 if (status
== RPC_STATUS_CANCEL
) {
845 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
846 free_nfs_cb_data(data
);
850 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
851 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
852 free_nfs_cb_data(data
);
858 * Async call for mounting an nfs share and geting the root filehandle
860 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
862 struct nfs_cb_data
*data
;
863 char *new_server
, *new_export
;
865 data
= malloc(sizeof(struct nfs_cb_data
));
867 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
870 memset(data
, 0, sizeof(struct nfs_cb_data
));
871 new_server
= strdup(server
);
872 new_export
= strdup(export
);
873 if (nfs
->server
!= NULL
) {
876 nfs
->server
= new_server
;
877 if (nfs
->export
!= NULL
) {
880 nfs
->export
= new_export
;
883 data
->private_data
= private_data
;
885 if (rpc_connect_program_async(nfs
->rpc
, server
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_5_cb
, data
) != 0) {
886 rpc_set_error(nfs
->rpc
, "Failed to start connection");
887 free_nfs_cb_data(data
);
897 * Functions to first look up a path, component by component, and then finally call a specific function once
898 * the filehandle for the final component is found.
900 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
902 struct nfs_cb_data
*data
= private_data
;
903 struct nfs_context
*nfs
= data
->nfs
;
907 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
909 if (status
== RPC_STATUS_ERROR
) {
910 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
911 free_nfs_cb_data(data
);
914 if (status
== RPC_STATUS_CANCEL
) {
915 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
916 free_nfs_cb_data(data
);
921 if (res
->status
!= NFS3_OK
) {
922 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
));
923 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
924 free_nfs_cb_data(data
);
928 attr
= res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
?
929 &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
:
932 /* This function will always invoke the callback and cleanup
933 * for failures. So no need to check the return value.
935 nfs_lookup_path_async_internal(nfs
, attr
, data
, &res
->LOOKUP3res_u
.resok
.object
);
938 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
943 while (*data
->path
== '/') {
948 slash
= strchr(path
, '/');
950 /* Clear slash so that path is a zero terminated string for
951 * the current path component. Set it back to '/' again later
952 * when we are finished referencing this component so that
953 * data->saved_path will still point to the full
957 data
->path
= slash
+1;
959 while (*data
->path
!= 0) {
965 data
->fh
.data
.data_len
= fh
->data
.data_len
;
966 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
967 if (data
->fh
.data
.data_val
== NULL
) {
968 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
969 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
970 free_nfs_cb_data(data
);
973 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
977 data
->continue_cb(nfs
, attr
, data
);
981 memset(&args
, 0, sizeof(LOOKUP3args
));
983 args
.what
.name
= path
;
985 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
986 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
987 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
988 free_nfs_cb_data(data
);
997 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
1003 while ((str
= strstr(path
, "//"))) {
1011 while ((str
= strstr(path
, "/./"))) {
1018 /* ^/../ -> error */
1019 if (!strncmp(path
, "/../", 4)) {
1020 rpc_set_error(nfs
->rpc
,
1021 "Absolute path starts with '/../' "
1022 "during normalization");
1026 /* ^[^/] -> error */
1027 if (path
[0] != '/') {
1028 rpc_set_error(nfs
->rpc
,
1029 "Absolute path does not start with '/'");
1033 /* /string/../ -> / */
1034 while ((str
= strstr(path
, "/../"))) {
1037 if (!strncmp(path
, "/../", 4)) {
1038 rpc_set_error(nfs
->rpc
,
1039 "Absolute path starts with '/../' "
1040 "during normalization");
1045 while (*tmp
!= '/') {
1049 while((*(tmp
++) = *(str
++)) != '\0')
1056 if (path
[len
- 1] == '/') {
1057 path
[len
- 1] = '\0';
1061 if (path
[0] == '\0') {
1062 rpc_set_error(nfs
->rpc
,
1063 "Absolute path became '' "
1064 "during normalization");
1070 if (!strcmp(&path
[len
- 2], "/.")) {
1071 path
[len
- 2] = '\0';
1076 /* ^/..$ -> error */
1077 if (!strcmp(path
, "/..")) {
1078 rpc_set_error(nfs
->rpc
,
1079 "Absolute path is '/..' "
1080 "during normalization");
1084 /* /string/..$ -> / */
1086 if (!strcmp(&path
[len
- 3], "/..")) {
1087 char *tmp
= &path
[len
- 3];
1088 while (*--tmp
!= '/')
1097 static void nfs_lookup_path_getattr_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1099 struct nfs_cb_data
*data
= private_data
;
1100 struct nfs_context
*nfs
= data
->nfs
;
1104 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1106 if (status
== RPC_STATUS_ERROR
) {
1107 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1108 free_nfs_cb_data(data
);
1111 if (status
== RPC_STATUS_CANCEL
) {
1112 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1113 free_nfs_cb_data(data
);
1118 if (res
->status
!= NFS3_OK
) {
1119 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
));
1120 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1121 free_nfs_cb_data(data
);
1125 attr
= &res
->GETATTR3res_u
.resok
.obj_attributes
;
1126 /* This function will always invoke the callback and cleanup
1127 * for failures. So no need to check the return value.
1129 nfs_lookup_path_async_internal(nfs
, attr
, data
, &nfs
->rootfh
);
1132 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
)
1134 struct nfs_cb_data
*data
;
1135 struct GETATTR3args args
;
1137 if (path
[0] == '\0') {
1141 data
= malloc(sizeof(struct nfs_cb_data
));
1143 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1144 "nfs_cb_data structure");
1145 if (free_continue_data
)
1146 free_continue_data(continue_data
);
1149 memset(data
, 0, sizeof(struct nfs_cb_data
));
1152 data
->continue_cb
= continue_cb
;
1153 data
->continue_data
= continue_data
;
1154 data
->free_continue_data
= free_continue_data
;
1155 data
->continue_int
= continue_int
;
1156 data
->private_data
= private_data
;
1157 if (path
[0] == '/') {
1158 data
->saved_path
= strdup(path
);
1160 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1161 if (data
->saved_path
== NULL
) {
1162 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1163 "malloc path string");
1164 free_nfs_cb_data(data
);
1167 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1170 if (data
->saved_path
== NULL
) {
1171 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1172 free_nfs_cb_data(data
);
1175 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1176 free_nfs_cb_data(data
);
1180 data
->path
= data
->saved_path
;
1181 if (data
->path
[0]) {
1182 /* This function will always invoke the callback and cleanup
1183 * for failures. So no need to check the return value.
1185 nfs_lookup_path_async_internal(nfs
, NULL
, data
, &nfs
->rootfh
);
1189 /* We have a request for "", so just perform a GETATTR3 so we can
1190 * return the attributes to the caller.
1192 memset(&args
, 0, sizeof(GETATTR3args
));
1193 args
.object
= nfs
->rootfh
;
1194 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lookup_path_getattr_cb
, &args
, data
) != 0) {
1195 free_nfs_cb_data(data
);
1205 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1208 struct nfs_cb_data
*data
= private_data
;
1209 struct nfs_context
*nfs
= data
->nfs
;
1216 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1218 if (status
== RPC_STATUS_ERROR
) {
1219 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1220 free_nfs_cb_data(data
);
1223 if (status
== RPC_STATUS_CANCEL
) {
1224 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1225 free_nfs_cb_data(data
);
1230 if (res
->status
!= NFS3_OK
) {
1231 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
));
1232 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1233 free_nfs_cb_data(data
);
1238 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1239 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1240 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1241 st
.st_mode
|= S_IFDIR
;
1243 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1244 st
.st_mode
|= S_IFREG
;
1246 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1247 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1248 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1250 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1252 st
.st_blksize
= NFS_BLKSIZE
;
1253 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ NFS_BLKSIZE
;
1255 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1256 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1257 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1259 data
->cb(0, nfs
, &st
, data
->private_data
);
1260 free_nfs_cb_data(data
);
1263 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1265 struct GETATTR3args args
;
1267 memset(&args
, 0, sizeof(GETATTR3args
));
1268 args
.object
= data
->fh
;
1270 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1271 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1272 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1273 free_nfs_cb_data(data
);
1279 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1281 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1282 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1291 * Async nfs_stat64()
1293 static void nfs_stat64_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1296 struct nfs_cb_data
*data
= private_data
;
1297 struct nfs_context
*nfs
= data
->nfs
;
1298 struct nfs_stat_64 st
;
1300 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1302 if (status
== RPC_STATUS_ERROR
) {
1303 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1304 free_nfs_cb_data(data
);
1307 if (status
== RPC_STATUS_CANCEL
) {
1308 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1309 free_nfs_cb_data(data
);
1314 if (res
->status
!= NFS3_OK
) {
1315 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
));
1316 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1317 free_nfs_cb_data(data
);
1322 st
.nfs_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1323 st
.nfs_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1324 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1325 st
.nfs_mode
|= S_IFDIR
;
1327 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1328 st
.nfs_mode
|= S_IFREG
;
1330 st
.nfs_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1331 st
.nfs_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1332 st
.nfs_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1334 st
.nfs_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1335 st
.nfs_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1336 st
.nfs_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1337 st
.nfs_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1339 data
->cb(0, nfs
, &st
, data
->private_data
);
1340 free_nfs_cb_data(data
);
1343 static int nfs_stat64_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1345 struct GETATTR3args args
;
1347 memset(&args
, 0, sizeof(GETATTR3args
));
1348 args
.object
= data
->fh
;
1350 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
1351 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1352 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1353 free_nfs_cb_data(data
);
1359 int nfs_stat64_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1361 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat64_continue_internal
, NULL
, NULL
, 0) != 0) {
1362 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1372 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1374 struct nfs_cb_data
*data
= private_data
;
1375 struct nfs_context
*nfs
= data
->nfs
;
1376 struct nfsfh
*nfsfh
;
1379 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1381 if (status
== RPC_STATUS_ERROR
) {
1382 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1383 free_nfs_cb_data(data
);
1386 if (status
== RPC_STATUS_CANCEL
) {
1387 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1388 free_nfs_cb_data(data
);
1393 if (res
->status
!= NFS3_OK
) {
1394 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1395 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1396 free_nfs_cb_data(data
);
1400 nfsfh
= malloc(sizeof(struct nfsfh
));
1401 if (nfsfh
== NULL
) {
1402 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1403 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1404 free_nfs_cb_data(data
);
1407 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1409 if (data
->continue_int
& O_SYNC
) {
1412 if (data
->continue_int
& O_APPEND
) {
1413 nfsfh
->is_append
= 1;
1416 /* steal the filehandle */
1417 nfsfh
->fh
= data
->fh
;
1418 data
->fh
.data
.data_val
= NULL
;
1420 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1421 free_nfs_cb_data(data
);
1424 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1427 struct nfs_cb_data
*data
= private_data
;
1428 struct nfs_context
*nfs
= data
->nfs
;
1429 struct nfsfh
*nfsfh
;
1430 unsigned int nfsmode
= 0;
1432 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1434 if (status
== RPC_STATUS_ERROR
) {
1435 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1436 free_nfs_cb_data(data
);
1439 if (status
== RPC_STATUS_CANCEL
) {
1440 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1441 free_nfs_cb_data(data
);
1446 if (res
->status
!= NFS3_OK
) {
1447 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
));
1448 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1449 free_nfs_cb_data(data
);
1453 if (data
->continue_int
& O_WRONLY
) {
1454 nfsmode
|= ACCESS3_MODIFY
;
1456 if (data
->continue_int
& O_RDWR
) {
1457 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1459 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1460 nfsmode
|= ACCESS3_READ
;
1464 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1465 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1466 nfsmode
&ACCESS3_READ
?'r':'-',
1467 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1468 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1469 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1470 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1471 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1472 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1473 free_nfs_cb_data(data
);
1477 /* Try to truncate it if we were requested to */
1478 if ((data
->continue_int
& O_TRUNC
) &&
1479 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1482 memset(&args
, 0, sizeof(SETATTR3args
));
1483 args
.object
= data
->fh
;
1484 args
.new_attributes
.size
.set_it
= 1;
1485 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1487 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1489 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1490 "SETATTR call for %s", data
->path
);
1491 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1492 data
->private_data
);
1493 free_nfs_cb_data(data
);
1499 nfsfh
= malloc(sizeof(struct nfsfh
));
1500 if (nfsfh
== NULL
) {
1501 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1502 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1503 free_nfs_cb_data(data
);
1506 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1508 if (data
->continue_int
& O_SYNC
) {
1511 if (data
->continue_int
& O_APPEND
) {
1512 nfsfh
->is_append
= 1;
1515 /* steal the filehandle */
1516 nfsfh
->fh
= data
->fh
;
1517 data
->fh
.data
.data_val
= NULL
;
1519 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1520 free_nfs_cb_data(data
);
1523 static int nfs_open_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1528 if (data
->continue_int
& O_WRONLY
) {
1529 nfsmode
|= ACCESS3_MODIFY
;
1531 if (data
->continue_int
& O_RDWR
) {
1532 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1534 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1535 nfsmode
|= ACCESS3_READ
;
1538 memset(&args
, 0, sizeof(ACCESS3args
));
1539 args
.object
= data
->fh
;
1540 args
.access
= nfsmode
;
1542 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1543 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1544 "call for %s", data
->path
);
1545 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1546 data
->private_data
);
1547 free_nfs_cb_data(data
);
1553 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1555 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1556 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1567 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
1569 /* steal saved_path */
1571 nfs
->cwd
= data
->saved_path
;
1572 data
->saved_path
= NULL
;
1574 data
->cb(0, nfs
, NULL
, data
->private_data
);
1575 free_nfs_cb_data(data
);
1580 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1582 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1583 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1594 static void nfs_fill_READ3args(READ3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
)
1596 memset(args
, 0, sizeof(READ3args
));
1597 args
->file
= fh
->fh
;
1598 args
->offset
= offset
;
1599 args
->count
= count
;
1602 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1604 struct nfs_mcb_data
*mdata
= private_data
;
1605 struct nfs_cb_data
*data
= mdata
->data
;
1606 struct nfs_context
*nfs
= data
->nfs
;
1609 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1613 if (status
== RPC_STATUS_ERROR
) {
1614 /* flag the failure but do not invoke callback until we have received all responses */
1617 if (status
== RPC_STATUS_CANCEL
) {
1618 /* flag the cancellation but do not invoke callback until we have received all responses */
1622 if (status
== RPC_STATUS_SUCCESS
) {
1624 if (res
->status
!= NFS3_OK
) {
1625 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1628 uint64_t count
= res
->READ3res_u
.resok
.count
;
1630 if (mdata
->update_pos
)
1631 data
->nfsfh
->offset
+= count
;
1633 /* if we have more than one call or we have received a short read we need a reassembly buffer */
1634 if (data
->num_calls
|| (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
)) {
1635 if (data
->buffer
== NULL
) {
1636 data
->buffer
= malloc(data
->count
);
1637 if (data
->buffer
== NULL
) {
1638 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data
->count
);
1644 if (count
<= mdata
->count
) {
1645 /* copy data into reassembly buffer if we have one */
1646 if (data
->buffer
!= NULL
) {
1647 memcpy(&data
->buffer
[mdata
->offset
- data
->offset
], res
->READ3res_u
.resok
.data
.data_val
, count
);
1649 if (data
->max_offset
< mdata
->offset
+ count
) {
1650 data
->max_offset
= mdata
->offset
+ count
;
1653 rpc_set_error(nfs
->rpc
, "NFS: Read overflow. Server has sent more data than requested!");
1657 /* check if we have received a short read */
1658 if (count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
) {
1660 rpc_set_error(nfs
->rpc
, "NFS: Read failed. No bytes read and not at EOF!");
1663 /* reissue reminder of this read request */
1665 mdata
->offset
+= count
;
1666 mdata
->count
-= count
;
1667 nfs_fill_READ3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
);
1668 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) == 0) {
1672 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1682 if (data
->num_calls
> 0) {
1683 /* still waiting for more replies */
1686 if (data
->oom
!= 0) {
1687 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1688 free_nfs_cb_data(data
);
1691 if (data
->error
!= 0) {
1692 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1693 free_nfs_cb_data(data
);
1696 if (data
->cancel
!= 0) {
1697 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1698 free_nfs_cb_data(data
);
1703 if (data
->max_offset
> data
->org_offset
+ data
->org_count
) {
1704 data
->max_offset
= data
->org_offset
+ data
->org_count
;
1706 data
->cb(data
->max_offset
- data
->org_offset
, nfs
, data
->buffer
+ (data
->org_offset
- data
->offset
), data
->private_data
);
1708 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1711 data
->nfsfh
->ra
.fh_offset
= data
->max_offset
;
1712 if (data
->nfsfh
->ra
.cur_ra
) {
1713 free(data
->nfsfh
->ra
.buf
);
1714 data
->nfsfh
->ra
.buf
= data
->buffer
;
1715 data
->nfsfh
->ra
.buf_offset
= data
->offset
;
1716 data
->nfsfh
->ra
.buf_count
= data
->count
;
1717 data
->nfsfh
->ra
.buf_ts
= time(NULL
);
1718 data
->buffer
= NULL
;
1720 free_nfs_cb_data(data
);
1723 static void nfs_ra_invalidate(struct nfsfh
*nfsfh
) {
1724 free(nfsfh
->ra
.buf
);
1725 nfsfh
->ra
.buf
= NULL
;
1726 nfsfh
->ra
.buf_offset
= 0;
1727 nfsfh
->ra
.buf_count
= 0;
1728 nfsfh
->ra
.buf_ts
= time(NULL
);
1729 nfsfh
->ra
.cur_ra
= NFS_BLKSIZE
;
1732 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
)
1734 struct nfs_cb_data
*data
;
1736 data
= malloc(sizeof(struct nfs_cb_data
));
1738 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1741 memset(data
, 0, sizeof(struct nfs_cb_data
));
1744 data
->private_data
= private_data
;
1745 data
->nfsfh
= nfsfh
;
1746 data
->org_offset
= offset
;
1747 data
->org_count
= count
;
1749 assert(data
->num_calls
== 0);
1751 if (nfs
->rpc
->readahead
&& time(NULL
) - nfsfh
->ra
.buf_ts
> NFS_RA_TIMEOUT
) {
1752 /* readahead cache timeout */
1753 nfs_ra_invalidate(nfsfh
);
1756 if (nfs
->rpc
->readahead
) {
1757 if (offset
>= nfsfh
->ra
.last_offset
&&
1758 offset
- NFS_BLKSIZE
<= nfsfh
->ra
.fh_offset
+ nfsfh
->ra
.cur_ra
) {
1759 if (nfs
->rpc
->readahead
> nfsfh
->ra
.cur_ra
) {
1760 nfsfh
->ra
.cur_ra
<<= 1;
1763 nfsfh
->ra
.cur_ra
= NFS_BLKSIZE
;
1766 nfsfh
->ra
.last_offset
= offset
;
1768 if (nfsfh
->ra
.buf_offset
<= offset
&&
1769 nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
>= offset
+ count
) {
1770 /* serve request completely from cache */
1771 data
->buffer
= malloc(count
);
1772 if (data
->buffer
== NULL
) {
1773 free_nfs_cb_data(data
);
1776 memcpy(data
->buffer
, nfsfh
->ra
.buf
+ (offset
- nfsfh
->ra
.buf_offset
), count
);
1777 data
->cb(count
, nfs
, data
->buffer
, data
->private_data
);
1778 nfsfh
->ra
.fh_offset
= offset
+ count
;
1779 free_nfs_cb_data(data
);
1783 /* align start offset to blocksize */
1784 count
+= offset
& (NFS_BLKSIZE
- 1);
1785 offset
&= ~(NFS_BLKSIZE
- 1);
1787 /* align end offset to blocksize and add readahead */
1788 count
+= nfsfh
->ra
.cur_ra
- 1;
1789 count
&= ~(NFS_BLKSIZE
- 1);
1791 data
->buffer
= malloc(count
);
1792 if (data
->buffer
== NULL
) {
1793 free_nfs_cb_data(data
);
1796 data
->offset
= offset
;
1797 data
->count
= count
;
1799 if (nfsfh
->ra
.buf_count
&& nfsfh
->ra
.buf_offset
<= offset
&&
1800 nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
>= offset
) {
1801 /* serve request partially from cache */
1802 size_t overlap
= (nfsfh
->ra
.buf_offset
+ nfsfh
->ra
.buf_count
) - offset
;
1803 if (overlap
> count
) count
= overlap
;
1804 memcpy(data
->buffer
, nfsfh
->ra
.buf
+ (offset
- nfsfh
->ra
.buf_offset
), overlap
);
1809 data
->offset
= offset
;
1810 data
->count
= count
;
1813 data
->max_offset
= offset
;
1815 /* chop requests into chunks of at most READMAX bytes if necessary.
1816 * we send all reads in parallel so that performance is still good.
1819 uint64_t readcount
= count
;
1820 struct nfs_mcb_data
*mdata
;
1823 if (readcount
> nfs_get_readmax(nfs
)) {
1824 readcount
= nfs_get_readmax(nfs
);
1827 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1828 if (mdata
== NULL
) {
1829 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1830 if (data
->num_calls
== 0) {
1831 free_nfs_cb_data(data
);
1837 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1839 mdata
->offset
= offset
;
1840 mdata
->count
= readcount
;
1841 mdata
->update_pos
= update_pos
;
1843 nfs_fill_READ3args(&args
, nfsfh
, offset
, readcount
);
1845 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1846 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1848 if (data
->num_calls
== 0) {
1849 free_nfs_cb_data(data
);
1857 offset
+= readcount
;
1859 } while (count
> 0);
1864 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1866 return nfs_pread_async_internal(nfs
, nfsfh
, offset
, count
, cb
, private_data
, 0);
1872 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1874 return nfs_pread_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
, 1);
1882 static void nfs_fill_WRITE3args (WRITE3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
,
1885 memset(args
, 0, sizeof(WRITE3args
));
1886 args
->file
= fh
->fh
;
1887 args
->offset
= offset
;
1888 args
->count
= count
;
1889 args
->stable
= fh
->is_sync
? FILE_SYNC
: UNSTABLE
;
1890 args
->data
.data_len
= count
;
1891 args
->data
.data_val
= buf
;
1894 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1896 struct nfs_mcb_data
*mdata
= private_data
;
1897 struct nfs_cb_data
*data
= mdata
->data
;
1898 struct nfs_context
*nfs
= data
->nfs
;
1901 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1905 if (status
== RPC_STATUS_ERROR
) {
1906 /* flag the failure but do not invoke callback until we have received all responses */
1909 if (status
== RPC_STATUS_CANCEL
) {
1910 /* flag the cancellation but do not invoke callback until we have received all responses */
1914 if (status
== RPC_STATUS_SUCCESS
) {
1916 if (res
->status
!= NFS3_OK
) {
1917 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1920 uint64_t count
= res
->WRITE3res_u
.resok
.count
;
1922 if (mdata
->update_pos
)
1923 data
->nfsfh
->offset
+= count
;
1925 if (count
< mdata
->count
) {
1927 rpc_set_error(nfs
->rpc
, "NFS: Write failed. No bytes written!");
1930 /* reissue reminder of this write request */
1932 mdata
->offset
+= count
;
1933 mdata
->count
-= count
;
1935 nfs_fill_WRITE3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
,
1936 &data
->usrbuf
[mdata
->offset
- data
->offset
]);
1937 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) == 0) {
1941 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1947 if (data
->max_offset
< mdata
->offset
+ count
) {
1948 data
->max_offset
= mdata
->offset
+ count
;
1956 if (data
->num_calls
> 0) {
1957 /* still waiting for more replies */
1960 if (data
->oom
!= 0) {
1961 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1962 free_nfs_cb_data(data
);
1965 if (data
->error
!= 0) {
1966 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1967 free_nfs_cb_data(data
);
1970 if (data
->cancel
!= 0) {
1971 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1972 free_nfs_cb_data(data
);
1976 data
->cb(data
->max_offset
- data
->offset
, nfs
, NULL
, data
->private_data
);
1978 free_nfs_cb_data(data
);
1982 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
)
1984 struct nfs_cb_data
*data
;
1986 data
= malloc(sizeof(struct nfs_cb_data
));
1988 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1991 memset(data
, 0, sizeof(struct nfs_cb_data
));
1994 data
->private_data
= private_data
;
1995 data
->nfsfh
= nfsfh
;
1998 /* hello, clang-analyzer */
1999 assert(data
->num_calls
== 0);
2001 /* chop requests into chunks of at most WRITEMAX bytes if necessary.
2002 * we send all writes in parallel so that performance is still good.
2004 data
->max_offset
= offset
;
2005 data
->offset
= offset
;
2008 uint64_t writecount
= count
;
2009 struct nfs_mcb_data
*mdata
;
2012 if (writecount
> nfs_get_writemax(nfs
)) {
2013 writecount
= nfs_get_writemax(nfs
);
2016 mdata
= malloc(sizeof(struct nfs_mcb_data
));
2017 if (mdata
== NULL
) {
2018 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
2019 if (data
->num_calls
== 0) {
2020 free_nfs_cb_data(data
);
2026 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
2028 mdata
->offset
= offset
;
2029 mdata
->count
= writecount
;
2030 mdata
->update_pos
= update_pos
;
2032 nfs_fill_WRITE3args(&args
, nfsfh
, offset
, writecount
, &buf
[offset
- data
->offset
]);
2034 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
2035 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
2037 if (data
->num_calls
== 0) {
2038 free_nfs_cb_data(data
);
2045 count
-= writecount
;
2046 offset
+= writecount
;
2048 } while (count
> 0);
2053 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
)
2055 return nfs_pwrite_async_internal(nfs
, nfsfh
, offset
, count
, buf
, cb
, private_data
, 0);
2061 static void nfs_write_append_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2063 struct nfs_cb_data
*data
= private_data
;
2064 struct nfs_context
*nfs
= data
->nfs
;
2067 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2069 if (status
== RPC_STATUS_ERROR
) {
2070 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2071 free_nfs_cb_data(data
);
2074 if (status
== RPC_STATUS_CANCEL
) {
2075 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2076 free_nfs_cb_data(data
);
2081 if (res
->status
!= NFS3_OK
) {
2082 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2083 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2084 free_nfs_cb_data(data
);
2088 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) {
2089 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2090 free_nfs_cb_data(data
);
2093 free_nfs_cb_data(data
);
2096 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
2098 nfs_ra_invalidate(nfsfh
);
2099 if (nfsfh
->is_append
) {
2100 struct GETATTR3args args
;
2101 struct nfs_cb_data
*data
;
2103 data
= malloc(sizeof(struct nfs_cb_data
));
2105 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2108 memset(data
, 0, sizeof(struct nfs_cb_data
));
2111 data
->private_data
= private_data
;
2112 data
->nfsfh
= nfsfh
;
2114 data
->count
= count
;
2116 memset(&args
, 0, sizeof(GETATTR3args
));
2117 args
.object
= nfsfh
->fh
;
2119 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_write_append_cb
, &args
, data
) != 0) {
2120 rpc_set_error(nfs
->rpc
, "out of memory: failed to send GETATTR");
2121 free_nfs_cb_data(data
);
2126 return nfs_pwrite_async_internal(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
, 1);
2136 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2138 if (nfsfh
->fh
.data
.data_val
!= NULL
){
2139 free(nfsfh
->fh
.data
.data_val
);
2140 nfsfh
->fh
.data
.data_val
= NULL
;
2142 free(nfsfh
->ra
.buf
);
2145 cb(0, nfs
, NULL
, private_data
);
2156 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2158 struct nfs_cb_data
*data
;
2159 struct GETATTR3args args
;
2161 data
= malloc(sizeof(struct nfs_cb_data
));
2163 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2166 memset(data
, 0, sizeof(struct nfs_cb_data
));
2169 data
->private_data
= private_data
;
2171 memset(&args
, 0, sizeof(GETATTR3args
));
2172 args
.object
= nfsfh
->fh
;
2174 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
2175 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
2176 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2177 free_nfs_cb_data(data
);
2188 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2190 struct nfs_cb_data
*data
= private_data
;
2191 struct nfs_context
*nfs
= data
->nfs
;
2194 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2196 if (status
== RPC_STATUS_ERROR
) {
2197 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2198 free_nfs_cb_data(data
);
2201 if (status
== RPC_STATUS_CANCEL
) {
2202 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2203 free_nfs_cb_data(data
);
2208 if (res
->status
!= NFS3_OK
) {
2209 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2210 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2211 free_nfs_cb_data(data
);
2215 data
->cb(0, nfs
, NULL
, data
->private_data
);
2216 free_nfs_cb_data(data
);
2219 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
2221 struct nfs_cb_data
*data
;
2222 struct COMMIT3args args
;
2224 data
= malloc(sizeof(struct nfs_cb_data
));
2226 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2229 memset(data
, 0, sizeof(struct nfs_cb_data
));
2232 data
->private_data
= private_data
;
2234 args
.file
= nfsfh
->fh
;
2237 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
2238 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
2239 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2240 free_nfs_cb_data(data
);
2252 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2254 struct nfs_cb_data
*data
= private_data
;
2255 struct nfs_context
*nfs
= data
->nfs
;
2258 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2260 if (status
== RPC_STATUS_ERROR
) {
2261 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2262 free_nfs_cb_data(data
);
2265 if (status
== RPC_STATUS_CANCEL
) {
2266 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2267 free_nfs_cb_data(data
);
2272 if (res
->status
!= NFS3_OK
) {
2273 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2274 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2275 free_nfs_cb_data(data
);
2279 data
->cb(0, nfs
, NULL
, data
->private_data
);
2280 free_nfs_cb_data(data
);
2283 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
2285 struct nfs_cb_data
*data
;
2288 data
= malloc(sizeof(struct nfs_cb_data
));
2290 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2293 memset(data
, 0, sizeof(struct nfs_cb_data
));
2296 data
->private_data
= private_data
;
2298 memset(&args
, 0, sizeof(SETATTR3args
));
2299 args
.object
= nfsfh
->fh
;
2300 args
.new_attributes
.size
.set_it
= 1;
2301 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2303 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2304 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2305 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2306 free_nfs_cb_data(data
);
2316 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2318 uint64_t offset
= data
->continue_int
;
2321 nfsfh
.fh
= data
->fh
;
2323 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2324 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2325 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2326 free_nfs_cb_data(data
);
2329 free_nfs_cb_data(data
);
2333 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2339 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2340 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2353 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2356 struct nfs_cb_data
*data
= private_data
;
2357 struct nfs_context
*nfs
= data
->nfs
;
2358 char *str
= data
->continue_data
;
2360 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2362 str
= &str
[strlen(str
) + 1];
2364 if (status
== RPC_STATUS_ERROR
) {
2365 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2366 free_nfs_cb_data(data
);
2369 if (status
== RPC_STATUS_CANCEL
) {
2370 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2371 free_nfs_cb_data(data
);
2376 if (res
->status
!= NFS3_OK
) {
2377 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
));
2378 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2379 free_nfs_cb_data(data
);
2383 data
->cb(0, nfs
, NULL
, data
->private_data
);
2384 free_nfs_cb_data(data
);
2387 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2389 char *str
= data
->continue_data
;
2392 str
= &str
[strlen(str
) + 1];
2394 memset(&args
, 0, sizeof(MKDIR3args
));
2395 args
.where
.dir
= data
->fh
;
2396 args
.where
.name
= str
;
2397 args
.attributes
.mode
.set_it
= 1;
2398 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2400 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2401 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2402 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2403 free_nfs_cb_data(data
);
2409 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2414 new_path
= strdup(path
);
2415 if (new_path
== NULL
) {
2416 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2420 ptr
= strrchr(new_path
, '/');
2423 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2428 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2429 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2430 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2444 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2447 struct nfs_cb_data
*data
= private_data
;
2448 struct nfs_context
*nfs
= data
->nfs
;
2449 char *str
= data
->continue_data
;
2451 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2453 str
= &str
[strlen(str
) + 1];
2455 if (status
== RPC_STATUS_ERROR
) {
2456 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2457 free_nfs_cb_data(data
);
2460 if (status
== RPC_STATUS_CANCEL
) {
2461 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2462 free_nfs_cb_data(data
);
2467 if (res
->status
!= NFS3_OK
) {
2468 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
));
2469 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2470 free_nfs_cb_data(data
);
2474 data
->cb(0, nfs
, NULL
, data
->private_data
);
2475 free_nfs_cb_data(data
);
2478 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2480 char *str
= data
->continue_data
;
2483 str
= &str
[strlen(str
) + 1];
2485 args
.object
.dir
= data
->fh
;
2486 args
.object
.name
= str
;
2487 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2488 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2489 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2490 free_nfs_cb_data(data
);
2496 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2501 new_path
= strdup(path
);
2502 if (new_path
== NULL
) {
2503 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2507 ptr
= strrchr(new_path
, '/');
2510 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2515 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2516 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2517 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2530 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2533 struct nfs_cb_data
*data
= private_data
;
2534 struct nfs_context
*nfs
= data
->nfs
;
2535 struct nfsfh
*nfsfh
;
2536 char *str
= data
->continue_data
;
2538 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2540 if (status
== RPC_STATUS_ERROR
) {
2541 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2542 free_nfs_cb_data(data
);
2545 if (status
== RPC_STATUS_CANCEL
) {
2546 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2547 free_nfs_cb_data(data
);
2551 str
= &str
[strlen(str
) + 1];
2553 if (res
->status
!= NFS3_OK
) {
2554 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
));
2555 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2560 nfsfh
= malloc(sizeof(struct nfsfh
));
2561 if (nfsfh
== NULL
) {
2562 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2563 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2564 free_nfs_cb_data(data
);
2567 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2569 /* copy the filehandle */
2570 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2571 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2572 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2574 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2575 free_nfs_cb_data(data
);
2580 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2583 struct nfs_cb_data
*data
= private_data
;
2584 struct nfs_context
*nfs
= data
->nfs
;
2585 char *str
= data
->continue_data
;
2588 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2590 if (status
== RPC_STATUS_ERROR
) {
2591 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2592 free_nfs_cb_data(data
);
2595 if (status
== RPC_STATUS_CANCEL
) {
2596 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2597 free_nfs_cb_data(data
);
2601 str
= &str
[strlen(str
) + 1];
2603 if (res
->status
!= NFS3_OK
) {
2604 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
));
2605 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2606 free_nfs_cb_data(data
);
2610 memset(&args
, 0, sizeof(LOOKUP3args
));
2611 args
.what
.dir
= data
->fh
;
2612 args
.what
.name
= str
;
2614 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2615 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2616 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2617 free_nfs_cb_data(data
);
2623 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2625 char *str
= data
->continue_data
;
2628 str
= &str
[strlen(str
) + 1];
2630 memset(&args
, 0, sizeof(CREATE3args
));
2631 args
.where
.dir
= data
->fh
;
2632 args
.where
.name
= str
;
2633 args
.how
.mode
= UNCHECKED
;
2634 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2635 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2637 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2638 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2639 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2640 free_nfs_cb_data(data
);
2646 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2651 new_path
= strdup(path
);
2652 if (new_path
== NULL
) {
2653 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2657 ptr
= strrchr(new_path
, '/');
2659 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2665 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2666 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2667 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2680 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2683 struct nfs_cb_data
*data
= private_data
;
2684 struct nfs_context
*nfs
= data
->nfs
;
2685 char *str
= data
->continue_data
;
2687 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2689 str
= &str
[strlen(str
) + 1];
2691 if (status
== RPC_STATUS_ERROR
) {
2692 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2693 free_nfs_cb_data(data
);
2696 if (status
== RPC_STATUS_CANCEL
) {
2697 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2698 free_nfs_cb_data(data
);
2703 if (res
->status
!= NFS3_OK
) {
2704 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
));
2705 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2706 free_nfs_cb_data(data
);
2710 data
->cb(0, nfs
, NULL
, data
->private_data
);
2711 free_nfs_cb_data(data
);
2714 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2716 char *str
= data
->continue_data
;
2717 struct REMOVE3args args
;
2719 str
= &str
[strlen(str
) + 1];
2721 args
.object
.dir
= data
->fh
;
2722 args
.object
.name
= str
;
2723 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2724 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2725 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2726 free_nfs_cb_data(data
);
2732 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2737 new_path
= strdup(path
);
2738 if (new_path
== NULL
) {
2739 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2743 ptr
= strrchr(new_path
, '/');
2746 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2751 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2752 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2753 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2764 struct mknod_cb_data
{
2771 static void free_mknod_cb_data(void *ptr
)
2773 struct mknod_cb_data
*data
= ptr
;
2779 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2782 struct nfs_cb_data
*data
= private_data
;
2783 struct nfs_context
*nfs
= data
->nfs
;
2784 char *str
= data
->continue_data
;
2786 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2788 str
= &str
[strlen(str
) + 1];
2790 if (status
== RPC_STATUS_ERROR
) {
2791 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2792 free_nfs_cb_data(data
);
2795 if (status
== RPC_STATUS_CANCEL
) {
2796 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2797 free_nfs_cb_data(data
);
2802 if (res
->status
!= NFS3_OK
) {
2803 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
));
2804 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2805 free_nfs_cb_data(data
);
2809 data
->cb(0, nfs
, NULL
, data
->private_data
);
2810 free_nfs_cb_data(data
);
2813 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
2815 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2816 char *str
= cb_data
->path
;
2819 str
= &str
[strlen(str
) + 1];
2821 args
.where
.dir
= data
->fh
;
2822 args
.where
.name
= str
;
2823 switch (cb_data
->mode
& S_IFMT
) {
2825 args
.what
.type
= NF3CHR
;
2826 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2827 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
);
2828 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2829 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2832 args
.what
.type
= NF3BLK
;
2833 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2834 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
);
2835 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2836 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2838 args
.what
.type
= NF3SOCK
;
2839 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2840 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
);
2843 args
.what
.type
= NF3FIFO
;
2844 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2845 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
);
2848 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2849 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2850 free_nfs_cb_data(data
);
2854 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2855 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2856 free_nfs_cb_data(data
);
2862 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2865 struct mknod_cb_data
*cb_data
;
2867 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2868 if (cb_data
== NULL
) {
2869 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2873 cb_data
->path
= strdup(path
);
2874 if (cb_data
->path
== NULL
) {
2875 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2880 ptr
= strrchr(cb_data
->path
, '/');
2882 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2883 free_mknod_cb_data(cb_data
);
2888 cb_data
->mode
= mode
;
2889 cb_data
->major
= major(dev
);
2890 cb_data
->minor
= minor(dev
);
2892 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2893 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2894 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2905 /* ReadDirPlus Emulation Callback data */
2906 struct rdpe_cb_data
{
2909 struct nfs_cb_data
*data
;
2912 /* ReadDirPlus Emulation LOOKUP Callback data */
2913 struct rdpe_lookup_cb_data
{
2914 struct rdpe_cb_data
*rdpe_cb_data
;
2915 struct nfsdirent
*nfsdirent
;
2918 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2919 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2921 LOOKUP3res
*res
= command_data
;
2922 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2923 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2924 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2925 struct nfsdir
*nfsdir
= data
->continue_data
;
2926 struct nfs_context
*nfs
= data
->nfs
;
2927 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2929 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2931 free(rdpe_lookup_cb_data
);
2933 rdpe_cb_data
->getattrcount
--;
2935 if (status
== RPC_STATUS_ERROR
) {
2936 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2937 "failed with RPC_STATUS_ERROR");
2938 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2940 if (status
== RPC_STATUS_CANCEL
) {
2941 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2942 "failed with RPC_STATUS_CANCEL");
2943 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2945 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2946 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2947 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2949 nfsdirent
->type
= attributes
->type
;
2950 nfsdirent
->mode
= attributes
->mode
;
2951 nfsdirent
->size
= attributes
->size
;
2953 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2954 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2955 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2956 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2957 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2958 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2959 nfsdirent
->uid
= attributes
->uid
;
2960 nfsdirent
->gid
= attributes
->gid
;
2961 nfsdirent
->nlink
= attributes
->nlink
;
2965 if (rdpe_cb_data
->getattrcount
== 0) {
2966 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2967 rpc_set_error(nfs
->rpc
, "READDIRPLUS emulation "
2968 "failed: %s", rpc_get_error(rpc
));
2969 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
2970 data
->private_data
);
2971 nfs_free_nfsdir(nfsdir
);
2973 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2977 data
->continue_data
= NULL
;
2978 free_nfs_cb_data(data
);
2982 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2984 READDIR3res
*res
= command_data
;
2985 struct nfs_cb_data
*data
= private_data
;
2986 struct nfs_context
*nfs
= data
->nfs
;
2987 struct nfsdir
*nfsdir
= data
->continue_data
;
2988 struct nfsdirent
*nfsdirent
;
2989 struct entry3
*entry
;
2990 uint64_t cookie
= 0;
2991 struct rdpe_cb_data
*rdpe_cb_data
;
2993 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2995 if (status
== RPC_STATUS_ERROR
) {
2996 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2997 nfs_free_nfsdir(nfsdir
);
2998 data
->continue_data
= NULL
;
2999 free_nfs_cb_data(data
);
3003 if (status
== RPC_STATUS_CANCEL
) {
3004 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3005 nfs_free_nfsdir(nfsdir
);
3006 data
->continue_data
= NULL
;
3007 free_nfs_cb_data(data
);
3011 if (res
->status
!= NFS3_OK
) {
3012 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
));
3013 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3014 nfs_free_nfsdir(nfsdir
);
3015 data
->continue_data
= NULL
;
3016 free_nfs_cb_data(data
);
3020 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
3021 while (entry
!= NULL
) {
3022 nfsdirent
= malloc(sizeof(struct nfsdirent
));
3023 if (nfsdirent
== NULL
) {
3024 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
3025 nfs_free_nfsdir(nfsdir
);
3026 data
->continue_data
= NULL
;
3027 free_nfs_cb_data(data
);
3030 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
3031 nfsdirent
->name
= strdup(entry
->name
);
3032 if (nfsdirent
->name
== NULL
) {
3033 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
3035 nfs_free_nfsdir(nfsdir
);
3036 data
->continue_data
= NULL
;
3037 free_nfs_cb_data(data
);
3040 nfsdirent
->inode
= entry
->fileid
;
3042 nfsdirent
->next
= nfsdir
->entries
;
3043 nfsdir
->entries
= nfsdirent
;
3045 cookie
= entry
->cookie
;
3046 entry
= entry
->nextentry
;
3049 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
3052 args
.dir
= data
->fh
;
3053 args
.cookie
= cookie
;
3054 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
3057 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
3058 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
3059 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3060 nfs_free_nfsdir(nfsdir
);
3061 data
->continue_data
= NULL
;
3062 free_nfs_cb_data(data
);
3068 if (res
->READDIR3res_u
.resok
.dir_attributes
.attributes_follow
)
3069 nfsdir
->attr
= res
->READDIR3res_u
.resok
.dir_attributes
.post_op_attr_u
.attributes
;
3071 /* steal the dirhandle */
3072 nfsdir
->current
= nfsdir
->entries
;
3074 if (nfsdir
->entries
) {
3075 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
3076 rdpe_cb_data
->getattrcount
= 0;
3077 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
3078 rdpe_cb_data
->data
= data
;
3079 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
3080 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
3083 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
3084 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
3085 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
3087 memset(&args
, 0, sizeof(LOOKUP3args
));
3088 args
.what
.dir
= data
->fh
;
3089 args
.what
.name
= nfsdirent
->name
;
3091 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
3092 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
3094 /* if we have already commands in flight, we cant just stop, we have to wait for the
3095 * commands in flight to complete
3097 if (rdpe_cb_data
->getattrcount
> 0) {
3098 nfs_free_nfsdir(nfsdir
);
3099 data
->continue_data
= NULL
;
3100 free_nfs_cb_data(data
);
3101 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
3102 free(rdpe_lookup_cb_data
);
3106 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3107 nfs_free_nfsdir(nfsdir
);
3108 data
->continue_data
= NULL
;
3109 free_nfs_cb_data(data
);
3110 free(rdpe_lookup_cb_data
);
3114 rdpe_cb_data
->getattrcount
++;
3119 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3121 READDIRPLUS3res
*res
= command_data
;
3122 struct nfs_cb_data
*data
= private_data
;
3123 struct nfs_context
*nfs
= data
->nfs
;
3124 struct nfsdir
*nfsdir
= data
->continue_data
;
3125 struct entryplus3
*entry
;
3126 uint64_t cookie
= 0;
3128 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3130 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
3133 args
.dir
= data
->fh
;
3134 args
.cookie
= cookie
;
3135 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
3138 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
3139 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
3140 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3141 nfs_free_nfsdir(nfsdir
);
3142 data
->continue_data
= NULL
;
3143 free_nfs_cb_data(data
);
3149 if (status
== RPC_STATUS_CANCEL
) {
3150 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3151 nfs_free_nfsdir(nfsdir
);
3152 data
->continue_data
= NULL
;
3153 free_nfs_cb_data(data
);
3157 if (res
->status
!= NFS3_OK
) {
3158 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
));
3159 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3160 nfs_free_nfsdir(nfsdir
);
3161 data
->continue_data
= NULL
;
3162 free_nfs_cb_data(data
);
3166 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
3167 while (entry
!= NULL
) {
3168 struct nfsdirent
*nfsdirent
;
3170 nfsdirent
= malloc(sizeof(struct nfsdirent
));
3171 if (nfsdirent
== NULL
) {
3172 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
3173 nfs_free_nfsdir(nfsdir
);
3174 data
->continue_data
= NULL
;
3175 free_nfs_cb_data(data
);
3178 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
3179 nfsdirent
->name
= strdup(entry
->name
);
3180 if (nfsdirent
->name
== NULL
) {
3181 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
3183 nfs_free_nfsdir(nfsdir
);
3184 data
->continue_data
= NULL
;
3185 free_nfs_cb_data(data
);
3188 nfsdirent
->inode
= entry
->fileid
;
3189 if (entry
->name_attributes
.attributes_follow
) {
3190 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
3191 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
3192 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
3194 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
3195 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
3196 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
3197 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
3198 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
3199 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
3200 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
3201 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
3202 nfsdirent
->nlink
= entry
->name_attributes
.post_op_attr_u
.attributes
.nlink
;
3205 nfsdirent
->next
= nfsdir
->entries
;
3206 nfsdir
->entries
= nfsdirent
;
3208 cookie
= entry
->cookie
;
3209 entry
= entry
->nextentry
;
3212 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
3213 READDIRPLUS3args args
;
3215 args
.dir
= data
->fh
;
3216 args
.cookie
= cookie
;
3217 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
3218 args
.dircount
= 8192;
3219 args
.maxcount
= 8192;
3221 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
3222 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
3223 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3224 nfs_free_nfsdir(nfsdir
);
3225 data
->continue_data
= NULL
;
3226 free_nfs_cb_data(data
);
3232 if (res
->READDIRPLUS3res_u
.resok
.dir_attributes
.attributes_follow
)
3233 nfsdir
->attr
= res
->READDIRPLUS3res_u
.resok
.dir_attributes
.post_op_attr_u
.attributes
;
3235 /* steal the dirhandle */
3236 data
->continue_data
= NULL
;
3237 nfsdir
->current
= nfsdir
->entries
;
3239 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
3240 free_nfs_cb_data(data
);
3243 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3245 READDIRPLUS3args args
;
3246 struct nfsdir
*nfsdir
= data
->continue_data
;;
3247 struct nfsdir
*cached
;
3249 cached
= nfs_dircache_find(nfs
, &data
->fh
);
3251 if (attr
&& attr
->mtime
.seconds
== cached
->attr
.mtime
.seconds
) {
3252 cached
->current
= cached
->entries
;
3253 data
->cb(0, nfs
, cached
, data
->private_data
);
3254 free_nfs_cb_data(data
);
3257 /* cache must be stale */
3258 nfs_free_nfsdir(cached
);
3262 nfsdir
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
3263 nfsdir
->fh
.data
.data_val
= malloc(nfsdir
->fh
.data
.data_len
);
3264 if (nfsdir
->fh
.data
.data_val
== NULL
) {
3265 rpc_set_error(nfs
->rpc
, "OOM when allocating fh for nfsdir");
3266 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3267 free_nfs_cb_data(data
);
3270 memcpy(nfsdir
->fh
.data
.data_val
, data
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3272 args
.dir
= data
->fh
;
3274 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
3275 args
.dircount
= 8192;
3276 args
.maxcount
= 8192;
3277 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
3278 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
3279 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3280 free_nfs_cb_data(data
);
3286 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3288 struct nfsdir
*nfsdir
;
3290 nfsdir
= malloc(sizeof(struct nfsdir
));
3291 if (nfsdir
== NULL
) {
3292 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
3295 memset(nfsdir
, 0, sizeof(struct nfsdir
));
3297 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
3298 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3306 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3308 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
3310 if (nfsdir
->current
!= NULL
) {
3311 nfsdir
->current
= nfsdir
->current
->next
;
3320 void nfs_closedir(struct nfs_context
*nfs
, struct nfsdir
*nfsdir
)
3322 nfs_dircache_add(nfs
, nfsdir
);
3329 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3340 struct lseek_cb_data
{
3341 struct nfs_context
*nfs
;
3342 struct nfsfh
*nfsfh
;
3348 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3351 struct lseek_cb_data
*data
= private_data
;
3352 struct nfs_context
*nfs
= data
->nfs
;
3355 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3357 if (status
== RPC_STATUS_ERROR
) {
3358 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3362 if (status
== RPC_STATUS_CANCEL
) {
3363 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3369 if (res
->status
!= NFS3_OK
) {
3370 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3371 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3376 size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3378 if (data
->offset
< 0 &&
3379 (uint64_t)(-data
->offset
) > size
) {
3380 data
->cb(-EINVAL
, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3382 data
->nfsfh
->offset
= data
->offset
+ size
;
3383 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3389 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3391 struct lseek_cb_data
*data
;
3392 struct GETATTR3args args
;
3394 if (whence
== SEEK_SET
) {
3396 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3398 nfsfh
->offset
= offset
;
3399 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3403 if (whence
== SEEK_CUR
) {
3405 nfsfh
->offset
< (uint64_t)(-offset
)) {
3406 cb(-EINVAL
, nfs
, &nfsfh
->offset
, private_data
);
3408 nfsfh
->offset
+= offset
;
3409 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3414 data
= malloc(sizeof(struct lseek_cb_data
));
3416 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3421 data
->nfsfh
= nfsfh
;
3422 data
->offset
= offset
;
3424 data
->private_data
= private_data
;
3426 memset(&args
, 0, sizeof(GETATTR3args
));
3427 args
.object
= nfsfh
->fh
;
3429 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3430 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3443 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3446 struct nfs_cb_data
*data
= private_data
;
3447 struct nfs_context
*nfs
= data
->nfs
;
3448 struct statvfs svfs
;
3450 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3452 if (status
== RPC_STATUS_ERROR
) {
3453 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3454 free_nfs_cb_data(data
);
3457 if (status
== RPC_STATUS_CANCEL
) {
3458 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3459 free_nfs_cb_data(data
);
3464 if (res
->status
!= NFS3_OK
) {
3465 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
));
3466 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3467 free_nfs_cb_data(data
);
3471 svfs
.f_bsize
= NFS_BLKSIZE
;
3472 svfs
.f_frsize
= NFS_BLKSIZE
;
3473 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/NFS_BLKSIZE
;
3474 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/NFS_BLKSIZE
;
3475 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/NFS_BLKSIZE
;
3476 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3477 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3478 #if !defined(ANDROID)
3479 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3482 svfs
.f_namemax
= 256;
3485 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3486 free_nfs_cb_data(data
);
3489 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3493 args
.fsroot
= data
->fh
;
3494 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3495 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3496 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3497 free_nfs_cb_data(data
);
3503 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3505 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3506 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3519 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3522 struct nfs_cb_data
*data
= private_data
;
3523 struct nfs_context
*nfs
= data
->nfs
;
3525 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3527 if (status
== RPC_STATUS_ERROR
) {
3528 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3529 free_nfs_cb_data(data
);
3532 if (status
== RPC_STATUS_CANCEL
) {
3533 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3534 free_nfs_cb_data(data
);
3539 if (res
->status
!= NFS3_OK
) {
3540 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
));
3541 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3542 free_nfs_cb_data(data
);
3547 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3548 free_nfs_cb_data(data
);
3551 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3555 args
.symlink
= data
->fh
;
3557 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3558 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3559 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3560 free_nfs_cb_data(data
);
3566 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3568 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3569 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3582 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3584 struct nfs_cb_data
*data
= private_data
;
3585 struct nfs_context
*nfs
= data
->nfs
;
3588 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3590 if (status
== RPC_STATUS_ERROR
) {
3591 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3592 free_nfs_cb_data(data
);
3595 if (status
== RPC_STATUS_CANCEL
) {
3596 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3597 free_nfs_cb_data(data
);
3602 if (res
->status
!= NFS3_OK
) {
3603 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3604 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3605 free_nfs_cb_data(data
);
3609 data
->cb(0, nfs
, NULL
, data
->private_data
);
3610 free_nfs_cb_data(data
);
3613 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3617 memset(&args
, 0, sizeof(SETATTR3args
));
3618 args
.object
= data
->fh
;
3619 args
.new_attributes
.mode
.set_it
= 1;
3620 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3622 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3623 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3624 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3625 free_nfs_cb_data(data
);
3632 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3634 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3635 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3645 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3647 struct nfs_cb_data
*data
;
3649 data
= malloc(sizeof(struct nfs_cb_data
));
3651 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3654 memset(data
, 0, sizeof(struct nfs_cb_data
));
3657 data
->private_data
= private_data
;
3658 data
->continue_int
= mode
;
3659 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3660 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3661 if (data
->fh
.data
.data_val
== NULL
) {
3662 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3663 free_nfs_cb_data(data
);
3666 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3668 if (nfs_chmod_continue_internal(nfs
, NULL
, data
) != 0) {
3680 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3682 struct nfs_cb_data
*data
= private_data
;
3683 struct nfs_context
*nfs
= data
->nfs
;
3686 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3688 if (status
== RPC_STATUS_ERROR
) {
3689 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3690 free_nfs_cb_data(data
);
3693 if (status
== RPC_STATUS_CANCEL
) {
3694 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3695 free_nfs_cb_data(data
);
3700 if (res
->status
!= NFS3_OK
) {
3701 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3702 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3703 free_nfs_cb_data(data
);
3707 data
->cb(0, nfs
, NULL
, data
->private_data
);
3708 free_nfs_cb_data(data
);
3711 struct nfs_chown_data
{
3716 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3719 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3721 memset(&args
, 0, sizeof(SETATTR3args
));
3722 args
.object
= data
->fh
;
3723 if (chown_data
->uid
!= (uid_t
)-1) {
3724 args
.new_attributes
.uid
.set_it
= 1;
3725 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3727 if (chown_data
->gid
!= (gid_t
)-1) {
3728 args
.new_attributes
.gid
.set_it
= 1;
3729 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3732 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3733 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3734 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3735 free_nfs_cb_data(data
);
3742 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3744 struct nfs_chown_data
*chown_data
;
3746 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3747 if (chown_data
== NULL
) {
3748 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3752 chown_data
->uid
= uid
;
3753 chown_data
->gid
= gid
;
3755 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3756 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3767 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3769 struct nfs_cb_data
*data
;
3770 struct nfs_chown_data
*chown_data
;
3772 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3773 if (chown_data
== NULL
) {
3774 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3778 chown_data
->uid
= uid
;
3779 chown_data
->gid
= gid
;
3781 data
= malloc(sizeof(struct nfs_cb_data
));
3783 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3787 memset(data
, 0, sizeof(struct nfs_cb_data
));
3790 data
->private_data
= private_data
;
3791 data
->continue_data
= chown_data
;
3792 data
->free_continue_data
= free
;
3793 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3794 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3795 if (data
->fh
.data
.data_val
== NULL
) {
3796 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3797 free_nfs_cb_data(data
);
3800 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3802 if (nfs_chown_continue_internal(nfs
, NULL
, data
) != 0) {
3816 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3818 struct nfs_cb_data
*data
= private_data
;
3819 struct nfs_context
*nfs
= data
->nfs
;
3822 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3824 if (status
== RPC_STATUS_ERROR
) {
3825 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3826 free_nfs_cb_data(data
);
3829 if (status
== RPC_STATUS_CANCEL
) {
3830 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3831 free_nfs_cb_data(data
);
3836 if (res
->status
!= NFS3_OK
) {
3837 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3838 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3839 free_nfs_cb_data(data
);
3843 data
->cb(0, nfs
, NULL
, data
->private_data
);
3844 free_nfs_cb_data(data
);
3847 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3850 struct timeval
*utimes_data
= data
->continue_data
;
3852 memset(&args
, 0, sizeof(SETATTR3args
));
3853 args
.object
= data
->fh
;
3854 if (utimes_data
!= NULL
) {
3855 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3856 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3857 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3858 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3859 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3860 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3862 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3863 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3866 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3867 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3868 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3869 free_nfs_cb_data(data
);
3876 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3878 struct timeval
*new_times
= NULL
;
3880 if (times
!= NULL
) {
3881 new_times
= malloc(sizeof(struct timeval
)*2);
3882 if (new_times
== NULL
) {
3883 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3887 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3890 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3891 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3901 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3903 struct timeval
*new_times
= NULL
;
3905 if (times
!= NULL
) {
3906 new_times
= malloc(sizeof(struct timeval
)*2);
3907 if (new_times
== NULL
) {
3908 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3912 new_times
[0].tv_sec
= times
->actime
;
3913 new_times
[0].tv_usec
= 0;
3914 new_times
[1].tv_sec
= times
->modtime
;
3915 new_times
[1].tv_usec
= 0;
3918 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3919 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3930 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3933 struct nfs_cb_data
*data
= private_data
;
3934 struct nfs_context
*nfs
= data
->nfs
;
3935 unsigned int nfsmode
= 0;
3937 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3939 if (status
== RPC_STATUS_ERROR
) {
3940 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3941 free_nfs_cb_data(data
);
3944 if (status
== RPC_STATUS_CANCEL
) {
3945 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3946 free_nfs_cb_data(data
);
3951 if (res
->status
!= NFS3_OK
) {
3952 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
));
3953 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3954 free_nfs_cb_data(data
);
3958 if (data
->continue_int
& R_OK
) {
3959 nfsmode
|= ACCESS3_READ
;
3961 if (data
->continue_int
& W_OK
) {
3962 nfsmode
|= ACCESS3_MODIFY
;
3964 if (data
->continue_int
& X_OK
) {
3965 nfsmode
|= ACCESS3_EXECUTE
;
3968 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3969 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3970 nfsmode
&ACCESS3_READ
?'r':'-',
3971 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3972 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3973 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3974 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3975 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3976 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3977 free_nfs_cb_data(data
);
3981 data
->cb(0, nfs
, NULL
, data
->private_data
);
3982 free_nfs_cb_data(data
);
3985 static int nfs_access_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
3990 if (data
->continue_int
& R_OK
) {
3991 nfsmode
|= ACCESS3_READ
;
3993 if (data
->continue_int
& W_OK
) {
3994 nfsmode
|= ACCESS3_MODIFY
;
3996 if (data
->continue_int
& X_OK
) {
3997 nfsmode
|= ACCESS3_EXECUTE
;
4000 memset(&args
, 0, sizeof(ACCESS3args
));
4001 args
.object
= data
->fh
;
4002 args
.access
= nfsmode
;
4004 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
4005 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
4006 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4007 free_nfs_cb_data(data
);
4013 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
4015 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
4016 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4028 struct nfs_symlink_data
{
4030 char *newpathparent
;
4031 char *newpathobject
;
4034 static void free_nfs_symlink_data(void *mem
)
4036 struct nfs_symlink_data
*data
= mem
;
4038 if (data
->oldpath
!= NULL
) {
4039 free(data
->oldpath
);
4041 if (data
->newpathparent
!= NULL
) {
4042 free(data
->newpathparent
);
4044 if (data
->newpathobject
!= NULL
) {
4045 free(data
->newpathobject
);
4050 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4053 struct nfs_cb_data
*data
= private_data
;
4054 struct nfs_context
*nfs
= data
->nfs
;
4055 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
4057 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4059 if (status
== RPC_STATUS_ERROR
) {
4060 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4061 free_nfs_cb_data(data
);
4064 if (status
== RPC_STATUS_CANCEL
) {
4065 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4066 free_nfs_cb_data(data
);
4071 if (res
->status
!= NFS3_OK
) {
4072 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
));
4073 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4074 free_nfs_cb_data(data
);
4078 data
->cb(0, nfs
, NULL
, data
->private_data
);
4079 free_nfs_cb_data(data
);
4082 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4084 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
4087 memset(&args
, 0, sizeof(SYMLINK3args
));
4088 args
.where
.dir
= data
->fh
;
4089 args
.where
.name
= symlink_data
->newpathobject
;
4090 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
4091 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
;
4092 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
4094 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
4095 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
4096 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4097 free_nfs_cb_data(data
);
4103 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4106 struct nfs_symlink_data
*symlink_data
;
4108 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
4109 if (symlink_data
== NULL
) {
4110 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
4113 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
4115 symlink_data
->oldpath
= strdup(oldpath
);
4116 if (symlink_data
->oldpath
== NULL
) {
4117 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4118 free_nfs_symlink_data(symlink_data
);
4122 symlink_data
->newpathparent
= strdup(newpath
);
4123 if (symlink_data
->newpathparent
== NULL
) {
4124 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
4125 free_nfs_symlink_data(symlink_data
);
4129 ptr
= strrchr(symlink_data
->newpathparent
, '/');
4131 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
4132 free_nfs_symlink_data(symlink_data
);
4138 symlink_data
->newpathobject
= strdup(ptr
);
4139 if (symlink_data
->newpathobject
== NULL
) {
4140 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
4141 free_nfs_symlink_data(symlink_data
);
4145 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
4146 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4158 struct nfs_rename_data
{
4161 struct nfs_fh3 olddir
;
4164 struct nfs_fh3 newdir
;
4167 static void free_nfs_rename_data(void *mem
)
4169 struct nfs_rename_data
*data
= mem
;
4171 if (data
->oldpath
!= NULL
) {
4172 free(data
->oldpath
);
4174 if (data
->olddir
.data
.data_val
!= NULL
) {
4175 free(data
->olddir
.data
.data_val
);
4177 if (data
->newpath
!= NULL
) {
4178 free(data
->newpath
);
4180 if (data
->newdir
.data
.data_val
!= NULL
) {
4181 free(data
->newdir
.data
.data_val
);
4186 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4189 struct nfs_cb_data
*data
= private_data
;
4190 struct nfs_context
*nfs
= data
->nfs
;
4191 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4193 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4195 if (status
== RPC_STATUS_ERROR
) {
4196 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4197 free_nfs_cb_data(data
);
4200 if (status
== RPC_STATUS_CANCEL
) {
4201 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4202 free_nfs_cb_data(data
);
4207 if (res
->status
!= NFS3_OK
) {
4208 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
));
4209 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4210 free_nfs_cb_data(data
);
4214 data
->cb(0, nfs
, NULL
, data
->private_data
);
4215 free_nfs_cb_data(data
);
4218 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4220 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4223 /* steal the filehandle */
4224 rename_data
->newdir
= data
->fh
;
4225 data
->fh
.data
.data_val
= NULL
;
4227 args
.from
.dir
= rename_data
->olddir
;
4228 args
.from
.name
= rename_data
->oldobject
;
4229 args
.to
.dir
= rename_data
->newdir
;
4230 args
.to
.name
= rename_data
->newobject
;
4231 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
4232 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
4233 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4234 free_nfs_cb_data(data
);
4241 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4243 struct nfs_rename_data
*rename_data
= data
->continue_data
;
4244 char* newpath
= strdup(rename_data
->newpath
);
4246 rpc_set_error(nfs
->rpc
, "Out of memory. Could not allocate memory to store target path for rename");
4247 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4248 free_nfs_cb_data(data
);
4252 /* steal the filehandle */
4253 rename_data
->olddir
= data
->fh
;
4254 data
->fh
.data
.data_val
= NULL
;
4256 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) {
4257 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", newpath
);
4258 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4259 free_nfs_cb_data(data
);
4263 data
->continue_data
= NULL
;
4264 free_nfs_cb_data(data
);
4271 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4274 struct nfs_rename_data
*rename_data
;
4276 rename_data
= malloc(sizeof(struct nfs_rename_data
));
4277 if (rename_data
== NULL
) {
4278 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
4281 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
4283 rename_data
->oldpath
= strdup(oldpath
);
4284 if (rename_data
->oldpath
== NULL
) {
4285 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4286 free_nfs_rename_data(rename_data
);
4289 ptr
= strrchr(rename_data
->oldpath
, '/');
4291 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
4292 free_nfs_rename_data(rename_data
);
4297 rename_data
->oldobject
= ptr
;
4300 rename_data
->newpath
= strdup(newpath
);
4301 if (rename_data
->newpath
== NULL
) {
4302 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4303 free_nfs_rename_data(rename_data
);
4306 ptr
= strrchr(rename_data
->newpath
, '/');
4308 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4309 free_nfs_rename_data(rename_data
);
4314 rename_data
->newobject
= ptr
;
4317 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
4318 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4329 struct nfs_link_data
{
4331 struct nfs_fh3 oldfh
;
4334 struct nfs_fh3 newdir
;
4337 static void free_nfs_link_data(void *mem
)
4339 struct nfs_link_data
*data
= mem
;
4341 if (data
->oldpath
!= NULL
) {
4342 free(data
->oldpath
);
4344 if (data
->oldfh
.data
.data_val
!= NULL
) {
4345 free(data
->oldfh
.data
.data_val
);
4347 if (data
->newpath
!= NULL
) {
4348 free(data
->newpath
);
4350 if (data
->newdir
.data
.data_val
!= NULL
) {
4351 free(data
->newdir
.data
.data_val
);
4356 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4359 struct nfs_cb_data
*data
= private_data
;
4360 struct nfs_context
*nfs
= data
->nfs
;
4361 struct nfs_link_data
*link_data
= data
->continue_data
;
4363 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4365 if (status
== RPC_STATUS_ERROR
) {
4366 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4367 free_nfs_cb_data(data
);
4370 if (status
== RPC_STATUS_CANCEL
) {
4371 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4372 free_nfs_cb_data(data
);
4377 if (res
->status
!= NFS3_OK
) {
4378 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
));
4379 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4380 free_nfs_cb_data(data
);
4384 data
->cb(0, nfs
, NULL
, data
->private_data
);
4385 free_nfs_cb_data(data
);
4388 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4390 struct nfs_link_data
*link_data
= data
->continue_data
;
4393 /* steal the filehandle */
4394 link_data
->newdir
= data
->fh
;
4395 data
->fh
.data
.data_val
= NULL
;
4397 memset(&args
, 0, sizeof(LINK3args
));
4398 args
.file
= link_data
->oldfh
;
4399 args
.link
.dir
= link_data
->newdir
;
4400 args
.link
.name
= link_data
->newobject
;
4401 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4402 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4403 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4404 free_nfs_cb_data(data
);
4411 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, fattr3
*attr _U_
, struct nfs_cb_data
*data
)
4413 struct nfs_link_data
*link_data
= data
->continue_data
;
4415 /* steal the filehandle */
4416 link_data
->oldfh
= data
->fh
;
4417 data
->fh
.data
.data_val
= NULL
;
4419 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) {
4420 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4421 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4422 free_nfs_cb_data(data
);
4425 data
->continue_data
= NULL
;
4426 free_nfs_cb_data(data
);
4432 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4435 struct nfs_link_data
*link_data
;
4437 link_data
= malloc(sizeof(struct nfs_link_data
));
4438 if (link_data
== NULL
) {
4439 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4442 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4444 link_data
->oldpath
= strdup(oldpath
);
4445 if (link_data
->oldpath
== NULL
) {
4446 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4447 free_nfs_link_data(link_data
);
4451 link_data
->newpath
= strdup(newpath
);
4452 if (link_data
->newpath
== NULL
) {
4453 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4454 free_nfs_link_data(link_data
);
4457 ptr
= strrchr(link_data
->newpath
, '/');
4459 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4460 free_nfs_link_data(link_data
);
4465 link_data
->newobject
= ptr
;
4468 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4469 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4477 //qqq replace later with lseek()
4478 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4480 return nfsfh
->offset
;
4486 * Get the maximum supported READ3 size by the server
4488 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4490 return nfs
->readmax
;
4494 * Get the maximum supported WRITE3 size by the server
4496 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4498 return nfs
->writemax
;
4501 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4502 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4505 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4506 rpc_set_uid(nfs
->rpc
, uid
);
4509 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4510 rpc_set_gid(nfs
->rpc
, gid
);
4513 void nfs_set_readahead(struct nfs_context
*nfs
, uint32_t v
) {
4514 rpc_set_readahead(nfs
->rpc
, v
);
4517 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4522 va_start(ap
, error_string
);
4524 vsnprintf(str
, 1024, error_string
, ap
);
4525 if (nfs
->rpc
->error_string
!= NULL
) {
4526 free(nfs
->rpc
->error_string
);
4528 nfs
->rpc
->error_string
= str
;
4534 struct mount_cb_data
{
4540 static void free_mount_cb_data(struct mount_cb_data
*data
)
4542 if (data
->server
!= NULL
) {
4544 data
->server
= NULL
;
4550 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4552 struct mount_cb_data
*data
= private_data
;
4554 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4556 if (status
== RPC_STATUS_ERROR
) {
4557 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4558 free_mount_cb_data(data
);
4561 if (status
== RPC_STATUS_CANCEL
) {
4562 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4563 free_mount_cb_data(data
);
4567 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4568 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4569 rpc_set_error(rpc
, "Failed to disconnect\n");
4571 free_mount_cb_data(data
);
4574 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4576 struct mount_cb_data
*data
= private_data
;
4578 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4580 /* Dont want any more callbacks even if the socket is closed */
4581 rpc
->connect_cb
= NULL
;
4583 if (status
== RPC_STATUS_ERROR
) {
4584 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4585 free_mount_cb_data(data
);
4588 if (status
== RPC_STATUS_CANCEL
) {
4589 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4590 free_mount_cb_data(data
);
4594 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4595 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4596 free_mount_cb_data(data
);
4601 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4603 struct mount_cb_data
*data
;
4605 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4607 data
= malloc(sizeof(struct mount_cb_data
));
4611 memset(data
, 0, sizeof(struct mount_cb_data
));
4613 data
->private_data
= private_data
;
4614 data
->server
= strdup(server
);
4615 if (data
->server
== NULL
) {
4616 free_mount_cb_data(data
);
4619 if (rpc_connect_program_async(rpc
, data
->server
, MOUNT_PROGRAM
, MOUNT_V3
, mount_export_4_cb
, data
) != 0) {
4620 rpc_set_error(rpc
, "Failed to start connection");
4621 free_mount_cb_data(data
);
4628 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4630 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4634 const char *nfs_get_server(struct nfs_context
*nfs
) {
4638 const char *nfs_get_export(struct nfs_context
*nfs
) {
4642 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4643 return &nfs
->rootfh
;
4646 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {