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 struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, char *url
, int dir
, int incomplete
)
178 struct nfs_url
*urls
;
179 char *strp
, *flagsp
, *strp2
;
181 if (strncmp(url
, "nfs://", 6)) {
182 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
186 urls
= malloc(sizeof(struct nfs_url
));
188 rpc_set_error(nfs
->rpc
, "Out of memory");
192 urls
->server
= strdup(url
+ 6);
193 if (urls
->server
== NULL
) {
194 nfs_destroy_url(urls
);
195 rpc_set_error(nfs
->rpc
, "Out of memory");
199 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
200 urls
->server
[0] == '?') {
202 flagsp
= strchr(urls
->server
, '?');
205 nfs_destroy_url(urls
);
206 rpc_set_error(nfs
->rpc
, "Invalid server string");
210 strp
= strchr(urls
->server
, '/');
213 flagsp
= strchr(urls
->server
, '?');
216 nfs_destroy_url(urls
);
217 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
221 urls
->path
= strdup(strp
);
222 if (urls
->path
== NULL
) {
223 nfs_destroy_url(urls
);
224 rpc_set_error(nfs
->rpc
, "Out of memory");
230 flagsp
= strchr(urls
->path
, '?');
234 strp
= strrchr(urls
->path
, '/');
237 flagsp
= strchr(urls
->path
, '?');
240 nfs_destroy_url(urls
);
241 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
244 urls
->file
= strdup(strp
);
245 if (urls
->path
== NULL
) {
246 nfs_destroy_url(urls
);
247 rpc_set_error(nfs
->rpc
, "Out of memory");
251 flagsp
= strchr(urls
->file
, '?');
258 if (urls
->file
&& !strlen(urls
->file
)) {
262 nfs_destroy_url(urls
);
263 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
268 if (urls
->server
&& strlen(urls
->server
) <= 1) {
273 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
275 flagsp
= strchr(strp
, '&');
279 strp2
= strchr(strp
, '=');
289 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
291 return nfs_parse_url(nfs
, url
, 0, 0);
294 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
296 return nfs_parse_url(nfs
, url
, 1, 0);
299 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
301 return nfs_parse_url(nfs
, url
, 0, 1);
305 void nfs_destroy_url(struct nfs_url
*url
)
315 struct nfs_context
*nfs_init_context(void)
317 struct nfs_context
*nfs
;
319 nfs
= malloc(sizeof(struct nfs_context
));
323 nfs
->rpc
= rpc_init_context();
324 if (nfs
->rpc
== NULL
) {
332 nfs
->rootfh
.data
.data_len
= 0;
333 nfs
->rootfh
.data
.data_val
= NULL
;
338 void nfs_destroy_context(struct nfs_context
*nfs
)
340 rpc_destroy_context(nfs
->rpc
);
353 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
354 free(nfs
->rootfh
.data
.data_val
);
355 nfs
->rootfh
.data
.data_val
= NULL
;
370 void free_rpc_cb_data(struct rpc_cb_data
*data
)
377 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
379 struct rpc_cb_data
*data
= private_data
;
381 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
383 /* Dont want any more callbacks even if the socket is closed */
384 rpc
->connect_cb
= NULL
;
386 if (status
== RPC_STATUS_ERROR
) {
387 data
->cb(rpc
, status
, command_data
, data
->private_data
);
388 free_rpc_cb_data(data
);
391 if (status
== RPC_STATUS_CANCEL
) {
392 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
393 free_rpc_cb_data(data
);
397 data
->cb(rpc
, status
, NULL
, data
->private_data
);
398 free_rpc_cb_data(data
);
401 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
403 struct rpc_cb_data
*data
= private_data
;
406 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
408 if (status
== RPC_STATUS_ERROR
) {
409 data
->cb(rpc
, status
, command_data
, data
->private_data
);
410 free_rpc_cb_data(data
);
413 if (status
== RPC_STATUS_CANCEL
) {
414 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
415 free_rpc_cb_data(data
);
419 rpc_port
= *(uint32_t *)command_data
;
421 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
422 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
423 free_rpc_cb_data(data
);
427 rpc_disconnect(rpc
, "normal disconnect");
428 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
429 data
->cb(rpc
, status
, command_data
, data
->private_data
);
430 free_rpc_cb_data(data
);
435 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
437 struct rpc_cb_data
*data
= private_data
;
439 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
441 if (status
== RPC_STATUS_ERROR
) {
442 data
->cb(rpc
, status
, command_data
, data
->private_data
);
443 free_rpc_cb_data(data
);
446 if (status
== RPC_STATUS_CANCEL
) {
447 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
448 free_rpc_cb_data(data
);
452 if (rpc_pmap_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
453 data
->cb(rpc
, status
, command_data
, data
->private_data
);
454 free_rpc_cb_data(data
);
459 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
461 struct rpc_cb_data
*data
= private_data
;
463 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
465 /* Dont want any more callbacks even if the socket is closed */
466 rpc
->connect_cb
= NULL
;
468 if (status
== RPC_STATUS_ERROR
) {
469 data
->cb(rpc
, status
, command_data
, data
->private_data
);
470 free_rpc_cb_data(data
);
473 if (status
== RPC_STATUS_CANCEL
) {
474 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
475 free_rpc_cb_data(data
);
479 if (rpc_pmap_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
480 data
->cb(rpc
, status
, command_data
, data
->private_data
);
481 free_rpc_cb_data(data
);
486 int rpc_connect_program_async(struct rpc_context
*rpc
, char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
488 struct rpc_cb_data
*data
;
490 data
= malloc(sizeof(struct rpc_cb_data
));
494 memset(data
, 0, sizeof(struct rpc_cb_data
));
495 data
->server
= strdup(server
);
496 data
->program
= program
;
497 data
->version
= version
;
500 data
->private_data
= private_data
;
502 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
503 rpc_set_error(rpc
, "Failed to start connection");
504 free_rpc_cb_data(data
);
510 void free_nfs_cb_data(struct nfs_cb_data
*data
)
512 if (data
->saved_path
!= NULL
) {
513 free(data
->saved_path
);
514 data
->saved_path
= NULL
;
517 if (data
->continue_data
!= NULL
) {
518 data
->free_continue_data(data
->continue_data
);
519 data
->continue_data
= NULL
;
522 if (data
->fh
.data
.data_val
!= NULL
) {
523 free(data
->fh
.data
.data_val
);
524 data
->fh
.data
.data_val
= NULL
;
527 if (data
->buffer
!= NULL
) {
539 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
541 struct nfs_cb_data
*data
= private_data
;
542 struct nfs_context
*nfs
= data
->nfs
;
544 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
546 if (status
== RPC_STATUS_ERROR
) {
547 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
548 free_nfs_cb_data(data
);
551 if (status
== RPC_STATUS_CANCEL
) {
552 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
553 free_nfs_cb_data(data
);
557 data
->cb(0, nfs
, NULL
, data
->private_data
);
558 free_nfs_cb_data(data
);
561 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
563 struct nfs_cb_data
*data
= private_data
;
564 struct nfs_context
*nfs
= data
->nfs
;
565 FSINFO3res
*res
= command_data
;
567 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
569 if (status
== RPC_STATUS_ERROR
) {
570 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
571 free_nfs_cb_data(data
);
574 if (status
== RPC_STATUS_CANCEL
) {
575 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
576 free_nfs_cb_data(data
);
580 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
581 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
583 if (rpc_nfs_getattr_async(rpc
, nfs_mount_10_cb
, &nfs
->rootfh
, data
) != 0) {
584 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
585 free_nfs_cb_data(data
);
590 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
592 struct nfs_cb_data
*data
= private_data
;
593 struct nfs_context
*nfs
= data
->nfs
;
595 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
597 if (status
== RPC_STATUS_ERROR
) {
598 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
599 free_nfs_cb_data(data
);
602 if (status
== RPC_STATUS_CANCEL
) {
603 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
604 free_nfs_cb_data(data
);
608 if (rpc_nfs_fsinfo_async(rpc
, nfs_mount_9_cb
, &nfs
->rootfh
, data
) != 0) {
609 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
610 free_nfs_cb_data(data
);
616 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
618 struct nfs_cb_data
*data
= private_data
;
619 struct nfs_context
*nfs
= data
->nfs
;
621 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
623 /* Dont want any more callbacks even if the socket is closed */
624 rpc
->connect_cb
= NULL
;
626 if (status
== RPC_STATUS_ERROR
) {
627 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
628 free_nfs_cb_data(data
);
631 if (status
== RPC_STATUS_CANCEL
) {
632 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
633 free_nfs_cb_data(data
);
637 if (rpc_nfs_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
638 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
639 free_nfs_cb_data(data
);
645 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
647 struct nfs_cb_data
*data
= private_data
;
648 struct nfs_context
*nfs
= data
->nfs
;
651 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
653 if (status
== RPC_STATUS_ERROR
) {
654 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
655 free_nfs_cb_data(data
);
658 if (status
== RPC_STATUS_CANCEL
) {
659 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
660 free_nfs_cb_data(data
);
665 if (res
->fhs_status
!= MNT3_OK
) {
666 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
));
667 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
668 free_nfs_cb_data(data
);
672 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
673 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
674 if (nfs
->rootfh
.data
.data_val
== NULL
) {
675 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
676 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
677 free_nfs_cb_data(data
);
680 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
682 rpc_disconnect(rpc
, "normal disconnect");
683 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
684 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
685 free_nfs_cb_data(data
);
688 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
689 rpc_set_autoreconnect(rpc
);
693 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
695 struct nfs_cb_data
*data
= private_data
;
696 struct nfs_context
*nfs
= data
->nfs
;
698 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
700 if (status
== RPC_STATUS_ERROR
) {
701 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
702 free_nfs_cb_data(data
);
705 if (status
== RPC_STATUS_CANCEL
) {
706 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
707 free_nfs_cb_data(data
);
711 if (rpc_mount_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
712 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
713 free_nfs_cb_data(data
);
718 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
720 struct nfs_cb_data
*data
= private_data
;
721 struct nfs_context
*nfs
= data
->nfs
;
723 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
725 /* Dont want any more callbacks even if the socket is closed */
726 rpc
->connect_cb
= NULL
;
728 if (status
== RPC_STATUS_ERROR
) {
729 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
730 free_nfs_cb_data(data
);
733 if (status
== RPC_STATUS_CANCEL
) {
734 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
735 free_nfs_cb_data(data
);
739 if (rpc_mount_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
740 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
741 free_nfs_cb_data(data
);
746 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
748 struct nfs_cb_data
*data
= private_data
;
749 struct nfs_context
*nfs
= data
->nfs
;
752 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
754 if (status
== RPC_STATUS_ERROR
) {
755 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
756 free_nfs_cb_data(data
);
759 if (status
== RPC_STATUS_CANCEL
) {
760 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
761 free_nfs_cb_data(data
);
765 mount_port
= *(uint32_t *)command_data
;
766 if (mount_port
== 0) {
767 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
768 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
769 free_nfs_cb_data(data
);
773 rpc_disconnect(rpc
, "normal disconnect");
774 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
775 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
776 free_nfs_cb_data(data
);
782 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
784 struct nfs_cb_data
*data
= private_data
;
785 struct nfs_context
*nfs
= data
->nfs
;
787 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
789 if (status
== RPC_STATUS_ERROR
) {
790 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
791 free_nfs_cb_data(data
);
794 if (status
== RPC_STATUS_CANCEL
) {
795 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
796 free_nfs_cb_data(data
);
800 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
801 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
802 free_nfs_cb_data(data
);
807 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
809 struct nfs_cb_data
*data
= private_data
;
810 struct nfs_context
*nfs
= data
->nfs
;
812 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
814 /* Dont want any more callbacks even if the socket is closed */
815 rpc
->connect_cb
= NULL
;
817 if (status
== RPC_STATUS_ERROR
) {
818 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
819 free_nfs_cb_data(data
);
822 if (status
== RPC_STATUS_CANCEL
) {
823 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
824 free_nfs_cb_data(data
);
828 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
829 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
830 free_nfs_cb_data(data
);
836 * Async call for mounting an nfs share and geting the root filehandle
838 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
840 struct nfs_cb_data
*data
;
841 char *new_server
, *new_export
;
843 data
= malloc(sizeof(struct nfs_cb_data
));
845 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
848 memset(data
, 0, sizeof(struct nfs_cb_data
));
849 new_server
= strdup(server
);
850 new_export
= strdup(export
);
851 if (nfs
->server
!= NULL
) {
854 nfs
->server
= new_server
;
855 if (nfs
->export
!= NULL
) {
858 nfs
->export
= new_export
;
861 data
->private_data
= private_data
;
863 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
864 rpc_set_error(nfs
->rpc
, "Failed to start connection");
865 free_nfs_cb_data(data
);
875 * Functions to first look up a path, component by component, and then finally call a specific function once
876 * the filehandle for the final component is found.
878 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
880 struct nfs_cb_data
*data
= private_data
;
881 struct nfs_context
*nfs
= data
->nfs
;
884 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
886 if (status
== RPC_STATUS_ERROR
) {
887 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
888 free_nfs_cb_data(data
);
891 if (status
== RPC_STATUS_CANCEL
) {
892 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
893 free_nfs_cb_data(data
);
898 if (res
->status
!= NFS3_OK
) {
899 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
));
900 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
901 free_nfs_cb_data(data
);
905 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
906 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
907 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
908 free_nfs_cb_data(data
);
913 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
917 while (*data
->path
== '/') {
922 str
= strchr(path
, '/');
927 while (*data
->path
!= 0) {
933 data
->fh
.data
.data_len
= fh
->data
.data_len
;
934 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
935 if (data
->fh
.data
.data_val
== NULL
) {
936 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
937 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
938 free_nfs_cb_data(data
);
941 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
942 data
->continue_cb(nfs
, data
);
946 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, fh
, path
, data
) != 0) {
947 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
948 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
949 free_nfs_cb_data(data
);
955 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
)
957 struct nfs_cb_data
*data
;
959 if (path
[0] != 0 && path
[0] != '/') {
960 rpc_set_error(nfs
->rpc
, "Pathname is not absolute %s", path
);
964 data
= malloc(sizeof(struct nfs_cb_data
));
966 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
969 memset(data
, 0, sizeof(struct nfs_cb_data
));
972 data
->continue_cb
= continue_cb
;
973 data
->continue_data
= continue_data
;
974 data
->free_continue_data
= free_continue_data
;
975 data
->continue_int
= continue_int
;
976 data
->private_data
= private_data
;
977 data
->saved_path
= strdup(path
);
978 if (data
->saved_path
== NULL
) {
979 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
980 free_nfs_cb_data(data
);
983 data
->path
= data
->saved_path
;
985 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
986 /* return 0 here since the callback will be invoked if there is a failure */
999 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1002 struct nfs_cb_data
*data
= private_data
;
1003 struct nfs_context
*nfs
= data
->nfs
;
1006 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1008 if (status
== RPC_STATUS_ERROR
) {
1009 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1010 free_nfs_cb_data(data
);
1013 if (status
== RPC_STATUS_CANCEL
) {
1014 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1015 free_nfs_cb_data(data
);
1020 if (res
->status
!= NFS3_OK
) {
1021 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
));
1022 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1023 free_nfs_cb_data(data
);
1028 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1029 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1030 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1031 st
.st_mode
|= S_IFDIR
;
1033 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1034 st
.st_mode
|= S_IFREG
;
1036 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1037 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1038 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1040 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1042 st
.st_blksize
= 4096;
1043 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1045 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1046 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1047 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1049 data
->cb(0, nfs
, &st
, data
->private_data
);
1050 free_nfs_cb_data(data
);
1053 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1055 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &data
->fh
, data
) != 0) {
1056 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1057 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1058 free_nfs_cb_data(data
);
1064 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1066 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1067 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1081 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1084 struct nfs_cb_data
*data
= private_data
;
1085 struct nfs_context
*nfs
= data
->nfs
;
1086 struct nfsfh
*nfsfh
;
1087 unsigned int nfsmode
= 0;
1089 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1091 if (status
== RPC_STATUS_ERROR
) {
1092 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1093 free_nfs_cb_data(data
);
1096 if (status
== RPC_STATUS_CANCEL
) {
1097 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1098 free_nfs_cb_data(data
);
1103 if (res
->status
!= NFS3_OK
) {
1104 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
));
1105 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1106 free_nfs_cb_data(data
);
1110 if (data
->continue_int
& O_WRONLY
) {
1111 nfsmode
|= ACCESS3_MODIFY
;
1113 if (data
->continue_int
& O_RDWR
) {
1114 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1116 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1117 nfsmode
|= ACCESS3_READ
;
1121 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1122 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1123 nfsmode
&ACCESS3_READ
?'r':'-',
1124 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1125 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1126 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1127 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1128 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1129 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1130 free_nfs_cb_data(data
);
1134 nfsfh
= malloc(sizeof(struct nfsfh
));
1135 if (nfsfh
== NULL
) {
1136 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1137 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1138 free_nfs_cb_data(data
);
1141 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1143 if (data
->continue_int
& O_SYNC
) {
1147 /* steal the filehandle */
1148 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
1149 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
1150 data
->fh
.data
.data_val
= NULL
;
1152 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1153 free_nfs_cb_data(data
);
1156 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1160 if (data
->continue_int
& O_WRONLY
) {
1161 nfsmode
|= ACCESS3_MODIFY
;
1163 if (data
->continue_int
& O_RDWR
) {
1164 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1166 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1167 nfsmode
|= ACCESS3_READ
;
1170 if (rpc_nfs_access_async(nfs
->rpc
, nfs_open_cb
, &data
->fh
, nfsmode
, data
) != 0) {
1171 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
1172 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1173 free_nfs_cb_data(data
);
1179 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1181 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
1182 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1196 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1198 struct nfs_cb_data
*data
= private_data
;
1199 struct nfs_context
*nfs
= data
->nfs
;
1202 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1204 if (status
== RPC_STATUS_ERROR
) {
1205 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1206 free_nfs_cb_data(data
);
1209 if (status
== RPC_STATUS_CANCEL
) {
1210 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1211 free_nfs_cb_data(data
);
1216 if (res
->status
!= NFS3_OK
) {
1217 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1218 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1219 free_nfs_cb_data(data
);
1223 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1224 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1225 free_nfs_cb_data(data
);
1228 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1230 struct nfs_mcb_data
*mdata
= private_data
;
1231 struct nfs_cb_data
*data
= mdata
->data
;
1232 struct nfs_context
*nfs
= data
->nfs
;
1235 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1239 if (status
== RPC_STATUS_ERROR
) {
1240 /* flag the failure but do not invoke callback until we have received all responses */
1243 if (status
== RPC_STATUS_CANCEL
) {
1244 /* flag the cancellation but do not invoke callback until we have received all responses */
1248 /* reassemble the data into the buffer */
1249 if (status
== RPC_STATUS_SUCCESS
) {
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
));
1255 if (res
->READ3res_u
.resok
.count
> 0) {
1256 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1257 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1258 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1264 if (data
->num_calls
> 0) {
1265 /* still waiting for more replies */
1270 if (data
->error
!= 0) {
1271 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1272 free_nfs_cb_data(data
);
1276 if (data
->cancel
!= 0) {
1277 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1278 free_nfs_cb_data(data
);
1283 data
->nfsfh
->offset
= data
->max_offset
;
1284 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1286 free_nfs_cb_data(data
);
1290 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1292 struct nfs_cb_data
*data
;
1294 data
= malloc(sizeof(struct nfs_cb_data
));
1296 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1299 memset(data
, 0, sizeof(struct nfs_cb_data
));
1302 data
->private_data
= private_data
;
1303 data
->nfsfh
= nfsfh
;
1305 nfsfh
->offset
= offset
;
1307 if (count
<= nfs_get_readmax(nfs
)) {
1308 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_cb
, &nfsfh
->fh
, offset
, count
, data
) != 0) {
1309 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1310 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1311 free_nfs_cb_data(data
);
1317 /* trying to read more than maximum server read size, we has to chop it up into smaller
1318 * reads and collect into a reassembly buffer.
1319 * we send all reads in parallel so that performance is still good.
1321 data
->max_offset
= offset
;
1322 data
->start_offset
= offset
;
1324 data
->buffer
= malloc(count
);
1325 if (data
->buffer
== NULL
) {
1326 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1327 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1328 free_nfs_cb_data(data
);
1333 uint64_t readcount
= count
;
1334 struct nfs_mcb_data
*mdata
;
1336 if (readcount
> nfs_get_readmax(nfs
)) {
1337 readcount
= nfs_get_readmax(nfs
);
1340 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1341 if (mdata
== NULL
) {
1342 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1345 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1347 mdata
->offset
= offset
;
1348 mdata
->count
= readcount
;
1349 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_mcb
, &nfsfh
->fh
, offset
, readcount
, mdata
) != 0) {
1350 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1351 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1357 offset
+= readcount
;
1367 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1369 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1377 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1379 struct nfs_cb_data
*data
= private_data
;
1380 struct nfs_context
*nfs
= data
->nfs
;
1383 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1385 if (status
== RPC_STATUS_ERROR
) {
1386 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1387 free_nfs_cb_data(data
);
1390 if (status
== RPC_STATUS_CANCEL
) {
1391 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1392 free_nfs_cb_data(data
);
1397 if (res
->status
!= NFS3_OK
) {
1398 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1399 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1400 free_nfs_cb_data(data
);
1404 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1405 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1406 free_nfs_cb_data(data
);
1409 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1411 struct nfs_mcb_data
*mdata
= private_data
;
1412 struct nfs_cb_data
*data
= mdata
->data
;
1413 struct nfs_context
*nfs
= data
->nfs
;
1416 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1420 if (status
== RPC_STATUS_ERROR
) {
1421 /* flag the failure but do not invoke callback until we have received all responses */
1424 if (status
== RPC_STATUS_CANCEL
) {
1425 /* flag the cancellation but do not invoke callback until we have received all responses */
1429 if (status
== RPC_STATUS_SUCCESS
) {
1431 if (res
->status
!= NFS3_OK
) {
1432 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1435 if (res
->WRITE3res_u
.resok
.count
> 0) {
1436 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1437 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1443 if (data
->num_calls
> 0) {
1444 /* still waiting for more replies */
1449 if (data
->error
!= 0) {
1450 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1451 free_nfs_cb_data(data
);
1455 if (data
->cancel
!= 0) {
1456 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1457 free_nfs_cb_data(data
);
1462 data
->nfsfh
->offset
= data
->max_offset
;
1463 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1465 free_nfs_cb_data(data
);
1470 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
)
1472 struct nfs_cb_data
*data
;
1474 data
= malloc(sizeof(struct nfs_cb_data
));
1476 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1479 memset(data
, 0, sizeof(struct nfs_cb_data
));
1482 data
->private_data
= private_data
;
1483 data
->nfsfh
= nfsfh
;
1485 nfsfh
->offset
= offset
;
1487 if (count
<= nfs_get_writemax(nfs
)) {
1488 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_cb
, &nfsfh
->fh
, buf
, offset
, count
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, data
) != 0) {
1489 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1490 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1491 free_nfs_cb_data(data
);
1497 /* trying to write more than maximum server write size, we has to chop it up into smaller
1499 * we send all writes in parallel so that performance is still good.
1501 data
->max_offset
= offset
;
1502 data
->start_offset
= offset
;
1505 uint64_t writecount
= count
;
1506 struct nfs_mcb_data
*mdata
;
1508 if (writecount
> nfs_get_writemax(nfs
)) {
1509 writecount
= nfs_get_writemax(nfs
);
1512 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1513 if (mdata
== NULL
) {
1514 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1517 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1519 mdata
->offset
= offset
;
1520 mdata
->count
= writecount
;
1522 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &nfsfh
->fh
, &buf
[offset
- data
->start_offset
], offset
, writecount
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, mdata
) != 0) {
1523 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1524 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1529 count
-= writecount
;
1530 offset
+= writecount
;
1540 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1542 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1552 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1554 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1555 free(nfsfh
->fh
.data
.data_val
);
1556 nfsfh
->fh
.data
.data_val
= NULL
;
1560 cb(0, nfs
, NULL
, private_data
);
1571 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1573 struct nfs_cb_data
*data
;
1575 data
= malloc(sizeof(struct nfs_cb_data
));
1577 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1580 memset(data
, 0, sizeof(struct nfs_cb_data
));
1583 data
->private_data
= private_data
;
1585 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &nfsfh
->fh
, data
) != 0) {
1586 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1587 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1588 free_nfs_cb_data(data
);
1599 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1601 struct nfs_cb_data
*data
= private_data
;
1602 struct nfs_context
*nfs
= data
->nfs
;
1605 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1607 if (status
== RPC_STATUS_ERROR
) {
1608 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1609 free_nfs_cb_data(data
);
1612 if (status
== RPC_STATUS_CANCEL
) {
1613 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1614 free_nfs_cb_data(data
);
1619 if (res
->status
!= NFS3_OK
) {
1620 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1621 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1622 free_nfs_cb_data(data
);
1626 data
->cb(0, nfs
, NULL
, data
->private_data
);
1627 free_nfs_cb_data(data
);
1630 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1632 struct nfs_cb_data
*data
;
1634 data
= malloc(sizeof(struct nfs_cb_data
));
1636 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1639 memset(data
, 0, sizeof(struct nfs_cb_data
));
1642 data
->private_data
= private_data
;
1644 if (rpc_nfs_commit_async(nfs
->rpc
, nfs_fsync_cb
, &nfsfh
->fh
, data
) != 0) {
1645 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1646 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1647 free_nfs_cb_data(data
);
1659 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1661 struct nfs_cb_data
*data
= private_data
;
1662 struct nfs_context
*nfs
= data
->nfs
;
1665 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1667 if (status
== RPC_STATUS_ERROR
) {
1668 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1669 free_nfs_cb_data(data
);
1672 if (status
== RPC_STATUS_CANCEL
) {
1673 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1674 free_nfs_cb_data(data
);
1679 if (res
->status
!= NFS3_OK
) {
1680 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1681 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1682 free_nfs_cb_data(data
);
1686 data
->cb(0, nfs
, NULL
, data
->private_data
);
1687 free_nfs_cb_data(data
);
1690 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1692 struct nfs_cb_data
*data
;
1695 data
= malloc(sizeof(struct nfs_cb_data
));
1697 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1700 memset(data
, 0, sizeof(struct nfs_cb_data
));
1703 data
->private_data
= private_data
;
1705 memset(&args
, 0, sizeof(SETATTR3args
));
1706 args
.object
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
1707 args
.object
.data
.data_val
= nfsfh
->fh
.data
.data_val
;
1708 args
.new_attributes
.size
.set_it
= 1;
1709 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1711 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1712 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1713 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1714 free_nfs_cb_data(data
);
1724 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1726 uint64_t offset
= data
->continue_int
;
1729 nfsfh
.fh
.data
.data_val
= data
->fh
.data
.data_val
;
1730 nfsfh
.fh
.data
.data_len
= data
->fh
.data
.data_len
;
1732 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1733 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1734 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1735 free_nfs_cb_data(data
);
1738 free_nfs_cb_data(data
);
1742 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1748 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1749 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1762 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1765 struct nfs_cb_data
*data
= private_data
;
1766 struct nfs_context
*nfs
= data
->nfs
;
1767 char *str
= data
->continue_data
;
1769 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1771 str
= &str
[strlen(str
) + 1];
1773 if (status
== RPC_STATUS_ERROR
) {
1774 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1775 free_nfs_cb_data(data
);
1778 if (status
== RPC_STATUS_CANCEL
) {
1779 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1780 free_nfs_cb_data(data
);
1785 if (res
->status
!= NFS3_OK
) {
1786 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
));
1787 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1788 free_nfs_cb_data(data
);
1792 data
->cb(0, nfs
, NULL
, data
->private_data
);
1793 free_nfs_cb_data(data
);
1796 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1798 char *str
= data
->continue_data
;
1801 str
= &str
[strlen(str
) + 1];
1803 memset(&args
, 0, sizeof(MKDIR3args
));
1804 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1805 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1806 args
.where
.name
= str
;
1807 args
.attributes
.mode
.set_it
= 1;
1808 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
1810 if (rpc_nfs_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
1811 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1812 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1813 free_nfs_cb_data(data
);
1819 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1824 new_path
= strdup(path
);
1825 if (new_path
== NULL
) {
1826 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1830 ptr
= strrchr(new_path
, '/');
1832 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1837 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1838 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1839 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1853 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1856 struct nfs_cb_data
*data
= private_data
;
1857 struct nfs_context
*nfs
= data
->nfs
;
1858 char *str
= data
->continue_data
;
1860 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1862 str
= &str
[strlen(str
) + 1];
1864 if (status
== RPC_STATUS_ERROR
) {
1865 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1866 free_nfs_cb_data(data
);
1869 if (status
== RPC_STATUS_CANCEL
) {
1870 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1871 free_nfs_cb_data(data
);
1876 if (res
->status
!= NFS3_OK
) {
1877 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
));
1878 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1879 free_nfs_cb_data(data
);
1883 data
->cb(0, nfs
, NULL
, data
->private_data
);
1884 free_nfs_cb_data(data
);
1887 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1889 char *str
= data
->continue_data
;
1891 str
= &str
[strlen(str
) + 1];
1893 if (rpc_nfs_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &data
->fh
, str
, data
) != 0) {
1894 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1895 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1896 free_nfs_cb_data(data
);
1902 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1907 new_path
= strdup(path
);
1908 if (new_path
== NULL
) {
1909 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1913 ptr
= strrchr(new_path
, '/');
1915 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1920 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1921 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1922 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1935 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1938 struct nfs_cb_data
*data
= private_data
;
1939 struct nfs_context
*nfs
= data
->nfs
;
1940 struct nfsfh
*nfsfh
;
1941 char *str
= data
->continue_data
;
1943 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1945 if (status
== RPC_STATUS_ERROR
) {
1946 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1947 free_nfs_cb_data(data
);
1950 if (status
== RPC_STATUS_CANCEL
) {
1951 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1952 free_nfs_cb_data(data
);
1956 str
= &str
[strlen(str
) + 1];
1958 if (res
->status
!= NFS3_OK
) {
1959 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
));
1960 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1965 nfsfh
= malloc(sizeof(struct nfsfh
));
1966 if (nfsfh
== NULL
) {
1967 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1968 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1969 free_nfs_cb_data(data
);
1972 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1974 /* copy the filehandle */
1975 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
1976 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
1977 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
1979 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1980 free_nfs_cb_data(data
);
1985 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1988 struct nfs_cb_data
*data
= private_data
;
1989 struct nfs_context
*nfs
= data
->nfs
;
1990 char *str
= data
->continue_data
;
1992 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1994 if (status
== RPC_STATUS_ERROR
) {
1995 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1996 free_nfs_cb_data(data
);
1999 if (status
== RPC_STATUS_CANCEL
) {
2000 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2001 free_nfs_cb_data(data
);
2005 str
= &str
[strlen(str
) + 1];
2007 if (res
->status
!= NFS3_OK
) {
2008 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
));
2009 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2010 free_nfs_cb_data(data
);
2014 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &data
->fh
, str
, data
) != 0) {
2015 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2016 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2017 free_nfs_cb_data(data
);
2023 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2025 char *str
= data
->continue_data
;
2028 str
= &str
[strlen(str
) + 1];
2030 memset(&args
, 0, sizeof(CREATE3args
));
2031 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
2032 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
2033 args
.where
.name
= str
;
2034 args
.how
.mode
= UNCHECKED
;
2035 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2036 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2038 if (rpc_nfs_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2039 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2040 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2041 free_nfs_cb_data(data
);
2047 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2052 new_path
= strdup(path
);
2053 if (new_path
== NULL
) {
2054 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2058 ptr
= strrchr(new_path
, '/');
2060 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2065 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2066 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2067 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2080 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2083 struct nfs_cb_data
*data
= private_data
;
2084 struct nfs_context
*nfs
= data
->nfs
;
2085 char *str
= data
->continue_data
;
2087 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2089 str
= &str
[strlen(str
) + 1];
2091 if (status
== RPC_STATUS_ERROR
) {
2092 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2093 free_nfs_cb_data(data
);
2096 if (status
== RPC_STATUS_CANCEL
) {
2097 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2098 free_nfs_cb_data(data
);
2103 if (res
->status
!= NFS3_OK
) {
2104 rpc_set_error(nfs
->rpc
, "NFS: REMOVE of %s/%s failed with %s(%d)", data
->saved_path
, str
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2105 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2106 free_nfs_cb_data(data
);
2110 data
->cb(0, nfs
, NULL
, data
->private_data
);
2111 free_nfs_cb_data(data
);
2114 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2116 char *str
= data
->continue_data
;
2118 str
= &str
[strlen(str
) + 1];
2120 if (rpc_nfs_remove_async(nfs
->rpc
, nfs_unlink_cb
, &data
->fh
, str
, data
) != 0) {
2121 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2122 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2123 free_nfs_cb_data(data
);
2129 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2134 new_path
= strdup(path
);
2135 if (new_path
== NULL
) {
2136 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2140 ptr
= strrchr(new_path
, '/');
2142 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2147 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2148 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2149 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2160 struct mknod_cb_data
{
2167 static void free_mknod_cb_data(void *ptr
)
2169 struct mknod_cb_data
*data
= ptr
;
2175 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2178 struct nfs_cb_data
*data
= private_data
;
2179 struct nfs_context
*nfs
= data
->nfs
;
2180 char *str
= data
->continue_data
;
2182 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2184 str
= &str
[strlen(str
) + 1];
2186 if (status
== RPC_STATUS_ERROR
) {
2187 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2188 free_nfs_cb_data(data
);
2191 if (status
== RPC_STATUS_CANCEL
) {
2192 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2193 free_nfs_cb_data(data
);
2198 if (res
->status
!= NFS3_OK
) {
2199 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
));
2200 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2201 free_nfs_cb_data(data
);
2205 data
->cb(0, nfs
, NULL
, data
->private_data
);
2206 free_nfs_cb_data(data
);
2209 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2211 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2212 char *str
= cb_data
->path
;
2214 str
= &str
[strlen(str
) + 1];
2216 if (rpc_nfs_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &data
->fh
, str
, cb_data
->mode
, cb_data
->major
, cb_data
->minor
, data
) != 0) {
2217 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2218 free_nfs_cb_data(data
);
2224 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2227 struct mknod_cb_data
*cb_data
;
2229 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2230 if (cb_data
== NULL
) {
2231 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2235 cb_data
->path
= strdup(path
);
2236 if (cb_data
->path
== NULL
) {
2237 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2242 ptr
= strrchr(cb_data
->path
, '/');
2244 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2249 cb_data
->mode
= mode
;
2250 cb_data
->major
= major(dev
);
2251 cb_data
->minor
= minor(dev
);
2253 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2254 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2255 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2256 free_mknod_cb_data(cb_data
);
2267 /* ReadDirPlus Emulation Callback data */
2268 struct rdpe_cb_data
{
2271 struct nfs_cb_data
*data
;
2274 /* ReadDirPlus Emulation LOOKUP Callback data */
2275 struct rdpe_lookup_cb_data
{
2276 struct rdpe_cb_data
*rdpe_cb_data
;
2277 struct nfsdirent
*nfsdirent
;
2280 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2281 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2283 LOOKUP3res
*res
= command_data
;
2284 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2285 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2286 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2287 struct nfsdir
*nfsdir
= data
->continue_data
;
2288 struct nfs_context
*nfs
= data
->nfs
;
2289 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2291 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2293 free(rdpe_lookup_cb_data
);
2295 rdpe_cb_data
->getattrcount
--;
2297 if (status
== RPC_STATUS_ERROR
) {
2298 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2300 if (status
== RPC_STATUS_CANCEL
) {
2301 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2303 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2304 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2306 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2307 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2308 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2310 nfsdirent
->type
= attributes
->type
;
2311 nfsdirent
->mode
= attributes
->mode
;
2312 nfsdirent
->size
= attributes
->size
;
2314 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2315 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2316 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2317 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2318 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2319 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2323 if (rdpe_cb_data
->getattrcount
== 0) {
2324 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2325 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2326 nfs_free_nfsdir(nfsdir
);
2328 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2332 data
->continue_data
= NULL
;
2333 free_nfs_cb_data(data
);
2337 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2339 READDIR3res
*res
= command_data
;
2340 struct nfs_cb_data
*data
= private_data
;
2341 struct nfs_context
*nfs
= data
->nfs
;
2342 struct nfsdir
*nfsdir
= data
->continue_data
;
2343 struct nfsdirent
*nfsdirent
;
2344 struct entry3
*entry
;
2346 struct rdpe_cb_data
*rdpe_cb_data
;
2348 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2350 if (status
== RPC_STATUS_ERROR
) {
2351 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2352 nfs_free_nfsdir(nfsdir
);
2353 data
->continue_data
= NULL
;
2354 free_nfs_cb_data(data
);
2358 if (status
== RPC_STATUS_CANCEL
) {
2359 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2360 nfs_free_nfsdir(nfsdir
);
2361 data
->continue_data
= NULL
;
2362 free_nfs_cb_data(data
);
2366 if (res
->status
!= NFS3_OK
) {
2367 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
));
2368 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2369 nfs_free_nfsdir(nfsdir
);
2370 data
->continue_data
= NULL
;
2371 free_nfs_cb_data(data
);
2375 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2376 while (entry
!= NULL
) {
2377 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2378 if (nfsdirent
== NULL
) {
2379 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2380 nfs_free_nfsdir(nfsdir
);
2381 data
->continue_data
= NULL
;
2382 free_nfs_cb_data(data
);
2385 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2386 nfsdirent
->name
= strdup(entry
->name
);
2387 if (nfsdirent
->name
== NULL
) {
2388 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2389 nfs_free_nfsdir(nfsdir
);
2390 data
->continue_data
= NULL
;
2391 free_nfs_cb_data(data
);
2394 nfsdirent
->inode
= entry
->fileid
;
2396 nfsdirent
->next
= nfsdir
->entries
;
2397 nfsdir
->entries
= nfsdirent
;
2399 cookie
= entry
->cookie
;
2400 entry
= entry
->nextentry
;
2403 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2404 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, cookie
, res
->READDIR3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2405 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2406 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2407 nfs_free_nfsdir(nfsdir
);
2408 data
->continue_data
= NULL
;
2409 free_nfs_cb_data(data
);
2415 /* steal the dirhandle */
2416 nfsdir
->current
= nfsdir
->entries
;
2418 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2419 rdpe_cb_data
->getattrcount
= 0;
2420 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2421 rdpe_cb_data
->data
= data
;
2422 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2423 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2425 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2426 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2427 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2429 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &data
->fh
, nfsdirent
->name
, rdpe_lookup_cb_data
) != 0) {
2430 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2432 /* if we have already commands in flight, we cant just stop, we have to wait for the
2433 * commands in flight to complete
2435 if (rdpe_cb_data
->getattrcount
> 0) {
2436 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2437 free(rdpe_lookup_cb_data
);
2441 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2442 nfs_free_nfsdir(nfsdir
);
2443 data
->continue_data
= NULL
;
2444 free_nfs_cb_data(data
);
2445 free(rdpe_lookup_cb_data
);
2449 rdpe_cb_data
->getattrcount
++;
2454 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2456 READDIRPLUS3res
*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 entryplus3
*entry
;
2463 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2465 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2468 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2469 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2470 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2471 nfs_free_nfsdir(nfsdir
);
2472 data
->continue_data
= NULL
;
2473 free_nfs_cb_data(data
);
2479 if (status
== RPC_STATUS_CANCEL
) {
2480 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2481 nfs_free_nfsdir(nfsdir
);
2482 data
->continue_data
= NULL
;
2483 free_nfs_cb_data(data
);
2487 if (res
->status
!= NFS3_OK
) {
2488 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
));
2489 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2490 nfs_free_nfsdir(nfsdir
);
2491 data
->continue_data
= NULL
;
2492 free_nfs_cb_data(data
);
2496 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2497 while (entry
!= NULL
) {
2498 struct nfsdirent
*nfsdirent
;
2500 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2501 if (nfsdirent
== NULL
) {
2502 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2503 nfs_free_nfsdir(nfsdir
);
2504 data
->continue_data
= NULL
;
2505 free_nfs_cb_data(data
);
2508 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2509 nfsdirent
->name
= strdup(entry
->name
);
2510 if (nfsdirent
->name
== NULL
) {
2511 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2512 nfs_free_nfsdir(nfsdir
);
2513 data
->continue_data
= NULL
;
2514 free_nfs_cb_data(data
);
2517 nfsdirent
->inode
= entry
->fileid
;
2518 if (entry
->name_attributes
.attributes_follow
) {
2519 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2520 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2521 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2523 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2524 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2525 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2526 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2527 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2528 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2531 nfsdirent
->next
= nfsdir
->entries
;
2532 nfsdir
->entries
= nfsdirent
;
2534 cookie
= entry
->cookie
;
2535 entry
= entry
->nextentry
;
2538 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2539 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, cookie
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2540 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2541 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2542 nfs_free_nfsdir(nfsdir
);
2543 data
->continue_data
= NULL
;
2544 free_nfs_cb_data(data
);
2550 /* steal the dirhandle */
2551 data
->continue_data
= NULL
;
2552 nfsdir
->current
= nfsdir
->entries
;
2554 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2555 free_nfs_cb_data(data
);
2558 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2562 memset(cv
, 0, sizeof(cookieverf3
));
2563 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2564 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2565 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2566 free_nfs_cb_data(data
);
2572 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2574 struct nfsdir
*nfsdir
;
2576 nfsdir
= malloc(sizeof(struct nfsdir
));
2577 if (nfsdir
== NULL
) {
2578 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2581 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2583 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2584 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2592 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2594 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2596 if (nfsdir
->current
!= NULL
) {
2597 nfsdir
->current
= nfsdir
->current
->next
;
2603 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2605 nfs_free_nfsdir(nfsdir
);
2617 struct lseek_cb_data
{
2618 struct nfs_context
*nfs
;
2619 struct nfsfh
*nfsfh
;
2625 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2628 struct lseek_cb_data
*data
= private_data
;
2629 struct nfs_context
*nfs
= data
->nfs
;
2631 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2633 if (status
== RPC_STATUS_ERROR
) {
2634 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2638 if (status
== RPC_STATUS_CANCEL
) {
2639 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2645 if (res
->status
!= NFS3_OK
) {
2646 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2647 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2652 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2653 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2657 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2659 struct lseek_cb_data
*data
;
2661 if (whence
== SEEK_SET
) {
2662 nfsfh
->offset
= offset
;
2663 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2666 if (whence
== SEEK_CUR
) {
2667 nfsfh
->offset
+= offset
;
2668 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2672 data
= malloc(sizeof(struct lseek_cb_data
));
2674 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2679 data
->nfsfh
= nfsfh
;
2680 data
->offset
= offset
;
2682 data
->private_data
= private_data
;
2684 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &nfsfh
->fh
, data
) != 0) {
2685 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
2698 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2701 struct nfs_cb_data
*data
= private_data
;
2702 struct nfs_context
*nfs
= data
->nfs
;
2703 struct statvfs svfs
;
2705 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2707 if (status
== RPC_STATUS_ERROR
) {
2708 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2709 free_nfs_cb_data(data
);
2712 if (status
== RPC_STATUS_CANCEL
) {
2713 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2714 free_nfs_cb_data(data
);
2719 if (res
->status
!= NFS3_OK
) {
2720 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
));
2721 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2722 free_nfs_cb_data(data
);
2726 svfs
.f_bsize
= 4096;
2727 svfs
.f_frsize
= 4096;
2728 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
2729 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
2730 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
2731 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
2732 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
2733 #if !defined(ANDROID)
2734 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
2737 svfs
.f_namemax
= 256;
2740 data
->cb(0, nfs
, &svfs
, data
->private_data
);
2741 free_nfs_cb_data(data
);
2744 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2746 if (rpc_nfs_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &data
->fh
, data
) != 0) {
2747 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
2748 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2749 free_nfs_cb_data(data
);
2755 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2757 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
2758 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2771 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2774 struct nfs_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
);
2781 free_nfs_cb_data(data
);
2784 if (status
== RPC_STATUS_CANCEL
) {
2785 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2786 free_nfs_cb_data(data
);
2791 if (res
->status
!= NFS3_OK
) {
2792 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
));
2793 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2794 free_nfs_cb_data(data
);
2799 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
2800 free_nfs_cb_data(data
);
2803 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2807 args
.symlink
.data
.data_len
= data
->fh
.data
.data_len
;
2808 args
.symlink
.data
.data_val
= data
->fh
.data
.data_val
;
2810 if (rpc_nfs_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
2811 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
2812 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2813 free_nfs_cb_data(data
);
2819 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2821 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
2822 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2835 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2837 struct nfs_cb_data
*data
= private_data
;
2838 struct nfs_context
*nfs
= data
->nfs
;
2841 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2843 if (status
== RPC_STATUS_ERROR
) {
2844 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2845 free_nfs_cb_data(data
);
2848 if (status
== RPC_STATUS_CANCEL
) {
2849 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2850 free_nfs_cb_data(data
);
2855 if (res
->status
!= NFS3_OK
) {
2856 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2857 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2858 free_nfs_cb_data(data
);
2862 data
->cb(0, nfs
, NULL
, data
->private_data
);
2863 free_nfs_cb_data(data
);
2866 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2870 memset(&args
, 0, sizeof(SETATTR3args
));
2871 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2872 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2873 args
.new_attributes
.mode
.set_it
= 1;
2874 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2876 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
2877 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2878 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2879 free_nfs_cb_data(data
);
2886 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2888 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
2889 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2899 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
2901 struct nfs_cb_data
*data
;
2903 data
= malloc(sizeof(struct nfs_cb_data
));
2905 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
2908 memset(data
, 0, sizeof(struct nfs_cb_data
));
2911 data
->private_data
= private_data
;
2912 data
->continue_int
= mode
;
2913 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2914 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2915 if (data
->fh
.data
.data_val
== NULL
) {
2916 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2917 free_nfs_cb_data(data
);
2920 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2922 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
2923 free_nfs_cb_data(data
);
2935 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2937 struct nfs_cb_data
*data
= private_data
;
2938 struct nfs_context
*nfs
= data
->nfs
;
2941 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2943 if (status
== RPC_STATUS_ERROR
) {
2944 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2945 free_nfs_cb_data(data
);
2948 if (status
== RPC_STATUS_CANCEL
) {
2949 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2950 free_nfs_cb_data(data
);
2955 if (res
->status
!= NFS3_OK
) {
2956 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2957 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2958 free_nfs_cb_data(data
);
2962 data
->cb(0, nfs
, NULL
, data
->private_data
);
2963 free_nfs_cb_data(data
);
2966 struct nfs_chown_data
{
2971 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2974 struct nfs_chown_data
*chown_data
= data
->continue_data
;
2976 memset(&args
, 0, sizeof(SETATTR3args
));
2977 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2978 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2979 if (chown_data
->uid
!= (uid_t
)-1) {
2980 args
.new_attributes
.uid
.set_it
= 1;
2981 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
2983 if (chown_data
->gid
!= (gid_t
)-1) {
2984 args
.new_attributes
.gid
.set_it
= 1;
2985 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
2988 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
2989 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2990 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2991 free_nfs_cb_data(data
);
2998 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3000 struct nfs_chown_data
*chown_data
;
3002 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3003 if (chown_data
== NULL
) {
3004 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3008 chown_data
->uid
= uid
;
3009 chown_data
->gid
= gid
;
3011 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3012 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3023 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3025 struct nfs_cb_data
*data
;
3026 struct nfs_chown_data
*chown_data
;
3028 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3029 if (chown_data
== NULL
) {
3030 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3034 chown_data
->uid
= uid
;
3035 chown_data
->gid
= gid
;
3038 data
= malloc(sizeof(struct nfs_cb_data
));
3040 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3043 memset(data
, 0, sizeof(struct nfs_cb_data
));
3046 data
->private_data
= private_data
;
3047 data
->continue_data
= chown_data
;
3048 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3049 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3050 if (data
->fh
.data
.data_val
== NULL
) {
3051 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3052 free_nfs_cb_data(data
);
3055 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3058 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3059 free_nfs_cb_data(data
);
3073 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3075 struct nfs_cb_data
*data
= private_data
;
3076 struct nfs_context
*nfs
= data
->nfs
;
3079 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3081 if (status
== RPC_STATUS_ERROR
) {
3082 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3083 free_nfs_cb_data(data
);
3086 if (status
== RPC_STATUS_CANCEL
) {
3087 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3088 free_nfs_cb_data(data
);
3093 if (res
->status
!= NFS3_OK
) {
3094 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3095 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3096 free_nfs_cb_data(data
);
3100 data
->cb(0, nfs
, NULL
, data
->private_data
);
3101 free_nfs_cb_data(data
);
3104 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3107 struct timeval
*utimes_data
= data
->continue_data
;
3109 memset(&args
, 0, sizeof(SETATTR3args
));
3110 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
3111 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
3112 if (utimes_data
!= NULL
) {
3113 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3114 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3115 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3116 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3117 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3118 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3120 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3121 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3124 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3125 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3126 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3127 free_nfs_cb_data(data
);
3134 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3136 struct timeval
*new_times
= NULL
;
3138 if (times
!= NULL
) {
3139 new_times
= malloc(sizeof(struct timeval
)*2);
3140 if (new_times
== NULL
) {
3141 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3145 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3148 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3149 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3159 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3161 struct timeval
*new_times
= NULL
;
3163 if (times
!= NULL
) {
3164 new_times
= malloc(sizeof(struct timeval
)*2);
3165 if (new_times
== NULL
) {
3166 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3170 new_times
[0].tv_sec
= times
->actime
;
3171 new_times
[0].tv_usec
= 0;
3172 new_times
[1].tv_sec
= times
->modtime
;
3173 new_times
[1].tv_usec
= 0;
3176 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3177 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3188 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3191 struct nfs_cb_data
*data
= private_data
;
3192 struct nfs_context
*nfs
= data
->nfs
;
3193 unsigned int nfsmode
= 0;
3195 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3197 if (status
== RPC_STATUS_ERROR
) {
3198 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3199 free_nfs_cb_data(data
);
3202 if (status
== RPC_STATUS_CANCEL
) {
3203 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3204 free_nfs_cb_data(data
);
3209 if (res
->status
!= NFS3_OK
) {
3210 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
));
3211 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3212 free_nfs_cb_data(data
);
3216 if (data
->continue_int
& R_OK
) {
3217 nfsmode
|= ACCESS3_READ
;
3219 if (data
->continue_int
& W_OK
) {
3220 nfsmode
|= ACCESS3_MODIFY
;
3222 if (data
->continue_int
& X_OK
) {
3223 nfsmode
|= ACCESS3_EXECUTE
;
3226 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3227 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3228 nfsmode
&ACCESS3_READ
?'r':'-',
3229 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3230 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3231 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3232 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3233 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3234 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3235 free_nfs_cb_data(data
);
3239 data
->cb(0, nfs
, NULL
, data
->private_data
);
3240 free_nfs_cb_data(data
);
3243 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3247 if (data
->continue_int
& R_OK
) {
3248 nfsmode
|= ACCESS3_READ
;
3250 if (data
->continue_int
& W_OK
) {
3251 nfsmode
|= ACCESS3_MODIFY
;
3253 if (data
->continue_int
& X_OK
) {
3254 nfsmode
|= ACCESS3_EXECUTE
;
3257 if (rpc_nfs_access_async(nfs
->rpc
, nfs_access_cb
, &data
->fh
, nfsmode
, data
) != 0) {
3258 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3259 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3260 free_nfs_cb_data(data
);
3266 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3268 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3269 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3281 struct nfs_symlink_data
{
3283 char *newpathparent
;
3284 char *newpathobject
;
3287 static void free_nfs_symlink_data(void *mem
)
3289 struct nfs_symlink_data
*data
= mem
;
3291 if (data
->oldpath
!= NULL
) {
3292 free(data
->oldpath
);
3294 if (data
->newpathparent
!= NULL
) {
3295 free(data
->newpathparent
);
3297 if (data
->newpathobject
!= NULL
) {
3298 free(data
->newpathobject
);
3303 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3306 struct nfs_cb_data
*data
= private_data
;
3307 struct nfs_context
*nfs
= data
->nfs
;
3308 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3310 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3312 if (status
== RPC_STATUS_ERROR
) {
3313 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3314 free_nfs_cb_data(data
);
3317 if (status
== RPC_STATUS_CANCEL
) {
3318 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3319 free_nfs_cb_data(data
);
3324 if (res
->status
!= NFS3_OK
) {
3325 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
));
3326 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3327 free_nfs_cb_data(data
);
3331 data
->cb(0, nfs
, NULL
, data
->private_data
);
3332 free_nfs_cb_data(data
);
3335 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3337 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3340 memset(&sa
, 0, sizeof(SYMLINK3args
));
3341 sa
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
3342 sa
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
3343 sa
.where
.name
= symlink_data
->newpathobject
;
3344 sa
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3345 sa
.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
;
3346 sa
.symlink
.symlink_data
= symlink_data
->oldpath
;
3348 if (rpc_nfs_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &sa
, data
) != 0) {
3349 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3350 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3351 free_nfs_cb_data(data
);
3357 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3360 struct nfs_symlink_data
*symlink_data
;
3362 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3363 if (symlink_data
== NULL
) {
3364 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3367 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3369 symlink_data
->oldpath
= strdup(oldpath
);
3370 if (symlink_data
->oldpath
== NULL
) {
3371 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3372 free_nfs_symlink_data(symlink_data
);
3376 symlink_data
->newpathparent
= strdup(newpath
);
3377 if (symlink_data
->newpathparent
== NULL
) {
3378 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3379 free_nfs_symlink_data(symlink_data
);
3383 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3385 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3386 free_nfs_symlink_data(symlink_data
);
3392 symlink_data
->newpathobject
= strdup(ptr
);
3393 if (symlink_data
->newpathobject
== NULL
) {
3394 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3395 free_nfs_symlink_data(symlink_data
);
3399 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3400 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3412 struct nfs_rename_data
{
3415 struct nfs_fh3 olddir
;
3418 struct nfs_fh3 newdir
;
3421 static void free_nfs_rename_data(void *mem
)
3423 struct nfs_rename_data
*data
= mem
;
3425 if (data
->oldpath
!= NULL
) {
3426 free(data
->oldpath
);
3428 if (data
->olddir
.data
.data_val
!= NULL
) {
3429 free(data
->olddir
.data
.data_val
);
3431 if (data
->newpath
!= NULL
) {
3432 free(data
->newpath
);
3434 if (data
->newdir
.data
.data_val
!= NULL
) {
3435 free(data
->newdir
.data
.data_val
);
3440 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3443 struct nfs_cb_data
*data
= private_data
;
3444 struct nfs_context
*nfs
= data
->nfs
;
3445 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3447 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3449 if (status
== RPC_STATUS_ERROR
) {
3450 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3451 free_nfs_cb_data(data
);
3454 if (status
== RPC_STATUS_CANCEL
) {
3455 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3456 free_nfs_cb_data(data
);
3461 if (res
->status
!= NFS3_OK
) {
3462 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
));
3463 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3464 free_nfs_cb_data(data
);
3468 data
->cb(0, nfs
, NULL
, data
->private_data
);
3469 free_nfs_cb_data(data
);
3472 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3474 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3476 /* steal the filehandle */
3477 rename_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3478 rename_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3479 data
->fh
.data
.data_val
= NULL
;
3481 if (rpc_nfs_rename_async(nfs
->rpc
, nfs_rename_cb
, &rename_data
->olddir
, rename_data
->oldobject
, &rename_data
->newdir
, rename_data
->newobject
, data
) != 0) {
3482 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3483 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3484 free_nfs_cb_data(data
);
3491 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3493 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3495 /* steal the filehandle */
3496 rename_data
->olddir
.data
.data_len
= data
->fh
.data
.data_len
;
3497 rename_data
->olddir
.data
.data_val
= data
->fh
.data
.data_val
;
3498 data
->fh
.data
.data_val
= NULL
;
3500 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) {
3501 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3502 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3503 free_nfs_cb_data(data
);
3506 data
->continue_data
= NULL
;
3507 free_nfs_cb_data(data
);
3513 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3516 struct nfs_rename_data
*rename_data
;
3518 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3519 if (rename_data
== NULL
) {
3520 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3523 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3525 rename_data
->oldpath
= strdup(oldpath
);
3526 if (rename_data
->oldpath
== NULL
) {
3527 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3528 free_nfs_rename_data(rename_data
);
3531 ptr
= strrchr(rename_data
->oldpath
, '/');
3533 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3534 free_nfs_rename_data(rename_data
);
3539 rename_data
->oldobject
= ptr
;
3542 rename_data
->newpath
= strdup(newpath
);
3543 if (rename_data
->newpath
== NULL
) {
3544 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3545 free_nfs_rename_data(rename_data
);
3548 ptr
= strrchr(rename_data
->newpath
, '/');
3550 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3551 free_nfs_rename_data(rename_data
);
3556 rename_data
->newobject
= ptr
;
3559 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3560 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3571 struct nfs_link_data
{
3573 struct nfs_fh3 oldfh
;
3576 struct nfs_fh3 newdir
;
3579 static void free_nfs_link_data(void *mem
)
3581 struct nfs_link_data
*data
= mem
;
3583 if (data
->oldpath
!= NULL
) {
3584 free(data
->oldpath
);
3586 if (data
->oldfh
.data
.data_val
!= NULL
) {
3587 free(data
->oldfh
.data
.data_val
);
3589 if (data
->newpath
!= NULL
) {
3590 free(data
->newpath
);
3592 if (data
->newdir
.data
.data_val
!= NULL
) {
3593 free(data
->newdir
.data
.data_val
);
3598 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3601 struct nfs_cb_data
*data
= private_data
;
3602 struct nfs_context
*nfs
= data
->nfs
;
3603 struct nfs_link_data
*link_data
= data
->continue_data
;
3605 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3607 if (status
== RPC_STATUS_ERROR
) {
3608 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3609 free_nfs_cb_data(data
);
3612 if (status
== RPC_STATUS_CANCEL
) {
3613 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3614 free_nfs_cb_data(data
);
3619 if (res
->status
!= NFS3_OK
) {
3620 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
));
3621 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3622 free_nfs_cb_data(data
);
3626 data
->cb(0, nfs
, NULL
, data
->private_data
);
3627 free_nfs_cb_data(data
);
3630 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3632 struct nfs_link_data
*link_data
= data
->continue_data
;
3634 /* steal the filehandle */
3635 link_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3636 link_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3637 data
->fh
.data
.data_val
= NULL
;
3639 if (rpc_nfs_link_async(nfs
->rpc
, nfs_link_cb
, &link_data
->oldfh
, &link_data
->newdir
, link_data
->newobject
, data
) != 0) {
3640 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3641 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3642 free_nfs_cb_data(data
);
3649 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3651 struct nfs_link_data
*link_data
= data
->continue_data
;
3653 /* steal the filehandle */
3654 link_data
->oldfh
.data
.data_len
= data
->fh
.data
.data_len
;
3655 link_data
->oldfh
.data
.data_val
= data
->fh
.data
.data_val
;
3656 data
->fh
.data
.data_val
= NULL
;
3658 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) {
3659 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3660 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3661 free_nfs_cb_data(data
);
3664 data
->continue_data
= NULL
;
3665 free_nfs_cb_data(data
);
3671 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3674 struct nfs_link_data
*link_data
;
3676 link_data
= malloc(sizeof(struct nfs_link_data
));
3677 if (link_data
== NULL
) {
3678 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
3681 memset(link_data
, 0, sizeof(struct nfs_link_data
));
3683 link_data
->oldpath
= strdup(oldpath
);
3684 if (link_data
->oldpath
== NULL
) {
3685 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3686 free_nfs_link_data(link_data
);
3690 link_data
->newpath
= strdup(newpath
);
3691 if (link_data
->newpath
== NULL
) {
3692 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3693 free_nfs_link_data(link_data
);
3696 ptr
= strrchr(link_data
->newpath
, '/');
3698 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3699 free_nfs_link_data(link_data
);
3704 link_data
->newobject
= ptr
;
3707 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
3708 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3716 //qqq replace later with lseek()
3717 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
3719 return nfsfh
->offset
;
3725 * Get the maximum supported READ3 size by the server
3727 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
3729 return nfs
->readmax
;
3733 * Get the maximum supported WRITE3 size by the server
3735 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
3737 return nfs
->writemax
;
3740 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
3745 va_start(ap
, error_string
);
3747 vsnprintf(str
, 1024, error_string
, ap
);
3748 if (nfs
->rpc
->error_string
!= NULL
) {
3749 free(nfs
->rpc
->error_string
);
3751 nfs
->rpc
->error_string
= str
;
3757 struct mount_cb_data
{
3763 static void free_mount_cb_data(struct mount_cb_data
*data
)
3765 if (data
->server
!= NULL
) {
3767 data
->server
= NULL
;
3773 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3775 struct mount_cb_data
*data
= private_data
;
3777 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3779 if (status
== RPC_STATUS_ERROR
) {
3780 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3781 free_mount_cb_data(data
);
3784 if (status
== RPC_STATUS_CANCEL
) {
3785 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3786 free_mount_cb_data(data
);
3790 data
->cb(rpc
, 0, command_data
, data
->private_data
);
3791 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
3792 rpc_set_error(rpc
, "Failed to disconnect\n");
3794 free_mount_cb_data(data
);
3797 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3799 struct mount_cb_data
*data
= private_data
;
3801 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3803 /* Dont want any more callbacks even if the socket is closed */
3804 rpc
->connect_cb
= NULL
;
3806 if (status
== RPC_STATUS_ERROR
) {
3807 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3808 free_mount_cb_data(data
);
3811 if (status
== RPC_STATUS_CANCEL
) {
3812 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3813 free_mount_cb_data(data
);
3817 if (rpc_mount_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
3818 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3819 free_mount_cb_data(data
);
3824 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3826 struct mount_cb_data
*data
= private_data
;
3827 uint32_t mount_port
;
3829 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3831 if (status
== RPC_STATUS_ERROR
) {
3832 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3833 free_mount_cb_data(data
);
3836 if (status
== RPC_STATUS_CANCEL
) {
3837 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3838 free_mount_cb_data(data
);
3842 mount_port
= *(uint32_t *)command_data
;
3843 if (mount_port
== 0) {
3844 rpc_set_error(rpc
, "RPC error. Mount program is not available");
3845 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
3846 free_mount_cb_data(data
);
3850 rpc_disconnect(rpc
, "normal disconnect");
3851 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
3852 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3853 free_mount_cb_data(data
);
3858 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3860 struct mount_cb_data
*data
= private_data
;
3862 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3864 if (status
== RPC_STATUS_ERROR
) {
3865 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3866 free_mount_cb_data(data
);
3869 if (status
== RPC_STATUS_CANCEL
) {
3870 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3871 free_mount_cb_data(data
);
3875 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
3876 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3877 free_mount_cb_data(data
);
3882 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3884 struct mount_cb_data
*data
= private_data
;
3886 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3888 /* Dont want any more callbacks even if the socket is closed */
3889 rpc
->connect_cb
= NULL
;
3891 if (status
== RPC_STATUS_ERROR
) {
3892 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3893 free_mount_cb_data(data
);
3896 if (status
== RPC_STATUS_CANCEL
) {
3897 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3898 free_mount_cb_data(data
);
3902 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
3903 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3904 free_mount_cb_data(data
);
3909 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
3911 struct mount_cb_data
*data
;
3913 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3915 data
= malloc(sizeof(struct mount_cb_data
));
3919 memset(data
, 0, sizeof(struct mount_cb_data
));
3921 data
->private_data
= private_data
;
3922 data
->server
= strdup(server
);
3923 if (data
->server
== NULL
) {
3924 free_mount_cb_data(data
);
3927 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
3928 free_mount_cb_data(data
);
3935 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
3937 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
3941 const char *nfs_get_server(struct nfs_context
*nfs
) {
3945 const char *nfs_get_export(struct nfs_context
*nfs
) {
3949 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
3950 return &nfs
->rootfh
;
3953 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {