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