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. */
135 uint64_t start_offset
, max_offset
;
141 struct nfs_mcb_data
{
142 struct nfs_cb_data
*data
;
147 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
149 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
151 rpc_set_auth(nfs
->rpc
, auth
);
154 int nfs_get_fd(struct nfs_context
*nfs
)
156 return rpc_get_fd(nfs
->rpc
);
159 int nfs_queue_length(struct nfs_context
*nfs
)
161 return rpc_queue_length(nfs
->rpc
);
164 int nfs_which_events(struct nfs_context
*nfs
)
166 return rpc_which_events(nfs
->rpc
);
169 int nfs_service(struct nfs_context
*nfs
, int revents
)
171 return rpc_service(nfs
->rpc
, revents
);
174 char *nfs_get_error(struct nfs_context
*nfs
)
176 return rpc_get_error(nfs
->rpc
);
179 static int nfs_set_context_args(struct nfs_context
*nfs
, char *arg
, char *val
)
181 if (!strncmp(arg
, "tcp-syncnt", 10)) {
182 rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs
), atoi(val
));
183 } else if (!strncmp(arg
, "uid", 3)) {
184 rpc_set_uid(nfs_get_rpc_context(nfs
), atoi(val
));
185 } else if (!strncmp(arg
, "gid", 3)) {
186 rpc_set_gid(nfs_get_rpc_context(nfs
), atoi(val
));
191 static struct nfs_url
*nfs_parse_url(struct nfs_context
*nfs
, const char *url
, int dir
, int incomplete
)
193 struct nfs_url
*urls
;
194 char *strp
, *flagsp
, *strp2
;
196 if (strncmp(url
, "nfs://", 6)) {
197 rpc_set_error(nfs
->rpc
, "Invalid URL specified");
201 urls
= malloc(sizeof(struct nfs_url
));
203 rpc_set_error(nfs
->rpc
, "Out of memory");
207 memset(urls
, 0x00, sizeof(struct nfs_url
));
208 urls
->server
= strdup(url
+ 6);
209 if (urls
->server
== NULL
) {
210 nfs_destroy_url(urls
);
211 rpc_set_error(nfs
->rpc
, "Out of memory");
215 if (urls
->server
[0] == '/' || urls
->server
[0] == '\0' ||
216 urls
->server
[0] == '?') {
218 flagsp
= strchr(urls
->server
, '?');
221 nfs_destroy_url(urls
);
222 rpc_set_error(nfs
->rpc
, "Invalid server string");
226 strp
= strchr(urls
->server
, '/');
229 flagsp
= strchr(urls
->server
, '?');
232 nfs_destroy_url(urls
);
233 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
237 urls
->path
= strdup(strp
);
238 if (urls
->path
== NULL
) {
239 nfs_destroy_url(urls
);
240 rpc_set_error(nfs
->rpc
, "Out of memory");
246 flagsp
= strchr(urls
->path
, '?');
250 strp
= strrchr(urls
->path
, '/');
253 flagsp
= strchr(urls
->path
, '?');
256 nfs_destroy_url(urls
);
257 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
260 urls
->file
= strdup(strp
);
261 if (urls
->path
== NULL
) {
262 nfs_destroy_url(urls
);
263 rpc_set_error(nfs
->rpc
, "Out of memory");
267 flagsp
= strchr(urls
->file
, '?');
274 if (urls
->file
&& !strlen(urls
->file
)) {
278 nfs_destroy_url(urls
);
279 rpc_set_error(nfs
->rpc
, "Incomplete or invalid URL specified.");
284 while (flagsp
!= NULL
&& *(flagsp
+1) != 0) {
286 flagsp
= strchr(strp
, '&');
290 strp2
= strchr(strp
, '=');
294 nfs_set_context_args(nfs
, strp
, strp2
);
298 if (urls
->server
&& strlen(urls
->server
) <= 1) {
306 struct nfs_url
*nfs_parse_url_full(struct nfs_context
*nfs
, const char *url
)
308 return nfs_parse_url(nfs
, url
, 0, 0);
311 struct nfs_url
*nfs_parse_url_dir(struct nfs_context
*nfs
, const char *url
)
313 return nfs_parse_url(nfs
, url
, 1, 0);
316 struct nfs_url
*nfs_parse_url_incomplete(struct nfs_context
*nfs
, const char *url
)
318 return nfs_parse_url(nfs
, url
, 0, 1);
322 void nfs_destroy_url(struct nfs_url
*url
)
332 struct nfs_context
*nfs_init_context(void)
334 struct nfs_context
*nfs
;
336 nfs
= malloc(sizeof(struct nfs_context
));
340 memset(nfs
, 0, sizeof(struct nfs_context
));
342 nfs
->rpc
= rpc_init_context();
343 if (nfs
->rpc
== NULL
) {
348 nfs
->cwd
= strdup("/");
353 void nfs_destroy_context(struct nfs_context
*nfs
)
355 rpc_destroy_context(nfs
->rpc
);
373 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
374 free(nfs
->rootfh
.data
.data_val
);
375 nfs
->rootfh
.data
.data_val
= NULL
;
390 void free_rpc_cb_data(struct rpc_cb_data
*data
)
397 static void rpc_connect_program_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
399 struct rpc_cb_data
*data
= private_data
;
401 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
403 /* Dont want any more callbacks even if the socket is closed */
404 rpc
->connect_cb
= NULL
;
406 if (status
== RPC_STATUS_ERROR
) {
407 data
->cb(rpc
, status
, command_data
, data
->private_data
);
408 free_rpc_cb_data(data
);
411 if (status
== RPC_STATUS_CANCEL
) {
412 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
413 free_rpc_cb_data(data
);
417 data
->cb(rpc
, status
, NULL
, data
->private_data
);
418 free_rpc_cb_data(data
);
421 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
423 struct rpc_cb_data
*data
= private_data
;
425 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
427 /* Dont want any more callbacks even if the socket is closed */
428 rpc
->connect_cb
= NULL
;
430 if (status
== RPC_STATUS_ERROR
) {
431 data
->cb(rpc
, status
, command_data
, data
->private_data
);
432 free_rpc_cb_data(data
);
435 if (status
== RPC_STATUS_CANCEL
) {
436 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
437 free_rpc_cb_data(data
);
441 switch (data
->program
) {
443 if (rpc_mount3_null_async(rpc
, rpc_connect_program_5_cb
,
445 data
->cb(rpc
, status
, command_data
, data
->private_data
);
446 free_rpc_cb_data(data
);
451 if (rpc_nfs3_null_async(rpc
, rpc_connect_program_5_cb
,
453 data
->cb(rpc
, status
, command_data
, data
->private_data
);
454 free_rpc_cb_data(data
);
460 data
->cb(rpc
, status
, NULL
, data
->private_data
);
461 free_rpc_cb_data(data
);
464 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
466 struct rpc_cb_data
*data
= private_data
;
469 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
471 if (status
== RPC_STATUS_ERROR
) {
472 data
->cb(rpc
, status
, command_data
, data
->private_data
);
473 free_rpc_cb_data(data
);
476 if (status
== RPC_STATUS_CANCEL
) {
477 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
478 free_rpc_cb_data(data
);
482 rpc_port
= *(uint32_t *)command_data
;
484 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
485 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
486 free_rpc_cb_data(data
);
490 rpc_disconnect(rpc
, "normal disconnect");
491 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
492 data
->cb(rpc
, status
, command_data
, data
->private_data
);
493 free_rpc_cb_data(data
);
498 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
500 struct rpc_cb_data
*data
= private_data
;
502 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
504 if (status
== RPC_STATUS_ERROR
) {
505 data
->cb(rpc
, status
, command_data
, data
->private_data
);
506 free_rpc_cb_data(data
);
509 if (status
== RPC_STATUS_CANCEL
) {
510 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
511 free_rpc_cb_data(data
);
515 if (rpc_pmap2_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
516 data
->cb(rpc
, status
, command_data
, data
->private_data
);
517 free_rpc_cb_data(data
);
522 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
524 struct rpc_cb_data
*data
= private_data
;
526 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
528 /* Dont want any more callbacks even if the socket is closed */
529 rpc
->connect_cb
= NULL
;
531 if (status
== RPC_STATUS_ERROR
) {
532 data
->cb(rpc
, status
, command_data
, data
->private_data
);
533 free_rpc_cb_data(data
);
536 if (status
== RPC_STATUS_CANCEL
) {
537 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
538 free_rpc_cb_data(data
);
542 if (rpc_pmap2_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
543 data
->cb(rpc
, status
, command_data
, data
->private_data
);
544 free_rpc_cb_data(data
);
549 int rpc_connect_program_async(struct rpc_context
*rpc
, const char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
551 struct rpc_cb_data
*data
;
553 data
= malloc(sizeof(struct rpc_cb_data
));
557 memset(data
, 0, sizeof(struct rpc_cb_data
));
558 data
->server
= strdup(server
);
559 data
->program
= program
;
560 data
->version
= version
;
563 data
->private_data
= private_data
;
565 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
566 rpc_set_error(rpc
, "Failed to start connection");
567 free_rpc_cb_data(data
);
573 static void free_nfs_cb_data(struct nfs_cb_data
*data
)
575 if (data
->continue_data
!= NULL
) {
576 assert(data
->free_continue_data
);
577 data
->free_continue_data(data
->continue_data
);
580 free(data
->saved_path
);
581 free(data
->fh
.data
.data_val
);
591 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
593 struct nfs_cb_data
*data
= private_data
;
594 struct nfs_context
*nfs
= data
->nfs
;
596 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
598 if (status
== RPC_STATUS_ERROR
) {
599 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
600 free_nfs_cb_data(data
);
603 if (status
== RPC_STATUS_CANCEL
) {
604 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
605 free_nfs_cb_data(data
);
609 data
->cb(0, nfs
, NULL
, data
->private_data
);
610 free_nfs_cb_data(data
);
613 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
615 struct nfs_cb_data
*data
= private_data
;
616 struct nfs_context
*nfs
= data
->nfs
;
617 FSINFO3res
*res
= command_data
;
618 struct GETATTR3args args
;
620 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
622 if (status
== RPC_STATUS_ERROR
) {
623 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
624 free_nfs_cb_data(data
);
627 if (status
== RPC_STATUS_CANCEL
) {
628 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
629 free_nfs_cb_data(data
);
633 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
634 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
636 memset(&args
, 0, sizeof(GETATTR3args
));
637 args
.object
= nfs
->rootfh
;
639 if (rpc_nfs3_getattr_async(rpc
, nfs_mount_10_cb
, &args
, data
) != 0) {
640 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
641 free_nfs_cb_data(data
);
646 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
648 struct nfs_cb_data
*data
= private_data
;
649 struct nfs_context
*nfs
= data
->nfs
;
650 struct FSINFO3args args
;
652 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
654 if (status
== RPC_STATUS_ERROR
) {
655 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
656 free_nfs_cb_data(data
);
659 if (status
== RPC_STATUS_CANCEL
) {
660 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
661 free_nfs_cb_data(data
);
665 args
.fsroot
= nfs
->rootfh
;
666 if (rpc_nfs3_fsinfo_async(rpc
, nfs_mount_9_cb
, &args
, data
) != 0) {
667 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
668 free_nfs_cb_data(data
);
673 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
675 struct nfs_cb_data
*data
= private_data
;
676 struct nfs_context
*nfs
= data
->nfs
;
679 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
681 if (status
== RPC_STATUS_ERROR
) {
682 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
683 free_nfs_cb_data(data
);
686 if (status
== RPC_STATUS_CANCEL
) {
687 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
688 free_nfs_cb_data(data
);
693 if (res
->fhs_status
!= MNT3_OK
) {
694 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
));
695 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
696 free_nfs_cb_data(data
);
700 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
701 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
702 if (nfs
->rootfh
.data
.data_val
== NULL
) {
703 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
704 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
705 free_nfs_cb_data(data
);
708 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
710 rpc_disconnect(rpc
, "normal disconnect");
712 if (rpc_connect_program_async(nfs
->rpc
, nfs
->server
, NFS_PROGRAM
, NFS_V3
, nfs_mount_8_cb
, data
) != 0) {
713 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
714 free_nfs_cb_data(data
);
718 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
719 rpc_set_autoreconnect(rpc
);
723 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
725 struct nfs_cb_data
*data
= private_data
;
726 struct nfs_context
*nfs
= data
->nfs
;
728 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
730 if (status
== RPC_STATUS_ERROR
) {
731 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
732 free_nfs_cb_data(data
);
735 if (status
== RPC_STATUS_CANCEL
) {
736 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
737 free_nfs_cb_data(data
);
741 if (rpc_mount3_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
742 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
743 free_nfs_cb_data(data
);
749 * Async call for mounting an nfs share and geting the root filehandle
751 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
753 struct nfs_cb_data
*data
;
754 char *new_server
, *new_export
;
756 data
= malloc(sizeof(struct nfs_cb_data
));
758 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
761 memset(data
, 0, sizeof(struct nfs_cb_data
));
762 new_server
= strdup(server
);
763 new_export
= strdup(export
);
764 if (nfs
->server
!= NULL
) {
767 nfs
->server
= new_server
;
768 if (nfs
->export
!= NULL
) {
771 nfs
->export
= new_export
;
774 data
->private_data
= private_data
;
776 if (rpc_connect_program_async(nfs
->rpc
, server
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_5_cb
, data
) != 0) {
777 rpc_set_error(nfs
->rpc
, "Failed to start connection");
778 free_nfs_cb_data(data
);
788 * Functions to first look up a path, component by component, and then finally call a specific function once
789 * the filehandle for the final component is found.
791 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
793 struct nfs_cb_data
*data
= private_data
;
794 struct nfs_context
*nfs
= data
->nfs
;
797 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
799 if (status
== RPC_STATUS_ERROR
) {
800 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
801 free_nfs_cb_data(data
);
804 if (status
== RPC_STATUS_CANCEL
) {
805 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
806 free_nfs_cb_data(data
);
811 if (res
->status
!= NFS3_OK
) {
812 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
));
813 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
814 free_nfs_cb_data(data
);
818 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
819 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
820 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
821 free_nfs_cb_data(data
);
826 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
831 while (*data
->path
== '/') {
836 slash
= strchr(path
, '/');
838 /* Clear slash so that path is a zero terminated string for
839 * the current path component. Set it back to '/' again later
840 * when we are finished referencing this component so that
841 * data->saved_path will still point to the full
845 data
->path
= slash
+1;
847 while (*data
->path
!= 0) {
853 data
->fh
.data
.data_len
= fh
->data
.data_len
;
854 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
855 if (data
->fh
.data
.data_val
== NULL
) {
856 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
857 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
858 free_nfs_cb_data(data
);
861 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
865 data
->continue_cb(nfs
, data
);
870 memset(&args
, 0, sizeof(LOOKUP3args
));
872 args
.what
.name
= path
;
874 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, &args
, data
) != 0) {
875 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
876 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
877 free_nfs_cb_data(data
);
886 static int nfs_normalize_path(struct nfs_context
*nfs
, char *path
)
892 while ((str
= strstr(path
, "//"))) {
900 while ((str
= strstr(path
, "/./"))) {
908 if (!strncmp(path
, "/../", 4)) {
909 rpc_set_error(nfs
->rpc
,
910 "Absolute path starts with '/../' "
911 "during normalization");
916 if (path
[0] != '/') {
917 rpc_set_error(nfs
->rpc
,
918 "Absolute path does not start with '/'");
922 /* /string/../ -> / */
923 while ((str
= strstr(path
, "/../"))) {
926 if (!strncmp(path
, "/../", 4)) {
927 rpc_set_error(nfs
->rpc
,
928 "Absolute path starts with '/../' "
929 "during normalization");
934 while (*tmp
!= '/') {
938 while((*(tmp
++) = *(str
++)) != '\0')
945 if (path
[len
- 1] == '/') {
946 path
[len
- 1] = '\0';
950 if (path
[0] == '\0') {
951 rpc_set_error(nfs
->rpc
,
952 "Absolute path became '' "
953 "during normalization");
959 if (!strcmp(&path
[len
- 2], "/.")) {
960 path
[len
- 2] = '\0';
966 if (!strcmp(path
, "/..")) {
967 rpc_set_error(nfs
->rpc
,
968 "Absolute path is '/..' "
969 "during normalization");
973 /* /string/..$ -> / */
975 if (!strcmp(&path
[len
- 3], "/..")) {
976 char *tmp
= &path
[len
- 3];
977 while (*--tmp
!= '/')
986 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
)
988 struct nfs_cb_data
*data
;
990 if (path
[0] == '\0') {
994 data
= malloc(sizeof(struct nfs_cb_data
));
996 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate "
997 "nfs_cb_data structure");
998 if (free_continue_data
)
999 free_continue_data(continue_data
);
1002 memset(data
, 0, sizeof(struct nfs_cb_data
));
1005 data
->continue_cb
= continue_cb
;
1006 data
->continue_data
= continue_data
;
1007 data
->free_continue_data
= free_continue_data
;
1008 data
->continue_int
= continue_int
;
1009 data
->private_data
= private_data
;
1010 if (path
[0] == '/') {
1011 data
->saved_path
= strdup(path
);
1013 data
->saved_path
= malloc(strlen(path
) + strlen(nfs
->cwd
) + 2);
1014 if (data
->saved_path
== NULL
) {
1015 rpc_set_error(nfs
->rpc
, "out of memory: failed to "
1016 "malloc path string");
1017 free_nfs_cb_data(data
);
1020 sprintf(data
->saved_path
, "%s/%s", nfs
->cwd
, path
);
1023 if (data
->saved_path
== NULL
) {
1024 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
1025 free_nfs_cb_data(data
);
1028 if (nfs_normalize_path(nfs
, data
->saved_path
) != 0) {
1029 free_nfs_cb_data(data
);
1033 data
->path
= data
->saved_path
;
1035 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
1036 /* return 0 here since the callback will be invoked if there is a failure */
1049 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1052 struct nfs_cb_data
*data
= private_data
;
1053 struct nfs_context
*nfs
= data
->nfs
;
1060 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1062 if (status
== RPC_STATUS_ERROR
) {
1063 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1064 free_nfs_cb_data(data
);
1067 if (status
== RPC_STATUS_CANCEL
) {
1068 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1069 free_nfs_cb_data(data
);
1074 if (res
->status
!= NFS3_OK
) {
1075 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
));
1076 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1077 free_nfs_cb_data(data
);
1082 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1083 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1084 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1085 st
.st_mode
|= S_IFDIR
;
1087 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1088 st
.st_mode
|= S_IFREG
;
1090 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1091 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1092 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1094 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1096 st
.st_blksize
= 4096;
1097 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
1099 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1100 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1101 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1103 data
->cb(0, nfs
, &st
, data
->private_data
);
1104 free_nfs_cb_data(data
);
1107 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1109 struct GETATTR3args args
;
1111 memset(&args
, 0, sizeof(GETATTR3args
));
1112 args
.object
= data
->fh
;
1114 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1115 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1116 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1117 free_nfs_cb_data(data
);
1123 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1125 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
1126 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1135 * Async nfs_stat64()
1137 static void nfs_stat64_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1140 struct nfs_cb_data
*data
= private_data
;
1141 struct nfs_context
*nfs
= data
->nfs
;
1142 struct nfs_stat_64 st
;
1144 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1146 if (status
== RPC_STATUS_ERROR
) {
1147 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1148 free_nfs_cb_data(data
);
1151 if (status
== RPC_STATUS_CANCEL
) {
1152 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1153 free_nfs_cb_data(data
);
1158 if (res
->status
!= NFS3_OK
) {
1159 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
));
1160 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1161 free_nfs_cb_data(data
);
1166 st
.nfs_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
1167 st
.nfs_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
1168 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
1169 st
.nfs_mode
|= S_IFDIR
;
1171 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
1172 st
.nfs_mode
|= S_IFREG
;
1174 st
.nfs_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
1175 st
.nfs_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
1176 st
.nfs_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
1178 st
.nfs_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1179 st
.nfs_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
1180 st
.nfs_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
1181 st
.nfs_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
1183 data
->cb(0, nfs
, &st
, data
->private_data
);
1184 free_nfs_cb_data(data
);
1187 static int nfs_stat64_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1189 struct GETATTR3args args
;
1191 memset(&args
, 0, sizeof(GETATTR3args
));
1192 args
.object
= data
->fh
;
1194 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat64_1_cb
, &args
, data
) != 0) {
1195 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1196 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1197 free_nfs_cb_data(data
);
1203 int nfs_stat64_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1205 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat64_continue_internal
, NULL
, NULL
, 0) != 0) {
1206 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1216 static void nfs_open_trunc_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1218 struct nfs_cb_data
*data
= private_data
;
1219 struct nfs_context
*nfs
= data
->nfs
;
1220 struct nfsfh
*nfsfh
;
1223 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1225 if (status
== RPC_STATUS_ERROR
) {
1226 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1227 free_nfs_cb_data(data
);
1230 if (status
== RPC_STATUS_CANCEL
) {
1231 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1232 free_nfs_cb_data(data
);
1237 if (res
->status
!= NFS3_OK
) {
1238 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1239 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1240 free_nfs_cb_data(data
);
1244 nfsfh
= malloc(sizeof(struct nfsfh
));
1245 if (nfsfh
== NULL
) {
1246 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1247 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1248 free_nfs_cb_data(data
);
1251 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1253 if (data
->continue_int
& O_SYNC
) {
1257 /* steal the filehandle */
1258 nfsfh
->fh
= data
->fh
;
1259 data
->fh
.data
.data_val
= NULL
;
1261 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1262 free_nfs_cb_data(data
);
1265 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1268 struct nfs_cb_data
*data
= private_data
;
1269 struct nfs_context
*nfs
= data
->nfs
;
1270 struct nfsfh
*nfsfh
;
1271 unsigned int nfsmode
= 0;
1273 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1275 if (status
== RPC_STATUS_ERROR
) {
1276 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1277 free_nfs_cb_data(data
);
1280 if (status
== RPC_STATUS_CANCEL
) {
1281 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1282 free_nfs_cb_data(data
);
1287 if (res
->status
!= NFS3_OK
) {
1288 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
));
1289 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1290 free_nfs_cb_data(data
);
1294 if (data
->continue_int
& O_WRONLY
) {
1295 nfsmode
|= ACCESS3_MODIFY
;
1297 if (data
->continue_int
& O_RDWR
) {
1298 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1300 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1301 nfsmode
|= ACCESS3_READ
;
1305 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
1306 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
1307 nfsmode
&ACCESS3_READ
?'r':'-',
1308 nfsmode
&ACCESS3_MODIFY
?'w':'-',
1309 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
1310 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
1311 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
1312 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
1313 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1314 free_nfs_cb_data(data
);
1318 /* Try to truncate it if we were requested to */
1319 if ((data
->continue_int
& O_TRUNC
) &&
1320 (data
->continue_int
& (O_RDWR
|O_WRONLY
))) {
1323 memset(&args
, 0, sizeof(SETATTR3args
));
1324 args
.object
= data
->fh
;
1325 args
.new_attributes
.size
.set_it
= 1;
1326 args
.new_attributes
.size
.set_size3_u
.size
= 0;
1328 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_open_trunc_cb
, &args
,
1330 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send "
1331 "SETATTR call for %s", data
->path
);
1332 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1333 data
->private_data
);
1334 free_nfs_cb_data(data
);
1340 nfsfh
= malloc(sizeof(struct nfsfh
));
1341 if (nfsfh
== NULL
) {
1342 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1343 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1344 free_nfs_cb_data(data
);
1347 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1349 if (data
->continue_int
& O_SYNC
) {
1353 /* steal the filehandle */
1354 nfsfh
->fh
= data
->fh
;
1355 data
->fh
.data
.data_val
= NULL
;
1357 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1358 free_nfs_cb_data(data
);
1361 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1366 if (data
->continue_int
& O_WRONLY
) {
1367 nfsmode
|= ACCESS3_MODIFY
;
1369 if (data
->continue_int
& O_RDWR
) {
1370 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1372 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1373 nfsmode
|= ACCESS3_READ
;
1376 memset(&args
, 0, sizeof(ACCESS3args
));
1377 args
.object
= data
->fh
;
1378 args
.access
= nfsmode
;
1380 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_open_cb
, &args
, data
) != 0) {
1381 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS "
1382 "call for %s", data
->path
);
1383 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
1384 data
->private_data
);
1385 free_nfs_cb_data(data
);
1391 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int flags
, nfs_cb cb
, void *private_data
)
1393 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, flags
) != 0) {
1394 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1405 static int nfs_chdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1407 /* steal saved_path */
1409 nfs
->cwd
= data
->saved_path
;
1410 data
->saved_path
= NULL
;
1412 data
->cb(0, nfs
, NULL
, data
->private_data
);
1413 free_nfs_cb_data(data
);
1418 int nfs_chdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1420 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chdir_continue_internal
, NULL
, NULL
, 0) != 0) {
1421 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1432 static void nfs_fill_READ3args(READ3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
)
1434 memset(args
, 0, sizeof(READ3args
));
1435 args
->file
= fh
->fh
;
1436 args
->offset
= offset
;
1437 args
->count
= count
;
1440 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1442 struct nfs_mcb_data
*mdata
= private_data
;
1443 struct nfs_cb_data
*data
= mdata
->data
;
1444 struct nfs_context
*nfs
= data
->nfs
;
1447 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1451 if (status
== RPC_STATUS_ERROR
) {
1452 /* flag the failure but do not invoke callback until we have received all responses */
1455 if (status
== RPC_STATUS_CANCEL
) {
1456 /* flag the cancellation but do not invoke callback until we have received all responses */
1460 if (status
== RPC_STATUS_SUCCESS
) {
1462 if (res
->status
!= NFS3_OK
) {
1463 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1466 /* if we have more than one call or we have received a short read we need a reassembly buffer */
1467 if (data
->num_calls
|| (res
->READ3res_u
.resok
.count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
)) {
1468 if (data
->buffer
== NULL
) {
1469 data
->buffer
= malloc(data
->request_size
);
1470 if (data
->buffer
== NULL
) {
1471 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data
->request_size
);
1476 if (res
->READ3res_u
.resok
.count
> 0) {
1477 if (res
->READ3res_u
.resok
.count
<= mdata
->count
) {
1478 /* copy data into reassembly buffer if we have one */
1479 if (data
->buffer
!= NULL
) {
1480 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1482 if (data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1483 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1486 rpc_set_error(nfs
->rpc
, "NFS: Read overflow. Server has sent more data than requested!");
1490 /* check if we have received a short read */
1491 if (res
->READ3res_u
.resok
.count
< mdata
->count
&& !res
->READ3res_u
.resok
.eof
) {
1492 if (res
->READ3res_u
.resok
.count
== 0) {
1493 rpc_set_error(nfs
->rpc
, "NFS: Read failed. No bytes read and not at EOF!");
1496 /* reissue reminder of this read request */
1498 mdata
->offset
+= res
->READ3res_u
.resok
.count
;
1499 mdata
->count
-= res
->READ3res_u
.resok
.count
;
1500 nfs_fill_READ3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
);
1501 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) == 0) {
1505 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1515 if (data
->num_calls
> 0) {
1516 /* still waiting for more replies */
1519 if (data
->oom
!= 0) {
1520 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1521 free_nfs_cb_data(data
);
1524 if (data
->error
!= 0) {
1525 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1526 free_nfs_cb_data(data
);
1529 if (data
->cancel
!= 0) {
1530 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1531 free_nfs_cb_data(data
);
1535 data
->nfsfh
->offset
= data
->max_offset
;
1537 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1539 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1542 free_nfs_cb_data(data
);
1545 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1547 struct nfs_cb_data
*data
;
1549 data
= malloc(sizeof(struct nfs_cb_data
));
1551 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1554 memset(data
, 0, sizeof(struct nfs_cb_data
));
1557 data
->private_data
= private_data
;
1558 data
->nfsfh
= nfsfh
;
1559 data
->request_size
= count
;
1561 nfsfh
->offset
= offset
;
1563 assert(data
->num_calls
== 0);
1565 /* chop requests into chunks of at most READMAX bytes if necessary.
1566 * we send all reads in parallel so that performance is still good.
1568 data
->max_offset
= offset
;
1569 data
->start_offset
= offset
;
1572 uint64_t readcount
= count
;
1573 struct nfs_mcb_data
*mdata
;
1576 if (readcount
> nfs_get_readmax(nfs
)) {
1577 readcount
= nfs_get_readmax(nfs
);
1580 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1581 if (mdata
== NULL
) {
1582 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1583 if (data
->num_calls
== 0) {
1584 free_nfs_cb_data(data
);
1590 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1592 mdata
->offset
= offset
;
1593 mdata
->count
= readcount
;
1595 nfs_fill_READ3args(&args
, nfsfh
, offset
, readcount
);
1597 if (rpc_nfs3_read_async(nfs
->rpc
, nfs_pread_mcb
, &args
, mdata
) != 0) {
1598 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1600 if (data
->num_calls
== 0) {
1601 free_nfs_cb_data(data
);
1609 offset
+= readcount
;
1611 } while (count
> 0);
1619 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1621 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1629 static void nfs_fill_WRITE3args (WRITE3args
*args
, struct nfsfh
*fh
, uint64_t offset
, uint64_t count
,
1632 memset(args
, 0, sizeof(WRITE3args
));
1633 args
->file
= fh
->fh
;
1634 args
->offset
= offset
;
1635 args
->count
= count
;
1636 args
->stable
= fh
->is_sync
?FILE_SYNC
:UNSTABLE
;
1637 args
->data
.data_len
= count
;
1638 args
->data
.data_val
= buf
;
1641 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1643 struct nfs_mcb_data
*mdata
= private_data
;
1644 struct nfs_cb_data
*data
= mdata
->data
;
1645 struct nfs_context
*nfs
= data
->nfs
;
1648 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1652 if (status
== RPC_STATUS_ERROR
) {
1653 /* flag the failure but do not invoke callback until we have received all responses */
1656 if (status
== RPC_STATUS_CANCEL
) {
1657 /* flag the cancellation but do not invoke callback until we have received all responses */
1661 if (status
== RPC_STATUS_SUCCESS
) {
1663 if (res
->status
!= NFS3_OK
) {
1664 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1667 if (res
->WRITE3res_u
.resok
.count
< mdata
->count
) {
1668 if (res
->WRITE3res_u
.resok
.count
== 0) {
1669 rpc_set_error(nfs
->rpc
, "NFS: Write failed. No bytes written!");
1672 /* reissue reminder of this write request */
1674 mdata
->offset
+= res
->WRITE3res_u
.resok
.count
;
1675 mdata
->count
-= res
->WRITE3res_u
.resok
.count
;
1676 nfs_fill_WRITE3args(&args
, data
->nfsfh
, mdata
->offset
, mdata
->count
,
1677 &data
->usrbuf
[mdata
->offset
- data
->start_offset
]);
1678 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) == 0) {
1682 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1687 if (res
->WRITE3res_u
.resok
.count
> 0) {
1688 if (data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1689 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1697 if (data
->num_calls
> 0) {
1698 /* still waiting for more replies */
1701 if (data
->oom
!= 0) {
1702 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
1703 free_nfs_cb_data(data
);
1706 if (data
->error
!= 0) {
1707 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1708 free_nfs_cb_data(data
);
1711 if (data
->cancel
!= 0) {
1712 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1713 free_nfs_cb_data(data
);
1717 data
->nfsfh
->offset
= data
->max_offset
;
1718 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1720 free_nfs_cb_data(data
);
1724 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
)
1726 struct nfs_cb_data
*data
;
1728 data
= malloc(sizeof(struct nfs_cb_data
));
1730 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1733 memset(data
, 0, sizeof(struct nfs_cb_data
));
1736 data
->private_data
= private_data
;
1737 data
->nfsfh
= nfsfh
;
1740 nfsfh
->offset
= offset
;
1742 /* hello, clang-analyzer */
1743 assert(data
->num_calls
== 0);
1745 /* chop requests into chunks of at most WRITEMAX bytes if necessary.
1746 * we send all writes in parallel so that performance is still good.
1748 data
->max_offset
= offset
;
1749 data
->start_offset
= offset
;
1752 uint64_t writecount
= count
;
1753 struct nfs_mcb_data
*mdata
;
1756 if (writecount
> nfs_get_writemax(nfs
)) {
1757 writecount
= nfs_get_writemax(nfs
);
1760 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1761 if (mdata
== NULL
) {
1762 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1763 if (data
->num_calls
== 0) {
1764 free_nfs_cb_data(data
);
1770 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1772 mdata
->offset
= offset
;
1773 mdata
->count
= writecount
;
1775 nfs_fill_WRITE3args(&args
, nfsfh
, offset
, writecount
, &buf
[offset
- data
->start_offset
]);
1777 if (rpc_nfs3_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &args
, mdata
) != 0) {
1778 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1780 if (data
->num_calls
== 0) {
1781 free_nfs_cb_data(data
);
1788 count
-= writecount
;
1789 offset
+= writecount
;
1791 } while (count
> 0);
1799 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1801 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1811 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1813 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1814 free(nfsfh
->fh
.data
.data_val
);
1815 nfsfh
->fh
.data
.data_val
= NULL
;
1819 cb(0, nfs
, NULL
, private_data
);
1830 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1832 struct nfs_cb_data
*data
;
1833 struct GETATTR3args args
;
1835 data
= malloc(sizeof(struct nfs_cb_data
));
1837 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1840 memset(data
, 0, sizeof(struct nfs_cb_data
));
1843 data
->private_data
= private_data
;
1845 memset(&args
, 0, sizeof(GETATTR3args
));
1846 args
.object
= nfsfh
->fh
;
1848 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &args
, data
) != 0) {
1849 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1850 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1851 free_nfs_cb_data(data
);
1862 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1864 struct nfs_cb_data
*data
= private_data
;
1865 struct nfs_context
*nfs
= data
->nfs
;
1868 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1870 if (status
== RPC_STATUS_ERROR
) {
1871 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1872 free_nfs_cb_data(data
);
1875 if (status
== RPC_STATUS_CANCEL
) {
1876 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1877 free_nfs_cb_data(data
);
1882 if (res
->status
!= NFS3_OK
) {
1883 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1884 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1885 free_nfs_cb_data(data
);
1889 data
->cb(0, nfs
, NULL
, data
->private_data
);
1890 free_nfs_cb_data(data
);
1893 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1895 struct nfs_cb_data
*data
;
1896 struct COMMIT3args args
;
1898 data
= malloc(sizeof(struct nfs_cb_data
));
1900 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1903 memset(data
, 0, sizeof(struct nfs_cb_data
));
1906 data
->private_data
= private_data
;
1908 args
.file
= nfsfh
->fh
;
1911 if (rpc_nfs3_commit_async(nfs
->rpc
, nfs_fsync_cb
, &args
, data
) != 0) {
1912 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1913 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1914 free_nfs_cb_data(data
);
1926 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1928 struct nfs_cb_data
*data
= private_data
;
1929 struct nfs_context
*nfs
= data
->nfs
;
1932 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1934 if (status
== RPC_STATUS_ERROR
) {
1935 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1936 free_nfs_cb_data(data
);
1939 if (status
== RPC_STATUS_CANCEL
) {
1940 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1941 free_nfs_cb_data(data
);
1946 if (res
->status
!= NFS3_OK
) {
1947 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1948 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1949 free_nfs_cb_data(data
);
1953 data
->cb(0, nfs
, NULL
, data
->private_data
);
1954 free_nfs_cb_data(data
);
1957 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1959 struct nfs_cb_data
*data
;
1962 data
= malloc(sizeof(struct nfs_cb_data
));
1964 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1967 memset(data
, 0, sizeof(struct nfs_cb_data
));
1970 data
->private_data
= private_data
;
1972 memset(&args
, 0, sizeof(SETATTR3args
));
1973 args
.object
= nfsfh
->fh
;
1974 args
.new_attributes
.size
.set_it
= 1;
1975 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1977 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1978 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1979 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1980 free_nfs_cb_data(data
);
1990 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1992 uint64_t offset
= data
->continue_int
;
1995 nfsfh
.fh
= data
->fh
;
1997 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1998 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1999 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2000 free_nfs_cb_data(data
);
2003 free_nfs_cb_data(data
);
2007 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
2013 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
2014 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2027 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2030 struct nfs_cb_data
*data
= private_data
;
2031 struct nfs_context
*nfs
= data
->nfs
;
2032 char *str
= data
->continue_data
;
2034 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2036 str
= &str
[strlen(str
) + 1];
2038 if (status
== RPC_STATUS_ERROR
) {
2039 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2040 free_nfs_cb_data(data
);
2043 if (status
== RPC_STATUS_CANCEL
) {
2044 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2045 free_nfs_cb_data(data
);
2050 if (res
->status
!= NFS3_OK
) {
2051 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
));
2052 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2053 free_nfs_cb_data(data
);
2057 data
->cb(0, nfs
, NULL
, data
->private_data
);
2058 free_nfs_cb_data(data
);
2061 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2063 char *str
= data
->continue_data
;
2066 str
= &str
[strlen(str
) + 1];
2068 memset(&args
, 0, sizeof(MKDIR3args
));
2069 args
.where
.dir
= data
->fh
;
2070 args
.where
.name
= str
;
2071 args
.attributes
.mode
.set_it
= 1;
2072 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
2074 if (rpc_nfs3_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
2075 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
2076 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2077 free_nfs_cb_data(data
);
2083 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2088 new_path
= strdup(path
);
2089 if (new_path
== NULL
) {
2090 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2094 ptr
= strrchr(new_path
, '/');
2097 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2102 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2103 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
2104 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
2118 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2121 struct nfs_cb_data
*data
= private_data
;
2122 struct nfs_context
*nfs
= data
->nfs
;
2123 char *str
= data
->continue_data
;
2125 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2127 str
= &str
[strlen(str
) + 1];
2129 if (status
== RPC_STATUS_ERROR
) {
2130 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2131 free_nfs_cb_data(data
);
2134 if (status
== RPC_STATUS_CANCEL
) {
2135 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2136 free_nfs_cb_data(data
);
2141 if (res
->status
!= NFS3_OK
) {
2142 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
));
2143 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2144 free_nfs_cb_data(data
);
2148 data
->cb(0, nfs
, NULL
, data
->private_data
);
2149 free_nfs_cb_data(data
);
2152 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2154 char *str
= data
->continue_data
;
2157 str
= &str
[strlen(str
) + 1];
2159 args
.object
.dir
= data
->fh
;
2160 args
.object
.name
= str
;
2161 if (rpc_nfs3_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &args
, data
) != 0) {
2162 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
2163 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2164 free_nfs_cb_data(data
);
2170 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2175 new_path
= strdup(path
);
2176 if (new_path
== NULL
) {
2177 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2181 ptr
= strrchr(new_path
, '/');
2184 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2189 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2190 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
2191 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2204 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2207 struct nfs_cb_data
*data
= private_data
;
2208 struct nfs_context
*nfs
= data
->nfs
;
2209 struct nfsfh
*nfsfh
;
2210 char *str
= data
->continue_data
;
2212 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2214 if (status
== RPC_STATUS_ERROR
) {
2215 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2216 free_nfs_cb_data(data
);
2219 if (status
== RPC_STATUS_CANCEL
) {
2220 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2221 free_nfs_cb_data(data
);
2225 str
= &str
[strlen(str
) + 1];
2227 if (res
->status
!= NFS3_OK
) {
2228 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
));
2229 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2234 nfsfh
= malloc(sizeof(struct nfsfh
));
2235 if (nfsfh
== NULL
) {
2236 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
2237 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2238 free_nfs_cb_data(data
);
2241 memset(nfsfh
, 0, sizeof(struct nfsfh
));
2243 /* copy the filehandle */
2244 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
2245 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
2246 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
2248 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
2249 free_nfs_cb_data(data
);
2254 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2257 struct nfs_cb_data
*data
= private_data
;
2258 struct nfs_context
*nfs
= data
->nfs
;
2259 char *str
= data
->continue_data
;
2262 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2264 if (status
== RPC_STATUS_ERROR
) {
2265 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2266 free_nfs_cb_data(data
);
2269 if (status
== RPC_STATUS_CANCEL
) {
2270 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2271 free_nfs_cb_data(data
);
2275 str
= &str
[strlen(str
) + 1];
2277 if (res
->status
!= NFS3_OK
) {
2278 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
));
2279 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2280 free_nfs_cb_data(data
);
2284 memset(&args
, 0, sizeof(LOOKUP3args
));
2285 args
.what
.dir
= data
->fh
;
2286 args
.what
.name
= str
;
2288 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &args
, data
) != 0) {
2289 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
2290 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2291 free_nfs_cb_data(data
);
2297 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2299 char *str
= data
->continue_data
;
2302 str
= &str
[strlen(str
) + 1];
2304 memset(&args
, 0, sizeof(CREATE3args
));
2305 args
.where
.dir
= data
->fh
;
2306 args
.where
.name
= str
;
2307 args
.how
.mode
= UNCHECKED
;
2308 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
2309 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2311 if (rpc_nfs3_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
2312 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
2313 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2314 free_nfs_cb_data(data
);
2320 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2325 new_path
= strdup(path
);
2326 if (new_path
== NULL
) {
2327 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2331 ptr
= strrchr(new_path
, '/');
2333 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2339 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
2340 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
2341 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2354 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2357 struct nfs_cb_data
*data
= private_data
;
2358 struct nfs_context
*nfs
= data
->nfs
;
2359 char *str
= data
->continue_data
;
2361 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2363 str
= &str
[strlen(str
) + 1];
2365 if (status
== RPC_STATUS_ERROR
) {
2366 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2367 free_nfs_cb_data(data
);
2370 if (status
== RPC_STATUS_CANCEL
) {
2371 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2372 free_nfs_cb_data(data
);
2377 if (res
->status
!= NFS3_OK
) {
2378 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
));
2379 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2380 free_nfs_cb_data(data
);
2384 data
->cb(0, nfs
, NULL
, data
->private_data
);
2385 free_nfs_cb_data(data
);
2388 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2390 char *str
= data
->continue_data
;
2391 struct REMOVE3args args
;
2393 str
= &str
[strlen(str
) + 1];
2395 args
.object
.dir
= data
->fh
;
2396 args
.object
.name
= str
;
2397 if (rpc_nfs3_remove_async(nfs
->rpc
, nfs_unlink_cb
, &args
, data
) != 0) {
2398 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
2399 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2400 free_nfs_cb_data(data
);
2406 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2411 new_path
= strdup(path
);
2412 if (new_path
== NULL
) {
2413 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2417 ptr
= strrchr(new_path
, '/');
2420 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2425 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2426 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2427 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2438 struct mknod_cb_data
{
2445 static void free_mknod_cb_data(void *ptr
)
2447 struct mknod_cb_data
*data
= ptr
;
2453 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2456 struct nfs_cb_data
*data
= private_data
;
2457 struct nfs_context
*nfs
= data
->nfs
;
2458 char *str
= data
->continue_data
;
2460 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2462 str
= &str
[strlen(str
) + 1];
2464 if (status
== RPC_STATUS_ERROR
) {
2465 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2466 free_nfs_cb_data(data
);
2469 if (status
== RPC_STATUS_CANCEL
) {
2470 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2471 free_nfs_cb_data(data
);
2476 if (res
->status
!= NFS3_OK
) {
2477 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
));
2478 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2479 free_nfs_cb_data(data
);
2483 data
->cb(0, nfs
, NULL
, data
->private_data
);
2484 free_nfs_cb_data(data
);
2487 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2489 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2490 char *str
= cb_data
->path
;
2493 str
= &str
[strlen(str
) + 1];
2495 args
.where
.dir
= data
->fh
;
2496 args
.where
.name
= str
;
2497 switch (cb_data
->mode
& S_IFMT
) {
2499 args
.what
.type
= NF3CHR
;
2500 args
.what
.mknoddata3_u
.chr_device
.dev_attributes
.mode
.set_it
= 1;
2501 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
);
2502 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata1
= cb_data
->major
;
2503 args
.what
.mknoddata3_u
.chr_device
.spec
.specdata2
= cb_data
->minor
;
2506 args
.what
.type
= NF3BLK
;
2507 args
.what
.mknoddata3_u
.blk_device
.dev_attributes
.mode
.set_it
= 1;
2508 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
);
2509 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata1
= cb_data
->major
;
2510 args
.what
.mknoddata3_u
.blk_device
.spec
.specdata2
= cb_data
->minor
;
2512 args
.what
.type
= NF3SOCK
;
2513 args
.what
.mknoddata3_u
.sock_attributes
.mode
.set_it
= 1;
2514 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
);
2517 args
.what
.type
= NF3FIFO
;
2518 args
.what
.mknoddata3_u
.pipe_attributes
.mode
.set_it
= 1;
2519 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
);
2522 rpc_set_error(nfs
->rpc
, "Invalid file type for NFS3/MKNOD call");
2523 data
->cb(-EINVAL
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2524 free_nfs_cb_data(data
);
2528 if (rpc_nfs3_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &args
, data
) != 0) {
2529 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2530 free_nfs_cb_data(data
);
2536 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2539 struct mknod_cb_data
*cb_data
;
2541 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2542 if (cb_data
== NULL
) {
2543 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2547 cb_data
->path
= strdup(path
);
2548 if (cb_data
->path
== NULL
) {
2549 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2554 ptr
= strrchr(cb_data
->path
, '/');
2556 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2557 free_mknod_cb_data(cb_data
);
2562 cb_data
->mode
= mode
;
2563 cb_data
->major
= major(dev
);
2564 cb_data
->minor
= minor(dev
);
2566 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2567 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2568 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2579 /* ReadDirPlus Emulation Callback data */
2580 struct rdpe_cb_data
{
2583 struct nfs_cb_data
*data
;
2586 /* ReadDirPlus Emulation LOOKUP Callback data */
2587 struct rdpe_lookup_cb_data
{
2588 struct rdpe_cb_data
*rdpe_cb_data
;
2589 struct nfsdirent
*nfsdirent
;
2592 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2593 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2595 LOOKUP3res
*res
= command_data
;
2596 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2597 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2598 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2599 struct nfsdir
*nfsdir
= data
->continue_data
;
2600 struct nfs_context
*nfs
= data
->nfs
;
2601 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2603 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2605 free(rdpe_lookup_cb_data
);
2607 rdpe_cb_data
->getattrcount
--;
2609 if (status
== RPC_STATUS_ERROR
) {
2610 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2611 "failed with RPC_STATUS_ERROR");
2612 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2614 if (status
== RPC_STATUS_CANCEL
) {
2615 rpc_set_error(nfs
->rpc
, "LOOKUP during READDIRPLUS emulation "
2616 "failed with RPC_STATUS_CANCEL");
2617 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2619 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2620 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2621 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2623 nfsdirent
->type
= attributes
->type
;
2624 nfsdirent
->mode
= attributes
->mode
;
2625 nfsdirent
->size
= attributes
->size
;
2627 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2628 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2629 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2630 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2631 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2632 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2633 nfsdirent
->uid
= attributes
->uid
;
2634 nfsdirent
->gid
= attributes
->gid
;
2638 if (rdpe_cb_data
->getattrcount
== 0) {
2639 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2640 rpc_set_error(nfs
->rpc
, "READDIRPLUS emulation "
2641 "failed: %s", rpc_get_error(rpc
));
2642 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
),
2643 data
->private_data
);
2644 nfs_free_nfsdir(nfsdir
);
2646 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2650 data
->continue_data
= NULL
;
2651 free_nfs_cb_data(data
);
2655 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2657 READDIR3res
*res
= command_data
;
2658 struct nfs_cb_data
*data
= private_data
;
2659 struct nfs_context
*nfs
= data
->nfs
;
2660 struct nfsdir
*nfsdir
= data
->continue_data
;
2661 struct nfsdirent
*nfsdirent
;
2662 struct entry3
*entry
;
2663 uint64_t cookie
= 0;
2664 struct rdpe_cb_data
*rdpe_cb_data
;
2666 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2668 if (status
== RPC_STATUS_ERROR
) {
2669 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2670 nfs_free_nfsdir(nfsdir
);
2671 data
->continue_data
= NULL
;
2672 free_nfs_cb_data(data
);
2676 if (status
== RPC_STATUS_CANCEL
) {
2677 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2678 nfs_free_nfsdir(nfsdir
);
2679 data
->continue_data
= NULL
;
2680 free_nfs_cb_data(data
);
2684 if (res
->status
!= NFS3_OK
) {
2685 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
));
2686 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2687 nfs_free_nfsdir(nfsdir
);
2688 data
->continue_data
= NULL
;
2689 free_nfs_cb_data(data
);
2693 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2694 while (entry
!= NULL
) {
2695 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2696 if (nfsdirent
== NULL
) {
2697 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2698 nfs_free_nfsdir(nfsdir
);
2699 data
->continue_data
= NULL
;
2700 free_nfs_cb_data(data
);
2703 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2704 nfsdirent
->name
= strdup(entry
->name
);
2705 if (nfsdirent
->name
== NULL
) {
2706 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2708 nfs_free_nfsdir(nfsdir
);
2709 data
->continue_data
= NULL
;
2710 free_nfs_cb_data(data
);
2713 nfsdirent
->inode
= entry
->fileid
;
2715 nfsdirent
->next
= nfsdir
->entries
;
2716 nfsdir
->entries
= nfsdirent
;
2718 cookie
= entry
->cookie
;
2719 entry
= entry
->nextentry
;
2722 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2725 args
.dir
= data
->fh
;
2726 args
.cookie
= cookie
;
2727 memcpy(&args
.cookieverf
, res
->READDIR3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2730 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2731 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2732 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2733 nfs_free_nfsdir(nfsdir
);
2734 data
->continue_data
= NULL
;
2735 free_nfs_cb_data(data
);
2741 /* steal the dirhandle */
2742 nfsdir
->current
= nfsdir
->entries
;
2744 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2745 rdpe_cb_data
->getattrcount
= 0;
2746 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2747 rdpe_cb_data
->data
= data
;
2748 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2749 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2752 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2753 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2754 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2756 memset(&args
, 0, sizeof(LOOKUP3args
));
2757 args
.what
.dir
= data
->fh
;
2758 args
.what
.name
= nfsdirent
->name
;
2760 if (rpc_nfs3_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &args
, rdpe_lookup_cb_data
) != 0) {
2761 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2763 /* if we have already commands in flight, we cant just stop, we have to wait for the
2764 * commands in flight to complete
2766 if (rdpe_cb_data
->getattrcount
> 0) {
2767 nfs_free_nfsdir(nfsdir
);
2768 data
->continue_data
= NULL
;
2769 free_nfs_cb_data(data
);
2770 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2771 free(rdpe_lookup_cb_data
);
2775 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2776 nfs_free_nfsdir(nfsdir
);
2777 data
->continue_data
= NULL
;
2778 free_nfs_cb_data(data
);
2779 free(rdpe_lookup_cb_data
);
2783 rdpe_cb_data
->getattrcount
++;
2787 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2789 READDIRPLUS3res
*res
= command_data
;
2790 struct nfs_cb_data
*data
= private_data
;
2791 struct nfs_context
*nfs
= data
->nfs
;
2792 struct nfsdir
*nfsdir
= data
->continue_data
;
2793 struct entryplus3
*entry
;
2794 uint64_t cookie
= 0;
2796 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2798 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2801 args
.dir
= data
->fh
;
2802 args
.cookie
= cookie
;
2803 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2806 if (rpc_nfs3_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &args
, data
) != 0) {
2807 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2808 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2809 nfs_free_nfsdir(nfsdir
);
2810 data
->continue_data
= NULL
;
2811 free_nfs_cb_data(data
);
2817 if (status
== RPC_STATUS_CANCEL
) {
2818 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2819 nfs_free_nfsdir(nfsdir
);
2820 data
->continue_data
= NULL
;
2821 free_nfs_cb_data(data
);
2825 if (res
->status
!= NFS3_OK
) {
2826 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
));
2827 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2828 nfs_free_nfsdir(nfsdir
);
2829 data
->continue_data
= NULL
;
2830 free_nfs_cb_data(data
);
2834 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2835 while (entry
!= NULL
) {
2836 struct nfsdirent
*nfsdirent
;
2838 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2839 if (nfsdirent
== NULL
) {
2840 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2841 nfs_free_nfsdir(nfsdir
);
2842 data
->continue_data
= NULL
;
2843 free_nfs_cb_data(data
);
2846 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2847 nfsdirent
->name
= strdup(entry
->name
);
2848 if (nfsdirent
->name
== NULL
) {
2849 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2851 nfs_free_nfsdir(nfsdir
);
2852 data
->continue_data
= NULL
;
2853 free_nfs_cb_data(data
);
2856 nfsdirent
->inode
= entry
->fileid
;
2857 if (entry
->name_attributes
.attributes_follow
) {
2858 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2859 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2860 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2862 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2863 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2864 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2865 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2866 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2867 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2868 nfsdirent
->uid
= entry
->name_attributes
.post_op_attr_u
.attributes
.uid
;
2869 nfsdirent
->gid
= entry
->name_attributes
.post_op_attr_u
.attributes
.gid
;
2872 nfsdirent
->next
= nfsdir
->entries
;
2873 nfsdir
->entries
= nfsdirent
;
2875 cookie
= entry
->cookie
;
2876 entry
= entry
->nextentry
;
2879 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2880 READDIRPLUS3args args
;
2882 args
.dir
= data
->fh
;
2883 args
.cookie
= cookie
;
2884 memcpy(&args
.cookieverf
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, sizeof(cookieverf3
));
2885 args
.dircount
= 8192;
2886 args
.maxcount
= 8192;
2888 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2889 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2890 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2891 nfs_free_nfsdir(nfsdir
);
2892 data
->continue_data
= NULL
;
2893 free_nfs_cb_data(data
);
2899 /* steal the dirhandle */
2900 data
->continue_data
= NULL
;
2901 nfsdir
->current
= nfsdir
->entries
;
2903 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2904 free_nfs_cb_data(data
);
2907 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2909 READDIRPLUS3args args
;
2911 args
.dir
= data
->fh
;
2913 memset(&args
.cookieverf
, 0, sizeof(cookieverf3
));
2914 args
.dircount
= 8192;
2915 args
.maxcount
= 8192;
2916 if (rpc_nfs3_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &args
, data
) != 0) {
2917 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2918 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2919 free_nfs_cb_data(data
);
2925 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2927 struct nfsdir
*nfsdir
;
2929 nfsdir
= malloc(sizeof(struct nfsdir
));
2930 if (nfsdir
== NULL
) {
2931 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2934 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2936 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2937 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2945 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2947 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2949 if (nfsdir
->current
!= NULL
) {
2950 nfsdir
->current
= nfsdir
->current
->next
;
2959 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2961 nfs_free_nfsdir(nfsdir
);
2968 void nfs_getcwd(struct nfs_context
*nfs
, const char **cwd
)
2979 struct lseek_cb_data
{
2980 struct nfs_context
*nfs
;
2981 struct nfsfh
*nfsfh
;
2987 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2990 struct lseek_cb_data
*data
= private_data
;
2991 struct nfs_context
*nfs
= data
->nfs
;
2993 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2995 if (status
== RPC_STATUS_ERROR
) {
2996 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3000 if (status
== RPC_STATUS_CANCEL
) {
3001 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3007 if (res
->status
!= NFS3_OK
) {
3008 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3009 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3014 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
3015 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
3019 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
3021 struct lseek_cb_data
*data
;
3022 struct GETATTR3args args
;
3024 if (whence
== SEEK_SET
) {
3025 nfsfh
->offset
= offset
;
3026 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3029 if (whence
== SEEK_CUR
) {
3030 nfsfh
->offset
+= offset
;
3031 cb(0, nfs
, &nfsfh
->offset
, private_data
);
3035 data
= malloc(sizeof(struct lseek_cb_data
));
3037 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
3042 data
->nfsfh
= nfsfh
;
3043 data
->offset
= offset
;
3045 data
->private_data
= private_data
;
3047 memset(&args
, 0, sizeof(GETATTR3args
));
3048 args
.object
= nfsfh
->fh
;
3050 if (rpc_nfs3_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &args
, data
) != 0) {
3051 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
3064 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3067 struct nfs_cb_data
*data
= private_data
;
3068 struct nfs_context
*nfs
= data
->nfs
;
3069 struct statvfs svfs
;
3071 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3073 if (status
== RPC_STATUS_ERROR
) {
3074 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3075 free_nfs_cb_data(data
);
3078 if (status
== RPC_STATUS_CANCEL
) {
3079 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3080 free_nfs_cb_data(data
);
3085 if (res
->status
!= NFS3_OK
) {
3086 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
));
3087 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3088 free_nfs_cb_data(data
);
3092 svfs
.f_bsize
= 4096;
3093 svfs
.f_frsize
= 4096;
3094 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
3095 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
3096 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
3097 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
3098 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
3099 #if !defined(ANDROID)
3100 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
3103 svfs
.f_namemax
= 256;
3106 data
->cb(0, nfs
, &svfs
, data
->private_data
);
3107 free_nfs_cb_data(data
);
3110 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3114 args
.fsroot
= data
->fh
;
3115 if (rpc_nfs3_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &args
, data
) != 0) {
3116 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
3117 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3118 free_nfs_cb_data(data
);
3124 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3126 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
3127 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3140 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3143 struct nfs_cb_data
*data
= private_data
;
3144 struct nfs_context
*nfs
= data
->nfs
;
3146 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3148 if (status
== RPC_STATUS_ERROR
) {
3149 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3150 free_nfs_cb_data(data
);
3153 if (status
== RPC_STATUS_CANCEL
) {
3154 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3155 free_nfs_cb_data(data
);
3160 if (res
->status
!= NFS3_OK
) {
3161 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
));
3162 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3163 free_nfs_cb_data(data
);
3168 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
3169 free_nfs_cb_data(data
);
3172 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3176 args
.symlink
= data
->fh
;
3178 if (rpc_nfs3_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
3179 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
3180 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3181 free_nfs_cb_data(data
);
3187 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
3189 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
3190 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3203 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3205 struct nfs_cb_data
*data
= private_data
;
3206 struct nfs_context
*nfs
= data
->nfs
;
3209 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3211 if (status
== RPC_STATUS_ERROR
) {
3212 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3213 free_nfs_cb_data(data
);
3216 if (status
== RPC_STATUS_CANCEL
) {
3217 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3218 free_nfs_cb_data(data
);
3223 if (res
->status
!= NFS3_OK
) {
3224 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3225 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3226 free_nfs_cb_data(data
);
3230 data
->cb(0, nfs
, NULL
, data
->private_data
);
3231 free_nfs_cb_data(data
);
3234 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3238 memset(&args
, 0, sizeof(SETATTR3args
));
3239 args
.object
= data
->fh
;
3240 args
.new_attributes
.mode
.set_it
= 1;
3241 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
3243 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
3244 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3245 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3246 free_nfs_cb_data(data
);
3253 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3255 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
3256 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3266 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
3268 struct nfs_cb_data
*data
;
3270 data
= malloc(sizeof(struct nfs_cb_data
));
3272 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
3275 memset(data
, 0, sizeof(struct nfs_cb_data
));
3278 data
->private_data
= private_data
;
3279 data
->continue_int
= mode
;
3280 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3281 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3282 if (data
->fh
.data
.data_val
== NULL
) {
3283 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3284 free_nfs_cb_data(data
);
3287 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3289 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
3301 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3303 struct nfs_cb_data
*data
= private_data
;
3304 struct nfs_context
*nfs
= data
->nfs
;
3307 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3309 if (status
== RPC_STATUS_ERROR
) {
3310 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3311 free_nfs_cb_data(data
);
3314 if (status
== RPC_STATUS_CANCEL
) {
3315 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3316 free_nfs_cb_data(data
);
3321 if (res
->status
!= NFS3_OK
) {
3322 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3323 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3324 free_nfs_cb_data(data
);
3328 data
->cb(0, nfs
, NULL
, data
->private_data
);
3329 free_nfs_cb_data(data
);
3332 struct nfs_chown_data
{
3337 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3340 struct nfs_chown_data
*chown_data
= data
->continue_data
;
3342 memset(&args
, 0, sizeof(SETATTR3args
));
3343 args
.object
= data
->fh
;
3344 if (chown_data
->uid
!= (uid_t
)-1) {
3345 args
.new_attributes
.uid
.set_it
= 1;
3346 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
3348 if (chown_data
->gid
!= (gid_t
)-1) {
3349 args
.new_attributes
.gid
.set_it
= 1;
3350 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
3353 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
3354 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3355 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3356 free_nfs_cb_data(data
);
3363 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3365 struct nfs_chown_data
*chown_data
;
3367 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3368 if (chown_data
== NULL
) {
3369 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3373 chown_data
->uid
= uid
;
3374 chown_data
->gid
= gid
;
3376 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
3377 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3388 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
3390 struct nfs_cb_data
*data
;
3391 struct nfs_chown_data
*chown_data
;
3393 chown_data
= malloc(sizeof(struct nfs_chown_data
));
3394 if (chown_data
== NULL
) {
3395 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
3399 chown_data
->uid
= uid
;
3400 chown_data
->gid
= gid
;
3402 data
= malloc(sizeof(struct nfs_cb_data
));
3404 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
3408 memset(data
, 0, sizeof(struct nfs_cb_data
));
3411 data
->private_data
= private_data
;
3412 data
->continue_data
= chown_data
;
3413 data
->free_continue_data
= free
;
3414 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
3415 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
3416 if (data
->fh
.data
.data_val
== NULL
) {
3417 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
3418 free_nfs_cb_data(data
);
3421 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
3423 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
3437 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3439 struct nfs_cb_data
*data
= private_data
;
3440 struct nfs_context
*nfs
= data
->nfs
;
3443 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3445 if (status
== RPC_STATUS_ERROR
) {
3446 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3447 free_nfs_cb_data(data
);
3450 if (status
== RPC_STATUS_CANCEL
) {
3451 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3452 free_nfs_cb_data(data
);
3457 if (res
->status
!= NFS3_OK
) {
3458 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
3459 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3460 free_nfs_cb_data(data
);
3464 data
->cb(0, nfs
, NULL
, data
->private_data
);
3465 free_nfs_cb_data(data
);
3468 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3471 struct timeval
*utimes_data
= data
->continue_data
;
3473 memset(&args
, 0, sizeof(SETATTR3args
));
3474 args
.object
= data
->fh
;
3475 if (utimes_data
!= NULL
) {
3476 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
3477 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
3478 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
3479 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
3480 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
3481 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
3483 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
3484 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
3487 if (rpc_nfs3_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
3488 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
3489 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3490 free_nfs_cb_data(data
);
3497 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
3499 struct timeval
*new_times
= NULL
;
3501 if (times
!= NULL
) {
3502 new_times
= malloc(sizeof(struct timeval
)*2);
3503 if (new_times
== NULL
) {
3504 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3508 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3511 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3512 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3522 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3524 struct timeval
*new_times
= NULL
;
3526 if (times
!= NULL
) {
3527 new_times
= malloc(sizeof(struct timeval
)*2);
3528 if (new_times
== NULL
) {
3529 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3533 new_times
[0].tv_sec
= times
->actime
;
3534 new_times
[0].tv_usec
= 0;
3535 new_times
[1].tv_sec
= times
->modtime
;
3536 new_times
[1].tv_usec
= 0;
3539 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3540 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3551 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3554 struct nfs_cb_data
*data
= private_data
;
3555 struct nfs_context
*nfs
= data
->nfs
;
3556 unsigned int nfsmode
= 0;
3558 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3560 if (status
== RPC_STATUS_ERROR
) {
3561 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3562 free_nfs_cb_data(data
);
3565 if (status
== RPC_STATUS_CANCEL
) {
3566 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3567 free_nfs_cb_data(data
);
3572 if (res
->status
!= NFS3_OK
) {
3573 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
));
3574 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3575 free_nfs_cb_data(data
);
3579 if (data
->continue_int
& R_OK
) {
3580 nfsmode
|= ACCESS3_READ
;
3582 if (data
->continue_int
& W_OK
) {
3583 nfsmode
|= ACCESS3_MODIFY
;
3585 if (data
->continue_int
& X_OK
) {
3586 nfsmode
|= ACCESS3_EXECUTE
;
3589 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3590 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3591 nfsmode
&ACCESS3_READ
?'r':'-',
3592 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3593 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3594 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3595 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3596 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3597 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3598 free_nfs_cb_data(data
);
3602 data
->cb(0, nfs
, NULL
, data
->private_data
);
3603 free_nfs_cb_data(data
);
3606 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3611 if (data
->continue_int
& R_OK
) {
3612 nfsmode
|= ACCESS3_READ
;
3614 if (data
->continue_int
& W_OK
) {
3615 nfsmode
|= ACCESS3_MODIFY
;
3617 if (data
->continue_int
& X_OK
) {
3618 nfsmode
|= ACCESS3_EXECUTE
;
3621 memset(&args
, 0, sizeof(ACCESS3args
));
3622 args
.object
= data
->fh
;
3623 args
.access
= nfsmode
;
3625 if (rpc_nfs3_access_async(nfs
->rpc
, nfs_access_cb
, &args
, data
) != 0) {
3626 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3627 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3628 free_nfs_cb_data(data
);
3634 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3636 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3637 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3649 struct nfs_symlink_data
{
3651 char *newpathparent
;
3652 char *newpathobject
;
3655 static void free_nfs_symlink_data(void *mem
)
3657 struct nfs_symlink_data
*data
= mem
;
3659 if (data
->oldpath
!= NULL
) {
3660 free(data
->oldpath
);
3662 if (data
->newpathparent
!= NULL
) {
3663 free(data
->newpathparent
);
3665 if (data
->newpathobject
!= NULL
) {
3666 free(data
->newpathobject
);
3671 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3674 struct nfs_cb_data
*data
= private_data
;
3675 struct nfs_context
*nfs
= data
->nfs
;
3676 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3678 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3680 if (status
== RPC_STATUS_ERROR
) {
3681 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3682 free_nfs_cb_data(data
);
3685 if (status
== RPC_STATUS_CANCEL
) {
3686 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3687 free_nfs_cb_data(data
);
3692 if (res
->status
!= NFS3_OK
) {
3693 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
));
3694 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3695 free_nfs_cb_data(data
);
3699 data
->cb(0, nfs
, NULL
, data
->private_data
);
3700 free_nfs_cb_data(data
);
3703 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3705 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3708 memset(&args
, 0, sizeof(SYMLINK3args
));
3709 args
.where
.dir
= data
->fh
;
3710 args
.where
.name
= symlink_data
->newpathobject
;
3711 args
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3712 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
;
3713 args
.symlink
.symlink_data
= symlink_data
->oldpath
;
3715 if (rpc_nfs3_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &args
, data
) != 0) {
3716 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3717 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3718 free_nfs_cb_data(data
);
3724 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3727 struct nfs_symlink_data
*symlink_data
;
3729 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3730 if (symlink_data
== NULL
) {
3731 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3734 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3736 symlink_data
->oldpath
= strdup(oldpath
);
3737 if (symlink_data
->oldpath
== NULL
) {
3738 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3739 free_nfs_symlink_data(symlink_data
);
3743 symlink_data
->newpathparent
= strdup(newpath
);
3744 if (symlink_data
->newpathparent
== NULL
) {
3745 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3746 free_nfs_symlink_data(symlink_data
);
3750 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3752 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3753 free_nfs_symlink_data(symlink_data
);
3759 symlink_data
->newpathobject
= strdup(ptr
);
3760 if (symlink_data
->newpathobject
== NULL
) {
3761 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3762 free_nfs_symlink_data(symlink_data
);
3766 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3767 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3779 struct nfs_rename_data
{
3782 struct nfs_fh3 olddir
;
3785 struct nfs_fh3 newdir
;
3788 static void free_nfs_rename_data(void *mem
)
3790 struct nfs_rename_data
*data
= mem
;
3792 if (data
->oldpath
!= NULL
) {
3793 free(data
->oldpath
);
3795 if (data
->olddir
.data
.data_val
!= NULL
) {
3796 free(data
->olddir
.data
.data_val
);
3798 if (data
->newpath
!= NULL
) {
3799 free(data
->newpath
);
3801 if (data
->newdir
.data
.data_val
!= NULL
) {
3802 free(data
->newdir
.data
.data_val
);
3807 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3810 struct nfs_cb_data
*data
= private_data
;
3811 struct nfs_context
*nfs
= data
->nfs
;
3812 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3814 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3816 if (status
== RPC_STATUS_ERROR
) {
3817 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3818 free_nfs_cb_data(data
);
3821 if (status
== RPC_STATUS_CANCEL
) {
3822 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3823 free_nfs_cb_data(data
);
3828 if (res
->status
!= NFS3_OK
) {
3829 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
));
3830 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3831 free_nfs_cb_data(data
);
3835 data
->cb(0, nfs
, NULL
, data
->private_data
);
3836 free_nfs_cb_data(data
);
3839 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3841 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3844 /* steal the filehandle */
3845 rename_data
->newdir
= data
->fh
;
3846 data
->fh
.data
.data_val
= NULL
;
3848 args
.from
.dir
= rename_data
->olddir
;
3849 args
.from
.name
= rename_data
->oldobject
;
3850 args
.to
.dir
= rename_data
->newdir
;
3851 args
.to
.name
= rename_data
->newobject
;
3852 if (rpc_nfs3_rename_async(nfs
->rpc
, nfs_rename_cb
, &args
, data
) != 0) {
3853 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3854 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3855 free_nfs_cb_data(data
);
3862 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3864 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3865 char* newpath
= strdup(rename_data
->newpath
);
3867 rpc_set_error(nfs
->rpc
, "Out of memory. Could not allocate memory to store target path for rename");
3868 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3869 free_nfs_cb_data(data
);
3873 /* steal the filehandle */
3874 rename_data
->olddir
= data
->fh
;
3875 data
->fh
.data
.data_val
= NULL
;
3877 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) {
3878 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", newpath
);
3879 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3880 free_nfs_cb_data(data
);
3884 data
->continue_data
= NULL
;
3885 free_nfs_cb_data(data
);
3892 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3895 struct nfs_rename_data
*rename_data
;
3897 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3898 if (rename_data
== NULL
) {
3899 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3902 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3904 rename_data
->oldpath
= strdup(oldpath
);
3905 if (rename_data
->oldpath
== NULL
) {
3906 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3907 free_nfs_rename_data(rename_data
);
3910 ptr
= strrchr(rename_data
->oldpath
, '/');
3912 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3913 free_nfs_rename_data(rename_data
);
3918 rename_data
->oldobject
= ptr
;
3921 rename_data
->newpath
= strdup(newpath
);
3922 if (rename_data
->newpath
== NULL
) {
3923 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3924 free_nfs_rename_data(rename_data
);
3927 ptr
= strrchr(rename_data
->newpath
, '/');
3929 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3930 free_nfs_rename_data(rename_data
);
3935 rename_data
->newobject
= ptr
;
3938 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3939 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3950 struct nfs_link_data
{
3952 struct nfs_fh3 oldfh
;
3955 struct nfs_fh3 newdir
;
3958 static void free_nfs_link_data(void *mem
)
3960 struct nfs_link_data
*data
= mem
;
3962 if (data
->oldpath
!= NULL
) {
3963 free(data
->oldpath
);
3965 if (data
->oldfh
.data
.data_val
!= NULL
) {
3966 free(data
->oldfh
.data
.data_val
);
3968 if (data
->newpath
!= NULL
) {
3969 free(data
->newpath
);
3971 if (data
->newdir
.data
.data_val
!= NULL
) {
3972 free(data
->newdir
.data
.data_val
);
3977 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3980 struct nfs_cb_data
*data
= private_data
;
3981 struct nfs_context
*nfs
= data
->nfs
;
3982 struct nfs_link_data
*link_data
= data
->continue_data
;
3984 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3986 if (status
== RPC_STATUS_ERROR
) {
3987 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3988 free_nfs_cb_data(data
);
3991 if (status
== RPC_STATUS_CANCEL
) {
3992 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3993 free_nfs_cb_data(data
);
3998 if (res
->status
!= NFS3_OK
) {
3999 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
));
4000 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4001 free_nfs_cb_data(data
);
4005 data
->cb(0, nfs
, NULL
, data
->private_data
);
4006 free_nfs_cb_data(data
);
4009 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4011 struct nfs_link_data
*link_data
= data
->continue_data
;
4014 /* steal the filehandle */
4015 link_data
->newdir
= data
->fh
;
4016 data
->fh
.data
.data_val
= NULL
;
4018 memset(&args
, 0, sizeof(LINK3args
));
4019 args
.file
= link_data
->oldfh
;
4020 args
.link
.dir
= link_data
->newdir
;
4021 args
.link
.name
= link_data
->newobject
;
4022 if (rpc_nfs3_link_async(nfs
->rpc
, nfs_link_cb
, &args
, data
) != 0) {
4023 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
4024 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4025 free_nfs_cb_data(data
);
4032 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
4034 struct nfs_link_data
*link_data
= data
->continue_data
;
4036 /* steal the filehandle */
4037 link_data
->oldfh
= data
->fh
;
4038 data
->fh
.data
.data_val
= NULL
;
4040 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) {
4041 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
4042 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
4043 free_nfs_cb_data(data
);
4046 data
->continue_data
= NULL
;
4047 free_nfs_cb_data(data
);
4053 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
4056 struct nfs_link_data
*link_data
;
4058 link_data
= malloc(sizeof(struct nfs_link_data
));
4059 if (link_data
== NULL
) {
4060 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
4063 memset(link_data
, 0, sizeof(struct nfs_link_data
));
4065 link_data
->oldpath
= strdup(oldpath
);
4066 if (link_data
->oldpath
== NULL
) {
4067 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
4068 free_nfs_link_data(link_data
);
4072 link_data
->newpath
= strdup(newpath
);
4073 if (link_data
->newpath
== NULL
) {
4074 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
4075 free_nfs_link_data(link_data
);
4078 ptr
= strrchr(link_data
->newpath
, '/');
4080 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
4081 free_nfs_link_data(link_data
);
4086 link_data
->newobject
= ptr
;
4089 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
4090 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
4098 //qqq replace later with lseek()
4099 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
4101 return nfsfh
->offset
;
4107 * Get the maximum supported READ3 size by the server
4109 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
4111 return nfs
->readmax
;
4115 * Get the maximum supported WRITE3 size by the server
4117 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
4119 return nfs
->writemax
;
4122 void nfs_set_tcp_syncnt(struct nfs_context
*nfs
, int v
) {
4123 rpc_set_tcp_syncnt(nfs
->rpc
, v
);
4126 void nfs_set_uid(struct nfs_context
*nfs
, int uid
) {
4127 rpc_set_uid(nfs
->rpc
, uid
);
4130 void nfs_set_gid(struct nfs_context
*nfs
, int gid
) {
4131 rpc_set_gid(nfs
->rpc
, gid
);
4134 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
4139 va_start(ap
, error_string
);
4141 vsnprintf(str
, 1024, error_string
, ap
);
4142 if (nfs
->rpc
->error_string
!= NULL
) {
4143 free(nfs
->rpc
->error_string
);
4145 nfs
->rpc
->error_string
= str
;
4151 struct mount_cb_data
{
4157 static void free_mount_cb_data(struct mount_cb_data
*data
)
4159 if (data
->server
!= NULL
) {
4161 data
->server
= NULL
;
4167 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4169 struct mount_cb_data
*data
= private_data
;
4171 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4173 if (status
== RPC_STATUS_ERROR
) {
4174 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4175 free_mount_cb_data(data
);
4178 if (status
== RPC_STATUS_CANCEL
) {
4179 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4180 free_mount_cb_data(data
);
4184 data
->cb(rpc
, 0, command_data
, data
->private_data
);
4185 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
4186 rpc_set_error(rpc
, "Failed to disconnect\n");
4188 free_mount_cb_data(data
);
4191 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4193 struct mount_cb_data
*data
= private_data
;
4195 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4197 /* Dont want any more callbacks even if the socket is closed */
4198 rpc
->connect_cb
= NULL
;
4200 if (status
== RPC_STATUS_ERROR
) {
4201 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4202 free_mount_cb_data(data
);
4205 if (status
== RPC_STATUS_CANCEL
) {
4206 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4207 free_mount_cb_data(data
);
4211 if (rpc_mount3_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
4212 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4213 free_mount_cb_data(data
);
4218 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4220 struct mount_cb_data
*data
= private_data
;
4221 uint32_t mount_port
;
4223 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4225 if (status
== RPC_STATUS_ERROR
) {
4226 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4227 free_mount_cb_data(data
);
4230 if (status
== RPC_STATUS_CANCEL
) {
4231 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4232 free_mount_cb_data(data
);
4236 mount_port
= *(uint32_t *)command_data
;
4237 if (mount_port
== 0) {
4238 rpc_set_error(rpc
, "RPC error. Mount program is not available");
4239 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
4240 free_mount_cb_data(data
);
4244 rpc_disconnect(rpc
, "normal disconnect");
4245 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
4246 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4247 free_mount_cb_data(data
);
4252 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4254 struct mount_cb_data
*data
= private_data
;
4256 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4258 if (status
== RPC_STATUS_ERROR
) {
4259 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4260 free_mount_cb_data(data
);
4263 if (status
== RPC_STATUS_CANCEL
) {
4264 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4265 free_mount_cb_data(data
);
4269 if (rpc_pmap2_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
4270 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4271 free_mount_cb_data(data
);
4276 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
4278 struct mount_cb_data
*data
= private_data
;
4280 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4282 /* Dont want any more callbacks even if the socket is closed */
4283 rpc
->connect_cb
= NULL
;
4285 if (status
== RPC_STATUS_ERROR
) {
4286 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
4287 free_mount_cb_data(data
);
4290 if (status
== RPC_STATUS_CANCEL
) {
4291 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
4292 free_mount_cb_data(data
);
4296 if (rpc_pmap2_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
4297 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
4298 free_mount_cb_data(data
);
4303 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
4305 struct mount_cb_data
*data
;
4307 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
4309 data
= malloc(sizeof(struct mount_cb_data
));
4313 memset(data
, 0, sizeof(struct mount_cb_data
));
4315 data
->private_data
= private_data
;
4316 data
->server
= strdup(server
);
4317 if (data
->server
== NULL
) {
4318 free_mount_cb_data(data
);
4321 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
4322 free_mount_cb_data(data
);
4329 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
4331 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
4335 const char *nfs_get_server(struct nfs_context
*nfs
) {
4339 const char *nfs_get_export(struct nfs_context
*nfs
) {
4343 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
4344 return &nfs
->rootfh
;
4347 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {