2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * High level api to nfs filesystems
25 #include "aros_compat.h"
29 #include "win32_compat.h"
37 #define statvfs statfs
50 #ifdef HAVE_SYS_STATVFS_H
51 #include <sys/statvfs.h>
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
68 #include <sys/types.h>
71 #include "libnfs-zdr.h"
73 #include "libnfs-raw.h"
74 #include "libnfs-raw-mount.h"
75 #include "libnfs-raw-nfs.h"
76 #include "libnfs-private.h"
79 struct nfsdirent
*entries
;
80 struct nfsdirent
*current
;
90 struct rpc_context
*rpc
;
93 struct nfs_fh3 rootfh
;
98 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
100 while (nfsdir
->entries
) {
101 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
102 if (nfsdir
->entries
->name
!= NULL
) {
103 free(nfsdir
->entries
->name
);
105 free(nfsdir
->entries
);
106 nfsdir
->entries
= dirent
;
112 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
115 struct nfs_context
*nfs
;
117 char *saved_path
, *path
;
122 continue_func continue_cb
;
124 void (*free_continue_data
)(void *);
129 /* for multi-read/write calls. */
133 uint64_t start_offset
, max_offset
;
137 struct nfs_mcb_data
{
138 struct nfs_cb_data
*data
;
143 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
146 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
148 rpc_set_auth(nfs
->rpc
, auth
);
151 int nfs_get_fd(struct nfs_context
*nfs
)
153 return rpc_get_fd(nfs
->rpc
);
156 int nfs_queue_length(struct nfs_context
*nfs
)
158 return rpc_queue_length(nfs
->rpc
);
161 int nfs_which_events(struct nfs_context
*nfs
)
163 return rpc_which_events(nfs
->rpc
);
166 int nfs_service(struct nfs_context
*nfs
, int revents
)
168 return rpc_service(nfs
->rpc
, revents
);
171 char *nfs_get_error(struct nfs_context
*nfs
)
173 return rpc_get_error(nfs
->rpc
);
176 struct nfs_context
*nfs_init_context(void)
178 struct nfs_context
*nfs
;
180 nfs
= malloc(sizeof(struct nfs_context
));
184 nfs
->rpc
= rpc_init_context();
185 if (nfs
->rpc
== NULL
) {
193 nfs
->rootfh
.data
.data_len
= 0;
194 nfs
->rootfh
.data
.data_val
= NULL
;
199 void nfs_destroy_context(struct nfs_context
*nfs
)
201 rpc_destroy_context(nfs
->rpc
);
214 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
215 free(nfs
->rootfh
.data
.data_val
);
216 nfs
->rootfh
.data
.data_val
= NULL
;
231 void free_rpc_cb_data(struct rpc_cb_data
*data
)
238 static void rpc_connect_program_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
240 struct rpc_cb_data
*data
= private_data
;
242 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
244 /* Dont want any more callbacks even if the socket is closed */
245 rpc
->connect_cb
= NULL
;
247 if (status
== RPC_STATUS_ERROR
) {
248 data
->cb(rpc
, status
, command_data
, data
->private_data
);
249 free_rpc_cb_data(data
);
252 if (status
== RPC_STATUS_CANCEL
) {
253 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
254 free_rpc_cb_data(data
);
258 data
->cb(rpc
, status
, NULL
, data
->private_data
);
259 free_rpc_cb_data(data
);
262 static void rpc_connect_program_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
264 struct rpc_cb_data
*data
= private_data
;
267 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
269 if (status
== RPC_STATUS_ERROR
) {
270 data
->cb(rpc
, status
, command_data
, data
->private_data
);
271 free_rpc_cb_data(data
);
274 if (status
== RPC_STATUS_CANCEL
) {
275 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
276 free_rpc_cb_data(data
);
280 rpc_port
= *(uint32_t *)command_data
;
282 rpc_set_error(rpc
, "RPC error. Program is not available on %s", data
->server
);
283 data
->cb(rpc
, RPC_STATUS_ERROR
, rpc_get_error(rpc
), data
->private_data
);
284 free_rpc_cb_data(data
);
288 rpc_disconnect(rpc
, "normal disconnect");
289 if (rpc_connect_async(rpc
, data
->server
, rpc_port
, rpc_connect_program_4_cb
, data
) != 0) {
290 data
->cb(rpc
, status
, command_data
, data
->private_data
);
291 free_rpc_cb_data(data
);
296 static void rpc_connect_program_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
298 struct rpc_cb_data
*data
= private_data
;
300 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
302 if (status
== RPC_STATUS_ERROR
) {
303 data
->cb(rpc
, status
, command_data
, data
->private_data
);
304 free_rpc_cb_data(data
);
307 if (status
== RPC_STATUS_CANCEL
) {
308 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
309 free_rpc_cb_data(data
);
313 if (rpc_pmap_getport_async(rpc
, data
->program
, data
->version
, IPPROTO_TCP
, rpc_connect_program_3_cb
, private_data
) != 0) {
314 data
->cb(rpc
, status
, command_data
, data
->private_data
);
315 free_rpc_cb_data(data
);
320 static void rpc_connect_program_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
322 struct rpc_cb_data
*data
= private_data
;
324 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
326 /* Dont want any more callbacks even if the socket is closed */
327 rpc
->connect_cb
= NULL
;
329 if (status
== RPC_STATUS_ERROR
) {
330 data
->cb(rpc
, status
, command_data
, data
->private_data
);
331 free_rpc_cb_data(data
);
334 if (status
== RPC_STATUS_CANCEL
) {
335 data
->cb(rpc
, status
, "Command was cancelled", data
->private_data
);
336 free_rpc_cb_data(data
);
340 if (rpc_pmap_null_async(rpc
, rpc_connect_program_2_cb
, data
) != 0) {
341 data
->cb(rpc
, status
, command_data
, data
->private_data
);
342 free_rpc_cb_data(data
);
347 int rpc_connect_program_async(struct rpc_context
*rpc
, char *server
, int program
, int version
, rpc_cb cb
, void *private_data
)
349 struct rpc_cb_data
*data
;
351 data
= malloc(sizeof(struct rpc_cb_data
));
355 memset(data
, 0, sizeof(struct rpc_cb_data
));
356 data
->server
= strdup(server
);
357 data
->program
= program
;
358 data
->version
= version
;
361 data
->private_data
= private_data
;
363 if (rpc_connect_async(rpc
, server
, 111, rpc_connect_program_1_cb
, data
) != 0) {
364 rpc_set_error(rpc
, "Failed to start connection");
365 free_rpc_cb_data(data
);
371 void free_nfs_cb_data(struct nfs_cb_data
*data
)
373 if (data
->saved_path
!= NULL
) {
374 free(data
->saved_path
);
375 data
->saved_path
= NULL
;
378 if (data
->continue_data
!= NULL
) {
379 data
->free_continue_data(data
->continue_data
);
380 data
->continue_data
= NULL
;
383 if (data
->fh
.data
.data_val
!= NULL
) {
384 free(data
->fh
.data
.data_val
);
385 data
->fh
.data
.data_val
= NULL
;
388 if (data
->buffer
!= NULL
) {
400 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
402 struct nfs_cb_data
*data
= private_data
;
403 struct nfs_context
*nfs
= data
->nfs
;
405 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
407 if (status
== RPC_STATUS_ERROR
) {
408 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
409 free_nfs_cb_data(data
);
412 if (status
== RPC_STATUS_CANCEL
) {
413 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
414 free_nfs_cb_data(data
);
418 data
->cb(0, nfs
, NULL
, data
->private_data
);
419 free_nfs_cb_data(data
);
422 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
424 struct nfs_cb_data
*data
= private_data
;
425 struct nfs_context
*nfs
= data
->nfs
;
426 FSINFO3res
*res
= command_data
;
428 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
430 if (status
== RPC_STATUS_ERROR
) {
431 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
432 free_nfs_cb_data(data
);
435 if (status
== RPC_STATUS_CANCEL
) {
436 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
437 free_nfs_cb_data(data
);
441 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
442 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
444 if (rpc_nfs_getattr_async(rpc
, nfs_mount_10_cb
, &nfs
->rootfh
, data
) != 0) {
445 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
446 free_nfs_cb_data(data
);
451 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
453 struct nfs_cb_data
*data
= private_data
;
454 struct nfs_context
*nfs
= data
->nfs
;
456 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
458 if (status
== RPC_STATUS_ERROR
) {
459 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
460 free_nfs_cb_data(data
);
463 if (status
== RPC_STATUS_CANCEL
) {
464 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
465 free_nfs_cb_data(data
);
469 if (rpc_nfs_fsinfo_async(rpc
, nfs_mount_9_cb
, &nfs
->rootfh
, data
) != 0) {
470 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
471 free_nfs_cb_data(data
);
477 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
479 struct nfs_cb_data
*data
= private_data
;
480 struct nfs_context
*nfs
= data
->nfs
;
482 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
484 /* Dont want any more callbacks even if the socket is closed */
485 rpc
->connect_cb
= NULL
;
487 if (status
== RPC_STATUS_ERROR
) {
488 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
489 free_nfs_cb_data(data
);
492 if (status
== RPC_STATUS_CANCEL
) {
493 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
494 free_nfs_cb_data(data
);
498 if (rpc_nfs_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
499 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
500 free_nfs_cb_data(data
);
506 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
508 struct nfs_cb_data
*data
= private_data
;
509 struct nfs_context
*nfs
= data
->nfs
;
512 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
514 if (status
== RPC_STATUS_ERROR
) {
515 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
516 free_nfs_cb_data(data
);
519 if (status
== RPC_STATUS_CANCEL
) {
520 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
521 free_nfs_cb_data(data
);
526 if (res
->fhs_status
!= MNT3_OK
) {
527 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
));
528 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
529 free_nfs_cb_data(data
);
533 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
534 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
535 if (nfs
->rootfh
.data
.data_val
== NULL
) {
536 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
537 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
538 free_nfs_cb_data(data
);
541 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
543 rpc_disconnect(rpc
, "normal disconnect");
544 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
545 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
546 free_nfs_cb_data(data
);
549 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
550 rpc_set_autoreconnect(rpc
);
554 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
556 struct nfs_cb_data
*data
= private_data
;
557 struct nfs_context
*nfs
= data
->nfs
;
559 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
561 if (status
== RPC_STATUS_ERROR
) {
562 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
563 free_nfs_cb_data(data
);
566 if (status
== RPC_STATUS_CANCEL
) {
567 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
568 free_nfs_cb_data(data
);
572 if (rpc_mount_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
573 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
574 free_nfs_cb_data(data
);
579 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
581 struct nfs_cb_data
*data
= private_data
;
582 struct nfs_context
*nfs
= data
->nfs
;
584 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
586 /* Dont want any more callbacks even if the socket is closed */
587 rpc
->connect_cb
= NULL
;
589 if (status
== RPC_STATUS_ERROR
) {
590 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
591 free_nfs_cb_data(data
);
594 if (status
== RPC_STATUS_CANCEL
) {
595 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
596 free_nfs_cb_data(data
);
600 if (rpc_mount_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
601 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
602 free_nfs_cb_data(data
);
607 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
609 struct nfs_cb_data
*data
= private_data
;
610 struct nfs_context
*nfs
= data
->nfs
;
613 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
615 if (status
== RPC_STATUS_ERROR
) {
616 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
617 free_nfs_cb_data(data
);
620 if (status
== RPC_STATUS_CANCEL
) {
621 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
622 free_nfs_cb_data(data
);
626 mount_port
= *(uint32_t *)command_data
;
627 if (mount_port
== 0) {
628 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
629 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
630 free_nfs_cb_data(data
);
634 rpc_disconnect(rpc
, "normal disconnect");
635 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
636 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
637 free_nfs_cb_data(data
);
643 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
645 struct nfs_cb_data
*data
= private_data
;
646 struct nfs_context
*nfs
= data
->nfs
;
648 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
650 if (status
== RPC_STATUS_ERROR
) {
651 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
652 free_nfs_cb_data(data
);
655 if (status
== RPC_STATUS_CANCEL
) {
656 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
657 free_nfs_cb_data(data
);
661 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
662 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
663 free_nfs_cb_data(data
);
668 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
670 struct nfs_cb_data
*data
= private_data
;
671 struct nfs_context
*nfs
= data
->nfs
;
673 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
675 /* Dont want any more callbacks even if the socket is closed */
676 rpc
->connect_cb
= NULL
;
678 if (status
== RPC_STATUS_ERROR
) {
679 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
680 free_nfs_cb_data(data
);
683 if (status
== RPC_STATUS_CANCEL
) {
684 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
685 free_nfs_cb_data(data
);
689 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
690 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
691 free_nfs_cb_data(data
);
697 * Async call for mounting an nfs share and geting the root filehandle
699 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
701 struct nfs_cb_data
*data
;
702 char *new_server
, *new_export
;
704 data
= malloc(sizeof(struct nfs_cb_data
));
706 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
709 memset(data
, 0, sizeof(struct nfs_cb_data
));
710 new_server
= strdup(server
);
711 new_export
= strdup(export
);
712 if (nfs
->server
!= NULL
) {
715 nfs
->server
= new_server
;
716 if (nfs
->export
!= NULL
) {
719 nfs
->export
= new_export
;
722 data
->private_data
= private_data
;
724 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
725 rpc_set_error(nfs
->rpc
, "Failed to start connection");
726 free_nfs_cb_data(data
);
736 * Functions to first look up a path, component by component, and then finally call a specific function once
737 * the filehandle for the final component is found.
739 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
741 struct nfs_cb_data
*data
= private_data
;
742 struct nfs_context
*nfs
= data
->nfs
;
745 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
747 if (status
== RPC_STATUS_ERROR
) {
748 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
749 free_nfs_cb_data(data
);
752 if (status
== RPC_STATUS_CANCEL
) {
753 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
754 free_nfs_cb_data(data
);
759 if (res
->status
!= NFS3_OK
) {
760 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
));
761 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
762 free_nfs_cb_data(data
);
766 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
767 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
768 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
769 free_nfs_cb_data(data
);
774 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
778 while (*data
->path
== '/') {
783 str
= strchr(path
, '/');
788 while (*data
->path
!= 0) {
794 data
->fh
.data
.data_len
= fh
->data
.data_len
;
795 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
796 if (data
->fh
.data
.data_val
== NULL
) {
797 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
798 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
799 free_nfs_cb_data(data
);
802 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
803 data
->continue_cb(nfs
, data
);
807 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, fh
, path
, data
) != 0) {
808 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
809 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
810 free_nfs_cb_data(data
);
816 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
)
818 struct nfs_cb_data
*data
;
820 if (path
[0] != 0 && path
[0] != '/') {
821 rpc_set_error(nfs
->rpc
, "Pathname is not absolute %s", path
);
825 data
= malloc(sizeof(struct nfs_cb_data
));
827 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
830 memset(data
, 0, sizeof(struct nfs_cb_data
));
833 data
->continue_cb
= continue_cb
;
834 data
->continue_data
= continue_data
;
835 data
->free_continue_data
= free_continue_data
;
836 data
->continue_int
= continue_int
;
837 data
->private_data
= private_data
;
838 data
->saved_path
= strdup(path
);
839 if (data
->saved_path
== NULL
) {
840 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
841 free_nfs_cb_data(data
);
844 data
->path
= data
->saved_path
;
846 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
847 /* return 0 here since the callback will be invoked if there is a failure */
860 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
863 struct nfs_cb_data
*data
= private_data
;
864 struct nfs_context
*nfs
= data
->nfs
;
867 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
869 if (status
== RPC_STATUS_ERROR
) {
870 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
871 free_nfs_cb_data(data
);
874 if (status
== RPC_STATUS_CANCEL
) {
875 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
876 free_nfs_cb_data(data
);
881 if (res
->status
!= NFS3_OK
) {
882 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
));
883 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
884 free_nfs_cb_data(data
);
889 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
890 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
891 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
892 st
.st_mode
|= S_IFDIR
;
894 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
895 st
.st_mode
|= S_IFREG
;
897 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
898 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
899 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
901 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
903 st
.st_blksize
= 4096;
904 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
906 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
907 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
908 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
910 data
->cb(0, nfs
, &st
, data
->private_data
);
911 free_nfs_cb_data(data
);
914 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
916 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &data
->fh
, data
) != 0) {
917 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
918 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
919 free_nfs_cb_data(data
);
925 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
927 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
928 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
942 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
945 struct nfs_cb_data
*data
= private_data
;
946 struct nfs_context
*nfs
= data
->nfs
;
948 unsigned int nfsmode
= 0;
950 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
952 if (status
== RPC_STATUS_ERROR
) {
953 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
954 free_nfs_cb_data(data
);
957 if (status
== RPC_STATUS_CANCEL
) {
958 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
959 free_nfs_cb_data(data
);
964 if (res
->status
!= NFS3_OK
) {
965 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
));
966 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
967 free_nfs_cb_data(data
);
971 if (data
->continue_int
& O_WRONLY
) {
972 nfsmode
|= ACCESS3_MODIFY
;
974 if (data
->continue_int
& O_RDWR
) {
975 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
977 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
978 nfsmode
|= ACCESS3_READ
;
982 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
983 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
984 nfsmode
&ACCESS3_READ
?'r':'-',
985 nfsmode
&ACCESS3_MODIFY
?'w':'-',
986 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
987 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
988 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
989 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
990 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
991 free_nfs_cb_data(data
);
995 nfsfh
= malloc(sizeof(struct nfsfh
));
997 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
998 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
999 free_nfs_cb_data(data
);
1002 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1004 if (data
->continue_int
& O_SYNC
) {
1008 /* steal the filehandle */
1009 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
1010 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
1011 data
->fh
.data
.data_val
= NULL
;
1013 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1014 free_nfs_cb_data(data
);
1017 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1021 if (data
->continue_int
& O_WRONLY
) {
1022 nfsmode
|= ACCESS3_MODIFY
;
1024 if (data
->continue_int
& O_RDWR
) {
1025 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
1027 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
1028 nfsmode
|= ACCESS3_READ
;
1031 if (rpc_nfs_access_async(nfs
->rpc
, nfs_open_cb
, &data
->fh
, nfsmode
, data
) != 0) {
1032 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
1033 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1034 free_nfs_cb_data(data
);
1040 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1042 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
1043 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1057 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1059 struct nfs_cb_data
*data
= private_data
;
1060 struct nfs_context
*nfs
= data
->nfs
;
1063 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1065 if (status
== RPC_STATUS_ERROR
) {
1066 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1067 free_nfs_cb_data(data
);
1070 if (status
== RPC_STATUS_CANCEL
) {
1071 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1072 free_nfs_cb_data(data
);
1077 if (res
->status
!= NFS3_OK
) {
1078 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1079 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1080 free_nfs_cb_data(data
);
1084 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
1085 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
1086 free_nfs_cb_data(data
);
1089 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1091 struct nfs_mcb_data
*mdata
= private_data
;
1092 struct nfs_cb_data
*data
= mdata
->data
;
1093 struct nfs_context
*nfs
= data
->nfs
;
1096 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1100 if (status
== RPC_STATUS_ERROR
) {
1101 /* flag the failure but do not invoke callback until we have received all responses */
1104 if (status
== RPC_STATUS_CANCEL
) {
1105 /* flag the cancellation but do not invoke callback until we have received all responses */
1109 /* reassemble the data into the buffer */
1110 if (status
== RPC_STATUS_SUCCESS
) {
1112 if (res
->status
!= NFS3_OK
) {
1113 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1116 if (res
->READ3res_u
.resok
.count
> 0) {
1117 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
1118 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
1119 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
1125 if (data
->num_calls
> 0) {
1126 /* still waiting for more replies */
1131 if (data
->error
!= 0) {
1132 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1133 free_nfs_cb_data(data
);
1137 if (data
->cancel
!= 0) {
1138 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1139 free_nfs_cb_data(data
);
1144 data
->nfsfh
->offset
= data
->max_offset
;
1145 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
1147 free_nfs_cb_data(data
);
1151 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1153 struct nfs_cb_data
*data
;
1155 data
= malloc(sizeof(struct nfs_cb_data
));
1157 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1160 memset(data
, 0, sizeof(struct nfs_cb_data
));
1163 data
->private_data
= private_data
;
1164 data
->nfsfh
= nfsfh
;
1166 nfsfh
->offset
= offset
;
1168 if (count
<= nfs_get_readmax(nfs
)) {
1169 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_cb
, &nfsfh
->fh
, offset
, count
, data
) != 0) {
1170 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1171 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1172 free_nfs_cb_data(data
);
1178 /* trying to read more than maximum server read size, we has to chop it up into smaller
1179 * reads and collect into a reassembly buffer.
1180 * we send all reads in parallel so that performance is still good.
1182 data
->max_offset
= offset
;
1183 data
->start_offset
= offset
;
1185 data
->buffer
= malloc(count
);
1186 if (data
->buffer
== NULL
) {
1187 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1188 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1189 free_nfs_cb_data(data
);
1194 uint64_t readcount
= count
;
1195 struct nfs_mcb_data
*mdata
;
1197 if (readcount
> nfs_get_readmax(nfs
)) {
1198 readcount
= nfs_get_readmax(nfs
);
1201 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1202 if (mdata
== NULL
) {
1203 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1206 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1208 mdata
->offset
= offset
;
1209 mdata
->count
= readcount
;
1210 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_mcb
, &nfsfh
->fh
, offset
, readcount
, mdata
) != 0) {
1211 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1212 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1218 offset
+= readcount
;
1228 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1230 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1238 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1240 struct nfs_cb_data
*data
= private_data
;
1241 struct nfs_context
*nfs
= data
->nfs
;
1244 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1246 if (status
== RPC_STATUS_ERROR
) {
1247 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1248 free_nfs_cb_data(data
);
1251 if (status
== RPC_STATUS_CANCEL
) {
1252 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1253 free_nfs_cb_data(data
);
1258 if (res
->status
!= NFS3_OK
) {
1259 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1260 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1261 free_nfs_cb_data(data
);
1265 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1266 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1267 free_nfs_cb_data(data
);
1270 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1272 struct nfs_mcb_data
*mdata
= private_data
;
1273 struct nfs_cb_data
*data
= mdata
->data
;
1274 struct nfs_context
*nfs
= data
->nfs
;
1277 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1281 if (status
== RPC_STATUS_ERROR
) {
1282 /* flag the failure but do not invoke callback until we have received all responses */
1285 if (status
== RPC_STATUS_CANCEL
) {
1286 /* flag the cancellation but do not invoke callback until we have received all responses */
1290 if (status
== RPC_STATUS_SUCCESS
) {
1292 if (res
->status
!= NFS3_OK
) {
1293 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1296 if (res
->WRITE3res_u
.resok
.count
> 0) {
1297 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1298 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1304 if (data
->num_calls
> 0) {
1305 /* still waiting for more replies */
1310 if (data
->error
!= 0) {
1311 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1312 free_nfs_cb_data(data
);
1316 if (data
->cancel
!= 0) {
1317 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1318 free_nfs_cb_data(data
);
1323 data
->nfsfh
->offset
= data
->max_offset
;
1324 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1326 free_nfs_cb_data(data
);
1331 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
)
1333 struct nfs_cb_data
*data
;
1335 data
= malloc(sizeof(struct nfs_cb_data
));
1337 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1340 memset(data
, 0, sizeof(struct nfs_cb_data
));
1343 data
->private_data
= private_data
;
1344 data
->nfsfh
= nfsfh
;
1346 nfsfh
->offset
= offset
;
1348 if (count
<= nfs_get_writemax(nfs
)) {
1349 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_cb
, &nfsfh
->fh
, buf
, offset
, count
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, data
) != 0) {
1350 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1351 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1352 free_nfs_cb_data(data
);
1358 /* trying to write more than maximum server write size, we has to chop it up into smaller
1360 * we send all writes in parallel so that performance is still good.
1362 data
->max_offset
= offset
;
1363 data
->start_offset
= offset
;
1366 uint64_t writecount
= count
;
1367 struct nfs_mcb_data
*mdata
;
1369 if (writecount
> nfs_get_writemax(nfs
)) {
1370 writecount
= nfs_get_writemax(nfs
);
1373 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1374 if (mdata
== NULL
) {
1375 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1378 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1380 mdata
->offset
= offset
;
1381 mdata
->count
= writecount
;
1383 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_mcb
, &nfsfh
->fh
, &buf
[offset
- data
->start_offset
], offset
, writecount
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, mdata
) != 0) {
1384 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1385 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1390 count
-= writecount
;
1391 offset
+= writecount
;
1401 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1403 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1413 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1415 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1416 free(nfsfh
->fh
.data
.data_val
);
1417 nfsfh
->fh
.data
.data_val
= NULL
;
1421 cb(0, nfs
, NULL
, private_data
);
1432 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1434 struct nfs_cb_data
*data
;
1436 data
= malloc(sizeof(struct nfs_cb_data
));
1438 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1441 memset(data
, 0, sizeof(struct nfs_cb_data
));
1444 data
->private_data
= private_data
;
1446 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &nfsfh
->fh
, data
) != 0) {
1447 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1448 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1449 free_nfs_cb_data(data
);
1460 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1462 struct nfs_cb_data
*data
= private_data
;
1463 struct nfs_context
*nfs
= data
->nfs
;
1466 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1468 if (status
== RPC_STATUS_ERROR
) {
1469 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1470 free_nfs_cb_data(data
);
1473 if (status
== RPC_STATUS_CANCEL
) {
1474 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1475 free_nfs_cb_data(data
);
1480 if (res
->status
!= NFS3_OK
) {
1481 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1482 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1483 free_nfs_cb_data(data
);
1487 data
->cb(0, nfs
, NULL
, data
->private_data
);
1488 free_nfs_cb_data(data
);
1491 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1493 struct nfs_cb_data
*data
;
1495 data
= malloc(sizeof(struct nfs_cb_data
));
1497 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1500 memset(data
, 0, sizeof(struct nfs_cb_data
));
1503 data
->private_data
= private_data
;
1505 if (rpc_nfs_commit_async(nfs
->rpc
, nfs_fsync_cb
, &nfsfh
->fh
, data
) != 0) {
1506 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1507 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1508 free_nfs_cb_data(data
);
1520 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1522 struct nfs_cb_data
*data
= private_data
;
1523 struct nfs_context
*nfs
= data
->nfs
;
1526 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1528 if (status
== RPC_STATUS_ERROR
) {
1529 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1530 free_nfs_cb_data(data
);
1533 if (status
== RPC_STATUS_CANCEL
) {
1534 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1535 free_nfs_cb_data(data
);
1540 if (res
->status
!= NFS3_OK
) {
1541 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1542 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1543 free_nfs_cb_data(data
);
1547 data
->cb(0, nfs
, NULL
, data
->private_data
);
1548 free_nfs_cb_data(data
);
1551 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1553 struct nfs_cb_data
*data
;
1556 data
= malloc(sizeof(struct nfs_cb_data
));
1558 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1561 memset(data
, 0, sizeof(struct nfs_cb_data
));
1564 data
->private_data
= private_data
;
1566 memset(&args
, 0, sizeof(SETATTR3args
));
1567 args
.object
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
1568 args
.object
.data
.data_val
= nfsfh
->fh
.data
.data_val
;
1569 args
.new_attributes
.size
.set_it
= 1;
1570 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1572 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1573 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1574 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1575 free_nfs_cb_data(data
);
1585 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1587 uint64_t offset
= data
->continue_int
;
1590 nfsfh
.fh
.data
.data_val
= data
->fh
.data
.data_val
;
1591 nfsfh
.fh
.data
.data_len
= data
->fh
.data
.data_len
;
1593 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1594 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1595 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1596 free_nfs_cb_data(data
);
1599 free_nfs_cb_data(data
);
1603 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1609 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1610 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1623 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1626 struct nfs_cb_data
*data
= private_data
;
1627 struct nfs_context
*nfs
= data
->nfs
;
1628 char *str
= data
->continue_data
;
1630 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1632 str
= &str
[strlen(str
) + 1];
1634 if (status
== RPC_STATUS_ERROR
) {
1635 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1636 free_nfs_cb_data(data
);
1639 if (status
== RPC_STATUS_CANCEL
) {
1640 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1641 free_nfs_cb_data(data
);
1646 if (res
->status
!= NFS3_OK
) {
1647 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
));
1648 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1649 free_nfs_cb_data(data
);
1653 data
->cb(0, nfs
, NULL
, data
->private_data
);
1654 free_nfs_cb_data(data
);
1657 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1659 char *str
= data
->continue_data
;
1662 str
= &str
[strlen(str
) + 1];
1664 memset(&args
, 0, sizeof(MKDIR3args
));
1665 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1666 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1667 args
.where
.name
= str
;
1668 args
.attributes
.mode
.set_it
= 1;
1669 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
1671 if (rpc_nfs_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
1672 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1673 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1674 free_nfs_cb_data(data
);
1680 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1685 new_path
= strdup(path
);
1686 if (new_path
== NULL
) {
1687 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1691 ptr
= strrchr(new_path
, '/');
1693 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1698 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1699 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1700 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1714 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1717 struct nfs_cb_data
*data
= private_data
;
1718 struct nfs_context
*nfs
= data
->nfs
;
1719 char *str
= data
->continue_data
;
1721 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1723 str
= &str
[strlen(str
) + 1];
1725 if (status
== RPC_STATUS_ERROR
) {
1726 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1727 free_nfs_cb_data(data
);
1730 if (status
== RPC_STATUS_CANCEL
) {
1731 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1732 free_nfs_cb_data(data
);
1737 if (res
->status
!= NFS3_OK
) {
1738 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
));
1739 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1740 free_nfs_cb_data(data
);
1744 data
->cb(0, nfs
, NULL
, data
->private_data
);
1745 free_nfs_cb_data(data
);
1748 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1750 char *str
= data
->continue_data
;
1752 str
= &str
[strlen(str
) + 1];
1754 if (rpc_nfs_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &data
->fh
, str
, data
) != 0) {
1755 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1756 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1757 free_nfs_cb_data(data
);
1763 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1768 new_path
= strdup(path
);
1769 if (new_path
== NULL
) {
1770 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1774 ptr
= strrchr(new_path
, '/');
1776 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1781 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1782 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1783 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1796 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1799 struct nfs_cb_data
*data
= private_data
;
1800 struct nfs_context
*nfs
= data
->nfs
;
1801 struct nfsfh
*nfsfh
;
1802 char *str
= data
->continue_data
;
1804 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1806 if (status
== RPC_STATUS_ERROR
) {
1807 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1808 free_nfs_cb_data(data
);
1811 if (status
== RPC_STATUS_CANCEL
) {
1812 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1813 free_nfs_cb_data(data
);
1817 str
= &str
[strlen(str
) + 1];
1819 if (res
->status
!= NFS3_OK
) {
1820 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
));
1821 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1826 nfsfh
= malloc(sizeof(struct nfsfh
));
1827 if (nfsfh
== NULL
) {
1828 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1829 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1830 free_nfs_cb_data(data
);
1833 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1835 /* copy the filehandle */
1836 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
1837 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
1838 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
1840 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1841 free_nfs_cb_data(data
);
1846 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1849 struct nfs_cb_data
*data
= private_data
;
1850 struct nfs_context
*nfs
= data
->nfs
;
1851 char *str
= data
->continue_data
;
1853 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1855 if (status
== RPC_STATUS_ERROR
) {
1856 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1857 free_nfs_cb_data(data
);
1860 if (status
== RPC_STATUS_CANCEL
) {
1861 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1862 free_nfs_cb_data(data
);
1866 str
= &str
[strlen(str
) + 1];
1868 if (res
->status
!= NFS3_OK
) {
1869 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
));
1870 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1875 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &data
->fh
, str
, data
) != 0) {
1876 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
1877 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1878 free_nfs_cb_data(data
);
1884 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1886 char *str
= data
->continue_data
;
1889 str
= &str
[strlen(str
) + 1];
1891 memset(&args
, 0, sizeof(CREATE3args
));
1892 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1893 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1894 args
.where
.name
= str
;
1895 args
.how
.mode
= UNCHECKED
;
1896 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
1897 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
1899 if (rpc_nfs_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
1900 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
1901 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1902 free_nfs_cb_data(data
);
1908 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1913 new_path
= strdup(path
);
1914 if (new_path
== NULL
) {
1915 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1919 ptr
= strrchr(new_path
, '/');
1921 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1926 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
1927 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
1928 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1941 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1944 struct nfs_cb_data
*data
= private_data
;
1945 struct nfs_context
*nfs
= data
->nfs
;
1946 char *str
= data
->continue_data
;
1948 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1950 str
= &str
[strlen(str
) + 1];
1952 if (status
== RPC_STATUS_ERROR
) {
1953 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1954 free_nfs_cb_data(data
);
1957 if (status
== RPC_STATUS_CANCEL
) {
1958 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1959 free_nfs_cb_data(data
);
1964 if (res
->status
!= NFS3_OK
) {
1965 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
));
1966 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1967 free_nfs_cb_data(data
);
1971 data
->cb(0, nfs
, NULL
, data
->private_data
);
1972 free_nfs_cb_data(data
);
1975 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1977 char *str
= data
->continue_data
;
1979 str
= &str
[strlen(str
) + 1];
1981 if (rpc_nfs_remove_async(nfs
->rpc
, nfs_unlink_cb
, &data
->fh
, str
, data
) != 0) {
1982 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
1983 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1984 free_nfs_cb_data(data
);
1990 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1995 new_path
= strdup(path
);
1996 if (new_path
== NULL
) {
1997 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2001 ptr
= strrchr(new_path
, '/');
2003 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2008 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2009 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
2010 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2021 struct mknod_cb_data
{
2028 static void free_mknod_cb_data(void *ptr
)
2030 struct mknod_cb_data
*data
= ptr
;
2036 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2039 struct nfs_cb_data
*data
= private_data
;
2040 struct nfs_context
*nfs
= data
->nfs
;
2041 char *str
= data
->continue_data
;
2043 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2045 str
= &str
[strlen(str
) + 1];
2047 if (status
== RPC_STATUS_ERROR
) {
2048 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2049 free_nfs_cb_data(data
);
2052 if (status
== RPC_STATUS_CANCEL
) {
2053 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2054 free_nfs_cb_data(data
);
2059 if (res
->status
!= NFS3_OK
) {
2060 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
));
2061 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2062 free_nfs_cb_data(data
);
2066 data
->cb(0, nfs
, NULL
, data
->private_data
);
2067 free_nfs_cb_data(data
);
2070 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2072 struct mknod_cb_data
*cb_data
= data
->continue_data
;
2073 char *str
= cb_data
->path
;
2075 str
= &str
[strlen(str
) + 1];
2077 if (rpc_nfs_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &data
->fh
, str
, cb_data
->mode
, cb_data
->major
, cb_data
->minor
, data
) != 0) {
2078 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2079 free_nfs_cb_data(data
);
2085 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
2088 struct mknod_cb_data
*cb_data
;
2090 cb_data
= malloc(sizeof(struct mknod_cb_data
));
2091 if (cb_data
== NULL
) {
2092 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
2096 cb_data
->path
= strdup(path
);
2097 if (cb_data
->path
== NULL
) {
2098 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
2103 ptr
= strrchr(cb_data
->path
, '/');
2105 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
2110 cb_data
->mode
= mode
;
2111 cb_data
->major
= major(dev
);
2112 cb_data
->minor
= minor(dev
);
2114 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
2115 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
2116 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2117 free_mknod_cb_data(cb_data
);
2128 /* ReadDirPlus Emulation Callback data */
2129 struct rdpe_cb_data
{
2132 struct nfs_cb_data
*data
;
2135 /* ReadDirPlus Emulation LOOKUP Callback data */
2136 struct rdpe_lookup_cb_data
{
2137 struct rdpe_cb_data
*rdpe_cb_data
;
2138 struct nfsdirent
*nfsdirent
;
2141 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
2142 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2144 LOOKUP3res
*res
= command_data
;
2145 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
2146 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
2147 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
2148 struct nfsdir
*nfsdir
= data
->continue_data
;
2149 struct nfs_context
*nfs
= data
->nfs
;
2150 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2152 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2154 free(rdpe_lookup_cb_data
);
2156 rdpe_cb_data
->getattrcount
--;
2158 if (status
== RPC_STATUS_ERROR
) {
2159 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2161 if (status
== RPC_STATUS_CANCEL
) {
2162 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2164 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2165 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2167 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2168 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2169 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2171 nfsdirent
->type
= attributes
->type
;
2172 nfsdirent
->mode
= attributes
->mode
;
2173 nfsdirent
->size
= attributes
->size
;
2175 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2176 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2177 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2178 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2179 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2180 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2184 if (rdpe_cb_data
->getattrcount
== 0) {
2185 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2186 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2187 nfs_free_nfsdir(nfsdir
);
2189 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2193 data
->continue_data
= NULL
;
2194 free_nfs_cb_data(data
);
2198 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2200 READDIR3res
*res
= command_data
;
2201 struct nfs_cb_data
*data
= private_data
;
2202 struct nfs_context
*nfs
= data
->nfs
;
2203 struct nfsdir
*nfsdir
= data
->continue_data
;
2204 struct nfsdirent
*nfsdirent
;
2205 struct entry3
*entry
;
2207 struct rdpe_cb_data
*rdpe_cb_data
;
2209 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2211 if (status
== RPC_STATUS_ERROR
) {
2212 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2213 nfs_free_nfsdir(nfsdir
);
2214 data
->continue_data
= NULL
;
2215 free_nfs_cb_data(data
);
2219 if (status
== RPC_STATUS_CANCEL
) {
2220 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2221 nfs_free_nfsdir(nfsdir
);
2222 data
->continue_data
= NULL
;
2223 free_nfs_cb_data(data
);
2227 if (res
->status
!= NFS3_OK
) {
2228 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
));
2229 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2230 nfs_free_nfsdir(nfsdir
);
2231 data
->continue_data
= NULL
;
2232 free_nfs_cb_data(data
);
2236 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2237 while (entry
!= NULL
) {
2238 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2239 if (nfsdirent
== NULL
) {
2240 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2241 nfs_free_nfsdir(nfsdir
);
2242 data
->continue_data
= NULL
;
2243 free_nfs_cb_data(data
);
2246 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2247 nfsdirent
->name
= strdup(entry
->name
);
2248 if (nfsdirent
->name
== NULL
) {
2249 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2250 nfs_free_nfsdir(nfsdir
);
2251 data
->continue_data
= NULL
;
2252 free_nfs_cb_data(data
);
2255 nfsdirent
->inode
= entry
->fileid
;
2257 nfsdirent
->next
= nfsdir
->entries
;
2258 nfsdir
->entries
= nfsdirent
;
2260 cookie
= entry
->cookie
;
2261 entry
= entry
->nextentry
;
2264 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2265 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, cookie
, res
->READDIR3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2266 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2267 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2268 nfs_free_nfsdir(nfsdir
);
2269 data
->continue_data
= NULL
;
2270 free_nfs_cb_data(data
);
2276 /* steal the dirhandle */
2277 nfsdir
->current
= nfsdir
->entries
;
2279 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2280 rdpe_cb_data
->getattrcount
= 0;
2281 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2282 rdpe_cb_data
->data
= data
;
2283 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2284 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2286 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2287 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2288 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2290 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &data
->fh
, nfsdirent
->name
, rdpe_lookup_cb_data
) != 0) {
2291 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2293 /* if we have already commands in flight, we cant just stop, we have to wait for the
2294 * commands in flight to complete
2296 if (rdpe_cb_data
->getattrcount
> 0) {
2297 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2298 free(rdpe_lookup_cb_data
);
2302 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2303 nfs_free_nfsdir(nfsdir
);
2304 data
->continue_data
= NULL
;
2305 free_nfs_cb_data(data
);
2306 free(rdpe_lookup_cb_data
);
2310 rdpe_cb_data
->getattrcount
++;
2315 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2317 READDIRPLUS3res
*res
= command_data
;
2318 struct nfs_cb_data
*data
= private_data
;
2319 struct nfs_context
*nfs
= data
->nfs
;
2320 struct nfsdir
*nfsdir
= data
->continue_data
;
2321 struct entryplus3
*entry
;
2324 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2326 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2329 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2330 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2331 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2332 nfs_free_nfsdir(nfsdir
);
2333 data
->continue_data
= NULL
;
2334 free_nfs_cb_data(data
);
2340 if (status
== RPC_STATUS_CANCEL
) {
2341 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2342 nfs_free_nfsdir(nfsdir
);
2343 data
->continue_data
= NULL
;
2344 free_nfs_cb_data(data
);
2348 if (res
->status
!= NFS3_OK
) {
2349 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
));
2350 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2351 nfs_free_nfsdir(nfsdir
);
2352 data
->continue_data
= NULL
;
2353 free_nfs_cb_data(data
);
2357 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2358 while (entry
!= NULL
) {
2359 struct nfsdirent
*nfsdirent
;
2361 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2362 if (nfsdirent
== NULL
) {
2363 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2364 nfs_free_nfsdir(nfsdir
);
2365 data
->continue_data
= NULL
;
2366 free_nfs_cb_data(data
);
2369 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2370 nfsdirent
->name
= strdup(entry
->name
);
2371 if (nfsdirent
->name
== NULL
) {
2372 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2373 nfs_free_nfsdir(nfsdir
);
2374 data
->continue_data
= NULL
;
2375 free_nfs_cb_data(data
);
2378 nfsdirent
->inode
= entry
->fileid
;
2379 if (entry
->name_attributes
.attributes_follow
) {
2380 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2381 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2382 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2384 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2385 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2386 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2387 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2388 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2389 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2392 nfsdirent
->next
= nfsdir
->entries
;
2393 nfsdir
->entries
= nfsdirent
;
2395 cookie
= entry
->cookie
;
2396 entry
= entry
->nextentry
;
2399 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2400 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, cookie
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2401 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2402 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2403 nfs_free_nfsdir(nfsdir
);
2404 data
->continue_data
= NULL
;
2405 free_nfs_cb_data(data
);
2411 /* steal the dirhandle */
2412 data
->continue_data
= NULL
;
2413 nfsdir
->current
= nfsdir
->entries
;
2415 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2416 free_nfs_cb_data(data
);
2419 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2423 memset(cv
, 0, sizeof(cookieverf3
));
2424 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2425 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2426 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2427 free_nfs_cb_data(data
);
2433 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2435 struct nfsdir
*nfsdir
;
2437 nfsdir
= malloc(sizeof(struct nfsdir
));
2438 if (nfsdir
== NULL
) {
2439 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2442 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2444 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2445 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2453 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2455 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2457 if (nfsdir
->current
!= NULL
) {
2458 nfsdir
->current
= nfsdir
->current
->next
;
2464 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2466 nfs_free_nfsdir(nfsdir
);
2478 struct lseek_cb_data
{
2479 struct nfs_context
*nfs
;
2480 struct nfsfh
*nfsfh
;
2486 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2489 struct lseek_cb_data
*data
= private_data
;
2490 struct nfs_context
*nfs
= data
->nfs
;
2492 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2494 if (status
== RPC_STATUS_ERROR
) {
2495 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2499 if (status
== RPC_STATUS_CANCEL
) {
2500 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2506 if (res
->status
!= NFS3_OK
) {
2507 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2508 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2513 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2514 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2518 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2520 struct lseek_cb_data
*data
;
2522 if (whence
== SEEK_SET
) {
2523 nfsfh
->offset
= offset
;
2524 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2527 if (whence
== SEEK_CUR
) {
2528 nfsfh
->offset
+= offset
;
2529 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2533 data
= malloc(sizeof(struct lseek_cb_data
));
2535 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2540 data
->nfsfh
= nfsfh
;
2541 data
->offset
= offset
;
2543 data
->private_data
= private_data
;
2545 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &nfsfh
->fh
, data
) != 0) {
2546 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
2559 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2562 struct nfs_cb_data
*data
= private_data
;
2563 struct nfs_context
*nfs
= data
->nfs
;
2564 struct statvfs svfs
;
2566 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2568 if (status
== RPC_STATUS_ERROR
) {
2569 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2570 free_nfs_cb_data(data
);
2573 if (status
== RPC_STATUS_CANCEL
) {
2574 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2575 free_nfs_cb_data(data
);
2580 if (res
->status
!= NFS3_OK
) {
2581 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
));
2582 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2583 free_nfs_cb_data(data
);
2587 svfs
.f_bsize
= 4096;
2588 svfs
.f_frsize
= 4096;
2589 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
2590 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
2591 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
2592 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
2593 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
2594 #if !defined(ANDROID)
2595 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
2598 svfs
.f_namemax
= 256;
2601 data
->cb(0, nfs
, &svfs
, data
->private_data
);
2602 free_nfs_cb_data(data
);
2605 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2607 if (rpc_nfs_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &data
->fh
, data
) != 0) {
2608 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
2609 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2610 free_nfs_cb_data(data
);
2616 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2618 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
2619 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2632 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2635 struct nfs_cb_data
*data
= private_data
;
2636 struct nfs_context
*nfs
= data
->nfs
;
2638 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2640 if (status
== RPC_STATUS_ERROR
) {
2641 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2642 free_nfs_cb_data(data
);
2645 if (status
== RPC_STATUS_CANCEL
) {
2646 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2647 free_nfs_cb_data(data
);
2652 if (res
->status
!= NFS3_OK
) {
2653 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
));
2654 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2655 free_nfs_cb_data(data
);
2660 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
2661 free_nfs_cb_data(data
);
2664 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2668 args
.symlink
.data
.data_len
= data
->fh
.data
.data_len
;
2669 args
.symlink
.data
.data_val
= data
->fh
.data
.data_val
;
2671 if (rpc_nfs_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
2672 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
2673 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2674 free_nfs_cb_data(data
);
2680 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2682 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
2683 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2696 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2698 struct nfs_cb_data
*data
= private_data
;
2699 struct nfs_context
*nfs
= data
->nfs
;
2702 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2704 if (status
== RPC_STATUS_ERROR
) {
2705 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2706 free_nfs_cb_data(data
);
2709 if (status
== RPC_STATUS_CANCEL
) {
2710 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2711 free_nfs_cb_data(data
);
2716 if (res
->status
!= NFS3_OK
) {
2717 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2718 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2719 free_nfs_cb_data(data
);
2723 data
->cb(0, nfs
, NULL
, data
->private_data
);
2724 free_nfs_cb_data(data
);
2727 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2731 memset(&args
, 0, sizeof(SETATTR3args
));
2732 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2733 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2734 args
.new_attributes
.mode
.set_it
= 1;
2735 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2737 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
2738 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2739 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2740 free_nfs_cb_data(data
);
2747 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2749 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
2750 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2760 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
2762 struct nfs_cb_data
*data
;
2764 data
= malloc(sizeof(struct nfs_cb_data
));
2766 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
2769 memset(data
, 0, sizeof(struct nfs_cb_data
));
2772 data
->private_data
= private_data
;
2773 data
->continue_int
= mode
;
2774 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2775 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2776 if (data
->fh
.data
.data_val
== NULL
) {
2777 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2778 free_nfs_cb_data(data
);
2781 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2783 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
2784 free_nfs_cb_data(data
);
2796 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2798 struct nfs_cb_data
*data
= private_data
;
2799 struct nfs_context
*nfs
= data
->nfs
;
2802 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2804 if (status
== RPC_STATUS_ERROR
) {
2805 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2806 free_nfs_cb_data(data
);
2809 if (status
== RPC_STATUS_CANCEL
) {
2810 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2811 free_nfs_cb_data(data
);
2816 if (res
->status
!= NFS3_OK
) {
2817 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2818 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2819 free_nfs_cb_data(data
);
2823 data
->cb(0, nfs
, NULL
, data
->private_data
);
2824 free_nfs_cb_data(data
);
2827 struct nfs_chown_data
{
2832 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2835 struct nfs_chown_data
*chown_data
= data
->continue_data
;
2837 memset(&args
, 0, sizeof(SETATTR3args
));
2838 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2839 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2840 if (chown_data
->uid
!= (uid_t
)-1) {
2841 args
.new_attributes
.uid
.set_it
= 1;
2842 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
2844 if (chown_data
->gid
!= (gid_t
)-1) {
2845 args
.new_attributes
.gid
.set_it
= 1;
2846 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
2849 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
2850 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2851 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2852 free_nfs_cb_data(data
);
2859 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2861 struct nfs_chown_data
*chown_data
;
2863 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2864 if (chown_data
== NULL
) {
2865 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2869 chown_data
->uid
= uid
;
2870 chown_data
->gid
= gid
;
2872 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
2873 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2884 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2886 struct nfs_cb_data
*data
;
2887 struct nfs_chown_data
*chown_data
;
2889 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2890 if (chown_data
== NULL
) {
2891 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2895 chown_data
->uid
= uid
;
2896 chown_data
->gid
= gid
;
2899 data
= malloc(sizeof(struct nfs_cb_data
));
2901 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
2904 memset(data
, 0, sizeof(struct nfs_cb_data
));
2907 data
->private_data
= private_data
;
2908 data
->continue_data
= chown_data
;
2909 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2910 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2911 if (data
->fh
.data
.data_val
== NULL
) {
2912 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2913 free_nfs_cb_data(data
);
2916 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2919 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
2920 free_nfs_cb_data(data
);
2934 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2936 struct nfs_cb_data
*data
= private_data
;
2937 struct nfs_context
*nfs
= data
->nfs
;
2940 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2942 if (status
== RPC_STATUS_ERROR
) {
2943 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2944 free_nfs_cb_data(data
);
2947 if (status
== RPC_STATUS_CANCEL
) {
2948 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2949 free_nfs_cb_data(data
);
2954 if (res
->status
!= NFS3_OK
) {
2955 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2956 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2957 free_nfs_cb_data(data
);
2961 data
->cb(0, nfs
, NULL
, data
->private_data
);
2962 free_nfs_cb_data(data
);
2965 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2968 struct timeval
*utimes_data
= data
->continue_data
;
2970 memset(&args
, 0, sizeof(SETATTR3args
));
2971 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2972 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2973 if (utimes_data
!= NULL
) {
2974 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
2975 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
2976 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
2977 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
2978 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
2979 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
2981 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
2982 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
2985 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
2986 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2987 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2988 free_nfs_cb_data(data
);
2995 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
2997 struct timeval
*new_times
= NULL
;
2999 if (times
!= NULL
) {
3000 new_times
= malloc(sizeof(struct timeval
)*2);
3001 if (new_times
== NULL
) {
3002 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3006 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
3009 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3010 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3020 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
3022 struct timeval
*new_times
= NULL
;
3024 if (times
!= NULL
) {
3025 new_times
= malloc(sizeof(struct timeval
)*2);
3026 if (new_times
== NULL
) {
3027 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
3031 new_times
[0].tv_sec
= times
->actime
;
3032 new_times
[0].tv_usec
= 0;
3033 new_times
[1].tv_sec
= times
->modtime
;
3034 new_times
[1].tv_usec
= 0;
3037 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
3038 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3049 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3052 struct nfs_cb_data
*data
= private_data
;
3053 struct nfs_context
*nfs
= data
->nfs
;
3054 unsigned int nfsmode
= 0;
3056 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3058 if (status
== RPC_STATUS_ERROR
) {
3059 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3060 free_nfs_cb_data(data
);
3063 if (status
== RPC_STATUS_CANCEL
) {
3064 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3065 free_nfs_cb_data(data
);
3070 if (res
->status
!= NFS3_OK
) {
3071 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
));
3072 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3073 free_nfs_cb_data(data
);
3077 if (data
->continue_int
& R_OK
) {
3078 nfsmode
|= ACCESS3_READ
;
3080 if (data
->continue_int
& W_OK
) {
3081 nfsmode
|= ACCESS3_MODIFY
;
3083 if (data
->continue_int
& X_OK
) {
3084 nfsmode
|= ACCESS3_EXECUTE
;
3087 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
3088 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
3089 nfsmode
&ACCESS3_READ
?'r':'-',
3090 nfsmode
&ACCESS3_MODIFY
?'w':'-',
3091 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
3092 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
3093 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
3094 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
3095 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3096 free_nfs_cb_data(data
);
3100 data
->cb(0, nfs
, NULL
, data
->private_data
);
3101 free_nfs_cb_data(data
);
3104 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3108 if (data
->continue_int
& R_OK
) {
3109 nfsmode
|= ACCESS3_READ
;
3111 if (data
->continue_int
& W_OK
) {
3112 nfsmode
|= ACCESS3_MODIFY
;
3114 if (data
->continue_int
& X_OK
) {
3115 nfsmode
|= ACCESS3_EXECUTE
;
3118 if (rpc_nfs_access_async(nfs
->rpc
, nfs_access_cb
, &data
->fh
, nfsmode
, data
) != 0) {
3119 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
3120 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3121 free_nfs_cb_data(data
);
3127 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
3129 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
3130 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3142 struct nfs_symlink_data
{
3144 char *newpathparent
;
3145 char *newpathobject
;
3148 static void free_nfs_symlink_data(void *mem
)
3150 struct nfs_symlink_data
*data
= mem
;
3152 if (data
->oldpath
!= NULL
) {
3153 free(data
->oldpath
);
3155 if (data
->newpathparent
!= NULL
) {
3156 free(data
->newpathparent
);
3158 if (data
->newpathobject
!= NULL
) {
3159 free(data
->newpathobject
);
3164 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3167 struct nfs_cb_data
*data
= private_data
;
3168 struct nfs_context
*nfs
= data
->nfs
;
3169 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3171 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3173 if (status
== RPC_STATUS_ERROR
) {
3174 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3175 free_nfs_cb_data(data
);
3178 if (status
== RPC_STATUS_CANCEL
) {
3179 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3180 free_nfs_cb_data(data
);
3185 if (res
->status
!= NFS3_OK
) {
3186 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
));
3187 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3188 free_nfs_cb_data(data
);
3192 data
->cb(0, nfs
, NULL
, data
->private_data
);
3193 free_nfs_cb_data(data
);
3196 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3198 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3201 memset(&sa
, 0, sizeof(SYMLINK3args
));
3202 sa
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
3203 sa
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
3204 sa
.where
.name
= symlink_data
->newpathobject
;
3205 sa
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3206 sa
.symlink
.symlink_attributes
.mode
.set_mode3_u
.mode
= S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IWGRP
|S_IXGRP
|S_IROTH
|S_IWOTH
|S_IXOTH
;
3207 sa
.symlink
.symlink_data
= symlink_data
->oldpath
;
3209 if (rpc_nfs_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &sa
, data
) != 0) {
3210 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3211 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3212 free_nfs_cb_data(data
);
3218 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3221 struct nfs_symlink_data
*symlink_data
;
3223 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3224 if (symlink_data
== NULL
) {
3225 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3228 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3230 symlink_data
->oldpath
= strdup(oldpath
);
3231 if (symlink_data
->oldpath
== NULL
) {
3232 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3233 free_nfs_symlink_data(symlink_data
);
3237 symlink_data
->newpathparent
= strdup(newpath
);
3238 if (symlink_data
->newpathparent
== NULL
) {
3239 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3240 free_nfs_symlink_data(symlink_data
);
3244 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3246 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3247 free_nfs_symlink_data(symlink_data
);
3253 symlink_data
->newpathobject
= strdup(ptr
);
3254 if (symlink_data
->newpathobject
== NULL
) {
3255 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3256 free_nfs_symlink_data(symlink_data
);
3260 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3261 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3273 struct nfs_rename_data
{
3276 struct nfs_fh3 olddir
;
3279 struct nfs_fh3 newdir
;
3282 static void free_nfs_rename_data(void *mem
)
3284 struct nfs_rename_data
*data
= mem
;
3286 if (data
->oldpath
!= NULL
) {
3287 free(data
->oldpath
);
3289 if (data
->olddir
.data
.data_val
!= NULL
) {
3290 free(data
->olddir
.data
.data_val
);
3292 if (data
->newpath
!= NULL
) {
3293 free(data
->newpath
);
3295 if (data
->newdir
.data
.data_val
!= NULL
) {
3296 free(data
->newdir
.data
.data_val
);
3301 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3304 struct nfs_cb_data
*data
= private_data
;
3305 struct nfs_context
*nfs
= data
->nfs
;
3306 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3308 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3310 if (status
== RPC_STATUS_ERROR
) {
3311 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3312 free_nfs_cb_data(data
);
3315 if (status
== RPC_STATUS_CANCEL
) {
3316 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3317 free_nfs_cb_data(data
);
3322 if (res
->status
!= NFS3_OK
) {
3323 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
));
3324 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3325 free_nfs_cb_data(data
);
3329 data
->cb(0, nfs
, NULL
, data
->private_data
);
3330 free_nfs_cb_data(data
);
3333 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3335 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3337 /* steal the filehandle */
3338 rename_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3339 rename_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3340 data
->fh
.data
.data_val
= NULL
;
3342 if (rpc_nfs_rename_async(nfs
->rpc
, nfs_rename_cb
, &rename_data
->olddir
, rename_data
->oldobject
, &rename_data
->newdir
, rename_data
->newobject
, data
) != 0) {
3343 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3344 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3345 free_nfs_cb_data(data
);
3352 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3354 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3356 /* steal the filehandle */
3357 rename_data
->olddir
.data
.data_len
= data
->fh
.data
.data_len
;
3358 rename_data
->olddir
.data
.data_val
= data
->fh
.data
.data_val
;
3359 data
->fh
.data
.data_val
= NULL
;
3361 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) {
3362 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3363 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3364 free_nfs_cb_data(data
);
3367 data
->continue_data
= NULL
;
3368 free_nfs_cb_data(data
);
3374 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3377 struct nfs_rename_data
*rename_data
;
3379 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3380 if (rename_data
== NULL
) {
3381 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3384 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3386 rename_data
->oldpath
= strdup(oldpath
);
3387 if (rename_data
->oldpath
== NULL
) {
3388 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3389 free_nfs_rename_data(rename_data
);
3392 ptr
= strrchr(rename_data
->oldpath
, '/');
3394 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3395 free_nfs_rename_data(rename_data
);
3400 rename_data
->oldobject
= ptr
;
3403 rename_data
->newpath
= strdup(newpath
);
3404 if (rename_data
->newpath
== NULL
) {
3405 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3406 free_nfs_rename_data(rename_data
);
3409 ptr
= strrchr(rename_data
->newpath
, '/');
3411 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3412 free_nfs_rename_data(rename_data
);
3417 rename_data
->newobject
= ptr
;
3420 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3421 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3432 struct nfs_link_data
{
3434 struct nfs_fh3 oldfh
;
3437 struct nfs_fh3 newdir
;
3440 static void free_nfs_link_data(void *mem
)
3442 struct nfs_link_data
*data
= mem
;
3444 if (data
->oldpath
!= NULL
) {
3445 free(data
->oldpath
);
3447 if (data
->oldfh
.data
.data_val
!= NULL
) {
3448 free(data
->oldfh
.data
.data_val
);
3450 if (data
->newpath
!= NULL
) {
3451 free(data
->newpath
);
3453 if (data
->newdir
.data
.data_val
!= NULL
) {
3454 free(data
->newdir
.data
.data_val
);
3459 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3462 struct nfs_cb_data
*data
= private_data
;
3463 struct nfs_context
*nfs
= data
->nfs
;
3464 struct nfs_link_data
*link_data
= data
->continue_data
;
3466 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3468 if (status
== RPC_STATUS_ERROR
) {
3469 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3470 free_nfs_cb_data(data
);
3473 if (status
== RPC_STATUS_CANCEL
) {
3474 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3475 free_nfs_cb_data(data
);
3480 if (res
->status
!= NFS3_OK
) {
3481 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
));
3482 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3483 free_nfs_cb_data(data
);
3487 data
->cb(0, nfs
, NULL
, data
->private_data
);
3488 free_nfs_cb_data(data
);
3491 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3493 struct nfs_link_data
*link_data
= data
->continue_data
;
3495 /* steal the filehandle */
3496 link_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3497 link_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3498 data
->fh
.data
.data_val
= NULL
;
3500 if (rpc_nfs_link_async(nfs
->rpc
, nfs_link_cb
, &link_data
->oldfh
, &link_data
->newdir
, link_data
->newobject
, data
) != 0) {
3501 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3502 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3503 free_nfs_cb_data(data
);
3510 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3512 struct nfs_link_data
*link_data
= data
->continue_data
;
3514 /* steal the filehandle */
3515 link_data
->oldfh
.data
.data_len
= data
->fh
.data
.data_len
;
3516 link_data
->oldfh
.data
.data_val
= data
->fh
.data
.data_val
;
3517 data
->fh
.data
.data_val
= NULL
;
3519 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) {
3520 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3521 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3522 free_nfs_cb_data(data
);
3525 data
->continue_data
= NULL
;
3526 free_nfs_cb_data(data
);
3532 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3535 struct nfs_link_data
*link_data
;
3537 link_data
= malloc(sizeof(struct nfs_link_data
));
3538 if (link_data
== NULL
) {
3539 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
3542 memset(link_data
, 0, sizeof(struct nfs_link_data
));
3544 link_data
->oldpath
= strdup(oldpath
);
3545 if (link_data
->oldpath
== NULL
) {
3546 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3547 free_nfs_link_data(link_data
);
3551 link_data
->newpath
= strdup(newpath
);
3552 if (link_data
->newpath
== NULL
) {
3553 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3554 free_nfs_link_data(link_data
);
3557 ptr
= strrchr(link_data
->newpath
, '/');
3559 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3560 free_nfs_link_data(link_data
);
3565 link_data
->newobject
= ptr
;
3568 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
3569 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3577 //qqq replace later with lseek()
3578 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
3580 return nfsfh
->offset
;
3586 * Get the maximum supported READ3 size by the server
3588 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
3590 return nfs
->readmax
;
3594 * Get the maximum supported WRITE3 size by the server
3596 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
3598 return nfs
->writemax
;
3601 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
3606 va_start(ap
, error_string
);
3608 vsnprintf(str
, 1024, error_string
, ap
);
3609 if (nfs
->rpc
->error_string
!= NULL
) {
3610 free(nfs
->rpc
->error_string
);
3612 nfs
->rpc
->error_string
= str
;
3618 struct mount_cb_data
{
3624 static void free_mount_cb_data(struct mount_cb_data
*data
)
3626 if (data
->server
!= NULL
) {
3628 data
->server
= NULL
;
3634 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3636 struct mount_cb_data
*data
= private_data
;
3638 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3640 if (status
== RPC_STATUS_ERROR
) {
3641 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3642 free_mount_cb_data(data
);
3645 if (status
== RPC_STATUS_CANCEL
) {
3646 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3647 free_mount_cb_data(data
);
3651 data
->cb(rpc
, 0, command_data
, data
->private_data
);
3652 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
3653 rpc_set_error(rpc
, "Failed to disconnect\n");
3655 free_mount_cb_data(data
);
3658 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3660 struct mount_cb_data
*data
= private_data
;
3662 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3664 /* Dont want any more callbacks even if the socket is closed */
3665 rpc
->connect_cb
= NULL
;
3667 if (status
== RPC_STATUS_ERROR
) {
3668 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3669 free_mount_cb_data(data
);
3672 if (status
== RPC_STATUS_CANCEL
) {
3673 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3674 free_mount_cb_data(data
);
3678 if (rpc_mount_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
3679 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3680 free_mount_cb_data(data
);
3685 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3687 struct mount_cb_data
*data
= private_data
;
3688 uint32_t mount_port
;
3690 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3692 if (status
== RPC_STATUS_ERROR
) {
3693 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3694 free_mount_cb_data(data
);
3697 if (status
== RPC_STATUS_CANCEL
) {
3698 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3699 free_mount_cb_data(data
);
3703 mount_port
= *(uint32_t *)command_data
;
3704 if (mount_port
== 0) {
3705 rpc_set_error(rpc
, "RPC error. Mount program is not available");
3706 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
3707 free_mount_cb_data(data
);
3711 rpc_disconnect(rpc
, "normal disconnect");
3712 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
3713 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3714 free_mount_cb_data(data
);
3719 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3721 struct mount_cb_data
*data
= private_data
;
3723 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3725 if (status
== RPC_STATUS_ERROR
) {
3726 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3727 free_mount_cb_data(data
);
3730 if (status
== RPC_STATUS_CANCEL
) {
3731 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3732 free_mount_cb_data(data
);
3736 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
3737 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3738 free_mount_cb_data(data
);
3743 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3745 struct mount_cb_data
*data
= private_data
;
3747 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3749 /* Dont want any more callbacks even if the socket is closed */
3750 rpc
->connect_cb
= NULL
;
3752 if (status
== RPC_STATUS_ERROR
) {
3753 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3754 free_mount_cb_data(data
);
3757 if (status
== RPC_STATUS_CANCEL
) {
3758 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3759 free_mount_cb_data(data
);
3763 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
3764 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3765 free_mount_cb_data(data
);
3770 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
3772 struct mount_cb_data
*data
;
3774 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3776 data
= malloc(sizeof(struct mount_cb_data
));
3780 memset(data
, 0, sizeof(struct mount_cb_data
));
3782 data
->private_data
= private_data
;
3783 data
->server
= strdup(server
);
3784 if (data
->server
== NULL
) {
3785 free_mount_cb_data(data
);
3788 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
3789 free_mount_cb_data(data
);
3796 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
3798 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
3802 const char *nfs_get_server(struct nfs_context
*nfs
) {
3806 const char *nfs_get_export(struct nfs_context
*nfs
) {
3810 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
3811 return &nfs
->rootfh
;
3814 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {