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
31 #include <sys/statvfs.h>
41 #include <sys/types.h>
45 #include "libnfs-raw.h"
46 #include "libnfs-raw-mount.h"
47 #include "libnfs-raw-nfs.h"
48 #include "libnfs-private.h"
51 struct nfsdirent
*entries
;
52 struct nfsdirent
*current
;
62 struct rpc_context
*rpc
;
65 struct nfs_fh3 rootfh
;
70 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
72 while (nfsdir
->entries
) {
73 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
74 if (nfsdir
->entries
->name
!= NULL
) {
75 free(nfsdir
->entries
->name
);
77 free(nfsdir
->entries
);
78 nfsdir
->entries
= dirent
;
84 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
87 struct nfs_context
*nfs
;
89 char *saved_path
, *path
;
94 continue_func continue_cb
;
96 void (*free_continue_data
)(void *);
101 /* for multi-read/write calls. */
105 off_t start_offset
, max_offset
;
109 struct nfs_mcb_data
{
110 struct nfs_cb_data
*data
;
115 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
118 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
120 rpc_set_auth(nfs
->rpc
, auth
);
123 int nfs_get_fd(struct nfs_context
*nfs
)
125 return rpc_get_fd(nfs
->rpc
);
128 int nfs_which_events(struct nfs_context
*nfs
)
130 return rpc_which_events(nfs
->rpc
);
133 int nfs_service(struct nfs_context
*nfs
, int revents
)
135 return rpc_service(nfs
->rpc
, revents
);
138 char *nfs_get_error(struct nfs_context
*nfs
)
140 return rpc_get_error(nfs
->rpc
);
143 struct nfs_context
*nfs_init_context(void)
145 struct nfs_context
*nfs
;
147 nfs
= malloc(sizeof(struct nfs_context
));
151 nfs
->rpc
= rpc_init_context();
152 if (nfs
->rpc
== NULL
) {
160 nfs
->rootfh
.data
.data_len
= 0;
161 nfs
->rootfh
.data
.data_val
= NULL
;
166 void nfs_destroy_context(struct nfs_context
*nfs
)
168 rpc_destroy_context(nfs
->rpc
);
181 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
182 free(nfs
->rootfh
.data
.data_val
);
183 nfs
->rootfh
.data
.data_val
= NULL
;
189 void free_nfs_cb_data(struct nfs_cb_data
*data
)
191 if (data
->saved_path
!= NULL
) {
192 free(data
->saved_path
);
193 data
->saved_path
= NULL
;
196 if (data
->continue_data
!= NULL
) {
197 data
->free_continue_data(data
->continue_data
);
198 data
->continue_data
= NULL
;
201 if (data
->fh
.data
.data_val
!= NULL
) {
202 free(data
->fh
.data
.data_val
);
203 data
->fh
.data
.data_val
= NULL
;
206 if (data
->buffer
!= NULL
) {
218 static void nfs_mount_10_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
220 struct nfs_cb_data
*data
= private_data
;
221 struct nfs_context
*nfs
= data
->nfs
;
223 if (status
== RPC_STATUS_ERROR
) {
224 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
225 free_nfs_cb_data(data
);
228 if (status
== RPC_STATUS_CANCEL
) {
229 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
230 free_nfs_cb_data(data
);
234 data
->cb(0, nfs
, NULL
, data
->private_data
);
235 free_nfs_cb_data(data
);
238 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
240 struct nfs_cb_data
*data
= private_data
;
241 struct nfs_context
*nfs
= data
->nfs
;
242 FSINFO3res
*res
= command_data
;
244 if (status
== RPC_STATUS_ERROR
) {
245 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
246 free_nfs_cb_data(data
);
249 if (status
== RPC_STATUS_CANCEL
) {
250 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
251 free_nfs_cb_data(data
);
255 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
256 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
258 if (rpc_nfs_getattr_async(rpc
, nfs_mount_10_cb
, &nfs
->rootfh
, data
) != 0) {
259 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
260 free_nfs_cb_data(data
);
265 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
267 struct nfs_cb_data
*data
= private_data
;
268 struct nfs_context
*nfs
= data
->nfs
;
270 if (status
== RPC_STATUS_ERROR
) {
271 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
272 free_nfs_cb_data(data
);
275 if (status
== RPC_STATUS_CANCEL
) {
276 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
277 free_nfs_cb_data(data
);
281 if (rpc_nfs_fsinfo_async(rpc
, nfs_mount_9_cb
, &nfs
->rootfh
, data
) != 0) {
282 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
283 free_nfs_cb_data(data
);
289 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
291 struct nfs_cb_data
*data
= private_data
;
292 struct nfs_context
*nfs
= data
->nfs
;
294 if (status
== RPC_STATUS_ERROR
) {
295 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
296 free_nfs_cb_data(data
);
299 if (status
== RPC_STATUS_CANCEL
) {
300 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
301 free_nfs_cb_data(data
);
305 if (rpc_nfs_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
306 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
307 free_nfs_cb_data(data
);
313 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
315 struct nfs_cb_data
*data
= private_data
;
316 struct nfs_context
*nfs
= data
->nfs
;
319 if (status
== RPC_STATUS_ERROR
) {
320 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
321 free_nfs_cb_data(data
);
324 if (status
== RPC_STATUS_CANCEL
) {
325 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
326 free_nfs_cb_data(data
);
331 if (res
->fhs_status
!= MNT3_OK
) {
332 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
));
333 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
334 free_nfs_cb_data(data
);
338 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
339 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
340 if (nfs
->rootfh
.data
.data_val
== NULL
) {
341 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
342 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
343 free_nfs_cb_data(data
);
346 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
348 rpc_disconnect(rpc
, "normal disconnect");
349 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
350 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
351 free_nfs_cb_data(data
);
357 static void nfs_mount_5_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
;
362 if (status
== RPC_STATUS_ERROR
) {
363 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
364 free_nfs_cb_data(data
);
367 if (status
== RPC_STATUS_CANCEL
) {
368 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
369 free_nfs_cb_data(data
);
373 if (rpc_mount_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
374 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
375 free_nfs_cb_data(data
);
380 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
382 struct nfs_cb_data
*data
= private_data
;
383 struct nfs_context
*nfs
= data
->nfs
;
385 if (status
== RPC_STATUS_ERROR
) {
386 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
387 free_nfs_cb_data(data
);
390 if (status
== RPC_STATUS_CANCEL
) {
391 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
392 free_nfs_cb_data(data
);
396 if (rpc_mount_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
397 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
398 free_nfs_cb_data(data
);
403 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
405 struct nfs_cb_data
*data
= private_data
;
406 struct nfs_context
*nfs
= data
->nfs
;
409 if (status
== RPC_STATUS_ERROR
) {
410 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
411 free_nfs_cb_data(data
);
414 if (status
== RPC_STATUS_CANCEL
) {
415 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
416 free_nfs_cb_data(data
);
420 mount_port
= *(uint32_t *)command_data
;
421 if (mount_port
== 0) {
422 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
423 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
424 free_nfs_cb_data(data
);
428 rpc_disconnect(rpc
, "normal disconnect");
429 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
430 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
431 free_nfs_cb_data(data
);
437 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
439 struct nfs_cb_data
*data
= private_data
;
440 struct nfs_context
*nfs
= data
->nfs
;
442 if (status
== RPC_STATUS_ERROR
) {
443 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
444 free_nfs_cb_data(data
);
447 if (status
== RPC_STATUS_CANCEL
) {
448 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
449 free_nfs_cb_data(data
);
453 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_3_cb
, private_data
) != 0) {
454 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
455 free_nfs_cb_data(data
);
460 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
462 struct nfs_cb_data
*data
= private_data
;
463 struct nfs_context
*nfs
= data
->nfs
;
465 if (status
== RPC_STATUS_ERROR
) {
466 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
467 free_nfs_cb_data(data
);
470 if (status
== RPC_STATUS_CANCEL
) {
471 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
472 free_nfs_cb_data(data
);
476 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
477 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
478 free_nfs_cb_data(data
);
484 * Async call for mounting an nfs share and geting the root filehandle
486 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
488 struct nfs_cb_data
*data
;
489 char *new_server
, *new_export
;
491 data
= malloc(sizeof(struct nfs_cb_data
));
493 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
496 bzero(data
, sizeof(struct nfs_cb_data
));
497 new_server
= strdup(server
);
498 new_export
= strdup(export
);
499 if (nfs
->server
!= NULL
) {
503 nfs
->server
= new_server
;
504 if (nfs
->export
!= NULL
) {
508 nfs
->export
= new_export
;
511 data
->private_data
= private_data
;
513 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
514 rpc_set_error(nfs
->rpc
, "Failed to start connection");
515 free_nfs_cb_data(data
);
525 * Functions to first look up a path, component by component, and then finally call a specific function once
526 * the filehandle for the final component is found.
528 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
530 struct nfs_cb_data
*data
= private_data
;
531 struct nfs_context
*nfs
= data
->nfs
;
534 if (status
== RPC_STATUS_ERROR
) {
535 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
536 free_nfs_cb_data(data
);
539 if (status
== RPC_STATUS_CANCEL
) {
540 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
541 free_nfs_cb_data(data
);
546 if (res
->status
!= NFS3_OK
) {
547 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
));
548 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
549 free_nfs_cb_data(data
);
553 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
554 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
555 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
556 free_nfs_cb_data(data
);
561 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
565 while (*data
->path
== '/') {
570 str
= strchr(path
, '/');
575 while (*data
->path
!= 0) {
581 data
->fh
.data
.data_len
= fh
->data
.data_len
;
582 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
583 if (data
->fh
.data
.data_val
== NULL
) {
584 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
585 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
586 free_nfs_cb_data(data
);
589 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
590 data
->continue_cb(nfs
, data
);
594 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, fh
, path
, data
) != 0) {
595 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
596 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
597 free_nfs_cb_data(data
);
603 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
)
605 struct nfs_cb_data
*data
;
607 if (path
[0] != '/') {
608 rpc_set_error(nfs
->rpc
, "Pathname is not absulute %s", path
);
612 data
= malloc(sizeof(struct nfs_cb_data
));
614 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
617 bzero(data
, sizeof(struct nfs_cb_data
));
620 data
->continue_cb
= continue_cb
;
621 data
->continue_data
= continue_data
;
622 data
->free_continue_data
= free_continue_data
;
623 data
->continue_int
= continue_int
;
624 data
->private_data
= private_data
;
625 data
->saved_path
= strdup(path
);
626 if (data
->saved_path
== NULL
) {
627 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
628 free_nfs_cb_data(data
);
631 data
->path
= data
->saved_path
;
633 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
634 /* return 0 here since the callback will be invoked if there is a failure */
647 static void nfs_stat_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
650 struct nfs_cb_data
*data
= private_data
;
651 struct nfs_context
*nfs
= data
->nfs
;
654 if (status
== RPC_STATUS_ERROR
) {
655 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
656 free_nfs_cb_data(data
);
659 if (status
== RPC_STATUS_CANCEL
) {
660 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
661 free_nfs_cb_data(data
);
666 if (res
->status
!= NFS3_OK
) {
667 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
));
668 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
669 free_nfs_cb_data(data
);
674 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
675 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
676 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
677 st
.st_mode
|= S_IFDIR
;
679 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
680 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
681 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
683 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
685 st
.st_blksize
= 4096;
686 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
688 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
689 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
690 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
692 data
->cb(0, nfs
, &st
, data
->private_data
);
693 free_nfs_cb_data(data
);
696 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
698 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &data
->fh
, data
) != 0) {
699 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
700 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
701 free_nfs_cb_data(data
);
707 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
709 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
710 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
724 static void nfs_open_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
727 struct nfs_cb_data
*data
= private_data
;
728 struct nfs_context
*nfs
= data
->nfs
;
730 unsigned int nfsmode
= 0;
732 if (status
== RPC_STATUS_ERROR
) {
733 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
734 free_nfs_cb_data(data
);
737 if (status
== RPC_STATUS_CANCEL
) {
738 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
739 free_nfs_cb_data(data
);
744 if (res
->status
!= NFS3_OK
) {
745 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
));
746 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
747 free_nfs_cb_data(data
);
751 if (data
->continue_int
& O_WRONLY
) {
752 nfsmode
|= ACCESS3_MODIFY
;
754 if (data
->continue_int
& O_RDWR
) {
755 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
757 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
758 nfsmode
|= ACCESS3_READ
;
762 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
763 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
764 nfsmode
&ACCESS3_READ
?'r':'-',
765 nfsmode
&ACCESS3_MODIFY
?'w':'-',
766 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
767 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
768 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
769 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
770 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
771 free_nfs_cb_data(data
);
775 nfsfh
= malloc(sizeof(struct nfsfh
));
777 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
778 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
779 free_nfs_cb_data(data
);
782 bzero(nfsfh
, sizeof(struct nfsfh
));
784 if (data
->continue_int
& O_SYNC
) {
788 /* steal the filehandle */
789 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
790 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
791 data
->fh
.data
.data_val
= NULL
;
793 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
794 free_nfs_cb_data(data
);
797 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
801 if (data
->continue_int
& O_WRONLY
) {
802 nfsmode
|= ACCESS3_MODIFY
;
804 if (data
->continue_int
& O_RDWR
) {
805 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
807 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
808 nfsmode
|= ACCESS3_READ
;
811 if (rpc_nfs_access_async(nfs
->rpc
, nfs_open_cb
, &data
->fh
, nfsmode
, data
) != 0) {
812 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
813 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
814 free_nfs_cb_data(data
);
820 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
822 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
823 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
837 static void nfs_pread_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
839 struct nfs_cb_data
*data
= private_data
;
840 struct nfs_context
*nfs
= data
->nfs
;
843 if (status
== RPC_STATUS_ERROR
) {
844 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
845 free_nfs_cb_data(data
);
848 if (status
== RPC_STATUS_CANCEL
) {
849 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
850 free_nfs_cb_data(data
);
855 if (res
->status
!= NFS3_OK
) {
856 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
857 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
858 free_nfs_cb_data(data
);
862 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
863 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
864 free_nfs_cb_data(data
);
867 static void nfs_pread_mcb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
869 struct nfs_mcb_data
*mdata
= private_data
;
870 struct nfs_cb_data
*data
= mdata
->data
;
871 struct nfs_context
*nfs
= data
->nfs
;
876 if (status
== RPC_STATUS_ERROR
) {
877 /* flag the failure but do not invoke callback until we have received all responses */
880 if (status
== RPC_STATUS_CANCEL
) {
881 /* flag the cancellation but do not invoke callback until we have received all responses */
885 /* reassemble the data into the buffer */
886 if (status
== RPC_STATUS_SUCCESS
) {
888 if (res
->status
!= NFS3_OK
) {
889 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
892 if (res
->READ3res_u
.resok
.count
> 0) {
893 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
894 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
895 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
901 if (data
->num_calls
> 0) {
902 /* still waiting for more replies */
907 if (data
->error
!= 0) {
908 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
909 free_nfs_cb_data(data
);
913 if (data
->cancel
!= 0) {
914 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
915 free_nfs_cb_data(data
);
920 data
->nfsfh
->offset
= data
->max_offset
;
921 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
923 free_nfs_cb_data(data
);
927 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, size_t count
, nfs_cb cb
, void *private_data
)
929 struct nfs_cb_data
*data
;
931 data
= malloc(sizeof(struct nfs_cb_data
));
933 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
936 bzero(data
, sizeof(struct nfs_cb_data
));
939 data
->private_data
= private_data
;
942 nfsfh
->offset
= offset
;
944 if (count
<= nfs_get_readmax(nfs
)) {
945 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_cb
, &nfsfh
->fh
, offset
, count
, data
) != 0) {
946 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
947 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
948 free_nfs_cb_data(data
);
954 /* trying to read more than maximum server read size, we has to chop it up into smaller
955 * reads and collect into a reassembly buffer.
956 * we send all reads in parallell so that performance is still good.
958 data
->max_offset
= offset
;
959 data
->start_offset
= offset
;
961 data
->buffer
= malloc(count
);
962 if (data
->buffer
== NULL
) {
963 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
964 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
965 free_nfs_cb_data(data
);
970 size_t readcount
= count
;
971 struct nfs_mcb_data
*mdata
;
973 if (readcount
> nfs_get_readmax(nfs
)) {
974 readcount
= nfs_get_readmax(nfs
);
977 mdata
= malloc(sizeof(struct nfs_mcb_data
));
979 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
982 bzero(mdata
, sizeof(struct nfs_mcb_data
));
984 mdata
->offset
= offset
;
985 mdata
->count
= readcount
;
986 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_mcb
, &nfsfh
->fh
, offset
, readcount
, mdata
) != 0) {
987 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
988 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1004 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, size_t count
, nfs_cb cb
, void *private_data
)
1006 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1014 static void nfs_pwrite_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1016 struct nfs_cb_data
*data
= private_data
;
1017 struct nfs_context
*nfs
= data
->nfs
;
1020 if (status
== RPC_STATUS_ERROR
) {
1021 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1022 free_nfs_cb_data(data
);
1025 if (status
== RPC_STATUS_CANCEL
) {
1026 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1027 free_nfs_cb_data(data
);
1032 if (res
->status
!= NFS3_OK
) {
1033 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1034 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1035 free_nfs_cb_data(data
);
1039 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1040 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1041 free_nfs_cb_data(data
);
1044 int nfs_pwrite_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, size_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1046 struct nfs_cb_data
*data
;
1048 data
= malloc(sizeof(struct nfs_cb_data
));
1050 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1053 bzero(data
, sizeof(struct nfs_cb_data
));
1056 data
->private_data
= private_data
;
1057 data
->nfsfh
= nfsfh
;
1059 nfsfh
->offset
= offset
;
1060 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_cb
, &nfsfh
->fh
, buf
, offset
, count
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, data
) != 0) {
1061 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1062 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1063 free_nfs_cb_data(data
);
1072 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, size_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1074 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1084 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1086 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1087 free(nfsfh
->fh
.data
.data_val
);
1088 nfsfh
->fh
.data
.data_val
= NULL
;
1092 cb(0, nfs
, NULL
, private_data
);
1103 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1105 struct nfs_cb_data
*data
;
1107 data
= malloc(sizeof(struct nfs_cb_data
));
1109 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1112 bzero(data
, sizeof(struct nfs_cb_data
));
1115 data
->private_data
= private_data
;
1117 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &nfsfh
->fh
, data
) != 0) {
1118 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1119 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1120 free_nfs_cb_data(data
);
1131 static void nfs_fsync_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1133 struct nfs_cb_data
*data
= private_data
;
1134 struct nfs_context
*nfs
= data
->nfs
;
1137 if (status
== RPC_STATUS_ERROR
) {
1138 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1139 free_nfs_cb_data(data
);
1142 if (status
== RPC_STATUS_CANCEL
) {
1143 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1144 free_nfs_cb_data(data
);
1149 if (res
->status
!= NFS3_OK
) {
1150 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1151 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1152 free_nfs_cb_data(data
);
1156 data
->cb(0, nfs
, NULL
, data
->private_data
);
1157 free_nfs_cb_data(data
);
1160 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1162 struct nfs_cb_data
*data
;
1164 data
= malloc(sizeof(struct nfs_cb_data
));
1166 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1169 bzero(data
, sizeof(struct nfs_cb_data
));
1172 data
->private_data
= private_data
;
1174 if (rpc_nfs_commit_async(nfs
->rpc
, nfs_fsync_cb
, &nfsfh
->fh
, data
) != 0) {
1175 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1176 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1177 free_nfs_cb_data(data
);
1189 static void nfs_ftruncate_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1191 struct nfs_cb_data
*data
= private_data
;
1192 struct nfs_context
*nfs
= data
->nfs
;
1195 if (status
== RPC_STATUS_ERROR
) {
1196 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1197 free_nfs_cb_data(data
);
1200 if (status
== RPC_STATUS_CANCEL
) {
1201 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1202 free_nfs_cb_data(data
);
1207 if (res
->status
!= NFS3_OK
) {
1208 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1209 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1210 free_nfs_cb_data(data
);
1214 data
->cb(0, nfs
, NULL
, data
->private_data
);
1215 free_nfs_cb_data(data
);
1218 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t length
, nfs_cb cb
, void *private_data
)
1220 struct nfs_cb_data
*data
;
1223 data
= malloc(sizeof(struct nfs_cb_data
));
1225 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1228 bzero(data
, sizeof(struct nfs_cb_data
));
1231 data
->private_data
= private_data
;
1233 bzero(&args
, sizeof(SETATTR3args
));
1234 args
.object
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
1235 args
.object
.data
.data_val
= nfsfh
->fh
.data
.data_val
;
1236 args
.new_attributes
.size
.set_it
= 1;
1237 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1239 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1240 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1241 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1242 free_nfs_cb_data(data
);
1252 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1254 off_t offset
= data
->continue_int
;
1257 nfsfh
.fh
.data
.data_val
= data
->fh
.data
.data_val
;
1258 nfsfh
.fh
.data
.data_len
= data
->fh
.data
.data_len
;
1260 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1261 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1262 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1263 free_nfs_cb_data(data
);
1266 free_nfs_cb_data(data
);
1270 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, off_t length
, nfs_cb cb
, void *private_data
)
1276 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1277 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1290 static void nfs_mkdir_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1293 struct nfs_cb_data
*data
= private_data
;
1294 struct nfs_context
*nfs
= data
->nfs
;
1295 char *str
= data
->continue_data
;
1297 str
= &str
[strlen(str
) + 1];
1299 if (status
== RPC_STATUS_ERROR
) {
1300 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1301 free_nfs_cb_data(data
);
1304 if (status
== RPC_STATUS_CANCEL
) {
1305 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1306 free_nfs_cb_data(data
);
1311 if (res
->status
!= NFS3_OK
) {
1312 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
));
1313 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1314 free_nfs_cb_data(data
);
1318 data
->cb(0, nfs
, NULL
, data
->private_data
);
1319 free_nfs_cb_data(data
);
1322 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1324 char *str
= data
->continue_data
;
1326 str
= &str
[strlen(str
) + 1];
1328 if (rpc_nfs_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &data
->fh
, str
, data
) != 0) {
1329 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1330 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1331 free_nfs_cb_data(data
);
1337 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1342 new_path
= strdup(path
);
1343 if (new_path
== NULL
) {
1344 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1348 ptr
= strrchr(new_path
, '/');
1350 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1355 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1356 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1357 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1371 static void nfs_rmdir_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1374 struct nfs_cb_data
*data
= private_data
;
1375 struct nfs_context
*nfs
= data
->nfs
;
1376 char *str
= data
->continue_data
;
1378 str
= &str
[strlen(str
) + 1];
1380 if (status
== RPC_STATUS_ERROR
) {
1381 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1382 free_nfs_cb_data(data
);
1385 if (status
== RPC_STATUS_CANCEL
) {
1386 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1387 free_nfs_cb_data(data
);
1392 if (res
->status
!= NFS3_OK
) {
1393 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
));
1394 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1395 free_nfs_cb_data(data
);
1399 data
->cb(0, nfs
, NULL
, data
->private_data
);
1400 free_nfs_cb_data(data
);
1403 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1405 char *str
= data
->continue_data
;
1407 str
= &str
[strlen(str
) + 1];
1409 if (rpc_nfs_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &data
->fh
, str
, data
) != 0) {
1410 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1411 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1412 free_nfs_cb_data(data
);
1418 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1423 new_path
= strdup(path
);
1424 if (new_path
== NULL
) {
1425 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1429 ptr
= strrchr(new_path
, '/');
1431 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1436 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1437 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1438 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1451 static void nfs_create_2_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1454 struct nfs_cb_data
*data
= private_data
;
1455 struct nfs_context
*nfs
= data
->nfs
;
1456 struct nfsfh
*nfsfh
;
1457 char *str
= data
->continue_data
;
1459 if (status
== RPC_STATUS_ERROR
) {
1460 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1461 free_nfs_cb_data(data
);
1464 if (status
== RPC_STATUS_CANCEL
) {
1465 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1466 free_nfs_cb_data(data
);
1470 str
= &str
[strlen(str
) + 1];
1472 if (res
->status
!= NFS3_OK
) {
1473 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
));
1474 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1479 nfsfh
= malloc(sizeof(struct nfsfh
));
1480 if (nfsfh
== NULL
) {
1481 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1482 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1483 free_nfs_cb_data(data
);
1486 bzero(nfsfh
, sizeof(struct nfsfh
));
1488 /* steal the filehandle */
1489 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
1490 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
1491 data
->fh
.data
.data_val
= NULL
;
1493 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1494 free_nfs_cb_data(data
);
1499 static void nfs_creat_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1502 struct nfs_cb_data
*data
= private_data
;
1503 struct nfs_context
*nfs
= data
->nfs
;
1504 char *str
= data
->continue_data
;
1506 if (status
== RPC_STATUS_ERROR
) {
1507 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1508 free_nfs_cb_data(data
);
1511 if (status
== RPC_STATUS_CANCEL
) {
1512 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1513 free_nfs_cb_data(data
);
1517 str
= &str
[strlen(str
) + 1];
1519 if (res
->status
!= NFS3_OK
) {
1520 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
));
1521 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1526 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &data
->fh
, str
, data
) != 0) {
1527 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
1528 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1529 free_nfs_cb_data(data
);
1535 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1537 char *str
= data
->continue_data
;
1539 str
= &str
[strlen(str
) + 1];
1541 if (rpc_nfs_create_async(nfs
->rpc
, nfs_creat_1_cb
, &data
->fh
, str
, data
->continue_int
, data
) != 0) {
1542 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
1543 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1544 free_nfs_cb_data(data
);
1550 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1555 new_path
= strdup(path
);
1556 if (new_path
== NULL
) {
1557 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1561 ptr
= strrchr(new_path
, '/');
1563 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1568 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1569 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
1570 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1583 static void nfs_unlink_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1586 struct nfs_cb_data
*data
= private_data
;
1587 struct nfs_context
*nfs
= data
->nfs
;
1588 char *str
= data
->continue_data
;
1590 str
= &str
[strlen(str
) + 1];
1592 if (status
== RPC_STATUS_ERROR
) {
1593 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1594 free_nfs_cb_data(data
);
1597 if (status
== RPC_STATUS_CANCEL
) {
1598 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1599 free_nfs_cb_data(data
);
1604 if (res
->status
!= NFS3_OK
) {
1605 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
));
1606 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1607 free_nfs_cb_data(data
);
1611 data
->cb(0, nfs
, NULL
, data
->private_data
);
1612 free_nfs_cb_data(data
);
1615 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1617 char *str
= data
->continue_data
;
1619 str
= &str
[strlen(str
) + 1];
1621 if (rpc_nfs_remove_async(nfs
->rpc
, nfs_unlink_cb
, &data
->fh
, str
, data
) != 0) {
1622 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
1623 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1624 free_nfs_cb_data(data
);
1630 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1635 new_path
= strdup(path
);
1636 if (new_path
== NULL
) {
1637 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1641 ptr
= strrchr(new_path
, '/');
1643 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1648 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1649 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
1650 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1664 static void nfs_opendir_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1667 struct nfs_cb_data
*data
= private_data
;
1668 struct nfs_context
*nfs
= data
->nfs
;
1669 struct nfsdir
*nfsdir
= data
->continue_data
;
1670 struct entry3
*entry
;
1673 if (status
== RPC_STATUS_ERROR
) {
1674 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1675 nfs_free_nfsdir(nfsdir
);
1676 data
->continue_data
= NULL
;
1677 free_nfs_cb_data(data
);
1680 if (status
== RPC_STATUS_CANCEL
) {
1681 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1682 nfs_free_nfsdir(nfsdir
);
1683 data
->continue_data
= NULL
;
1684 free_nfs_cb_data(data
);
1689 if (res
->status
!= NFS3_OK
) {
1690 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
));
1691 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1692 nfs_free_nfsdir(nfsdir
);
1693 data
->continue_data
= NULL
;
1694 free_nfs_cb_data(data
);
1698 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
1699 while (entry
!= NULL
) {
1700 struct nfsdirent
*nfsdirent
;
1702 nfsdirent
= malloc(sizeof(struct nfsdirent
));
1703 if (nfsdirent
== NULL
) {
1704 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
1705 nfs_free_nfsdir(nfsdir
);
1706 data
->continue_data
= NULL
;
1707 free_nfs_cb_data(data
);
1710 bzero(nfsdirent
, sizeof(struct nfsdirent
));
1711 nfsdirent
->name
= strdup(entry
->name
);
1712 if (nfsdirent
->name
== NULL
) {
1713 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
1714 nfs_free_nfsdir(nfsdir
);
1715 data
->continue_data
= NULL
;
1716 free_nfs_cb_data(data
);
1719 nfsdirent
->inode
= entry
->fileid
;
1720 nfsdirent
->next
= nfsdir
->entries
;
1721 nfsdir
->entries
= nfsdirent
;
1723 cookie
= entry
->cookie
;
1724 entry
= entry
->nextentry
;
1727 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
1728 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, cookie
, res
->READDIR3res_u
.resok
.cookieverf
, 20000, data
) != 0) {
1729 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
1730 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1731 nfs_free_nfsdir(nfsdir
);
1732 data
->continue_data
= NULL
;
1733 free_nfs_cb_data(data
);
1739 /* steal the dirhandle */
1740 data
->continue_data
= NULL
;
1741 nfsdir
->current
= nfsdir
->entries
;
1743 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
1744 free_nfs_cb_data(data
);
1747 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1751 bzero(cv
, sizeof(cookieverf3
));
1752 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, 0, (char *)&cv
, 20000, data
) != 0) {
1753 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
1754 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1755 free_nfs_cb_data(data
);
1761 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1763 struct nfsdir
*nfsdir
;
1765 nfsdir
= malloc(sizeof(struct nfsdir
));
1766 if (nfsdir
== NULL
) {
1767 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
1770 bzero(nfsdir
, sizeof(struct nfsdir
));
1772 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
1773 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1781 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
1783 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
1785 if (nfsdir
->current
!= NULL
) {
1786 nfsdir
->current
= nfsdir
->current
->next
;
1792 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
1794 nfs_free_nfsdir(nfsdir
);
1806 struct lseek_cb_data
{
1807 struct nfs_context
*nfs
;
1808 struct nfsfh
*nfsfh
;
1814 static void nfs_lseek_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1817 struct lseek_cb_data
*data
= private_data
;
1818 struct nfs_context
*nfs
= data
->nfs
;
1820 if (status
== RPC_STATUS_ERROR
) {
1821 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1825 if (status
== RPC_STATUS_CANCEL
) {
1826 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1832 if (res
->status
!= NFS3_OK
) {
1833 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1834 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1839 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1840 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
1844 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, int whence
, nfs_cb cb
, void *private_data
)
1846 struct lseek_cb_data
*data
;
1848 if (whence
== SEEK_SET
) {
1849 nfsfh
->offset
= offset
;
1850 cb(0, nfs
, &nfsfh
->offset
, private_data
);
1853 if (whence
== SEEK_CUR
) {
1854 nfsfh
->offset
+= offset
;
1855 cb(0, nfs
, &nfsfh
->offset
, private_data
);
1859 data
= malloc(sizeof(struct lseek_cb_data
));
1861 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
1866 data
->nfsfh
= nfsfh
;
1867 data
->offset
= offset
;
1869 data
->private_data
= private_data
;
1871 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &nfsfh
->fh
, data
) != 0) {
1872 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
1885 static void nfs_statvfs_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1888 struct nfs_cb_data
*data
= private_data
;
1889 struct nfs_context
*nfs
= data
->nfs
;
1890 struct statvfs svfs
;
1892 if (status
== RPC_STATUS_ERROR
) {
1893 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1894 free_nfs_cb_data(data
);
1897 if (status
== RPC_STATUS_CANCEL
) {
1898 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1899 free_nfs_cb_data(data
);
1904 if (res
->status
!= NFS3_OK
) {
1905 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
));
1906 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1907 free_nfs_cb_data(data
);
1911 svfs
.f_bsize
= 4096;
1912 svfs
.f_frsize
= 4096;
1913 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
1914 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
1915 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
1916 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
1917 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
1918 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
1921 svfs
.f_namemax
= 256;
1923 data
->cb(0, nfs
, &svfs
, data
->private_data
);
1924 free_nfs_cb_data(data
);
1927 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1929 if (rpc_nfs_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &data
->fh
, data
) != 0) {
1930 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
1931 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1932 free_nfs_cb_data(data
);
1938 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1940 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
1941 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1954 static void nfs_readlink_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1957 struct nfs_cb_data
*data
= private_data
;
1958 struct nfs_context
*nfs
= data
->nfs
;
1960 if (status
== RPC_STATUS_ERROR
) {
1961 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1962 free_nfs_cb_data(data
);
1965 if (status
== RPC_STATUS_CANCEL
) {
1966 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1967 free_nfs_cb_data(data
);
1972 if (res
->status
!= NFS3_OK
) {
1973 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
));
1974 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1975 free_nfs_cb_data(data
);
1980 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
1981 free_nfs_cb_data(data
);
1984 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1986 if (rpc_nfs_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &data
->fh
, data
) != 0) {
1987 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
1988 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1989 free_nfs_cb_data(data
);
1995 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1997 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
1998 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2011 static void nfs_chmod_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2013 struct nfs_cb_data
*data
= private_data
;
2014 struct nfs_context
*nfs
= data
->nfs
;
2017 if (status
== RPC_STATUS_ERROR
) {
2018 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2019 free_nfs_cb_data(data
);
2022 if (status
== RPC_STATUS_CANCEL
) {
2023 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2024 free_nfs_cb_data(data
);
2029 if (res
->status
!= NFS3_OK
) {
2030 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2031 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2032 free_nfs_cb_data(data
);
2036 data
->cb(0, nfs
, NULL
, data
->private_data
);
2037 free_nfs_cb_data(data
);
2040 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2044 bzero(&args
, sizeof(SETATTR3args
));
2045 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2046 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2047 args
.new_attributes
.mode
.set_it
= 1;
2048 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2050 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
2051 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2052 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2053 free_nfs_cb_data(data
);
2060 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2062 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
2063 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2073 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
2075 struct nfs_cb_data
*data
;
2077 data
= malloc(sizeof(struct nfs_cb_data
));
2079 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
2082 bzero(data
, sizeof(struct nfs_cb_data
));
2085 data
->private_data
= private_data
;
2086 data
->continue_int
= mode
;
2087 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2088 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2089 if (data
->fh
.data
.data_val
== NULL
) {
2090 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2091 free_nfs_cb_data(data
);
2094 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2096 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
2097 free_nfs_cb_data(data
);
2109 static void nfs_chown_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2111 struct nfs_cb_data
*data
= private_data
;
2112 struct nfs_context
*nfs
= data
->nfs
;
2115 if (status
== RPC_STATUS_ERROR
) {
2116 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2117 free_nfs_cb_data(data
);
2120 if (status
== RPC_STATUS_CANCEL
) {
2121 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2122 free_nfs_cb_data(data
);
2127 if (res
->status
!= NFS3_OK
) {
2128 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2129 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2130 free_nfs_cb_data(data
);
2134 data
->cb(0, nfs
, NULL
, data
->private_data
);
2135 free_nfs_cb_data(data
);
2138 struct nfs_chown_data
{
2143 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2146 struct nfs_chown_data
*chown_data
= data
->continue_data
;
2148 bzero(&args
, sizeof(SETATTR3args
));
2149 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2150 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2151 if (chown_data
->uid
!= (uid_t
)-1) {
2152 args
.new_attributes
.uid
.set_it
= 1;
2153 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
2155 if (chown_data
->gid
!= (gid_t
)-1) {
2156 args
.new_attributes
.gid
.set_it
= 1;
2157 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
2160 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
2161 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2162 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2163 free_nfs_cb_data(data
);
2170 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2172 struct nfs_chown_data
*chown_data
;
2174 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2175 if (chown_data
== NULL
) {
2176 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2180 chown_data
->uid
= uid
;
2181 chown_data
->gid
= gid
;
2183 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
2184 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2195 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2197 struct nfs_cb_data
*data
;
2198 struct nfs_chown_data
*chown_data
;
2200 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2201 if (chown_data
== NULL
) {
2202 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2206 chown_data
->uid
= uid
;
2207 chown_data
->gid
= gid
;
2210 data
= malloc(sizeof(struct nfs_cb_data
));
2212 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
2215 bzero(data
, sizeof(struct nfs_cb_data
));
2218 data
->private_data
= private_data
;
2219 data
->continue_data
= chown_data
;
2220 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2221 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2222 if (data
->fh
.data
.data_val
== NULL
) {
2223 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2224 free_nfs_cb_data(data
);
2227 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2230 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
2231 free_nfs_cb_data(data
);
2245 static void nfs_utimes_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2247 struct nfs_cb_data
*data
= private_data
;
2248 struct nfs_context
*nfs
= data
->nfs
;
2251 if (status
== RPC_STATUS_ERROR
) {
2252 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2253 free_nfs_cb_data(data
);
2256 if (status
== RPC_STATUS_CANCEL
) {
2257 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2258 free_nfs_cb_data(data
);
2263 if (res
->status
!= NFS3_OK
) {
2264 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2265 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2266 free_nfs_cb_data(data
);
2270 data
->cb(0, nfs
, NULL
, data
->private_data
);
2271 free_nfs_cb_data(data
);
2274 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2277 struct timeval
*utimes_data
= data
->continue_data
;
2279 bzero(&args
, sizeof(SETATTR3args
));
2280 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2281 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2282 if (utimes_data
!= NULL
) {
2283 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
2284 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
2285 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
2286 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
2287 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
2288 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
2290 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
2291 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
2294 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
2295 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2296 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2297 free_nfs_cb_data(data
);
2304 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
2306 struct timeval
*new_times
= NULL
;
2308 if (times
!= NULL
) {
2309 new_times
= malloc(sizeof(struct timeval
)*2);
2310 if (new_times
== NULL
) {
2311 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2315 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
2318 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2319 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2329 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
2331 struct timeval
*new_times
= NULL
;
2333 if (times
!= NULL
) {
2334 new_times
= malloc(sizeof(struct timeval
)*2);
2335 if (new_times
== NULL
) {
2336 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2340 new_times
[0].tv_sec
= times
->actime
;
2341 new_times
[0].tv_usec
= 0;
2342 new_times
[1].tv_sec
= times
->modtime
;
2343 new_times
[1].tv_usec
= 0;
2346 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2347 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2361 static void nfs_access_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2364 struct nfs_cb_data
*data
= private_data
;
2365 struct nfs_context
*nfs
= data
->nfs
;
2366 unsigned int nfsmode
= 0;
2368 if (status
== RPC_STATUS_ERROR
) {
2369 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2370 free_nfs_cb_data(data
);
2373 if (status
== RPC_STATUS_CANCEL
) {
2374 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2375 free_nfs_cb_data(data
);
2380 if (res
->status
!= NFS3_OK
) {
2381 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
));
2382 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2383 free_nfs_cb_data(data
);
2387 if (data
->continue_int
& R_OK
) {
2388 nfsmode
|= ACCESS3_READ
;
2390 if (data
->continue_int
& W_OK
) {
2391 nfsmode
|= ACCESS3_MODIFY
;
2393 if (data
->continue_int
& X_OK
) {
2394 nfsmode
|= ACCESS3_EXECUTE
;
2397 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
2398 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2399 nfsmode
&ACCESS3_READ
?'r':'-',
2400 nfsmode
&ACCESS3_MODIFY
?'w':'-',
2401 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
2402 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
2403 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
2404 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
2405 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2406 free_nfs_cb_data(data
);
2410 data
->cb(0, nfs
, NULL
, data
->private_data
);
2411 free_nfs_cb_data(data
);
2414 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2418 if (data
->continue_int
& R_OK
) {
2419 nfsmode
|= ACCESS3_READ
;
2421 if (data
->continue_int
& W_OK
) {
2422 nfsmode
|= ACCESS3_MODIFY
;
2424 if (data
->continue_int
& X_OK
) {
2425 nfsmode
|= ACCESS3_EXECUTE
;
2428 if (rpc_nfs_access_async(nfs
->rpc
, nfs_access_cb
, &data
->fh
, nfsmode
, data
) != 0) {
2429 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
2430 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2431 free_nfs_cb_data(data
);
2437 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2439 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
2440 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2452 struct nfs_symlink_data
{
2454 char *newpathparent
;
2455 char *newpathobject
;
2458 static void free_nfs_symlink_data(void *mem
)
2460 struct nfs_symlink_data
*data
= mem
;
2462 if (data
->oldpath
!= NULL
) {
2463 free(data
->oldpath
);
2465 if (data
->newpathparent
!= NULL
) {
2466 free(data
->newpathparent
);
2468 if (data
->newpathobject
!= NULL
) {
2469 free(data
->newpathobject
);
2474 static void nfs_symlink_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2477 struct nfs_cb_data
*data
= private_data
;
2478 struct nfs_context
*nfs
= data
->nfs
;
2479 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
2481 if (status
== RPC_STATUS_ERROR
) {
2482 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2483 free_nfs_cb_data(data
);
2486 if (status
== RPC_STATUS_CANCEL
) {
2487 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2488 free_nfs_cb_data(data
);
2493 if (res
->status
!= NFS3_OK
) {
2494 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
));
2495 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2496 free_nfs_cb_data(data
);
2500 data
->cb(0, nfs
, NULL
, data
->private_data
);
2501 free_nfs_cb_data(data
);
2504 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2506 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
2508 if (rpc_nfs_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &data
->fh
, symlink_data
->newpathobject
, symlink_data
->oldpath
, data
) != 0) {
2509 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
2510 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2511 free_nfs_cb_data(data
);
2517 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
2520 struct nfs_symlink_data
*symlink_data
;
2522 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
2523 if (symlink_data
== NULL
) {
2524 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
2527 bzero(symlink_data
, sizeof(struct nfs_symlink_data
));
2529 symlink_data
->oldpath
= strdup(oldpath
);
2530 if (symlink_data
->oldpath
== NULL
) {
2531 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
2532 free_nfs_symlink_data(symlink_data
);
2536 symlink_data
->newpathparent
= strdup(newpath
);
2537 if (symlink_data
->newpathparent
== NULL
) {
2538 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
2539 free_nfs_symlink_data(symlink_data
);
2543 ptr
= strrchr(symlink_data
->newpathparent
, '/');
2545 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
2546 free_nfs_symlink_data(symlink_data
);
2552 symlink_data
->newpathobject
= strdup(ptr
);
2553 if (symlink_data
->newpathobject
== NULL
) {
2554 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
2555 free_nfs_symlink_data(symlink_data
);
2559 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
2560 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2572 struct nfs_rename_data
{
2575 struct nfs_fh3 olddir
;
2578 struct nfs_fh3 newdir
;
2581 static void free_nfs_rename_data(void *mem
)
2583 struct nfs_rename_data
*data
= mem
;
2585 if (data
->oldpath
!= NULL
) {
2586 free(data
->oldpath
);
2588 if (data
->olddir
.data
.data_val
!= NULL
) {
2589 free(data
->olddir
.data
.data_val
);
2591 if (data
->newpath
!= NULL
) {
2592 free(data
->newpath
);
2594 if (data
->newdir
.data
.data_val
!= NULL
) {
2595 free(data
->newdir
.data
.data_val
);
2600 static void nfs_rename_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2603 struct nfs_cb_data
*data
= private_data
;
2604 struct nfs_context
*nfs
= data
->nfs
;
2605 struct nfs_rename_data
*rename_data
= data
->continue_data
;
2607 if (status
== RPC_STATUS_ERROR
) {
2608 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2609 free_nfs_cb_data(data
);
2612 if (status
== RPC_STATUS_CANCEL
) {
2613 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2614 free_nfs_cb_data(data
);
2619 if (res
->status
!= NFS3_OK
) {
2620 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
));
2621 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2622 free_nfs_cb_data(data
);
2626 data
->cb(0, nfs
, NULL
, data
->private_data
);
2627 free_nfs_cb_data(data
);
2630 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2632 struct nfs_rename_data
*rename_data
= data
->continue_data
;
2634 /* steal the filehandle */
2635 rename_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
2636 rename_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
2637 data
->fh
.data
.data_val
= NULL
;
2639 if (rpc_nfs_rename_async(nfs
->rpc
, nfs_rename_cb
, &rename_data
->olddir
, rename_data
->oldobject
, &rename_data
->newdir
, rename_data
->newobject
, data
) != 0) {
2640 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
2641 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2642 free_nfs_cb_data(data
);
2649 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2651 struct nfs_rename_data
*rename_data
= data
->continue_data
;
2653 /* steal the filehandle */
2654 rename_data
->olddir
.data
.data_len
= data
->fh
.data
.data_len
;
2655 rename_data
->olddir
.data
.data_val
= data
->fh
.data
.data_val
;
2656 data
->fh
.data
.data_val
= NULL
;
2658 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) {
2659 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
2660 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2661 free_nfs_cb_data(data
);
2664 data
->continue_data
= NULL
;
2665 free_nfs_cb_data(data
);
2671 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
2674 struct nfs_rename_data
*rename_data
;
2676 rename_data
= malloc(sizeof(struct nfs_rename_data
));
2677 if (rename_data
== NULL
) {
2678 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
2681 bzero(rename_data
, sizeof(struct nfs_rename_data
));
2683 rename_data
->oldpath
= strdup(oldpath
);
2684 if (rename_data
->oldpath
== NULL
) {
2685 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
2686 free_nfs_rename_data(rename_data
);
2689 ptr
= strrchr(rename_data
->oldpath
, '/');
2691 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
2692 free_nfs_rename_data(rename_data
);
2697 rename_data
->oldobject
= ptr
;
2700 rename_data
->newpath
= strdup(newpath
);
2701 if (rename_data
->newpath
== NULL
) {
2702 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
2703 free_nfs_rename_data(rename_data
);
2706 ptr
= strrchr(rename_data
->newpath
, '/');
2708 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
2709 free_nfs_rename_data(rename_data
);
2714 rename_data
->newobject
= ptr
;
2717 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
2718 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2729 struct nfs_link_data
{
2731 struct nfs_fh3 oldfh
;
2734 struct nfs_fh3 newdir
;
2737 static void free_nfs_link_data(void *mem
)
2739 struct nfs_link_data
*data
= mem
;
2741 if (data
->oldpath
!= NULL
) {
2742 free(data
->oldpath
);
2744 if (data
->oldfh
.data
.data_val
!= NULL
) {
2745 free(data
->oldfh
.data
.data_val
);
2747 if (data
->newpath
!= NULL
) {
2748 free(data
->newpath
);
2750 if (data
->newdir
.data
.data_val
!= NULL
) {
2751 free(data
->newdir
.data
.data_val
);
2756 static void nfs_link_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2759 struct nfs_cb_data
*data
= private_data
;
2760 struct nfs_context
*nfs
= data
->nfs
;
2761 struct nfs_link_data
*link_data
= data
->continue_data
;
2763 if (status
== RPC_STATUS_ERROR
) {
2764 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2765 free_nfs_cb_data(data
);
2768 if (status
== RPC_STATUS_CANCEL
) {
2769 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2770 free_nfs_cb_data(data
);
2775 if (res
->status
!= NFS3_OK
) {
2776 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
));
2777 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2778 free_nfs_cb_data(data
);
2782 data
->cb(0, nfs
, NULL
, data
->private_data
);
2783 free_nfs_cb_data(data
);
2786 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2788 struct nfs_link_data
*link_data
= data
->continue_data
;
2790 /* steal the filehandle */
2791 link_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
2792 link_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
2793 data
->fh
.data
.data_val
= NULL
;
2795 if (rpc_nfs_link_async(nfs
->rpc
, nfs_link_cb
, &link_data
->oldfh
, &link_data
->newdir
, link_data
->newobject
, data
) != 0) {
2796 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
2797 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2798 free_nfs_cb_data(data
);
2805 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2807 struct nfs_link_data
*link_data
= data
->continue_data
;
2809 /* steal the filehandle */
2810 link_data
->oldfh
.data
.data_len
= data
->fh
.data
.data_len
;
2811 link_data
->oldfh
.data
.data_val
= data
->fh
.data
.data_val
;
2812 data
->fh
.data
.data_val
= NULL
;
2814 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) {
2815 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
2816 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2817 free_nfs_cb_data(data
);
2820 data
->continue_data
= NULL
;
2821 free_nfs_cb_data(data
);
2827 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
2830 struct nfs_link_data
*link_data
;
2832 link_data
= malloc(sizeof(struct nfs_link_data
));
2833 if (link_data
== NULL
) {
2834 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
2837 bzero(link_data
, sizeof(struct nfs_link_data
));
2839 link_data
->oldpath
= strdup(oldpath
);
2840 if (link_data
->oldpath
== NULL
) {
2841 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
2842 free_nfs_link_data(link_data
);
2846 link_data
->newpath
= strdup(newpath
);
2847 if (link_data
->newpath
== NULL
) {
2848 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
2849 free_nfs_link_data(link_data
);
2852 ptr
= strrchr(link_data
->newpath
, '/');
2854 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
2855 free_nfs_link_data(link_data
);
2860 link_data
->newobject
= ptr
;
2863 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
2864 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2872 //qqq replace later with lseek()
2873 off_t
nfs_get_current_offset(struct nfsfh
*nfsfh
)
2875 return nfsfh
->offset
;
2881 * Get the maximum supported READ3 size by the server
2883 size_t nfs_get_readmax(struct nfs_context
*nfs
)
2885 return nfs
->readmax
;
2889 * Get the maximum supported WRITE3 size by the server
2891 size_t nfs_get_writemax(struct nfs_context
*nfs
)
2893 return nfs
->writemax
;
2896 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
2901 va_start(ap
, error_string
);
2904 vsnprintf(str
, 1024, error_string
, ap
);
2906 vasprintf(&str
, error_string
, ap
);
2908 if (nfs
->rpc
->error_string
!= NULL
) {
2909 free(nfs
->rpc
->error_string
);
2911 fs
->rpc
->error_string
= str
;
2917 struct mount_cb_data
{
2923 static void free_mount_cb_data(struct mount_cb_data
*data
)
2925 if (data
->server
!= NULL
) {
2927 data
->server
= NULL
;
2933 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2935 struct mount_cb_data
*data
= private_data
;
2937 if (status
== RPC_STATUS_ERROR
) {
2938 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
2939 free_mount_cb_data(data
);
2942 if (status
== RPC_STATUS_CANCEL
) {
2943 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
2944 free_mount_cb_data(data
);
2948 data
->cb(rpc
, 0, command_data
, data
->private_data
);
2949 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
2950 rpc_set_error(rpc
, "Failed to disconnect\n");
2952 free_mount_cb_data(data
);
2955 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2957 struct mount_cb_data
*data
= private_data
;
2959 if (status
== RPC_STATUS_ERROR
) {
2960 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
2961 free_mount_cb_data(data
);
2964 if (status
== RPC_STATUS_CANCEL
) {
2965 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
2966 free_mount_cb_data(data
);
2970 if (rpc_mount_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
2971 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
2972 free_mount_cb_data(data
);
2977 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2979 struct mount_cb_data
*data
= private_data
;
2980 uint32_t mount_port
;
2982 if (status
== RPC_STATUS_ERROR
) {
2983 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
2984 free_mount_cb_data(data
);
2987 if (status
== RPC_STATUS_CANCEL
) {
2988 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
2989 free_mount_cb_data(data
);
2993 mount_port
= *(uint32_t *)command_data
;
2994 if (mount_port
== 0) {
2995 rpc_set_error(rpc
, "RPC error. Mount program is not available");
2996 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
2997 free_mount_cb_data(data
);
3001 rpc_disconnect(rpc
, "normal disconnect");
3002 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
3003 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3004 free_mount_cb_data(data
);
3009 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3011 struct mount_cb_data
*data
= private_data
;
3013 if (status
== RPC_STATUS_ERROR
) {
3014 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3015 free_mount_cb_data(data
);
3018 if (status
== RPC_STATUS_CANCEL
) {
3019 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3020 free_mount_cb_data(data
);
3024 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, mount_export_3_cb
, private_data
) != 0) {
3025 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3026 free_mount_cb_data(data
);
3031 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3033 struct mount_cb_data
*data
= private_data
;
3035 if (status
== RPC_STATUS_ERROR
) {
3036 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3037 free_mount_cb_data(data
);
3040 if (status
== RPC_STATUS_CANCEL
) {
3041 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3042 free_mount_cb_data(data
);
3046 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
3047 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3048 free_mount_cb_data(data
);
3053 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
3055 struct mount_cb_data
*data
;
3057 data
= malloc(sizeof(struct mount_cb_data
));
3061 bzero(data
, sizeof(struct mount_cb_data
));
3063 data
->private_data
= private_data
;
3064 data
->server
= strdup(server
);
3065 if (data
->server
== NULL
) {
3066 free_mount_cb_data(data
);
3069 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
3070 free_mount_cb_data(data
);
3077 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
3082 const char *nfs_get_server(struct nfs_context
*nfs
) {
3086 const char *nfs_get_export(struct nfs_context
*nfs
) {
3092 int poll(struct pollfd
*fds
, int nfsd
, int timeout
)
3094 fd_set rfds
, wfds
, efds
;
3100 if (fds
->events
& POLLIN
) {
3101 FD_SET(fds
->fd
, &rfds
);
3103 if (fds
->events
& POLLOUT
) {
3104 FD_SET(fds
->fd
, &wfds
);
3106 FD_SET(fds
->fd
, &efds
);
3107 select(fds
->fd
+ 1, &rfds
, &wfds
, &efds
, NULL
);
3109 if (FD_ISSET(fds
->fd
, &rfds
)) {
3110 fds
->revents
|= POLLIN
;
3112 if (FD_ISSET(fds
->fd
, &wfds
)) {
3113 fds
->revents
|= POLLOUT
;
3115 if (FD_ISSET(fds
->fd
, &efds
)) {
3116 fds
->revents
|= POLLHUP
;