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
27 #include <sys/types.h>
29 #include <sys/statvfs.h>
34 #include "libnfs-raw.h"
35 #include "libnfs-raw-mount.h"
36 #include "libnfs-raw-nfs.h"
37 #include "libnfs-private.h"
40 struct nfsdirent
*entries
;
41 struct nfsdirent
*current
;
51 struct rpc_context
*rpc
;
54 struct nfs_fh3 rootfh
;
59 void nfs_free_nfsdir(struct nfsdir
*nfsdir
)
61 while (nfsdir
->entries
) {
62 struct nfsdirent
*dirent
= nfsdir
->entries
->next
;
63 if (nfsdir
->entries
->name
!= NULL
) {
64 free(nfsdir
->entries
->name
);
66 free(nfsdir
->entries
);
67 nfsdir
->entries
= dirent
;
73 typedef int (*continue_func
)(struct nfs_context
*nfs
, struct nfs_cb_data
*data
);
76 struct nfs_context
*nfs
;
78 char *saved_path
, *path
;
83 continue_func continue_cb
;
85 void (*free_continue_data
)(void *);
90 /* for multi-read/write calls. */
94 off_t start_offset
, max_offset
;
99 struct nfs_cb_data
*data
;
104 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
);
107 void nfs_set_auth(struct nfs_context
*nfs
, struct AUTH
*auth
)
109 return rpc_set_auth(nfs
->rpc
, auth
);
112 int nfs_get_fd(struct nfs_context
*nfs
)
114 return rpc_get_fd(nfs
->rpc
);
117 int nfs_which_events(struct nfs_context
*nfs
)
119 return rpc_which_events(nfs
->rpc
);
122 int nfs_service(struct nfs_context
*nfs
, int revents
)
124 return rpc_service(nfs
->rpc
, revents
);
127 char *nfs_get_error(struct nfs_context
*nfs
)
129 return rpc_get_error(nfs
->rpc
);
132 struct nfs_context
*nfs_init_context(void)
134 struct nfs_context
*nfs
;
136 nfs
= malloc(sizeof(struct nfs_context
));
140 nfs
->rpc
= rpc_init_context();
141 if (nfs
->rpc
== NULL
) {
149 void nfs_destroy_context(struct nfs_context
*nfs
)
151 rpc_destroy_context(nfs
->rpc
);
164 if (nfs
->rootfh
.data
.data_val
!= NULL
) {
165 free(nfs
->rootfh
.data
.data_val
);
166 nfs
->rootfh
.data
.data_val
= NULL
;
172 void free_nfs_cb_data(struct nfs_cb_data
*data
)
174 if (data
->saved_path
!= NULL
) {
175 free(data
->saved_path
);
176 data
->saved_path
= NULL
;
179 if (data
->continue_data
!= NULL
) {
180 data
->free_continue_data(data
->continue_data
);
181 data
->continue_data
= NULL
;
184 if (data
->fh
.data
.data_val
!= NULL
) {
185 free(data
->fh
.data
.data_val
);
186 data
->fh
.data
.data_val
= NULL
;
189 if (data
->buffer
!= NULL
) {
201 static void nfs_mount_10_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
203 struct nfs_cb_data
*data
= private_data
;
204 struct nfs_context
*nfs
= data
->nfs
;
206 if (status
== RPC_STATUS_ERROR
) {
207 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
208 free_nfs_cb_data(data
);
211 if (status
== RPC_STATUS_CANCEL
) {
212 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
213 free_nfs_cb_data(data
);
217 data
->cb(0, nfs
, NULL
, data
->private_data
);
218 free_nfs_cb_data(data
);
221 static void nfs_mount_9_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
223 struct nfs_cb_data
*data
= private_data
;
224 struct nfs_context
*nfs
= data
->nfs
;
225 FSINFO3res
*res
= command_data
;
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 nfs
->readmax
= res
->FSINFO3res_u
.resok
.rtmax
;
239 nfs
->writemax
= res
->FSINFO3res_u
.resok
.wtmax
;
241 if (rpc_nfs_getattr_async(rpc
, nfs_mount_10_cb
, &nfs
->rootfh
, data
) != 0) {
242 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
243 free_nfs_cb_data(data
);
248 static void nfs_mount_8_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
250 struct nfs_cb_data
*data
= private_data
;
251 struct nfs_context
*nfs
= data
->nfs
;
253 if (status
== RPC_STATUS_ERROR
) {
254 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
255 free_nfs_cb_data(data
);
258 if (status
== RPC_STATUS_CANCEL
) {
259 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
260 free_nfs_cb_data(data
);
264 if (rpc_nfs_fsinfo_async(rpc
, nfs_mount_9_cb
, &nfs
->rootfh
, data
) != 0) {
265 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
266 free_nfs_cb_data(data
);
272 static void nfs_mount_7_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
274 struct nfs_cb_data
*data
= private_data
;
275 struct nfs_context
*nfs
= data
->nfs
;
277 if (status
== RPC_STATUS_ERROR
) {
278 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
279 free_nfs_cb_data(data
);
282 if (status
== RPC_STATUS_CANCEL
) {
283 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
284 free_nfs_cb_data(data
);
288 if (rpc_nfs_null_async(rpc
, nfs_mount_8_cb
, data
) != 0) {
289 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
290 free_nfs_cb_data(data
);
296 static void nfs_mount_6_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
298 struct nfs_cb_data
*data
= private_data
;
299 struct nfs_context
*nfs
= data
->nfs
;
302 if (status
== RPC_STATUS_ERROR
) {
303 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
304 free_nfs_cb_data(data
);
307 if (status
== RPC_STATUS_CANCEL
) {
308 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
309 free_nfs_cb_data(data
);
314 if (res
->fhs_status
!= MNT3_OK
) {
315 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
));
316 data
->cb(mountstat3_to_errno(res
->fhs_status
), nfs
, rpc_get_error(rpc
), data
->private_data
);
317 free_nfs_cb_data(data
);
321 nfs
->rootfh
.data
.data_len
= res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
322 nfs
->rootfh
.data
.data_val
= malloc(nfs
->rootfh
.data
.data_len
);
323 if (nfs
->rootfh
.data
.data_val
== NULL
) {
324 rpc_set_error(rpc
, "Out of memory. Could not allocate memory to store root filehandle");
325 data
->cb(-ENOMEM
, nfs
, rpc_get_error(rpc
), data
->private_data
);
326 free_nfs_cb_data(data
);
329 memcpy(nfs
->rootfh
.data
.data_val
, res
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, nfs
->rootfh
.data
.data_len
);
331 rpc_disconnect(rpc
, "normal disconnect");
332 if (rpc_connect_async(rpc
, nfs
->server
, 2049, nfs_mount_7_cb
, data
) != 0) {
333 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
334 free_nfs_cb_data(data
);
340 static void nfs_mount_5_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
342 struct nfs_cb_data
*data
= private_data
;
343 struct nfs_context
*nfs
= data
->nfs
;
345 if (status
== RPC_STATUS_ERROR
) {
346 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
347 free_nfs_cb_data(data
);
350 if (status
== RPC_STATUS_CANCEL
) {
351 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
352 free_nfs_cb_data(data
);
356 if (rpc_mount_mnt_async(rpc
, nfs_mount_6_cb
, nfs
->export
, data
) != 0) {
357 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
358 free_nfs_cb_data(data
);
363 static void nfs_mount_4_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
365 struct nfs_cb_data
*data
= private_data
;
366 struct nfs_context
*nfs
= data
->nfs
;
368 if (status
== RPC_STATUS_ERROR
) {
369 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
370 free_nfs_cb_data(data
);
373 if (status
== RPC_STATUS_CANCEL
) {
374 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
375 free_nfs_cb_data(data
);
379 if (rpc_mount_null_async(rpc
, nfs_mount_5_cb
, data
) != 0) {
380 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
381 free_nfs_cb_data(data
);
386 static void nfs_mount_3_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
388 struct nfs_cb_data
*data
= private_data
;
389 struct nfs_context
*nfs
= data
->nfs
;
392 if (status
== RPC_STATUS_ERROR
) {
393 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
394 free_nfs_cb_data(data
);
397 if (status
== RPC_STATUS_CANCEL
) {
398 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
399 free_nfs_cb_data(data
);
403 mount_port
= *(uint32_t *)command_data
;
404 if (mount_port
== 0) {
405 rpc_set_error(rpc
, "RPC error. Mount program is not available on %s", nfs
->server
);
406 data
->cb(-ENOENT
, nfs
, command_data
, data
->private_data
);
407 free_nfs_cb_data(data
);
411 rpc_disconnect(rpc
, "normal disconnect");
412 if (rpc_connect_async(rpc
, nfs
->server
, mount_port
, nfs_mount_4_cb
, data
) != 0) {
413 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
414 free_nfs_cb_data(data
);
420 static void nfs_mount_2_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
422 struct nfs_cb_data
*data
= private_data
;
423 struct nfs_context
*nfs
= data
->nfs
;
425 if (status
== RPC_STATUS_ERROR
) {
426 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
427 free_nfs_cb_data(data
);
430 if (status
== RPC_STATUS_CANCEL
) {
431 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
432 free_nfs_cb_data(data
);
436 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, nfs_mount_3_cb
, private_data
) != 0) {
437 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
438 free_nfs_cb_data(data
);
443 static void nfs_mount_1_cb(struct rpc_context
*rpc
, int status
, void *command_data
, void *private_data
)
445 struct nfs_cb_data
*data
= private_data
;
446 struct nfs_context
*nfs
= data
->nfs
;
448 if (status
== RPC_STATUS_ERROR
) {
449 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
450 free_nfs_cb_data(data
);
453 if (status
== RPC_STATUS_CANCEL
) {
454 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
455 free_nfs_cb_data(data
);
459 if (rpc_pmap_null_async(rpc
, nfs_mount_2_cb
, data
) != 0) {
460 data
->cb(-ENOMEM
, nfs
, command_data
, data
->private_data
);
461 free_nfs_cb_data(data
);
467 * Async call for mounting an nfs share and geting the root filehandle
469 int nfs_mount_async(struct nfs_context
*nfs
, const char *server
, const char *export
, nfs_cb cb
, void *private_data
)
471 struct nfs_cb_data
*data
;
473 data
= malloc(sizeof(struct nfs_cb_data
));
475 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
478 bzero(data
, sizeof(struct nfs_cb_data
));
479 nfs
->server
= strdup(server
);
480 nfs
->export
= strdup(export
);
483 data
->private_data
= private_data
;
485 if (rpc_connect_async(nfs
->rpc
, server
, 111, nfs_mount_1_cb
, data
) != 0) {
486 rpc_set_error(nfs
->rpc
, "Failed to start connection");
487 free_nfs_cb_data(data
);
497 * Functions to first look up a path, component by component, and then finally call a specific function once
498 * the filehandle for the final component is found.
500 static void nfs_lookup_path_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
502 struct nfs_cb_data
*data
= private_data
;
503 struct nfs_context
*nfs
= data
->nfs
;
506 if (status
== RPC_STATUS_ERROR
) {
507 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
508 free_nfs_cb_data(data
);
511 if (status
== RPC_STATUS_CANCEL
) {
512 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
513 free_nfs_cb_data(data
);
518 if (res
->status
!= NFS3_OK
) {
519 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
));
520 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
521 free_nfs_cb_data(data
);
525 if (nfs_lookup_path_async_internal(nfs
, data
, &res
->LOOKUP3res_u
.resok
.object
) != 0) {
526 rpc_set_error(nfs
->rpc
, "Failed to create lookup pdu");
527 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
528 free_nfs_cb_data(data
);
533 static int nfs_lookup_path_async_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
, struct nfs_fh3
*fh
)
537 while (*data
->path
== '/') {
542 str
= index(path
, '/');
547 while (*data
->path
!= 0) {
553 data
->fh
.data
.data_len
= fh
->data
.data_len
;
554 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
555 if (data
->fh
.data
.data_val
== NULL
) {
556 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh for %s", data
->path
);
557 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
558 free_nfs_cb_data(data
);
561 memcpy(data
->fh
.data
.data_val
, fh
->data
.data_val
, data
->fh
.data
.data_len
);
562 data
->continue_cb(nfs
, data
);
566 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_lookup_path_1_cb
, fh
, path
, data
) != 0) {
567 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s", data
->path
);
568 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
569 free_nfs_cb_data(data
);
575 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
)
577 struct nfs_cb_data
*data
;
579 if (path
[0] != '/') {
580 rpc_set_error(nfs
->rpc
, "Pathname is not absulute %s", path
);
584 data
= malloc(sizeof(struct nfs_cb_data
));
586 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
589 bzero(data
, sizeof(struct nfs_cb_data
));
592 data
->continue_cb
= continue_cb
;
593 data
->continue_data
= continue_data
;
594 data
->free_continue_data
= free_continue_data
;
595 data
->continue_int
= continue_int
;
596 data
->private_data
= private_data
;
597 data
->saved_path
= strdup(path
);
598 if (data
->saved_path
== NULL
) {
599 rpc_set_error(nfs
->rpc
, "out of memory: failed to copy path string");
600 free_nfs_cb_data(data
);
603 data
->path
= data
->saved_path
;
605 if (nfs_lookup_path_async_internal(nfs
, data
, &nfs
->rootfh
) != 0) {
606 /* return 0 here since the callback will be invoked if there is a failure */
619 static void nfs_stat_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
622 struct nfs_cb_data
*data
= private_data
;
623 struct nfs_context
*nfs
= data
->nfs
;
626 if (status
== RPC_STATUS_ERROR
) {
627 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
628 free_nfs_cb_data(data
);
631 if (status
== RPC_STATUS_CANCEL
) {
632 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
633 free_nfs_cb_data(data
);
638 if (res
->status
!= NFS3_OK
) {
639 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
));
640 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
641 free_nfs_cb_data(data
);
646 st
.st_ino
= res
->GETATTR3res_u
.resok
.obj_attributes
.fileid
;
647 st
.st_mode
= res
->GETATTR3res_u
.resok
.obj_attributes
.mode
;
648 st
.st_nlink
= res
->GETATTR3res_u
.resok
.obj_attributes
.nlink
;
649 st
.st_uid
= res
->GETATTR3res_u
.resok
.obj_attributes
.uid
;
650 st
.st_gid
= res
->GETATTR3res_u
.resok
.obj_attributes
.gid
;
652 st
.st_size
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
653 st
.st_blksize
= 4096;
654 st
.st_blocks
= res
->GETATTR3res_u
.resok
.obj_attributes
.size
/ 4096;
655 st
.st_atime
= res
->GETATTR3res_u
.resok
.obj_attributes
.atime
.seconds
;
656 st
.st_mtime
= res
->GETATTR3res_u
.resok
.obj_attributes
.mtime
.seconds
;
657 st
.st_ctime
= res
->GETATTR3res_u
.resok
.obj_attributes
.ctime
.seconds
;
659 data
->cb(0, nfs
, &st
, data
->private_data
);
660 free_nfs_cb_data(data
);
663 static int nfs_stat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
665 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &data
->fh
, data
) != 0) {
666 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
667 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
668 free_nfs_cb_data(data
);
674 int nfs_stat_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
676 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_stat_continue_internal
, NULL
, NULL
, 0) != 0) {
677 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
691 static void nfs_open_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
694 struct nfs_cb_data
*data
= private_data
;
695 struct nfs_context
*nfs
= data
->nfs
;
697 unsigned int nfsmode
= 0;
699 if (status
== RPC_STATUS_ERROR
) {
700 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
701 free_nfs_cb_data(data
);
704 if (status
== RPC_STATUS_CANCEL
) {
705 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
706 free_nfs_cb_data(data
);
711 if (res
->status
!= NFS3_OK
) {
712 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
));
713 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
714 free_nfs_cb_data(data
);
718 if (data
->continue_int
& O_WRONLY
) {
719 nfsmode
|= ACCESS3_MODIFY
;
721 if (data
->continue_int
& O_RDWR
) {
722 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
724 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
725 nfsmode
|= ACCESS3_READ
;
729 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
730 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
731 nfsmode
&ACCESS3_READ
?'r':'-',
732 nfsmode
&ACCESS3_MODIFY
?'w':'-',
733 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
734 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
735 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
736 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
737 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
738 free_nfs_cb_data(data
);
742 nfsfh
= malloc(sizeof(struct nfsfh
));
744 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
745 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
746 free_nfs_cb_data(data
);
749 bzero(nfsfh
, sizeof(struct nfsfh
));
751 if (data
->continue_int
& O_SYNC
) {
755 /* steal the filehandle */
756 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
757 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
758 data
->fh
.data
.data_val
= NULL
;
760 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
761 free_nfs_cb_data(data
);
764 static int nfs_open_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
768 if (data
->continue_int
& O_WRONLY
) {
769 nfsmode
|= ACCESS3_MODIFY
;
771 if (data
->continue_int
& O_RDWR
) {
772 nfsmode
|= ACCESS3_READ
|ACCESS3_MODIFY
;
774 if (!(data
->continue_int
& (O_WRONLY
|O_RDWR
))) {
775 nfsmode
|= ACCESS3_READ
;
778 if (rpc_nfs_access_async(nfs
->rpc
, nfs_open_cb
, &data
->fh
, nfsmode
, data
) != 0) {
779 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
780 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
781 free_nfs_cb_data(data
);
787 int nfs_open_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
789 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_open_continue_internal
, NULL
, NULL
, mode
) != 0) {
790 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
804 static void nfs_pread_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
806 struct nfs_cb_data
*data
= private_data
;
807 struct nfs_context
*nfs
= data
->nfs
;
810 if (status
== RPC_STATUS_ERROR
) {
811 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
812 free_nfs_cb_data(data
);
815 if (status
== RPC_STATUS_CANCEL
) {
816 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
817 free_nfs_cb_data(data
);
822 if (res
->status
!= NFS3_OK
) {
823 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
824 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
825 free_nfs_cb_data(data
);
829 data
->nfsfh
->offset
+= res
->READ3res_u
.resok
.count
;
830 data
->cb(res
->READ3res_u
.resok
.count
, nfs
, res
->READ3res_u
.resok
.data
.data_val
, data
->private_data
);
831 free_nfs_cb_data(data
);
834 static void nfs_pread_mcb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
836 struct nfs_mcb_data
*mdata
= private_data
;
837 struct nfs_cb_data
*data
= mdata
->data
;
838 struct nfs_context
*nfs
= data
->nfs
;
843 if (status
== RPC_STATUS_ERROR
) {
844 /* flag the failure but do not invoke callback until we have received all responses */
847 if (status
== RPC_STATUS_CANCEL
) {
848 /* flag the cancellation but do not invoke callback until we have received all responses */
852 /* reassemble the data into the buffer */
853 if (status
== RPC_STATUS_SUCCESS
) {
855 if (res
->status
!= NFS3_OK
) {
856 rpc_set_error(nfs
->rpc
, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
859 memcpy(&data
->buffer
[mdata
->offset
], res
->READ3res_u
.resok
.data
.data_val
, res
->READ3res_u
.resok
.count
);
860 if ((unsigned)data
->max_offset
< mdata
->offset
+ res
->READ3res_u
.resok
.count
) {
861 data
->max_offset
= mdata
->offset
+ res
->READ3res_u
.resok
.count
;
866 if (data
->num_calls
> 0) {
867 /* still waiting for more replies */
873 if (data
->error
!= 0) {
874 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
875 free_nfs_cb_data(data
);
879 if (data
->cancel
!= 0) {
880 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
881 free_nfs_cb_data(data
);
886 data
->nfsfh
->offset
= data
->max_offset
;
887 data
->cb(data
->max_offset
- data
->start_offset
, nfs
, data
->buffer
, data
->private_data
);
888 free_nfs_cb_data(data
);
892 int nfs_pread_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, size_t count
, nfs_cb cb
, void *private_data
)
894 struct nfs_cb_data
*data
;
896 data
= malloc(sizeof(struct nfs_cb_data
));
898 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
901 bzero(data
, sizeof(struct nfs_cb_data
));
904 data
->private_data
= private_data
;
907 nfsfh
->offset
= offset
;
909 if (count
<= nfs_get_readmax(nfs
)) {
910 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_cb
, &nfsfh
->fh
, offset
, count
, data
) != 0) {
911 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
912 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
913 free_nfs_cb_data(data
);
919 /* trying to read more than maximum server read size, we has to chop it up into smaller
920 * reads and collect into a reassembly buffer.
921 * we send all reads in parallell so that performance is still good.
923 data
->start_offset
= offset
;
925 data
->buffer
= malloc(count
);
926 if (data
->buffer
== NULL
) {
927 rpc_set_error(nfs
->rpc
, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count
);
928 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
929 free_nfs_cb_data(data
);
934 size_t readcount
= count
;
935 struct nfs_mcb_data
*mdata
;
937 if (readcount
> nfs_get_readmax(nfs
)) {
938 readcount
= nfs_get_readmax(nfs
);
941 mdata
= malloc(sizeof(struct nfs_mcb_data
));
943 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_mcb_data structure");
946 bzero(mdata
, sizeof(struct nfs_mcb_data
));
948 mdata
->offset
= offset
;
949 mdata
->count
= readcount
;
951 if (rpc_nfs_read_async(nfs
->rpc
, nfs_pread_mcb
, &nfsfh
->fh
, offset
, readcount
, mdata
) != 0) {
952 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READ call for %s", data
->path
);
953 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
969 int nfs_read_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, size_t count
, nfs_cb cb
, void *private_data
)
971 return nfs_pread_async(nfs
, nfsfh
, nfsfh
->offset
, count
, cb
, private_data
);
979 static void nfs_pwrite_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
981 struct nfs_cb_data
*data
= private_data
;
982 struct nfs_context
*nfs
= data
->nfs
;
985 if (status
== RPC_STATUS_ERROR
) {
986 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
987 free_nfs_cb_data(data
);
990 if (status
== RPC_STATUS_CANCEL
) {
991 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
992 free_nfs_cb_data(data
);
997 if (res
->status
!= NFS3_OK
) {
998 rpc_set_error(nfs
->rpc
, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
999 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1000 free_nfs_cb_data(data
);
1004 data
->nfsfh
->offset
+= res
->WRITE3res_u
.resok
.count
;
1005 data
->cb(res
->WRITE3res_u
.resok
.count
, nfs
, NULL
, data
->private_data
);
1006 free_nfs_cb_data(data
);
1009 int nfs_pwrite_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, size_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1011 struct nfs_cb_data
*data
;
1013 data
= malloc(sizeof(struct nfs_cb_data
));
1015 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1018 bzero(data
, sizeof(struct nfs_cb_data
));
1021 data
->private_data
= private_data
;
1022 data
->nfsfh
= nfsfh
;
1024 nfsfh
->offset
= offset
;
1025 if (rpc_nfs_write_async(nfs
->rpc
, nfs_pwrite_cb
, &nfsfh
->fh
, buf
, offset
, count
, nfsfh
->is_sync
?FILE_SYNC
:UNSTABLE
, data
) != 0) {
1026 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send WRITE call for %s", data
->path
);
1027 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1028 free_nfs_cb_data(data
);
1037 int nfs_write_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, size_t count
, char *buf
, nfs_cb cb
, void *private_data
)
1039 return nfs_pwrite_async(nfs
, nfsfh
, nfsfh
->offset
, count
, buf
, cb
, private_data
);
1049 int nfs_close_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1051 if (nfsfh
->fh
.data
.data_val
!= NULL
){
1052 free(nfsfh
->fh
.data
.data_val
);
1053 nfsfh
->fh
.data
.data_val
= NULL
;
1057 cb(0, nfs
, NULL
, private_data
);
1068 int nfs_fstat_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1070 struct nfs_cb_data
*data
;
1072 data
= malloc(sizeof(struct nfs_cb_data
));
1074 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1077 bzero(data
, sizeof(struct nfs_cb_data
));
1080 data
->private_data
= private_data
;
1082 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_stat_1_cb
, &nfsfh
->fh
, data
) != 0) {
1083 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send STAT GETATTR call for %s", data
->path
);
1084 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1085 free_nfs_cb_data(data
);
1096 static void nfs_fsync_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1098 struct nfs_cb_data
*data
= private_data
;
1099 struct nfs_context
*nfs
= data
->nfs
;
1102 if (status
== RPC_STATUS_ERROR
) {
1103 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1104 free_nfs_cb_data(data
);
1107 if (status
== RPC_STATUS_CANCEL
) {
1108 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1109 free_nfs_cb_data(data
);
1114 if (res
->status
!= NFS3_OK
) {
1115 rpc_set_error(nfs
->rpc
, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1116 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1117 free_nfs_cb_data(data
);
1121 data
->cb(0, nfs
, NULL
, data
->private_data
);
1122 free_nfs_cb_data(data
);
1125 int nfs_fsync_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, nfs_cb cb
, void *private_data
)
1127 struct nfs_cb_data
*data
;
1129 data
= malloc(sizeof(struct nfs_cb_data
));
1131 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1134 bzero(data
, sizeof(struct nfs_cb_data
));
1137 data
->private_data
= private_data
;
1139 if (rpc_nfs_commit_async(nfs
->rpc
, nfs_fsync_cb
, &nfsfh
->fh
, data
) != 0) {
1140 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send COMMIT call for %s", data
->path
);
1141 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1142 free_nfs_cb_data(data
);
1154 static void nfs_ftruncate_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1156 struct nfs_cb_data
*data
= private_data
;
1157 struct nfs_context
*nfs
= data
->nfs
;
1160 if (status
== RPC_STATUS_ERROR
) {
1161 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1162 free_nfs_cb_data(data
);
1165 if (status
== RPC_STATUS_CANCEL
) {
1166 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1167 free_nfs_cb_data(data
);
1172 if (res
->status
!= NFS3_OK
) {
1173 rpc_set_error(nfs
->rpc
, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1174 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1175 free_nfs_cb_data(data
);
1179 data
->cb(0, nfs
, NULL
, data
->private_data
);
1180 free_nfs_cb_data(data
);
1183 int nfs_ftruncate_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t length
, nfs_cb cb
, void *private_data
)
1185 struct nfs_cb_data
*data
;
1188 data
= malloc(sizeof(struct nfs_cb_data
));
1190 rpc_set_error(nfs
->rpc
, "out of memory: failed to allocate nfs_cb_data structure");
1193 bzero(data
, sizeof(struct nfs_cb_data
));
1196 data
->private_data
= private_data
;
1198 bzero(&args
, sizeof(SETATTR3args
));
1199 args
.object
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
1200 args
.object
.data
.data_val
= nfsfh
->fh
.data
.data_val
;
1201 args
.new_attributes
.size
.set_it
= 1;
1202 args
.new_attributes
.size
.set_size3_u
.size
= length
;
1204 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_ftruncate_cb
, &args
, data
) != 0) {
1205 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1206 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1207 free_nfs_cb_data(data
);
1217 static int nfs_truncate_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1219 off_t offset
= data
->continue_int
;
1222 nfsfh
.fh
.data
.data_val
= data
->fh
.data
.data_val
;
1223 nfsfh
.fh
.data
.data_len
= data
->fh
.data
.data_len
;
1225 if (nfs_ftruncate_async(nfs
, &nfsfh
, offset
, data
->cb
, data
->private_data
) != 0) {
1226 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
1227 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1228 free_nfs_cb_data(data
);
1231 free_nfs_cb_data(data
);
1235 int nfs_truncate_async(struct nfs_context
*nfs
, const char *path
, off_t length
, nfs_cb cb
, void *private_data
)
1241 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_truncate_continue_internal
, NULL
, NULL
, offset
) != 0) {
1242 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1255 static void nfs_mkdir_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1258 struct nfs_cb_data
*data
= private_data
;
1259 struct nfs_context
*nfs
= data
->nfs
;
1260 char *str
= data
->continue_data
;
1262 str
= &str
[strlen(str
) + 1];
1264 if (status
== RPC_STATUS_ERROR
) {
1265 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1266 free_nfs_cb_data(data
);
1269 if (status
== RPC_STATUS_CANCEL
) {
1270 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1271 free_nfs_cb_data(data
);
1276 if (res
->status
!= NFS3_OK
) {
1277 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
));
1278 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1279 free_nfs_cb_data(data
);
1283 data
->cb(0, nfs
, NULL
, data
->private_data
);
1284 free_nfs_cb_data(data
);
1287 static int nfs_mkdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1289 char *str
= data
->continue_data
;
1291 str
= &str
[strlen(str
) + 1];
1293 if (rpc_nfs_mkdir_async(nfs
->rpc
, nfs_mkdir_cb
, &data
->fh
, str
, data
) != 0) {
1294 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send MKDIR call for %s", data
->path
);
1295 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1296 free_nfs_cb_data(data
);
1302 int nfs_mkdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1307 new_path
= strdup(path
);
1308 if (new_path
== NULL
) {
1309 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1313 ptr
= rindex(new_path
, '/');
1315 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1320 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1321 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_mkdir_continue_internal
, new_path
, free
, 0) != 0) {
1322 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path component");
1336 static void nfs_rmdir_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1339 struct nfs_cb_data
*data
= private_data
;
1340 struct nfs_context
*nfs
= data
->nfs
;
1341 char *str
= data
->continue_data
;
1343 str
= &str
[strlen(str
) + 1];
1345 if (status
== RPC_STATUS_ERROR
) {
1346 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1347 free_nfs_cb_data(data
);
1350 if (status
== RPC_STATUS_CANCEL
) {
1351 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1352 free_nfs_cb_data(data
);
1357 if (res
->status
!= NFS3_OK
) {
1358 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
));
1359 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1360 free_nfs_cb_data(data
);
1364 data
->cb(0, nfs
, NULL
, data
->private_data
);
1365 free_nfs_cb_data(data
);
1368 static int nfs_rmdir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1370 char *str
= data
->continue_data
;
1372 str
= &str
[strlen(str
) + 1];
1374 if (rpc_nfs_rmdir_async(nfs
->rpc
, nfs_rmdir_cb
, &data
->fh
, str
, data
) != 0) {
1375 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RMDIR call for %s", data
->path
);
1376 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1377 free_nfs_cb_data(data
);
1383 int nfs_rmdir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1388 new_path
= strdup(path
);
1389 if (new_path
== NULL
) {
1390 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1394 ptr
= rindex(new_path
, '/');
1396 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1401 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1402 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_rmdir_continue_internal
, new_path
, free
, 0) != 0) {
1403 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1416 static void nfs_create_2_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1419 struct nfs_cb_data
*data
= private_data
;
1420 struct nfs_context
*nfs
= data
->nfs
;
1421 struct nfsfh
*nfsfh
;
1422 char *str
= data
->continue_data
;
1424 if (status
== RPC_STATUS_ERROR
) {
1425 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1426 free_nfs_cb_data(data
);
1429 if (status
== RPC_STATUS_CANCEL
) {
1430 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1431 free_nfs_cb_data(data
);
1435 str
= &str
[strlen(str
) + 1];
1437 if (res
->status
!= NFS3_OK
) {
1438 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
));
1439 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1444 nfsfh
= malloc(sizeof(struct nfsfh
));
1445 if (nfsfh
== NULL
) {
1446 rpc_set_error(nfs
->rpc
, "NFS: Failed to allocate nfsfh structure");
1447 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1448 free_nfs_cb_data(data
);
1451 bzero(nfsfh
, sizeof(struct nfsfh
));
1453 /* steal the filehandle */
1454 nfsfh
->fh
.data
.data_len
= data
->fh
.data
.data_len
;
1455 nfsfh
->fh
.data
.data_val
= data
->fh
.data
.data_val
;
1456 data
->fh
.data
.data_val
= NULL
;
1458 data
->cb(0, nfs
, nfsfh
, data
->private_data
);
1459 free_nfs_cb_data(data
);
1464 static void nfs_creat_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1467 struct nfs_cb_data
*data
= private_data
;
1468 struct nfs_context
*nfs
= data
->nfs
;
1469 char *str
= data
->continue_data
;
1471 if (status
== RPC_STATUS_ERROR
) {
1472 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1473 free_nfs_cb_data(data
);
1476 if (status
== RPC_STATUS_CANCEL
) {
1477 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1478 free_nfs_cb_data(data
);
1482 str
= &str
[strlen(str
) + 1];
1484 if (res
->status
!= NFS3_OK
) {
1485 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
));
1486 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1491 if (rpc_nfs_lookup_async(nfs
->rpc
, nfs_create_2_cb
, &data
->fh
, str
, data
) != 0) {
1492 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send lookup call for %s/%s", data
->saved_path
, str
);
1493 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1494 free_nfs_cb_data(data
);
1500 static int nfs_creat_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1502 char *str
= data
->continue_data
;
1504 str
= &str
[strlen(str
) + 1];
1506 if (rpc_nfs_create_async(nfs
->rpc
, nfs_creat_1_cb
, &data
->fh
, str
, data
->continue_int
, data
) != 0) {
1507 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send CREATE call for %s/%s", data
->path
, str
);
1508 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1509 free_nfs_cb_data(data
);
1515 int nfs_creat_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
1520 new_path
= strdup(path
);
1521 if (new_path
== NULL
) {
1522 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1526 ptr
= rindex(new_path
, '/');
1528 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1533 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1534 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_creat_continue_internal
, new_path
, free
, mode
) != 0) {
1535 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1548 static void nfs_unlink_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1551 struct nfs_cb_data
*data
= private_data
;
1552 struct nfs_context
*nfs
= data
->nfs
;
1553 char *str
= data
->continue_data
;
1555 str
= &str
[strlen(str
) + 1];
1557 if (status
== RPC_STATUS_ERROR
) {
1558 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1559 free_nfs_cb_data(data
);
1562 if (status
== RPC_STATUS_CANCEL
) {
1563 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1564 free_nfs_cb_data(data
);
1569 if (res
->status
!= NFS3_OK
) {
1570 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
));
1571 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1572 free_nfs_cb_data(data
);
1576 data
->cb(0, nfs
, NULL
, data
->private_data
);
1577 free_nfs_cb_data(data
);
1580 static int nfs_unlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1582 char *str
= data
->continue_data
;
1584 str
= &str
[strlen(str
) + 1];
1586 if (rpc_nfs_remove_async(nfs
->rpc
, nfs_unlink_cb
, &data
->fh
, str
, data
) != 0) {
1587 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send REMOVE call for %s", data
->path
);
1588 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1589 free_nfs_cb_data(data
);
1595 int nfs_unlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1600 new_path
= strdup(path
);
1601 if (new_path
== NULL
) {
1602 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for path");
1606 ptr
= rindex(new_path
, '/');
1608 rpc_set_error(nfs
->rpc
, "Invalid path %s", path
);
1613 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1614 if (nfs_lookuppath_async(nfs
, new_path
, cb
, private_data
, nfs_unlink_continue_internal
, new_path
, free
, 0) != 0) {
1615 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1629 static void nfs_opendir_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1632 struct nfs_cb_data
*data
= private_data
;
1633 struct nfs_context
*nfs
= data
->nfs
;
1634 struct nfsdir
*nfsdir
= data
->continue_data
;;
1635 struct entry3
*entry
;
1638 if (status
== RPC_STATUS_ERROR
) {
1639 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1640 nfs_free_nfsdir(nfsdir
);
1641 data
->continue_data
= NULL
;
1642 free_nfs_cb_data(data
);
1645 if (status
== RPC_STATUS_CANCEL
) {
1646 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1647 nfs_free_nfsdir(nfsdir
);
1648 data
->continue_data
= NULL
;
1649 free_nfs_cb_data(data
);
1654 if (res
->status
!= NFS3_OK
) {
1655 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
));
1656 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1657 nfs_free_nfsdir(nfsdir
);
1658 data
->continue_data
= NULL
;
1659 free_nfs_cb_data(data
);
1663 entry
=res
->READDIR3res_u
.resok
.reply
.entries
;
1664 while (entry
!= NULL
) {
1665 struct nfsdirent
*nfsdirent
;
1667 nfsdirent
= malloc(sizeof(struct nfsdirent
));
1668 if (nfsdirent
== NULL
) {
1669 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent", data
->private_data
);
1670 nfs_free_nfsdir(nfsdir
);
1671 data
->continue_data
= NULL
;
1672 free_nfs_cb_data(data
);
1675 bzero(nfsdirent
, sizeof(struct nfsdirent
));
1676 nfsdirent
->name
= strdup(entry
->name
);
1677 if (nfsdirent
->name
== NULL
) {
1678 data
->cb(-ENOMEM
, nfs
, "Failed to allocate dirent->name", data
->private_data
);
1679 nfs_free_nfsdir(nfsdir
);
1680 data
->continue_data
= NULL
;
1681 free_nfs_cb_data(data
);
1684 nfsdirent
->inode
= entry
->fileid
;
1685 nfsdirent
->next
= nfsdir
->entries
;
1686 nfsdir
->entries
= nfsdirent
;
1688 cookie
= entry
->cookie
;
1689 entry
= entry
->nextentry
;
1692 if (res
->READDIR3res_u
.resok
.reply
.eof
== 0) {
1693 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, cookie
, res
->READDIR3res_u
.resok
.cookieverf
, 20000, data
) != 0) {
1694 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
1695 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1696 nfs_free_nfsdir(nfsdir
);
1697 data
->continue_data
= NULL
;
1698 free_nfs_cb_data(data
);
1704 /* steal the dirhandle */
1705 data
->continue_data
= NULL
;
1706 nfsdir
->current
= nfsdir
->entries
;
1708 data
->cb(0, nfs
, nfsdir
, data
->private_data
);
1709 free_nfs_cb_data(data
);
1712 static int nfs_opendir_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1716 bzero(cv
, sizeof(cookieverf3
));
1717 if (rpc_nfs_readdir_async(nfs
->rpc
, nfs_opendir_cb
, &data
->fh
, 0, (char *)&cv
, 20000, data
) != 0) {
1718 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READDIR call for %s", data
->path
);
1719 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1720 free_nfs_cb_data(data
);
1726 int nfs_opendir_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1728 struct nfsdir
*nfsdir
;
1730 nfsdir
= malloc(sizeof(struct nfsdir
));
1731 if (nfsdir
== NULL
) {
1732 rpc_set_error(nfs
->rpc
, "failed to allocate buffer for nfsdir");
1735 bzero(nfsdir
, sizeof(struct nfsdir
));
1737 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_opendir_continue_internal
, nfsdir
, free
, 0) != 0) {
1738 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1746 struct nfsdirent
*nfs_readdir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
1748 struct nfsdirent
*nfsdirent
= nfsdir
->current
;
1750 if (nfsdir
->current
!= NULL
) {
1751 nfsdir
->current
= nfsdir
->current
->next
;
1757 void nfs_closedir(struct nfs_context
*nfs _U_
, struct nfsdir
*nfsdir
)
1759 nfs_free_nfsdir(nfsdir
);
1771 struct lseek_cb_data
{
1772 struct nfs_context
*nfs
;
1773 struct nfsfh
*nfsfh
;
1779 static void nfs_lseek_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1782 struct lseek_cb_data
*data
= private_data
;
1783 struct nfs_context
*nfs
= data
->nfs
;
1785 if (status
== RPC_STATUS_ERROR
) {
1786 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1790 if (status
== RPC_STATUS_CANCEL
) {
1791 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1797 if (res
->status
!= NFS3_OK
) {
1798 rpc_set_error(nfs
->rpc
, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1799 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1804 data
->nfsfh
->offset
= data
->offset
+ res
->GETATTR3res_u
.resok
.obj_attributes
.size
;
1805 data
->cb(0, nfs
, &data
->nfsfh
->offset
, data
->private_data
);
1809 int nfs_lseek_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, off_t offset
, int whence
, nfs_cb cb
, void *private_data
)
1811 struct lseek_cb_data
*data
;
1813 if (whence
== SEEK_SET
) {
1814 nfsfh
->offset
= offset
;
1815 cb(0, nfs
, &nfsfh
->offset
, private_data
);
1818 if (whence
== SEEK_CUR
) {
1819 nfsfh
->offset
+= offset
;
1820 cb(0, nfs
, &nfsfh
->offset
, private_data
);
1824 data
= malloc(sizeof(struct lseek_cb_data
));
1826 rpc_set_error(nfs
->rpc
, "Out Of Memory: Failed to malloc lseek cb data");
1831 data
->nfsfh
= nfsfh
;
1832 data
->offset
= offset
;
1834 data
->private_data
= private_data
;
1836 if (rpc_nfs_getattr_async(nfs
->rpc
, nfs_lseek_1_cb
, &nfsfh
->fh
, data
) != 0) {
1837 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LSEEK GETATTR call");
1850 static void nfs_statvfs_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1853 struct nfs_cb_data
*data
= private_data
;
1854 struct nfs_context
*nfs
= data
->nfs
;
1855 struct statvfs svfs
;
1857 if (status
== RPC_STATUS_ERROR
) {
1858 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1859 free_nfs_cb_data(data
);
1862 if (status
== RPC_STATUS_CANCEL
) {
1863 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1864 free_nfs_cb_data(data
);
1869 if (res
->status
!= NFS3_OK
) {
1870 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
));
1871 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1872 free_nfs_cb_data(data
);
1876 svfs
.f_bsize
= 4096;
1877 svfs
.f_frsize
= 4096;
1878 svfs
.f_blocks
= res
->FSSTAT3res_u
.resok
.tbytes
/4096;
1879 svfs
.f_bfree
= res
->FSSTAT3res_u
.resok
.fbytes
/4096;
1880 svfs
.f_bavail
= res
->FSSTAT3res_u
.resok
.abytes
/4096;
1881 svfs
.f_files
= res
->FSSTAT3res_u
.resok
.tfiles
;
1882 svfs
.f_ffree
= res
->FSSTAT3res_u
.resok
.ffiles
;
1883 svfs
.f_favail
= res
->FSSTAT3res_u
.resok
.afiles
;
1886 svfs
.f_namemax
= 256;
1888 data
->cb(0, nfs
, &svfs
, data
->private_data
);
1889 free_nfs_cb_data(data
);
1892 static int nfs_statvfs_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1894 if (rpc_nfs_fsstat_async(nfs
->rpc
, nfs_statvfs_1_cb
, &data
->fh
, data
) != 0) {
1895 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send FSSTAT call for %s", data
->path
);
1896 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1897 free_nfs_cb_data(data
);
1903 int nfs_statvfs_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1905 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_statvfs_continue_internal
, NULL
, NULL
, 0) != 0) {
1906 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1919 static void nfs_readlink_1_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1922 struct nfs_cb_data
*data
= private_data
;
1923 struct nfs_context
*nfs
= data
->nfs
;
1925 if (status
== RPC_STATUS_ERROR
) {
1926 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1927 free_nfs_cb_data(data
);
1930 if (status
== RPC_STATUS_CANCEL
) {
1931 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1932 free_nfs_cb_data(data
);
1937 if (res
->status
!= NFS3_OK
) {
1938 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
));
1939 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1940 free_nfs_cb_data(data
);
1945 data
->cb(0, nfs
, res
->READLINK3res_u
.resok
.data
, data
->private_data
);
1946 free_nfs_cb_data(data
);
1949 static int nfs_readlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
1951 if (rpc_nfs_readlink_async(nfs
->rpc
, nfs_readlink_1_cb
, &data
->fh
, data
) != 0) {
1952 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send READLINK call for %s", data
->path
);
1953 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1954 free_nfs_cb_data(data
);
1960 int nfs_readlink_async(struct nfs_context
*nfs
, const char *path
, nfs_cb cb
, void *private_data
)
1962 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_readlink_continue_internal
, NULL
, NULL
, 0) != 0) {
1963 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
1976 static void nfs_chmod_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
1978 struct nfs_cb_data
*data
= private_data
;
1979 struct nfs_context
*nfs
= data
->nfs
;
1982 if (status
== RPC_STATUS_ERROR
) {
1983 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
1984 free_nfs_cb_data(data
);
1987 if (status
== RPC_STATUS_CANCEL
) {
1988 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
1989 free_nfs_cb_data(data
);
1994 if (res
->status
!= NFS3_OK
) {
1995 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
1996 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
1997 free_nfs_cb_data(data
);
2001 data
->cb(0, nfs
, NULL
, data
->private_data
);
2002 free_nfs_cb_data(data
);
2005 static int nfs_chmod_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2009 bzero(&args
, sizeof(SETATTR3args
));
2010 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2011 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2012 args
.new_attributes
.mode
.set_it
= 1;
2013 args
.new_attributes
.mode
.set_mode3_u
.mode
= data
->continue_int
;
2015 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chmod_cb
, &args
, data
) != 0) {
2016 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2017 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2018 free_nfs_cb_data(data
);
2025 int nfs_chmod_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2027 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chmod_continue_internal
, NULL
, NULL
, mode
) != 0) {
2028 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2038 int nfs_fchmod_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
, nfs_cb cb
, void *private_data
)
2040 struct nfs_cb_data
*data
;
2042 data
= malloc(sizeof(struct nfs_cb_data
));
2044 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for nfs mount data");
2047 bzero(data
, sizeof(struct nfs_cb_data
));
2050 data
->private_data
= private_data
;
2051 data
->continue_int
= mode
;
2052 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2053 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2054 if (data
->fh
.data
.data_val
== NULL
) {
2055 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2056 free_nfs_cb_data(data
);
2059 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2061 if (nfs_chmod_continue_internal(nfs
, data
) != 0) {
2062 free_nfs_cb_data(data
);
2074 static void nfs_chown_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2076 struct nfs_cb_data
*data
= private_data
;
2077 struct nfs_context
*nfs
= data
->nfs
;
2080 if (status
== RPC_STATUS_ERROR
) {
2081 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2082 free_nfs_cb_data(data
);
2085 if (status
== RPC_STATUS_CANCEL
) {
2086 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2087 free_nfs_cb_data(data
);
2092 if (res
->status
!= NFS3_OK
) {
2093 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2094 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2095 free_nfs_cb_data(data
);
2099 data
->cb(0, nfs
, NULL
, data
->private_data
);
2100 free_nfs_cb_data(data
);
2103 struct nfs_chown_data
{
2108 static int nfs_chown_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2111 struct nfs_chown_data
*chown_data
= data
->continue_data
;
2113 bzero(&args
, sizeof(SETATTR3args
));
2114 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2115 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2116 if (chown_data
->uid
!= (uid_t
)-1) {
2117 args
.new_attributes
.uid
.set_it
= 1;
2118 args
.new_attributes
.uid
.set_uid3_u
.uid
= chown_data
->uid
;
2120 if (chown_data
->gid
!= (gid_t
)-1) {
2121 args
.new_attributes
.gid
.set_it
= 1;
2122 args
.new_attributes
.gid
.set_gid3_u
.gid
= chown_data
->gid
;
2125 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_chown_cb
, &args
, data
) != 0) {
2126 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2127 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2128 free_nfs_cb_data(data
);
2135 int nfs_chown_async(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2137 struct nfs_chown_data
*chown_data
;
2139 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2140 if (chown_data
== NULL
) {
2141 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2145 chown_data
->uid
= uid
;
2146 chown_data
->gid
= gid
;
2148 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_chown_continue_internal
, chown_data
, free
, 0) != 0) {
2149 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2160 int nfs_fchown_async(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
, nfs_cb cb
, void *private_data
)
2162 struct nfs_cb_data
*data
;
2163 struct nfs_chown_data
*chown_data
;
2165 chown_data
= malloc(sizeof(struct nfs_chown_data
));
2166 if (chown_data
== NULL
) {
2167 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for chown data structure");
2171 chown_data
->uid
= uid
;
2172 chown_data
->gid
= gid
;
2175 data
= malloc(sizeof(struct nfs_cb_data
));
2177 rpc_set_error(nfs
->rpc
, "out of memory. failed to allocate memory for fchown data");
2180 bzero(data
, sizeof(struct nfs_cb_data
));
2183 data
->private_data
= private_data
;
2184 data
->continue_data
= chown_data
;
2185 data
->fh
.data
.data_len
= nfsfh
->fh
.data
.data_len
;
2186 data
->fh
.data
.data_val
= malloc(data
->fh
.data
.data_len
);
2187 if (data
->fh
.data
.data_val
== NULL
) {
2188 rpc_set_error(nfs
->rpc
, "Out of memory: Failed to allocate fh");
2189 free_nfs_cb_data(data
);
2192 memcpy(data
->fh
.data
.data_val
, nfsfh
->fh
.data
.data_val
, data
->fh
.data
.data_len
);
2195 if (nfs_chown_continue_internal(nfs
, data
) != 0) {
2196 free_nfs_cb_data(data
);
2210 static void nfs_utimes_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2212 struct nfs_cb_data
*data
= private_data
;
2213 struct nfs_context
*nfs
= data
->nfs
;
2216 if (status
== RPC_STATUS_ERROR
) {
2217 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2218 free_nfs_cb_data(data
);
2221 if (status
== RPC_STATUS_CANCEL
) {
2222 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2223 free_nfs_cb_data(data
);
2228 if (res
->status
!= NFS3_OK
) {
2229 rpc_set_error(nfs
->rpc
, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res
->status
), nfsstat3_to_errno(res
->status
));
2230 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2231 free_nfs_cb_data(data
);
2235 data
->cb(0, nfs
, NULL
, data
->private_data
);
2236 free_nfs_cb_data(data
);
2239 static int nfs_utimes_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2242 struct timeval
*utimes_data
= data
->continue_data
;
2244 bzero(&args
, sizeof(SETATTR3args
));
2245 args
.object
.data
.data_len
= data
->fh
.data
.data_len
;
2246 args
.object
.data
.data_val
= data
->fh
.data
.data_val
;
2247 if (utimes_data
!= NULL
) {
2248 args
.new_attributes
.atime
.set_it
= SET_TO_CLIENT_TIME
;
2249 args
.new_attributes
.atime
.set_atime_u
.atime
.seconds
= utimes_data
[0].tv_sec
;
2250 args
.new_attributes
.atime
.set_atime_u
.atime
.nseconds
= utimes_data
[0].tv_usec
* 1000;
2251 args
.new_attributes
.mtime
.set_it
= SET_TO_CLIENT_TIME
;
2252 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.seconds
= utimes_data
[1].tv_sec
;
2253 args
.new_attributes
.mtime
.set_mtime_u
.mtime
.nseconds
= utimes_data
[1].tv_usec
* 1000;
2255 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
2256 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
2259 if (rpc_nfs_setattr_async(nfs
->rpc
, nfs_utimes_cb
, &args
, data
) != 0) {
2260 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SETATTR call for %s", data
->path
);
2261 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2262 free_nfs_cb_data(data
);
2269 int nfs_utimes_async(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
, nfs_cb cb
, void *private_data
)
2271 struct timeval
*new_times
= NULL
;
2273 if (times
!= NULL
) {
2274 new_times
= malloc(sizeof(struct timeval
)*2);
2275 if (new_times
== NULL
) {
2276 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2280 memcpy(new_times
, times
, sizeof(struct timeval
)*2);
2283 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2284 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2294 int nfs_utime_async(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
, nfs_cb cb
, void *private_data
)
2296 struct timeval
*new_times
= NULL
;
2298 if (times
!= NULL
) {
2299 new_times
= malloc(sizeof(struct timeval
)*2);
2300 if (new_times
== NULL
) {
2301 rpc_set_error(nfs
->rpc
, "Failed to allocate memory for timeval structure");
2305 new_times
[0].tv_sec
= times
->actime
;
2306 new_times
[0].tv_usec
= 0;
2307 new_times
[1].tv_sec
= times
->modtime
;
2308 new_times
[1].tv_usec
= 0;
2311 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_utimes_continue_internal
, new_times
, free
, 0) != 0) {
2312 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2326 static void nfs_access_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2329 struct nfs_cb_data
*data
= private_data
;
2330 struct nfs_context
*nfs
= data
->nfs
;
2331 unsigned int nfsmode
= 0;
2333 if (status
== RPC_STATUS_ERROR
) {
2334 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2335 free_nfs_cb_data(data
);
2338 if (status
== RPC_STATUS_CANCEL
) {
2339 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2340 free_nfs_cb_data(data
);
2345 if (res
->status
!= NFS3_OK
) {
2346 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
));
2347 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2348 free_nfs_cb_data(data
);
2352 if (data
->continue_int
& R_OK
) {
2353 nfsmode
|= ACCESS3_READ
;
2355 if (data
->continue_int
& W_OK
) {
2356 nfsmode
|= ACCESS3_MODIFY
;
2358 if (data
->continue_int
& X_OK
) {
2359 nfsmode
|= ACCESS3_EXECUTE
;
2362 if (res
->ACCESS3res_u
.resok
.access
!= nfsmode
) {
2363 rpc_set_error(nfs
->rpc
, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2364 nfsmode
&ACCESS3_READ
?'r':'-',
2365 nfsmode
&ACCESS3_MODIFY
?'w':'-',
2366 nfsmode
&ACCESS3_EXECUTE
?'x':'-',
2367 res
->ACCESS3res_u
.resok
.access
&ACCESS3_READ
?'r':'-',
2368 res
->ACCESS3res_u
.resok
.access
&ACCESS3_MODIFY
?'w':'-',
2369 res
->ACCESS3res_u
.resok
.access
&ACCESS3_EXECUTE
?'x':'-');
2370 data
->cb(-EACCES
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2371 free_nfs_cb_data(data
);
2375 data
->cb(0, nfs
, NULL
, data
->private_data
);
2376 free_nfs_cb_data(data
);
2379 static int nfs_access_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2383 if (data
->continue_int
& R_OK
) {
2384 nfsmode
|= ACCESS3_READ
;
2386 if (data
->continue_int
& W_OK
) {
2387 nfsmode
|= ACCESS3_MODIFY
;
2389 if (data
->continue_int
& X_OK
) {
2390 nfsmode
|= ACCESS3_EXECUTE
;
2393 if (rpc_nfs_access_async(nfs
->rpc
, nfs_access_cb
, &data
->fh
, nfsmode
, data
) != 0) {
2394 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send OPEN ACCESS call for %s", data
->path
);
2395 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2396 free_nfs_cb_data(data
);
2402 int nfs_access_async(struct nfs_context
*nfs
, const char *path
, int mode
, nfs_cb cb
, void *private_data
)
2404 if (nfs_lookuppath_async(nfs
, path
, cb
, private_data
, nfs_access_continue_internal
, NULL
, NULL
, mode
) != 0) {
2405 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2417 struct nfs_symlink_data
{
2419 char *newpathparent
;
2420 char *newpathobject
;
2423 static void free_nfs_symlink_data(void *mem
)
2425 struct nfs_symlink_data
*data
= mem
;
2427 if (data
->oldpath
!= NULL
) {
2428 free(data
->oldpath
);
2430 if (data
->newpathparent
!= NULL
) {
2431 free(data
->newpathparent
);
2433 if (data
->newpathobject
!= NULL
) {
2434 free(data
->newpathobject
);
2439 static void nfs_symlink_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2442 struct nfs_cb_data
*data
= private_data
;
2443 struct nfs_context
*nfs
= data
->nfs
;
2444 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
2446 if (status
== RPC_STATUS_ERROR
) {
2447 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2448 free_nfs_cb_data(data
);
2451 if (status
== RPC_STATUS_CANCEL
) {
2452 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2453 free_nfs_cb_data(data
);
2458 if (res
->status
!= NFS3_OK
) {
2459 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
));
2460 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2461 free_nfs_cb_data(data
);
2465 data
->cb(0, nfs
, NULL
, data
->private_data
);
2466 free_nfs_cb_data(data
);
2469 static int nfs_symlink_continue_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2471 struct nfs_symlink_data
*symlink_data
= data
->continue_data
;
2473 if (rpc_nfs_symlink_async(nfs
->rpc
, nfs_symlink_cb
, &data
->fh
, symlink_data
->newpathobject
, symlink_data
->oldpath
, data
) != 0) {
2474 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send SYMLINK call for %s", data
->path
);
2475 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2476 free_nfs_cb_data(data
);
2482 int nfs_symlink_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
2485 struct nfs_symlink_data
*symlink_data
;
2487 symlink_data
= malloc(sizeof(struct nfs_symlink_data
));
2488 if (symlink_data
== NULL
) {
2489 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for symlink data");
2492 bzero(symlink_data
, sizeof(struct nfs_symlink_data
));
2494 symlink_data
->oldpath
= strdup(oldpath
);
2495 if (symlink_data
->oldpath
== NULL
) {
2496 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
2497 free_nfs_symlink_data(symlink_data
);
2501 symlink_data
->newpathparent
= strdup(newpath
);
2502 if (symlink_data
->newpathparent
== NULL
) {
2503 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
2504 free_nfs_symlink_data(symlink_data
);
2508 ptr
= rindex(symlink_data
->newpathparent
, '/');
2510 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
2511 free_nfs_symlink_data(symlink_data
);
2517 symlink_data
->newpathobject
= strdup(ptr
);
2518 if (symlink_data
->newpathobject
== NULL
) {
2519 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate mode buffer for new path");
2520 free_nfs_symlink_data(symlink_data
);
2524 if (nfs_lookuppath_async(nfs
, symlink_data
->newpathparent
, cb
, private_data
, nfs_symlink_continue_internal
, symlink_data
, free_nfs_symlink_data
, 0) != 0) {
2525 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2537 struct nfs_rename_data
{
2540 struct nfs_fh3 olddir
;
2543 struct nfs_fh3 newdir
;
2546 static void free_nfs_rename_data(void *mem
)
2548 struct nfs_rename_data
*data
= mem
;
2550 if (data
->oldpath
!= NULL
) {
2551 free(data
->oldpath
);
2553 if (data
->olddir
.data
.data_val
!= NULL
) {
2554 free(data
->olddir
.data
.data_val
);
2556 if (data
->newpath
!= NULL
) {
2557 free(data
->newpath
);
2559 if (data
->newdir
.data
.data_val
!= NULL
) {
2560 free(data
->newdir
.data
.data_val
);
2565 static void nfs_rename_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2568 struct nfs_cb_data
*data
= private_data
;
2569 struct nfs_context
*nfs
= data
->nfs
;
2570 struct nfs_rename_data
*rename_data
= data
->continue_data
;
2572 if (status
== RPC_STATUS_ERROR
) {
2573 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2574 free_nfs_cb_data(data
);
2577 if (status
== RPC_STATUS_CANCEL
) {
2578 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2579 free_nfs_cb_data(data
);
2584 if (res
->status
!= NFS3_OK
) {
2585 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
));
2586 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2587 free_nfs_cb_data(data
);
2591 data
->cb(0, nfs
, NULL
, data
->private_data
);
2592 free_nfs_cb_data(data
);
2595 static int nfs_rename_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2597 struct nfs_rename_data
*rename_data
= data
->continue_data
;
2599 /* steal the filehandle */
2600 rename_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
2601 rename_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
2602 data
->fh
.data
.data_val
= NULL
;
2604 if (rpc_nfs_rename_async(nfs
->rpc
, nfs_rename_cb
, &rename_data
->olddir
, rename_data
->oldobject
, &rename_data
->newdir
, rename_data
->newobject
, data
) != 0) {
2605 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send RENAME call for %s", data
->path
);
2606 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2607 free_nfs_cb_data(data
);
2614 static int nfs_rename_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2616 struct nfs_rename_data
*rename_data
= data
->continue_data
;
2618 /* steal the filehandle */
2619 rename_data
->olddir
.data
.data_len
= data
->fh
.data
.data_len
;
2620 rename_data
->olddir
.data
.data_val
= data
->fh
.data
.data_val
;
2621 data
->fh
.data
.data_val
= NULL
;
2623 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) {
2624 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", rename_data
->newpath
);
2625 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2626 free_nfs_cb_data(data
);
2629 data
->continue_data
= NULL
;
2630 free_nfs_cb_data(data
);
2636 int nfs_rename_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
2639 struct nfs_rename_data
*rename_data
;
2641 rename_data
= malloc(sizeof(struct nfs_rename_data
));
2642 if (rename_data
== NULL
) {
2643 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for rename data");
2646 bzero(rename_data
, sizeof(struct nfs_rename_data
));
2648 rename_data
->oldpath
= strdup(oldpath
);
2649 if (rename_data
->oldpath
== NULL
) {
2650 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
2651 free_nfs_rename_data(rename_data
);
2654 ptr
= rindex(rename_data
->oldpath
, '/');
2656 rpc_set_error(nfs
->rpc
, "Invalid path %s", oldpath
);
2657 free_nfs_rename_data(rename_data
);
2662 rename_data
->oldobject
= ptr
;
2665 rename_data
->newpath
= strdup(newpath
);
2666 if (rename_data
->newpath
== NULL
) {
2667 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
2668 free_nfs_rename_data(rename_data
);
2671 ptr
= rindex(rename_data
->newpath
, '/');
2673 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
2674 free_nfs_rename_data(rename_data
);
2679 rename_data
->newobject
= ptr
;
2682 if (nfs_lookuppath_async(nfs
, rename_data
->oldpath
, cb
, private_data
, nfs_rename_continue_1_internal
, rename_data
, free_nfs_rename_data
, 0) != 0) {
2683 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2694 struct nfs_link_data
{
2696 struct nfs_fh3 oldfh
;
2699 struct nfs_fh3 newdir
;
2702 static void free_nfs_link_data(void *mem
)
2704 struct nfs_link_data
*data
= mem
;
2706 if (data
->oldpath
!= NULL
) {
2707 free(data
->oldpath
);
2709 if (data
->oldfh
.data
.data_val
!= NULL
) {
2710 free(data
->oldfh
.data
.data_val
);
2712 if (data
->newpath
!= NULL
) {
2713 free(data
->newpath
);
2715 if (data
->newdir
.data
.data_val
!= NULL
) {
2716 free(data
->newdir
.data
.data_val
);
2721 static void nfs_link_cb(struct rpc_context
*rpc _U_
, int status
, void *command_data
, void *private_data
)
2724 struct nfs_cb_data
*data
= private_data
;
2725 struct nfs_context
*nfs
= data
->nfs
;
2726 struct nfs_link_data
*link_data
= data
->continue_data
;
2728 if (status
== RPC_STATUS_ERROR
) {
2729 data
->cb(-EFAULT
, nfs
, command_data
, data
->private_data
);
2730 free_nfs_cb_data(data
);
2733 if (status
== RPC_STATUS_CANCEL
) {
2734 data
->cb(-EINTR
, nfs
, "Command was cancelled", data
->private_data
);
2735 free_nfs_cb_data(data
);
2740 if (res
->status
!= NFS3_OK
) {
2741 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
));
2742 data
->cb(nfsstat3_to_errno(res
->status
), nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2743 free_nfs_cb_data(data
);
2747 data
->cb(0, nfs
, NULL
, data
->private_data
);
2748 free_nfs_cb_data(data
);
2751 static int nfs_link_continue_2_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2753 struct nfs_link_data
*link_data
= data
->continue_data
;
2755 /* steal the filehandle */
2756 link_data
->newdir
.data
.data_len
= data
->fh
.data
.data_len
;
2757 link_data
->newdir
.data
.data_val
= data
->fh
.data
.data_val
;
2758 data
->fh
.data
.data_val
= NULL
;
2760 if (rpc_nfs_link_async(nfs
->rpc
, nfs_link_cb
, &link_data
->oldfh
, &link_data
->newdir
, link_data
->newobject
, data
) != 0) {
2761 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LINK call for %s", data
->path
);
2762 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2763 free_nfs_cb_data(data
);
2770 static int nfs_link_continue_1_internal(struct nfs_context
*nfs
, struct nfs_cb_data
*data
)
2772 struct nfs_link_data
*link_data
= data
->continue_data
;
2774 /* steal the filehandle */
2775 link_data
->oldfh
.data
.data_len
= data
->fh
.data
.data_len
;
2776 link_data
->oldfh
.data
.data_val
= data
->fh
.data
.data_val
;
2777 data
->fh
.data
.data_val
= NULL
;
2779 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) {
2780 rpc_set_error(nfs
->rpc
, "RPC error: Failed to send LOOKUP call for %s", link_data
->newpath
);
2781 data
->cb(-ENOMEM
, nfs
, rpc_get_error(nfs
->rpc
), data
->private_data
);
2782 free_nfs_cb_data(data
);
2785 data
->continue_data
= NULL
;
2786 free_nfs_cb_data(data
);
2792 int nfs_link_async(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
, nfs_cb cb
, void *private_data
)
2795 struct nfs_link_data
*link_data
;
2797 link_data
= malloc(sizeof(struct nfs_link_data
));
2798 if (link_data
== NULL
) {
2799 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for link data");
2802 bzero(link_data
, sizeof(struct nfs_link_data
));
2804 link_data
->oldpath
= strdup(oldpath
);
2805 if (link_data
->oldpath
== NULL
) {
2806 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for oldpath");
2807 free_nfs_link_data(link_data
);
2811 link_data
->newpath
= strdup(newpath
);
2812 if (link_data
->newpath
== NULL
) {
2813 rpc_set_error(nfs
->rpc
, "Out of memory, failed to allocate buffer for newpath");
2814 free_nfs_link_data(link_data
);
2817 ptr
= rindex(link_data
->newpath
, '/');
2819 rpc_set_error(nfs
->rpc
, "Invalid path %s", newpath
);
2820 free_nfs_link_data(link_data
);
2825 link_data
->newobject
= ptr
;
2828 if (nfs_lookuppath_async(nfs
, link_data
->oldpath
, cb
, private_data
, nfs_link_continue_1_internal
, link_data
, free_nfs_link_data
, 0) != 0) {
2829 rpc_set_error(nfs
->rpc
, "Out of memory: failed to start parsing the path components");
2837 //qqq replace later with lseek()
2838 off_t
nfs_get_current_offset(struct nfsfh
*nfsfh
)
2840 return nfsfh
->offset
;
2846 * Get the maximum supported READ3 size by the server
2848 size_t nfs_get_readmax(struct nfs_context
*nfs
)
2850 return nfs
->readmax
;
2854 * Get the maximum supported WRITE3 size by the server
2856 size_t nfs_get_writemax(struct nfs_context
*nfs
)
2858 return nfs
->writemax
;
2861 void nfs_set_error(struct nfs_context
*nfs
, char *error_string
, ...)
2866 if (nfs
->rpc
->error_string
!= NULL
) {
2867 free(nfs
->rpc
->error_string
);
2869 va_start(ap
, error_string
);
2870 vasprintf(&str
, error_string
, ap
);
2871 nfs
->rpc
->error_string
= str
;