Allow nfs_lookuppath_async to have path[0]=='\0' this happens when we
[deb_libnfs.git] / lib / libnfs.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
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.
8
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.
13
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/>.
16*/
17/*
18 * High level api to nfs filesystems
19 */
00748f36
RS
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
108c622a
RS
24#ifdef AROS
25#include "aros_compat.h"
00748f36
RS
26#endif
27
a8a1b858
M
28#ifdef WIN32
29#include "win32_compat.h"
6874f61e
RS
30#else
31#include <strings.h>
d7c6e9aa 32#include <utime.h>
108c622a 33#endif /*WIN32*/
d7c6e9aa 34
d7c6e9aa 35#ifdef ANDROID
252aa90d 36#define statvfs statfs
108c622a 37#endif
a8a1b858
M
38
39#define _GNU_SOURCE
6874f61e 40
108c622a
RS
41#ifdef HAVE_UNISTD_H
42#include <unistd.h>
43#endif
44
45#ifdef HAVE_SYS_VFS_H
46#include <sys/vfs.h>
47#endif
48
49#ifdef HAVE_SYS_STATVFS_H
50#include <sys/statvfs.h>
51#endif
52
84004dbf 53#include <stdio.h>
1896d37b 54#include <stdarg.h>
84004dbf
RS
55#include <stdlib.h>
56#include <string.h>
f3a75078 57#include <assert.h>
84004dbf
RS
58#include <errno.h>
59#include <sys/types.h>
60#include <sys/stat.h>
84004dbf 61#include <fcntl.h>
763cd6e3
RS
62#include <netinet/in.h>
63#include "libnfs-zdr.h"
84004dbf
RS
64#include "libnfs.h"
65#include "libnfs-raw.h"
66#include "libnfs-raw-mount.h"
67#include "libnfs-raw-nfs.h"
1896d37b
RS
68#include "libnfs-private.h"
69
70struct nfsdir {
71 struct nfsdirent *entries;
72 struct nfsdirent *current;
73};
84004dbf
RS
74
75struct nfsfh {
76 struct nfs_fh3 fh;
77 int is_sync;
183451cf 78 uint64_t offset;
84004dbf
RS
79};
80
1896d37b
RS
81struct nfs_context {
82 struct rpc_context *rpc;
83 char *server;
84 char *export;
85 struct nfs_fh3 rootfh;
183451cf
RS
86 uint64_t readmax;
87 uint64_t writemax;
84004dbf
RS
88};
89
90void nfs_free_nfsdir(struct nfsdir *nfsdir)
91{
92 while (nfsdir->entries) {
93 struct nfsdirent *dirent = nfsdir->entries->next;
94 if (nfsdir->entries->name != NULL) {
95 free(nfsdir->entries->name);
96 }
97 free(nfsdir->entries);
98 nfsdir->entries = dirent;
99 }
100 free(nfsdir);
101}
102
84004dbf
RS
103struct nfs_cb_data;
104typedef int (*continue_func)(struct nfs_context *nfs, struct nfs_cb_data *data);
105
106struct nfs_cb_data {
107 struct nfs_context *nfs;
108 struct nfsfh *nfsfh;
109 char *saved_path, *path;
110
111 nfs_cb cb;
112 void *private_data;
113
114 continue_func continue_cb;
115 void *continue_data;
116 void (*free_continue_data)(void *);
117 int continue_int;
118
119 struct nfs_fh3 fh;
921f877b
RS
120
121 /* for multi-read/write calls. */
122 int error;
123 int cancel;
124 int num_calls;
183451cf 125 uint64_t start_offset, max_offset;
921f877b
RS
126 char *buffer;
127};
128
129struct nfs_mcb_data {
130 struct nfs_cb_data *data;
183451cf
RS
131 uint64_t offset;
132 uint64_t count;
84004dbf
RS
133};
134
135static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh);
136
137
67ba2239 138void nfs_set_auth(struct nfs_context *nfs, struct AUTH *auth)
84004dbf 139{
9896c715 140 rpc_set_auth(nfs->rpc, auth);
84004dbf
RS
141}
142
143int nfs_get_fd(struct nfs_context *nfs)
144{
145 return rpc_get_fd(nfs->rpc);
146}
147
83aa785d
RS
148int nfs_queue_length(struct nfs_context *nfs)
149{
150 return rpc_queue_length(nfs->rpc);
151}
152
84004dbf
RS
153int nfs_which_events(struct nfs_context *nfs)
154{
155 return rpc_which_events(nfs->rpc);
156}
157
158int nfs_service(struct nfs_context *nfs, int revents)
159{
160 return rpc_service(nfs->rpc, revents);
161}
162
163char *nfs_get_error(struct nfs_context *nfs)
164{
165 return rpc_get_error(nfs->rpc);
166};
167
168struct nfs_context *nfs_init_context(void)
169{
170 struct nfs_context *nfs;
171
172 nfs = malloc(sizeof(struct nfs_context));
173 if (nfs == NULL) {
84004dbf
RS
174 return NULL;
175 }
176 nfs->rpc = rpc_init_context();
177 if (nfs->rpc == NULL) {
84004dbf
RS
178 free(nfs);
179 return NULL;
180 }
181
b077fdeb
RS
182 nfs->server = NULL;
183 nfs->export = NULL;
184
963c2f83
RS
185 nfs->rootfh.data.data_len = 0;
186 nfs->rootfh.data.data_val = NULL;
187
84004dbf
RS
188 return nfs;
189}
190
191void nfs_destroy_context(struct nfs_context *nfs)
192{
193 rpc_destroy_context(nfs->rpc);
194 nfs->rpc = NULL;
195
196 if (nfs->server) {
197 free(nfs->server);
198 nfs->server = NULL;
199 }
200
201 if (nfs->export) {
202 free(nfs->export);
203 nfs->export = NULL;
204 }
205
206 if (nfs->rootfh.data.data_val != NULL) {
207 free(nfs->rootfh.data.data_val);
208 nfs->rootfh.data.data_val = NULL;
209 }
210
211 free(nfs);
212}
213
214void free_nfs_cb_data(struct nfs_cb_data *data)
215{
216 if (data->saved_path != NULL) {
217 free(data->saved_path);
218 data->saved_path = NULL;
219 }
220
221 if (data->continue_data != NULL) {
222 data->free_continue_data(data->continue_data);
223 data->continue_data = NULL;
224 }
225
226 if (data->fh.data.data_val != NULL) {
227 free(data->fh.data.data_val);
228 data->fh.data.data_val = NULL;
229 }
230
921f877b
RS
231 if (data->buffer != NULL) {
232 free(data->buffer);
233 data->buffer = NULL;
234 }
235
84004dbf
RS
236 free(data);
237}
238
239
240
241
242
f3a75078 243static void nfs_mount_10_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
244{
245 struct nfs_cb_data *data = private_data;
246 struct nfs_context *nfs = data->nfs;
247
f3a75078
RS
248 assert(rpc->magic == RPC_CONTEXT_MAGIC);
249
84004dbf
RS
250 if (status == RPC_STATUS_ERROR) {
251 data->cb(-EFAULT, nfs, command_data, data->private_data);
252 free_nfs_cb_data(data);
253 return;
254 }
255 if (status == RPC_STATUS_CANCEL) {
256 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
257 free_nfs_cb_data(data);
258 return;
259 }
260
261 data->cb(0, nfs, NULL, data->private_data);
262 free_nfs_cb_data(data);
263}
264
17ef62fa 265static void nfs_mount_9_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
266{
267 struct nfs_cb_data *data = private_data;
268 struct nfs_context *nfs = data->nfs;
17ef62fa 269 FSINFO3res *res = command_data;
84004dbf 270
f3a75078
RS
271 assert(rpc->magic == RPC_CONTEXT_MAGIC);
272
84004dbf
RS
273 if (status == RPC_STATUS_ERROR) {
274 data->cb(-EFAULT, nfs, command_data, data->private_data);
275 free_nfs_cb_data(data);
276 return;
277 }
278 if (status == RPC_STATUS_CANCEL) {
279 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
280 free_nfs_cb_data(data);
281 return;
282 }
283
17ef62fa
RS
284 nfs->readmax = res->FSINFO3res_u.resok.rtmax;
285 nfs->writemax = res->FSINFO3res_u.resok.wtmax;
84004dbf 286
17ef62fa 287 if (rpc_nfs_getattr_async(rpc, nfs_mount_10_cb, &nfs->rootfh, data) != 0) {
84004dbf
RS
288 data->cb(-ENOMEM, nfs, command_data, data->private_data);
289 free_nfs_cb_data(data);
290 return;
291 }
292}
293
17ef62fa
RS
294static void nfs_mount_8_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
295{
296 struct nfs_cb_data *data = private_data;
297 struct nfs_context *nfs = data->nfs;
298
f3a75078
RS
299 assert(rpc->magic == RPC_CONTEXT_MAGIC);
300
17ef62fa
RS
301 if (status == RPC_STATUS_ERROR) {
302 data->cb(-EFAULT, nfs, command_data, data->private_data);
303 free_nfs_cb_data(data);
304 return;
305 }
306 if (status == RPC_STATUS_CANCEL) {
307 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
308 free_nfs_cb_data(data);
309 return;
310 }
311
312 if (rpc_nfs_fsinfo_async(rpc, nfs_mount_9_cb, &nfs->rootfh, data) != 0) {
313 data->cb(-ENOMEM, nfs, command_data, data->private_data);
314 free_nfs_cb_data(data);
315 return;
316 }
317}
318
319
84004dbf
RS
320static void nfs_mount_7_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
321{
322 struct nfs_cb_data *data = private_data;
323 struct nfs_context *nfs = data->nfs;
324
f3a75078
RS
325 assert(rpc->magic == RPC_CONTEXT_MAGIC);
326
14a062eb
RS
327 /* Dont want any more callbacks even if the socket is closed */
328 rpc->connect_cb = NULL;
329
84004dbf
RS
330 if (status == RPC_STATUS_ERROR) {
331 data->cb(-EFAULT, nfs, command_data, data->private_data);
332 free_nfs_cb_data(data);
333 return;
334 }
335 if (status == RPC_STATUS_CANCEL) {
336 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
337 free_nfs_cb_data(data);
338 return;
339 }
340
341 if (rpc_nfs_null_async(rpc, nfs_mount_8_cb, data) != 0) {
342 data->cb(-ENOMEM, nfs, command_data, data->private_data);
343 free_nfs_cb_data(data);
344 return;
345 }
346}
347
348
349static void nfs_mount_6_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
350{
351 struct nfs_cb_data *data = private_data;
352 struct nfs_context *nfs = data->nfs;
353 mountres3 *res;
354
f3a75078
RS
355 assert(rpc->magic == RPC_CONTEXT_MAGIC);
356
84004dbf
RS
357 if (status == RPC_STATUS_ERROR) {
358 data->cb(-EFAULT, nfs, command_data, data->private_data);
359 free_nfs_cb_data(data);
360 return;
361 }
362 if (status == RPC_STATUS_CANCEL) {
363 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
364 free_nfs_cb_data(data);
365 return;
366 }
367
368 res = command_data;
369 if (res->fhs_status != MNT3_OK) {
370 rpc_set_error(rpc, "RPC error: Mount failed with error %s(%d) %s(%d)", mountstat3_to_str(res->fhs_status), res->fhs_status, strerror(-mountstat3_to_errno(res->fhs_status)), -mountstat3_to_errno(res->fhs_status));
371 data->cb(mountstat3_to_errno(res->fhs_status), nfs, rpc_get_error(rpc), data->private_data);
372 free_nfs_cb_data(data);
373 return;
374 }
375
376 nfs->rootfh.data.data_len = res->mountres3_u.mountinfo.fhandle.fhandle3_len;
377 nfs->rootfh.data.data_val = malloc(nfs->rootfh.data.data_len);
378 if (nfs->rootfh.data.data_val == NULL) {
379 rpc_set_error(rpc, "Out of memory. Could not allocate memory to store root filehandle");
380 data->cb(-ENOMEM, nfs, rpc_get_error(rpc), data->private_data);
381 free_nfs_cb_data(data);
382 return;
383 }
384 memcpy(nfs->rootfh.data.data_val, res->mountres3_u.mountinfo.fhandle.fhandle3_val, nfs->rootfh.data.data_len);
385
386 rpc_disconnect(rpc, "normal disconnect");
387 if (rpc_connect_async(rpc, nfs->server, 2049, nfs_mount_7_cb, data) != 0) {
388 data->cb(-ENOMEM, nfs, command_data, data->private_data);
389 free_nfs_cb_data(data);
390 return;
391 }
1744ef90
RS
392 /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
393 rpc_set_autoreconnect(rpc);
84004dbf
RS
394}
395
396
397static void nfs_mount_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
398{
399 struct nfs_cb_data *data = private_data;
400 struct nfs_context *nfs = data->nfs;
401
f3a75078
RS
402 assert(rpc->magic == RPC_CONTEXT_MAGIC);
403
84004dbf
RS
404 if (status == RPC_STATUS_ERROR) {
405 data->cb(-EFAULT, nfs, command_data, data->private_data);
406 free_nfs_cb_data(data);
407 return;
408 }
409 if (status == RPC_STATUS_CANCEL) {
410 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
411 free_nfs_cb_data(data);
412 return;
413 }
414
415 if (rpc_mount_mnt_async(rpc, nfs_mount_6_cb, nfs->export, data) != 0) {
416 data->cb(-ENOMEM, nfs, command_data, data->private_data);
417 free_nfs_cb_data(data);
418 return;
419 }
420}
421
422static void nfs_mount_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
423{
424 struct nfs_cb_data *data = private_data;
425 struct nfs_context *nfs = data->nfs;
426
f3a75078
RS
427 assert(rpc->magic == RPC_CONTEXT_MAGIC);
428
14a062eb
RS
429 /* Dont want any more callbacks even if the socket is closed */
430 rpc->connect_cb = NULL;
431
84004dbf
RS
432 if (status == RPC_STATUS_ERROR) {
433 data->cb(-EFAULT, nfs, command_data, data->private_data);
434 free_nfs_cb_data(data);
435 return;
436 }
437 if (status == RPC_STATUS_CANCEL) {
438 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
439 free_nfs_cb_data(data);
440 return;
441 }
442
443 if (rpc_mount_null_async(rpc, nfs_mount_5_cb, data) != 0) {
444 data->cb(-ENOMEM, nfs, command_data, data->private_data);
445 free_nfs_cb_data(data);
446 return;
447 }
448}
449
450static void nfs_mount_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
451{
452 struct nfs_cb_data *data = private_data;
453 struct nfs_context *nfs = data->nfs;
454 uint32_t mount_port;
455
f3a75078
RS
456 assert(rpc->magic == RPC_CONTEXT_MAGIC);
457
84004dbf
RS
458 if (status == RPC_STATUS_ERROR) {
459 data->cb(-EFAULT, nfs, command_data, data->private_data);
460 free_nfs_cb_data(data);
461 return;
462 }
463 if (status == RPC_STATUS_CANCEL) {
464 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
465 free_nfs_cb_data(data);
466 return;
467 }
468
469 mount_port = *(uint32_t *)command_data;
470 if (mount_port == 0) {
471 rpc_set_error(rpc, "RPC error. Mount program is not available on %s", nfs->server);
472 data->cb(-ENOENT, nfs, command_data, data->private_data);
473 free_nfs_cb_data(data);
474 return;
475 }
476
477 rpc_disconnect(rpc, "normal disconnect");
478 if (rpc_connect_async(rpc, nfs->server, mount_port, nfs_mount_4_cb, data) != 0) {
479 data->cb(-ENOMEM, nfs, command_data, data->private_data);
480 free_nfs_cb_data(data);
481 return;
482 }
483}
484
485
486static void nfs_mount_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
487{
488 struct nfs_cb_data *data = private_data;
489 struct nfs_context *nfs = data->nfs;
490
f3a75078
RS
491 assert(rpc->magic == RPC_CONTEXT_MAGIC);
492
84004dbf
RS
493 if (status == RPC_STATUS_ERROR) {
494 data->cb(-EFAULT, nfs, command_data, data->private_data);
495 free_nfs_cb_data(data);
496 return;
497 }
498 if (status == RPC_STATUS_CANCEL) {
499 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
500 free_nfs_cb_data(data);
501 return;
502 }
503
5c6b1176 504 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, nfs_mount_3_cb, private_data) != 0) {
84004dbf
RS
505 data->cb(-ENOMEM, nfs, command_data, data->private_data);
506 free_nfs_cb_data(data);
507 return;
508 }
509}
510
511static void nfs_mount_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
512{
513 struct nfs_cb_data *data = private_data;
514 struct nfs_context *nfs = data->nfs;
515
f3a75078
RS
516 assert(rpc->magic == RPC_CONTEXT_MAGIC);
517
14a062eb
RS
518 /* Dont want any more callbacks even if the socket is closed */
519 rpc->connect_cb = NULL;
520
84004dbf
RS
521 if (status == RPC_STATUS_ERROR) {
522 data->cb(-EFAULT, nfs, command_data, data->private_data);
523 free_nfs_cb_data(data);
524 return;
525 }
526 if (status == RPC_STATUS_CANCEL) {
527 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
528 free_nfs_cb_data(data);
529 return;
530 }
531
532 if (rpc_pmap_null_async(rpc, nfs_mount_2_cb, data) != 0) {
533 data->cb(-ENOMEM, nfs, command_data, data->private_data);
534 free_nfs_cb_data(data);
535 return;
536 }
537}
538
539/*
540 * Async call for mounting an nfs share and geting the root filehandle
541 */
542int nfs_mount_async(struct nfs_context *nfs, const char *server, const char *export, nfs_cb cb, void *private_data)
543{
544 struct nfs_cb_data *data;
b077fdeb 545 char *new_server, *new_export;
84004dbf
RS
546
547 data = malloc(sizeof(struct nfs_cb_data));
548 if (data == NULL) {
cbbf9d3e 549 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data");
84004dbf
RS
550 return -1;
551 }
ea98629a 552 memset(data, 0, sizeof(struct nfs_cb_data));
b077fdeb
RS
553 new_server = strdup(server);
554 new_export = strdup(export);
555 if (nfs->server != NULL) {
556 free(nfs->server);
b077fdeb
RS
557 }
558 nfs->server = new_server;
559 if (nfs->export != NULL) {
560 free(nfs->export);
b077fdeb
RS
561 }
562 nfs->export = new_export;
84004dbf
RS
563 data->nfs = nfs;
564 data->cb = cb;
565 data->private_data = private_data;
566
567 if (rpc_connect_async(nfs->rpc, server, 111, nfs_mount_1_cb, data) != 0) {
cbbf9d3e 568 rpc_set_error(nfs->rpc, "Failed to start connection");
84004dbf 569 free_nfs_cb_data(data);
cbbf9d3e 570 return -1;
84004dbf
RS
571 }
572
573 return 0;
574}
575
576
577
578/*
579 * Functions to first look up a path, component by component, and then finally call a specific function once
580 * the filehandle for the final component is found.
581 */
f3a75078 582static void nfs_lookup_path_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
583{
584 struct nfs_cb_data *data = private_data;
585 struct nfs_context *nfs = data->nfs;
586 LOOKUP3res *res;
587
f3a75078
RS
588 assert(rpc->magic == RPC_CONTEXT_MAGIC);
589
84004dbf
RS
590 if (status == RPC_STATUS_ERROR) {
591 data->cb(-EFAULT, nfs, command_data, data->private_data);
592 free_nfs_cb_data(data);
593 return;
594 }
595 if (status == RPC_STATUS_CANCEL) {
596 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
597 free_nfs_cb_data(data);
598 return;
599 }
600
601 res = command_data;
602 if (res->status != NFS3_OK) {
603 rpc_set_error(nfs->rpc, "NFS: Lookup of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
604 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
605 free_nfs_cb_data(data);
606 return;
607 }
608
609 if (nfs_lookup_path_async_internal(nfs, data, &res->LOOKUP3res_u.resok.object) != 0) {
610 rpc_set_error(nfs->rpc, "Failed to create lookup pdu");
611 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
612 free_nfs_cb_data(data);
613 return;
614 }
615}
616
617static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh)
618{
619 char *path, *str;
620
621 while (*data->path == '/') {
622 data->path++;
623 }
624
625 path = data->path;
93e9306f 626 str = strchr(path, '/');
84004dbf
RS
627 if (str != NULL) {
628 *str = 0;
629 data->path = str+1;
630 } else {
631 while (*data->path != 0) {
632 data->path++;
633 }
634 }
635
636 if (*path == 0) {
637 data->fh.data.data_len = fh->data.data_len;
638 data->fh.data.data_val = malloc(data->fh.data.data_len);
639 if (data->fh.data.data_val == NULL) {
640 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh for %s", data->path);
641 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
642 free_nfs_cb_data(data);
643 return -1;
644 }
645 memcpy(data->fh.data.data_val, fh->data.data_val, data->fh.data.data_len);
646 data->continue_cb(nfs, data);
647 return 0;
648 }
649
650 if (rpc_nfs_lookup_async(nfs->rpc, nfs_lookup_path_1_cb, fh, path, data) != 0) {
651 rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s", data->path);
652 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
653 free_nfs_cb_data(data);
654 return -1;
655 }
656 return 0;
657}
658
659static 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)
660{
661 struct nfs_cb_data *data;
662
73f4ae7c
RS
663 if (path[0] != 0 && path[0] != '/') {
664 rpc_set_error(nfs->rpc, "Pathname is not absolute %s", path);
84004dbf
RS
665 return -1;
666 }
667
668 data = malloc(sizeof(struct nfs_cb_data));
669 if (data == NULL) {
670 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
cbbf9d3e 671 return -1;
84004dbf 672 }
ea98629a 673 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
674 data->nfs = nfs;
675 data->cb = cb;
676 data->continue_cb = continue_cb;
677 data->continue_data = continue_data;
678 data->free_continue_data = free_continue_data;
679 data->continue_int = continue_int;
680 data->private_data = private_data;
681 data->saved_path = strdup(path);
682 if (data->saved_path == NULL) {
683 rpc_set_error(nfs->rpc, "out of memory: failed to copy path string");
84004dbf 684 free_nfs_cb_data(data);
cbbf9d3e 685 return -1;
84004dbf
RS
686 }
687 data->path = data->saved_path;
688
689 if (nfs_lookup_path_async_internal(nfs, data, &nfs->rootfh) != 0) {
84004dbf
RS
690 /* return 0 here since the callback will be invoked if there is a failure */
691 return 0;
692 }
693 return 0;
694}
695
696
697
698
699
700/*
701 * Async stat()
702 */
f3a75078 703static void nfs_stat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
704{
705 GETATTR3res *res;
706 struct nfs_cb_data *data = private_data;
707 struct nfs_context *nfs = data->nfs;
708 struct stat st;
709
f3a75078
RS
710 assert(rpc->magic == RPC_CONTEXT_MAGIC);
711
84004dbf
RS
712 if (status == RPC_STATUS_ERROR) {
713 data->cb(-EFAULT, nfs, command_data, data->private_data);
714 free_nfs_cb_data(data);
715 return;
716 }
717 if (status == RPC_STATUS_CANCEL) {
718 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
719 free_nfs_cb_data(data);
720 return;
721 }
722
723 res = command_data;
724 if (res->status != NFS3_OK) {
725 rpc_set_error(nfs->rpc, "NFS: GETATTR of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
726 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
727 free_nfs_cb_data(data);
728 return;
729 }
730
731 st.st_dev = -1;
732 st.st_ino = res->GETATTR3res_u.resok.obj_attributes.fileid;
733 st.st_mode = res->GETATTR3res_u.resok.obj_attributes.mode;
e8cab72a
RS
734 if (res->GETATTR3res_u.resok.obj_attributes.type == NF3DIR) {
735 st.st_mode |= S_IFDIR ;
736 }
d7ec001f
RS
737 if (res->GETATTR3res_u.resok.obj_attributes.type == NF3REG) {
738 st.st_mode |= S_IFREG ;
739 }
84004dbf
RS
740 st.st_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink;
741 st.st_uid = res->GETATTR3res_u.resok.obj_attributes.uid;
742 st.st_gid = res->GETATTR3res_u.resok.obj_attributes.gid;
743 st.st_rdev = 0;
744 st.st_size = res->GETATTR3res_u.resok.obj_attributes.size;
a8a1b858 745#ifndef WIN32
84004dbf
RS
746 st.st_blksize = 4096;
747 st.st_blocks = res->GETATTR3res_u.resok.obj_attributes.size / 4096;
a8a1b858 748#endif//WIN32
84004dbf
RS
749 st.st_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds;
750 st.st_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds;
751 st.st_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds;
752
753 data->cb(0, nfs, &st, data->private_data);
754 free_nfs_cb_data(data);
755}
756
757static int nfs_stat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
758{
759 if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &data->fh, data) != 0) {
760 rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path);
761 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
762 free_nfs_cb_data(data);
763 return -1;
764 }
765 return 0;
766}
767
768int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
769{
770 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_stat_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 771 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
772 return -1;
773 }
774
775 return 0;
776}
777
778
779
780
781
782/*
783 * Async open()
784 */
f3a75078 785static void nfs_open_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
786{
787 ACCESS3res *res;
788 struct nfs_cb_data *data = private_data;
789 struct nfs_context *nfs = data->nfs;
790 struct nfsfh *nfsfh;
791 unsigned int nfsmode = 0;
792
f3a75078
RS
793 assert(rpc->magic == RPC_CONTEXT_MAGIC);
794
84004dbf
RS
795 if (status == RPC_STATUS_ERROR) {
796 data->cb(-EFAULT, nfs, command_data, data->private_data);
797 free_nfs_cb_data(data);
798 return;
799 }
800 if (status == RPC_STATUS_CANCEL) {
801 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
802 free_nfs_cb_data(data);
803 return;
804 }
805
806 res = command_data;
807 if (res->status != NFS3_OK) {
808 rpc_set_error(nfs->rpc, "NFS: ACCESS of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
809 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
810 free_nfs_cb_data(data);
811 return;
812 }
813
814 if (data->continue_int & O_WRONLY) {
815 nfsmode |= ACCESS3_MODIFY;
816 }
817 if (data->continue_int & O_RDWR) {
818 nfsmode |= ACCESS3_READ|ACCESS3_MODIFY;
819 }
820 if (!(data->continue_int & (O_WRONLY|O_RDWR))) {
821 nfsmode |= ACCESS3_READ;
822 }
823
824
825 if (res->ACCESS3res_u.resok.access != nfsmode) {
826 rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
827 nfsmode&ACCESS3_READ?'r':'-',
828 nfsmode&ACCESS3_MODIFY?'w':'-',
829 nfsmode&ACCESS3_EXECUTE?'x':'-',
830 res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-',
831 res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-',
832 res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-');
833 data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data);
834 free_nfs_cb_data(data);
835 return;
836 }
837
838 nfsfh = malloc(sizeof(struct nfsfh));
839 if (nfsfh == NULL) {
840 rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure");
841 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
842 free_nfs_cb_data(data);
843 return;
844 }
ea98629a 845 memset(nfsfh, 0, sizeof(struct nfsfh));
84004dbf
RS
846
847 if (data->continue_int & O_SYNC) {
848 nfsfh->is_sync = 1;
849 }
850
851 /* steal the filehandle */
852 nfsfh->fh.data.data_len = data->fh.data.data_len;
853 nfsfh->fh.data.data_val = data->fh.data.data_val;
854 data->fh.data.data_val = NULL;
855
856 data->cb(0, nfs, nfsfh, data->private_data);
857 free_nfs_cb_data(data);
858}
859
860static int nfs_open_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
861{
862 int nfsmode = 0;
863
864 if (data->continue_int & O_WRONLY) {
865 nfsmode |= ACCESS3_MODIFY;
866 }
867 if (data->continue_int & O_RDWR) {
868 nfsmode |= ACCESS3_READ|ACCESS3_MODIFY;
869 }
870 if (!(data->continue_int & (O_WRONLY|O_RDWR))) {
871 nfsmode |= ACCESS3_READ;
872 }
873
874 if (rpc_nfs_access_async(nfs->rpc, nfs_open_cb, &data->fh, nfsmode, data) != 0) {
875 rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
876 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
877 free_nfs_cb_data(data);
878 return -1;
879 }
880 return 0;
881}
882
883int nfs_open_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
884{
885 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_open_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e
RS
886 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
887 return -1;
84004dbf
RS
888 }
889
890 return 0;
891}
892
893
894
895
896
897/*
898 * Async pread()
899 */
f3a75078 900static void nfs_pread_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
901{
902 struct nfs_cb_data *data = private_data;
903 struct nfs_context *nfs = data->nfs;
904 READ3res *res;
905
f3a75078
RS
906 assert(rpc->magic == RPC_CONTEXT_MAGIC);
907
84004dbf
RS
908 if (status == RPC_STATUS_ERROR) {
909 data->cb(-EFAULT, nfs, command_data, data->private_data);
910 free_nfs_cb_data(data);
911 return;
912 }
913 if (status == RPC_STATUS_CANCEL) {
914 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
915 free_nfs_cb_data(data);
916 return;
917 }
918
919 res = command_data;
920 if (res->status != NFS3_OK) {
921 rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
922 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
923 free_nfs_cb_data(data);
924 return;
925 }
926
927 data->nfsfh->offset += res->READ3res_u.resok.count;
928 data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_data);
929 free_nfs_cb_data(data);
930}
931
f3a75078 932static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
921f877b
RS
933{
934 struct nfs_mcb_data *mdata = private_data;
935 struct nfs_cb_data *data = mdata->data;
936 struct nfs_context *nfs = data->nfs;
937 READ3res *res;
938
f3a75078
RS
939 assert(rpc->magic == RPC_CONTEXT_MAGIC);
940
921f877b
RS
941 data->num_calls--;
942
943 if (status == RPC_STATUS_ERROR) {
944 /* flag the failure but do not invoke callback until we have received all responses */
945 data->error = 1;
946 }
947 if (status == RPC_STATUS_CANCEL) {
948 /* flag the cancellation but do not invoke callback until we have received all responses */
949 data->cancel = 1;
950 }
951
952 /* reassemble the data into the buffer */
953 if (status == RPC_STATUS_SUCCESS) {
954 res = command_data;
955 if (res->status != NFS3_OK) {
956 rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
957 data->error = 1;
e4a5ba42
RS
958 } else {
959 if (res->READ3res_u.resok.count > 0) {
960 memcpy(&data->buffer[mdata->offset - data->start_offset], res->READ3res_u.resok.data.data_val, res->READ3res_u.resok.count);
961 if ((unsigned)data->max_offset < mdata->offset + res->READ3res_u.resok.count) {
962 data->max_offset = mdata->offset + res->READ3res_u.resok.count;
963 }
921f877b
RS
964 }
965 }
966 }
967
968 if (data->num_calls > 0) {
969 /* still waiting for more replies */
970 free(mdata);
971 return;
972 }
973
921f877b
RS
974 if (data->error != 0) {
975 data->cb(-EFAULT, nfs, command_data, data->private_data);
976 free_nfs_cb_data(data);
977 free(mdata);
978 return;
979 }
980 if (data->cancel != 0) {
981 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
982 free_nfs_cb_data(data);
983 free(mdata);
984 return;
985 }
986
987 data->nfsfh->offset = data->max_offset;
7ed9d87a
RS
988 data->cb(data->max_offset - data->start_offset, nfs, data->buffer, data->private_data);
989
921f877b
RS
990 free_nfs_cb_data(data);
991 free(mdata);
992}
993
183451cf 994int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, nfs_cb cb, void *private_data)
84004dbf
RS
995{
996 struct nfs_cb_data *data;
997
998 data = malloc(sizeof(struct nfs_cb_data));
999 if (data == NULL) {
1000 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1001 return -1;
1002 }
ea98629a 1003 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1004 data->nfs = nfs;
1005 data->cb = cb;
1006 data->private_data = private_data;
1007 data->nfsfh = nfsfh;
1008
1009 nfsfh->offset = offset;
921f877b
RS
1010
1011 if (count <= nfs_get_readmax(nfs)) {
1012 if (rpc_nfs_read_async(nfs->rpc, nfs_pread_cb, &nfsfh->fh, offset, count, data) != 0) {
1013 rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
1014 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1015 free_nfs_cb_data(data);
1016 return -1;
1017 }
1018 return 0;
1019 }
1020
1021 /* trying to read more than maximum server read size, we has to chop it up into smaller
1022 * reads and collect into a reassembly buffer.
1023 * we send all reads in parallell so that performance is still good.
1024 */
7ed9d87a 1025 data->max_offset = offset;
921f877b
RS
1026 data->start_offset = offset;
1027
1028 data->buffer = malloc(count);
1029 if (data->buffer == NULL) {
1030 rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count);
84004dbf
RS
1031 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1032 free_nfs_cb_data(data);
1033 return -1;
1034 }
921f877b
RS
1035
1036 while (count > 0) {
183451cf 1037 uint64_t readcount = count;
921f877b
RS
1038 struct nfs_mcb_data *mdata;
1039
1040 if (readcount > nfs_get_readmax(nfs)) {
1041 readcount = nfs_get_readmax(nfs);
1042 }
1043
1044 mdata = malloc(sizeof(struct nfs_mcb_data));
1045 if (mdata == NULL) {
1046 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure");
921f877b
RS
1047 return -1;
1048 }
ea98629a 1049 memset(mdata, 0, sizeof(struct nfs_mcb_data));
921f877b
RS
1050 mdata->data = data;
1051 mdata->offset = offset;
1052 mdata->count = readcount;
921f877b
RS
1053 if (rpc_nfs_read_async(nfs->rpc, nfs_pread_mcb, &nfsfh->fh, offset, readcount, mdata) != 0) {
1054 rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
1055 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1056 free(mdata);
1057 return -1;
1058 }
1059
1060 count -= readcount;
1061 offset += readcount;
1062 data->num_calls++;
1063 }
1064
1065 return 0;
84004dbf
RS
1066}
1067
1068/*
1069 * Async read()
1070 */
183451cf 1071int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, nfs_cb cb, void *private_data)
84004dbf
RS
1072{
1073 return nfs_pread_async(nfs, nfsfh, nfsfh->offset, count, cb, private_data);
1074}
1075
1076
1077
1078/*
1079 * Async pwrite()
1080 */
f3a75078 1081static void nfs_pwrite_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1082{
1083 struct nfs_cb_data *data = private_data;
1084 struct nfs_context *nfs = data->nfs;
1085 WRITE3res *res;
1086
f3a75078
RS
1087 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1088
84004dbf
RS
1089 if (status == RPC_STATUS_ERROR) {
1090 data->cb(-EFAULT, nfs, command_data, data->private_data);
1091 free_nfs_cb_data(data);
1092 return;
1093 }
1094 if (status == RPC_STATUS_CANCEL) {
1095 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1096 free_nfs_cb_data(data);
1097 return;
1098 }
1099
1100 res = command_data;
1101 if (res->status != NFS3_OK) {
1102 rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1103 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1104 free_nfs_cb_data(data);
1105 return;
1106 }
1107
1108 data->nfsfh->offset += res->WRITE3res_u.resok.count;
1109 data->cb(res->WRITE3res_u.resok.count, nfs, NULL, data->private_data);
1110 free_nfs_cb_data(data);
1111}
1112
f3a75078 1113static void nfs_pwrite_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
18c27b73
RS
1114{
1115 struct nfs_mcb_data *mdata = private_data;
1116 struct nfs_cb_data *data = mdata->data;
1117 struct nfs_context *nfs = data->nfs;
1118 WRITE3res *res;
1119
f3a75078
RS
1120 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1121
18c27b73
RS
1122 data->num_calls--;
1123
1124 if (status == RPC_STATUS_ERROR) {
1125 /* flag the failure but do not invoke callback until we have received all responses */
1126 data->error = 1;
1127 }
1128 if (status == RPC_STATUS_CANCEL) {
1129 /* flag the cancellation but do not invoke callback until we have received all responses */
1130 data->cancel = 1;
1131 }
1132
1133 if (status == RPC_STATUS_SUCCESS) {
1134 res = command_data;
1135 if (res->status != NFS3_OK) {
1136 rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1137 data->error = 1;
1138 } else {
1139 if (res->WRITE3res_u.resok.count > 0) {
1140 if ((unsigned)data->max_offset < mdata->offset + res->WRITE3res_u.resok.count) {
1141 data->max_offset = mdata->offset + res->WRITE3res_u.resok.count;
1142 }
1143 }
1144 }
1145 }
1146
1147 if (data->num_calls > 0) {
1148 /* still waiting for more replies */
1149 free(mdata);
1150 return;
1151 }
1152
1153 if (data->error != 0) {
1154 data->cb(-EFAULT, nfs, command_data, data->private_data);
1155 free_nfs_cb_data(data);
1156 free(mdata);
1157 return;
1158 }
1159 if (data->cancel != 0) {
1160 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1161 free_nfs_cb_data(data);
1162 free(mdata);
1163 return;
1164 }
1165
1166 data->nfsfh->offset = data->max_offset;
1167 data->cb(data->max_offset - data->start_offset, nfs, NULL, data->private_data);
1168
1169 free_nfs_cb_data(data);
1170 free(mdata);
1171}
1172
1173
183451cf 1174int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, char *buf, nfs_cb cb, void *private_data)
84004dbf
RS
1175{
1176 struct nfs_cb_data *data;
1177
1178 data = malloc(sizeof(struct nfs_cb_data));
1179 if (data == NULL) {
1180 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1181 return -1;
1182 }
ea98629a 1183 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1184 data->nfs = nfs;
1185 data->cb = cb;
1186 data->private_data = private_data;
1187 data->nfsfh = nfsfh;
1188
1189 nfsfh->offset = offset;
18c27b73
RS
1190
1191 if (count <= nfs_get_writemax(nfs)) {
1192 if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_cb, &nfsfh->fh, buf, offset, count, nfsfh->is_sync?FILE_SYNC:UNSTABLE, data) != 0) {
1193 rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path);
1194 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1195 free_nfs_cb_data(data);
1196 return -1;
1197 }
1198 return 0;
84004dbf 1199 }
18c27b73
RS
1200
1201 /* trying to write more than maximum server write size, we has to chop it up into smaller
1202 * chunks.
1203 * we send all writes in parallell so that performance is still good.
1204 */
1205 data->max_offset = offset;
1206 data->start_offset = offset;
1207
1208 while (count > 0) {
183451cf 1209 uint64_t writecount = count;
18c27b73
RS
1210 struct nfs_mcb_data *mdata;
1211
1212 if (writecount > nfs_get_writemax(nfs)) {
1213 writecount = nfs_get_writemax(nfs);
1214 }
1215
1216 mdata = malloc(sizeof(struct nfs_mcb_data));
1217 if (mdata == NULL) {
1218 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure");
1219 return -1;
1220 }
1221 memset(mdata, 0, sizeof(struct nfs_mcb_data));
1222 mdata->data = data;
1223 mdata->offset = offset;
1224 mdata->count = writecount;
1225
1226 if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_mcb, &nfsfh->fh, &buf[offset - data->start_offset], offset, writecount, nfsfh->is_sync?FILE_SYNC:UNSTABLE, mdata) != 0) {
1227 rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path);
1228 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1229 free(mdata);
1230 return -1;
1231 }
1232
1233 count -= writecount;
1234 offset += writecount;
1235 data->num_calls++;
1236 }
1237
84004dbf
RS
1238 return 0;
1239}
1240
1241/*
1242 * Async write()
1243 */
183451cf 1244int nfs_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, char *buf, nfs_cb cb, void *private_data)
84004dbf
RS
1245{
1246 return nfs_pwrite_async(nfs, nfsfh, nfsfh->offset, count, buf, cb, private_data);
1247}
1248
1249
1250
1251
1252/*
1253 * close
1254 */
1255
1256int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1257{
1258 if (nfsfh->fh.data.data_val != NULL){
1259 free(nfsfh->fh.data.data_val);
1260 nfsfh->fh.data.data_val = NULL;
1261 }
1262 free(nfsfh);
1263
1264 cb(0, nfs, NULL, private_data);
1265 return 0;
1266};
1267
1268
1269
1270
1271
1272/*
1273 * Async fstat()
1274 */
1275int nfs_fstat_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1276{
1277 struct nfs_cb_data *data;
1278
1279 data = malloc(sizeof(struct nfs_cb_data));
1280 if (data == NULL) {
1281 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1282 return -1;
1283 }
ea98629a 1284 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1285 data->nfs = nfs;
1286 data->cb = cb;
1287 data->private_data = private_data;
1288
1289 if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &nfsfh->fh, data) != 0) {
1290 rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path);
1291 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1292 free_nfs_cb_data(data);
1293 return -1;
1294 }
1295 return 0;
1296}
1297
1298
1299
1300/*
1301 * Async fsync()
1302 */
f3a75078 1303static void nfs_fsync_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1304{
1305 struct nfs_cb_data *data = private_data;
1306 struct nfs_context *nfs = data->nfs;
1307 COMMIT3res *res;
1308
f3a75078
RS
1309 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1310
84004dbf
RS
1311 if (status == RPC_STATUS_ERROR) {
1312 data->cb(-EFAULT, nfs, command_data, data->private_data);
1313 free_nfs_cb_data(data);
1314 return;
1315 }
1316 if (status == RPC_STATUS_CANCEL) {
1317 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1318 free_nfs_cb_data(data);
1319 return;
1320 }
1321
1322 res = command_data;
1323 if (res->status != NFS3_OK) {
1324 rpc_set_error(nfs->rpc, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1325 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1326 free_nfs_cb_data(data);
1327 return;
1328 }
1329
1330 data->cb(0, nfs, NULL, data->private_data);
1331 free_nfs_cb_data(data);
1332}
1333
1334int nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1335{
1336 struct nfs_cb_data *data;
1337
1338 data = malloc(sizeof(struct nfs_cb_data));
1339 if (data == NULL) {
1340 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1341 return -1;
1342 }
ea98629a 1343 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1344 data->nfs = nfs;
1345 data->cb = cb;
1346 data->private_data = private_data;
1347
1348 if (rpc_nfs_commit_async(nfs->rpc, nfs_fsync_cb, &nfsfh->fh, data) != 0) {
1349 rpc_set_error(nfs->rpc, "RPC error: Failed to send COMMIT call for %s", data->path);
1350 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1351 free_nfs_cb_data(data);
1352 return -1;
1353 }
1354 return 0;
1355}
1356
1357
1358
1359
1360/*
1361 * Async ftruncate()
1362 */
f3a75078 1363static void nfs_ftruncate_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1364{
1365 struct nfs_cb_data *data = private_data;
1366 struct nfs_context *nfs = data->nfs;
1367 SETATTR3res *res;
1368
f3a75078
RS
1369 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1370
84004dbf
RS
1371 if (status == RPC_STATUS_ERROR) {
1372 data->cb(-EFAULT, nfs, command_data, data->private_data);
1373 free_nfs_cb_data(data);
1374 return;
1375 }
1376 if (status == RPC_STATUS_CANCEL) {
1377 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1378 free_nfs_cb_data(data);
1379 return;
1380 }
1381
1382 res = command_data;
1383 if (res->status != NFS3_OK) {
1384 rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1385 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1386 free_nfs_cb_data(data);
1387 return;
1388 }
1389
1390 data->cb(0, nfs, NULL, data->private_data);
1391 free_nfs_cb_data(data);
1392}
1393
183451cf 1394int nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t length, nfs_cb cb, void *private_data)
84004dbf
RS
1395{
1396 struct nfs_cb_data *data;
1397 SETATTR3args args;
1398
1399 data = malloc(sizeof(struct nfs_cb_data));
1400 if (data == NULL) {
1401 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1402 return -1;
1403 }
ea98629a 1404 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1405 data->nfs = nfs;
1406 data->cb = cb;
1407 data->private_data = private_data;
1408
ea98629a 1409 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
1410 args.object.data.data_len = nfsfh->fh.data.data_len;
1411 args.object.data.data_val = nfsfh->fh.data.data_val;
1412 args.new_attributes.size.set_it = 1;
1413 args.new_attributes.size.set_size3_u.size = length;
1414
1415 if (rpc_nfs_setattr_async(nfs->rpc, nfs_ftruncate_cb, &args, data) != 0) {
1416 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
1417 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1418 free_nfs_cb_data(data);
1419 return -1;
1420 }
1421 return 0;
1422}
1423
1424
1425/*
1426 * Async truncate()
1427 */
1428static int nfs_truncate_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1429{
183451cf 1430 uint64_t offset = data->continue_int;
84004dbf
RS
1431 struct nfsfh nfsfh;
1432
1433 nfsfh.fh.data.data_val = data->fh.data.data_val;
1434 nfsfh.fh.data.data_len = data->fh.data.data_len;
1435
1436 if (nfs_ftruncate_async(nfs, &nfsfh, offset, data->cb, data->private_data) != 0) {
1437 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
1438 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1439 free_nfs_cb_data(data);
1440 return -1;
1441 }
1442 free_nfs_cb_data(data);
1443 return 0;
1444}
1445
183451cf 1446int nfs_truncate_async(struct nfs_context *nfs, const char *path, uint64_t length, nfs_cb cb, void *private_data)
84004dbf 1447{
183451cf 1448 uint64_t offset;
84004dbf
RS
1449
1450 offset = length;
1451
1452 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_truncate_continue_internal, NULL, NULL, offset) != 0) {
cbbf9d3e
RS
1453 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1454 return -1;
84004dbf
RS
1455 }
1456
1457 return 0;
1458}
1459
1460
1461
1462
1463/*
1464 * Async mkdir()
1465 */
f3a75078 1466static void nfs_mkdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1467{
1468 MKDIR3res *res;
1469 struct nfs_cb_data *data = private_data;
1470 struct nfs_context *nfs = data->nfs;
1471 char *str = data->continue_data;
1472
f3a75078
RS
1473 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1474
84004dbf
RS
1475 str = &str[strlen(str) + 1];
1476
1477 if (status == RPC_STATUS_ERROR) {
1478 data->cb(-EFAULT, nfs, command_data, data->private_data);
1479 free_nfs_cb_data(data);
1480 return;
1481 }
1482 if (status == RPC_STATUS_CANCEL) {
1483 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1484 free_nfs_cb_data(data);
1485 return;
1486 }
1487
1488 res = command_data;
1489 if (res->status != NFS3_OK) {
1490 rpc_set_error(nfs->rpc, "NFS: MKDIR of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1491 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1492 free_nfs_cb_data(data);
1493 return;
1494 }
1495
1496 data->cb(0, nfs, NULL, data->private_data);
1497 free_nfs_cb_data(data);
1498}
1499
1500static int nfs_mkdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1501{
1502 char *str = data->continue_data;
7edc9026
RS
1503 MKDIR3args args;
1504
84004dbf
RS
1505 str = &str[strlen(str) + 1];
1506
7edc9026
RS
1507 memset(&args, 0, sizeof(MKDIR3args));
1508 args.where.dir.data.data_len = data->fh.data.data_len;
1509 args.where.dir.data.data_val = data->fh.data.data_val;
1510 args.where.name = str;
1511 args.attributes.mode.set_it = 1;
1512 args.attributes.mode.set_mode3_u.mode = 0755;
1513
1514 if (rpc_nfs_mkdir_async(nfs->rpc, nfs_mkdir_cb, &args, data) != 0) {
84004dbf
RS
1515 rpc_set_error(nfs->rpc, "RPC error: Failed to send MKDIR call for %s", data->path);
1516 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1517 free_nfs_cb_data(data);
1518 return -1;
1519 }
1520 return 0;
1521}
1522
1523int nfs_mkdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1524{
1525 char *new_path;
1526 char *ptr;
1527
1528 new_path = strdup(path);
1529 if (new_path == NULL) {
cbbf9d3e 1530 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1531 return -1;
1532 }
1533
93e9306f 1534 ptr = strrchr(new_path, '/');
84004dbf 1535 if (ptr == NULL) {
cbbf9d3e
RS
1536 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1537 return -1;
84004dbf
RS
1538 }
1539 *ptr = 0;
1540
1541 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1542 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_mkdir_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1543 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path component");
1544 return -1;
84004dbf
RS
1545 }
1546
1547 return 0;
1548}
1549
1550
1551
1552
1553
1554/*
1555 * Async rmdir()
1556 */
f3a75078 1557static void nfs_rmdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1558{
1559 RMDIR3res *res;
1560 struct nfs_cb_data *data = private_data;
1561 struct nfs_context *nfs = data->nfs;
1562 char *str = data->continue_data;
1563
f3a75078
RS
1564 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1565
84004dbf
RS
1566 str = &str[strlen(str) + 1];
1567
1568 if (status == RPC_STATUS_ERROR) {
1569 data->cb(-EFAULT, nfs, command_data, data->private_data);
1570 free_nfs_cb_data(data);
1571 return;
1572 }
1573 if (status == RPC_STATUS_CANCEL) {
1574 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1575 free_nfs_cb_data(data);
1576 return;
1577 }
1578
1579 res = command_data;
1580 if (res->status != NFS3_OK) {
1581 rpc_set_error(nfs->rpc, "NFS: RMDIR of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1582 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1583 free_nfs_cb_data(data);
1584 return;
1585 }
1586
1587 data->cb(0, nfs, NULL, data->private_data);
1588 free_nfs_cb_data(data);
1589}
1590
1591static int nfs_rmdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1592{
1593 char *str = data->continue_data;
1594
1595 str = &str[strlen(str) + 1];
1596
1597 if (rpc_nfs_rmdir_async(nfs->rpc, nfs_rmdir_cb, &data->fh, str, data) != 0) {
1598 rpc_set_error(nfs->rpc, "RPC error: Failed to send RMDIR call for %s", data->path);
1599 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1600 free_nfs_cb_data(data);
1601 return -1;
1602 }
1603 return 0;
1604}
1605
1606int nfs_rmdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1607{
1608 char *new_path;
1609 char *ptr;
1610
1611 new_path = strdup(path);
1612 if (new_path == NULL) {
cbbf9d3e 1613 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1614 return -1;
1615 }
1616
93e9306f 1617 ptr = strrchr(new_path, '/');
84004dbf 1618 if (ptr == NULL) {
cbbf9d3e
RS
1619 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1620 return -1;
84004dbf
RS
1621 }
1622 *ptr = 0;
1623
1624 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1625 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_rmdir_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1626 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1627 return -1;
84004dbf
RS
1628 }
1629
1630 return 0;
1631}
1632
1633
1634
1635
1636/*
1637 * Async creat()
1638 */
f3a75078 1639static void nfs_create_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1640{
1641 LOOKUP3res *res;
1642 struct nfs_cb_data *data = private_data;
1643 struct nfs_context *nfs = data->nfs;
1644 struct nfsfh *nfsfh;
1645 char *str = data->continue_data;
1646
f3a75078
RS
1647 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1648
84004dbf
RS
1649 if (status == RPC_STATUS_ERROR) {
1650 data->cb(-EFAULT, nfs, command_data, data->private_data);
1651 free_nfs_cb_data(data);
1652 return;
1653 }
1654 if (status == RPC_STATUS_CANCEL) {
1655 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1656 free_nfs_cb_data(data);
1657 return;
1658 }
1659
1660 str = &str[strlen(str) + 1];
1661 res = command_data;
1662 if (res->status != NFS3_OK) {
1663 rpc_set_error(nfs->rpc, "NFS: CREATE of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1664 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1665
1666 return;
1667 }
1668
1669 nfsfh = malloc(sizeof(struct nfsfh));
1670 if (nfsfh == NULL) {
1671 rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure");
1672 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1673 free_nfs_cb_data(data);
1674 return;
1675 }
ea98629a 1676 memset(nfsfh, 0, sizeof(struct nfsfh));
84004dbf
RS
1677
1678 /* steal the filehandle */
1679 nfsfh->fh.data.data_len = data->fh.data.data_len;
1680 nfsfh->fh.data.data_val = data->fh.data.data_val;
1681 data->fh.data.data_val = NULL;
1682
1683 data->cb(0, nfs, nfsfh, data->private_data);
1684 free_nfs_cb_data(data);
1685}
1686
1687
1688
f3a75078 1689static void nfs_creat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1690{
1691 CREATE3res *res;
1692 struct nfs_cb_data *data = private_data;
1693 struct nfs_context *nfs = data->nfs;
1694 char *str = data->continue_data;
1695
f3a75078
RS
1696 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1697
84004dbf
RS
1698 if (status == RPC_STATUS_ERROR) {
1699 data->cb(-EFAULT, nfs, command_data, data->private_data);
1700 free_nfs_cb_data(data);
1701 return;
1702 }
1703 if (status == RPC_STATUS_CANCEL) {
1704 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1705 free_nfs_cb_data(data);
1706 return;
1707 }
1708
1709 str = &str[strlen(str) + 1];
1710 res = command_data;
1711 if (res->status != NFS3_OK) {
1712 rpc_set_error(nfs->rpc, "NFS: CREATE of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1713 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1714
1715 return;
1716 }
1717
1718 if (rpc_nfs_lookup_async(nfs->rpc, nfs_create_2_cb, &data->fh, str, data) != 0) {
1719 rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s/%s", data->saved_path, str);
1720 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1721 free_nfs_cb_data(data);
1722 return;
1723 }
1724 return;
1725}
1726
1727static int nfs_creat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1728{
1729 char *str = data->continue_data;
c985c015
RS
1730 CREATE3args args;
1731
84004dbf
RS
1732 str = &str[strlen(str) + 1];
1733
c985c015
RS
1734 memset(&args, 0, sizeof(CREATE3args));
1735 args.where.dir.data.data_len = data->fh.data.data_len;
1736 args.where.dir.data.data_val = data->fh.data.data_val;
1737 args.where.name = str;
1738 args.how.mode = UNCHECKED;
1739 args.how.createhow3_u.obj_attributes.mode.set_it = 1;
1740 args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = data->continue_int;
1741
1742 if (rpc_nfs_create_async(nfs->rpc, nfs_creat_1_cb, &args, data) != 0) {
84004dbf
RS
1743 rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str);
1744 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1745 free_nfs_cb_data(data);
1746 return -1;
1747 }
1748 return 0;
1749}
1750
1751int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
1752{
1753 char *new_path;
1754 char *ptr;
1755
1756 new_path = strdup(path);
1757 if (new_path == NULL) {
cbbf9d3e 1758 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1759 return -1;
1760 }
1761
93e9306f 1762 ptr = strrchr(new_path, '/');
84004dbf 1763 if (ptr == NULL) {
cbbf9d3e
RS
1764 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1765 return -1;
84004dbf
RS
1766 }
1767 *ptr = 0;
1768
73f4ae7c 1769 /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
84004dbf 1770 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_creat_continue_internal, new_path, free, mode) != 0) {
cbbf9d3e
RS
1771 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1772 return -1;
84004dbf
RS
1773 }
1774
1775 return 0;
1776}
1777
1778
1779
1780
1781/*
1782 * Async unlink()
1783 */
f3a75078 1784static void nfs_unlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1785{
1786 REMOVE3res *res;
1787 struct nfs_cb_data *data = private_data;
1788 struct nfs_context *nfs = data->nfs;
1789 char *str = data->continue_data;
1790
f3a75078
RS
1791 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1792
84004dbf
RS
1793 str = &str[strlen(str) + 1];
1794
1795 if (status == RPC_STATUS_ERROR) {
1796 data->cb(-EFAULT, nfs, command_data, data->private_data);
1797 free_nfs_cb_data(data);
1798 return;
1799 }
1800 if (status == RPC_STATUS_CANCEL) {
1801 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1802 free_nfs_cb_data(data);
1803 return;
1804 }
1805
1806 res = command_data;
1807 if (res->status != NFS3_OK) {
1808 rpc_set_error(nfs->rpc, "NFS: REMOVE of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1809 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1810 free_nfs_cb_data(data);
1811 return;
1812 }
1813
1814 data->cb(0, nfs, NULL, data->private_data);
1815 free_nfs_cb_data(data);
1816}
1817
1818static int nfs_unlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1819{
1820 char *str = data->continue_data;
1821
1822 str = &str[strlen(str) + 1];
1823
1824 if (rpc_nfs_remove_async(nfs->rpc, nfs_unlink_cb, &data->fh, str, data) != 0) {
1825 rpc_set_error(nfs->rpc, "RPC error: Failed to send REMOVE call for %s", data->path);
1826 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1827 free_nfs_cb_data(data);
1828 return -1;
1829 }
1830 return 0;
1831}
1832
1833int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1834{
1835 char *new_path;
1836 char *ptr;
1837
1838 new_path = strdup(path);
1839 if (new_path == NULL) {
cbbf9d3e 1840 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1841 return -1;
1842 }
1843
93e9306f 1844 ptr = strrchr(new_path, '/');
84004dbf 1845 if (ptr == NULL) {
cbbf9d3e
RS
1846 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1847 return -1;
84004dbf
RS
1848 }
1849 *ptr = 0;
1850
1851 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1852 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_unlink_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1853 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1854 return -1;
84004dbf
RS
1855 }
1856
1857 return 0;
1858}
1859
1860
1ec6b50a
RS
1861/*
1862 * Async mknod()
1863 */
1864struct mknod_cb_data {
1865 char *path;
1866 int mode;
1867 int major;
1868 int minor;
1869};
1870
1871static void free_mknod_cb_data(void *ptr)
1872{
1873 struct mknod_cb_data *data = ptr;
1874
1875 free(data->path);
1876 free(data);
1877}
1878
f3a75078 1879static void nfs_mknod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
1ec6b50a
RS
1880{
1881 MKNOD3res *res;
1882 struct nfs_cb_data *data = private_data;
1883 struct nfs_context *nfs = data->nfs;
1884 char *str = data->continue_data;
1885
f3a75078
RS
1886 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1887
1ec6b50a
RS
1888 str = &str[strlen(str) + 1];
1889
1890 if (status == RPC_STATUS_ERROR) {
1891 data->cb(-EFAULT, nfs, command_data, data->private_data);
1892 free_nfs_cb_data(data);
1893 return;
1894 }
1895 if (status == RPC_STATUS_CANCEL) {
1896 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1897 free_nfs_cb_data(data);
1898 return;
1899 }
1900
1901 res = command_data;
1902 if (res->status != NFS3_OK) {
1903 rpc_set_error(nfs->rpc, "NFS: MKNOD of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1904 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1905 free_nfs_cb_data(data);
1906 return;
1907 }
1908
1909 data->cb(0, nfs, NULL, data->private_data);
1910 free_nfs_cb_data(data);
1911}
1912
1913static int nfs_mknod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1914{
1915 struct mknod_cb_data *cb_data = data->continue_data;
1916 char *str = cb_data->path;
1917
1918 str = &str[strlen(str) + 1];
1919
1920 if (rpc_nfs_mknod_async(nfs->rpc, nfs_mknod_cb, &data->fh, str, cb_data->mode, cb_data->major, cb_data->minor, data) != 0) {
1921 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1922 free_nfs_cb_data(data);
1923 return -1;
1924 }
1925 return 0;
1926}
1927
1928int nfs_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev, nfs_cb cb, void *private_data)
1929{
1930 char *ptr;
1931 struct mknod_cb_data *cb_data;
1932
1933 cb_data = malloc(sizeof(struct mknod_cb_data));
1934 if (cb_data == NULL) {
1935 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for cb data");
1936 return -1;
1937 }
1938
1939 cb_data->path = strdup(path);
1940 if (cb_data->path == NULL) {
1941 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
1942 free(cb_data);
1943 return -1;
1944 }
1945
1946 ptr = strrchr(cb_data->path, '/');
1947 if (ptr == NULL) {
1948 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1949 return -1;
1950 }
1951 *ptr = 0;
1952
1953 cb_data->mode = mode;
1954 cb_data->major = major(dev);
1955 cb_data->minor = minor(dev);
1956
1957 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1958 if (nfs_lookuppath_async(nfs, cb_data->path, cb, private_data, nfs_mknod_continue_internal, cb_data, free_mknod_cb_data, 0) != 0) {
1959 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1960 free_mknod_cb_data(cb_data);
1961 return -1;
1962 }
1963
1964 return 0;
1965}
84004dbf 1966
84004dbf
RS
1967/*
1968 * Async opendir()
1969 */
fb69c64c
RS
1970
1971/* ReadDirPlus Emulation Callback data */
1972struct rdpe_cb_data {
1973 int getattrcount;
1974 int status;
1975 struct nfs_cb_data *data;
1976};
1977
1978/* ReadDirPlus Emulation LOOKUP Callback data */
1979struct rdpe_lookup_cb_data {
1980 struct rdpe_cb_data *rdpe_cb_data;
1981 struct nfsdirent *nfsdirent;
1982};
1983
1984/* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
f3a75078 1985static void nfs_opendir3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf 1986{
fb69c64c
RS
1987 LOOKUP3res *res = command_data;
1988 struct rdpe_lookup_cb_data *rdpe_lookup_cb_data = private_data;
1989 struct rdpe_cb_data *rdpe_cb_data = rdpe_lookup_cb_data->rdpe_cb_data;
1990 struct nfs_cb_data *data = rdpe_cb_data->data;
1991 struct nfsdir *nfsdir = data->continue_data;
1992 struct nfs_context *nfs = data->nfs;
1993 struct nfsdirent *nfsdirent = rdpe_lookup_cb_data->nfsdirent;
1994
f3a75078
RS
1995 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1996
fb69c64c
RS
1997 free(rdpe_lookup_cb_data);
1998
1999 rdpe_cb_data->getattrcount--;
2000
2001 if (status == RPC_STATUS_ERROR) {
2002 rdpe_cb_data->status = RPC_STATUS_ERROR;
2003 }
2004 if (status == RPC_STATUS_CANCEL) {
2005 rdpe_cb_data->status = RPC_STATUS_CANCEL;
2006 }
2007 if (status == RPC_STATUS_SUCCESS && res->status != NFS3_OK) {
2008 rdpe_cb_data->status = RPC_STATUS_ERROR;
2009 }
2010 if (status == RPC_STATUS_SUCCESS && res->status == NFS3_OK) {
2011 if (res->LOOKUP3res_u.resok.obj_attributes.attributes_follow) {
2012 fattr3 *attributes = &res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes;
2013
2014 nfsdirent->type = attributes->type;
2015 nfsdirent->mode = attributes->mode;
2016 nfsdirent->size = attributes->size;
2017
2018 nfsdirent->atime.tv_sec = attributes->atime.seconds;
2019 nfsdirent->atime.tv_usec = attributes->atime.nseconds/1000;
2020 nfsdirent->mtime.tv_sec = attributes->mtime.seconds;
2021 nfsdirent->mtime.tv_usec = attributes->mtime.nseconds/1000;
2022 nfsdirent->ctime.tv_sec = attributes->ctime.seconds;
2023 nfsdirent->ctime.tv_usec = attributes->ctime.nseconds/1000;
2024 }
2025 }
2026
2027 if (rdpe_cb_data->getattrcount == 0) {
2028 if (rdpe_cb_data->status != RPC_STATUS_SUCCESS) {
2029 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2030 nfs_free_nfsdir(nfsdir);
2031 } else {
2032 data->cb(0, nfs, nfsdir, data->private_data);
2033 }
2034 free(rdpe_cb_data);
2035
2036 data->continue_data = NULL;
2037 free_nfs_cb_data(data);
2038 }
2039}
2040
f3a75078 2041static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
fb69c64c
RS
2042{
2043 READDIR3res *res = command_data;
84004dbf
RS
2044 struct nfs_cb_data *data = private_data;
2045 struct nfs_context *nfs = data->nfs;
fb6510bb 2046 struct nfsdir *nfsdir = data->continue_data;
fb69c64c
RS
2047 struct nfsdirent *nfsdirent;
2048 struct entry3 *entry;
84004dbf 2049 uint64_t cookie;
fb69c64c
RS
2050 struct rdpe_cb_data *rdpe_cb_data;
2051
f3a75078
RS
2052 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2053
84004dbf
RS
2054 if (status == RPC_STATUS_ERROR) {
2055 data->cb(-EFAULT, nfs, command_data, data->private_data);
2056 nfs_free_nfsdir(nfsdir);
2057 data->continue_data = NULL;
2058 free_nfs_cb_data(data);
2059 return;
2060 }
fb69c64c 2061
84004dbf
RS
2062 if (status == RPC_STATUS_CANCEL) {
2063 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2064 nfs_free_nfsdir(nfsdir);
2065 data->continue_data = NULL;
2066 free_nfs_cb_data(data);
2067 return;
2068 }
2069
84004dbf
RS
2070 if (res->status != NFS3_OK) {
2071 rpc_set_error(nfs->rpc, "NFS: READDIR of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2072 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2073 nfs_free_nfsdir(nfsdir);
2074 data->continue_data = NULL;
2075 free_nfs_cb_data(data);
2076 return;
fb69c64c
RS
2077 }
2078
2079 entry =res->READDIR3res_u.resok.reply.entries;
2080 while (entry != NULL) {
2081 nfsdirent = malloc(sizeof(struct nfsdirent));
2082 if (nfsdirent == NULL) {
2083 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
2084 nfs_free_nfsdir(nfsdir);
2085 data->continue_data = NULL;
2086 free_nfs_cb_data(data);
2087 return;
2088 }
2089 memset(nfsdirent, 0, sizeof(struct nfsdirent));
2090 nfsdirent->name = strdup(entry->name);
2091 if (nfsdirent->name == NULL) {
2092 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
2093 nfs_free_nfsdir(nfsdir);
2094 data->continue_data = NULL;
2095 free_nfs_cb_data(data);
2096 return;
2097 }
2098 nfsdirent->inode = entry->fileid;
2099
2100 nfsdirent->next = nfsdir->entries;
2101 nfsdir->entries = nfsdirent;
2102
2103 cookie = entry->cookie;
2104 entry = entry->nextentry;
2105 }
2106
2107 if (res->READDIR3res_u.resok.reply.eof == 0) {
2108 if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, cookie, res->READDIR3res_u.resok.cookieverf, 8192, data) != 0) {
2109 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
2110 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2111 nfs_free_nfsdir(nfsdir);
2112 data->continue_data = NULL;
2113 free_nfs_cb_data(data);
2114 return;
2115 }
2116 return;
2117 }
2118
2119 /* steal the dirhandle */
2120 nfsdir->current = nfsdir->entries;
2121
2122 rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data));
2123 rdpe_cb_data->getattrcount = 0;
2124 rdpe_cb_data->status = RPC_STATUS_SUCCESS;
2125 rdpe_cb_data->data = data;
2126 for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) {
2127 struct rdpe_lookup_cb_data *rdpe_lookup_cb_data;
2128
2129 rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data));
2130 rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data;
2131 rdpe_lookup_cb_data->nfsdirent = nfsdirent;
2132
2133 if (rpc_nfs_lookup_async(nfs->rpc, nfs_opendir3_cb, &data->fh, nfsdirent->name, rdpe_lookup_cb_data) != 0) {
2134 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call");
2135
2136 /* if we have already commands in flight, we cant just stop, we have to wait for the
2137 * commands in flight to complete
2138 */
2139 if (rdpe_cb_data->getattrcount > 0) {
2140 rdpe_cb_data->status = RPC_STATUS_ERROR;
2141 free(rdpe_lookup_cb_data);
2142 return;
2143 }
2144
2145 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2146 nfs_free_nfsdir(nfsdir);
2147 data->continue_data = NULL;
2148 free_nfs_cb_data(data);
2149 free(rdpe_lookup_cb_data);
2150 free(rdpe_cb_data);
2151 return;
2152 }
2153 rdpe_cb_data->getattrcount++;
2154 }
2155}
2156
2157
f3a75078 2158static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
fb69c64c
RS
2159{
2160 READDIRPLUS3res *res = command_data;
2161 struct nfs_cb_data *data = private_data;
2162 struct nfs_context *nfs = data->nfs;
2163 struct nfsdir *nfsdir = data->continue_data;
2164 struct entryplus3 *entry;
2165 uint64_t cookie;
2166
f3a75078 2167 assert(rpc->magic == RPC_CONTEXT_MAGIC);
fb69c64c
RS
2168
2169 if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){
2170 cookieverf3 cv;
2171
2172 if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) {
2173 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
2174 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2175 nfs_free_nfsdir(nfsdir);
2176 data->continue_data = NULL;
2177 free_nfs_cb_data(data);
2178 return;
2179 }
2180 return;
2181 }
2182
2183 if (status == RPC_STATUS_CANCEL) {
2184 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2185 nfs_free_nfsdir(nfsdir);
2186 data->continue_data = NULL;
2187 free_nfs_cb_data(data);
2188 return;
2189 }
2190
2191 if (res->status != NFS3_OK) {
2192 rpc_set_error(nfs->rpc, "NFS: READDIRPLUS of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2193 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2194 nfs_free_nfsdir(nfsdir);
2195 data->continue_data = NULL;
2196 free_nfs_cb_data(data);
2197 return;
84004dbf
RS
2198 }
2199
f390f181 2200 entry =res->READDIRPLUS3res_u.resok.reply.entries;
84004dbf
RS
2201 while (entry != NULL) {
2202 struct nfsdirent *nfsdirent;
2203
2204 nfsdirent = malloc(sizeof(struct nfsdirent));
2205 if (nfsdirent == NULL) {
2206 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
2207 nfs_free_nfsdir(nfsdir);
2208 data->continue_data = NULL;
2209 free_nfs_cb_data(data);
2210 return;
2211 }
ea98629a 2212 memset(nfsdirent, 0, sizeof(struct nfsdirent));
84004dbf
RS
2213 nfsdirent->name = strdup(entry->name);
2214 if (nfsdirent->name == NULL) {
2215 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
2216 nfs_free_nfsdir(nfsdir);
2217 data->continue_data = NULL;
2218 free_nfs_cb_data(data);
2219 return;
2220 }
2221 nfsdirent->inode = entry->fileid;
0804e67d
RS
2222 if (entry->name_attributes.attributes_follow) {
2223 nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type;
2224 nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode;
2225 nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size;
2226
2227 nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds;
2228 nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000;
2229 nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds;
2230 nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000;
2231 nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds;
2232 nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000;
2233 }
2234
84004dbf
RS
2235 nfsdirent->next = nfsdir->entries;
2236 nfsdir->entries = nfsdirent;
2237
2238 cookie = entry->cookie;
2239 entry = entry->nextentry;
2240 }
2241
f390f181
RS
2242 if (res->READDIRPLUS3res_u.resok.reply.eof == 0) {
2243 if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, cookie, res->READDIRPLUS3res_u.resok.cookieverf, 8192, data) != 0) {
2244 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
2245 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2246 nfs_free_nfsdir(nfsdir);
2247 data->continue_data = NULL;
2248 free_nfs_cb_data(data);
2249 return;
2250 }
2251 return;
2252 }
2253
2254 /* steal the dirhandle */
2255 data->continue_data = NULL;
2256 nfsdir->current = nfsdir->entries;
2257
2258 data->cb(0, nfs, nfsdir, data->private_data);
2259 free_nfs_cb_data(data);
2260}
2261
2262static int nfs_opendir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2263{
2264 cookieverf3 cv;
2265
ea98629a 2266 memset(cv, 0, sizeof(cookieverf3));
f390f181
RS
2267 if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) {
2268 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
2269 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2270 free_nfs_cb_data(data);
2271 return -1;
2272 }
2273 return 0;
2274}
2275
2276int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2277{
2278 struct nfsdir *nfsdir;
2279
2280 nfsdir = malloc(sizeof(struct nfsdir));
2281 if (nfsdir == NULL) {
cbbf9d3e 2282 rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir");
84004dbf
RS
2283 return -1;
2284 }
ea98629a 2285 memset(nfsdir, 0, sizeof(struct nfsdir));
84004dbf
RS
2286
2287 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) {
cbbf9d3e
RS
2288 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2289 return -1;
84004dbf
RS
2290 }
2291
2292 return 0;
2293}
2294
2295
2296struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
2297{
2298 struct nfsdirent *nfsdirent = nfsdir->current;
2299
2300 if (nfsdir->current != NULL) {
2301 nfsdir->current = nfsdir->current->next;
2302 }
2303 return nfsdirent;
2304}
2305
2306
2307void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
2308{
2309 nfs_free_nfsdir(nfsdir);
2310}
2311
2312
2313
2314
2315
2316
2317
2318/*
2319 * Async lseek()
2320 */
2321struct lseek_cb_data {
2322 struct nfs_context *nfs;
2323 struct nfsfh *nfsfh;
183451cf 2324 uint64_t offset;
84004dbf
RS
2325 nfs_cb cb;
2326 void *private_data;
2327};
2328
f3a75078 2329static void nfs_lseek_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2330{
2331 GETATTR3res *res;
2332 struct lseek_cb_data *data = private_data;
2333 struct nfs_context *nfs = data->nfs;
2334
f3a75078
RS
2335 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2336
84004dbf
RS
2337 if (status == RPC_STATUS_ERROR) {
2338 data->cb(-EFAULT, nfs, command_data, data->private_data);
2339 free(data);
2340 return;
2341 }
2342 if (status == RPC_STATUS_CANCEL) {
2343 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2344 free(data);
2345 return;
2346 }
2347
2348 res = command_data;
2349 if (res->status != NFS3_OK) {
2350 rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2351 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2352 free(data);
2353 return;
2354 }
2355
2356 data->nfsfh->offset = data->offset + res->GETATTR3res_u.resok.obj_attributes.size;
2357 data->cb(0, nfs, &data->nfsfh->offset, data->private_data);
2358 free(data);
2359}
2360
183451cf 2361int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, int whence, nfs_cb cb, void *private_data)
84004dbf
RS
2362{
2363 struct lseek_cb_data *data;
2364
2365 if (whence == SEEK_SET) {
2366 nfsfh->offset = offset;
2367 cb(0, nfs, &nfsfh->offset, private_data);
2368 return 0;
2369 }
2370 if (whence == SEEK_CUR) {
2371 nfsfh->offset += offset;
2372 cb(0, nfs, &nfsfh->offset, private_data);
2373 return 0;
2374 }
2375
2376 data = malloc(sizeof(struct lseek_cb_data));
2377 if (data == NULL) {
2378 rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data");
2379 return -1;
2380 }
2381
2382 data->nfs = nfs;
2383 data->nfsfh = nfsfh;
2384 data->offset = offset;
2385 data->cb = cb;
2386 data->private_data = private_data;
2387
2388 if (rpc_nfs_getattr_async(nfs->rpc, nfs_lseek_1_cb, &nfsfh->fh, data) != 0) {
2389 rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call");
2390 free(data);
cbbf9d3e 2391 return -1;
84004dbf
RS
2392 }
2393 return 0;
2394}
2395
2396
2397
2398
2399/*
2400 * Async statvfs()
2401 */
f3a75078 2402static void nfs_statvfs_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2403{
2404 FSSTAT3res *res;
2405 struct nfs_cb_data *data = private_data;
2406 struct nfs_context *nfs = data->nfs;
2407 struct statvfs svfs;
2408
f3a75078
RS
2409 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2410
84004dbf
RS
2411 if (status == RPC_STATUS_ERROR) {
2412 data->cb(-EFAULT, nfs, command_data, data->private_data);
2413 free_nfs_cb_data(data);
2414 return;
2415 }
2416 if (status == RPC_STATUS_CANCEL) {
2417 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2418 free_nfs_cb_data(data);
2419 return;
2420 }
2421
2422 res = command_data;
2423 if (res->status != NFS3_OK) {
2424 rpc_set_error(nfs->rpc, "NFS: FSSTAT of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2425 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2426 free_nfs_cb_data(data);
2427 return;
2428 }
2429
2430 svfs.f_bsize = 4096;
2431 svfs.f_frsize = 4096;
2432 svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/4096;
2433 svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/4096;
2434 svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/4096;
2435 svfs.f_files = res->FSSTAT3res_u.resok.tfiles;
2436 svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles;
252aa90d 2437#if !defined(ANDROID)
84004dbf
RS
2438 svfs.f_favail = res->FSSTAT3res_u.resok.afiles;
2439 svfs.f_fsid = 0;
2440 svfs.f_flag = 0;
2441 svfs.f_namemax = 256;
252aa90d 2442#endif
84004dbf
RS
2443
2444 data->cb(0, nfs, &svfs, data->private_data);
2445 free_nfs_cb_data(data);
2446}
2447
2448static int nfs_statvfs_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2449{
2450 if (rpc_nfs_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &data->fh, data) != 0) {
2451 rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT call for %s", data->path);
2452 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2453 free_nfs_cb_data(data);
2454 return -1;
2455 }
2456 return 0;
2457}
2458
2459int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2460{
2461 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2462 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2463 return -1;
2464 }
2465
2466 return 0;
2467}
2468
2469
2470
2471
2472/*
2473 * Async readlink()
2474 */
f3a75078 2475static void nfs_readlink_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2476{
2477 READLINK3res *res;
2478 struct nfs_cb_data *data = private_data;
2479 struct nfs_context *nfs = data->nfs;
2480
f3a75078
RS
2481 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2482
84004dbf
RS
2483 if (status == RPC_STATUS_ERROR) {
2484 data->cb(-EFAULT, nfs, command_data, data->private_data);
2485 free_nfs_cb_data(data);
2486 return;
2487 }
2488 if (status == RPC_STATUS_CANCEL) {
2489 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2490 free_nfs_cb_data(data);
2491 return;
2492 }
2493
2494 res = command_data;
2495 if (res->status != NFS3_OK) {
2496 rpc_set_error(nfs->rpc, "NFS: READLINK of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2497 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2498 free_nfs_cb_data(data);
2499 return;
2500 }
2501
2502
2503 data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data);
2504 free_nfs_cb_data(data);
2505}
2506
2507static int nfs_readlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2508{
16104b27
RS
2509 READLINK3args args;
2510
2511 args.symlink.data.data_len = data->fh.data.data_len;
2512 args.symlink.data.data_val = data->fh.data.data_val;
2513
2514 if (rpc_nfs_readlink_async(nfs->rpc, nfs_readlink_1_cb, &args, data) != 0) {
84004dbf
RS
2515 rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path);
2516 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2517 free_nfs_cb_data(data);
2518 return -1;
2519 }
2520 return 0;
2521}
2522
2523int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2524{
2525 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2526 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2527 return -1;
2528 }
2529
2530 return 0;
2531}
2532
2533
2534
2535
2536/*
2537 * Async chmod()
2538 */
f3a75078 2539static void nfs_chmod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2540{
2541 struct nfs_cb_data *data = private_data;
2542 struct nfs_context *nfs = data->nfs;
2543 SETATTR3res *res;
2544
f3a75078
RS
2545 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2546
84004dbf
RS
2547 if (status == RPC_STATUS_ERROR) {
2548 data->cb(-EFAULT, nfs, command_data, data->private_data);
2549 free_nfs_cb_data(data);
2550 return;
2551 }
2552 if (status == RPC_STATUS_CANCEL) {
2553 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2554 free_nfs_cb_data(data);
2555 return;
2556 }
2557
2558 res = command_data;
2559 if (res->status != NFS3_OK) {
2560 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2561 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2562 free_nfs_cb_data(data);
2563 return;
2564 }
2565
2566 data->cb(0, nfs, NULL, data->private_data);
2567 free_nfs_cb_data(data);
2568}
2569
2570static int nfs_chmod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2571{
2572 SETATTR3args args;
2573
ea98629a 2574 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2575 args.object.data.data_len = data->fh.data.data_len;
2576 args.object.data.data_val = data->fh.data.data_val;
2577 args.new_attributes.mode.set_it = 1;
2578 args.new_attributes.mode.set_mode3_u.mode = data->continue_int;
2579
2580 if (rpc_nfs_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) {
2581 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2582 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2583 free_nfs_cb_data(data);
2584 return -1;
2585 }
2586 return 0;
2587}
2588
2589
2590int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
2591{
2592 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e 2593 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2594 return -1;
2595 }
2596
2597 return 0;
2598}
2599
2600/*
2601 * Async fchmod()
2602 */
2603int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data)
2604{
2605 struct nfs_cb_data *data;
2606
2607 data = malloc(sizeof(struct nfs_cb_data));
2608 if (data == NULL) {
cbbf9d3e 2609 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data");
84004dbf
RS
2610 return -1;
2611 }
ea98629a 2612 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
2613 data->nfs = nfs;
2614 data->cb = cb;
2615 data->private_data = private_data;
2616 data->continue_int = mode;
2617 data->fh.data.data_len = nfsfh->fh.data.data_len;
2618 data->fh.data.data_val = malloc(data->fh.data.data_len);
2619 if (data->fh.data.data_val == NULL) {
2620 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
2621 free_nfs_cb_data(data);
2622 return -1;
2623 }
2624 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
2625
2626 if (nfs_chmod_continue_internal(nfs, data) != 0) {
2627 free_nfs_cb_data(data);
2628 return -1;
2629 }
2630
2631 return 0;
2632}
2633
2634
2635
2636/*
2637 * Async chown()
2638 */
f3a75078 2639static void nfs_chown_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2640{
2641 struct nfs_cb_data *data = private_data;
2642 struct nfs_context *nfs = data->nfs;
2643 SETATTR3res *res;
2644
f3a75078
RS
2645 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2646
84004dbf
RS
2647 if (status == RPC_STATUS_ERROR) {
2648 data->cb(-EFAULT, nfs, command_data, data->private_data);
2649 free_nfs_cb_data(data);
2650 return;
2651 }
2652 if (status == RPC_STATUS_CANCEL) {
2653 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2654 free_nfs_cb_data(data);
2655 return;
2656 }
2657
2658 res = command_data;
2659 if (res->status != NFS3_OK) {
2660 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2661 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2662 free_nfs_cb_data(data);
2663 return;
2664 }
2665
2666 data->cb(0, nfs, NULL, data->private_data);
2667 free_nfs_cb_data(data);
2668}
2669
2670struct nfs_chown_data {
2671 uid_t uid;
2672 gid_t gid;
2673};
2674
2675static int nfs_chown_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2676{
2677 SETATTR3args args;
2678 struct nfs_chown_data *chown_data = data->continue_data;
2679
ea98629a 2680 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2681 args.object.data.data_len = data->fh.data.data_len;
2682 args.object.data.data_val = data->fh.data.data_val;
2683 if (chown_data->uid != (uid_t)-1) {
2684 args.new_attributes.uid.set_it = 1;
2685 args.new_attributes.uid.set_uid3_u.uid = chown_data->uid;
2686 }
2687 if (chown_data->gid != (gid_t)-1) {
2688 args.new_attributes.gid.set_it = 1;
2689 args.new_attributes.gid.set_gid3_u.gid = chown_data->gid;
2690 }
2691
2692 if (rpc_nfs_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) {
2693 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2694 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2695 free_nfs_cb_data(data);
2696 return -1;
2697 }
2698 return 0;
2699}
2700
2701
2702int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data)
2703{
2704 struct nfs_chown_data *chown_data;
2705
2706 chown_data = malloc(sizeof(struct nfs_chown_data));
2707 if (chown_data == NULL) {
cbbf9d3e 2708 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
2709 return -1;
2710 }
2711
2712 chown_data->uid = uid;
2713 chown_data->gid = gid;
2714
2715 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) {
cbbf9d3e 2716 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2717 return -1;
2718 }
2719
2720 return 0;
2721}
2722
2723
2724/*
2725 * Async fchown()
2726 */
2727int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data)
2728{
2729 struct nfs_cb_data *data;
2730 struct nfs_chown_data *chown_data;
2731
2732 chown_data = malloc(sizeof(struct nfs_chown_data));
2733 if (chown_data == NULL) {
cbbf9d3e 2734 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
2735 return -1;
2736 }
2737
2738 chown_data->uid = uid;
2739 chown_data->gid = gid;
2740
2741
2742 data = malloc(sizeof(struct nfs_cb_data));
2743 if (data == NULL) {
cbbf9d3e 2744 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data");
84004dbf
RS
2745 return -1;
2746 }
ea98629a 2747 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
2748 data->nfs = nfs;
2749 data->cb = cb;
2750 data->private_data = private_data;
2751 data->continue_data = chown_data;
2752 data->fh.data.data_len = nfsfh->fh.data.data_len;
2753 data->fh.data.data_val = malloc(data->fh.data.data_len);
2754 if (data->fh.data.data_val == NULL) {
2755 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
2756 free_nfs_cb_data(data);
2757 return -1;
2758 }
2759 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
2760
2761
2762 if (nfs_chown_continue_internal(nfs, data) != 0) {
2763 free_nfs_cb_data(data);
2764 return -1;
2765 }
2766
2767 return 0;
2768}
2769
2770
2771
2772
2773
2774/*
2775 * Async utimes()
2776 */
f3a75078 2777static void nfs_utimes_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2778{
2779 struct nfs_cb_data *data = private_data;
2780 struct nfs_context *nfs = data->nfs;
2781 SETATTR3res *res;
2782
f3a75078
RS
2783 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2784
84004dbf
RS
2785 if (status == RPC_STATUS_ERROR) {
2786 data->cb(-EFAULT, nfs, command_data, data->private_data);
2787 free_nfs_cb_data(data);
2788 return;
2789 }
2790 if (status == RPC_STATUS_CANCEL) {
2791 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2792 free_nfs_cb_data(data);
2793 return;
2794 }
2795
2796 res = command_data;
2797 if (res->status != NFS3_OK) {
2798 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2799 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2800 free_nfs_cb_data(data);
2801 return;
2802 }
2803
2804 data->cb(0, nfs, NULL, data->private_data);
2805 free_nfs_cb_data(data);
2806}
2807
2808static int nfs_utimes_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2809{
2810 SETATTR3args args;
2811 struct timeval *utimes_data = data->continue_data;
2812
ea98629a 2813 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2814 args.object.data.data_len = data->fh.data.data_len;
2815 args.object.data.data_val = data->fh.data.data_val;
2816 if (utimes_data != NULL) {
2817 args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME;
2818 args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec;
2819 args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000;
2820 args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME;
2821 args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec;
2822 args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000;
2823 } else {
2824 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2825 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2826 }
2827
2828 if (rpc_nfs_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) {
2829 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2830 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2831 free_nfs_cb_data(data);
2832 return -1;
2833 }
2834 return 0;
2835}
2836
2837
2838int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data)
2839{
2840 struct timeval *new_times = NULL;
2841
2842 if (times != NULL) {
2843 new_times = malloc(sizeof(struct timeval)*2);
2844 if (new_times == NULL) {
cbbf9d3e 2845 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
2846 return -1;
2847 }
2848
2849 memcpy(new_times, times, sizeof(struct timeval)*2);
2850 }
2851
2852 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 2853 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2854 return -1;
2855 }
2856
2857 return 0;
2858}
2859
2860/*
2861 * Async utime()
2862 */
2863int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data)
2864{
2865 struct timeval *new_times = NULL;
2866
2867 if (times != NULL) {
2868 new_times = malloc(sizeof(struct timeval)*2);
2869 if (new_times == NULL) {
cbbf9d3e 2870 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
2871 return -1;
2872 }
2873
2874 new_times[0].tv_sec = times->actime;
2875 new_times[0].tv_usec = 0;
2876 new_times[1].tv_sec = times->modtime;
2877 new_times[1].tv_usec = 0;
2878 }
2879
2880 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 2881 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2882 return -1;
2883 }
2884
2885 return 0;
2886}
2887
2888
2889
2890
2891
2892/*
2893 * Async access()
2894 */
f3a75078 2895static void nfs_access_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2896{
2897 ACCESS3res *res;
2898 struct nfs_cb_data *data = private_data;
2899 struct nfs_context *nfs = data->nfs;
2900 unsigned int nfsmode = 0;
2901
f3a75078
RS
2902 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2903
84004dbf
RS
2904 if (status == RPC_STATUS_ERROR) {
2905 data->cb(-EFAULT, nfs, command_data, data->private_data);
2906 free_nfs_cb_data(data);
2907 return;
2908 }
2909 if (status == RPC_STATUS_CANCEL) {
2910 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2911 free_nfs_cb_data(data);
2912 return;
2913 }
2914
2915 res = command_data;
2916 if (res->status != NFS3_OK) {
2917 rpc_set_error(nfs->rpc, "NFS: ACCESS of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2918 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2919 free_nfs_cb_data(data);
2920 return;
2921 }
2922
2923 if (data->continue_int & R_OK) {
2924 nfsmode |= ACCESS3_READ;
2925 }
2926 if (data->continue_int & W_OK) {
2927 nfsmode |= ACCESS3_MODIFY;
2928 }
2929 if (data->continue_int & X_OK) {
2930 nfsmode |= ACCESS3_EXECUTE;
2931 }
2932
2933 if (res->ACCESS3res_u.resok.access != nfsmode) {
2934 rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2935 nfsmode&ACCESS3_READ?'r':'-',
2936 nfsmode&ACCESS3_MODIFY?'w':'-',
2937 nfsmode&ACCESS3_EXECUTE?'x':'-',
2938 res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-',
2939 res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-',
2940 res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-');
2941 data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data);
2942 free_nfs_cb_data(data);
2943 return;
2944 }
2945
2946 data->cb(0, nfs, NULL, data->private_data);
2947 free_nfs_cb_data(data);
2948}
2949
2950static int nfs_access_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2951{
2952 int nfsmode = 0;
2953
2954 if (data->continue_int & R_OK) {
2955 nfsmode |= ACCESS3_READ;
2956 }
2957 if (data->continue_int & W_OK) {
2958 nfsmode |= ACCESS3_MODIFY;
2959 }
2960 if (data->continue_int & X_OK) {
2961 nfsmode |= ACCESS3_EXECUTE;
2962 }
2963
2964 if (rpc_nfs_access_async(nfs->rpc, nfs_access_cb, &data->fh, nfsmode, data) != 0) {
2965 rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
2966 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2967 free_nfs_cb_data(data);
2968 return -1;
2969 }
2970 return 0;
2971}
2972
2973int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
2974{
2975 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e
RS
2976 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2977 return -1;
84004dbf
RS
2978 }
2979
2980 return 0;
2981}
2982
2983
2984
2985/*
2986 * Async symlink()
2987 */
2988struct nfs_symlink_data {
2989 char *oldpath;
2990 char *newpathparent;
2991 char *newpathobject;
2992};
2993
2994static void free_nfs_symlink_data(void *mem)
2995{
2996 struct nfs_symlink_data *data = mem;
2997
2998 if (data->oldpath != NULL) {
2999 free(data->oldpath);
3000 }
3001 if (data->newpathparent != NULL) {
3002 free(data->newpathparent);
3003 }
3004 if (data->newpathobject != NULL) {
3005 free(data->newpathobject);
3006 }
3007 free(data);
3008}
3009
f3a75078 3010static void nfs_symlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3011{
3012 SYMLINK3res *res;
3013 struct nfs_cb_data *data = private_data;
3014 struct nfs_context *nfs = data->nfs;
3015 struct nfs_symlink_data *symlink_data = data->continue_data;
3016
f3a75078
RS
3017 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3018
84004dbf
RS
3019 if (status == RPC_STATUS_ERROR) {
3020 data->cb(-EFAULT, nfs, command_data, data->private_data);
3021 free_nfs_cb_data(data);
3022 return;
3023 }
3024 if (status == RPC_STATUS_CANCEL) {
3025 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3026 free_nfs_cb_data(data);
3027 return;
3028 }
3029
3030 res = command_data;
3031 if (res->status != NFS3_OK) {
3032 rpc_set_error(nfs->rpc, "NFS: SYMLINK %s/%s -> %s failed with %s(%d)", symlink_data->newpathparent, symlink_data->newpathobject, symlink_data->oldpath, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
3033 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3034 free_nfs_cb_data(data);
3035 return;
3036 }
3037
3038 data->cb(0, nfs, NULL, data->private_data);
3039 free_nfs_cb_data(data);
3040}
3041
3042static int nfs_symlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3043{
3044 struct nfs_symlink_data *symlink_data = data->continue_data;
8e255816 3045 SYMLINK3args sa;
84004dbf 3046
8e255816
RS
3047 memset(&sa, 0, sizeof(SYMLINK3args));
3048 sa.where.dir.data.data_len = data->fh.data.data_len;
3049 sa.where.dir.data.data_val = data->fh.data.data_val;
3050 sa.where.name = symlink_data->newpathobject;
3051 sa.symlink.symlink_attributes.mode.set_it = 1;
3052 sa.symlink.symlink_attributes.mode.set_mode3_u.mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH;
3053 sa.symlink.symlink_data = symlink_data->oldpath;
3054
3055 if (rpc_nfs_symlink_async(nfs->rpc, nfs_symlink_cb, &sa, data) != 0) {
84004dbf
RS
3056 rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path);
3057 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3058 free_nfs_cb_data(data);
3059 return -1;
3060 }
3061 return 0;
3062}
3063
3064int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3065{
3066 char *ptr;
3067 struct nfs_symlink_data *symlink_data;
3068
3069 symlink_data = malloc(sizeof(struct nfs_symlink_data));
3070 if (symlink_data == NULL) {
cbbf9d3e 3071 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data");
84004dbf
RS
3072 return -1;
3073 }
ea98629a 3074 memset(symlink_data, 0, sizeof(struct nfs_symlink_data));
84004dbf
RS
3075
3076 symlink_data->oldpath = strdup(oldpath);
3077 if (symlink_data->oldpath == NULL) {
cbbf9d3e 3078 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3079 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3080 return -1;
84004dbf
RS
3081 }
3082
3083 symlink_data->newpathparent = strdup(newpath);
3084 if (symlink_data->newpathparent == NULL) {
cbbf9d3e 3085 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 3086 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3087 return -1;
84004dbf
RS
3088 }
3089
93e9306f 3090 ptr = strrchr(symlink_data->newpathparent, '/');
84004dbf 3091 if (ptr == NULL) {
cbbf9d3e 3092 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 3093 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3094 return -1;
84004dbf
RS
3095 }
3096 *ptr = 0;
3097 ptr++;
3098
3099 symlink_data->newpathobject = strdup(ptr);
3100 if (symlink_data->newpathobject == NULL) {
cbbf9d3e 3101 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 3102 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3103 return -1;
84004dbf
RS
3104 }
3105
3106 if (nfs_lookuppath_async(nfs, symlink_data->newpathparent, cb, private_data, nfs_symlink_continue_internal, symlink_data, free_nfs_symlink_data, 0) != 0) {
cbbf9d3e
RS
3107 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3108 return -1;
84004dbf
RS
3109 }
3110
3111 return 0;
3112}
3113
3114
3115
3116/*
3117 * Async rename()
3118 */
3119struct nfs_rename_data {
3120 char *oldpath;
3121 char *oldobject;
3122 struct nfs_fh3 olddir;
3123 char *newpath;
3124 char *newobject;
3125 struct nfs_fh3 newdir;
3126};
3127
3128static void free_nfs_rename_data(void *mem)
3129{
3130 struct nfs_rename_data *data = mem;
3131
3132 if (data->oldpath != NULL) {
3133 free(data->oldpath);
3134 }
3135 if (data->olddir.data.data_val != NULL) {
3136 free(data->olddir.data.data_val);
3137 }
3138 if (data->newpath != NULL) {
3139 free(data->newpath);
3140 }
3141 if (data->newdir.data.data_val != NULL) {
3142 free(data->newdir.data.data_val);
3143 }
3144 free(data);
3145}
3146
f3a75078 3147static void nfs_rename_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3148{
3149 RENAME3res *res;
3150 struct nfs_cb_data *data = private_data;
3151 struct nfs_context *nfs = data->nfs;
3152 struct nfs_rename_data *rename_data = data->continue_data;
3153
f3a75078
RS
3154 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3155
84004dbf
RS
3156 if (status == RPC_STATUS_ERROR) {
3157 data->cb(-EFAULT, nfs, command_data, data->private_data);
3158 free_nfs_cb_data(data);
3159 return;
3160 }
3161 if (status == RPC_STATUS_CANCEL) {
3162 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3163 free_nfs_cb_data(data);
3164 return;
3165 }
3166
3167 res = command_data;
3168 if (res->status != NFS3_OK) {
3169 rpc_set_error(nfs->rpc, "NFS: RENAME %s/%s -> %s/%s failed with %s(%d)", rename_data->oldpath, rename_data->oldobject, rename_data->newpath, rename_data->newobject, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
3170 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3171 free_nfs_cb_data(data);
3172 return;
3173 }
3174
3175 data->cb(0, nfs, NULL, data->private_data);
3176 free_nfs_cb_data(data);
3177}
3178
3179static int nfs_rename_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3180{
3181 struct nfs_rename_data *rename_data = data->continue_data;
3182
3183 /* steal the filehandle */
3184 rename_data->newdir.data.data_len = data->fh.data.data_len;
3185 rename_data->newdir.data.data_val = data->fh.data.data_val;
3186 data->fh.data.data_val = NULL;
3187
3188 if (rpc_nfs_rename_async(nfs->rpc, nfs_rename_cb, &rename_data->olddir, rename_data->oldobject, &rename_data->newdir, rename_data->newobject, data) != 0) {
3189 rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path);
3190 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3191 free_nfs_cb_data(data);
3192 return -1;
3193 }
3194 return 0;
3195}
3196
3197
3198static int nfs_rename_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3199{
3200 struct nfs_rename_data *rename_data = data->continue_data;
3201
3202 /* steal the filehandle */
3203 rename_data->olddir.data.data_len = data->fh.data.data_len;
3204 rename_data->olddir.data.data_val = data->fh.data.data_val;
3205 data->fh.data.data_val = NULL;
3206
3207 if (nfs_lookuppath_async(nfs, rename_data->newpath, data->cb, data->private_data, nfs_rename_continue_2_internal, rename_data, free_nfs_rename_data, 0) != 0) {
3208 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", rename_data->newpath);
3209 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3210 free_nfs_cb_data(data);
3211 return -1;
3212 }
3213 data->continue_data = NULL;
3214 free_nfs_cb_data(data);
3215
3216 return 0;
3217}
3218
3219
3220int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3221{
3222 char *ptr;
3223 struct nfs_rename_data *rename_data;
3224
3225 rename_data = malloc(sizeof(struct nfs_rename_data));
3226 if (rename_data == NULL) {
cbbf9d3e 3227 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data");
84004dbf
RS
3228 return -1;
3229 }
ea98629a 3230 memset(rename_data, 0, sizeof(struct nfs_rename_data));
84004dbf
RS
3231
3232 rename_data->oldpath = strdup(oldpath);
3233 if (rename_data->oldpath == NULL) {
cbbf9d3e 3234 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3235 free_nfs_rename_data(rename_data);
cbbf9d3e 3236 return -1;
84004dbf 3237 }
93e9306f 3238 ptr = strrchr(rename_data->oldpath, '/');
84004dbf 3239 if (ptr == NULL) {
cbbf9d3e 3240 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 3241 free_nfs_rename_data(rename_data);
cbbf9d3e 3242 return -1;
84004dbf
RS
3243 }
3244 *ptr = 0;
3245 ptr++;
3246 rename_data->oldobject = ptr;
3247
3248
3249 rename_data->newpath = strdup(newpath);
3250 if (rename_data->newpath == NULL) {
cbbf9d3e 3251 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 3252 free_nfs_rename_data(rename_data);
cbbf9d3e 3253 return -1;
84004dbf 3254 }
93e9306f 3255 ptr = strrchr(rename_data->newpath, '/');
84004dbf 3256 if (ptr == NULL) {
cbbf9d3e 3257 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 3258 free_nfs_rename_data(rename_data);
cbbf9d3e 3259 return -1;
84004dbf
RS
3260 }
3261 *ptr = 0;
3262 ptr++;
3263 rename_data->newobject = ptr;
3264
3265
3266 if (nfs_lookuppath_async(nfs, rename_data->oldpath, cb, private_data, nfs_rename_continue_1_internal, rename_data, free_nfs_rename_data, 0) != 0) {
cbbf9d3e
RS
3267 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3268 return -1;
84004dbf
RS
3269 }
3270
3271 return 0;
3272}
3273
3274
3275/*
3276 * Async link()
3277 */
3278struct nfs_link_data {
3279 char *oldpath;
3280 struct nfs_fh3 oldfh;
3281 char *newpath;
3282 char *newobject;
3283 struct nfs_fh3 newdir;
3284};
3285
3286static void free_nfs_link_data(void *mem)
3287{
3288 struct nfs_link_data *data = mem;
3289
3290 if (data->oldpath != NULL) {
3291 free(data->oldpath);
3292 }
3293 if (data->oldfh.data.data_val != NULL) {
3294 free(data->oldfh.data.data_val);
3295 }
3296 if (data->newpath != NULL) {
3297 free(data->newpath);
3298 }
3299 if (data->newdir.data.data_val != NULL) {
3300 free(data->newdir.data.data_val);
3301 }
3302 free(data);
3303}
3304
f3a75078 3305static void nfs_link_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3306{
3307 LINK3res *res;
3308 struct nfs_cb_data *data = private_data;
3309 struct nfs_context *nfs = data->nfs;
3310 struct nfs_link_data *link_data = data->continue_data;
3311
f3a75078
RS
3312 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3313
84004dbf
RS
3314 if (status == RPC_STATUS_ERROR) {
3315 data->cb(-EFAULT, nfs, command_data, data->private_data);
3316 free_nfs_cb_data(data);
3317 return;
3318 }
3319 if (status == RPC_STATUS_CANCEL) {
3320 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3321 free_nfs_cb_data(data);
3322 return;
3323 }
3324
3325 res = command_data;
3326 if (res->status != NFS3_OK) {
3327 rpc_set_error(nfs->rpc, "NFS: LINK %s -> %s/%s failed with %s(%d)", link_data->oldpath, link_data->newpath, link_data->newobject, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
3328 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3329 free_nfs_cb_data(data);
3330 return;
3331 }
3332
3333 data->cb(0, nfs, NULL, data->private_data);
3334 free_nfs_cb_data(data);
3335}
3336
3337static int nfs_link_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3338{
3339 struct nfs_link_data *link_data = data->continue_data;
3340
3341 /* steal the filehandle */
3342 link_data->newdir.data.data_len = data->fh.data.data_len;
3343 link_data->newdir.data.data_val = data->fh.data.data_val;
3344 data->fh.data.data_val = NULL;
3345
3346 if (rpc_nfs_link_async(nfs->rpc, nfs_link_cb, &link_data->oldfh, &link_data->newdir, link_data->newobject, data) != 0) {
3347 rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path);
3348 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3349 free_nfs_cb_data(data);
3350 return -1;
3351 }
3352 return 0;
3353}
3354
3355
3356static int nfs_link_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3357{
3358 struct nfs_link_data *link_data = data->continue_data;
3359
3360 /* steal the filehandle */
3361 link_data->oldfh.data.data_len = data->fh.data.data_len;
3362 link_data->oldfh.data.data_val = data->fh.data.data_val;
3363 data->fh.data.data_val = NULL;
3364
3365 if (nfs_lookuppath_async(nfs, link_data->newpath, data->cb, data->private_data, nfs_link_continue_2_internal, link_data, free_nfs_link_data, 0) != 0) {
3366 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath);
3367 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3368 free_nfs_cb_data(data);
3369 return -1;
3370 }
3371 data->continue_data = NULL;
3372 free_nfs_cb_data(data);
3373
3374 return 0;
3375}
3376
3377
3378int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3379{
3380 char *ptr;
3381 struct nfs_link_data *link_data;
3382
3383 link_data = malloc(sizeof(struct nfs_link_data));
3384 if (link_data == NULL) {
cbbf9d3e 3385 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data");
84004dbf
RS
3386 return -1;
3387 }
ea98629a 3388 memset(link_data, 0, sizeof(struct nfs_link_data));
84004dbf
RS
3389
3390 link_data->oldpath = strdup(oldpath);
3391 if (link_data->oldpath == NULL) {
cbbf9d3e 3392 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3393 free_nfs_link_data(link_data);
cbbf9d3e 3394 return -1;
84004dbf
RS
3395 }
3396
3397 link_data->newpath = strdup(newpath);
3398 if (link_data->newpath == NULL) {
cbbf9d3e 3399 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 3400 free_nfs_link_data(link_data);
cbbf9d3e 3401 return -1;
84004dbf 3402 }
93e9306f 3403 ptr = strrchr(link_data->newpath, '/');
84004dbf 3404 if (ptr == NULL) {
cbbf9d3e 3405 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 3406 free_nfs_link_data(link_data);
cbbf9d3e 3407 return -1;
84004dbf
RS
3408 }
3409 *ptr = 0;
3410 ptr++;
3411 link_data->newobject = ptr;
3412
3413
3414 if (nfs_lookuppath_async(nfs, link_data->oldpath, cb, private_data, nfs_link_continue_1_internal, link_data, free_nfs_link_data, 0) != 0) {
cbbf9d3e
RS
3415 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3416 return -1;
84004dbf
RS
3417 }
3418
3419 return 0;
3420}
3421
3422
3423//qqq replace later with lseek()
183451cf 3424uint64_t nfs_get_current_offset(struct nfsfh *nfsfh)
84004dbf
RS
3425{
3426 return nfsfh->offset;
3427}
3428
17ef62fa
RS
3429
3430
3431/*
3432 * Get the maximum supported READ3 size by the server
3433 */
183451cf 3434uint64_t nfs_get_readmax(struct nfs_context *nfs)
17ef62fa
RS
3435{
3436 return nfs->readmax;
3437}
3438
3439/*
3440 * Get the maximum supported WRITE3 size by the server
3441 */
183451cf 3442uint64_t nfs_get_writemax(struct nfs_context *nfs)
17ef62fa 3443{
763cd6e3 3444 /* Some ZDR libraries can not marshall PDUs bigger than this */
18c27b73
RS
3445 if (nfs->writemax < 32768) {
3446 return nfs->writemax;
3447 }
3448 return 32768;
17ef62fa 3449}
1896d37b
RS
3450
3451void nfs_set_error(struct nfs_context *nfs, char *error_string, ...)
3452{
3453 va_list ap;
1e8994af 3454 char *str = NULL;
1896d37b 3455
1e8994af 3456 va_start(ap, error_string);
2606f9bb
RS
3457 str = malloc(1024);
3458 vsnprintf(str, 1024, error_string, ap);
1896d37b
RS
3459 if (nfs->rpc->error_string != NULL) {
3460 free(nfs->rpc->error_string);
3461 }
1896d37b 3462 nfs->rpc->error_string = str;
a8a1b858 3463 va_end(ap);
1896d37b 3464}
7f0242ca
RS
3465
3466
3467
3468struct mount_cb_data {
3469 rpc_cb cb;
3470 void *private_data;
3471 char *server;
3472};
3473
3474static void free_mount_cb_data(struct mount_cb_data *data)
3475{
3476 if (data->server != NULL) {
3477 free(data->server);
3478 data->server = NULL;
3479 }
3480
3481 free(data);
3482}
3483
3484static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3485{
3486 struct mount_cb_data *data = private_data;
3487
f3a75078
RS
3488 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3489
3490 if (status == RPC_STATUS_ERROR) {
7f0242ca
RS
3491 data->cb(rpc, -EFAULT, command_data, data->private_data);
3492 free_mount_cb_data(data);
3493 return;
3494 }
3495 if (status == RPC_STATUS_CANCEL) {
3496 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3497 free_mount_cb_data(data);
3498 return;
3499 }
3500
3501 data->cb(rpc, 0, command_data, data->private_data);
3502 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
3503 rpc_set_error(rpc, "Failed to disconnect\n");
3504 }
3505 free_mount_cb_data(data);
3506}
3507
3508static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3509{
3510 struct mount_cb_data *data = private_data;
3511
f3a75078
RS
3512 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3513
14a062eb
RS
3514 /* Dont want any more callbacks even if the socket is closed */
3515 rpc->connect_cb = NULL;
3516
7f0242ca
RS
3517 if (status == RPC_STATUS_ERROR) {
3518 data->cb(rpc, -EFAULT, command_data, data->private_data);
3519 free_mount_cb_data(data);
3520 return;
3521 }
3522 if (status == RPC_STATUS_CANCEL) {
3523 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3524 free_mount_cb_data(data);
3525 return;
3526 }
3527
3528 if (rpc_mount_export_async(rpc, mount_export_5_cb, data) != 0) {
3529 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3530 free_mount_cb_data(data);
3531 return;
3532 }
3533}
3534
3535static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3536{
3537 struct mount_cb_data *data = private_data;
3538 uint32_t mount_port;
3539
f3a75078
RS
3540 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3541
3542 if (status == RPC_STATUS_ERROR) {
7f0242ca
RS
3543 data->cb(rpc, -EFAULT, command_data, data->private_data);
3544 free_mount_cb_data(data);
3545 return;
3546 }
3547 if (status == RPC_STATUS_CANCEL) {
3548 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3549 free_mount_cb_data(data);
3550 return;
3551 }
3552
3553 mount_port = *(uint32_t *)command_data;
3554 if (mount_port == 0) {
3555 rpc_set_error(rpc, "RPC error. Mount program is not available");
3556 data->cb(rpc, -ENOENT, command_data, data->private_data);
3557 free_mount_cb_data(data);
3558 return;
3559 }
3560
3561 rpc_disconnect(rpc, "normal disconnect");
3562 if (rpc_connect_async(rpc, data->server, mount_port, mount_export_4_cb, data) != 0) {
3563 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3564 free_mount_cb_data(data);
3565 return;
3566 }
3567}
3568
3569static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3570{
3571 struct mount_cb_data *data = private_data;
3572
f3a75078
RS
3573 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3574
7f0242ca
RS
3575 if (status == RPC_STATUS_ERROR) {
3576 data->cb(rpc, -EFAULT, command_data, data->private_data);
3577 free_mount_cb_data(data);
3578 return;
3579 }
3580 if (status == RPC_STATUS_CANCEL) {
3581 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3582 free_mount_cb_data(data);
3583 return;
3584 }
3585
5c6b1176 3586 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, mount_export_3_cb, private_data) != 0) {
7f0242ca
RS
3587 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3588 free_mount_cb_data(data);
3589 return;
3590 }
3591}
3592
3593static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3594{
3595 struct mount_cb_data *data = private_data;
3596
f3a75078
RS
3597 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3598
14a062eb
RS
3599 /* Dont want any more callbacks even if the socket is closed */
3600 rpc->connect_cb = NULL;
3601
7f0242ca
RS
3602 if (status == RPC_STATUS_ERROR) {
3603 data->cb(rpc, -EFAULT, command_data, data->private_data);
3604 free_mount_cb_data(data);
3605 return;
3606 }
3607 if (status == RPC_STATUS_CANCEL) {
3608 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3609 free_mount_cb_data(data);
3610 return;
3611 }
3612
3613 if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) {
3614 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3615 free_mount_cb_data(data);
3616 return;
3617 }
3618}
3619
3620int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data)
3621{
3622 struct mount_cb_data *data;
3623
f3a75078
RS
3624 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3625
7f0242ca
RS
3626 data = malloc(sizeof(struct mount_cb_data));
3627 if (data == NULL) {
3628 return -1;
3629 }
ea98629a 3630 memset(data, 0, sizeof(struct mount_cb_data));
7f0242ca
RS
3631 data->cb = cb;
3632 data->private_data = private_data;
3633 data->server = strdup(server);
3634 if (data->server == NULL) {
3635 free_mount_cb_data(data);
3636 return -1;
3637 }
3638 if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) {
3639 free_mount_cb_data(data);
3640 return -1;
3641 }
3642
3643 return 0;
3644}
3645
df5af25f
RS
3646struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs)
3647{
f3a75078 3648 assert(nfs->rpc->magic == RPC_CONTEXT_MAGIC);
df5af25f
RS
3649 return nfs->rpc;
3650}
3651
b077fdeb
RS
3652const char *nfs_get_server(struct nfs_context *nfs) {
3653 return nfs->server;
3654}
3655
3656const char *nfs_get_export(struct nfs_context *nfs) {
3657 return nfs->export;
3658}
0ad9a1f1 3659
8724c833
RS
3660const struct nfs_fh3 *nfs_get_rootfh(struct nfs_context *nfs) {
3661 return &nfs->rootfh;
3662}
fa3c25be
RS
3663
3664struct nfs_fh3 *nfs_get_fh(struct nfsfh *nfsfh) {
3665 return &nfsfh->fh;
3666}