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