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
;
222 void free_nfs_cb_data(struct nfs_cb_data
*data
)
224 if (data
->saved_path
!= NULL
) {
225 free(data
->saved_path
);
226 data
->saved_path
= NULL
;
229 if (data
->continue_data
!= NULL
) {
230 data
->free_continue_data(data
->continue_data
);
231 data
->continue_data
= NULL
;
234 if (data
->fh
.data
.data_val
!= NULL
) {
235 free(data
->fh
.data
.data_val
);
236 data
->fh
.data
.data_val
= NULL
;
239 if (data
->buffer
!= NULL
) {
251 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
253 struct nfs_cb_data
*data
= private_data
;
254 struct nfs_context
*nfs
= data
->nfs
;
256 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
258 if (status
== RPC_STATUS_ERROR
) {
259 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
260 free_nfs_cb_data(data
);
263 if (status
== RPC_STATUS_CANCEL
) {
264 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
265 free_nfs_cb_data(data
);
269 data
->cb(0, nfs
, NULL
, data
->private_data
);
270 free_nfs_cb_data(data
);
273 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
275 struct nfs_cb_data
*data
= private_data
;
276 struct nfs_context
*nfs
= data
->nfs
;
277 FSINFO3res
*res
= command_data
;
279 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
281 if (status
== RPC_STATUS_ERROR
) {
282 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
283 free_nfs_cb_data(data
);
286 if (status
== RPC_STATUS_CANCEL
) {
287 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
288 free_nfs_cb_data(data
);
292 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
293 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
295 if (rpc_nfs_getattr_async(rpc
, nfs_mount_10_cb
, &nfs
->rootfh
, data
) != 0) {
296 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
297 free_nfs_cb_data(data
);
302 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
304 struct nfs_cb_data
*data
= private_data
;
305 struct nfs_context
*nfs
= data
->nfs
;
307 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
309 if (status
== RPC_STATUS_ERROR
) {
310 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
311 free_nfs_cb_data(data
);
314 if (status
== RPC_STATUS_CANCEL
) {
315 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
316 free_nfs_cb_data(data
);
320 if (rpc_nfs_fsinfo_async(rpc
, nfs_mount_9_cb
, &nfs
->rootfh
, data
) != 0) {
321 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
322 free_nfs_cb_data(data
);
328 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
330 struct nfs_cb_data
*data
= private_data
;
331 struct nfs_context
*nfs
= data
->nfs
;
333 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
335 /* Dont want any more callbacks even if the socket is closed */
336 rpc
->connect_cb
= NULL
;
338 if (status
== RPC_STATUS_ERROR
) {
339 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
340 free_nfs_cb_data(data
);
343 if (status
== RPC_STATUS_CANCEL
) {
344 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
345 free_nfs_cb_data(data
);
349 if (rpc_nfs_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
350 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
351 free_nfs_cb_data(data
);
357 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
359 struct nfs_cb_data
*data
= private_data
;
360 struct nfs_context
*nfs
= data
->nfs
;
363 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
365 if (status
== RPC_STATUS_ERROR
) {
366 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
367 free_nfs_cb_data(data
);
370 if (status
== RPC_STATUS_CANCEL
) {
371 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
372 free_nfs_cb_data(data
);
377 if (res
->fhs_status
!= MNT3_OK
) {
378 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
));
379 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
380 free_nfs_cb_data(data
);
384 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
385 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
386 if (nfs
->rootfh
.data
.data_val
== NULL
) {
387 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
388 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
389 free_nfs_cb_data(data
);
392 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
394 rpc_disconnect(rpc
, "normal disconnect");
395 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
396 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
397 free_nfs_cb_data(data
);
400 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
401 rpc_set_autoreconnect(rpc
);
405 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
407 struct nfs_cb_data
*data
= private_data
;
408 struct nfs_context
*nfs
= data
->nfs
;
410 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
412 if (status
== RPC_STATUS_ERROR
) {
413 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
414 free_nfs_cb_data(data
);
417 if (status
== RPC_STATUS_CANCEL
) {
418 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
419 free_nfs_cb_data(data
);
423 if (rpc_mount_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
424 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
425 free_nfs_cb_data(data
);
430 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
432 struct nfs_cb_data
*data
= private_data
;
433 struct nfs_context
*nfs
= data
->nfs
;
435 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
437 /* Dont want any more callbacks even if the socket is closed */
438 rpc
->connect_cb
= NULL
;
440 if (status
== RPC_STATUS_ERROR
) {
441 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
442 free_nfs_cb_data(data
);
445 if (status
== RPC_STATUS_CANCEL
) {
446 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
447 free_nfs_cb_data(data
);
451 if (rpc_mount_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
452 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
453 free_nfs_cb_data(data
);
458 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
460 struct nfs_cb_data
*data
= private_data
;
461 struct nfs_context
*nfs
= data
->nfs
;
464 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
466 if (status
== RPC_STATUS_ERROR
) {
467 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
468 free_nfs_cb_data(data
);
471 if (status
== RPC_STATUS_CANCEL
) {
472 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
473 free_nfs_cb_data(data
);
477 mount_port
= *(uint32_t *)command_data
;
478 if (mount_port
== 0) {
479 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
480 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
481 free_nfs_cb_data(data
);
485 rpc_disconnect(rpc
, "normal disconnect");
486 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
487 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
488 free_nfs_cb_data(data
);
494 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
496 struct nfs_cb_data
*data
= private_data
;
497 struct nfs_context
*nfs
= data
->nfs
;
499 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
501 if (status
== RPC_STATUS_ERROR
) {
502 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
503 free_nfs_cb_data(data
);
506 if (status
== RPC_STATUS_CANCEL
) {
507 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
508 free_nfs_cb_data(data
);
512 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
513 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
514 free_nfs_cb_data(data
);
519 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
521 struct nfs_cb_data
*data
= private_data
;
522 struct nfs_context
*nfs
= data
->nfs
;
524 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
526 /* Dont want any more callbacks even if the socket is closed */
527 rpc
->connect_cb
= NULL
;
529 if (status
== RPC_STATUS_ERROR
) {
530 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
531 free_nfs_cb_data(data
);
534 if (status
== RPC_STATUS_CANCEL
) {
535 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
536 free_nfs_cb_data(data
);
540 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
541 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
542 free_nfs_cb_data(data
);
548 * Async call for mounting an nfs share and geting the root filehandle
550 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
552 struct nfs_cb_data
*data
;
553 char *new_server
, *new_export
;
555 data
= malloc(sizeof(struct nfs_cb_data
));
557 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
560 memset(data
, 0, sizeof(struct nfs_cb_data
));
561 new_server
= strdup(server
);
562 new_export
= strdup(export
);
563 if (nfs
->server
!= NULL
) {
566 nfs
->server
= new_server
;
567 if (nfs
->export
!= NULL
) {
570 nfs
->export
= new_export
;
573 data
->private_data
= private_data
;
575 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
576 rpc_set_error(nfs
->rpc
, "Failed to start connection");
577 free_nfs_cb_data(data
);
587 * Functions to first look up a path, component by component, and then finally call a specific function once
588 * the filehandle for the final component is found.
590 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
592 struct nfs_cb_data
*data
= private_data
;
593 struct nfs_context
*nfs
= data
->nfs
;
596 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
598 if (status
== RPC_STATUS_ERROR
) {
599 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
600 free_nfs_cb_data(data
);
603 if (status
== RPC_STATUS_CANCEL
) {
604 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
605 free_nfs_cb_data(data
);
610 if (res
->status
!= NFS3_OK
) {
611 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
));
612 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
613 free_nfs_cb_data(data
);
617 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
618 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
619 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
620 free_nfs_cb_data(data
);
625 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
629 while (*data
->path
== '/') {
634 str
= strchr(path
, '/');
639 while (*data
->path
!= 0) {
645 data
->fh
.data
.data_len
= fh
->data
.data_len
;
646 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
647 if (data
->fh
.data
.data_val
== NULL
) {
648 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
649 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
650 free_nfs_cb_data(data
);
653 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
654 data
->continue_cb(nfs
, data
);
658 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, fh
, path
, data
) != 0) {
659 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
660 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
661 free_nfs_cb_data(data
);
667 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
)
669 struct nfs_cb_data
*data
;
671 if (path
[0] != 0 && path
[0] != '/') {
672 rpc_set_error(nfs
->rpc
, "Pathname is not absolute %s", path
);
676 data
= malloc(sizeof(struct nfs_cb_data
));
678 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
681 memset(data
, 0, sizeof(struct nfs_cb_data
));
684 data
->continue_cb
= continue_cb
;
685 data
->continue_data
= continue_data
;
686 data
->free_continue_data
= free_continue_data
;
687 data
->continue_int
= continue_int
;
688 data
->private_data
= private_data
;
689 data
->saved_path
= strdup(path
);
690 if (data
->saved_path
== NULL
) {
691 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
692 free_nfs_cb_data(data
);
695 data
->path
= data
->saved_path
;
697 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
698 /* return 0 here since the callback will be invoked if there is a failure */
711 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
714 struct nfs_cb_data
*data
= private_data
;
715 struct nfs_context
*nfs
= data
->nfs
;
718 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
720 if (status
== RPC_STATUS_ERROR
) {
721 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
722 free_nfs_cb_data(data
);
725 if (status
== RPC_STATUS_CANCEL
) {
726 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
727 free_nfs_cb_data(data
);
732 if (res
->status
!= NFS3_OK
) {
733 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
));
734 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
735 free_nfs_cb_data(data
);
740 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
741 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
742 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
743 st
.st_mode
|= S_IFDIR
;
745 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
746 st
.st_mode
|= S_IFREG
;
748 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
749 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
750 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
752 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
754 st
.st_blksize
= 4096;
755 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
757 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
758 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
759 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
761 data
->cb(0, nfs
, &st
, data
->private_data
);
762 free_nfs_cb_data(data
);
765 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
767 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &data
->fh
, data
) != 0) {
768 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
769 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
770 free_nfs_cb_data(data
);
776 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
778 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
779 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
793 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
796 struct nfs_cb_data
*data
= private_data
;
797 struct nfs_context
*nfs
= data
->nfs
;
799 unsigned int nfsmode
= 0;
801 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
803 if (status
== RPC_STATUS_ERROR
) {
804 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
805 free_nfs_cb_data(data
);
808 if (status
== RPC_STATUS_CANCEL
) {
809 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
810 free_nfs_cb_data(data
);
815 if (res
->status
!= NFS3_OK
) {
816 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
));
817 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
818 free_nfs_cb_data(data
);
822 if (data
->continue_int
& O_WRONLY
) {
823 nfsmode
|= ACCESS3_MODIFY
;
825 if (data
->continue_int
& O_RDWR
) {
826 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
828 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
829 nfsmode
|= ACCESS3_READ
;
833 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
834 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
835 nfsmode
&ACCESS3_READ
?'r':'-',
836 nfsmode
&ACCESS3_MODIFY
?'w':'-',
837 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
838 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
839 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
840 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
841 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
842 free_nfs_cb_data(data
);
846 nfsfh
= malloc(sizeof(struct nfsfh
));
848 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
849 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
850 free_nfs_cb_data(data
);
853 memset(nfsfh
, 0, sizeof(struct nfsfh
));
855 if (data
->continue_int
& O_SYNC
) {
859 /* steal the filehandle */
860 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
861 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
862 data
->fh
.data
.data_val
= NULL
;
864 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
865 free_nfs_cb_data(data
);
868 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
872 if (data
->continue_int
& O_WRONLY
) {
873 nfsmode
|= ACCESS3_MODIFY
;
875 if (data
->continue_int
& O_RDWR
) {
876 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
878 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
879 nfsmode
|= ACCESS3_READ
;
882 if (rpc_nfs_access_async(nfs
->rpc
, nfs_open_cb
, &data
->fh
, nfsmode
, data
) != 0) {
883 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
884 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
885 free_nfs_cb_data(data
);
891 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
893 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
894 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
908 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
910 struct nfs_cb_data
*data
= private_data
;
911 struct nfs_context
*nfs
= data
->nfs
;
914 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
916 if (status
== RPC_STATUS_ERROR
) {
917 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
918 free_nfs_cb_data(data
);
921 if (status
== RPC_STATUS_CANCEL
) {
922 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
923 free_nfs_cb_data(data
);
928 if (res
->status
!= NFS3_OK
) {
929 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
930 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
931 free_nfs_cb_data(data
);
935 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
936 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
937 free_nfs_cb_data(data
);
940 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
942 struct nfs_mcb_data
*mdata
= private_data
;
943 struct nfs_cb_data
*data
= mdata
->data
;
944 struct nfs_context
*nfs
= data
->nfs
;
947 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
951 if (status
== RPC_STATUS_ERROR
) {
952 /* flag the failure but do not invoke callback until we have received all responses */
955 if (status
== RPC_STATUS_CANCEL
) {
956 /* flag the cancellation but do not invoke callback until we have received all responses */
960 /* reassemble the data into the buffer */
961 if (status
== RPC_STATUS_SUCCESS
) {
963 if (res
->status
!= NFS3_OK
) {
964 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
967 if (res
->READ3res_u
.resok
.count
> 0) {
968 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
969 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
970 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
976 if (data
->num_calls
> 0) {
977 /* still waiting for more replies */
982 if (data
->error
!= 0) {
983 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
984 free_nfs_cb_data(data
);
988 if (data
->cancel
!= 0) {
989 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
990 free_nfs_cb_data(data
);
995 data
->nfsfh
->offset
= data
->max_offset
;
996 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
998 free_nfs_cb_data(data
);
1002 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
1004 struct nfs_cb_data
*data
;
1006 data
= malloc(sizeof(struct nfs_cb_data
));
1008 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1011 memset(data
, 0, sizeof(struct nfs_cb_data
));
1014 data
->private_data
= private_data
;
1015 data
->nfsfh
= nfsfh
;
1017 nfsfh
->offset
= offset
;
1019 if (count
<= nfs_get_readmax(nfs
)) {
1020 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_cb
, &nfsfh
->fh
, offset
, count
, data
) != 0) {
1021 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1022 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1023 free_nfs_cb_data(data
);
1029 /* trying to read more than maximum server read size, we has to chop it up into smaller
1030 * reads and collect into a reassembly buffer.
1031 * we send all reads in parallell so that performance is still good.
1033 data
->max_offset
= offset
;
1034 data
->start_offset
= offset
;
1036 data
->buffer
= malloc(count
);
1037 if (data
->buffer
== NULL
) {
1038 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1039 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1040 free_nfs_cb_data(data
);
1045 uint64_t readcount
= count
;
1046 struct nfs_mcb_data
*mdata
;
1048 if (readcount
> nfs_get_readmax(nfs
)) {
1049 readcount
= nfs_get_readmax(nfs
);
1052 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1053 if (mdata
== NULL
) {
1054 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1057 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1059 mdata
->offset
= offset
;
1060 mdata
->count
= readcount
;
1061 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_mcb
, &nfsfh
->fh
, offset
, readcount
, mdata
) != 0) {
1062 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1063 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1069 offset
+= readcount
;
1079 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1081 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1089 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1091 struct nfs_cb_data
*data
= private_data
;
1092 struct nfs_context
*nfs
= data
->nfs
;
1095 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1097 if (status
== RPC_STATUS_ERROR
) {
1098 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1099 free_nfs_cb_data(data
);
1102 if (status
== RPC_STATUS_CANCEL
) {
1103 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1104 free_nfs_cb_data(data
);
1109 if (res
->status
!= NFS3_OK
) {
1110 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1111 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1112 free_nfs_cb_data(data
);
1116 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1117 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1118 free_nfs_cb_data(data
);
1121 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1123 struct nfs_mcb_data
*mdata
= private_data
;
1124 struct nfs_cb_data
*data
= mdata
->data
;
1125 struct nfs_context
*nfs
= data
->nfs
;
1128 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1132 if (status
== RPC_STATUS_ERROR
) {
1133 /* flag the failure but do not invoke callback until we have received all responses */
1136 if (status
== RPC_STATUS_CANCEL
) {
1137 /* flag the cancellation but do not invoke callback until we have received all responses */
1141 if (status
== RPC_STATUS_SUCCESS
) {
1143 if (res
->status
!= NFS3_OK
) {
1144 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1147 if (res
->WRITE3res_u
.resok
.count
> 0) {
1148 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1149 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1155 if (data
->num_calls
> 0) {
1156 /* still waiting for more replies */
1161 if (data
->error
!= 0) {
1162 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1163 free_nfs_cb_data(data
);
1167 if (data
->cancel
!= 0) {
1168 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1169 free_nfs_cb_data(data
);
1174 data
->nfsfh
->offset
= data
->max_offset
;
1175 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1177 free_nfs_cb_data(data
);
1182 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
)
1184 struct nfs_cb_data
*data
;
1186 data
= malloc(sizeof(struct nfs_cb_data
));
1188 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1191 memset(data
, 0, sizeof(struct nfs_cb_data
));
1194 data
->private_data
= private_data
;
1195 data
->nfsfh
= nfsfh
;
1197 nfsfh
->offset
= offset
;
1199 if (count
<= nfs_get_writemax(nfs
)) {
1200 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_cb
, &nfsfh
->fh
, buf
, offset
, count
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, data
) != 0) {
1201 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1202 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1203 free_nfs_cb_data(data
);
1209 /* trying to write more than maximum server write size, we has to chop it up into smaller
1211 * we send all writes in parallell so that performance is still good.
1213 data
->max_offset
= offset
;
1214 data
->start_offset
= offset
;
1217 uint64_t writecount
= count
;
1218 struct nfs_mcb_data
*mdata
;
1220 if (writecount
> nfs_get_writemax(nfs
)) {
1221 writecount
= nfs_get_writemax(nfs
);
1224 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1225 if (mdata
== NULL
) {
1226 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1229 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1231 mdata
->offset
= offset
;
1232 mdata
->count
= writecount
;
1234 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) {
1235 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1236 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1241 count
-= writecount
;
1242 offset
+= writecount
;
1252 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1254 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1264 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1266 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1267 free(nfsfh
->fh
.data
.data_val
);
1268 nfsfh
->fh
.data
.data_val
= NULL
;
1272 cb(0, nfs
, NULL
, private_data
);
1283 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1285 struct nfs_cb_data
*data
;
1287 data
= malloc(sizeof(struct nfs_cb_data
));
1289 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1292 memset(data
, 0, sizeof(struct nfs_cb_data
));
1295 data
->private_data
= private_data
;
1297 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &nfsfh
->fh
, data
) != 0) {
1298 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1299 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1300 free_nfs_cb_data(data
);
1311 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1313 struct nfs_cb_data
*data
= private_data
;
1314 struct nfs_context
*nfs
= data
->nfs
;
1317 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1319 if (status
== RPC_STATUS_ERROR
) {
1320 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1321 free_nfs_cb_data(data
);
1324 if (status
== RPC_STATUS_CANCEL
) {
1325 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1326 free_nfs_cb_data(data
);
1331 if (res
->status
!= NFS3_OK
) {
1332 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1333 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1334 free_nfs_cb_data(data
);
1338 data
->cb(0, nfs
, NULL
, data
->private_data
);
1339 free_nfs_cb_data(data
);
1342 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1344 struct nfs_cb_data
*data
;
1346 data
= malloc(sizeof(struct nfs_cb_data
));
1348 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1351 memset(data
, 0, sizeof(struct nfs_cb_data
));
1354 data
->private_data
= private_data
;
1356 if (rpc_nfs_commit_async(nfs
->rpc
, nfs_fsync_cb
, &nfsfh
->fh
, data
) != 0) {
1357 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1358 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1359 free_nfs_cb_data(data
);
1371 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1373 struct nfs_cb_data
*data
= private_data
;
1374 struct nfs_context
*nfs
= data
->nfs
;
1377 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1379 if (status
== RPC_STATUS_ERROR
) {
1380 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1381 free_nfs_cb_data(data
);
1384 if (status
== RPC_STATUS_CANCEL
) {
1385 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1386 free_nfs_cb_data(data
);
1391 if (res
->status
!= NFS3_OK
) {
1392 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1393 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1394 free_nfs_cb_data(data
);
1398 data
->cb(0, nfs
, NULL
, data
->private_data
);
1399 free_nfs_cb_data(data
);
1402 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1404 struct nfs_cb_data
*data
;
1407 data
= malloc(sizeof(struct nfs_cb_data
));
1409 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1412 memset(data
, 0, sizeof(struct nfs_cb_data
));
1415 data
->private_data
= private_data
;
1417 memset(&args
, 0, sizeof(SETATTR3args
));
1418 args
.object
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
1419 args
.object
.data
.data_val
= nfsfh
->fh
.data
.data_val
;
1420 args
.new_attributes
.size
.set_it
= 1;
1421 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1423 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1424 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1425 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1426 free_nfs_cb_data(data
);
1436 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1438 uint64_t offset
= data
->continue_int
;
1441 nfsfh
.fh
.data
.data_val
= data
->fh
.data
.data_val
;
1442 nfsfh
.fh
.data
.data_len
= data
->fh
.data
.data_len
;
1444 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1445 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1446 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1447 free_nfs_cb_data(data
);
1450 free_nfs_cb_data(data
);
1454 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1460 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1461 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1474 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1477 struct nfs_cb_data
*data
= private_data
;
1478 struct nfs_context
*nfs
= data
->nfs
;
1479 char *str
= data
->continue_data
;
1481 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1483 str
= &str
[strlen(str
) + 1];
1485 if (status
== RPC_STATUS_ERROR
) {
1486 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1487 free_nfs_cb_data(data
);
1490 if (status
== RPC_STATUS_CANCEL
) {
1491 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1492 free_nfs_cb_data(data
);
1497 if (res
->status
!= NFS3_OK
) {
1498 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
));
1499 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1500 free_nfs_cb_data(data
);
1504 data
->cb(0, nfs
, NULL
, data
->private_data
);
1505 free_nfs_cb_data(data
);
1508 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1510 char *str
= data
->continue_data
;
1513 str
= &str
[strlen(str
) + 1];
1515 memset(&args
, 0, sizeof(MKDIR3args
));
1516 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1517 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1518 args
.where
.name
= str
;
1519 args
.attributes
.mode
.set_it
= 1;
1520 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
1522 if (rpc_nfs_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
1523 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1524 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1525 free_nfs_cb_data(data
);
1531 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1536 new_path
= strdup(path
);
1537 if (new_path
== NULL
) {
1538 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1542 ptr
= strrchr(new_path
, '/');
1544 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1549 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1550 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1551 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1565 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1568 struct nfs_cb_data
*data
= private_data
;
1569 struct nfs_context
*nfs
= data
->nfs
;
1570 char *str
= data
->continue_data
;
1572 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1574 str
= &str
[strlen(str
) + 1];
1576 if (status
== RPC_STATUS_ERROR
) {
1577 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1578 free_nfs_cb_data(data
);
1581 if (status
== RPC_STATUS_CANCEL
) {
1582 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1583 free_nfs_cb_data(data
);
1588 if (res
->status
!= NFS3_OK
) {
1589 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
));
1590 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1591 free_nfs_cb_data(data
);
1595 data
->cb(0, nfs
, NULL
, data
->private_data
);
1596 free_nfs_cb_data(data
);
1599 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1601 char *str
= data
->continue_data
;
1603 str
= &str
[strlen(str
) + 1];
1605 if (rpc_nfs_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &data
->fh
, str
, data
) != 0) {
1606 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1607 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1608 free_nfs_cb_data(data
);
1614 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1619 new_path
= strdup(path
);
1620 if (new_path
== NULL
) {
1621 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1625 ptr
= strrchr(new_path
, '/');
1627 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1632 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1633 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1634 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1647 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1650 struct nfs_cb_data
*data
= private_data
;
1651 struct nfs_context
*nfs
= data
->nfs
;
1652 struct nfsfh
*nfsfh
;
1653 char *str
= data
->continue_data
;
1655 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1657 if (status
== RPC_STATUS_ERROR
) {
1658 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1659 free_nfs_cb_data(data
);
1662 if (status
== RPC_STATUS_CANCEL
) {
1663 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1664 free_nfs_cb_data(data
);
1668 str
= &str
[strlen(str
) + 1];
1670 if (res
->status
!= NFS3_OK
) {
1671 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
));
1672 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1677 nfsfh
= malloc(sizeof(struct nfsfh
));
1678 if (nfsfh
== NULL
) {
1679 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1680 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1681 free_nfs_cb_data(data
);
1684 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1686 /* copy the filehandle */
1687 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
1688 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
1689 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
1691 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1692 free_nfs_cb_data(data
);
1697 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1700 struct nfs_cb_data
*data
= private_data
;
1701 struct nfs_context
*nfs
= data
->nfs
;
1702 char *str
= data
->continue_data
;
1704 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1706 if (status
== RPC_STATUS_ERROR
) {
1707 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1708 free_nfs_cb_data(data
);
1711 if (status
== RPC_STATUS_CANCEL
) {
1712 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1713 free_nfs_cb_data(data
);
1717 str
= &str
[strlen(str
) + 1];
1719 if (res
->status
!= NFS3_OK
) {
1720 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
));
1721 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1726 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &data
->fh
, str
, data
) != 0) {
1727 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
1728 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1729 free_nfs_cb_data(data
);
1735 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1737 char *str
= data
->continue_data
;
1740 str
= &str
[strlen(str
) + 1];
1742 memset(&args
, 0, sizeof(CREATE3args
));
1743 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1744 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1745 args
.where
.name
= str
;
1746 args
.how
.mode
= UNCHECKED
;
1747 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
1748 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
1750 if (rpc_nfs_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
1751 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
1752 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1753 free_nfs_cb_data(data
);
1759 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1764 new_path
= strdup(path
);
1765 if (new_path
== NULL
) {
1766 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1770 ptr
= strrchr(new_path
, '/');
1772 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1777 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
1778 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
1779 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1792 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1795 struct nfs_cb_data
*data
= private_data
;
1796 struct nfs_context
*nfs
= data
->nfs
;
1797 char *str
= data
->continue_data
;
1799 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1801 str
= &str
[strlen(str
) + 1];
1803 if (status
== RPC_STATUS_ERROR
) {
1804 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1805 free_nfs_cb_data(data
);
1808 if (status
== RPC_STATUS_CANCEL
) {
1809 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1810 free_nfs_cb_data(data
);
1815 if (res
->status
!= NFS3_OK
) {
1816 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
));
1817 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1818 free_nfs_cb_data(data
);
1822 data
->cb(0, nfs
, NULL
, data
->private_data
);
1823 free_nfs_cb_data(data
);
1826 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1828 char *str
= data
->continue_data
;
1830 str
= &str
[strlen(str
) + 1];
1832 if (rpc_nfs_remove_async(nfs
->rpc
, nfs_unlink_cb
, &data
->fh
, str
, data
) != 0) {
1833 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
1834 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1835 free_nfs_cb_data(data
);
1841 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1846 new_path
= strdup(path
);
1847 if (new_path
== NULL
) {
1848 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1852 ptr
= strrchr(new_path
, '/');
1854 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1859 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1860 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
1861 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1872 struct mknod_cb_data
{
1879 static void free_mknod_cb_data(void *ptr
)
1881 struct mknod_cb_data
*data
= ptr
;
1887 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1890 struct nfs_cb_data
*data
= private_data
;
1891 struct nfs_context
*nfs
= data
->nfs
;
1892 char *str
= data
->continue_data
;
1894 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1896 str
= &str
[strlen(str
) + 1];
1898 if (status
== RPC_STATUS_ERROR
) {
1899 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1900 free_nfs_cb_data(data
);
1903 if (status
== RPC_STATUS_CANCEL
) {
1904 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1905 free_nfs_cb_data(data
);
1910 if (res
->status
!= NFS3_OK
) {
1911 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
));
1912 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1913 free_nfs_cb_data(data
);
1917 data
->cb(0, nfs
, NULL
, data
->private_data
);
1918 free_nfs_cb_data(data
);
1921 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1923 struct mknod_cb_data
*cb_data
= data
->continue_data
;
1924 char *str
= cb_data
->path
;
1926 str
= &str
[strlen(str
) + 1];
1928 if (rpc_nfs_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &data
->fh
, str
, cb_data
->mode
, cb_data
->major
, cb_data
->minor
, data
) != 0) {
1929 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1930 free_nfs_cb_data(data
);
1936 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
1939 struct mknod_cb_data
*cb_data
;
1941 cb_data
= malloc(sizeof(struct mknod_cb_data
));
1942 if (cb_data
== NULL
) {
1943 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
1947 cb_data
->path
= strdup(path
);
1948 if (cb_data
->path
== NULL
) {
1949 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1954 ptr
= strrchr(cb_data
->path
, '/');
1956 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1961 cb_data
->mode
= mode
;
1962 cb_data
->major
= major(dev
);
1963 cb_data
->minor
= minor(dev
);
1965 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1966 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
1967 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1968 free_mknod_cb_data(cb_data
);
1979 /* ReadDirPlus Emulation Callback data */
1980 struct rdpe_cb_data
{
1983 struct nfs_cb_data
*data
;
1986 /* ReadDirPlus Emulation LOOKUP Callback data */
1987 struct rdpe_lookup_cb_data
{
1988 struct rdpe_cb_data
*rdpe_cb_data
;
1989 struct nfsdirent
*nfsdirent
;
1992 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
1993 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1995 LOOKUP3res
*res
= command_data
;
1996 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
1997 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
1998 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
1999 struct nfsdir
*nfsdir
= data
->continue_data
;
2000 struct nfs_context
*nfs
= data
->nfs
;
2001 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
2003 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2005 free(rdpe_lookup_cb_data
);
2007 rdpe_cb_data
->getattrcount
--;
2009 if (status
== RPC_STATUS_ERROR
) {
2010 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2012 if (status
== RPC_STATUS_CANCEL
) {
2013 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2015 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2016 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2018 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2019 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2020 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2022 nfsdirent
->type
= attributes
->type
;
2023 nfsdirent
->mode
= attributes
->mode
;
2024 nfsdirent
->size
= attributes
->size
;
2026 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2027 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2028 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2029 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2030 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2031 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2035 if (rdpe_cb_data
->getattrcount
== 0) {
2036 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2037 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2038 nfs_free_nfsdir(nfsdir
);
2040 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2044 data
->continue_data
= NULL
;
2045 free_nfs_cb_data(data
);
2049 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2051 READDIR3res
*res
= command_data
;
2052 struct nfs_cb_data
*data
= private_data
;
2053 struct nfs_context
*nfs
= data
->nfs
;
2054 struct nfsdir
*nfsdir
= data
->continue_data
;
2055 struct nfsdirent
*nfsdirent
;
2056 struct entry3
*entry
;
2058 struct rdpe_cb_data
*rdpe_cb_data
;
2060 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2062 if (status
== RPC_STATUS_ERROR
) {
2063 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2064 nfs_free_nfsdir(nfsdir
);
2065 data
->continue_data
= NULL
;
2066 free_nfs_cb_data(data
);
2070 if (status
== RPC_STATUS_CANCEL
) {
2071 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2072 nfs_free_nfsdir(nfsdir
);
2073 data
->continue_data
= NULL
;
2074 free_nfs_cb_data(data
);
2078 if (res
->status
!= NFS3_OK
) {
2079 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
));
2080 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2081 nfs_free_nfsdir(nfsdir
);
2082 data
->continue_data
= NULL
;
2083 free_nfs_cb_data(data
);
2087 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2088 while (entry
!= NULL
) {
2089 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2090 if (nfsdirent
== NULL
) {
2091 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2092 nfs_free_nfsdir(nfsdir
);
2093 data
->continue_data
= NULL
;
2094 free_nfs_cb_data(data
);
2097 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2098 nfsdirent
->name
= strdup(entry
->name
);
2099 if (nfsdirent
->name
== NULL
) {
2100 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2101 nfs_free_nfsdir(nfsdir
);
2102 data
->continue_data
= NULL
;
2103 free_nfs_cb_data(data
);
2106 nfsdirent
->inode
= entry
->fileid
;
2108 nfsdirent
->next
= nfsdir
->entries
;
2109 nfsdir
->entries
= nfsdirent
;
2111 cookie
= entry
->cookie
;
2112 entry
= entry
->nextentry
;
2115 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2116 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, cookie
, res
->READDIR3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2117 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2118 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2119 nfs_free_nfsdir(nfsdir
);
2120 data
->continue_data
= NULL
;
2121 free_nfs_cb_data(data
);
2127 /* steal the dirhandle */
2128 nfsdir
->current
= nfsdir
->entries
;
2130 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2131 rdpe_cb_data
->getattrcount
= 0;
2132 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2133 rdpe_cb_data
->data
= data
;
2134 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2135 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2137 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2138 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2139 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2141 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &data
->fh
, nfsdirent
->name
, rdpe_lookup_cb_data
) != 0) {
2142 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2144 /* if we have already commands in flight, we cant just stop, we have to wait for the
2145 * commands in flight to complete
2147 if (rdpe_cb_data
->getattrcount
> 0) {
2148 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2149 free(rdpe_lookup_cb_data
);
2153 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2154 nfs_free_nfsdir(nfsdir
);
2155 data
->continue_data
= NULL
;
2156 free_nfs_cb_data(data
);
2157 free(rdpe_lookup_cb_data
);
2161 rdpe_cb_data
->getattrcount
++;
2166 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2168 READDIRPLUS3res
*res
= command_data
;
2169 struct nfs_cb_data
*data
= private_data
;
2170 struct nfs_context
*nfs
= data
->nfs
;
2171 struct nfsdir
*nfsdir
= data
->continue_data
;
2172 struct entryplus3
*entry
;
2175 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2177 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2180 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2181 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2182 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2183 nfs_free_nfsdir(nfsdir
);
2184 data
->continue_data
= NULL
;
2185 free_nfs_cb_data(data
);
2191 if (status
== RPC_STATUS_CANCEL
) {
2192 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2193 nfs_free_nfsdir(nfsdir
);
2194 data
->continue_data
= NULL
;
2195 free_nfs_cb_data(data
);
2199 if (res
->status
!= NFS3_OK
) {
2200 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
));
2201 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2202 nfs_free_nfsdir(nfsdir
);
2203 data
->continue_data
= NULL
;
2204 free_nfs_cb_data(data
);
2208 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2209 while (entry
!= NULL
) {
2210 struct nfsdirent
*nfsdirent
;
2212 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2213 if (nfsdirent
== NULL
) {
2214 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2215 nfs_free_nfsdir(nfsdir
);
2216 data
->continue_data
= NULL
;
2217 free_nfs_cb_data(data
);
2220 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2221 nfsdirent
->name
= strdup(entry
->name
);
2222 if (nfsdirent
->name
== NULL
) {
2223 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2224 nfs_free_nfsdir(nfsdir
);
2225 data
->continue_data
= NULL
;
2226 free_nfs_cb_data(data
);
2229 nfsdirent
->inode
= entry
->fileid
;
2230 if (entry
->name_attributes
.attributes_follow
) {
2231 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2232 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2233 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2235 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2236 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2237 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2238 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2239 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2240 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2243 nfsdirent
->next
= nfsdir
->entries
;
2244 nfsdir
->entries
= nfsdirent
;
2246 cookie
= entry
->cookie
;
2247 entry
= entry
->nextentry
;
2250 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2251 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, cookie
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2252 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2253 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2254 nfs_free_nfsdir(nfsdir
);
2255 data
->continue_data
= NULL
;
2256 free_nfs_cb_data(data
);
2262 /* steal the dirhandle */
2263 data
->continue_data
= NULL
;
2264 nfsdir
->current
= nfsdir
->entries
;
2266 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2267 free_nfs_cb_data(data
);
2270 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2274 memset(cv
, 0, sizeof(cookieverf3
));
2275 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2276 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2277 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2278 free_nfs_cb_data(data
);
2284 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2286 struct nfsdir
*nfsdir
;
2288 nfsdir
= malloc(sizeof(struct nfsdir
));
2289 if (nfsdir
== NULL
) {
2290 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2293 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2295 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2296 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2304 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2306 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2308 if (nfsdir
->current
!= NULL
) {
2309 nfsdir
->current
= nfsdir
->current
->next
;
2315 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2317 nfs_free_nfsdir(nfsdir
);
2329 struct lseek_cb_data
{
2330 struct nfs_context
*nfs
;
2331 struct nfsfh
*nfsfh
;
2337 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2340 struct lseek_cb_data
*data
= private_data
;
2341 struct nfs_context
*nfs
= data
->nfs
;
2343 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2345 if (status
== RPC_STATUS_ERROR
) {
2346 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2350 if (status
== RPC_STATUS_CANCEL
) {
2351 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2357 if (res
->status
!= NFS3_OK
) {
2358 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2359 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2364 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2365 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2369 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2371 struct lseek_cb_data
*data
;
2373 if (whence
== SEEK_SET
) {
2374 nfsfh
->offset
= offset
;
2375 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2378 if (whence
== SEEK_CUR
) {
2379 nfsfh
->offset
+= offset
;
2380 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2384 data
= malloc(sizeof(struct lseek_cb_data
));
2386 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2391 data
->nfsfh
= nfsfh
;
2392 data
->offset
= offset
;
2394 data
->private_data
= private_data
;
2396 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &nfsfh
->fh
, data
) != 0) {
2397 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
2410 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2413 struct nfs_cb_data
*data
= private_data
;
2414 struct nfs_context
*nfs
= data
->nfs
;
2415 struct statvfs svfs
;
2417 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2419 if (status
== RPC_STATUS_ERROR
) {
2420 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2421 free_nfs_cb_data(data
);
2424 if (status
== RPC_STATUS_CANCEL
) {
2425 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2426 free_nfs_cb_data(data
);
2431 if (res
->status
!= NFS3_OK
) {
2432 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
));
2433 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2434 free_nfs_cb_data(data
);
2438 svfs
.f_bsize
= 4096;
2439 svfs
.f_frsize
= 4096;
2440 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
2441 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
2442 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
2443 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
2444 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
2445 #if !defined(ANDROID)
2446 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
2449 svfs
.f_namemax
= 256;
2452 data
->cb(0, nfs
, &svfs
, data
->private_data
);
2453 free_nfs_cb_data(data
);
2456 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2458 if (rpc_nfs_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &data
->fh
, data
) != 0) {
2459 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
2460 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2461 free_nfs_cb_data(data
);
2467 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2469 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
2470 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2483 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2486 struct nfs_cb_data
*data
= private_data
;
2487 struct nfs_context
*nfs
= data
->nfs
;
2489 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2491 if (status
== RPC_STATUS_ERROR
) {
2492 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2493 free_nfs_cb_data(data
);
2496 if (status
== RPC_STATUS_CANCEL
) {
2497 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2498 free_nfs_cb_data(data
);
2503 if (res
->status
!= NFS3_OK
) {
2504 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
));
2505 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2506 free_nfs_cb_data(data
);
2511 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
2512 free_nfs_cb_data(data
);
2515 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2519 args
.symlink
.data
.data_len
= data
->fh
.data
.data_len
;
2520 args
.symlink
.data
.data_val
= data
->fh
.data
.data_val
;
2522 if (rpc_nfs_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
2523 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
2524 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2525 free_nfs_cb_data(data
);
2531 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2533 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
2534 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2547 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2549 struct nfs_cb_data
*data
= private_data
;
2550 struct nfs_context
*nfs
= data
->nfs
;
2553 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2555 if (status
== RPC_STATUS_ERROR
) {
2556 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2557 free_nfs_cb_data(data
);
2560 if (status
== RPC_STATUS_CANCEL
) {
2561 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2562 free_nfs_cb_data(data
);
2567 if (res
->status
!= NFS3_OK
) {
2568 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2569 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2570 free_nfs_cb_data(data
);
2574 data
->cb(0, nfs
, NULL
, data
->private_data
);
2575 free_nfs_cb_data(data
);
2578 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2582 memset(&args
, 0, sizeof(SETATTR3args
));
2583 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2584 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2585 args
.new_attributes
.mode
.set_it
= 1;
2586 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2588 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
2589 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2590 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2591 free_nfs_cb_data(data
);
2598 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2600 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
2601 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2611 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
2613 struct nfs_cb_data
*data
;
2615 data
= malloc(sizeof(struct nfs_cb_data
));
2617 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
2620 memset(data
, 0, sizeof(struct nfs_cb_data
));
2623 data
->private_data
= private_data
;
2624 data
->continue_int
= mode
;
2625 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2626 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2627 if (data
->fh
.data
.data_val
== NULL
) {
2628 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2629 free_nfs_cb_data(data
);
2632 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2634 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
2635 free_nfs_cb_data(data
);
2647 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2649 struct nfs_cb_data
*data
= private_data
;
2650 struct nfs_context
*nfs
= data
->nfs
;
2653 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2655 if (status
== RPC_STATUS_ERROR
) {
2656 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2657 free_nfs_cb_data(data
);
2660 if (status
== RPC_STATUS_CANCEL
) {
2661 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2662 free_nfs_cb_data(data
);
2667 if (res
->status
!= NFS3_OK
) {
2668 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2669 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2670 free_nfs_cb_data(data
);
2674 data
->cb(0, nfs
, NULL
, data
->private_data
);
2675 free_nfs_cb_data(data
);
2678 struct nfs_chown_data
{
2683 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2686 struct nfs_chown_data
*chown_data
= data
->continue_data
;
2688 memset(&args
, 0, sizeof(SETATTR3args
));
2689 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2690 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2691 if (chown_data
->uid
!= (uid_t
)-1) {
2692 args
.new_attributes
.uid
.set_it
= 1;
2693 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
2695 if (chown_data
->gid
!= (gid_t
)-1) {
2696 args
.new_attributes
.gid
.set_it
= 1;
2697 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
2700 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
2701 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2702 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2703 free_nfs_cb_data(data
);
2710 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2712 struct nfs_chown_data
*chown_data
;
2714 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2715 if (chown_data
== NULL
) {
2716 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2720 chown_data
->uid
= uid
;
2721 chown_data
->gid
= gid
;
2723 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
2724 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2735 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2737 struct nfs_cb_data
*data
;
2738 struct nfs_chown_data
*chown_data
;
2740 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2741 if (chown_data
== NULL
) {
2742 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2746 chown_data
->uid
= uid
;
2747 chown_data
->gid
= gid
;
2750 data
= malloc(sizeof(struct nfs_cb_data
));
2752 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
2755 memset(data
, 0, sizeof(struct nfs_cb_data
));
2758 data
->private_data
= private_data
;
2759 data
->continue_data
= chown_data
;
2760 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2761 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2762 if (data
->fh
.data
.data_val
== NULL
) {
2763 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2764 free_nfs_cb_data(data
);
2767 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2770 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
2771 free_nfs_cb_data(data
);
2785 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2787 struct nfs_cb_data
*data
= private_data
;
2788 struct nfs_context
*nfs
= data
->nfs
;
2791 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2793 if (status
== RPC_STATUS_ERROR
) {
2794 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2795 free_nfs_cb_data(data
);
2798 if (status
== RPC_STATUS_CANCEL
) {
2799 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2800 free_nfs_cb_data(data
);
2805 if (res
->status
!= NFS3_OK
) {
2806 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2807 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2808 free_nfs_cb_data(data
);
2812 data
->cb(0, nfs
, NULL
, data
->private_data
);
2813 free_nfs_cb_data(data
);
2816 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2819 struct timeval
*utimes_data
= data
->continue_data
;
2821 memset(&args
, 0, sizeof(SETATTR3args
));
2822 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2823 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2824 if (utimes_data
!= NULL
) {
2825 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
2826 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
2827 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
2828 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
2829 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
2830 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
2832 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
2833 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
2836 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
2837 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2838 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2839 free_nfs_cb_data(data
);
2846 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
2848 struct timeval
*new_times
= NULL
;
2850 if (times
!= NULL
) {
2851 new_times
= malloc(sizeof(struct timeval
)*2);
2852 if (new_times
== NULL
) {
2853 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2857 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
2860 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2861 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2871 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
2873 struct timeval
*new_times
= NULL
;
2875 if (times
!= NULL
) {
2876 new_times
= malloc(sizeof(struct timeval
)*2);
2877 if (new_times
== NULL
) {
2878 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2882 new_times
[0].tv_sec
= times
->actime
;
2883 new_times
[0].tv_usec
= 0;
2884 new_times
[1].tv_sec
= times
->modtime
;
2885 new_times
[1].tv_usec
= 0;
2888 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2889 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2900 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2903 struct nfs_cb_data
*data
= private_data
;
2904 struct nfs_context
*nfs
= data
->nfs
;
2905 unsigned int nfsmode
= 0;
2907 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2909 if (status
== RPC_STATUS_ERROR
) {
2910 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2911 free_nfs_cb_data(data
);
2914 if (status
== RPC_STATUS_CANCEL
) {
2915 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2916 free_nfs_cb_data(data
);
2921 if (res
->status
!= NFS3_OK
) {
2922 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
));
2923 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2924 free_nfs_cb_data(data
);
2928 if (data
->continue_int
& R_OK
) {
2929 nfsmode
|= ACCESS3_READ
;
2931 if (data
->continue_int
& W_OK
) {
2932 nfsmode
|= ACCESS3_MODIFY
;
2934 if (data
->continue_int
& X_OK
) {
2935 nfsmode
|= ACCESS3_EXECUTE
;
2938 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
2939 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2940 nfsmode
&ACCESS3_READ
?'r':'-',
2941 nfsmode
&ACCESS3_MODIFY
?'w':'-',
2942 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
2943 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
2944 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
2945 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
2946 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2947 free_nfs_cb_data(data
);
2951 data
->cb(0, nfs
, NULL
, data
->private_data
);
2952 free_nfs_cb_data(data
);
2955 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2959 if (data
->continue_int
& R_OK
) {
2960 nfsmode
|= ACCESS3_READ
;
2962 if (data
->continue_int
& W_OK
) {
2963 nfsmode
|= ACCESS3_MODIFY
;
2965 if (data
->continue_int
& X_OK
) {
2966 nfsmode
|= ACCESS3_EXECUTE
;
2969 if (rpc_nfs_access_async(nfs
->rpc
, nfs_access_cb
, &data
->fh
, nfsmode
, data
) != 0) {
2970 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
2971 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2972 free_nfs_cb_data(data
);
2978 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2980 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
2981 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2993 struct nfs_symlink_data
{
2995 char *newpathparent
;
2996 char *newpathobject
;
2999 static void free_nfs_symlink_data(void *mem
)
3001 struct nfs_symlink_data
*data
= mem
;
3003 if (data
->oldpath
!= NULL
) {
3004 free(data
->oldpath
);
3006 if (data
->newpathparent
!= NULL
) {
3007 free(data
->newpathparent
);
3009 if (data
->newpathobject
!= NULL
) {
3010 free(data
->newpathobject
);
3015 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3018 struct nfs_cb_data
*data
= private_data
;
3019 struct nfs_context
*nfs
= data
->nfs
;
3020 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3022 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3024 if (status
== RPC_STATUS_ERROR
) {
3025 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3026 free_nfs_cb_data(data
);
3029 if (status
== RPC_STATUS_CANCEL
) {
3030 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3031 free_nfs_cb_data(data
);
3036 if (res
->status
!= NFS3_OK
) {
3037 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
));
3038 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3039 free_nfs_cb_data(data
);
3043 data
->cb(0, nfs
, NULL
, data
->private_data
);
3044 free_nfs_cb_data(data
);
3047 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3049 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3052 memset(&sa
, 0, sizeof(SYMLINK3args
));
3053 sa
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
3054 sa
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
3055 sa
.where
.name
= symlink_data
->newpathobject
;
3056 sa
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3057 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
;
3058 sa
.symlink
.symlink_data
= symlink_data
->oldpath
;
3060 if (rpc_nfs_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &sa
, data
) != 0) {
3061 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3062 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3063 free_nfs_cb_data(data
);
3069 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3072 struct nfs_symlink_data
*symlink_data
;
3074 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3075 if (symlink_data
== NULL
) {
3076 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3079 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3081 symlink_data
->oldpath
= strdup(oldpath
);
3082 if (symlink_data
->oldpath
== NULL
) {
3083 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3084 free_nfs_symlink_data(symlink_data
);
3088 symlink_data
->newpathparent
= strdup(newpath
);
3089 if (symlink_data
->newpathparent
== NULL
) {
3090 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3091 free_nfs_symlink_data(symlink_data
);
3095 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3097 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3098 free_nfs_symlink_data(symlink_data
);
3104 symlink_data
->newpathobject
= strdup(ptr
);
3105 if (symlink_data
->newpathobject
== NULL
) {
3106 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3107 free_nfs_symlink_data(symlink_data
);
3111 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3112 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3124 struct nfs_rename_data
{
3127 struct nfs_fh3 olddir
;
3130 struct nfs_fh3 newdir
;
3133 static void free_nfs_rename_data(void *mem
)
3135 struct nfs_rename_data
*data
= mem
;
3137 if (data
->oldpath
!= NULL
) {
3138 free(data
->oldpath
);
3140 if (data
->olddir
.data
.data_val
!= NULL
) {
3141 free(data
->olddir
.data
.data_val
);
3143 if (data
->newpath
!= NULL
) {
3144 free(data
->newpath
);
3146 if (data
->newdir
.data
.data_val
!= NULL
) {
3147 free(data
->newdir
.data
.data_val
);
3152 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3155 struct nfs_cb_data
*data
= private_data
;
3156 struct nfs_context
*nfs
= data
->nfs
;
3157 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3159 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3161 if (status
== RPC_STATUS_ERROR
) {
3162 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3163 free_nfs_cb_data(data
);
3166 if (status
== RPC_STATUS_CANCEL
) {
3167 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3168 free_nfs_cb_data(data
);
3173 if (res
->status
!= NFS3_OK
) {
3174 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
));
3175 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3176 free_nfs_cb_data(data
);
3180 data
->cb(0, nfs
, NULL
, data
->private_data
);
3181 free_nfs_cb_data(data
);
3184 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3186 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3188 /* steal the filehandle */
3189 rename_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3190 rename_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3191 data
->fh
.data
.data_val
= NULL
;
3193 if (rpc_nfs_rename_async(nfs
->rpc
, nfs_rename_cb
, &rename_data
->olddir
, rename_data
->oldobject
, &rename_data
->newdir
, rename_data
->newobject
, data
) != 0) {
3194 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3195 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3196 free_nfs_cb_data(data
);
3203 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3205 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3207 /* steal the filehandle */
3208 rename_data
->olddir
.data
.data_len
= data
->fh
.data
.data_len
;
3209 rename_data
->olddir
.data
.data_val
= data
->fh
.data
.data_val
;
3210 data
->fh
.data
.data_val
= NULL
;
3212 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) {
3213 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3214 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3215 free_nfs_cb_data(data
);
3218 data
->continue_data
= NULL
;
3219 free_nfs_cb_data(data
);
3225 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3228 struct nfs_rename_data
*rename_data
;
3230 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3231 if (rename_data
== NULL
) {
3232 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3235 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3237 rename_data
->oldpath
= strdup(oldpath
);
3238 if (rename_data
->oldpath
== NULL
) {
3239 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3240 free_nfs_rename_data(rename_data
);
3243 ptr
= strrchr(rename_data
->oldpath
, '/');
3245 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3246 free_nfs_rename_data(rename_data
);
3251 rename_data
->oldobject
= ptr
;
3254 rename_data
->newpath
= strdup(newpath
);
3255 if (rename_data
->newpath
== NULL
) {
3256 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3257 free_nfs_rename_data(rename_data
);
3260 ptr
= strrchr(rename_data
->newpath
, '/');
3262 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3263 free_nfs_rename_data(rename_data
);
3268 rename_data
->newobject
= ptr
;
3271 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3272 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3283 struct nfs_link_data
{
3285 struct nfs_fh3 oldfh
;
3288 struct nfs_fh3 newdir
;
3291 static void free_nfs_link_data(void *mem
)
3293 struct nfs_link_data
*data
= mem
;
3295 if (data
->oldpath
!= NULL
) {
3296 free(data
->oldpath
);
3298 if (data
->oldfh
.data
.data_val
!= NULL
) {
3299 free(data
->oldfh
.data
.data_val
);
3301 if (data
->newpath
!= NULL
) {
3302 free(data
->newpath
);
3304 if (data
->newdir
.data
.data_val
!= NULL
) {
3305 free(data
->newdir
.data
.data_val
);
3310 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3313 struct nfs_cb_data
*data
= private_data
;
3314 struct nfs_context
*nfs
= data
->nfs
;
3315 struct nfs_link_data
*link_data
= data
->continue_data
;
3317 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3319 if (status
== RPC_STATUS_ERROR
) {
3320 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3321 free_nfs_cb_data(data
);
3324 if (status
== RPC_STATUS_CANCEL
) {
3325 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3326 free_nfs_cb_data(data
);
3331 if (res
->status
!= NFS3_OK
) {
3332 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
));
3333 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3334 free_nfs_cb_data(data
);
3338 data
->cb(0, nfs
, NULL
, data
->private_data
);
3339 free_nfs_cb_data(data
);
3342 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3344 struct nfs_link_data
*link_data
= data
->continue_data
;
3346 /* steal the filehandle */
3347 link_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3348 link_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3349 data
->fh
.data
.data_val
= NULL
;
3351 if (rpc_nfs_link_async(nfs
->rpc
, nfs_link_cb
, &link_data
->oldfh
, &link_data
->newdir
, link_data
->newobject
, data
) != 0) {
3352 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3353 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3354 free_nfs_cb_data(data
);
3361 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3363 struct nfs_link_data
*link_data
= data
->continue_data
;
3365 /* steal the filehandle */
3366 link_data
->oldfh
.data
.data_len
= data
->fh
.data
.data_len
;
3367 link_data
->oldfh
.data
.data_val
= data
->fh
.data
.data_val
;
3368 data
->fh
.data
.data_val
= NULL
;
3370 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) {
3371 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3372 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3373 free_nfs_cb_data(data
);
3376 data
->continue_data
= NULL
;
3377 free_nfs_cb_data(data
);
3383 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3386 struct nfs_link_data
*link_data
;
3388 link_data
= malloc(sizeof(struct nfs_link_data
));
3389 if (link_data
== NULL
) {
3390 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
3393 memset(link_data
, 0, sizeof(struct nfs_link_data
));
3395 link_data
->oldpath
= strdup(oldpath
);
3396 if (link_data
->oldpath
== NULL
) {
3397 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3398 free_nfs_link_data(link_data
);
3402 link_data
->newpath
= strdup(newpath
);
3403 if (link_data
->newpath
== NULL
) {
3404 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3405 free_nfs_link_data(link_data
);
3408 ptr
= strrchr(link_data
->newpath
, '/');
3410 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3411 free_nfs_link_data(link_data
);
3416 link_data
->newobject
= ptr
;
3419 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
3420 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3428 //qqq replace later with lseek()
3429 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
3431 return nfsfh
->offset
;
3437 * Get the maximum supported READ3 size by the server
3439 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
3441 return nfs
->readmax
;
3445 * Get the maximum supported WRITE3 size by the server
3447 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
3449 /* Some ZDR libraries can not marshall PDUs bigger than this */
3450 if (nfs
->writemax
< 32768) {
3451 return nfs
->writemax
;
3456 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
3461 va_start(ap
, error_string
);
3463 vsnprintf(str
, 1024, error_string
, ap
);
3464 if (nfs
->rpc
->error_string
!= NULL
) {
3465 free(nfs
->rpc
->error_string
);
3467 nfs
->rpc
->error_string
= str
;
3473 struct mount_cb_data
{
3479 static void free_mount_cb_data(struct mount_cb_data
*data
)
3481 if (data
->server
!= NULL
) {
3483 data
->server
= NULL
;
3489 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3491 struct mount_cb_data
*data
= private_data
;
3493 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3495 if (status
== RPC_STATUS_ERROR
) {
3496 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3497 free_mount_cb_data(data
);
3500 if (status
== RPC_STATUS_CANCEL
) {
3501 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3502 free_mount_cb_data(data
);
3506 data
->cb(rpc
, 0, command_data
, data
->private_data
);
3507 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
3508 rpc_set_error(rpc
, "Failed to disconnect\n");
3510 free_mount_cb_data(data
);
3513 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3515 struct mount_cb_data
*data
= private_data
;
3517 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3519 /* Dont want any more callbacks even if the socket is closed */
3520 rpc
->connect_cb
= NULL
;
3522 if (status
== RPC_STATUS_ERROR
) {
3523 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3524 free_mount_cb_data(data
);
3527 if (status
== RPC_STATUS_CANCEL
) {
3528 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3529 free_mount_cb_data(data
);
3533 if (rpc_mount_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
3534 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3535 free_mount_cb_data(data
);
3540 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3542 struct mount_cb_data
*data
= private_data
;
3543 uint32_t mount_port
;
3545 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3547 if (status
== RPC_STATUS_ERROR
) {
3548 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3549 free_mount_cb_data(data
);
3552 if (status
== RPC_STATUS_CANCEL
) {
3553 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3554 free_mount_cb_data(data
);
3558 mount_port
= *(uint32_t *)command_data
;
3559 if (mount_port
== 0) {
3560 rpc_set_error(rpc
, "RPC error. Mount program is not available");
3561 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
3562 free_mount_cb_data(data
);
3566 rpc_disconnect(rpc
, "normal disconnect");
3567 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
3568 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3569 free_mount_cb_data(data
);
3574 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3576 struct mount_cb_data
*data
= private_data
;
3578 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3580 if (status
== RPC_STATUS_ERROR
) {
3581 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3582 free_mount_cb_data(data
);
3585 if (status
== RPC_STATUS_CANCEL
) {
3586 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3587 free_mount_cb_data(data
);
3591 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
3592 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3593 free_mount_cb_data(data
);
3598 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3600 struct mount_cb_data
*data
= private_data
;
3602 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3604 /* Dont want any more callbacks even if the socket is closed */
3605 rpc
->connect_cb
= NULL
;
3607 if (status
== RPC_STATUS_ERROR
) {
3608 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3609 free_mount_cb_data(data
);
3612 if (status
== RPC_STATUS_CANCEL
) {
3613 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3614 free_mount_cb_data(data
);
3618 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
3619 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3620 free_mount_cb_data(data
);
3625 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
3627 struct mount_cb_data
*data
;
3629 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3631 data
= malloc(sizeof(struct mount_cb_data
));
3635 memset(data
, 0, sizeof(struct mount_cb_data
));
3637 data
->private_data
= private_data
;
3638 data
->server
= strdup(server
);
3639 if (data
->server
== NULL
) {
3640 free_mount_cb_data(data
);
3643 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
3644 free_mount_cb_data(data
);
3651 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
3653 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
3657 const char *nfs_get_server(struct nfs_context
*nfs
) {
3661 const char *nfs_get_export(struct nfs_context
*nfs
) {
3665 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
3666 return &nfs
->rootfh
;
3669 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {