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