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