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"
36 #define statvfs statfs
49 #ifdef HAVE_SYS_STATVFS_H
50 #include <sys/statvfs.h>
59 #include <sys/types.h>
62 #include <netinet/in.h>
63 #include "libnfs-zdr.h"
65 #include "libnfs-raw.h"
66 #include "libnfs-raw-mount.h"
67 #include "libnfs-raw-nfs.h"
68 #include "libnfs-private.h"
71 struct nfsdirent
*entries
;
72 struct nfsdirent
*current
;
82 struct rpc_context
*rpc
;
85 struct nfs_fh3 rootfh
;
90 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
92 while (nfsdir
->entries
) {
93 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
94 if (nfsdir
->entries
->name
!= NULL
) {
95 free(nfsdir
->entries
->name
);
97 free(nfsdir
->entries
);
98 nfsdir
->entries
= dirent
;
104 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
107 struct nfs_context
*nfs
;
109 char *saved_path
, *path
;
114 continue_func continue_cb
;
116 void (*free_continue_data
)(void *);
121 /* for multi-read/write calls. */
125 uint64_t start_offset
, max_offset
;
129 struct nfs_mcb_data
{
130 struct nfs_cb_data
*data
;
135 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
138 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
140 rpc_set_auth(nfs
->rpc
, auth
);
143 int nfs_get_fd(struct nfs_context
*nfs
)
145 return rpc_get_fd(nfs
->rpc
);
148 int nfs_queue_length(struct nfs_context
*nfs
)
150 return rpc_queue_length(nfs
->rpc
);
153 int nfs_which_events(struct nfs_context
*nfs
)
155 return rpc_which_events(nfs
->rpc
);
158 int nfs_service(struct nfs_context
*nfs
, int revents
)
160 return rpc_service(nfs
->rpc
, revents
);
163 char *nfs_get_error(struct nfs_context
*nfs
)
165 return rpc_get_error(nfs
->rpc
);
168 struct nfs_context
*nfs_init_context(void)
170 struct nfs_context
*nfs
;
172 nfs
= malloc(sizeof(struct nfs_context
));
176 nfs
->rpc
= rpc_init_context();
177 if (nfs
->rpc
== NULL
) {
185 nfs
->rootfh
.data
.data_len
= 0;
186 nfs
->rootfh
.data
.data_val
= NULL
;
191 void nfs_destroy_context(struct nfs_context
*nfs
)
193 rpc_destroy_context(nfs
->rpc
);
206 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
207 free(nfs
->rootfh
.data
.data_val
);
208 nfs
->rootfh
.data
.data_val
= NULL
;
214 void free_nfs_cb_data(struct nfs_cb_data
*data
)
216 if (data
->saved_path
!= NULL
) {
217 free(data
->saved_path
);
218 data
->saved_path
= NULL
;
221 if (data
->continue_data
!= NULL
) {
222 data
->free_continue_data(data
->continue_data
);
223 data
->continue_data
= NULL
;
226 if (data
->fh
.data
.data_val
!= NULL
) {
227 free(data
->fh
.data
.data_val
);
228 data
->fh
.data
.data_val
= NULL
;
231 if (data
->buffer
!= NULL
) {
243 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
245 struct nfs_cb_data
*data
= private_data
;
246 struct nfs_context
*nfs
= data
->nfs
;
248 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
250 if (status
== RPC_STATUS_ERROR
) {
251 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
252 free_nfs_cb_data(data
);
255 if (status
== RPC_STATUS_CANCEL
) {
256 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
257 free_nfs_cb_data(data
);
261 data
->cb(0, nfs
, NULL
, data
->private_data
);
262 free_nfs_cb_data(data
);
265 static void nfs_mount_9_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
;
269 FSINFO3res
*res
= command_data
;
271 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
273 if (status
== RPC_STATUS_ERROR
) {
274 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
275 free_nfs_cb_data(data
);
278 if (status
== RPC_STATUS_CANCEL
) {
279 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
280 free_nfs_cb_data(data
);
284 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
285 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
287 if (rpc_nfs_getattr_async(rpc
, nfs_mount_10_cb
, &nfs
->rootfh
, data
) != 0) {
288 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
289 free_nfs_cb_data(data
);
294 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
296 struct nfs_cb_data
*data
= private_data
;
297 struct nfs_context
*nfs
= data
->nfs
;
299 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
301 if (status
== RPC_STATUS_ERROR
) {
302 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
303 free_nfs_cb_data(data
);
306 if (status
== RPC_STATUS_CANCEL
) {
307 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
308 free_nfs_cb_data(data
);
312 if (rpc_nfs_fsinfo_async(rpc
, nfs_mount_9_cb
, &nfs
->rootfh
, data
) != 0) {
313 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
314 free_nfs_cb_data(data
);
320 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
322 struct nfs_cb_data
*data
= private_data
;
323 struct nfs_context
*nfs
= data
->nfs
;
325 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
327 /* Dont want any more callbacks even if the socket is closed */
328 rpc
->connect_cb
= NULL
;
330 if (status
== RPC_STATUS_ERROR
) {
331 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
332 free_nfs_cb_data(data
);
335 if (status
== RPC_STATUS_CANCEL
) {
336 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
337 free_nfs_cb_data(data
);
341 if (rpc_nfs_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
342 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
343 free_nfs_cb_data(data
);
349 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
351 struct nfs_cb_data
*data
= private_data
;
352 struct nfs_context
*nfs
= data
->nfs
;
355 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
357 if (status
== RPC_STATUS_ERROR
) {
358 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
359 free_nfs_cb_data(data
);
362 if (status
== RPC_STATUS_CANCEL
) {
363 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
364 free_nfs_cb_data(data
);
369 if (res
->fhs_status
!= MNT3_OK
) {
370 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
));
371 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
372 free_nfs_cb_data(data
);
376 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
377 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
378 if (nfs
->rootfh
.data
.data_val
== NULL
) {
379 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
380 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
381 free_nfs_cb_data(data
);
384 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
386 rpc_disconnect(rpc
, "normal disconnect");
387 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
388 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
389 free_nfs_cb_data(data
);
392 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
393 rpc_set_autoreconnect(rpc
);
397 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
399 struct nfs_cb_data
*data
= private_data
;
400 struct nfs_context
*nfs
= data
->nfs
;
402 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
404 if (status
== RPC_STATUS_ERROR
) {
405 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
406 free_nfs_cb_data(data
);
409 if (status
== RPC_STATUS_CANCEL
) {
410 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
411 free_nfs_cb_data(data
);
415 if (rpc_mount_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
416 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
417 free_nfs_cb_data(data
);
422 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
424 struct nfs_cb_data
*data
= private_data
;
425 struct nfs_context
*nfs
= data
->nfs
;
427 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
429 /* Dont want any more callbacks even if the socket is closed */
430 rpc
->connect_cb
= NULL
;
432 if (status
== RPC_STATUS_ERROR
) {
433 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
434 free_nfs_cb_data(data
);
437 if (status
== RPC_STATUS_CANCEL
) {
438 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
439 free_nfs_cb_data(data
);
443 if (rpc_mount_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
444 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
445 free_nfs_cb_data(data
);
450 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
452 struct nfs_cb_data
*data
= private_data
;
453 struct nfs_context
*nfs
= data
->nfs
;
456 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
458 if (status
== RPC_STATUS_ERROR
) {
459 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
460 free_nfs_cb_data(data
);
463 if (status
== RPC_STATUS_CANCEL
) {
464 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
465 free_nfs_cb_data(data
);
469 mount_port
= *(uint32_t *)command_data
;
470 if (mount_port
== 0) {
471 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
472 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
473 free_nfs_cb_data(data
);
477 rpc_disconnect(rpc
, "normal disconnect");
478 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
479 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
480 free_nfs_cb_data(data
);
486 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
488 struct nfs_cb_data
*data
= private_data
;
489 struct nfs_context
*nfs
= data
->nfs
;
491 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
493 if (status
== RPC_STATUS_ERROR
) {
494 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
495 free_nfs_cb_data(data
);
498 if (status
== RPC_STATUS_CANCEL
) {
499 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
500 free_nfs_cb_data(data
);
504 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
505 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
506 free_nfs_cb_data(data
);
511 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
513 struct nfs_cb_data
*data
= private_data
;
514 struct nfs_context
*nfs
= data
->nfs
;
516 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
518 /* Dont want any more callbacks even if the socket is closed */
519 rpc
->connect_cb
= NULL
;
521 if (status
== RPC_STATUS_ERROR
) {
522 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
523 free_nfs_cb_data(data
);
526 if (status
== RPC_STATUS_CANCEL
) {
527 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
528 free_nfs_cb_data(data
);
532 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
533 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
534 free_nfs_cb_data(data
);
540 * Async call for mounting an nfs share and geting the root filehandle
542 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
544 struct nfs_cb_data
*data
;
545 char *new_server
, *new_export
;
547 data
= malloc(sizeof(struct nfs_cb_data
));
549 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
552 memset(data
, 0, sizeof(struct nfs_cb_data
));
553 new_server
= strdup(server
);
554 new_export
= strdup(export
);
555 if (nfs
->server
!= NULL
) {
558 nfs
->server
= new_server
;
559 if (nfs
->export
!= NULL
) {
562 nfs
->export
= new_export
;
565 data
->private_data
= private_data
;
567 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
568 rpc_set_error(nfs
->rpc
, "Failed to start connection");
569 free_nfs_cb_data(data
);
579 * Functions to first look up a path, component by component, and then finally call a specific function once
580 * the filehandle for the final component is found.
582 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
584 struct nfs_cb_data
*data
= private_data
;
585 struct nfs_context
*nfs
= data
->nfs
;
588 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
590 if (status
== RPC_STATUS_ERROR
) {
591 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
592 free_nfs_cb_data(data
);
595 if (status
== RPC_STATUS_CANCEL
) {
596 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
597 free_nfs_cb_data(data
);
602 if (res
->status
!= NFS3_OK
) {
603 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
));
604 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
605 free_nfs_cb_data(data
);
609 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
610 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
611 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
612 free_nfs_cb_data(data
);
617 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
621 while (*data
->path
== '/') {
626 str
= strchr(path
, '/');
631 while (*data
->path
!= 0) {
637 data
->fh
.data
.data_len
= fh
->data
.data_len
;
638 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
639 if (data
->fh
.data
.data_val
== NULL
) {
640 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
641 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
642 free_nfs_cb_data(data
);
645 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
646 data
->continue_cb(nfs
, data
);
650 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, fh
, path
, data
) != 0) {
651 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
652 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
653 free_nfs_cb_data(data
);
659 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
)
661 struct nfs_cb_data
*data
;
663 if (path
[0] != 0 && path
[0] != '/') {
664 rpc_set_error(nfs
->rpc
, "Pathname is not absolute %s", path
);
668 data
= malloc(sizeof(struct nfs_cb_data
));
670 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
673 memset(data
, 0, sizeof(struct nfs_cb_data
));
676 data
->continue_cb
= continue_cb
;
677 data
->continue_data
= continue_data
;
678 data
->free_continue_data
= free_continue_data
;
679 data
->continue_int
= continue_int
;
680 data
->private_data
= private_data
;
681 data
->saved_path
= strdup(path
);
682 if (data
->saved_path
== NULL
) {
683 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
684 free_nfs_cb_data(data
);
687 data
->path
= data
->saved_path
;
689 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
690 /* return 0 here since the callback will be invoked if there is a failure */
703 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
706 struct nfs_cb_data
*data
= private_data
;
707 struct nfs_context
*nfs
= data
->nfs
;
710 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
712 if (status
== RPC_STATUS_ERROR
) {
713 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
714 free_nfs_cb_data(data
);
717 if (status
== RPC_STATUS_CANCEL
) {
718 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
719 free_nfs_cb_data(data
);
724 if (res
->status
!= NFS3_OK
) {
725 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
));
726 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
727 free_nfs_cb_data(data
);
732 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
733 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
734 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
735 st
.st_mode
|= S_IFDIR
;
737 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
738 st
.st_mode
|= S_IFREG
;
740 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
741 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
742 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
744 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
746 st
.st_blksize
= 4096;
747 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
749 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
750 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
751 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
753 data
->cb(0, nfs
, &st
, data
->private_data
);
754 free_nfs_cb_data(data
);
757 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
759 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &data
->fh
, data
) != 0) {
760 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
761 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
762 free_nfs_cb_data(data
);
768 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
770 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
771 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
785 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
788 struct nfs_cb_data
*data
= private_data
;
789 struct nfs_context
*nfs
= data
->nfs
;
791 unsigned int nfsmode
= 0;
793 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
795 if (status
== RPC_STATUS_ERROR
) {
796 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
797 free_nfs_cb_data(data
);
800 if (status
== RPC_STATUS_CANCEL
) {
801 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
802 free_nfs_cb_data(data
);
807 if (res
->status
!= NFS3_OK
) {
808 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
));
809 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
810 free_nfs_cb_data(data
);
814 if (data
->continue_int
& O_WRONLY
) {
815 nfsmode
|= ACCESS3_MODIFY
;
817 if (data
->continue_int
& O_RDWR
) {
818 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
820 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
821 nfsmode
|= ACCESS3_READ
;
825 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
826 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
827 nfsmode
&ACCESS3_READ
?'r':'-',
828 nfsmode
&ACCESS3_MODIFY
?'w':'-',
829 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
830 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
831 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
832 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
833 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
834 free_nfs_cb_data(data
);
838 nfsfh
= malloc(sizeof(struct nfsfh
));
840 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
841 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
842 free_nfs_cb_data(data
);
845 memset(nfsfh
, 0, sizeof(struct nfsfh
));
847 if (data
->continue_int
& O_SYNC
) {
851 /* steal the filehandle */
852 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
853 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
854 data
->fh
.data
.data_val
= NULL
;
856 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
857 free_nfs_cb_data(data
);
860 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
864 if (data
->continue_int
& O_WRONLY
) {
865 nfsmode
|= ACCESS3_MODIFY
;
867 if (data
->continue_int
& O_RDWR
) {
868 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
870 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
871 nfsmode
|= ACCESS3_READ
;
874 if (rpc_nfs_access_async(nfs
->rpc
, nfs_open_cb
, &data
->fh
, nfsmode
, data
) != 0) {
875 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
876 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
877 free_nfs_cb_data(data
);
883 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
885 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
886 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
900 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
902 struct nfs_cb_data
*data
= private_data
;
903 struct nfs_context
*nfs
= data
->nfs
;
906 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
908 if (status
== RPC_STATUS_ERROR
) {
909 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
910 free_nfs_cb_data(data
);
913 if (status
== RPC_STATUS_CANCEL
) {
914 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
915 free_nfs_cb_data(data
);
920 if (res
->status
!= NFS3_OK
) {
921 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
922 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
923 free_nfs_cb_data(data
);
927 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
928 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
929 free_nfs_cb_data(data
);
932 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
934 struct nfs_mcb_data
*mdata
= private_data
;
935 struct nfs_cb_data
*data
= mdata
->data
;
936 struct nfs_context
*nfs
= data
->nfs
;
939 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
943 if (status
== RPC_STATUS_ERROR
) {
944 /* flag the failure but do not invoke callback until we have received all responses */
947 if (status
== RPC_STATUS_CANCEL
) {
948 /* flag the cancellation but do not invoke callback until we have received all responses */
952 /* reassemble the data into the buffer */
953 if (status
== RPC_STATUS_SUCCESS
) {
955 if (res
->status
!= NFS3_OK
) {
956 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
959 if (res
->READ3res_u
.resok
.count
> 0) {
960 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
961 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
962 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
968 if (data
->num_calls
> 0) {
969 /* still waiting for more replies */
974 if (data
->error
!= 0) {
975 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
976 free_nfs_cb_data(data
);
980 if (data
->cancel
!= 0) {
981 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
982 free_nfs_cb_data(data
);
987 data
->nfsfh
->offset
= data
->max_offset
;
988 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
990 free_nfs_cb_data(data
);
994 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
996 struct nfs_cb_data
*data
;
998 data
= malloc(sizeof(struct nfs_cb_data
));
1000 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1003 memset(data
, 0, sizeof(struct nfs_cb_data
));
1006 data
->private_data
= private_data
;
1007 data
->nfsfh
= nfsfh
;
1009 nfsfh
->offset
= offset
;
1011 if (count
<= nfs_get_readmax(nfs
)) {
1012 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_cb
, &nfsfh
->fh
, offset
, count
, data
) != 0) {
1013 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1014 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1015 free_nfs_cb_data(data
);
1021 /* trying to read more than maximum server read size, we has to chop it up into smaller
1022 * reads and collect into a reassembly buffer.
1023 * we send all reads in parallell so that performance is still good.
1025 data
->max_offset
= offset
;
1026 data
->start_offset
= offset
;
1028 data
->buffer
= malloc(count
);
1029 if (data
->buffer
== NULL
) {
1030 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1031 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1032 free_nfs_cb_data(data
);
1037 uint64_t readcount
= count
;
1038 struct nfs_mcb_data
*mdata
;
1040 if (readcount
> nfs_get_readmax(nfs
)) {
1041 readcount
= nfs_get_readmax(nfs
);
1044 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1045 if (mdata
== NULL
) {
1046 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1049 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1051 mdata
->offset
= offset
;
1052 mdata
->count
= readcount
;
1053 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_mcb
, &nfsfh
->fh
, offset
, readcount
, mdata
) != 0) {
1054 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1055 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1061 offset
+= readcount
;
1071 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1073 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1081 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1083 struct nfs_cb_data
*data
= private_data
;
1084 struct nfs_context
*nfs
= data
->nfs
;
1087 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1089 if (status
== RPC_STATUS_ERROR
) {
1090 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1091 free_nfs_cb_data(data
);
1094 if (status
== RPC_STATUS_CANCEL
) {
1095 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1096 free_nfs_cb_data(data
);
1101 if (res
->status
!= NFS3_OK
) {
1102 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1103 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1104 free_nfs_cb_data(data
);
1108 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1109 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1110 free_nfs_cb_data(data
);
1113 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1115 struct nfs_mcb_data
*mdata
= private_data
;
1116 struct nfs_cb_data
*data
= mdata
->data
;
1117 struct nfs_context
*nfs
= data
->nfs
;
1120 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1124 if (status
== RPC_STATUS_ERROR
) {
1125 /* flag the failure but do not invoke callback until we have received all responses */
1128 if (status
== RPC_STATUS_CANCEL
) {
1129 /* flag the cancellation but do not invoke callback until we have received all responses */
1133 if (status
== RPC_STATUS_SUCCESS
) {
1135 if (res
->status
!= NFS3_OK
) {
1136 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1139 if (res
->WRITE3res_u
.resok
.count
> 0) {
1140 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1141 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1147 if (data
->num_calls
> 0) {
1148 /* still waiting for more replies */
1153 if (data
->error
!= 0) {
1154 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1155 free_nfs_cb_data(data
);
1159 if (data
->cancel
!= 0) {
1160 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1161 free_nfs_cb_data(data
);
1166 data
->nfsfh
->offset
= data
->max_offset
;
1167 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1169 free_nfs_cb_data(data
);
1174 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
)
1176 struct nfs_cb_data
*data
;
1178 data
= malloc(sizeof(struct nfs_cb_data
));
1180 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1183 memset(data
, 0, sizeof(struct nfs_cb_data
));
1186 data
->private_data
= private_data
;
1187 data
->nfsfh
= nfsfh
;
1189 nfsfh
->offset
= offset
;
1191 if (count
<= nfs_get_writemax(nfs
)) {
1192 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_cb
, &nfsfh
->fh
, buf
, offset
, count
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, data
) != 0) {
1193 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1194 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1195 free_nfs_cb_data(data
);
1201 /* trying to write more than maximum server write size, we has to chop it up into smaller
1203 * we send all writes in parallell so that performance is still good.
1205 data
->max_offset
= offset
;
1206 data
->start_offset
= offset
;
1209 uint64_t writecount
= count
;
1210 struct nfs_mcb_data
*mdata
;
1212 if (writecount
> nfs_get_writemax(nfs
)) {
1213 writecount
= nfs_get_writemax(nfs
);
1216 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1217 if (mdata
== NULL
) {
1218 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1221 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1223 mdata
->offset
= offset
;
1224 mdata
->count
= writecount
;
1226 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) {
1227 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1228 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1233 count
-= writecount
;
1234 offset
+= writecount
;
1244 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1246 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1256 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1258 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1259 free(nfsfh
->fh
.data
.data_val
);
1260 nfsfh
->fh
.data
.data_val
= NULL
;
1264 cb(0, nfs
, NULL
, private_data
);
1275 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1277 struct nfs_cb_data
*data
;
1279 data
= malloc(sizeof(struct nfs_cb_data
));
1281 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1284 memset(data
, 0, sizeof(struct nfs_cb_data
));
1287 data
->private_data
= private_data
;
1289 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &nfsfh
->fh
, data
) != 0) {
1290 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1291 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1292 free_nfs_cb_data(data
);
1303 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1305 struct nfs_cb_data
*data
= private_data
;
1306 struct nfs_context
*nfs
= data
->nfs
;
1309 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1311 if (status
== RPC_STATUS_ERROR
) {
1312 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1313 free_nfs_cb_data(data
);
1316 if (status
== RPC_STATUS_CANCEL
) {
1317 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1318 free_nfs_cb_data(data
);
1323 if (res
->status
!= NFS3_OK
) {
1324 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1325 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1326 free_nfs_cb_data(data
);
1330 data
->cb(0, nfs
, NULL
, data
->private_data
);
1331 free_nfs_cb_data(data
);
1334 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1336 struct nfs_cb_data
*data
;
1338 data
= malloc(sizeof(struct nfs_cb_data
));
1340 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1343 memset(data
, 0, sizeof(struct nfs_cb_data
));
1346 data
->private_data
= private_data
;
1348 if (rpc_nfs_commit_async(nfs
->rpc
, nfs_fsync_cb
, &nfsfh
->fh
, data
) != 0) {
1349 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1350 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1351 free_nfs_cb_data(data
);
1363 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1365 struct nfs_cb_data
*data
= private_data
;
1366 struct nfs_context
*nfs
= data
->nfs
;
1369 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1371 if (status
== RPC_STATUS_ERROR
) {
1372 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1373 free_nfs_cb_data(data
);
1376 if (status
== RPC_STATUS_CANCEL
) {
1377 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1378 free_nfs_cb_data(data
);
1383 if (res
->status
!= NFS3_OK
) {
1384 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1385 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1386 free_nfs_cb_data(data
);
1390 data
->cb(0, nfs
, NULL
, data
->private_data
);
1391 free_nfs_cb_data(data
);
1394 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1396 struct nfs_cb_data
*data
;
1399 data
= malloc(sizeof(struct nfs_cb_data
));
1401 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1404 memset(data
, 0, sizeof(struct nfs_cb_data
));
1407 data
->private_data
= private_data
;
1409 memset(&args
, 0, sizeof(SETATTR3args
));
1410 args
.object
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
1411 args
.object
.data
.data_val
= nfsfh
->fh
.data
.data_val
;
1412 args
.new_attributes
.size
.set_it
= 1;
1413 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1415 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1416 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1417 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1418 free_nfs_cb_data(data
);
1428 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1430 uint64_t offset
= data
->continue_int
;
1433 nfsfh
.fh
.data
.data_val
= data
->fh
.data
.data_val
;
1434 nfsfh
.fh
.data
.data_len
= data
->fh
.data
.data_len
;
1436 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1437 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1438 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1439 free_nfs_cb_data(data
);
1442 free_nfs_cb_data(data
);
1446 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1452 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1453 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1466 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1469 struct nfs_cb_data
*data
= private_data
;
1470 struct nfs_context
*nfs
= data
->nfs
;
1471 char *str
= data
->continue_data
;
1473 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1475 str
= &str
[strlen(str
) + 1];
1477 if (status
== RPC_STATUS_ERROR
) {
1478 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1479 free_nfs_cb_data(data
);
1482 if (status
== RPC_STATUS_CANCEL
) {
1483 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1484 free_nfs_cb_data(data
);
1489 if (res
->status
!= NFS3_OK
) {
1490 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
));
1491 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1492 free_nfs_cb_data(data
);
1496 data
->cb(0, nfs
, NULL
, data
->private_data
);
1497 free_nfs_cb_data(data
);
1500 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1502 char *str
= data
->continue_data
;
1505 str
= &str
[strlen(str
) + 1];
1507 memset(&args
, 0, sizeof(MKDIR3args
));
1508 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1509 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1510 args
.where
.name
= str
;
1511 args
.attributes
.mode
.set_it
= 1;
1512 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
1514 if (rpc_nfs_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
1515 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1516 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1517 free_nfs_cb_data(data
);
1523 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1528 new_path
= strdup(path
);
1529 if (new_path
== NULL
) {
1530 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1534 ptr
= strrchr(new_path
, '/');
1536 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1541 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1542 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1543 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1557 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1560 struct nfs_cb_data
*data
= private_data
;
1561 struct nfs_context
*nfs
= data
->nfs
;
1562 char *str
= data
->continue_data
;
1564 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1566 str
= &str
[strlen(str
) + 1];
1568 if (status
== RPC_STATUS_ERROR
) {
1569 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1570 free_nfs_cb_data(data
);
1573 if (status
== RPC_STATUS_CANCEL
) {
1574 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1575 free_nfs_cb_data(data
);
1580 if (res
->status
!= NFS3_OK
) {
1581 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
));
1582 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1583 free_nfs_cb_data(data
);
1587 data
->cb(0, nfs
, NULL
, data
->private_data
);
1588 free_nfs_cb_data(data
);
1591 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1593 char *str
= data
->continue_data
;
1595 str
= &str
[strlen(str
) + 1];
1597 if (rpc_nfs_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &data
->fh
, str
, data
) != 0) {
1598 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1599 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1600 free_nfs_cb_data(data
);
1606 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1611 new_path
= strdup(path
);
1612 if (new_path
== NULL
) {
1613 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1617 ptr
= strrchr(new_path
, '/');
1619 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1624 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1625 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1626 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1639 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1642 struct nfs_cb_data
*data
= private_data
;
1643 struct nfs_context
*nfs
= data
->nfs
;
1644 struct nfsfh
*nfsfh
;
1645 char *str
= data
->continue_data
;
1647 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1649 if (status
== RPC_STATUS_ERROR
) {
1650 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1651 free_nfs_cb_data(data
);
1654 if (status
== RPC_STATUS_CANCEL
) {
1655 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1656 free_nfs_cb_data(data
);
1660 str
= &str
[strlen(str
) + 1];
1662 if (res
->status
!= NFS3_OK
) {
1663 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
));
1664 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1669 nfsfh
= malloc(sizeof(struct nfsfh
));
1670 if (nfsfh
== NULL
) {
1671 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1672 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1673 free_nfs_cb_data(data
);
1676 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1678 /* copy the filehandle */
1679 nfsfh
->fh
.data
.data_len
= res
->LOOKUP3res_u
.resok
.object
.data
.data_len
;
1680 nfsfh
->fh
.data
.data_val
= malloc(nfsfh
->fh
.data
.data_len
);
1681 memcpy(nfsfh
->fh
.data
.data_val
, res
->LOOKUP3res_u
.resok
.object
.data
.data_val
, nfsfh
->fh
.data
.data_len
);
1683 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1684 free_nfs_cb_data(data
);
1689 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1692 struct nfs_cb_data
*data
= private_data
;
1693 struct nfs_context
*nfs
= data
->nfs
;
1694 char *str
= data
->continue_data
;
1696 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1698 if (status
== RPC_STATUS_ERROR
) {
1699 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1700 free_nfs_cb_data(data
);
1703 if (status
== RPC_STATUS_CANCEL
) {
1704 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1705 free_nfs_cb_data(data
);
1709 str
= &str
[strlen(str
) + 1];
1711 if (res
->status
!= NFS3_OK
) {
1712 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
));
1713 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1718 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &data
->fh
, str
, data
) != 0) {
1719 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
1720 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1721 free_nfs_cb_data(data
);
1727 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1729 char *str
= data
->continue_data
;
1732 str
= &str
[strlen(str
) + 1];
1734 memset(&args
, 0, sizeof(CREATE3args
));
1735 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1736 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1737 args
.where
.name
= str
;
1738 args
.how
.mode
= UNCHECKED
;
1739 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
1740 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
1742 if (rpc_nfs_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
1743 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
1744 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1745 free_nfs_cb_data(data
);
1751 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1756 new_path
= strdup(path
);
1757 if (new_path
== NULL
) {
1758 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1762 ptr
= strrchr(new_path
, '/');
1764 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1769 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
1770 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
1771 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1784 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1787 struct nfs_cb_data
*data
= private_data
;
1788 struct nfs_context
*nfs
= data
->nfs
;
1789 char *str
= data
->continue_data
;
1791 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1793 str
= &str
[strlen(str
) + 1];
1795 if (status
== RPC_STATUS_ERROR
) {
1796 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1797 free_nfs_cb_data(data
);
1800 if (status
== RPC_STATUS_CANCEL
) {
1801 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1802 free_nfs_cb_data(data
);
1807 if (res
->status
!= NFS3_OK
) {
1808 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
));
1809 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1810 free_nfs_cb_data(data
);
1814 data
->cb(0, nfs
, NULL
, data
->private_data
);
1815 free_nfs_cb_data(data
);
1818 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1820 char *str
= data
->continue_data
;
1822 str
= &str
[strlen(str
) + 1];
1824 if (rpc_nfs_remove_async(nfs
->rpc
, nfs_unlink_cb
, &data
->fh
, str
, data
) != 0) {
1825 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
1826 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1827 free_nfs_cb_data(data
);
1833 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1838 new_path
= strdup(path
);
1839 if (new_path
== NULL
) {
1840 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1844 ptr
= strrchr(new_path
, '/');
1846 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1851 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1852 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
1853 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1864 struct mknod_cb_data
{
1871 static void free_mknod_cb_data(void *ptr
)
1873 struct mknod_cb_data
*data
= ptr
;
1879 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1882 struct nfs_cb_data
*data
= private_data
;
1883 struct nfs_context
*nfs
= data
->nfs
;
1884 char *str
= data
->continue_data
;
1886 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1888 str
= &str
[strlen(str
) + 1];
1890 if (status
== RPC_STATUS_ERROR
) {
1891 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1892 free_nfs_cb_data(data
);
1895 if (status
== RPC_STATUS_CANCEL
) {
1896 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1897 free_nfs_cb_data(data
);
1902 if (res
->status
!= NFS3_OK
) {
1903 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
));
1904 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1905 free_nfs_cb_data(data
);
1909 data
->cb(0, nfs
, NULL
, data
->private_data
);
1910 free_nfs_cb_data(data
);
1913 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1915 struct mknod_cb_data
*cb_data
= data
->continue_data
;
1916 char *str
= cb_data
->path
;
1918 str
= &str
[strlen(str
) + 1];
1920 if (rpc_nfs_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &data
->fh
, str
, cb_data
->mode
, cb_data
->major
, cb_data
->minor
, data
) != 0) {
1921 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1922 free_nfs_cb_data(data
);
1928 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
1931 struct mknod_cb_data
*cb_data
;
1933 cb_data
= malloc(sizeof(struct mknod_cb_data
));
1934 if (cb_data
== NULL
) {
1935 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
1939 cb_data
->path
= strdup(path
);
1940 if (cb_data
->path
== NULL
) {
1941 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1946 ptr
= strrchr(cb_data
->path
, '/');
1948 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1953 cb_data
->mode
= mode
;
1954 cb_data
->major
= major(dev
);
1955 cb_data
->minor
= minor(dev
);
1957 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1958 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
1959 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1960 free_mknod_cb_data(cb_data
);
1971 /* ReadDirPlus Emulation Callback data */
1972 struct rdpe_cb_data
{
1975 struct nfs_cb_data
*data
;
1978 /* ReadDirPlus Emulation LOOKUP Callback data */
1979 struct rdpe_lookup_cb_data
{
1980 struct rdpe_cb_data
*rdpe_cb_data
;
1981 struct nfsdirent
*nfsdirent
;
1984 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
1985 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1987 LOOKUP3res
*res
= command_data
;
1988 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
1989 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
1990 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
1991 struct nfsdir
*nfsdir
= data
->continue_data
;
1992 struct nfs_context
*nfs
= data
->nfs
;
1993 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
1995 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1997 free(rdpe_lookup_cb_data
);
1999 rdpe_cb_data
->getattrcount
--;
2001 if (status
== RPC_STATUS_ERROR
) {
2002 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2004 if (status
== RPC_STATUS_CANCEL
) {
2005 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
2007 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
2008 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2010 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
2011 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
2012 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
2014 nfsdirent
->type
= attributes
->type
;
2015 nfsdirent
->mode
= attributes
->mode
;
2016 nfsdirent
->size
= attributes
->size
;
2018 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
2019 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
2020 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
2021 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2022 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2023 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2027 if (rdpe_cb_data
->getattrcount
== 0) {
2028 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2029 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2030 nfs_free_nfsdir(nfsdir
);
2032 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2036 data
->continue_data
= NULL
;
2037 free_nfs_cb_data(data
);
2041 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2043 READDIR3res
*res
= command_data
;
2044 struct nfs_cb_data
*data
= private_data
;
2045 struct nfs_context
*nfs
= data
->nfs
;
2046 struct nfsdir
*nfsdir
= data
->continue_data
;
2047 struct nfsdirent
*nfsdirent
;
2048 struct entry3
*entry
;
2050 struct rdpe_cb_data
*rdpe_cb_data
;
2052 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2054 if (status
== RPC_STATUS_ERROR
) {
2055 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2056 nfs_free_nfsdir(nfsdir
);
2057 data
->continue_data
= NULL
;
2058 free_nfs_cb_data(data
);
2062 if (status
== RPC_STATUS_CANCEL
) {
2063 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2064 nfs_free_nfsdir(nfsdir
);
2065 data
->continue_data
= NULL
;
2066 free_nfs_cb_data(data
);
2070 if (res
->status
!= NFS3_OK
) {
2071 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
));
2072 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2073 nfs_free_nfsdir(nfsdir
);
2074 data
->continue_data
= NULL
;
2075 free_nfs_cb_data(data
);
2079 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2080 while (entry
!= NULL
) {
2081 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2082 if (nfsdirent
== NULL
) {
2083 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2084 nfs_free_nfsdir(nfsdir
);
2085 data
->continue_data
= NULL
;
2086 free_nfs_cb_data(data
);
2089 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2090 nfsdirent
->name
= strdup(entry
->name
);
2091 if (nfsdirent
->name
== NULL
) {
2092 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2093 nfs_free_nfsdir(nfsdir
);
2094 data
->continue_data
= NULL
;
2095 free_nfs_cb_data(data
);
2098 nfsdirent
->inode
= entry
->fileid
;
2100 nfsdirent
->next
= nfsdir
->entries
;
2101 nfsdir
->entries
= nfsdirent
;
2103 cookie
= entry
->cookie
;
2104 entry
= entry
->nextentry
;
2107 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2108 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, cookie
, res
->READDIR3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2109 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2110 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2111 nfs_free_nfsdir(nfsdir
);
2112 data
->continue_data
= NULL
;
2113 free_nfs_cb_data(data
);
2119 /* steal the dirhandle */
2120 nfsdir
->current
= nfsdir
->entries
;
2122 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2123 rdpe_cb_data
->getattrcount
= 0;
2124 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2125 rdpe_cb_data
->data
= data
;
2126 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2127 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2129 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2130 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2131 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2133 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &data
->fh
, nfsdirent
->name
, rdpe_lookup_cb_data
) != 0) {
2134 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2136 /* if we have already commands in flight, we cant just stop, we have to wait for the
2137 * commands in flight to complete
2139 if (rdpe_cb_data
->getattrcount
> 0) {
2140 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2141 free(rdpe_lookup_cb_data
);
2145 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2146 nfs_free_nfsdir(nfsdir
);
2147 data
->continue_data
= NULL
;
2148 free_nfs_cb_data(data
);
2149 free(rdpe_lookup_cb_data
);
2153 rdpe_cb_data
->getattrcount
++;
2158 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2160 READDIRPLUS3res
*res
= command_data
;
2161 struct nfs_cb_data
*data
= private_data
;
2162 struct nfs_context
*nfs
= data
->nfs
;
2163 struct nfsdir
*nfsdir
= data
->continue_data
;
2164 struct entryplus3
*entry
;
2167 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2169 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2172 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2173 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2174 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2175 nfs_free_nfsdir(nfsdir
);
2176 data
->continue_data
= NULL
;
2177 free_nfs_cb_data(data
);
2183 if (status
== RPC_STATUS_CANCEL
) {
2184 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2185 nfs_free_nfsdir(nfsdir
);
2186 data
->continue_data
= NULL
;
2187 free_nfs_cb_data(data
);
2191 if (res
->status
!= NFS3_OK
) {
2192 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
));
2193 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2194 nfs_free_nfsdir(nfsdir
);
2195 data
->continue_data
= NULL
;
2196 free_nfs_cb_data(data
);
2200 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2201 while (entry
!= NULL
) {
2202 struct nfsdirent
*nfsdirent
;
2204 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2205 if (nfsdirent
== NULL
) {
2206 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2207 nfs_free_nfsdir(nfsdir
);
2208 data
->continue_data
= NULL
;
2209 free_nfs_cb_data(data
);
2212 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2213 nfsdirent
->name
= strdup(entry
->name
);
2214 if (nfsdirent
->name
== NULL
) {
2215 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2216 nfs_free_nfsdir(nfsdir
);
2217 data
->continue_data
= NULL
;
2218 free_nfs_cb_data(data
);
2221 nfsdirent
->inode
= entry
->fileid
;
2222 if (entry
->name_attributes
.attributes_follow
) {
2223 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2224 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2225 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2227 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2228 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2229 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2230 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2231 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2232 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2235 nfsdirent
->next
= nfsdir
->entries
;
2236 nfsdir
->entries
= nfsdirent
;
2238 cookie
= entry
->cookie
;
2239 entry
= entry
->nextentry
;
2242 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2243 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, cookie
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2244 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2245 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2246 nfs_free_nfsdir(nfsdir
);
2247 data
->continue_data
= NULL
;
2248 free_nfs_cb_data(data
);
2254 /* steal the dirhandle */
2255 data
->continue_data
= NULL
;
2256 nfsdir
->current
= nfsdir
->entries
;
2258 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2259 free_nfs_cb_data(data
);
2262 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2266 memset(cv
, 0, sizeof(cookieverf3
));
2267 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2268 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2269 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2270 free_nfs_cb_data(data
);
2276 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2278 struct nfsdir
*nfsdir
;
2280 nfsdir
= malloc(sizeof(struct nfsdir
));
2281 if (nfsdir
== NULL
) {
2282 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2285 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2287 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2288 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2296 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2298 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2300 if (nfsdir
->current
!= NULL
) {
2301 nfsdir
->current
= nfsdir
->current
->next
;
2307 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2309 nfs_free_nfsdir(nfsdir
);
2321 struct lseek_cb_data
{
2322 struct nfs_context
*nfs
;
2323 struct nfsfh
*nfsfh
;
2329 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2332 struct lseek_cb_data
*data
= private_data
;
2333 struct nfs_context
*nfs
= data
->nfs
;
2335 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2337 if (status
== RPC_STATUS_ERROR
) {
2338 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2342 if (status
== RPC_STATUS_CANCEL
) {
2343 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2349 if (res
->status
!= NFS3_OK
) {
2350 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2351 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2356 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2357 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2361 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2363 struct lseek_cb_data
*data
;
2365 if (whence
== SEEK_SET
) {
2366 nfsfh
->offset
= offset
;
2367 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2370 if (whence
== SEEK_CUR
) {
2371 nfsfh
->offset
+= offset
;
2372 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2376 data
= malloc(sizeof(struct lseek_cb_data
));
2378 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2383 data
->nfsfh
= nfsfh
;
2384 data
->offset
= offset
;
2386 data
->private_data
= private_data
;
2388 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &nfsfh
->fh
, data
) != 0) {
2389 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
2402 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2405 struct nfs_cb_data
*data
= private_data
;
2406 struct nfs_context
*nfs
= data
->nfs
;
2407 struct statvfs svfs
;
2409 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2411 if (status
== RPC_STATUS_ERROR
) {
2412 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2413 free_nfs_cb_data(data
);
2416 if (status
== RPC_STATUS_CANCEL
) {
2417 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2418 free_nfs_cb_data(data
);
2423 if (res
->status
!= NFS3_OK
) {
2424 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
));
2425 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2426 free_nfs_cb_data(data
);
2430 svfs
.f_bsize
= 4096;
2431 svfs
.f_frsize
= 4096;
2432 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
2433 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
2434 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
2435 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
2436 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
2437 #if !defined(ANDROID)
2438 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
2441 svfs
.f_namemax
= 256;
2444 data
->cb(0, nfs
, &svfs
, data
->private_data
);
2445 free_nfs_cb_data(data
);
2448 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2450 if (rpc_nfs_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &data
->fh
, data
) != 0) {
2451 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
2452 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2453 free_nfs_cb_data(data
);
2459 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2461 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
2462 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2475 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2478 struct nfs_cb_data
*data
= private_data
;
2479 struct nfs_context
*nfs
= data
->nfs
;
2481 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2483 if (status
== RPC_STATUS_ERROR
) {
2484 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2485 free_nfs_cb_data(data
);
2488 if (status
== RPC_STATUS_CANCEL
) {
2489 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2490 free_nfs_cb_data(data
);
2495 if (res
->status
!= NFS3_OK
) {
2496 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
));
2497 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2498 free_nfs_cb_data(data
);
2503 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
2504 free_nfs_cb_data(data
);
2507 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2511 args
.symlink
.data
.data_len
= data
->fh
.data
.data_len
;
2512 args
.symlink
.data
.data_val
= data
->fh
.data
.data_val
;
2514 if (rpc_nfs_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
2515 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
2516 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2517 free_nfs_cb_data(data
);
2523 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2525 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
2526 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2539 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2541 struct nfs_cb_data
*data
= private_data
;
2542 struct nfs_context
*nfs
= data
->nfs
;
2545 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2547 if (status
== RPC_STATUS_ERROR
) {
2548 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2549 free_nfs_cb_data(data
);
2552 if (status
== RPC_STATUS_CANCEL
) {
2553 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2554 free_nfs_cb_data(data
);
2559 if (res
->status
!= NFS3_OK
) {
2560 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2561 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2562 free_nfs_cb_data(data
);
2566 data
->cb(0, nfs
, NULL
, data
->private_data
);
2567 free_nfs_cb_data(data
);
2570 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2574 memset(&args
, 0, sizeof(SETATTR3args
));
2575 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2576 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2577 args
.new_attributes
.mode
.set_it
= 1;
2578 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2580 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
2581 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2582 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2583 free_nfs_cb_data(data
);
2590 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2592 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
2593 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2603 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
2605 struct nfs_cb_data
*data
;
2607 data
= malloc(sizeof(struct nfs_cb_data
));
2609 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
2612 memset(data
, 0, sizeof(struct nfs_cb_data
));
2615 data
->private_data
= private_data
;
2616 data
->continue_int
= mode
;
2617 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2618 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2619 if (data
->fh
.data
.data_val
== NULL
) {
2620 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2621 free_nfs_cb_data(data
);
2624 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2626 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
2627 free_nfs_cb_data(data
);
2639 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2641 struct nfs_cb_data
*data
= private_data
;
2642 struct nfs_context
*nfs
= data
->nfs
;
2645 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2647 if (status
== RPC_STATUS_ERROR
) {
2648 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2649 free_nfs_cb_data(data
);
2652 if (status
== RPC_STATUS_CANCEL
) {
2653 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2654 free_nfs_cb_data(data
);
2659 if (res
->status
!= NFS3_OK
) {
2660 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2661 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2662 free_nfs_cb_data(data
);
2666 data
->cb(0, nfs
, NULL
, data
->private_data
);
2667 free_nfs_cb_data(data
);
2670 struct nfs_chown_data
{
2675 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2678 struct nfs_chown_data
*chown_data
= data
->continue_data
;
2680 memset(&args
, 0, sizeof(SETATTR3args
));
2681 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2682 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2683 if (chown_data
->uid
!= (uid_t
)-1) {
2684 args
.new_attributes
.uid
.set_it
= 1;
2685 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
2687 if (chown_data
->gid
!= (gid_t
)-1) {
2688 args
.new_attributes
.gid
.set_it
= 1;
2689 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
2692 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
2693 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2694 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2695 free_nfs_cb_data(data
);
2702 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2704 struct nfs_chown_data
*chown_data
;
2706 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2707 if (chown_data
== NULL
) {
2708 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2712 chown_data
->uid
= uid
;
2713 chown_data
->gid
= gid
;
2715 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
2716 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2727 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2729 struct nfs_cb_data
*data
;
2730 struct nfs_chown_data
*chown_data
;
2732 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2733 if (chown_data
== NULL
) {
2734 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2738 chown_data
->uid
= uid
;
2739 chown_data
->gid
= gid
;
2742 data
= malloc(sizeof(struct nfs_cb_data
));
2744 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
2747 memset(data
, 0, sizeof(struct nfs_cb_data
));
2750 data
->private_data
= private_data
;
2751 data
->continue_data
= chown_data
;
2752 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2753 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2754 if (data
->fh
.data
.data_val
== NULL
) {
2755 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2756 free_nfs_cb_data(data
);
2759 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2762 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
2763 free_nfs_cb_data(data
);
2777 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2779 struct nfs_cb_data
*data
= private_data
;
2780 struct nfs_context
*nfs
= data
->nfs
;
2783 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2785 if (status
== RPC_STATUS_ERROR
) {
2786 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2787 free_nfs_cb_data(data
);
2790 if (status
== RPC_STATUS_CANCEL
) {
2791 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2792 free_nfs_cb_data(data
);
2797 if (res
->status
!= NFS3_OK
) {
2798 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2799 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2800 free_nfs_cb_data(data
);
2804 data
->cb(0, nfs
, NULL
, data
->private_data
);
2805 free_nfs_cb_data(data
);
2808 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2811 struct timeval
*utimes_data
= data
->continue_data
;
2813 memset(&args
, 0, sizeof(SETATTR3args
));
2814 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2815 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2816 if (utimes_data
!= NULL
) {
2817 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
2818 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
2819 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
2820 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
2821 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
2822 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
2824 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
2825 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
2828 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
2829 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2830 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2831 free_nfs_cb_data(data
);
2838 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
2840 struct timeval
*new_times
= NULL
;
2842 if (times
!= NULL
) {
2843 new_times
= malloc(sizeof(struct timeval
)*2);
2844 if (new_times
== NULL
) {
2845 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2849 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
2852 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2853 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2863 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
2865 struct timeval
*new_times
= NULL
;
2867 if (times
!= NULL
) {
2868 new_times
= malloc(sizeof(struct timeval
)*2);
2869 if (new_times
== NULL
) {
2870 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2874 new_times
[0].tv_sec
= times
->actime
;
2875 new_times
[0].tv_usec
= 0;
2876 new_times
[1].tv_sec
= times
->modtime
;
2877 new_times
[1].tv_usec
= 0;
2880 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2881 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2895 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2898 struct nfs_cb_data
*data
= private_data
;
2899 struct nfs_context
*nfs
= data
->nfs
;
2900 unsigned int nfsmode
= 0;
2902 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2904 if (status
== RPC_STATUS_ERROR
) {
2905 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2906 free_nfs_cb_data(data
);
2909 if (status
== RPC_STATUS_CANCEL
) {
2910 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2911 free_nfs_cb_data(data
);
2916 if (res
->status
!= NFS3_OK
) {
2917 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
));
2918 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2919 free_nfs_cb_data(data
);
2923 if (data
->continue_int
& R_OK
) {
2924 nfsmode
|= ACCESS3_READ
;
2926 if (data
->continue_int
& W_OK
) {
2927 nfsmode
|= ACCESS3_MODIFY
;
2929 if (data
->continue_int
& X_OK
) {
2930 nfsmode
|= ACCESS3_EXECUTE
;
2933 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
2934 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2935 nfsmode
&ACCESS3_READ
?'r':'-',
2936 nfsmode
&ACCESS3_MODIFY
?'w':'-',
2937 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
2938 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
2939 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
2940 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
2941 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2942 free_nfs_cb_data(data
);
2946 data
->cb(0, nfs
, NULL
, data
->private_data
);
2947 free_nfs_cb_data(data
);
2950 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2954 if (data
->continue_int
& R_OK
) {
2955 nfsmode
|= ACCESS3_READ
;
2957 if (data
->continue_int
& W_OK
) {
2958 nfsmode
|= ACCESS3_MODIFY
;
2960 if (data
->continue_int
& X_OK
) {
2961 nfsmode
|= ACCESS3_EXECUTE
;
2964 if (rpc_nfs_access_async(nfs
->rpc
, nfs_access_cb
, &data
->fh
, nfsmode
, data
) != 0) {
2965 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
2966 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2967 free_nfs_cb_data(data
);
2973 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2975 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
2976 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2988 struct nfs_symlink_data
{
2990 char *newpathparent
;
2991 char *newpathobject
;
2994 static void free_nfs_symlink_data(void *mem
)
2996 struct nfs_symlink_data
*data
= mem
;
2998 if (data
->oldpath
!= NULL
) {
2999 free(data
->oldpath
);
3001 if (data
->newpathparent
!= NULL
) {
3002 free(data
->newpathparent
);
3004 if (data
->newpathobject
!= NULL
) {
3005 free(data
->newpathobject
);
3010 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3013 struct nfs_cb_data
*data
= private_data
;
3014 struct nfs_context
*nfs
= data
->nfs
;
3015 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3017 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3019 if (status
== RPC_STATUS_ERROR
) {
3020 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3021 free_nfs_cb_data(data
);
3024 if (status
== RPC_STATUS_CANCEL
) {
3025 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3026 free_nfs_cb_data(data
);
3031 if (res
->status
!= NFS3_OK
) {
3032 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
));
3033 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3034 free_nfs_cb_data(data
);
3038 data
->cb(0, nfs
, NULL
, data
->private_data
);
3039 free_nfs_cb_data(data
);
3042 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3044 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3047 memset(&sa
, 0, sizeof(SYMLINK3args
));
3048 sa
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
3049 sa
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
3050 sa
.where
.name
= symlink_data
->newpathobject
;
3051 sa
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3052 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
;
3053 sa
.symlink
.symlink_data
= symlink_data
->oldpath
;
3055 if (rpc_nfs_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &sa
, data
) != 0) {
3056 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3057 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3058 free_nfs_cb_data(data
);
3064 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3067 struct nfs_symlink_data
*symlink_data
;
3069 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3070 if (symlink_data
== NULL
) {
3071 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3074 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3076 symlink_data
->oldpath
= strdup(oldpath
);
3077 if (symlink_data
->oldpath
== NULL
) {
3078 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3079 free_nfs_symlink_data(symlink_data
);
3083 symlink_data
->newpathparent
= strdup(newpath
);
3084 if (symlink_data
->newpathparent
== NULL
) {
3085 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3086 free_nfs_symlink_data(symlink_data
);
3090 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3092 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3093 free_nfs_symlink_data(symlink_data
);
3099 symlink_data
->newpathobject
= strdup(ptr
);
3100 if (symlink_data
->newpathobject
== NULL
) {
3101 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3102 free_nfs_symlink_data(symlink_data
);
3106 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3107 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3119 struct nfs_rename_data
{
3122 struct nfs_fh3 olddir
;
3125 struct nfs_fh3 newdir
;
3128 static void free_nfs_rename_data(void *mem
)
3130 struct nfs_rename_data
*data
= mem
;
3132 if (data
->oldpath
!= NULL
) {
3133 free(data
->oldpath
);
3135 if (data
->olddir
.data
.data_val
!= NULL
) {
3136 free(data
->olddir
.data
.data_val
);
3138 if (data
->newpath
!= NULL
) {
3139 free(data
->newpath
);
3141 if (data
->newdir
.data
.data_val
!= NULL
) {
3142 free(data
->newdir
.data
.data_val
);
3147 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3150 struct nfs_cb_data
*data
= private_data
;
3151 struct nfs_context
*nfs
= data
->nfs
;
3152 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3154 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3156 if (status
== RPC_STATUS_ERROR
) {
3157 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3158 free_nfs_cb_data(data
);
3161 if (status
== RPC_STATUS_CANCEL
) {
3162 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3163 free_nfs_cb_data(data
);
3168 if (res
->status
!= NFS3_OK
) {
3169 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
));
3170 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3171 free_nfs_cb_data(data
);
3175 data
->cb(0, nfs
, NULL
, data
->private_data
);
3176 free_nfs_cb_data(data
);
3179 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3181 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3183 /* steal the filehandle */
3184 rename_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3185 rename_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3186 data
->fh
.data
.data_val
= NULL
;
3188 if (rpc_nfs_rename_async(nfs
->rpc
, nfs_rename_cb
, &rename_data
->olddir
, rename_data
->oldobject
, &rename_data
->newdir
, rename_data
->newobject
, data
) != 0) {
3189 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3190 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3191 free_nfs_cb_data(data
);
3198 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3200 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3202 /* steal the filehandle */
3203 rename_data
->olddir
.data
.data_len
= data
->fh
.data
.data_len
;
3204 rename_data
->olddir
.data
.data_val
= data
->fh
.data
.data_val
;
3205 data
->fh
.data
.data_val
= NULL
;
3207 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) {
3208 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3209 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3210 free_nfs_cb_data(data
);
3213 data
->continue_data
= NULL
;
3214 free_nfs_cb_data(data
);
3220 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3223 struct nfs_rename_data
*rename_data
;
3225 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3226 if (rename_data
== NULL
) {
3227 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3230 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3232 rename_data
->oldpath
= strdup(oldpath
);
3233 if (rename_data
->oldpath
== NULL
) {
3234 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3235 free_nfs_rename_data(rename_data
);
3238 ptr
= strrchr(rename_data
->oldpath
, '/');
3240 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3241 free_nfs_rename_data(rename_data
);
3246 rename_data
->oldobject
= ptr
;
3249 rename_data
->newpath
= strdup(newpath
);
3250 if (rename_data
->newpath
== NULL
) {
3251 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3252 free_nfs_rename_data(rename_data
);
3255 ptr
= strrchr(rename_data
->newpath
, '/');
3257 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3258 free_nfs_rename_data(rename_data
);
3263 rename_data
->newobject
= ptr
;
3266 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3267 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3278 struct nfs_link_data
{
3280 struct nfs_fh3 oldfh
;
3283 struct nfs_fh3 newdir
;
3286 static void free_nfs_link_data(void *mem
)
3288 struct nfs_link_data
*data
= mem
;
3290 if (data
->oldpath
!= NULL
) {
3291 free(data
->oldpath
);
3293 if (data
->oldfh
.data
.data_val
!= NULL
) {
3294 free(data
->oldfh
.data
.data_val
);
3296 if (data
->newpath
!= NULL
) {
3297 free(data
->newpath
);
3299 if (data
->newdir
.data
.data_val
!= NULL
) {
3300 free(data
->newdir
.data
.data_val
);
3305 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3308 struct nfs_cb_data
*data
= private_data
;
3309 struct nfs_context
*nfs
= data
->nfs
;
3310 struct nfs_link_data
*link_data
= data
->continue_data
;
3312 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3314 if (status
== RPC_STATUS_ERROR
) {
3315 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3316 free_nfs_cb_data(data
);
3319 if (status
== RPC_STATUS_CANCEL
) {
3320 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3321 free_nfs_cb_data(data
);
3326 if (res
->status
!= NFS3_OK
) {
3327 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
));
3328 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3329 free_nfs_cb_data(data
);
3333 data
->cb(0, nfs
, NULL
, data
->private_data
);
3334 free_nfs_cb_data(data
);
3337 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3339 struct nfs_link_data
*link_data
= data
->continue_data
;
3341 /* steal the filehandle */
3342 link_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3343 link_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3344 data
->fh
.data
.data_val
= NULL
;
3346 if (rpc_nfs_link_async(nfs
->rpc
, nfs_link_cb
, &link_data
->oldfh
, &link_data
->newdir
, link_data
->newobject
, data
) != 0) {
3347 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3348 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3349 free_nfs_cb_data(data
);
3356 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3358 struct nfs_link_data
*link_data
= data
->continue_data
;
3360 /* steal the filehandle */
3361 link_data
->oldfh
.data
.data_len
= data
->fh
.data
.data_len
;
3362 link_data
->oldfh
.data
.data_val
= data
->fh
.data
.data_val
;
3363 data
->fh
.data
.data_val
= NULL
;
3365 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) {
3366 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3367 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3368 free_nfs_cb_data(data
);
3371 data
->continue_data
= NULL
;
3372 free_nfs_cb_data(data
);
3378 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3381 struct nfs_link_data
*link_data
;
3383 link_data
= malloc(sizeof(struct nfs_link_data
));
3384 if (link_data
== NULL
) {
3385 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
3388 memset(link_data
, 0, sizeof(struct nfs_link_data
));
3390 link_data
->oldpath
= strdup(oldpath
);
3391 if (link_data
->oldpath
== NULL
) {
3392 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3393 free_nfs_link_data(link_data
);
3397 link_data
->newpath
= strdup(newpath
);
3398 if (link_data
->newpath
== NULL
) {
3399 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3400 free_nfs_link_data(link_data
);
3403 ptr
= strrchr(link_data
->newpath
, '/');
3405 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3406 free_nfs_link_data(link_data
);
3411 link_data
->newobject
= ptr
;
3414 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
3415 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3423 //qqq replace later with lseek()
3424 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
3426 return nfsfh
->offset
;
3432 * Get the maximum supported READ3 size by the server
3434 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
3436 return nfs
->readmax
;
3440 * Get the maximum supported WRITE3 size by the server
3442 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
3444 /* Some ZDR libraries can not marshall PDUs bigger than this */
3445 if (nfs
->writemax
< 32768) {
3446 return nfs
->writemax
;
3451 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
3456 va_start(ap
, error_string
);
3458 vsnprintf(str
, 1024, error_string
, ap
);
3459 if (nfs
->rpc
->error_string
!= NULL
) {
3460 free(nfs
->rpc
->error_string
);
3462 nfs
->rpc
->error_string
= str
;
3468 struct mount_cb_data
{
3474 static void free_mount_cb_data(struct mount_cb_data
*data
)
3476 if (data
->server
!= NULL
) {
3478 data
->server
= NULL
;
3484 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3486 struct mount_cb_data
*data
= private_data
;
3488 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3490 if (status
== RPC_STATUS_ERROR
) {
3491 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3492 free_mount_cb_data(data
);
3495 if (status
== RPC_STATUS_CANCEL
) {
3496 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3497 free_mount_cb_data(data
);
3501 data
->cb(rpc
, 0, command_data
, data
->private_data
);
3502 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
3503 rpc_set_error(rpc
, "Failed to disconnect\n");
3505 free_mount_cb_data(data
);
3508 static void mount_export_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3510 struct mount_cb_data
*data
= private_data
;
3512 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3514 /* Dont want any more callbacks even if the socket is closed */
3515 rpc
->connect_cb
= NULL
;
3517 if (status
== RPC_STATUS_ERROR
) {
3518 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3519 free_mount_cb_data(data
);
3522 if (status
== RPC_STATUS_CANCEL
) {
3523 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3524 free_mount_cb_data(data
);
3528 if (rpc_mount_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
3529 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3530 free_mount_cb_data(data
);
3535 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3537 struct mount_cb_data
*data
= private_data
;
3538 uint32_t mount_port
;
3540 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3542 if (status
== RPC_STATUS_ERROR
) {
3543 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3544 free_mount_cb_data(data
);
3547 if (status
== RPC_STATUS_CANCEL
) {
3548 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3549 free_mount_cb_data(data
);
3553 mount_port
= *(uint32_t *)command_data
;
3554 if (mount_port
== 0) {
3555 rpc_set_error(rpc
, "RPC error. Mount program is not available");
3556 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
3557 free_mount_cb_data(data
);
3561 rpc_disconnect(rpc
, "normal disconnect");
3562 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
3563 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3564 free_mount_cb_data(data
);
3569 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3571 struct mount_cb_data
*data
= private_data
;
3573 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3575 if (status
== RPC_STATUS_ERROR
) {
3576 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3577 free_mount_cb_data(data
);
3580 if (status
== RPC_STATUS_CANCEL
) {
3581 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3582 free_mount_cb_data(data
);
3586 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
3587 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3588 free_mount_cb_data(data
);
3593 static void mount_export_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3595 struct mount_cb_data
*data
= private_data
;
3597 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3599 /* Dont want any more callbacks even if the socket is closed */
3600 rpc
->connect_cb
= NULL
;
3602 if (status
== RPC_STATUS_ERROR
) {
3603 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3604 free_mount_cb_data(data
);
3607 if (status
== RPC_STATUS_CANCEL
) {
3608 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3609 free_mount_cb_data(data
);
3613 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
3614 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3615 free_mount_cb_data(data
);
3620 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
3622 struct mount_cb_data
*data
;
3624 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3626 data
= malloc(sizeof(struct mount_cb_data
));
3630 memset(data
, 0, sizeof(struct mount_cb_data
));
3632 data
->private_data
= private_data
;
3633 data
->server
= strdup(server
);
3634 if (data
->server
== NULL
) {
3635 free_mount_cb_data(data
);
3638 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
3639 free_mount_cb_data(data
);
3646 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
3648 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
3652 const char *nfs_get_server(struct nfs_context
*nfs
) {
3656 const char *nfs_get_export(struct nfs_context
*nfs
) {
3660 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
3661 return &nfs
->rootfh
;
3664 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {