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
21 #include "win32_compat.h"
25 #include <sys/statvfs.h>
38 #include <sys/types.h>
42 #include "libnfs-raw.h"
43 #include "libnfs-raw-mount.h"
44 #include "libnfs-raw-nfs.h"
45 #include "libnfs-private.h"
48 struct nfsdirent
*entries
;
49 struct nfsdirent
*current
;
59 struct rpc_context
*rpc
;
62 struct nfs_fh3 rootfh
;
67 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
69 while (nfsdir
->entries
) {
70 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
71 if (nfsdir
->entries
->name
!= NULL
) {
72 free(nfsdir
->entries
->name
);
74 free(nfsdir
->entries
);
75 nfsdir
->entries
= dirent
;
81 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
84 struct nfs_context
*nfs
;
86 char *saved_path
, *path
;
91 continue_func continue_cb
;
93 void (*free_continue_data
)(void *);
98 /* for multi-read/write calls. */
102 uint64_t start_offset
, max_offset
;
106 struct nfs_mcb_data
{
107 struct nfs_cb_data
*data
;
112 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
115 void nfs_set_auth(struct nfs_context
*nfs
, AUTH
*auth
)
117 rpc_set_auth(nfs
->rpc
, auth
);
120 int nfs_get_fd(struct nfs_context
*nfs
)
122 return rpc_get_fd(nfs
->rpc
);
125 int nfs_queue_length(struct nfs_context
*nfs
)
127 return rpc_queue_length(nfs
->rpc
);
130 int nfs_which_events(struct nfs_context
*nfs
)
132 return rpc_which_events(nfs
->rpc
);
135 int nfs_service(struct nfs_context
*nfs
, int revents
)
137 return rpc_service(nfs
->rpc
, revents
);
140 char *nfs_get_error(struct nfs_context
*nfs
)
142 return rpc_get_error(nfs
->rpc
);
145 struct nfs_context
*nfs_init_context(void)
147 struct nfs_context
*nfs
;
149 nfs
= malloc(sizeof(struct nfs_context
));
153 nfs
->rpc
= rpc_init_context();
154 if (nfs
->rpc
== NULL
) {
162 nfs
->rootfh
.data
.data_len
= 0;
163 nfs
->rootfh
.data
.data_val
= NULL
;
168 void nfs_destroy_context(struct nfs_context
*nfs
)
170 rpc_destroy_context(nfs
->rpc
);
183 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
184 free(nfs
->rootfh
.data
.data_val
);
185 nfs
->rootfh
.data
.data_val
= NULL
;
191 void free_nfs_cb_data(struct nfs_cb_data
*data
)
193 if (data
->saved_path
!= NULL
) {
194 free(data
->saved_path
);
195 data
->saved_path
= NULL
;
198 if (data
->continue_data
!= NULL
) {
199 data
->free_continue_data(data
->continue_data
);
200 data
->continue_data
= NULL
;
203 if (data
->fh
.data
.data_val
!= NULL
) {
204 free(data
->fh
.data
.data_val
);
205 data
->fh
.data
.data_val
= NULL
;
208 if (data
->buffer
!= NULL
) {
220 static void nfs_mount_10_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
222 struct nfs_cb_data
*data
= private_data
;
223 struct nfs_context
*nfs
= data
->nfs
;
225 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
227 if (status
== RPC_STATUS_ERROR
) {
228 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
229 free_nfs_cb_data(data
);
232 if (status
== RPC_STATUS_CANCEL
) {
233 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
234 free_nfs_cb_data(data
);
238 data
->cb(0, nfs
, NULL
, data
->private_data
);
239 free_nfs_cb_data(data
);
242 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
244 struct nfs_cb_data
*data
= private_data
;
245 struct nfs_context
*nfs
= data
->nfs
;
246 FSINFO3res
*res
= command_data
;
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 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
262 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
264 if (rpc_nfs_getattr_async(rpc
, nfs_mount_10_cb
, &nfs
->rootfh
, data
) != 0) {
265 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
266 free_nfs_cb_data(data
);
271 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
273 struct nfs_cb_data
*data
= private_data
;
274 struct nfs_context
*nfs
= data
->nfs
;
276 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
278 if (status
== RPC_STATUS_ERROR
) {
279 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
280 free_nfs_cb_data(data
);
283 if (status
== RPC_STATUS_CANCEL
) {
284 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
285 free_nfs_cb_data(data
);
289 if (rpc_nfs_fsinfo_async(rpc
, nfs_mount_9_cb
, &nfs
->rootfh
, data
) != 0) {
290 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
291 free_nfs_cb_data(data
);
297 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
299 struct nfs_cb_data
*data
= private_data
;
300 struct nfs_context
*nfs
= data
->nfs
;
302 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
304 /* Dont want any more callbacks even if the socket is closed */
305 rpc
->connect_cb
= NULL
;
307 if (status
== RPC_STATUS_ERROR
) {
308 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
309 free_nfs_cb_data(data
);
312 if (status
== RPC_STATUS_CANCEL
) {
313 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
314 free_nfs_cb_data(data
);
318 if (rpc_nfs_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
319 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
320 free_nfs_cb_data(data
);
326 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
328 struct nfs_cb_data
*data
= private_data
;
329 struct nfs_context
*nfs
= data
->nfs
;
332 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
334 if (status
== RPC_STATUS_ERROR
) {
335 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
336 free_nfs_cb_data(data
);
339 if (status
== RPC_STATUS_CANCEL
) {
340 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
341 free_nfs_cb_data(data
);
346 if (res
->fhs_status
!= MNT3_OK
) {
347 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
));
348 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
349 free_nfs_cb_data(data
);
353 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
354 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
355 if (nfs
->rootfh
.data
.data_val
== NULL
) {
356 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
357 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
358 free_nfs_cb_data(data
);
361 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
363 rpc_disconnect(rpc
, "normal disconnect");
364 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
365 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
366 free_nfs_cb_data(data
);
369 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
370 rpc_set_autoreconnect(rpc
);
374 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
376 struct nfs_cb_data
*data
= private_data
;
377 struct nfs_context
*nfs
= data
->nfs
;
379 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
381 if (status
== RPC_STATUS_ERROR
) {
382 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
383 free_nfs_cb_data(data
);
386 if (status
== RPC_STATUS_CANCEL
) {
387 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
388 free_nfs_cb_data(data
);
392 if (rpc_mount_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
393 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
394 free_nfs_cb_data(data
);
399 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
401 struct nfs_cb_data
*data
= private_data
;
402 struct nfs_context
*nfs
= data
->nfs
;
404 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
406 /* Dont want any more callbacks even if the socket is closed */
407 rpc
->connect_cb
= NULL
;
409 if (status
== RPC_STATUS_ERROR
) {
410 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
411 free_nfs_cb_data(data
);
414 if (status
== RPC_STATUS_CANCEL
) {
415 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
416 free_nfs_cb_data(data
);
420 if (rpc_mount_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
421 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
422 free_nfs_cb_data(data
);
427 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
429 struct nfs_cb_data
*data
= private_data
;
430 struct nfs_context
*nfs
= data
->nfs
;
433 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
435 if (status
== RPC_STATUS_ERROR
) {
436 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
437 free_nfs_cb_data(data
);
440 if (status
== RPC_STATUS_CANCEL
) {
441 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
442 free_nfs_cb_data(data
);
446 mount_port
= *(uint32_t *)command_data
;
447 if (mount_port
== 0) {
448 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
449 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
450 free_nfs_cb_data(data
);
454 rpc_disconnect(rpc
, "normal disconnect");
455 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
456 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
457 free_nfs_cb_data(data
);
463 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
465 struct nfs_cb_data
*data
= private_data
;
466 struct nfs_context
*nfs
= data
->nfs
;
468 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
470 if (status
== RPC_STATUS_ERROR
) {
471 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
472 free_nfs_cb_data(data
);
475 if (status
== RPC_STATUS_CANCEL
) {
476 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
477 free_nfs_cb_data(data
);
481 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, nfs_mount_3_cb
, private_data
) != 0) {
482 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
483 free_nfs_cb_data(data
);
488 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
490 struct nfs_cb_data
*data
= private_data
;
491 struct nfs_context
*nfs
= data
->nfs
;
493 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
495 /* Dont want any more callbacks even if the socket is closed */
496 rpc
->connect_cb
= NULL
;
498 if (status
== RPC_STATUS_ERROR
) {
499 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
500 free_nfs_cb_data(data
);
503 if (status
== RPC_STATUS_CANCEL
) {
504 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
505 free_nfs_cb_data(data
);
509 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
510 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
511 free_nfs_cb_data(data
);
517 * Async call for mounting an nfs share and geting the root filehandle
519 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
521 struct nfs_cb_data
*data
;
522 char *new_server
, *new_export
;
524 data
= malloc(sizeof(struct nfs_cb_data
));
526 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
529 memset(data
, 0, sizeof(struct nfs_cb_data
));
530 new_server
= strdup(server
);
531 new_export
= strdup(export
);
532 if (nfs
->server
!= NULL
) {
535 nfs
->server
= new_server
;
536 if (nfs
->export
!= NULL
) {
539 nfs
->export
= new_export
;
542 data
->private_data
= private_data
;
544 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
545 rpc_set_error(nfs
->rpc
, "Failed to start connection");
546 free_nfs_cb_data(data
);
556 * Functions to first look up a path, component by component, and then finally call a specific function once
557 * the filehandle for the final component is found.
559 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
561 struct nfs_cb_data
*data
= private_data
;
562 struct nfs_context
*nfs
= data
->nfs
;
565 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
567 if (status
== RPC_STATUS_ERROR
) {
568 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
569 free_nfs_cb_data(data
);
572 if (status
== RPC_STATUS_CANCEL
) {
573 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
574 free_nfs_cb_data(data
);
579 if (res
->status
!= NFS3_OK
) {
580 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
));
581 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
582 free_nfs_cb_data(data
);
586 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
587 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
588 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
589 free_nfs_cb_data(data
);
594 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
598 while (*data
->path
== '/') {
603 str
= strchr(path
, '/');
608 while (*data
->path
!= 0) {
614 data
->fh
.data
.data_len
= fh
->data
.data_len
;
615 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
616 if (data
->fh
.data
.data_val
== NULL
) {
617 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
618 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
619 free_nfs_cb_data(data
);
622 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
623 data
->continue_cb(nfs
, data
);
627 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, fh
, path
, data
) != 0) {
628 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
629 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
630 free_nfs_cb_data(data
);
636 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
)
638 struct nfs_cb_data
*data
;
640 if (path
[0] != '/') {
641 rpc_set_error(nfs
->rpc
, "Pathname is not absulute %s", path
);
645 data
= malloc(sizeof(struct nfs_cb_data
));
647 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
650 memset(data
, 0, sizeof(struct nfs_cb_data
));
653 data
->continue_cb
= continue_cb
;
654 data
->continue_data
= continue_data
;
655 data
->free_continue_data
= free_continue_data
;
656 data
->continue_int
= continue_int
;
657 data
->private_data
= private_data
;
658 data
->saved_path
= strdup(path
);
659 if (data
->saved_path
== NULL
) {
660 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
661 free_nfs_cb_data(data
);
664 data
->path
= data
->saved_path
;
666 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
667 /* return 0 here since the callback will be invoked if there is a failure */
680 static void nfs_stat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
683 struct nfs_cb_data
*data
= private_data
;
684 struct nfs_context
*nfs
= data
->nfs
;
687 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
689 if (status
== RPC_STATUS_ERROR
) {
690 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
691 free_nfs_cb_data(data
);
694 if (status
== RPC_STATUS_CANCEL
) {
695 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
696 free_nfs_cb_data(data
);
701 if (res
->status
!= NFS3_OK
) {
702 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
));
703 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
704 free_nfs_cb_data(data
);
709 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
710 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
711 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3DIR
) {
712 st
.st_mode
|= S_IFDIR
;
714 if (res
->GETATTR3res_u
.resok
.obj_attributes
.type
== NF3REG
) {
715 st
.st_mode
|= S_IFREG
;
717 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
718 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
719 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
721 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
723 st
.st_blksize
= 4096;
724 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
726 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
727 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
728 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
730 data
->cb(0, nfs
, &st
, data
->private_data
);
731 free_nfs_cb_data(data
);
734 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
736 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &data
->fh
, data
) != 0) {
737 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
738 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
739 free_nfs_cb_data(data
);
745 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
747 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
748 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
762 static void nfs_open_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
765 struct nfs_cb_data
*data
= private_data
;
766 struct nfs_context
*nfs
= data
->nfs
;
768 unsigned int nfsmode
= 0;
770 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
772 if (status
== RPC_STATUS_ERROR
) {
773 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
774 free_nfs_cb_data(data
);
777 if (status
== RPC_STATUS_CANCEL
) {
778 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
779 free_nfs_cb_data(data
);
784 if (res
->status
!= NFS3_OK
) {
785 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
));
786 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
787 free_nfs_cb_data(data
);
791 if (data
->continue_int
& O_WRONLY
) {
792 nfsmode
|= ACCESS3_MODIFY
;
794 if (data
->continue_int
& O_RDWR
) {
795 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
797 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
798 nfsmode
|= ACCESS3_READ
;
802 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
803 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
804 nfsmode
&ACCESS3_READ
?'r':'-',
805 nfsmode
&ACCESS3_MODIFY
?'w':'-',
806 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
807 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
808 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
809 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
810 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
811 free_nfs_cb_data(data
);
815 nfsfh
= malloc(sizeof(struct nfsfh
));
817 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
818 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
819 free_nfs_cb_data(data
);
822 memset(nfsfh
, 0, sizeof(struct nfsfh
));
824 if (data
->continue_int
& O_SYNC
) {
828 /* steal the filehandle */
829 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
830 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
831 data
->fh
.data
.data_val
= NULL
;
833 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
834 free_nfs_cb_data(data
);
837 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
841 if (data
->continue_int
& O_WRONLY
) {
842 nfsmode
|= ACCESS3_MODIFY
;
844 if (data
->continue_int
& O_RDWR
) {
845 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
847 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
848 nfsmode
|= ACCESS3_READ
;
851 if (rpc_nfs_access_async(nfs
->rpc
, nfs_open_cb
, &data
->fh
, nfsmode
, data
) != 0) {
852 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
853 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
854 free_nfs_cb_data(data
);
860 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
862 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
863 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
877 static void nfs_pread_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
879 struct nfs_cb_data
*data
= private_data
;
880 struct nfs_context
*nfs
= data
->nfs
;
883 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
885 if (status
== RPC_STATUS_ERROR
) {
886 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
887 free_nfs_cb_data(data
);
890 if (status
== RPC_STATUS_CANCEL
) {
891 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
892 free_nfs_cb_data(data
);
897 if (res
->status
!= NFS3_OK
) {
898 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
899 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
900 free_nfs_cb_data(data
);
904 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
905 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
906 free_nfs_cb_data(data
);
909 static void nfs_pread_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
911 struct nfs_mcb_data
*mdata
= private_data
;
912 struct nfs_cb_data
*data
= mdata
->data
;
913 struct nfs_context
*nfs
= data
->nfs
;
916 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
920 if (status
== RPC_STATUS_ERROR
) {
921 /* flag the failure but do not invoke callback until we have received all responses */
924 if (status
== RPC_STATUS_CANCEL
) {
925 /* flag the cancellation but do not invoke callback until we have received all responses */
929 /* reassemble the data into the buffer */
930 if (status
== RPC_STATUS_SUCCESS
) {
932 if (res
->status
!= NFS3_OK
) {
933 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
936 if (res
->READ3res_u
.resok
.count
> 0) {
937 memcpy(&data
->buffer
[mdata
->offset
- data
->start_offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
938 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
939 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
945 if (data
->num_calls
> 0) {
946 /* still waiting for more replies */
951 if (data
->error
!= 0) {
952 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
953 free_nfs_cb_data(data
);
957 if (data
->cancel
!= 0) {
958 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
959 free_nfs_cb_data(data
);
964 data
->nfsfh
->offset
= data
->max_offset
;
965 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
967 free_nfs_cb_data(data
);
971 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, nfs_cb cb
, void *private_data
)
973 struct nfs_cb_data
*data
;
975 data
= malloc(sizeof(struct nfs_cb_data
));
977 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
980 memset(data
, 0, sizeof(struct nfs_cb_data
));
983 data
->private_data
= private_data
;
986 nfsfh
->offset
= offset
;
988 if (count
<= nfs_get_readmax(nfs
)) {
989 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_cb
, &nfsfh
->fh
, offset
, count
, data
) != 0) {
990 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
991 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
992 free_nfs_cb_data(data
);
998 /* trying to read more than maximum server read size, we has to chop it up into smaller
999 * reads and collect into a reassembly buffer.
1000 * we send all reads in parallell so that performance is still good.
1002 data
->max_offset
= offset
;
1003 data
->start_offset
= offset
;
1005 data
->buffer
= malloc(count
);
1006 if (data
->buffer
== NULL
) {
1007 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
1008 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1009 free_nfs_cb_data(data
);
1014 uint64_t readcount
= count
;
1015 struct nfs_mcb_data
*mdata
;
1017 if (readcount
> nfs_get_readmax(nfs
)) {
1018 readcount
= nfs_get_readmax(nfs
);
1021 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1022 if (mdata
== NULL
) {
1023 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1026 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1028 mdata
->offset
= offset
;
1029 mdata
->count
= readcount
;
1030 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_mcb
, &nfsfh
->fh
, offset
, readcount
, mdata
) != 0) {
1031 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
1032 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1038 offset
+= readcount
;
1048 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, nfs_cb cb
, void *private_data
)
1050 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
1058 static void nfs_pwrite_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1060 struct nfs_cb_data
*data
= private_data
;
1061 struct nfs_context
*nfs
= data
->nfs
;
1064 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1066 if (status
== RPC_STATUS_ERROR
) {
1067 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1068 free_nfs_cb_data(data
);
1071 if (status
== RPC_STATUS_CANCEL
) {
1072 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1073 free_nfs_cb_data(data
);
1078 if (res
->status
!= NFS3_OK
) {
1079 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1080 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1081 free_nfs_cb_data(data
);
1085 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1086 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1087 free_nfs_cb_data(data
);
1090 static void nfs_pwrite_mcb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1092 struct nfs_mcb_data
*mdata
= private_data
;
1093 struct nfs_cb_data
*data
= mdata
->data
;
1094 struct nfs_context
*nfs
= data
->nfs
;
1097 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1101 if (status
== RPC_STATUS_ERROR
) {
1102 /* flag the failure but do not invoke callback until we have received all responses */
1105 if (status
== RPC_STATUS_CANCEL
) {
1106 /* flag the cancellation but do not invoke callback until we have received all responses */
1110 if (status
== RPC_STATUS_SUCCESS
) {
1112 if (res
->status
!= NFS3_OK
) {
1113 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1116 if (res
->WRITE3res_u
.resok
.count
> 0) {
1117 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->WRITE3res_u
.resok
.count
) {
1118 data
->max_offset
= mdata
->offset
+ res
->WRITE3res_u
.resok
.count
;
1124 if (data
->num_calls
> 0) {
1125 /* still waiting for more replies */
1130 if (data
->error
!= 0) {
1131 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1132 free_nfs_cb_data(data
);
1136 if (data
->cancel
!= 0) {
1137 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1138 free_nfs_cb_data(data
);
1143 data
->nfsfh
->offset
= data
->max_offset
;
1144 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, NULL
, data
->private_data
);
1146 free_nfs_cb_data(data
);
1151 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
)
1153 struct nfs_cb_data
*data
;
1155 data
= malloc(sizeof(struct nfs_cb_data
));
1157 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1160 memset(data
, 0, sizeof(struct nfs_cb_data
));
1163 data
->private_data
= private_data
;
1164 data
->nfsfh
= nfsfh
;
1166 nfsfh
->offset
= offset
;
1168 if (count
<= nfs_get_writemax(nfs
)) {
1169 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_cb
, &nfsfh
->fh
, buf
, offset
, count
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, data
) != 0) {
1170 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1171 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1172 free_nfs_cb_data(data
);
1178 /* trying to write more than maximum server write size, we has to chop it up into smaller
1180 * we send all writes in parallell so that performance is still good.
1182 data
->max_offset
= offset
;
1183 data
->start_offset
= offset
;
1186 uint64_t writecount
= count
;
1187 struct nfs_mcb_data
*mdata
;
1189 if (writecount
> nfs_get_writemax(nfs
)) {
1190 writecount
= nfs_get_writemax(nfs
);
1193 mdata
= malloc(sizeof(struct nfs_mcb_data
));
1194 if (mdata
== NULL
) {
1195 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
1198 memset(mdata
, 0, sizeof(struct nfs_mcb_data
));
1200 mdata
->offset
= offset
;
1201 mdata
->count
= writecount
;
1203 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) {
1204 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1205 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1210 count
-= writecount
;
1211 offset
+= writecount
;
1221 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1223 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1233 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1235 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1236 free(nfsfh
->fh
.data
.data_val
);
1237 nfsfh
->fh
.data
.data_val
= NULL
;
1241 cb(0, nfs
, NULL
, private_data
);
1252 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1254 struct nfs_cb_data
*data
;
1256 data
= malloc(sizeof(struct nfs_cb_data
));
1258 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1261 memset(data
, 0, sizeof(struct nfs_cb_data
));
1264 data
->private_data
= private_data
;
1266 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &nfsfh
->fh
, data
) != 0) {
1267 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1268 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1269 free_nfs_cb_data(data
);
1280 static void nfs_fsync_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1282 struct nfs_cb_data
*data
= private_data
;
1283 struct nfs_context
*nfs
= data
->nfs
;
1286 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1288 if (status
== RPC_STATUS_ERROR
) {
1289 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1290 free_nfs_cb_data(data
);
1293 if (status
== RPC_STATUS_CANCEL
) {
1294 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1295 free_nfs_cb_data(data
);
1300 if (res
->status
!= NFS3_OK
) {
1301 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1302 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1303 free_nfs_cb_data(data
);
1307 data
->cb(0, nfs
, NULL
, data
->private_data
);
1308 free_nfs_cb_data(data
);
1311 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1313 struct nfs_cb_data
*data
;
1315 data
= malloc(sizeof(struct nfs_cb_data
));
1317 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1320 memset(data
, 0, sizeof(struct nfs_cb_data
));
1323 data
->private_data
= private_data
;
1325 if (rpc_nfs_commit_async(nfs
->rpc
, nfs_fsync_cb
, &nfsfh
->fh
, data
) != 0) {
1326 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1327 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1328 free_nfs_cb_data(data
);
1340 static void nfs_ftruncate_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1342 struct nfs_cb_data
*data
= private_data
;
1343 struct nfs_context
*nfs
= data
->nfs
;
1346 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1348 if (status
== RPC_STATUS_ERROR
) {
1349 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1350 free_nfs_cb_data(data
);
1353 if (status
== RPC_STATUS_CANCEL
) {
1354 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1355 free_nfs_cb_data(data
);
1360 if (res
->status
!= NFS3_OK
) {
1361 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1362 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1363 free_nfs_cb_data(data
);
1367 data
->cb(0, nfs
, NULL
, data
->private_data
);
1368 free_nfs_cb_data(data
);
1371 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
, nfs_cb cb
, void *private_data
)
1373 struct nfs_cb_data
*data
;
1376 data
= malloc(sizeof(struct nfs_cb_data
));
1378 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1381 memset(data
, 0, sizeof(struct nfs_cb_data
));
1384 data
->private_data
= private_data
;
1386 memset(&args
, 0, sizeof(SETATTR3args
));
1387 args
.object
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
1388 args
.object
.data
.data_val
= nfsfh
->fh
.data
.data_val
;
1389 args
.new_attributes
.size
.set_it
= 1;
1390 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1392 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1393 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1394 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1395 free_nfs_cb_data(data
);
1405 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1407 uint64_t offset
= data
->continue_int
;
1410 nfsfh
.fh
.data
.data_val
= data
->fh
.data
.data_val
;
1411 nfsfh
.fh
.data
.data_len
= data
->fh
.data
.data_len
;
1413 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1414 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1415 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1416 free_nfs_cb_data(data
);
1419 free_nfs_cb_data(data
);
1423 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, uint64_t length
, nfs_cb cb
, void *private_data
)
1429 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1430 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1443 static void nfs_mkdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1446 struct nfs_cb_data
*data
= private_data
;
1447 struct nfs_context
*nfs
= data
->nfs
;
1448 char *str
= data
->continue_data
;
1450 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1452 str
= &str
[strlen(str
) + 1];
1454 if (status
== RPC_STATUS_ERROR
) {
1455 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1456 free_nfs_cb_data(data
);
1459 if (status
== RPC_STATUS_CANCEL
) {
1460 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1461 free_nfs_cb_data(data
);
1466 if (res
->status
!= NFS3_OK
) {
1467 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
));
1468 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1469 free_nfs_cb_data(data
);
1473 data
->cb(0, nfs
, NULL
, data
->private_data
);
1474 free_nfs_cb_data(data
);
1477 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1479 char *str
= data
->continue_data
;
1482 str
= &str
[strlen(str
) + 1];
1484 memset(&args
, 0, sizeof(MKDIR3args
));
1485 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1486 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1487 args
.where
.name
= str
;
1488 args
.attributes
.mode
.set_it
= 1;
1489 args
.attributes
.mode
.set_mode3_u
.mode
= 0755;
1491 if (rpc_nfs_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &args
, data
) != 0) {
1492 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1493 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1494 free_nfs_cb_data(data
);
1500 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1505 new_path
= strdup(path
);
1506 if (new_path
== NULL
) {
1507 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1511 ptr
= strrchr(new_path
, '/');
1513 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1518 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1519 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1520 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1534 static void nfs_rmdir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1537 struct nfs_cb_data
*data
= private_data
;
1538 struct nfs_context
*nfs
= data
->nfs
;
1539 char *str
= data
->continue_data
;
1541 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1543 str
= &str
[strlen(str
) + 1];
1545 if (status
== RPC_STATUS_ERROR
) {
1546 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1547 free_nfs_cb_data(data
);
1550 if (status
== RPC_STATUS_CANCEL
) {
1551 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1552 free_nfs_cb_data(data
);
1557 if (res
->status
!= NFS3_OK
) {
1558 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
));
1559 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1560 free_nfs_cb_data(data
);
1564 data
->cb(0, nfs
, NULL
, data
->private_data
);
1565 free_nfs_cb_data(data
);
1568 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1570 char *str
= data
->continue_data
;
1572 str
= &str
[strlen(str
) + 1];
1574 if (rpc_nfs_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &data
->fh
, str
, data
) != 0) {
1575 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1576 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1577 free_nfs_cb_data(data
);
1583 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1588 new_path
= strdup(path
);
1589 if (new_path
== NULL
) {
1590 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1594 ptr
= strrchr(new_path
, '/');
1596 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1601 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1602 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1603 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1616 static void nfs_create_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1619 struct nfs_cb_data
*data
= private_data
;
1620 struct nfs_context
*nfs
= data
->nfs
;
1621 struct nfsfh
*nfsfh
;
1622 char *str
= data
->continue_data
;
1624 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1626 if (status
== RPC_STATUS_ERROR
) {
1627 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1628 free_nfs_cb_data(data
);
1631 if (status
== RPC_STATUS_CANCEL
) {
1632 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1633 free_nfs_cb_data(data
);
1637 str
= &str
[strlen(str
) + 1];
1639 if (res
->status
!= NFS3_OK
) {
1640 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
));
1641 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1646 nfsfh
= malloc(sizeof(struct nfsfh
));
1647 if (nfsfh
== NULL
) {
1648 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1649 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1650 free_nfs_cb_data(data
);
1653 memset(nfsfh
, 0, sizeof(struct nfsfh
));
1655 /* steal the filehandle */
1656 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
1657 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
1658 data
->fh
.data
.data_val
= NULL
;
1660 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1661 free_nfs_cb_data(data
);
1666 static void nfs_creat_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1669 struct nfs_cb_data
*data
= private_data
;
1670 struct nfs_context
*nfs
= data
->nfs
;
1671 char *str
= data
->continue_data
;
1673 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1675 if (status
== RPC_STATUS_ERROR
) {
1676 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1677 free_nfs_cb_data(data
);
1680 if (status
== RPC_STATUS_CANCEL
) {
1681 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1682 free_nfs_cb_data(data
);
1686 str
= &str
[strlen(str
) + 1];
1688 if (res
->status
!= NFS3_OK
) {
1689 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
));
1690 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1695 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &data
->fh
, str
, data
) != 0) {
1696 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
1697 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1698 free_nfs_cb_data(data
);
1704 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1706 char *str
= data
->continue_data
;
1709 str
= &str
[strlen(str
) + 1];
1712 memset(&args
, 0, sizeof(CREATE3args
));
1713 args
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
1714 args
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
1715 args
.where
.name
= str
;
1716 args
.how
.mode
= UNCHECKED
;
1717 args
.how
.createhow3_u
.obj_attributes
.mode
.set_it
= 1;
1718 args
.how
.createhow3_u
.obj_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
1720 if (rpc_nfs_create_async(nfs
->rpc
, nfs_creat_1_cb
, &args
, data
) != 0) {
1721 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
1722 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1723 free_nfs_cb_data(data
);
1729 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1734 new_path
= strdup(path
);
1735 if (new_path
== NULL
) {
1736 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1740 ptr
= strrchr(new_path
, '/');
1742 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1747 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1748 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
1749 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1762 static void nfs_unlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1765 struct nfs_cb_data
*data
= private_data
;
1766 struct nfs_context
*nfs
= data
->nfs
;
1767 char *str
= data
->continue_data
;
1769 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1771 str
= &str
[strlen(str
) + 1];
1773 if (status
== RPC_STATUS_ERROR
) {
1774 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1775 free_nfs_cb_data(data
);
1778 if (status
== RPC_STATUS_CANCEL
) {
1779 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1780 free_nfs_cb_data(data
);
1785 if (res
->status
!= NFS3_OK
) {
1786 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
));
1787 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1788 free_nfs_cb_data(data
);
1792 data
->cb(0, nfs
, NULL
, data
->private_data
);
1793 free_nfs_cb_data(data
);
1796 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1798 char *str
= data
->continue_data
;
1800 str
= &str
[strlen(str
) + 1];
1802 if (rpc_nfs_remove_async(nfs
->rpc
, nfs_unlink_cb
, &data
->fh
, str
, data
) != 0) {
1803 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
1804 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1805 free_nfs_cb_data(data
);
1811 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1816 new_path
= strdup(path
);
1817 if (new_path
== NULL
) {
1818 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1822 ptr
= strrchr(new_path
, '/');
1824 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1829 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1830 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
1831 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1842 struct mknod_cb_data
{
1849 static void free_mknod_cb_data(void *ptr
)
1851 struct mknod_cb_data
*data
= ptr
;
1857 static void nfs_mknod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1860 struct nfs_cb_data
*data
= private_data
;
1861 struct nfs_context
*nfs
= data
->nfs
;
1862 char *str
= data
->continue_data
;
1864 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1866 str
= &str
[strlen(str
) + 1];
1868 if (status
== RPC_STATUS_ERROR
) {
1869 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1870 free_nfs_cb_data(data
);
1873 if (status
== RPC_STATUS_CANCEL
) {
1874 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1875 free_nfs_cb_data(data
);
1880 if (res
->status
!= NFS3_OK
) {
1881 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
));
1882 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1883 free_nfs_cb_data(data
);
1887 data
->cb(0, nfs
, NULL
, data
->private_data
);
1888 free_nfs_cb_data(data
);
1891 static int nfs_mknod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1893 struct mknod_cb_data
*cb_data
= data
->continue_data
;
1894 char *str
= cb_data
->path
;
1896 str
= &str
[strlen(str
) + 1];
1898 if (rpc_nfs_mknod_async(nfs
->rpc
, nfs_mknod_cb
, &data
->fh
, str
, cb_data
->mode
, cb_data
->major
, cb_data
->minor
, data
) != 0) {
1899 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1900 free_nfs_cb_data(data
);
1906 int nfs_mknod_async(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
, nfs_cb cb
, void *private_data
)
1909 struct mknod_cb_data
*cb_data
;
1911 cb_data
= malloc(sizeof(struct mknod_cb_data
));
1912 if (cb_data
== NULL
) {
1913 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for cb data");
1917 cb_data
->path
= strdup(path
);
1918 if (cb_data
->path
== NULL
) {
1919 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1924 ptr
= strrchr(cb_data
->path
, '/');
1926 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1931 cb_data
->mode
= mode
;
1932 cb_data
->major
= major(dev
);
1933 cb_data
->minor
= minor(dev
);
1935 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1936 if (nfs_lookuppath_async(nfs
, cb_data
->path
, cb
, private_data
, nfs_mknod_continue_internal
, cb_data
, free_mknod_cb_data
, 0) != 0) {
1937 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1938 free_mknod_cb_data(cb_data
);
1949 /* ReadDirPlus Emulation Callback data */
1950 struct rdpe_cb_data
{
1953 struct nfs_cb_data
*data
;
1956 /* ReadDirPlus Emulation LOOKUP Callback data */
1957 struct rdpe_lookup_cb_data
{
1958 struct rdpe_cb_data
*rdpe_cb_data
;
1959 struct nfsdirent
*nfsdirent
;
1962 /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
1963 static void nfs_opendir3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
1965 LOOKUP3res
*res
= command_data
;
1966 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
= private_data
;
1967 struct rdpe_cb_data
*rdpe_cb_data
= rdpe_lookup_cb_data
->rdpe_cb_data
;
1968 struct nfs_cb_data
*data
= rdpe_cb_data
->data
;
1969 struct nfsdir
*nfsdir
= data
->continue_data
;
1970 struct nfs_context
*nfs
= data
->nfs
;
1971 struct nfsdirent
*nfsdirent
= rdpe_lookup_cb_data
->nfsdirent
;
1973 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1975 free(rdpe_lookup_cb_data
);
1977 rdpe_cb_data
->getattrcount
--;
1979 if (status
== RPC_STATUS_ERROR
) {
1980 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
1982 if (status
== RPC_STATUS_CANCEL
) {
1983 rdpe_cb_data
->status
= RPC_STATUS_CANCEL
;
1985 if (status
== RPC_STATUS_SUCCESS
&& res
->status
!= NFS3_OK
) {
1986 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
1988 if (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3_OK
) {
1989 if (res
->LOOKUP3res_u
.resok
.obj_attributes
.attributes_follow
) {
1990 fattr3
*attributes
= &res
->LOOKUP3res_u
.resok
.obj_attributes
.post_op_attr_u
.attributes
;
1992 nfsdirent
->type
= attributes
->type
;
1993 nfsdirent
->mode
= attributes
->mode
;
1994 nfsdirent
->size
= attributes
->size
;
1996 nfsdirent
->atime
.tv_sec
= attributes
->atime
.seconds
;
1997 nfsdirent
->atime
.tv_usec
= attributes
->atime
.nseconds
/1000;
1998 nfsdirent
->mtime
.tv_sec
= attributes
->mtime
.seconds
;
1999 nfsdirent
->mtime
.tv_usec
= attributes
->mtime
.nseconds
/1000;
2000 nfsdirent
->ctime
.tv_sec
= attributes
->ctime
.seconds
;
2001 nfsdirent
->ctime
.tv_usec
= attributes
->ctime
.nseconds
/1000;
2005 if (rdpe_cb_data
->getattrcount
== 0) {
2006 if (rdpe_cb_data
->status
!= RPC_STATUS_SUCCESS
) {
2007 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2008 nfs_free_nfsdir(nfsdir
);
2010 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2014 data
->continue_data
= NULL
;
2015 free_nfs_cb_data(data
);
2019 static void nfs_opendir2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2021 READDIR3res
*res
= command_data
;
2022 struct nfs_cb_data
*data
= private_data
;
2023 struct nfs_context
*nfs
= data
->nfs
;
2024 struct nfsdir
*nfsdir
= data
->continue_data
;
2025 struct nfsdirent
*nfsdirent
;
2026 struct entry3
*entry
;
2028 struct rdpe_cb_data
*rdpe_cb_data
;
2030 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2032 if (status
== RPC_STATUS_ERROR
) {
2033 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2034 nfs_free_nfsdir(nfsdir
);
2035 data
->continue_data
= NULL
;
2036 free_nfs_cb_data(data
);
2040 if (status
== RPC_STATUS_CANCEL
) {
2041 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2042 nfs_free_nfsdir(nfsdir
);
2043 data
->continue_data
= NULL
;
2044 free_nfs_cb_data(data
);
2048 if (res
->status
!= NFS3_OK
) {
2049 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
));
2050 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2051 nfs_free_nfsdir(nfsdir
);
2052 data
->continue_data
= NULL
;
2053 free_nfs_cb_data(data
);
2057 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
2058 while (entry
!= NULL
) {
2059 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2060 if (nfsdirent
== NULL
) {
2061 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2062 nfs_free_nfsdir(nfsdir
);
2063 data
->continue_data
= NULL
;
2064 free_nfs_cb_data(data
);
2067 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2068 nfsdirent
->name
= strdup(entry
->name
);
2069 if (nfsdirent
->name
== NULL
) {
2070 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2071 nfs_free_nfsdir(nfsdir
);
2072 data
->continue_data
= NULL
;
2073 free_nfs_cb_data(data
);
2076 nfsdirent
->inode
= entry
->fileid
;
2078 nfsdirent
->next
= nfsdir
->entries
;
2079 nfsdir
->entries
= nfsdirent
;
2081 cookie
= entry
->cookie
;
2082 entry
= entry
->nextentry
;
2085 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
2086 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, cookie
, res
->READDIR3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2087 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2088 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2089 nfs_free_nfsdir(nfsdir
);
2090 data
->continue_data
= NULL
;
2091 free_nfs_cb_data(data
);
2097 /* steal the dirhandle */
2098 nfsdir
->current
= nfsdir
->entries
;
2100 rdpe_cb_data
= malloc(sizeof(struct rdpe_cb_data
));
2101 rdpe_cb_data
->getattrcount
= 0;
2102 rdpe_cb_data
->status
= RPC_STATUS_SUCCESS
;
2103 rdpe_cb_data
->data
= data
;
2104 for (nfsdirent
= nfsdir
->entries
; nfsdirent
; nfsdirent
= nfsdirent
->next
) {
2105 struct rdpe_lookup_cb_data
*rdpe_lookup_cb_data
;
2107 rdpe_lookup_cb_data
= malloc(sizeof(struct rdpe_lookup_cb_data
));
2108 rdpe_lookup_cb_data
->rdpe_cb_data
= rdpe_cb_data
;
2109 rdpe_lookup_cb_data
->nfsdirent
= nfsdirent
;
2111 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_opendir3_cb
, &data
->fh
, nfsdirent
->name
, rdpe_lookup_cb_data
) != 0) {
2112 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR LOOKUP call");
2114 /* if we have already commands in flight, we cant just stop, we have to wait for the
2115 * commands in flight to complete
2117 if (rdpe_cb_data
->getattrcount
> 0) {
2118 rdpe_cb_data
->status
= RPC_STATUS_ERROR
;
2119 free(rdpe_lookup_cb_data
);
2123 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2124 nfs_free_nfsdir(nfsdir
);
2125 data
->continue_data
= NULL
;
2126 free_nfs_cb_data(data
);
2127 free(rdpe_lookup_cb_data
);
2131 rdpe_cb_data
->getattrcount
++;
2136 static void nfs_opendir_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2138 READDIRPLUS3res
*res
= command_data
;
2139 struct nfs_cb_data
*data
= private_data
;
2140 struct nfs_context
*nfs
= data
->nfs
;
2141 struct nfsdir
*nfsdir
= data
->continue_data
;
2142 struct entryplus3
*entry
;
2145 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2147 if (status
== RPC_STATUS_ERROR
|| (status
== RPC_STATUS_SUCCESS
&& res
->status
== NFS3ERR_NOTSUPP
) ){
2150 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir2_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2151 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
2152 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2153 nfs_free_nfsdir(nfsdir
);
2154 data
->continue_data
= NULL
;
2155 free_nfs_cb_data(data
);
2161 if (status
== RPC_STATUS_CANCEL
) {
2162 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2163 nfs_free_nfsdir(nfsdir
);
2164 data
->continue_data
= NULL
;
2165 free_nfs_cb_data(data
);
2169 if (res
->status
!= NFS3_OK
) {
2170 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
));
2171 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2172 nfs_free_nfsdir(nfsdir
);
2173 data
->continue_data
= NULL
;
2174 free_nfs_cb_data(data
);
2178 entry
=res
->READDIRPLUS3res_u
.resok
.reply
.entries
;
2179 while (entry
!= NULL
) {
2180 struct nfsdirent
*nfsdirent
;
2182 nfsdirent
= malloc(sizeof(struct nfsdirent
));
2183 if (nfsdirent
== NULL
) {
2184 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
2185 nfs_free_nfsdir(nfsdir
);
2186 data
->continue_data
= NULL
;
2187 free_nfs_cb_data(data
);
2190 memset(nfsdirent
, 0, sizeof(struct nfsdirent
));
2191 nfsdirent
->name
= strdup(entry
->name
);
2192 if (nfsdirent
->name
== NULL
) {
2193 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
2194 nfs_free_nfsdir(nfsdir
);
2195 data
->continue_data
= NULL
;
2196 free_nfs_cb_data(data
);
2199 nfsdirent
->inode
= entry
->fileid
;
2200 if (entry
->name_attributes
.attributes_follow
) {
2201 nfsdirent
->type
= entry
->name_attributes
.post_op_attr_u
.attributes
.type
;
2202 nfsdirent
->mode
= entry
->name_attributes
.post_op_attr_u
.attributes
.mode
;
2203 nfsdirent
->size
= entry
->name_attributes
.post_op_attr_u
.attributes
.size
;
2205 nfsdirent
->atime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.seconds
;
2206 nfsdirent
->atime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.atime
.nseconds
/1000;
2207 nfsdirent
->mtime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.seconds
;
2208 nfsdirent
->mtime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.mtime
.nseconds
/1000;
2209 nfsdirent
->ctime
.tv_sec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.seconds
;
2210 nfsdirent
->ctime
.tv_usec
= entry
->name_attributes
.post_op_attr_u
.attributes
.ctime
.nseconds
/1000;
2213 nfsdirent
->next
= nfsdir
->entries
;
2214 nfsdir
->entries
= nfsdirent
;
2216 cookie
= entry
->cookie
;
2217 entry
= entry
->nextentry
;
2220 if (res
->READDIRPLUS3res_u
.resok
.reply
.eof
== 0) {
2221 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, cookie
, res
->READDIRPLUS3res_u
.resok
.cookieverf
, 8192, data
) != 0) {
2222 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2223 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2224 nfs_free_nfsdir(nfsdir
);
2225 data
->continue_data
= NULL
;
2226 free_nfs_cb_data(data
);
2232 /* steal the dirhandle */
2233 data
->continue_data
= NULL
;
2234 nfsdir
->current
= nfsdir
->entries
;
2236 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
2237 free_nfs_cb_data(data
);
2240 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2244 memset(cv
, 0, sizeof(cookieverf3
));
2245 if (rpc_nfs_readdirplus_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, 0, (char *)&cv
, 8192, data
) != 0) {
2246 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIRPLUS call for %s", data
->path
);
2247 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2248 free_nfs_cb_data(data
);
2254 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2256 struct nfsdir
*nfsdir
;
2258 nfsdir
= malloc(sizeof(struct nfsdir
));
2259 if (nfsdir
== NULL
) {
2260 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
2263 memset(nfsdir
, 0, sizeof(struct nfsdir
));
2265 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
2266 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2274 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2276 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
2278 if (nfsdir
->current
!= NULL
) {
2279 nfsdir
->current
= nfsdir
->current
->next
;
2285 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
2287 nfs_free_nfsdir(nfsdir
);
2299 struct lseek_cb_data
{
2300 struct nfs_context
*nfs
;
2301 struct nfsfh
*nfsfh
;
2307 static void nfs_lseek_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2310 struct lseek_cb_data
*data
= private_data
;
2311 struct nfs_context
*nfs
= data
->nfs
;
2313 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2315 if (status
== RPC_STATUS_ERROR
) {
2316 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2320 if (status
== RPC_STATUS_CANCEL
) {
2321 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2327 if (res
->status
!= NFS3_OK
) {
2328 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2329 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2334 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
2335 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
2339 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, nfs_cb cb
, void *private_data
)
2341 struct lseek_cb_data
*data
;
2343 if (whence
== SEEK_SET
) {
2344 nfsfh
->offset
= offset
;
2345 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2348 if (whence
== SEEK_CUR
) {
2349 nfsfh
->offset
+= offset
;
2350 cb(0, nfs
, &nfsfh
->offset
, private_data
);
2354 data
= malloc(sizeof(struct lseek_cb_data
));
2356 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
2361 data
->nfsfh
= nfsfh
;
2362 data
->offset
= offset
;
2364 data
->private_data
= private_data
;
2366 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &nfsfh
->fh
, data
) != 0) {
2367 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
2380 static void nfs_statvfs_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2383 struct nfs_cb_data
*data
= private_data
;
2384 struct nfs_context
*nfs
= data
->nfs
;
2385 struct statvfs svfs
;
2387 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2389 if (status
== RPC_STATUS_ERROR
) {
2390 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2391 free_nfs_cb_data(data
);
2394 if (status
== RPC_STATUS_CANCEL
) {
2395 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2396 free_nfs_cb_data(data
);
2401 if (res
->status
!= NFS3_OK
) {
2402 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
));
2403 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2404 free_nfs_cb_data(data
);
2408 svfs
.f_bsize
= 4096;
2409 svfs
.f_frsize
= 4096;
2410 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
2411 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
2412 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
2413 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
2414 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
2415 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
2418 svfs
.f_namemax
= 256;
2420 data
->cb(0, nfs
, &svfs
, data
->private_data
);
2421 free_nfs_cb_data(data
);
2424 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2426 if (rpc_nfs_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &data
->fh
, data
) != 0) {
2427 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
2428 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2429 free_nfs_cb_data(data
);
2435 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2437 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
2438 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2451 static void nfs_readlink_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2454 struct nfs_cb_data
*data
= private_data
;
2455 struct nfs_context
*nfs
= data
->nfs
;
2457 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2459 if (status
== RPC_STATUS_ERROR
) {
2460 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2461 free_nfs_cb_data(data
);
2464 if (status
== RPC_STATUS_CANCEL
) {
2465 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2466 free_nfs_cb_data(data
);
2471 if (res
->status
!= NFS3_OK
) {
2472 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
));
2473 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2474 free_nfs_cb_data(data
);
2479 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
2480 free_nfs_cb_data(data
);
2483 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2487 args
.symlink
.data
.data_len
= data
->fh
.data
.data_len
;
2488 args
.symlink
.data
.data_val
= data
->fh
.data
.data_val
;
2490 if (rpc_nfs_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &args
, data
) != 0) {
2491 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
2492 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2493 free_nfs_cb_data(data
);
2499 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
2501 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
2502 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2515 static void nfs_chmod_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2517 struct nfs_cb_data
*data
= private_data
;
2518 struct nfs_context
*nfs
= data
->nfs
;
2521 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2523 if (status
== RPC_STATUS_ERROR
) {
2524 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2525 free_nfs_cb_data(data
);
2528 if (status
== RPC_STATUS_CANCEL
) {
2529 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2530 free_nfs_cb_data(data
);
2535 if (res
->status
!= NFS3_OK
) {
2536 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2537 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2538 free_nfs_cb_data(data
);
2542 data
->cb(0, nfs
, NULL
, data
->private_data
);
2543 free_nfs_cb_data(data
);
2546 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2550 memset(&args
, 0, sizeof(SETATTR3args
));
2551 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2552 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2553 args
.new_attributes
.mode
.set_it
= 1;
2554 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2556 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
2557 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2558 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2559 free_nfs_cb_data(data
);
2566 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2568 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
2569 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2579 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
2581 struct nfs_cb_data
*data
;
2583 data
= malloc(sizeof(struct nfs_cb_data
));
2585 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
2588 memset(data
, 0, sizeof(struct nfs_cb_data
));
2591 data
->private_data
= private_data
;
2592 data
->continue_int
= mode
;
2593 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2594 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2595 if (data
->fh
.data
.data_val
== NULL
) {
2596 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2597 free_nfs_cb_data(data
);
2600 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2602 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
2603 free_nfs_cb_data(data
);
2615 static void nfs_chown_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2617 struct nfs_cb_data
*data
= private_data
;
2618 struct nfs_context
*nfs
= data
->nfs
;
2621 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2623 if (status
== RPC_STATUS_ERROR
) {
2624 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2625 free_nfs_cb_data(data
);
2628 if (status
== RPC_STATUS_CANCEL
) {
2629 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2630 free_nfs_cb_data(data
);
2635 if (res
->status
!= NFS3_OK
) {
2636 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2637 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2638 free_nfs_cb_data(data
);
2642 data
->cb(0, nfs
, NULL
, data
->private_data
);
2643 free_nfs_cb_data(data
);
2646 struct nfs_chown_data
{
2651 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2654 struct nfs_chown_data
*chown_data
= data
->continue_data
;
2656 memset(&args
, 0, sizeof(SETATTR3args
));
2657 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2658 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2659 if (chown_data
->uid
!= (uid_t
)-1) {
2660 args
.new_attributes
.uid
.set_it
= 1;
2661 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
2663 if (chown_data
->gid
!= (gid_t
)-1) {
2664 args
.new_attributes
.gid
.set_it
= 1;
2665 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
2668 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
2669 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2670 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2671 free_nfs_cb_data(data
);
2678 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2680 struct nfs_chown_data
*chown_data
;
2682 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2683 if (chown_data
== NULL
) {
2684 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2688 chown_data
->uid
= uid
;
2689 chown_data
->gid
= gid
;
2691 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
2692 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2703 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2705 struct nfs_cb_data
*data
;
2706 struct nfs_chown_data
*chown_data
;
2708 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2709 if (chown_data
== NULL
) {
2710 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2714 chown_data
->uid
= uid
;
2715 chown_data
->gid
= gid
;
2718 data
= malloc(sizeof(struct nfs_cb_data
));
2720 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
2723 memset(data
, 0, sizeof(struct nfs_cb_data
));
2726 data
->private_data
= private_data
;
2727 data
->continue_data
= chown_data
;
2728 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2729 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2730 if (data
->fh
.data
.data_val
== NULL
) {
2731 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2732 free_nfs_cb_data(data
);
2735 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2738 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
2739 free_nfs_cb_data(data
);
2753 static void nfs_utimes_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2755 struct nfs_cb_data
*data
= private_data
;
2756 struct nfs_context
*nfs
= data
->nfs
;
2759 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2761 if (status
== RPC_STATUS_ERROR
) {
2762 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2763 free_nfs_cb_data(data
);
2766 if (status
== RPC_STATUS_CANCEL
) {
2767 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2768 free_nfs_cb_data(data
);
2773 if (res
->status
!= NFS3_OK
) {
2774 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2775 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2776 free_nfs_cb_data(data
);
2780 data
->cb(0, nfs
, NULL
, data
->private_data
);
2781 free_nfs_cb_data(data
);
2784 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2787 struct timeval
*utimes_data
= data
->continue_data
;
2789 memset(&args
, 0, sizeof(SETATTR3args
));
2790 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2791 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2792 if (utimes_data
!= NULL
) {
2793 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
2794 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
2795 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
2796 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
2797 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
2798 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
2800 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
2801 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
2804 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
2805 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2806 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2807 free_nfs_cb_data(data
);
2814 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
2816 struct timeval
*new_times
= NULL
;
2818 if (times
!= NULL
) {
2819 new_times
= malloc(sizeof(struct timeval
)*2);
2820 if (new_times
== NULL
) {
2821 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2825 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
2828 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2829 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2839 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
2841 struct timeval
*new_times
= NULL
;
2843 if (times
!= NULL
) {
2844 new_times
= malloc(sizeof(struct timeval
)*2);
2845 if (new_times
== NULL
) {
2846 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2850 new_times
[0].tv_sec
= times
->actime
;
2851 new_times
[0].tv_usec
= 0;
2852 new_times
[1].tv_sec
= times
->modtime
;
2853 new_times
[1].tv_usec
= 0;
2856 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2857 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2871 static void nfs_access_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2874 struct nfs_cb_data
*data
= private_data
;
2875 struct nfs_context
*nfs
= data
->nfs
;
2876 unsigned int nfsmode
= 0;
2878 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2880 if (status
== RPC_STATUS_ERROR
) {
2881 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2882 free_nfs_cb_data(data
);
2885 if (status
== RPC_STATUS_CANCEL
) {
2886 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2887 free_nfs_cb_data(data
);
2892 if (res
->status
!= NFS3_OK
) {
2893 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
));
2894 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2895 free_nfs_cb_data(data
);
2899 if (data
->continue_int
& R_OK
) {
2900 nfsmode
|= ACCESS3_READ
;
2902 if (data
->continue_int
& W_OK
) {
2903 nfsmode
|= ACCESS3_MODIFY
;
2905 if (data
->continue_int
& X_OK
) {
2906 nfsmode
|= ACCESS3_EXECUTE
;
2909 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
2910 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2911 nfsmode
&ACCESS3_READ
?'r':'-',
2912 nfsmode
&ACCESS3_MODIFY
?'w':'-',
2913 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
2914 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
2915 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
2916 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
2917 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2918 free_nfs_cb_data(data
);
2922 data
->cb(0, nfs
, NULL
, data
->private_data
);
2923 free_nfs_cb_data(data
);
2926 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2930 if (data
->continue_int
& R_OK
) {
2931 nfsmode
|= ACCESS3_READ
;
2933 if (data
->continue_int
& W_OK
) {
2934 nfsmode
|= ACCESS3_MODIFY
;
2936 if (data
->continue_int
& X_OK
) {
2937 nfsmode
|= ACCESS3_EXECUTE
;
2940 if (rpc_nfs_access_async(nfs
->rpc
, nfs_access_cb
, &data
->fh
, nfsmode
, data
) != 0) {
2941 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
2942 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2943 free_nfs_cb_data(data
);
2949 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2951 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
2952 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2964 struct nfs_symlink_data
{
2966 char *newpathparent
;
2967 char *newpathobject
;
2970 static void free_nfs_symlink_data(void *mem
)
2972 struct nfs_symlink_data
*data
= mem
;
2974 if (data
->oldpath
!= NULL
) {
2975 free(data
->oldpath
);
2977 if (data
->newpathparent
!= NULL
) {
2978 free(data
->newpathparent
);
2980 if (data
->newpathobject
!= NULL
) {
2981 free(data
->newpathobject
);
2986 static void nfs_symlink_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
2989 struct nfs_cb_data
*data
= private_data
;
2990 struct nfs_context
*nfs
= data
->nfs
;
2991 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
2993 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
2995 if (status
== RPC_STATUS_ERROR
) {
2996 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2997 free_nfs_cb_data(data
);
3000 if (status
== RPC_STATUS_CANCEL
) {
3001 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3002 free_nfs_cb_data(data
);
3007 if (res
->status
!= NFS3_OK
) {
3008 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
));
3009 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3010 free_nfs_cb_data(data
);
3014 data
->cb(0, nfs
, NULL
, data
->private_data
);
3015 free_nfs_cb_data(data
);
3018 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3020 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
3023 memset(&sa
, 0, sizeof(SYMLINK3args
));
3024 sa
.where
.dir
.data
.data_len
= data
->fh
.data
.data_len
;
3025 sa
.where
.dir
.data
.data_val
= data
->fh
.data
.data_val
;
3026 sa
.where
.name
= symlink_data
->newpathobject
;
3027 sa
.symlink
.symlink_attributes
.mode
.set_it
= 1;
3028 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
;
3029 sa
.symlink
.symlink_data
= symlink_data
->oldpath
;
3031 if (rpc_nfs_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &sa
, data
) != 0) {
3032 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
3033 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3034 free_nfs_cb_data(data
);
3040 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3043 struct nfs_symlink_data
*symlink_data
;
3045 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
3046 if (symlink_data
== NULL
) {
3047 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
3050 memset(symlink_data
, 0, sizeof(struct nfs_symlink_data
));
3052 symlink_data
->oldpath
= strdup(oldpath
);
3053 if (symlink_data
->oldpath
== NULL
) {
3054 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3055 free_nfs_symlink_data(symlink_data
);
3059 symlink_data
->newpathparent
= strdup(newpath
);
3060 if (symlink_data
->newpathparent
== NULL
) {
3061 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3062 free_nfs_symlink_data(symlink_data
);
3066 ptr
= strrchr(symlink_data
->newpathparent
, '/');
3068 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3069 free_nfs_symlink_data(symlink_data
);
3075 symlink_data
->newpathobject
= strdup(ptr
);
3076 if (symlink_data
->newpathobject
== NULL
) {
3077 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
3078 free_nfs_symlink_data(symlink_data
);
3082 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
3083 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3095 struct nfs_rename_data
{
3098 struct nfs_fh3 olddir
;
3101 struct nfs_fh3 newdir
;
3104 static void free_nfs_rename_data(void *mem
)
3106 struct nfs_rename_data
*data
= mem
;
3108 if (data
->oldpath
!= NULL
) {
3109 free(data
->oldpath
);
3111 if (data
->olddir
.data
.data_val
!= NULL
) {
3112 free(data
->olddir
.data
.data_val
);
3114 if (data
->newpath
!= NULL
) {
3115 free(data
->newpath
);
3117 if (data
->newdir
.data
.data_val
!= NULL
) {
3118 free(data
->newdir
.data
.data_val
);
3123 static void nfs_rename_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3126 struct nfs_cb_data
*data
= private_data
;
3127 struct nfs_context
*nfs
= data
->nfs
;
3128 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3130 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3132 if (status
== RPC_STATUS_ERROR
) {
3133 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3134 free_nfs_cb_data(data
);
3137 if (status
== RPC_STATUS_CANCEL
) {
3138 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3139 free_nfs_cb_data(data
);
3144 if (res
->status
!= NFS3_OK
) {
3145 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
));
3146 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3147 free_nfs_cb_data(data
);
3151 data
->cb(0, nfs
, NULL
, data
->private_data
);
3152 free_nfs_cb_data(data
);
3155 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3157 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3159 /* steal the filehandle */
3160 rename_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3161 rename_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3162 data
->fh
.data
.data_val
= NULL
;
3164 if (rpc_nfs_rename_async(nfs
->rpc
, nfs_rename_cb
, &rename_data
->olddir
, rename_data
->oldobject
, &rename_data
->newdir
, rename_data
->newobject
, data
) != 0) {
3165 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
3166 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3167 free_nfs_cb_data(data
);
3174 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3176 struct nfs_rename_data
*rename_data
= data
->continue_data
;
3178 /* steal the filehandle */
3179 rename_data
->olddir
.data
.data_len
= data
->fh
.data
.data_len
;
3180 rename_data
->olddir
.data
.data_val
= data
->fh
.data
.data_val
;
3181 data
->fh
.data
.data_val
= NULL
;
3183 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) {
3184 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
3185 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3186 free_nfs_cb_data(data
);
3189 data
->continue_data
= NULL
;
3190 free_nfs_cb_data(data
);
3196 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3199 struct nfs_rename_data
*rename_data
;
3201 rename_data
= malloc(sizeof(struct nfs_rename_data
));
3202 if (rename_data
== NULL
) {
3203 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
3206 memset(rename_data
, 0, sizeof(struct nfs_rename_data
));
3208 rename_data
->oldpath
= strdup(oldpath
);
3209 if (rename_data
->oldpath
== NULL
) {
3210 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3211 free_nfs_rename_data(rename_data
);
3214 ptr
= strrchr(rename_data
->oldpath
, '/');
3216 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
3217 free_nfs_rename_data(rename_data
);
3222 rename_data
->oldobject
= ptr
;
3225 rename_data
->newpath
= strdup(newpath
);
3226 if (rename_data
->newpath
== NULL
) {
3227 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3228 free_nfs_rename_data(rename_data
);
3231 ptr
= strrchr(rename_data
->newpath
, '/');
3233 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3234 free_nfs_rename_data(rename_data
);
3239 rename_data
->newobject
= ptr
;
3242 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
3243 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3254 struct nfs_link_data
{
3256 struct nfs_fh3 oldfh
;
3259 struct nfs_fh3 newdir
;
3262 static void free_nfs_link_data(void *mem
)
3264 struct nfs_link_data
*data
= mem
;
3266 if (data
->oldpath
!= NULL
) {
3267 free(data
->oldpath
);
3269 if (data
->oldfh
.data
.data_val
!= NULL
) {
3270 free(data
->oldfh
.data
.data_val
);
3272 if (data
->newpath
!= NULL
) {
3273 free(data
->newpath
);
3275 if (data
->newdir
.data
.data_val
!= NULL
) {
3276 free(data
->newdir
.data
.data_val
);
3281 static void nfs_link_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3284 struct nfs_cb_data
*data
= private_data
;
3285 struct nfs_context
*nfs
= data
->nfs
;
3286 struct nfs_link_data
*link_data
= data
->continue_data
;
3288 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3290 if (status
== RPC_STATUS_ERROR
) {
3291 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
3292 free_nfs_cb_data(data
);
3295 if (status
== RPC_STATUS_CANCEL
) {
3296 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
3297 free_nfs_cb_data(data
);
3302 if (res
->status
!= NFS3_OK
) {
3303 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
));
3304 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3305 free_nfs_cb_data(data
);
3309 data
->cb(0, nfs
, NULL
, data
->private_data
);
3310 free_nfs_cb_data(data
);
3313 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3315 struct nfs_link_data
*link_data
= data
->continue_data
;
3317 /* steal the filehandle */
3318 link_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
3319 link_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
3320 data
->fh
.data
.data_val
= NULL
;
3322 if (rpc_nfs_link_async(nfs
->rpc
, nfs_link_cb
, &link_data
->oldfh
, &link_data
->newdir
, link_data
->newobject
, data
) != 0) {
3323 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
3324 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3325 free_nfs_cb_data(data
);
3332 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
3334 struct nfs_link_data
*link_data
= data
->continue_data
;
3336 /* steal the filehandle */
3337 link_data
->oldfh
.data
.data_len
= data
->fh
.data
.data_len
;
3338 link_data
->oldfh
.data
.data_val
= data
->fh
.data
.data_val
;
3339 data
->fh
.data
.data_val
= NULL
;
3341 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) {
3342 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
3343 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
3344 free_nfs_cb_data(data
);
3347 data
->continue_data
= NULL
;
3348 free_nfs_cb_data(data
);
3354 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
3357 struct nfs_link_data
*link_data
;
3359 link_data
= malloc(sizeof(struct nfs_link_data
));
3360 if (link_data
== NULL
) {
3361 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
3364 memset(link_data
, 0, sizeof(struct nfs_link_data
));
3366 link_data
->oldpath
= strdup(oldpath
);
3367 if (link_data
->oldpath
== NULL
) {
3368 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
3369 free_nfs_link_data(link_data
);
3373 link_data
->newpath
= strdup(newpath
);
3374 if (link_data
->newpath
== NULL
) {
3375 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
3376 free_nfs_link_data(link_data
);
3379 ptr
= strrchr(link_data
->newpath
, '/');
3381 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
3382 free_nfs_link_data(link_data
);
3387 link_data
->newobject
= ptr
;
3390 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
3391 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
3399 //qqq replace later with lseek()
3400 uint64_t nfs_get_current_offset(struct nfsfh
*nfsfh
)
3402 return nfsfh
->offset
;
3408 * Get the maximum supported READ3 size by the server
3410 uint64_t nfs_get_readmax(struct nfs_context
*nfs
)
3412 return nfs
->readmax
;
3416 * Get the maximum supported WRITE3 size by the server
3418 uint64_t nfs_get_writemax(struct nfs_context
*nfs
)
3420 /* Some XDR libraries can not marshall PDUs bigger than this */
3421 if (nfs
->writemax
< 32768) {
3422 return nfs
->writemax
;
3427 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
3432 va_start(ap
, error_string
);
3434 vsnprintf(str
, 1024, error_string
, ap
);
3435 if (nfs
->rpc
->error_string
!= NULL
) {
3436 free(nfs
->rpc
->error_string
);
3438 nfs
->rpc
->error_string
= str
;
3444 struct mount_cb_data
{
3450 static void free_mount_cb_data(struct mount_cb_data
*data
)
3452 if (data
->server
!= NULL
) {
3454 data
->server
= NULL
;
3460 static void mount_export_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3462 struct mount_cb_data
*data
= private_data
;
3464 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3466 if (status
== RPC_STATUS_ERROR
) {
3467 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3468 free_mount_cb_data(data
);
3471 if (status
== RPC_STATUS_CANCEL
) {
3472 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3473 free_mount_cb_data(data
);
3477 data
->cb(rpc
, 0, command_data
, data
->private_data
);
3478 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
3479 rpc_set_error(rpc
, "Failed to disconnect\n");
3481 free_mount_cb_data(data
);
3484 static void mount_export_4_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 /* Dont want any more callbacks even if the socket is closed */
3491 rpc
->connect_cb
= NULL
;
3493 if (status
== RPC_STATUS_ERROR
) {
3494 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3495 free_mount_cb_data(data
);
3498 if (status
== RPC_STATUS_CANCEL
) {
3499 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3500 free_mount_cb_data(data
);
3504 if (rpc_mount_export_async(rpc
, mount_export_5_cb
, data
) != 0) {
3505 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3506 free_mount_cb_data(data
);
3511 static void mount_export_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3513 struct mount_cb_data
*data
= private_data
;
3514 uint32_t mount_port
;
3516 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3518 if (status
== RPC_STATUS_ERROR
) {
3519 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3520 free_mount_cb_data(data
);
3523 if (status
== RPC_STATUS_CANCEL
) {
3524 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3525 free_mount_cb_data(data
);
3529 mount_port
= *(uint32_t *)command_data
;
3530 if (mount_port
== 0) {
3531 rpc_set_error(rpc
, "RPC error. Mount program is not available");
3532 data
->cb(rpc
, -ENOENT
, command_data
, data
->private_data
);
3533 free_mount_cb_data(data
);
3537 rpc_disconnect(rpc
, "normal disconnect");
3538 if (rpc_connect_async(rpc
, data
->server
, mount_port
, mount_export_4_cb
, data
) != 0) {
3539 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3540 free_mount_cb_data(data
);
3545 static void mount_export_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
3547 struct mount_cb_data
*data
= private_data
;
3549 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3551 if (status
== RPC_STATUS_ERROR
) {
3552 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3553 free_mount_cb_data(data
);
3556 if (status
== RPC_STATUS_CANCEL
) {
3557 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3558 free_mount_cb_data(data
);
3562 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, mount_export_3_cb
, private_data
) != 0) {
3563 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3564 free_mount_cb_data(data
);
3569 static void mount_export_1_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 /* Dont want any more callbacks even if the socket is closed */
3576 rpc
->connect_cb
= NULL
;
3578 if (status
== RPC_STATUS_ERROR
) {
3579 data
->cb(rpc
, -EFAULT
, command_data
, data
->private_data
);
3580 free_mount_cb_data(data
);
3583 if (status
== RPC_STATUS_CANCEL
) {
3584 data
->cb(rpc
, -EINTR
, "Command was cancelled", data
->private_data
);
3585 free_mount_cb_data(data
);
3589 if (rpc_pmap_null_async(rpc
, mount_export_2_cb
, data
) != 0) {
3590 data
->cb(rpc
, -ENOMEM
, command_data
, data
->private_data
);
3591 free_mount_cb_data(data
);
3596 int mount_getexports_async(struct rpc_context
*rpc
, const char *server
, rpc_cb cb
, void *private_data
)
3598 struct mount_cb_data
*data
;
3600 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
3602 data
= malloc(sizeof(struct mount_cb_data
));
3606 memset(data
, 0, sizeof(struct mount_cb_data
));
3608 data
->private_data
= private_data
;
3609 data
->server
= strdup(server
);
3610 if (data
->server
== NULL
) {
3611 free_mount_cb_data(data
);
3614 if (rpc_connect_async(rpc
, data
->server
, 111, mount_export_1_cb
, data
) != 0) {
3615 free_mount_cb_data(data
);
3622 struct rpc_context
*nfs_get_rpc_context(struct nfs_context
*nfs
)
3624 assert(nfs
->rpc
->magic
== RPC_CONTEXT_MAGIC
);
3628 const char *nfs_get_server(struct nfs_context
*nfs
) {
3632 const char *nfs_get_export(struct nfs_context
*nfs
) {
3636 const struct nfs_fh3
*nfs_get_rootfh(struct nfs_context
*nfs
) {
3637 return &nfs
->rootfh
;
3640 struct nfs_fh3
*nfs_get_fh(struct nfsfh
*nfsfh
) {