2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * High level api to nfs filesystems
25 #include "aros_compat.h"
29 #include "win32_compat.h"
37 #define statvfs statfs
50 #ifdef HAVE_SYS_STATVFS_H
51 #include <sys/statvfs.h>
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
68 #include <sys/types.h>
71 #include "libnfs-zdr.h"
73 #include "libnfs-raw.h"
74 #include "libnfs-raw-mount.h"
75 #include "libnfs-raw-nfs.h"
76 #include "libnfs-private.h"
79 struct nfsdirent
*entries
;
80 struct nfsdirent
*current
;
90 struct rpc_context
*rpc
;
93 struct nfs_fh3 rootfh
;
99 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
101 while (nfsdir
->entries
) {
102 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
103 if (nfsdir
->entries
->name
!= NULL
) {
104 free(nfsdir
->entries
->name
);
106 free(nfsdir
->entries
);
107 nfsdir
->entries
= dirent
;
113 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
116 struct nfs_context
*nfs
;
118 char *saved_path
, *path
;
123 continue_func continue_cb
;
125 void (*free_continue_data
)(void *);
130 /* for multi-read/write calls. */
134 uint64_t start_offset
, max_offset
;
138 struct nfs_mcb_data
{
139 struct nfs_cb_data
*data
;
144 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
147 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
149 rpc_set_auth(nfs
->rpc
, auth
);
152 int nfs_get_fd(struct nfs_context
*nfs
)
154 return rpc_get_fd(nfs
->rpc
);
157 int nfs_queue_length(struct nfs_context
*nfs
)
159 return rpc_queue_length(nfs
->rpc
);
162 int nfs_which_events(struct nfs_context
*nfs
)
164 return rpc_which_events(nfs
->rpc
);
167 int nfs_service(struct nfs_context
*nfs
, int revents
)
169 return rpc_service(nfs
->rpc
, revents
);
172 char *nfs_get_error(struct nfs_context
*nfs
)
174 return rpc_get_error(nfs
->rpc
);
177 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
179 if (!strncmp(arg
, "tcp-syncnt", 10)) {
180 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
181 } else if (!strncmp(arg
, "uid", 3)) {
182 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
183 } else if (!strncmp(arg
, "gid", 3)) {
184 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
189 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
191 struct nfs_url
*urls
;
192 char *strp
, *flagsp
, *strp2
;
194 if (strncmp(url
, "nfs://", 6)) {
195 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
199 urls
= malloc(sizeof(struct nfs_url
));
201 rpc_set_error(nfs
->rpc
, "Out of memory");
205 memset(urls
, 0x00, sizeof(struct nfs_url
));
206 urls
->server
= strdup(url
+ 6);
207 if (urls
->server
== NULL
) {
208 nfs_destroy_url(urls
);
209 rpc_set_error(nfs
->rpc
, "Out of memory");
213 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
214 urls
->server
[0] == '?') {
216 flagsp
= strchr(urls
->server
, '?');
219 nfs_destroy_url(urls
);
220 rpc_set_error(nfs
->rpc
, "Invalid server string");
224 strp
= strchr(urls
->server
, '/');
227 flagsp
= strchr(urls
->server
, '?');
230 nfs_destroy_url(urls
);
231 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
235 urls
->path
= strdup(strp
);
236 if (urls
->path
== NULL
) {
237 nfs_destroy_url(urls
);
238 rpc_set_error(nfs
->rpc
, "Out of memory");
244 flagsp
= strchr(urls
->path
, '?');
248 strp
= strrchr(urls
->path
, '/');
251 flagsp
= strchr(urls
->path
, '?');
254 nfs_destroy_url(urls
);
255 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
258 urls
->file
= strdup(strp
);
259 if (urls
->path
== NULL
) {
260 nfs_destroy_url(urls
);
261 rpc_set_error(nfs
->rpc
, "Out of memory");
265 flagsp
= strchr(urls
->file
, '?');
272 if (urls
->file
&& !strlen(urls
->file
)) {
276 nfs_destroy_url(urls
);
277 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
282 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
284 flagsp
= strchr(strp
, '&');
288 strp2
= strchr(strp
, '=');
292 nfs_set_context_args(nfs
, strp
, strp2
);
296 if (urls
->server
&& strlen(urls
->server
) <= 1) {
304 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
306 return nfs_parse_url(nfs
, url
, 0, 0);
309 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
311 return nfs_parse_url(nfs
, url
, 1, 0);
314 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
316 return nfs_parse_url(nfs
, url
, 0, 1);
320 void nfs_destroy_url(struct nfs_url
*url
)
330 struct nfs_context
*nfs_init_context(void)
332 struct nfs_context
*nfs
;
334 nfs
= malloc(sizeof(struct nfs_context
));
338 memset(nfs
, 0, sizeof(struct nfs_context
));
340 nfs
->rpc
= rpc_init_context();
341 if (nfs
->rpc
== NULL
) {
346 nfs
->cwd
= strdup("/");
351 void nfs_destroy_context(struct nfs_context
*nfs
)
353 rpc_destroy_context(nfs
->rpc
);
371 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
372 free(nfs
->rootfh
.data
.data_val
);
373 nfs
->rootfh
.data
.data_val
= NULL
;
388 void free_rpc_cb_data(struct rpc_cb_data
*data
)
395 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
397 struct rpc_cb_data
*data
= private_data
;
399 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
401 /* Dont want any more callbacks even if the socket is closed */
402 rpc
->connect_cb
= NULL
;
404 if (status
== RPC_STATUS_ERROR
) {
405 data
->cb(rpc
, status
, command_data
, data
->private_data
);
406 free_rpc_cb_data(data
);
409 if (status
== RPC_STATUS_CANCEL
) {
410 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
411 free_rpc_cb_data(data
);
415 data
->cb(rpc
, status
, NULL
, data
->private_data
);
416 free_rpc_cb_data(data
);
419 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
421 struct rpc_cb_data
*data
= private_data
;
424 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
426 if (status
== RPC_STATUS_ERROR
) {
427 data
->cb(rpc
, status
, command_data
, data
->private_data
);
428 free_rpc_cb_data(data
);
431 if (status
== RPC_STATUS_CANCEL
) {
432 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
433 free_rpc_cb_data(data
);
437 rpc_port
= *(uint32_t *)command_data
;
439 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
440 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
441 free_rpc_cb_data(data
);
445 rpc_disconnect(rpc
, "normal disconnect");
446 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
447 data
->cb(rpc
, status
, command_data
, data
->private_data
);
448 free_rpc_cb_data(data
);
453 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
455 struct rpc_cb_data
*data
= private_data
;
457 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
459 if (status
== RPC_STATUS_ERROR
) {
460 data
->cb(rpc
, status
, command_data
, data
->private_data
);
461 free_rpc_cb_data(data
);
464 if (status
== RPC_STATUS_CANCEL
) {
465 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
466 free_rpc_cb_data(data
);
470 if (rpc_pmap_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
471 data
->cb(rpc
, status
, command_data
, data
->private_data
);
472 free_rpc_cb_data(data
);
477 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
479 struct rpc_cb_data
*data
= private_data
;
481 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
483 /* Dont want any more callbacks even if the socket is closed */
484 rpc
->connect_cb
= NULL
;
486 if (status
== RPC_STATUS_ERROR
) {
487 data
->cb(rpc
, status
, command_data
, data
->private_data
);
488 free_rpc_cb_data(data
);
491 if (status
== RPC_STATUS_CANCEL
) {
492 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
493 free_rpc_cb_data(data
);
497 if (rpc_pmap_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
498 data
->cb(rpc
, status
, command_data
, data
->private_data
);
499 free_rpc_cb_data(data
);
504 int rpc_connect_program_async(struct rpc_context
*rpc
, char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
506 struct rpc_cb_data
*data
;
508 data
= malloc(sizeof(struct rpc_cb_data
));
512 memset(data
, 0, sizeof(struct rpc_cb_data
));
513 data
->server
= strdup(server
);
514 data
->program
= program
;
515 data
->version
= version
;
518 data
->private_data
= private_data
;
520 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
521 rpc_set_error(rpc
, "Failed to start connection");
522 free_rpc_cb_data(data
);
528 void free_nfs_cb_data(struct nfs_cb_data
*data
)
530 if (data
->saved_path
!= NULL
) {
531 free(data
->saved_path
);
532 data
->saved_path
= NULL
;
535 if (data
->continue_data
!= NULL
) {
536 data
->free_continue_data(data
->continue_data
);
537 data
->continue_data
= NULL
;
540 if (data
->fh
.data
.data_val
!= NULL
) {
541 free(data
->fh
.data
.data_val
);
542 data
->fh
.data
.data_val
= NULL
;
545 if (data
->buffer
!= NULL
) {
557 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
559 struct nfs_cb_data
*data
= private_data
;
560 struct nfs_context
*nfs
= data
->nfs
;
562 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
564 if (status
== RPC_STATUS_ERROR
) {
565 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
566 free_nfs_cb_data(data
);
569 if (status
== RPC_STATUS_CANCEL
) {
570 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
571 free_nfs_cb_data(data
);
575 data
->cb(0, nfs
, NULL
, data
->private_data
);
576 free_nfs_cb_data(data
);
579 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
581 struct nfs_cb_data
*data
= private_data
;
582 struct nfs_context
*nfs
= data
->nfs
;
583 FSINFO3res
*res
= command_data
;
584 struct GETATTR3args args
;
586 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
588 if (status
== RPC_STATUS_ERROR
) {
589 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
590 free_nfs_cb_data(data
);
593 if (status
== RPC_STATUS_CANCEL
) {
594 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
595 free_nfs_cb_data(data
);
599 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
600 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
602 memset(&args
, 0, sizeof(GETATTR3args
));
603 args
.object
= nfs
->rootfh
;
605 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
606 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
607 free_nfs_cb_data(data
);
612 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
614 struct nfs_cb_data
*data
= private_data
;
615 struct nfs_context
*nfs
= data
->nfs
;
616 struct FSINFO3args args
;
618 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
620 if (status
== RPC_STATUS_ERROR
) {
621 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
622 free_nfs_cb_data(data
);
625 if (status
== RPC_STATUS_CANCEL
) {
626 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
627 free_nfs_cb_data(data
);
631 args
.fsroot
= nfs
->rootfh
;
632 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
633 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
634 free_nfs_cb_data(data
);
640 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
642 struct nfs_cb_data
*data
= private_data
;
643 struct nfs_context
*nfs
= data
->nfs
;
645 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
647 /* Dont want any more callbacks even if the socket is closed */
648 rpc
->connect_cb
= NULL
;
650 if (status
== RPC_STATUS_ERROR
) {
651 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
652 free_nfs_cb_data(data
);
655 if (status
== RPC_STATUS_CANCEL
) {
656 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
657 free_nfs_cb_data(data
);
661 if (rpc_nfs3_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
662 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
663 free_nfs_cb_data(data
);
669 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
671 struct nfs_cb_data
*data
= private_data
;
672 struct nfs_context
*nfs
= data
->nfs
;
675 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
677 if (status
== RPC_STATUS_ERROR
) {
678 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
679 free_nfs_cb_data(data
);
682 if (status
== RPC_STATUS_CANCEL
) {
683 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
684 free_nfs_cb_data(data
);
689 if (res
->fhs_status
!= MNT3_OK
) {
690 rpc_set_error(rpc
, "RPC error: Mount failed with error %s(%d) %s(%d)", mountstat3_to_str(res
->fhs_status
), res
->fhs_status
, strerror(-mountstat3_to_errno(res
->fhs_status
)), -mountstat3_to_errno(res
->fhs_status
));
691 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
692 free_nfs_cb_data(data
);
696 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
697 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
698 if (nfs
->rootfh
.data
.data_val
== NULL
) {
699 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
700 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
701 free_nfs_cb_data(data
);
704 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
706 rpc_disconnect(rpc
, "normal disconnect");
707 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
708 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
709 free_nfs_cb_data(data
);
712 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
713 rpc_set_autoreconnect(rpc
);
717 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
719 struct nfs_cb_data
*data
= private_data
;
720 struct nfs_context
*nfs
= data
->nfs
;
722 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
724 if (status
== RPC_STATUS_ERROR
) {
725 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
726 free_nfs_cb_data(data
);
729 if (status
== RPC_STATUS_CANCEL
) {
730 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
731 free_nfs_cb_data(data
);
735 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
736 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
737 free_nfs_cb_data(data
);
742 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
744 struct nfs_cb_data
*data
= private_data
;
745 struct nfs_context
*nfs
= data
->nfs
;
747 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
749 /* Dont want any more callbacks even if the socket is closed */
750 rpc
->connect_cb
= NULL
;
752 if (status
== RPC_STATUS_ERROR
) {
753 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
754 free_nfs_cb_data(data
);
757 if (status
== RPC_STATUS_CANCEL
) {
758 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
759 free_nfs_cb_data(data
);
763 if (rpc_mount3_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
764 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
765 free_nfs_cb_data(data
);
770 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
772 struct nfs_cb_data
*data
= private_data
;
773 struct nfs_context
*nfs
= data
->nfs
;
776 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
778 if (status
== RPC_STATUS_ERROR
) {
779 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
780 free_nfs_cb_data(data
);
783 if (status
== RPC_STATUS_CANCEL
) {
784 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
785 free_nfs_cb_data(data
);
789 mount_port
= *(uint32_t *)command_data
;
790 if (mount_port
== 0) {
791 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
792 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
793 free_nfs_cb_data(data
);
797 rpc_disconnect(rpc
, "normal disconnect");
798 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
799 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
800 free_nfs_cb_data(data
);
806 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
808 struct nfs_cb_data
*data
= private_data
;
809 struct nfs_context
*nfs
= data
->nfs
;
811 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
813 if (status
== RPC_STATUS_ERROR
) {
814 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
815 free_nfs_cb_data(data
);
818 if (status
== RPC_STATUS_CANCEL
) {
819 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
820 free_nfs_cb_data(data
);
824 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
825 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
826 free_nfs_cb_data(data
);
831 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
833 struct nfs_cb_data
*data
= private_data
;
834 struct nfs_context
*nfs
= data
->nfs
;
836 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
838 /* Dont want any more callbacks even if the socket is closed */
839 rpc
->connect_cb
= NULL
;
841 if (status
== RPC_STATUS_ERROR
) {
842 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
843 free_nfs_cb_data(data
);
846 if (status
== RPC_STATUS_CANCEL
) {
847 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
848 free_nfs_cb_data(data
);
852 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
853 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
854 free_nfs_cb_data(data
);
860 * Async call for mounting an nfs share and geting the root filehandle
862 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
864 struct nfs_cb_data
*data
;
865 char *new_server
, *new_export
;
867 data
= malloc(sizeof(struct nfs_cb_data
));
869 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
872 memset(data
, 0, sizeof(struct nfs_cb_data
));
873 new_server
= strdup(server
);
874 new_export
= strdup(export
);
875 if (nfs
->server
!= NULL
) {
878 nfs
->server
= new_server
;
879 if (nfs
->export
!= NULL
) {
882 nfs
->export
= new_export
;
885 data
->private_data
= private_data
;
887 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
888 rpc_set_error(nfs
->rpc
, "Failed to start connection");
889 free_nfs_cb_data(data
);
899 * Functions to first look up a path, component by component, and then finally call a specific function once
900 * the filehandle for the final component is found.
902 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
904 struct nfs_cb_data
*data
= private_data
;
905 struct nfs_context
*nfs
= data
->nfs
;
908 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
910 if (status
== RPC_STATUS_ERROR
) {
911 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
912 free_nfs_cb_data(data
);
915 if (status
== RPC_STATUS_CANCEL
) {
916 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
917 free_nfs_cb_data(data
);
922 if (res
->status
!= NFS3_OK
) {
923 rpc_set_error(nfs
->rpc
, "NFS: Lookup of %s failed with %s(%d)", data
->saved_path
, nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
924 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
925 free_nfs_cb_data(data
);
929 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
930 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
931 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
932 free_nfs_cb_data(data
);
937 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
942 while (*data
->path
== '/') {
947 slash
= strchr(path
, '/');
949 /* Clear slash so that path is a zero terminated string for
950 * the current path component. Set it back to '/' again later
951 * when we are finished referencing this component so that
952 * data->saved_path will still point to the full
956 data
->path
= slash
+1;
958 while (*data
->path
!= 0) {
964 data
->fh
.data
.data_len
= fh
->data
.data_len
;
965 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
966 if (data
->fh
.data
.data_val
== NULL
) {
967 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
968 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
969 free_nfs_cb_data(data
);
972 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
976 data
->continue_cb(nfs
, data
);
981 memset(&args
, 0, sizeof(LOOKUP3args
));
983 args
.what
.name
= path
;
985 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
986 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
987 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
988 free_nfs_cb_data(data
);
997 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
1003 while (str
= strstr(path
, "//")) {
1011 while (str
= strstr(path
, "/./")) {
1018 /* ^/../ -> error */
1019 if (!strncmp(path
, "/../", 4)) {
1020 rpc_set_error(nfs
->rpc
,
1021 "Absolute path starts with '/../' "
1022 "during normalization");
1026 /* ^[^/] -> error */
1027 if (path
[0] != '/') {
1028 rpc_set_error(nfs
->rpc
,
1029 "Absolute path does not start with '/'");
1033 /* /string/../ -> / */
1034 while (str
= strstr(path
, "/../")) {
1037 if (!strncmp(path
, "/../", 4)) {
1038 rpc_set_error(nfs
->rpc
,
1039 "Absolute path starts with '/../' "
1040 "during normalization");
1045 while (*tmp
!= '/') {
1049 while((*(tmp
++) = *(str
++)) != '\0')
1056 if (path
[len
- 1] == '/') {
1057 path
[len
- 1] = '\0';
1061 if (path
[0] == '\0') {
1062 rpc_set_error(nfs
->rpc
,
1063 "Absolute path became '' "
1064 "during normalization");
1070 if (!strcmp(&path
[len
- 2], "/.")) {
1071 path
[len
- 2] = '\0';
1076 /* ^/..$ -> error */
1077 if (!strcmp(path
, "/..")) {
1078 rpc_set_error(nfs
->rpc
,
1079 "Absolute path is '/..' "
1080 "during normalization");
1084 /* /string/..$ -> / */
1086 if (!strcmp(&path
[len
- 3], "/..")) {
1087 char *tmp
= &path
[len
- 3];
1088 while (*--tmp
!= '/')
1097 static int nfs_lookuppath_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
, continue_func continue_cb
, void *continue_data
, void (*free_continue_data
)(void *), int continue_int
)
1099 struct nfs_cb_data
*data
;
1101 if (path
[0] == '\0') {
1105 data
= malloc(sizeof(struct nfs_cb_data
));
1107 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
1108 "nfs_cb_data structure");
1111 memset(data
, 0, sizeof(struct nfs_cb_data
));
1114 data
->continue_cb
= continue_cb
;
1115 data
->continue_data
= continue_data
;
1116 data
->free_continue_data
= free_continue_data
;
1117 data
->continue_int
= continue_int
;
1118 data
->private_data
= private_data
;
1119 if (path
[0] == '/') {
1120 data
->saved_path
= strdup(path
);
1122 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1123 if (data
->saved_path
== NULL
) {
1124 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1125 "malloc path string");
1126 free_nfs_cb_data(data
);
1129 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1132 if (data
->saved_path
== NULL
) {
1133 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1134 free_nfs_cb_data(data
);
1137 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1138 free_nfs_cb_data(data
);
1142 data
->path
= data
->saved_path
;
1144 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1145 /* return 0 here since the callback will be invoked if there is a failure */
1158 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1161 struct nfs_cb_data
*data
= private_data
;
1162 struct nfs_context
*nfs
= data
->nfs
;
1169 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1171 if (status
== RPC_STATUS_ERROR
) {
1172 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1173 free_nfs_cb_data(data
);
1176 if (status
== RPC_STATUS_CANCEL
) {
1177 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1178 free_nfs_cb_data(data
);
1183 if (res
->status
!= NFS3_OK
) {
1184 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
));
1185 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1186 free_nfs_cb_data(data
);
1191 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1192 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1193 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1194 st
.st_mode
|= S_IFDIR
;
1196 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1197 st
.st_mode
|= S_IFREG
;
1199 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1200 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1201 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1203 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1205 st
.st_blksize
= 4096;
1206 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1208 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1209 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1210 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1212 data
->cb(0, nfs
, &st
, data
->private_data
);
1213 free_nfs_cb_data(data
);
1216 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1218 struct GETATTR3args args
;
1220 memset(&args
, 0, sizeof(GETATTR3args
));
1221 args
.object
= data
->fh
;
1223 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1224 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1225 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1226 free_nfs_cb_data(data
);
1232 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1234 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1235 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1247 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1250 struct nfs_cb_data
*data
= private_data
;
1251 struct nfs_context
*nfs
= data
->nfs
;
1252 struct nfsfh
*nfsfh
;
1253 unsigned int nfsmode
= 0;
1255 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1257 if (status
== RPC_STATUS_ERROR
) {
1258 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1259 free_nfs_cb_data(data
);
1262 if (status
== RPC_STATUS_CANCEL
) {
1263 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1264 free_nfs_cb_data(data
);
1269 if (res
->status
!= NFS3_OK
) {
1270 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
));
1271 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1272 free_nfs_cb_data(data
);
1276 if (data
->continue_int
& O_WRONLY
) {
1277 nfsmode
|= ACCESS3_MODIFY
;
1279 if (data
->continue_int
& O_RDWR
) {
1280 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1282 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1283 nfsmode
|= ACCESS3_READ
;
1287 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1288 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1289 nfsmode
&ACCESS3_READ
?'r':'-',
1290 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1291 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1292 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1293 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1294 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1295 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1296 free_nfs_cb_data(data
);
1300 nfsfh
= malloc(sizeof(struct nfsfh
));
1301 if (nfsfh
== NULL
) {
1302 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1303 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1304 free_nfs_cb_data(data
);
1307 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1309 if (data
->continue_int
& O_SYNC
) {
1313 /* steal the filehandle */
1314 nfsfh
->fh
= data
->fh
;
1315 data
->fh
.data
.data_val
= NULL
;
1317 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1318 free_nfs_cb_data(data
);
1321 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1326 if (data
->continue_int
& O_WRONLY
) {
1327 nfsmode
|= ACCESS3_MODIFY
;
1329 if (data
->continue_int
& O_RDWR
) {
1330 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1332 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1333 nfsmode
|= ACCESS3_READ
;
1336 memset(&args
, 0, sizeof(ACCESS3args
));
1337 args
.object
= data
->fh
;
1338 args
.access
= nfsmode
;
1340 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1341 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
1342 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1343 free_nfs_cb_data(data
);
1349 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1351 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
1352 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1363 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1365 /* steal saved_path */
1367 nfs
->cwd
= data
->saved_path
;
1368 data
->saved_path
= NULL
;
1370 data
->cb(0, nfs
, NULL
, data
->private_data
);
1371 free_nfs_cb_data(data
);
1376 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1378 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1379 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1390 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1392 struct nfs_cb_data
*data
= private_data
;
1393 struct nfs_context
*nfs
= data
->nfs
;
1396 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1398 if (status
== RPC_STATUS_ERROR
) {
1399 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1400 free_nfs_cb_data(data
);
1403 if (status
== RPC_STATUS_CANCEL
) {
1404 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1405 free_nfs_cb_data(data
);
1410 if (res
->status
!= NFS3_OK
) {
1411 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1412 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1413 free_nfs_cb_data(data
);
1417 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1418 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1419 free_nfs_cb_data(data
);
1422 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1424 struct nfs_mcb_data
*mdata
= private_data
;
1425 struct nfs_cb_data
*data
= mdata
->data
;
1426 struct nfs_context
*nfs
= data
->nfs
;
1429 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1433 if (status
== RPC_STATUS_ERROR
) {
1434 /* flag the failure but do not invoke callback until we have received all responses */
1437 if (status
== RPC_STATUS_CANCEL
) {
1438 /* flag the cancellation but do not invoke callback until we have received all responses */
1442 /* reassemble the data into the buffer */
1443 if (status
== RPC_STATUS_SUCCESS
) {
1445 if (res
->status
!= NFS3_OK
) {
1446 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1449 if (res
->READ3res_u
.resok
.count
> 0) {
1450 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1451 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1452 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1458 if (data
->num_calls
> 0) {
1459 /* still waiting for more replies */
1464 if (data
->error
!= 0) {
1465 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1466 free_nfs_cb_data(data
);
1470 if (data
->cancel
!= 0) {
1471 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1472 free_nfs_cb_data(data
);
1477 data
->nfsfh
->offset
= data
->max_offset
;
1478 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1480 free_nfs_cb_data(data
);
1484 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1486 struct nfs_cb_data
*data
;
1488 data
= malloc(sizeof(struct nfs_cb_data
));
1490 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1493 memset(data
, 0, sizeof(struct nfs_cb_data
));
1496 data
->private_data
= private_data
;
1497 data
->nfsfh
= nfsfh
;
1499 nfsfh
->offset
= offset
;
1501 if (count
<= nfs_get_readmax(nfs
)) {
1504 memset(&args
, 0, sizeof(READ3args
));
1505 args
.file
= nfsfh
->fh
;
1506 args
.offset
= offset
;
1509 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_cb
, &args
, data
) != 0) {
1510 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1511 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1512 free_nfs_cb_data(data
);
1518 /* trying to read more than maximum server read size, we has to chop it up into smaller
1519 * reads and collect into a reassembly buffer.
1520 * we send all reads in parallel so that performance is still good.
1522 data
->max_offset
= offset
;
1523 data
->start_offset
= offset
;
1525 data
->buffer
= malloc(count
);
1526 if (data
->buffer
== NULL
) {
1527 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1528 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1529 free_nfs_cb_data(data
);
1534 uint64_t readcount
= count
;
1535 struct nfs_mcb_data
*mdata
;
1538 if (readcount
> nfs_get_readmax(nfs
)) {
1539 readcount
= nfs_get_readmax(nfs
);
1542 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1543 if (mdata
== NULL
) {
1544 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1547 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1549 mdata
->offset
= offset
;
1550 mdata
->count
= readcount
;
1552 memset(&args
, 0, sizeof(READ3args
));
1553 args
.file
= nfsfh
->fh
;
1554 args
.offset
= offset
;
1555 args
.count
= readcount
;
1557 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1558 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1559 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1565 offset
+= readcount
;
1575 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1577 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1585 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1587 struct nfs_cb_data
*data
= private_data
;
1588 struct nfs_context
*nfs
= data
->nfs
;
1591 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1593 if (status
== RPC_STATUS_ERROR
) {
1594 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1595 free_nfs_cb_data(data
);
1598 if (status
== RPC_STATUS_CANCEL
) {
1599 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1600 free_nfs_cb_data(data
);
1605 if (res
->status
!= NFS3_OK
) {
1606 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1607 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1608 free_nfs_cb_data(data
);
1612 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1613 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1614 free_nfs_cb_data(data
);
1617 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1619 struct nfs_mcb_data
*mdata
= private_data
;
1620 struct nfs_cb_data
*data
= mdata
->data
;
1621 struct nfs_context
*nfs
= data
->nfs
;
1624 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1628 if (status
== RPC_STATUS_ERROR
) {
1629 /* flag the failure but do not invoke callback until we have received all responses */
1632 if (status
== RPC_STATUS_CANCEL
) {
1633 /* flag the cancellation but do not invoke callback until we have received all responses */
1637 if (status
== RPC_STATUS_SUCCESS
) {
1639 if (res
->status
!= NFS3_OK
) {
1640 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1643 if (res
->WRITE3res_u
.resok
.count
> 0) {
1644 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1645 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1651 if (data
->num_calls
> 0) {
1652 /* still waiting for more replies */
1657 if (data
->error
!= 0) {
1658 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1659 free_nfs_cb_data(data
);
1663 if (data
->cancel
!= 0) {
1664 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1665 free_nfs_cb_data(data
);
1670 data
->nfsfh
->offset
= data
->max_offset
;
1671 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1673 free_nfs_cb_data(data
);
1678 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
)
1680 struct nfs_cb_data
*data
;
1682 data
= malloc(sizeof(struct nfs_cb_data
));
1684 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1687 memset(data
, 0, sizeof(struct nfs_cb_data
));
1690 data
->private_data
= private_data
;
1691 data
->nfsfh
= nfsfh
;
1693 nfsfh
->offset
= offset
;
1695 if (count
<= nfs_get_writemax(nfs
)) {
1698 memset(&args
, 0, sizeof(WRITE3args
));
1699 args
.file
= nfsfh
->fh
;
1700 args
.offset
= offset
;
1702 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1703 args
.data
.data_len
= count
;
1704 args
.data
.data_val
= buf
;
1706 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_cb
, &args
, data
) != 0) {
1707 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1708 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1709 free_nfs_cb_data(data
);
1715 /* trying to write more than maximum server write size, we has to chop it up into smaller
1717 * we send all writes in parallel so that performance is still good.
1719 data
->max_offset
= offset
;
1720 data
->start_offset
= offset
;
1723 uint64_t writecount
= count
;
1724 struct nfs_mcb_data
*mdata
;
1727 if (writecount
> nfs_get_writemax(nfs
)) {
1728 writecount
= nfs_get_writemax(nfs
);
1731 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1732 if (mdata
== NULL
) {
1733 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1736 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1738 mdata
->offset
= offset
;
1739 mdata
->count
= writecount
;
1741 memset(&args
, 0, sizeof(WRITE3args
));
1742 args
.file
= nfsfh
->fh
;
1743 args
.offset
= offset
;
1744 args
.count
= writecount
;
1745 args
.stable
= nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1746 args
.data
.data_len
= writecount
;
1747 args
.data
.data_val
= &buf
[offset
- data
->start_offset
];
1749 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1750 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1751 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1756 count
-= writecount
;
1757 offset
+= writecount
;
1767 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1769 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1779 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1781 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1782 free(nfsfh
->fh
.data
.data_val
);
1783 nfsfh
->fh
.data
.data_val
= NULL
;
1787 cb(0, nfs
, NULL
, private_data
);
1798 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1800 struct nfs_cb_data
*data
;
1801 struct GETATTR3args args
;
1803 data
= malloc(sizeof(struct nfs_cb_data
));
1805 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1808 memset(data
, 0, sizeof(struct nfs_cb_data
));
1811 data
->private_data
= private_data
;
1813 memset(&args
, 0, sizeof(GETATTR3args
));
1814 args
.object
= nfsfh
->fh
;
1816 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1817 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1818 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1819 free_nfs_cb_data(data
);
1830 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1832 struct nfs_cb_data
*data
= private_data
;
1833 struct nfs_context
*nfs
= data
->nfs
;
1836 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1838 if (status
== RPC_STATUS_ERROR
) {
1839 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1840 free_nfs_cb_data(data
);
1843 if (status
== RPC_STATUS_CANCEL
) {
1844 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1845 free_nfs_cb_data(data
);
1850 if (res
->status
!= NFS3_OK
) {
1851 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1852 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1853 free_nfs_cb_data(data
);
1857 data
->cb(0, nfs
, NULL
, data
->private_data
);
1858 free_nfs_cb_data(data
);
1861 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1863 struct nfs_cb_data
*data
;
1864 struct COMMIT3args args
;
1866 data
= malloc(sizeof(struct nfs_cb_data
));
1868 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1871 memset(data
, 0, sizeof(struct nfs_cb_data
));
1874 data
->private_data
= private_data
;
1876 args
.file
= nfsfh
->fh
;
1879 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1880 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1881 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1882 free_nfs_cb_data(data
);
1894 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1896 struct nfs_cb_data
*data
= private_data
;
1897 struct nfs_context
*nfs
= data
->nfs
;
1900 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1902 if (status
== RPC_STATUS_ERROR
) {
1903 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1904 free_nfs_cb_data(data
);
1907 if (status
== RPC_STATUS_CANCEL
) {
1908 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1909 free_nfs_cb_data(data
);
1914 if (res
->status
!= NFS3_OK
) {
1915 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1916 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1917 free_nfs_cb_data(data
);
1921 data
->cb(0, nfs
, NULL
, data
->private_data
);
1922 free_nfs_cb_data(data
);
1925 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1927 struct nfs_cb_data
*data
;
1930 data
= malloc(sizeof(struct nfs_cb_data
));
1932 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1935 memset(data
, 0, sizeof(struct nfs_cb_data
));
1938 data
->private_data
= private_data
;
1940 memset(&args
, 0, sizeof(SETATTR3args
));
1941 args
.object
= nfsfh
->fh
;
1942 args
.new_attributes
.size
.set_it
= 1;
1943 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1945 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1946 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1947 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1948 free_nfs_cb_data(data
);
1958 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1960 uint64_t offset
= data
->continue_int
;
1963 nfsfh
.fh
= data
->fh
;
1965 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1966 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1967 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1968 free_nfs_cb_data(data
);
1971 free_nfs_cb_data(data
);
1975 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1981 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1982 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1995 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1998 struct nfs_cb_data
*data
= private_data
;
1999 struct nfs_context
*nfs
= data
->nfs
;
2000 char *str
= data
->continue_data
;
2002 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2004 str
= &str
[strlen(str
) + 1];
2006 if (status
== RPC_STATUS_ERROR
) {
2007 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2008 free_nfs_cb_data(data
);
2011 if (status
== RPC_STATUS_CANCEL
) {
2012 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2013 free_nfs_cb_data(data
);
2018 if (res
->status
!= NFS3_OK
) {
2019 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
));
2020 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2021 free_nfs_cb_data(data
);
2025 data
->cb(0, nfs
, NULL
, data
->private_data
);
2026 free_nfs_cb_data(data
);
2029 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2031 char *str
= data
->continue_data
;
2034 str
= &str
[strlen(str
) + 1];
2036 memset(&args
, 0, sizeof(MKDIR3args
));
2037 args
.where
.dir
= data
->fh
;
2038 args
.where
.name
= str
;
2039 args
.attributes
.mode
.set_it
= 1;
2040 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2042 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2043 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2044 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2045 free_nfs_cb_data(data
);
2051 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2056 new_path
= strdup(path
);
2057 if (new_path
== NULL
) {
2058 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2062 ptr
= strrchr(new_path
, '/');
2064 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2069 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2070 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2071 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2085 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2088 struct nfs_cb_data
*data
= private_data
;
2089 struct nfs_context
*nfs
= data
->nfs
;
2090 char *str
= data
->continue_data
;
2092 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2094 str
= &str
[strlen(str
) + 1];
2096 if (status
== RPC_STATUS_ERROR
) {
2097 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2098 free_nfs_cb_data(data
);
2101 if (status
== RPC_STATUS_CANCEL
) {
2102 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2103 free_nfs_cb_data(data
);
2108 if (res
->status
!= NFS3_OK
) {
2109 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
));
2110 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2111 free_nfs_cb_data(data
);
2115 data
->cb(0, nfs
, NULL
, data
->private_data
);
2116 free_nfs_cb_data(data
);
2119 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2121 char *str
= data
->continue_data
;
2124 str
= &str
[strlen(str
) + 1];
2126 args
.object
.dir
= data
->fh
;
2127 args
.object
.name
= str
;
2128 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2129 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2130 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2131 free_nfs_cb_data(data
);
2137 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2142 new_path
= strdup(path
);
2143 if (new_path
== NULL
) {
2144 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2148 ptr
= strrchr(new_path
, '/');
2150 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2155 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2156 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2157 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2170 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2173 struct nfs_cb_data
*data
= private_data
;
2174 struct nfs_context
*nfs
= data
->nfs
;
2175 struct nfsfh
*nfsfh
;
2176 char *str
= data
->continue_data
;
2178 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2180 if (status
== RPC_STATUS_ERROR
) {
2181 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2182 free_nfs_cb_data(data
);
2185 if (status
== RPC_STATUS_CANCEL
) {
2186 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2187 free_nfs_cb_data(data
);
2191 str
= &str
[strlen(str
) + 1];
2193 if (res
->status
!= NFS3_OK
) {
2194 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
));
2195 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2200 nfsfh
= malloc(sizeof(struct nfsfh
));
2201 if (nfsfh
== NULL
) {
2202 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2203 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2204 free_nfs_cb_data(data
);
2207 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2209 /* copy the filehandle */
2210 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2211 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2212 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2214 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2215 free_nfs_cb_data(data
);
2220 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2223 struct nfs_cb_data
*data
= private_data
;
2224 struct nfs_context
*nfs
= data
->nfs
;
2225 char *str
= data
->continue_data
;
2228 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2230 if (status
== RPC_STATUS_ERROR
) {
2231 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2232 free_nfs_cb_data(data
);
2235 if (status
== RPC_STATUS_CANCEL
) {
2236 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2237 free_nfs_cb_data(data
);
2241 str
= &str
[strlen(str
) + 1];
2243 if (res
->status
!= NFS3_OK
) {
2244 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
));
2245 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2246 free_nfs_cb_data(data
);
2250 memset(&args
, 0, sizeof(LOOKUP3args
));
2251 args
.what
.dir
= data
->fh
;
2252 args
.what
.name
= str
;
2254 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2255 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2256 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2257 free_nfs_cb_data(data
);
2263 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2265 char *str
= data
->continue_data
;
2268 str
= &str
[strlen(str
) + 1];
2270 memset(&args
, 0, sizeof(CREATE3args
));
2271 args
.where
.dir
= data
->fh
;
2272 args
.where
.name
= str
;
2273 args
.how
.mode
= UNCHECKED
;
2274 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2275 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2277 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2278 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2279 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2280 free_nfs_cb_data(data
);
2286 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2291 new_path
= strdup(path
);
2292 if (new_path
== NULL
) {
2293 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2297 ptr
= strrchr(new_path
, '/');
2299 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2304 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2305 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2306 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2319 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2322 struct nfs_cb_data
*data
= private_data
;
2323 struct nfs_context
*nfs
= data
->nfs
;
2324 char *str
= data
->continue_data
;
2326 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2328 str
= &str
[strlen(str
) + 1];
2330 if (status
== RPC_STATUS_ERROR
) {
2331 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2332 free_nfs_cb_data(data
);
2335 if (status
== RPC_STATUS_CANCEL
) {
2336 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2337 free_nfs_cb_data(data
);
2342 if (res
->status
!= NFS3_OK
) {
2343 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
));
2344 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2345 free_nfs_cb_data(data
);
2349 data
->cb(0, nfs
, NULL
, data
->private_data
);
2350 free_nfs_cb_data(data
);
2353 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2355 char *str
= data
->continue_data
;
2356 struct REMOVE3args args
;
2358 str
= &str
[strlen(str
) + 1];
2360 args
.object
.dir
= data
->fh
;
2361 args
.object
.name
= str
;
2362 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2363 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2364 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2365 free_nfs_cb_data(data
);
2371 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2376 new_path
= strdup(path
);
2377 if (new_path
== NULL
) {
2378 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2382 ptr
= strrchr(new_path
, '/');
2384 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2389 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2390 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2391 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2402 struct mknod_cb_data
{
2409 static void free_mknod_cb_data(void *ptr
)
2411 struct mknod_cb_data
*data
= ptr
;
2417 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2420 struct nfs_cb_data
*data
= private_data
;
2421 struct nfs_context
*nfs
= data
->nfs
;
2422 char *str
= data
->continue_data
;
2424 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2426 str
= &str
[strlen(str
) + 1];
2428 if (status
== RPC_STATUS_ERROR
) {
2429 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2430 free_nfs_cb_data(data
);
2433 if (status
== RPC_STATUS_CANCEL
) {
2434 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2435 free_nfs_cb_data(data
);
2440 if (res
->status
!= NFS3_OK
) {
2441 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
));
2442 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2443 free_nfs_cb_data(data
);
2447 data
->cb(0, nfs
, NULL
, data
->private_data
);
2448 free_nfs_cb_data(data
);
2451 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2453 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2454 char *str
= cb_data
->path
;
2457 str
= &str
[strlen(str
) + 1];
2459 args
.where
.dir
= data
->fh
;
2460 args
.where
.name
= str
;
2461 switch (cb_data
->mode
& S_IFMT
) {
2463 args
.what
.type
= NF3CHR
;
2464 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2465 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
& (S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
);
2466 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2467 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2470 args
.what
.type
= NF3BLK
;
2471 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2472 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
& (S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
);
2473 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2474 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2476 args
.what
.type
= NF3SOCK
;
2477 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2478 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
& (S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
);
2481 args
.what
.type
= NF3FIFO
;
2482 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2483 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_mode3_u
.mode
= cb_data
->mode
& (S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
);
2486 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2487 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2488 free_nfs_cb_data(data
);
2492 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2493 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2494 free_nfs_cb_data(data
);
2500 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2503 struct mknod_cb_data
*cb_data
;
2505 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2506 if (cb_data
== NULL
) {
2507 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2511 cb_data
->path
= strdup(path
);
2512 if (cb_data
->path
== NULL
) {
2513 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2518 ptr
= strrchr(cb_data
->path
, '/');
2520 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2525 cb_data
->mode
= mode
;
2526 cb_data
->major
= major(dev
);
2527 cb_data
->minor
= minor(dev
);
2529 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2530 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2531 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2532 free_mknod_cb_data(cb_data
);
2543 /* ReadDirPlus Emulation Callback data */
2544 struct rdpe_cb_data
{
2547 struct nfs_cb_data
*data
;
2550 /* ReadDirPlus Emulation LOOKUP Callback data */
2551 struct rdpe_lookup_cb_data
{
2552 struct rdpe_cb_data
*rdpe_cb_data
;
2553 struct nfsdirent
*nfsdirent
;
2556 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2557 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2559 LOOKUP3res
*res
= command_data
;
2560 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2561 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2562 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2563 struct nfsdir
*nfsdir
= data
->continue_data
;
2564 struct nfs_context
*nfs
= data
->nfs
;
2565 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2567 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2569 free(rdpe_lookup_cb_data
);
2571 rdpe_cb_data
->getattrcount
--;
2573 if (status
== RPC_STATUS_ERROR
) {
2574 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2576 if (status
== RPC_STATUS_CANCEL
) {
2577 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2579 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2580 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2582 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2583 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2584 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2586 nfsdirent
->type
= attributes
->type
;
2587 nfsdirent
->mode
= attributes
->mode
;
2588 nfsdirent
->size
= attributes
->size
;
2590 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2591 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2592 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2593 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2594 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2595 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2596 nfsdirent
->uid
= attributes
->uid
;
2597 nfsdirent
->gid
= attributes
->gid
;
2601 if (rdpe_cb_data
->getattrcount
== 0) {
2602 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2603 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2604 nfs_free_nfsdir(nfsdir
);
2606 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2610 data
->continue_data
= NULL
;
2611 free_nfs_cb_data(data
);
2615 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2617 READDIR3res
*res
= command_data
;
2618 struct nfs_cb_data
*data
= private_data
;
2619 struct nfs_context
*nfs
= data
->nfs
;
2620 struct nfsdir
*nfsdir
= data
->continue_data
;
2621 struct nfsdirent
*nfsdirent
;
2622 struct entry3
*entry
;
2623 uint64_t cookie
= 0;
2624 struct rdpe_cb_data
*rdpe_cb_data
;
2626 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2628 if (status
== RPC_STATUS_ERROR
) {
2629 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2630 nfs_free_nfsdir(nfsdir
);
2631 data
->continue_data
= NULL
;
2632 free_nfs_cb_data(data
);
2636 if (status
== RPC_STATUS_CANCEL
) {
2637 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2638 nfs_free_nfsdir(nfsdir
);
2639 data
->continue_data
= NULL
;
2640 free_nfs_cb_data(data
);
2644 if (res
->status
!= NFS3_OK
) {
2645 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
));
2646 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2647 nfs_free_nfsdir(nfsdir
);
2648 data
->continue_data
= NULL
;
2649 free_nfs_cb_data(data
);
2653 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2654 while (entry
!= NULL
) {
2655 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2656 if (nfsdirent
== NULL
) {
2657 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2658 nfs_free_nfsdir(nfsdir
);
2659 data
->continue_data
= NULL
;
2660 free_nfs_cb_data(data
);
2663 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2664 nfsdirent
->name
= strdup(entry
->name
);
2665 if (nfsdirent
->name
== NULL
) {
2666 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2667 nfs_free_nfsdir(nfsdir
);
2668 data
->continue_data
= NULL
;
2669 free_nfs_cb_data(data
);
2672 nfsdirent
->inode
= entry
->fileid
;
2674 nfsdirent
->next
= nfsdir
->entries
;
2675 nfsdir
->entries
= nfsdirent
;
2677 cookie
= entry
->cookie
;
2678 entry
= entry
->nextentry
;
2681 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2684 args
.dir
= data
->fh
;
2685 args
.cookie
= cookie
;
2686 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2689 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2690 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2691 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2692 nfs_free_nfsdir(nfsdir
);
2693 data
->continue_data
= NULL
;
2694 free_nfs_cb_data(data
);
2700 /* steal the dirhandle */
2701 nfsdir
->current
= nfsdir
->entries
;
2703 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2704 rdpe_cb_data
->getattrcount
= 0;
2705 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2706 rdpe_cb_data
->data
= data
;
2707 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2708 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2711 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2712 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2713 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2715 memset(&args
, 0, sizeof(LOOKUP3args
));
2716 args
.what
.dir
= data
->fh
;
2717 args
.what
.name
= nfsdirent
->name
;
2719 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2720 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2722 /* if we have already commands in flight, we cant just stop, we have to wait for the
2723 * commands in flight to complete
2725 if (rdpe_cb_data
->getattrcount
> 0) {
2726 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2727 free(rdpe_lookup_cb_data
);
2731 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2732 nfs_free_nfsdir(nfsdir
);
2733 data
->continue_data
= NULL
;
2734 free_nfs_cb_data(data
);
2735 free(rdpe_lookup_cb_data
);
2739 rdpe_cb_data
->getattrcount
++;
2744 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2746 READDIRPLUS3res
*res
= command_data
;
2747 struct nfs_cb_data
*data
= private_data
;
2748 struct nfs_context
*nfs
= data
->nfs
;
2749 struct nfsdir
*nfsdir
= data
->continue_data
;
2750 struct entryplus3
*entry
;
2751 uint64_t cookie
= 0;
2753 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2755 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2758 args
.dir
= data
->fh
;
2759 args
.cookie
= cookie
;
2760 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2763 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2764 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2765 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2766 nfs_free_nfsdir(nfsdir
);
2767 data
->continue_data
= NULL
;
2768 free_nfs_cb_data(data
);
2774 if (status
== RPC_STATUS_CANCEL
) {
2775 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2776 nfs_free_nfsdir(nfsdir
);
2777 data
->continue_data
= NULL
;
2778 free_nfs_cb_data(data
);
2782 if (res
->status
!= NFS3_OK
) {
2783 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
));
2784 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2785 nfs_free_nfsdir(nfsdir
);
2786 data
->continue_data
= NULL
;
2787 free_nfs_cb_data(data
);
2791 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2792 while (entry
!= NULL
) {
2793 struct nfsdirent
*nfsdirent
;
2795 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2796 if (nfsdirent
== NULL
) {
2797 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2798 nfs_free_nfsdir(nfsdir
);
2799 data
->continue_data
= NULL
;
2800 free_nfs_cb_data(data
);
2803 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2804 nfsdirent
->name
= strdup(entry
->name
);
2805 if (nfsdirent
->name
== NULL
) {
2806 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2807 nfs_free_nfsdir(nfsdir
);
2808 data
->continue_data
= NULL
;
2809 free_nfs_cb_data(data
);
2812 nfsdirent
->inode
= entry
->fileid
;
2813 if (entry
->name_attributes
.attributes_follow
) {
2814 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2815 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2816 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2818 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2819 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2820 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2821 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2822 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2823 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2824 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2825 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2828 nfsdirent
->next
= nfsdir
->entries
;
2829 nfsdir
->entries
= nfsdirent
;
2831 cookie
= entry
->cookie
;
2832 entry
= entry
->nextentry
;
2835 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2836 READDIRPLUS3args args
;
2838 args
.dir
= data
->fh
;
2839 args
.cookie
= cookie
;
2840 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2841 args
.dircount
= 8192;
2842 args
.maxcount
= 8192;
2844 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2845 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2846 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2847 nfs_free_nfsdir(nfsdir
);
2848 data
->continue_data
= NULL
;
2849 free_nfs_cb_data(data
);
2855 /* steal the dirhandle */
2856 data
->continue_data
= NULL
;
2857 nfsdir
->current
= nfsdir
->entries
;
2859 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2860 free_nfs_cb_data(data
);
2863 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2865 READDIRPLUS3args args
;
2867 args
.dir
= data
->fh
;
2869 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2870 args
.dircount
= 8192;
2871 args
.maxcount
= 8192;
2872 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2873 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2874 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2875 free_nfs_cb_data(data
);
2881 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2883 struct nfsdir
*nfsdir
;
2885 nfsdir
= malloc(sizeof(struct nfsdir
));
2886 if (nfsdir
== NULL
) {
2887 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2890 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2892 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2893 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2901 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2903 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2905 if (nfsdir
->current
!= NULL
) {
2906 nfsdir
->current
= nfsdir
->current
->next
;
2915 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2917 nfs_free_nfsdir(nfsdir
);
2924 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
2935 struct lseek_cb_data
{
2936 struct nfs_context
*nfs
;
2937 struct nfsfh
*nfsfh
;
2943 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2946 struct lseek_cb_data
*data
= private_data
;
2947 struct nfs_context
*nfs
= data
->nfs
;
2949 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2951 if (status
== RPC_STATUS_ERROR
) {
2952 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2956 if (status
== RPC_STATUS_CANCEL
) {
2957 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2963 if (res
->status
!= NFS3_OK
) {
2964 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2965 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2970 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2971 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2975 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2977 struct lseek_cb_data
*data
;
2978 struct GETATTR3args args
;
2980 if (whence
== SEEK_SET
) {
2981 nfsfh
->offset
= offset
;
2982 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2985 if (whence
== SEEK_CUR
) {
2986 nfsfh
->offset
+= offset
;
2987 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2991 data
= malloc(sizeof(struct lseek_cb_data
));
2993 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2998 data
->nfsfh
= nfsfh
;
2999 data
->offset
= offset
;
3001 data
->private_data
= private_data
;
3003 memset(&args
, 0, sizeof(GETATTR3args
));
3004 args
.object
= nfsfh
->fh
;
3006 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3007 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3020 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3023 struct nfs_cb_data
*data
= private_data
;
3024 struct nfs_context
*nfs
= data
->nfs
;
3025 struct statvfs svfs
;
3027 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3029 if (status
== RPC_STATUS_ERROR
) {
3030 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3031 free_nfs_cb_data(data
);
3034 if (status
== RPC_STATUS_CANCEL
) {
3035 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3036 free_nfs_cb_data(data
);
3041 if (res
->status
!= NFS3_OK
) {
3042 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
));
3043 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3044 free_nfs_cb_data(data
);
3048 svfs
.f_bsize
= 4096;
3049 svfs
.f_frsize
= 4096;
3050 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3051 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3052 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3053 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3054 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3055 #if !defined(ANDROID)
3056 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3059 svfs
.f_namemax
= 256;
3062 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3063 free_nfs_cb_data(data
);
3066 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3070 args
.fsroot
= data
->fh
;
3071 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3072 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3073 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3074 free_nfs_cb_data(data
);
3080 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3082 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3083 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3096 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3099 struct nfs_cb_data
*data
= private_data
;
3100 struct nfs_context
*nfs
= data
->nfs
;
3102 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3104 if (status
== RPC_STATUS_ERROR
) {
3105 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3106 free_nfs_cb_data(data
);
3109 if (status
== RPC_STATUS_CANCEL
) {
3110 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3111 free_nfs_cb_data(data
);
3116 if (res
->status
!= NFS3_OK
) {
3117 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
));
3118 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3119 free_nfs_cb_data(data
);
3124 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3125 free_nfs_cb_data(data
);
3128 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3132 args
.symlink
= data
->fh
;
3134 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3135 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3136 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3137 free_nfs_cb_data(data
);
3143 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3145 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3146 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3159 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3161 struct nfs_cb_data
*data
= private_data
;
3162 struct nfs_context
*nfs
= data
->nfs
;
3165 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3167 if (status
== RPC_STATUS_ERROR
) {
3168 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3169 free_nfs_cb_data(data
);
3172 if (status
== RPC_STATUS_CANCEL
) {
3173 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3174 free_nfs_cb_data(data
);
3179 if (res
->status
!= NFS3_OK
) {
3180 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3181 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3182 free_nfs_cb_data(data
);
3186 data
->cb(0, nfs
, NULL
, data
->private_data
);
3187 free_nfs_cb_data(data
);
3190 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3194 memset(&args
, 0, sizeof(SETATTR3args
));
3195 args
.object
= data
->fh
;
3196 args
.new_attributes
.mode
.set_it
= 1;
3197 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3199 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3200 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3201 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3202 free_nfs_cb_data(data
);
3209 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3211 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3212 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3222 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3224 struct nfs_cb_data
*data
;
3226 data
= malloc(sizeof(struct nfs_cb_data
));
3228 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3231 memset(data
, 0, sizeof(struct nfs_cb_data
));
3234 data
->private_data
= private_data
;
3235 data
->continue_int
= mode
;
3236 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3237 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3238 if (data
->fh
.data
.data_val
== NULL
) {
3239 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3240 free_nfs_cb_data(data
);
3243 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3245 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3246 free_nfs_cb_data(data
);
3258 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3260 struct nfs_cb_data
*data
= private_data
;
3261 struct nfs_context
*nfs
= data
->nfs
;
3264 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3266 if (status
== RPC_STATUS_ERROR
) {
3267 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3268 free_nfs_cb_data(data
);
3271 if (status
== RPC_STATUS_CANCEL
) {
3272 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3273 free_nfs_cb_data(data
);
3278 if (res
->status
!= NFS3_OK
) {
3279 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3280 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3281 free_nfs_cb_data(data
);
3285 data
->cb(0, nfs
, NULL
, data
->private_data
);
3286 free_nfs_cb_data(data
);
3289 struct nfs_chown_data
{
3294 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3297 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3299 memset(&args
, 0, sizeof(SETATTR3args
));
3300 args
.object
= data
->fh
;
3301 if (chown_data
->uid
!= (uid_t
)-1) {
3302 args
.new_attributes
.uid
.set_it
= 1;
3303 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3305 if (chown_data
->gid
!= (gid_t
)-1) {
3306 args
.new_attributes
.gid
.set_it
= 1;
3307 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3310 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3311 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3312 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3313 free_nfs_cb_data(data
);
3320 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3322 struct nfs_chown_data
*chown_data
;
3324 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3325 if (chown_data
== NULL
) {
3326 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3330 chown_data
->uid
= uid
;
3331 chown_data
->gid
= gid
;
3333 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3334 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3345 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3347 struct nfs_cb_data
*data
;
3348 struct nfs_chown_data
*chown_data
;
3350 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3351 if (chown_data
== NULL
) {
3352 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3356 chown_data
->uid
= uid
;
3357 chown_data
->gid
= gid
;
3360 data
= malloc(sizeof(struct nfs_cb_data
));
3362 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3365 memset(data
, 0, sizeof(struct nfs_cb_data
));
3368 data
->private_data
= private_data
;
3369 data
->continue_data
= chown_data
;
3370 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3371 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3372 if (data
->fh
.data
.data_val
== NULL
) {
3373 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3374 free_nfs_cb_data(data
);
3377 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3380 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3381 free_nfs_cb_data(data
);
3395 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3397 struct nfs_cb_data
*data
= private_data
;
3398 struct nfs_context
*nfs
= data
->nfs
;
3401 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3403 if (status
== RPC_STATUS_ERROR
) {
3404 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3405 free_nfs_cb_data(data
);
3408 if (status
== RPC_STATUS_CANCEL
) {
3409 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3410 free_nfs_cb_data(data
);
3415 if (res
->status
!= NFS3_OK
) {
3416 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3417 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3418 free_nfs_cb_data(data
);
3422 data
->cb(0, nfs
, NULL
, data
->private_data
);
3423 free_nfs_cb_data(data
);
3426 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3429 struct timeval
*utimes_data
= data
->continue_data
;
3431 memset(&args
, 0, sizeof(SETATTR3args
));
3432 args
.object
= data
->fh
;
3433 if (utimes_data
!= NULL
) {
3434 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3435 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3436 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3437 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3438 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3439 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3441 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3442 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3445 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3446 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3447 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3448 free_nfs_cb_data(data
);
3455 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3457 struct timeval
*new_times
= NULL
;
3459 if (times
!= NULL
) {
3460 new_times
= malloc(sizeof(struct timeval
)*2);
3461 if (new_times
== NULL
) {
3462 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3466 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3469 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3470 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3480 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3482 struct timeval
*new_times
= NULL
;
3484 if (times
!= NULL
) {
3485 new_times
= malloc(sizeof(struct timeval
)*2);
3486 if (new_times
== NULL
) {
3487 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3491 new_times
[0].tv_sec
= times
->actime
;
3492 new_times
[0].tv_usec
= 0;
3493 new_times
[1].tv_sec
= times
->modtime
;
3494 new_times
[1].tv_usec
= 0;
3497 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3498 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3509 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3512 struct nfs_cb_data
*data
= private_data
;
3513 struct nfs_context
*nfs
= data
->nfs
;
3514 unsigned int nfsmode
= 0;
3516 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3518 if (status
== RPC_STATUS_ERROR
) {
3519 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3520 free_nfs_cb_data(data
);
3523 if (status
== RPC_STATUS_CANCEL
) {
3524 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3525 free_nfs_cb_data(data
);
3530 if (res
->status
!= NFS3_OK
) {
3531 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
));
3532 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3533 free_nfs_cb_data(data
);
3537 if (data
->continue_int
& R_OK
) {
3538 nfsmode
|= ACCESS3_READ
;
3540 if (data
->continue_int
& W_OK
) {
3541 nfsmode
|= ACCESS3_MODIFY
;
3543 if (data
->continue_int
& X_OK
) {
3544 nfsmode
|= ACCESS3_EXECUTE
;
3547 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3548 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3549 nfsmode
&ACCESS3_READ
?'r':'-',
3550 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3551 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3552 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3553 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3554 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3555 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3556 free_nfs_cb_data(data
);
3560 data
->cb(0, nfs
, NULL
, data
->private_data
);
3561 free_nfs_cb_data(data
);
3564 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3569 if (data
->continue_int
& R_OK
) {
3570 nfsmode
|= ACCESS3_READ
;
3572 if (data
->continue_int
& W_OK
) {
3573 nfsmode
|= ACCESS3_MODIFY
;
3575 if (data
->continue_int
& X_OK
) {
3576 nfsmode
|= ACCESS3_EXECUTE
;
3579 memset(&args
, 0, sizeof(ACCESS3args
));
3580 args
.object
= data
->fh
;
3581 args
.access
= nfsmode
;
3583 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3584 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3585 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3586 free_nfs_cb_data(data
);
3592 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3594 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3595 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3607 struct nfs_symlink_data
{
3609 char *newpathparent
;
3610 char *newpathobject
;
3613 static void free_nfs_symlink_data(void *mem
)
3615 struct nfs_symlink_data
*data
= mem
;
3617 if (data
->oldpath
!= NULL
) {
3618 free(data
->oldpath
);
3620 if (data
->newpathparent
!= NULL
) {
3621 free(data
->newpathparent
);
3623 if (data
->newpathobject
!= NULL
) {
3624 free(data
->newpathobject
);
3629 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3632 struct nfs_cb_data
*data
= private_data
;
3633 struct nfs_context
*nfs
= data
->nfs
;
3634 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3636 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3638 if (status
== RPC_STATUS_ERROR
) {
3639 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3640 free_nfs_cb_data(data
);
3643 if (status
== RPC_STATUS_CANCEL
) {
3644 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3645 free_nfs_cb_data(data
);
3650 if (res
->status
!= NFS3_OK
) {
3651 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
));
3652 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3653 free_nfs_cb_data(data
);
3657 data
->cb(0, nfs
, NULL
, data
->private_data
);
3658 free_nfs_cb_data(data
);
3661 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3663 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3666 memset(&args
, 0, sizeof(SYMLINK3args
));
3667 args
.where
.dir
= data
->fh
;
3668 args
.where
.name
= symlink_data
->newpathobject
;
3669 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3670 args
.symlink
.symlink_attributes
.mode
.set_mode3_u
.mode
= S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
;
3671 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3673 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3674 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3675 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3676 free_nfs_cb_data(data
);
3682 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3685 struct nfs_symlink_data
*symlink_data
;
3687 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3688 if (symlink_data
== NULL
) {
3689 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3692 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3694 symlink_data
->oldpath
= strdup(oldpath
);
3695 if (symlink_data
->oldpath
== NULL
) {
3696 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3697 free_nfs_symlink_data(symlink_data
);
3701 symlink_data
->newpathparent
= strdup(newpath
);
3702 if (symlink_data
->newpathparent
== NULL
) {
3703 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3704 free_nfs_symlink_data(symlink_data
);
3708 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3710 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3711 free_nfs_symlink_data(symlink_data
);
3717 symlink_data
->newpathobject
= strdup(ptr
);
3718 if (symlink_data
->newpathobject
== NULL
) {
3719 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3720 free_nfs_symlink_data(symlink_data
);
3724 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3725 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3737 struct nfs_rename_data
{
3740 struct nfs_fh3 olddir
;
3743 struct nfs_fh3 newdir
;
3746 static void free_nfs_rename_data(void *mem
)
3748 struct nfs_rename_data
*data
= mem
;
3750 if (data
->oldpath
!= NULL
) {
3751 free(data
->oldpath
);
3753 if (data
->olddir
.data
.data_val
!= NULL
) {
3754 free(data
->olddir
.data
.data_val
);
3756 if (data
->newpath
!= NULL
) {
3757 free(data
->newpath
);
3759 if (data
->newdir
.data
.data_val
!= NULL
) {
3760 free(data
->newdir
.data
.data_val
);
3765 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3768 struct nfs_cb_data
*data
= private_data
;
3769 struct nfs_context
*nfs
= data
->nfs
;
3770 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3772 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3774 if (status
== RPC_STATUS_ERROR
) {
3775 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3776 free_nfs_cb_data(data
);
3779 if (status
== RPC_STATUS_CANCEL
) {
3780 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3781 free_nfs_cb_data(data
);
3786 if (res
->status
!= NFS3_OK
) {
3787 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
));
3788 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3789 free_nfs_cb_data(data
);
3793 data
->cb(0, nfs
, NULL
, data
->private_data
);
3794 free_nfs_cb_data(data
);
3797 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3799 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3802 /* steal the filehandle */
3803 rename_data
->newdir
= data
->fh
;
3804 data
->fh
.data
.data_val
= NULL
;
3806 args
.from
.dir
= rename_data
->olddir
;
3807 args
.from
.name
= rename_data
->oldobject
;
3808 args
.to
.dir
= rename_data
->newdir
;
3809 args
.to
.name
= rename_data
->newobject
;
3810 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3811 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3812 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3813 free_nfs_cb_data(data
);
3820 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3822 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3824 /* steal the filehandle */
3825 rename_data
->olddir
= data
->fh
;
3826 data
->fh
.data
.data_val
= NULL
;
3828 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) {
3829 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3830 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3831 free_nfs_cb_data(data
);
3834 data
->continue_data
= NULL
;
3835 free_nfs_cb_data(data
);
3841 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3844 struct nfs_rename_data
*rename_data
;
3846 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3847 if (rename_data
== NULL
) {
3848 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3851 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3853 rename_data
->oldpath
= strdup(oldpath
);
3854 if (rename_data
->oldpath
== NULL
) {
3855 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3856 free_nfs_rename_data(rename_data
);
3859 ptr
= strrchr(rename_data
->oldpath
, '/');
3861 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3862 free_nfs_rename_data(rename_data
);
3867 rename_data
->oldobject
= ptr
;
3870 rename_data
->newpath
= strdup(newpath
);
3871 if (rename_data
->newpath
== NULL
) {
3872 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3873 free_nfs_rename_data(rename_data
);
3876 ptr
= strrchr(rename_data
->newpath
, '/');
3878 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3879 free_nfs_rename_data(rename_data
);
3884 rename_data
->newobject
= ptr
;
3887 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3888 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3899 struct nfs_link_data
{
3901 struct nfs_fh3 oldfh
;
3904 struct nfs_fh3 newdir
;
3907 static void free_nfs_link_data(void *mem
)
3909 struct nfs_link_data
*data
= mem
;
3911 if (data
->oldpath
!= NULL
) {
3912 free(data
->oldpath
);
3914 if (data
->oldfh
.data
.data_val
!= NULL
) {
3915 free(data
->oldfh
.data
.data_val
);
3917 if (data
->newpath
!= NULL
) {
3918 free(data
->newpath
);
3920 if (data
->newdir
.data
.data_val
!= NULL
) {
3921 free(data
->newdir
.data
.data_val
);
3926 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3929 struct nfs_cb_data
*data
= private_data
;
3930 struct nfs_context
*nfs
= data
->nfs
;
3931 struct nfs_link_data
*link_data
= data
->continue_data
;
3933 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3935 if (status
== RPC_STATUS_ERROR
) {
3936 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3937 free_nfs_cb_data(data
);
3940 if (status
== RPC_STATUS_CANCEL
) {
3941 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3942 free_nfs_cb_data(data
);
3947 if (res
->status
!= NFS3_OK
) {
3948 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
));
3949 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3950 free_nfs_cb_data(data
);
3954 data
->cb(0, nfs
, NULL
, data
->private_data
);
3955 free_nfs_cb_data(data
);
3958 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3960 struct nfs_link_data
*link_data
= data
->continue_data
;
3963 /* steal the filehandle */
3964 link_data
->newdir
= data
->fh
;
3965 data
->fh
.data
.data_val
= NULL
;
3967 memset(&args
, 0, sizeof(LINK3args
));
3968 args
.file
= link_data
->oldfh
;
3969 args
.link
.dir
= link_data
->newdir
;
3970 args
.link
.name
= link_data
->newobject
;
3971 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
3972 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3973 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3974 free_nfs_cb_data(data
);
3981 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3983 struct nfs_link_data
*link_data
= data
->continue_data
;
3985 /* steal the filehandle */
3986 link_data
->oldfh
= data
->fh
;
3987 data
->fh
.data
.data_val
= NULL
;
3989 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) {
3990 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3991 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3992 free_nfs_cb_data(data
);
3995 data
->continue_data
= NULL
;
3996 free_nfs_cb_data(data
);
4002 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4005 struct nfs_link_data
*link_data
;
4007 link_data
= malloc(sizeof(struct nfs_link_data
));
4008 if (link_data
== NULL
) {
4009 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4012 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4014 link_data
->oldpath
= strdup(oldpath
);
4015 if (link_data
->oldpath
== NULL
) {
4016 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4017 free_nfs_link_data(link_data
);
4021 link_data
->newpath
= strdup(newpath
);
4022 if (link_data
->newpath
== NULL
) {
4023 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4024 free_nfs_link_data(link_data
);
4027 ptr
= strrchr(link_data
->newpath
, '/');
4029 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4030 free_nfs_link_data(link_data
);
4035 link_data
->newobject
= ptr
;
4038 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4039 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4047 //qqq replace later with lseek()
4048 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4050 return nfsfh
->offset
;
4056 * Get the maximum supported READ3 size by the server
4058 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4060 return nfs
->readmax
;
4064 * Get the maximum supported WRITE3 size by the server
4066 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4068 return nfs
->writemax
;
4071 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4072 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4075 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4076 rpc_set_uid(nfs
->rpc
, uid
);
4079 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4080 rpc_set_gid(nfs
->rpc
, gid
);
4083 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4088 va_start(ap
, error_string
);
4090 vsnprintf(str
, 1024, error_string
, ap
);
4091 if (nfs
->rpc
->error_string
!= NULL
) {
4092 free(nfs
->rpc
->error_string
);
4094 nfs
->rpc
->error_string
= str
;
4100 struct mount_cb_data
{
4106 static void free_mount_cb_data(struct mount_cb_data
*data
)
4108 if (data
->server
!= NULL
) {
4110 data
->server
= NULL
;
4116 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4118 struct mount_cb_data
*data
= private_data
;
4120 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4122 if (status
== RPC_STATUS_ERROR
) {
4123 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4124 free_mount_cb_data(data
);
4127 if (status
== RPC_STATUS_CANCEL
) {
4128 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4129 free_mount_cb_data(data
);
4133 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4134 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4135 rpc_set_error(rpc
, "Failed to disconnect\n");
4137 free_mount_cb_data(data
);
4140 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4142 struct mount_cb_data
*data
= private_data
;
4144 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4146 /* Dont want any more callbacks even if the socket is closed */
4147 rpc
->connect_cb
= NULL
;
4149 if (status
== RPC_STATUS_ERROR
) {
4150 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4151 free_mount_cb_data(data
);
4154 if (status
== RPC_STATUS_CANCEL
) {
4155 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4156 free_mount_cb_data(data
);
4160 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4161 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4162 free_mount_cb_data(data
);
4167 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4169 struct mount_cb_data
*data
= private_data
;
4170 uint32_t mount_port
;
4172 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4174 if (status
== RPC_STATUS_ERROR
) {
4175 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4176 free_mount_cb_data(data
);
4179 if (status
== RPC_STATUS_CANCEL
) {
4180 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4181 free_mount_cb_data(data
);
4185 mount_port
= *(uint32_t *)command_data
;
4186 if (mount_port
== 0) {
4187 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4188 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4189 free_mount_cb_data(data
);
4193 rpc_disconnect(rpc
, "normal disconnect");
4194 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4195 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4196 free_mount_cb_data(data
);
4201 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4203 struct mount_cb_data
*data
= private_data
;
4205 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4207 if (status
== RPC_STATUS_ERROR
) {
4208 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4209 free_mount_cb_data(data
);
4212 if (status
== RPC_STATUS_CANCEL
) {
4213 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4214 free_mount_cb_data(data
);
4218 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4219 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4220 free_mount_cb_data(data
);
4225 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4227 struct mount_cb_data
*data
= private_data
;
4229 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4231 /* Dont want any more callbacks even if the socket is closed */
4232 rpc
->connect_cb
= NULL
;
4234 if (status
== RPC_STATUS_ERROR
) {
4235 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4236 free_mount_cb_data(data
);
4239 if (status
== RPC_STATUS_CANCEL
) {
4240 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4241 free_mount_cb_data(data
);
4245 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4246 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4247 free_mount_cb_data(data
);
4252 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4254 struct mount_cb_data
*data
;
4256 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4258 data
= malloc(sizeof(struct mount_cb_data
));
4262 memset(data
, 0, sizeof(struct mount_cb_data
));
4264 data
->private_data
= private_data
;
4265 data
->server
= strdup(server
);
4266 if (data
->server
== NULL
) {
4267 free_mount_cb_data(data
);
4270 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4271 free_mount_cb_data(data
);
4278 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4280 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4284 const char *nfs_get_server(struct nfs_context
*nfs
) {
4288 const char *nfs_get_export(struct nfs_context
*nfs
) {
4292 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4293 return &nfs
->rootfh
;
4296 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {