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