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