remove obsolete comment
[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
663 if (path[0] != '/') {
664 rpc_set_error(nfs->rpc, "Pathname is not absulute %s", path);
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
1735 memset(&args, 0, sizeof(CREATE3args));
1736 args.where.dir.data.data_len = data->fh.data.data_len;
1737 args.where.dir.data.data_val = data->fh.data.data_val;
1738 args.where.name = str;
1739 args.how.mode = UNCHECKED;
1740 args.how.createhow3_u.obj_attributes.mode.set_it = 1;
1741 args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = data->continue_int;
1742
1743 if (rpc_nfs_create_async(nfs->rpc, nfs_creat_1_cb, &args, data) != 0) {
84004dbf
RS
1744 rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str);
1745 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1746 free_nfs_cb_data(data);
1747 return -1;
1748 }
1749 return 0;
1750}
1751
1752int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
1753{
1754 char *new_path;
1755 char *ptr;
1756
1757 new_path = strdup(path);
1758 if (new_path == NULL) {
cbbf9d3e 1759 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1760 return -1;
1761 }
1762
93e9306f 1763 ptr = strrchr(new_path, '/');
84004dbf 1764 if (ptr == NULL) {
cbbf9d3e
RS
1765 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1766 return -1;
84004dbf
RS
1767 }
1768 *ptr = 0;
1769
1770 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1771 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_creat_continue_internal, new_path, free, mode) != 0) {
cbbf9d3e
RS
1772 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1773 return -1;
84004dbf
RS
1774 }
1775
1776 return 0;
1777}
1778
1779
1780
1781
1782/*
1783 * Async unlink()
1784 */
f3a75078 1785static void nfs_unlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
1786{
1787 REMOVE3res *res;
1788 struct nfs_cb_data *data = private_data;
1789 struct nfs_context *nfs = data->nfs;
1790 char *str = data->continue_data;
1791
f3a75078
RS
1792 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1793
84004dbf
RS
1794 str = &str[strlen(str) + 1];
1795
1796 if (status == RPC_STATUS_ERROR) {
1797 data->cb(-EFAULT, nfs, command_data, data->private_data);
1798 free_nfs_cb_data(data);
1799 return;
1800 }
1801 if (status == RPC_STATUS_CANCEL) {
1802 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1803 free_nfs_cb_data(data);
1804 return;
1805 }
1806
1807 res = command_data;
1808 if (res->status != NFS3_OK) {
1809 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));
1810 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1811 free_nfs_cb_data(data);
1812 return;
1813 }
1814
1815 data->cb(0, nfs, NULL, data->private_data);
1816 free_nfs_cb_data(data);
1817}
1818
1819static int nfs_unlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1820{
1821 char *str = data->continue_data;
1822
1823 str = &str[strlen(str) + 1];
1824
1825 if (rpc_nfs_remove_async(nfs->rpc, nfs_unlink_cb, &data->fh, str, data) != 0) {
1826 rpc_set_error(nfs->rpc, "RPC error: Failed to send REMOVE call for %s", data->path);
1827 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1828 free_nfs_cb_data(data);
1829 return -1;
1830 }
1831 return 0;
1832}
1833
1834int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1835{
1836 char *new_path;
1837 char *ptr;
1838
1839 new_path = strdup(path);
1840 if (new_path == NULL) {
cbbf9d3e 1841 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1842 return -1;
1843 }
1844
93e9306f 1845 ptr = strrchr(new_path, '/');
84004dbf 1846 if (ptr == NULL) {
cbbf9d3e
RS
1847 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1848 return -1;
84004dbf
RS
1849 }
1850 *ptr = 0;
1851
1852 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1853 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_unlink_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1854 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1855 return -1;
84004dbf
RS
1856 }
1857
1858 return 0;
1859}
1860
1861
1ec6b50a
RS
1862/*
1863 * Async mknod()
1864 */
1865struct mknod_cb_data {
1866 char *path;
1867 int mode;
1868 int major;
1869 int minor;
1870};
1871
1872static void free_mknod_cb_data(void *ptr)
1873{
1874 struct mknod_cb_data *data = ptr;
1875
1876 free(data->path);
1877 free(data);
1878}
1879
f3a75078 1880static void nfs_mknod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
1ec6b50a
RS
1881{
1882 MKNOD3res *res;
1883 struct nfs_cb_data *data = private_data;
1884 struct nfs_context *nfs = data->nfs;
1885 char *str = data->continue_data;
1886
f3a75078
RS
1887 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1888
1ec6b50a
RS
1889 str = &str[strlen(str) + 1];
1890
1891 if (status == RPC_STATUS_ERROR) {
1892 data->cb(-EFAULT, nfs, command_data, data->private_data);
1893 free_nfs_cb_data(data);
1894 return;
1895 }
1896 if (status == RPC_STATUS_CANCEL) {
1897 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1898 free_nfs_cb_data(data);
1899 return;
1900 }
1901
1902 res = command_data;
1903 if (res->status != NFS3_OK) {
1904 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));
1905 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1906 free_nfs_cb_data(data);
1907 return;
1908 }
1909
1910 data->cb(0, nfs, NULL, data->private_data);
1911 free_nfs_cb_data(data);
1912}
1913
1914static int nfs_mknod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1915{
1916 struct mknod_cb_data *cb_data = data->continue_data;
1917 char *str = cb_data->path;
1918
1919 str = &str[strlen(str) + 1];
1920
1921 if (rpc_nfs_mknod_async(nfs->rpc, nfs_mknod_cb, &data->fh, str, cb_data->mode, cb_data->major, cb_data->minor, data) != 0) {
1922 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1923 free_nfs_cb_data(data);
1924 return -1;
1925 }
1926 return 0;
1927}
1928
1929int nfs_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev, nfs_cb cb, void *private_data)
1930{
1931 char *ptr;
1932 struct mknod_cb_data *cb_data;
1933
1934 cb_data = malloc(sizeof(struct mknod_cb_data));
1935 if (cb_data == NULL) {
1936 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for cb data");
1937 return -1;
1938 }
1939
1940 cb_data->path = strdup(path);
1941 if (cb_data->path == NULL) {
1942 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
1943 free(cb_data);
1944 return -1;
1945 }
1946
1947 ptr = strrchr(cb_data->path, '/');
1948 if (ptr == NULL) {
1949 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1950 return -1;
1951 }
1952 *ptr = 0;
1953
1954 cb_data->mode = mode;
1955 cb_data->major = major(dev);
1956 cb_data->minor = minor(dev);
1957
1958 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1959 if (nfs_lookuppath_async(nfs, cb_data->path, cb, private_data, nfs_mknod_continue_internal, cb_data, free_mknod_cb_data, 0) != 0) {
1960 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1961 free_mknod_cb_data(cb_data);
1962 return -1;
1963 }
1964
1965 return 0;
1966}
84004dbf 1967
84004dbf
RS
1968/*
1969 * Async opendir()
1970 */
fb69c64c
RS
1971
1972/* ReadDirPlus Emulation Callback data */
1973struct rdpe_cb_data {
1974 int getattrcount;
1975 int status;
1976 struct nfs_cb_data *data;
1977};
1978
1979/* ReadDirPlus Emulation LOOKUP Callback data */
1980struct rdpe_lookup_cb_data {
1981 struct rdpe_cb_data *rdpe_cb_data;
1982 struct nfsdirent *nfsdirent;
1983};
1984
1985/* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
f3a75078 1986static void nfs_opendir3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf 1987{
fb69c64c
RS
1988 LOOKUP3res *res = command_data;
1989 struct rdpe_lookup_cb_data *rdpe_lookup_cb_data = private_data;
1990 struct rdpe_cb_data *rdpe_cb_data = rdpe_lookup_cb_data->rdpe_cb_data;
1991 struct nfs_cb_data *data = rdpe_cb_data->data;
1992 struct nfsdir *nfsdir = data->continue_data;
1993 struct nfs_context *nfs = data->nfs;
1994 struct nfsdirent *nfsdirent = rdpe_lookup_cb_data->nfsdirent;
1995
f3a75078
RS
1996 assert(rpc->magic == RPC_CONTEXT_MAGIC);
1997
fb69c64c
RS
1998 free(rdpe_lookup_cb_data);
1999
2000 rdpe_cb_data->getattrcount--;
2001
2002 if (status == RPC_STATUS_ERROR) {
2003 rdpe_cb_data->status = RPC_STATUS_ERROR;
2004 }
2005 if (status == RPC_STATUS_CANCEL) {
2006 rdpe_cb_data->status = RPC_STATUS_CANCEL;
2007 }
2008 if (status == RPC_STATUS_SUCCESS && res->status != NFS3_OK) {
2009 rdpe_cb_data->status = RPC_STATUS_ERROR;
2010 }
2011 if (status == RPC_STATUS_SUCCESS && res->status == NFS3_OK) {
2012 if (res->LOOKUP3res_u.resok.obj_attributes.attributes_follow) {
2013 fattr3 *attributes = &res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes;
2014
2015 nfsdirent->type = attributes->type;
2016 nfsdirent->mode = attributes->mode;
2017 nfsdirent->size = attributes->size;
2018
2019 nfsdirent->atime.tv_sec = attributes->atime.seconds;
2020 nfsdirent->atime.tv_usec = attributes->atime.nseconds/1000;
2021 nfsdirent->mtime.tv_sec = attributes->mtime.seconds;
2022 nfsdirent->mtime.tv_usec = attributes->mtime.nseconds/1000;
2023 nfsdirent->ctime.tv_sec = attributes->ctime.seconds;
2024 nfsdirent->ctime.tv_usec = attributes->ctime.nseconds/1000;
2025 }
2026 }
2027
2028 if (rdpe_cb_data->getattrcount == 0) {
2029 if (rdpe_cb_data->status != RPC_STATUS_SUCCESS) {
2030 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2031 nfs_free_nfsdir(nfsdir);
2032 } else {
2033 data->cb(0, nfs, nfsdir, data->private_data);
2034 }
2035 free(rdpe_cb_data);
2036
2037 data->continue_data = NULL;
2038 free_nfs_cb_data(data);
2039 }
2040}
2041
f3a75078 2042static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
fb69c64c
RS
2043{
2044 READDIR3res *res = command_data;
84004dbf
RS
2045 struct nfs_cb_data *data = private_data;
2046 struct nfs_context *nfs = data->nfs;
fb6510bb 2047 struct nfsdir *nfsdir = data->continue_data;
fb69c64c
RS
2048 struct nfsdirent *nfsdirent;
2049 struct entry3 *entry;
84004dbf 2050 uint64_t cookie;
fb69c64c
RS
2051 struct rdpe_cb_data *rdpe_cb_data;
2052
f3a75078
RS
2053 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2054
84004dbf
RS
2055 if (status == RPC_STATUS_ERROR) {
2056 data->cb(-EFAULT, nfs, command_data, data->private_data);
2057 nfs_free_nfsdir(nfsdir);
2058 data->continue_data = NULL;
2059 free_nfs_cb_data(data);
2060 return;
2061 }
fb69c64c 2062
84004dbf
RS
2063 if (status == RPC_STATUS_CANCEL) {
2064 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2065 nfs_free_nfsdir(nfsdir);
2066 data->continue_data = NULL;
2067 free_nfs_cb_data(data);
2068 return;
2069 }
2070
84004dbf
RS
2071 if (res->status != NFS3_OK) {
2072 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));
2073 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2074 nfs_free_nfsdir(nfsdir);
2075 data->continue_data = NULL;
2076 free_nfs_cb_data(data);
2077 return;
fb69c64c
RS
2078 }
2079
2080 entry =res->READDIR3res_u.resok.reply.entries;
2081 while (entry != NULL) {
2082 nfsdirent = malloc(sizeof(struct nfsdirent));
2083 if (nfsdirent == NULL) {
2084 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
2085 nfs_free_nfsdir(nfsdir);
2086 data->continue_data = NULL;
2087 free_nfs_cb_data(data);
2088 return;
2089 }
2090 memset(nfsdirent, 0, sizeof(struct nfsdirent));
2091 nfsdirent->name = strdup(entry->name);
2092 if (nfsdirent->name == NULL) {
2093 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
2094 nfs_free_nfsdir(nfsdir);
2095 data->continue_data = NULL;
2096 free_nfs_cb_data(data);
2097 return;
2098 }
2099 nfsdirent->inode = entry->fileid;
2100
2101 nfsdirent->next = nfsdir->entries;
2102 nfsdir->entries = nfsdirent;
2103
2104 cookie = entry->cookie;
2105 entry = entry->nextentry;
2106 }
2107
2108 if (res->READDIR3res_u.resok.reply.eof == 0) {
2109 if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, cookie, res->READDIR3res_u.resok.cookieverf, 8192, data) != 0) {
2110 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
2111 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2112 nfs_free_nfsdir(nfsdir);
2113 data->continue_data = NULL;
2114 free_nfs_cb_data(data);
2115 return;
2116 }
2117 return;
2118 }
2119
2120 /* steal the dirhandle */
2121 nfsdir->current = nfsdir->entries;
2122
2123 rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data));
2124 rdpe_cb_data->getattrcount = 0;
2125 rdpe_cb_data->status = RPC_STATUS_SUCCESS;
2126 rdpe_cb_data->data = data;
2127 for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) {
2128 struct rdpe_lookup_cb_data *rdpe_lookup_cb_data;
2129
2130 rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data));
2131 rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data;
2132 rdpe_lookup_cb_data->nfsdirent = nfsdirent;
2133
2134 if (rpc_nfs_lookup_async(nfs->rpc, nfs_opendir3_cb, &data->fh, nfsdirent->name, rdpe_lookup_cb_data) != 0) {
2135 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call");
2136
2137 /* if we have already commands in flight, we cant just stop, we have to wait for the
2138 * commands in flight to complete
2139 */
2140 if (rdpe_cb_data->getattrcount > 0) {
2141 rdpe_cb_data->status = RPC_STATUS_ERROR;
2142 free(rdpe_lookup_cb_data);
2143 return;
2144 }
2145
2146 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2147 nfs_free_nfsdir(nfsdir);
2148 data->continue_data = NULL;
2149 free_nfs_cb_data(data);
2150 free(rdpe_lookup_cb_data);
2151 free(rdpe_cb_data);
2152 return;
2153 }
2154 rdpe_cb_data->getattrcount++;
2155 }
2156}
2157
2158
f3a75078 2159static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
fb69c64c
RS
2160{
2161 READDIRPLUS3res *res = command_data;
2162 struct nfs_cb_data *data = private_data;
2163 struct nfs_context *nfs = data->nfs;
2164 struct nfsdir *nfsdir = data->continue_data;
2165 struct entryplus3 *entry;
2166 uint64_t cookie;
2167
f3a75078 2168 assert(rpc->magic == RPC_CONTEXT_MAGIC);
fb69c64c
RS
2169
2170 if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){
2171 cookieverf3 cv;
2172
2173 if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) {
2174 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
2175 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2176 nfs_free_nfsdir(nfsdir);
2177 data->continue_data = NULL;
2178 free_nfs_cb_data(data);
2179 return;
2180 }
2181 return;
2182 }
2183
2184 if (status == RPC_STATUS_CANCEL) {
2185 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2186 nfs_free_nfsdir(nfsdir);
2187 data->continue_data = NULL;
2188 free_nfs_cb_data(data);
2189 return;
2190 }
2191
2192 if (res->status != NFS3_OK) {
2193 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));
2194 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2195 nfs_free_nfsdir(nfsdir);
2196 data->continue_data = NULL;
2197 free_nfs_cb_data(data);
2198 return;
84004dbf
RS
2199 }
2200
f390f181 2201 entry =res->READDIRPLUS3res_u.resok.reply.entries;
84004dbf
RS
2202 while (entry != NULL) {
2203 struct nfsdirent *nfsdirent;
2204
2205 nfsdirent = malloc(sizeof(struct nfsdirent));
2206 if (nfsdirent == NULL) {
2207 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
2208 nfs_free_nfsdir(nfsdir);
2209 data->continue_data = NULL;
2210 free_nfs_cb_data(data);
2211 return;
2212 }
ea98629a 2213 memset(nfsdirent, 0, sizeof(struct nfsdirent));
84004dbf
RS
2214 nfsdirent->name = strdup(entry->name);
2215 if (nfsdirent->name == NULL) {
2216 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
2217 nfs_free_nfsdir(nfsdir);
2218 data->continue_data = NULL;
2219 free_nfs_cb_data(data);
2220 return;
2221 }
2222 nfsdirent->inode = entry->fileid;
0804e67d
RS
2223 if (entry->name_attributes.attributes_follow) {
2224 nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type;
2225 nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode;
2226 nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size;
2227
2228 nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds;
2229 nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000;
2230 nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds;
2231 nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000;
2232 nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds;
2233 nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000;
2234 }
2235
84004dbf
RS
2236 nfsdirent->next = nfsdir->entries;
2237 nfsdir->entries = nfsdirent;
2238
2239 cookie = entry->cookie;
2240 entry = entry->nextentry;
2241 }
2242
f390f181
RS
2243 if (res->READDIRPLUS3res_u.resok.reply.eof == 0) {
2244 if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, cookie, res->READDIRPLUS3res_u.resok.cookieverf, 8192, data) != 0) {
2245 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
2246 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2247 nfs_free_nfsdir(nfsdir);
2248 data->continue_data = NULL;
2249 free_nfs_cb_data(data);
2250 return;
2251 }
2252 return;
2253 }
2254
2255 /* steal the dirhandle */
2256 data->continue_data = NULL;
2257 nfsdir->current = nfsdir->entries;
2258
2259 data->cb(0, nfs, nfsdir, data->private_data);
2260 free_nfs_cb_data(data);
2261}
2262
2263static int nfs_opendir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2264{
2265 cookieverf3 cv;
2266
ea98629a 2267 memset(cv, 0, sizeof(cookieverf3));
f390f181
RS
2268 if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) {
2269 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
2270 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2271 free_nfs_cb_data(data);
2272 return -1;
2273 }
2274 return 0;
2275}
2276
2277int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2278{
2279 struct nfsdir *nfsdir;
2280
2281 nfsdir = malloc(sizeof(struct nfsdir));
2282 if (nfsdir == NULL) {
cbbf9d3e 2283 rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir");
84004dbf
RS
2284 return -1;
2285 }
ea98629a 2286 memset(nfsdir, 0, sizeof(struct nfsdir));
84004dbf
RS
2287
2288 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) {
cbbf9d3e
RS
2289 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2290 return -1;
84004dbf
RS
2291 }
2292
2293 return 0;
2294}
2295
2296
2297struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
2298{
2299 struct nfsdirent *nfsdirent = nfsdir->current;
2300
2301 if (nfsdir->current != NULL) {
2302 nfsdir->current = nfsdir->current->next;
2303 }
2304 return nfsdirent;
2305}
2306
2307
2308void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
2309{
2310 nfs_free_nfsdir(nfsdir);
2311}
2312
2313
2314
2315
2316
2317
2318
2319/*
2320 * Async lseek()
2321 */
2322struct lseek_cb_data {
2323 struct nfs_context *nfs;
2324 struct nfsfh *nfsfh;
183451cf 2325 uint64_t offset;
84004dbf
RS
2326 nfs_cb cb;
2327 void *private_data;
2328};
2329
f3a75078 2330static void nfs_lseek_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2331{
2332 GETATTR3res *res;
2333 struct lseek_cb_data *data = private_data;
2334 struct nfs_context *nfs = data->nfs;
2335
f3a75078
RS
2336 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2337
84004dbf
RS
2338 if (status == RPC_STATUS_ERROR) {
2339 data->cb(-EFAULT, nfs, command_data, data->private_data);
2340 free(data);
2341 return;
2342 }
2343 if (status == RPC_STATUS_CANCEL) {
2344 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2345 free(data);
2346 return;
2347 }
2348
2349 res = command_data;
2350 if (res->status != NFS3_OK) {
2351 rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2352 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2353 free(data);
2354 return;
2355 }
2356
2357 data->nfsfh->offset = data->offset + res->GETATTR3res_u.resok.obj_attributes.size;
2358 data->cb(0, nfs, &data->nfsfh->offset, data->private_data);
2359 free(data);
2360}
2361
183451cf 2362int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, int whence, nfs_cb cb, void *private_data)
84004dbf
RS
2363{
2364 struct lseek_cb_data *data;
2365
2366 if (whence == SEEK_SET) {
2367 nfsfh->offset = offset;
2368 cb(0, nfs, &nfsfh->offset, private_data);
2369 return 0;
2370 }
2371 if (whence == SEEK_CUR) {
2372 nfsfh->offset += offset;
2373 cb(0, nfs, &nfsfh->offset, private_data);
2374 return 0;
2375 }
2376
2377 data = malloc(sizeof(struct lseek_cb_data));
2378 if (data == NULL) {
2379 rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data");
2380 return -1;
2381 }
2382
2383 data->nfs = nfs;
2384 data->nfsfh = nfsfh;
2385 data->offset = offset;
2386 data->cb = cb;
2387 data->private_data = private_data;
2388
2389 if (rpc_nfs_getattr_async(nfs->rpc, nfs_lseek_1_cb, &nfsfh->fh, data) != 0) {
2390 rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call");
2391 free(data);
cbbf9d3e 2392 return -1;
84004dbf
RS
2393 }
2394 return 0;
2395}
2396
2397
2398
2399
2400/*
2401 * Async statvfs()
2402 */
f3a75078 2403static void nfs_statvfs_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2404{
2405 FSSTAT3res *res;
2406 struct nfs_cb_data *data = private_data;
2407 struct nfs_context *nfs = data->nfs;
2408 struct statvfs svfs;
2409
f3a75078
RS
2410 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2411
84004dbf
RS
2412 if (status == RPC_STATUS_ERROR) {
2413 data->cb(-EFAULT, nfs, command_data, data->private_data);
2414 free_nfs_cb_data(data);
2415 return;
2416 }
2417 if (status == RPC_STATUS_CANCEL) {
2418 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2419 free_nfs_cb_data(data);
2420 return;
2421 }
2422
2423 res = command_data;
2424 if (res->status != NFS3_OK) {
2425 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));
2426 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2427 free_nfs_cb_data(data);
2428 return;
2429 }
2430
2431 svfs.f_bsize = 4096;
2432 svfs.f_frsize = 4096;
2433 svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/4096;
2434 svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/4096;
2435 svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/4096;
2436 svfs.f_files = res->FSSTAT3res_u.resok.tfiles;
2437 svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles;
252aa90d 2438#if !defined(ANDROID)
84004dbf
RS
2439 svfs.f_favail = res->FSSTAT3res_u.resok.afiles;
2440 svfs.f_fsid = 0;
2441 svfs.f_flag = 0;
2442 svfs.f_namemax = 256;
252aa90d 2443#endif
84004dbf
RS
2444
2445 data->cb(0, nfs, &svfs, data->private_data);
2446 free_nfs_cb_data(data);
2447}
2448
2449static int nfs_statvfs_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2450{
2451 if (rpc_nfs_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &data->fh, data) != 0) {
2452 rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT call for %s", data->path);
2453 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2454 free_nfs_cb_data(data);
2455 return -1;
2456 }
2457 return 0;
2458}
2459
2460int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2461{
2462 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2463 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2464 return -1;
2465 }
2466
2467 return 0;
2468}
2469
2470
2471
2472
2473/*
2474 * Async readlink()
2475 */
f3a75078 2476static void nfs_readlink_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2477{
2478 READLINK3res *res;
2479 struct nfs_cb_data *data = private_data;
2480 struct nfs_context *nfs = data->nfs;
2481
f3a75078
RS
2482 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2483
84004dbf
RS
2484 if (status == RPC_STATUS_ERROR) {
2485 data->cb(-EFAULT, nfs, command_data, data->private_data);
2486 free_nfs_cb_data(data);
2487 return;
2488 }
2489 if (status == RPC_STATUS_CANCEL) {
2490 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2491 free_nfs_cb_data(data);
2492 return;
2493 }
2494
2495 res = command_data;
2496 if (res->status != NFS3_OK) {
2497 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));
2498 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2499 free_nfs_cb_data(data);
2500 return;
2501 }
2502
2503
2504 data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data);
2505 free_nfs_cb_data(data);
2506}
2507
2508static int nfs_readlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2509{
16104b27
RS
2510 READLINK3args args;
2511
2512 args.symlink.data.data_len = data->fh.data.data_len;
2513 args.symlink.data.data_val = data->fh.data.data_val;
2514
2515 if (rpc_nfs_readlink_async(nfs->rpc, nfs_readlink_1_cb, &args, data) != 0) {
84004dbf
RS
2516 rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path);
2517 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2518 free_nfs_cb_data(data);
2519 return -1;
2520 }
2521 return 0;
2522}
2523
2524int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2525{
2526 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2527 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2528 return -1;
2529 }
2530
2531 return 0;
2532}
2533
2534
2535
2536
2537/*
2538 * Async chmod()
2539 */
f3a75078 2540static void nfs_chmod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2541{
2542 struct nfs_cb_data *data = private_data;
2543 struct nfs_context *nfs = data->nfs;
2544 SETATTR3res *res;
2545
f3a75078
RS
2546 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2547
84004dbf
RS
2548 if (status == RPC_STATUS_ERROR) {
2549 data->cb(-EFAULT, nfs, command_data, data->private_data);
2550 free_nfs_cb_data(data);
2551 return;
2552 }
2553 if (status == RPC_STATUS_CANCEL) {
2554 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2555 free_nfs_cb_data(data);
2556 return;
2557 }
2558
2559 res = command_data;
2560 if (res->status != NFS3_OK) {
2561 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2562 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2563 free_nfs_cb_data(data);
2564 return;
2565 }
2566
2567 data->cb(0, nfs, NULL, data->private_data);
2568 free_nfs_cb_data(data);
2569}
2570
2571static int nfs_chmod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2572{
2573 SETATTR3args args;
2574
ea98629a 2575 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2576 args.object.data.data_len = data->fh.data.data_len;
2577 args.object.data.data_val = data->fh.data.data_val;
2578 args.new_attributes.mode.set_it = 1;
2579 args.new_attributes.mode.set_mode3_u.mode = data->continue_int;
2580
2581 if (rpc_nfs_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) {
2582 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2583 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2584 free_nfs_cb_data(data);
2585 return -1;
2586 }
2587 return 0;
2588}
2589
2590
2591int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
2592{
2593 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e 2594 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2595 return -1;
2596 }
2597
2598 return 0;
2599}
2600
2601/*
2602 * Async fchmod()
2603 */
2604int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data)
2605{
2606 struct nfs_cb_data *data;
2607
2608 data = malloc(sizeof(struct nfs_cb_data));
2609 if (data == NULL) {
cbbf9d3e 2610 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data");
84004dbf
RS
2611 return -1;
2612 }
ea98629a 2613 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
2614 data->nfs = nfs;
2615 data->cb = cb;
2616 data->private_data = private_data;
2617 data->continue_int = mode;
2618 data->fh.data.data_len = nfsfh->fh.data.data_len;
2619 data->fh.data.data_val = malloc(data->fh.data.data_len);
2620 if (data->fh.data.data_val == NULL) {
2621 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
2622 free_nfs_cb_data(data);
2623 return -1;
2624 }
2625 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
2626
2627 if (nfs_chmod_continue_internal(nfs, data) != 0) {
2628 free_nfs_cb_data(data);
2629 return -1;
2630 }
2631
2632 return 0;
2633}
2634
2635
2636
2637/*
2638 * Async chown()
2639 */
f3a75078 2640static void nfs_chown_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2641{
2642 struct nfs_cb_data *data = private_data;
2643 struct nfs_context *nfs = data->nfs;
2644 SETATTR3res *res;
2645
f3a75078
RS
2646 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2647
84004dbf
RS
2648 if (status == RPC_STATUS_ERROR) {
2649 data->cb(-EFAULT, nfs, command_data, data->private_data);
2650 free_nfs_cb_data(data);
2651 return;
2652 }
2653 if (status == RPC_STATUS_CANCEL) {
2654 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2655 free_nfs_cb_data(data);
2656 return;
2657 }
2658
2659 res = command_data;
2660 if (res->status != NFS3_OK) {
2661 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2662 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2663 free_nfs_cb_data(data);
2664 return;
2665 }
2666
2667 data->cb(0, nfs, NULL, data->private_data);
2668 free_nfs_cb_data(data);
2669}
2670
2671struct nfs_chown_data {
2672 uid_t uid;
2673 gid_t gid;
2674};
2675
2676static int nfs_chown_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2677{
2678 SETATTR3args args;
2679 struct nfs_chown_data *chown_data = data->continue_data;
2680
ea98629a 2681 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2682 args.object.data.data_len = data->fh.data.data_len;
2683 args.object.data.data_val = data->fh.data.data_val;
2684 if (chown_data->uid != (uid_t)-1) {
2685 args.new_attributes.uid.set_it = 1;
2686 args.new_attributes.uid.set_uid3_u.uid = chown_data->uid;
2687 }
2688 if (chown_data->gid != (gid_t)-1) {
2689 args.new_attributes.gid.set_it = 1;
2690 args.new_attributes.gid.set_gid3_u.gid = chown_data->gid;
2691 }
2692
2693 if (rpc_nfs_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) {
2694 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2695 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2696 free_nfs_cb_data(data);
2697 return -1;
2698 }
2699 return 0;
2700}
2701
2702
2703int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data)
2704{
2705 struct nfs_chown_data *chown_data;
2706
2707 chown_data = malloc(sizeof(struct nfs_chown_data));
2708 if (chown_data == NULL) {
cbbf9d3e 2709 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
2710 return -1;
2711 }
2712
2713 chown_data->uid = uid;
2714 chown_data->gid = gid;
2715
2716 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) {
cbbf9d3e 2717 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2718 return -1;
2719 }
2720
2721 return 0;
2722}
2723
2724
2725/*
2726 * Async fchown()
2727 */
2728int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data)
2729{
2730 struct nfs_cb_data *data;
2731 struct nfs_chown_data *chown_data;
2732
2733 chown_data = malloc(sizeof(struct nfs_chown_data));
2734 if (chown_data == NULL) {
cbbf9d3e 2735 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
2736 return -1;
2737 }
2738
2739 chown_data->uid = uid;
2740 chown_data->gid = gid;
2741
2742
2743 data = malloc(sizeof(struct nfs_cb_data));
2744 if (data == NULL) {
cbbf9d3e 2745 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data");
84004dbf
RS
2746 return -1;
2747 }
ea98629a 2748 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
2749 data->nfs = nfs;
2750 data->cb = cb;
2751 data->private_data = private_data;
2752 data->continue_data = chown_data;
2753 data->fh.data.data_len = nfsfh->fh.data.data_len;
2754 data->fh.data.data_val = malloc(data->fh.data.data_len);
2755 if (data->fh.data.data_val == NULL) {
2756 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
2757 free_nfs_cb_data(data);
2758 return -1;
2759 }
2760 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
2761
2762
2763 if (nfs_chown_continue_internal(nfs, data) != 0) {
2764 free_nfs_cb_data(data);
2765 return -1;
2766 }
2767
2768 return 0;
2769}
2770
2771
2772
2773
2774
2775/*
2776 * Async utimes()
2777 */
f3a75078 2778static void nfs_utimes_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2779{
2780 struct nfs_cb_data *data = private_data;
2781 struct nfs_context *nfs = data->nfs;
2782 SETATTR3res *res;
2783
f3a75078
RS
2784 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2785
84004dbf
RS
2786 if (status == RPC_STATUS_ERROR) {
2787 data->cb(-EFAULT, nfs, command_data, data->private_data);
2788 free_nfs_cb_data(data);
2789 return;
2790 }
2791 if (status == RPC_STATUS_CANCEL) {
2792 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2793 free_nfs_cb_data(data);
2794 return;
2795 }
2796
2797 res = command_data;
2798 if (res->status != NFS3_OK) {
2799 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2800 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2801 free_nfs_cb_data(data);
2802 return;
2803 }
2804
2805 data->cb(0, nfs, NULL, data->private_data);
2806 free_nfs_cb_data(data);
2807}
2808
2809static int nfs_utimes_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2810{
2811 SETATTR3args args;
2812 struct timeval *utimes_data = data->continue_data;
2813
ea98629a 2814 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2815 args.object.data.data_len = data->fh.data.data_len;
2816 args.object.data.data_val = data->fh.data.data_val;
2817 if (utimes_data != NULL) {
2818 args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME;
2819 args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec;
2820 args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000;
2821 args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME;
2822 args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec;
2823 args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000;
2824 } else {
2825 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2826 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2827 }
2828
2829 if (rpc_nfs_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) {
2830 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2831 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2832 free_nfs_cb_data(data);
2833 return -1;
2834 }
2835 return 0;
2836}
2837
2838
2839int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data)
2840{
2841 struct timeval *new_times = NULL;
2842
2843 if (times != NULL) {
2844 new_times = malloc(sizeof(struct timeval)*2);
2845 if (new_times == NULL) {
cbbf9d3e 2846 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
2847 return -1;
2848 }
2849
2850 memcpy(new_times, times, sizeof(struct timeval)*2);
2851 }
2852
2853 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 2854 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2855 return -1;
2856 }
2857
2858 return 0;
2859}
2860
2861/*
2862 * Async utime()
2863 */
2864int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data)
2865{
2866 struct timeval *new_times = NULL;
2867
2868 if (times != NULL) {
2869 new_times = malloc(sizeof(struct timeval)*2);
2870 if (new_times == NULL) {
cbbf9d3e 2871 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
2872 return -1;
2873 }
2874
2875 new_times[0].tv_sec = times->actime;
2876 new_times[0].tv_usec = 0;
2877 new_times[1].tv_sec = times->modtime;
2878 new_times[1].tv_usec = 0;
2879 }
2880
2881 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 2882 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2883 return -1;
2884 }
2885
2886 return 0;
2887}
2888
2889
2890
2891
2892
2893/*
2894 * Async access()
2895 */
f3a75078 2896static void nfs_access_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
2897{
2898 ACCESS3res *res;
2899 struct nfs_cb_data *data = private_data;
2900 struct nfs_context *nfs = data->nfs;
2901 unsigned int nfsmode = 0;
2902
f3a75078
RS
2903 assert(rpc->magic == RPC_CONTEXT_MAGIC);
2904
84004dbf
RS
2905 if (status == RPC_STATUS_ERROR) {
2906 data->cb(-EFAULT, nfs, command_data, data->private_data);
2907 free_nfs_cb_data(data);
2908 return;
2909 }
2910 if (status == RPC_STATUS_CANCEL) {
2911 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2912 free_nfs_cb_data(data);
2913 return;
2914 }
2915
2916 res = command_data;
2917 if (res->status != NFS3_OK) {
2918 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));
2919 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2920 free_nfs_cb_data(data);
2921 return;
2922 }
2923
2924 if (data->continue_int & R_OK) {
2925 nfsmode |= ACCESS3_READ;
2926 }
2927 if (data->continue_int & W_OK) {
2928 nfsmode |= ACCESS3_MODIFY;
2929 }
2930 if (data->continue_int & X_OK) {
2931 nfsmode |= ACCESS3_EXECUTE;
2932 }
2933
2934 if (res->ACCESS3res_u.resok.access != nfsmode) {
2935 rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2936 nfsmode&ACCESS3_READ?'r':'-',
2937 nfsmode&ACCESS3_MODIFY?'w':'-',
2938 nfsmode&ACCESS3_EXECUTE?'x':'-',
2939 res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-',
2940 res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-',
2941 res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-');
2942 data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data);
2943 free_nfs_cb_data(data);
2944 return;
2945 }
2946
2947 data->cb(0, nfs, NULL, data->private_data);
2948 free_nfs_cb_data(data);
2949}
2950
2951static int nfs_access_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2952{
2953 int nfsmode = 0;
2954
2955 if (data->continue_int & R_OK) {
2956 nfsmode |= ACCESS3_READ;
2957 }
2958 if (data->continue_int & W_OK) {
2959 nfsmode |= ACCESS3_MODIFY;
2960 }
2961 if (data->continue_int & X_OK) {
2962 nfsmode |= ACCESS3_EXECUTE;
2963 }
2964
2965 if (rpc_nfs_access_async(nfs->rpc, nfs_access_cb, &data->fh, nfsmode, data) != 0) {
2966 rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
2967 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2968 free_nfs_cb_data(data);
2969 return -1;
2970 }
2971 return 0;
2972}
2973
2974int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
2975{
2976 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e
RS
2977 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2978 return -1;
84004dbf
RS
2979 }
2980
2981 return 0;
2982}
2983
2984
2985
2986/*
2987 * Async symlink()
2988 */
2989struct nfs_symlink_data {
2990 char *oldpath;
2991 char *newpathparent;
2992 char *newpathobject;
2993};
2994
2995static void free_nfs_symlink_data(void *mem)
2996{
2997 struct nfs_symlink_data *data = mem;
2998
2999 if (data->oldpath != NULL) {
3000 free(data->oldpath);
3001 }
3002 if (data->newpathparent != NULL) {
3003 free(data->newpathparent);
3004 }
3005 if (data->newpathobject != NULL) {
3006 free(data->newpathobject);
3007 }
3008 free(data);
3009}
3010
f3a75078 3011static void nfs_symlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3012{
3013 SYMLINK3res *res;
3014 struct nfs_cb_data *data = private_data;
3015 struct nfs_context *nfs = data->nfs;
3016 struct nfs_symlink_data *symlink_data = data->continue_data;
3017
f3a75078
RS
3018 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3019
84004dbf
RS
3020 if (status == RPC_STATUS_ERROR) {
3021 data->cb(-EFAULT, nfs, command_data, data->private_data);
3022 free_nfs_cb_data(data);
3023 return;
3024 }
3025 if (status == RPC_STATUS_CANCEL) {
3026 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3027 free_nfs_cb_data(data);
3028 return;
3029 }
3030
3031 res = command_data;
3032 if (res->status != NFS3_OK) {
3033 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));
3034 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3035 free_nfs_cb_data(data);
3036 return;
3037 }
3038
3039 data->cb(0, nfs, NULL, data->private_data);
3040 free_nfs_cb_data(data);
3041}
3042
3043static int nfs_symlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3044{
3045 struct nfs_symlink_data *symlink_data = data->continue_data;
8e255816 3046 SYMLINK3args sa;
84004dbf 3047
8e255816
RS
3048 memset(&sa, 0, sizeof(SYMLINK3args));
3049 sa.where.dir.data.data_len = data->fh.data.data_len;
3050 sa.where.dir.data.data_val = data->fh.data.data_val;
3051 sa.where.name = symlink_data->newpathobject;
3052 sa.symlink.symlink_attributes.mode.set_it = 1;
3053 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;
3054 sa.symlink.symlink_data = symlink_data->oldpath;
3055
3056 if (rpc_nfs_symlink_async(nfs->rpc, nfs_symlink_cb, &sa, data) != 0) {
84004dbf
RS
3057 rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path);
3058 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3059 free_nfs_cb_data(data);
3060 return -1;
3061 }
3062 return 0;
3063}
3064
3065int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3066{
3067 char *ptr;
3068 struct nfs_symlink_data *symlink_data;
3069
3070 symlink_data = malloc(sizeof(struct nfs_symlink_data));
3071 if (symlink_data == NULL) {
cbbf9d3e 3072 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data");
84004dbf
RS
3073 return -1;
3074 }
ea98629a 3075 memset(symlink_data, 0, sizeof(struct nfs_symlink_data));
84004dbf
RS
3076
3077 symlink_data->oldpath = strdup(oldpath);
3078 if (symlink_data->oldpath == NULL) {
cbbf9d3e 3079 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3080 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3081 return -1;
84004dbf
RS
3082 }
3083
3084 symlink_data->newpathparent = strdup(newpath);
3085 if (symlink_data->newpathparent == NULL) {
cbbf9d3e 3086 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 3087 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3088 return -1;
84004dbf
RS
3089 }
3090
93e9306f 3091 ptr = strrchr(symlink_data->newpathparent, '/');
84004dbf 3092 if (ptr == NULL) {
cbbf9d3e 3093 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 3094 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3095 return -1;
84004dbf
RS
3096 }
3097 *ptr = 0;
3098 ptr++;
3099
3100 symlink_data->newpathobject = strdup(ptr);
3101 if (symlink_data->newpathobject == NULL) {
cbbf9d3e 3102 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 3103 free_nfs_symlink_data(symlink_data);
cbbf9d3e 3104 return -1;
84004dbf
RS
3105 }
3106
3107 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
3108 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3109 return -1;
84004dbf
RS
3110 }
3111
3112 return 0;
3113}
3114
3115
3116
3117/*
3118 * Async rename()
3119 */
3120struct nfs_rename_data {
3121 char *oldpath;
3122 char *oldobject;
3123 struct nfs_fh3 olddir;
3124 char *newpath;
3125 char *newobject;
3126 struct nfs_fh3 newdir;
3127};
3128
3129static void free_nfs_rename_data(void *mem)
3130{
3131 struct nfs_rename_data *data = mem;
3132
3133 if (data->oldpath != NULL) {
3134 free(data->oldpath);
3135 }
3136 if (data->olddir.data.data_val != NULL) {
3137 free(data->olddir.data.data_val);
3138 }
3139 if (data->newpath != NULL) {
3140 free(data->newpath);
3141 }
3142 if (data->newdir.data.data_val != NULL) {
3143 free(data->newdir.data.data_val);
3144 }
3145 free(data);
3146}
3147
f3a75078 3148static void nfs_rename_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3149{
3150 RENAME3res *res;
3151 struct nfs_cb_data *data = private_data;
3152 struct nfs_context *nfs = data->nfs;
3153 struct nfs_rename_data *rename_data = data->continue_data;
3154
f3a75078
RS
3155 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3156
84004dbf
RS
3157 if (status == RPC_STATUS_ERROR) {
3158 data->cb(-EFAULT, nfs, command_data, data->private_data);
3159 free_nfs_cb_data(data);
3160 return;
3161 }
3162 if (status == RPC_STATUS_CANCEL) {
3163 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3164 free_nfs_cb_data(data);
3165 return;
3166 }
3167
3168 res = command_data;
3169 if (res->status != NFS3_OK) {
3170 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));
3171 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3172 free_nfs_cb_data(data);
3173 return;
3174 }
3175
3176 data->cb(0, nfs, NULL, data->private_data);
3177 free_nfs_cb_data(data);
3178}
3179
3180static int nfs_rename_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3181{
3182 struct nfs_rename_data *rename_data = data->continue_data;
3183
3184 /* steal the filehandle */
3185 rename_data->newdir.data.data_len = data->fh.data.data_len;
3186 rename_data->newdir.data.data_val = data->fh.data.data_val;
3187 data->fh.data.data_val = NULL;
3188
3189 if (rpc_nfs_rename_async(nfs->rpc, nfs_rename_cb, &rename_data->olddir, rename_data->oldobject, &rename_data->newdir, rename_data->newobject, data) != 0) {
3190 rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path);
3191 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3192 free_nfs_cb_data(data);
3193 return -1;
3194 }
3195 return 0;
3196}
3197
3198
3199static int nfs_rename_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3200{
3201 struct nfs_rename_data *rename_data = data->continue_data;
3202
3203 /* steal the filehandle */
3204 rename_data->olddir.data.data_len = data->fh.data.data_len;
3205 rename_data->olddir.data.data_val = data->fh.data.data_val;
3206 data->fh.data.data_val = NULL;
3207
3208 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) {
3209 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", rename_data->newpath);
3210 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3211 free_nfs_cb_data(data);
3212 return -1;
3213 }
3214 data->continue_data = NULL;
3215 free_nfs_cb_data(data);
3216
3217 return 0;
3218}
3219
3220
3221int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3222{
3223 char *ptr;
3224 struct nfs_rename_data *rename_data;
3225
3226 rename_data = malloc(sizeof(struct nfs_rename_data));
3227 if (rename_data == NULL) {
cbbf9d3e 3228 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data");
84004dbf
RS
3229 return -1;
3230 }
ea98629a 3231 memset(rename_data, 0, sizeof(struct nfs_rename_data));
84004dbf
RS
3232
3233 rename_data->oldpath = strdup(oldpath);
3234 if (rename_data->oldpath == NULL) {
cbbf9d3e 3235 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3236 free_nfs_rename_data(rename_data);
cbbf9d3e 3237 return -1;
84004dbf 3238 }
93e9306f 3239 ptr = strrchr(rename_data->oldpath, '/');
84004dbf 3240 if (ptr == NULL) {
cbbf9d3e 3241 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 3242 free_nfs_rename_data(rename_data);
cbbf9d3e 3243 return -1;
84004dbf
RS
3244 }
3245 *ptr = 0;
3246 ptr++;
3247 rename_data->oldobject = ptr;
3248
3249
3250 rename_data->newpath = strdup(newpath);
3251 if (rename_data->newpath == NULL) {
cbbf9d3e 3252 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 3253 free_nfs_rename_data(rename_data);
cbbf9d3e 3254 return -1;
84004dbf 3255 }
93e9306f 3256 ptr = strrchr(rename_data->newpath, '/');
84004dbf 3257 if (ptr == NULL) {
cbbf9d3e 3258 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 3259 free_nfs_rename_data(rename_data);
cbbf9d3e 3260 return -1;
84004dbf
RS
3261 }
3262 *ptr = 0;
3263 ptr++;
3264 rename_data->newobject = ptr;
3265
3266
3267 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
3268 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3269 return -1;
84004dbf
RS
3270 }
3271
3272 return 0;
3273}
3274
3275
3276/*
3277 * Async link()
3278 */
3279struct nfs_link_data {
3280 char *oldpath;
3281 struct nfs_fh3 oldfh;
3282 char *newpath;
3283 char *newobject;
3284 struct nfs_fh3 newdir;
3285};
3286
3287static void free_nfs_link_data(void *mem)
3288{
3289 struct nfs_link_data *data = mem;
3290
3291 if (data->oldpath != NULL) {
3292 free(data->oldpath);
3293 }
3294 if (data->oldfh.data.data_val != NULL) {
3295 free(data->oldfh.data.data_val);
3296 }
3297 if (data->newpath != NULL) {
3298 free(data->newpath);
3299 }
3300 if (data->newdir.data.data_val != NULL) {
3301 free(data->newdir.data.data_val);
3302 }
3303 free(data);
3304}
3305
f3a75078 3306static void nfs_link_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
3307{
3308 LINK3res *res;
3309 struct nfs_cb_data *data = private_data;
3310 struct nfs_context *nfs = data->nfs;
3311 struct nfs_link_data *link_data = data->continue_data;
3312
f3a75078
RS
3313 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3314
84004dbf
RS
3315 if (status == RPC_STATUS_ERROR) {
3316 data->cb(-EFAULT, nfs, command_data, data->private_data);
3317 free_nfs_cb_data(data);
3318 return;
3319 }
3320 if (status == RPC_STATUS_CANCEL) {
3321 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3322 free_nfs_cb_data(data);
3323 return;
3324 }
3325
3326 res = command_data;
3327 if (res->status != NFS3_OK) {
3328 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));
3329 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3330 free_nfs_cb_data(data);
3331 return;
3332 }
3333
3334 data->cb(0, nfs, NULL, data->private_data);
3335 free_nfs_cb_data(data);
3336}
3337
3338static int nfs_link_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3339{
3340 struct nfs_link_data *link_data = data->continue_data;
3341
3342 /* steal the filehandle */
3343 link_data->newdir.data.data_len = data->fh.data.data_len;
3344 link_data->newdir.data.data_val = data->fh.data.data_val;
3345 data->fh.data.data_val = NULL;
3346
3347 if (rpc_nfs_link_async(nfs->rpc, nfs_link_cb, &link_data->oldfh, &link_data->newdir, link_data->newobject, data) != 0) {
3348 rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path);
3349 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3350 free_nfs_cb_data(data);
3351 return -1;
3352 }
3353 return 0;
3354}
3355
3356
3357static int nfs_link_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3358{
3359 struct nfs_link_data *link_data = data->continue_data;
3360
3361 /* steal the filehandle */
3362 link_data->oldfh.data.data_len = data->fh.data.data_len;
3363 link_data->oldfh.data.data_val = data->fh.data.data_val;
3364 data->fh.data.data_val = NULL;
3365
3366 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) {
3367 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath);
3368 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3369 free_nfs_cb_data(data);
3370 return -1;
3371 }
3372 data->continue_data = NULL;
3373 free_nfs_cb_data(data);
3374
3375 return 0;
3376}
3377
3378
3379int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3380{
3381 char *ptr;
3382 struct nfs_link_data *link_data;
3383
3384 link_data = malloc(sizeof(struct nfs_link_data));
3385 if (link_data == NULL) {
cbbf9d3e 3386 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data");
84004dbf
RS
3387 return -1;
3388 }
ea98629a 3389 memset(link_data, 0, sizeof(struct nfs_link_data));
84004dbf
RS
3390
3391 link_data->oldpath = strdup(oldpath);
3392 if (link_data->oldpath == NULL) {
cbbf9d3e 3393 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3394 free_nfs_link_data(link_data);
cbbf9d3e 3395 return -1;
84004dbf
RS
3396 }
3397
3398 link_data->newpath = strdup(newpath);
3399 if (link_data->newpath == NULL) {
cbbf9d3e 3400 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 3401 free_nfs_link_data(link_data);
cbbf9d3e 3402 return -1;
84004dbf 3403 }
93e9306f 3404 ptr = strrchr(link_data->newpath, '/');
84004dbf 3405 if (ptr == NULL) {
cbbf9d3e 3406 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 3407 free_nfs_link_data(link_data);
cbbf9d3e 3408 return -1;
84004dbf
RS
3409 }
3410 *ptr = 0;
3411 ptr++;
3412 link_data->newobject = ptr;
3413
3414
3415 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
3416 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3417 return -1;
84004dbf
RS
3418 }
3419
3420 return 0;
3421}
3422
3423
3424//qqq replace later with lseek()
183451cf 3425uint64_t nfs_get_current_offset(struct nfsfh *nfsfh)
84004dbf
RS
3426{
3427 return nfsfh->offset;
3428}
3429
17ef62fa
RS
3430
3431
3432/*
3433 * Get the maximum supported READ3 size by the server
3434 */
183451cf 3435uint64_t nfs_get_readmax(struct nfs_context *nfs)
17ef62fa
RS
3436{
3437 return nfs->readmax;
3438}
3439
3440/*
3441 * Get the maximum supported WRITE3 size by the server
3442 */
183451cf 3443uint64_t nfs_get_writemax(struct nfs_context *nfs)
17ef62fa 3444{
763cd6e3 3445 /* Some ZDR libraries can not marshall PDUs bigger than this */
18c27b73
RS
3446 if (nfs->writemax < 32768) {
3447 return nfs->writemax;
3448 }
3449 return 32768;
17ef62fa 3450}
1896d37b
RS
3451
3452void nfs_set_error(struct nfs_context *nfs, char *error_string, ...)
3453{
3454 va_list ap;
1e8994af 3455 char *str = NULL;
1896d37b 3456
1e8994af 3457 va_start(ap, error_string);
2606f9bb
RS
3458 str = malloc(1024);
3459 vsnprintf(str, 1024, error_string, ap);
1896d37b
RS
3460 if (nfs->rpc->error_string != NULL) {
3461 free(nfs->rpc->error_string);
3462 }
1896d37b 3463 nfs->rpc->error_string = str;
a8a1b858 3464 va_end(ap);
1896d37b 3465}
7f0242ca
RS
3466
3467
3468
3469struct mount_cb_data {
3470 rpc_cb cb;
3471 void *private_data;
3472 char *server;
3473};
3474
3475static void free_mount_cb_data(struct mount_cb_data *data)
3476{
3477 if (data->server != NULL) {
3478 free(data->server);
3479 data->server = NULL;
3480 }
3481
3482 free(data);
3483}
3484
3485static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3486{
3487 struct mount_cb_data *data = private_data;
3488
f3a75078
RS
3489 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3490
3491 if (status == RPC_STATUS_ERROR) {
7f0242ca
RS
3492 data->cb(rpc, -EFAULT, command_data, data->private_data);
3493 free_mount_cb_data(data);
3494 return;
3495 }
3496 if (status == RPC_STATUS_CANCEL) {
3497 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3498 free_mount_cb_data(data);
3499 return;
3500 }
3501
3502 data->cb(rpc, 0, command_data, data->private_data);
3503 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
3504 rpc_set_error(rpc, "Failed to disconnect\n");
3505 }
3506 free_mount_cb_data(data);
3507}
3508
3509static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3510{
3511 struct mount_cb_data *data = private_data;
3512
f3a75078
RS
3513 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3514
14a062eb
RS
3515 /* Dont want any more callbacks even if the socket is closed */
3516 rpc->connect_cb = NULL;
3517
7f0242ca
RS
3518 if (status == RPC_STATUS_ERROR) {
3519 data->cb(rpc, -EFAULT, command_data, data->private_data);
3520 free_mount_cb_data(data);
3521 return;
3522 }
3523 if (status == RPC_STATUS_CANCEL) {
3524 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3525 free_mount_cb_data(data);
3526 return;
3527 }
3528
3529 if (rpc_mount_export_async(rpc, mount_export_5_cb, data) != 0) {
3530 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3531 free_mount_cb_data(data);
3532 return;
3533 }
3534}
3535
3536static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3537{
3538 struct mount_cb_data *data = private_data;
3539 uint32_t mount_port;
3540
f3a75078
RS
3541 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3542
3543 if (status == RPC_STATUS_ERROR) {
7f0242ca
RS
3544 data->cb(rpc, -EFAULT, command_data, data->private_data);
3545 free_mount_cb_data(data);
3546 return;
3547 }
3548 if (status == RPC_STATUS_CANCEL) {
3549 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3550 free_mount_cb_data(data);
3551 return;
3552 }
3553
3554 mount_port = *(uint32_t *)command_data;
3555 if (mount_port == 0) {
3556 rpc_set_error(rpc, "RPC error. Mount program is not available");
3557 data->cb(rpc, -ENOENT, command_data, data->private_data);
3558 free_mount_cb_data(data);
3559 return;
3560 }
3561
3562 rpc_disconnect(rpc, "normal disconnect");
3563 if (rpc_connect_async(rpc, data->server, mount_port, mount_export_4_cb, data) != 0) {
3564 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3565 free_mount_cb_data(data);
3566 return;
3567 }
3568}
3569
3570static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3571{
3572 struct mount_cb_data *data = private_data;
3573
f3a75078
RS
3574 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3575
7f0242ca
RS
3576 if (status == RPC_STATUS_ERROR) {
3577 data->cb(rpc, -EFAULT, command_data, data->private_data);
3578 free_mount_cb_data(data);
3579 return;
3580 }
3581 if (status == RPC_STATUS_CANCEL) {
3582 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3583 free_mount_cb_data(data);
3584 return;
3585 }
3586
5c6b1176 3587 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, mount_export_3_cb, private_data) != 0) {
7f0242ca
RS
3588 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3589 free_mount_cb_data(data);
3590 return;
3591 }
3592}
3593
3594static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3595{
3596 struct mount_cb_data *data = private_data;
3597
f3a75078
RS
3598 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3599
14a062eb
RS
3600 /* Dont want any more callbacks even if the socket is closed */
3601 rpc->connect_cb = NULL;
3602
7f0242ca
RS
3603 if (status == RPC_STATUS_ERROR) {
3604 data->cb(rpc, -EFAULT, command_data, data->private_data);
3605 free_mount_cb_data(data);
3606 return;
3607 }
3608 if (status == RPC_STATUS_CANCEL) {
3609 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3610 free_mount_cb_data(data);
3611 return;
3612 }
3613
3614 if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) {
3615 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3616 free_mount_cb_data(data);
3617 return;
3618 }
3619}
3620
3621int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data)
3622{
3623 struct mount_cb_data *data;
3624
f3a75078
RS
3625 assert(rpc->magic == RPC_CONTEXT_MAGIC);
3626
7f0242ca
RS
3627 data = malloc(sizeof(struct mount_cb_data));
3628 if (data == NULL) {
3629 return -1;
3630 }
ea98629a 3631 memset(data, 0, sizeof(struct mount_cb_data));
7f0242ca
RS
3632 data->cb = cb;
3633 data->private_data = private_data;
3634 data->server = strdup(server);
3635 if (data->server == NULL) {
3636 free_mount_cb_data(data);
3637 return -1;
3638 }
3639 if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) {
3640 free_mount_cb_data(data);
3641 return -1;
3642 }
3643
3644 return 0;
3645}
3646
df5af25f
RS
3647struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs)
3648{
f3a75078 3649 assert(nfs->rpc->magic == RPC_CONTEXT_MAGIC);
df5af25f
RS
3650 return nfs->rpc;
3651}
3652
b077fdeb
RS
3653const char *nfs_get_server(struct nfs_context *nfs) {
3654 return nfs->server;
3655}
3656
3657const char *nfs_get_export(struct nfs_context *nfs) {
3658 return nfs->export;
3659}
0ad9a1f1 3660
8724c833
RS
3661const struct nfs_fh3 *nfs_get_rootfh(struct nfs_context *nfs) {
3662 return &nfs->rootfh;
3663}
fa3c25be
RS
3664
3665struct nfs_fh3 *nfs_get_fh(struct nfsfh *nfsfh) {
3666 return &nfsfh->fh;
3667}