2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * High level api to nfs filesystems
25 #include "aros_compat.h"
29 #include "win32_compat.h"
37 #define statvfs statfs
50 #ifdef HAVE_SYS_STATVFS_H
51 #include <sys/statvfs.h>
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
68 #include <sys/types.h>
71 #include "libnfs-zdr.h"
73 #include "libnfs-raw.h"
74 #include "libnfs-raw-mount.h"
75 #include "libnfs-raw-nfs.h"
76 #include "libnfs-raw-portmap.h"
77 #include "libnfs-private.h"
80 struct nfsdirent
*entries
;
81 struct nfsdirent
*current
;
91 struct rpc_context
*rpc
;
94 struct nfs_fh3 rootfh
;
100 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
102 while (nfsdir
->entries
) {
103 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
104 if (nfsdir
->entries
->name
!= NULL
) {
105 free(nfsdir
->entries
->name
);
107 free(nfsdir
->entries
);
108 nfsdir
->entries
= dirent
;
114 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
117 struct nfs_context
*nfs
;
119 char *saved_path
, *path
;
124 continue_func continue_cb
;
126 void (*free_continue_data
)(void *);
131 /* for multi-read/write calls. */
136 uint64_t start_offset
, max_offset
;
142 struct nfs_mcb_data
{
143 struct nfs_cb_data
*data
;
148 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
150 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
152 rpc_set_auth(nfs
->rpc
, auth
);
155 int nfs_get_fd(struct nfs_context
*nfs
)
157 return rpc_get_fd(nfs
->rpc
);
160 int nfs_queue_length(struct nfs_context
*nfs
)
162 return rpc_queue_length(nfs
->rpc
);
165 int nfs_which_events(struct nfs_context
*nfs
)
167 return rpc_which_events(nfs
->rpc
);
170 int nfs_service(struct nfs_context
*nfs
, int revents
)
172 return rpc_service(nfs
->rpc
, revents
);
175 char *nfs_get_error(struct nfs_context
*nfs
)
177 return rpc_get_error(nfs
->rpc
);
180 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
182 if (!strncmp(arg
, "tcp-syncnt", 10)) {
183 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
184 } else if (!strncmp(arg
, "uid", 3)) {
185 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
186 } else if (!strncmp(arg
, "gid", 3)) {
187 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
192 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
194 struct nfs_url
*urls
;
195 char *strp
, *flagsp
, *strp2
;
197 if (strncmp(url
, "nfs://", 6)) {
198 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
202 urls
= malloc(sizeof(struct nfs_url
));
204 rpc_set_error(nfs
->rpc
, "Out of memory");
208 memset(urls
, 0x00, sizeof(struct nfs_url
));
209 urls
->server
= strdup(url
+ 6);
210 if (urls
->server
== NULL
) {
211 nfs_destroy_url(urls
);
212 rpc_set_error(nfs
->rpc
, "Out of memory");
216 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
217 urls
->server
[0] == '?') {
219 flagsp
= strchr(urls
->server
, '?');
222 nfs_destroy_url(urls
);
223 rpc_set_error(nfs
->rpc
, "Invalid server string");
227 strp
= strchr(urls
->server
, '/');
230 flagsp
= strchr(urls
->server
, '?');
233 nfs_destroy_url(urls
);
234 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
238 urls
->path
= strdup(strp
);
239 if (urls
->path
== NULL
) {
240 nfs_destroy_url(urls
);
241 rpc_set_error(nfs
->rpc
, "Out of memory");
247 flagsp
= strchr(urls
->path
, '?');
251 strp
= strrchr(urls
->path
, '/');
254 flagsp
= strchr(urls
->path
, '?');
257 nfs_destroy_url(urls
);
258 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
261 urls
->file
= strdup(strp
);
262 if (urls
->path
== NULL
) {
263 nfs_destroy_url(urls
);
264 rpc_set_error(nfs
->rpc
, "Out of memory");
268 flagsp
= strchr(urls
->file
, '?');
275 if (urls
->file
&& !strlen(urls
->file
)) {
279 nfs_destroy_url(urls
);
280 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
285 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
287 flagsp
= strchr(strp
, '&');
291 strp2
= strchr(strp
, '=');
295 nfs_set_context_args(nfs
, strp
, strp2
);
299 if (urls
->server
&& strlen(urls
->server
) <= 1) {
307 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
309 return nfs_parse_url(nfs
, url
, 0, 0);
312 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
314 return nfs_parse_url(nfs
, url
, 1, 0);
317 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
319 return nfs_parse_url(nfs
, url
, 0, 1);
323 void nfs_destroy_url(struct nfs_url
*url
)
333 struct nfs_context
*nfs_init_context(void)
335 struct nfs_context
*nfs
;
337 nfs
= malloc(sizeof(struct nfs_context
));
341 memset(nfs
, 0, sizeof(struct nfs_context
));
343 nfs
->rpc
= rpc_init_context();
344 if (nfs
->rpc
== NULL
) {
349 nfs
->cwd
= strdup("/");
354 void nfs_destroy_context(struct nfs_context
*nfs
)
356 rpc_destroy_context(nfs
->rpc
);
374 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
375 free(nfs
->rootfh
.data
.data_val
);
376 nfs
->rootfh
.data
.data_val
= NULL
;
391 void free_rpc_cb_data(struct rpc_cb_data
*data
)
398 static void rpc_connect_program_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
400 struct rpc_cb_data
*data
= private_data
;
402 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
404 /* Dont want any more callbacks even if the socket is closed */
405 rpc
->connect_cb
= NULL
;
407 if (status
== RPC_STATUS_ERROR
) {
408 data
->cb(rpc
, status
, command_data
, data
->private_data
);
409 free_rpc_cb_data(data
);
412 if (status
== RPC_STATUS_CANCEL
) {
413 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
414 free_rpc_cb_data(data
);
418 data
->cb(rpc
, status
, NULL
, data
->private_data
);
419 free_rpc_cb_data(data
);
422 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
424 struct rpc_cb_data
*data
= private_data
;
426 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
428 /* Dont want any more callbacks even if the socket is closed */
429 rpc
->connect_cb
= NULL
;
431 if (status
== RPC_STATUS_ERROR
) {
432 data
->cb(rpc
, status
, command_data
, data
->private_data
);
433 free_rpc_cb_data(data
);
436 if (status
== RPC_STATUS_CANCEL
) {
437 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
438 free_rpc_cb_data(data
);
442 switch (data
->program
) {
444 if (rpc_mount3_null_async(rpc
, rpc_connect_program_5_cb
,
446 data
->cb(rpc
, status
, command_data
, data
->private_data
);
447 free_rpc_cb_data(data
);
452 if (rpc_nfs3_null_async(rpc
, rpc_connect_program_5_cb
,
454 data
->cb(rpc
, status
, command_data
, data
->private_data
);
455 free_rpc_cb_data(data
);
461 data
->cb(rpc
, status
, NULL
, data
->private_data
);
462 free_rpc_cb_data(data
);
465 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
467 struct rpc_cb_data
*data
= private_data
;
468 struct pmap3_getaddr_result
*gar
;
469 uint32_t rpc_port
= 0;
472 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
474 if (status
== RPC_STATUS_ERROR
) {
475 data
->cb(rpc
, status
, command_data
, data
->private_data
);
476 free_rpc_cb_data(data
);
479 if (status
== RPC_STATUS_CANCEL
) {
480 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
481 free_rpc_cb_data(data
);
485 switch (rpc
->s
.ss_family
) {
487 rpc_port
= *(uint32_t *)command_data
;
490 /* ouch. portmapper and ipv6 are not great */
492 if (gar
->addr
== NULL
) {
495 ptr
= strrchr(gar
->addr
, '.');
499 rpc_port
= atoi(ptr
+ 1);
501 ptr
= strrchr(gar
->addr
, '.');
505 rpc_port
+= 256 * atoi(ptr
+ 1);
509 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
510 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
511 free_rpc_cb_data(data
);
515 rpc_disconnect(rpc
, "normal disconnect");
516 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
517 data
->cb(rpc
, status
, command_data
, data
->private_data
);
518 free_rpc_cb_data(data
);
523 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
525 struct rpc_cb_data
*data
= private_data
;
526 struct pmap3_mapping map
;
528 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
530 if (status
== RPC_STATUS_ERROR
) {
531 data
->cb(rpc
, status
, command_data
, data
->private_data
);
532 free_rpc_cb_data(data
);
535 if (status
== RPC_STATUS_CANCEL
) {
536 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
537 free_rpc_cb_data(data
);
541 switch (rpc
->s
.ss_family
) {
543 if (rpc_pmap2_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
544 data
->cb(rpc
, status
, command_data
, data
->private_data
);
545 free_rpc_cb_data(data
);
550 map
.prog
=data
->program
;
551 map
.vers
=data
->version
;
555 if (rpc_pmap3_getaddr_async(rpc
, &map
, rpc_connect_program_3_cb
, private_data
) != 0) {
556 data
->cb(rpc
, status
, command_data
, data
->private_data
);
557 free_rpc_cb_data(data
);
564 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
566 struct rpc_cb_data
*data
= private_data
;
568 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
570 /* Dont want any more callbacks even if the socket is closed */
571 rpc
->connect_cb
= NULL
;
573 if (status
== RPC_STATUS_ERROR
) {
574 data
->cb(rpc
, status
, command_data
, data
->private_data
);
575 free_rpc_cb_data(data
);
578 if (status
== RPC_STATUS_CANCEL
) {
579 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
580 free_rpc_cb_data(data
);
584 switch (rpc
->s
.ss_family
) {
586 if (rpc_pmap2_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
587 data
->cb(rpc
, status
, command_data
, data
->private_data
);
588 free_rpc_cb_data(data
);
593 if (rpc_pmap3_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
594 data
->cb(rpc
, status
, command_data
, data
->private_data
);
595 free_rpc_cb_data(data
);
602 int rpc_connect_program_async(struct rpc_context
*rpc
, const char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
604 struct rpc_cb_data
*data
;
606 data
= malloc(sizeof(struct rpc_cb_data
));
610 memset(data
, 0, sizeof(struct rpc_cb_data
));
611 data
->server
= strdup(server
);
612 data
->program
= program
;
613 data
->version
= version
;
616 data
->private_data
= private_data
;
618 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
619 rpc_set_error(rpc
, "Failed to start connection");
620 free_rpc_cb_data(data
);
626 static void free_nfs_cb_data(struct nfs_cb_data
*data
)
628 if (data
->continue_data
!= NULL
) {
629 assert(data
->free_continue_data
);
630 data
->free_continue_data(data
->continue_data
);
633 free(data
->saved_path
);
634 free(data
->fh
.data
.data_val
);
644 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
646 struct nfs_cb_data
*data
= private_data
;
647 struct nfs_context
*nfs
= data
->nfs
;
649 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
651 if (status
== RPC_STATUS_ERROR
) {
652 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
653 free_nfs_cb_data(data
);
656 if (status
== RPC_STATUS_CANCEL
) {
657 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
658 free_nfs_cb_data(data
);
662 data
->cb(0, nfs
, NULL
, data
->private_data
);
663 free_nfs_cb_data(data
);
666 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
668 struct nfs_cb_data
*data
= private_data
;
669 struct nfs_context
*nfs
= data
->nfs
;
670 FSINFO3res
*res
= command_data
;
671 struct GETATTR3args args
;
673 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
675 if (status
== RPC_STATUS_ERROR
) {
676 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
677 free_nfs_cb_data(data
);
680 if (status
== RPC_STATUS_CANCEL
) {
681 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
682 free_nfs_cb_data(data
);
686 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
687 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
689 memset(&args
, 0, sizeof(GETATTR3args
));
690 args
.object
= nfs
->rootfh
;
692 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
693 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
694 free_nfs_cb_data(data
);
699 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
701 struct nfs_cb_data
*data
= private_data
;
702 struct nfs_context
*nfs
= data
->nfs
;
703 struct FSINFO3args args
;
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 args
.fsroot
= nfs
->rootfh
;
719 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
720 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
721 free_nfs_cb_data(data
);
726 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
728 struct nfs_cb_data
*data
= private_data
;
729 struct nfs_context
*nfs
= data
->nfs
;
732 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
734 if (status
== RPC_STATUS_ERROR
) {
735 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
736 free_nfs_cb_data(data
);
739 if (status
== RPC_STATUS_CANCEL
) {
740 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
741 free_nfs_cb_data(data
);
746 if (res
->fhs_status
!= MNT3_OK
) {
747 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
));
748 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
749 free_nfs_cb_data(data
);
753 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
754 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
755 if (nfs
->rootfh
.data
.data_val
== NULL
) {
756 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
757 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
758 free_nfs_cb_data(data
);
761 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
763 rpc_disconnect(rpc
, "normal disconnect");
765 if (rpc_connect_program_async(nfs
->rpc
, nfs
->server
, NFS_PROGRAM
, NFS_V3
, nfs_mount_8_cb
, data
) != 0) {
766 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
767 free_nfs_cb_data(data
);
771 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
772 rpc_set_autoreconnect(rpc
);
776 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
778 struct nfs_cb_data
*data
= private_data
;
779 struct nfs_context
*nfs
= data
->nfs
;
781 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
783 if (status
== RPC_STATUS_ERROR
) {
784 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
785 free_nfs_cb_data(data
);
788 if (status
== RPC_STATUS_CANCEL
) {
789 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
790 free_nfs_cb_data(data
);
794 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
795 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
796 free_nfs_cb_data(data
);
802 * Async call for mounting an nfs share and geting the root filehandle
804 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
806 struct nfs_cb_data
*data
;
807 char *new_server
, *new_export
;
809 data
= malloc(sizeof(struct nfs_cb_data
));
811 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
814 memset(data
, 0, sizeof(struct nfs_cb_data
));
815 new_server
= strdup(server
);
816 new_export
= strdup(export
);
817 if (nfs
->server
!= NULL
) {
820 nfs
->server
= new_server
;
821 if (nfs
->export
!= NULL
) {
824 nfs
->export
= new_export
;
827 data
->private_data
= private_data
;
829 if (rpc_connect_program_async(nfs
->rpc
, server
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_5_cb
, data
) != 0) {
830 rpc_set_error(nfs
->rpc
, "Failed to start connection");
831 free_nfs_cb_data(data
);
841 * Functions to first look up a path, component by component, and then finally call a specific function once
842 * the filehandle for the final component is found.
844 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
846 struct nfs_cb_data
*data
= private_data
;
847 struct nfs_context
*nfs
= data
->nfs
;
850 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
852 if (status
== RPC_STATUS_ERROR
) {
853 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
854 free_nfs_cb_data(data
);
857 if (status
== RPC_STATUS_CANCEL
) {
858 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
859 free_nfs_cb_data(data
);
864 if (res
->status
!= NFS3_OK
) {
865 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
));
866 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
867 free_nfs_cb_data(data
);
871 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
872 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
873 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
874 free_nfs_cb_data(data
);
879 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
884 while (*data
->path
== '/') {
889 slash
= strchr(path
, '/');
891 /* Clear slash so that path is a zero terminated string for
892 * the current path component. Set it back to '/' again later
893 * when we are finished referencing this component so that
894 * data->saved_path will still point to the full
898 data
->path
= slash
+1;
900 while (*data
->path
!= 0) {
906 data
->fh
.data
.data_len
= fh
->data
.data_len
;
907 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
908 if (data
->fh
.data
.data_val
== NULL
) {
909 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
910 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
911 free_nfs_cb_data(data
);
914 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
918 data
->continue_cb(nfs
, data
);
923 memset(&args
, 0, sizeof(LOOKUP3args
));
925 args
.what
.name
= path
;
927 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
928 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
929 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
930 free_nfs_cb_data(data
);
939 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
945 while ((str
= strstr(path
, "//"))) {
953 while ((str
= strstr(path
, "/./"))) {
961 if (!strncmp(path
, "/../", 4)) {
962 rpc_set_error(nfs
->rpc
,
963 "Absolute path starts with '/../' "
964 "during normalization");
969 if (path
[0] != '/') {
970 rpc_set_error(nfs
->rpc
,
971 "Absolute path does not start with '/'");
975 /* /string/../ -> / */
976 while ((str
= strstr(path
, "/../"))) {
979 if (!strncmp(path
, "/../", 4)) {
980 rpc_set_error(nfs
->rpc
,
981 "Absolute path starts with '/../' "
982 "during normalization");
987 while (*tmp
!= '/') {
991 while((*(tmp
++) = *(str
++)) != '\0')
998 if (path
[len
- 1] == '/') {
999 path
[len
- 1] = '\0';
1003 if (path
[0] == '\0') {
1004 rpc_set_error(nfs
->rpc
,
1005 "Absolute path became '' "
1006 "during normalization");
1012 if (!strcmp(&path
[len
- 2], "/.")) {
1013 path
[len
- 2] = '\0';
1018 /* ^/..$ -> error */
1019 if (!strcmp(path
, "/..")) {
1020 rpc_set_error(nfs
->rpc
,
1021 "Absolute path is '/..' "
1022 "during normalization");
1026 /* /string/..$ -> / */
1028 if (!strcmp(&path
[len
- 3], "/..")) {
1029 char *tmp
= &path
[len
- 3];
1030 while (*--tmp
!= '/')
1039 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
)
1041 struct nfs_cb_data
*data
;
1043 if (path
[0] == '\0') {
1047 data
= malloc(sizeof(struct nfs_cb_data
));
1049 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1050 "nfs_cb_data structure");
1051 if (free_continue_data
)
1052 free_continue_data(continue_data
);
1055 memset(data
, 0, sizeof(struct nfs_cb_data
));
1058 data
->continue_cb
= continue_cb
;
1059 data
->continue_data
= continue_data
;
1060 data
->free_continue_data
= free_continue_data
;
1061 data
->continue_int
= continue_int
;
1062 data
->private_data
= private_data
;
1063 if (path
[0] == '/') {
1064 data
->saved_path
= strdup(path
);
1066 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1067 if (data
->saved_path
== NULL
) {
1068 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1069 "malloc path string");
1070 free_nfs_cb_data(data
);
1073 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1076 if (data
->saved_path
== NULL
) {
1077 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1078 free_nfs_cb_data(data
);
1081 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1082 free_nfs_cb_data(data
);
1086 data
->path
= data
->saved_path
;
1088 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1089 /* return 0 here since the callback will be invoked if there is a failure */
1102 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1105 struct nfs_cb_data
*data
= private_data
;
1106 struct nfs_context
*nfs
= data
->nfs
;
1113 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1115 if (status
== RPC_STATUS_ERROR
) {
1116 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1117 free_nfs_cb_data(data
);
1120 if (status
== RPC_STATUS_CANCEL
) {
1121 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1122 free_nfs_cb_data(data
);
1127 if (res
->status
!= NFS3_OK
) {
1128 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
));
1129 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1130 free_nfs_cb_data(data
);
1135 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1136 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1137 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1138 st
.st_mode
|= S_IFDIR
;
1140 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1141 st
.st_mode
|= S_IFREG
;
1143 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1144 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1145 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1147 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1149 st
.st_blksize
= 4096;
1150 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1152 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1153 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1154 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1156 data
->cb(0, nfs
, &st
, data
->private_data
);
1157 free_nfs_cb_data(data
);
1160 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1162 struct GETATTR3args args
;
1164 memset(&args
, 0, sizeof(GETATTR3args
));
1165 args
.object
= data
->fh
;
1167 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1168 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1169 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1170 free_nfs_cb_data(data
);
1176 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1178 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1179 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1188 * Async nfs_stat64()
1190 static void nfs_stat64_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1193 struct nfs_cb_data
*data
= private_data
;
1194 struct nfs_context
*nfs
= data
->nfs
;
1195 struct nfs_stat_64 st
;
1197 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1199 if (status
== RPC_STATUS_ERROR
) {
1200 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1201 free_nfs_cb_data(data
);
1204 if (status
== RPC_STATUS_CANCEL
) {
1205 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1206 free_nfs_cb_data(data
);
1211 if (res
->status
!= NFS3_OK
) {
1212 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
));
1213 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1214 free_nfs_cb_data(data
);
1219 st
.nfs_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1220 st
.nfs_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1221 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1222 st
.nfs_mode
|= S_IFDIR
;
1224 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1225 st
.nfs_mode
|= S_IFREG
;
1227 st
.nfs_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1228 st
.nfs_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1229 st
.nfs_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1231 st
.nfs_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1232 st
.nfs_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1233 st
.nfs_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1234 st
.nfs_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1236 data
->cb(0, nfs
, &st
, data
->private_data
);
1237 free_nfs_cb_data(data
);
1240 static int nfs_stat64_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1242 struct GETATTR3args args
;
1244 memset(&args
, 0, sizeof(GETATTR3args
));
1245 args
.object
= data
->fh
;
1247 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
1248 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1249 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1250 free_nfs_cb_data(data
);
1256 int nfs_stat64_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1258 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat64_continue_internal
, NULL
, NULL
, 0) != 0) {
1259 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1269 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1271 struct nfs_cb_data
*data
= private_data
;
1272 struct nfs_context
*nfs
= data
->nfs
;
1273 struct nfsfh
*nfsfh
;
1276 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1278 if (status
== RPC_STATUS_ERROR
) {
1279 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1280 free_nfs_cb_data(data
);
1283 if (status
== RPC_STATUS_CANCEL
) {
1284 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1285 free_nfs_cb_data(data
);
1290 if (res
->status
!= NFS3_OK
) {
1291 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1292 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1293 free_nfs_cb_data(data
);
1297 nfsfh
= malloc(sizeof(struct nfsfh
));
1298 if (nfsfh
== NULL
) {
1299 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1300 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1301 free_nfs_cb_data(data
);
1304 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1306 if (data
->continue_int
& O_SYNC
) {
1310 /* steal the filehandle */
1311 nfsfh
->fh
= data
->fh
;
1312 data
->fh
.data
.data_val
= NULL
;
1314 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1315 free_nfs_cb_data(data
);
1318 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1321 struct nfs_cb_data
*data
= private_data
;
1322 struct nfs_context
*nfs
= data
->nfs
;
1323 struct nfsfh
*nfsfh
;
1324 unsigned int nfsmode
= 0;
1326 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1328 if (status
== RPC_STATUS_ERROR
) {
1329 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1330 free_nfs_cb_data(data
);
1333 if (status
== RPC_STATUS_CANCEL
) {
1334 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1335 free_nfs_cb_data(data
);
1340 if (res
->status
!= NFS3_OK
) {
1341 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
));
1342 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1343 free_nfs_cb_data(data
);
1347 if (data
->continue_int
& O_WRONLY
) {
1348 nfsmode
|= ACCESS3_MODIFY
;
1350 if (data
->continue_int
& O_RDWR
) {
1351 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1353 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1354 nfsmode
|= ACCESS3_READ
;
1358 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1359 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1360 nfsmode
&ACCESS3_READ
?'r':'-',
1361 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1362 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1363 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1364 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1365 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1366 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1367 free_nfs_cb_data(data
);
1371 /* Try to truncate it if we were requested to */
1372 if ((data
->continue_int
& O_TRUNC
) &&
1373 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1376 memset(&args
, 0, sizeof(SETATTR3args
));
1377 args
.object
= data
->fh
;
1378 args
.new_attributes
.size
.set_it
= 1;
1379 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1381 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1383 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1384 "SETATTR call for %s", data
->path
);
1385 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1386 data
->private_data
);
1387 free_nfs_cb_data(data
);
1393 nfsfh
= malloc(sizeof(struct nfsfh
));
1394 if (nfsfh
== NULL
) {
1395 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1396 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1397 free_nfs_cb_data(data
);
1400 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1402 if (data
->continue_int
& O_SYNC
) {
1406 /* steal the filehandle */
1407 nfsfh
->fh
= data
->fh
;
1408 data
->fh
.data
.data_val
= NULL
;
1410 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1411 free_nfs_cb_data(data
);
1414 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1419 if (data
->continue_int
& O_WRONLY
) {
1420 nfsmode
|= ACCESS3_MODIFY
;
1422 if (data
->continue_int
& O_RDWR
) {
1423 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1425 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1426 nfsmode
|= ACCESS3_READ
;
1429 memset(&args
, 0, sizeof(ACCESS3args
));
1430 args
.object
= data
->fh
;
1431 args
.access
= nfsmode
;
1433 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1434 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1435 "call for %s", data
->path
);
1436 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1437 data
->private_data
);
1438 free_nfs_cb_data(data
);
1444 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1446 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1447 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1458 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1460 /* steal saved_path */
1462 nfs
->cwd
= data
->saved_path
;
1463 data
->saved_path
= NULL
;
1465 data
->cb(0, nfs
, NULL
, data
->private_data
);
1466 free_nfs_cb_data(data
);
1471 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1473 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1474 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1485 static void nfs_fill_READ3args(READ3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
)
1487 memset(args
, 0, sizeof(READ3args
));
1488 args
->file
= fh
->fh
;
1489 args
->offset
= offset
;
1490 args
->count
= count
;
1493 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1495 struct nfs_mcb_data
*mdata
= private_data
;
1496 struct nfs_cb_data
*data
= mdata
->data
;
1497 struct nfs_context
*nfs
= data
->nfs
;
1500 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1504 if (status
== RPC_STATUS_ERROR
) {
1505 /* flag the failure but do not invoke callback until we have received all responses */
1508 if (status
== RPC_STATUS_CANCEL
) {
1509 /* flag the cancellation but do not invoke callback until we have received all responses */
1513 if (status
== RPC_STATUS_SUCCESS
) {
1515 if (res
->status
!= NFS3_OK
) {
1516 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1519 /* if we have more than one call or we have received a short read we need a reassembly buffer */
1520 if (data
->num_calls
|| (res
->READ3res_u
.resok
.count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
)) {
1521 if (data
->buffer
== NULL
) {
1522 data
->buffer
= malloc(data
->request_size
);
1523 if (data
->buffer
== NULL
) {
1524 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data
->request_size
);
1529 if (res
->READ3res_u
.resok
.count
> 0) {
1530 if (res
->READ3res_u
.resok
.count
<= mdata
->count
) {
1531 /* copy data into reassembly buffer if we have one */
1532 if (data
->buffer
!= NULL
) {
1533 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1535 if (data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1536 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1539 rpc_set_error(nfs
->rpc
, "NFS: Read overflow. Server has sent more data than requested!");
1543 /* check if we have received a short read */
1544 if (res
->READ3res_u
.resok
.count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
) {
1545 if (res
->READ3res_u
.resok
.count
== 0) {
1546 rpc_set_error(nfs
->rpc
, "NFS: Read failed. No bytes read and not at EOF!");
1549 /* reissue reminder of this read request */
1551 mdata
->offset
+= res
->READ3res_u
.resok
.count
;
1552 mdata
->count
-= res
->READ3res_u
.resok
.count
;
1553 nfs_fill_READ3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
);
1554 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) == 0) {
1558 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1568 if (data
->num_calls
> 0) {
1569 /* still waiting for more replies */
1572 if (data
->oom
!= 0) {
1573 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1574 free_nfs_cb_data(data
);
1577 if (data
->error
!= 0) {
1578 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1579 free_nfs_cb_data(data
);
1582 if (data
->cancel
!= 0) {
1583 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1584 free_nfs_cb_data(data
);
1588 data
->nfsfh
->offset
= data
->max_offset
;
1590 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1592 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1595 free_nfs_cb_data(data
);
1598 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1600 struct nfs_cb_data
*data
;
1602 data
= malloc(sizeof(struct nfs_cb_data
));
1604 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1607 memset(data
, 0, sizeof(struct nfs_cb_data
));
1610 data
->private_data
= private_data
;
1611 data
->nfsfh
= nfsfh
;
1612 data
->request_size
= count
;
1614 nfsfh
->offset
= offset
;
1616 assert(data
->num_calls
== 0);
1618 /* chop requests into chunks of at most READMAX bytes if necessary.
1619 * we send all reads in parallel so that performance is still good.
1621 data
->max_offset
= offset
;
1622 data
->start_offset
= offset
;
1625 uint64_t readcount
= count
;
1626 struct nfs_mcb_data
*mdata
;
1629 if (readcount
> nfs_get_readmax(nfs
)) {
1630 readcount
= nfs_get_readmax(nfs
);
1633 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1634 if (mdata
== NULL
) {
1635 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1636 if (data
->num_calls
== 0) {
1637 free_nfs_cb_data(data
);
1643 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1645 mdata
->offset
= offset
;
1646 mdata
->count
= readcount
;
1648 nfs_fill_READ3args(&args
, nfsfh
, offset
, readcount
);
1650 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1651 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1653 if (data
->num_calls
== 0) {
1654 free_nfs_cb_data(data
);
1662 offset
+= readcount
;
1664 } while (count
> 0);
1672 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1674 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1682 static void nfs_fill_WRITE3args (WRITE3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
,
1685 memset(args
, 0, sizeof(WRITE3args
));
1686 args
->file
= fh
->fh
;
1687 args
->offset
= offset
;
1688 args
->count
= count
;
1689 args
->stable
= fh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1690 args
->data
.data_len
= count
;
1691 args
->data
.data_val
= buf
;
1694 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1696 struct nfs_mcb_data
*mdata
= private_data
;
1697 struct nfs_cb_data
*data
= mdata
->data
;
1698 struct nfs_context
*nfs
= data
->nfs
;
1701 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1705 if (status
== RPC_STATUS_ERROR
) {
1706 /* flag the failure but do not invoke callback until we have received all responses */
1709 if (status
== RPC_STATUS_CANCEL
) {
1710 /* flag the cancellation but do not invoke callback until we have received all responses */
1714 if (status
== RPC_STATUS_SUCCESS
) {
1716 if (res
->status
!= NFS3_OK
) {
1717 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1720 if (res
->WRITE3res_u
.resok
.count
< mdata
->count
) {
1721 if (res
->WRITE3res_u
.resok
.count
== 0) {
1722 rpc_set_error(nfs
->rpc
, "NFS: Write failed. No bytes written!");
1725 /* reissue reminder of this write request */
1727 mdata
->offset
+= res
->WRITE3res_u
.resok
.count
;
1728 mdata
->count
-= res
->WRITE3res_u
.resok
.count
;
1729 nfs_fill_WRITE3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
,
1730 &data
->usrbuf
[mdata
->offset
- data
->start_offset
]);
1731 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) == 0) {
1735 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1740 if (res
->WRITE3res_u
.resok
.count
> 0) {
1741 if (data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1742 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1750 if (data
->num_calls
> 0) {
1751 /* still waiting for more replies */
1754 if (data
->oom
!= 0) {
1755 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1756 free_nfs_cb_data(data
);
1759 if (data
->error
!= 0) {
1760 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1761 free_nfs_cb_data(data
);
1764 if (data
->cancel
!= 0) {
1765 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1766 free_nfs_cb_data(data
);
1770 data
->nfsfh
->offset
= data
->max_offset
;
1771 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1773 free_nfs_cb_data(data
);
1777 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
)
1779 struct nfs_cb_data
*data
;
1781 data
= malloc(sizeof(struct nfs_cb_data
));
1783 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1786 memset(data
, 0, sizeof(struct nfs_cb_data
));
1789 data
->private_data
= private_data
;
1790 data
->nfsfh
= nfsfh
;
1793 nfsfh
->offset
= offset
;
1795 /* hello, clang-analyzer */
1796 assert(data
->num_calls
== 0);
1798 /* chop requests into chunks of at most WRITEMAX bytes if necessary.
1799 * we send all writes in parallel so that performance is still good.
1801 data
->max_offset
= offset
;
1802 data
->start_offset
= offset
;
1805 uint64_t writecount
= count
;
1806 struct nfs_mcb_data
*mdata
;
1809 if (writecount
> nfs_get_writemax(nfs
)) {
1810 writecount
= nfs_get_writemax(nfs
);
1813 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1814 if (mdata
== NULL
) {
1815 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1816 if (data
->num_calls
== 0) {
1817 free_nfs_cb_data(data
);
1823 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1825 mdata
->offset
= offset
;
1826 mdata
->count
= writecount
;
1828 nfs_fill_WRITE3args(&args
, nfsfh
, offset
, writecount
, &buf
[offset
- data
->start_offset
]);
1830 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1831 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1833 if (data
->num_calls
== 0) {
1834 free_nfs_cb_data(data
);
1841 count
-= writecount
;
1842 offset
+= writecount
;
1844 } while (count
> 0);
1852 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1854 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1864 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1866 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1867 free(nfsfh
->fh
.data
.data_val
);
1868 nfsfh
->fh
.data
.data_val
= NULL
;
1872 cb(0, nfs
, NULL
, private_data
);
1883 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1885 struct nfs_cb_data
*data
;
1886 struct GETATTR3args args
;
1888 data
= malloc(sizeof(struct nfs_cb_data
));
1890 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1893 memset(data
, 0, sizeof(struct nfs_cb_data
));
1896 data
->private_data
= private_data
;
1898 memset(&args
, 0, sizeof(GETATTR3args
));
1899 args
.object
= nfsfh
->fh
;
1901 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1902 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1903 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1904 free_nfs_cb_data(data
);
1915 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1917 struct nfs_cb_data
*data
= private_data
;
1918 struct nfs_context
*nfs
= data
->nfs
;
1921 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1923 if (status
== RPC_STATUS_ERROR
) {
1924 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1925 free_nfs_cb_data(data
);
1928 if (status
== RPC_STATUS_CANCEL
) {
1929 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1930 free_nfs_cb_data(data
);
1935 if (res
->status
!= NFS3_OK
) {
1936 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1937 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1938 free_nfs_cb_data(data
);
1942 data
->cb(0, nfs
, NULL
, data
->private_data
);
1943 free_nfs_cb_data(data
);
1946 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1948 struct nfs_cb_data
*data
;
1949 struct COMMIT3args args
;
1951 data
= malloc(sizeof(struct nfs_cb_data
));
1953 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1956 memset(data
, 0, sizeof(struct nfs_cb_data
));
1959 data
->private_data
= private_data
;
1961 args
.file
= nfsfh
->fh
;
1964 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1965 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1966 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1967 free_nfs_cb_data(data
);
1979 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1981 struct nfs_cb_data
*data
= private_data
;
1982 struct nfs_context
*nfs
= data
->nfs
;
1985 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1987 if (status
== RPC_STATUS_ERROR
) {
1988 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1989 free_nfs_cb_data(data
);
1992 if (status
== RPC_STATUS_CANCEL
) {
1993 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1994 free_nfs_cb_data(data
);
1999 if (res
->status
!= NFS3_OK
) {
2000 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2001 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2002 free_nfs_cb_data(data
);
2006 data
->cb(0, nfs
, NULL
, data
->private_data
);
2007 free_nfs_cb_data(data
);
2010 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
2012 struct nfs_cb_data
*data
;
2015 data
= malloc(sizeof(struct nfs_cb_data
));
2017 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2020 memset(data
, 0, sizeof(struct nfs_cb_data
));
2023 data
->private_data
= private_data
;
2025 memset(&args
, 0, sizeof(SETATTR3args
));
2026 args
.object
= nfsfh
->fh
;
2027 args
.new_attributes
.size
.set_it
= 1;
2028 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2030 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2031 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2032 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2033 free_nfs_cb_data(data
);
2043 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2045 uint64_t offset
= data
->continue_int
;
2048 nfsfh
.fh
= data
->fh
;
2050 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2051 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2052 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2053 free_nfs_cb_data(data
);
2056 free_nfs_cb_data(data
);
2060 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2066 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2067 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2080 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2083 struct nfs_cb_data
*data
= private_data
;
2084 struct nfs_context
*nfs
= data
->nfs
;
2085 char *str
= data
->continue_data
;
2087 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2089 str
= &str
[strlen(str
) + 1];
2091 if (status
== RPC_STATUS_ERROR
) {
2092 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2093 free_nfs_cb_data(data
);
2096 if (status
== RPC_STATUS_CANCEL
) {
2097 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2098 free_nfs_cb_data(data
);
2103 if (res
->status
!= NFS3_OK
) {
2104 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
));
2105 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2106 free_nfs_cb_data(data
);
2110 data
->cb(0, nfs
, NULL
, data
->private_data
);
2111 free_nfs_cb_data(data
);
2114 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2116 char *str
= data
->continue_data
;
2119 str
= &str
[strlen(str
) + 1];
2121 memset(&args
, 0, sizeof(MKDIR3args
));
2122 args
.where
.dir
= data
->fh
;
2123 args
.where
.name
= str
;
2124 args
.attributes
.mode
.set_it
= 1;
2125 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2127 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2128 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2129 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2130 free_nfs_cb_data(data
);
2136 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2141 new_path
= strdup(path
);
2142 if (new_path
== NULL
) {
2143 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2147 ptr
= strrchr(new_path
, '/');
2150 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2155 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2156 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2157 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2171 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2174 struct nfs_cb_data
*data
= private_data
;
2175 struct nfs_context
*nfs
= data
->nfs
;
2176 char *str
= data
->continue_data
;
2178 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2180 str
= &str
[strlen(str
) + 1];
2182 if (status
== RPC_STATUS_ERROR
) {
2183 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2184 free_nfs_cb_data(data
);
2187 if (status
== RPC_STATUS_CANCEL
) {
2188 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2189 free_nfs_cb_data(data
);
2194 if (res
->status
!= NFS3_OK
) {
2195 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
));
2196 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2197 free_nfs_cb_data(data
);
2201 data
->cb(0, nfs
, NULL
, data
->private_data
);
2202 free_nfs_cb_data(data
);
2205 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2207 char *str
= data
->continue_data
;
2210 str
= &str
[strlen(str
) + 1];
2212 args
.object
.dir
= data
->fh
;
2213 args
.object
.name
= str
;
2214 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2215 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2216 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2217 free_nfs_cb_data(data
);
2223 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2228 new_path
= strdup(path
);
2229 if (new_path
== NULL
) {
2230 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2234 ptr
= strrchr(new_path
, '/');
2237 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2242 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2243 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2244 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2257 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2260 struct nfs_cb_data
*data
= private_data
;
2261 struct nfs_context
*nfs
= data
->nfs
;
2262 struct nfsfh
*nfsfh
;
2263 char *str
= data
->continue_data
;
2265 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2267 if (status
== RPC_STATUS_ERROR
) {
2268 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2269 free_nfs_cb_data(data
);
2272 if (status
== RPC_STATUS_CANCEL
) {
2273 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2274 free_nfs_cb_data(data
);
2278 str
= &str
[strlen(str
) + 1];
2280 if (res
->status
!= NFS3_OK
) {
2281 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
));
2282 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2287 nfsfh
= malloc(sizeof(struct nfsfh
));
2288 if (nfsfh
== NULL
) {
2289 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2290 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2291 free_nfs_cb_data(data
);
2294 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2296 /* copy the filehandle */
2297 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2298 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2299 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2301 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2302 free_nfs_cb_data(data
);
2307 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2310 struct nfs_cb_data
*data
= private_data
;
2311 struct nfs_context
*nfs
= data
->nfs
;
2312 char *str
= data
->continue_data
;
2315 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2317 if (status
== RPC_STATUS_ERROR
) {
2318 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2319 free_nfs_cb_data(data
);
2322 if (status
== RPC_STATUS_CANCEL
) {
2323 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2324 free_nfs_cb_data(data
);
2328 str
= &str
[strlen(str
) + 1];
2330 if (res
->status
!= NFS3_OK
) {
2331 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
));
2332 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2333 free_nfs_cb_data(data
);
2337 memset(&args
, 0, sizeof(LOOKUP3args
));
2338 args
.what
.dir
= data
->fh
;
2339 args
.what
.name
= str
;
2341 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2342 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2343 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2344 free_nfs_cb_data(data
);
2350 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2352 char *str
= data
->continue_data
;
2355 str
= &str
[strlen(str
) + 1];
2357 memset(&args
, 0, sizeof(CREATE3args
));
2358 args
.where
.dir
= data
->fh
;
2359 args
.where
.name
= str
;
2360 args
.how
.mode
= UNCHECKED
;
2361 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2362 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2364 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2365 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2366 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2367 free_nfs_cb_data(data
);
2373 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2378 new_path
= strdup(path
);
2379 if (new_path
== NULL
) {
2380 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2384 ptr
= strrchr(new_path
, '/');
2386 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2392 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2393 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2394 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2407 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2410 struct nfs_cb_data
*data
= private_data
;
2411 struct nfs_context
*nfs
= data
->nfs
;
2412 char *str
= data
->continue_data
;
2414 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2416 str
= &str
[strlen(str
) + 1];
2418 if (status
== RPC_STATUS_ERROR
) {
2419 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2420 free_nfs_cb_data(data
);
2423 if (status
== RPC_STATUS_CANCEL
) {
2424 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2425 free_nfs_cb_data(data
);
2430 if (res
->status
!= NFS3_OK
) {
2431 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
));
2432 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2433 free_nfs_cb_data(data
);
2437 data
->cb(0, nfs
, NULL
, data
->private_data
);
2438 free_nfs_cb_data(data
);
2441 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2443 char *str
= data
->continue_data
;
2444 struct REMOVE3args args
;
2446 str
= &str
[strlen(str
) + 1];
2448 args
.object
.dir
= data
->fh
;
2449 args
.object
.name
= str
;
2450 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2451 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2452 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2453 free_nfs_cb_data(data
);
2459 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2464 new_path
= strdup(path
);
2465 if (new_path
== NULL
) {
2466 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2470 ptr
= strrchr(new_path
, '/');
2473 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2478 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2479 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2480 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2491 struct mknod_cb_data
{
2498 static void free_mknod_cb_data(void *ptr
)
2500 struct mknod_cb_data
*data
= ptr
;
2506 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2509 struct nfs_cb_data
*data
= private_data
;
2510 struct nfs_context
*nfs
= data
->nfs
;
2511 char *str
= data
->continue_data
;
2513 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2515 str
= &str
[strlen(str
) + 1];
2517 if (status
== RPC_STATUS_ERROR
) {
2518 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2519 free_nfs_cb_data(data
);
2522 if (status
== RPC_STATUS_CANCEL
) {
2523 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2524 free_nfs_cb_data(data
);
2529 if (res
->status
!= NFS3_OK
) {
2530 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
));
2531 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2532 free_nfs_cb_data(data
);
2536 data
->cb(0, nfs
, NULL
, data
->private_data
);
2537 free_nfs_cb_data(data
);
2540 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2542 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2543 char *str
= cb_data
->path
;
2546 str
= &str
[strlen(str
) + 1];
2548 args
.where
.dir
= data
->fh
;
2549 args
.where
.name
= str
;
2550 switch (cb_data
->mode
& S_IFMT
) {
2552 args
.what
.type
= NF3CHR
;
2553 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2554 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
);
2555 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2556 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2559 args
.what
.type
= NF3BLK
;
2560 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2561 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
);
2562 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2563 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2565 args
.what
.type
= NF3SOCK
;
2566 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2567 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
);
2570 args
.what
.type
= NF3FIFO
;
2571 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2572 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
);
2575 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2576 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2577 free_nfs_cb_data(data
);
2581 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2582 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2583 free_nfs_cb_data(data
);
2589 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2592 struct mknod_cb_data
*cb_data
;
2594 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2595 if (cb_data
== NULL
) {
2596 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2600 cb_data
->path
= strdup(path
);
2601 if (cb_data
->path
== NULL
) {
2602 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2607 ptr
= strrchr(cb_data
->path
, '/');
2609 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2610 free_mknod_cb_data(cb_data
);
2615 cb_data
->mode
= mode
;
2616 cb_data
->major
= major(dev
);
2617 cb_data
->minor
= minor(dev
);
2619 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2620 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2621 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2632 /* ReadDirPlus Emulation Callback data */
2633 struct rdpe_cb_data
{
2636 struct nfs_cb_data
*data
;
2639 /* ReadDirPlus Emulation LOOKUP Callback data */
2640 struct rdpe_lookup_cb_data
{
2641 struct rdpe_cb_data
*rdpe_cb_data
;
2642 struct nfsdirent
*nfsdirent
;
2645 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2646 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2648 LOOKUP3res
*res
= command_data
;
2649 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2650 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2651 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2652 struct nfsdir
*nfsdir
= data
->continue_data
;
2653 struct nfs_context
*nfs
= data
->nfs
;
2654 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2656 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2658 free(rdpe_lookup_cb_data
);
2660 rdpe_cb_data
->getattrcount
--;
2662 if (status
== RPC_STATUS_ERROR
) {
2663 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2664 "failed with RPC_STATUS_ERROR");
2665 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2667 if (status
== RPC_STATUS_CANCEL
) {
2668 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2669 "failed with RPC_STATUS_CANCEL");
2670 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2672 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2673 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2674 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2676 nfsdirent
->type
= attributes
->type
;
2677 nfsdirent
->mode
= attributes
->mode
;
2678 nfsdirent
->size
= attributes
->size
;
2680 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2681 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2682 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2683 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2684 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2685 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2686 nfsdirent
->uid
= attributes
->uid
;
2687 nfsdirent
->gid
= attributes
->gid
;
2691 if (rdpe_cb_data
->getattrcount
== 0) {
2692 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2693 rpc_set_error(nfs
->rpc
, "READDIRPLUS emulation "
2694 "failed: %s", rpc_get_error(rpc
));
2695 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
2696 data
->private_data
);
2697 nfs_free_nfsdir(nfsdir
);
2699 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2703 data
->continue_data
= NULL
;
2704 free_nfs_cb_data(data
);
2708 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2710 READDIR3res
*res
= command_data
;
2711 struct nfs_cb_data
*data
= private_data
;
2712 struct nfs_context
*nfs
= data
->nfs
;
2713 struct nfsdir
*nfsdir
= data
->continue_data
;
2714 struct nfsdirent
*nfsdirent
;
2715 struct entry3
*entry
;
2716 uint64_t cookie
= 0;
2717 struct rdpe_cb_data
*rdpe_cb_data
;
2719 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2721 if (status
== RPC_STATUS_ERROR
) {
2722 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2723 nfs_free_nfsdir(nfsdir
);
2724 data
->continue_data
= NULL
;
2725 free_nfs_cb_data(data
);
2729 if (status
== RPC_STATUS_CANCEL
) {
2730 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2731 nfs_free_nfsdir(nfsdir
);
2732 data
->continue_data
= NULL
;
2733 free_nfs_cb_data(data
);
2737 if (res
->status
!= NFS3_OK
) {
2738 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
));
2739 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2740 nfs_free_nfsdir(nfsdir
);
2741 data
->continue_data
= NULL
;
2742 free_nfs_cb_data(data
);
2746 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2747 while (entry
!= NULL
) {
2748 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2749 if (nfsdirent
== NULL
) {
2750 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2751 nfs_free_nfsdir(nfsdir
);
2752 data
->continue_data
= NULL
;
2753 free_nfs_cb_data(data
);
2756 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2757 nfsdirent
->name
= strdup(entry
->name
);
2758 if (nfsdirent
->name
== NULL
) {
2759 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2761 nfs_free_nfsdir(nfsdir
);
2762 data
->continue_data
= NULL
;
2763 free_nfs_cb_data(data
);
2766 nfsdirent
->inode
= entry
->fileid
;
2768 nfsdirent
->next
= nfsdir
->entries
;
2769 nfsdir
->entries
= nfsdirent
;
2771 cookie
= entry
->cookie
;
2772 entry
= entry
->nextentry
;
2775 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2778 args
.dir
= data
->fh
;
2779 args
.cookie
= cookie
;
2780 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2783 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2784 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2785 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2786 nfs_free_nfsdir(nfsdir
);
2787 data
->continue_data
= NULL
;
2788 free_nfs_cb_data(data
);
2794 /* steal the dirhandle */
2795 nfsdir
->current
= nfsdir
->entries
;
2797 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2798 rdpe_cb_data
->getattrcount
= 0;
2799 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2800 rdpe_cb_data
->data
= data
;
2801 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2802 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2805 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2806 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2807 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2809 memset(&args
, 0, sizeof(LOOKUP3args
));
2810 args
.what
.dir
= data
->fh
;
2811 args
.what
.name
= nfsdirent
->name
;
2813 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2814 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2816 /* if we have already commands in flight, we cant just stop, we have to wait for the
2817 * commands in flight to complete
2819 if (rdpe_cb_data
->getattrcount
> 0) {
2820 nfs_free_nfsdir(nfsdir
);
2821 data
->continue_data
= NULL
;
2822 free_nfs_cb_data(data
);
2823 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2824 free(rdpe_lookup_cb_data
);
2828 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2829 nfs_free_nfsdir(nfsdir
);
2830 data
->continue_data
= NULL
;
2831 free_nfs_cb_data(data
);
2832 free(rdpe_lookup_cb_data
);
2836 rdpe_cb_data
->getattrcount
++;
2840 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2842 READDIRPLUS3res
*res
= command_data
;
2843 struct nfs_cb_data
*data
= private_data
;
2844 struct nfs_context
*nfs
= data
->nfs
;
2845 struct nfsdir
*nfsdir
= data
->continue_data
;
2846 struct entryplus3
*entry
;
2847 uint64_t cookie
= 0;
2849 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2851 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2854 args
.dir
= data
->fh
;
2855 args
.cookie
= cookie
;
2856 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2859 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2860 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2861 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2862 nfs_free_nfsdir(nfsdir
);
2863 data
->continue_data
= NULL
;
2864 free_nfs_cb_data(data
);
2870 if (status
== RPC_STATUS_CANCEL
) {
2871 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2872 nfs_free_nfsdir(nfsdir
);
2873 data
->continue_data
= NULL
;
2874 free_nfs_cb_data(data
);
2878 if (res
->status
!= NFS3_OK
) {
2879 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
));
2880 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2881 nfs_free_nfsdir(nfsdir
);
2882 data
->continue_data
= NULL
;
2883 free_nfs_cb_data(data
);
2887 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2888 while (entry
!= NULL
) {
2889 struct nfsdirent
*nfsdirent
;
2891 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2892 if (nfsdirent
== NULL
) {
2893 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2894 nfs_free_nfsdir(nfsdir
);
2895 data
->continue_data
= NULL
;
2896 free_nfs_cb_data(data
);
2899 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2900 nfsdirent
->name
= strdup(entry
->name
);
2901 if (nfsdirent
->name
== NULL
) {
2902 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2904 nfs_free_nfsdir(nfsdir
);
2905 data
->continue_data
= NULL
;
2906 free_nfs_cb_data(data
);
2909 nfsdirent
->inode
= entry
->fileid
;
2910 if (entry
->name_attributes
.attributes_follow
) {
2911 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2912 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2913 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2915 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2916 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2917 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2918 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2919 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2920 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2921 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2922 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2925 nfsdirent
->next
= nfsdir
->entries
;
2926 nfsdir
->entries
= nfsdirent
;
2928 cookie
= entry
->cookie
;
2929 entry
= entry
->nextentry
;
2932 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2933 READDIRPLUS3args args
;
2935 args
.dir
= data
->fh
;
2936 args
.cookie
= cookie
;
2937 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2938 args
.dircount
= 8192;
2939 args
.maxcount
= 8192;
2941 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2942 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2943 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2944 nfs_free_nfsdir(nfsdir
);
2945 data
->continue_data
= NULL
;
2946 free_nfs_cb_data(data
);
2952 /* steal the dirhandle */
2953 data
->continue_data
= NULL
;
2954 nfsdir
->current
= nfsdir
->entries
;
2956 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2957 free_nfs_cb_data(data
);
2960 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2962 READDIRPLUS3args args
;
2964 args
.dir
= data
->fh
;
2966 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2967 args
.dircount
= 8192;
2968 args
.maxcount
= 8192;
2969 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2970 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2971 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2972 free_nfs_cb_data(data
);
2978 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2980 struct nfsdir
*nfsdir
;
2982 nfsdir
= malloc(sizeof(struct nfsdir
));
2983 if (nfsdir
== NULL
) {
2984 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2987 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2989 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2990 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2998 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3000 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
3002 if (nfsdir
->current
!= NULL
) {
3003 nfsdir
->current
= nfsdir
->current
->next
;
3012 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3014 nfs_free_nfsdir(nfsdir
);
3021 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3032 struct lseek_cb_data
{
3033 struct nfs_context
*nfs
;
3034 struct nfsfh
*nfsfh
;
3040 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3043 struct lseek_cb_data
*data
= private_data
;
3044 struct nfs_context
*nfs
= data
->nfs
;
3046 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3048 if (status
== RPC_STATUS_ERROR
) {
3049 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3053 if (status
== RPC_STATUS_CANCEL
) {
3054 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3060 if (res
->status
!= NFS3_OK
) {
3061 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3062 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3067 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3068 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3072 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3074 struct lseek_cb_data
*data
;
3075 struct GETATTR3args args
;
3077 if (whence
== SEEK_SET
) {
3078 nfsfh
->offset
= offset
;
3079 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3082 if (whence
== SEEK_CUR
) {
3083 nfsfh
->offset
+= offset
;
3084 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3088 data
= malloc(sizeof(struct lseek_cb_data
));
3090 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3095 data
->nfsfh
= nfsfh
;
3096 data
->offset
= offset
;
3098 data
->private_data
= private_data
;
3100 memset(&args
, 0, sizeof(GETATTR3args
));
3101 args
.object
= nfsfh
->fh
;
3103 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3104 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3117 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3120 struct nfs_cb_data
*data
= private_data
;
3121 struct nfs_context
*nfs
= data
->nfs
;
3122 struct statvfs svfs
;
3124 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3126 if (status
== RPC_STATUS_ERROR
) {
3127 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3128 free_nfs_cb_data(data
);
3131 if (status
== RPC_STATUS_CANCEL
) {
3132 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3133 free_nfs_cb_data(data
);
3138 if (res
->status
!= NFS3_OK
) {
3139 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
));
3140 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3141 free_nfs_cb_data(data
);
3145 svfs
.f_bsize
= 4096;
3146 svfs
.f_frsize
= 4096;
3147 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3148 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3149 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3150 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3151 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3152 #if !defined(ANDROID)
3153 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3156 svfs
.f_namemax
= 256;
3159 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3160 free_nfs_cb_data(data
);
3163 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3167 args
.fsroot
= data
->fh
;
3168 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3169 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3170 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3171 free_nfs_cb_data(data
);
3177 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3179 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3180 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3193 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3196 struct nfs_cb_data
*data
= private_data
;
3197 struct nfs_context
*nfs
= data
->nfs
;
3199 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3201 if (status
== RPC_STATUS_ERROR
) {
3202 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3203 free_nfs_cb_data(data
);
3206 if (status
== RPC_STATUS_CANCEL
) {
3207 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3208 free_nfs_cb_data(data
);
3213 if (res
->status
!= NFS3_OK
) {
3214 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
));
3215 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3216 free_nfs_cb_data(data
);
3221 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3222 free_nfs_cb_data(data
);
3225 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3229 args
.symlink
= data
->fh
;
3231 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3232 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3233 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3234 free_nfs_cb_data(data
);
3240 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3242 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3243 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3256 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3258 struct nfs_cb_data
*data
= private_data
;
3259 struct nfs_context
*nfs
= data
->nfs
;
3262 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3264 if (status
== RPC_STATUS_ERROR
) {
3265 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3266 free_nfs_cb_data(data
);
3269 if (status
== RPC_STATUS_CANCEL
) {
3270 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3271 free_nfs_cb_data(data
);
3276 if (res
->status
!= NFS3_OK
) {
3277 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3278 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3279 free_nfs_cb_data(data
);
3283 data
->cb(0, nfs
, NULL
, data
->private_data
);
3284 free_nfs_cb_data(data
);
3287 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3291 memset(&args
, 0, sizeof(SETATTR3args
));
3292 args
.object
= data
->fh
;
3293 args
.new_attributes
.mode
.set_it
= 1;
3294 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3296 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3297 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3298 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3299 free_nfs_cb_data(data
);
3306 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3308 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3309 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3319 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3321 struct nfs_cb_data
*data
;
3323 data
= malloc(sizeof(struct nfs_cb_data
));
3325 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3328 memset(data
, 0, sizeof(struct nfs_cb_data
));
3331 data
->private_data
= private_data
;
3332 data
->continue_int
= mode
;
3333 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3334 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3335 if (data
->fh
.data
.data_val
== NULL
) {
3336 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3337 free_nfs_cb_data(data
);
3340 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3342 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3354 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3356 struct nfs_cb_data
*data
= private_data
;
3357 struct nfs_context
*nfs
= data
->nfs
;
3360 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3362 if (status
== RPC_STATUS_ERROR
) {
3363 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3364 free_nfs_cb_data(data
);
3367 if (status
== RPC_STATUS_CANCEL
) {
3368 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3369 free_nfs_cb_data(data
);
3374 if (res
->status
!= NFS3_OK
) {
3375 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3376 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3377 free_nfs_cb_data(data
);
3381 data
->cb(0, nfs
, NULL
, data
->private_data
);
3382 free_nfs_cb_data(data
);
3385 struct nfs_chown_data
{
3390 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3393 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3395 memset(&args
, 0, sizeof(SETATTR3args
));
3396 args
.object
= data
->fh
;
3397 if (chown_data
->uid
!= (uid_t
)-1) {
3398 args
.new_attributes
.uid
.set_it
= 1;
3399 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3401 if (chown_data
->gid
!= (gid_t
)-1) {
3402 args
.new_attributes
.gid
.set_it
= 1;
3403 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3406 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3407 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3408 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3409 free_nfs_cb_data(data
);
3416 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3418 struct nfs_chown_data
*chown_data
;
3420 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3421 if (chown_data
== NULL
) {
3422 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3426 chown_data
->uid
= uid
;
3427 chown_data
->gid
= gid
;
3429 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3430 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3441 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3443 struct nfs_cb_data
*data
;
3444 struct nfs_chown_data
*chown_data
;
3446 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3447 if (chown_data
== NULL
) {
3448 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3452 chown_data
->uid
= uid
;
3453 chown_data
->gid
= gid
;
3455 data
= malloc(sizeof(struct nfs_cb_data
));
3457 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3461 memset(data
, 0, sizeof(struct nfs_cb_data
));
3464 data
->private_data
= private_data
;
3465 data
->continue_data
= chown_data
;
3466 data
->free_continue_data
= free
;
3467 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3468 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3469 if (data
->fh
.data
.data_val
== NULL
) {
3470 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3471 free_nfs_cb_data(data
);
3474 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3476 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3490 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3492 struct nfs_cb_data
*data
= private_data
;
3493 struct nfs_context
*nfs
= data
->nfs
;
3496 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3498 if (status
== RPC_STATUS_ERROR
) {
3499 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3500 free_nfs_cb_data(data
);
3503 if (status
== RPC_STATUS_CANCEL
) {
3504 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3505 free_nfs_cb_data(data
);
3510 if (res
->status
!= NFS3_OK
) {
3511 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3512 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3513 free_nfs_cb_data(data
);
3517 data
->cb(0, nfs
, NULL
, data
->private_data
);
3518 free_nfs_cb_data(data
);
3521 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3524 struct timeval
*utimes_data
= data
->continue_data
;
3526 memset(&args
, 0, sizeof(SETATTR3args
));
3527 args
.object
= data
->fh
;
3528 if (utimes_data
!= NULL
) {
3529 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3530 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3531 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3532 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3533 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3534 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3536 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3537 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3540 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3541 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3542 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3543 free_nfs_cb_data(data
);
3550 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3552 struct timeval
*new_times
= NULL
;
3554 if (times
!= NULL
) {
3555 new_times
= malloc(sizeof(struct timeval
)*2);
3556 if (new_times
== NULL
) {
3557 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3561 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3564 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3565 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3575 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3577 struct timeval
*new_times
= NULL
;
3579 if (times
!= NULL
) {
3580 new_times
= malloc(sizeof(struct timeval
)*2);
3581 if (new_times
== NULL
) {
3582 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3586 new_times
[0].tv_sec
= times
->actime
;
3587 new_times
[0].tv_usec
= 0;
3588 new_times
[1].tv_sec
= times
->modtime
;
3589 new_times
[1].tv_usec
= 0;
3592 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3593 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3604 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3607 struct nfs_cb_data
*data
= private_data
;
3608 struct nfs_context
*nfs
= data
->nfs
;
3609 unsigned int nfsmode
= 0;
3611 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3613 if (status
== RPC_STATUS_ERROR
) {
3614 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3615 free_nfs_cb_data(data
);
3618 if (status
== RPC_STATUS_CANCEL
) {
3619 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3620 free_nfs_cb_data(data
);
3625 if (res
->status
!= NFS3_OK
) {
3626 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
));
3627 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3628 free_nfs_cb_data(data
);
3632 if (data
->continue_int
& R_OK
) {
3633 nfsmode
|= ACCESS3_READ
;
3635 if (data
->continue_int
& W_OK
) {
3636 nfsmode
|= ACCESS3_MODIFY
;
3638 if (data
->continue_int
& X_OK
) {
3639 nfsmode
|= ACCESS3_EXECUTE
;
3642 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3643 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3644 nfsmode
&ACCESS3_READ
?'r':'-',
3645 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3646 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3647 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3648 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3649 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3650 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3651 free_nfs_cb_data(data
);
3655 data
->cb(0, nfs
, NULL
, data
->private_data
);
3656 free_nfs_cb_data(data
);
3659 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3664 if (data
->continue_int
& R_OK
) {
3665 nfsmode
|= ACCESS3_READ
;
3667 if (data
->continue_int
& W_OK
) {
3668 nfsmode
|= ACCESS3_MODIFY
;
3670 if (data
->continue_int
& X_OK
) {
3671 nfsmode
|= ACCESS3_EXECUTE
;
3674 memset(&args
, 0, sizeof(ACCESS3args
));
3675 args
.object
= data
->fh
;
3676 args
.access
= nfsmode
;
3678 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3679 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3680 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3681 free_nfs_cb_data(data
);
3687 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3689 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3690 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3702 struct nfs_symlink_data
{
3704 char *newpathparent
;
3705 char *newpathobject
;
3708 static void free_nfs_symlink_data(void *mem
)
3710 struct nfs_symlink_data
*data
= mem
;
3712 if (data
->oldpath
!= NULL
) {
3713 free(data
->oldpath
);
3715 if (data
->newpathparent
!= NULL
) {
3716 free(data
->newpathparent
);
3718 if (data
->newpathobject
!= NULL
) {
3719 free(data
->newpathobject
);
3724 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3727 struct nfs_cb_data
*data
= private_data
;
3728 struct nfs_context
*nfs
= data
->nfs
;
3729 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3731 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3733 if (status
== RPC_STATUS_ERROR
) {
3734 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3735 free_nfs_cb_data(data
);
3738 if (status
== RPC_STATUS_CANCEL
) {
3739 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3740 free_nfs_cb_data(data
);
3745 if (res
->status
!= NFS3_OK
) {
3746 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
));
3747 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3748 free_nfs_cb_data(data
);
3752 data
->cb(0, nfs
, NULL
, data
->private_data
);
3753 free_nfs_cb_data(data
);
3756 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3758 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3761 memset(&args
, 0, sizeof(SYMLINK3args
));
3762 args
.where
.dir
= data
->fh
;
3763 args
.where
.name
= symlink_data
->newpathobject
;
3764 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3765 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
;
3766 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3768 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3769 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3770 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3771 free_nfs_cb_data(data
);
3777 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3780 struct nfs_symlink_data
*symlink_data
;
3782 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3783 if (symlink_data
== NULL
) {
3784 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3787 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3789 symlink_data
->oldpath
= strdup(oldpath
);
3790 if (symlink_data
->oldpath
== NULL
) {
3791 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3792 free_nfs_symlink_data(symlink_data
);
3796 symlink_data
->newpathparent
= strdup(newpath
);
3797 if (symlink_data
->newpathparent
== NULL
) {
3798 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3799 free_nfs_symlink_data(symlink_data
);
3803 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3805 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3806 free_nfs_symlink_data(symlink_data
);
3812 symlink_data
->newpathobject
= strdup(ptr
);
3813 if (symlink_data
->newpathobject
== NULL
) {
3814 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3815 free_nfs_symlink_data(symlink_data
);
3819 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3820 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3832 struct nfs_rename_data
{
3835 struct nfs_fh3 olddir
;
3838 struct nfs_fh3 newdir
;
3841 static void free_nfs_rename_data(void *mem
)
3843 struct nfs_rename_data
*data
= mem
;
3845 if (data
->oldpath
!= NULL
) {
3846 free(data
->oldpath
);
3848 if (data
->olddir
.data
.data_val
!= NULL
) {
3849 free(data
->olddir
.data
.data_val
);
3851 if (data
->newpath
!= NULL
) {
3852 free(data
->newpath
);
3854 if (data
->newdir
.data
.data_val
!= NULL
) {
3855 free(data
->newdir
.data
.data_val
);
3860 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3863 struct nfs_cb_data
*data
= private_data
;
3864 struct nfs_context
*nfs
= data
->nfs
;
3865 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3867 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3869 if (status
== RPC_STATUS_ERROR
) {
3870 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3871 free_nfs_cb_data(data
);
3874 if (status
== RPC_STATUS_CANCEL
) {
3875 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3876 free_nfs_cb_data(data
);
3881 if (res
->status
!= NFS3_OK
) {
3882 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
));
3883 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3884 free_nfs_cb_data(data
);
3888 data
->cb(0, nfs
, NULL
, data
->private_data
);
3889 free_nfs_cb_data(data
);
3892 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3894 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3897 /* steal the filehandle */
3898 rename_data
->newdir
= data
->fh
;
3899 data
->fh
.data
.data_val
= NULL
;
3901 args
.from
.dir
= rename_data
->olddir
;
3902 args
.from
.name
= rename_data
->oldobject
;
3903 args
.to
.dir
= rename_data
->newdir
;
3904 args
.to
.name
= rename_data
->newobject
;
3905 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3906 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3907 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3908 free_nfs_cb_data(data
);
3915 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3917 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3918 char* newpath
= strdup(rename_data
->newpath
);
3920 rpc_set_error(nfs
->rpc
, "Out of memory. Could not allocate memory to store target path for rename");
3921 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3922 free_nfs_cb_data(data
);
3926 /* steal the filehandle */
3927 rename_data
->olddir
= data
->fh
;
3928 data
->fh
.data
.data_val
= NULL
;
3930 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) {
3931 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", newpath
);
3932 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3933 free_nfs_cb_data(data
);
3937 data
->continue_data
= NULL
;
3938 free_nfs_cb_data(data
);
3945 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3948 struct nfs_rename_data
*rename_data
;
3950 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3951 if (rename_data
== NULL
) {
3952 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3955 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3957 rename_data
->oldpath
= strdup(oldpath
);
3958 if (rename_data
->oldpath
== NULL
) {
3959 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3960 free_nfs_rename_data(rename_data
);
3963 ptr
= strrchr(rename_data
->oldpath
, '/');
3965 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3966 free_nfs_rename_data(rename_data
);
3971 rename_data
->oldobject
= ptr
;
3974 rename_data
->newpath
= strdup(newpath
);
3975 if (rename_data
->newpath
== NULL
) {
3976 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3977 free_nfs_rename_data(rename_data
);
3980 ptr
= strrchr(rename_data
->newpath
, '/');
3982 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3983 free_nfs_rename_data(rename_data
);
3988 rename_data
->newobject
= ptr
;
3991 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3992 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4003 struct nfs_link_data
{
4005 struct nfs_fh3 oldfh
;
4008 struct nfs_fh3 newdir
;
4011 static void free_nfs_link_data(void *mem
)
4013 struct nfs_link_data
*data
= mem
;
4015 if (data
->oldpath
!= NULL
) {
4016 free(data
->oldpath
);
4018 if (data
->oldfh
.data
.data_val
!= NULL
) {
4019 free(data
->oldfh
.data
.data_val
);
4021 if (data
->newpath
!= NULL
) {
4022 free(data
->newpath
);
4024 if (data
->newdir
.data
.data_val
!= NULL
) {
4025 free(data
->newdir
.data
.data_val
);
4030 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4033 struct nfs_cb_data
*data
= private_data
;
4034 struct nfs_context
*nfs
= data
->nfs
;
4035 struct nfs_link_data
*link_data
= data
->continue_data
;
4037 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4039 if (status
== RPC_STATUS_ERROR
) {
4040 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4041 free_nfs_cb_data(data
);
4044 if (status
== RPC_STATUS_CANCEL
) {
4045 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4046 free_nfs_cb_data(data
);
4051 if (res
->status
!= NFS3_OK
) {
4052 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
));
4053 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4054 free_nfs_cb_data(data
);
4058 data
->cb(0, nfs
, NULL
, data
->private_data
);
4059 free_nfs_cb_data(data
);
4062 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4064 struct nfs_link_data
*link_data
= data
->continue_data
;
4067 /* steal the filehandle */
4068 link_data
->newdir
= data
->fh
;
4069 data
->fh
.data
.data_val
= NULL
;
4071 memset(&args
, 0, sizeof(LINK3args
));
4072 args
.file
= link_data
->oldfh
;
4073 args
.link
.dir
= link_data
->newdir
;
4074 args
.link
.name
= link_data
->newobject
;
4075 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4076 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4077 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4078 free_nfs_cb_data(data
);
4085 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4087 struct nfs_link_data
*link_data
= data
->continue_data
;
4089 /* steal the filehandle */
4090 link_data
->oldfh
= data
->fh
;
4091 data
->fh
.data
.data_val
= NULL
;
4093 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) {
4094 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4095 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4096 free_nfs_cb_data(data
);
4099 data
->continue_data
= NULL
;
4100 free_nfs_cb_data(data
);
4106 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4109 struct nfs_link_data
*link_data
;
4111 link_data
= malloc(sizeof(struct nfs_link_data
));
4112 if (link_data
== NULL
) {
4113 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4116 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4118 link_data
->oldpath
= strdup(oldpath
);
4119 if (link_data
->oldpath
== NULL
) {
4120 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4121 free_nfs_link_data(link_data
);
4125 link_data
->newpath
= strdup(newpath
);
4126 if (link_data
->newpath
== NULL
) {
4127 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4128 free_nfs_link_data(link_data
);
4131 ptr
= strrchr(link_data
->newpath
, '/');
4133 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4134 free_nfs_link_data(link_data
);
4139 link_data
->newobject
= ptr
;
4142 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4143 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4151 //qqq replace later with lseek()
4152 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4154 return nfsfh
->offset
;
4160 * Get the maximum supported READ3 size by the server
4162 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4164 return nfs
->readmax
;
4168 * Get the maximum supported WRITE3 size by the server
4170 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4172 return nfs
->writemax
;
4175 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4176 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4179 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4180 rpc_set_uid(nfs
->rpc
, uid
);
4183 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4184 rpc_set_gid(nfs
->rpc
, gid
);
4187 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4192 va_start(ap
, error_string
);
4194 vsnprintf(str
, 1024, error_string
, ap
);
4195 if (nfs
->rpc
->error_string
!= NULL
) {
4196 free(nfs
->rpc
->error_string
);
4198 nfs
->rpc
->error_string
= str
;
4204 struct mount_cb_data
{
4210 static void free_mount_cb_data(struct mount_cb_data
*data
)
4212 if (data
->server
!= NULL
) {
4214 data
->server
= NULL
;
4220 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4222 struct mount_cb_data
*data
= private_data
;
4224 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4226 if (status
== RPC_STATUS_ERROR
) {
4227 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4228 free_mount_cb_data(data
);
4231 if (status
== RPC_STATUS_CANCEL
) {
4232 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4233 free_mount_cb_data(data
);
4237 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4238 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4239 rpc_set_error(rpc
, "Failed to disconnect\n");
4241 free_mount_cb_data(data
);
4244 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4246 struct mount_cb_data
*data
= private_data
;
4248 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4250 /* Dont want any more callbacks even if the socket is closed */
4251 rpc
->connect_cb
= NULL
;
4253 if (status
== RPC_STATUS_ERROR
) {
4254 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4255 free_mount_cb_data(data
);
4258 if (status
== RPC_STATUS_CANCEL
) {
4259 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4260 free_mount_cb_data(data
);
4264 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4265 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4266 free_mount_cb_data(data
);
4271 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4273 struct mount_cb_data
*data
;
4275 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4277 data
= malloc(sizeof(struct mount_cb_data
));
4281 memset(data
, 0, sizeof(struct mount_cb_data
));
4283 data
->private_data
= private_data
;
4284 data
->server
= strdup(server
);
4285 if (data
->server
== NULL
) {
4286 free_mount_cb_data(data
);
4289 if (rpc_connect_program_async(rpc
, data
->server
, MOUNT_PROGRAM
, MOUNT_V3
, mount_export_4_cb
, data
) != 0) {
4290 rpc_set_error(rpc
, "Failed to start connection");
4291 free_mount_cb_data(data
);
4298 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4300 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4304 const char *nfs_get_server(struct nfs_context
*nfs
) {
4308 const char *nfs_get_export(struct nfs_context
*nfs
) {
4312 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4313 return &nfs
->rootfh
;
4316 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {