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
;
1036 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1038 if (status
== RPC_STATUS_ERROR
) {
1039 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1040 free_nfs_cb_data(data
);
1043 if (status
== RPC_STATUS_CANCEL
) {
1044 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1045 free_nfs_cb_data(data
);
1050 if (res
->status
!= NFS3_OK
) {
1051 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
));
1052 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1053 free_nfs_cb_data(data
);
1058 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1059 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1060 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1061 st
.st_mode
|= S_IFDIR
;
1063 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1064 st
.st_mode
|= S_IFREG
;
1066 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1067 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1068 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1070 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1072 st
.st_blksize
= 4096;
1073 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1075 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1076 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1077 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1079 data
->cb(0, nfs
, &st
, data
->private_data
);
1080 free_nfs_cb_data(data
);
1083 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1085 struct GETATTR3args args
;
1087 memset(&args
, 0, sizeof(GETATTR3args
));
1088 args
.object
= data
->fh
;
1090 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1091 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1092 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1093 free_nfs_cb_data(data
);
1099 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1101 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1102 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1116 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1119 struct nfs_cb_data
*data
= private_data
;
1120 struct nfs_context
*nfs
= data
->nfs
;
1121 struct nfsfh
*nfsfh
;
1122 unsigned int nfsmode
= 0;
1124 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1126 if (status
== RPC_STATUS_ERROR
) {
1127 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1128 free_nfs_cb_data(data
);
1131 if (status
== RPC_STATUS_CANCEL
) {
1132 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1133 free_nfs_cb_data(data
);
1138 if (res
->status
!= NFS3_OK
) {
1139 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
));
1140 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1141 free_nfs_cb_data(data
);
1145 if (data
->continue_int
& O_WRONLY
) {
1146 nfsmode
|= ACCESS3_MODIFY
;
1148 if (data
->continue_int
& O_RDWR
) {
1149 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1151 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1152 nfsmode
|= ACCESS3_READ
;
1156 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1157 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1158 nfsmode
&ACCESS3_READ
?'r':'-',
1159 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1160 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1161 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1162 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1163 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1164 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1165 free_nfs_cb_data(data
);
1169 nfsfh
= malloc(sizeof(struct nfsfh
));
1170 if (nfsfh
== NULL
) {
1171 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1172 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1173 free_nfs_cb_data(data
);
1176 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1178 if (data
->continue_int
& O_SYNC
) {
1182 /* steal the filehandle */
1183 nfsfh
->fh
= data
->fh
;
1184 data
->fh
.data
.data_val
= NULL
;
1186 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1187 free_nfs_cb_data(data
);
1190 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1195 if (data
->continue_int
& O_WRONLY
) {
1196 nfsmode
|= ACCESS3_MODIFY
;
1198 if (data
->continue_int
& O_RDWR
) {
1199 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1201 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1202 nfsmode
|= ACCESS3_READ
;
1205 memset(&args
, 0, sizeof(ACCESS3args
));
1206 args
.object
= data
->fh
;
1207 args
.access
= nfsmode
;
1209 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1210 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
1211 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1212 free_nfs_cb_data(data
);
1218 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1220 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
1221 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1235 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1237 struct nfs_cb_data
*data
= private_data
;
1238 struct nfs_context
*nfs
= data
->nfs
;
1241 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1243 if (status
== RPC_STATUS_ERROR
) {
1244 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1245 free_nfs_cb_data(data
);
1248 if (status
== RPC_STATUS_CANCEL
) {
1249 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1250 free_nfs_cb_data(data
);
1255 if (res
->status
!= NFS3_OK
) {
1256 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1257 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1258 free_nfs_cb_data(data
);
1262 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1263 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1264 free_nfs_cb_data(data
);
1267 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1269 struct nfs_mcb_data
*mdata
= private_data
;
1270 struct nfs_cb_data
*data
= mdata
->data
;
1271 struct nfs_context
*nfs
= data
->nfs
;
1274 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1278 if (status
== RPC_STATUS_ERROR
) {
1279 /* flag the failure but do not invoke callback until we have received all responses */
1282 if (status
== RPC_STATUS_CANCEL
) {
1283 /* flag the cancellation but do not invoke callback until we have received all responses */
1287 /* reassemble the data into the buffer */
1288 if (status
== RPC_STATUS_SUCCESS
) {
1290 if (res
->status
!= NFS3_OK
) {
1291 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1294 if (res
->READ3res_u
.resok
.count
> 0) {
1295 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1296 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1297 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1303 if (data
->num_calls
> 0) {
1304 /* still waiting for more replies */
1309 if (data
->error
!= 0) {
1310 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1311 free_nfs_cb_data(data
);
1315 if (data
->cancel
!= 0) {
1316 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1317 free_nfs_cb_data(data
);
1322 data
->nfsfh
->offset
= data
->max_offset
;
1323 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1325 free_nfs_cb_data(data
);
1329 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1331 struct nfs_cb_data
*data
;
1333 data
= malloc(sizeof(struct nfs_cb_data
));
1335 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1338 memset(data
, 0, sizeof(struct nfs_cb_data
));
1341 data
->private_data
= private_data
;
1342 data
->nfsfh
= nfsfh
;
1344 nfsfh
->offset
= offset
;
1346 if (count
<= nfs_get_readmax(nfs
)) {
1349 memset(&args
, 0, sizeof(READ3args
));
1350 args
.file
= nfsfh
->fh
;
1351 args
.offset
= offset
;
1354 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_cb
, &args
, data
) != 0) {
1355 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1356 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1357 free_nfs_cb_data(data
);
1363 /* trying to read more than maximum server read size, we has to chop it up into smaller
1364 * reads and collect into a reassembly buffer.
1365 * we send all reads in parallel so that performance is still good.
1367 data
->max_offset
= offset
;
1368 data
->start_offset
= offset
;
1370 data
->buffer
= malloc(count
);
1371 if (data
->buffer
== NULL
) {
1372 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1373 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1374 free_nfs_cb_data(data
);
1379 uint64_t readcount
= count
;
1380 struct nfs_mcb_data
*mdata
;
1383 if (readcount
> nfs_get_readmax(nfs
)) {
1384 readcount
= nfs_get_readmax(nfs
);
1387 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1388 if (mdata
== NULL
) {
1389 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1392 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1394 mdata
->offset
= offset
;
1395 mdata
->count
= readcount
;
1397 memset(&args
, 0, sizeof(READ3args
));
1398 args
.file
= nfsfh
->fh
;
1399 args
.offset
= offset
;
1400 args
.count
= readcount
;
1402 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1403 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1404 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1410 offset
+= readcount
;
1420 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1422 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1430 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1432 struct nfs_cb_data
*data
= private_data
;
1433 struct nfs_context
*nfs
= data
->nfs
;
1436 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1438 if (status
== RPC_STATUS_ERROR
) {
1439 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1440 free_nfs_cb_data(data
);
1443 if (status
== RPC_STATUS_CANCEL
) {
1444 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1445 free_nfs_cb_data(data
);
1450 if (res
->status
!= NFS3_OK
) {
1451 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1452 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1453 free_nfs_cb_data(data
);
1457 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1458 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1459 free_nfs_cb_data(data
);
1462 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1464 struct nfs_mcb_data
*mdata
= private_data
;
1465 struct nfs_cb_data
*data
= mdata
->data
;
1466 struct nfs_context
*nfs
= data
->nfs
;
1469 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1473 if (status
== RPC_STATUS_ERROR
) {
1474 /* flag the failure but do not invoke callback until we have received all responses */
1477 if (status
== RPC_STATUS_CANCEL
) {
1478 /* flag the cancellation but do not invoke callback until we have received all responses */
1482 if (status
== RPC_STATUS_SUCCESS
) {
1484 if (res
->status
!= NFS3_OK
) {
1485 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1488 if (res
->WRITE3res_u
.resok
.count
> 0) {
1489 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1490 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1496 if (data
->num_calls
> 0) {
1497 /* still waiting for more replies */
1502 if (data
->error
!= 0) {
1503 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1504 free_nfs_cb_data(data
);
1508 if (data
->cancel
!= 0) {
1509 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1510 free_nfs_cb_data(data
);
1515 data
->nfsfh
->offset
= data
->max_offset
;
1516 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1518 free_nfs_cb_data(data
);
1523 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
)
1525 struct nfs_cb_data
*data
;
1527 data
= malloc(sizeof(struct nfs_cb_data
));
1529 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1532 memset(data
, 0, sizeof(struct nfs_cb_data
));
1535 data
->private_data
= private_data
;
1536 data
->nfsfh
= nfsfh
;
1538 nfsfh
->offset
= offset
;
1540 if (count
<= nfs_get_writemax(nfs
)) {
1543 memset(&args
, 0, sizeof(WRITE3args
));
1544 args
.file
= nfsfh
->fh
;
1545 args
.offset
= offset
;
1547 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1548 args
.data
.data_len
= count
;
1549 args
.data
.data_val
= buf
;
1551 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_cb
, &args
, data
) != 0) {
1552 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1553 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1554 free_nfs_cb_data(data
);
1560 /* trying to write more than maximum server write size, we has to chop it up into smaller
1562 * we send all writes in parallel so that performance is still good.
1564 data
->max_offset
= offset
;
1565 data
->start_offset
= offset
;
1568 uint64_t writecount
= count
;
1569 struct nfs_mcb_data
*mdata
;
1572 if (writecount
> nfs_get_writemax(nfs
)) {
1573 writecount
= nfs_get_writemax(nfs
);
1576 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1577 if (mdata
== NULL
) {
1578 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1581 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1583 mdata
->offset
= offset
;
1584 mdata
->count
= writecount
;
1586 memset(&args
, 0, sizeof(WRITE3args
));
1587 args
.file
= nfsfh
->fh
;
1588 args
.offset
= offset
;
1589 args
.count
= writecount
;
1590 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1591 args
.data
.data_len
= writecount
;
1592 args
.data
.data_val
= &buf
[offset
- data
->start_offset
];
1594 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1595 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1596 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1601 count
-= writecount
;
1602 offset
+= writecount
;
1612 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1614 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1624 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1626 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1627 free(nfsfh
->fh
.data
.data_val
);
1628 nfsfh
->fh
.data
.data_val
= NULL
;
1632 cb(0, nfs
, NULL
, private_data
);
1643 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1645 struct nfs_cb_data
*data
;
1646 struct GETATTR3args args
;
1648 data
= malloc(sizeof(struct nfs_cb_data
));
1650 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1653 memset(data
, 0, sizeof(struct nfs_cb_data
));
1656 data
->private_data
= private_data
;
1658 memset(&args
, 0, sizeof(GETATTR3args
));
1659 args
.object
= nfsfh
->fh
;
1661 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1662 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1663 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1664 free_nfs_cb_data(data
);
1675 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1677 struct nfs_cb_data
*data
= private_data
;
1678 struct nfs_context
*nfs
= data
->nfs
;
1681 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1683 if (status
== RPC_STATUS_ERROR
) {
1684 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1685 free_nfs_cb_data(data
);
1688 if (status
== RPC_STATUS_CANCEL
) {
1689 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1690 free_nfs_cb_data(data
);
1695 if (res
->status
!= NFS3_OK
) {
1696 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1697 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1698 free_nfs_cb_data(data
);
1702 data
->cb(0, nfs
, NULL
, data
->private_data
);
1703 free_nfs_cb_data(data
);
1706 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1708 struct nfs_cb_data
*data
;
1709 struct COMMIT3args args
;
1711 data
= malloc(sizeof(struct nfs_cb_data
));
1713 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1716 memset(data
, 0, sizeof(struct nfs_cb_data
));
1719 data
->private_data
= private_data
;
1721 args
.file
= nfsfh
->fh
;
1724 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1725 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1726 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1727 free_nfs_cb_data(data
);
1739 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1741 struct nfs_cb_data
*data
= private_data
;
1742 struct nfs_context
*nfs
= data
->nfs
;
1745 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1747 if (status
== RPC_STATUS_ERROR
) {
1748 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1749 free_nfs_cb_data(data
);
1752 if (status
== RPC_STATUS_CANCEL
) {
1753 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1754 free_nfs_cb_data(data
);
1759 if (res
->status
!= NFS3_OK
) {
1760 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1761 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1762 free_nfs_cb_data(data
);
1766 data
->cb(0, nfs
, NULL
, data
->private_data
);
1767 free_nfs_cb_data(data
);
1770 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1772 struct nfs_cb_data
*data
;
1775 data
= malloc(sizeof(struct nfs_cb_data
));
1777 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1780 memset(data
, 0, sizeof(struct nfs_cb_data
));
1783 data
->private_data
= private_data
;
1785 memset(&args
, 0, sizeof(SETATTR3args
));
1786 args
.object
= nfsfh
->fh
;
1787 args
.new_attributes
.size
.set_it
= 1;
1788 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1790 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1791 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1792 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1793 free_nfs_cb_data(data
);
1803 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1805 uint64_t offset
= data
->continue_int
;
1808 nfsfh
.fh
= data
->fh
;
1810 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1811 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1812 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1813 free_nfs_cb_data(data
);
1816 free_nfs_cb_data(data
);
1820 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1826 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1827 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1840 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1843 struct nfs_cb_data
*data
= private_data
;
1844 struct nfs_context
*nfs
= data
->nfs
;
1845 char *str
= data
->continue_data
;
1847 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1849 str
= &str
[strlen(str
) + 1];
1851 if (status
== RPC_STATUS_ERROR
) {
1852 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1853 free_nfs_cb_data(data
);
1856 if (status
== RPC_STATUS_CANCEL
) {
1857 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1858 free_nfs_cb_data(data
);
1863 if (res
->status
!= NFS3_OK
) {
1864 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
));
1865 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1866 free_nfs_cb_data(data
);
1870 data
->cb(0, nfs
, NULL
, data
->private_data
);
1871 free_nfs_cb_data(data
);
1874 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1876 char *str
= data
->continue_data
;
1879 str
= &str
[strlen(str
) + 1];
1881 memset(&args
, 0, sizeof(MKDIR3args
));
1882 args
.where
.dir
= data
->fh
;
1883 args
.where
.name
= str
;
1884 args
.attributes
.mode
.set_it
= 1;
1885 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
1887 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
1888 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1889 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1890 free_nfs_cb_data(data
);
1896 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1901 new_path
= strdup(path
);
1902 if (new_path
== NULL
) {
1903 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1907 ptr
= strrchr(new_path
, '/');
1909 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1914 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1915 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1916 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1930 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1933 struct nfs_cb_data
*data
= private_data
;
1934 struct nfs_context
*nfs
= data
->nfs
;
1935 char *str
= data
->continue_data
;
1937 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1939 str
= &str
[strlen(str
) + 1];
1941 if (status
== RPC_STATUS_ERROR
) {
1942 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1943 free_nfs_cb_data(data
);
1946 if (status
== RPC_STATUS_CANCEL
) {
1947 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1948 free_nfs_cb_data(data
);
1953 if (res
->status
!= NFS3_OK
) {
1954 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
));
1955 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1956 free_nfs_cb_data(data
);
1960 data
->cb(0, nfs
, NULL
, data
->private_data
);
1961 free_nfs_cb_data(data
);
1964 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1966 char *str
= data
->continue_data
;
1969 str
= &str
[strlen(str
) + 1];
1971 args
.object
.dir
= data
->fh
;
1972 args
.object
.name
= str
;
1973 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
1974 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1975 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1976 free_nfs_cb_data(data
);
1982 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1987 new_path
= strdup(path
);
1988 if (new_path
== NULL
) {
1989 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1993 ptr
= strrchr(new_path
, '/');
1995 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2000 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2001 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2002 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2015 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2018 struct nfs_cb_data
*data
= private_data
;
2019 struct nfs_context
*nfs
= data
->nfs
;
2020 struct nfsfh
*nfsfh
;
2021 char *str
= data
->continue_data
;
2023 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2025 if (status
== RPC_STATUS_ERROR
) {
2026 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2027 free_nfs_cb_data(data
);
2030 if (status
== RPC_STATUS_CANCEL
) {
2031 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2032 free_nfs_cb_data(data
);
2036 str
= &str
[strlen(str
) + 1];
2038 if (res
->status
!= NFS3_OK
) {
2039 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
));
2040 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2045 nfsfh
= malloc(sizeof(struct nfsfh
));
2046 if (nfsfh
== NULL
) {
2047 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2048 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2049 free_nfs_cb_data(data
);
2052 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2054 /* copy the filehandle */
2055 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2056 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2057 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2059 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2060 free_nfs_cb_data(data
);
2065 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2068 struct nfs_cb_data
*data
= private_data
;
2069 struct nfs_context
*nfs
= data
->nfs
;
2070 char *str
= data
->continue_data
;
2073 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2075 if (status
== RPC_STATUS_ERROR
) {
2076 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2077 free_nfs_cb_data(data
);
2080 if (status
== RPC_STATUS_CANCEL
) {
2081 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2082 free_nfs_cb_data(data
);
2086 str
= &str
[strlen(str
) + 1];
2088 if (res
->status
!= NFS3_OK
) {
2089 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
));
2090 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2091 free_nfs_cb_data(data
);
2095 memset(&args
, 0, sizeof(LOOKUP3args
));
2096 args
.what
.dir
= data
->fh
;
2097 args
.what
.name
= str
;
2099 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2100 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2101 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2102 free_nfs_cb_data(data
);
2108 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2110 char *str
= data
->continue_data
;
2113 str
= &str
[strlen(str
) + 1];
2115 memset(&args
, 0, sizeof(CREATE3args
));
2116 args
.where
.dir
= data
->fh
;
2117 args
.where
.name
= str
;
2118 args
.how
.mode
= UNCHECKED
;
2119 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2120 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2122 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2123 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2124 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2125 free_nfs_cb_data(data
);
2131 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2136 new_path
= strdup(path
);
2137 if (new_path
== NULL
) {
2138 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2142 ptr
= strrchr(new_path
, '/');
2144 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2149 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2150 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2151 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2164 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2167 struct nfs_cb_data
*data
= private_data
;
2168 struct nfs_context
*nfs
= data
->nfs
;
2169 char *str
= data
->continue_data
;
2171 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2173 str
= &str
[strlen(str
) + 1];
2175 if (status
== RPC_STATUS_ERROR
) {
2176 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2177 free_nfs_cb_data(data
);
2180 if (status
== RPC_STATUS_CANCEL
) {
2181 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2182 free_nfs_cb_data(data
);
2187 if (res
->status
!= NFS3_OK
) {
2188 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
));
2189 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2190 free_nfs_cb_data(data
);
2194 data
->cb(0, nfs
, NULL
, data
->private_data
);
2195 free_nfs_cb_data(data
);
2198 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2200 char *str
= data
->continue_data
;
2201 struct REMOVE3args args
;
2203 str
= &str
[strlen(str
) + 1];
2205 args
.object
.dir
= data
->fh
;
2206 args
.object
.name
= str
;
2207 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2208 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2209 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2210 free_nfs_cb_data(data
);
2216 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2221 new_path
= strdup(path
);
2222 if (new_path
== NULL
) {
2223 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2227 ptr
= strrchr(new_path
, '/');
2229 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2234 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2235 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2236 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2247 struct mknod_cb_data
{
2254 static void free_mknod_cb_data(void *ptr
)
2256 struct mknod_cb_data
*data
= ptr
;
2262 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2265 struct nfs_cb_data
*data
= private_data
;
2266 struct nfs_context
*nfs
= data
->nfs
;
2267 char *str
= data
->continue_data
;
2269 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2271 str
= &str
[strlen(str
) + 1];
2273 if (status
== RPC_STATUS_ERROR
) {
2274 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2275 free_nfs_cb_data(data
);
2278 if (status
== RPC_STATUS_CANCEL
) {
2279 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2280 free_nfs_cb_data(data
);
2285 if (res
->status
!= NFS3_OK
) {
2286 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
));
2287 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2288 free_nfs_cb_data(data
);
2292 data
->cb(0, nfs
, NULL
, data
->private_data
);
2293 free_nfs_cb_data(data
);
2296 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2298 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2299 char *str
= cb_data
->path
;
2302 str
= &str
[strlen(str
) + 1];
2304 args
.where
.dir
= data
->fh
;
2305 args
.where
.name
= str
;
2306 switch (cb_data
->mode
& S_IFMT
) {
2308 args
.what
.type
= NF3CHR
;
2309 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2310 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
);
2311 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2312 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2315 args
.what
.type
= NF3BLK
;
2316 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2317 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
);
2318 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2319 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2321 args
.what
.type
= NF3SOCK
;
2322 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2323 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
);
2326 args
.what
.type
= NF3FIFO
;
2327 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2328 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
);
2331 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2332 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2333 free_nfs_cb_data(data
);
2337 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2338 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2339 free_nfs_cb_data(data
);
2345 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2348 struct mknod_cb_data
*cb_data
;
2350 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2351 if (cb_data
== NULL
) {
2352 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2356 cb_data
->path
= strdup(path
);
2357 if (cb_data
->path
== NULL
) {
2358 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2363 ptr
= strrchr(cb_data
->path
, '/');
2365 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2370 cb_data
->mode
= mode
;
2371 cb_data
->major
= major(dev
);
2372 cb_data
->minor
= minor(dev
);
2374 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2375 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2376 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2377 free_mknod_cb_data(cb_data
);
2388 /* ReadDirPlus Emulation Callback data */
2389 struct rdpe_cb_data
{
2392 struct nfs_cb_data
*data
;
2395 /* ReadDirPlus Emulation LOOKUP Callback data */
2396 struct rdpe_lookup_cb_data
{
2397 struct rdpe_cb_data
*rdpe_cb_data
;
2398 struct nfsdirent
*nfsdirent
;
2401 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2402 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2404 LOOKUP3res
*res
= command_data
;
2405 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2406 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2407 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2408 struct nfsdir
*nfsdir
= data
->continue_data
;
2409 struct nfs_context
*nfs
= data
->nfs
;
2410 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2412 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2414 free(rdpe_lookup_cb_data
);
2416 rdpe_cb_data
->getattrcount
--;
2418 if (status
== RPC_STATUS_ERROR
) {
2419 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2421 if (status
== RPC_STATUS_CANCEL
) {
2422 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2424 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2425 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2427 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2428 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2429 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2431 nfsdirent
->type
= attributes
->type
;
2432 nfsdirent
->mode
= attributes
->mode
;
2433 nfsdirent
->size
= attributes
->size
;
2435 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2436 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2437 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2438 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2439 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2440 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2441 nfsdirent
->uid
= attributes
->uid
;
2442 nfsdirent
->gid
= attributes
->gid
;
2446 if (rdpe_cb_data
->getattrcount
== 0) {
2447 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2448 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2449 nfs_free_nfsdir(nfsdir
);
2451 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2455 data
->continue_data
= NULL
;
2456 free_nfs_cb_data(data
);
2460 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2462 READDIR3res
*res
= command_data
;
2463 struct nfs_cb_data
*data
= private_data
;
2464 struct nfs_context
*nfs
= data
->nfs
;
2465 struct nfsdir
*nfsdir
= data
->continue_data
;
2466 struct nfsdirent
*nfsdirent
;
2467 struct entry3
*entry
;
2469 struct rdpe_cb_data
*rdpe_cb_data
;
2471 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2473 if (status
== RPC_STATUS_ERROR
) {
2474 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2475 nfs_free_nfsdir(nfsdir
);
2476 data
->continue_data
= NULL
;
2477 free_nfs_cb_data(data
);
2481 if (status
== RPC_STATUS_CANCEL
) {
2482 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2483 nfs_free_nfsdir(nfsdir
);
2484 data
->continue_data
= NULL
;
2485 free_nfs_cb_data(data
);
2489 if (res
->status
!= NFS3_OK
) {
2490 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
));
2491 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2492 nfs_free_nfsdir(nfsdir
);
2493 data
->continue_data
= NULL
;
2494 free_nfs_cb_data(data
);
2498 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2499 while (entry
!= NULL
) {
2500 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2501 if (nfsdirent
== NULL
) {
2502 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2503 nfs_free_nfsdir(nfsdir
);
2504 data
->continue_data
= NULL
;
2505 free_nfs_cb_data(data
);
2508 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2509 nfsdirent
->name
= strdup(entry
->name
);
2510 if (nfsdirent
->name
== NULL
) {
2511 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2512 nfs_free_nfsdir(nfsdir
);
2513 data
->continue_data
= NULL
;
2514 free_nfs_cb_data(data
);
2517 nfsdirent
->inode
= entry
->fileid
;
2519 nfsdirent
->next
= nfsdir
->entries
;
2520 nfsdir
->entries
= nfsdirent
;
2522 cookie
= entry
->cookie
;
2523 entry
= entry
->nextentry
;
2526 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2529 args
.dir
= data
->fh
;
2530 args
.cookie
= cookie
;
2531 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2534 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2535 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2536 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2537 nfs_free_nfsdir(nfsdir
);
2538 data
->continue_data
= NULL
;
2539 free_nfs_cb_data(data
);
2545 /* steal the dirhandle */
2546 nfsdir
->current
= nfsdir
->entries
;
2548 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2549 rdpe_cb_data
->getattrcount
= 0;
2550 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2551 rdpe_cb_data
->data
= data
;
2552 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2553 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2556 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2557 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2558 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2560 memset(&args
, 0, sizeof(LOOKUP3args
));
2561 args
.what
.dir
= data
->fh
;
2562 args
.what
.name
= nfsdirent
->name
;
2564 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2565 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2567 /* if we have already commands in flight, we cant just stop, we have to wait for the
2568 * commands in flight to complete
2570 if (rdpe_cb_data
->getattrcount
> 0) {
2571 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2572 free(rdpe_lookup_cb_data
);
2576 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2577 nfs_free_nfsdir(nfsdir
);
2578 data
->continue_data
= NULL
;
2579 free_nfs_cb_data(data
);
2580 free(rdpe_lookup_cb_data
);
2584 rdpe_cb_data
->getattrcount
++;
2589 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2591 READDIRPLUS3res
*res
= command_data
;
2592 struct nfs_cb_data
*data
= private_data
;
2593 struct nfs_context
*nfs
= data
->nfs
;
2594 struct nfsdir
*nfsdir
= data
->continue_data
;
2595 struct entryplus3
*entry
;
2598 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2600 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2603 args
.dir
= data
->fh
;
2604 args
.cookie
= cookie
;
2605 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2608 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2609 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2610 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2611 nfs_free_nfsdir(nfsdir
);
2612 data
->continue_data
= NULL
;
2613 free_nfs_cb_data(data
);
2619 if (status
== RPC_STATUS_CANCEL
) {
2620 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2621 nfs_free_nfsdir(nfsdir
);
2622 data
->continue_data
= NULL
;
2623 free_nfs_cb_data(data
);
2627 if (res
->status
!= NFS3_OK
) {
2628 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
));
2629 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2630 nfs_free_nfsdir(nfsdir
);
2631 data
->continue_data
= NULL
;
2632 free_nfs_cb_data(data
);
2636 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2637 while (entry
!= NULL
) {
2638 struct nfsdirent
*nfsdirent
;
2640 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2641 if (nfsdirent
== NULL
) {
2642 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2643 nfs_free_nfsdir(nfsdir
);
2644 data
->continue_data
= NULL
;
2645 free_nfs_cb_data(data
);
2648 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2649 nfsdirent
->name
= strdup(entry
->name
);
2650 if (nfsdirent
->name
== NULL
) {
2651 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2652 nfs_free_nfsdir(nfsdir
);
2653 data
->continue_data
= NULL
;
2654 free_nfs_cb_data(data
);
2657 nfsdirent
->inode
= entry
->fileid
;
2658 if (entry
->name_attributes
.attributes_follow
) {
2659 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2660 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2661 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2663 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2664 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2665 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2666 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2667 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2668 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2669 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2670 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2673 nfsdirent
->next
= nfsdir
->entries
;
2674 nfsdir
->entries
= nfsdirent
;
2676 cookie
= entry
->cookie
;
2677 entry
= entry
->nextentry
;
2680 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2681 READDIRPLUS3args args
;
2683 args
.dir
= data
->fh
;
2684 args
.cookie
= cookie
;
2685 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2686 args
.dircount
= 8192;
2687 args
.maxcount
= 8192;
2689 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2690 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2691 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2692 nfs_free_nfsdir(nfsdir
);
2693 data
->continue_data
= NULL
;
2694 free_nfs_cb_data(data
);
2700 /* steal the dirhandle */
2701 data
->continue_data
= NULL
;
2702 nfsdir
->current
= nfsdir
->entries
;
2704 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2705 free_nfs_cb_data(data
);
2708 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2710 READDIRPLUS3args args
;
2712 args
.dir
= data
->fh
;
2714 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2715 args
.dircount
= 8192;
2716 args
.maxcount
= 8192;
2717 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2718 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2719 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2720 free_nfs_cb_data(data
);
2726 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2728 struct nfsdir
*nfsdir
;
2730 nfsdir
= malloc(sizeof(struct nfsdir
));
2731 if (nfsdir
== NULL
) {
2732 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2735 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2737 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2738 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2746 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2748 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2750 if (nfsdir
->current
!= NULL
) {
2751 nfsdir
->current
= nfsdir
->current
->next
;
2757 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2759 nfs_free_nfsdir(nfsdir
);
2771 struct lseek_cb_data
{
2772 struct nfs_context
*nfs
;
2773 struct nfsfh
*nfsfh
;
2779 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2782 struct lseek_cb_data
*data
= private_data
;
2783 struct nfs_context
*nfs
= data
->nfs
;
2785 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2787 if (status
== RPC_STATUS_ERROR
) {
2788 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2792 if (status
== RPC_STATUS_CANCEL
) {
2793 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2799 if (res
->status
!= NFS3_OK
) {
2800 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2801 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2806 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2807 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2811 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2813 struct lseek_cb_data
*data
;
2814 struct GETATTR3args args
;
2816 if (whence
== SEEK_SET
) {
2817 nfsfh
->offset
= offset
;
2818 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2821 if (whence
== SEEK_CUR
) {
2822 nfsfh
->offset
+= offset
;
2823 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2827 data
= malloc(sizeof(struct lseek_cb_data
));
2829 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2834 data
->nfsfh
= nfsfh
;
2835 data
->offset
= offset
;
2837 data
->private_data
= private_data
;
2839 memset(&args
, 0, sizeof(GETATTR3args
));
2840 args
.object
= nfsfh
->fh
;
2842 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
2843 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
2856 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2859 struct nfs_cb_data
*data
= private_data
;
2860 struct nfs_context
*nfs
= data
->nfs
;
2861 struct statvfs svfs
;
2863 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2865 if (status
== RPC_STATUS_ERROR
) {
2866 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2867 free_nfs_cb_data(data
);
2870 if (status
== RPC_STATUS_CANCEL
) {
2871 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2872 free_nfs_cb_data(data
);
2877 if (res
->status
!= NFS3_OK
) {
2878 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
));
2879 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2880 free_nfs_cb_data(data
);
2884 svfs
.f_bsize
= 4096;
2885 svfs
.f_frsize
= 4096;
2886 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
2887 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
2888 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
2889 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
2890 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
2891 #if !defined(ANDROID)
2892 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
2895 svfs
.f_namemax
= 256;
2898 data
->cb(0, nfs
, &svfs
, data
->private_data
);
2899 free_nfs_cb_data(data
);
2902 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2906 args
.fsroot
= data
->fh
;
2907 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
2908 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
2909 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2910 free_nfs_cb_data(data
);
2916 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2918 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
2919 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2932 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2935 struct nfs_cb_data
*data
= private_data
;
2936 struct nfs_context
*nfs
= data
->nfs
;
2938 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2940 if (status
== RPC_STATUS_ERROR
) {
2941 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2942 free_nfs_cb_data(data
);
2945 if (status
== RPC_STATUS_CANCEL
) {
2946 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2947 free_nfs_cb_data(data
);
2952 if (res
->status
!= NFS3_OK
) {
2953 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
));
2954 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2955 free_nfs_cb_data(data
);
2960 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
2961 free_nfs_cb_data(data
);
2964 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2968 args
.symlink
= data
->fh
;
2970 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
2971 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
2972 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2973 free_nfs_cb_data(data
);
2979 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2981 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
2982 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2995 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2997 struct nfs_cb_data
*data
= private_data
;
2998 struct nfs_context
*nfs
= data
->nfs
;
3001 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3003 if (status
== RPC_STATUS_ERROR
) {
3004 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3005 free_nfs_cb_data(data
);
3008 if (status
== RPC_STATUS_CANCEL
) {
3009 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3010 free_nfs_cb_data(data
);
3015 if (res
->status
!= NFS3_OK
) {
3016 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3017 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3018 free_nfs_cb_data(data
);
3022 data
->cb(0, nfs
, NULL
, data
->private_data
);
3023 free_nfs_cb_data(data
);
3026 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3030 memset(&args
, 0, sizeof(SETATTR3args
));
3031 args
.object
= data
->fh
;
3032 args
.new_attributes
.mode
.set_it
= 1;
3033 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3035 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3036 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3037 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3038 free_nfs_cb_data(data
);
3045 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3047 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3048 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3058 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3060 struct nfs_cb_data
*data
;
3062 data
= malloc(sizeof(struct nfs_cb_data
));
3064 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3067 memset(data
, 0, sizeof(struct nfs_cb_data
));
3070 data
->private_data
= private_data
;
3071 data
->continue_int
= mode
;
3072 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3073 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3074 if (data
->fh
.data
.data_val
== NULL
) {
3075 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3076 free_nfs_cb_data(data
);
3079 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3081 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3082 free_nfs_cb_data(data
);
3094 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3096 struct nfs_cb_data
*data
= private_data
;
3097 struct nfs_context
*nfs
= data
->nfs
;
3100 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3102 if (status
== RPC_STATUS_ERROR
) {
3103 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3104 free_nfs_cb_data(data
);
3107 if (status
== RPC_STATUS_CANCEL
) {
3108 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3109 free_nfs_cb_data(data
);
3114 if (res
->status
!= NFS3_OK
) {
3115 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3116 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3117 free_nfs_cb_data(data
);
3121 data
->cb(0, nfs
, NULL
, data
->private_data
);
3122 free_nfs_cb_data(data
);
3125 struct nfs_chown_data
{
3130 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3133 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3135 memset(&args
, 0, sizeof(SETATTR3args
));
3136 args
.object
= data
->fh
;
3137 if (chown_data
->uid
!= (uid_t
)-1) {
3138 args
.new_attributes
.uid
.set_it
= 1;
3139 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3141 if (chown_data
->gid
!= (gid_t
)-1) {
3142 args
.new_attributes
.gid
.set_it
= 1;
3143 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3146 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3147 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3148 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3149 free_nfs_cb_data(data
);
3156 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3158 struct nfs_chown_data
*chown_data
;
3160 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3161 if (chown_data
== NULL
) {
3162 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3166 chown_data
->uid
= uid
;
3167 chown_data
->gid
= gid
;
3169 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3170 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3181 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3183 struct nfs_cb_data
*data
;
3184 struct nfs_chown_data
*chown_data
;
3186 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3187 if (chown_data
== NULL
) {
3188 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3192 chown_data
->uid
= uid
;
3193 chown_data
->gid
= gid
;
3196 data
= malloc(sizeof(struct nfs_cb_data
));
3198 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3201 memset(data
, 0, sizeof(struct nfs_cb_data
));
3204 data
->private_data
= private_data
;
3205 data
->continue_data
= chown_data
;
3206 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3207 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3208 if (data
->fh
.data
.data_val
== NULL
) {
3209 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3210 free_nfs_cb_data(data
);
3213 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3216 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3217 free_nfs_cb_data(data
);
3231 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3233 struct nfs_cb_data
*data
= private_data
;
3234 struct nfs_context
*nfs
= data
->nfs
;
3237 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3239 if (status
== RPC_STATUS_ERROR
) {
3240 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3241 free_nfs_cb_data(data
);
3244 if (status
== RPC_STATUS_CANCEL
) {
3245 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3246 free_nfs_cb_data(data
);
3251 if (res
->status
!= NFS3_OK
) {
3252 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3253 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3254 free_nfs_cb_data(data
);
3258 data
->cb(0, nfs
, NULL
, data
->private_data
);
3259 free_nfs_cb_data(data
);
3262 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3265 struct timeval
*utimes_data
= data
->continue_data
;
3267 memset(&args
, 0, sizeof(SETATTR3args
));
3268 args
.object
= data
->fh
;
3269 if (utimes_data
!= NULL
) {
3270 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3271 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3272 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3273 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3274 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3275 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3277 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3278 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3281 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3282 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3283 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3284 free_nfs_cb_data(data
);
3291 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3293 struct timeval
*new_times
= NULL
;
3295 if (times
!= NULL
) {
3296 new_times
= malloc(sizeof(struct timeval
)*2);
3297 if (new_times
== NULL
) {
3298 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3302 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3305 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3306 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3316 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3318 struct timeval
*new_times
= NULL
;
3320 if (times
!= NULL
) {
3321 new_times
= malloc(sizeof(struct timeval
)*2);
3322 if (new_times
== NULL
) {
3323 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3327 new_times
[0].tv_sec
= times
->actime
;
3328 new_times
[0].tv_usec
= 0;
3329 new_times
[1].tv_sec
= times
->modtime
;
3330 new_times
[1].tv_usec
= 0;
3333 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3334 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3345 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3348 struct nfs_cb_data
*data
= private_data
;
3349 struct nfs_context
*nfs
= data
->nfs
;
3350 unsigned int nfsmode
= 0;
3352 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3354 if (status
== RPC_STATUS_ERROR
) {
3355 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3356 free_nfs_cb_data(data
);
3359 if (status
== RPC_STATUS_CANCEL
) {
3360 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3361 free_nfs_cb_data(data
);
3366 if (res
->status
!= NFS3_OK
) {
3367 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
));
3368 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3369 free_nfs_cb_data(data
);
3373 if (data
->continue_int
& R_OK
) {
3374 nfsmode
|= ACCESS3_READ
;
3376 if (data
->continue_int
& W_OK
) {
3377 nfsmode
|= ACCESS3_MODIFY
;
3379 if (data
->continue_int
& X_OK
) {
3380 nfsmode
|= ACCESS3_EXECUTE
;
3383 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3384 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3385 nfsmode
&ACCESS3_READ
?'r':'-',
3386 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3387 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3388 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3389 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3390 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3391 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3392 free_nfs_cb_data(data
);
3396 data
->cb(0, nfs
, NULL
, data
->private_data
);
3397 free_nfs_cb_data(data
);
3400 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3405 if (data
->continue_int
& R_OK
) {
3406 nfsmode
|= ACCESS3_READ
;
3408 if (data
->continue_int
& W_OK
) {
3409 nfsmode
|= ACCESS3_MODIFY
;
3411 if (data
->continue_int
& X_OK
) {
3412 nfsmode
|= ACCESS3_EXECUTE
;
3415 memset(&args
, 0, sizeof(ACCESS3args
));
3416 args
.object
= data
->fh
;
3417 args
.access
= nfsmode
;
3419 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3420 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3421 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3422 free_nfs_cb_data(data
);
3428 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3430 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3431 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3443 struct nfs_symlink_data
{
3445 char *newpathparent
;
3446 char *newpathobject
;
3449 static void free_nfs_symlink_data(void *mem
)
3451 struct nfs_symlink_data
*data
= mem
;
3453 if (data
->oldpath
!= NULL
) {
3454 free(data
->oldpath
);
3456 if (data
->newpathparent
!= NULL
) {
3457 free(data
->newpathparent
);
3459 if (data
->newpathobject
!= NULL
) {
3460 free(data
->newpathobject
);
3465 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3468 struct nfs_cb_data
*data
= private_data
;
3469 struct nfs_context
*nfs
= data
->nfs
;
3470 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3472 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3474 if (status
== RPC_STATUS_ERROR
) {
3475 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3476 free_nfs_cb_data(data
);
3479 if (status
== RPC_STATUS_CANCEL
) {
3480 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3481 free_nfs_cb_data(data
);
3486 if (res
->status
!= NFS3_OK
) {
3487 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
));
3488 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3489 free_nfs_cb_data(data
);
3493 data
->cb(0, nfs
, NULL
, data
->private_data
);
3494 free_nfs_cb_data(data
);
3497 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3499 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3502 memset(&args
, 0, sizeof(SYMLINK3args
));
3503 args
.where
.dir
= data
->fh
;
3504 args
.where
.name
= symlink_data
->newpathobject
;
3505 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3506 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
;
3507 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3509 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3510 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3511 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3512 free_nfs_cb_data(data
);
3518 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3521 struct nfs_symlink_data
*symlink_data
;
3523 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3524 if (symlink_data
== NULL
) {
3525 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3528 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3530 symlink_data
->oldpath
= strdup(oldpath
);
3531 if (symlink_data
->oldpath
== NULL
) {
3532 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3533 free_nfs_symlink_data(symlink_data
);
3537 symlink_data
->newpathparent
= strdup(newpath
);
3538 if (symlink_data
->newpathparent
== NULL
) {
3539 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3540 free_nfs_symlink_data(symlink_data
);
3544 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3546 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3547 free_nfs_symlink_data(symlink_data
);
3553 symlink_data
->newpathobject
= strdup(ptr
);
3554 if (symlink_data
->newpathobject
== NULL
) {
3555 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3556 free_nfs_symlink_data(symlink_data
);
3560 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3561 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3573 struct nfs_rename_data
{
3576 struct nfs_fh3 olddir
;
3579 struct nfs_fh3 newdir
;
3582 static void free_nfs_rename_data(void *mem
)
3584 struct nfs_rename_data
*data
= mem
;
3586 if (data
->oldpath
!= NULL
) {
3587 free(data
->oldpath
);
3589 if (data
->olddir
.data
.data_val
!= NULL
) {
3590 free(data
->olddir
.data
.data_val
);
3592 if (data
->newpath
!= NULL
) {
3593 free(data
->newpath
);
3595 if (data
->newdir
.data
.data_val
!= NULL
) {
3596 free(data
->newdir
.data
.data_val
);
3601 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3604 struct nfs_cb_data
*data
= private_data
;
3605 struct nfs_context
*nfs
= data
->nfs
;
3606 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3608 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3610 if (status
== RPC_STATUS_ERROR
) {
3611 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3612 free_nfs_cb_data(data
);
3615 if (status
== RPC_STATUS_CANCEL
) {
3616 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3617 free_nfs_cb_data(data
);
3622 if (res
->status
!= NFS3_OK
) {
3623 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
));
3624 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3625 free_nfs_cb_data(data
);
3629 data
->cb(0, nfs
, NULL
, data
->private_data
);
3630 free_nfs_cb_data(data
);
3633 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3635 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3638 /* steal the filehandle */
3639 rename_data
->newdir
= data
->fh
;
3640 data
->fh
.data
.data_val
= NULL
;
3642 args
.from
.dir
= rename_data
->olddir
;
3643 args
.from
.name
= rename_data
->oldobject
;
3644 args
.to
.dir
= rename_data
->newdir
;
3645 args
.to
.name
= rename_data
->newobject
;
3646 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3647 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3648 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3649 free_nfs_cb_data(data
);
3656 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3658 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3660 /* steal the filehandle */
3661 rename_data
->olddir
= data
->fh
;
3662 data
->fh
.data
.data_val
= NULL
;
3664 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) {
3665 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3666 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3667 free_nfs_cb_data(data
);
3670 data
->continue_data
= NULL
;
3671 free_nfs_cb_data(data
);
3677 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3680 struct nfs_rename_data
*rename_data
;
3682 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3683 if (rename_data
== NULL
) {
3684 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3687 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3689 rename_data
->oldpath
= strdup(oldpath
);
3690 if (rename_data
->oldpath
== NULL
) {
3691 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3692 free_nfs_rename_data(rename_data
);
3695 ptr
= strrchr(rename_data
->oldpath
, '/');
3697 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3698 free_nfs_rename_data(rename_data
);
3703 rename_data
->oldobject
= ptr
;
3706 rename_data
->newpath
= strdup(newpath
);
3707 if (rename_data
->newpath
== NULL
) {
3708 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3709 free_nfs_rename_data(rename_data
);
3712 ptr
= strrchr(rename_data
->newpath
, '/');
3714 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3715 free_nfs_rename_data(rename_data
);
3720 rename_data
->newobject
= ptr
;
3723 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3724 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3735 struct nfs_link_data
{
3737 struct nfs_fh3 oldfh
;
3740 struct nfs_fh3 newdir
;
3743 static void free_nfs_link_data(void *mem
)
3745 struct nfs_link_data
*data
= mem
;
3747 if (data
->oldpath
!= NULL
) {
3748 free(data
->oldpath
);
3750 if (data
->oldfh
.data
.data_val
!= NULL
) {
3751 free(data
->oldfh
.data
.data_val
);
3753 if (data
->newpath
!= NULL
) {
3754 free(data
->newpath
);
3756 if (data
->newdir
.data
.data_val
!= NULL
) {
3757 free(data
->newdir
.data
.data_val
);
3762 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3765 struct nfs_cb_data
*data
= private_data
;
3766 struct nfs_context
*nfs
= data
->nfs
;
3767 struct nfs_link_data
*link_data
= data
->continue_data
;
3769 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3771 if (status
== RPC_STATUS_ERROR
) {
3772 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3773 free_nfs_cb_data(data
);
3776 if (status
== RPC_STATUS_CANCEL
) {
3777 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3778 free_nfs_cb_data(data
);
3783 if (res
->status
!= NFS3_OK
) {
3784 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
));
3785 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3786 free_nfs_cb_data(data
);
3790 data
->cb(0, nfs
, NULL
, data
->private_data
);
3791 free_nfs_cb_data(data
);
3794 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3796 struct nfs_link_data
*link_data
= data
->continue_data
;
3799 /* steal the filehandle */
3800 link_data
->newdir
= data
->fh
;
3801 data
->fh
.data
.data_val
= NULL
;
3803 memset(&args
, 0, sizeof(LINK3args
));
3804 args
.file
= link_data
->oldfh
;
3805 args
.link
.dir
= link_data
->newdir
;
3806 args
.link
.name
= link_data
->newobject
;
3807 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
3808 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3809 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3810 free_nfs_cb_data(data
);
3817 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3819 struct nfs_link_data
*link_data
= data
->continue_data
;
3821 /* steal the filehandle */
3822 link_data
->oldfh
= data
->fh
;
3823 data
->fh
.data
.data_val
= NULL
;
3825 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) {
3826 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3827 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3828 free_nfs_cb_data(data
);
3831 data
->continue_data
= NULL
;
3832 free_nfs_cb_data(data
);
3838 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3841 struct nfs_link_data
*link_data
;
3843 link_data
= malloc(sizeof(struct nfs_link_data
));
3844 if (link_data
== NULL
) {
3845 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
3848 memset(link_data
, 0, sizeof(struct nfs_link_data
));
3850 link_data
->oldpath
= strdup(oldpath
);
3851 if (link_data
->oldpath
== NULL
) {
3852 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3853 free_nfs_link_data(link_data
);
3857 link_data
->newpath
= strdup(newpath
);
3858 if (link_data
->newpath
== NULL
) {
3859 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3860 free_nfs_link_data(link_data
);
3863 ptr
= strrchr(link_data
->newpath
, '/');
3865 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3866 free_nfs_link_data(link_data
);
3871 link_data
->newobject
= ptr
;
3874 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
3875 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3883 //qqq replace later with lseek()
3884 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
3886 return nfsfh
->offset
;
3892 * Get the maximum supported READ3 size by the server
3894 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
3896 return nfs
->readmax
;
3900 * Get the maximum supported WRITE3 size by the server
3902 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
3904 return nfs
->writemax
;
3907 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
3908 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
3911 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
3912 rpc_set_uid(nfs
->rpc
, uid
);
3915 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
3916 rpc_set_gid(nfs
->rpc
, gid
);
3919 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
3924 va_start(ap
, error_string
);
3926 vsnprintf(str
, 1024, error_string
, ap
);
3927 if (nfs
->rpc
->error_string
!= NULL
) {
3928 free(nfs
->rpc
->error_string
);
3930 nfs
->rpc
->error_string
= str
;
3936 struct mount_cb_data
{
3942 static void free_mount_cb_data(struct mount_cb_data
*data
)
3944 if (data
->server
!= NULL
) {
3946 data
->server
= NULL
;
3952 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3954 struct mount_cb_data
*data
= private_data
;
3956 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3958 if (status
== RPC_STATUS_ERROR
) {
3959 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3960 free_mount_cb_data(data
);
3963 if (status
== RPC_STATUS_CANCEL
) {
3964 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3965 free_mount_cb_data(data
);
3969 data
->cb(rpc
, 0, command_data
, data
->private_data
);
3970 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
3971 rpc_set_error(rpc
, "Failed to disconnect\n");
3973 free_mount_cb_data(data
);
3976 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3978 struct mount_cb_data
*data
= private_data
;
3980 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3982 /* Dont want any more callbacks even if the socket is closed */
3983 rpc
->connect_cb
= NULL
;
3985 if (status
== RPC_STATUS_ERROR
) {
3986 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3987 free_mount_cb_data(data
);
3990 if (status
== RPC_STATUS_CANCEL
) {
3991 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3992 free_mount_cb_data(data
);
3996 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
3997 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3998 free_mount_cb_data(data
);
4003 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4005 struct mount_cb_data
*data
= private_data
;
4006 uint32_t mount_port
;
4008 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4010 if (status
== RPC_STATUS_ERROR
) {
4011 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4012 free_mount_cb_data(data
);
4015 if (status
== RPC_STATUS_CANCEL
) {
4016 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4017 free_mount_cb_data(data
);
4021 mount_port
= *(uint32_t *)command_data
;
4022 if (mount_port
== 0) {
4023 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4024 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4025 free_mount_cb_data(data
);
4029 rpc_disconnect(rpc
, "normal disconnect");
4030 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4031 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4032 free_mount_cb_data(data
);
4037 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4039 struct mount_cb_data
*data
= private_data
;
4041 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4043 if (status
== RPC_STATUS_ERROR
) {
4044 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4045 free_mount_cb_data(data
);
4048 if (status
== RPC_STATUS_CANCEL
) {
4049 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4050 free_mount_cb_data(data
);
4054 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4055 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4056 free_mount_cb_data(data
);
4061 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4063 struct mount_cb_data
*data
= private_data
;
4065 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4067 /* Dont want any more callbacks even if the socket is closed */
4068 rpc
->connect_cb
= NULL
;
4070 if (status
== RPC_STATUS_ERROR
) {
4071 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4072 free_mount_cb_data(data
);
4075 if (status
== RPC_STATUS_CANCEL
) {
4076 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4077 free_mount_cb_data(data
);
4081 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4082 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4083 free_mount_cb_data(data
);
4088 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4090 struct mount_cb_data
*data
;
4092 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4094 data
= malloc(sizeof(struct mount_cb_data
));
4098 memset(data
, 0, sizeof(struct mount_cb_data
));
4100 data
->private_data
= private_data
;
4101 data
->server
= strdup(server
);
4102 if (data
->server
== NULL
) {
4103 free_mount_cb_data(data
);
4106 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4107 free_mount_cb_data(data
);
4114 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4116 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4120 const char *nfs_get_server(struct nfs_context
*nfs
) {
4124 const char *nfs_get_export(struct nfs_context
*nfs
) {
4128 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4129 return &nfs
->rootfh
;
4132 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {