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
;
98 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
100 while (nfsdir
->entries
) {
101 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
102 if (nfsdir
->entries
->name
!= NULL
) {
103 free(nfsdir
->entries
->name
);
105 free(nfsdir
->entries
);
106 nfsdir
->entries
= dirent
;
112 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
115 struct nfs_context
*nfs
;
117 char *saved_path
, *path
;
122 continue_func continue_cb
;
124 void (*free_continue_data
)(void *);
129 /* for multi-read/write calls. */
133 uint64_t start_offset
, max_offset
;
137 struct nfs_mcb_data
{
138 struct nfs_cb_data
*data
;
143 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 nfs
->rpc
= rpc_init_context();
338 if (nfs
->rpc
== NULL
) {
346 nfs
->rootfh
.data
.data_len
= 0;
347 nfs
->rootfh
.data
.data_val
= NULL
;
352 void nfs_destroy_context(struct nfs_context
*nfs
)
354 rpc_destroy_context(nfs
->rpc
);
367 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
368 free(nfs
->rootfh
.data
.data_val
);
369 nfs
->rootfh
.data
.data_val
= NULL
;
384 void free_rpc_cb_data(struct rpc_cb_data
*data
)
391 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
393 struct rpc_cb_data
*data
= private_data
;
395 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
397 /* Dont want any more callbacks even if the socket is closed */
398 rpc
->connect_cb
= NULL
;
400 if (status
== RPC_STATUS_ERROR
) {
401 data
->cb(rpc
, status
, command_data
, data
->private_data
);
402 free_rpc_cb_data(data
);
405 if (status
== RPC_STATUS_CANCEL
) {
406 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
407 free_rpc_cb_data(data
);
411 data
->cb(rpc
, status
, NULL
, data
->private_data
);
412 free_rpc_cb_data(data
);
415 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
417 struct rpc_cb_data
*data
= private_data
;
420 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
422 if (status
== RPC_STATUS_ERROR
) {
423 data
->cb(rpc
, status
, command_data
, data
->private_data
);
424 free_rpc_cb_data(data
);
427 if (status
== RPC_STATUS_CANCEL
) {
428 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
429 free_rpc_cb_data(data
);
433 rpc_port
= *(uint32_t *)command_data
;
435 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
436 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
437 free_rpc_cb_data(data
);
441 rpc_disconnect(rpc
, "normal disconnect");
442 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
443 data
->cb(rpc
, status
, command_data
, data
->private_data
);
444 free_rpc_cb_data(data
);
449 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
451 struct rpc_cb_data
*data
= private_data
;
453 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
455 if (status
== RPC_STATUS_ERROR
) {
456 data
->cb(rpc
, status
, command_data
, data
->private_data
);
457 free_rpc_cb_data(data
);
460 if (status
== RPC_STATUS_CANCEL
) {
461 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
462 free_rpc_cb_data(data
);
466 if (rpc_pmap_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
467 data
->cb(rpc
, status
, command_data
, data
->private_data
);
468 free_rpc_cb_data(data
);
473 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
475 struct rpc_cb_data
*data
= private_data
;
477 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
479 /* Dont want any more callbacks even if the socket is closed */
480 rpc
->connect_cb
= NULL
;
482 if (status
== RPC_STATUS_ERROR
) {
483 data
->cb(rpc
, status
, command_data
, data
->private_data
);
484 free_rpc_cb_data(data
);
487 if (status
== RPC_STATUS_CANCEL
) {
488 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
489 free_rpc_cb_data(data
);
493 if (rpc_pmap_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
494 data
->cb(rpc
, status
, command_data
, data
->private_data
);
495 free_rpc_cb_data(data
);
500 int rpc_connect_program_async(struct rpc_context
*rpc
, char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
502 struct rpc_cb_data
*data
;
504 data
= malloc(sizeof(struct rpc_cb_data
));
508 memset(data
, 0, sizeof(struct rpc_cb_data
));
509 data
->server
= strdup(server
);
510 data
->program
= program
;
511 data
->version
= version
;
514 data
->private_data
= private_data
;
516 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
517 rpc_set_error(rpc
, "Failed to start connection");
518 free_rpc_cb_data(data
);
524 void free_nfs_cb_data(struct nfs_cb_data
*data
)
526 if (data
->saved_path
!= NULL
) {
527 free(data
->saved_path
);
528 data
->saved_path
= NULL
;
531 if (data
->continue_data
!= NULL
) {
532 data
->free_continue_data(data
->continue_data
);
533 data
->continue_data
= NULL
;
536 if (data
->fh
.data
.data_val
!= NULL
) {
537 free(data
->fh
.data
.data_val
);
538 data
->fh
.data
.data_val
= NULL
;
541 if (data
->buffer
!= NULL
) {
553 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
555 struct nfs_cb_data
*data
= private_data
;
556 struct nfs_context
*nfs
= data
->nfs
;
558 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
560 if (status
== RPC_STATUS_ERROR
) {
561 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
562 free_nfs_cb_data(data
);
565 if (status
== RPC_STATUS_CANCEL
) {
566 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
567 free_nfs_cb_data(data
);
571 data
->cb(0, nfs
, NULL
, data
->private_data
);
572 free_nfs_cb_data(data
);
575 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
577 struct nfs_cb_data
*data
= private_data
;
578 struct nfs_context
*nfs
= data
->nfs
;
579 FSINFO3res
*res
= command_data
;
580 struct GETATTR3args args
;
582 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
584 if (status
== RPC_STATUS_ERROR
) {
585 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
586 free_nfs_cb_data(data
);
589 if (status
== RPC_STATUS_CANCEL
) {
590 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
591 free_nfs_cb_data(data
);
595 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
596 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
598 memset(&args
, 0, sizeof(GETATTR3args
));
599 args
.object
= nfs
->rootfh
;
601 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
602 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
603 free_nfs_cb_data(data
);
608 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
610 struct nfs_cb_data
*data
= private_data
;
611 struct nfs_context
*nfs
= data
->nfs
;
612 struct FSINFO3args args
;
614 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
616 if (status
== RPC_STATUS_ERROR
) {
617 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
618 free_nfs_cb_data(data
);
621 if (status
== RPC_STATUS_CANCEL
) {
622 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
623 free_nfs_cb_data(data
);
627 args
.fsroot
= nfs
->rootfh
;
628 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
629 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
630 free_nfs_cb_data(data
);
636 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
638 struct nfs_cb_data
*data
= private_data
;
639 struct nfs_context
*nfs
= data
->nfs
;
641 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
643 /* Dont want any more callbacks even if the socket is closed */
644 rpc
->connect_cb
= NULL
;
646 if (status
== RPC_STATUS_ERROR
) {
647 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
648 free_nfs_cb_data(data
);
651 if (status
== RPC_STATUS_CANCEL
) {
652 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
653 free_nfs_cb_data(data
);
657 if (rpc_nfs3_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
658 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
659 free_nfs_cb_data(data
);
665 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
667 struct nfs_cb_data
*data
= private_data
;
668 struct nfs_context
*nfs
= data
->nfs
;
671 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
673 if (status
== RPC_STATUS_ERROR
) {
674 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
675 free_nfs_cb_data(data
);
678 if (status
== RPC_STATUS_CANCEL
) {
679 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
680 free_nfs_cb_data(data
);
685 if (res
->fhs_status
!= MNT3_OK
) {
686 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
));
687 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
688 free_nfs_cb_data(data
);
692 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
693 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
694 if (nfs
->rootfh
.data
.data_val
== NULL
) {
695 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
696 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
697 free_nfs_cb_data(data
);
700 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
702 rpc_disconnect(rpc
, "normal disconnect");
703 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
704 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
705 free_nfs_cb_data(data
);
708 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
709 rpc_set_autoreconnect(rpc
);
713 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
715 struct nfs_cb_data
*data
= private_data
;
716 struct nfs_context
*nfs
= data
->nfs
;
718 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
720 if (status
== RPC_STATUS_ERROR
) {
721 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
722 free_nfs_cb_data(data
);
725 if (status
== RPC_STATUS_CANCEL
) {
726 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
727 free_nfs_cb_data(data
);
731 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
732 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
733 free_nfs_cb_data(data
);
738 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
740 struct nfs_cb_data
*data
= private_data
;
741 struct nfs_context
*nfs
= data
->nfs
;
743 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
745 /* Dont want any more callbacks even if the socket is closed */
746 rpc
->connect_cb
= NULL
;
748 if (status
== RPC_STATUS_ERROR
) {
749 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
750 free_nfs_cb_data(data
);
753 if (status
== RPC_STATUS_CANCEL
) {
754 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
755 free_nfs_cb_data(data
);
759 if (rpc_mount3_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
760 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
761 free_nfs_cb_data(data
);
766 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
768 struct nfs_cb_data
*data
= private_data
;
769 struct nfs_context
*nfs
= data
->nfs
;
772 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
774 if (status
== RPC_STATUS_ERROR
) {
775 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
776 free_nfs_cb_data(data
);
779 if (status
== RPC_STATUS_CANCEL
) {
780 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
781 free_nfs_cb_data(data
);
785 mount_port
= *(uint32_t *)command_data
;
786 if (mount_port
== 0) {
787 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
788 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
789 free_nfs_cb_data(data
);
793 rpc_disconnect(rpc
, "normal disconnect");
794 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
795 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
796 free_nfs_cb_data(data
);
802 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
804 struct nfs_cb_data
*data
= private_data
;
805 struct nfs_context
*nfs
= data
->nfs
;
807 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
809 if (status
== RPC_STATUS_ERROR
) {
810 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
811 free_nfs_cb_data(data
);
814 if (status
== RPC_STATUS_CANCEL
) {
815 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
816 free_nfs_cb_data(data
);
820 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
821 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
822 free_nfs_cb_data(data
);
827 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
829 struct nfs_cb_data
*data
= private_data
;
830 struct nfs_context
*nfs
= data
->nfs
;
832 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
834 /* Dont want any more callbacks even if the socket is closed */
835 rpc
->connect_cb
= NULL
;
837 if (status
== RPC_STATUS_ERROR
) {
838 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
839 free_nfs_cb_data(data
);
842 if (status
== RPC_STATUS_CANCEL
) {
843 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
844 free_nfs_cb_data(data
);
848 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
849 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
850 free_nfs_cb_data(data
);
856 * Async call for mounting an nfs share and geting the root filehandle
858 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
860 struct nfs_cb_data
*data
;
861 char *new_server
, *new_export
;
863 data
= malloc(sizeof(struct nfs_cb_data
));
865 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
868 memset(data
, 0, sizeof(struct nfs_cb_data
));
869 new_server
= strdup(server
);
870 new_export
= strdup(export
);
871 if (nfs
->server
!= NULL
) {
874 nfs
->server
= new_server
;
875 if (nfs
->export
!= NULL
) {
878 nfs
->export
= new_export
;
881 data
->private_data
= private_data
;
883 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
884 rpc_set_error(nfs
->rpc
, "Failed to start connection");
885 free_nfs_cb_data(data
);
895 * Functions to first look up a path, component by component, and then finally call a specific function once
896 * the filehandle for the final component is found.
898 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
900 struct nfs_cb_data
*data
= private_data
;
901 struct nfs_context
*nfs
= data
->nfs
;
904 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
906 if (status
== RPC_STATUS_ERROR
) {
907 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
908 free_nfs_cb_data(data
);
911 if (status
== RPC_STATUS_CANCEL
) {
912 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
913 free_nfs_cb_data(data
);
918 if (res
->status
!= NFS3_OK
) {
919 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
));
920 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
921 free_nfs_cb_data(data
);
925 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
926 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
927 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
928 free_nfs_cb_data(data
);
933 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
938 while (*data
->path
== '/') {
943 str
= strchr(path
, '/');
948 while (*data
->path
!= 0) {
954 data
->fh
.data
.data_len
= fh
->data
.data_len
;
955 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
956 if (data
->fh
.data
.data_val
== NULL
) {
957 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
958 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
959 free_nfs_cb_data(data
);
962 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
963 data
->continue_cb(nfs
, data
);
968 memset(&args
, 0, sizeof(LOOKUP3args
));
970 args
.what
.name
= path
;
972 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
973 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
974 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
975 free_nfs_cb_data(data
);
981 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
)
983 struct nfs_cb_data
*data
;
985 if (path
[0] != 0 && path
[0] != '/') {
986 rpc_set_error(nfs
->rpc
, "Pathname is not absolute %s", path
);
990 data
= malloc(sizeof(struct nfs_cb_data
));
992 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
995 memset(data
, 0, sizeof(struct nfs_cb_data
));
998 data
->continue_cb
= continue_cb
;
999 data
->continue_data
= continue_data
;
1000 data
->free_continue_data
= free_continue_data
;
1001 data
->continue_int
= continue_int
;
1002 data
->private_data
= private_data
;
1003 data
->saved_path
= strdup(path
);
1004 if (data
->saved_path
== NULL
) {
1005 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1006 free_nfs_cb_data(data
);
1009 data
->path
= data
->saved_path
;
1011 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1012 /* return 0 here since the callback will be invoked if there is a failure */
1025 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1028 struct nfs_cb_data
*data
= private_data
;
1029 struct nfs_context
*nfs
= data
->nfs
;
1032 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1034 if (status
== RPC_STATUS_ERROR
) {
1035 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1036 free_nfs_cb_data(data
);
1039 if (status
== RPC_STATUS_CANCEL
) {
1040 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1041 free_nfs_cb_data(data
);
1046 if (res
->status
!= NFS3_OK
) {
1047 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
));
1048 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1049 free_nfs_cb_data(data
);
1054 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1055 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1056 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1057 st
.st_mode
|= S_IFDIR
;
1059 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1060 st
.st_mode
|= S_IFREG
;
1062 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1063 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1064 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1066 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1068 st
.st_blksize
= 4096;
1069 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1071 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1072 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1073 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1075 data
->cb(0, nfs
, &st
, data
->private_data
);
1076 free_nfs_cb_data(data
);
1079 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1081 struct GETATTR3args args
;
1083 memset(&args
, 0, sizeof(GETATTR3args
));
1084 args
.object
= data
->fh
;
1086 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1087 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1088 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1089 free_nfs_cb_data(data
);
1095 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1097 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1098 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1112 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1115 struct nfs_cb_data
*data
= private_data
;
1116 struct nfs_context
*nfs
= data
->nfs
;
1117 struct nfsfh
*nfsfh
;
1118 unsigned int nfsmode
= 0;
1120 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1122 if (status
== RPC_STATUS_ERROR
) {
1123 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1124 free_nfs_cb_data(data
);
1127 if (status
== RPC_STATUS_CANCEL
) {
1128 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1129 free_nfs_cb_data(data
);
1134 if (res
->status
!= NFS3_OK
) {
1135 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
));
1136 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1137 free_nfs_cb_data(data
);
1141 if (data
->continue_int
& O_WRONLY
) {
1142 nfsmode
|= ACCESS3_MODIFY
;
1144 if (data
->continue_int
& O_RDWR
) {
1145 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1147 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1148 nfsmode
|= ACCESS3_READ
;
1152 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1153 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1154 nfsmode
&ACCESS3_READ
?'r':'-',
1155 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1156 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1157 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1158 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1159 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1160 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1161 free_nfs_cb_data(data
);
1165 nfsfh
= malloc(sizeof(struct nfsfh
));
1166 if (nfsfh
== NULL
) {
1167 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1168 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1169 free_nfs_cb_data(data
);
1172 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1174 if (data
->continue_int
& O_SYNC
) {
1178 /* steal the filehandle */
1179 nfsfh
->fh
= data
->fh
;
1180 data
->fh
.data
.data_val
= NULL
;
1182 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1183 free_nfs_cb_data(data
);
1186 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1191 if (data
->continue_int
& O_WRONLY
) {
1192 nfsmode
|= ACCESS3_MODIFY
;
1194 if (data
->continue_int
& O_RDWR
) {
1195 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1197 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1198 nfsmode
|= ACCESS3_READ
;
1201 memset(&args
, 0, sizeof(ACCESS3args
));
1202 args
.object
= data
->fh
;
1203 args
.access
= nfsmode
;
1205 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1206 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
1207 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1208 free_nfs_cb_data(data
);
1214 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1216 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
1217 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1231 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1233 struct nfs_cb_data
*data
= private_data
;
1234 struct nfs_context
*nfs
= data
->nfs
;
1237 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1239 if (status
== RPC_STATUS_ERROR
) {
1240 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1241 free_nfs_cb_data(data
);
1244 if (status
== RPC_STATUS_CANCEL
) {
1245 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1246 free_nfs_cb_data(data
);
1251 if (res
->status
!= NFS3_OK
) {
1252 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1253 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1254 free_nfs_cb_data(data
);
1258 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1259 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1260 free_nfs_cb_data(data
);
1263 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1265 struct nfs_mcb_data
*mdata
= private_data
;
1266 struct nfs_cb_data
*data
= mdata
->data
;
1267 struct nfs_context
*nfs
= data
->nfs
;
1270 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1274 if (status
== RPC_STATUS_ERROR
) {
1275 /* flag the failure but do not invoke callback until we have received all responses */
1278 if (status
== RPC_STATUS_CANCEL
) {
1279 /* flag the cancellation but do not invoke callback until we have received all responses */
1283 /* reassemble the data into the buffer */
1284 if (status
== RPC_STATUS_SUCCESS
) {
1286 if (res
->status
!= NFS3_OK
) {
1287 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1290 if (res
->READ3res_u
.resok
.count
> 0) {
1291 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1292 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1293 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1299 if (data
->num_calls
> 0) {
1300 /* still waiting for more replies */
1305 if (data
->error
!= 0) {
1306 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1307 free_nfs_cb_data(data
);
1311 if (data
->cancel
!= 0) {
1312 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1313 free_nfs_cb_data(data
);
1318 data
->nfsfh
->offset
= data
->max_offset
;
1319 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1321 free_nfs_cb_data(data
);
1325 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1327 struct nfs_cb_data
*data
;
1329 data
= malloc(sizeof(struct nfs_cb_data
));
1331 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1334 memset(data
, 0, sizeof(struct nfs_cb_data
));
1337 data
->private_data
= private_data
;
1338 data
->nfsfh
= nfsfh
;
1340 nfsfh
->offset
= offset
;
1342 if (count
<= nfs_get_readmax(nfs
)) {
1345 memset(&args
, 0, sizeof(READ3args
));
1346 args
.file
= nfsfh
->fh
;
1347 args
.offset
= offset
;
1350 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_cb
, &args
, data
) != 0) {
1351 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1352 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1353 free_nfs_cb_data(data
);
1359 /* trying to read more than maximum server read size, we has to chop it up into smaller
1360 * reads and collect into a reassembly buffer.
1361 * we send all reads in parallel so that performance is still good.
1363 data
->max_offset
= offset
;
1364 data
->start_offset
= offset
;
1366 data
->buffer
= malloc(count
);
1367 if (data
->buffer
== NULL
) {
1368 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1369 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1370 free_nfs_cb_data(data
);
1375 uint64_t readcount
= count
;
1376 struct nfs_mcb_data
*mdata
;
1379 if (readcount
> nfs_get_readmax(nfs
)) {
1380 readcount
= nfs_get_readmax(nfs
);
1383 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1384 if (mdata
== NULL
) {
1385 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1388 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1390 mdata
->offset
= offset
;
1391 mdata
->count
= readcount
;
1393 memset(&args
, 0, sizeof(READ3args
));
1394 args
.file
= nfsfh
->fh
;
1395 args
.offset
= offset
;
1396 args
.count
= readcount
;
1398 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1399 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1400 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1406 offset
+= readcount
;
1416 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1418 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1426 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1428 struct nfs_cb_data
*data
= private_data
;
1429 struct nfs_context
*nfs
= data
->nfs
;
1432 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1434 if (status
== RPC_STATUS_ERROR
) {
1435 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1436 free_nfs_cb_data(data
);
1439 if (status
== RPC_STATUS_CANCEL
) {
1440 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1441 free_nfs_cb_data(data
);
1446 if (res
->status
!= NFS3_OK
) {
1447 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1448 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1449 free_nfs_cb_data(data
);
1453 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1454 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1455 free_nfs_cb_data(data
);
1458 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1460 struct nfs_mcb_data
*mdata
= private_data
;
1461 struct nfs_cb_data
*data
= mdata
->data
;
1462 struct nfs_context
*nfs
= data
->nfs
;
1465 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1469 if (status
== RPC_STATUS_ERROR
) {
1470 /* flag the failure but do not invoke callback until we have received all responses */
1473 if (status
== RPC_STATUS_CANCEL
) {
1474 /* flag the cancellation but do not invoke callback until we have received all responses */
1478 if (status
== RPC_STATUS_SUCCESS
) {
1480 if (res
->status
!= NFS3_OK
) {
1481 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1484 if (res
->WRITE3res_u
.resok
.count
> 0) {
1485 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1486 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1492 if (data
->num_calls
> 0) {
1493 /* still waiting for more replies */
1498 if (data
->error
!= 0) {
1499 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1500 free_nfs_cb_data(data
);
1504 if (data
->cancel
!= 0) {
1505 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1506 free_nfs_cb_data(data
);
1511 data
->nfsfh
->offset
= data
->max_offset
;
1512 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1514 free_nfs_cb_data(data
);
1519 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
)
1521 struct nfs_cb_data
*data
;
1523 data
= malloc(sizeof(struct nfs_cb_data
));
1525 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1528 memset(data
, 0, sizeof(struct nfs_cb_data
));
1531 data
->private_data
= private_data
;
1532 data
->nfsfh
= nfsfh
;
1534 nfsfh
->offset
= offset
;
1536 if (count
<= nfs_get_writemax(nfs
)) {
1539 memset(&args
, 0, sizeof(WRITE3args
));
1540 args
.file
= nfsfh
->fh
;
1541 args
.offset
= offset
;
1543 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1544 args
.data
.data_len
= count
;
1545 args
.data
.data_val
= buf
;
1547 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_cb
, &args
, data
) != 0) {
1548 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1549 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1550 free_nfs_cb_data(data
);
1556 /* trying to write more than maximum server write size, we has to chop it up into smaller
1558 * we send all writes in parallel so that performance is still good.
1560 data
->max_offset
= offset
;
1561 data
->start_offset
= offset
;
1564 uint64_t writecount
= count
;
1565 struct nfs_mcb_data
*mdata
;
1568 if (writecount
> nfs_get_writemax(nfs
)) {
1569 writecount
= nfs_get_writemax(nfs
);
1572 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1573 if (mdata
== NULL
) {
1574 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1577 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1579 mdata
->offset
= offset
;
1580 mdata
->count
= writecount
;
1582 memset(&args
, 0, sizeof(WRITE3args
));
1583 args
.file
= nfsfh
->fh
;
1584 args
.offset
= offset
;
1585 args
.count
= writecount
;
1586 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1587 args
.data
.data_len
= writecount
;
1588 args
.data
.data_val
= &buf
[offset
- data
->start_offset
];
1590 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1591 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1592 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1597 count
-= writecount
;
1598 offset
+= writecount
;
1608 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1610 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1620 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1622 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1623 free(nfsfh
->fh
.data
.data_val
);
1624 nfsfh
->fh
.data
.data_val
= NULL
;
1628 cb(0, nfs
, NULL
, private_data
);
1639 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1641 struct nfs_cb_data
*data
;
1642 struct GETATTR3args args
;
1644 data
= malloc(sizeof(struct nfs_cb_data
));
1646 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1649 memset(data
, 0, sizeof(struct nfs_cb_data
));
1652 data
->private_data
= private_data
;
1654 memset(&args
, 0, sizeof(GETATTR3args
));
1655 args
.object
= nfsfh
->fh
;
1657 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1658 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1659 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1660 free_nfs_cb_data(data
);
1671 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1673 struct nfs_cb_data
*data
= private_data
;
1674 struct nfs_context
*nfs
= data
->nfs
;
1677 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1679 if (status
== RPC_STATUS_ERROR
) {
1680 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1681 free_nfs_cb_data(data
);
1684 if (status
== RPC_STATUS_CANCEL
) {
1685 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1686 free_nfs_cb_data(data
);
1691 if (res
->status
!= NFS3_OK
) {
1692 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1693 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1694 free_nfs_cb_data(data
);
1698 data
->cb(0, nfs
, NULL
, data
->private_data
);
1699 free_nfs_cb_data(data
);
1702 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1704 struct nfs_cb_data
*data
;
1705 struct COMMIT3args args
;
1707 data
= malloc(sizeof(struct nfs_cb_data
));
1709 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1712 memset(data
, 0, sizeof(struct nfs_cb_data
));
1715 data
->private_data
= private_data
;
1717 args
.file
= nfsfh
->fh
;
1720 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1721 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1722 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1723 free_nfs_cb_data(data
);
1735 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1737 struct nfs_cb_data
*data
= private_data
;
1738 struct nfs_context
*nfs
= data
->nfs
;
1741 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1743 if (status
== RPC_STATUS_ERROR
) {
1744 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1745 free_nfs_cb_data(data
);
1748 if (status
== RPC_STATUS_CANCEL
) {
1749 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1750 free_nfs_cb_data(data
);
1755 if (res
->status
!= NFS3_OK
) {
1756 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1757 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1758 free_nfs_cb_data(data
);
1762 data
->cb(0, nfs
, NULL
, data
->private_data
);
1763 free_nfs_cb_data(data
);
1766 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1768 struct nfs_cb_data
*data
;
1771 data
= malloc(sizeof(struct nfs_cb_data
));
1773 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1776 memset(data
, 0, sizeof(struct nfs_cb_data
));
1779 data
->private_data
= private_data
;
1781 memset(&args
, 0, sizeof(SETATTR3args
));
1782 args
.object
= nfsfh
->fh
;
1783 args
.new_attributes
.size
.set_it
= 1;
1784 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1786 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1787 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1788 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1789 free_nfs_cb_data(data
);
1799 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1801 uint64_t offset
= data
->continue_int
;
1804 nfsfh
.fh
= data
->fh
;
1806 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1807 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1808 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1809 free_nfs_cb_data(data
);
1812 free_nfs_cb_data(data
);
1816 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1822 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1823 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1836 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1839 struct nfs_cb_data
*data
= private_data
;
1840 struct nfs_context
*nfs
= data
->nfs
;
1841 char *str
= data
->continue_data
;
1843 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1845 str
= &str
[strlen(str
) + 1];
1847 if (status
== RPC_STATUS_ERROR
) {
1848 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1849 free_nfs_cb_data(data
);
1852 if (status
== RPC_STATUS_CANCEL
) {
1853 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1854 free_nfs_cb_data(data
);
1859 if (res
->status
!= NFS3_OK
) {
1860 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
));
1861 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1862 free_nfs_cb_data(data
);
1866 data
->cb(0, nfs
, NULL
, data
->private_data
);
1867 free_nfs_cb_data(data
);
1870 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1872 char *str
= data
->continue_data
;
1875 str
= &str
[strlen(str
) + 1];
1877 memset(&args
, 0, sizeof(MKDIR3args
));
1878 args
.where
.dir
= data
->fh
;
1879 args
.where
.name
= str
;
1880 args
.attributes
.mode
.set_it
= 1;
1881 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
1883 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
1884 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1885 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1886 free_nfs_cb_data(data
);
1892 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1897 new_path
= strdup(path
);
1898 if (new_path
== NULL
) {
1899 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1903 ptr
= strrchr(new_path
, '/');
1905 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1910 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1911 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1912 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1926 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1929 struct nfs_cb_data
*data
= private_data
;
1930 struct nfs_context
*nfs
= data
->nfs
;
1931 char *str
= data
->continue_data
;
1933 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1935 str
= &str
[strlen(str
) + 1];
1937 if (status
== RPC_STATUS_ERROR
) {
1938 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1939 free_nfs_cb_data(data
);
1942 if (status
== RPC_STATUS_CANCEL
) {
1943 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1944 free_nfs_cb_data(data
);
1949 if (res
->status
!= NFS3_OK
) {
1950 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
));
1951 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1952 free_nfs_cb_data(data
);
1956 data
->cb(0, nfs
, NULL
, data
->private_data
);
1957 free_nfs_cb_data(data
);
1960 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1962 char *str
= data
->continue_data
;
1965 str
= &str
[strlen(str
) + 1];
1967 args
.object
.dir
= data
->fh
;
1968 args
.object
.name
= str
;
1969 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
1970 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1971 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1972 free_nfs_cb_data(data
);
1978 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1983 new_path
= strdup(path
);
1984 if (new_path
== NULL
) {
1985 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1989 ptr
= strrchr(new_path
, '/');
1991 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1996 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1997 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1998 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2011 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2014 struct nfs_cb_data
*data
= private_data
;
2015 struct nfs_context
*nfs
= data
->nfs
;
2016 struct nfsfh
*nfsfh
;
2017 char *str
= data
->continue_data
;
2019 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2021 if (status
== RPC_STATUS_ERROR
) {
2022 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2023 free_nfs_cb_data(data
);
2026 if (status
== RPC_STATUS_CANCEL
) {
2027 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2028 free_nfs_cb_data(data
);
2032 str
= &str
[strlen(str
) + 1];
2034 if (res
->status
!= NFS3_OK
) {
2035 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
));
2036 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2041 nfsfh
= malloc(sizeof(struct nfsfh
));
2042 if (nfsfh
== NULL
) {
2043 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2044 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2045 free_nfs_cb_data(data
);
2048 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2050 /* copy the filehandle */
2051 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2052 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2053 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2055 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2056 free_nfs_cb_data(data
);
2061 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2064 struct nfs_cb_data
*data
= private_data
;
2065 struct nfs_context
*nfs
= data
->nfs
;
2066 char *str
= data
->continue_data
;
2069 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2071 if (status
== RPC_STATUS_ERROR
) {
2072 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2073 free_nfs_cb_data(data
);
2076 if (status
== RPC_STATUS_CANCEL
) {
2077 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2078 free_nfs_cb_data(data
);
2082 str
= &str
[strlen(str
) + 1];
2084 if (res
->status
!= NFS3_OK
) {
2085 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
));
2086 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2087 free_nfs_cb_data(data
);
2091 memset(&args
, 0, sizeof(LOOKUP3args
));
2092 args
.what
.dir
= data
->fh
;
2093 args
.what
.name
= str
;
2095 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2096 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2097 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2098 free_nfs_cb_data(data
);
2104 static int nfs_creat_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(CREATE3args
));
2112 args
.where
.dir
= data
->fh
;
2113 args
.where
.name
= str
;
2114 args
.how
.mode
= UNCHECKED
;
2115 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2116 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2118 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2119 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2120 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2121 free_nfs_cb_data(data
);
2127 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2132 new_path
= strdup(path
);
2133 if (new_path
== NULL
) {
2134 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2138 ptr
= strrchr(new_path
, '/');
2140 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2145 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2146 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2147 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2160 static void nfs_unlink_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: REMOVE 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_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2196 char *str
= data
->continue_data
;
2197 struct REMOVE3args args
;
2199 str
= &str
[strlen(str
) + 1];
2201 args
.object
.dir
= data
->fh
;
2202 args
.object
.name
= str
;
2203 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2204 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE 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_unlink_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_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2232 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2243 struct mknod_cb_data
{
2250 static void free_mknod_cb_data(void *ptr
)
2252 struct mknod_cb_data
*data
= ptr
;
2258 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2261 struct nfs_cb_data
*data
= private_data
;
2262 struct nfs_context
*nfs
= data
->nfs
;
2263 char *str
= data
->continue_data
;
2265 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2267 str
= &str
[strlen(str
) + 1];
2269 if (status
== RPC_STATUS_ERROR
) {
2270 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2271 free_nfs_cb_data(data
);
2274 if (status
== RPC_STATUS_CANCEL
) {
2275 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2276 free_nfs_cb_data(data
);
2281 if (res
->status
!= NFS3_OK
) {
2282 rpc_set_error(nfs
->rpc
, "NFS: MKNOD of %s/%s failed with %s(%d)", data
->saved_path
, str
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2283 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2284 free_nfs_cb_data(data
);
2288 data
->cb(0, nfs
, NULL
, data
->private_data
);
2289 free_nfs_cb_data(data
);
2292 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2294 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2295 char *str
= cb_data
->path
;
2298 str
= &str
[strlen(str
) + 1];
2300 args
.where
.dir
= data
->fh
;
2301 args
.where
.name
= str
;
2302 switch (cb_data
->mode
& S_IFMT
) {
2304 args
.what
.type
= NF3CHR
;
2305 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2306 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
);
2307 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2308 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2311 args
.what
.type
= NF3BLK
;
2312 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2313 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
);
2314 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2315 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2317 args
.what
.type
= NF3SOCK
;
2318 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2319 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
);
2322 args
.what
.type
= NF3FIFO
;
2323 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2324 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
);
2327 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2328 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2329 free_nfs_cb_data(data
);
2333 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2334 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2335 free_nfs_cb_data(data
);
2341 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2344 struct mknod_cb_data
*cb_data
;
2346 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2347 if (cb_data
== NULL
) {
2348 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2352 cb_data
->path
= strdup(path
);
2353 if (cb_data
->path
== NULL
) {
2354 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2359 ptr
= strrchr(cb_data
->path
, '/');
2361 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2366 cb_data
->mode
= mode
;
2367 cb_data
->major
= major(dev
);
2368 cb_data
->minor
= minor(dev
);
2370 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2371 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2372 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2373 free_mknod_cb_data(cb_data
);
2384 /* ReadDirPlus Emulation Callback data */
2385 struct rdpe_cb_data
{
2388 struct nfs_cb_data
*data
;
2391 /* ReadDirPlus Emulation LOOKUP Callback data */
2392 struct rdpe_lookup_cb_data
{
2393 struct rdpe_cb_data
*rdpe_cb_data
;
2394 struct nfsdirent
*nfsdirent
;
2397 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2398 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2400 LOOKUP3res
*res
= command_data
;
2401 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2402 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2403 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2404 struct nfsdir
*nfsdir
= data
->continue_data
;
2405 struct nfs_context
*nfs
= data
->nfs
;
2406 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2408 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2410 free(rdpe_lookup_cb_data
);
2412 rdpe_cb_data
->getattrcount
--;
2414 if (status
== RPC_STATUS_ERROR
) {
2415 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2417 if (status
== RPC_STATUS_CANCEL
) {
2418 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2420 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2421 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2423 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2424 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2425 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2427 nfsdirent
->type
= attributes
->type
;
2428 nfsdirent
->mode
= attributes
->mode
;
2429 nfsdirent
->size
= attributes
->size
;
2431 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2432 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2433 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2434 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2435 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2436 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2437 nfsdirent
->uid
= attributes
->uid
;
2438 nfsdirent
->gid
= attributes
->gid
;
2442 if (rdpe_cb_data
->getattrcount
== 0) {
2443 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2444 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2445 nfs_free_nfsdir(nfsdir
);
2447 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2451 data
->continue_data
= NULL
;
2452 free_nfs_cb_data(data
);
2456 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2458 READDIR3res
*res
= command_data
;
2459 struct nfs_cb_data
*data
= private_data
;
2460 struct nfs_context
*nfs
= data
->nfs
;
2461 struct nfsdir
*nfsdir
= data
->continue_data
;
2462 struct nfsdirent
*nfsdirent
;
2463 struct entry3
*entry
;
2465 struct rdpe_cb_data
*rdpe_cb_data
;
2467 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2469 if (status
== RPC_STATUS_ERROR
) {
2470 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2471 nfs_free_nfsdir(nfsdir
);
2472 data
->continue_data
= NULL
;
2473 free_nfs_cb_data(data
);
2477 if (status
== RPC_STATUS_CANCEL
) {
2478 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2479 nfs_free_nfsdir(nfsdir
);
2480 data
->continue_data
= NULL
;
2481 free_nfs_cb_data(data
);
2485 if (res
->status
!= NFS3_OK
) {
2486 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
));
2487 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2488 nfs_free_nfsdir(nfsdir
);
2489 data
->continue_data
= NULL
;
2490 free_nfs_cb_data(data
);
2494 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2495 while (entry
!= NULL
) {
2496 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2497 if (nfsdirent
== NULL
) {
2498 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2499 nfs_free_nfsdir(nfsdir
);
2500 data
->continue_data
= NULL
;
2501 free_nfs_cb_data(data
);
2504 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2505 nfsdirent
->name
= strdup(entry
->name
);
2506 if (nfsdirent
->name
== NULL
) {
2507 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2508 nfs_free_nfsdir(nfsdir
);
2509 data
->continue_data
= NULL
;
2510 free_nfs_cb_data(data
);
2513 nfsdirent
->inode
= entry
->fileid
;
2515 nfsdirent
->next
= nfsdir
->entries
;
2516 nfsdir
->entries
= nfsdirent
;
2518 cookie
= entry
->cookie
;
2519 entry
= entry
->nextentry
;
2522 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2525 args
.dir
= data
->fh
;
2526 args
.cookie
= cookie
;
2527 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2530 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2531 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2532 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2533 nfs_free_nfsdir(nfsdir
);
2534 data
->continue_data
= NULL
;
2535 free_nfs_cb_data(data
);
2541 /* steal the dirhandle */
2542 nfsdir
->current
= nfsdir
->entries
;
2544 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2545 rdpe_cb_data
->getattrcount
= 0;
2546 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2547 rdpe_cb_data
->data
= data
;
2548 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2549 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2552 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2553 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2554 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2556 memset(&args
, 0, sizeof(LOOKUP3args
));
2557 args
.what
.dir
= data
->fh
;
2558 args
.what
.name
= nfsdirent
->name
;
2560 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2561 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2563 /* if we have already commands in flight, we cant just stop, we have to wait for the
2564 * commands in flight to complete
2566 if (rdpe_cb_data
->getattrcount
> 0) {
2567 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2568 free(rdpe_lookup_cb_data
);
2572 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2573 nfs_free_nfsdir(nfsdir
);
2574 data
->continue_data
= NULL
;
2575 free_nfs_cb_data(data
);
2576 free(rdpe_lookup_cb_data
);
2580 rdpe_cb_data
->getattrcount
++;
2585 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2587 READDIRPLUS3res
*res
= command_data
;
2588 struct nfs_cb_data
*data
= private_data
;
2589 struct nfs_context
*nfs
= data
->nfs
;
2590 struct nfsdir
*nfsdir
= data
->continue_data
;
2591 struct entryplus3
*entry
;
2594 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2596 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2599 args
.dir
= data
->fh
;
2600 args
.cookie
= cookie
;
2601 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2604 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2605 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2606 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2607 nfs_free_nfsdir(nfsdir
);
2608 data
->continue_data
= NULL
;
2609 free_nfs_cb_data(data
);
2615 if (status
== RPC_STATUS_CANCEL
) {
2616 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2617 nfs_free_nfsdir(nfsdir
);
2618 data
->continue_data
= NULL
;
2619 free_nfs_cb_data(data
);
2623 if (res
->status
!= NFS3_OK
) {
2624 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
));
2625 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2626 nfs_free_nfsdir(nfsdir
);
2627 data
->continue_data
= NULL
;
2628 free_nfs_cb_data(data
);
2632 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2633 while (entry
!= NULL
) {
2634 struct nfsdirent
*nfsdirent
;
2636 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2637 if (nfsdirent
== NULL
) {
2638 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2639 nfs_free_nfsdir(nfsdir
);
2640 data
->continue_data
= NULL
;
2641 free_nfs_cb_data(data
);
2644 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2645 nfsdirent
->name
= strdup(entry
->name
);
2646 if (nfsdirent
->name
== NULL
) {
2647 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2648 nfs_free_nfsdir(nfsdir
);
2649 data
->continue_data
= NULL
;
2650 free_nfs_cb_data(data
);
2653 nfsdirent
->inode
= entry
->fileid
;
2654 if (entry
->name_attributes
.attributes_follow
) {
2655 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2656 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2657 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2659 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2660 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2661 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2662 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2663 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2664 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2665 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2666 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2669 nfsdirent
->next
= nfsdir
->entries
;
2670 nfsdir
->entries
= nfsdirent
;
2672 cookie
= entry
->cookie
;
2673 entry
= entry
->nextentry
;
2676 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2677 READDIRPLUS3args args
;
2679 args
.dir
= data
->fh
;
2680 args
.cookie
= cookie
;
2681 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2682 args
.dircount
= 8192;
2683 args
.maxcount
= 8192;
2685 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2686 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2687 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2688 nfs_free_nfsdir(nfsdir
);
2689 data
->continue_data
= NULL
;
2690 free_nfs_cb_data(data
);
2696 /* steal the dirhandle */
2697 data
->continue_data
= NULL
;
2698 nfsdir
->current
= nfsdir
->entries
;
2700 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2701 free_nfs_cb_data(data
);
2704 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2706 READDIRPLUS3args args
;
2708 args
.dir
= data
->fh
;
2710 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2711 args
.dircount
= 8192;
2712 args
.maxcount
= 8192;
2713 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2714 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2715 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2716 free_nfs_cb_data(data
);
2722 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2724 struct nfsdir
*nfsdir
;
2726 nfsdir
= malloc(sizeof(struct nfsdir
));
2727 if (nfsdir
== NULL
) {
2728 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2731 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2733 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2734 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2742 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2744 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2746 if (nfsdir
->current
!= NULL
) {
2747 nfsdir
->current
= nfsdir
->current
->next
;
2753 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2755 nfs_free_nfsdir(nfsdir
);
2767 struct lseek_cb_data
{
2768 struct nfs_context
*nfs
;
2769 struct nfsfh
*nfsfh
;
2775 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2778 struct lseek_cb_data
*data
= private_data
;
2779 struct nfs_context
*nfs
= data
->nfs
;
2781 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2783 if (status
== RPC_STATUS_ERROR
) {
2784 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2788 if (status
== RPC_STATUS_CANCEL
) {
2789 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2795 if (res
->status
!= NFS3_OK
) {
2796 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2797 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2802 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2803 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2807 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2809 struct lseek_cb_data
*data
;
2810 struct GETATTR3args args
;
2812 if (whence
== SEEK_SET
) {
2813 nfsfh
->offset
= offset
;
2814 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2817 if (whence
== SEEK_CUR
) {
2818 nfsfh
->offset
+= offset
;
2819 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2823 data
= malloc(sizeof(struct lseek_cb_data
));
2825 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2830 data
->nfsfh
= nfsfh
;
2831 data
->offset
= offset
;
2833 data
->private_data
= private_data
;
2835 memset(&args
, 0, sizeof(GETATTR3args
));
2836 args
.object
= nfsfh
->fh
;
2838 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
2839 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
2852 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2855 struct nfs_cb_data
*data
= private_data
;
2856 struct nfs_context
*nfs
= data
->nfs
;
2857 struct statvfs svfs
;
2859 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2861 if (status
== RPC_STATUS_ERROR
) {
2862 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2863 free_nfs_cb_data(data
);
2866 if (status
== RPC_STATUS_CANCEL
) {
2867 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2868 free_nfs_cb_data(data
);
2873 if (res
->status
!= NFS3_OK
) {
2874 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
));
2875 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2876 free_nfs_cb_data(data
);
2880 svfs
.f_bsize
= 4096;
2881 svfs
.f_frsize
= 4096;
2882 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
2883 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
2884 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
2885 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
2886 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
2887 #if !defined(ANDROID)
2888 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
2891 svfs
.f_namemax
= 256;
2894 data
->cb(0, nfs
, &svfs
, data
->private_data
);
2895 free_nfs_cb_data(data
);
2898 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2902 args
.fsroot
= data
->fh
;
2903 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
2904 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
2905 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2906 free_nfs_cb_data(data
);
2912 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2914 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
2915 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2928 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2931 struct nfs_cb_data
*data
= private_data
;
2932 struct nfs_context
*nfs
= data
->nfs
;
2934 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2936 if (status
== RPC_STATUS_ERROR
) {
2937 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2938 free_nfs_cb_data(data
);
2941 if (status
== RPC_STATUS_CANCEL
) {
2942 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2943 free_nfs_cb_data(data
);
2948 if (res
->status
!= NFS3_OK
) {
2949 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
));
2950 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2951 free_nfs_cb_data(data
);
2956 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
2957 free_nfs_cb_data(data
);
2960 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2964 args
.symlink
= data
->fh
;
2966 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
2967 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
2968 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2969 free_nfs_cb_data(data
);
2975 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2977 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
2978 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2991 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2993 struct nfs_cb_data
*data
= private_data
;
2994 struct nfs_context
*nfs
= data
->nfs
;
2997 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2999 if (status
== RPC_STATUS_ERROR
) {
3000 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3001 free_nfs_cb_data(data
);
3004 if (status
== RPC_STATUS_CANCEL
) {
3005 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3006 free_nfs_cb_data(data
);
3011 if (res
->status
!= NFS3_OK
) {
3012 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3013 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3014 free_nfs_cb_data(data
);
3018 data
->cb(0, nfs
, NULL
, data
->private_data
);
3019 free_nfs_cb_data(data
);
3022 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3026 memset(&args
, 0, sizeof(SETATTR3args
));
3027 args
.object
= data
->fh
;
3028 args
.new_attributes
.mode
.set_it
= 1;
3029 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3031 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3032 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3033 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3034 free_nfs_cb_data(data
);
3041 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3043 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3044 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3054 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3056 struct nfs_cb_data
*data
;
3058 data
= malloc(sizeof(struct nfs_cb_data
));
3060 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3063 memset(data
, 0, sizeof(struct nfs_cb_data
));
3066 data
->private_data
= private_data
;
3067 data
->continue_int
= mode
;
3068 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3069 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3070 if (data
->fh
.data
.data_val
== NULL
) {
3071 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3072 free_nfs_cb_data(data
);
3075 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3077 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3078 free_nfs_cb_data(data
);
3090 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3092 struct nfs_cb_data
*data
= private_data
;
3093 struct nfs_context
*nfs
= data
->nfs
;
3096 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3098 if (status
== RPC_STATUS_ERROR
) {
3099 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3100 free_nfs_cb_data(data
);
3103 if (status
== RPC_STATUS_CANCEL
) {
3104 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3105 free_nfs_cb_data(data
);
3110 if (res
->status
!= NFS3_OK
) {
3111 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3112 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3113 free_nfs_cb_data(data
);
3117 data
->cb(0, nfs
, NULL
, data
->private_data
);
3118 free_nfs_cb_data(data
);
3121 struct nfs_chown_data
{
3126 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3129 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3131 memset(&args
, 0, sizeof(SETATTR3args
));
3132 args
.object
= data
->fh
;
3133 if (chown_data
->uid
!= (uid_t
)-1) {
3134 args
.new_attributes
.uid
.set_it
= 1;
3135 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3137 if (chown_data
->gid
!= (gid_t
)-1) {
3138 args
.new_attributes
.gid
.set_it
= 1;
3139 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3142 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3143 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3144 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3145 free_nfs_cb_data(data
);
3152 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3154 struct nfs_chown_data
*chown_data
;
3156 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3157 if (chown_data
== NULL
) {
3158 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3162 chown_data
->uid
= uid
;
3163 chown_data
->gid
= gid
;
3165 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3166 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3177 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3179 struct nfs_cb_data
*data
;
3180 struct nfs_chown_data
*chown_data
;
3182 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3183 if (chown_data
== NULL
) {
3184 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3188 chown_data
->uid
= uid
;
3189 chown_data
->gid
= gid
;
3192 data
= malloc(sizeof(struct nfs_cb_data
));
3194 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3197 memset(data
, 0, sizeof(struct nfs_cb_data
));
3200 data
->private_data
= private_data
;
3201 data
->continue_data
= chown_data
;
3202 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3203 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3204 if (data
->fh
.data
.data_val
== NULL
) {
3205 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3206 free_nfs_cb_data(data
);
3209 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3212 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3213 free_nfs_cb_data(data
);
3227 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3229 struct nfs_cb_data
*data
= private_data
;
3230 struct nfs_context
*nfs
= data
->nfs
;
3233 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3235 if (status
== RPC_STATUS_ERROR
) {
3236 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3237 free_nfs_cb_data(data
);
3240 if (status
== RPC_STATUS_CANCEL
) {
3241 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3242 free_nfs_cb_data(data
);
3247 if (res
->status
!= NFS3_OK
) {
3248 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3249 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3250 free_nfs_cb_data(data
);
3254 data
->cb(0, nfs
, NULL
, data
->private_data
);
3255 free_nfs_cb_data(data
);
3258 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3261 struct timeval
*utimes_data
= data
->continue_data
;
3263 memset(&args
, 0, sizeof(SETATTR3args
));
3264 args
.object
= data
->fh
;
3265 if (utimes_data
!= NULL
) {
3266 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3267 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3268 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3269 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3270 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3271 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3273 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3274 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3277 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_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_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3289 struct timeval
*new_times
= NULL
;
3291 if (times
!= NULL
) {
3292 new_times
= malloc(sizeof(struct timeval
)*2);
3293 if (new_times
== NULL
) {
3294 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3298 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3301 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3302 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3312 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3314 struct timeval
*new_times
= NULL
;
3316 if (times
!= NULL
) {
3317 new_times
= malloc(sizeof(struct timeval
)*2);
3318 if (new_times
== NULL
) {
3319 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3323 new_times
[0].tv_sec
= times
->actime
;
3324 new_times
[0].tv_usec
= 0;
3325 new_times
[1].tv_sec
= times
->modtime
;
3326 new_times
[1].tv_usec
= 0;
3329 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3330 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3341 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3344 struct nfs_cb_data
*data
= private_data
;
3345 struct nfs_context
*nfs
= data
->nfs
;
3346 unsigned int nfsmode
= 0;
3348 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3350 if (status
== RPC_STATUS_ERROR
) {
3351 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3352 free_nfs_cb_data(data
);
3355 if (status
== RPC_STATUS_CANCEL
) {
3356 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3357 free_nfs_cb_data(data
);
3362 if (res
->status
!= NFS3_OK
) {
3363 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
));
3364 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3365 free_nfs_cb_data(data
);
3369 if (data
->continue_int
& R_OK
) {
3370 nfsmode
|= ACCESS3_READ
;
3372 if (data
->continue_int
& W_OK
) {
3373 nfsmode
|= ACCESS3_MODIFY
;
3375 if (data
->continue_int
& X_OK
) {
3376 nfsmode
|= ACCESS3_EXECUTE
;
3379 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3380 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3381 nfsmode
&ACCESS3_READ
?'r':'-',
3382 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3383 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3384 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3385 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3386 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3387 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3388 free_nfs_cb_data(data
);
3392 data
->cb(0, nfs
, NULL
, data
->private_data
);
3393 free_nfs_cb_data(data
);
3396 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3401 if (data
->continue_int
& R_OK
) {
3402 nfsmode
|= ACCESS3_READ
;
3404 if (data
->continue_int
& W_OK
) {
3405 nfsmode
|= ACCESS3_MODIFY
;
3407 if (data
->continue_int
& X_OK
) {
3408 nfsmode
|= ACCESS3_EXECUTE
;
3411 memset(&args
, 0, sizeof(ACCESS3args
));
3412 args
.object
= data
->fh
;
3413 args
.access
= nfsmode
;
3415 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3416 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3417 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3418 free_nfs_cb_data(data
);
3424 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3426 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3427 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3439 struct nfs_symlink_data
{
3441 char *newpathparent
;
3442 char *newpathobject
;
3445 static void free_nfs_symlink_data(void *mem
)
3447 struct nfs_symlink_data
*data
= mem
;
3449 if (data
->oldpath
!= NULL
) {
3450 free(data
->oldpath
);
3452 if (data
->newpathparent
!= NULL
) {
3453 free(data
->newpathparent
);
3455 if (data
->newpathobject
!= NULL
) {
3456 free(data
->newpathobject
);
3461 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3464 struct nfs_cb_data
*data
= private_data
;
3465 struct nfs_context
*nfs
= data
->nfs
;
3466 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3468 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3470 if (status
== RPC_STATUS_ERROR
) {
3471 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3472 free_nfs_cb_data(data
);
3475 if (status
== RPC_STATUS_CANCEL
) {
3476 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3477 free_nfs_cb_data(data
);
3482 if (res
->status
!= NFS3_OK
) {
3483 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
));
3484 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3485 free_nfs_cb_data(data
);
3489 data
->cb(0, nfs
, NULL
, data
->private_data
);
3490 free_nfs_cb_data(data
);
3493 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3495 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3498 memset(&args
, 0, sizeof(SYMLINK3args
));
3499 args
.where
.dir
= data
->fh
;
3500 args
.where
.name
= symlink_data
->newpathobject
;
3501 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3502 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
;
3503 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3505 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3506 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3507 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3508 free_nfs_cb_data(data
);
3514 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3517 struct nfs_symlink_data
*symlink_data
;
3519 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3520 if (symlink_data
== NULL
) {
3521 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3524 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3526 symlink_data
->oldpath
= strdup(oldpath
);
3527 if (symlink_data
->oldpath
== NULL
) {
3528 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3529 free_nfs_symlink_data(symlink_data
);
3533 symlink_data
->newpathparent
= strdup(newpath
);
3534 if (symlink_data
->newpathparent
== NULL
) {
3535 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3536 free_nfs_symlink_data(symlink_data
);
3540 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3542 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3543 free_nfs_symlink_data(symlink_data
);
3549 symlink_data
->newpathobject
= strdup(ptr
);
3550 if (symlink_data
->newpathobject
== NULL
) {
3551 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3552 free_nfs_symlink_data(symlink_data
);
3556 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3557 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3569 struct nfs_rename_data
{
3572 struct nfs_fh3 olddir
;
3575 struct nfs_fh3 newdir
;
3578 static void free_nfs_rename_data(void *mem
)
3580 struct nfs_rename_data
*data
= mem
;
3582 if (data
->oldpath
!= NULL
) {
3583 free(data
->oldpath
);
3585 if (data
->olddir
.data
.data_val
!= NULL
) {
3586 free(data
->olddir
.data
.data_val
);
3588 if (data
->newpath
!= NULL
) {
3589 free(data
->newpath
);
3591 if (data
->newdir
.data
.data_val
!= NULL
) {
3592 free(data
->newdir
.data
.data_val
);
3597 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3600 struct nfs_cb_data
*data
= private_data
;
3601 struct nfs_context
*nfs
= data
->nfs
;
3602 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3604 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3606 if (status
== RPC_STATUS_ERROR
) {
3607 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3608 free_nfs_cb_data(data
);
3611 if (status
== RPC_STATUS_CANCEL
) {
3612 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3613 free_nfs_cb_data(data
);
3618 if (res
->status
!= NFS3_OK
) {
3619 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
));
3620 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3621 free_nfs_cb_data(data
);
3625 data
->cb(0, nfs
, NULL
, data
->private_data
);
3626 free_nfs_cb_data(data
);
3629 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3631 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3634 /* steal the filehandle */
3635 rename_data
->newdir
= data
->fh
;
3636 data
->fh
.data
.data_val
= NULL
;
3638 args
.from
.dir
= rename_data
->olddir
;
3639 args
.from
.name
= rename_data
->oldobject
;
3640 args
.to
.dir
= rename_data
->newdir
;
3641 args
.to
.name
= rename_data
->newobject
;
3642 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3643 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3644 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3645 free_nfs_cb_data(data
);
3652 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3654 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3656 /* steal the filehandle */
3657 rename_data
->olddir
= data
->fh
;
3658 data
->fh
.data
.data_val
= NULL
;
3660 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) {
3661 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3662 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3663 free_nfs_cb_data(data
);
3666 data
->continue_data
= NULL
;
3667 free_nfs_cb_data(data
);
3673 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3676 struct nfs_rename_data
*rename_data
;
3678 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3679 if (rename_data
== NULL
) {
3680 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3683 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3685 rename_data
->oldpath
= strdup(oldpath
);
3686 if (rename_data
->oldpath
== NULL
) {
3687 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3688 free_nfs_rename_data(rename_data
);
3691 ptr
= strrchr(rename_data
->oldpath
, '/');
3693 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3694 free_nfs_rename_data(rename_data
);
3699 rename_data
->oldobject
= ptr
;
3702 rename_data
->newpath
= strdup(newpath
);
3703 if (rename_data
->newpath
== NULL
) {
3704 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3705 free_nfs_rename_data(rename_data
);
3708 ptr
= strrchr(rename_data
->newpath
, '/');
3710 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3711 free_nfs_rename_data(rename_data
);
3716 rename_data
->newobject
= ptr
;
3719 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3720 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3731 struct nfs_link_data
{
3733 struct nfs_fh3 oldfh
;
3736 struct nfs_fh3 newdir
;
3739 static void free_nfs_link_data(void *mem
)
3741 struct nfs_link_data
*data
= mem
;
3743 if (data
->oldpath
!= NULL
) {
3744 free(data
->oldpath
);
3746 if (data
->oldfh
.data
.data_val
!= NULL
) {
3747 free(data
->oldfh
.data
.data_val
);
3749 if (data
->newpath
!= NULL
) {
3750 free(data
->newpath
);
3752 if (data
->newdir
.data
.data_val
!= NULL
) {
3753 free(data
->newdir
.data
.data_val
);
3758 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3761 struct nfs_cb_data
*data
= private_data
;
3762 struct nfs_context
*nfs
= data
->nfs
;
3763 struct nfs_link_data
*link_data
= data
->continue_data
;
3765 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3767 if (status
== RPC_STATUS_ERROR
) {
3768 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3769 free_nfs_cb_data(data
);
3772 if (status
== RPC_STATUS_CANCEL
) {
3773 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3774 free_nfs_cb_data(data
);
3779 if (res
->status
!= NFS3_OK
) {
3780 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
));
3781 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3782 free_nfs_cb_data(data
);
3786 data
->cb(0, nfs
, NULL
, data
->private_data
);
3787 free_nfs_cb_data(data
);
3790 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3792 struct nfs_link_data
*link_data
= data
->continue_data
;
3795 /* steal the filehandle */
3796 link_data
->newdir
= data
->fh
;
3797 data
->fh
.data
.data_val
= NULL
;
3799 memset(&args
, 0, sizeof(LINK3args
));
3800 args
.file
= link_data
->oldfh
;
3801 args
.link
.dir
= link_data
->newdir
;
3802 args
.link
.name
= link_data
->newobject
;
3803 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
3804 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3805 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3806 free_nfs_cb_data(data
);
3813 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3815 struct nfs_link_data
*link_data
= data
->continue_data
;
3817 /* steal the filehandle */
3818 link_data
->oldfh
= data
->fh
;
3819 data
->fh
.data
.data_val
= NULL
;
3821 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) {
3822 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3823 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3824 free_nfs_cb_data(data
);
3827 data
->continue_data
= NULL
;
3828 free_nfs_cb_data(data
);
3834 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3837 struct nfs_link_data
*link_data
;
3839 link_data
= malloc(sizeof(struct nfs_link_data
));
3840 if (link_data
== NULL
) {
3841 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
3844 memset(link_data
, 0, sizeof(struct nfs_link_data
));
3846 link_data
->oldpath
= strdup(oldpath
);
3847 if (link_data
->oldpath
== NULL
) {
3848 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3849 free_nfs_link_data(link_data
);
3853 link_data
->newpath
= strdup(newpath
);
3854 if (link_data
->newpath
== NULL
) {
3855 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3856 free_nfs_link_data(link_data
);
3859 ptr
= strrchr(link_data
->newpath
, '/');
3861 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3862 free_nfs_link_data(link_data
);
3867 link_data
->newobject
= ptr
;
3870 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
3871 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3879 //qqq replace later with lseek()
3880 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
3882 return nfsfh
->offset
;
3888 * Get the maximum supported READ3 size by the server
3890 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
3892 return nfs
->readmax
;
3896 * Get the maximum supported WRITE3 size by the server
3898 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
3900 return nfs
->writemax
;
3903 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
3908 va_start(ap
, error_string
);
3910 vsnprintf(str
, 1024, error_string
, ap
);
3911 if (nfs
->rpc
->error_string
!= NULL
) {
3912 free(nfs
->rpc
->error_string
);
3914 nfs
->rpc
->error_string
= str
;
3920 struct mount_cb_data
{
3926 static void free_mount_cb_data(struct mount_cb_data
*data
)
3928 if (data
->server
!= NULL
) {
3930 data
->server
= NULL
;
3936 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3938 struct mount_cb_data
*data
= private_data
;
3940 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3942 if (status
== RPC_STATUS_ERROR
) {
3943 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3944 free_mount_cb_data(data
);
3947 if (status
== RPC_STATUS_CANCEL
) {
3948 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3949 free_mount_cb_data(data
);
3953 data
->cb(rpc
, 0, command_data
, data
->private_data
);
3954 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
3955 rpc_set_error(rpc
, "Failed to disconnect\n");
3957 free_mount_cb_data(data
);
3960 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3962 struct mount_cb_data
*data
= private_data
;
3964 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3966 /* Dont want any more callbacks even if the socket is closed */
3967 rpc
->connect_cb
= NULL
;
3969 if (status
== RPC_STATUS_ERROR
) {
3970 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3971 free_mount_cb_data(data
);
3974 if (status
== RPC_STATUS_CANCEL
) {
3975 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3976 free_mount_cb_data(data
);
3980 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
3981 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3982 free_mount_cb_data(data
);
3987 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3989 struct mount_cb_data
*data
= private_data
;
3990 uint32_t mount_port
;
3992 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3994 if (status
== RPC_STATUS_ERROR
) {
3995 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3996 free_mount_cb_data(data
);
3999 if (status
== RPC_STATUS_CANCEL
) {
4000 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4001 free_mount_cb_data(data
);
4005 mount_port
= *(uint32_t *)command_data
;
4006 if (mount_port
== 0) {
4007 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4008 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4009 free_mount_cb_data(data
);
4013 rpc_disconnect(rpc
, "normal disconnect");
4014 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4015 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4016 free_mount_cb_data(data
);
4021 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4023 struct mount_cb_data
*data
= private_data
;
4025 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4027 if (status
== RPC_STATUS_ERROR
) {
4028 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4029 free_mount_cb_data(data
);
4032 if (status
== RPC_STATUS_CANCEL
) {
4033 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4034 free_mount_cb_data(data
);
4038 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4039 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4040 free_mount_cb_data(data
);
4045 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4047 struct mount_cb_data
*data
= private_data
;
4049 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4051 /* Dont want any more callbacks even if the socket is closed */
4052 rpc
->connect_cb
= NULL
;
4054 if (status
== RPC_STATUS_ERROR
) {
4055 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4056 free_mount_cb_data(data
);
4059 if (status
== RPC_STATUS_CANCEL
) {
4060 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4061 free_mount_cb_data(data
);
4065 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4066 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4067 free_mount_cb_data(data
);
4072 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4074 struct mount_cb_data
*data
;
4076 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4078 data
= malloc(sizeof(struct mount_cb_data
));
4082 memset(data
, 0, sizeof(struct mount_cb_data
));
4084 data
->private_data
= private_data
;
4085 data
->server
= strdup(server
);
4086 if (data
->server
== NULL
) {
4087 free_mount_cb_data(data
);
4090 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4091 free_mount_cb_data(data
);
4098 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4100 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4104 const char *nfs_get_server(struct nfs_context
*nfs
) {
4108 const char *nfs_get_export(struct nfs_context
*nfs
) {
4112 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4113 return &nfs
->rootfh
;
4116 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {