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-private.h"
79 struct nfsdirent
*entries
;
80 struct nfsdirent
*current
;
90 struct rpc_context
*rpc
;
93 struct nfs_fh3 rootfh
;
99 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
101 while (nfsdir
->entries
) {
102 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
103 if (nfsdir
->entries
->name
!= NULL
) {
104 free(nfsdir
->entries
->name
);
106 free(nfsdir
->entries
);
107 nfsdir
->entries
= dirent
;
113 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
116 struct nfs_context
*nfs
;
118 char *saved_path
, *path
;
123 continue_func continue_cb
;
125 void (*free_continue_data
)(void *);
130 /* for multi-read/write calls. */
134 uint64_t start_offset
, max_offset
;
138 struct nfs_mcb_data
{
139 struct nfs_cb_data
*data
;
144 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
146 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
148 rpc_set_auth(nfs
->rpc
, auth
);
151 int nfs_get_fd(struct nfs_context
*nfs
)
153 return rpc_get_fd(nfs
->rpc
);
156 int nfs_queue_length(struct nfs_context
*nfs
)
158 return rpc_queue_length(nfs
->rpc
);
161 int nfs_which_events(struct nfs_context
*nfs
)
163 return rpc_which_events(nfs
->rpc
);
166 int nfs_service(struct nfs_context
*nfs
, int revents
)
168 return rpc_service(nfs
->rpc
, revents
);
171 char *nfs_get_error(struct nfs_context
*nfs
)
173 return rpc_get_error(nfs
->rpc
);
176 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
178 if (!strncmp(arg
, "tcp-syncnt", 10)) {
179 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
180 } else if (!strncmp(arg
, "uid", 3)) {
181 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
182 } else if (!strncmp(arg
, "gid", 3)) {
183 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
188 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
190 struct nfs_url
*urls
;
191 char *strp
, *flagsp
, *strp2
;
193 if (strncmp(url
, "nfs://", 6)) {
194 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
198 urls
= malloc(sizeof(struct nfs_url
));
200 rpc_set_error(nfs
->rpc
, "Out of memory");
204 memset(urls
, 0x00, sizeof(struct nfs_url
));
205 urls
->server
= strdup(url
+ 6);
206 if (urls
->server
== NULL
) {
207 nfs_destroy_url(urls
);
208 rpc_set_error(nfs
->rpc
, "Out of memory");
212 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
213 urls
->server
[0] == '?') {
215 flagsp
= strchr(urls
->server
, '?');
218 nfs_destroy_url(urls
);
219 rpc_set_error(nfs
->rpc
, "Invalid server string");
223 strp
= strchr(urls
->server
, '/');
226 flagsp
= strchr(urls
->server
, '?');
229 nfs_destroy_url(urls
);
230 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
234 urls
->path
= strdup(strp
);
235 if (urls
->path
== NULL
) {
236 nfs_destroy_url(urls
);
237 rpc_set_error(nfs
->rpc
, "Out of memory");
243 flagsp
= strchr(urls
->path
, '?');
247 strp
= strrchr(urls
->path
, '/');
250 flagsp
= strchr(urls
->path
, '?');
253 nfs_destroy_url(urls
);
254 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
257 urls
->file
= strdup(strp
);
258 if (urls
->path
== NULL
) {
259 nfs_destroy_url(urls
);
260 rpc_set_error(nfs
->rpc
, "Out of memory");
264 flagsp
= strchr(urls
->file
, '?');
271 if (urls
->file
&& !strlen(urls
->file
)) {
275 nfs_destroy_url(urls
);
276 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
281 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
283 flagsp
= strchr(strp
, '&');
287 strp2
= strchr(strp
, '=');
291 nfs_set_context_args(nfs
, strp
, strp2
);
295 if (urls
->server
&& strlen(urls
->server
) <= 1) {
303 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
305 return nfs_parse_url(nfs
, url
, 0, 0);
308 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
310 return nfs_parse_url(nfs
, url
, 1, 0);
313 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
315 return nfs_parse_url(nfs
, url
, 0, 1);
319 void nfs_destroy_url(struct nfs_url
*url
)
329 struct nfs_context
*nfs_init_context(void)
331 struct nfs_context
*nfs
;
333 nfs
= malloc(sizeof(struct nfs_context
));
337 memset(nfs
, 0, sizeof(struct nfs_context
));
339 nfs
->rpc
= rpc_init_context();
340 if (nfs
->rpc
== NULL
) {
345 nfs
->cwd
= strdup("/");
350 void nfs_destroy_context(struct nfs_context
*nfs
)
352 rpc_destroy_context(nfs
->rpc
);
370 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
371 free(nfs
->rootfh
.data
.data_val
);
372 nfs
->rootfh
.data
.data_val
= NULL
;
387 void free_rpc_cb_data(struct rpc_cb_data
*data
)
394 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
396 struct rpc_cb_data
*data
= private_data
;
398 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
400 /* Dont want any more callbacks even if the socket is closed */
401 rpc
->connect_cb
= NULL
;
403 if (status
== RPC_STATUS_ERROR
) {
404 data
->cb(rpc
, status
, command_data
, data
->private_data
);
405 free_rpc_cb_data(data
);
408 if (status
== RPC_STATUS_CANCEL
) {
409 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
410 free_rpc_cb_data(data
);
414 data
->cb(rpc
, status
, NULL
, data
->private_data
);
415 free_rpc_cb_data(data
);
418 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
420 struct rpc_cb_data
*data
= private_data
;
423 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
425 if (status
== RPC_STATUS_ERROR
) {
426 data
->cb(rpc
, status
, command_data
, data
->private_data
);
427 free_rpc_cb_data(data
);
430 if (status
== RPC_STATUS_CANCEL
) {
431 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
432 free_rpc_cb_data(data
);
436 rpc_port
= *(uint32_t *)command_data
;
438 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
439 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
440 free_rpc_cb_data(data
);
444 rpc_disconnect(rpc
, "normal disconnect");
445 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
446 data
->cb(rpc
, status
, command_data
, data
->private_data
);
447 free_rpc_cb_data(data
);
452 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
454 struct rpc_cb_data
*data
= private_data
;
456 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
458 if (status
== RPC_STATUS_ERROR
) {
459 data
->cb(rpc
, status
, command_data
, data
->private_data
);
460 free_rpc_cb_data(data
);
463 if (status
== RPC_STATUS_CANCEL
) {
464 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
465 free_rpc_cb_data(data
);
469 if (rpc_pmap_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
470 data
->cb(rpc
, status
, command_data
, data
->private_data
);
471 free_rpc_cb_data(data
);
476 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
478 struct rpc_cb_data
*data
= private_data
;
480 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
482 /* Dont want any more callbacks even if the socket is closed */
483 rpc
->connect_cb
= NULL
;
485 if (status
== RPC_STATUS_ERROR
) {
486 data
->cb(rpc
, status
, command_data
, data
->private_data
);
487 free_rpc_cb_data(data
);
490 if (status
== RPC_STATUS_CANCEL
) {
491 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
492 free_rpc_cb_data(data
);
496 if (rpc_pmap_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
497 data
->cb(rpc
, status
, command_data
, data
->private_data
);
498 free_rpc_cb_data(data
);
503 int rpc_connect_program_async(struct rpc_context
*rpc
, char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
505 struct rpc_cb_data
*data
;
507 data
= malloc(sizeof(struct rpc_cb_data
));
511 memset(data
, 0, sizeof(struct rpc_cb_data
));
512 data
->server
= strdup(server
);
513 data
->program
= program
;
514 data
->version
= version
;
517 data
->private_data
= private_data
;
519 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
520 rpc_set_error(rpc
, "Failed to start connection");
521 free_rpc_cb_data(data
);
527 void free_nfs_cb_data(struct nfs_cb_data
*data
)
529 if (data
->saved_path
!= NULL
) {
530 free(data
->saved_path
);
531 data
->saved_path
= NULL
;
534 if (data
->continue_data
!= NULL
) {
535 assert(data
->free_continue_data
);
536 data
->free_continue_data(data
->continue_data
);
537 data
->continue_data
= NULL
;
540 if (data
->fh
.data
.data_val
!= NULL
) {
541 free(data
->fh
.data
.data_val
);
542 data
->fh
.data
.data_val
= NULL
;
545 if (data
->buffer
!= NULL
) {
557 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
559 struct nfs_cb_data
*data
= private_data
;
560 struct nfs_context
*nfs
= data
->nfs
;
562 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
564 if (status
== RPC_STATUS_ERROR
) {
565 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
566 free_nfs_cb_data(data
);
569 if (status
== RPC_STATUS_CANCEL
) {
570 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
571 free_nfs_cb_data(data
);
575 data
->cb(0, nfs
, NULL
, data
->private_data
);
576 free_nfs_cb_data(data
);
579 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
581 struct nfs_cb_data
*data
= private_data
;
582 struct nfs_context
*nfs
= data
->nfs
;
583 FSINFO3res
*res
= command_data
;
584 struct GETATTR3args args
;
586 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
588 if (status
== RPC_STATUS_ERROR
) {
589 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
590 free_nfs_cb_data(data
);
593 if (status
== RPC_STATUS_CANCEL
) {
594 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
595 free_nfs_cb_data(data
);
599 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
600 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
602 memset(&args
, 0, sizeof(GETATTR3args
));
603 args
.object
= nfs
->rootfh
;
605 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
606 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
607 free_nfs_cb_data(data
);
612 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
614 struct nfs_cb_data
*data
= private_data
;
615 struct nfs_context
*nfs
= data
->nfs
;
616 struct FSINFO3args args
;
618 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
620 if (status
== RPC_STATUS_ERROR
) {
621 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
622 free_nfs_cb_data(data
);
625 if (status
== RPC_STATUS_CANCEL
) {
626 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
627 free_nfs_cb_data(data
);
631 args
.fsroot
= nfs
->rootfh
;
632 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
633 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
634 free_nfs_cb_data(data
);
640 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
642 struct nfs_cb_data
*data
= private_data
;
643 struct nfs_context
*nfs
= data
->nfs
;
645 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
647 /* Dont want any more callbacks even if the socket is closed */
648 rpc
->connect_cb
= NULL
;
650 if (status
== RPC_STATUS_ERROR
) {
651 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
652 free_nfs_cb_data(data
);
655 if (status
== RPC_STATUS_CANCEL
) {
656 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
657 free_nfs_cb_data(data
);
661 if (rpc_nfs3_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
662 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
663 free_nfs_cb_data(data
);
669 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
671 struct nfs_cb_data
*data
= private_data
;
672 struct nfs_context
*nfs
= data
->nfs
;
675 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
677 if (status
== RPC_STATUS_ERROR
) {
678 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
679 free_nfs_cb_data(data
);
682 if (status
== RPC_STATUS_CANCEL
) {
683 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
684 free_nfs_cb_data(data
);
689 if (res
->fhs_status
!= MNT3_OK
) {
690 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
));
691 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
692 free_nfs_cb_data(data
);
696 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
697 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
698 if (nfs
->rootfh
.data
.data_val
== NULL
) {
699 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
700 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
701 free_nfs_cb_data(data
);
704 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
706 rpc_disconnect(rpc
, "normal disconnect");
707 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
708 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
709 free_nfs_cb_data(data
);
712 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
713 rpc_set_autoreconnect(rpc
);
717 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
719 struct nfs_cb_data
*data
= private_data
;
720 struct nfs_context
*nfs
= data
->nfs
;
722 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
724 if (status
== RPC_STATUS_ERROR
) {
725 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
726 free_nfs_cb_data(data
);
729 if (status
== RPC_STATUS_CANCEL
) {
730 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
731 free_nfs_cb_data(data
);
735 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
736 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
737 free_nfs_cb_data(data
);
742 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
744 struct nfs_cb_data
*data
= private_data
;
745 struct nfs_context
*nfs
= data
->nfs
;
747 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
749 /* Dont want any more callbacks even if the socket is closed */
750 rpc
->connect_cb
= NULL
;
752 if (status
== RPC_STATUS_ERROR
) {
753 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
754 free_nfs_cb_data(data
);
757 if (status
== RPC_STATUS_CANCEL
) {
758 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
759 free_nfs_cb_data(data
);
763 if (rpc_mount3_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
764 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
765 free_nfs_cb_data(data
);
770 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
772 struct nfs_cb_data
*data
= private_data
;
773 struct nfs_context
*nfs
= data
->nfs
;
776 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
778 if (status
== RPC_STATUS_ERROR
) {
779 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
780 free_nfs_cb_data(data
);
783 if (status
== RPC_STATUS_CANCEL
) {
784 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
785 free_nfs_cb_data(data
);
789 mount_port
= *(uint32_t *)command_data
;
790 if (mount_port
== 0) {
791 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
792 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
793 free_nfs_cb_data(data
);
797 rpc_disconnect(rpc
, "normal disconnect");
798 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
799 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
800 free_nfs_cb_data(data
);
806 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
808 struct nfs_cb_data
*data
= private_data
;
809 struct nfs_context
*nfs
= data
->nfs
;
811 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
813 if (status
== RPC_STATUS_ERROR
) {
814 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
815 free_nfs_cb_data(data
);
818 if (status
== RPC_STATUS_CANCEL
) {
819 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
820 free_nfs_cb_data(data
);
824 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
825 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
826 free_nfs_cb_data(data
);
831 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
833 struct nfs_cb_data
*data
= private_data
;
834 struct nfs_context
*nfs
= data
->nfs
;
836 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
838 /* Dont want any more callbacks even if the socket is closed */
839 rpc
->connect_cb
= NULL
;
841 if (status
== RPC_STATUS_ERROR
) {
842 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
843 free_nfs_cb_data(data
);
846 if (status
== RPC_STATUS_CANCEL
) {
847 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
848 free_nfs_cb_data(data
);
852 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
853 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
854 free_nfs_cb_data(data
);
860 * Async call for mounting an nfs share and geting the root filehandle
862 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
864 struct nfs_cb_data
*data
;
865 char *new_server
, *new_export
;
867 data
= malloc(sizeof(struct nfs_cb_data
));
869 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
872 memset(data
, 0, sizeof(struct nfs_cb_data
));
873 new_server
= strdup(server
);
874 new_export
= strdup(export
);
875 if (nfs
->server
!= NULL
) {
878 nfs
->server
= new_server
;
879 if (nfs
->export
!= NULL
) {
882 nfs
->export
= new_export
;
885 data
->private_data
= private_data
;
887 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
888 rpc_set_error(nfs
->rpc
, "Failed to start connection");
889 free_nfs_cb_data(data
);
899 * Functions to first look up a path, component by component, and then finally call a specific function once
900 * the filehandle for the final component is found.
902 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
904 struct nfs_cb_data
*data
= private_data
;
905 struct nfs_context
*nfs
= data
->nfs
;
908 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
910 if (status
== RPC_STATUS_ERROR
) {
911 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
912 free_nfs_cb_data(data
);
915 if (status
== RPC_STATUS_CANCEL
) {
916 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
917 free_nfs_cb_data(data
);
922 if (res
->status
!= NFS3_OK
) {
923 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
));
924 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
925 free_nfs_cb_data(data
);
929 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
930 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
931 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
932 free_nfs_cb_data(data
);
937 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
942 while (*data
->path
== '/') {
947 slash
= strchr(path
, '/');
949 /* Clear slash so that path is a zero terminated string for
950 * the current path component. Set it back to '/' again later
951 * when we are finished referencing this component so that
952 * data->saved_path will still point to the full
956 data
->path
= slash
+1;
958 while (*data
->path
!= 0) {
964 data
->fh
.data
.data_len
= fh
->data
.data_len
;
965 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
966 if (data
->fh
.data
.data_val
== NULL
) {
967 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
968 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
969 free_nfs_cb_data(data
);
972 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
976 data
->continue_cb(nfs
, data
);
981 memset(&args
, 0, sizeof(LOOKUP3args
));
983 args
.what
.name
= path
;
985 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
986 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
987 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
988 free_nfs_cb_data(data
);
997 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
1003 while ((str
= strstr(path
, "//"))) {
1011 while ((str
= strstr(path
, "/./"))) {
1018 /* ^/../ -> error */
1019 if (!strncmp(path
, "/../", 4)) {
1020 rpc_set_error(nfs
->rpc
,
1021 "Absolute path starts with '/../' "
1022 "during normalization");
1026 /* ^[^/] -> error */
1027 if (path
[0] != '/') {
1028 rpc_set_error(nfs
->rpc
,
1029 "Absolute path does not start with '/'");
1033 /* /string/../ -> / */
1034 while ((str
= strstr(path
, "/../"))) {
1037 if (!strncmp(path
, "/../", 4)) {
1038 rpc_set_error(nfs
->rpc
,
1039 "Absolute path starts with '/../' "
1040 "during normalization");
1045 while (*tmp
!= '/') {
1049 while((*(tmp
++) = *(str
++)) != '\0')
1056 if (path
[len
- 1] == '/') {
1057 path
[len
- 1] = '\0';
1061 if (path
[0] == '\0') {
1062 rpc_set_error(nfs
->rpc
,
1063 "Absolute path became '' "
1064 "during normalization");
1070 if (!strcmp(&path
[len
- 2], "/.")) {
1071 path
[len
- 2] = '\0';
1076 /* ^/..$ -> error */
1077 if (!strcmp(path
, "/..")) {
1078 rpc_set_error(nfs
->rpc
,
1079 "Absolute path is '/..' "
1080 "during normalization");
1084 /* /string/..$ -> / */
1086 if (!strcmp(&path
[len
- 3], "/..")) {
1087 char *tmp
= &path
[len
- 3];
1088 while (*--tmp
!= '/')
1097 static 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
)
1099 struct nfs_cb_data
*data
;
1101 if (path
[0] == '\0') {
1105 data
= malloc(sizeof(struct nfs_cb_data
));
1107 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1108 "nfs_cb_data structure");
1109 if (free_continue_data
)
1110 free_continue_data(continue_data
);
1113 memset(data
, 0, sizeof(struct nfs_cb_data
));
1116 data
->continue_cb
= continue_cb
;
1117 data
->continue_data
= continue_data
;
1118 data
->free_continue_data
= free_continue_data
;
1119 data
->continue_int
= continue_int
;
1120 data
->private_data
= private_data
;
1121 if (path
[0] == '/') {
1122 data
->saved_path
= strdup(path
);
1124 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1125 if (data
->saved_path
== NULL
) {
1126 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1127 "malloc path string");
1128 free_nfs_cb_data(data
);
1131 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1134 if (data
->saved_path
== NULL
) {
1135 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1136 free_nfs_cb_data(data
);
1139 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1140 free_nfs_cb_data(data
);
1144 data
->path
= data
->saved_path
;
1146 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1147 /* return 0 here since the callback will be invoked if there is a failure */
1160 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1163 struct nfs_cb_data
*data
= private_data
;
1164 struct nfs_context
*nfs
= data
->nfs
;
1171 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1173 if (status
== RPC_STATUS_ERROR
) {
1174 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1175 free_nfs_cb_data(data
);
1178 if (status
== RPC_STATUS_CANCEL
) {
1179 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1180 free_nfs_cb_data(data
);
1185 if (res
->status
!= NFS3_OK
) {
1186 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
));
1187 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1188 free_nfs_cb_data(data
);
1193 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1194 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1195 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1196 st
.st_mode
|= S_IFDIR
;
1198 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1199 st
.st_mode
|= S_IFREG
;
1201 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1202 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1203 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1205 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1207 st
.st_blksize
= 4096;
1208 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1210 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1211 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1212 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1214 data
->cb(0, nfs
, &st
, data
->private_data
);
1215 free_nfs_cb_data(data
);
1218 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1220 struct GETATTR3args args
;
1222 memset(&args
, 0, sizeof(GETATTR3args
));
1223 args
.object
= data
->fh
;
1225 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1226 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1227 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1228 free_nfs_cb_data(data
);
1234 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1236 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1237 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1249 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1251 struct nfs_cb_data
*data
= private_data
;
1252 struct nfs_context
*nfs
= data
->nfs
;
1253 struct nfsfh
*nfsfh
;
1256 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1258 if (status
== RPC_STATUS_ERROR
) {
1259 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1260 free_nfs_cb_data(data
);
1263 if (status
== RPC_STATUS_CANCEL
) {
1264 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1265 free_nfs_cb_data(data
);
1270 if (res
->status
!= NFS3_OK
) {
1271 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1272 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1273 free_nfs_cb_data(data
);
1277 nfsfh
= malloc(sizeof(struct nfsfh
));
1278 if (nfsfh
== NULL
) {
1279 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1280 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1281 free_nfs_cb_data(data
);
1284 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1286 if (data
->continue_int
& O_SYNC
) {
1290 /* steal the filehandle */
1291 nfsfh
->fh
= data
->fh
;
1292 data
->fh
.data
.data_val
= NULL
;
1294 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1295 free_nfs_cb_data(data
);
1298 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1301 struct nfs_cb_data
*data
= private_data
;
1302 struct nfs_context
*nfs
= data
->nfs
;
1303 struct nfsfh
*nfsfh
;
1304 unsigned int nfsmode
= 0;
1306 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1308 if (status
== RPC_STATUS_ERROR
) {
1309 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1310 free_nfs_cb_data(data
);
1313 if (status
== RPC_STATUS_CANCEL
) {
1314 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1315 free_nfs_cb_data(data
);
1320 if (res
->status
!= NFS3_OK
) {
1321 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
));
1322 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1323 free_nfs_cb_data(data
);
1327 if (data
->continue_int
& O_WRONLY
) {
1328 nfsmode
|= ACCESS3_MODIFY
;
1330 if (data
->continue_int
& O_RDWR
) {
1331 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1333 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1334 nfsmode
|= ACCESS3_READ
;
1338 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1339 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1340 nfsmode
&ACCESS3_READ
?'r':'-',
1341 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1342 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1343 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1344 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1345 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1346 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1347 free_nfs_cb_data(data
);
1351 /* Try to truncate it if we were requested to */
1352 if ((data
->continue_int
& O_TRUNC
) &&
1353 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1356 memset(&args
, 0, sizeof(SETATTR3args
));
1357 args
.object
= data
->fh
;
1358 args
.new_attributes
.size
.set_it
= 1;
1359 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1361 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1363 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1364 "SETATTR call for %s", data
->path
);
1365 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1366 data
->private_data
);
1367 free_nfs_cb_data(data
);
1373 nfsfh
= malloc(sizeof(struct nfsfh
));
1374 if (nfsfh
== NULL
) {
1375 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1376 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1377 free_nfs_cb_data(data
);
1380 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1382 if (data
->continue_int
& O_SYNC
) {
1386 /* steal the filehandle */
1387 nfsfh
->fh
= data
->fh
;
1388 data
->fh
.data
.data_val
= NULL
;
1390 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1391 free_nfs_cb_data(data
);
1394 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1399 if (data
->continue_int
& O_WRONLY
) {
1400 nfsmode
|= ACCESS3_MODIFY
;
1402 if (data
->continue_int
& O_RDWR
) {
1403 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1405 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1406 nfsmode
|= ACCESS3_READ
;
1409 memset(&args
, 0, sizeof(ACCESS3args
));
1410 args
.object
= data
->fh
;
1411 args
.access
= nfsmode
;
1413 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1414 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1415 "call for %s", data
->path
);
1416 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1417 data
->private_data
);
1418 free_nfs_cb_data(data
);
1424 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1426 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1427 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1438 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1440 /* steal saved_path */
1442 nfs
->cwd
= data
->saved_path
;
1443 data
->saved_path
= NULL
;
1445 data
->cb(0, nfs
, NULL
, data
->private_data
);
1446 free_nfs_cb_data(data
);
1451 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1453 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1454 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1465 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1467 struct nfs_cb_data
*data
= private_data
;
1468 struct nfs_context
*nfs
= data
->nfs
;
1471 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1473 if (status
== RPC_STATUS_ERROR
) {
1474 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1475 free_nfs_cb_data(data
);
1478 if (status
== RPC_STATUS_CANCEL
) {
1479 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1480 free_nfs_cb_data(data
);
1485 if (res
->status
!= NFS3_OK
) {
1486 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1487 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1488 free_nfs_cb_data(data
);
1492 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1493 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1494 free_nfs_cb_data(data
);
1497 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1499 struct nfs_mcb_data
*mdata
= private_data
;
1500 struct nfs_cb_data
*data
= mdata
->data
;
1501 struct nfs_context
*nfs
= data
->nfs
;
1504 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1508 if (status
== RPC_STATUS_ERROR
) {
1509 /* flag the failure but do not invoke callback until we have received all responses */
1512 if (status
== RPC_STATUS_CANCEL
) {
1513 /* flag the cancellation but do not invoke callback until we have received all responses */
1517 /* reassemble the data into the buffer */
1518 if (status
== RPC_STATUS_SUCCESS
) {
1520 if (res
->status
!= NFS3_OK
) {
1521 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1524 if (res
->READ3res_u
.resok
.count
> 0) {
1525 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1526 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1527 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1533 if (data
->num_calls
> 0) {
1534 /* still waiting for more replies */
1539 if (data
->error
!= 0) {
1540 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1541 free_nfs_cb_data(data
);
1545 if (data
->cancel
!= 0) {
1546 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1547 free_nfs_cb_data(data
);
1552 data
->nfsfh
->offset
= data
->max_offset
;
1553 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1555 free_nfs_cb_data(data
);
1559 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1561 struct nfs_cb_data
*data
;
1563 data
= malloc(sizeof(struct nfs_cb_data
));
1565 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1568 memset(data
, 0, sizeof(struct nfs_cb_data
));
1571 data
->private_data
= private_data
;
1572 data
->nfsfh
= nfsfh
;
1574 nfsfh
->offset
= offset
;
1576 if (count
<= nfs_get_readmax(nfs
)) {
1579 memset(&args
, 0, sizeof(READ3args
));
1580 args
.file
= nfsfh
->fh
;
1581 args
.offset
= offset
;
1584 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_cb
, &args
, data
) != 0) {
1585 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1586 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1587 free_nfs_cb_data(data
);
1593 /* trying to read more than maximum server read size, we has to chop it up into smaller
1594 * reads and collect into a reassembly buffer.
1595 * we send all reads in parallel so that performance is still good.
1597 data
->max_offset
= offset
;
1598 data
->start_offset
= offset
;
1600 data
->buffer
= malloc(count
);
1601 if (data
->buffer
== NULL
) {
1602 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1603 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1604 free_nfs_cb_data(data
);
1609 uint64_t readcount
= count
;
1610 struct nfs_mcb_data
*mdata
;
1613 if (readcount
> nfs_get_readmax(nfs
)) {
1614 readcount
= nfs_get_readmax(nfs
);
1617 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1618 if (mdata
== NULL
) {
1619 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1622 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1624 mdata
->offset
= offset
;
1625 mdata
->count
= readcount
;
1627 memset(&args
, 0, sizeof(READ3args
));
1628 args
.file
= nfsfh
->fh
;
1629 args
.offset
= offset
;
1630 args
.count
= readcount
;
1632 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1633 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1634 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1640 offset
+= readcount
;
1650 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1652 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1660 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1662 struct nfs_cb_data
*data
= private_data
;
1663 struct nfs_context
*nfs
= data
->nfs
;
1666 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1668 if (status
== RPC_STATUS_ERROR
) {
1669 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1670 free_nfs_cb_data(data
);
1673 if (status
== RPC_STATUS_CANCEL
) {
1674 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1675 free_nfs_cb_data(data
);
1680 if (res
->status
!= NFS3_OK
) {
1681 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1682 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1683 free_nfs_cb_data(data
);
1687 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1688 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1689 free_nfs_cb_data(data
);
1692 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1694 struct nfs_mcb_data
*mdata
= private_data
;
1695 struct nfs_cb_data
*data
= mdata
->data
;
1696 struct nfs_context
*nfs
= data
->nfs
;
1699 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1703 if (status
== RPC_STATUS_ERROR
) {
1704 /* flag the failure but do not invoke callback until we have received all responses */
1707 if (status
== RPC_STATUS_CANCEL
) {
1708 /* flag the cancellation but do not invoke callback until we have received all responses */
1712 if (status
== RPC_STATUS_SUCCESS
) {
1714 if (res
->status
!= NFS3_OK
) {
1715 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1718 if (res
->WRITE3res_u
.resok
.count
> 0) {
1719 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1720 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1726 if (data
->num_calls
> 0) {
1727 /* still waiting for more replies */
1732 if (data
->error
!= 0) {
1733 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1734 free_nfs_cb_data(data
);
1738 if (data
->cancel
!= 0) {
1739 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1740 free_nfs_cb_data(data
);
1745 data
->nfsfh
->offset
= data
->max_offset
;
1746 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1748 free_nfs_cb_data(data
);
1753 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
)
1755 struct nfs_cb_data
*data
;
1757 data
= malloc(sizeof(struct nfs_cb_data
));
1759 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1762 memset(data
, 0, sizeof(struct nfs_cb_data
));
1765 data
->private_data
= private_data
;
1766 data
->nfsfh
= nfsfh
;
1768 nfsfh
->offset
= offset
;
1770 if (count
<= nfs_get_writemax(nfs
)) {
1773 memset(&args
, 0, sizeof(WRITE3args
));
1774 args
.file
= nfsfh
->fh
;
1775 args
.offset
= offset
;
1777 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1778 args
.data
.data_len
= count
;
1779 args
.data
.data_val
= buf
;
1781 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_cb
, &args
, data
) != 0) {
1782 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1783 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1784 free_nfs_cb_data(data
);
1790 /* trying to write more than maximum server write size, we has to chop it up into smaller
1792 * we send all writes in parallel so that performance is still good.
1794 data
->max_offset
= offset
;
1795 data
->start_offset
= offset
;
1798 uint64_t writecount
= count
;
1799 struct nfs_mcb_data
*mdata
;
1802 if (writecount
> nfs_get_writemax(nfs
)) {
1803 writecount
= nfs_get_writemax(nfs
);
1806 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1807 if (mdata
== NULL
) {
1808 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1811 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1813 mdata
->offset
= offset
;
1814 mdata
->count
= writecount
;
1816 memset(&args
, 0, sizeof(WRITE3args
));
1817 args
.file
= nfsfh
->fh
;
1818 args
.offset
= offset
;
1819 args
.count
= writecount
;
1820 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1821 args
.data
.data_len
= writecount
;
1822 args
.data
.data_val
= &buf
[offset
- data
->start_offset
];
1824 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1825 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1826 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1831 count
-= writecount
;
1832 offset
+= writecount
;
1842 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1844 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1854 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1856 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1857 free(nfsfh
->fh
.data
.data_val
);
1858 nfsfh
->fh
.data
.data_val
= NULL
;
1862 cb(0, nfs
, NULL
, private_data
);
1873 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1875 struct nfs_cb_data
*data
;
1876 struct GETATTR3args args
;
1878 data
= malloc(sizeof(struct nfs_cb_data
));
1880 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1883 memset(data
, 0, sizeof(struct nfs_cb_data
));
1886 data
->private_data
= private_data
;
1888 memset(&args
, 0, sizeof(GETATTR3args
));
1889 args
.object
= nfsfh
->fh
;
1891 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1892 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1893 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1894 free_nfs_cb_data(data
);
1905 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1907 struct nfs_cb_data
*data
= private_data
;
1908 struct nfs_context
*nfs
= data
->nfs
;
1911 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1913 if (status
== RPC_STATUS_ERROR
) {
1914 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1915 free_nfs_cb_data(data
);
1918 if (status
== RPC_STATUS_CANCEL
) {
1919 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1920 free_nfs_cb_data(data
);
1925 if (res
->status
!= NFS3_OK
) {
1926 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1927 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1928 free_nfs_cb_data(data
);
1932 data
->cb(0, nfs
, NULL
, data
->private_data
);
1933 free_nfs_cb_data(data
);
1936 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1938 struct nfs_cb_data
*data
;
1939 struct COMMIT3args args
;
1941 data
= malloc(sizeof(struct nfs_cb_data
));
1943 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1946 memset(data
, 0, sizeof(struct nfs_cb_data
));
1949 data
->private_data
= private_data
;
1951 args
.file
= nfsfh
->fh
;
1954 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1955 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1956 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1957 free_nfs_cb_data(data
);
1969 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1971 struct nfs_cb_data
*data
= private_data
;
1972 struct nfs_context
*nfs
= data
->nfs
;
1975 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1977 if (status
== RPC_STATUS_ERROR
) {
1978 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1979 free_nfs_cb_data(data
);
1982 if (status
== RPC_STATUS_CANCEL
) {
1983 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1984 free_nfs_cb_data(data
);
1989 if (res
->status
!= NFS3_OK
) {
1990 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1991 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1992 free_nfs_cb_data(data
);
1996 data
->cb(0, nfs
, NULL
, data
->private_data
);
1997 free_nfs_cb_data(data
);
2000 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
2002 struct nfs_cb_data
*data
;
2005 data
= malloc(sizeof(struct nfs_cb_data
));
2007 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
2010 memset(data
, 0, sizeof(struct nfs_cb_data
));
2013 data
->private_data
= private_data
;
2015 memset(&args
, 0, sizeof(SETATTR3args
));
2016 args
.object
= nfsfh
->fh
;
2017 args
.new_attributes
.size
.set_it
= 1;
2018 args
.new_attributes
.size
.set_size3_u
.size
= length
;
2020 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
2021 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2022 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2023 free_nfs_cb_data(data
);
2033 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2035 uint64_t offset
= data
->continue_int
;
2038 nfsfh
.fh
= data
->fh
;
2040 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
2041 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2042 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2043 free_nfs_cb_data(data
);
2046 free_nfs_cb_data(data
);
2050 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2056 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2057 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2070 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2073 struct nfs_cb_data
*data
= private_data
;
2074 struct nfs_context
*nfs
= data
->nfs
;
2075 char *str
= data
->continue_data
;
2077 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2079 str
= &str
[strlen(str
) + 1];
2081 if (status
== RPC_STATUS_ERROR
) {
2082 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2083 free_nfs_cb_data(data
);
2086 if (status
== RPC_STATUS_CANCEL
) {
2087 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2088 free_nfs_cb_data(data
);
2093 if (res
->status
!= NFS3_OK
) {
2094 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
));
2095 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2096 free_nfs_cb_data(data
);
2100 data
->cb(0, nfs
, NULL
, data
->private_data
);
2101 free_nfs_cb_data(data
);
2104 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2106 char *str
= data
->continue_data
;
2109 str
= &str
[strlen(str
) + 1];
2111 memset(&args
, 0, sizeof(MKDIR3args
));
2112 args
.where
.dir
= data
->fh
;
2113 args
.where
.name
= str
;
2114 args
.attributes
.mode
.set_it
= 1;
2115 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2117 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2118 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2119 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2120 free_nfs_cb_data(data
);
2126 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2131 new_path
= strdup(path
);
2132 if (new_path
== NULL
) {
2133 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2137 ptr
= strrchr(new_path
, '/');
2139 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2144 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2145 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2146 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2160 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2163 struct nfs_cb_data
*data
= private_data
;
2164 struct nfs_context
*nfs
= data
->nfs
;
2165 char *str
= data
->continue_data
;
2167 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2169 str
= &str
[strlen(str
) + 1];
2171 if (status
== RPC_STATUS_ERROR
) {
2172 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2173 free_nfs_cb_data(data
);
2176 if (status
== RPC_STATUS_CANCEL
) {
2177 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2178 free_nfs_cb_data(data
);
2183 if (res
->status
!= NFS3_OK
) {
2184 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
));
2185 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2186 free_nfs_cb_data(data
);
2190 data
->cb(0, nfs
, NULL
, data
->private_data
);
2191 free_nfs_cb_data(data
);
2194 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2196 char *str
= data
->continue_data
;
2199 str
= &str
[strlen(str
) + 1];
2201 args
.object
.dir
= data
->fh
;
2202 args
.object
.name
= str
;
2203 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2204 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2205 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2206 free_nfs_cb_data(data
);
2212 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2217 new_path
= strdup(path
);
2218 if (new_path
== NULL
) {
2219 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2223 ptr
= strrchr(new_path
, '/');
2225 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2230 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2231 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2232 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2245 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2248 struct nfs_cb_data
*data
= private_data
;
2249 struct nfs_context
*nfs
= data
->nfs
;
2250 struct nfsfh
*nfsfh
;
2251 char *str
= data
->continue_data
;
2253 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2255 if (status
== RPC_STATUS_ERROR
) {
2256 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2257 free_nfs_cb_data(data
);
2260 if (status
== RPC_STATUS_CANCEL
) {
2261 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2262 free_nfs_cb_data(data
);
2266 str
= &str
[strlen(str
) + 1];
2268 if (res
->status
!= NFS3_OK
) {
2269 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
));
2270 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2275 nfsfh
= malloc(sizeof(struct nfsfh
));
2276 if (nfsfh
== NULL
) {
2277 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2278 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2279 free_nfs_cb_data(data
);
2282 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2284 /* copy the filehandle */
2285 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2286 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2287 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2289 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2290 free_nfs_cb_data(data
);
2295 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2298 struct nfs_cb_data
*data
= private_data
;
2299 struct nfs_context
*nfs
= data
->nfs
;
2300 char *str
= data
->continue_data
;
2303 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2305 if (status
== RPC_STATUS_ERROR
) {
2306 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2307 free_nfs_cb_data(data
);
2310 if (status
== RPC_STATUS_CANCEL
) {
2311 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2312 free_nfs_cb_data(data
);
2316 str
= &str
[strlen(str
) + 1];
2318 if (res
->status
!= NFS3_OK
) {
2319 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
));
2320 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2321 free_nfs_cb_data(data
);
2325 memset(&args
, 0, sizeof(LOOKUP3args
));
2326 args
.what
.dir
= data
->fh
;
2327 args
.what
.name
= str
;
2329 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2330 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2331 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2332 free_nfs_cb_data(data
);
2338 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2340 char *str
= data
->continue_data
;
2343 str
= &str
[strlen(str
) + 1];
2345 memset(&args
, 0, sizeof(CREATE3args
));
2346 args
.where
.dir
= data
->fh
;
2347 args
.where
.name
= str
;
2348 args
.how
.mode
= UNCHECKED
;
2349 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2350 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2352 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2353 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2354 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2355 free_nfs_cb_data(data
);
2361 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2366 new_path
= strdup(path
);
2367 if (new_path
== NULL
) {
2368 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2372 ptr
= strrchr(new_path
, '/');
2374 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2380 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2381 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2382 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2395 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2398 struct nfs_cb_data
*data
= private_data
;
2399 struct nfs_context
*nfs
= data
->nfs
;
2400 char *str
= data
->continue_data
;
2402 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2404 str
= &str
[strlen(str
) + 1];
2406 if (status
== RPC_STATUS_ERROR
) {
2407 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2408 free_nfs_cb_data(data
);
2411 if (status
== RPC_STATUS_CANCEL
) {
2412 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2413 free_nfs_cb_data(data
);
2418 if (res
->status
!= NFS3_OK
) {
2419 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
));
2420 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2421 free_nfs_cb_data(data
);
2425 data
->cb(0, nfs
, NULL
, data
->private_data
);
2426 free_nfs_cb_data(data
);
2429 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2431 char *str
= data
->continue_data
;
2432 struct REMOVE3args args
;
2434 str
= &str
[strlen(str
) + 1];
2436 args
.object
.dir
= data
->fh
;
2437 args
.object
.name
= str
;
2438 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2439 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2440 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2441 free_nfs_cb_data(data
);
2447 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2452 new_path
= strdup(path
);
2453 if (new_path
== NULL
) {
2454 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2458 ptr
= strrchr(new_path
, '/');
2460 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2465 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2466 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2467 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2478 struct mknod_cb_data
{
2485 static void free_mknod_cb_data(void *ptr
)
2487 struct mknod_cb_data
*data
= ptr
;
2493 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2496 struct nfs_cb_data
*data
= private_data
;
2497 struct nfs_context
*nfs
= data
->nfs
;
2498 char *str
= data
->continue_data
;
2500 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2502 str
= &str
[strlen(str
) + 1];
2504 if (status
== RPC_STATUS_ERROR
) {
2505 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2506 free_nfs_cb_data(data
);
2509 if (status
== RPC_STATUS_CANCEL
) {
2510 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2511 free_nfs_cb_data(data
);
2516 if (res
->status
!= NFS3_OK
) {
2517 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
));
2518 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2519 free_nfs_cb_data(data
);
2523 data
->cb(0, nfs
, NULL
, data
->private_data
);
2524 free_nfs_cb_data(data
);
2527 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2529 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2530 char *str
= cb_data
->path
;
2533 str
= &str
[strlen(str
) + 1];
2535 args
.where
.dir
= data
->fh
;
2536 args
.where
.name
= str
;
2537 switch (cb_data
->mode
& S_IFMT
) {
2539 args
.what
.type
= NF3CHR
;
2540 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2541 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
);
2542 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2543 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2546 args
.what
.type
= NF3BLK
;
2547 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2548 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
);
2549 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2550 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2552 args
.what
.type
= NF3SOCK
;
2553 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2554 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
);
2557 args
.what
.type
= NF3FIFO
;
2558 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2559 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
);
2562 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2563 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2564 free_nfs_cb_data(data
);
2568 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2569 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2570 free_nfs_cb_data(data
);
2576 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2579 struct mknod_cb_data
*cb_data
;
2581 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2582 if (cb_data
== NULL
) {
2583 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2587 cb_data
->path
= strdup(path
);
2588 if (cb_data
->path
== NULL
) {
2589 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2594 ptr
= strrchr(cb_data
->path
, '/');
2596 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2597 free_mknod_cb_data(cb_data
);
2602 cb_data
->mode
= mode
;
2603 cb_data
->major
= major(dev
);
2604 cb_data
->minor
= minor(dev
);
2606 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2607 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2608 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2609 free_mknod_cb_data(cb_data
);
2620 /* ReadDirPlus Emulation Callback data */
2621 struct rdpe_cb_data
{
2624 struct nfs_cb_data
*data
;
2627 /* ReadDirPlus Emulation LOOKUP Callback data */
2628 struct rdpe_lookup_cb_data
{
2629 struct rdpe_cb_data
*rdpe_cb_data
;
2630 struct nfsdirent
*nfsdirent
;
2633 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2634 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2636 LOOKUP3res
*res
= command_data
;
2637 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2638 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2639 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2640 struct nfsdir
*nfsdir
= data
->continue_data
;
2641 struct nfs_context
*nfs
= data
->nfs
;
2642 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2644 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2646 free(rdpe_lookup_cb_data
);
2648 rdpe_cb_data
->getattrcount
--;
2650 if (status
== RPC_STATUS_ERROR
) {
2651 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2653 if (status
== RPC_STATUS_CANCEL
) {
2654 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2656 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2657 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2659 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2660 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2661 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2663 nfsdirent
->type
= attributes
->type
;
2664 nfsdirent
->mode
= attributes
->mode
;
2665 nfsdirent
->size
= attributes
->size
;
2667 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2668 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2669 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2670 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2671 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2672 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2673 nfsdirent
->uid
= attributes
->uid
;
2674 nfsdirent
->gid
= attributes
->gid
;
2678 if (rdpe_cb_data
->getattrcount
== 0) {
2679 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2680 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2681 nfs_free_nfsdir(nfsdir
);
2683 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2687 data
->continue_data
= NULL
;
2688 free_nfs_cb_data(data
);
2692 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2694 READDIR3res
*res
= command_data
;
2695 struct nfs_cb_data
*data
= private_data
;
2696 struct nfs_context
*nfs
= data
->nfs
;
2697 struct nfsdir
*nfsdir
= data
->continue_data
;
2698 struct nfsdirent
*nfsdirent
;
2699 struct entry3
*entry
;
2700 uint64_t cookie
= 0;
2701 struct rdpe_cb_data
*rdpe_cb_data
;
2703 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2705 if (status
== RPC_STATUS_ERROR
) {
2706 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2707 nfs_free_nfsdir(nfsdir
);
2708 data
->continue_data
= NULL
;
2709 free_nfs_cb_data(data
);
2713 if (status
== RPC_STATUS_CANCEL
) {
2714 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2715 nfs_free_nfsdir(nfsdir
);
2716 data
->continue_data
= NULL
;
2717 free_nfs_cb_data(data
);
2721 if (res
->status
!= NFS3_OK
) {
2722 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
));
2723 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2724 nfs_free_nfsdir(nfsdir
);
2725 data
->continue_data
= NULL
;
2726 free_nfs_cb_data(data
);
2730 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2731 while (entry
!= NULL
) {
2732 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2733 if (nfsdirent
== NULL
) {
2734 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2735 nfs_free_nfsdir(nfsdir
);
2736 data
->continue_data
= NULL
;
2737 free_nfs_cb_data(data
);
2740 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2741 nfsdirent
->name
= strdup(entry
->name
);
2742 if (nfsdirent
->name
== NULL
) {
2743 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2744 nfs_free_nfsdir(nfsdir
);
2745 data
->continue_data
= NULL
;
2746 free_nfs_cb_data(data
);
2749 nfsdirent
->inode
= entry
->fileid
;
2751 nfsdirent
->next
= nfsdir
->entries
;
2752 nfsdir
->entries
= nfsdirent
;
2754 cookie
= entry
->cookie
;
2755 entry
= entry
->nextentry
;
2758 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2761 args
.dir
= data
->fh
;
2762 args
.cookie
= cookie
;
2763 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2766 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2767 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2768 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2769 nfs_free_nfsdir(nfsdir
);
2770 data
->continue_data
= NULL
;
2771 free_nfs_cb_data(data
);
2777 /* steal the dirhandle */
2778 nfsdir
->current
= nfsdir
->entries
;
2780 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2781 rdpe_cb_data
->getattrcount
= 0;
2782 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2783 rdpe_cb_data
->data
= data
;
2784 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2785 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2788 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2789 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2790 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2792 memset(&args
, 0, sizeof(LOOKUP3args
));
2793 args
.what
.dir
= data
->fh
;
2794 args
.what
.name
= nfsdirent
->name
;
2796 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2797 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2799 /* if we have already commands in flight, we cant just stop, we have to wait for the
2800 * commands in flight to complete
2802 if (rdpe_cb_data
->getattrcount
> 0) {
2803 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2804 free(rdpe_lookup_cb_data
);
2808 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2809 nfs_free_nfsdir(nfsdir
);
2810 data
->continue_data
= NULL
;
2811 free_nfs_cb_data(data
);
2812 free(rdpe_lookup_cb_data
);
2816 rdpe_cb_data
->getattrcount
++;
2821 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2823 READDIRPLUS3res
*res
= command_data
;
2824 struct nfs_cb_data
*data
= private_data
;
2825 struct nfs_context
*nfs
= data
->nfs
;
2826 struct nfsdir
*nfsdir
= data
->continue_data
;
2827 struct entryplus3
*entry
;
2828 uint64_t cookie
= 0;
2830 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2832 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2835 args
.dir
= data
->fh
;
2836 args
.cookie
= cookie
;
2837 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2840 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2841 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2842 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2843 nfs_free_nfsdir(nfsdir
);
2844 data
->continue_data
= NULL
;
2845 free_nfs_cb_data(data
);
2851 if (status
== RPC_STATUS_CANCEL
) {
2852 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2853 nfs_free_nfsdir(nfsdir
);
2854 data
->continue_data
= NULL
;
2855 free_nfs_cb_data(data
);
2859 if (res
->status
!= NFS3_OK
) {
2860 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
));
2861 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2862 nfs_free_nfsdir(nfsdir
);
2863 data
->continue_data
= NULL
;
2864 free_nfs_cb_data(data
);
2868 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2869 while (entry
!= NULL
) {
2870 struct nfsdirent
*nfsdirent
;
2872 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2873 if (nfsdirent
== NULL
) {
2874 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2875 nfs_free_nfsdir(nfsdir
);
2876 data
->continue_data
= NULL
;
2877 free_nfs_cb_data(data
);
2880 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2881 nfsdirent
->name
= strdup(entry
->name
);
2882 if (nfsdirent
->name
== NULL
) {
2883 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2885 nfs_free_nfsdir(nfsdir
);
2886 data
->continue_data
= NULL
;
2887 free_nfs_cb_data(data
);
2890 nfsdirent
->inode
= entry
->fileid
;
2891 if (entry
->name_attributes
.attributes_follow
) {
2892 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2893 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2894 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2896 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2897 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2898 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2899 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2900 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2901 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2902 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2903 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2906 nfsdirent
->next
= nfsdir
->entries
;
2907 nfsdir
->entries
= nfsdirent
;
2909 cookie
= entry
->cookie
;
2910 entry
= entry
->nextentry
;
2913 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2914 READDIRPLUS3args args
;
2916 args
.dir
= data
->fh
;
2917 args
.cookie
= cookie
;
2918 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2919 args
.dircount
= 8192;
2920 args
.maxcount
= 8192;
2922 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2923 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2924 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2925 nfs_free_nfsdir(nfsdir
);
2926 data
->continue_data
= NULL
;
2927 free_nfs_cb_data(data
);
2933 /* steal the dirhandle */
2934 data
->continue_data
= NULL
;
2935 nfsdir
->current
= nfsdir
->entries
;
2937 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2938 free_nfs_cb_data(data
);
2941 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2943 READDIRPLUS3args args
;
2945 args
.dir
= data
->fh
;
2947 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2948 args
.dircount
= 8192;
2949 args
.maxcount
= 8192;
2950 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2951 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2952 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2953 free_nfs_cb_data(data
);
2959 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2961 struct nfsdir
*nfsdir
;
2963 nfsdir
= malloc(sizeof(struct nfsdir
));
2964 if (nfsdir
== NULL
) {
2965 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2968 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2970 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2971 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2979 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2981 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2983 if (nfsdir
->current
!= NULL
) {
2984 nfsdir
->current
= nfsdir
->current
->next
;
2993 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2995 nfs_free_nfsdir(nfsdir
);
3002 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
3013 struct lseek_cb_data
{
3014 struct nfs_context
*nfs
;
3015 struct nfsfh
*nfsfh
;
3021 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3024 struct lseek_cb_data
*data
= private_data
;
3025 struct nfs_context
*nfs
= data
->nfs
;
3027 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3029 if (status
== RPC_STATUS_ERROR
) {
3030 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3034 if (status
== RPC_STATUS_CANCEL
) {
3035 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3041 if (res
->status
!= NFS3_OK
) {
3042 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3043 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3048 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3049 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3053 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3055 struct lseek_cb_data
*data
;
3056 struct GETATTR3args args
;
3058 if (whence
== SEEK_SET
) {
3059 nfsfh
->offset
= offset
;
3060 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3063 if (whence
== SEEK_CUR
) {
3064 nfsfh
->offset
+= offset
;
3065 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3069 data
= malloc(sizeof(struct lseek_cb_data
));
3071 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3076 data
->nfsfh
= nfsfh
;
3077 data
->offset
= offset
;
3079 data
->private_data
= private_data
;
3081 memset(&args
, 0, sizeof(GETATTR3args
));
3082 args
.object
= nfsfh
->fh
;
3084 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3085 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3098 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3101 struct nfs_cb_data
*data
= private_data
;
3102 struct nfs_context
*nfs
= data
->nfs
;
3103 struct statvfs svfs
;
3105 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3107 if (status
== RPC_STATUS_ERROR
) {
3108 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3109 free_nfs_cb_data(data
);
3112 if (status
== RPC_STATUS_CANCEL
) {
3113 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3114 free_nfs_cb_data(data
);
3119 if (res
->status
!= NFS3_OK
) {
3120 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
));
3121 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3122 free_nfs_cb_data(data
);
3126 svfs
.f_bsize
= 4096;
3127 svfs
.f_frsize
= 4096;
3128 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3129 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3130 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3131 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3132 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3133 #if !defined(ANDROID)
3134 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3137 svfs
.f_namemax
= 256;
3140 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3141 free_nfs_cb_data(data
);
3144 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3148 args
.fsroot
= data
->fh
;
3149 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3150 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3151 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3152 free_nfs_cb_data(data
);
3158 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3160 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3161 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3174 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3177 struct nfs_cb_data
*data
= private_data
;
3178 struct nfs_context
*nfs
= data
->nfs
;
3180 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3182 if (status
== RPC_STATUS_ERROR
) {
3183 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3184 free_nfs_cb_data(data
);
3187 if (status
== RPC_STATUS_CANCEL
) {
3188 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3189 free_nfs_cb_data(data
);
3194 if (res
->status
!= NFS3_OK
) {
3195 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
));
3196 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3197 free_nfs_cb_data(data
);
3202 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3203 free_nfs_cb_data(data
);
3206 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3210 args
.symlink
= data
->fh
;
3212 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3213 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3214 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3215 free_nfs_cb_data(data
);
3221 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3223 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3224 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3237 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3239 struct nfs_cb_data
*data
= private_data
;
3240 struct nfs_context
*nfs
= data
->nfs
;
3243 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3245 if (status
== RPC_STATUS_ERROR
) {
3246 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3247 free_nfs_cb_data(data
);
3250 if (status
== RPC_STATUS_CANCEL
) {
3251 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3252 free_nfs_cb_data(data
);
3257 if (res
->status
!= NFS3_OK
) {
3258 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3259 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3260 free_nfs_cb_data(data
);
3264 data
->cb(0, nfs
, NULL
, data
->private_data
);
3265 free_nfs_cb_data(data
);
3268 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3272 memset(&args
, 0, sizeof(SETATTR3args
));
3273 args
.object
= data
->fh
;
3274 args
.new_attributes
.mode
.set_it
= 1;
3275 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3277 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3278 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3279 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3280 free_nfs_cb_data(data
);
3287 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3289 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3290 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3300 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3302 struct nfs_cb_data
*data
;
3304 data
= malloc(sizeof(struct nfs_cb_data
));
3306 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3309 memset(data
, 0, sizeof(struct nfs_cb_data
));
3312 data
->private_data
= private_data
;
3313 data
->continue_int
= mode
;
3314 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3315 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3316 if (data
->fh
.data
.data_val
== NULL
) {
3317 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3318 free_nfs_cb_data(data
);
3321 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3323 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3324 free_nfs_cb_data(data
);
3336 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3338 struct nfs_cb_data
*data
= private_data
;
3339 struct nfs_context
*nfs
= data
->nfs
;
3342 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3344 if (status
== RPC_STATUS_ERROR
) {
3345 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3346 free_nfs_cb_data(data
);
3349 if (status
== RPC_STATUS_CANCEL
) {
3350 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3351 free_nfs_cb_data(data
);
3356 if (res
->status
!= NFS3_OK
) {
3357 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3358 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3359 free_nfs_cb_data(data
);
3363 data
->cb(0, nfs
, NULL
, data
->private_data
);
3364 free_nfs_cb_data(data
);
3367 struct nfs_chown_data
{
3372 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3375 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3377 memset(&args
, 0, sizeof(SETATTR3args
));
3378 args
.object
= data
->fh
;
3379 if (chown_data
->uid
!= (uid_t
)-1) {
3380 args
.new_attributes
.uid
.set_it
= 1;
3381 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3383 if (chown_data
->gid
!= (gid_t
)-1) {
3384 args
.new_attributes
.gid
.set_it
= 1;
3385 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3388 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3389 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3390 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3391 free_nfs_cb_data(data
);
3398 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3400 struct nfs_chown_data
*chown_data
;
3402 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3403 if (chown_data
== NULL
) {
3404 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3408 chown_data
->uid
= uid
;
3409 chown_data
->gid
= gid
;
3411 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3412 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3423 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3425 struct nfs_cb_data
*data
;
3426 struct nfs_chown_data
*chown_data
;
3428 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3429 if (chown_data
== NULL
) {
3430 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3434 chown_data
->uid
= uid
;
3435 chown_data
->gid
= gid
;
3437 data
= malloc(sizeof(struct nfs_cb_data
));
3439 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3443 memset(data
, 0, sizeof(struct nfs_cb_data
));
3446 data
->private_data
= private_data
;
3447 data
->continue_data
= chown_data
;
3448 data
->free_continue_data
= free
;
3449 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3450 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3451 if (data
->fh
.data
.data_val
== NULL
) {
3452 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3453 free_nfs_cb_data(data
);
3456 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3458 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3459 free_nfs_cb_data(data
);
3473 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3475 struct nfs_cb_data
*data
= private_data
;
3476 struct nfs_context
*nfs
= data
->nfs
;
3479 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3481 if (status
== RPC_STATUS_ERROR
) {
3482 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3483 free_nfs_cb_data(data
);
3486 if (status
== RPC_STATUS_CANCEL
) {
3487 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3488 free_nfs_cb_data(data
);
3493 if (res
->status
!= NFS3_OK
) {
3494 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3495 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3496 free_nfs_cb_data(data
);
3500 data
->cb(0, nfs
, NULL
, data
->private_data
);
3501 free_nfs_cb_data(data
);
3504 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3507 struct timeval
*utimes_data
= data
->continue_data
;
3509 memset(&args
, 0, sizeof(SETATTR3args
));
3510 args
.object
= data
->fh
;
3511 if (utimes_data
!= NULL
) {
3512 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3513 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3514 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3515 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3516 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3517 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3519 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3520 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3523 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3524 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3525 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3526 free_nfs_cb_data(data
);
3533 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3535 struct timeval
*new_times
= NULL
;
3537 if (times
!= NULL
) {
3538 new_times
= malloc(sizeof(struct timeval
)*2);
3539 if (new_times
== NULL
) {
3540 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3544 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3547 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3548 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3558 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3560 struct timeval
*new_times
= NULL
;
3562 if (times
!= NULL
) {
3563 new_times
= malloc(sizeof(struct timeval
)*2);
3564 if (new_times
== NULL
) {
3565 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3569 new_times
[0].tv_sec
= times
->actime
;
3570 new_times
[0].tv_usec
= 0;
3571 new_times
[1].tv_sec
= times
->modtime
;
3572 new_times
[1].tv_usec
= 0;
3575 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3576 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3587 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3590 struct nfs_cb_data
*data
= private_data
;
3591 struct nfs_context
*nfs
= data
->nfs
;
3592 unsigned int nfsmode
= 0;
3594 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3596 if (status
== RPC_STATUS_ERROR
) {
3597 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3598 free_nfs_cb_data(data
);
3601 if (status
== RPC_STATUS_CANCEL
) {
3602 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3603 free_nfs_cb_data(data
);
3608 if (res
->status
!= NFS3_OK
) {
3609 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
));
3610 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3611 free_nfs_cb_data(data
);
3615 if (data
->continue_int
& R_OK
) {
3616 nfsmode
|= ACCESS3_READ
;
3618 if (data
->continue_int
& W_OK
) {
3619 nfsmode
|= ACCESS3_MODIFY
;
3621 if (data
->continue_int
& X_OK
) {
3622 nfsmode
|= ACCESS3_EXECUTE
;
3625 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3626 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3627 nfsmode
&ACCESS3_READ
?'r':'-',
3628 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3629 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3630 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3631 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3632 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3633 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3634 free_nfs_cb_data(data
);
3638 data
->cb(0, nfs
, NULL
, data
->private_data
);
3639 free_nfs_cb_data(data
);
3642 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3647 if (data
->continue_int
& R_OK
) {
3648 nfsmode
|= ACCESS3_READ
;
3650 if (data
->continue_int
& W_OK
) {
3651 nfsmode
|= ACCESS3_MODIFY
;
3653 if (data
->continue_int
& X_OK
) {
3654 nfsmode
|= ACCESS3_EXECUTE
;
3657 memset(&args
, 0, sizeof(ACCESS3args
));
3658 args
.object
= data
->fh
;
3659 args
.access
= nfsmode
;
3661 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3662 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3663 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3664 free_nfs_cb_data(data
);
3670 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3672 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3673 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3685 struct nfs_symlink_data
{
3687 char *newpathparent
;
3688 char *newpathobject
;
3691 static void free_nfs_symlink_data(void *mem
)
3693 struct nfs_symlink_data
*data
= mem
;
3695 if (data
->oldpath
!= NULL
) {
3696 free(data
->oldpath
);
3698 if (data
->newpathparent
!= NULL
) {
3699 free(data
->newpathparent
);
3701 if (data
->newpathobject
!= NULL
) {
3702 free(data
->newpathobject
);
3707 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3710 struct nfs_cb_data
*data
= private_data
;
3711 struct nfs_context
*nfs
= data
->nfs
;
3712 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3714 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3716 if (status
== RPC_STATUS_ERROR
) {
3717 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3718 free_nfs_cb_data(data
);
3721 if (status
== RPC_STATUS_CANCEL
) {
3722 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3723 free_nfs_cb_data(data
);
3728 if (res
->status
!= NFS3_OK
) {
3729 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
));
3730 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3731 free_nfs_cb_data(data
);
3735 data
->cb(0, nfs
, NULL
, data
->private_data
);
3736 free_nfs_cb_data(data
);
3739 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3741 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3744 memset(&args
, 0, sizeof(SYMLINK3args
));
3745 args
.where
.dir
= data
->fh
;
3746 args
.where
.name
= symlink_data
->newpathobject
;
3747 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3748 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
;
3749 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3751 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3752 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3753 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3754 free_nfs_cb_data(data
);
3760 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3763 struct nfs_symlink_data
*symlink_data
;
3765 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3766 if (symlink_data
== NULL
) {
3767 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3770 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3772 symlink_data
->oldpath
= strdup(oldpath
);
3773 if (symlink_data
->oldpath
== NULL
) {
3774 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3775 free_nfs_symlink_data(symlink_data
);
3779 symlink_data
->newpathparent
= strdup(newpath
);
3780 if (symlink_data
->newpathparent
== NULL
) {
3781 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3782 free_nfs_symlink_data(symlink_data
);
3786 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3788 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3789 free_nfs_symlink_data(symlink_data
);
3795 symlink_data
->newpathobject
= strdup(ptr
);
3796 if (symlink_data
->newpathobject
== NULL
) {
3797 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3798 free_nfs_symlink_data(symlink_data
);
3802 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3803 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3815 struct nfs_rename_data
{
3818 struct nfs_fh3 olddir
;
3821 struct nfs_fh3 newdir
;
3824 static void free_nfs_rename_data(void *mem
)
3826 struct nfs_rename_data
*data
= mem
;
3828 if (data
->oldpath
!= NULL
) {
3829 free(data
->oldpath
);
3831 if (data
->olddir
.data
.data_val
!= NULL
) {
3832 free(data
->olddir
.data
.data_val
);
3834 if (data
->newpath
!= NULL
) {
3835 free(data
->newpath
);
3837 if (data
->newdir
.data
.data_val
!= NULL
) {
3838 free(data
->newdir
.data
.data_val
);
3843 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3846 struct nfs_cb_data
*data
= private_data
;
3847 struct nfs_context
*nfs
= data
->nfs
;
3848 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3850 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3852 if (status
== RPC_STATUS_ERROR
) {
3853 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3854 free_nfs_cb_data(data
);
3857 if (status
== RPC_STATUS_CANCEL
) {
3858 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3859 free_nfs_cb_data(data
);
3864 if (res
->status
!= NFS3_OK
) {
3865 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
));
3866 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3867 free_nfs_cb_data(data
);
3871 data
->cb(0, nfs
, NULL
, data
->private_data
);
3872 free_nfs_cb_data(data
);
3875 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3877 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3880 /* steal the filehandle */
3881 rename_data
->newdir
= data
->fh
;
3882 data
->fh
.data
.data_val
= NULL
;
3884 args
.from
.dir
= rename_data
->olddir
;
3885 args
.from
.name
= rename_data
->oldobject
;
3886 args
.to
.dir
= rename_data
->newdir
;
3887 args
.to
.name
= rename_data
->newobject
;
3888 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3889 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3890 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3891 free_nfs_cb_data(data
);
3898 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3900 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3902 /* steal the filehandle */
3903 rename_data
->olddir
= data
->fh
;
3904 data
->fh
.data
.data_val
= NULL
;
3906 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) {
3907 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3908 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3909 free_nfs_cb_data(data
);
3912 data
->continue_data
= NULL
;
3913 free_nfs_cb_data(data
);
3919 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3922 struct nfs_rename_data
*rename_data
;
3924 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3925 if (rename_data
== NULL
) {
3926 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3929 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3931 rename_data
->oldpath
= strdup(oldpath
);
3932 if (rename_data
->oldpath
== NULL
) {
3933 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3934 free_nfs_rename_data(rename_data
);
3937 ptr
= strrchr(rename_data
->oldpath
, '/');
3939 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3940 free_nfs_rename_data(rename_data
);
3945 rename_data
->oldobject
= ptr
;
3948 rename_data
->newpath
= strdup(newpath
);
3949 if (rename_data
->newpath
== NULL
) {
3950 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3951 free_nfs_rename_data(rename_data
);
3954 ptr
= strrchr(rename_data
->newpath
, '/');
3956 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3957 free_nfs_rename_data(rename_data
);
3962 rename_data
->newobject
= ptr
;
3965 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3966 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3977 struct nfs_link_data
{
3979 struct nfs_fh3 oldfh
;
3982 struct nfs_fh3 newdir
;
3985 static void free_nfs_link_data(void *mem
)
3987 struct nfs_link_data
*data
= mem
;
3989 if (data
->oldpath
!= NULL
) {
3990 free(data
->oldpath
);
3992 if (data
->oldfh
.data
.data_val
!= NULL
) {
3993 free(data
->oldfh
.data
.data_val
);
3995 if (data
->newpath
!= NULL
) {
3996 free(data
->newpath
);
3998 if (data
->newdir
.data
.data_val
!= NULL
) {
3999 free(data
->newdir
.data
.data_val
);
4004 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4007 struct nfs_cb_data
*data
= private_data
;
4008 struct nfs_context
*nfs
= data
->nfs
;
4009 struct nfs_link_data
*link_data
= data
->continue_data
;
4011 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4013 if (status
== RPC_STATUS_ERROR
) {
4014 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
4015 free_nfs_cb_data(data
);
4018 if (status
== RPC_STATUS_CANCEL
) {
4019 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
4020 free_nfs_cb_data(data
);
4025 if (res
->status
!= NFS3_OK
) {
4026 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
));
4027 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4028 free_nfs_cb_data(data
);
4032 data
->cb(0, nfs
, NULL
, data
->private_data
);
4033 free_nfs_cb_data(data
);
4036 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4038 struct nfs_link_data
*link_data
= data
->continue_data
;
4041 /* steal the filehandle */
4042 link_data
->newdir
= data
->fh
;
4043 data
->fh
.data
.data_val
= NULL
;
4045 memset(&args
, 0, sizeof(LINK3args
));
4046 args
.file
= link_data
->oldfh
;
4047 args
.link
.dir
= link_data
->newdir
;
4048 args
.link
.name
= link_data
->newobject
;
4049 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4050 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4051 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4052 free_nfs_cb_data(data
);
4059 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4061 struct nfs_link_data
*link_data
= data
->continue_data
;
4063 /* steal the filehandle */
4064 link_data
->oldfh
= data
->fh
;
4065 data
->fh
.data
.data_val
= NULL
;
4067 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) {
4068 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4069 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4070 free_nfs_cb_data(data
);
4073 data
->continue_data
= NULL
;
4074 free_nfs_cb_data(data
);
4080 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4083 struct nfs_link_data
*link_data
;
4085 link_data
= malloc(sizeof(struct nfs_link_data
));
4086 if (link_data
== NULL
) {
4087 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4090 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4092 link_data
->oldpath
= strdup(oldpath
);
4093 if (link_data
->oldpath
== NULL
) {
4094 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4095 free_nfs_link_data(link_data
);
4099 link_data
->newpath
= strdup(newpath
);
4100 if (link_data
->newpath
== NULL
) {
4101 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4102 free_nfs_link_data(link_data
);
4105 ptr
= strrchr(link_data
->newpath
, '/');
4107 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4108 free_nfs_link_data(link_data
);
4113 link_data
->newobject
= ptr
;
4116 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4117 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4125 //qqq replace later with lseek()
4126 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4128 return nfsfh
->offset
;
4134 * Get the maximum supported READ3 size by the server
4136 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4138 return nfs
->readmax
;
4142 * Get the maximum supported WRITE3 size by the server
4144 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4146 return nfs
->writemax
;
4149 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4150 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4153 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4154 rpc_set_uid(nfs
->rpc
, uid
);
4157 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4158 rpc_set_gid(nfs
->rpc
, gid
);
4161 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4166 va_start(ap
, error_string
);
4168 vsnprintf(str
, 1024, error_string
, ap
);
4169 if (nfs
->rpc
->error_string
!= NULL
) {
4170 free(nfs
->rpc
->error_string
);
4172 nfs
->rpc
->error_string
= str
;
4178 struct mount_cb_data
{
4184 static void free_mount_cb_data(struct mount_cb_data
*data
)
4186 if (data
->server
!= NULL
) {
4188 data
->server
= NULL
;
4194 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4196 struct mount_cb_data
*data
= private_data
;
4198 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4200 if (status
== RPC_STATUS_ERROR
) {
4201 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4202 free_mount_cb_data(data
);
4205 if (status
== RPC_STATUS_CANCEL
) {
4206 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4207 free_mount_cb_data(data
);
4211 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4212 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4213 rpc_set_error(rpc
, "Failed to disconnect\n");
4215 free_mount_cb_data(data
);
4218 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4220 struct mount_cb_data
*data
= private_data
;
4222 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4224 /* Dont want any more callbacks even if the socket is closed */
4225 rpc
->connect_cb
= NULL
;
4227 if (status
== RPC_STATUS_ERROR
) {
4228 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4229 free_mount_cb_data(data
);
4232 if (status
== RPC_STATUS_CANCEL
) {
4233 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4234 free_mount_cb_data(data
);
4238 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4239 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4240 free_mount_cb_data(data
);
4245 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4247 struct mount_cb_data
*data
= private_data
;
4248 uint32_t mount_port
;
4250 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4252 if (status
== RPC_STATUS_ERROR
) {
4253 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4254 free_mount_cb_data(data
);
4257 if (status
== RPC_STATUS_CANCEL
) {
4258 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4259 free_mount_cb_data(data
);
4263 mount_port
= *(uint32_t *)command_data
;
4264 if (mount_port
== 0) {
4265 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4266 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4267 free_mount_cb_data(data
);
4271 rpc_disconnect(rpc
, "normal disconnect");
4272 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4273 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4274 free_mount_cb_data(data
);
4279 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4281 struct mount_cb_data
*data
= private_data
;
4283 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4285 if (status
== RPC_STATUS_ERROR
) {
4286 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4287 free_mount_cb_data(data
);
4290 if (status
== RPC_STATUS_CANCEL
) {
4291 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4292 free_mount_cb_data(data
);
4296 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4297 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4298 free_mount_cb_data(data
);
4303 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4305 struct mount_cb_data
*data
= private_data
;
4307 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4309 /* Dont want any more callbacks even if the socket is closed */
4310 rpc
->connect_cb
= NULL
;
4312 if (status
== RPC_STATUS_ERROR
) {
4313 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4314 free_mount_cb_data(data
);
4317 if (status
== RPC_STATUS_CANCEL
) {
4318 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4319 free_mount_cb_data(data
);
4323 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4324 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4325 free_mount_cb_data(data
);
4330 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4332 struct mount_cb_data
*data
;
4334 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4336 data
= malloc(sizeof(struct mount_cb_data
));
4340 memset(data
, 0, sizeof(struct mount_cb_data
));
4342 data
->private_data
= private_data
;
4343 data
->server
= strdup(server
);
4344 if (data
->server
== NULL
) {
4345 free_mount_cb_data(data
);
4348 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4349 free_mount_cb_data(data
);
4356 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4358 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4362 const char *nfs_get_server(struct nfs_context
*nfs
) {
4366 const char *nfs_get_export(struct nfs_context
*nfs
) {
4370 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4371 return &nfs
->rootfh
;
4374 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {