2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * High level api to nfs filesystems
25 #include "aros_compat.h"
29 #include "win32_compat.h"
37 #define statvfs statfs
50 #ifdef HAVE_SYS_STATVFS_H
51 #include <sys/statvfs.h>
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
68 #include <sys/types.h>
71 #include "libnfs-zdr.h"
73 #include "libnfs-raw.h"
74 #include "libnfs-raw-mount.h"
75 #include "libnfs-raw-nfs.h"
76 #include "libnfs-private.h"
79 struct nfsdirent
*entries
;
80 struct nfsdirent
*current
;
90 struct rpc_context
*rpc
;
93 struct nfs_fh3 rootfh
;
98 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
100 while (nfsdir
->entries
) {
101 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
102 if (nfsdir
->entries
->name
!= NULL
) {
103 free(nfsdir
->entries
->name
);
105 free(nfsdir
->entries
);
106 nfsdir
->entries
= dirent
;
112 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
115 struct nfs_context
*nfs
;
117 char *saved_path
, *path
;
122 continue_func continue_cb
;
124 void (*free_continue_data
)(void *);
129 /* for multi-read/write calls. */
133 uint64_t start_offset
, max_offset
;
137 struct nfs_mcb_data
{
138 struct nfs_cb_data
*data
;
143 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
146 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
148 rpc_set_auth(nfs
->rpc
, auth
);
151 int nfs_get_fd(struct nfs_context
*nfs
)
153 return rpc_get_fd(nfs
->rpc
);
156 int nfs_queue_length(struct nfs_context
*nfs
)
158 return rpc_queue_length(nfs
->rpc
);
161 int nfs_which_events(struct nfs_context
*nfs
)
163 return rpc_which_events(nfs
->rpc
);
166 int nfs_service(struct nfs_context
*nfs
, int revents
)
168 return rpc_service(nfs
->rpc
, revents
);
171 char *nfs_get_error(struct nfs_context
*nfs
)
173 return rpc_get_error(nfs
->rpc
);
176 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
178 if (!strncmp(arg
, "tcp-syncnt", 10)) {
179 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
180 } else if (!strncmp(arg
, "uid", 3)) {
181 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
182 } else if (!strncmp(arg
, "gid", 3)) {
183 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
188 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
190 struct nfs_url
*urls
;
191 char *strp
, *flagsp
, *strp2
;
193 if (strncmp(url
, "nfs://", 6)) {
194 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
198 urls
= malloc(sizeof(struct nfs_url
));
200 rpc_set_error(nfs
->rpc
, "Out of memory");
204 memset(urls
, 0x00, sizeof(struct nfs_url
));
205 urls
->server
= strdup(url
+ 6);
206 if (urls
->server
== NULL
) {
207 nfs_destroy_url(urls
);
208 rpc_set_error(nfs
->rpc
, "Out of memory");
212 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
213 urls
->server
[0] == '?') {
215 flagsp
= strchr(urls
->server
, '?');
218 nfs_destroy_url(urls
);
219 rpc_set_error(nfs
->rpc
, "Invalid server string");
223 strp
= strchr(urls
->server
, '/');
226 flagsp
= strchr(urls
->server
, '?');
229 nfs_destroy_url(urls
);
230 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
234 urls
->path
= strdup(strp
);
235 if (urls
->path
== NULL
) {
236 nfs_destroy_url(urls
);
237 rpc_set_error(nfs
->rpc
, "Out of memory");
243 flagsp
= strchr(urls
->path
, '?');
247 strp
= strrchr(urls
->path
, '/');
250 flagsp
= strchr(urls
->path
, '?');
253 nfs_destroy_url(urls
);
254 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
257 urls
->file
= strdup(strp
);
258 if (urls
->path
== NULL
) {
259 nfs_destroy_url(urls
);
260 rpc_set_error(nfs
->rpc
, "Out of memory");
264 flagsp
= strchr(urls
->file
, '?');
271 if (urls
->file
&& !strlen(urls
->file
)) {
275 nfs_destroy_url(urls
);
276 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
281 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
283 flagsp
= strchr(strp
, '&');
287 strp2
= strchr(strp
, '=');
291 nfs_set_context_args(nfs
, strp
, strp2
);
295 if (urls
->server
&& strlen(urls
->server
) <= 1) {
303 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
305 return nfs_parse_url(nfs
, url
, 0, 0);
308 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
310 return nfs_parse_url(nfs
, url
, 1, 0);
313 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
315 return nfs_parse_url(nfs
, url
, 0, 1);
319 void nfs_destroy_url(struct nfs_url
*url
)
329 struct nfs_context
*nfs_init_context(void)
331 struct nfs_context
*nfs
;
333 nfs
= malloc(sizeof(struct nfs_context
));
337 nfs
->rpc
= rpc_init_context();
338 if (nfs
->rpc
== NULL
) {
346 nfs
->rootfh
.data
.data_len
= 0;
347 nfs
->rootfh
.data
.data_val
= NULL
;
352 void nfs_destroy_context(struct nfs_context
*nfs
)
354 rpc_destroy_context(nfs
->rpc
);
367 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
368 free(nfs
->rootfh
.data
.data_val
);
369 nfs
->rootfh
.data
.data_val
= NULL
;
384 void free_rpc_cb_data(struct rpc_cb_data
*data
)
391 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
393 struct rpc_cb_data
*data
= private_data
;
395 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
397 /* Dont want any more callbacks even if the socket is closed */
398 rpc
->connect_cb
= NULL
;
400 if (status
== RPC_STATUS_ERROR
) {
401 data
->cb(rpc
, status
, command_data
, data
->private_data
);
402 free_rpc_cb_data(data
);
405 if (status
== RPC_STATUS_CANCEL
) {
406 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
407 free_rpc_cb_data(data
);
411 data
->cb(rpc
, status
, NULL
, data
->private_data
);
412 free_rpc_cb_data(data
);
415 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
417 struct rpc_cb_data
*data
= private_data
;
420 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
422 if (status
== RPC_STATUS_ERROR
) {
423 data
->cb(rpc
, status
, command_data
, data
->private_data
);
424 free_rpc_cb_data(data
);
427 if (status
== RPC_STATUS_CANCEL
) {
428 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
429 free_rpc_cb_data(data
);
433 rpc_port
= *(uint32_t *)command_data
;
435 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
436 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
437 free_rpc_cb_data(data
);
441 rpc_disconnect(rpc
, "normal disconnect");
442 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
443 data
->cb(rpc
, status
, command_data
, data
->private_data
);
444 free_rpc_cb_data(data
);
449 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
451 struct rpc_cb_data
*data
= private_data
;
453 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
455 if (status
== RPC_STATUS_ERROR
) {
456 data
->cb(rpc
, status
, command_data
, data
->private_data
);
457 free_rpc_cb_data(data
);
460 if (status
== RPC_STATUS_CANCEL
) {
461 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
462 free_rpc_cb_data(data
);
466 if (rpc_pmap_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
467 data
->cb(rpc
, status
, command_data
, data
->private_data
);
468 free_rpc_cb_data(data
);
473 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
475 struct rpc_cb_data
*data
= private_data
;
477 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
479 /* Dont want any more callbacks even if the socket is closed */
480 rpc
->connect_cb
= NULL
;
482 if (status
== RPC_STATUS_ERROR
) {
483 data
->cb(rpc
, status
, command_data
, data
->private_data
);
484 free_rpc_cb_data(data
);
487 if (status
== RPC_STATUS_CANCEL
) {
488 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
489 free_rpc_cb_data(data
);
493 if (rpc_pmap_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
494 data
->cb(rpc
, status
, command_data
, data
->private_data
);
495 free_rpc_cb_data(data
);
500 int rpc_connect_program_async(struct rpc_context
*rpc
, char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
502 struct rpc_cb_data
*data
;
504 data
= malloc(sizeof(struct rpc_cb_data
));
508 memset(data
, 0, sizeof(struct rpc_cb_data
));
509 data
->server
= strdup(server
);
510 data
->program
= program
;
511 data
->version
= version
;
514 data
->private_data
= private_data
;
516 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
517 rpc_set_error(rpc
, "Failed to start connection");
518 free_rpc_cb_data(data
);
524 void free_nfs_cb_data(struct nfs_cb_data
*data
)
526 if (data
->saved_path
!= NULL
) {
527 free(data
->saved_path
);
528 data
->saved_path
= NULL
;
531 if (data
->continue_data
!= NULL
) {
532 data
->free_continue_data(data
->continue_data
);
533 data
->continue_data
= NULL
;
536 if (data
->fh
.data
.data_val
!= NULL
) {
537 free(data
->fh
.data
.data_val
);
538 data
->fh
.data
.data_val
= NULL
;
541 if (data
->buffer
!= NULL
) {
553 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
555 struct nfs_cb_data
*data
= private_data
;
556 struct nfs_context
*nfs
= data
->nfs
;
558 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
560 if (status
== RPC_STATUS_ERROR
) {
561 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
562 free_nfs_cb_data(data
);
565 if (status
== RPC_STATUS_CANCEL
) {
566 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
567 free_nfs_cb_data(data
);
571 data
->cb(0, nfs
, NULL
, data
->private_data
);
572 free_nfs_cb_data(data
);
575 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
577 struct nfs_cb_data
*data
= private_data
;
578 struct nfs_context
*nfs
= data
->nfs
;
579 FSINFO3res
*res
= command_data
;
580 struct GETATTR3args args
;
582 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
584 if (status
== RPC_STATUS_ERROR
) {
585 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
586 free_nfs_cb_data(data
);
589 if (status
== RPC_STATUS_CANCEL
) {
590 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
591 free_nfs_cb_data(data
);
595 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
596 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
598 memset(&args
, 0, sizeof(GETATTR3args
));
599 args
.object
= nfs
->rootfh
;
601 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
602 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
603 free_nfs_cb_data(data
);
608 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
610 struct nfs_cb_data
*data
= private_data
;
611 struct nfs_context
*nfs
= data
->nfs
;
612 struct FSINFO3args args
;
614 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
616 if (status
== RPC_STATUS_ERROR
) {
617 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
618 free_nfs_cb_data(data
);
621 if (status
== RPC_STATUS_CANCEL
) {
622 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
623 free_nfs_cb_data(data
);
627 args
.fsroot
= nfs
->rootfh
;
628 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
629 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
630 free_nfs_cb_data(data
);
636 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
638 struct nfs_cb_data
*data
= private_data
;
639 struct nfs_context
*nfs
= data
->nfs
;
641 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
643 /* Dont want any more callbacks even if the socket is closed */
644 rpc
->connect_cb
= NULL
;
646 if (status
== RPC_STATUS_ERROR
) {
647 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
648 free_nfs_cb_data(data
);
651 if (status
== RPC_STATUS_CANCEL
) {
652 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
653 free_nfs_cb_data(data
);
657 if (rpc_nfs3_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
658 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
659 free_nfs_cb_data(data
);
665 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
667 struct nfs_cb_data
*data
= private_data
;
668 struct nfs_context
*nfs
= data
->nfs
;
671 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
673 if (status
== RPC_STATUS_ERROR
) {
674 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
675 free_nfs_cb_data(data
);
678 if (status
== RPC_STATUS_CANCEL
) {
679 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
680 free_nfs_cb_data(data
);
685 if (res
->fhs_status
!= MNT3_OK
) {
686 rpc_set_error(rpc
, "RPC error: Mount failed with error %s(%d) %s(%d)", mountstat3_to_str(res
->fhs_status
), res
->fhs_status
, strerror(-mountstat3_to_errno(res
->fhs_status
)), -mountstat3_to_errno(res
->fhs_status
));
687 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
688 free_nfs_cb_data(data
);
692 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
693 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
694 if (nfs
->rootfh
.data
.data_val
== NULL
) {
695 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
696 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
697 free_nfs_cb_data(data
);
700 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
702 rpc_disconnect(rpc
, "normal disconnect");
703 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
704 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
705 free_nfs_cb_data(data
);
708 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
709 rpc_set_autoreconnect(rpc
);
713 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
715 struct nfs_cb_data
*data
= private_data
;
716 struct nfs_context
*nfs
= data
->nfs
;
718 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
720 if (status
== RPC_STATUS_ERROR
) {
721 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
722 free_nfs_cb_data(data
);
725 if (status
== RPC_STATUS_CANCEL
) {
726 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
727 free_nfs_cb_data(data
);
731 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
732 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
733 free_nfs_cb_data(data
);
738 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
740 struct nfs_cb_data
*data
= private_data
;
741 struct nfs_context
*nfs
= data
->nfs
;
743 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
745 /* Dont want any more callbacks even if the socket is closed */
746 rpc
->connect_cb
= NULL
;
748 if (status
== RPC_STATUS_ERROR
) {
749 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
750 free_nfs_cb_data(data
);
753 if (status
== RPC_STATUS_CANCEL
) {
754 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
755 free_nfs_cb_data(data
);
759 if (rpc_mount3_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
760 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
761 free_nfs_cb_data(data
);
766 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
768 struct nfs_cb_data
*data
= private_data
;
769 struct nfs_context
*nfs
= data
->nfs
;
772 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
774 if (status
== RPC_STATUS_ERROR
) {
775 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
776 free_nfs_cb_data(data
);
779 if (status
== RPC_STATUS_CANCEL
) {
780 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
781 free_nfs_cb_data(data
);
785 mount_port
= *(uint32_t *)command_data
;
786 if (mount_port
== 0) {
787 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
788 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
789 free_nfs_cb_data(data
);
793 rpc_disconnect(rpc
, "normal disconnect");
794 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
795 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
796 free_nfs_cb_data(data
);
802 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
804 struct nfs_cb_data
*data
= private_data
;
805 struct nfs_context
*nfs
= data
->nfs
;
807 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
809 if (status
== RPC_STATUS_ERROR
) {
810 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
811 free_nfs_cb_data(data
);
814 if (status
== RPC_STATUS_CANCEL
) {
815 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
816 free_nfs_cb_data(data
);
820 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
821 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
822 free_nfs_cb_data(data
);
827 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
829 struct nfs_cb_data
*data
= private_data
;
830 struct nfs_context
*nfs
= data
->nfs
;
832 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
834 /* Dont want any more callbacks even if the socket is closed */
835 rpc
->connect_cb
= NULL
;
837 if (status
== RPC_STATUS_ERROR
) {
838 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
839 free_nfs_cb_data(data
);
842 if (status
== RPC_STATUS_CANCEL
) {
843 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
844 free_nfs_cb_data(data
);
848 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
849 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
850 free_nfs_cb_data(data
);
856 * Async call for mounting an nfs share and geting the root filehandle
858 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
860 struct nfs_cb_data
*data
;
861 char *new_server
, *new_export
;
863 data
= malloc(sizeof(struct nfs_cb_data
));
865 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
868 memset(data
, 0, sizeof(struct nfs_cb_data
));
869 new_server
= strdup(server
);
870 new_export
= strdup(export
);
871 if (nfs
->server
!= NULL
) {
874 nfs
->server
= new_server
;
875 if (nfs
->export
!= NULL
) {
878 nfs
->export
= new_export
;
881 data
->private_data
= private_data
;
883 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
884 rpc_set_error(nfs
->rpc
, "Failed to start connection");
885 free_nfs_cb_data(data
);
895 * Functions to first look up a path, component by component, and then finally call a specific function once
896 * the filehandle for the final component is found.
898 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
900 struct nfs_cb_data
*data
= private_data
;
901 struct nfs_context
*nfs
= data
->nfs
;
904 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
906 if (status
== RPC_STATUS_ERROR
) {
907 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
908 free_nfs_cb_data(data
);
911 if (status
== RPC_STATUS_CANCEL
) {
912 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
913 free_nfs_cb_data(data
);
918 if (res
->status
!= NFS3_OK
) {
919 rpc_set_error(nfs
->rpc
, "NFS: Lookup of %s failed with %s(%d)", data
->saved_path
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
920 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
921 free_nfs_cb_data(data
);
925 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
926 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
927 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
928 free_nfs_cb_data(data
);
933 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
938 while (*data
->path
== '/') {
943 str
= strchr(path
, '/');
948 while (*data
->path
!= 0) {
954 data
->fh
.data
.data_len
= fh
->data
.data_len
;
955 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
956 if (data
->fh
.data
.data_val
== NULL
) {
957 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
958 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
959 free_nfs_cb_data(data
);
962 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
963 data
->continue_cb(nfs
, data
);
968 memset(&args
, 0, sizeof(LOOKUP3args
));
970 args
.what
.name
= path
;
972 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
973 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
974 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
975 free_nfs_cb_data(data
);
981 static int nfs_lookuppath_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
, continue_func continue_cb
, void *continue_data
, void (*free_continue_data
)(void *), int continue_int
)
983 struct nfs_cb_data
*data
;
985 if (path
[0] != 0 && path
[0] != '/') {
986 rpc_set_error(nfs
->rpc
, "Pathname is not absolute %s", path
);
990 data
= malloc(sizeof(struct nfs_cb_data
));
992 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
995 memset(data
, 0, sizeof(struct nfs_cb_data
));
998 data
->continue_cb
= continue_cb
;
999 data
->continue_data
= continue_data
;
1000 data
->free_continue_data
= free_continue_data
;
1001 data
->continue_int
= continue_int
;
1002 data
->private_data
= private_data
;
1003 data
->saved_path
= strdup(path
);
1004 if (data
->saved_path
== NULL
) {
1005 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1006 free_nfs_cb_data(data
);
1009 data
->path
= data
->saved_path
;
1011 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1012 /* return 0 here since the callback will be invoked if there is a failure */
1025 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1028 struct nfs_cb_data
*data
= private_data
;
1029 struct nfs_context
*nfs
= data
->nfs
;
1032 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1034 if (status
== RPC_STATUS_ERROR
) {
1035 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1036 free_nfs_cb_data(data
);
1039 if (status
== RPC_STATUS_CANCEL
) {
1040 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1041 free_nfs_cb_data(data
);
1046 if (res
->status
!= NFS3_OK
) {
1047 rpc_set_error(nfs
->rpc
, "NFS: GETATTR of %s failed with %s(%d)", data
->saved_path
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1048 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1049 free_nfs_cb_data(data
);
1054 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1055 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1056 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1057 st
.st_mode
|= S_IFDIR
;
1059 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1060 st
.st_mode
|= S_IFREG
;
1062 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1063 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1064 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1066 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1068 st
.st_blksize
= 4096;
1069 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1071 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1072 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1073 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1075 data
->cb(0, nfs
, &st
, data
->private_data
);
1076 free_nfs_cb_data(data
);
1079 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1081 struct GETATTR3args args
;
1083 memset(&args
, 0, sizeof(GETATTR3args
));
1084 args
.object
= data
->fh
;
1086 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1087 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1088 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1089 free_nfs_cb_data(data
);
1095 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1097 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1098 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1112 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1115 struct nfs_cb_data
*data
= private_data
;
1116 struct nfs_context
*nfs
= data
->nfs
;
1117 struct nfsfh
*nfsfh
;
1118 unsigned int nfsmode
= 0;
1120 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1122 if (status
== RPC_STATUS_ERROR
) {
1123 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1124 free_nfs_cb_data(data
);
1127 if (status
== RPC_STATUS_CANCEL
) {
1128 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1129 free_nfs_cb_data(data
);
1134 if (res
->status
!= NFS3_OK
) {
1135 rpc_set_error(nfs
->rpc
, "NFS: ACCESS of %s failed with %s(%d)", data
->saved_path
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1136 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1137 free_nfs_cb_data(data
);
1141 if (data
->continue_int
& O_WRONLY
) {
1142 nfsmode
|= ACCESS3_MODIFY
;
1144 if (data
->continue_int
& O_RDWR
) {
1145 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1147 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1148 nfsmode
|= ACCESS3_READ
;
1152 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1153 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1154 nfsmode
&ACCESS3_READ
?'r':'-',
1155 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1156 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1157 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1158 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1159 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1160 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1161 free_nfs_cb_data(data
);
1165 nfsfh
= malloc(sizeof(struct nfsfh
));
1166 if (nfsfh
== NULL
) {
1167 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1168 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1169 free_nfs_cb_data(data
);
1172 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1174 if (data
->continue_int
& O_SYNC
) {
1178 /* steal the filehandle */
1179 nfsfh
->fh
= data
->fh
;
1180 data
->fh
.data
.data_val
= NULL
;
1182 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1183 free_nfs_cb_data(data
);
1186 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1191 if (data
->continue_int
& O_WRONLY
) {
1192 nfsmode
|= ACCESS3_MODIFY
;
1194 if (data
->continue_int
& O_RDWR
) {
1195 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1197 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1198 nfsmode
|= ACCESS3_READ
;
1201 memset(&args
, 0, sizeof(ACCESS3args
));
1202 args
.object
= data
->fh
;
1203 args
.access
= nfsmode
;
1205 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1206 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
1207 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1208 free_nfs_cb_data(data
);
1214 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1216 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
1217 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1231 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1233 struct nfs_cb_data
*data
= private_data
;
1234 struct nfs_context
*nfs
= data
->nfs
;
1237 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1239 if (status
== RPC_STATUS_ERROR
) {
1240 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1241 free_nfs_cb_data(data
);
1244 if (status
== RPC_STATUS_CANCEL
) {
1245 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1246 free_nfs_cb_data(data
);
1251 if (res
->status
!= NFS3_OK
) {
1252 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1253 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1254 free_nfs_cb_data(data
);
1258 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1259 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1260 free_nfs_cb_data(data
);
1263 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1265 struct nfs_mcb_data
*mdata
= private_data
;
1266 struct nfs_cb_data
*data
= mdata
->data
;
1267 struct nfs_context
*nfs
= data
->nfs
;
1270 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1274 if (status
== RPC_STATUS_ERROR
) {
1275 /* flag the failure but do not invoke callback until we have received all responses */
1278 if (status
== RPC_STATUS_CANCEL
) {
1279 /* flag the cancellation but do not invoke callback until we have received all responses */
1283 /* reassemble the data into the buffer */
1284 if (status
== RPC_STATUS_SUCCESS
) {
1286 if (res
->status
!= NFS3_OK
) {
1287 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1290 if (res
->READ3res_u
.resok
.count
> 0) {
1291 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1292 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1293 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1299 if (data
->num_calls
> 0) {
1300 /* still waiting for more replies */
1305 if (data
->error
!= 0) {
1306 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1307 free_nfs_cb_data(data
);
1311 if (data
->cancel
!= 0) {
1312 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1313 free_nfs_cb_data(data
);
1318 data
->nfsfh
->offset
= data
->max_offset
;
1319 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1321 free_nfs_cb_data(data
);
1325 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1327 struct nfs_cb_data
*data
;
1329 data
= malloc(sizeof(struct nfs_cb_data
));
1331 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1334 memset(data
, 0, sizeof(struct nfs_cb_data
));
1337 data
->private_data
= private_data
;
1338 data
->nfsfh
= nfsfh
;
1340 nfsfh
->offset
= offset
;
1342 if (count
<= nfs_get_readmax(nfs
)) {
1345 memset(&args
, 0, sizeof(READ3args
));
1346 args
.file
= nfsfh
->fh
;
1347 args
.offset
= offset
;
1350 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_cb
, &args
, data
) != 0) {
1351 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1352 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1353 free_nfs_cb_data(data
);
1359 /* trying to read more than maximum server read size, we has to chop it up into smaller
1360 * reads and collect into a reassembly buffer.
1361 * we send all reads in parallel so that performance is still good.
1363 data
->max_offset
= offset
;
1364 data
->start_offset
= offset
;
1366 data
->buffer
= malloc(count
);
1367 if (data
->buffer
== NULL
) {
1368 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1369 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1370 free_nfs_cb_data(data
);
1375 uint64_t readcount
= count
;
1376 struct nfs_mcb_data
*mdata
;
1379 if (readcount
> nfs_get_readmax(nfs
)) {
1380 readcount
= nfs_get_readmax(nfs
);
1383 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1384 if (mdata
== NULL
) {
1385 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1388 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1390 mdata
->offset
= offset
;
1391 mdata
->count
= readcount
;
1393 memset(&args
, 0, sizeof(READ3args
));
1394 args
.file
= nfsfh
->fh
;
1395 args
.offset
= offset
;
1396 args
.count
= readcount
;
1398 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1399 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1400 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1406 offset
+= readcount
;
1416 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1418 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1426 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1428 struct nfs_cb_data
*data
= private_data
;
1429 struct nfs_context
*nfs
= data
->nfs
;
1432 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1434 if (status
== RPC_STATUS_ERROR
) {
1435 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1436 free_nfs_cb_data(data
);
1439 if (status
== RPC_STATUS_CANCEL
) {
1440 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1441 free_nfs_cb_data(data
);
1446 if (res
->status
!= NFS3_OK
) {
1447 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1448 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1449 free_nfs_cb_data(data
);
1453 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1454 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1455 free_nfs_cb_data(data
);
1458 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1460 struct nfs_mcb_data
*mdata
= private_data
;
1461 struct nfs_cb_data
*data
= mdata
->data
;
1462 struct nfs_context
*nfs
= data
->nfs
;
1465 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1469 if (status
== RPC_STATUS_ERROR
) {
1470 /* flag the failure but do not invoke callback until we have received all responses */
1473 if (status
== RPC_STATUS_CANCEL
) {
1474 /* flag the cancellation but do not invoke callback until we have received all responses */
1478 if (status
== RPC_STATUS_SUCCESS
) {
1480 if (res
->status
!= NFS3_OK
) {
1481 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1484 if (res
->WRITE3res_u
.resok
.count
> 0) {
1485 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1486 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1492 if (data
->num_calls
> 0) {
1493 /* still waiting for more replies */
1498 if (data
->error
!= 0) {
1499 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1500 free_nfs_cb_data(data
);
1504 if (data
->cancel
!= 0) {
1505 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1506 free_nfs_cb_data(data
);
1511 data
->nfsfh
->offset
= data
->max_offset
;
1512 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1514 free_nfs_cb_data(data
);
1519 int nfs_pwrite_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1521 struct nfs_cb_data
*data
;
1523 data
= malloc(sizeof(struct nfs_cb_data
));
1525 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1528 memset(data
, 0, sizeof(struct nfs_cb_data
));
1531 data
->private_data
= private_data
;
1532 data
->nfsfh
= nfsfh
;
1534 nfsfh
->offset
= offset
;
1536 if (count
<= nfs_get_writemax(nfs
)) {
1539 memset(&args
, 0, sizeof(WRITE3args
));
1540 args
.file
= nfsfh
->fh
;
1541 args
.offset
= offset
;
1543 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1544 args
.data
.data_len
= count
;
1545 args
.data
.data_val
= buf
;
1547 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_cb
, &args
, data
) != 0) {
1548 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1549 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1550 free_nfs_cb_data(data
);
1556 /* trying to write more than maximum server write size, we has to chop it up into smaller
1558 * we send all writes in parallel so that performance is still good.
1560 data
->max_offset
= offset
;
1561 data
->start_offset
= offset
;
1564 uint64_t writecount
= count
;
1565 struct nfs_mcb_data
*mdata
;
1568 if (writecount
> nfs_get_writemax(nfs
)) {
1569 writecount
= nfs_get_writemax(nfs
);
1572 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1573 if (mdata
== NULL
) {
1574 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1577 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1579 mdata
->offset
= offset
;
1580 mdata
->count
= writecount
;
1582 memset(&args
, 0, sizeof(WRITE3args
));
1583 args
.file
= nfsfh
->fh
;
1584 args
.offset
= offset
;
1585 args
.count
= writecount
;
1586 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1587 args
.data
.data_len
= writecount
;
1588 args
.data
.data_val
= &buf
[offset
- data
->start_offset
];
1590 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1591 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1592 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1597 count
-= writecount
;
1598 offset
+= writecount
;
1608 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1610 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1620 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1622 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1623 free(nfsfh
->fh
.data
.data_val
);
1624 nfsfh
->fh
.data
.data_val
= NULL
;
1628 cb(0, nfs
, NULL
, private_data
);
1639 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1641 struct nfs_cb_data
*data
;
1642 struct GETATTR3args args
;
1644 data
= malloc(sizeof(struct nfs_cb_data
));
1646 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1649 memset(data
, 0, sizeof(struct nfs_cb_data
));
1652 data
->private_data
= private_data
;
1654 memset(&args
, 0, sizeof(GETATTR3args
));
1655 args
.object
= nfsfh
->fh
;
1657 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1658 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1659 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1660 free_nfs_cb_data(data
);
1671 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1673 struct nfs_cb_data
*data
= private_data
;
1674 struct nfs_context
*nfs
= data
->nfs
;
1677 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1679 if (status
== RPC_STATUS_ERROR
) {
1680 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1681 free_nfs_cb_data(data
);
1684 if (status
== RPC_STATUS_CANCEL
) {
1685 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1686 free_nfs_cb_data(data
);
1691 if (res
->status
!= NFS3_OK
) {
1692 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1693 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1694 free_nfs_cb_data(data
);
1698 data
->cb(0, nfs
, NULL
, data
->private_data
);
1699 free_nfs_cb_data(data
);
1702 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1704 struct nfs_cb_data
*data
;
1705 struct COMMIT3args args
;
1707 data
= malloc(sizeof(struct nfs_cb_data
));
1709 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1712 memset(data
, 0, sizeof(struct nfs_cb_data
));
1715 data
->private_data
= private_data
;
1717 args
.file
= nfsfh
->fh
;
1720 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1721 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1722 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1723 free_nfs_cb_data(data
);
1735 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1737 struct nfs_cb_data
*data
= private_data
;
1738 struct nfs_context
*nfs
= data
->nfs
;
1741 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1743 if (status
== RPC_STATUS_ERROR
) {
1744 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1745 free_nfs_cb_data(data
);
1748 if (status
== RPC_STATUS_CANCEL
) {
1749 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1750 free_nfs_cb_data(data
);
1755 if (res
->status
!= NFS3_OK
) {
1756 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1757 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1758 free_nfs_cb_data(data
);
1762 data
->cb(0, nfs
, NULL
, data
->private_data
);
1763 free_nfs_cb_data(data
);
1766 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1768 struct nfs_cb_data
*data
;
1771 data
= malloc(sizeof(struct nfs_cb_data
));
1773 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1776 memset(data
, 0, sizeof(struct nfs_cb_data
));
1779 data
->private_data
= private_data
;
1781 memset(&args
, 0, sizeof(SETATTR3args
));
1782 args
.object
= nfsfh
->fh
;
1783 args
.new_attributes
.size
.set_it
= 1;
1784 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1786 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1787 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1788 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1789 free_nfs_cb_data(data
);
1799 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1801 uint64_t offset
= data
->continue_int
;
1804 nfsfh
.fh
= data
->fh
;
1806 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1807 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1808 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1809 free_nfs_cb_data(data
);
1812 free_nfs_cb_data(data
);
1816 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1822 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1823 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1836 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1839 struct nfs_cb_data
*data
= private_data
;
1840 struct nfs_context
*nfs
= data
->nfs
;
1841 char *str
= data
->continue_data
;
1843 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1845 str
= &str
[strlen(str
) + 1];
1847 if (status
== RPC_STATUS_ERROR
) {
1848 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1849 free_nfs_cb_data(data
);
1852 if (status
== RPC_STATUS_CANCEL
) {
1853 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1854 free_nfs_cb_data(data
);
1859 if (res
->status
!= NFS3_OK
) {
1860 rpc_set_error(nfs
->rpc
, "NFS: MKDIR of %s/%s failed with %s(%d)", data
->saved_path
, str
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1861 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1862 free_nfs_cb_data(data
);
1866 data
->cb(0, nfs
, NULL
, data
->private_data
);
1867 free_nfs_cb_data(data
);
1870 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1872 char *str
= data
->continue_data
;
1875 str
= &str
[strlen(str
) + 1];
1877 memset(&args
, 0, sizeof(MKDIR3args
));
1878 args
.where
.dir
= data
->fh
;
1879 args
.where
.name
= str
;
1880 args
.attributes
.mode
.set_it
= 1;
1881 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
1883 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
1884 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1885 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1886 free_nfs_cb_data(data
);
1892 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1897 new_path
= strdup(path
);
1898 if (new_path
== NULL
) {
1899 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1903 ptr
= strrchr(new_path
, '/');
1905 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1910 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1911 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1912 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1926 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1929 struct nfs_cb_data
*data
= private_data
;
1930 struct nfs_context
*nfs
= data
->nfs
;
1931 char *str
= data
->continue_data
;
1933 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1935 str
= &str
[strlen(str
) + 1];
1937 if (status
== RPC_STATUS_ERROR
) {
1938 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1939 free_nfs_cb_data(data
);
1942 if (status
== RPC_STATUS_CANCEL
) {
1943 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1944 free_nfs_cb_data(data
);
1949 if (res
->status
!= NFS3_OK
) {
1950 rpc_set_error(nfs
->rpc
, "NFS: RMDIR of %s/%s failed with %s(%d)", data
->saved_path
, str
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1951 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1952 free_nfs_cb_data(data
);
1956 data
->cb(0, nfs
, NULL
, data
->private_data
);
1957 free_nfs_cb_data(data
);
1960 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1962 char *str
= data
->continue_data
;
1965 str
= &str
[strlen(str
) + 1];
1967 args
.object
.dir
= data
->fh
;
1968 args
.object
.name
= str
;
1969 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
1970 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1971 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1972 free_nfs_cb_data(data
);
1978 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1983 new_path
= strdup(path
);
1984 if (new_path
== NULL
) {
1985 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1989 ptr
= strrchr(new_path
, '/');
1991 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1996 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1997 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1998 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2011 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2014 struct nfs_cb_data
*data
= private_data
;
2015 struct nfs_context
*nfs
= data
->nfs
;
2016 struct nfsfh
*nfsfh
;
2017 char *str
= data
->continue_data
;
2019 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2021 if (status
== RPC_STATUS_ERROR
) {
2022 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2023 free_nfs_cb_data(data
);
2026 if (status
== RPC_STATUS_CANCEL
) {
2027 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2028 free_nfs_cb_data(data
);
2032 str
= &str
[strlen(str
) + 1];
2034 if (res
->status
!= NFS3_OK
) {
2035 rpc_set_error(nfs
->rpc
, "NFS: CREATE of %s/%s failed with %s(%d)", data
->saved_path
, str
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2036 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2041 nfsfh
= malloc(sizeof(struct nfsfh
));
2042 if (nfsfh
== NULL
) {
2043 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2044 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2045 free_nfs_cb_data(data
);
2048 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2050 /* copy the filehandle */
2051 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2052 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2053 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2055 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2056 free_nfs_cb_data(data
);
2061 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2064 struct nfs_cb_data
*data
= private_data
;
2065 struct nfs_context
*nfs
= data
->nfs
;
2066 char *str
= data
->continue_data
;
2069 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2071 if (status
== RPC_STATUS_ERROR
) {
2072 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2073 free_nfs_cb_data(data
);
2076 if (status
== RPC_STATUS_CANCEL
) {
2077 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2078 free_nfs_cb_data(data
);
2082 str
= &str
[strlen(str
) + 1];
2084 if (res
->status
!= NFS3_OK
) {
2085 rpc_set_error(nfs
->rpc
, "NFS: CREATE of %s/%s failed with %s(%d)", data
->saved_path
, str
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2086 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2087 free_nfs_cb_data(data
);
2091 memset(&args
, 0, sizeof(LOOKUP3args
));
2092 args
.what
.dir
= data
->fh
;
2093 args
.what
.name
= str
;
2095 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2096 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2097 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2098 free_nfs_cb_data(data
);
2104 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2106 char *str
= data
->continue_data
;
2109 str
= &str
[strlen(str
) + 1];
2111 memset(&args
, 0, sizeof(CREATE3args
));
2112 args
.where
.dir
= data
->fh
;
2113 args
.where
.name
= str
;
2114 args
.how
.mode
= UNCHECKED
;
2115 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2116 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2118 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2119 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2120 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2121 free_nfs_cb_data(data
);
2127 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2132 new_path
= strdup(path
);
2133 if (new_path
== NULL
) {
2134 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2138 ptr
= strrchr(new_path
, '/');
2140 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2145 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2146 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2147 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2160 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2163 struct nfs_cb_data
*data
= private_data
;
2164 struct nfs_context
*nfs
= data
->nfs
;
2165 char *str
= data
->continue_data
;
2167 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2169 str
= &str
[strlen(str
) + 1];
2171 if (status
== RPC_STATUS_ERROR
) {
2172 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2173 free_nfs_cb_data(data
);
2176 if (status
== RPC_STATUS_CANCEL
) {
2177 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2178 free_nfs_cb_data(data
);
2183 if (res
->status
!= NFS3_OK
) {
2184 rpc_set_error(nfs
->rpc
, "NFS: REMOVE of %s/%s failed with %s(%d)", data
->saved_path
, str
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2185 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2186 free_nfs_cb_data(data
);
2190 data
->cb(0, nfs
, NULL
, data
->private_data
);
2191 free_nfs_cb_data(data
);
2194 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2196 char *str
= data
->continue_data
;
2197 struct REMOVE3args args
;
2199 str
= &str
[strlen(str
) + 1];
2201 args
.object
.dir
= data
->fh
;
2202 args
.object
.name
= str
;
2203 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2204 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2205 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2206 free_nfs_cb_data(data
);
2212 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2217 new_path
= strdup(path
);
2218 if (new_path
== NULL
) {
2219 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2223 ptr
= strrchr(new_path
, '/');
2225 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2230 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2231 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2232 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2243 struct mknod_cb_data
{
2250 static void free_mknod_cb_data(void *ptr
)
2252 struct mknod_cb_data
*data
= ptr
;
2258 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2261 struct nfs_cb_data
*data
= private_data
;
2262 struct nfs_context
*nfs
= data
->nfs
;
2263 char *str
= data
->continue_data
;
2265 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2267 str
= &str
[strlen(str
) + 1];
2269 if (status
== RPC_STATUS_ERROR
) {
2270 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2271 free_nfs_cb_data(data
);
2274 if (status
== RPC_STATUS_CANCEL
) {
2275 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2276 free_nfs_cb_data(data
);
2281 if (res
->status
!= NFS3_OK
) {
2282 rpc_set_error(nfs
->rpc
, "NFS: MKNOD of %s/%s failed with %s(%d)", data
->saved_path
, str
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2283 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2284 free_nfs_cb_data(data
);
2288 data
->cb(0, nfs
, NULL
, data
->private_data
);
2289 free_nfs_cb_data(data
);
2292 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2294 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2295 char *str
= cb_data
->path
;
2298 str
= &str
[strlen(str
) + 1];
2300 args
.where
.dir
= data
->fh
;
2301 args
.where
.name
= str
;
2302 switch (cb_data
->mode
& S_IFMT
) {
2304 args
.what
.type
= NF3CHR
;
2305 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2306 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
& (S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
);
2307 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2308 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2311 args
.what
.type
= NF3BLK
;
2312 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2313 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
& (S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
);
2314 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2315 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2317 args
.what
.type
= NF3SOCK
;
2318 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2319 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
& (S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
);
2322 args
.what
.type
= NF3FIFO
;
2323 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2324 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
& (S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
);
2327 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2328 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2329 free_nfs_cb_data(data
);
2333 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2334 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2335 free_nfs_cb_data(data
);
2341 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2344 struct mknod_cb_data
*cb_data
;
2346 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2347 if (cb_data
== NULL
) {
2348 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2352 cb_data
->path
= strdup(path
);
2353 if (cb_data
->path
== NULL
) {
2354 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2359 ptr
= strrchr(cb_data
->path
, '/');
2361 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2366 cb_data
->mode
= mode
;
2367 cb_data
->major
= major(dev
);
2368 cb_data
->minor
= minor(dev
);
2370 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2371 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2372 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2373 free_mknod_cb_data(cb_data
);
2384 /* ReadDirPlus Emulation Callback data */
2385 struct rdpe_cb_data
{
2388 struct nfs_cb_data
*data
;
2391 /* ReadDirPlus Emulation LOOKUP Callback data */
2392 struct rdpe_lookup_cb_data
{
2393 struct rdpe_cb_data
*rdpe_cb_data
;
2394 struct nfsdirent
*nfsdirent
;
2397 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2398 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2400 LOOKUP3res
*res
= command_data
;
2401 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2402 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2403 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2404 struct nfsdir
*nfsdir
= data
->continue_data
;
2405 struct nfs_context
*nfs
= data
->nfs
;
2406 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2408 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2410 free(rdpe_lookup_cb_data
);
2412 rdpe_cb_data
->getattrcount
--;
2414 if (status
== RPC_STATUS_ERROR
) {
2415 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2417 if (status
== RPC_STATUS_CANCEL
) {
2418 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2420 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2421 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2423 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2424 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2425 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2427 nfsdirent
->type
= attributes
->type
;
2428 nfsdirent
->mode
= attributes
->mode
;
2429 nfsdirent
->size
= attributes
->size
;
2431 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2432 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2433 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2434 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2435 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2436 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2440 if (rdpe_cb_data
->getattrcount
== 0) {
2441 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2442 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2443 nfs_free_nfsdir(nfsdir
);
2445 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2449 data
->continue_data
= NULL
;
2450 free_nfs_cb_data(data
);
2454 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2456 READDIR3res
*res
= command_data
;
2457 struct nfs_cb_data
*data
= private_data
;
2458 struct nfs_context
*nfs
= data
->nfs
;
2459 struct nfsdir
*nfsdir
= data
->continue_data
;
2460 struct nfsdirent
*nfsdirent
;
2461 struct entry3
*entry
;
2463 struct rdpe_cb_data
*rdpe_cb_data
;
2465 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2467 if (status
== RPC_STATUS_ERROR
) {
2468 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2469 nfs_free_nfsdir(nfsdir
);
2470 data
->continue_data
= NULL
;
2471 free_nfs_cb_data(data
);
2475 if (status
== RPC_STATUS_CANCEL
) {
2476 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2477 nfs_free_nfsdir(nfsdir
);
2478 data
->continue_data
= NULL
;
2479 free_nfs_cb_data(data
);
2483 if (res
->status
!= NFS3_OK
) {
2484 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
));
2485 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2486 nfs_free_nfsdir(nfsdir
);
2487 data
->continue_data
= NULL
;
2488 free_nfs_cb_data(data
);
2492 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2493 while (entry
!= NULL
) {
2494 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2495 if (nfsdirent
== NULL
) {
2496 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2497 nfs_free_nfsdir(nfsdir
);
2498 data
->continue_data
= NULL
;
2499 free_nfs_cb_data(data
);
2502 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2503 nfsdirent
->name
= strdup(entry
->name
);
2504 if (nfsdirent
->name
== NULL
) {
2505 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2506 nfs_free_nfsdir(nfsdir
);
2507 data
->continue_data
= NULL
;
2508 free_nfs_cb_data(data
);
2511 nfsdirent
->inode
= entry
->fileid
;
2513 nfsdirent
->next
= nfsdir
->entries
;
2514 nfsdir
->entries
= nfsdirent
;
2516 cookie
= entry
->cookie
;
2517 entry
= entry
->nextentry
;
2520 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2523 args
.dir
= data
->fh
;
2524 args
.cookie
= cookie
;
2525 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2528 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2529 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2530 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2531 nfs_free_nfsdir(nfsdir
);
2532 data
->continue_data
= NULL
;
2533 free_nfs_cb_data(data
);
2539 /* steal the dirhandle */
2540 nfsdir
->current
= nfsdir
->entries
;
2542 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2543 rdpe_cb_data
->getattrcount
= 0;
2544 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2545 rdpe_cb_data
->data
= data
;
2546 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2547 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2550 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2551 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2552 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2554 memset(&args
, 0, sizeof(LOOKUP3args
));
2555 args
.what
.dir
= data
->fh
;
2556 args
.what
.name
= nfsdirent
->name
;
2558 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2559 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2561 /* if we have already commands in flight, we cant just stop, we have to wait for the
2562 * commands in flight to complete
2564 if (rdpe_cb_data
->getattrcount
> 0) {
2565 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2566 free(rdpe_lookup_cb_data
);
2570 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2571 nfs_free_nfsdir(nfsdir
);
2572 data
->continue_data
= NULL
;
2573 free_nfs_cb_data(data
);
2574 free(rdpe_lookup_cb_data
);
2578 rdpe_cb_data
->getattrcount
++;
2583 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2585 READDIRPLUS3res
*res
= command_data
;
2586 struct nfs_cb_data
*data
= private_data
;
2587 struct nfs_context
*nfs
= data
->nfs
;
2588 struct nfsdir
*nfsdir
= data
->continue_data
;
2589 struct entryplus3
*entry
;
2592 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2594 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2597 args
.dir
= data
->fh
;
2598 args
.cookie
= cookie
;
2599 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2602 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2603 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2604 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2605 nfs_free_nfsdir(nfsdir
);
2606 data
->continue_data
= NULL
;
2607 free_nfs_cb_data(data
);
2613 if (status
== RPC_STATUS_CANCEL
) {
2614 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2615 nfs_free_nfsdir(nfsdir
);
2616 data
->continue_data
= NULL
;
2617 free_nfs_cb_data(data
);
2621 if (res
->status
!= NFS3_OK
) {
2622 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
));
2623 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2624 nfs_free_nfsdir(nfsdir
);
2625 data
->continue_data
= NULL
;
2626 free_nfs_cb_data(data
);
2630 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2631 while (entry
!= NULL
) {
2632 struct nfsdirent
*nfsdirent
;
2634 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2635 if (nfsdirent
== NULL
) {
2636 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2637 nfs_free_nfsdir(nfsdir
);
2638 data
->continue_data
= NULL
;
2639 free_nfs_cb_data(data
);
2642 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2643 nfsdirent
->name
= strdup(entry
->name
);
2644 if (nfsdirent
->name
== NULL
) {
2645 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2646 nfs_free_nfsdir(nfsdir
);
2647 data
->continue_data
= NULL
;
2648 free_nfs_cb_data(data
);
2651 nfsdirent
->inode
= entry
->fileid
;
2652 if (entry
->name_attributes
.attributes_follow
) {
2653 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2654 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2655 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2657 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2658 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2659 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2660 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2661 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2662 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2665 nfsdirent
->next
= nfsdir
->entries
;
2666 nfsdir
->entries
= nfsdirent
;
2668 cookie
= entry
->cookie
;
2669 entry
= entry
->nextentry
;
2672 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2673 READDIRPLUS3args args
;
2675 args
.dir
= data
->fh
;
2676 args
.cookie
= cookie
;
2677 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2678 args
.dircount
= 8192;
2679 args
.maxcount
= 8192;
2681 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2682 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2683 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2684 nfs_free_nfsdir(nfsdir
);
2685 data
->continue_data
= NULL
;
2686 free_nfs_cb_data(data
);
2692 /* steal the dirhandle */
2693 data
->continue_data
= NULL
;
2694 nfsdir
->current
= nfsdir
->entries
;
2696 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2697 free_nfs_cb_data(data
);
2700 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2702 READDIRPLUS3args args
;
2704 args
.dir
= data
->fh
;
2706 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2707 args
.dircount
= 8192;
2708 args
.maxcount
= 8192;
2709 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2710 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2711 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2712 free_nfs_cb_data(data
);
2718 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2720 struct nfsdir
*nfsdir
;
2722 nfsdir
= malloc(sizeof(struct nfsdir
));
2723 if (nfsdir
== NULL
) {
2724 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2727 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2729 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2730 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2738 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2740 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2742 if (nfsdir
->current
!= NULL
) {
2743 nfsdir
->current
= nfsdir
->current
->next
;
2749 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2751 nfs_free_nfsdir(nfsdir
);
2763 struct lseek_cb_data
{
2764 struct nfs_context
*nfs
;
2765 struct nfsfh
*nfsfh
;
2771 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2774 struct lseek_cb_data
*data
= private_data
;
2775 struct nfs_context
*nfs
= data
->nfs
;
2777 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2779 if (status
== RPC_STATUS_ERROR
) {
2780 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2784 if (status
== RPC_STATUS_CANCEL
) {
2785 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2791 if (res
->status
!= NFS3_OK
) {
2792 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2793 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2798 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2799 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2803 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2805 struct lseek_cb_data
*data
;
2806 struct GETATTR3args args
;
2808 if (whence
== SEEK_SET
) {
2809 nfsfh
->offset
= offset
;
2810 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2813 if (whence
== SEEK_CUR
) {
2814 nfsfh
->offset
+= offset
;
2815 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2819 data
= malloc(sizeof(struct lseek_cb_data
));
2821 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2826 data
->nfsfh
= nfsfh
;
2827 data
->offset
= offset
;
2829 data
->private_data
= private_data
;
2831 memset(&args
, 0, sizeof(GETATTR3args
));
2832 args
.object
= nfsfh
->fh
;
2834 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
2835 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
2848 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2851 struct nfs_cb_data
*data
= private_data
;
2852 struct nfs_context
*nfs
= data
->nfs
;
2853 struct statvfs svfs
;
2855 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2857 if (status
== RPC_STATUS_ERROR
) {
2858 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2859 free_nfs_cb_data(data
);
2862 if (status
== RPC_STATUS_CANCEL
) {
2863 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2864 free_nfs_cb_data(data
);
2869 if (res
->status
!= NFS3_OK
) {
2870 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
));
2871 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2872 free_nfs_cb_data(data
);
2876 svfs
.f_bsize
= 4096;
2877 svfs
.f_frsize
= 4096;
2878 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
2879 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
2880 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
2881 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
2882 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
2883 #if !defined(ANDROID)
2884 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
2887 svfs
.f_namemax
= 256;
2890 data
->cb(0, nfs
, &svfs
, data
->private_data
);
2891 free_nfs_cb_data(data
);
2894 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2898 args
.fsroot
= data
->fh
;
2899 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
2900 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
2901 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2902 free_nfs_cb_data(data
);
2908 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2910 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
2911 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2924 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2927 struct nfs_cb_data
*data
= private_data
;
2928 struct nfs_context
*nfs
= data
->nfs
;
2930 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2932 if (status
== RPC_STATUS_ERROR
) {
2933 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2934 free_nfs_cb_data(data
);
2937 if (status
== RPC_STATUS_CANCEL
) {
2938 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2939 free_nfs_cb_data(data
);
2944 if (res
->status
!= NFS3_OK
) {
2945 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
));
2946 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2947 free_nfs_cb_data(data
);
2952 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
2953 free_nfs_cb_data(data
);
2956 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2960 args
.symlink
= data
->fh
;
2962 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
2963 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
2964 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2965 free_nfs_cb_data(data
);
2971 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2973 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
2974 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2987 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2989 struct nfs_cb_data
*data
= private_data
;
2990 struct nfs_context
*nfs
= data
->nfs
;
2993 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2995 if (status
== RPC_STATUS_ERROR
) {
2996 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2997 free_nfs_cb_data(data
);
3000 if (status
== RPC_STATUS_CANCEL
) {
3001 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3002 free_nfs_cb_data(data
);
3007 if (res
->status
!= NFS3_OK
) {
3008 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3009 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3010 free_nfs_cb_data(data
);
3014 data
->cb(0, nfs
, NULL
, data
->private_data
);
3015 free_nfs_cb_data(data
);
3018 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3022 memset(&args
, 0, sizeof(SETATTR3args
));
3023 args
.object
= data
->fh
;
3024 args
.new_attributes
.mode
.set_it
= 1;
3025 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3027 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3028 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3029 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3030 free_nfs_cb_data(data
);
3037 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3039 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3040 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3050 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3052 struct nfs_cb_data
*data
;
3054 data
= malloc(sizeof(struct nfs_cb_data
));
3056 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3059 memset(data
, 0, sizeof(struct nfs_cb_data
));
3062 data
->private_data
= private_data
;
3063 data
->continue_int
= mode
;
3064 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3065 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3066 if (data
->fh
.data
.data_val
== NULL
) {
3067 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3068 free_nfs_cb_data(data
);
3071 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3073 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3074 free_nfs_cb_data(data
);
3086 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3088 struct nfs_cb_data
*data
= private_data
;
3089 struct nfs_context
*nfs
= data
->nfs
;
3092 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3094 if (status
== RPC_STATUS_ERROR
) {
3095 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3096 free_nfs_cb_data(data
);
3099 if (status
== RPC_STATUS_CANCEL
) {
3100 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3101 free_nfs_cb_data(data
);
3106 if (res
->status
!= NFS3_OK
) {
3107 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3108 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3109 free_nfs_cb_data(data
);
3113 data
->cb(0, nfs
, NULL
, data
->private_data
);
3114 free_nfs_cb_data(data
);
3117 struct nfs_chown_data
{
3122 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3125 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3127 memset(&args
, 0, sizeof(SETATTR3args
));
3128 args
.object
= data
->fh
;
3129 if (chown_data
->uid
!= (uid_t
)-1) {
3130 args
.new_attributes
.uid
.set_it
= 1;
3131 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3133 if (chown_data
->gid
!= (gid_t
)-1) {
3134 args
.new_attributes
.gid
.set_it
= 1;
3135 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3138 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3139 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3140 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3141 free_nfs_cb_data(data
);
3148 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3150 struct nfs_chown_data
*chown_data
;
3152 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3153 if (chown_data
== NULL
) {
3154 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3158 chown_data
->uid
= uid
;
3159 chown_data
->gid
= gid
;
3161 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3162 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3173 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3175 struct nfs_cb_data
*data
;
3176 struct nfs_chown_data
*chown_data
;
3178 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3179 if (chown_data
== NULL
) {
3180 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3184 chown_data
->uid
= uid
;
3185 chown_data
->gid
= gid
;
3188 data
= malloc(sizeof(struct nfs_cb_data
));
3190 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3193 memset(data
, 0, sizeof(struct nfs_cb_data
));
3196 data
->private_data
= private_data
;
3197 data
->continue_data
= chown_data
;
3198 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3199 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3200 if (data
->fh
.data
.data_val
== NULL
) {
3201 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3202 free_nfs_cb_data(data
);
3205 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3208 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3209 free_nfs_cb_data(data
);
3223 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3225 struct nfs_cb_data
*data
= private_data
;
3226 struct nfs_context
*nfs
= data
->nfs
;
3229 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3231 if (status
== RPC_STATUS_ERROR
) {
3232 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3233 free_nfs_cb_data(data
);
3236 if (status
== RPC_STATUS_CANCEL
) {
3237 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3238 free_nfs_cb_data(data
);
3243 if (res
->status
!= NFS3_OK
) {
3244 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3245 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3246 free_nfs_cb_data(data
);
3250 data
->cb(0, nfs
, NULL
, data
->private_data
);
3251 free_nfs_cb_data(data
);
3254 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3257 struct timeval
*utimes_data
= data
->continue_data
;
3259 memset(&args
, 0, sizeof(SETATTR3args
));
3260 args
.object
= data
->fh
;
3261 if (utimes_data
!= NULL
) {
3262 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3263 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3264 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3265 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3266 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3267 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3269 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3270 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3273 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3274 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3275 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3276 free_nfs_cb_data(data
);
3283 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3285 struct timeval
*new_times
= NULL
;
3287 if (times
!= NULL
) {
3288 new_times
= malloc(sizeof(struct timeval
)*2);
3289 if (new_times
== NULL
) {
3290 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3294 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3297 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3298 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3308 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3310 struct timeval
*new_times
= NULL
;
3312 if (times
!= NULL
) {
3313 new_times
= malloc(sizeof(struct timeval
)*2);
3314 if (new_times
== NULL
) {
3315 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3319 new_times
[0].tv_sec
= times
->actime
;
3320 new_times
[0].tv_usec
= 0;
3321 new_times
[1].tv_sec
= times
->modtime
;
3322 new_times
[1].tv_usec
= 0;
3325 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3326 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3337 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3340 struct nfs_cb_data
*data
= private_data
;
3341 struct nfs_context
*nfs
= data
->nfs
;
3342 unsigned int nfsmode
= 0;
3344 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3346 if (status
== RPC_STATUS_ERROR
) {
3347 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3348 free_nfs_cb_data(data
);
3351 if (status
== RPC_STATUS_CANCEL
) {
3352 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3353 free_nfs_cb_data(data
);
3358 if (res
->status
!= NFS3_OK
) {
3359 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
));
3360 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3361 free_nfs_cb_data(data
);
3365 if (data
->continue_int
& R_OK
) {
3366 nfsmode
|= ACCESS3_READ
;
3368 if (data
->continue_int
& W_OK
) {
3369 nfsmode
|= ACCESS3_MODIFY
;
3371 if (data
->continue_int
& X_OK
) {
3372 nfsmode
|= ACCESS3_EXECUTE
;
3375 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3376 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3377 nfsmode
&ACCESS3_READ
?'r':'-',
3378 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3379 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3380 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3381 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3382 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3383 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3384 free_nfs_cb_data(data
);
3388 data
->cb(0, nfs
, NULL
, data
->private_data
);
3389 free_nfs_cb_data(data
);
3392 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3397 if (data
->continue_int
& R_OK
) {
3398 nfsmode
|= ACCESS3_READ
;
3400 if (data
->continue_int
& W_OK
) {
3401 nfsmode
|= ACCESS3_MODIFY
;
3403 if (data
->continue_int
& X_OK
) {
3404 nfsmode
|= ACCESS3_EXECUTE
;
3407 memset(&args
, 0, sizeof(ACCESS3args
));
3408 args
.object
= data
->fh
;
3409 args
.access
= nfsmode
;
3411 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3412 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3413 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3414 free_nfs_cb_data(data
);
3420 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3422 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3423 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3435 struct nfs_symlink_data
{
3437 char *newpathparent
;
3438 char *newpathobject
;
3441 static void free_nfs_symlink_data(void *mem
)
3443 struct nfs_symlink_data
*data
= mem
;
3445 if (data
->oldpath
!= NULL
) {
3446 free(data
->oldpath
);
3448 if (data
->newpathparent
!= NULL
) {
3449 free(data
->newpathparent
);
3451 if (data
->newpathobject
!= NULL
) {
3452 free(data
->newpathobject
);
3457 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3460 struct nfs_cb_data
*data
= private_data
;
3461 struct nfs_context
*nfs
= data
->nfs
;
3462 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3464 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3466 if (status
== RPC_STATUS_ERROR
) {
3467 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3468 free_nfs_cb_data(data
);
3471 if (status
== RPC_STATUS_CANCEL
) {
3472 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3473 free_nfs_cb_data(data
);
3478 if (res
->status
!= NFS3_OK
) {
3479 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
));
3480 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3481 free_nfs_cb_data(data
);
3485 data
->cb(0, nfs
, NULL
, data
->private_data
);
3486 free_nfs_cb_data(data
);
3489 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3491 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3494 memset(&args
, 0, sizeof(SYMLINK3args
));
3495 args
.where
.dir
= data
->fh
;
3496 args
.where
.name
= symlink_data
->newpathobject
;
3497 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3498 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
;
3499 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3501 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3502 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3503 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3504 free_nfs_cb_data(data
);
3510 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3513 struct nfs_symlink_data
*symlink_data
;
3515 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3516 if (symlink_data
== NULL
) {
3517 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3520 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3522 symlink_data
->oldpath
= strdup(oldpath
);
3523 if (symlink_data
->oldpath
== NULL
) {
3524 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3525 free_nfs_symlink_data(symlink_data
);
3529 symlink_data
->newpathparent
= strdup(newpath
);
3530 if (symlink_data
->newpathparent
== NULL
) {
3531 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3532 free_nfs_symlink_data(symlink_data
);
3536 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3538 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3539 free_nfs_symlink_data(symlink_data
);
3545 symlink_data
->newpathobject
= strdup(ptr
);
3546 if (symlink_data
->newpathobject
== NULL
) {
3547 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3548 free_nfs_symlink_data(symlink_data
);
3552 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3553 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3565 struct nfs_rename_data
{
3568 struct nfs_fh3 olddir
;
3571 struct nfs_fh3 newdir
;
3574 static void free_nfs_rename_data(void *mem
)
3576 struct nfs_rename_data
*data
= mem
;
3578 if (data
->oldpath
!= NULL
) {
3579 free(data
->oldpath
);
3581 if (data
->olddir
.data
.data_val
!= NULL
) {
3582 free(data
->olddir
.data
.data_val
);
3584 if (data
->newpath
!= NULL
) {
3585 free(data
->newpath
);
3587 if (data
->newdir
.data
.data_val
!= NULL
) {
3588 free(data
->newdir
.data
.data_val
);
3593 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3596 struct nfs_cb_data
*data
= private_data
;
3597 struct nfs_context
*nfs
= data
->nfs
;
3598 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3600 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3602 if (status
== RPC_STATUS_ERROR
) {
3603 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3604 free_nfs_cb_data(data
);
3607 if (status
== RPC_STATUS_CANCEL
) {
3608 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3609 free_nfs_cb_data(data
);
3614 if (res
->status
!= NFS3_OK
) {
3615 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
));
3616 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3617 free_nfs_cb_data(data
);
3621 data
->cb(0, nfs
, NULL
, data
->private_data
);
3622 free_nfs_cb_data(data
);
3625 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3627 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3630 /* steal the filehandle */
3631 rename_data
->newdir
= data
->fh
;
3632 data
->fh
.data
.data_val
= NULL
;
3634 args
.from
.dir
= rename_data
->olddir
;
3635 args
.from
.name
= rename_data
->oldobject
;
3636 args
.to
.dir
= rename_data
->newdir
;
3637 args
.to
.name
= rename_data
->newobject
;
3638 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3639 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3640 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3641 free_nfs_cb_data(data
);
3648 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3650 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3652 /* steal the filehandle */
3653 rename_data
->olddir
= data
->fh
;
3654 data
->fh
.data
.data_val
= NULL
;
3656 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) {
3657 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3658 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3659 free_nfs_cb_data(data
);
3662 data
->continue_data
= NULL
;
3663 free_nfs_cb_data(data
);
3669 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3672 struct nfs_rename_data
*rename_data
;
3674 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3675 if (rename_data
== NULL
) {
3676 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3679 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3681 rename_data
->oldpath
= strdup(oldpath
);
3682 if (rename_data
->oldpath
== NULL
) {
3683 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3684 free_nfs_rename_data(rename_data
);
3687 ptr
= strrchr(rename_data
->oldpath
, '/');
3689 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3690 free_nfs_rename_data(rename_data
);
3695 rename_data
->oldobject
= ptr
;
3698 rename_data
->newpath
= strdup(newpath
);
3699 if (rename_data
->newpath
== NULL
) {
3700 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3701 free_nfs_rename_data(rename_data
);
3704 ptr
= strrchr(rename_data
->newpath
, '/');
3706 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3707 free_nfs_rename_data(rename_data
);
3712 rename_data
->newobject
= ptr
;
3715 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3716 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3727 struct nfs_link_data
{
3729 struct nfs_fh3 oldfh
;
3732 struct nfs_fh3 newdir
;
3735 static void free_nfs_link_data(void *mem
)
3737 struct nfs_link_data
*data
= mem
;
3739 if (data
->oldpath
!= NULL
) {
3740 free(data
->oldpath
);
3742 if (data
->oldfh
.data
.data_val
!= NULL
) {
3743 free(data
->oldfh
.data
.data_val
);
3745 if (data
->newpath
!= NULL
) {
3746 free(data
->newpath
);
3748 if (data
->newdir
.data
.data_val
!= NULL
) {
3749 free(data
->newdir
.data
.data_val
);
3754 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3757 struct nfs_cb_data
*data
= private_data
;
3758 struct nfs_context
*nfs
= data
->nfs
;
3759 struct nfs_link_data
*link_data
= data
->continue_data
;
3761 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3763 if (status
== RPC_STATUS_ERROR
) {
3764 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3765 free_nfs_cb_data(data
);
3768 if (status
== RPC_STATUS_CANCEL
) {
3769 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3770 free_nfs_cb_data(data
);
3775 if (res
->status
!= NFS3_OK
) {
3776 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
));
3777 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3778 free_nfs_cb_data(data
);
3782 data
->cb(0, nfs
, NULL
, data
->private_data
);
3783 free_nfs_cb_data(data
);
3786 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3788 struct nfs_link_data
*link_data
= data
->continue_data
;
3791 /* steal the filehandle */
3792 link_data
->newdir
= data
->fh
;
3793 data
->fh
.data
.data_val
= NULL
;
3795 memset(&args
, 0, sizeof(LINK3args
));
3796 args
.file
= link_data
->oldfh
;
3797 args
.link
.dir
= link_data
->newdir
;
3798 args
.link
.name
= link_data
->newobject
;
3799 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
3800 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3801 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3802 free_nfs_cb_data(data
);
3809 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3811 struct nfs_link_data
*link_data
= data
->continue_data
;
3813 /* steal the filehandle */
3814 link_data
->oldfh
= data
->fh
;
3815 data
->fh
.data
.data_val
= NULL
;
3817 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) {
3818 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3819 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3820 free_nfs_cb_data(data
);
3823 data
->continue_data
= NULL
;
3824 free_nfs_cb_data(data
);
3830 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3833 struct nfs_link_data
*link_data
;
3835 link_data
= malloc(sizeof(struct nfs_link_data
));
3836 if (link_data
== NULL
) {
3837 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
3840 memset(link_data
, 0, sizeof(struct nfs_link_data
));
3842 link_data
->oldpath
= strdup(oldpath
);
3843 if (link_data
->oldpath
== NULL
) {
3844 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3845 free_nfs_link_data(link_data
);
3849 link_data
->newpath
= strdup(newpath
);
3850 if (link_data
->newpath
== NULL
) {
3851 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3852 free_nfs_link_data(link_data
);
3855 ptr
= strrchr(link_data
->newpath
, '/');
3857 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3858 free_nfs_link_data(link_data
);
3863 link_data
->newobject
= ptr
;
3866 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
3867 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3875 //qqq replace later with lseek()
3876 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
3878 return nfsfh
->offset
;
3884 * Get the maximum supported READ3 size by the server
3886 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
3888 return nfs
->readmax
;
3892 * Get the maximum supported WRITE3 size by the server
3894 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
3896 return nfs
->writemax
;
3899 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
3904 va_start(ap
, error_string
);
3906 vsnprintf(str
, 1024, error_string
, ap
);
3907 if (nfs
->rpc
->error_string
!= NULL
) {
3908 free(nfs
->rpc
->error_string
);
3910 nfs
->rpc
->error_string
= str
;
3916 struct mount_cb_data
{
3922 static void free_mount_cb_data(struct mount_cb_data
*data
)
3924 if (data
->server
!= NULL
) {
3926 data
->server
= NULL
;
3932 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3934 struct mount_cb_data
*data
= private_data
;
3936 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3938 if (status
== RPC_STATUS_ERROR
) {
3939 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3940 free_mount_cb_data(data
);
3943 if (status
== RPC_STATUS_CANCEL
) {
3944 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3945 free_mount_cb_data(data
);
3949 data
->cb(rpc
, 0, command_data
, data
->private_data
);
3950 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
3951 rpc_set_error(rpc
, "Failed to disconnect\n");
3953 free_mount_cb_data(data
);
3956 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3958 struct mount_cb_data
*data
= private_data
;
3960 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3962 /* Dont want any more callbacks even if the socket is closed */
3963 rpc
->connect_cb
= NULL
;
3965 if (status
== RPC_STATUS_ERROR
) {
3966 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3967 free_mount_cb_data(data
);
3970 if (status
== RPC_STATUS_CANCEL
) {
3971 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3972 free_mount_cb_data(data
);
3976 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
3977 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3978 free_mount_cb_data(data
);
3983 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3985 struct mount_cb_data
*data
= private_data
;
3986 uint32_t mount_port
;
3988 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3990 if (status
== RPC_STATUS_ERROR
) {
3991 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3992 free_mount_cb_data(data
);
3995 if (status
== RPC_STATUS_CANCEL
) {
3996 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3997 free_mount_cb_data(data
);
4001 mount_port
= *(uint32_t *)command_data
;
4002 if (mount_port
== 0) {
4003 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4004 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4005 free_mount_cb_data(data
);
4009 rpc_disconnect(rpc
, "normal disconnect");
4010 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4011 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4012 free_mount_cb_data(data
);
4017 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4019 struct mount_cb_data
*data
= private_data
;
4021 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4023 if (status
== RPC_STATUS_ERROR
) {
4024 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4025 free_mount_cb_data(data
);
4028 if (status
== RPC_STATUS_CANCEL
) {
4029 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4030 free_mount_cb_data(data
);
4034 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4035 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4036 free_mount_cb_data(data
);
4041 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4043 struct mount_cb_data
*data
= private_data
;
4045 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4047 /* Dont want any more callbacks even if the socket is closed */
4048 rpc
->connect_cb
= NULL
;
4050 if (status
== RPC_STATUS_ERROR
) {
4051 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4052 free_mount_cb_data(data
);
4055 if (status
== RPC_STATUS_CANCEL
) {
4056 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4057 free_mount_cb_data(data
);
4061 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4062 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4063 free_mount_cb_data(data
);
4068 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4070 struct mount_cb_data
*data
;
4072 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4074 data
= malloc(sizeof(struct mount_cb_data
));
4078 memset(data
, 0, sizeof(struct mount_cb_data
));
4080 data
->private_data
= private_data
;
4081 data
->server
= strdup(server
);
4082 if (data
->server
== NULL
) {
4083 free_mount_cb_data(data
);
4086 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4087 free_mount_cb_data(data
);
4094 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4096 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4100 const char *nfs_get_server(struct nfs_context
*nfs
) {
4104 const char *nfs_get_export(struct nfs_context
*nfs
) {
4108 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4109 return &nfs
->rootfh
;
4112 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {