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