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_string_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 if (nfsdir
->entries
) {
2798 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2799 rdpe_cb_data
->getattrcount
= 0;
2800 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2801 rdpe_cb_data
->data
= data
;
2802 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2803 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2806 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2807 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2808 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2810 memset(&args
, 0, sizeof(LOOKUP3args
));
2811 args
.what
.dir
= data
->fh
;
2812 args
.what
.name
= nfsdirent
->name
;
2814 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2815 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2817 /* if we have already commands in flight, we cant just stop, we have to wait for the
2818 * commands in flight to complete
2820 if (rdpe_cb_data
->getattrcount
> 0) {
2821 nfs_free_nfsdir(nfsdir
);
2822 data
->continue_data
= NULL
;
2823 free_nfs_cb_data(data
);
2824 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2825 free(rdpe_lookup_cb_data
);
2829 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2830 nfs_free_nfsdir(nfsdir
);
2831 data
->continue_data
= NULL
;
2832 free_nfs_cb_data(data
);
2833 free(rdpe_lookup_cb_data
);
2837 rdpe_cb_data
->getattrcount
++;
2842 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2844 READDIRPLUS3res
*res
= command_data
;
2845 struct nfs_cb_data
*data
= private_data
;
2846 struct nfs_context
*nfs
= data
->nfs
;
2847 struct nfsdir
*nfsdir
= data
->continue_data
;
2848 struct entryplus3
*entry
;
2849 uint64_t cookie
= 0;
2851 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2853 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2856 args
.dir
= data
->fh
;
2857 args
.cookie
= cookie
;
2858 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2861 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2862 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2863 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2864 nfs_free_nfsdir(nfsdir
);
2865 data
->continue_data
= NULL
;
2866 free_nfs_cb_data(data
);
2872 if (status
== RPC_STATUS_CANCEL
) {
2873 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2874 nfs_free_nfsdir(nfsdir
);
2875 data
->continue_data
= NULL
;
2876 free_nfs_cb_data(data
);
2880 if (res
->status
!= NFS3_OK
) {
2881 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
));
2882 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2883 nfs_free_nfsdir(nfsdir
);
2884 data
->continue_data
= NULL
;
2885 free_nfs_cb_data(data
);
2889 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2890 while (entry
!= NULL
) {
2891 struct nfsdirent
*nfsdirent
;
2893 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2894 if (nfsdirent
== NULL
) {
2895 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2896 nfs_free_nfsdir(nfsdir
);
2897 data
->continue_data
= NULL
;
2898 free_nfs_cb_data(data
);
2901 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2902 nfsdirent
->name
= strdup(entry
->name
);
2903 if (nfsdirent
->name
== NULL
) {
2904 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2906 nfs_free_nfsdir(nfsdir
);
2907 data
->continue_data
= NULL
;
2908 free_nfs_cb_data(data
);
2911 nfsdirent
->inode
= entry
->fileid
;
2912 if (entry
->name_attributes
.attributes_follow
) {
2913 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2914 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2915 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2917 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2918 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2919 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2920 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2921 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2922 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2923 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2924 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2927 nfsdirent
->next
= nfsdir
->entries
;
2928 nfsdir
->entries
= nfsdirent
;
2930 cookie
= entry
->cookie
;
2931 entry
= entry
->nextentry
;
2934 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2935 READDIRPLUS3args args
;
2937 args
.dir
= data
->fh
;
2938 args
.cookie
= cookie
;
2939 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2940 args
.dircount
= 8192;
2941 args
.maxcount
= 8192;
2943 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2944 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2945 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2946 nfs_free_nfsdir(nfsdir
);
2947 data
->continue_data
= NULL
;
2948 free_nfs_cb_data(data
);
2954 /* steal the dirhandle */
2955 data
->continue_data
= NULL
;
2956 nfsdir
->current
= nfsdir
->entries
;
2958 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2959 free_nfs_cb_data(data
);
2962 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2964 READDIRPLUS3args args
;
2966 args
.dir
= data
->fh
;
2968 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2969 args
.dircount
= 8192;
2970 args
.maxcount
= 8192;
2971 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2972 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2973 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2974 free_nfs_cb_data(data
);
2980 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2982 struct nfsdir
*nfsdir
;
2984 nfsdir
= malloc(sizeof(struct nfsdir
));
2985 if (nfsdir
== NULL
) {
2986 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2989 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2991 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2992 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3000 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3002 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
3004 if (nfsdir
->current
!= NULL
) {
3005 nfsdir
->current
= nfsdir
->current
->next
;
3014 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
3016 nfs_free_nfsdir(nfsdir
);
3023 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3034 struct lseek_cb_data
{
3035 struct nfs_context
*nfs
;
3036 struct nfsfh
*nfsfh
;
3042 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3045 struct lseek_cb_data
*data
= private_data
;
3046 struct nfs_context
*nfs
= data
->nfs
;
3048 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3050 if (status
== RPC_STATUS_ERROR
) {
3051 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3055 if (status
== RPC_STATUS_CANCEL
) {
3056 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3062 if (res
->status
!= NFS3_OK
) {
3063 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3064 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3069 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3070 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3074 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3076 struct lseek_cb_data
*data
;
3077 struct GETATTR3args args
;
3079 if (whence
== SEEK_SET
) {
3080 nfsfh
->offset
= offset
;
3081 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3084 if (whence
== SEEK_CUR
) {
3085 nfsfh
->offset
+= offset
;
3086 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3090 data
= malloc(sizeof(struct lseek_cb_data
));
3092 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3097 data
->nfsfh
= nfsfh
;
3098 data
->offset
= offset
;
3100 data
->private_data
= private_data
;
3102 memset(&args
, 0, sizeof(GETATTR3args
));
3103 args
.object
= nfsfh
->fh
;
3105 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3106 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3119 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3122 struct nfs_cb_data
*data
= private_data
;
3123 struct nfs_context
*nfs
= data
->nfs
;
3124 struct statvfs svfs
;
3126 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3128 if (status
== RPC_STATUS_ERROR
) {
3129 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3130 free_nfs_cb_data(data
);
3133 if (status
== RPC_STATUS_CANCEL
) {
3134 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3135 free_nfs_cb_data(data
);
3140 if (res
->status
!= NFS3_OK
) {
3141 rpc_set_error(nfs
->rpc
, "NFS: FSSTAT of %s failed with %s(%d)", data
->saved_path
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3142 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3143 free_nfs_cb_data(data
);
3147 svfs
.f_bsize
= 4096;
3148 svfs
.f_frsize
= 4096;
3149 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3150 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3151 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3152 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3153 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3154 #if !defined(ANDROID)
3155 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3158 svfs
.f_namemax
= 256;
3161 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3162 free_nfs_cb_data(data
);
3165 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3169 args
.fsroot
= data
->fh
;
3170 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3171 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3172 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3173 free_nfs_cb_data(data
);
3179 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3181 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3182 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3195 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3198 struct nfs_cb_data
*data
= private_data
;
3199 struct nfs_context
*nfs
= data
->nfs
;
3201 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3203 if (status
== RPC_STATUS_ERROR
) {
3204 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3205 free_nfs_cb_data(data
);
3208 if (status
== RPC_STATUS_CANCEL
) {
3209 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3210 free_nfs_cb_data(data
);
3215 if (res
->status
!= NFS3_OK
) {
3216 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
));
3217 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3218 free_nfs_cb_data(data
);
3223 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3224 free_nfs_cb_data(data
);
3227 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3231 args
.symlink
= data
->fh
;
3233 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3234 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3235 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3236 free_nfs_cb_data(data
);
3242 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3244 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3245 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3258 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3260 struct nfs_cb_data
*data
= private_data
;
3261 struct nfs_context
*nfs
= data
->nfs
;
3264 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3266 if (status
== RPC_STATUS_ERROR
) {
3267 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3268 free_nfs_cb_data(data
);
3271 if (status
== RPC_STATUS_CANCEL
) {
3272 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3273 free_nfs_cb_data(data
);
3278 if (res
->status
!= NFS3_OK
) {
3279 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3280 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3281 free_nfs_cb_data(data
);
3285 data
->cb(0, nfs
, NULL
, data
->private_data
);
3286 free_nfs_cb_data(data
);
3289 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3293 memset(&args
, 0, sizeof(SETATTR3args
));
3294 args
.object
= data
->fh
;
3295 args
.new_attributes
.mode
.set_it
= 1;
3296 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3298 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3299 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3300 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3301 free_nfs_cb_data(data
);
3308 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3310 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3311 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3321 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3323 struct nfs_cb_data
*data
;
3325 data
= malloc(sizeof(struct nfs_cb_data
));
3327 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3330 memset(data
, 0, sizeof(struct nfs_cb_data
));
3333 data
->private_data
= private_data
;
3334 data
->continue_int
= mode
;
3335 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3336 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3337 if (data
->fh
.data
.data_val
== NULL
) {
3338 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3339 free_nfs_cb_data(data
);
3342 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3344 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3356 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3358 struct nfs_cb_data
*data
= private_data
;
3359 struct nfs_context
*nfs
= data
->nfs
;
3362 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3364 if (status
== RPC_STATUS_ERROR
) {
3365 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3366 free_nfs_cb_data(data
);
3369 if (status
== RPC_STATUS_CANCEL
) {
3370 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3371 free_nfs_cb_data(data
);
3376 if (res
->status
!= NFS3_OK
) {
3377 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3378 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3379 free_nfs_cb_data(data
);
3383 data
->cb(0, nfs
, NULL
, data
->private_data
);
3384 free_nfs_cb_data(data
);
3387 struct nfs_chown_data
{
3392 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3395 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3397 memset(&args
, 0, sizeof(SETATTR3args
));
3398 args
.object
= data
->fh
;
3399 if (chown_data
->uid
!= (uid_t
)-1) {
3400 args
.new_attributes
.uid
.set_it
= 1;
3401 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3403 if (chown_data
->gid
!= (gid_t
)-1) {
3404 args
.new_attributes
.gid
.set_it
= 1;
3405 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3408 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3409 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3410 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3411 free_nfs_cb_data(data
);
3418 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3420 struct nfs_chown_data
*chown_data
;
3422 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3423 if (chown_data
== NULL
) {
3424 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3428 chown_data
->uid
= uid
;
3429 chown_data
->gid
= gid
;
3431 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3432 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3443 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3445 struct nfs_cb_data
*data
;
3446 struct nfs_chown_data
*chown_data
;
3448 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3449 if (chown_data
== NULL
) {
3450 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3454 chown_data
->uid
= uid
;
3455 chown_data
->gid
= gid
;
3457 data
= malloc(sizeof(struct nfs_cb_data
));
3459 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3463 memset(data
, 0, sizeof(struct nfs_cb_data
));
3466 data
->private_data
= private_data
;
3467 data
->continue_data
= chown_data
;
3468 data
->free_continue_data
= free
;
3469 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3470 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3471 if (data
->fh
.data
.data_val
== NULL
) {
3472 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3473 free_nfs_cb_data(data
);
3476 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3478 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3492 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3494 struct nfs_cb_data
*data
= private_data
;
3495 struct nfs_context
*nfs
= data
->nfs
;
3498 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3500 if (status
== RPC_STATUS_ERROR
) {
3501 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3502 free_nfs_cb_data(data
);
3505 if (status
== RPC_STATUS_CANCEL
) {
3506 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3507 free_nfs_cb_data(data
);
3512 if (res
->status
!= NFS3_OK
) {
3513 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3514 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3515 free_nfs_cb_data(data
);
3519 data
->cb(0, nfs
, NULL
, data
->private_data
);
3520 free_nfs_cb_data(data
);
3523 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3526 struct timeval
*utimes_data
= data
->continue_data
;
3528 memset(&args
, 0, sizeof(SETATTR3args
));
3529 args
.object
= data
->fh
;
3530 if (utimes_data
!= NULL
) {
3531 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3532 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3533 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3534 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3535 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3536 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3538 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3539 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3542 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3543 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3544 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3545 free_nfs_cb_data(data
);
3552 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3554 struct timeval
*new_times
= NULL
;
3556 if (times
!= NULL
) {
3557 new_times
= malloc(sizeof(struct timeval
)*2);
3558 if (new_times
== NULL
) {
3559 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3563 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3566 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3567 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3577 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3579 struct timeval
*new_times
= NULL
;
3581 if (times
!= NULL
) {
3582 new_times
= malloc(sizeof(struct timeval
)*2);
3583 if (new_times
== NULL
) {
3584 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3588 new_times
[0].tv_sec
= times
->actime
;
3589 new_times
[0].tv_usec
= 0;
3590 new_times
[1].tv_sec
= times
->modtime
;
3591 new_times
[1].tv_usec
= 0;
3594 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3595 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3606 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3609 struct nfs_cb_data
*data
= private_data
;
3610 struct nfs_context
*nfs
= data
->nfs
;
3611 unsigned int nfsmode
= 0;
3613 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3615 if (status
== RPC_STATUS_ERROR
) {
3616 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3617 free_nfs_cb_data(data
);
3620 if (status
== RPC_STATUS_CANCEL
) {
3621 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3622 free_nfs_cb_data(data
);
3627 if (res
->status
!= NFS3_OK
) {
3628 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
));
3629 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3630 free_nfs_cb_data(data
);
3634 if (data
->continue_int
& R_OK
) {
3635 nfsmode
|= ACCESS3_READ
;
3637 if (data
->continue_int
& W_OK
) {
3638 nfsmode
|= ACCESS3_MODIFY
;
3640 if (data
->continue_int
& X_OK
) {
3641 nfsmode
|= ACCESS3_EXECUTE
;
3644 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3645 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3646 nfsmode
&ACCESS3_READ
?'r':'-',
3647 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3648 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3649 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3650 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3651 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3652 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3653 free_nfs_cb_data(data
);
3657 data
->cb(0, nfs
, NULL
, data
->private_data
);
3658 free_nfs_cb_data(data
);
3661 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3666 if (data
->continue_int
& R_OK
) {
3667 nfsmode
|= ACCESS3_READ
;
3669 if (data
->continue_int
& W_OK
) {
3670 nfsmode
|= ACCESS3_MODIFY
;
3672 if (data
->continue_int
& X_OK
) {
3673 nfsmode
|= ACCESS3_EXECUTE
;
3676 memset(&args
, 0, sizeof(ACCESS3args
));
3677 args
.object
= data
->fh
;
3678 args
.access
= nfsmode
;
3680 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3681 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3682 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3683 free_nfs_cb_data(data
);
3689 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3691 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3692 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3704 struct nfs_symlink_data
{
3706 char *newpathparent
;
3707 char *newpathobject
;
3710 static void free_nfs_symlink_data(void *mem
)
3712 struct nfs_symlink_data
*data
= mem
;
3714 if (data
->oldpath
!= NULL
) {
3715 free(data
->oldpath
);
3717 if (data
->newpathparent
!= NULL
) {
3718 free(data
->newpathparent
);
3720 if (data
->newpathobject
!= NULL
) {
3721 free(data
->newpathobject
);
3726 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3729 struct nfs_cb_data
*data
= private_data
;
3730 struct nfs_context
*nfs
= data
->nfs
;
3731 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3733 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3735 if (status
== RPC_STATUS_ERROR
) {
3736 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3737 free_nfs_cb_data(data
);
3740 if (status
== RPC_STATUS_CANCEL
) {
3741 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3742 free_nfs_cb_data(data
);
3747 if (res
->status
!= NFS3_OK
) {
3748 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
));
3749 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3750 free_nfs_cb_data(data
);
3754 data
->cb(0, nfs
, NULL
, data
->private_data
);
3755 free_nfs_cb_data(data
);
3758 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3760 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3763 memset(&args
, 0, sizeof(SYMLINK3args
));
3764 args
.where
.dir
= data
->fh
;
3765 args
.where
.name
= symlink_data
->newpathobject
;
3766 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3767 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
;
3768 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3770 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3771 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3772 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3773 free_nfs_cb_data(data
);
3779 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3782 struct nfs_symlink_data
*symlink_data
;
3784 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3785 if (symlink_data
== NULL
) {
3786 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3789 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3791 symlink_data
->oldpath
= strdup(oldpath
);
3792 if (symlink_data
->oldpath
== NULL
) {
3793 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3794 free_nfs_symlink_data(symlink_data
);
3798 symlink_data
->newpathparent
= strdup(newpath
);
3799 if (symlink_data
->newpathparent
== NULL
) {
3800 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3801 free_nfs_symlink_data(symlink_data
);
3805 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3807 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3808 free_nfs_symlink_data(symlink_data
);
3814 symlink_data
->newpathobject
= strdup(ptr
);
3815 if (symlink_data
->newpathobject
== NULL
) {
3816 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3817 free_nfs_symlink_data(symlink_data
);
3821 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3822 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3834 struct nfs_rename_data
{
3837 struct nfs_fh3 olddir
;
3840 struct nfs_fh3 newdir
;
3843 static void free_nfs_rename_data(void *mem
)
3845 struct nfs_rename_data
*data
= mem
;
3847 if (data
->oldpath
!= NULL
) {
3848 free(data
->oldpath
);
3850 if (data
->olddir
.data
.data_val
!= NULL
) {
3851 free(data
->olddir
.data
.data_val
);
3853 if (data
->newpath
!= NULL
) {
3854 free(data
->newpath
);
3856 if (data
->newdir
.data
.data_val
!= NULL
) {
3857 free(data
->newdir
.data
.data_val
);
3862 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3865 struct nfs_cb_data
*data
= private_data
;
3866 struct nfs_context
*nfs
= data
->nfs
;
3867 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3869 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3871 if (status
== RPC_STATUS_ERROR
) {
3872 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3873 free_nfs_cb_data(data
);
3876 if (status
== RPC_STATUS_CANCEL
) {
3877 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3878 free_nfs_cb_data(data
);
3883 if (res
->status
!= NFS3_OK
) {
3884 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
));
3885 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3886 free_nfs_cb_data(data
);
3890 data
->cb(0, nfs
, NULL
, data
->private_data
);
3891 free_nfs_cb_data(data
);
3894 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3896 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3899 /* steal the filehandle */
3900 rename_data
->newdir
= data
->fh
;
3901 data
->fh
.data
.data_val
= NULL
;
3903 args
.from
.dir
= rename_data
->olddir
;
3904 args
.from
.name
= rename_data
->oldobject
;
3905 args
.to
.dir
= rename_data
->newdir
;
3906 args
.to
.name
= rename_data
->newobject
;
3907 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3908 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3909 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3910 free_nfs_cb_data(data
);
3917 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3919 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3920 char* newpath
= strdup(rename_data
->newpath
);
3922 rpc_set_error(nfs
->rpc
, "Out of memory. Could not allocate memory to store target path for rename");
3923 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3924 free_nfs_cb_data(data
);
3928 /* steal the filehandle */
3929 rename_data
->olddir
= data
->fh
;
3930 data
->fh
.data
.data_val
= NULL
;
3932 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) {
3933 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", newpath
);
3934 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3935 free_nfs_cb_data(data
);
3939 data
->continue_data
= NULL
;
3940 free_nfs_cb_data(data
);
3947 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3950 struct nfs_rename_data
*rename_data
;
3952 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3953 if (rename_data
== NULL
) {
3954 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3957 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3959 rename_data
->oldpath
= strdup(oldpath
);
3960 if (rename_data
->oldpath
== NULL
) {
3961 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3962 free_nfs_rename_data(rename_data
);
3965 ptr
= strrchr(rename_data
->oldpath
, '/');
3967 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3968 free_nfs_rename_data(rename_data
);
3973 rename_data
->oldobject
= ptr
;
3976 rename_data
->newpath
= strdup(newpath
);
3977 if (rename_data
->newpath
== NULL
) {
3978 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3979 free_nfs_rename_data(rename_data
);
3982 ptr
= strrchr(rename_data
->newpath
, '/');
3984 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3985 free_nfs_rename_data(rename_data
);
3990 rename_data
->newobject
= ptr
;
3993 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3994 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4005 struct nfs_link_data
{
4007 struct nfs_fh3 oldfh
;
4010 struct nfs_fh3 newdir
;
4013 static void free_nfs_link_data(void *mem
)
4015 struct nfs_link_data
*data
= mem
;
4017 if (data
->oldpath
!= NULL
) {
4018 free(data
->oldpath
);
4020 if (data
->oldfh
.data
.data_val
!= NULL
) {
4021 free(data
->oldfh
.data
.data_val
);
4023 if (data
->newpath
!= NULL
) {
4024 free(data
->newpath
);
4026 if (data
->newdir
.data
.data_val
!= NULL
) {
4027 free(data
->newdir
.data
.data_val
);
4032 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4035 struct nfs_cb_data
*data
= private_data
;
4036 struct nfs_context
*nfs
= data
->nfs
;
4037 struct nfs_link_data
*link_data
= data
->continue_data
;
4039 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4041 if (status
== RPC_STATUS_ERROR
) {
4042 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4043 free_nfs_cb_data(data
);
4046 if (status
== RPC_STATUS_CANCEL
) {
4047 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4048 free_nfs_cb_data(data
);
4053 if (res
->status
!= NFS3_OK
) {
4054 rpc_set_error(nfs
->rpc
, "NFS: 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
));
4055 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4056 free_nfs_cb_data(data
);
4060 data
->cb(0, nfs
, NULL
, data
->private_data
);
4061 free_nfs_cb_data(data
);
4064 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4066 struct nfs_link_data
*link_data
= data
->continue_data
;
4069 /* steal the filehandle */
4070 link_data
->newdir
= data
->fh
;
4071 data
->fh
.data
.data_val
= NULL
;
4073 memset(&args
, 0, sizeof(LINK3args
));
4074 args
.file
= link_data
->oldfh
;
4075 args
.link
.dir
= link_data
->newdir
;
4076 args
.link
.name
= link_data
->newobject
;
4077 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4078 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4079 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4080 free_nfs_cb_data(data
);
4087 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4089 struct nfs_link_data
*link_data
= data
->continue_data
;
4091 /* steal the filehandle */
4092 link_data
->oldfh
= data
->fh
;
4093 data
->fh
.data
.data_val
= NULL
;
4095 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) {
4096 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4097 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4098 free_nfs_cb_data(data
);
4101 data
->continue_data
= NULL
;
4102 free_nfs_cb_data(data
);
4108 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4111 struct nfs_link_data
*link_data
;
4113 link_data
= malloc(sizeof(struct nfs_link_data
));
4114 if (link_data
== NULL
) {
4115 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4118 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4120 link_data
->oldpath
= strdup(oldpath
);
4121 if (link_data
->oldpath
== NULL
) {
4122 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4123 free_nfs_link_data(link_data
);
4127 link_data
->newpath
= strdup(newpath
);
4128 if (link_data
->newpath
== NULL
) {
4129 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4130 free_nfs_link_data(link_data
);
4133 ptr
= strrchr(link_data
->newpath
, '/');
4135 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4136 free_nfs_link_data(link_data
);
4141 link_data
->newobject
= ptr
;
4144 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4145 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4153 //qqq replace later with lseek()
4154 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4156 return nfsfh
->offset
;
4162 * Get the maximum supported READ3 size by the server
4164 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4166 return nfs
->readmax
;
4170 * Get the maximum supported WRITE3 size by the server
4172 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4174 return nfs
->writemax
;
4177 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4178 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4181 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4182 rpc_set_uid(nfs
->rpc
, uid
);
4185 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4186 rpc_set_gid(nfs
->rpc
, gid
);
4189 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4194 va_start(ap
, error_string
);
4196 vsnprintf(str
, 1024, error_string
, ap
);
4197 if (nfs
->rpc
->error_string
!= NULL
) {
4198 free(nfs
->rpc
->error_string
);
4200 nfs
->rpc
->error_string
= str
;
4206 struct mount_cb_data
{
4212 static void free_mount_cb_data(struct mount_cb_data
*data
)
4214 if (data
->server
!= NULL
) {
4216 data
->server
= NULL
;
4222 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4224 struct mount_cb_data
*data
= private_data
;
4226 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4228 if (status
== RPC_STATUS_ERROR
) {
4229 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4230 free_mount_cb_data(data
);
4233 if (status
== RPC_STATUS_CANCEL
) {
4234 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4235 free_mount_cb_data(data
);
4239 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4240 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4241 rpc_set_error(rpc
, "Failed to disconnect\n");
4243 free_mount_cb_data(data
);
4246 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4248 struct mount_cb_data
*data
= private_data
;
4250 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4252 /* Dont want any more callbacks even if the socket is closed */
4253 rpc
->connect_cb
= NULL
;
4255 if (status
== RPC_STATUS_ERROR
) {
4256 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4257 free_mount_cb_data(data
);
4260 if (status
== RPC_STATUS_CANCEL
) {
4261 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4262 free_mount_cb_data(data
);
4266 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4267 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4268 free_mount_cb_data(data
);
4273 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4275 struct mount_cb_data
*data
;
4277 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4279 data
= malloc(sizeof(struct mount_cb_data
));
4283 memset(data
, 0, sizeof(struct mount_cb_data
));
4285 data
->private_data
= private_data
;
4286 data
->server
= strdup(server
);
4287 if (data
->server
== NULL
) {
4288 free_mount_cb_data(data
);
4291 if (rpc_connect_program_async(rpc
, data
->server
, MOUNT_PROGRAM
, MOUNT_V3
, mount_export_4_cb
, data
) != 0) {
4292 rpc_set_error(rpc
, "Failed to start connection");
4293 free_mount_cb_data(data
);
4300 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4302 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4306 const char *nfs_get_server(struct nfs_context
*nfs
) {
4310 const char *nfs_get_export(struct nfs_context
*nfs
) {
4314 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4315 return &nfs
->rootfh
;
4318 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {