rpc_nfs_create_async, args is now a pointer, not a structur
[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;
183451cf 54 uint64_t offset;
84004dbf
RS
55};
56
1896d37b
RS
57struct nfs_context {
58 struct rpc_context *rpc;
59 char *server;
60 char *export;
61 struct nfs_fh3 rootfh;
183451cf
RS
62 uint64_t readmax;
63 uint64_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;
183451cf 101 uint64_t start_offset, max_offset;
921f877b
RS
102 char *buffer;
103};
104
105struct nfs_mcb_data {
106 struct nfs_cb_data *data;
183451cf
RS
107 uint64_t offset;
108 uint64_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
5c6b1176 462 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, nfs_mount_3_cb, private_data) != 0) {
84004dbf
RS
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 }
d7ec001f
RS
689 if (res->GETATTR3res_u.resok.obj_attributes.type == NF3REG) {
690 st.st_mode |= S_IFREG ;
691 }
84004dbf
RS
692 st.st_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink;
693 st.st_uid = res->GETATTR3res_u.resok.obj_attributes.uid;
694 st.st_gid = res->GETATTR3res_u.resok.obj_attributes.gid;
695 st.st_rdev = 0;
696 st.st_size = res->GETATTR3res_u.resok.obj_attributes.size;
a8a1b858 697#ifndef WIN32
84004dbf
RS
698 st.st_blksize = 4096;
699 st.st_blocks = res->GETATTR3res_u.resok.obj_attributes.size / 4096;
a8a1b858 700#endif//WIN32
84004dbf
RS
701 st.st_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds;
702 st.st_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds;
703 st.st_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds;
704
705 data->cb(0, nfs, &st, data->private_data);
706 free_nfs_cb_data(data);
707}
708
709static int nfs_stat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
710{
711 if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &data->fh, data) != 0) {
712 rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path);
713 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
714 free_nfs_cb_data(data);
715 return -1;
716 }
717 return 0;
718}
719
720int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
721{
722 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_stat_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 723 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
724 return -1;
725 }
726
727 return 0;
728}
729
730
731
732
733
734/*
735 * Async open()
736 */
737static void nfs_open_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
738{
739 ACCESS3res *res;
740 struct nfs_cb_data *data = private_data;
741 struct nfs_context *nfs = data->nfs;
742 struct nfsfh *nfsfh;
743 unsigned int nfsmode = 0;
744
745 if (status == RPC_STATUS_ERROR) {
746 data->cb(-EFAULT, nfs, command_data, data->private_data);
747 free_nfs_cb_data(data);
748 return;
749 }
750 if (status == RPC_STATUS_CANCEL) {
751 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
752 free_nfs_cb_data(data);
753 return;
754 }
755
756 res = command_data;
757 if (res->status != NFS3_OK) {
758 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));
759 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
760 free_nfs_cb_data(data);
761 return;
762 }
763
764 if (data->continue_int & O_WRONLY) {
765 nfsmode |= ACCESS3_MODIFY;
766 }
767 if (data->continue_int & O_RDWR) {
768 nfsmode |= ACCESS3_READ|ACCESS3_MODIFY;
769 }
770 if (!(data->continue_int & (O_WRONLY|O_RDWR))) {
771 nfsmode |= ACCESS3_READ;
772 }
773
774
775 if (res->ACCESS3res_u.resok.access != nfsmode) {
776 rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
777 nfsmode&ACCESS3_READ?'r':'-',
778 nfsmode&ACCESS3_MODIFY?'w':'-',
779 nfsmode&ACCESS3_EXECUTE?'x':'-',
780 res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-',
781 res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-',
782 res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-');
783 data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data);
784 free_nfs_cb_data(data);
785 return;
786 }
787
788 nfsfh = malloc(sizeof(struct nfsfh));
789 if (nfsfh == NULL) {
790 rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure");
791 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
792 free_nfs_cb_data(data);
793 return;
794 }
ea98629a 795 memset(nfsfh, 0, sizeof(struct nfsfh));
84004dbf
RS
796
797 if (data->continue_int & O_SYNC) {
798 nfsfh->is_sync = 1;
799 }
800
801 /* steal the filehandle */
802 nfsfh->fh.data.data_len = data->fh.data.data_len;
803 nfsfh->fh.data.data_val = data->fh.data.data_val;
804 data->fh.data.data_val = NULL;
805
806 data->cb(0, nfs, nfsfh, data->private_data);
807 free_nfs_cb_data(data);
808}
809
810static int nfs_open_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
811{
812 int nfsmode = 0;
813
814 if (data->continue_int & O_WRONLY) {
815 nfsmode |= ACCESS3_MODIFY;
816 }
817 if (data->continue_int & O_RDWR) {
818 nfsmode |= ACCESS3_READ|ACCESS3_MODIFY;
819 }
820 if (!(data->continue_int & (O_WRONLY|O_RDWR))) {
821 nfsmode |= ACCESS3_READ;
822 }
823
824 if (rpc_nfs_access_async(nfs->rpc, nfs_open_cb, &data->fh, nfsmode, data) != 0) {
825 rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
826 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
827 free_nfs_cb_data(data);
828 return -1;
829 }
830 return 0;
831}
832
833int nfs_open_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
834{
835 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_open_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e
RS
836 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
837 return -1;
84004dbf
RS
838 }
839
840 return 0;
841}
842
843
844
845
846
847/*
848 * Async pread()
849 */
850static void nfs_pread_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
851{
852 struct nfs_cb_data *data = private_data;
853 struct nfs_context *nfs = data->nfs;
854 READ3res *res;
855
856 if (status == RPC_STATUS_ERROR) {
857 data->cb(-EFAULT, nfs, command_data, data->private_data);
858 free_nfs_cb_data(data);
859 return;
860 }
861 if (status == RPC_STATUS_CANCEL) {
862 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
863 free_nfs_cb_data(data);
864 return;
865 }
866
867 res = command_data;
868 if (res->status != NFS3_OK) {
869 rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
870 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
871 free_nfs_cb_data(data);
872 return;
873 }
874
875 data->nfsfh->offset += res->READ3res_u.resok.count;
876 data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_data);
877 free_nfs_cb_data(data);
878}
879
921f877b
RS
880static void nfs_pread_mcb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
881{
882 struct nfs_mcb_data *mdata = private_data;
883 struct nfs_cb_data *data = mdata->data;
884 struct nfs_context *nfs = data->nfs;
885 READ3res *res;
886
887 data->num_calls--;
888
889 if (status == RPC_STATUS_ERROR) {
890 /* flag the failure but do not invoke callback until we have received all responses */
891 data->error = 1;
892 }
893 if (status == RPC_STATUS_CANCEL) {
894 /* flag the cancellation but do not invoke callback until we have received all responses */
895 data->cancel = 1;
896 }
897
898 /* reassemble the data into the buffer */
899 if (status == RPC_STATUS_SUCCESS) {
900 res = command_data;
901 if (res->status != NFS3_OK) {
902 rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
903 data->error = 1;
e4a5ba42
RS
904 } else {
905 if (res->READ3res_u.resok.count > 0) {
906 memcpy(&data->buffer[mdata->offset - data->start_offset], res->READ3res_u.resok.data.data_val, res->READ3res_u.resok.count);
907 if ((unsigned)data->max_offset < mdata->offset + res->READ3res_u.resok.count) {
908 data->max_offset = mdata->offset + res->READ3res_u.resok.count;
909 }
921f877b
RS
910 }
911 }
912 }
913
914 if (data->num_calls > 0) {
915 /* still waiting for more replies */
916 free(mdata);
917 return;
918 }
919
921f877b
RS
920 if (data->error != 0) {
921 data->cb(-EFAULT, nfs, command_data, data->private_data);
922 free_nfs_cb_data(data);
923 free(mdata);
924 return;
925 }
926 if (data->cancel != 0) {
927 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
928 free_nfs_cb_data(data);
929 free(mdata);
930 return;
931 }
932
933 data->nfsfh->offset = data->max_offset;
7ed9d87a
RS
934 data->cb(data->max_offset - data->start_offset, nfs, data->buffer, data->private_data);
935
921f877b
RS
936 free_nfs_cb_data(data);
937 free(mdata);
938}
939
183451cf 940int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, nfs_cb cb, void *private_data)
84004dbf
RS
941{
942 struct nfs_cb_data *data;
943
944 data = malloc(sizeof(struct nfs_cb_data));
945 if (data == NULL) {
946 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
947 return -1;
948 }
ea98629a 949 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
950 data->nfs = nfs;
951 data->cb = cb;
952 data->private_data = private_data;
953 data->nfsfh = nfsfh;
954
955 nfsfh->offset = offset;
921f877b
RS
956
957 if (count <= nfs_get_readmax(nfs)) {
958 if (rpc_nfs_read_async(nfs->rpc, nfs_pread_cb, &nfsfh->fh, offset, count, data) != 0) {
959 rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
960 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
961 free_nfs_cb_data(data);
962 return -1;
963 }
964 return 0;
965 }
966
967 /* trying to read more than maximum server read size, we has to chop it up into smaller
968 * reads and collect into a reassembly buffer.
969 * we send all reads in parallell so that performance is still good.
970 */
7ed9d87a 971 data->max_offset = offset;
921f877b
RS
972 data->start_offset = offset;
973
974 data->buffer = malloc(count);
975 if (data->buffer == NULL) {
976 rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count);
84004dbf
RS
977 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
978 free_nfs_cb_data(data);
979 return -1;
980 }
921f877b
RS
981
982 while (count > 0) {
183451cf 983 uint64_t readcount = count;
921f877b
RS
984 struct nfs_mcb_data *mdata;
985
986 if (readcount > nfs_get_readmax(nfs)) {
987 readcount = nfs_get_readmax(nfs);
988 }
989
990 mdata = malloc(sizeof(struct nfs_mcb_data));
991 if (mdata == NULL) {
992 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure");
921f877b
RS
993 return -1;
994 }
ea98629a 995 memset(mdata, 0, sizeof(struct nfs_mcb_data));
921f877b
RS
996 mdata->data = data;
997 mdata->offset = offset;
998 mdata->count = readcount;
921f877b
RS
999 if (rpc_nfs_read_async(nfs->rpc, nfs_pread_mcb, &nfsfh->fh, offset, readcount, mdata) != 0) {
1000 rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
1001 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1002 free(mdata);
1003 return -1;
1004 }
1005
1006 count -= readcount;
1007 offset += readcount;
1008 data->num_calls++;
1009 }
1010
1011 return 0;
84004dbf
RS
1012}
1013
1014/*
1015 * Async read()
1016 */
183451cf 1017int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, nfs_cb cb, void *private_data)
84004dbf
RS
1018{
1019 return nfs_pread_async(nfs, nfsfh, nfsfh->offset, count, cb, private_data);
1020}
1021
1022
1023
1024/*
1025 * Async pwrite()
1026 */
1027static void nfs_pwrite_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1028{
1029 struct nfs_cb_data *data = private_data;
1030 struct nfs_context *nfs = data->nfs;
1031 WRITE3res *res;
1032
1033 if (status == RPC_STATUS_ERROR) {
1034 data->cb(-EFAULT, nfs, command_data, data->private_data);
1035 free_nfs_cb_data(data);
1036 return;
1037 }
1038 if (status == RPC_STATUS_CANCEL) {
1039 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1040 free_nfs_cb_data(data);
1041 return;
1042 }
1043
1044 res = command_data;
1045 if (res->status != NFS3_OK) {
1046 rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1047 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1048 free_nfs_cb_data(data);
1049 return;
1050 }
1051
1052 data->nfsfh->offset += res->WRITE3res_u.resok.count;
1053 data->cb(res->WRITE3res_u.resok.count, nfs, NULL, data->private_data);
1054 free_nfs_cb_data(data);
1055}
1056
18c27b73
RS
1057static void nfs_pwrite_mcb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1058{
1059 struct nfs_mcb_data *mdata = private_data;
1060 struct nfs_cb_data *data = mdata->data;
1061 struct nfs_context *nfs = data->nfs;
1062 WRITE3res *res;
1063
1064 data->num_calls--;
1065
1066 if (status == RPC_STATUS_ERROR) {
1067 /* flag the failure but do not invoke callback until we have received all responses */
1068 data->error = 1;
1069 }
1070 if (status == RPC_STATUS_CANCEL) {
1071 /* flag the cancellation but do not invoke callback until we have received all responses */
1072 data->cancel = 1;
1073 }
1074
1075 if (status == RPC_STATUS_SUCCESS) {
1076 res = command_data;
1077 if (res->status != NFS3_OK) {
1078 rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1079 data->error = 1;
1080 } else {
1081 if (res->WRITE3res_u.resok.count > 0) {
1082 if ((unsigned)data->max_offset < mdata->offset + res->WRITE3res_u.resok.count) {
1083 data->max_offset = mdata->offset + res->WRITE3res_u.resok.count;
1084 }
1085 }
1086 }
1087 }
1088
1089 if (data->num_calls > 0) {
1090 /* still waiting for more replies */
1091 free(mdata);
1092 return;
1093 }
1094
1095 if (data->error != 0) {
1096 data->cb(-EFAULT, nfs, command_data, data->private_data);
1097 free_nfs_cb_data(data);
1098 free(mdata);
1099 return;
1100 }
1101 if (data->cancel != 0) {
1102 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1103 free_nfs_cb_data(data);
1104 free(mdata);
1105 return;
1106 }
1107
1108 data->nfsfh->offset = data->max_offset;
1109 data->cb(data->max_offset - data->start_offset, nfs, NULL, data->private_data);
1110
1111 free_nfs_cb_data(data);
1112 free(mdata);
1113}
1114
1115
183451cf 1116int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, char *buf, nfs_cb cb, void *private_data)
84004dbf
RS
1117{
1118 struct nfs_cb_data *data;
1119
1120 data = malloc(sizeof(struct nfs_cb_data));
1121 if (data == NULL) {
1122 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1123 return -1;
1124 }
ea98629a 1125 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1126 data->nfs = nfs;
1127 data->cb = cb;
1128 data->private_data = private_data;
1129 data->nfsfh = nfsfh;
1130
1131 nfsfh->offset = offset;
18c27b73
RS
1132
1133 if (count <= nfs_get_writemax(nfs)) {
1134 if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_cb, &nfsfh->fh, buf, offset, count, nfsfh->is_sync?FILE_SYNC:UNSTABLE, data) != 0) {
1135 rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path);
1136 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1137 free_nfs_cb_data(data);
1138 return -1;
1139 }
1140 return 0;
84004dbf 1141 }
18c27b73
RS
1142
1143 /* trying to write more than maximum server write size, we has to chop it up into smaller
1144 * chunks.
1145 * we send all writes in parallell so that performance is still good.
1146 */
1147 data->max_offset = offset;
1148 data->start_offset = offset;
1149
1150 while (count > 0) {
183451cf 1151 uint64_t writecount = count;
18c27b73
RS
1152 struct nfs_mcb_data *mdata;
1153
1154 if (writecount > nfs_get_writemax(nfs)) {
1155 writecount = nfs_get_writemax(nfs);
1156 }
1157
1158 mdata = malloc(sizeof(struct nfs_mcb_data));
1159 if (mdata == NULL) {
1160 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure");
1161 return -1;
1162 }
1163 memset(mdata, 0, sizeof(struct nfs_mcb_data));
1164 mdata->data = data;
1165 mdata->offset = offset;
1166 mdata->count = writecount;
1167
1168 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) {
1169 rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path);
1170 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1171 free(mdata);
1172 return -1;
1173 }
1174
1175 count -= writecount;
1176 offset += writecount;
1177 data->num_calls++;
1178 }
1179
84004dbf
RS
1180 return 0;
1181}
1182
1183/*
1184 * Async write()
1185 */
183451cf 1186int nfs_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, char *buf, nfs_cb cb, void *private_data)
84004dbf
RS
1187{
1188 return nfs_pwrite_async(nfs, nfsfh, nfsfh->offset, count, buf, cb, private_data);
1189}
1190
1191
1192
1193
1194/*
1195 * close
1196 */
1197
1198int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1199{
1200 if (nfsfh->fh.data.data_val != NULL){
1201 free(nfsfh->fh.data.data_val);
1202 nfsfh->fh.data.data_val = NULL;
1203 }
1204 free(nfsfh);
1205
1206 cb(0, nfs, NULL, private_data);
1207 return 0;
1208};
1209
1210
1211
1212
1213
1214/*
1215 * Async fstat()
1216 */
1217int nfs_fstat_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1218{
1219 struct nfs_cb_data *data;
1220
1221 data = malloc(sizeof(struct nfs_cb_data));
1222 if (data == NULL) {
1223 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1224 return -1;
1225 }
ea98629a 1226 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1227 data->nfs = nfs;
1228 data->cb = cb;
1229 data->private_data = private_data;
1230
1231 if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &nfsfh->fh, data) != 0) {
1232 rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path);
1233 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1234 free_nfs_cb_data(data);
1235 return -1;
1236 }
1237 return 0;
1238}
1239
1240
1241
1242/*
1243 * Async fsync()
1244 */
1245static void nfs_fsync_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1246{
1247 struct nfs_cb_data *data = private_data;
1248 struct nfs_context *nfs = data->nfs;
1249 COMMIT3res *res;
1250
1251 if (status == RPC_STATUS_ERROR) {
1252 data->cb(-EFAULT, nfs, command_data, data->private_data);
1253 free_nfs_cb_data(data);
1254 return;
1255 }
1256 if (status == RPC_STATUS_CANCEL) {
1257 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1258 free_nfs_cb_data(data);
1259 return;
1260 }
1261
1262 res = command_data;
1263 if (res->status != NFS3_OK) {
1264 rpc_set_error(nfs->rpc, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1265 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1266 free_nfs_cb_data(data);
1267 return;
1268 }
1269
1270 data->cb(0, nfs, NULL, data->private_data);
1271 free_nfs_cb_data(data);
1272}
1273
1274int nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1275{
1276 struct nfs_cb_data *data;
1277
1278 data = malloc(sizeof(struct nfs_cb_data));
1279 if (data == NULL) {
1280 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1281 return -1;
1282 }
ea98629a 1283 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1284 data->nfs = nfs;
1285 data->cb = cb;
1286 data->private_data = private_data;
1287
1288 if (rpc_nfs_commit_async(nfs->rpc, nfs_fsync_cb, &nfsfh->fh, data) != 0) {
1289 rpc_set_error(nfs->rpc, "RPC error: Failed to send COMMIT call for %s", data->path);
1290 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1291 free_nfs_cb_data(data);
1292 return -1;
1293 }
1294 return 0;
1295}
1296
1297
1298
1299
1300/*
1301 * Async ftruncate()
1302 */
1303static void nfs_ftruncate_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1304{
1305 struct nfs_cb_data *data = private_data;
1306 struct nfs_context *nfs = data->nfs;
1307 SETATTR3res *res;
1308
1309 if (status == RPC_STATUS_ERROR) {
1310 data->cb(-EFAULT, nfs, command_data, data->private_data);
1311 free_nfs_cb_data(data);
1312 return;
1313 }
1314 if (status == RPC_STATUS_CANCEL) {
1315 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1316 free_nfs_cb_data(data);
1317 return;
1318 }
1319
1320 res = command_data;
1321 if (res->status != NFS3_OK) {
1322 rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1323 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1324 free_nfs_cb_data(data);
1325 return;
1326 }
1327
1328 data->cb(0, nfs, NULL, data->private_data);
1329 free_nfs_cb_data(data);
1330}
1331
183451cf 1332int nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t length, nfs_cb cb, void *private_data)
84004dbf
RS
1333{
1334 struct nfs_cb_data *data;
1335 SETATTR3args args;
1336
1337 data = malloc(sizeof(struct nfs_cb_data));
1338 if (data == NULL) {
1339 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1340 return -1;
1341 }
ea98629a 1342 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1343 data->nfs = nfs;
1344 data->cb = cb;
1345 data->private_data = private_data;
1346
ea98629a 1347 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
1348 args.object.data.data_len = nfsfh->fh.data.data_len;
1349 args.object.data.data_val = nfsfh->fh.data.data_val;
1350 args.new_attributes.size.set_it = 1;
1351 args.new_attributes.size.set_size3_u.size = length;
1352
1353 if (rpc_nfs_setattr_async(nfs->rpc, nfs_ftruncate_cb, &args, data) != 0) {
1354 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
1355 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1356 free_nfs_cb_data(data);
1357 return -1;
1358 }
1359 return 0;
1360}
1361
1362
1363/*
1364 * Async truncate()
1365 */
1366static int nfs_truncate_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1367{
183451cf 1368 uint64_t offset = data->continue_int;
84004dbf
RS
1369 struct nfsfh nfsfh;
1370
1371 nfsfh.fh.data.data_val = data->fh.data.data_val;
1372 nfsfh.fh.data.data_len = data->fh.data.data_len;
1373
1374 if (nfs_ftruncate_async(nfs, &nfsfh, offset, data->cb, data->private_data) != 0) {
1375 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
1376 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1377 free_nfs_cb_data(data);
1378 return -1;
1379 }
1380 free_nfs_cb_data(data);
1381 return 0;
1382}
1383
183451cf 1384int nfs_truncate_async(struct nfs_context *nfs, const char *path, uint64_t length, nfs_cb cb, void *private_data)
84004dbf 1385{
183451cf 1386 uint64_t offset;
84004dbf
RS
1387
1388 offset = length;
1389
1390 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_truncate_continue_internal, NULL, NULL, offset) != 0) {
cbbf9d3e
RS
1391 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1392 return -1;
84004dbf
RS
1393 }
1394
1395 return 0;
1396}
1397
1398
1399
1400
1401/*
1402 * Async mkdir()
1403 */
1404static void nfs_mkdir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1405{
1406 MKDIR3res *res;
1407 struct nfs_cb_data *data = private_data;
1408 struct nfs_context *nfs = data->nfs;
1409 char *str = data->continue_data;
1410
1411 str = &str[strlen(str) + 1];
1412
1413 if (status == RPC_STATUS_ERROR) {
1414 data->cb(-EFAULT, nfs, command_data, data->private_data);
1415 free_nfs_cb_data(data);
1416 return;
1417 }
1418 if (status == RPC_STATUS_CANCEL) {
1419 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1420 free_nfs_cb_data(data);
1421 return;
1422 }
1423
1424 res = command_data;
1425 if (res->status != NFS3_OK) {
1426 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));
1427 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1428 free_nfs_cb_data(data);
1429 return;
1430 }
1431
1432 data->cb(0, nfs, NULL, data->private_data);
1433 free_nfs_cb_data(data);
1434}
1435
1436static int nfs_mkdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1437{
1438 char *str = data->continue_data;
1439
1440 str = &str[strlen(str) + 1];
1441
1442 if (rpc_nfs_mkdir_async(nfs->rpc, nfs_mkdir_cb, &data->fh, str, data) != 0) {
1443 rpc_set_error(nfs->rpc, "RPC error: Failed to send MKDIR call for %s", data->path);
1444 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1445 free_nfs_cb_data(data);
1446 return -1;
1447 }
1448 return 0;
1449}
1450
1451int nfs_mkdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1452{
1453 char *new_path;
1454 char *ptr;
1455
1456 new_path = strdup(path);
1457 if (new_path == NULL) {
cbbf9d3e 1458 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1459 return -1;
1460 }
1461
93e9306f 1462 ptr = strrchr(new_path, '/');
84004dbf 1463 if (ptr == NULL) {
cbbf9d3e
RS
1464 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1465 return -1;
84004dbf
RS
1466 }
1467 *ptr = 0;
1468
1469 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1470 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_mkdir_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1471 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path component");
1472 return -1;
84004dbf
RS
1473 }
1474
1475 return 0;
1476}
1477
1478
1479
1480
1481
1482/*
1483 * Async rmdir()
1484 */
1485static void nfs_rmdir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1486{
1487 RMDIR3res *res;
1488 struct nfs_cb_data *data = private_data;
1489 struct nfs_context *nfs = data->nfs;
1490 char *str = data->continue_data;
1491
1492 str = &str[strlen(str) + 1];
1493
1494 if (status == RPC_STATUS_ERROR) {
1495 data->cb(-EFAULT, nfs, command_data, data->private_data);
1496 free_nfs_cb_data(data);
1497 return;
1498 }
1499 if (status == RPC_STATUS_CANCEL) {
1500 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1501 free_nfs_cb_data(data);
1502 return;
1503 }
1504
1505 res = command_data;
1506 if (res->status != NFS3_OK) {
1507 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));
1508 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1509 free_nfs_cb_data(data);
1510 return;
1511 }
1512
1513 data->cb(0, nfs, NULL, data->private_data);
1514 free_nfs_cb_data(data);
1515}
1516
1517static int nfs_rmdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1518{
1519 char *str = data->continue_data;
1520
1521 str = &str[strlen(str) + 1];
1522
1523 if (rpc_nfs_rmdir_async(nfs->rpc, nfs_rmdir_cb, &data->fh, str, data) != 0) {
1524 rpc_set_error(nfs->rpc, "RPC error: Failed to send RMDIR call for %s", data->path);
1525 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1526 free_nfs_cb_data(data);
1527 return -1;
1528 }
1529 return 0;
1530}
1531
1532int nfs_rmdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1533{
1534 char *new_path;
1535 char *ptr;
1536
1537 new_path = strdup(path);
1538 if (new_path == NULL) {
cbbf9d3e 1539 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1540 return -1;
1541 }
1542
93e9306f 1543 ptr = strrchr(new_path, '/');
84004dbf 1544 if (ptr == NULL) {
cbbf9d3e
RS
1545 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1546 return -1;
84004dbf
RS
1547 }
1548 *ptr = 0;
1549
1550 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1551 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_rmdir_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1552 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1553 return -1;
84004dbf
RS
1554 }
1555
1556 return 0;
1557}
1558
1559
1560
1561
1562/*
1563 * Async creat()
1564 */
1565static void nfs_create_2_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1566{
1567 LOOKUP3res *res;
1568 struct nfs_cb_data *data = private_data;
1569 struct nfs_context *nfs = data->nfs;
1570 struct nfsfh *nfsfh;
1571 char *str = data->continue_data;
1572
1573 if (status == RPC_STATUS_ERROR) {
1574 data->cb(-EFAULT, nfs, command_data, data->private_data);
1575 free_nfs_cb_data(data);
1576 return;
1577 }
1578 if (status == RPC_STATUS_CANCEL) {
1579 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1580 free_nfs_cb_data(data);
1581 return;
1582 }
1583
1584 str = &str[strlen(str) + 1];
1585 res = command_data;
1586 if (res->status != NFS3_OK) {
1587 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));
1588 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1589
1590 return;
1591 }
1592
1593 nfsfh = malloc(sizeof(struct nfsfh));
1594 if (nfsfh == NULL) {
1595 rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure");
1596 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1597 free_nfs_cb_data(data);
1598 return;
1599 }
ea98629a 1600 memset(nfsfh, 0, sizeof(struct nfsfh));
84004dbf
RS
1601
1602 /* steal the filehandle */
1603 nfsfh->fh.data.data_len = data->fh.data.data_len;
1604 nfsfh->fh.data.data_val = data->fh.data.data_val;
1605 data->fh.data.data_val = NULL;
1606
1607 data->cb(0, nfs, nfsfh, data->private_data);
1608 free_nfs_cb_data(data);
1609}
1610
1611
1612
1613static void nfs_creat_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1614{
1615 CREATE3res *res;
1616 struct nfs_cb_data *data = private_data;
1617 struct nfs_context *nfs = data->nfs;
1618 char *str = data->continue_data;
1619
1620 if (status == RPC_STATUS_ERROR) {
1621 data->cb(-EFAULT, nfs, command_data, data->private_data);
1622 free_nfs_cb_data(data);
1623 return;
1624 }
1625 if (status == RPC_STATUS_CANCEL) {
1626 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1627 free_nfs_cb_data(data);
1628 return;
1629 }
1630
1631 str = &str[strlen(str) + 1];
1632 res = command_data;
1633 if (res->status != NFS3_OK) {
1634 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));
1635 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1636
1637 return;
1638 }
1639
1640 if (rpc_nfs_lookup_async(nfs->rpc, nfs_create_2_cb, &data->fh, str, data) != 0) {
1641 rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s/%s", data->saved_path, str);
1642 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1643 free_nfs_cb_data(data);
1644 return;
1645 }
1646 return;
1647}
1648
1649static int nfs_creat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1650{
1651 char *str = data->continue_data;
c985c015
RS
1652 CREATE3args args;
1653
84004dbf
RS
1654 str = &str[strlen(str) + 1];
1655
c985c015
RS
1656
1657 memset(&args, 0, sizeof(CREATE3args));
1658 args.where.dir.data.data_len = data->fh.data.data_len;
1659 args.where.dir.data.data_val = data->fh.data.data_val;
1660 args.where.name = str;
1661 args.how.mode = UNCHECKED;
1662 args.how.createhow3_u.obj_attributes.mode.set_it = 1;
1663 args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = data->continue_int;
1664
1665 if (rpc_nfs_create_async(nfs->rpc, nfs_creat_1_cb, &args, data) != 0) {
84004dbf
RS
1666 rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str);
1667 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1668 free_nfs_cb_data(data);
1669 return -1;
1670 }
1671 return 0;
1672}
1673
1674int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
1675{
1676 char *new_path;
1677 char *ptr;
1678
1679 new_path = strdup(path);
1680 if (new_path == NULL) {
cbbf9d3e 1681 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1682 return -1;
1683 }
1684
93e9306f 1685 ptr = strrchr(new_path, '/');
84004dbf 1686 if (ptr == NULL) {
cbbf9d3e
RS
1687 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1688 return -1;
84004dbf
RS
1689 }
1690 *ptr = 0;
1691
1692 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1693 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_creat_continue_internal, new_path, free, mode) != 0) {
cbbf9d3e
RS
1694 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1695 return -1;
84004dbf
RS
1696 }
1697
1698 return 0;
1699}
1700
1701
1702
1703
1704/*
1705 * Async unlink()
1706 */
1707static void nfs_unlink_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1708{
1709 REMOVE3res *res;
1710 struct nfs_cb_data *data = private_data;
1711 struct nfs_context *nfs = data->nfs;
1712 char *str = data->continue_data;
1713
1714 str = &str[strlen(str) + 1];
1715
1716 if (status == RPC_STATUS_ERROR) {
1717 data->cb(-EFAULT, nfs, command_data, data->private_data);
1718 free_nfs_cb_data(data);
1719 return;
1720 }
1721 if (status == RPC_STATUS_CANCEL) {
1722 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1723 free_nfs_cb_data(data);
1724 return;
1725 }
1726
1727 res = command_data;
1728 if (res->status != NFS3_OK) {
1729 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));
1730 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1731 free_nfs_cb_data(data);
1732 return;
1733 }
1734
1735 data->cb(0, nfs, NULL, data->private_data);
1736 free_nfs_cb_data(data);
1737}
1738
1739static int nfs_unlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1740{
1741 char *str = data->continue_data;
1742
1743 str = &str[strlen(str) + 1];
1744
1745 if (rpc_nfs_remove_async(nfs->rpc, nfs_unlink_cb, &data->fh, str, data) != 0) {
1746 rpc_set_error(nfs->rpc, "RPC error: Failed to send REMOVE call for %s", data->path);
1747 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1748 free_nfs_cb_data(data);
1749 return -1;
1750 }
1751 return 0;
1752}
1753
1754int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1755{
1756 char *new_path;
1757 char *ptr;
1758
1759 new_path = strdup(path);
1760 if (new_path == NULL) {
cbbf9d3e 1761 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1762 return -1;
1763 }
1764
93e9306f 1765 ptr = strrchr(new_path, '/');
84004dbf 1766 if (ptr == NULL) {
cbbf9d3e
RS
1767 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1768 return -1;
84004dbf
RS
1769 }
1770 *ptr = 0;
1771
1772 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1773 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_unlink_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1774 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1775 return -1;
84004dbf
RS
1776 }
1777
1778 return 0;
1779}
1780
1781
1ec6b50a
RS
1782/*
1783 * Async mknod()
1784 */
1785struct mknod_cb_data {
1786 char *path;
1787 int mode;
1788 int major;
1789 int minor;
1790};
1791
1792static void free_mknod_cb_data(void *ptr)
1793{
1794 struct mknod_cb_data *data = ptr;
1795
1796 free(data->path);
1797 free(data);
1798}
1799
1800static void nfs_mknod_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1801{
1802 MKNOD3res *res;
1803 struct nfs_cb_data *data = private_data;
1804 struct nfs_context *nfs = data->nfs;
1805 char *str = data->continue_data;
1806
1807 str = &str[strlen(str) + 1];
1808
1809 if (status == RPC_STATUS_ERROR) {
1810 data->cb(-EFAULT, nfs, command_data, data->private_data);
1811 free_nfs_cb_data(data);
1812 return;
1813 }
1814 if (status == RPC_STATUS_CANCEL) {
1815 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1816 free_nfs_cb_data(data);
1817 return;
1818 }
1819
1820 res = command_data;
1821 if (res->status != NFS3_OK) {
1822 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));
1823 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1824 free_nfs_cb_data(data);
1825 return;
1826 }
1827
1828 data->cb(0, nfs, NULL, data->private_data);
1829 free_nfs_cb_data(data);
1830}
1831
1832static int nfs_mknod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1833{
1834 struct mknod_cb_data *cb_data = data->continue_data;
1835 char *str = cb_data->path;
1836
1837 str = &str[strlen(str) + 1];
1838
1839 if (rpc_nfs_mknod_async(nfs->rpc, nfs_mknod_cb, &data->fh, str, cb_data->mode, cb_data->major, cb_data->minor, data) != 0) {
1840 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1841 free_nfs_cb_data(data);
1842 return -1;
1843 }
1844 return 0;
1845}
1846
1847int nfs_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev, nfs_cb cb, void *private_data)
1848{
1849 char *ptr;
1850 struct mknod_cb_data *cb_data;
1851
1852 cb_data = malloc(sizeof(struct mknod_cb_data));
1853 if (cb_data == NULL) {
1854 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for cb data");
1855 return -1;
1856 }
1857
1858 cb_data->path = strdup(path);
1859 if (cb_data->path == NULL) {
1860 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
1861 free(cb_data);
1862 return -1;
1863 }
1864
1865 ptr = strrchr(cb_data->path, '/');
1866 if (ptr == NULL) {
1867 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1868 return -1;
1869 }
1870 *ptr = 0;
1871
1872 cb_data->mode = mode;
1873 cb_data->major = major(dev);
1874 cb_data->minor = minor(dev);
1875
1876 /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1877 if (nfs_lookuppath_async(nfs, cb_data->path, cb, private_data, nfs_mknod_continue_internal, cb_data, free_mknod_cb_data, 0) != 0) {
1878 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1879 free_mknod_cb_data(cb_data);
1880 return -1;
1881 }
1882
1883 return 0;
1884}
84004dbf 1885
84004dbf
RS
1886/*
1887 * Async opendir()
1888 */
fb69c64c
RS
1889
1890/* ReadDirPlus Emulation Callback data */
1891struct rdpe_cb_data {
1892 int getattrcount;
1893 int status;
1894 struct nfs_cb_data *data;
1895};
1896
1897/* ReadDirPlus Emulation LOOKUP Callback data */
1898struct rdpe_lookup_cb_data {
1899 struct rdpe_cb_data *rdpe_cb_data;
1900 struct nfsdirent *nfsdirent;
1901};
1902
1903/* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
1904static void nfs_opendir3_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
84004dbf 1905{
fb69c64c
RS
1906 LOOKUP3res *res = command_data;
1907 struct rdpe_lookup_cb_data *rdpe_lookup_cb_data = private_data;
1908 struct rdpe_cb_data *rdpe_cb_data = rdpe_lookup_cb_data->rdpe_cb_data;
1909 struct nfs_cb_data *data = rdpe_cb_data->data;
1910 struct nfsdir *nfsdir = data->continue_data;
1911 struct nfs_context *nfs = data->nfs;
1912 struct nfsdirent *nfsdirent = rdpe_lookup_cb_data->nfsdirent;
1913
1914 free(rdpe_lookup_cb_data);
1915
1916 rdpe_cb_data->getattrcount--;
1917
1918 if (status == RPC_STATUS_ERROR) {
1919 rdpe_cb_data->status = RPC_STATUS_ERROR;
1920 }
1921 if (status == RPC_STATUS_CANCEL) {
1922 rdpe_cb_data->status = RPC_STATUS_CANCEL;
1923 }
1924 if (status == RPC_STATUS_SUCCESS && res->status != NFS3_OK) {
1925 rdpe_cb_data->status = RPC_STATUS_ERROR;
1926 }
1927 if (status == RPC_STATUS_SUCCESS && res->status == NFS3_OK) {
1928 if (res->LOOKUP3res_u.resok.obj_attributes.attributes_follow) {
1929 fattr3 *attributes = &res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes;
1930
1931 nfsdirent->type = attributes->type;
1932 nfsdirent->mode = attributes->mode;
1933 nfsdirent->size = attributes->size;
1934
1935 nfsdirent->atime.tv_sec = attributes->atime.seconds;
1936 nfsdirent->atime.tv_usec = attributes->atime.nseconds/1000;
1937 nfsdirent->mtime.tv_sec = attributes->mtime.seconds;
1938 nfsdirent->mtime.tv_usec = attributes->mtime.nseconds/1000;
1939 nfsdirent->ctime.tv_sec = attributes->ctime.seconds;
1940 nfsdirent->ctime.tv_usec = attributes->ctime.nseconds/1000;
1941 }
1942 }
1943
1944 if (rdpe_cb_data->getattrcount == 0) {
1945 if (rdpe_cb_data->status != RPC_STATUS_SUCCESS) {
1946 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1947 nfs_free_nfsdir(nfsdir);
1948 } else {
1949 data->cb(0, nfs, nfsdir, data->private_data);
1950 }
1951 free(rdpe_cb_data);
1952
1953 data->continue_data = NULL;
1954 free_nfs_cb_data(data);
1955 }
1956}
1957
1958static void nfs_opendir2_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1959{
1960 READDIR3res *res = command_data;
84004dbf
RS
1961 struct nfs_cb_data *data = private_data;
1962 struct nfs_context *nfs = data->nfs;
fb6510bb 1963 struct nfsdir *nfsdir = data->continue_data;
fb69c64c
RS
1964 struct nfsdirent *nfsdirent;
1965 struct entry3 *entry;
84004dbf 1966 uint64_t cookie;
fb69c64c
RS
1967 struct rdpe_cb_data *rdpe_cb_data;
1968
84004dbf
RS
1969 if (status == RPC_STATUS_ERROR) {
1970 data->cb(-EFAULT, nfs, command_data, data->private_data);
1971 nfs_free_nfsdir(nfsdir);
1972 data->continue_data = NULL;
1973 free_nfs_cb_data(data);
1974 return;
1975 }
fb69c64c 1976
84004dbf
RS
1977 if (status == RPC_STATUS_CANCEL) {
1978 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1979 nfs_free_nfsdir(nfsdir);
1980 data->continue_data = NULL;
1981 free_nfs_cb_data(data);
1982 return;
1983 }
1984
84004dbf
RS
1985 if (res->status != NFS3_OK) {
1986 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));
1987 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1988 nfs_free_nfsdir(nfsdir);
1989 data->continue_data = NULL;
1990 free_nfs_cb_data(data);
1991 return;
fb69c64c
RS
1992 }
1993
1994 entry =res->READDIR3res_u.resok.reply.entries;
1995 while (entry != NULL) {
1996 nfsdirent = malloc(sizeof(struct nfsdirent));
1997 if (nfsdirent == NULL) {
1998 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
1999 nfs_free_nfsdir(nfsdir);
2000 data->continue_data = NULL;
2001 free_nfs_cb_data(data);
2002 return;
2003 }
2004 memset(nfsdirent, 0, sizeof(struct nfsdirent));
2005 nfsdirent->name = strdup(entry->name);
2006 if (nfsdirent->name == NULL) {
2007 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
2008 nfs_free_nfsdir(nfsdir);
2009 data->continue_data = NULL;
2010 free_nfs_cb_data(data);
2011 return;
2012 }
2013 nfsdirent->inode = entry->fileid;
2014
2015 nfsdirent->next = nfsdir->entries;
2016 nfsdir->entries = nfsdirent;
2017
2018 cookie = entry->cookie;
2019 entry = entry->nextentry;
2020 }
2021
2022 if (res->READDIR3res_u.resok.reply.eof == 0) {
2023 if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, cookie, res->READDIR3res_u.resok.cookieverf, 8192, data) != 0) {
2024 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
2025 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2026 nfs_free_nfsdir(nfsdir);
2027 data->continue_data = NULL;
2028 free_nfs_cb_data(data);
2029 return;
2030 }
2031 return;
2032 }
2033
2034 /* steal the dirhandle */
2035 nfsdir->current = nfsdir->entries;
2036
2037 rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data));
2038 rdpe_cb_data->getattrcount = 0;
2039 rdpe_cb_data->status = RPC_STATUS_SUCCESS;
2040 rdpe_cb_data->data = data;
2041 for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) {
2042 struct rdpe_lookup_cb_data *rdpe_lookup_cb_data;
2043
2044 rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data));
2045 rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data;
2046 rdpe_lookup_cb_data->nfsdirent = nfsdirent;
2047
2048 if (rpc_nfs_lookup_async(nfs->rpc, nfs_opendir3_cb, &data->fh, nfsdirent->name, rdpe_lookup_cb_data) != 0) {
2049 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call");
2050
2051 /* if we have already commands in flight, we cant just stop, we have to wait for the
2052 * commands in flight to complete
2053 */
2054 if (rdpe_cb_data->getattrcount > 0) {
2055 rdpe_cb_data->status = RPC_STATUS_ERROR;
2056 free(rdpe_lookup_cb_data);
2057 return;
2058 }
2059
2060 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2061 nfs_free_nfsdir(nfsdir);
2062 data->continue_data = NULL;
2063 free_nfs_cb_data(data);
2064 free(rdpe_lookup_cb_data);
2065 free(rdpe_cb_data);
2066 return;
2067 }
2068 rdpe_cb_data->getattrcount++;
2069 }
2070}
2071
2072
2073static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2074{
2075 READDIRPLUS3res *res = command_data;
2076 struct nfs_cb_data *data = private_data;
2077 struct nfs_context *nfs = data->nfs;
2078 struct nfsdir *nfsdir = data->continue_data;
2079 struct entryplus3 *entry;
2080 uint64_t cookie;
2081
2082
2083 if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){
2084 cookieverf3 cv;
2085
2086 if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) {
2087 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
2088 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2089 nfs_free_nfsdir(nfsdir);
2090 data->continue_data = NULL;
2091 free_nfs_cb_data(data);
2092 return;
2093 }
2094 return;
2095 }
2096
2097 if (status == RPC_STATUS_CANCEL) {
2098 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2099 nfs_free_nfsdir(nfsdir);
2100 data->continue_data = NULL;
2101 free_nfs_cb_data(data);
2102 return;
2103 }
2104
2105 if (res->status != NFS3_OK) {
2106 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));
2107 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2108 nfs_free_nfsdir(nfsdir);
2109 data->continue_data = NULL;
2110 free_nfs_cb_data(data);
2111 return;
84004dbf
RS
2112 }
2113
f390f181 2114 entry =res->READDIRPLUS3res_u.resok.reply.entries;
84004dbf
RS
2115 while (entry != NULL) {
2116 struct nfsdirent *nfsdirent;
2117
2118 nfsdirent = malloc(sizeof(struct nfsdirent));
2119 if (nfsdirent == NULL) {
2120 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
2121 nfs_free_nfsdir(nfsdir);
2122 data->continue_data = NULL;
2123 free_nfs_cb_data(data);
2124 return;
2125 }
ea98629a 2126 memset(nfsdirent, 0, sizeof(struct nfsdirent));
84004dbf
RS
2127 nfsdirent->name = strdup(entry->name);
2128 if (nfsdirent->name == NULL) {
2129 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
2130 nfs_free_nfsdir(nfsdir);
2131 data->continue_data = NULL;
2132 free_nfs_cb_data(data);
2133 return;
2134 }
2135 nfsdirent->inode = entry->fileid;
0804e67d
RS
2136 if (entry->name_attributes.attributes_follow) {
2137 nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type;
2138 nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode;
2139 nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size;
2140
2141 nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds;
2142 nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000;
2143 nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds;
2144 nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000;
2145 nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds;
2146 nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000;
2147 }
2148
84004dbf
RS
2149 nfsdirent->next = nfsdir->entries;
2150 nfsdir->entries = nfsdirent;
2151
2152 cookie = entry->cookie;
2153 entry = entry->nextentry;
2154 }
2155
f390f181
RS
2156 if (res->READDIRPLUS3res_u.resok.reply.eof == 0) {
2157 if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, cookie, res->READDIRPLUS3res_u.resok.cookieverf, 8192, data) != 0) {
2158 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
2159 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2160 nfs_free_nfsdir(nfsdir);
2161 data->continue_data = NULL;
2162 free_nfs_cb_data(data);
2163 return;
2164 }
2165 return;
2166 }
2167
2168 /* steal the dirhandle */
2169 data->continue_data = NULL;
2170 nfsdir->current = nfsdir->entries;
2171
2172 data->cb(0, nfs, nfsdir, data->private_data);
2173 free_nfs_cb_data(data);
2174}
2175
2176static int nfs_opendir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2177{
2178 cookieverf3 cv;
2179
ea98629a 2180 memset(cv, 0, sizeof(cookieverf3));
f390f181
RS
2181 if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) {
2182 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
2183 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2184 free_nfs_cb_data(data);
2185 return -1;
2186 }
2187 return 0;
2188}
2189
2190int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2191{
2192 struct nfsdir *nfsdir;
2193
2194 nfsdir = malloc(sizeof(struct nfsdir));
2195 if (nfsdir == NULL) {
cbbf9d3e 2196 rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir");
84004dbf
RS
2197 return -1;
2198 }
ea98629a 2199 memset(nfsdir, 0, sizeof(struct nfsdir));
84004dbf
RS
2200
2201 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) {
cbbf9d3e
RS
2202 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2203 return -1;
84004dbf
RS
2204 }
2205
2206 return 0;
2207}
2208
2209
2210struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
2211{
2212 struct nfsdirent *nfsdirent = nfsdir->current;
2213
2214 if (nfsdir->current != NULL) {
2215 nfsdir->current = nfsdir->current->next;
2216 }
2217 return nfsdirent;
2218}
2219
2220
2221void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
2222{
2223 nfs_free_nfsdir(nfsdir);
2224}
2225
2226
2227
2228
2229
2230
2231
2232/*
2233 * Async lseek()
2234 */
2235struct lseek_cb_data {
2236 struct nfs_context *nfs;
2237 struct nfsfh *nfsfh;
183451cf 2238 uint64_t offset;
84004dbf
RS
2239 nfs_cb cb;
2240 void *private_data;
2241};
2242
2243static void nfs_lseek_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2244{
2245 GETATTR3res *res;
2246 struct lseek_cb_data *data = private_data;
2247 struct nfs_context *nfs = data->nfs;
2248
2249 if (status == RPC_STATUS_ERROR) {
2250 data->cb(-EFAULT, nfs, command_data, data->private_data);
2251 free(data);
2252 return;
2253 }
2254 if (status == RPC_STATUS_CANCEL) {
2255 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2256 free(data);
2257 return;
2258 }
2259
2260 res = command_data;
2261 if (res->status != NFS3_OK) {
2262 rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2263 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2264 free(data);
2265 return;
2266 }
2267
2268 data->nfsfh->offset = data->offset + res->GETATTR3res_u.resok.obj_attributes.size;
2269 data->cb(0, nfs, &data->nfsfh->offset, data->private_data);
2270 free(data);
2271}
2272
183451cf 2273int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, int whence, nfs_cb cb, void *private_data)
84004dbf
RS
2274{
2275 struct lseek_cb_data *data;
2276
2277 if (whence == SEEK_SET) {
2278 nfsfh->offset = offset;
2279 cb(0, nfs, &nfsfh->offset, private_data);
2280 return 0;
2281 }
2282 if (whence == SEEK_CUR) {
2283 nfsfh->offset += offset;
2284 cb(0, nfs, &nfsfh->offset, private_data);
2285 return 0;
2286 }
2287
2288 data = malloc(sizeof(struct lseek_cb_data));
2289 if (data == NULL) {
2290 rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data");
2291 return -1;
2292 }
2293
2294 data->nfs = nfs;
2295 data->nfsfh = nfsfh;
2296 data->offset = offset;
2297 data->cb = cb;
2298 data->private_data = private_data;
2299
2300 if (rpc_nfs_getattr_async(nfs->rpc, nfs_lseek_1_cb, &nfsfh->fh, data) != 0) {
2301 rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call");
2302 free(data);
cbbf9d3e 2303 return -1;
84004dbf
RS
2304 }
2305 return 0;
2306}
2307
2308
2309
2310
2311/*
2312 * Async statvfs()
2313 */
2314static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2315{
2316 FSSTAT3res *res;
2317 struct nfs_cb_data *data = private_data;
2318 struct nfs_context *nfs = data->nfs;
2319 struct statvfs svfs;
2320
2321 if (status == RPC_STATUS_ERROR) {
2322 data->cb(-EFAULT, nfs, command_data, data->private_data);
2323 free_nfs_cb_data(data);
2324 return;
2325 }
2326 if (status == RPC_STATUS_CANCEL) {
2327 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2328 free_nfs_cb_data(data);
2329 return;
2330 }
2331
2332 res = command_data;
2333 if (res->status != NFS3_OK) {
2334 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));
2335 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2336 free_nfs_cb_data(data);
2337 return;
2338 }
2339
2340 svfs.f_bsize = 4096;
2341 svfs.f_frsize = 4096;
2342 svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/4096;
2343 svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/4096;
2344 svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/4096;
2345 svfs.f_files = res->FSSTAT3res_u.resok.tfiles;
2346 svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles;
2347 svfs.f_favail = res->FSSTAT3res_u.resok.afiles;
2348 svfs.f_fsid = 0;
2349 svfs.f_flag = 0;
2350 svfs.f_namemax = 256;
2351
2352 data->cb(0, nfs, &svfs, data->private_data);
2353 free_nfs_cb_data(data);
2354}
2355
2356static int nfs_statvfs_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2357{
2358 if (rpc_nfs_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &data->fh, data) != 0) {
2359 rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT call for %s", data->path);
2360 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2361 free_nfs_cb_data(data);
2362 return -1;
2363 }
2364 return 0;
2365}
2366
2367int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2368{
2369 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2370 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2371 return -1;
2372 }
2373
2374 return 0;
2375}
2376
2377
2378
2379
2380/*
2381 * Async readlink()
2382 */
2383static void nfs_readlink_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2384{
2385 READLINK3res *res;
2386 struct nfs_cb_data *data = private_data;
2387 struct nfs_context *nfs = data->nfs;
2388
2389 if (status == RPC_STATUS_ERROR) {
2390 data->cb(-EFAULT, nfs, command_data, data->private_data);
2391 free_nfs_cb_data(data);
2392 return;
2393 }
2394 if (status == RPC_STATUS_CANCEL) {
2395 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2396 free_nfs_cb_data(data);
2397 return;
2398 }
2399
2400 res = command_data;
2401 if (res->status != NFS3_OK) {
2402 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));
2403 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2404 free_nfs_cb_data(data);
2405 return;
2406 }
2407
2408
2409 data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data);
2410 free_nfs_cb_data(data);
2411}
2412
2413static int nfs_readlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2414{
2415 if (rpc_nfs_readlink_async(nfs->rpc, nfs_readlink_1_cb, &data->fh, data) != 0) {
2416 rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path);
2417 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2418 free_nfs_cb_data(data);
2419 return -1;
2420 }
2421 return 0;
2422}
2423
2424int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2425{
2426 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2427 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2428 return -1;
2429 }
2430
2431 return 0;
2432}
2433
2434
2435
2436
2437/*
2438 * Async chmod()
2439 */
2440static void nfs_chmod_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2441{
2442 struct nfs_cb_data *data = private_data;
2443 struct nfs_context *nfs = data->nfs;
2444 SETATTR3res *res;
2445
2446 if (status == RPC_STATUS_ERROR) {
2447 data->cb(-EFAULT, nfs, command_data, data->private_data);
2448 free_nfs_cb_data(data);
2449 return;
2450 }
2451 if (status == RPC_STATUS_CANCEL) {
2452 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2453 free_nfs_cb_data(data);
2454 return;
2455 }
2456
2457 res = command_data;
2458 if (res->status != NFS3_OK) {
2459 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2460 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2461 free_nfs_cb_data(data);
2462 return;
2463 }
2464
2465 data->cb(0, nfs, NULL, data->private_data);
2466 free_nfs_cb_data(data);
2467}
2468
2469static int nfs_chmod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2470{
2471 SETATTR3args args;
2472
ea98629a 2473 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2474 args.object.data.data_len = data->fh.data.data_len;
2475 args.object.data.data_val = data->fh.data.data_val;
2476 args.new_attributes.mode.set_it = 1;
2477 args.new_attributes.mode.set_mode3_u.mode = data->continue_int;
2478
2479 if (rpc_nfs_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) {
2480 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2481 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2482 free_nfs_cb_data(data);
2483 return -1;
2484 }
2485 return 0;
2486}
2487
2488
2489int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
2490{
2491 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e 2492 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2493 return -1;
2494 }
2495
2496 return 0;
2497}
2498
2499/*
2500 * Async fchmod()
2501 */
2502int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data)
2503{
2504 struct nfs_cb_data *data;
2505
2506 data = malloc(sizeof(struct nfs_cb_data));
2507 if (data == NULL) {
cbbf9d3e 2508 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data");
84004dbf
RS
2509 return -1;
2510 }
ea98629a 2511 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
2512 data->nfs = nfs;
2513 data->cb = cb;
2514 data->private_data = private_data;
2515 data->continue_int = mode;
2516 data->fh.data.data_len = nfsfh->fh.data.data_len;
2517 data->fh.data.data_val = malloc(data->fh.data.data_len);
2518 if (data->fh.data.data_val == NULL) {
2519 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
2520 free_nfs_cb_data(data);
2521 return -1;
2522 }
2523 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
2524
2525 if (nfs_chmod_continue_internal(nfs, data) != 0) {
2526 free_nfs_cb_data(data);
2527 return -1;
2528 }
2529
2530 return 0;
2531}
2532
2533
2534
2535/*
2536 * Async chown()
2537 */
2538static void nfs_chown_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2539{
2540 struct nfs_cb_data *data = private_data;
2541 struct nfs_context *nfs = data->nfs;
2542 SETATTR3res *res;
2543
2544 if (status == RPC_STATUS_ERROR) {
2545 data->cb(-EFAULT, nfs, command_data, data->private_data);
2546 free_nfs_cb_data(data);
2547 return;
2548 }
2549 if (status == RPC_STATUS_CANCEL) {
2550 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2551 free_nfs_cb_data(data);
2552 return;
2553 }
2554
2555 res = command_data;
2556 if (res->status != NFS3_OK) {
2557 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2558 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2559 free_nfs_cb_data(data);
2560 return;
2561 }
2562
2563 data->cb(0, nfs, NULL, data->private_data);
2564 free_nfs_cb_data(data);
2565}
2566
2567struct nfs_chown_data {
2568 uid_t uid;
2569 gid_t gid;
2570};
2571
2572static int nfs_chown_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2573{
2574 SETATTR3args args;
2575 struct nfs_chown_data *chown_data = data->continue_data;
2576
ea98629a 2577 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2578 args.object.data.data_len = data->fh.data.data_len;
2579 args.object.data.data_val = data->fh.data.data_val;
2580 if (chown_data->uid != (uid_t)-1) {
2581 args.new_attributes.uid.set_it = 1;
2582 args.new_attributes.uid.set_uid3_u.uid = chown_data->uid;
2583 }
2584 if (chown_data->gid != (gid_t)-1) {
2585 args.new_attributes.gid.set_it = 1;
2586 args.new_attributes.gid.set_gid3_u.gid = chown_data->gid;
2587 }
2588
2589 if (rpc_nfs_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) {
2590 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2591 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2592 free_nfs_cb_data(data);
2593 return -1;
2594 }
2595 return 0;
2596}
2597
2598
2599int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data)
2600{
2601 struct nfs_chown_data *chown_data;
2602
2603 chown_data = malloc(sizeof(struct nfs_chown_data));
2604 if (chown_data == NULL) {
cbbf9d3e 2605 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
2606 return -1;
2607 }
2608
2609 chown_data->uid = uid;
2610 chown_data->gid = gid;
2611
2612 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) {
cbbf9d3e 2613 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2614 return -1;
2615 }
2616
2617 return 0;
2618}
2619
2620
2621/*
2622 * Async fchown()
2623 */
2624int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data)
2625{
2626 struct nfs_cb_data *data;
2627 struct nfs_chown_data *chown_data;
2628
2629 chown_data = malloc(sizeof(struct nfs_chown_data));
2630 if (chown_data == NULL) {
cbbf9d3e 2631 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
2632 return -1;
2633 }
2634
2635 chown_data->uid = uid;
2636 chown_data->gid = gid;
2637
2638
2639 data = malloc(sizeof(struct nfs_cb_data));
2640 if (data == NULL) {
cbbf9d3e 2641 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data");
84004dbf
RS
2642 return -1;
2643 }
ea98629a 2644 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
2645 data->nfs = nfs;
2646 data->cb = cb;
2647 data->private_data = private_data;
2648 data->continue_data = chown_data;
2649 data->fh.data.data_len = nfsfh->fh.data.data_len;
2650 data->fh.data.data_val = malloc(data->fh.data.data_len);
2651 if (data->fh.data.data_val == NULL) {
2652 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
2653 free_nfs_cb_data(data);
2654 return -1;
2655 }
2656 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
2657
2658
2659 if (nfs_chown_continue_internal(nfs, data) != 0) {
2660 free_nfs_cb_data(data);
2661 return -1;
2662 }
2663
2664 return 0;
2665}
2666
2667
2668
2669
2670
2671/*
2672 * Async utimes()
2673 */
2674static void nfs_utimes_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2675{
2676 struct nfs_cb_data *data = private_data;
2677 struct nfs_context *nfs = data->nfs;
2678 SETATTR3res *res;
2679
2680 if (status == RPC_STATUS_ERROR) {
2681 data->cb(-EFAULT, nfs, command_data, data->private_data);
2682 free_nfs_cb_data(data);
2683 return;
2684 }
2685 if (status == RPC_STATUS_CANCEL) {
2686 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2687 free_nfs_cb_data(data);
2688 return;
2689 }
2690
2691 res = command_data;
2692 if (res->status != NFS3_OK) {
2693 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2694 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2695 free_nfs_cb_data(data);
2696 return;
2697 }
2698
2699 data->cb(0, nfs, NULL, data->private_data);
2700 free_nfs_cb_data(data);
2701}
2702
2703static int nfs_utimes_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2704{
2705 SETATTR3args args;
2706 struct timeval *utimes_data = data->continue_data;
2707
ea98629a 2708 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2709 args.object.data.data_len = data->fh.data.data_len;
2710 args.object.data.data_val = data->fh.data.data_val;
2711 if (utimes_data != NULL) {
2712 args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME;
2713 args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec;
2714 args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000;
2715 args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME;
2716 args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec;
2717 args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000;
2718 } else {
2719 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2720 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2721 }
2722
2723 if (rpc_nfs_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) {
2724 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2725 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2726 free_nfs_cb_data(data);
2727 return -1;
2728 }
2729 return 0;
2730}
2731
2732
2733int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data)
2734{
2735 struct timeval *new_times = NULL;
2736
2737 if (times != NULL) {
2738 new_times = malloc(sizeof(struct timeval)*2);
2739 if (new_times == NULL) {
cbbf9d3e 2740 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
2741 return -1;
2742 }
2743
2744 memcpy(new_times, times, sizeof(struct timeval)*2);
2745 }
2746
2747 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 2748 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2749 return -1;
2750 }
2751
2752 return 0;
2753}
2754
2755/*
2756 * Async utime()
2757 */
2758int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data)
2759{
2760 struct timeval *new_times = NULL;
2761
2762 if (times != NULL) {
2763 new_times = malloc(sizeof(struct timeval)*2);
2764 if (new_times == NULL) {
cbbf9d3e 2765 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
2766 return -1;
2767 }
2768
2769 new_times[0].tv_sec = times->actime;
2770 new_times[0].tv_usec = 0;
2771 new_times[1].tv_sec = times->modtime;
2772 new_times[1].tv_usec = 0;
2773 }
2774
2775 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 2776 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2777 return -1;
2778 }
2779
2780 return 0;
2781}
2782
2783
2784
2785
2786
2787/*
2788 * Async access()
2789 */
2790static void nfs_access_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2791{
2792 ACCESS3res *res;
2793 struct nfs_cb_data *data = private_data;
2794 struct nfs_context *nfs = data->nfs;
2795 unsigned int nfsmode = 0;
2796
2797 if (status == RPC_STATUS_ERROR) {
2798 data->cb(-EFAULT, nfs, command_data, data->private_data);
2799 free_nfs_cb_data(data);
2800 return;
2801 }
2802 if (status == RPC_STATUS_CANCEL) {
2803 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2804 free_nfs_cb_data(data);
2805 return;
2806 }
2807
2808 res = command_data;
2809 if (res->status != NFS3_OK) {
2810 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));
2811 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2812 free_nfs_cb_data(data);
2813 return;
2814 }
2815
2816 if (data->continue_int & R_OK) {
2817 nfsmode |= ACCESS3_READ;
2818 }
2819 if (data->continue_int & W_OK) {
2820 nfsmode |= ACCESS3_MODIFY;
2821 }
2822 if (data->continue_int & X_OK) {
2823 nfsmode |= ACCESS3_EXECUTE;
2824 }
2825
2826 if (res->ACCESS3res_u.resok.access != nfsmode) {
2827 rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2828 nfsmode&ACCESS3_READ?'r':'-',
2829 nfsmode&ACCESS3_MODIFY?'w':'-',
2830 nfsmode&ACCESS3_EXECUTE?'x':'-',
2831 res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-',
2832 res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-',
2833 res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-');
2834 data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data);
2835 free_nfs_cb_data(data);
2836 return;
2837 }
2838
2839 data->cb(0, nfs, NULL, data->private_data);
2840 free_nfs_cb_data(data);
2841}
2842
2843static int nfs_access_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2844{
2845 int nfsmode = 0;
2846
2847 if (data->continue_int & R_OK) {
2848 nfsmode |= ACCESS3_READ;
2849 }
2850 if (data->continue_int & W_OK) {
2851 nfsmode |= ACCESS3_MODIFY;
2852 }
2853 if (data->continue_int & X_OK) {
2854 nfsmode |= ACCESS3_EXECUTE;
2855 }
2856
2857 if (rpc_nfs_access_async(nfs->rpc, nfs_access_cb, &data->fh, nfsmode, data) != 0) {
2858 rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
2859 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2860 free_nfs_cb_data(data);
2861 return -1;
2862 }
2863 return 0;
2864}
2865
2866int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
2867{
2868 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e
RS
2869 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2870 return -1;
84004dbf
RS
2871 }
2872
2873 return 0;
2874}
2875
2876
2877
2878/*
2879 * Async symlink()
2880 */
2881struct nfs_symlink_data {
2882 char *oldpath;
2883 char *newpathparent;
2884 char *newpathobject;
2885};
2886
2887static void free_nfs_symlink_data(void *mem)
2888{
2889 struct nfs_symlink_data *data = mem;
2890
2891 if (data->oldpath != NULL) {
2892 free(data->oldpath);
2893 }
2894 if (data->newpathparent != NULL) {
2895 free(data->newpathparent);
2896 }
2897 if (data->newpathobject != NULL) {
2898 free(data->newpathobject);
2899 }
2900 free(data);
2901}
2902
2903static void nfs_symlink_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2904{
2905 SYMLINK3res *res;
2906 struct nfs_cb_data *data = private_data;
2907 struct nfs_context *nfs = data->nfs;
2908 struct nfs_symlink_data *symlink_data = data->continue_data;
2909
2910 if (status == RPC_STATUS_ERROR) {
2911 data->cb(-EFAULT, nfs, command_data, data->private_data);
2912 free_nfs_cb_data(data);
2913 return;
2914 }
2915 if (status == RPC_STATUS_CANCEL) {
2916 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2917 free_nfs_cb_data(data);
2918 return;
2919 }
2920
2921 res = command_data;
2922 if (res->status != NFS3_OK) {
2923 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));
2924 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2925 free_nfs_cb_data(data);
2926 return;
2927 }
2928
2929 data->cb(0, nfs, NULL, data->private_data);
2930 free_nfs_cb_data(data);
2931}
2932
2933static int nfs_symlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2934{
2935 struct nfs_symlink_data *symlink_data = data->continue_data;
2936
2937 if (rpc_nfs_symlink_async(nfs->rpc, nfs_symlink_cb, &data->fh, symlink_data->newpathobject, symlink_data->oldpath, data) != 0) {
2938 rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path);
2939 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2940 free_nfs_cb_data(data);
2941 return -1;
2942 }
2943 return 0;
2944}
2945
2946int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
2947{
2948 char *ptr;
2949 struct nfs_symlink_data *symlink_data;
2950
2951 symlink_data = malloc(sizeof(struct nfs_symlink_data));
2952 if (symlink_data == NULL) {
cbbf9d3e 2953 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data");
84004dbf
RS
2954 return -1;
2955 }
ea98629a 2956 memset(symlink_data, 0, sizeof(struct nfs_symlink_data));
84004dbf
RS
2957
2958 symlink_data->oldpath = strdup(oldpath);
2959 if (symlink_data->oldpath == NULL) {
cbbf9d3e 2960 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 2961 free_nfs_symlink_data(symlink_data);
cbbf9d3e 2962 return -1;
84004dbf
RS
2963 }
2964
2965 symlink_data->newpathparent = strdup(newpath);
2966 if (symlink_data->newpathparent == NULL) {
cbbf9d3e 2967 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 2968 free_nfs_symlink_data(symlink_data);
cbbf9d3e 2969 return -1;
84004dbf
RS
2970 }
2971
93e9306f 2972 ptr = strrchr(symlink_data->newpathparent, '/');
84004dbf 2973 if (ptr == NULL) {
cbbf9d3e 2974 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 2975 free_nfs_symlink_data(symlink_data);
cbbf9d3e 2976 return -1;
84004dbf
RS
2977 }
2978 *ptr = 0;
2979 ptr++;
2980
2981 symlink_data->newpathobject = strdup(ptr);
2982 if (symlink_data->newpathobject == NULL) {
cbbf9d3e 2983 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 2984 free_nfs_symlink_data(symlink_data);
cbbf9d3e 2985 return -1;
84004dbf
RS
2986 }
2987
2988 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
2989 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2990 return -1;
84004dbf
RS
2991 }
2992
2993 return 0;
2994}
2995
2996
2997
2998/*
2999 * Async rename()
3000 */
3001struct nfs_rename_data {
3002 char *oldpath;
3003 char *oldobject;
3004 struct nfs_fh3 olddir;
3005 char *newpath;
3006 char *newobject;
3007 struct nfs_fh3 newdir;
3008};
3009
3010static void free_nfs_rename_data(void *mem)
3011{
3012 struct nfs_rename_data *data = mem;
3013
3014 if (data->oldpath != NULL) {
3015 free(data->oldpath);
3016 }
3017 if (data->olddir.data.data_val != NULL) {
3018 free(data->olddir.data.data_val);
3019 }
3020 if (data->newpath != NULL) {
3021 free(data->newpath);
3022 }
3023 if (data->newdir.data.data_val != NULL) {
3024 free(data->newdir.data.data_val);
3025 }
3026 free(data);
3027}
3028
3029static void nfs_rename_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
3030{
3031 RENAME3res *res;
3032 struct nfs_cb_data *data = private_data;
3033 struct nfs_context *nfs = data->nfs;
3034 struct nfs_rename_data *rename_data = data->continue_data;
3035
3036 if (status == RPC_STATUS_ERROR) {
3037 data->cb(-EFAULT, nfs, command_data, data->private_data);
3038 free_nfs_cb_data(data);
3039 return;
3040 }
3041 if (status == RPC_STATUS_CANCEL) {
3042 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3043 free_nfs_cb_data(data);
3044 return;
3045 }
3046
3047 res = command_data;
3048 if (res->status != NFS3_OK) {
3049 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));
3050 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3051 free_nfs_cb_data(data);
3052 return;
3053 }
3054
3055 data->cb(0, nfs, NULL, data->private_data);
3056 free_nfs_cb_data(data);
3057}
3058
3059static int nfs_rename_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3060{
3061 struct nfs_rename_data *rename_data = data->continue_data;
3062
3063 /* steal the filehandle */
3064 rename_data->newdir.data.data_len = data->fh.data.data_len;
3065 rename_data->newdir.data.data_val = data->fh.data.data_val;
3066 data->fh.data.data_val = NULL;
3067
3068 if (rpc_nfs_rename_async(nfs->rpc, nfs_rename_cb, &rename_data->olddir, rename_data->oldobject, &rename_data->newdir, rename_data->newobject, data) != 0) {
3069 rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path);
3070 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3071 free_nfs_cb_data(data);
3072 return -1;
3073 }
3074 return 0;
3075}
3076
3077
3078static int nfs_rename_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3079{
3080 struct nfs_rename_data *rename_data = data->continue_data;
3081
3082 /* steal the filehandle */
3083 rename_data->olddir.data.data_len = data->fh.data.data_len;
3084 rename_data->olddir.data.data_val = data->fh.data.data_val;
3085 data->fh.data.data_val = NULL;
3086
3087 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) {
3088 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", rename_data->newpath);
3089 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3090 free_nfs_cb_data(data);
3091 return -1;
3092 }
3093 data->continue_data = NULL;
3094 free_nfs_cb_data(data);
3095
3096 return 0;
3097}
3098
3099
3100int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3101{
3102 char *ptr;
3103 struct nfs_rename_data *rename_data;
3104
3105 rename_data = malloc(sizeof(struct nfs_rename_data));
3106 if (rename_data == NULL) {
cbbf9d3e 3107 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data");
84004dbf
RS
3108 return -1;
3109 }
ea98629a 3110 memset(rename_data, 0, sizeof(struct nfs_rename_data));
84004dbf
RS
3111
3112 rename_data->oldpath = strdup(oldpath);
3113 if (rename_data->oldpath == NULL) {
cbbf9d3e 3114 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3115 free_nfs_rename_data(rename_data);
cbbf9d3e 3116 return -1;
84004dbf 3117 }
93e9306f 3118 ptr = strrchr(rename_data->oldpath, '/');
84004dbf 3119 if (ptr == NULL) {
cbbf9d3e 3120 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 3121 free_nfs_rename_data(rename_data);
cbbf9d3e 3122 return -1;
84004dbf
RS
3123 }
3124 *ptr = 0;
3125 ptr++;
3126 rename_data->oldobject = ptr;
3127
3128
3129 rename_data->newpath = strdup(newpath);
3130 if (rename_data->newpath == NULL) {
cbbf9d3e 3131 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 3132 free_nfs_rename_data(rename_data);
cbbf9d3e 3133 return -1;
84004dbf 3134 }
93e9306f 3135 ptr = strrchr(rename_data->newpath, '/');
84004dbf 3136 if (ptr == NULL) {
cbbf9d3e 3137 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 3138 free_nfs_rename_data(rename_data);
cbbf9d3e 3139 return -1;
84004dbf
RS
3140 }
3141 *ptr = 0;
3142 ptr++;
3143 rename_data->newobject = ptr;
3144
3145
3146 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
3147 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3148 return -1;
84004dbf
RS
3149 }
3150
3151 return 0;
3152}
3153
3154
3155/*
3156 * Async link()
3157 */
3158struct nfs_link_data {
3159 char *oldpath;
3160 struct nfs_fh3 oldfh;
3161 char *newpath;
3162 char *newobject;
3163 struct nfs_fh3 newdir;
3164};
3165
3166static void free_nfs_link_data(void *mem)
3167{
3168 struct nfs_link_data *data = mem;
3169
3170 if (data->oldpath != NULL) {
3171 free(data->oldpath);
3172 }
3173 if (data->oldfh.data.data_val != NULL) {
3174 free(data->oldfh.data.data_val);
3175 }
3176 if (data->newpath != NULL) {
3177 free(data->newpath);
3178 }
3179 if (data->newdir.data.data_val != NULL) {
3180 free(data->newdir.data.data_val);
3181 }
3182 free(data);
3183}
3184
3185static void nfs_link_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
3186{
3187 LINK3res *res;
3188 struct nfs_cb_data *data = private_data;
3189 struct nfs_context *nfs = data->nfs;
3190 struct nfs_link_data *link_data = data->continue_data;
3191
3192 if (status == RPC_STATUS_ERROR) {
3193 data->cb(-EFAULT, nfs, command_data, data->private_data);
3194 free_nfs_cb_data(data);
3195 return;
3196 }
3197 if (status == RPC_STATUS_CANCEL) {
3198 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
3199 free_nfs_cb_data(data);
3200 return;
3201 }
3202
3203 res = command_data;
3204 if (res->status != NFS3_OK) {
3205 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));
3206 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
3207 free_nfs_cb_data(data);
3208 return;
3209 }
3210
3211 data->cb(0, nfs, NULL, data->private_data);
3212 free_nfs_cb_data(data);
3213}
3214
3215static int nfs_link_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3216{
3217 struct nfs_link_data *link_data = data->continue_data;
3218
3219 /* steal the filehandle */
3220 link_data->newdir.data.data_len = data->fh.data.data_len;
3221 link_data->newdir.data.data_val = data->fh.data.data_val;
3222 data->fh.data.data_val = NULL;
3223
3224 if (rpc_nfs_link_async(nfs->rpc, nfs_link_cb, &link_data->oldfh, &link_data->newdir, link_data->newobject, data) != 0) {
3225 rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path);
3226 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3227 free_nfs_cb_data(data);
3228 return -1;
3229 }
3230 return 0;
3231}
3232
3233
3234static int nfs_link_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
3235{
3236 struct nfs_link_data *link_data = data->continue_data;
3237
3238 /* steal the filehandle */
3239 link_data->oldfh.data.data_len = data->fh.data.data_len;
3240 link_data->oldfh.data.data_val = data->fh.data.data_val;
3241 data->fh.data.data_val = NULL;
3242
3243 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) {
3244 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath);
3245 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
3246 free_nfs_cb_data(data);
3247 return -1;
3248 }
3249 data->continue_data = NULL;
3250 free_nfs_cb_data(data);
3251
3252 return 0;
3253}
3254
3255
3256int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
3257{
3258 char *ptr;
3259 struct nfs_link_data *link_data;
3260
3261 link_data = malloc(sizeof(struct nfs_link_data));
3262 if (link_data == NULL) {
cbbf9d3e 3263 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data");
84004dbf
RS
3264 return -1;
3265 }
ea98629a 3266 memset(link_data, 0, sizeof(struct nfs_link_data));
84004dbf
RS
3267
3268 link_data->oldpath = strdup(oldpath);
3269 if (link_data->oldpath == NULL) {
cbbf9d3e 3270 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 3271 free_nfs_link_data(link_data);
cbbf9d3e 3272 return -1;
84004dbf
RS
3273 }
3274
3275 link_data->newpath = strdup(newpath);
3276 if (link_data->newpath == NULL) {
cbbf9d3e 3277 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 3278 free_nfs_link_data(link_data);
cbbf9d3e 3279 return -1;
84004dbf 3280 }
93e9306f 3281 ptr = strrchr(link_data->newpath, '/');
84004dbf 3282 if (ptr == NULL) {
cbbf9d3e 3283 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 3284 free_nfs_link_data(link_data);
cbbf9d3e 3285 return -1;
84004dbf
RS
3286 }
3287 *ptr = 0;
3288 ptr++;
3289 link_data->newobject = ptr;
3290
3291
3292 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
3293 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
3294 return -1;
84004dbf
RS
3295 }
3296
3297 return 0;
3298}
3299
3300
3301//qqq replace later with lseek()
183451cf 3302uint64_t nfs_get_current_offset(struct nfsfh *nfsfh)
84004dbf
RS
3303{
3304 return nfsfh->offset;
3305}
3306
17ef62fa
RS
3307
3308
3309/*
3310 * Get the maximum supported READ3 size by the server
3311 */
183451cf 3312uint64_t nfs_get_readmax(struct nfs_context *nfs)
17ef62fa
RS
3313{
3314 return nfs->readmax;
3315}
3316
3317/*
3318 * Get the maximum supported WRITE3 size by the server
3319 */
183451cf 3320uint64_t nfs_get_writemax(struct nfs_context *nfs)
17ef62fa 3321{
18c27b73
RS
3322 /* Some XDR libraries can not marshall PDUs bigger than this */
3323 if (nfs->writemax < 32768) {
3324 return nfs->writemax;
3325 }
3326 return 32768;
17ef62fa 3327}
1896d37b
RS
3328
3329void nfs_set_error(struct nfs_context *nfs, char *error_string, ...)
3330{
3331 va_list ap;
1e8994af 3332 char *str = NULL;
1896d37b 3333
1e8994af 3334 va_start(ap, error_string);
2606f9bb
RS
3335 str = malloc(1024);
3336 vsnprintf(str, 1024, error_string, ap);
1896d37b
RS
3337 if (nfs->rpc->error_string != NULL) {
3338 free(nfs->rpc->error_string);
3339 }
1896d37b 3340 nfs->rpc->error_string = str;
a8a1b858 3341 va_end(ap);
1896d37b 3342}
7f0242ca
RS
3343
3344
3345
3346struct mount_cb_data {
3347 rpc_cb cb;
3348 void *private_data;
3349 char *server;
3350};
3351
3352static void free_mount_cb_data(struct mount_cb_data *data)
3353{
3354 if (data->server != NULL) {
3355 free(data->server);
3356 data->server = NULL;
3357 }
3358
3359 free(data);
3360}
3361
3362static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3363{
3364 struct mount_cb_data *data = private_data;
3365
3366 if (status == RPC_STATUS_ERROR) {
3367 data->cb(rpc, -EFAULT, command_data, data->private_data);
3368 free_mount_cb_data(data);
3369 return;
3370 }
3371 if (status == RPC_STATUS_CANCEL) {
3372 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3373 free_mount_cb_data(data);
3374 return;
3375 }
3376
3377 data->cb(rpc, 0, command_data, data->private_data);
3378 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
3379 rpc_set_error(rpc, "Failed to disconnect\n");
3380 }
3381 free_mount_cb_data(data);
3382}
3383
3384static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3385{
3386 struct mount_cb_data *data = private_data;
3387
14a062eb
RS
3388 /* Dont want any more callbacks even if the socket is closed */
3389 rpc->connect_cb = NULL;
3390
7f0242ca
RS
3391 if (status == RPC_STATUS_ERROR) {
3392 data->cb(rpc, -EFAULT, command_data, data->private_data);
3393 free_mount_cb_data(data);
3394 return;
3395 }
3396 if (status == RPC_STATUS_CANCEL) {
3397 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3398 free_mount_cb_data(data);
3399 return;
3400 }
3401
3402 if (rpc_mount_export_async(rpc, mount_export_5_cb, data) != 0) {
3403 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3404 free_mount_cb_data(data);
3405 return;
3406 }
3407}
3408
3409static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3410{
3411 struct mount_cb_data *data = private_data;
3412 uint32_t mount_port;
3413
3414 if (status == RPC_STATUS_ERROR) {
3415 data->cb(rpc, -EFAULT, command_data, data->private_data);
3416 free_mount_cb_data(data);
3417 return;
3418 }
3419 if (status == RPC_STATUS_CANCEL) {
3420 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3421 free_mount_cb_data(data);
3422 return;
3423 }
3424
3425 mount_port = *(uint32_t *)command_data;
3426 if (mount_port == 0) {
3427 rpc_set_error(rpc, "RPC error. Mount program is not available");
3428 data->cb(rpc, -ENOENT, command_data, data->private_data);
3429 free_mount_cb_data(data);
3430 return;
3431 }
3432
3433 rpc_disconnect(rpc, "normal disconnect");
3434 if (rpc_connect_async(rpc, data->server, mount_port, mount_export_4_cb, data) != 0) {
3435 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3436 free_mount_cb_data(data);
3437 return;
3438 }
3439}
3440
3441static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3442{
3443 struct mount_cb_data *data = private_data;
3444
3445 if (status == RPC_STATUS_ERROR) {
3446 data->cb(rpc, -EFAULT, command_data, data->private_data);
3447 free_mount_cb_data(data);
3448 return;
3449 }
3450 if (status == RPC_STATUS_CANCEL) {
3451 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3452 free_mount_cb_data(data);
3453 return;
3454 }
3455
5c6b1176 3456 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, mount_export_3_cb, private_data) != 0) {
7f0242ca
RS
3457 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3458 free_mount_cb_data(data);
3459 return;
3460 }
3461}
3462
3463static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3464{
3465 struct mount_cb_data *data = private_data;
3466
14a062eb
RS
3467 /* Dont want any more callbacks even if the socket is closed */
3468 rpc->connect_cb = NULL;
3469
7f0242ca
RS
3470 if (status == RPC_STATUS_ERROR) {
3471 data->cb(rpc, -EFAULT, command_data, data->private_data);
3472 free_mount_cb_data(data);
3473 return;
3474 }
3475 if (status == RPC_STATUS_CANCEL) {
3476 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3477 free_mount_cb_data(data);
3478 return;
3479 }
3480
3481 if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) {
3482 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3483 free_mount_cb_data(data);
3484 return;
3485 }
3486}
3487
3488int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data)
3489{
3490 struct mount_cb_data *data;
3491
3492 data = malloc(sizeof(struct mount_cb_data));
3493 if (data == NULL) {
3494 return -1;
3495 }
ea98629a 3496 memset(data, 0, sizeof(struct mount_cb_data));
7f0242ca
RS
3497 data->cb = cb;
3498 data->private_data = private_data;
3499 data->server = strdup(server);
3500 if (data->server == NULL) {
3501 free_mount_cb_data(data);
3502 return -1;
3503 }
3504 if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) {
3505 free_mount_cb_data(data);
3506 return -1;
3507 }
3508
3509 return 0;
3510}
3511
df5af25f
RS
3512struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs)
3513{
3514 return nfs->rpc;
3515}
3516
b077fdeb
RS
3517const char *nfs_get_server(struct nfs_context *nfs) {
3518 return nfs->server;
3519}
3520
3521const char *nfs_get_export(struct nfs_context *nfs) {
3522 return nfs->export;
3523}
0ad9a1f1 3524
8724c833
RS
3525const struct nfs_fh3 *nfs_get_rootfh(struct nfs_context *nfs) {
3526 return &nfs->rootfh;
3527}