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