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; | |
f893b680 | 96 | char *cwd; |
84004dbf RS |
97 | }; |
98 | ||
99 | void nfs_free_nfsdir(struct nfsdir *nfsdir) | |
100 | { | |
101 | while (nfsdir->entries) { | |
102 | struct nfsdirent *dirent = nfsdir->entries->next; | |
103 | if (nfsdir->entries->name != NULL) { | |
104 | free(nfsdir->entries->name); | |
105 | } | |
106 | free(nfsdir->entries); | |
107 | nfsdir->entries = dirent; | |
108 | } | |
109 | free(nfsdir); | |
110 | } | |
111 | ||
84004dbf RS |
112 | struct nfs_cb_data; |
113 | typedef int (*continue_func)(struct nfs_context *nfs, struct nfs_cb_data *data); | |
114 | ||
115 | struct nfs_cb_data { | |
116 | struct nfs_context *nfs; | |
117 | struct nfsfh *nfsfh; | |
118 | char *saved_path, *path; | |
119 | ||
120 | nfs_cb cb; | |
121 | void *private_data; | |
122 | ||
123 | continue_func continue_cb; | |
124 | void *continue_data; | |
125 | void (*free_continue_data)(void *); | |
126 | int continue_int; | |
127 | ||
128 | struct nfs_fh3 fh; | |
921f877b RS |
129 | |
130 | /* for multi-read/write calls. */ | |
131 | int error; | |
132 | int cancel; | |
133 | int num_calls; | |
183451cf | 134 | uint64_t start_offset, max_offset; |
921f877b RS |
135 | char *buffer; |
136 | }; | |
137 | ||
138 | struct nfs_mcb_data { | |
139 | struct nfs_cb_data *data; | |
183451cf RS |
140 | uint64_t offset; |
141 | uint64_t count; | |
84004dbf RS |
142 | }; |
143 | ||
144 | static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh); | |
145 | ||
67ba2239 | 146 | void nfs_set_auth(struct nfs_context *nfs, struct AUTH *auth) |
84004dbf | 147 | { |
9896c715 | 148 | rpc_set_auth(nfs->rpc, auth); |
84004dbf RS |
149 | } |
150 | ||
151 | int nfs_get_fd(struct nfs_context *nfs) | |
152 | { | |
153 | return rpc_get_fd(nfs->rpc); | |
154 | } | |
155 | ||
83aa785d RS |
156 | int nfs_queue_length(struct nfs_context *nfs) |
157 | { | |
158 | return rpc_queue_length(nfs->rpc); | |
159 | } | |
160 | ||
84004dbf RS |
161 | int nfs_which_events(struct nfs_context *nfs) |
162 | { | |
163 | return rpc_which_events(nfs->rpc); | |
164 | } | |
165 | ||
166 | int nfs_service(struct nfs_context *nfs, int revents) | |
167 | { | |
168 | return rpc_service(nfs->rpc, revents); | |
169 | } | |
170 | ||
171 | char *nfs_get_error(struct nfs_context *nfs) | |
172 | { | |
173 | return rpc_get_error(nfs->rpc); | |
174 | }; | |
175 | ||
20d6926e | 176 | static int nfs_set_context_args(struct nfs_context *nfs, char *arg, char *val) |
36f488dc RS |
177 | { |
178 | if (!strncmp(arg, "tcp-syncnt", 10)) { | |
20d6926e | 179 | rpc_set_tcp_syncnt(nfs_get_rpc_context(nfs), atoi(val)); |
36f488dc | 180 | } else if (!strncmp(arg, "uid", 3)) { |
20d6926e | 181 | rpc_set_uid(nfs_get_rpc_context(nfs), atoi(val)); |
36f488dc | 182 | } else if (!strncmp(arg, "gid", 3)) { |
20d6926e | 183 | rpc_set_gid(nfs_get_rpc_context(nfs), atoi(val)); |
36f488dc RS |
184 | } |
185 | return 0; | |
186 | } | |
187 | ||
6bdcd5b6 | 188 | static struct nfs_url *nfs_parse_url(struct nfs_context *nfs, const char *url, int dir, int incomplete) |
d2ec73c7 PL |
189 | { |
190 | struct nfs_url *urls; | |
191 | char *strp, *flagsp, *strp2; | |
192 | ||
193 | if (strncmp(url, "nfs://", 6)) { | |
194 | rpc_set_error(nfs->rpc, "Invalid URL specified"); | |
195 | return NULL; | |
196 | } | |
197 | ||
198 | urls = malloc(sizeof(struct nfs_url)); | |
199 | if (urls == NULL) { | |
200 | rpc_set_error(nfs->rpc, "Out of memory"); | |
201 | return NULL; | |
202 | } | |
203 | ||
b70567b6 | 204 | memset(urls, 0x00, sizeof(struct nfs_url)); |
d2ec73c7 PL |
205 | urls->server = strdup(url + 6); |
206 | if (urls->server == NULL) { | |
207 | nfs_destroy_url(urls); | |
208 | rpc_set_error(nfs->rpc, "Out of memory"); | |
209 | return NULL; | |
210 | } | |
211 | ||
212 | if (urls->server[0] == '/' || urls->server[0] == '\0' || | |
213 | urls->server[0] == '?') { | |
214 | if (incomplete) { | |
215 | flagsp = strchr(urls->server, '?'); | |
216 | goto flags; | |
217 | } | |
218 | nfs_destroy_url(urls); | |
219 | rpc_set_error(nfs->rpc, "Invalid server string"); | |
220 | return NULL; | |
221 | } | |
222 | ||
223 | strp = strchr(urls->server, '/'); | |
224 | if (strp == NULL) { | |
225 | if (incomplete) { | |
226 | flagsp = strchr(urls->server, '?'); | |
227 | goto flags; | |
228 | } | |
229 | nfs_destroy_url(urls); | |
230 | rpc_set_error(nfs->rpc, "Incomplete or invalid URL specified."); | |
231 | return NULL; | |
232 | } | |
233 | ||
234 | urls->path = strdup(strp); | |
235 | if (urls->path == NULL) { | |
236 | nfs_destroy_url(urls); | |
237 | rpc_set_error(nfs->rpc, "Out of memory"); | |
238 | return NULL; | |
239 | } | |
240 | *strp = 0; | |
241 | ||
242 | if (dir) { | |
243 | flagsp = strchr(urls->path, '?'); | |
244 | goto flags; | |
245 | } | |
246 | ||
247 | strp = strrchr(urls->path, '/'); | |
248 | if (strp == NULL) { | |
249 | if (incomplete) { | |
250 | flagsp = strchr(urls->path, '?'); | |
251 | goto flags; | |
252 | } | |
253 | nfs_destroy_url(urls); | |
254 | rpc_set_error(nfs->rpc, "Incomplete or invalid URL specified."); | |
255 | return NULL; | |
256 | } | |
257 | urls->file = strdup(strp); | |
258 | if (urls->path == NULL) { | |
259 | nfs_destroy_url(urls); | |
260 | rpc_set_error(nfs->rpc, "Out of memory"); | |
261 | return NULL; | |
262 | } | |
263 | *strp = 0; | |
264 | flagsp = strchr(urls->file, '?'); | |
265 | ||
266 | flags: | |
267 | if (flagsp) { | |
268 | *flagsp = 0; | |
269 | } | |
270 | ||
271 | if (urls->file && !strlen(urls->file)) { | |
272 | free(urls->file); | |
273 | urls->file = NULL; | |
274 | if (!incomplete) { | |
275 | nfs_destroy_url(urls); | |
276 | rpc_set_error(nfs->rpc, "Incomplete or invalid URL specified."); | |
277 | return NULL; | |
278 | } | |
279 | } | |
280 | ||
d2ec73c7 PL |
281 | while (flagsp != NULL && *(flagsp+1) != 0) { |
282 | strp = flagsp + 1; | |
283 | flagsp = strchr(strp, '&'); | |
284 | if (flagsp) { | |
285 | *flagsp = 0; | |
286 | } | |
287 | strp2 = strchr(strp, '='); | |
288 | if (strp2) { | |
289 | *strp2 = 0; | |
290 | strp2++; | |
20d6926e | 291 | nfs_set_context_args(nfs, strp, strp2); |
d2ec73c7 PL |
292 | } |
293 | } | |
294 | ||
1f1b1c5c PL |
295 | if (urls->server && strlen(urls->server) <= 1) { |
296 | free(urls->server); | |
297 | urls->server = NULL; | |
298 | } | |
299 | ||
d2ec73c7 PL |
300 | return urls; |
301 | } | |
302 | ||
303 | struct nfs_url *nfs_parse_url_full(struct nfs_context *nfs, const char *url) | |
304 | { | |
305 | return nfs_parse_url(nfs, url, 0, 0); | |
306 | } | |
307 | ||
308 | struct nfs_url *nfs_parse_url_dir(struct nfs_context *nfs, const char *url) | |
309 | { | |
310 | return nfs_parse_url(nfs, url, 1, 0); | |
311 | } | |
312 | ||
313 | struct nfs_url *nfs_parse_url_incomplete(struct nfs_context *nfs, const char *url) | |
314 | { | |
315 | return nfs_parse_url(nfs, url, 0, 1); | |
316 | } | |
317 | ||
318 | ||
319 | void nfs_destroy_url(struct nfs_url *url) | |
320 | { | |
321 | if (url) { | |
322 | free(url->server); | |
323 | free(url->path); | |
324 | free(url->file); | |
325 | } | |
326 | free(url); | |
327 | } | |
328 | ||
84004dbf RS |
329 | struct nfs_context *nfs_init_context(void) |
330 | { | |
331 | struct nfs_context *nfs; | |
332 | ||
333 | nfs = malloc(sizeof(struct nfs_context)); | |
334 | if (nfs == NULL) { | |
84004dbf RS |
335 | return NULL; |
336 | } | |
f893b680 RS |
337 | memset(nfs, 0, sizeof(struct nfs_context)); |
338 | ||
84004dbf RS |
339 | nfs->rpc = rpc_init_context(); |
340 | if (nfs->rpc == NULL) { | |
84004dbf RS |
341 | free(nfs); |
342 | return NULL; | |
343 | } | |
344 | ||
f893b680 | 345 | nfs->cwd = strdup("/"); |
963c2f83 | 346 | |
84004dbf RS |
347 | return nfs; |
348 | } | |
349 | ||
350 | void nfs_destroy_context(struct nfs_context *nfs) | |
351 | { | |
352 | rpc_destroy_context(nfs->rpc); | |
353 | nfs->rpc = NULL; | |
354 | ||
355 | if (nfs->server) { | |
356 | free(nfs->server); | |
357 | nfs->server = NULL; | |
358 | } | |
359 | ||
360 | if (nfs->export) { | |
361 | free(nfs->export); | |
362 | nfs->export = NULL; | |
363 | } | |
364 | ||
f893b680 RS |
365 | if (nfs->cwd) { |
366 | free(nfs->cwd); | |
367 | nfs->cwd = NULL; | |
368 | } | |
369 | ||
84004dbf RS |
370 | if (nfs->rootfh.data.data_val != NULL) { |
371 | free(nfs->rootfh.data.data_val); | |
372 | nfs->rootfh.data.data_val = NULL; | |
373 | } | |
374 | ||
375 | free(nfs); | |
376 | } | |
377 | ||
8733f38d RS |
378 | struct rpc_cb_data { |
379 | char *server; | |
380 | uint32_t program; | |
381 | uint32_t version; | |
382 | ||
383 | rpc_cb cb; | |
384 | void *private_data; | |
2452c57f | 385 | }; |
8733f38d RS |
386 | |
387 | void free_rpc_cb_data(struct rpc_cb_data *data) | |
388 | { | |
389 | free(data->server); | |
390 | data->server = NULL; | |
391 | free(data); | |
392 | } | |
393 | ||
394 | static void rpc_connect_program_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
395 | { | |
396 | struct rpc_cb_data *data = private_data; | |
397 | ||
398 | assert(rpc->magic == RPC_CONTEXT_MAGIC); | |
399 | ||
400 | /* Dont want any more callbacks even if the socket is closed */ | |
401 | rpc->connect_cb = NULL; | |
402 | ||
403 | if (status == RPC_STATUS_ERROR) { | |
404 | data->cb(rpc, status, command_data, data->private_data); | |
405 | free_rpc_cb_data(data); | |
406 | return; | |
407 | } | |
408 | if (status == RPC_STATUS_CANCEL) { | |
409 | data->cb(rpc, status, "Command was cancelled", data->private_data); | |
410 | free_rpc_cb_data(data); | |
411 | return; | |
412 | } | |
413 | ||
414 | data->cb(rpc, status, NULL, data->private_data); | |
415 | free_rpc_cb_data(data); | |
416 | } | |
417 | ||
418 | static void rpc_connect_program_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
419 | { | |
420 | struct rpc_cb_data *data = private_data; | |
421 | uint32_t rpc_port; | |
422 | ||
423 | assert(rpc->magic == RPC_CONTEXT_MAGIC); | |
424 | ||
4b1ae88a | 425 | if (status == RPC_STATUS_ERROR) { |
8733f38d RS |
426 | data->cb(rpc, status, command_data, data->private_data); |
427 | free_rpc_cb_data(data); | |
428 | return; | |
429 | } | |
430 | if (status == RPC_STATUS_CANCEL) { | |
431 | data->cb(rpc, status, "Command was cancelled", data->private_data); | |
432 | free_rpc_cb_data(data); | |
433 | return; | |
434 | } | |
435 | ||
436 | rpc_port = *(uint32_t *)command_data; | |
437 | if (rpc_port == 0) { | |
438 | rpc_set_error(rpc, "RPC error. Program is not available on %s", data->server); | |
439 | data->cb(rpc, RPC_STATUS_ERROR, rpc_get_error(rpc), data->private_data); | |
440 | free_rpc_cb_data(data); | |
441 | return; | |
442 | } | |
443 | ||
444 | rpc_disconnect(rpc, "normal disconnect"); | |
445 | if (rpc_connect_async(rpc, data->server, rpc_port, rpc_connect_program_4_cb, data) != 0) { | |
446 | data->cb(rpc, status, command_data, data->private_data); | |
447 | free_rpc_cb_data(data); | |
448 | return; | |
449 | } | |
450 | } | |
451 | ||
452 | static void rpc_connect_program_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
453 | { | |
454 | struct rpc_cb_data *data = private_data; | |
455 | ||
456 | assert(rpc->magic == RPC_CONTEXT_MAGIC); | |
457 | ||
458 | if (status == RPC_STATUS_ERROR) { | |
459 | data->cb(rpc, status, command_data, data->private_data); | |
460 | free_rpc_cb_data(data); | |
461 | return; | |
462 | } | |
463 | if (status == RPC_STATUS_CANCEL) { | |
464 | data->cb(rpc, status, "Command was cancelled", data->private_data); | |
465 | free_rpc_cb_data(data); | |
466 | return; | |
467 | } | |
468 | ||
469 | if (rpc_pmap_getport_async(rpc, data->program, data->version, IPPROTO_TCP, rpc_connect_program_3_cb, private_data) != 0) { | |
470 | data->cb(rpc, status, command_data, data->private_data); | |
471 | free_rpc_cb_data(data); | |
472 | return; | |
473 | } | |
474 | } | |
475 | ||
476 | static void rpc_connect_program_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
477 | { | |
478 | struct rpc_cb_data *data = private_data; | |
479 | ||
480 | assert(rpc->magic == RPC_CONTEXT_MAGIC); | |
481 | ||
482 | /* Dont want any more callbacks even if the socket is closed */ | |
483 | rpc->connect_cb = NULL; | |
484 | ||
485 | if (status == RPC_STATUS_ERROR) { | |
486 | data->cb(rpc, status, command_data, data->private_data); | |
487 | free_rpc_cb_data(data); | |
488 | return; | |
489 | } | |
490 | if (status == RPC_STATUS_CANCEL) { | |
491 | data->cb(rpc, status, "Command was cancelled", data->private_data); | |
492 | free_rpc_cb_data(data); | |
493 | return; | |
494 | } | |
495 | ||
496 | if (rpc_pmap_null_async(rpc, rpc_connect_program_2_cb, data) != 0) { | |
497 | data->cb(rpc, status, command_data, data->private_data); | |
498 | free_rpc_cb_data(data); | |
499 | return; | |
500 | } | |
501 | } | |
502 | ||
503 | int rpc_connect_program_async(struct rpc_context *rpc, char *server, int program, int version, rpc_cb cb, void *private_data) | |
504 | { | |
505 | struct rpc_cb_data *data; | |
506 | ||
507 | data = malloc(sizeof(struct rpc_cb_data)); | |
508 | if (data == NULL) { | |
509 | return -1; | |
510 | } | |
511 | memset(data, 0, sizeof(struct rpc_cb_data)); | |
512 | data->server = strdup(server); | |
513 | data->program = program; | |
514 | data->version = version; | |
515 | ||
516 | data->cb = cb; | |
517 | data->private_data = private_data; | |
518 | ||
519 | if (rpc_connect_async(rpc, server, 111, rpc_connect_program_1_cb, data) != 0) { | |
520 | rpc_set_error(rpc, "Failed to start connection"); | |
521 | free_rpc_cb_data(data); | |
522 | return -1; | |
523 | } | |
524 | return 0; | |
525 | } | |
526 | ||
2fa14f9b | 527 | static void free_nfs_cb_data(struct nfs_cb_data *data) |
84004dbf | 528 | { |
84004dbf | 529 | if (data->continue_data != NULL) { |
206a2ff6 | 530 | assert(data->free_continue_data); |
84004dbf | 531 | data->free_continue_data(data->continue_data); |
84004dbf RS |
532 | } |
533 | ||
2fa14f9b AR |
534 | free(data->saved_path); |
535 | free(data->fh.data.data_val); | |
536 | free(data->buffer); | |
921f877b | 537 | |
84004dbf RS |
538 | free(data); |
539 | } | |
540 | ||
541 | ||
542 | ||
543 | ||
544 | ||
f3a75078 | 545 | static void nfs_mount_10_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
546 | { |
547 | struct nfs_cb_data *data = private_data; | |
548 | struct nfs_context *nfs = data->nfs; | |
549 | ||
f3a75078 RS |
550 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
551 | ||
84004dbf RS |
552 | if (status == RPC_STATUS_ERROR) { |
553 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
554 | free_nfs_cb_data(data); | |
555 | return; | |
556 | } | |
557 | if (status == RPC_STATUS_CANCEL) { | |
558 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
559 | free_nfs_cb_data(data); | |
560 | return; | |
561 | } | |
562 | ||
563 | data->cb(0, nfs, NULL, data->private_data); | |
564 | free_nfs_cb_data(data); | |
565 | } | |
566 | ||
17ef62fa | 567 | static void nfs_mount_9_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
568 | { |
569 | struct nfs_cb_data *data = private_data; | |
570 | struct nfs_context *nfs = data->nfs; | |
17ef62fa | 571 | FSINFO3res *res = command_data; |
463d59bf | 572 | struct GETATTR3args args; |
84004dbf | 573 | |
f3a75078 RS |
574 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
575 | ||
84004dbf RS |
576 | if (status == RPC_STATUS_ERROR) { |
577 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
578 | free_nfs_cb_data(data); | |
579 | return; | |
580 | } | |
581 | if (status == RPC_STATUS_CANCEL) { | |
582 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
583 | free_nfs_cb_data(data); | |
584 | return; | |
585 | } | |
586 | ||
17ef62fa RS |
587 | nfs->readmax = res->FSINFO3res_u.resok.rtmax; |
588 | nfs->writemax = res->FSINFO3res_u.resok.wtmax; | |
84004dbf | 589 | |
463d59bf | 590 | memset(&args, 0, sizeof(GETATTR3args)); |
2452c57f | 591 | args.object = nfs->rootfh; |
463d59bf RS |
592 | |
593 | if (rpc_nfs3_getattr_async(rpc, nfs_mount_10_cb, &args, data) != 0) { | |
84004dbf RS |
594 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
595 | free_nfs_cb_data(data); | |
596 | return; | |
597 | } | |
598 | } | |
599 | ||
17ef62fa RS |
600 | static void nfs_mount_8_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
601 | { | |
602 | struct nfs_cb_data *data = private_data; | |
603 | struct nfs_context *nfs = data->nfs; | |
c4ba61c5 | 604 | struct FSINFO3args args; |
17ef62fa | 605 | |
f3a75078 RS |
606 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
607 | ||
17ef62fa RS |
608 | if (status == RPC_STATUS_ERROR) { |
609 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
610 | free_nfs_cb_data(data); | |
611 | return; | |
612 | } | |
613 | if (status == RPC_STATUS_CANCEL) { | |
614 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
615 | free_nfs_cb_data(data); | |
616 | return; | |
617 | } | |
618 | ||
c4ba61c5 RS |
619 | args.fsroot = nfs->rootfh; |
620 | if (rpc_nfs3_fsinfo_async(rpc, nfs_mount_9_cb, &args, data) != 0) { | |
17ef62fa RS |
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 | ||
dc3ed8c3 | 649 | if (rpc_nfs3_null_async(rpc, nfs_mount_8_cb, data) != 0) { |
84004dbf RS |
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 | ||
eda77ec3 | 723 | if (rpc_mount3_mnt_async(rpc, nfs_mount_6_cb, nfs->export, data) != 0) { |
84004dbf RS |
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 | ||
eda77ec3 | 751 | if (rpc_mount3_null_async(rpc, nfs_mount_5_cb, data) != 0) { |
84004dbf RS |
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 | ||
4b1ae88a | 766 | if (status == RPC_STATUS_ERROR) { |
84004dbf RS |
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 | { | |
f893b680 | 927 | char *path, *slash; |
463d59bf | 928 | LOOKUP3args args; |
84004dbf RS |
929 | |
930 | while (*data->path == '/') { | |
931 | data->path++; | |
932 | } | |
933 | ||
934 | path = data->path; | |
f893b680 RS |
935 | slash = strchr(path, '/'); |
936 | if (slash != NULL) { | |
937 | /* Clear slash so that path is a zero terminated string for | |
938 | * the current path component. Set it back to '/' again later | |
939 | * when we are finished referencing this component so that | |
940 | * data->saved_path will still point to the full | |
941 | * normalized path. | |
942 | */ | |
943 | *slash = 0; | |
944 | data->path = slash+1; | |
84004dbf RS |
945 | } else { |
946 | while (*data->path != 0) { | |
947 | data->path++; | |
948 | } | |
949 | } | |
950 | ||
951 | if (*path == 0) { | |
952 | data->fh.data.data_len = fh->data.data_len; | |
953 | data->fh.data.data_val = malloc(data->fh.data.data_len); | |
954 | if (data->fh.data.data_val == NULL) { | |
955 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh for %s", data->path); | |
956 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
957 | free_nfs_cb_data(data); | |
958 | return -1; | |
959 | } | |
960 | memcpy(data->fh.data.data_val, fh->data.data_val, data->fh.data.data_len); | |
f893b680 RS |
961 | if (slash != NULL) { |
962 | *slash = '/'; | |
963 | } | |
84004dbf RS |
964 | data->continue_cb(nfs, data); |
965 | return 0; | |
966 | } | |
967 | ||
463d59bf RS |
968 | |
969 | memset(&args, 0, sizeof(LOOKUP3args)); | |
2452c57f RS |
970 | args.what.dir = *fh; |
971 | args.what.name = path; | |
463d59bf RS |
972 | |
973 | if (rpc_nfs3_lookup_async(nfs->rpc, nfs_lookup_path_1_cb, &args, data) != 0) { | |
84004dbf RS |
974 | rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s", data->path); |
975 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
976 | free_nfs_cb_data(data); | |
977 | return -1; | |
978 | } | |
f893b680 RS |
979 | if (slash != NULL) { |
980 | *slash = '/'; | |
981 | } | |
982 | return 0; | |
983 | } | |
984 | ||
985 | static int nfs_normalize_path(struct nfs_context *nfs, char *path) | |
986 | { | |
987 | char *str; | |
988 | int len; | |
989 | ||
990 | /* // -> / */ | |
4b1ae88a | 991 | while ((str = strstr(path, "//"))) { |
f893b680 RS |
992 | while(*str) { |
993 | *str = *(str + 1); | |
994 | str++; | |
995 | } | |
996 | } | |
997 | ||
998 | /* /./ -> / */ | |
4b1ae88a | 999 | while ((str = strstr(path, "/./"))) { |
f893b680 RS |
1000 | while(*(str + 1)) { |
1001 | *str = *(str + 2); | |
1002 | str++; | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | /* ^/../ -> error */ | |
1007 | if (!strncmp(path, "/../", 4)) { | |
1008 | rpc_set_error(nfs->rpc, | |
1009 | "Absolute path starts with '/../' " | |
1010 | "during normalization"); | |
1011 | return -1; | |
1012 | } | |
1013 | ||
1014 | /* ^[^/] -> error */ | |
1015 | if (path[0] != '/') { | |
1016 | rpc_set_error(nfs->rpc, | |
1017 | "Absolute path does not start with '/'"); | |
1018 | return -1; | |
1019 | } | |
1020 | ||
1021 | /* /string/../ -> / */ | |
4b1ae88a | 1022 | while ((str = strstr(path, "/../"))) { |
f893b680 RS |
1023 | char *tmp; |
1024 | ||
1025 | if (!strncmp(path, "/../", 4)) { | |
1026 | rpc_set_error(nfs->rpc, | |
1027 | "Absolute path starts with '/../' " | |
1028 | "during normalization"); | |
1029 | return -1; | |
1030 | } | |
1031 | ||
1032 | tmp = str - 1; | |
1033 | while (*tmp != '/') { | |
1034 | tmp--; | |
1035 | } | |
1036 | str += 3; | |
1037 | while((*(tmp++) = *(str++)) != '\0') | |
1038 | ; | |
1039 | } | |
1040 | ||
1041 | /* /$ -> \0 */ | |
1042 | len = strlen(path); | |
1043 | if (len >= 1) { | |
1044 | if (path[len - 1] == '/') { | |
1045 | path[len - 1] = '\0'; | |
1046 | len--; | |
1047 | } | |
1048 | } | |
1049 | if (path[0] == '\0') { | |
1050 | rpc_set_error(nfs->rpc, | |
1051 | "Absolute path became '' " | |
1052 | "during normalization"); | |
1053 | return -1; | |
1054 | } | |
1055 | ||
1056 | /* /.$ -> \0 */ | |
1057 | if (len >= 2) { | |
1058 | if (!strcmp(&path[len - 2], "/.")) { | |
1059 | path[len - 2] = '\0'; | |
1060 | len -= 2; | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | /* ^/..$ -> error */ | |
1065 | if (!strcmp(path, "/..")) { | |
1066 | rpc_set_error(nfs->rpc, | |
1067 | "Absolute path is '/..' " | |
1068 | "during normalization"); | |
1069 | return -1; | |
1070 | } | |
1071 | ||
1072 | /* /string/..$ -> / */ | |
1073 | if (len >= 3) { | |
1074 | if (!strcmp(&path[len - 3], "/..")) { | |
1075 | char *tmp = &path[len - 3]; | |
1076 | while (*--tmp != '/') | |
1077 | ; | |
4b1ae88a | 1078 | *tmp = '\0'; |
f893b680 RS |
1079 | } |
1080 | } | |
1081 | ||
84004dbf RS |
1082 | return 0; |
1083 | } | |
1084 | ||
1085 | 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) | |
1086 | { | |
1087 | struct nfs_cb_data *data; | |
1088 | ||
f893b680 | 1089 | if (path[0] == '\0') { |
34edf264 | 1090 | path = "."; |
84004dbf RS |
1091 | } |
1092 | ||
1093 | data = malloc(sizeof(struct nfs_cb_data)); | |
1094 | if (data == NULL) { | |
f893b680 RS |
1095 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate " |
1096 | "nfs_cb_data structure"); | |
e59193c9 AR |
1097 | if (free_continue_data) |
1098 | free_continue_data(continue_data); | |
cbbf9d3e | 1099 | return -1; |
84004dbf | 1100 | } |
ea98629a | 1101 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
1102 | data->nfs = nfs; |
1103 | data->cb = cb; | |
1104 | data->continue_cb = continue_cb; | |
1105 | data->continue_data = continue_data; | |
1106 | data->free_continue_data = free_continue_data; | |
1107 | data->continue_int = continue_int; | |
1108 | data->private_data = private_data; | |
f893b680 RS |
1109 | if (path[0] == '/') { |
1110 | data->saved_path = strdup(path); | |
1111 | } else { | |
1112 | data->saved_path = malloc(strlen(path) + strlen(nfs->cwd) + 2); | |
1113 | if (data->saved_path == NULL) { | |
1114 | rpc_set_error(nfs->rpc, "out of memory: failed to " | |
1115 | "malloc path string"); | |
1116 | free_nfs_cb_data(data); | |
1117 | return -1; | |
1118 | } | |
1119 | sprintf(data->saved_path, "%s/%s", nfs->cwd, path); | |
1120 | } | |
1121 | ||
84004dbf RS |
1122 | if (data->saved_path == NULL) { |
1123 | rpc_set_error(nfs->rpc, "out of memory: failed to copy path string"); | |
84004dbf | 1124 | free_nfs_cb_data(data); |
cbbf9d3e | 1125 | return -1; |
84004dbf | 1126 | } |
f893b680 RS |
1127 | if (nfs_normalize_path(nfs, data->saved_path) != 0) { |
1128 | free_nfs_cb_data(data); | |
1129 | return -1; | |
1130 | } | |
1131 | ||
84004dbf RS |
1132 | data->path = data->saved_path; |
1133 | ||
1134 | if (nfs_lookup_path_async_internal(nfs, data, &nfs->rootfh) != 0) { | |
84004dbf RS |
1135 | /* return 0 here since the callback will be invoked if there is a failure */ |
1136 | return 0; | |
1137 | } | |
1138 | return 0; | |
1139 | } | |
1140 | ||
1141 | ||
1142 | ||
1143 | ||
1144 | ||
1145 | /* | |
1146 | * Async stat() | |
1147 | */ | |
f3a75078 | 1148 | static void nfs_stat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
1149 | { |
1150 | GETATTR3res *res; | |
1151 | struct nfs_cb_data *data = private_data; | |
1152 | struct nfs_context *nfs = data->nfs; | |
be184101 M |
1153 | #ifdef WIN32 |
1154 | struct __stat64 st; | |
1155 | #else | |
84004dbf | 1156 | struct stat st; |
be184101 | 1157 | #endif |
84004dbf | 1158 | |
f3a75078 RS |
1159 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1160 | ||
84004dbf RS |
1161 | if (status == RPC_STATUS_ERROR) { |
1162 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1163 | free_nfs_cb_data(data); | |
1164 | return; | |
1165 | } | |
1166 | if (status == RPC_STATUS_CANCEL) { | |
1167 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1168 | free_nfs_cb_data(data); | |
1169 | return; | |
1170 | } | |
1171 | ||
1172 | res = command_data; | |
1173 | if (res->status != NFS3_OK) { | |
1174 | 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)); | |
1175 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1176 | free_nfs_cb_data(data); | |
1177 | return; | |
1178 | } | |
1179 | ||
1180 | st.st_dev = -1; | |
1181 | st.st_ino = res->GETATTR3res_u.resok.obj_attributes.fileid; | |
1182 | st.st_mode = res->GETATTR3res_u.resok.obj_attributes.mode; | |
e8cab72a RS |
1183 | if (res->GETATTR3res_u.resok.obj_attributes.type == NF3DIR) { |
1184 | st.st_mode |= S_IFDIR ; | |
1185 | } | |
d7ec001f RS |
1186 | if (res->GETATTR3res_u.resok.obj_attributes.type == NF3REG) { |
1187 | st.st_mode |= S_IFREG ; | |
1188 | } | |
84004dbf RS |
1189 | st.st_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink; |
1190 | st.st_uid = res->GETATTR3res_u.resok.obj_attributes.uid; | |
1191 | st.st_gid = res->GETATTR3res_u.resok.obj_attributes.gid; | |
1192 | st.st_rdev = 0; | |
1193 | st.st_size = res->GETATTR3res_u.resok.obj_attributes.size; | |
a8a1b858 | 1194 | #ifndef WIN32 |
84004dbf RS |
1195 | st.st_blksize = 4096; |
1196 | st.st_blocks = res->GETATTR3res_u.resok.obj_attributes.size / 4096; | |
4b1ae88a | 1197 | #endif//WIN32 |
84004dbf RS |
1198 | st.st_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds; |
1199 | st.st_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds; | |
1200 | st.st_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds; | |
1201 | ||
1202 | data->cb(0, nfs, &st, data->private_data); | |
1203 | free_nfs_cb_data(data); | |
1204 | } | |
1205 | ||
1206 | static int nfs_stat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1207 | { | |
463d59bf RS |
1208 | struct GETATTR3args args; |
1209 | ||
1210 | memset(&args, 0, sizeof(GETATTR3args)); | |
2452c57f | 1211 | args.object = data->fh; |
463d59bf RS |
1212 | |
1213 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_stat_1_cb, &args, data) != 0) { | |
84004dbf RS |
1214 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); |
1215 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1216 | free_nfs_cb_data(data); | |
1217 | return -1; | |
1218 | } | |
1219 | return 0; | |
1220 | } | |
1221 | ||
1222 | int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
1223 | { | |
1224 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_stat_continue_internal, NULL, NULL, 0) != 0) { | |
cbbf9d3e | 1225 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
1226 | return -1; |
1227 | } | |
1228 | ||
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | ||
479302f7 RS |
1233 | /* |
1234 | * Async nfs_stat64() | |
1235 | */ | |
1236 | static void nfs_stat64_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
1237 | { | |
1238 | GETATTR3res *res; | |
1239 | struct nfs_cb_data *data = private_data; | |
1240 | struct nfs_context *nfs = data->nfs; | |
1241 | struct nfs_stat_64 st; | |
1242 | ||
1243 | assert(rpc->magic == RPC_CONTEXT_MAGIC); | |
1244 | ||
1245 | if (status == RPC_STATUS_ERROR) { | |
1246 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1247 | free_nfs_cb_data(data); | |
1248 | return; | |
1249 | } | |
1250 | if (status == RPC_STATUS_CANCEL) { | |
1251 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1252 | free_nfs_cb_data(data); | |
1253 | return; | |
1254 | } | |
1255 | ||
1256 | res = command_data; | |
1257 | if (res->status != NFS3_OK) { | |
1258 | 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)); | |
1259 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1260 | free_nfs_cb_data(data); | |
1261 | return; | |
1262 | } | |
1263 | ||
1264 | st.nfs_dev = -1; | |
1265 | st.nfs_ino = res->GETATTR3res_u.resok.obj_attributes.fileid; | |
1266 | st.nfs_mode = res->GETATTR3res_u.resok.obj_attributes.mode; | |
1267 | if (res->GETATTR3res_u.resok.obj_attributes.type == NF3DIR) { | |
1268 | st.nfs_mode |= S_IFDIR ; | |
1269 | } | |
1270 | if (res->GETATTR3res_u.resok.obj_attributes.type == NF3REG) { | |
1271 | st.nfs_mode |= S_IFREG ; | |
1272 | } | |
1273 | st.nfs_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink; | |
1274 | st.nfs_uid = res->GETATTR3res_u.resok.obj_attributes.uid; | |
1275 | st.nfs_gid = res->GETATTR3res_u.resok.obj_attributes.gid; | |
1276 | st.nfs_rdev = 0; | |
1277 | st.nfs_size = res->GETATTR3res_u.resok.obj_attributes.size; | |
1278 | st.nfs_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds; | |
1279 | st.nfs_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds; | |
1280 | st.nfs_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds; | |
1281 | ||
1282 | data->cb(0, nfs, &st, data->private_data); | |
1283 | free_nfs_cb_data(data); | |
1284 | } | |
1285 | ||
1286 | static int nfs_stat64_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1287 | { | |
1288 | struct GETATTR3args args; | |
1289 | ||
1290 | memset(&args, 0, sizeof(GETATTR3args)); | |
1291 | args.object = data->fh; | |
1292 | ||
1293 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_stat64_1_cb, &args, data) != 0) { | |
1294 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); | |
1295 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1296 | free_nfs_cb_data(data); | |
1297 | return -1; | |
1298 | } | |
1299 | return 0; | |
1300 | } | |
1301 | ||
1302 | int nfs_stat64_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
1303 | { | |
1304 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_stat64_continue_internal, NULL, NULL, 0) != 0) { | |
1305 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); | |
1306 | return -1; | |
1307 | } | |
1308 | ||
1309 | return 0; | |
1310 | } | |
84004dbf | 1311 | |
84004dbf RS |
1312 | /* |
1313 | * Async open() | |
1314 | */ | |
22a0f15b RS |
1315 | static void nfs_open_trunc_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
1316 | { | |
1317 | struct nfs_cb_data *data = private_data; | |
1318 | struct nfs_context *nfs = data->nfs; | |
be7b4360 | 1319 | struct nfsfh *nfsfh; |
22a0f15b RS |
1320 | SETATTR3res *res; |
1321 | ||
1322 | assert(rpc->magic == RPC_CONTEXT_MAGIC); | |
1323 | ||
1324 | if (status == RPC_STATUS_ERROR) { | |
1325 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1326 | free_nfs_cb_data(data); | |
1327 | return; | |
1328 | } | |
1329 | if (status == RPC_STATUS_CANCEL) { | |
1330 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1331 | free_nfs_cb_data(data); | |
1332 | return; | |
1333 | } | |
1334 | ||
1335 | res = command_data; | |
1336 | if (res->status != NFS3_OK) { | |
1337 | rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1338 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1339 | free_nfs_cb_data(data); | |
1340 | return; | |
1341 | } | |
1342 | ||
be7b4360 RS |
1343 | nfsfh = malloc(sizeof(struct nfsfh)); |
1344 | if (nfsfh == NULL) { | |
1345 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); | |
1346 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1347 | free_nfs_cb_data(data); | |
1348 | return; | |
1349 | } | |
1350 | memset(nfsfh, 0, sizeof(struct nfsfh)); | |
1351 | ||
1352 | if (data->continue_int & O_SYNC) { | |
1353 | nfsfh->is_sync = 1; | |
1354 | } | |
1355 | ||
1356 | /* steal the filehandle */ | |
1357 | nfsfh->fh = data->fh; | |
1358 | data->fh.data.data_val = NULL; | |
1359 | ||
1360 | data->cb(0, nfs, nfsfh, data->private_data); | |
22a0f15b RS |
1361 | free_nfs_cb_data(data); |
1362 | } | |
1363 | ||
f3a75078 | 1364 | static void nfs_open_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
1365 | { |
1366 | ACCESS3res *res; | |
1367 | struct nfs_cb_data *data = private_data; | |
1368 | struct nfs_context *nfs = data->nfs; | |
1369 | struct nfsfh *nfsfh; | |
1370 | unsigned int nfsmode = 0; | |
1371 | ||
f3a75078 RS |
1372 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1373 | ||
84004dbf RS |
1374 | if (status == RPC_STATUS_ERROR) { |
1375 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1376 | free_nfs_cb_data(data); | |
1377 | return; | |
1378 | } | |
1379 | if (status == RPC_STATUS_CANCEL) { | |
1380 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1381 | free_nfs_cb_data(data); | |
1382 | return; | |
1383 | } | |
1384 | ||
1385 | res = command_data; | |
1386 | if (res->status != NFS3_OK) { | |
1387 | 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)); | |
1388 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1389 | free_nfs_cb_data(data); | |
1390 | return; | |
1391 | } | |
1392 | ||
1393 | if (data->continue_int & O_WRONLY) { | |
1394 | nfsmode |= ACCESS3_MODIFY; | |
1395 | } | |
1396 | if (data->continue_int & O_RDWR) { | |
1397 | nfsmode |= ACCESS3_READ|ACCESS3_MODIFY; | |
1398 | } | |
1399 | if (!(data->continue_int & (O_WRONLY|O_RDWR))) { | |
1400 | nfsmode |= ACCESS3_READ; | |
1401 | } | |
1402 | ||
1403 | ||
1404 | if (res->ACCESS3res_u.resok.access != nfsmode) { | |
1405 | rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c", | |
1406 | nfsmode&ACCESS3_READ?'r':'-', | |
1407 | nfsmode&ACCESS3_MODIFY?'w':'-', | |
1408 | nfsmode&ACCESS3_EXECUTE?'x':'-', | |
1409 | res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-', | |
1410 | res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-', | |
1411 | res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-'); | |
1412 | data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1413 | free_nfs_cb_data(data); | |
1414 | return; | |
1415 | } | |
1416 | ||
22a0f15b RS |
1417 | /* Try to truncate it if we were requested to */ |
1418 | if ((data->continue_int & O_TRUNC) && | |
1419 | (data->continue_int & (O_RDWR|O_WRONLY))) { | |
1420 | SETATTR3args args; | |
1421 | ||
1422 | memset(&args, 0, sizeof(SETATTR3args)); | |
be7b4360 | 1423 | args.object = data->fh; |
22a0f15b RS |
1424 | args.new_attributes.size.set_it = 1; |
1425 | args.new_attributes.size.set_size3_u.size = 0; | |
1426 | ||
1427 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_open_trunc_cb, &args, | |
1428 | data) != 0) { | |
1429 | rpc_set_error(nfs->rpc, "RPC error: Failed to send " | |
1430 | "SETATTR call for %s", data->path); | |
1431 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), | |
1432 | data->private_data); | |
1433 | free_nfs_cb_data(data); | |
1434 | return; | |
1435 | } | |
1436 | return; | |
1437 | } | |
1438 | ||
be7b4360 RS |
1439 | nfsfh = malloc(sizeof(struct nfsfh)); |
1440 | if (nfsfh == NULL) { | |
1441 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); | |
1442 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1443 | free_nfs_cb_data(data); | |
1444 | return; | |
1445 | } | |
1446 | memset(nfsfh, 0, sizeof(struct nfsfh)); | |
1447 | ||
1448 | if (data->continue_int & O_SYNC) { | |
1449 | nfsfh->is_sync = 1; | |
1450 | } | |
1451 | ||
1452 | /* steal the filehandle */ | |
1453 | nfsfh->fh = data->fh; | |
1454 | data->fh.data.data_val = NULL; | |
1455 | ||
84004dbf RS |
1456 | data->cb(0, nfs, nfsfh, data->private_data); |
1457 | free_nfs_cb_data(data); | |
1458 | } | |
1459 | ||
1460 | static int nfs_open_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1461 | { | |
1462 | int nfsmode = 0; | |
463d59bf | 1463 | ACCESS3args args; |
84004dbf RS |
1464 | |
1465 | if (data->continue_int & O_WRONLY) { | |
1466 | nfsmode |= ACCESS3_MODIFY; | |
1467 | } | |
1468 | if (data->continue_int & O_RDWR) { | |
1469 | nfsmode |= ACCESS3_READ|ACCESS3_MODIFY; | |
1470 | } | |
1471 | if (!(data->continue_int & (O_WRONLY|O_RDWR))) { | |
1472 | nfsmode |= ACCESS3_READ; | |
1473 | } | |
1474 | ||
463d59bf | 1475 | memset(&args, 0, sizeof(ACCESS3args)); |
2452c57f | 1476 | args.object = data->fh; |
463d59bf RS |
1477 | args.access = nfsmode; |
1478 | ||
1479 | if (rpc_nfs3_access_async(nfs->rpc, nfs_open_cb, &args, data) != 0) { | |
22a0f15b RS |
1480 | rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS " |
1481 | "call for %s", data->path); | |
1482 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), | |
1483 | data->private_data); | |
84004dbf RS |
1484 | free_nfs_cb_data(data); |
1485 | return -1; | |
1486 | } | |
1487 | return 0; | |
1488 | } | |
1489 | ||
3cea44dc | 1490 | int nfs_open_async(struct nfs_context *nfs, const char *path, int flags, nfs_cb cb, void *private_data) |
84004dbf | 1491 | { |
3cea44dc | 1492 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_open_continue_internal, NULL, NULL, flags) != 0) { |
cbbf9d3e RS |
1493 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
1494 | return -1; | |
84004dbf RS |
1495 | } |
1496 | ||
1497 | return 0; | |
1498 | } | |
1499 | ||
1500 | ||
f893b680 RS |
1501 | /* |
1502 | * Async chdir() | |
1503 | */ | |
1504 | static int nfs_chdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1505 | { | |
1506 | /* steal saved_path */ | |
1507 | free(nfs->cwd); | |
1508 | nfs->cwd = data->saved_path; | |
1509 | data->saved_path = NULL; | |
84004dbf | 1510 | |
f893b680 RS |
1511 | data->cb(0, nfs, NULL, data->private_data); |
1512 | free_nfs_cb_data(data); | |
1513 | ||
1514 | return 0; | |
1515 | } | |
1516 | ||
1517 | int nfs_chdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
1518 | { | |
1519 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chdir_continue_internal, NULL, NULL, 0) != 0) { | |
1520 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); | |
1521 | return -1; | |
1522 | } | |
1523 | ||
1524 | return 0; | |
1525 | } | |
84004dbf RS |
1526 | |
1527 | ||
1528 | /* | |
1529 | * Async pread() | |
1530 | */ | |
f3a75078 | 1531 | static void nfs_pread_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
1532 | { |
1533 | struct nfs_cb_data *data = private_data; | |
1534 | struct nfs_context *nfs = data->nfs; | |
1535 | READ3res *res; | |
1536 | ||
f3a75078 RS |
1537 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1538 | ||
84004dbf RS |
1539 | if (status == RPC_STATUS_ERROR) { |
1540 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1541 | free_nfs_cb_data(data); | |
1542 | return; | |
1543 | } | |
1544 | if (status == RPC_STATUS_CANCEL) { | |
1545 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1546 | free_nfs_cb_data(data); | |
1547 | return; | |
1548 | } | |
1549 | ||
1550 | res = command_data; | |
1551 | if (res->status != NFS3_OK) { | |
1552 | rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1553 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1554 | free_nfs_cb_data(data); | |
1555 | return; | |
1556 | } | |
1557 | ||
1558 | data->nfsfh->offset += res->READ3res_u.resok.count; | |
1559 | data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_data); | |
1560 | free_nfs_cb_data(data); | |
1561 | } | |
1562 | ||
f3a75078 | 1563 | static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
921f877b RS |
1564 | { |
1565 | struct nfs_mcb_data *mdata = private_data; | |
1566 | struct nfs_cb_data *data = mdata->data; | |
1567 | struct nfs_context *nfs = data->nfs; | |
1568 | READ3res *res; | |
1569 | ||
f3a75078 RS |
1570 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1571 | ||
921f877b RS |
1572 | data->num_calls--; |
1573 | ||
1574 | if (status == RPC_STATUS_ERROR) { | |
1575 | /* flag the failure but do not invoke callback until we have received all responses */ | |
1576 | data->error = 1; | |
1577 | } | |
1578 | if (status == RPC_STATUS_CANCEL) { | |
1579 | /* flag the cancellation but do not invoke callback until we have received all responses */ | |
1580 | data->cancel = 1; | |
1581 | } | |
1582 | ||
1583 | /* reassemble the data into the buffer */ | |
1584 | if (status == RPC_STATUS_SUCCESS) { | |
1585 | res = command_data; | |
1586 | if (res->status != NFS3_OK) { | |
1587 | rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1588 | data->error = 1; | |
e4a5ba42 RS |
1589 | } else { |
1590 | if (res->READ3res_u.resok.count > 0) { | |
1591 | memcpy(&data->buffer[mdata->offset - data->start_offset], res->READ3res_u.resok.data.data_val, res->READ3res_u.resok.count); | |
1592 | if ((unsigned)data->max_offset < mdata->offset + res->READ3res_u.resok.count) { | |
1593 | data->max_offset = mdata->offset + res->READ3res_u.resok.count; | |
1594 | } | |
921f877b RS |
1595 | } |
1596 | } | |
1597 | } | |
1598 | ||
1599 | if (data->num_calls > 0) { | |
1600 | /* still waiting for more replies */ | |
1601 | free(mdata); | |
1602 | return; | |
1603 | } | |
1604 | ||
921f877b RS |
1605 | if (data->error != 0) { |
1606 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1607 | free_nfs_cb_data(data); | |
1608 | free(mdata); | |
1609 | return; | |
1610 | } | |
1611 | if (data->cancel != 0) { | |
1612 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1613 | free_nfs_cb_data(data); | |
1614 | free(mdata); | |
1615 | return; | |
1616 | } | |
1617 | ||
1618 | data->nfsfh->offset = data->max_offset; | |
7ed9d87a RS |
1619 | data->cb(data->max_offset - data->start_offset, nfs, data->buffer, data->private_data); |
1620 | ||
921f877b RS |
1621 | free_nfs_cb_data(data); |
1622 | free(mdata); | |
1623 | } | |
1624 | ||
183451cf | 1625 | 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 |
1626 | { |
1627 | struct nfs_cb_data *data; | |
1628 | ||
1629 | data = malloc(sizeof(struct nfs_cb_data)); | |
1630 | if (data == NULL) { | |
1631 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
1632 | return -1; |
1633 | } | |
ea98629a | 1634 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
1635 | data->nfs = nfs; |
1636 | data->cb = cb; | |
1637 | data->private_data = private_data; | |
1638 | data->nfsfh = nfsfh; | |
1639 | ||
1640 | nfsfh->offset = offset; | |
921f877b RS |
1641 | |
1642 | if (count <= nfs_get_readmax(nfs)) { | |
463d59bf RS |
1643 | READ3args args; |
1644 | ||
1645 | memset(&args, 0, sizeof(READ3args)); | |
2452c57f | 1646 | args.file = nfsfh->fh; |
463d59bf RS |
1647 | args.offset = offset; |
1648 | args.count = count; | |
1649 | ||
1650 | if (rpc_nfs3_read_async(nfs->rpc, nfs_pread_cb, &args, data) != 0) { | |
921f877b RS |
1651 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path); |
1652 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1653 | free_nfs_cb_data(data); | |
1654 | return -1; | |
1655 | } | |
1656 | return 0; | |
1657 | } | |
1658 | ||
c8f0bac1 AR |
1659 | assert(count > 0); |
1660 | assert(data->num_calls == 0); | |
1661 | ||
921f877b RS |
1662 | /* trying to read more than maximum server read size, we has to chop it up into smaller |
1663 | * reads and collect into a reassembly buffer. | |
cd67ec7d | 1664 | * we send all reads in parallel so that performance is still good. |
921f877b | 1665 | */ |
7ed9d87a | 1666 | data->max_offset = offset; |
921f877b RS |
1667 | data->start_offset = offset; |
1668 | ||
1669 | data->buffer = malloc(count); | |
1670 | if (data->buffer == NULL) { | |
1671 | rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count); | |
84004dbf RS |
1672 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
1673 | free_nfs_cb_data(data); | |
1674 | return -1; | |
1675 | } | |
921f877b RS |
1676 | |
1677 | while (count > 0) { | |
183451cf | 1678 | uint64_t readcount = count; |
921f877b | 1679 | struct nfs_mcb_data *mdata; |
463d59bf | 1680 | READ3args args; |
921f877b RS |
1681 | |
1682 | if (readcount > nfs_get_readmax(nfs)) { | |
1683 | readcount = nfs_get_readmax(nfs); | |
1684 | } | |
1685 | ||
1686 | mdata = malloc(sizeof(struct nfs_mcb_data)); | |
1687 | if (mdata == NULL) { | |
1688 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure"); | |
c8f0bac1 AR |
1689 | if (data->num_calls == 0) |
1690 | free_nfs_cb_data(data); | |
921f877b RS |
1691 | return -1; |
1692 | } | |
ea98629a | 1693 | memset(mdata, 0, sizeof(struct nfs_mcb_data)); |
921f877b RS |
1694 | mdata->data = data; |
1695 | mdata->offset = offset; | |
1696 | mdata->count = readcount; | |
463d59bf RS |
1697 | |
1698 | memset(&args, 0, sizeof(READ3args)); | |
2452c57f | 1699 | args.file = nfsfh->fh; |
463d59bf RS |
1700 | args.offset = offset; |
1701 | args.count = readcount; | |
1702 | ||
1703 | if (rpc_nfs3_read_async(nfs->rpc, nfs_pread_mcb, &args, mdata) != 0) { | |
921f877b RS |
1704 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path); |
1705 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1706 | free(mdata); | |
c8f0bac1 AR |
1707 | if (data->num_calls == 0) |
1708 | free_nfs_cb_data(data); | |
921f877b RS |
1709 | return -1; |
1710 | } | |
1711 | ||
1712 | count -= readcount; | |
1713 | offset += readcount; | |
1714 | data->num_calls++; | |
1715 | } | |
1716 | ||
1717 | return 0; | |
84004dbf RS |
1718 | } |
1719 | ||
1720 | /* | |
1721 | * Async read() | |
1722 | */ | |
183451cf | 1723 | int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, nfs_cb cb, void *private_data) |
84004dbf RS |
1724 | { |
1725 | return nfs_pread_async(nfs, nfsfh, nfsfh->offset, count, cb, private_data); | |
1726 | } | |
1727 | ||
1728 | ||
1729 | ||
1730 | /* | |
1731 | * Async pwrite() | |
1732 | */ | |
f3a75078 | 1733 | static void nfs_pwrite_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
1734 | { |
1735 | struct nfs_cb_data *data = private_data; | |
1736 | struct nfs_context *nfs = data->nfs; | |
1737 | WRITE3res *res; | |
1738 | ||
f3a75078 RS |
1739 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1740 | ||
84004dbf RS |
1741 | if (status == RPC_STATUS_ERROR) { |
1742 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1743 | free_nfs_cb_data(data); | |
1744 | return; | |
1745 | } | |
1746 | if (status == RPC_STATUS_CANCEL) { | |
1747 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1748 | free_nfs_cb_data(data); | |
1749 | return; | |
1750 | } | |
1751 | ||
1752 | res = command_data; | |
1753 | if (res->status != NFS3_OK) { | |
1754 | rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1755 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1756 | free_nfs_cb_data(data); | |
1757 | return; | |
1758 | } | |
1759 | ||
1760 | data->nfsfh->offset += res->WRITE3res_u.resok.count; | |
1761 | data->cb(res->WRITE3res_u.resok.count, nfs, NULL, data->private_data); | |
1762 | free_nfs_cb_data(data); | |
1763 | } | |
1764 | ||
f3a75078 | 1765 | static void nfs_pwrite_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
18c27b73 RS |
1766 | { |
1767 | struct nfs_mcb_data *mdata = private_data; | |
1768 | struct nfs_cb_data *data = mdata->data; | |
1769 | struct nfs_context *nfs = data->nfs; | |
1770 | WRITE3res *res; | |
1771 | ||
f3a75078 RS |
1772 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1773 | ||
18c27b73 RS |
1774 | data->num_calls--; |
1775 | ||
1776 | if (status == RPC_STATUS_ERROR) { | |
1777 | /* flag the failure but do not invoke callback until we have received all responses */ | |
1778 | data->error = 1; | |
1779 | } | |
1780 | if (status == RPC_STATUS_CANCEL) { | |
1781 | /* flag the cancellation but do not invoke callback until we have received all responses */ | |
1782 | data->cancel = 1; | |
1783 | } | |
1784 | ||
1785 | if (status == RPC_STATUS_SUCCESS) { | |
1786 | res = command_data; | |
1787 | if (res->status != NFS3_OK) { | |
1788 | rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1789 | data->error = 1; | |
1790 | } else { | |
1791 | if (res->WRITE3res_u.resok.count > 0) { | |
1792 | if ((unsigned)data->max_offset < mdata->offset + res->WRITE3res_u.resok.count) { | |
1793 | data->max_offset = mdata->offset + res->WRITE3res_u.resok.count; | |
1794 | } | |
1795 | } | |
1796 | } | |
1797 | } | |
1798 | ||
1799 | if (data->num_calls > 0) { | |
1800 | /* still waiting for more replies */ | |
1801 | free(mdata); | |
1802 | return; | |
1803 | } | |
1804 | ||
1805 | if (data->error != 0) { | |
1806 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1807 | free_nfs_cb_data(data); | |
1808 | free(mdata); | |
1809 | return; | |
1810 | } | |
1811 | if (data->cancel != 0) { | |
1812 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1813 | free_nfs_cb_data(data); | |
1814 | free(mdata); | |
1815 | return; | |
1816 | } | |
1817 | ||
1818 | data->nfsfh->offset = data->max_offset; | |
1819 | data->cb(data->max_offset - data->start_offset, nfs, NULL, data->private_data); | |
1820 | ||
1821 | free_nfs_cb_data(data); | |
1822 | free(mdata); | |
1823 | } | |
1824 | ||
1825 | ||
183451cf | 1826 | 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 |
1827 | { |
1828 | struct nfs_cb_data *data; | |
1829 | ||
1830 | data = malloc(sizeof(struct nfs_cb_data)); | |
1831 | if (data == NULL) { | |
1832 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
1833 | return -1; |
1834 | } | |
ea98629a | 1835 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
1836 | data->nfs = nfs; |
1837 | data->cb = cb; | |
1838 | data->private_data = private_data; | |
1839 | data->nfsfh = nfsfh; | |
1840 | ||
1841 | nfsfh->offset = offset; | |
18c27b73 RS |
1842 | |
1843 | if (count <= nfs_get_writemax(nfs)) { | |
463d59bf RS |
1844 | WRITE3args args; |
1845 | ||
1846 | memset(&args, 0, sizeof(WRITE3args)); | |
2452c57f | 1847 | args.file = nfsfh->fh; |
463d59bf RS |
1848 | args.offset = offset; |
1849 | args.count = count; | |
1850 | args.stable = nfsfh->is_sync?FILE_SYNC:UNSTABLE; | |
1851 | args.data.data_len = count; | |
1852 | args.data.data_val = buf; | |
1853 | ||
1854 | if (rpc_nfs3_write_async(nfs->rpc, nfs_pwrite_cb, &args, data) != 0) { | |
18c27b73 RS |
1855 | rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path); |
1856 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1857 | free_nfs_cb_data(data); | |
1858 | return -1; | |
1859 | } | |
1860 | return 0; | |
84004dbf | 1861 | } |
18c27b73 | 1862 | |
63d4a058 AR |
1863 | /* hello, clang-analyzer */ |
1864 | assert(count > 0); | |
1865 | assert(data->num_calls == 0); | |
1866 | ||
18c27b73 RS |
1867 | /* trying to write more than maximum server write size, we has to chop it up into smaller |
1868 | * chunks. | |
cd67ec7d | 1869 | * we send all writes in parallel so that performance is still good. |
18c27b73 RS |
1870 | */ |
1871 | data->max_offset = offset; | |
1872 | data->start_offset = offset; | |
1873 | ||
1874 | while (count > 0) { | |
183451cf | 1875 | uint64_t writecount = count; |
18c27b73 | 1876 | struct nfs_mcb_data *mdata; |
463d59bf | 1877 | WRITE3args args; |
18c27b73 RS |
1878 | |
1879 | if (writecount > nfs_get_writemax(nfs)) { | |
1880 | writecount = nfs_get_writemax(nfs); | |
1881 | } | |
1882 | ||
1883 | mdata = malloc(sizeof(struct nfs_mcb_data)); | |
1884 | if (mdata == NULL) { | |
1885 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure"); | |
63d4a058 AR |
1886 | if (data->num_calls == 0) |
1887 | free_nfs_cb_data(data); | |
18c27b73 RS |
1888 | return -1; |
1889 | } | |
1890 | memset(mdata, 0, sizeof(struct nfs_mcb_data)); | |
1891 | mdata->data = data; | |
1892 | mdata->offset = offset; | |
1893 | mdata->count = writecount; | |
1894 | ||
463d59bf | 1895 | memset(&args, 0, sizeof(WRITE3args)); |
2452c57f | 1896 | args.file = nfsfh->fh; |
463d59bf RS |
1897 | args.offset = offset; |
1898 | args.count = writecount; | |
1899 | args.stable = nfsfh->is_sync?FILE_SYNC:UNSTABLE; | |
1900 | args.data.data_len = writecount; | |
1901 | args.data.data_val = &buf[offset - data->start_offset]; | |
1902 | ||
1903 | if (rpc_nfs3_write_async(nfs->rpc, nfs_pwrite_mcb, &args, mdata) != 0) { | |
18c27b73 RS |
1904 | rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path); |
1905 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1906 | free(mdata); | |
63d4a058 AR |
1907 | if (data->num_calls == 0) |
1908 | free_nfs_cb_data(data); | |
1909 | ||
18c27b73 RS |
1910 | return -1; |
1911 | } | |
1912 | ||
1913 | count -= writecount; | |
1914 | offset += writecount; | |
1915 | data->num_calls++; | |
1916 | } | |
1917 | ||
84004dbf RS |
1918 | return 0; |
1919 | } | |
1920 | ||
1921 | /* | |
1922 | * Async write() | |
1923 | */ | |
183451cf | 1924 | int nfs_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, char *buf, nfs_cb cb, void *private_data) |
84004dbf RS |
1925 | { |
1926 | return nfs_pwrite_async(nfs, nfsfh, nfsfh->offset, count, buf, cb, private_data); | |
1927 | } | |
1928 | ||
1929 | ||
1930 | ||
1931 | ||
1932 | /* | |
1933 | * close | |
1934 | */ | |
4b1ae88a | 1935 | |
84004dbf RS |
1936 | int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) |
1937 | { | |
1938 | if (nfsfh->fh.data.data_val != NULL){ | |
1939 | free(nfsfh->fh.data.data_val); | |
1940 | nfsfh->fh.data.data_val = NULL; | |
1941 | } | |
1942 | free(nfsfh); | |
1943 | ||
1944 | cb(0, nfs, NULL, private_data); | |
1945 | return 0; | |
1946 | }; | |
1947 | ||
1948 | ||
1949 | ||
1950 | ||
1951 | ||
1952 | /* | |
1953 | * Async fstat() | |
1954 | */ | |
1955 | int nfs_fstat_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) | |
1956 | { | |
1957 | struct nfs_cb_data *data; | |
463d59bf | 1958 | struct GETATTR3args args; |
84004dbf RS |
1959 | |
1960 | data = malloc(sizeof(struct nfs_cb_data)); | |
1961 | if (data == NULL) { | |
1962 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
1963 | return -1; |
1964 | } | |
ea98629a | 1965 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
1966 | data->nfs = nfs; |
1967 | data->cb = cb; | |
1968 | data->private_data = private_data; | |
1969 | ||
463d59bf | 1970 | memset(&args, 0, sizeof(GETATTR3args)); |
2452c57f | 1971 | args.object = nfsfh->fh; |
463d59bf RS |
1972 | |
1973 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_stat_1_cb, &args, data) != 0) { | |
84004dbf RS |
1974 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); |
1975 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1976 | free_nfs_cb_data(data); | |
1977 | return -1; | |
1978 | } | |
1979 | return 0; | |
1980 | } | |
1981 | ||
1982 | ||
1983 | ||
1984 | /* | |
1985 | * Async fsync() | |
1986 | */ | |
f3a75078 | 1987 | static void nfs_fsync_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
1988 | { |
1989 | struct nfs_cb_data *data = private_data; | |
1990 | struct nfs_context *nfs = data->nfs; | |
1991 | COMMIT3res *res; | |
1992 | ||
f3a75078 RS |
1993 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1994 | ||
84004dbf RS |
1995 | if (status == RPC_STATUS_ERROR) { |
1996 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1997 | free_nfs_cb_data(data); | |
1998 | return; | |
1999 | } | |
2000 | if (status == RPC_STATUS_CANCEL) { | |
2001 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2002 | free_nfs_cb_data(data); | |
2003 | return; | |
2004 | } | |
2005 | ||
2006 | res = command_data; | |
2007 | if (res->status != NFS3_OK) { | |
2008 | rpc_set_error(nfs->rpc, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
2009 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2010 | free_nfs_cb_data(data); | |
2011 | return; | |
2012 | } | |
2013 | ||
2014 | data->cb(0, nfs, NULL, data->private_data); | |
2015 | free_nfs_cb_data(data); | |
2016 | } | |
2017 | ||
2018 | int nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) | |
2019 | { | |
2020 | struct nfs_cb_data *data; | |
1a5636ff | 2021 | struct COMMIT3args args; |
84004dbf RS |
2022 | |
2023 | data = malloc(sizeof(struct nfs_cb_data)); | |
2024 | if (data == NULL) { | |
2025 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
2026 | return -1; |
2027 | } | |
ea98629a | 2028 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
2029 | data->nfs = nfs; |
2030 | data->cb = cb; | |
2031 | data->private_data = private_data; | |
2032 | ||
1a5636ff RS |
2033 | args.file = nfsfh->fh; |
2034 | args.offset = 0; | |
2035 | args.count = 0; | |
2036 | if (rpc_nfs3_commit_async(nfs->rpc, nfs_fsync_cb, &args, data) != 0) { | |
84004dbf RS |
2037 | rpc_set_error(nfs->rpc, "RPC error: Failed to send COMMIT call for %s", data->path); |
2038 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2039 | free_nfs_cb_data(data); | |
2040 | return -1; | |
2041 | } | |
2042 | return 0; | |
2043 | } | |
2044 | ||
2045 | ||
2046 | ||
2047 | ||
2048 | /* | |
2049 | * Async ftruncate() | |
2050 | */ | |
f3a75078 | 2051 | static void nfs_ftruncate_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
2052 | { |
2053 | struct nfs_cb_data *data = private_data; | |
2054 | struct nfs_context *nfs = data->nfs; | |
2055 | SETATTR3res *res; | |
2056 | ||
f3a75078 RS |
2057 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2058 | ||
84004dbf RS |
2059 | if (status == RPC_STATUS_ERROR) { |
2060 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2061 | free_nfs_cb_data(data); | |
2062 | return; | |
2063 | } | |
2064 | if (status == RPC_STATUS_CANCEL) { | |
2065 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2066 | free_nfs_cb_data(data); | |
2067 | return; | |
2068 | } | |
2069 | ||
2070 | res = command_data; | |
2071 | if (res->status != NFS3_OK) { | |
2072 | rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
2073 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2074 | free_nfs_cb_data(data); | |
2075 | return; | |
2076 | } | |
2077 | ||
2078 | data->cb(0, nfs, NULL, data->private_data); | |
2079 | free_nfs_cb_data(data); | |
2080 | } | |
2081 | ||
183451cf | 2082 | int nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t length, nfs_cb cb, void *private_data) |
84004dbf RS |
2083 | { |
2084 | struct nfs_cb_data *data; | |
2085 | SETATTR3args args; | |
2086 | ||
2087 | data = malloc(sizeof(struct nfs_cb_data)); | |
2088 | if (data == NULL) { | |
2089 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
2090 | return -1; |
2091 | } | |
ea98629a | 2092 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
2093 | data->nfs = nfs; |
2094 | data->cb = cb; | |
2095 | data->private_data = private_data; | |
2096 | ||
ea98629a | 2097 | memset(&args, 0, sizeof(SETATTR3args)); |
2452c57f | 2098 | args.object = nfsfh->fh; |
84004dbf RS |
2099 | args.new_attributes.size.set_it = 1; |
2100 | args.new_attributes.size.set_size3_u.size = length; | |
2101 | ||
b701254d | 2102 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_ftruncate_cb, &args, data) != 0) { |
84004dbf RS |
2103 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
2104 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2105 | free_nfs_cb_data(data); | |
2106 | return -1; | |
2107 | } | |
2108 | return 0; | |
2109 | } | |
2110 | ||
2111 | ||
2112 | /* | |
2113 | * Async truncate() | |
2114 | */ | |
2115 | static int nfs_truncate_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2116 | { | |
183451cf | 2117 | uint64_t offset = data->continue_int; |
84004dbf RS |
2118 | struct nfsfh nfsfh; |
2119 | ||
2452c57f | 2120 | nfsfh.fh = data->fh; |
84004dbf RS |
2121 | |
2122 | if (nfs_ftruncate_async(nfs, &nfsfh, offset, data->cb, data->private_data) != 0) { | |
2123 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); | |
2124 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2125 | free_nfs_cb_data(data); | |
2126 | return -1; | |
2127 | } | |
2128 | free_nfs_cb_data(data); | |
2129 | return 0; | |
2130 | } | |
2131 | ||
183451cf | 2132 | int nfs_truncate_async(struct nfs_context *nfs, const char *path, uint64_t length, nfs_cb cb, void *private_data) |
84004dbf | 2133 | { |
183451cf | 2134 | uint64_t offset; |
84004dbf RS |
2135 | |
2136 | offset = length; | |
2137 | ||
2138 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_truncate_continue_internal, NULL, NULL, offset) != 0) { | |
cbbf9d3e RS |
2139 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
2140 | return -1; | |
84004dbf RS |
2141 | } |
2142 | ||
2143 | return 0; | |
2144 | } | |
2145 | ||
2146 | ||
2147 | ||
2148 | ||
2149 | /* | |
2150 | * Async mkdir() | |
2151 | */ | |
f3a75078 | 2152 | static void nfs_mkdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
2153 | { |
2154 | MKDIR3res *res; | |
2155 | struct nfs_cb_data *data = private_data; | |
2156 | struct nfs_context *nfs = data->nfs; | |
2157 | char *str = data->continue_data; | |
4b1ae88a | 2158 | |
f3a75078 RS |
2159 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2160 | ||
84004dbf RS |
2161 | str = &str[strlen(str) + 1]; |
2162 | ||
2163 | if (status == RPC_STATUS_ERROR) { | |
2164 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2165 | free_nfs_cb_data(data); | |
2166 | return; | |
2167 | } | |
2168 | if (status == RPC_STATUS_CANCEL) { | |
2169 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2170 | free_nfs_cb_data(data); | |
2171 | return; | |
2172 | } | |
2173 | ||
2174 | res = command_data; | |
2175 | if (res->status != NFS3_OK) { | |
2176 | 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)); | |
2177 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2178 | free_nfs_cb_data(data); | |
2179 | return; | |
2180 | } | |
2181 | ||
2182 | data->cb(0, nfs, NULL, data->private_data); | |
2183 | free_nfs_cb_data(data); | |
2184 | } | |
2185 | ||
2186 | static int nfs_mkdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2187 | { | |
2188 | char *str = data->continue_data; | |
7edc9026 RS |
2189 | MKDIR3args args; |
2190 | ||
84004dbf RS |
2191 | str = &str[strlen(str) + 1]; |
2192 | ||
7edc9026 | 2193 | memset(&args, 0, sizeof(MKDIR3args)); |
2452c57f | 2194 | args.where.dir = data->fh; |
7edc9026 RS |
2195 | args.where.name = str; |
2196 | args.attributes.mode.set_it = 1; | |
2197 | args.attributes.mode.set_mode3_u.mode = 0755; | |
2198 | ||
acabd6bf | 2199 | if (rpc_nfs3_mkdir_async(nfs->rpc, nfs_mkdir_cb, &args, data) != 0) { |
84004dbf RS |
2200 | rpc_set_error(nfs->rpc, "RPC error: Failed to send MKDIR 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_mkdir_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) { |
b1a197f5 | 2221 | free(new_path); |
cbbf9d3e RS |
2222 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
2223 | return -1; | |
84004dbf RS |
2224 | } |
2225 | *ptr = 0; | |
2226 | ||
2227 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ | |
2228 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_mkdir_continue_internal, new_path, free, 0) != 0) { | |
cbbf9d3e RS |
2229 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path component"); |
2230 | return -1; | |
84004dbf RS |
2231 | } |
2232 | ||
2233 | return 0; | |
2234 | } | |
2235 | ||
2236 | ||
2237 | ||
2238 | ||
2239 | ||
2240 | /* | |
2241 | * Async rmdir() | |
2242 | */ | |
f3a75078 | 2243 | static void nfs_rmdir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
2244 | { |
2245 | RMDIR3res *res; | |
2246 | struct nfs_cb_data *data = private_data; | |
2247 | struct nfs_context *nfs = data->nfs; | |
2248 | char *str = data->continue_data; | |
4b1ae88a | 2249 | |
f3a75078 RS |
2250 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2251 | ||
84004dbf RS |
2252 | str = &str[strlen(str) + 1]; |
2253 | ||
2254 | if (status == RPC_STATUS_ERROR) { | |
2255 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2256 | free_nfs_cb_data(data); | |
2257 | return; | |
2258 | } | |
2259 | if (status == RPC_STATUS_CANCEL) { | |
2260 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2261 | free_nfs_cb_data(data); | |
2262 | return; | |
2263 | } | |
2264 | ||
2265 | res = command_data; | |
2266 | if (res->status != NFS3_OK) { | |
2267 | 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)); | |
2268 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2269 | free_nfs_cb_data(data); | |
2270 | return; | |
2271 | } | |
2272 | ||
2273 | data->cb(0, nfs, NULL, data->private_data); | |
2274 | free_nfs_cb_data(data); | |
2275 | } | |
2276 | ||
2277 | static int nfs_rmdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2278 | { | |
2279 | char *str = data->continue_data; | |
bf817ee6 RS |
2280 | RMDIR3args args; |
2281 | ||
84004dbf RS |
2282 | str = &str[strlen(str) + 1]; |
2283 | ||
bf817ee6 RS |
2284 | args.object.dir = data->fh; |
2285 | args.object.name = str; | |
2286 | if (rpc_nfs3_rmdir_async(nfs->rpc, nfs_rmdir_cb, &args, data) != 0) { | |
84004dbf RS |
2287 | rpc_set_error(nfs->rpc, "RPC error: Failed to send RMDIR call for %s", data->path); |
2288 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2289 | free_nfs_cb_data(data); | |
2290 | return -1; | |
2291 | } | |
2292 | return 0; | |
2293 | } | |
2294 | ||
2295 | int nfs_rmdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
2296 | { | |
2297 | char *new_path; | |
2298 | char *ptr; | |
2299 | ||
2300 | new_path = strdup(path); | |
2301 | if (new_path == NULL) { | |
cbbf9d3e | 2302 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
84004dbf RS |
2303 | return -1; |
2304 | } | |
2305 | ||
93e9306f | 2306 | ptr = strrchr(new_path, '/'); |
84004dbf | 2307 | if (ptr == NULL) { |
2257433d | 2308 | free(new_path); |
cbbf9d3e RS |
2309 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
2310 | return -1; | |
84004dbf RS |
2311 | } |
2312 | *ptr = 0; | |
2313 | ||
2314 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ | |
2315 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_rmdir_continue_internal, new_path, free, 0) != 0) { | |
cbbf9d3e RS |
2316 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
2317 | return -1; | |
84004dbf RS |
2318 | } |
2319 | ||
2320 | return 0; | |
2321 | } | |
2322 | ||
2323 | ||
2324 | ||
2325 | ||
2326 | /* | |
2327 | * Async creat() | |
2328 | */ | |
f3a75078 | 2329 | static void nfs_create_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
2330 | { |
2331 | LOOKUP3res *res; | |
2332 | struct nfs_cb_data *data = private_data; | |
2333 | struct nfs_context *nfs = data->nfs; | |
2334 | struct nfsfh *nfsfh; | |
2335 | char *str = data->continue_data; | |
2336 | ||
f3a75078 RS |
2337 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2338 | ||
84004dbf RS |
2339 | if (status == RPC_STATUS_ERROR) { |
2340 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2341 | free_nfs_cb_data(data); | |
2342 | return; | |
2343 | } | |
2344 | if (status == RPC_STATUS_CANCEL) { | |
2345 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2346 | free_nfs_cb_data(data); | |
2347 | return; | |
2348 | } | |
2349 | ||
2350 | str = &str[strlen(str) + 1]; | |
2351 | res = command_data; | |
2352 | if (res->status != NFS3_OK) { | |
2353 | 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)); | |
2354 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2355 | ||
2356 | return; | |
2357 | } | |
2358 | ||
2359 | nfsfh = malloc(sizeof(struct nfsfh)); | |
2360 | if (nfsfh == NULL) { | |
2361 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); | |
2362 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2363 | free_nfs_cb_data(data); | |
2364 | return; | |
2365 | } | |
ea98629a | 2366 | memset(nfsfh, 0, sizeof(struct nfsfh)); |
84004dbf | 2367 | |
0c1a9d7a RS |
2368 | /* copy the filehandle */ |
2369 | nfsfh->fh.data.data_len = res->LOOKUP3res_u.resok.object.data.data_len; | |
2370 | nfsfh->fh.data.data_val = malloc(nfsfh->fh.data.data_len); | |
2371 | memcpy(nfsfh->fh.data.data_val, res->LOOKUP3res_u.resok.object.data.data_val, nfsfh->fh.data.data_len); | |
84004dbf RS |
2372 | |
2373 | data->cb(0, nfs, nfsfh, data->private_data); | |
2374 | free_nfs_cb_data(data); | |
2375 | } | |
2376 | ||
2377 | ||
2378 | ||
f3a75078 | 2379 | static void nfs_creat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
2380 | { |
2381 | CREATE3res *res; | |
2382 | struct nfs_cb_data *data = private_data; | |
2383 | struct nfs_context *nfs = data->nfs; | |
2384 | char *str = data->continue_data; | |
463d59bf | 2385 | LOOKUP3args args; |
84004dbf | 2386 | |
f3a75078 RS |
2387 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2388 | ||
84004dbf RS |
2389 | if (status == RPC_STATUS_ERROR) { |
2390 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2391 | free_nfs_cb_data(data); | |
2392 | return; | |
2393 | } | |
2394 | if (status == RPC_STATUS_CANCEL) { | |
2395 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2396 | free_nfs_cb_data(data); | |
2397 | return; | |
2398 | } | |
2399 | ||
2400 | str = &str[strlen(str) + 1]; | |
2401 | res = command_data; | |
2402 | if (res->status != NFS3_OK) { | |
2403 | 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)); | |
2404 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
fadbf8cf | 2405 | free_nfs_cb_data(data); |
84004dbf RS |
2406 | return; |
2407 | } | |
2408 | ||
463d59bf | 2409 | memset(&args, 0, sizeof(LOOKUP3args)); |
2452c57f RS |
2410 | args.what.dir = data->fh; |
2411 | args.what.name = str; | |
463d59bf RS |
2412 | |
2413 | if (rpc_nfs3_lookup_async(nfs->rpc, nfs_create_2_cb, &args, data) != 0) { | |
84004dbf RS |
2414 | rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s/%s", data->saved_path, str); |
2415 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2416 | free_nfs_cb_data(data); | |
2417 | return; | |
2418 | } | |
2419 | return; | |
2420 | } | |
2421 | ||
2422 | static int nfs_creat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2423 | { | |
2424 | char *str = data->continue_data; | |
c985c015 RS |
2425 | CREATE3args args; |
2426 | ||
84004dbf RS |
2427 | str = &str[strlen(str) + 1]; |
2428 | ||
c985c015 | 2429 | memset(&args, 0, sizeof(CREATE3args)); |
2452c57f | 2430 | args.where.dir = data->fh; |
c985c015 RS |
2431 | args.where.name = str; |
2432 | args.how.mode = UNCHECKED; | |
2433 | args.how.createhow3_u.obj_attributes.mode.set_it = 1; | |
2434 | args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = data->continue_int; | |
2435 | ||
789c3f12 | 2436 | if (rpc_nfs3_create_async(nfs->rpc, nfs_creat_1_cb, &args, data) != 0) { |
84004dbf RS |
2437 | rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str); |
2438 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2439 | free_nfs_cb_data(data); | |
2440 | return -1; | |
2441 | } | |
2442 | return 0; | |
2443 | } | |
2444 | ||
2445 | int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) | |
2446 | { | |
2447 | char *new_path; | |
2448 | char *ptr; | |
2449 | ||
2450 | new_path = strdup(path); | |
2451 | if (new_path == NULL) { | |
cbbf9d3e | 2452 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
84004dbf RS |
2453 | return -1; |
2454 | } | |
2455 | ||
93e9306f | 2456 | ptr = strrchr(new_path, '/'); |
84004dbf | 2457 | if (ptr == NULL) { |
cbbf9d3e | 2458 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
a1287496 | 2459 | free(new_path); |
cbbf9d3e | 2460 | return -1; |
84004dbf RS |
2461 | } |
2462 | *ptr = 0; | |
2463 | ||
73f4ae7c | 2464 | /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */ |
84004dbf | 2465 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_creat_continue_internal, new_path, free, mode) != 0) { |
cbbf9d3e RS |
2466 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
2467 | return -1; | |
84004dbf RS |
2468 | } |
2469 | ||
2470 | return 0; | |
2471 | } | |
2472 | ||
2473 | ||
2474 | ||
2475 | ||
2476 | /* | |
2477 | * Async unlink() | |
2478 | */ | |
f3a75078 | 2479 | static void nfs_unlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
2480 | { |
2481 | REMOVE3res *res; | |
2482 | struct nfs_cb_data *data = private_data; | |
2483 | struct nfs_context *nfs = data->nfs; | |
2484 | char *str = data->continue_data; | |
4b1ae88a | 2485 | |
f3a75078 RS |
2486 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2487 | ||
84004dbf RS |
2488 | str = &str[strlen(str) + 1]; |
2489 | ||
2490 | if (status == RPC_STATUS_ERROR) { | |
2491 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2492 | free_nfs_cb_data(data); | |
2493 | return; | |
2494 | } | |
2495 | if (status == RPC_STATUS_CANCEL) { | |
2496 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2497 | free_nfs_cb_data(data); | |
2498 | return; | |
2499 | } | |
2500 | ||
2501 | res = command_data; | |
2502 | if (res->status != NFS3_OK) { | |
2503 | 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)); | |
2504 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2505 | free_nfs_cb_data(data); | |
2506 | return; | |
2507 | } | |
2508 | ||
2509 | data->cb(0, nfs, NULL, data->private_data); | |
2510 | free_nfs_cb_data(data); | |
2511 | } | |
2512 | ||
2513 | static int nfs_unlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2514 | { | |
2515 | char *str = data->continue_data; | |
a0a5aa26 RS |
2516 | struct REMOVE3args args; |
2517 | ||
84004dbf RS |
2518 | str = &str[strlen(str) + 1]; |
2519 | ||
a0a5aa26 RS |
2520 | args.object.dir = data->fh; |
2521 | args.object.name = str; | |
2522 | if (rpc_nfs3_remove_async(nfs->rpc, nfs_unlink_cb, &args, data) != 0) { | |
84004dbf RS |
2523 | rpc_set_error(nfs->rpc, "RPC error: Failed to send REMOVE call for %s", data->path); |
2524 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2525 | free_nfs_cb_data(data); | |
2526 | return -1; | |
2527 | } | |
2528 | return 0; | |
2529 | } | |
2530 | ||
2531 | int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
2532 | { | |
2533 | char *new_path; | |
2534 | char *ptr; | |
2535 | ||
2536 | new_path = strdup(path); | |
2537 | if (new_path == NULL) { | |
cbbf9d3e | 2538 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
84004dbf RS |
2539 | return -1; |
2540 | } | |
2541 | ||
93e9306f | 2542 | ptr = strrchr(new_path, '/'); |
84004dbf | 2543 | if (ptr == NULL) { |
bcbb21cd | 2544 | free(new_path); |
cbbf9d3e RS |
2545 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
2546 | return -1; | |
84004dbf RS |
2547 | } |
2548 | *ptr = 0; | |
2549 | ||
2550 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ | |
2551 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_unlink_continue_internal, new_path, free, 0) != 0) { | |
cbbf9d3e RS |
2552 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
2553 | return -1; | |
84004dbf RS |
2554 | } |
2555 | ||
2556 | return 0; | |
2557 | } | |
2558 | ||
2559 | ||
1ec6b50a RS |
2560 | /* |
2561 | * Async mknod() | |
2562 | */ | |
2563 | struct mknod_cb_data { | |
2564 | char *path; | |
2565 | int mode; | |
2566 | int major; | |
2567 | int minor; | |
2568 | }; | |
2569 | ||
2570 | static void free_mknod_cb_data(void *ptr) | |
2571 | { | |
2572 | struct mknod_cb_data *data = ptr; | |
2573 | ||
2574 | free(data->path); | |
2575 | free(data); | |
2576 | } | |
2577 | ||
f3a75078 | 2578 | static void nfs_mknod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
1ec6b50a RS |
2579 | { |
2580 | MKNOD3res *res; | |
2581 | struct nfs_cb_data *data = private_data; | |
2582 | struct nfs_context *nfs = data->nfs; | |
2583 | char *str = data->continue_data; | |
4b1ae88a | 2584 | |
f3a75078 RS |
2585 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2586 | ||
1ec6b50a RS |
2587 | str = &str[strlen(str) + 1]; |
2588 | ||
2589 | if (status == RPC_STATUS_ERROR) { | |
2590 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2591 | free_nfs_cb_data(data); | |
2592 | return; | |
2593 | } | |
2594 | if (status == RPC_STATUS_CANCEL) { | |
2595 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2596 | free_nfs_cb_data(data); | |
2597 | return; | |
2598 | } | |
2599 | ||
2600 | res = command_data; | |
2601 | if (res->status != NFS3_OK) { | |
2602 | 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)); | |
2603 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2604 | free_nfs_cb_data(data); | |
2605 | return; | |
2606 | } | |
2607 | ||
2608 | data->cb(0, nfs, NULL, data->private_data); | |
2609 | free_nfs_cb_data(data); | |
2610 | } | |
2611 | ||
2612 | static int nfs_mknod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2613 | { | |
2614 | struct mknod_cb_data *cb_data = data->continue_data; | |
2615 | char *str = cb_data->path; | |
b17cb0a2 | 2616 | MKNOD3args args; |
4b1ae88a | 2617 | |
1ec6b50a RS |
2618 | str = &str[strlen(str) + 1]; |
2619 | ||
b17cb0a2 RS |
2620 | args.where.dir = data->fh; |
2621 | args.where.name = str; | |
2622 | switch (cb_data->mode & S_IFMT) { | |
2623 | case S_IFCHR: | |
2624 | args.what.type = NF3CHR; | |
2625 | args.what.mknoddata3_u.chr_device.dev_attributes.mode.set_it = 1; | |
2626 | args.what.mknoddata3_u.chr_device.dev_attributes.mode.set_mode3_u.mode = cb_data->mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); | |
2627 | args.what.mknoddata3_u.chr_device.spec.specdata1 = cb_data->major; | |
2628 | args.what.mknoddata3_u.chr_device.spec.specdata2 = cb_data->minor; | |
2629 | break; | |
2630 | case S_IFBLK: | |
2631 | args.what.type = NF3BLK; | |
2632 | args.what.mknoddata3_u.blk_device.dev_attributes.mode.set_it = 1; | |
2633 | args.what.mknoddata3_u.blk_device.dev_attributes.mode.set_mode3_u.mode = cb_data->mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); | |
2634 | args.what.mknoddata3_u.blk_device.spec.specdata1 = cb_data->major; | |
2635 | args.what.mknoddata3_u.blk_device.spec.specdata2 = cb_data->minor; | |
2636 | case S_IFSOCK: | |
2637 | args.what.type = NF3SOCK; | |
2638 | args.what.mknoddata3_u.sock_attributes.mode.set_it = 1; | |
2639 | args.what.mknoddata3_u.sock_attributes.mode.set_mode3_u.mode = cb_data->mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); | |
2640 | break; | |
2641 | case S_IFIFO: | |
2642 | args.what.type = NF3FIFO; | |
2643 | args.what.mknoddata3_u.pipe_attributes.mode.set_it = 1; | |
2644 | args.what.mknoddata3_u.pipe_attributes.mode.set_mode3_u.mode = cb_data->mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); | |
2645 | break; | |
2646 | default: | |
2647 | rpc_set_error(nfs->rpc, "Invalid file type for NFS3/MKNOD call"); | |
2648 | data->cb(-EINVAL, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2649 | free_nfs_cb_data(data); | |
2650 | return -1; | |
2651 | } | |
2652 | ||
2653 | if (rpc_nfs3_mknod_async(nfs->rpc, nfs_mknod_cb, &args, data) != 0) { | |
1ec6b50a RS |
2654 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
2655 | free_nfs_cb_data(data); | |
2656 | return -1; | |
2657 | } | |
2658 | return 0; | |
2659 | } | |
2660 | ||
2661 | int nfs_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev, nfs_cb cb, void *private_data) | |
2662 | { | |
2663 | char *ptr; | |
2664 | struct mknod_cb_data *cb_data; | |
2665 | ||
2666 | cb_data = malloc(sizeof(struct mknod_cb_data)); | |
2667 | if (cb_data == NULL) { | |
2668 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for cb data"); | |
2669 | return -1; | |
2670 | } | |
2671 | ||
2672 | cb_data->path = strdup(path); | |
2673 | if (cb_data->path == NULL) { | |
2674 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); | |
4b1ae88a | 2675 | free(cb_data); |
1ec6b50a RS |
2676 | return -1; |
2677 | } | |
2678 | ||
2679 | ptr = strrchr(cb_data->path, '/'); | |
2680 | if (ptr == NULL) { | |
2681 | rpc_set_error(nfs->rpc, "Invalid path %s", path); | |
feb2fc2f | 2682 | free_mknod_cb_data(cb_data); |
1ec6b50a RS |
2683 | return -1; |
2684 | } | |
2685 | *ptr = 0; | |
2686 | ||
2687 | cb_data->mode = mode; | |
2688 | cb_data->major = major(dev); | |
2689 | cb_data->minor = minor(dev); | |
2690 | ||
2691 | /* data->path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ | |
2692 | if (nfs_lookuppath_async(nfs, cb_data->path, cb, private_data, nfs_mknod_continue_internal, cb_data, free_mknod_cb_data, 0) != 0) { | |
2693 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); | |
1ec6b50a RS |
2694 | return -1; |
2695 | } | |
2696 | ||
2697 | return 0; | |
2698 | } | |
84004dbf | 2699 | |
84004dbf RS |
2700 | /* |
2701 | * Async opendir() | |
2702 | */ | |
fb69c64c RS |
2703 | |
2704 | /* ReadDirPlus Emulation Callback data */ | |
2705 | struct rdpe_cb_data { | |
2706 | int getattrcount; | |
2707 | int status; | |
2708 | struct nfs_cb_data *data; | |
2709 | }; | |
2710 | ||
2711 | /* ReadDirPlus Emulation LOOKUP Callback data */ | |
2712 | struct rdpe_lookup_cb_data { | |
2713 | struct rdpe_cb_data *rdpe_cb_data; | |
2714 | struct nfsdirent *nfsdirent; | |
2715 | }; | |
2716 | ||
2717 | /* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */ | |
f3a75078 | 2718 | static void nfs_opendir3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf | 2719 | { |
fb69c64c RS |
2720 | LOOKUP3res *res = command_data; |
2721 | struct rdpe_lookup_cb_data *rdpe_lookup_cb_data = private_data; | |
2722 | struct rdpe_cb_data *rdpe_cb_data = rdpe_lookup_cb_data->rdpe_cb_data; | |
2723 | struct nfs_cb_data *data = rdpe_cb_data->data; | |
2724 | struct nfsdir *nfsdir = data->continue_data; | |
2725 | struct nfs_context *nfs = data->nfs; | |
2726 | struct nfsdirent *nfsdirent = rdpe_lookup_cb_data->nfsdirent; | |
2727 | ||
f3a75078 RS |
2728 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2729 | ||
fb69c64c RS |
2730 | free(rdpe_lookup_cb_data); |
2731 | ||
2732 | rdpe_cb_data->getattrcount--; | |
2733 | ||
2734 | if (status == RPC_STATUS_ERROR) { | |
2735 | rdpe_cb_data->status = RPC_STATUS_ERROR; | |
2736 | } | |
2737 | if (status == RPC_STATUS_CANCEL) { | |
2738 | rdpe_cb_data->status = RPC_STATUS_CANCEL; | |
2739 | } | |
2740 | if (status == RPC_STATUS_SUCCESS && res->status != NFS3_OK) { | |
2741 | rdpe_cb_data->status = RPC_STATUS_ERROR; | |
2742 | } | |
2743 | if (status == RPC_STATUS_SUCCESS && res->status == NFS3_OK) { | |
2744 | if (res->LOOKUP3res_u.resok.obj_attributes.attributes_follow) { | |
2745 | fattr3 *attributes = &res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes; | |
2746 | ||
2747 | nfsdirent->type = attributes->type; | |
2748 | nfsdirent->mode = attributes->mode; | |
2749 | nfsdirent->size = attributes->size; | |
2750 | ||
2751 | nfsdirent->atime.tv_sec = attributes->atime.seconds; | |
2752 | nfsdirent->atime.tv_usec = attributes->atime.nseconds/1000; | |
2753 | nfsdirent->mtime.tv_sec = attributes->mtime.seconds; | |
2754 | nfsdirent->mtime.tv_usec = attributes->mtime.nseconds/1000; | |
2755 | nfsdirent->ctime.tv_sec = attributes->ctime.seconds; | |
2756 | nfsdirent->ctime.tv_usec = attributes->ctime.nseconds/1000; | |
7bda8bad RS |
2757 | nfsdirent->uid = attributes->uid; |
2758 | nfsdirent->gid = attributes->gid; | |
fb69c64c RS |
2759 | } |
2760 | } | |
2761 | ||
2762 | if (rdpe_cb_data->getattrcount == 0) { | |
2763 | if (rdpe_cb_data->status != RPC_STATUS_SUCCESS) { | |
2764 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2765 | nfs_free_nfsdir(nfsdir); | |
2766 | } else { | |
2767 | data->cb(0, nfs, nfsdir, data->private_data); | |
2768 | } | |
2769 | free(rdpe_cb_data); | |
2770 | ||
2771 | data->continue_data = NULL; | |
2772 | free_nfs_cb_data(data); | |
2773 | } | |
2774 | } | |
2775 | ||
f3a75078 | 2776 | static void nfs_opendir2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
fb69c64c RS |
2777 | { |
2778 | READDIR3res *res = command_data; | |
84004dbf RS |
2779 | struct nfs_cb_data *data = private_data; |
2780 | struct nfs_context *nfs = data->nfs; | |
fb6510bb | 2781 | struct nfsdir *nfsdir = data->continue_data; |
fb69c64c RS |
2782 | struct nfsdirent *nfsdirent; |
2783 | struct entry3 *entry; | |
ecbd14a1 | 2784 | uint64_t cookie = 0; |
fb69c64c | 2785 | struct rdpe_cb_data *rdpe_cb_data; |
4b1ae88a | 2786 | |
f3a75078 RS |
2787 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2788 | ||
84004dbf RS |
2789 | if (status == RPC_STATUS_ERROR) { |
2790 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2791 | nfs_free_nfsdir(nfsdir); | |
2792 | data->continue_data = NULL; | |
2793 | free_nfs_cb_data(data); | |
2794 | return; | |
2795 | } | |
fb69c64c | 2796 | |
84004dbf RS |
2797 | if (status == RPC_STATUS_CANCEL) { |
2798 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2799 | nfs_free_nfsdir(nfsdir); | |
2800 | data->continue_data = NULL; | |
2801 | free_nfs_cb_data(data); | |
2802 | return; | |
2803 | } | |
2804 | ||
84004dbf RS |
2805 | if (res->status != NFS3_OK) { |
2806 | 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)); | |
2807 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2808 | nfs_free_nfsdir(nfsdir); | |
2809 | data->continue_data = NULL; | |
2810 | free_nfs_cb_data(data); | |
2811 | return; | |
fb69c64c RS |
2812 | } |
2813 | ||
2814 | entry =res->READDIR3res_u.resok.reply.entries; | |
2815 | while (entry != NULL) { | |
2816 | nfsdirent = malloc(sizeof(struct nfsdirent)); | |
2817 | if (nfsdirent == NULL) { | |
2818 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data); | |
2819 | nfs_free_nfsdir(nfsdir); | |
2820 | data->continue_data = NULL; | |
2821 | free_nfs_cb_data(data); | |
2822 | return; | |
2823 | } | |
2824 | memset(nfsdirent, 0, sizeof(struct nfsdirent)); | |
2825 | nfsdirent->name = strdup(entry->name); | |
2826 | if (nfsdirent->name == NULL) { | |
2827 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data); | |
a26bebef | 2828 | free(nfsdirent); |
fb69c64c RS |
2829 | nfs_free_nfsdir(nfsdir); |
2830 | data->continue_data = NULL; | |
2831 | free_nfs_cb_data(data); | |
2832 | return; | |
2833 | } | |
2834 | nfsdirent->inode = entry->fileid; | |
2835 | ||
2836 | nfsdirent->next = nfsdir->entries; | |
2837 | nfsdir->entries = nfsdirent; | |
2838 | ||
2839 | cookie = entry->cookie; | |
2840 | entry = entry->nextentry; | |
2841 | } | |
2842 | ||
2843 | if (res->READDIR3res_u.resok.reply.eof == 0) { | |
a64a16a2 RS |
2844 | READDIR3args args; |
2845 | ||
2846 | args.dir = data->fh; | |
2847 | args.cookie = cookie; | |
2452c57f | 2848 | memcpy(&args.cookieverf, res->READDIR3res_u.resok.cookieverf, sizeof(cookieverf3)); |
a64a16a2 RS |
2849 | args.count = 8192; |
2850 | ||
2851 | if (rpc_nfs3_readdir_async(nfs->rpc, nfs_opendir2_cb, &args, data) != 0) { | |
fb69c64c RS |
2852 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path); |
2853 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2854 | nfs_free_nfsdir(nfsdir); | |
2855 | data->continue_data = NULL; | |
2856 | free_nfs_cb_data(data); | |
2857 | return; | |
2858 | } | |
2859 | return; | |
2860 | } | |
2861 | ||
2862 | /* steal the dirhandle */ | |
2863 | nfsdir->current = nfsdir->entries; | |
2864 | ||
2865 | rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data)); | |
2866 | rdpe_cb_data->getattrcount = 0; | |
2867 | rdpe_cb_data->status = RPC_STATUS_SUCCESS; | |
2868 | rdpe_cb_data->data = data; | |
2869 | for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) { | |
2870 | struct rdpe_lookup_cb_data *rdpe_lookup_cb_data; | |
463d59bf | 2871 | LOOKUP3args args; |
fb69c64c RS |
2872 | |
2873 | rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data)); | |
2874 | rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data; | |
2875 | rdpe_lookup_cb_data->nfsdirent = nfsdirent; | |
2876 | ||
463d59bf | 2877 | memset(&args, 0, sizeof(LOOKUP3args)); |
2452c57f RS |
2878 | args.what.dir = data->fh; |
2879 | args.what.name = nfsdirent->name; | |
463d59bf RS |
2880 | |
2881 | if (rpc_nfs3_lookup_async(nfs->rpc, nfs_opendir3_cb, &args, rdpe_lookup_cb_data) != 0) { | |
fb69c64c RS |
2882 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call"); |
2883 | ||
2884 | /* if we have already commands in flight, we cant just stop, we have to wait for the | |
2885 | * commands in flight to complete | |
2886 | */ | |
2887 | if (rdpe_cb_data->getattrcount > 0) { | |
a26bebef AR |
2888 | nfs_free_nfsdir(nfsdir); |
2889 | data->continue_data = NULL; | |
2890 | free_nfs_cb_data(data); | |
fb69c64c RS |
2891 | rdpe_cb_data->status = RPC_STATUS_ERROR; |
2892 | free(rdpe_lookup_cb_data); | |
2893 | return; | |
2894 | } | |
2895 | ||
2896 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2897 | nfs_free_nfsdir(nfsdir); | |
2898 | data->continue_data = NULL; | |
2899 | free_nfs_cb_data(data); | |
2900 | free(rdpe_lookup_cb_data); | |
2901 | free(rdpe_cb_data); | |
2902 | return; | |
2903 | } | |
2904 | rdpe_cb_data->getattrcount++; | |
2905 | } | |
2906 | } | |
2907 | ||
f3a75078 | 2908 | static void nfs_opendir_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
fb69c64c RS |
2909 | { |
2910 | READDIRPLUS3res *res = command_data; | |
2911 | struct nfs_cb_data *data = private_data; | |
2912 | struct nfs_context *nfs = data->nfs; | |
2913 | struct nfsdir *nfsdir = data->continue_data; | |
2914 | struct entryplus3 *entry; | |
ecbd14a1 | 2915 | uint64_t cookie = 0; |
4b1ae88a | 2916 | |
f3a75078 | 2917 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
fb69c64c RS |
2918 | |
2919 | if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){ | |
a64a16a2 RS |
2920 | READDIR3args args; |
2921 | ||
2922 | args.dir = data->fh; | |
2923 | args.cookie = cookie; | |
2452c57f | 2924 | memset(&args.cookieverf, 0, sizeof(cookieverf3)); |
a64a16a2 | 2925 | args.count = 8192; |
fb69c64c | 2926 | |
a64a16a2 | 2927 | if (rpc_nfs3_readdir_async(nfs->rpc, nfs_opendir2_cb, &args, data) != 0) { |
fb69c64c RS |
2928 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path); |
2929 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2930 | nfs_free_nfsdir(nfsdir); | |
2931 | data->continue_data = NULL; | |
2932 | free_nfs_cb_data(data); | |
2933 | return; | |
2934 | } | |
2935 | return; | |
2936 | } | |
2937 | ||
2938 | if (status == RPC_STATUS_CANCEL) { | |
2939 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2940 | nfs_free_nfsdir(nfsdir); | |
2941 | data->continue_data = NULL; | |
2942 | free_nfs_cb_data(data); | |
2943 | return; | |
2944 | } | |
2945 | ||
2946 | if (res->status != NFS3_OK) { | |
2947 | 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)); | |
2948 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2949 | nfs_free_nfsdir(nfsdir); | |
2950 | data->continue_data = NULL; | |
2951 | free_nfs_cb_data(data); | |
2952 | return; | |
84004dbf RS |
2953 | } |
2954 | ||
f390f181 | 2955 | entry =res->READDIRPLUS3res_u.resok.reply.entries; |
84004dbf RS |
2956 | while (entry != NULL) { |
2957 | struct nfsdirent *nfsdirent; | |
2958 | ||
2959 | nfsdirent = malloc(sizeof(struct nfsdirent)); | |
2960 | if (nfsdirent == NULL) { | |
2961 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data); | |
2962 | nfs_free_nfsdir(nfsdir); | |
2963 | data->continue_data = NULL; | |
2964 | free_nfs_cb_data(data); | |
2965 | return; | |
2966 | } | |
ea98629a | 2967 | memset(nfsdirent, 0, sizeof(struct nfsdirent)); |
84004dbf RS |
2968 | nfsdirent->name = strdup(entry->name); |
2969 | if (nfsdirent->name == NULL) { | |
2970 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data); | |
b2fc5c54 | 2971 | free(nfsdirent); |
84004dbf RS |
2972 | nfs_free_nfsdir(nfsdir); |
2973 | data->continue_data = NULL; | |
2974 | free_nfs_cb_data(data); | |
2975 | return; | |
2976 | } | |
2977 | nfsdirent->inode = entry->fileid; | |
0804e67d RS |
2978 | if (entry->name_attributes.attributes_follow) { |
2979 | nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type; | |
2980 | nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode; | |
2981 | nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size; | |
2982 | ||
2983 | nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds; | |
2984 | nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000; | |
2985 | nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds; | |
2986 | nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000; | |
2987 | nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds; | |
2988 | nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000; | |
7bda8bad RS |
2989 | nfsdirent->uid = entry->name_attributes.post_op_attr_u.attributes.uid; |
2990 | nfsdirent->gid = entry->name_attributes.post_op_attr_u.attributes.gid; | |
0804e67d RS |
2991 | } |
2992 | ||
84004dbf RS |
2993 | nfsdirent->next = nfsdir->entries; |
2994 | nfsdir->entries = nfsdirent; | |
2995 | ||
2996 | cookie = entry->cookie; | |
2997 | entry = entry->nextentry; | |
2998 | } | |
2999 | ||
f390f181 | 3000 | if (res->READDIRPLUS3res_u.resok.reply.eof == 0) { |
1a5b83c9 RS |
3001 | READDIRPLUS3args args; |
3002 | ||
3003 | args.dir = data->fh; | |
3004 | args.cookie = cookie; | |
3005 | memcpy(&args.cookieverf, res->READDIRPLUS3res_u.resok.cookieverf, sizeof(cookieverf3)); | |
3006 | args.dircount = 8192; | |
3007 | args.maxcount = 8192; | |
3008 | ||
3009 | if (rpc_nfs3_readdirplus_async(nfs->rpc, nfs_opendir_cb, &args, data) != 0) { | |
f390f181 | 3010 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path); |
84004dbf RS |
3011 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
3012 | nfs_free_nfsdir(nfsdir); | |
3013 | data->continue_data = NULL; | |
3014 | free_nfs_cb_data(data); | |
3015 | return; | |
3016 | } | |
3017 | return; | |
3018 | } | |
3019 | ||
3020 | /* steal the dirhandle */ | |
3021 | data->continue_data = NULL; | |
3022 | nfsdir->current = nfsdir->entries; | |
3023 | ||
3024 | data->cb(0, nfs, nfsdir, data->private_data); | |
3025 | free_nfs_cb_data(data); | |
3026 | } | |
3027 | ||
3028 | static int nfs_opendir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3029 | { | |
1a5b83c9 | 3030 | READDIRPLUS3args args; |
84004dbf | 3031 | |
1a5b83c9 RS |
3032 | args.dir = data->fh; |
3033 | args.cookie = 0; | |
3034 | memset(&args.cookieverf, 0, sizeof(cookieverf3)); | |
3035 | args.dircount = 8192; | |
3036 | args.maxcount = 8192; | |
3037 | if (rpc_nfs3_readdirplus_async(nfs->rpc, nfs_opendir_cb, &args, data) != 0) { | |
f390f181 | 3038 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path); |
84004dbf RS |
3039 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
3040 | free_nfs_cb_data(data); | |
3041 | return -1; | |
3042 | } | |
3043 | return 0; | |
3044 | } | |
3045 | ||
3046 | int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
3047 | { | |
3048 | struct nfsdir *nfsdir; | |
3049 | ||
3050 | nfsdir = malloc(sizeof(struct nfsdir)); | |
3051 | if (nfsdir == NULL) { | |
cbbf9d3e | 3052 | rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir"); |
84004dbf RS |
3053 | return -1; |
3054 | } | |
ea98629a | 3055 | memset(nfsdir, 0, sizeof(struct nfsdir)); |
84004dbf RS |
3056 | |
3057 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) { | |
cbbf9d3e RS |
3058 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
3059 | return -1; | |
84004dbf RS |
3060 | } |
3061 | ||
3062 | return 0; | |
3063 | } | |
3064 | ||
3065 | ||
3066 | struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir) | |
3067 | { | |
3068 | struct nfsdirent *nfsdirent = nfsdir->current; | |
3069 | ||
3070 | if (nfsdir->current != NULL) { | |
3071 | nfsdir->current = nfsdir->current->next; | |
3072 | } | |
3073 | return nfsdirent; | |
3074 | } | |
3075 | ||
3076 | ||
f893b680 RS |
3077 | /* |
3078 | * closedir() | |
3079 | */ | |
84004dbf RS |
3080 | void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir) |
3081 | { | |
3082 | nfs_free_nfsdir(nfsdir); | |
3083 | } | |
3084 | ||
3085 | ||
f893b680 RS |
3086 | /* |
3087 | * getcwd() | |
3088 | */ | |
3089 | void nfs_getcwd(struct nfs_context *nfs, const char **cwd) | |
3090 | { | |
3091 | if (cwd) { | |
3092 | *cwd = nfs->cwd; | |
3093 | } | |
3094 | } | |
84004dbf RS |
3095 | |
3096 | ||
3097 | /* | |
3098 | * Async lseek() | |
3099 | */ | |
3100 | struct lseek_cb_data { | |
3101 | struct nfs_context *nfs; | |
3102 | struct nfsfh *nfsfh; | |
183451cf | 3103 | uint64_t offset; |
84004dbf RS |
3104 | nfs_cb cb; |
3105 | void *private_data; | |
3106 | }; | |
3107 | ||
f3a75078 | 3108 | static void nfs_lseek_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3109 | { |
3110 | GETATTR3res *res; | |
3111 | struct lseek_cb_data *data = private_data; | |
3112 | struct nfs_context *nfs = data->nfs; | |
3113 | ||
f3a75078 RS |
3114 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3115 | ||
84004dbf RS |
3116 | if (status == RPC_STATUS_ERROR) { |
3117 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3118 | free(data); | |
3119 | return; | |
3120 | } | |
3121 | if (status == RPC_STATUS_CANCEL) { | |
3122 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3123 | free(data); | |
3124 | return; | |
3125 | } | |
3126 | ||
3127 | res = command_data; | |
3128 | if (res->status != NFS3_OK) { | |
3129 | rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
3130 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3131 | free(data); | |
3132 | return; | |
3133 | } | |
3134 | ||
3135 | data->nfsfh->offset = data->offset + res->GETATTR3res_u.resok.obj_attributes.size; | |
3136 | data->cb(0, nfs, &data->nfsfh->offset, data->private_data); | |
3137 | free(data); | |
3138 | } | |
3139 | ||
183451cf | 3140 | int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, int whence, nfs_cb cb, void *private_data) |
84004dbf RS |
3141 | { |
3142 | struct lseek_cb_data *data; | |
463d59bf | 3143 | struct GETATTR3args args; |
84004dbf RS |
3144 | |
3145 | if (whence == SEEK_SET) { | |
3146 | nfsfh->offset = offset; | |
3147 | cb(0, nfs, &nfsfh->offset, private_data); | |
3148 | return 0; | |
3149 | } | |
3150 | if (whence == SEEK_CUR) { | |
3151 | nfsfh->offset += offset; | |
3152 | cb(0, nfs, &nfsfh->offset, private_data); | |
3153 | return 0; | |
3154 | } | |
3155 | ||
3156 | data = malloc(sizeof(struct lseek_cb_data)); | |
3157 | if (data == NULL) { | |
3158 | rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data"); | |
3159 | return -1; | |
3160 | } | |
3161 | ||
3162 | data->nfs = nfs; | |
3163 | data->nfsfh = nfsfh; | |
3164 | data->offset = offset; | |
3165 | data->cb = cb; | |
3166 | data->private_data = private_data; | |
3167 | ||
463d59bf | 3168 | memset(&args, 0, sizeof(GETATTR3args)); |
2452c57f | 3169 | args.object = nfsfh->fh; |
463d59bf RS |
3170 | |
3171 | if (rpc_nfs3_getattr_async(nfs->rpc, nfs_lseek_1_cb, &args, data) != 0) { | |
84004dbf RS |
3172 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call"); |
3173 | free(data); | |
cbbf9d3e | 3174 | return -1; |
84004dbf RS |
3175 | } |
3176 | return 0; | |
3177 | } | |
3178 | ||
3179 | ||
3180 | ||
3181 | ||
3182 | /* | |
3183 | * Async statvfs() | |
3184 | */ | |
f3a75078 | 3185 | static void nfs_statvfs_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3186 | { |
3187 | FSSTAT3res *res; | |
3188 | struct nfs_cb_data *data = private_data; | |
3189 | struct nfs_context *nfs = data->nfs; | |
3190 | struct statvfs svfs; | |
3191 | ||
f3a75078 RS |
3192 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3193 | ||
84004dbf RS |
3194 | if (status == RPC_STATUS_ERROR) { |
3195 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3196 | free_nfs_cb_data(data); | |
3197 | return; | |
3198 | } | |
3199 | if (status == RPC_STATUS_CANCEL) { | |
3200 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3201 | free_nfs_cb_data(data); | |
3202 | return; | |
3203 | } | |
3204 | ||
3205 | res = command_data; | |
3206 | if (res->status != NFS3_OK) { | |
3207 | 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)); | |
3208 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3209 | free_nfs_cb_data(data); | |
3210 | return; | |
3211 | } | |
3212 | ||
3213 | svfs.f_bsize = 4096; | |
3214 | svfs.f_frsize = 4096; | |
3215 | svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/4096; | |
3216 | svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/4096; | |
3217 | svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/4096; | |
3218 | svfs.f_files = res->FSSTAT3res_u.resok.tfiles; | |
3219 | svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles; | |
252aa90d | 3220 | #if !defined(ANDROID) |
84004dbf RS |
3221 | svfs.f_favail = res->FSSTAT3res_u.resok.afiles; |
3222 | svfs.f_fsid = 0; | |
3223 | svfs.f_flag = 0; | |
3224 | svfs.f_namemax = 256; | |
252aa90d | 3225 | #endif |
84004dbf RS |
3226 | |
3227 | data->cb(0, nfs, &svfs, data->private_data); | |
3228 | free_nfs_cb_data(data); | |
3229 | } | |
3230 | ||
3231 | static int nfs_statvfs_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3232 | { | |
0249c17b RS |
3233 | FSSTAT3args args; |
3234 | ||
3235 | args.fsroot = data->fh; | |
3236 | if (rpc_nfs3_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &args, data) != 0) { | |
84004dbf RS |
3237 | rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT call for %s", data->path); |
3238 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3239 | free_nfs_cb_data(data); | |
3240 | return -1; | |
3241 | } | |
3242 | return 0; | |
3243 | } | |
3244 | ||
3245 | int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
3246 | { | |
3247 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) { | |
cbbf9d3e | 3248 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
3249 | return -1; |
3250 | } | |
3251 | ||
3252 | return 0; | |
3253 | } | |
3254 | ||
3255 | ||
3256 | ||
3257 | ||
3258 | /* | |
3259 | * Async readlink() | |
3260 | */ | |
f3a75078 | 3261 | static void nfs_readlink_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3262 | { |
3263 | READLINK3res *res; | |
3264 | struct nfs_cb_data *data = private_data; | |
3265 | struct nfs_context *nfs = data->nfs; | |
3266 | ||
f3a75078 RS |
3267 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3268 | ||
84004dbf RS |
3269 | if (status == RPC_STATUS_ERROR) { |
3270 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3271 | free_nfs_cb_data(data); | |
3272 | return; | |
3273 | } | |
3274 | if (status == RPC_STATUS_CANCEL) { | |
3275 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3276 | free_nfs_cb_data(data); | |
3277 | return; | |
3278 | } | |
3279 | ||
3280 | res = command_data; | |
3281 | if (res->status != NFS3_OK) { | |
3282 | 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)); | |
3283 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3284 | free_nfs_cb_data(data); | |
3285 | return; | |
3286 | } | |
3287 | ||
4b1ae88a | 3288 | |
84004dbf RS |
3289 | data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data); |
3290 | free_nfs_cb_data(data); | |
3291 | } | |
3292 | ||
3293 | static int nfs_readlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3294 | { | |
16104b27 RS |
3295 | READLINK3args args; |
3296 | ||
2452c57f | 3297 | args.symlink = data->fh; |
16104b27 | 3298 | |
7cd23ccc | 3299 | if (rpc_nfs3_readlink_async(nfs->rpc, nfs_readlink_1_cb, &args, data) != 0) { |
84004dbf RS |
3300 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path); |
3301 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3302 | free_nfs_cb_data(data); | |
3303 | return -1; | |
3304 | } | |
3305 | return 0; | |
3306 | } | |
3307 | ||
3308 | int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
3309 | { | |
3310 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) { | |
cbbf9d3e | 3311 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
3312 | return -1; |
3313 | } | |
3314 | ||
3315 | return 0; | |
3316 | } | |
3317 | ||
3318 | ||
3319 | ||
3320 | ||
3321 | /* | |
3322 | * Async chmod() | |
3323 | */ | |
f3a75078 | 3324 | static void nfs_chmod_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3325 | { |
3326 | struct nfs_cb_data *data = private_data; | |
3327 | struct nfs_context *nfs = data->nfs; | |
3328 | SETATTR3res *res; | |
3329 | ||
f3a75078 RS |
3330 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3331 | ||
84004dbf RS |
3332 | if (status == RPC_STATUS_ERROR) { |
3333 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3334 | free_nfs_cb_data(data); | |
3335 | return; | |
3336 | } | |
3337 | if (status == RPC_STATUS_CANCEL) { | |
3338 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3339 | free_nfs_cb_data(data); | |
3340 | return; | |
3341 | } | |
3342 | ||
3343 | res = command_data; | |
3344 | if (res->status != NFS3_OK) { | |
3345 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
3346 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3347 | free_nfs_cb_data(data); | |
3348 | return; | |
3349 | } | |
3350 | ||
3351 | data->cb(0, nfs, NULL, data->private_data); | |
3352 | free_nfs_cb_data(data); | |
3353 | } | |
3354 | ||
3355 | static int nfs_chmod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3356 | { | |
3357 | SETATTR3args args; | |
3358 | ||
ea98629a | 3359 | memset(&args, 0, sizeof(SETATTR3args)); |
2452c57f | 3360 | args.object = data->fh; |
84004dbf RS |
3361 | args.new_attributes.mode.set_it = 1; |
3362 | args.new_attributes.mode.set_mode3_u.mode = data->continue_int; | |
3363 | ||
b701254d | 3364 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) { |
84004dbf RS |
3365 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
3366 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3367 | free_nfs_cb_data(data); | |
3368 | return -1; | |
3369 | } | |
3370 | return 0; | |
3371 | } | |
3372 | ||
3373 | ||
3374 | int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) | |
3375 | { | |
3376 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) { | |
cbbf9d3e | 3377 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
3378 | return -1; |
3379 | } | |
3380 | ||
3381 | return 0; | |
3382 | } | |
3383 | ||
3384 | /* | |
3385 | * Async fchmod() | |
3386 | */ | |
3387 | int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data) | |
3388 | { | |
3389 | struct nfs_cb_data *data; | |
3390 | ||
3391 | data = malloc(sizeof(struct nfs_cb_data)); | |
3392 | if (data == NULL) { | |
cbbf9d3e | 3393 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data"); |
84004dbf RS |
3394 | return -1; |
3395 | } | |
ea98629a | 3396 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
3397 | data->nfs = nfs; |
3398 | data->cb = cb; | |
3399 | data->private_data = private_data; | |
3400 | data->continue_int = mode; | |
3401 | data->fh.data.data_len = nfsfh->fh.data.data_len; | |
3402 | data->fh.data.data_val = malloc(data->fh.data.data_len); | |
3403 | if (data->fh.data.data_val == NULL) { | |
3404 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh"); | |
3405 | free_nfs_cb_data(data); | |
3406 | return -1; | |
3407 | } | |
3408 | memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len); | |
3409 | ||
3410 | if (nfs_chmod_continue_internal(nfs, data) != 0) { | |
84004dbf RS |
3411 | return -1; |
3412 | } | |
3413 | ||
3414 | return 0; | |
3415 | } | |
3416 | ||
3417 | ||
3418 | ||
3419 | /* | |
3420 | * Async chown() | |
3421 | */ | |
f3a75078 | 3422 | static void nfs_chown_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3423 | { |
3424 | struct nfs_cb_data *data = private_data; | |
3425 | struct nfs_context *nfs = data->nfs; | |
3426 | SETATTR3res *res; | |
3427 | ||
f3a75078 RS |
3428 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3429 | ||
84004dbf RS |
3430 | if (status == RPC_STATUS_ERROR) { |
3431 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3432 | free_nfs_cb_data(data); | |
3433 | return; | |
3434 | } | |
3435 | if (status == RPC_STATUS_CANCEL) { | |
3436 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3437 | free_nfs_cb_data(data); | |
3438 | return; | |
3439 | } | |
3440 | ||
3441 | res = command_data; | |
3442 | if (res->status != NFS3_OK) { | |
3443 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
3444 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3445 | free_nfs_cb_data(data); | |
3446 | return; | |
3447 | } | |
3448 | ||
3449 | data->cb(0, nfs, NULL, data->private_data); | |
3450 | free_nfs_cb_data(data); | |
3451 | } | |
3452 | ||
3453 | struct nfs_chown_data { | |
3454 | uid_t uid; | |
3455 | gid_t gid; | |
3456 | }; | |
3457 | ||
3458 | static int nfs_chown_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3459 | { | |
3460 | SETATTR3args args; | |
3461 | struct nfs_chown_data *chown_data = data->continue_data; | |
3462 | ||
ea98629a | 3463 | memset(&args, 0, sizeof(SETATTR3args)); |
2452c57f | 3464 | args.object = data->fh; |
84004dbf RS |
3465 | if (chown_data->uid != (uid_t)-1) { |
3466 | args.new_attributes.uid.set_it = 1; | |
3467 | args.new_attributes.uid.set_uid3_u.uid = chown_data->uid; | |
3468 | } | |
3469 | if (chown_data->gid != (gid_t)-1) { | |
3470 | args.new_attributes.gid.set_it = 1; | |
3471 | args.new_attributes.gid.set_gid3_u.gid = chown_data->gid; | |
3472 | } | |
3473 | ||
b701254d | 3474 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) { |
84004dbf RS |
3475 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
3476 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3477 | free_nfs_cb_data(data); | |
3478 | return -1; | |
3479 | } | |
3480 | return 0; | |
3481 | } | |
3482 | ||
3483 | ||
3484 | int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data) | |
3485 | { | |
3486 | struct nfs_chown_data *chown_data; | |
3487 | ||
3488 | chown_data = malloc(sizeof(struct nfs_chown_data)); | |
3489 | if (chown_data == NULL) { | |
cbbf9d3e | 3490 | rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure"); |
84004dbf RS |
3491 | return -1; |
3492 | } | |
3493 | ||
3494 | chown_data->uid = uid; | |
3495 | chown_data->gid = gid; | |
3496 | ||
3497 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) { | |
cbbf9d3e | 3498 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
3499 | return -1; |
3500 | } | |
3501 | ||
3502 | return 0; | |
3503 | } | |
3504 | ||
3505 | ||
3506 | /* | |
3507 | * Async fchown() | |
3508 | */ | |
3509 | int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data) | |
3510 | { | |
3511 | struct nfs_cb_data *data; | |
3512 | struct nfs_chown_data *chown_data; | |
3513 | ||
3514 | chown_data = malloc(sizeof(struct nfs_chown_data)); | |
3515 | if (chown_data == NULL) { | |
cbbf9d3e | 3516 | rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure"); |
84004dbf RS |
3517 | return -1; |
3518 | } | |
3519 | ||
3520 | chown_data->uid = uid; | |
3521 | chown_data->gid = gid; | |
3522 | ||
84004dbf RS |
3523 | data = malloc(sizeof(struct nfs_cb_data)); |
3524 | if (data == NULL) { | |
cbbf9d3e | 3525 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data"); |
b6619d88 | 3526 | free(chown_data); |
84004dbf RS |
3527 | return -1; |
3528 | } | |
ea98629a | 3529 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
3530 | data->nfs = nfs; |
3531 | data->cb = cb; | |
3532 | data->private_data = private_data; | |
3533 | data->continue_data = chown_data; | |
6b1f14ca | 3534 | data->free_continue_data = free; |
84004dbf RS |
3535 | data->fh.data.data_len = nfsfh->fh.data.data_len; |
3536 | data->fh.data.data_val = malloc(data->fh.data.data_len); | |
3537 | if (data->fh.data.data_val == NULL) { | |
3538 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh"); | |
3539 | free_nfs_cb_data(data); | |
3540 | return -1; | |
3541 | } | |
3542 | memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len); | |
3543 | ||
84004dbf | 3544 | if (nfs_chown_continue_internal(nfs, data) != 0) { |
84004dbf RS |
3545 | return -1; |
3546 | } | |
3547 | ||
3548 | return 0; | |
3549 | } | |
3550 | ||
3551 | ||
3552 | ||
3553 | ||
3554 | ||
3555 | /* | |
3556 | * Async utimes() | |
3557 | */ | |
f3a75078 | 3558 | static void nfs_utimes_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3559 | { |
3560 | struct nfs_cb_data *data = private_data; | |
3561 | struct nfs_context *nfs = data->nfs; | |
3562 | SETATTR3res *res; | |
3563 | ||
f3a75078 RS |
3564 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3565 | ||
84004dbf RS |
3566 | if (status == RPC_STATUS_ERROR) { |
3567 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3568 | free_nfs_cb_data(data); | |
3569 | return; | |
3570 | } | |
3571 | if (status == RPC_STATUS_CANCEL) { | |
3572 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3573 | free_nfs_cb_data(data); | |
3574 | return; | |
3575 | } | |
3576 | ||
3577 | res = command_data; | |
3578 | if (res->status != NFS3_OK) { | |
3579 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
3580 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3581 | free_nfs_cb_data(data); | |
3582 | return; | |
3583 | } | |
3584 | ||
3585 | data->cb(0, nfs, NULL, data->private_data); | |
3586 | free_nfs_cb_data(data); | |
3587 | } | |
3588 | ||
3589 | static int nfs_utimes_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3590 | { | |
3591 | SETATTR3args args; | |
3592 | struct timeval *utimes_data = data->continue_data; | |
3593 | ||
ea98629a | 3594 | memset(&args, 0, sizeof(SETATTR3args)); |
2452c57f | 3595 | args.object = data->fh; |
84004dbf RS |
3596 | if (utimes_data != NULL) { |
3597 | args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME; | |
3598 | args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec; | |
3599 | args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000; | |
3600 | args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME; | |
3601 | args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec; | |
3602 | args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000; | |
3603 | } else { | |
3604 | args.new_attributes.atime.set_it = SET_TO_SERVER_TIME; | |
3605 | args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME; | |
3606 | } | |
3607 | ||
b701254d | 3608 | if (rpc_nfs3_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) { |
84004dbf RS |
3609 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); |
3610 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3611 | free_nfs_cb_data(data); | |
3612 | return -1; | |
3613 | } | |
3614 | return 0; | |
3615 | } | |
3616 | ||
3617 | ||
3618 | int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data) | |
3619 | { | |
3620 | struct timeval *new_times = NULL; | |
3621 | ||
3622 | if (times != NULL) { | |
3623 | new_times = malloc(sizeof(struct timeval)*2); | |
3624 | if (new_times == NULL) { | |
cbbf9d3e | 3625 | rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure"); |
84004dbf RS |
3626 | return -1; |
3627 | } | |
3628 | ||
3629 | memcpy(new_times, times, sizeof(struct timeval)*2); | |
3630 | } | |
3631 | ||
3632 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) { | |
cbbf9d3e | 3633 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
3634 | return -1; |
3635 | } | |
3636 | ||
3637 | return 0; | |
3638 | } | |
3639 | ||
3640 | /* | |
3641 | * Async utime() | |
3642 | */ | |
3643 | int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data) | |
3644 | { | |
3645 | struct timeval *new_times = NULL; | |
3646 | ||
3647 | if (times != NULL) { | |
3648 | new_times = malloc(sizeof(struct timeval)*2); | |
3649 | if (new_times == NULL) { | |
cbbf9d3e | 3650 | rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure"); |
84004dbf RS |
3651 | return -1; |
3652 | } | |
3653 | ||
3654 | new_times[0].tv_sec = times->actime; | |
3655 | new_times[0].tv_usec = 0; | |
3656 | new_times[1].tv_sec = times->modtime; | |
3657 | new_times[1].tv_usec = 0; | |
3658 | } | |
3659 | ||
3660 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) { | |
cbbf9d3e | 3661 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
3662 | return -1; |
3663 | } | |
3664 | ||
3665 | return 0; | |
3666 | } | |
84004dbf RS |
3667 | |
3668 | ||
3669 | /* | |
3670 | * Async access() | |
3671 | */ | |
f3a75078 | 3672 | static void nfs_access_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3673 | { |
3674 | ACCESS3res *res; | |
3675 | struct nfs_cb_data *data = private_data; | |
3676 | struct nfs_context *nfs = data->nfs; | |
3677 | unsigned int nfsmode = 0; | |
3678 | ||
f3a75078 RS |
3679 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3680 | ||
84004dbf RS |
3681 | if (status == RPC_STATUS_ERROR) { |
3682 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3683 | free_nfs_cb_data(data); | |
3684 | return; | |
3685 | } | |
3686 | if (status == RPC_STATUS_CANCEL) { | |
3687 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3688 | free_nfs_cb_data(data); | |
3689 | return; | |
3690 | } | |
3691 | ||
3692 | res = command_data; | |
3693 | if (res->status != NFS3_OK) { | |
3694 | 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)); | |
3695 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3696 | free_nfs_cb_data(data); | |
3697 | return; | |
3698 | } | |
3699 | ||
3700 | if (data->continue_int & R_OK) { | |
3701 | nfsmode |= ACCESS3_READ; | |
3702 | } | |
3703 | if (data->continue_int & W_OK) { | |
3704 | nfsmode |= ACCESS3_MODIFY; | |
3705 | } | |
3706 | if (data->continue_int & X_OK) { | |
3707 | nfsmode |= ACCESS3_EXECUTE; | |
3708 | } | |
3709 | ||
3710 | if (res->ACCESS3res_u.resok.access != nfsmode) { | |
3711 | rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c", | |
3712 | nfsmode&ACCESS3_READ?'r':'-', | |
3713 | nfsmode&ACCESS3_MODIFY?'w':'-', | |
3714 | nfsmode&ACCESS3_EXECUTE?'x':'-', | |
3715 | res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-', | |
3716 | res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-', | |
3717 | res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-'); | |
3718 | data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3719 | free_nfs_cb_data(data); | |
3720 | return; | |
3721 | } | |
3722 | ||
3723 | data->cb(0, nfs, NULL, data->private_data); | |
3724 | free_nfs_cb_data(data); | |
3725 | } | |
3726 | ||
3727 | static int nfs_access_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3728 | { | |
3729 | int nfsmode = 0; | |
463d59bf | 3730 | ACCESS3args args; |
84004dbf RS |
3731 | |
3732 | if (data->continue_int & R_OK) { | |
3733 | nfsmode |= ACCESS3_READ; | |
3734 | } | |
3735 | if (data->continue_int & W_OK) { | |
3736 | nfsmode |= ACCESS3_MODIFY; | |
3737 | } | |
3738 | if (data->continue_int & X_OK) { | |
3739 | nfsmode |= ACCESS3_EXECUTE; | |
3740 | } | |
3741 | ||
463d59bf | 3742 | memset(&args, 0, sizeof(ACCESS3args)); |
2452c57f | 3743 | args.object = data->fh; |
463d59bf RS |
3744 | args.access = nfsmode; |
3745 | ||
3746 | if (rpc_nfs3_access_async(nfs->rpc, nfs_access_cb, &args, data) != 0) { | |
84004dbf RS |
3747 | rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path); |
3748 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3749 | free_nfs_cb_data(data); | |
3750 | return -1; | |
3751 | } | |
3752 | return 0; | |
3753 | } | |
3754 | ||
3755 | int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) | |
3756 | { | |
3757 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) { | |
cbbf9d3e RS |
3758 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
3759 | return -1; | |
84004dbf RS |
3760 | } |
3761 | ||
3762 | return 0; | |
3763 | } | |
3764 | ||
3765 | ||
3766 | ||
3767 | /* | |
3768 | * Async symlink() | |
3769 | */ | |
3770 | struct nfs_symlink_data { | |
3771 | char *oldpath; | |
3772 | char *newpathparent; | |
3773 | char *newpathobject; | |
3774 | }; | |
3775 | ||
3776 | static void free_nfs_symlink_data(void *mem) | |
3777 | { | |
3778 | struct nfs_symlink_data *data = mem; | |
3779 | ||
3780 | if (data->oldpath != NULL) { | |
3781 | free(data->oldpath); | |
3782 | } | |
3783 | if (data->newpathparent != NULL) { | |
3784 | free(data->newpathparent); | |
3785 | } | |
3786 | if (data->newpathobject != NULL) { | |
3787 | free(data->newpathobject); | |
3788 | } | |
3789 | free(data); | |
3790 | } | |
3791 | ||
f3a75078 | 3792 | static void nfs_symlink_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3793 | { |
3794 | SYMLINK3res *res; | |
3795 | struct nfs_cb_data *data = private_data; | |
3796 | struct nfs_context *nfs = data->nfs; | |
3797 | struct nfs_symlink_data *symlink_data = data->continue_data; | |
4b1ae88a | 3798 | |
f3a75078 RS |
3799 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3800 | ||
84004dbf RS |
3801 | if (status == RPC_STATUS_ERROR) { |
3802 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3803 | free_nfs_cb_data(data); | |
3804 | return; | |
3805 | } | |
3806 | if (status == RPC_STATUS_CANCEL) { | |
3807 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3808 | free_nfs_cb_data(data); | |
3809 | return; | |
3810 | } | |
3811 | ||
3812 | res = command_data; | |
3813 | if (res->status != NFS3_OK) { | |
3814 | 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)); | |
3815 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3816 | free_nfs_cb_data(data); | |
3817 | return; | |
3818 | } | |
3819 | ||
3820 | data->cb(0, nfs, NULL, data->private_data); | |
3821 | free_nfs_cb_data(data); | |
3822 | } | |
3823 | ||
3824 | static int nfs_symlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3825 | { | |
3826 | struct nfs_symlink_data *symlink_data = data->continue_data; | |
87f81c85 | 3827 | SYMLINK3args args; |
84004dbf | 3828 | |
87f81c85 | 3829 | memset(&args, 0, sizeof(SYMLINK3args)); |
2452c57f | 3830 | args.where.dir = data->fh; |
87f81c85 RS |
3831 | args.where.name = symlink_data->newpathobject; |
3832 | args.symlink.symlink_attributes.mode.set_it = 1; | |
3833 | args.symlink.symlink_attributes.mode.set_mode3_u.mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH; | |
3834 | args.symlink.symlink_data = symlink_data->oldpath; | |
8e255816 | 3835 | |
87f81c85 | 3836 | if (rpc_nfs3_symlink_async(nfs->rpc, nfs_symlink_cb, &args, data) != 0) { |
84004dbf RS |
3837 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path); |
3838 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3839 | free_nfs_cb_data(data); | |
3840 | return -1; | |
3841 | } | |
3842 | return 0; | |
3843 | } | |
3844 | ||
3845 | int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) | |
3846 | { | |
3847 | char *ptr; | |
3848 | struct nfs_symlink_data *symlink_data; | |
3849 | ||
3850 | symlink_data = malloc(sizeof(struct nfs_symlink_data)); | |
3851 | if (symlink_data == NULL) { | |
cbbf9d3e | 3852 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data"); |
84004dbf RS |
3853 | return -1; |
3854 | } | |
ea98629a | 3855 | memset(symlink_data, 0, sizeof(struct nfs_symlink_data)); |
84004dbf RS |
3856 | |
3857 | symlink_data->oldpath = strdup(oldpath); | |
3858 | if (symlink_data->oldpath == NULL) { | |
cbbf9d3e | 3859 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
84004dbf | 3860 | free_nfs_symlink_data(symlink_data); |
cbbf9d3e | 3861 | return -1; |
84004dbf RS |
3862 | } |
3863 | ||
3864 | symlink_data->newpathparent = strdup(newpath); | |
3865 | if (symlink_data->newpathparent == NULL) { | |
cbbf9d3e | 3866 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path"); |
84004dbf | 3867 | free_nfs_symlink_data(symlink_data); |
cbbf9d3e | 3868 | return -1; |
84004dbf RS |
3869 | } |
3870 | ||
93e9306f | 3871 | ptr = strrchr(symlink_data->newpathparent, '/'); |
84004dbf | 3872 | if (ptr == NULL) { |
cbbf9d3e | 3873 | rpc_set_error(nfs->rpc, "Invalid path %s", oldpath); |
84004dbf | 3874 | free_nfs_symlink_data(symlink_data); |
cbbf9d3e | 3875 | return -1; |
84004dbf RS |
3876 | } |
3877 | *ptr = 0; | |
3878 | ptr++; | |
3879 | ||
3880 | symlink_data->newpathobject = strdup(ptr); | |
3881 | if (symlink_data->newpathobject == NULL) { | |
cbbf9d3e | 3882 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path"); |
84004dbf | 3883 | free_nfs_symlink_data(symlink_data); |
cbbf9d3e | 3884 | return -1; |
84004dbf RS |
3885 | } |
3886 | ||
3887 | 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 |
3888 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
3889 | return -1; | |
84004dbf RS |
3890 | } |
3891 | ||
3892 | return 0; | |
3893 | } | |
3894 | ||
3895 | ||
3896 | ||
3897 | /* | |
3898 | * Async rename() | |
3899 | */ | |
3900 | struct nfs_rename_data { | |
3901 | char *oldpath; | |
3902 | char *oldobject; | |
3903 | struct nfs_fh3 olddir; | |
3904 | char *newpath; | |
3905 | char *newobject; | |
3906 | struct nfs_fh3 newdir; | |
3907 | }; | |
3908 | ||
3909 | static void free_nfs_rename_data(void *mem) | |
3910 | { | |
3911 | struct nfs_rename_data *data = mem; | |
3912 | ||
3913 | if (data->oldpath != NULL) { | |
3914 | free(data->oldpath); | |
3915 | } | |
3916 | if (data->olddir.data.data_val != NULL) { | |
3917 | free(data->olddir.data.data_val); | |
3918 | } | |
3919 | if (data->newpath != NULL) { | |
3920 | free(data->newpath); | |
3921 | } | |
3922 | if (data->newdir.data.data_val != NULL) { | |
3923 | free(data->newdir.data.data_val); | |
3924 | } | |
3925 | free(data); | |
3926 | } | |
3927 | ||
f3a75078 | 3928 | static void nfs_rename_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
3929 | { |
3930 | RENAME3res *res; | |
3931 | struct nfs_cb_data *data = private_data; | |
3932 | struct nfs_context *nfs = data->nfs; | |
3933 | struct nfs_rename_data *rename_data = data->continue_data; | |
4b1ae88a | 3934 | |
f3a75078 RS |
3935 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3936 | ||
84004dbf RS |
3937 | if (status == RPC_STATUS_ERROR) { |
3938 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
3939 | free_nfs_cb_data(data); | |
3940 | return; | |
3941 | } | |
3942 | if (status == RPC_STATUS_CANCEL) { | |
3943 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
3944 | free_nfs_cb_data(data); | |
3945 | return; | |
3946 | } | |
3947 | ||
3948 | res = command_data; | |
3949 | if (res->status != NFS3_OK) { | |
3950 | 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)); | |
3951 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3952 | free_nfs_cb_data(data); | |
3953 | return; | |
3954 | } | |
3955 | ||
3956 | data->cb(0, nfs, NULL, data->private_data); | |
3957 | free_nfs_cb_data(data); | |
3958 | } | |
3959 | ||
3960 | static int nfs_rename_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3961 | { | |
3962 | struct nfs_rename_data *rename_data = data->continue_data; | |
5940c705 | 3963 | RENAME3args args; |
84004dbf RS |
3964 | |
3965 | /* steal the filehandle */ | |
2452c57f | 3966 | rename_data->newdir = data->fh; |
84004dbf RS |
3967 | data->fh.data.data_val = NULL; |
3968 | ||
5940c705 RS |
3969 | args.from.dir = rename_data->olddir; |
3970 | args.from.name = rename_data->oldobject; | |
3971 | args.to.dir = rename_data->newdir; | |
3972 | args.to.name = rename_data->newobject; | |
3973 | if (rpc_nfs3_rename_async(nfs->rpc, nfs_rename_cb, &args, data) != 0) { | |
84004dbf RS |
3974 | rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path); |
3975 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3976 | free_nfs_cb_data(data); | |
3977 | return -1; | |
3978 | } | |
3979 | return 0; | |
3980 | } | |
3981 | ||
3982 | ||
3983 | static int nfs_rename_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
3984 | { | |
3985 | struct nfs_rename_data *rename_data = data->continue_data; | |
766bb4af AR |
3986 | char* newpath = strdup(rename_data->newpath); |
3987 | if (!newpath) { | |
3988 | rpc_set_error(nfs->rpc, "Out of memory. Could not allocate memory to store target path for rename"); | |
3989 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
3990 | free_nfs_cb_data(data); | |
3991 | return -1; | |
3992 | } | |
84004dbf RS |
3993 | |
3994 | /* steal the filehandle */ | |
2452c57f | 3995 | rename_data->olddir = data->fh; |
84004dbf RS |
3996 | data->fh.data.data_val = NULL; |
3997 | ||
3998 | 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) { | |
766bb4af | 3999 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", newpath); |
84004dbf RS |
4000 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
4001 | free_nfs_cb_data(data); | |
766bb4af | 4002 | free(newpath); |
84004dbf RS |
4003 | return -1; |
4004 | } | |
4005 | data->continue_data = NULL; | |
4006 | free_nfs_cb_data(data); | |
766bb4af | 4007 | free(newpath); |
84004dbf RS |
4008 | |
4009 | return 0; | |
4010 | } | |
4011 | ||
4012 | ||
4013 | int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) | |
4014 | { | |
4015 | char *ptr; | |
4016 | struct nfs_rename_data *rename_data; | |
4017 | ||
4018 | rename_data = malloc(sizeof(struct nfs_rename_data)); | |
4019 | if (rename_data == NULL) { | |
cbbf9d3e | 4020 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data"); |
84004dbf RS |
4021 | return -1; |
4022 | } | |
ea98629a | 4023 | memset(rename_data, 0, sizeof(struct nfs_rename_data)); |
84004dbf RS |
4024 | |
4025 | rename_data->oldpath = strdup(oldpath); | |
4026 | if (rename_data->oldpath == NULL) { | |
cbbf9d3e | 4027 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
84004dbf | 4028 | free_nfs_rename_data(rename_data); |
cbbf9d3e | 4029 | return -1; |
84004dbf | 4030 | } |
93e9306f | 4031 | ptr = strrchr(rename_data->oldpath, '/'); |
84004dbf | 4032 | if (ptr == NULL) { |
cbbf9d3e | 4033 | rpc_set_error(nfs->rpc, "Invalid path %s", oldpath); |
84004dbf | 4034 | free_nfs_rename_data(rename_data); |
cbbf9d3e | 4035 | return -1; |
84004dbf RS |
4036 | } |
4037 | *ptr = 0; | |
4038 | ptr++; | |
4039 | rename_data->oldobject = ptr; | |
4040 | ||
4041 | ||
4042 | rename_data->newpath = strdup(newpath); | |
4043 | if (rename_data->newpath == NULL) { | |
cbbf9d3e | 4044 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath"); |
84004dbf | 4045 | free_nfs_rename_data(rename_data); |
cbbf9d3e | 4046 | return -1; |
84004dbf | 4047 | } |
93e9306f | 4048 | ptr = strrchr(rename_data->newpath, '/'); |
84004dbf | 4049 | if (ptr == NULL) { |
cbbf9d3e | 4050 | rpc_set_error(nfs->rpc, "Invalid path %s", newpath); |
84004dbf | 4051 | free_nfs_rename_data(rename_data); |
cbbf9d3e | 4052 | return -1; |
84004dbf RS |
4053 | } |
4054 | *ptr = 0; | |
4055 | ptr++; | |
4056 | rename_data->newobject = ptr; | |
4057 | ||
4058 | ||
4059 | 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 |
4060 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
4061 | return -1; | |
84004dbf RS |
4062 | } |
4063 | ||
4064 | return 0; | |
4065 | } | |
4066 | ||
4067 | ||
4068 | /* | |
4069 | * Async link() | |
4070 | */ | |
4071 | struct nfs_link_data { | |
4072 | char *oldpath; | |
4073 | struct nfs_fh3 oldfh; | |
4074 | char *newpath; | |
4075 | char *newobject; | |
4076 | struct nfs_fh3 newdir; | |
4077 | }; | |
4078 | ||
4079 | static void free_nfs_link_data(void *mem) | |
4080 | { | |
4081 | struct nfs_link_data *data = mem; | |
4082 | ||
4083 | if (data->oldpath != NULL) { | |
4084 | free(data->oldpath); | |
4085 | } | |
4086 | if (data->oldfh.data.data_val != NULL) { | |
4087 | free(data->oldfh.data.data_val); | |
4088 | } | |
4089 | if (data->newpath != NULL) { | |
4090 | free(data->newpath); | |
4091 | } | |
4092 | if (data->newdir.data.data_val != NULL) { | |
4093 | free(data->newdir.data.data_val); | |
4094 | } | |
4095 | free(data); | |
4096 | } | |
4097 | ||
f3a75078 | 4098 | static void nfs_link_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
4099 | { |
4100 | LINK3res *res; | |
4101 | struct nfs_cb_data *data = private_data; | |
4102 | struct nfs_context *nfs = data->nfs; | |
4103 | struct nfs_link_data *link_data = data->continue_data; | |
4b1ae88a | 4104 | |
f3a75078 RS |
4105 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4106 | ||
84004dbf RS |
4107 | if (status == RPC_STATUS_ERROR) { |
4108 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
4109 | free_nfs_cb_data(data); | |
4110 | return; | |
4111 | } | |
4112 | if (status == RPC_STATUS_CANCEL) { | |
4113 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
4114 | free_nfs_cb_data(data); | |
4115 | return; | |
4116 | } | |
4117 | ||
4118 | res = command_data; | |
4119 | if (res->status != NFS3_OK) { | |
4120 | 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)); | |
4121 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
4122 | free_nfs_cb_data(data); | |
4123 | return; | |
4124 | } | |
4125 | ||
4126 | data->cb(0, nfs, NULL, data->private_data); | |
4127 | free_nfs_cb_data(data); | |
4128 | } | |
4129 | ||
4130 | static int nfs_link_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
4131 | { | |
4132 | struct nfs_link_data *link_data = data->continue_data; | |
560f9434 | 4133 | LINK3args args; |
84004dbf RS |
4134 | |
4135 | /* steal the filehandle */ | |
2452c57f | 4136 | link_data->newdir = data->fh; |
84004dbf RS |
4137 | data->fh.data.data_val = NULL; |
4138 | ||
560f9434 RS |
4139 | memset(&args, 0, sizeof(LINK3args)); |
4140 | args.file = link_data->oldfh; | |
4141 | args.link.dir = link_data->newdir; | |
4142 | args.link.name = link_data->newobject; | |
4143 | if (rpc_nfs3_link_async(nfs->rpc, nfs_link_cb, &args, data) != 0) { | |
84004dbf RS |
4144 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path); |
4145 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
4146 | free_nfs_cb_data(data); | |
4147 | return -1; | |
4148 | } | |
4149 | return 0; | |
4150 | } | |
4151 | ||
4152 | ||
4153 | static int nfs_link_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
4154 | { | |
4155 | struct nfs_link_data *link_data = data->continue_data; | |
4156 | ||
4157 | /* steal the filehandle */ | |
2452c57f | 4158 | link_data->oldfh = data->fh; |
84004dbf RS |
4159 | data->fh.data.data_val = NULL; |
4160 | ||
4161 | 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) { | |
4162 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath); | |
4163 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
4164 | free_nfs_cb_data(data); | |
4165 | return -1; | |
4166 | } | |
4167 | data->continue_data = NULL; | |
4168 | free_nfs_cb_data(data); | |
4169 | ||
4170 | return 0; | |
4171 | } | |
4172 | ||
4173 | ||
4174 | int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) | |
4175 | { | |
4176 | char *ptr; | |
4177 | struct nfs_link_data *link_data; | |
4178 | ||
4179 | link_data = malloc(sizeof(struct nfs_link_data)); | |
4180 | if (link_data == NULL) { | |
cbbf9d3e | 4181 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data"); |
84004dbf RS |
4182 | return -1; |
4183 | } | |
ea98629a | 4184 | memset(link_data, 0, sizeof(struct nfs_link_data)); |
84004dbf RS |
4185 | |
4186 | link_data->oldpath = strdup(oldpath); | |
4187 | if (link_data->oldpath == NULL) { | |
cbbf9d3e | 4188 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
84004dbf | 4189 | free_nfs_link_data(link_data); |
cbbf9d3e | 4190 | return -1; |
84004dbf RS |
4191 | } |
4192 | ||
4193 | link_data->newpath = strdup(newpath); | |
4194 | if (link_data->newpath == NULL) { | |
cbbf9d3e | 4195 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath"); |
84004dbf | 4196 | free_nfs_link_data(link_data); |
cbbf9d3e | 4197 | return -1; |
84004dbf | 4198 | } |
93e9306f | 4199 | ptr = strrchr(link_data->newpath, '/'); |
84004dbf | 4200 | if (ptr == NULL) { |
cbbf9d3e | 4201 | rpc_set_error(nfs->rpc, "Invalid path %s", newpath); |
84004dbf | 4202 | free_nfs_link_data(link_data); |
cbbf9d3e | 4203 | return -1; |
84004dbf RS |
4204 | } |
4205 | *ptr = 0; | |
4206 | ptr++; | |
4207 | link_data->newobject = ptr; | |
4208 | ||
4209 | ||
4210 | 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 |
4211 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
4212 | return -1; | |
84004dbf RS |
4213 | } |
4214 | ||
4215 | return 0; | |
4216 | } | |
4217 | ||
4218 | ||
4219 | //qqq replace later with lseek() | |
183451cf | 4220 | uint64_t nfs_get_current_offset(struct nfsfh *nfsfh) |
84004dbf RS |
4221 | { |
4222 | return nfsfh->offset; | |
4223 | } | |
4224 | ||
17ef62fa RS |
4225 | |
4226 | ||
4227 | /* | |
4228 | * Get the maximum supported READ3 size by the server | |
4229 | */ | |
183451cf | 4230 | uint64_t nfs_get_readmax(struct nfs_context *nfs) |
17ef62fa RS |
4231 | { |
4232 | return nfs->readmax; | |
4233 | } | |
4234 | ||
4235 | /* | |
4236 | * Get the maximum supported WRITE3 size by the server | |
4237 | */ | |
183451cf | 4238 | uint64_t nfs_get_writemax(struct nfs_context *nfs) |
17ef62fa | 4239 | { |
e7f3a781 | 4240 | return nfs->writemax; |
17ef62fa | 4241 | } |
1896d37b | 4242 | |
d43a8953 PL |
4243 | void nfs_set_tcp_syncnt(struct nfs_context *nfs, int v) { |
4244 | rpc_set_tcp_syncnt(nfs->rpc, v); | |
4245 | } | |
4246 | ||
4247 | void nfs_set_uid(struct nfs_context *nfs, int uid) { | |
4248 | rpc_set_uid(nfs->rpc, uid); | |
4249 | } | |
4250 | ||
4251 | void nfs_set_gid(struct nfs_context *nfs, int gid) { | |
4252 | rpc_set_gid(nfs->rpc, gid); | |
4253 | } | |
4254 | ||
1896d37b RS |
4255 | void nfs_set_error(struct nfs_context *nfs, char *error_string, ...) |
4256 | { | |
4257 | va_list ap; | |
1e8994af | 4258 | char *str = NULL; |
1896d37b | 4259 | |
1e8994af | 4260 | va_start(ap, error_string); |
2606f9bb RS |
4261 | str = malloc(1024); |
4262 | vsnprintf(str, 1024, error_string, ap); | |
1896d37b RS |
4263 | if (nfs->rpc->error_string != NULL) { |
4264 | free(nfs->rpc->error_string); | |
4265 | } | |
1896d37b | 4266 | nfs->rpc->error_string = str; |
f893b680 | 4267 | va_end(ap); |
1896d37b | 4268 | } |
7f0242ca RS |
4269 | |
4270 | ||
4271 | ||
4272 | struct mount_cb_data { | |
4273 | rpc_cb cb; | |
4274 | void *private_data; | |
4275 | char *server; | |
4276 | }; | |
4277 | ||
4278 | static void free_mount_cb_data(struct mount_cb_data *data) | |
4279 | { | |
4280 | if (data->server != NULL) { | |
4281 | free(data->server); | |
4282 | data->server = NULL; | |
4283 | } | |
4284 | ||
4285 | free(data); | |
4286 | } | |
4287 | ||
4288 | static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
4289 | { | |
4290 | struct mount_cb_data *data = private_data; | |
4291 | ||
f3a75078 RS |
4292 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4293 | ||
4294 | if (status == RPC_STATUS_ERROR) { | |
7f0242ca RS |
4295 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
4296 | free_mount_cb_data(data); | |
4297 | return; | |
4298 | } | |
4299 | if (status == RPC_STATUS_CANCEL) { | |
4300 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
4301 | free_mount_cb_data(data); | |
4302 | return; | |
4303 | } | |
4304 | ||
4305 | data->cb(rpc, 0, command_data, data->private_data); | |
4306 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { | |
4307 | rpc_set_error(rpc, "Failed to disconnect\n"); | |
4308 | } | |
4309 | free_mount_cb_data(data); | |
4310 | } | |
4311 | ||
4312 | static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
4313 | { | |
4314 | struct mount_cb_data *data = private_data; | |
4315 | ||
f3a75078 RS |
4316 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4317 | ||
14a062eb RS |
4318 | /* Dont want any more callbacks even if the socket is closed */ |
4319 | rpc->connect_cb = NULL; | |
4320 | ||
4b1ae88a | 4321 | if (status == RPC_STATUS_ERROR) { |
7f0242ca RS |
4322 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
4323 | free_mount_cb_data(data); | |
4324 | return; | |
4325 | } | |
4326 | if (status == RPC_STATUS_CANCEL) { | |
4327 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
4328 | free_mount_cb_data(data); | |
4329 | return; | |
4330 | } | |
4331 | ||
eda77ec3 | 4332 | if (rpc_mount3_export_async(rpc, mount_export_5_cb, data) != 0) { |
7f0242ca RS |
4333 | data->cb(rpc, -ENOMEM, command_data, data->private_data); |
4334 | free_mount_cb_data(data); | |
4335 | return; | |
4336 | } | |
4337 | } | |
4338 | ||
4339 | static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
4340 | { | |
4341 | struct mount_cb_data *data = private_data; | |
4342 | uint32_t mount_port; | |
4343 | ||
f3a75078 RS |
4344 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4345 | ||
4346 | if (status == RPC_STATUS_ERROR) { | |
7f0242ca RS |
4347 | data->cb(rpc, -EFAULT, command_data, data->private_data); |
4348 | free_mount_cb_data(data); | |
4349 | return; | |
4350 | } | |
4351 | if (status == RPC_STATUS_CANCEL) { | |
4352 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
4353 | free_mount_cb_data(data); | |
4354 | return; | |
4355 | } | |
4356 | ||
4357 | mount_port = *(uint32_t *)command_data; | |
4358 | if (mount_port == 0) { | |
4359 | rpc_set_error(rpc, "RPC error. Mount program is not available"); | |
4360 | data->cb(rpc, -ENOENT, command_data, data->private_data); | |
4361 | free_mount_cb_data(data); | |
4362 | return; | |
4363 | } | |
4364 | ||
4365 | rpc_disconnect(rpc, "normal disconnect"); | |
4366 | if (rpc_connect_async(rpc, data->server, mount_port, mount_export_4_cb, data) != 0) { | |
4367 | data->cb(rpc, -ENOMEM, command_data, data->private_data); | |
4368 | free_mount_cb_data(data); | |
4369 | return; | |
4370 | } | |
4371 | } | |
4372 | ||
4373 | static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
4374 | { | |
4375 | struct mount_cb_data *data = private_data; | |
4376 | ||
f3a75078 RS |
4377 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4378 | ||
7f0242ca RS |
4379 | if (status == RPC_STATUS_ERROR) { |
4380 | data->cb(rpc, -EFAULT, command_data, data->private_data); | |
4381 | free_mount_cb_data(data); | |
4382 | return; | |
4383 | } | |
4384 | if (status == RPC_STATUS_CANCEL) { | |
4385 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
4386 | free_mount_cb_data(data); | |
4387 | return; | |
4388 | } | |
4389 | ||
5c6b1176 | 4390 | if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, mount_export_3_cb, private_data) != 0) { |
7f0242ca RS |
4391 | data->cb(rpc, -ENOMEM, command_data, data->private_data); |
4392 | free_mount_cb_data(data); | |
4393 | return; | |
4394 | } | |
4395 | } | |
4396 | ||
4397 | static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
4398 | { | |
4399 | struct mount_cb_data *data = private_data; | |
4400 | ||
f3a75078 RS |
4401 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4402 | ||
14a062eb RS |
4403 | /* Dont want any more callbacks even if the socket is closed */ |
4404 | rpc->connect_cb = NULL; | |
4405 | ||
7f0242ca RS |
4406 | if (status == RPC_STATUS_ERROR) { |
4407 | data->cb(rpc, -EFAULT, command_data, data->private_data); | |
4408 | free_mount_cb_data(data); | |
4409 | return; | |
4410 | } | |
4411 | if (status == RPC_STATUS_CANCEL) { | |
4412 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
4413 | free_mount_cb_data(data); | |
4414 | return; | |
4415 | } | |
4416 | ||
4417 | if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) { | |
4418 | data->cb(rpc, -ENOMEM, command_data, data->private_data); | |
4419 | free_mount_cb_data(data); | |
4420 | return; | |
4421 | } | |
4422 | } | |
4423 | ||
4424 | int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data) | |
4425 | { | |
4426 | struct mount_cb_data *data; | |
4427 | ||
f3a75078 RS |
4428 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4429 | ||
7f0242ca RS |
4430 | data = malloc(sizeof(struct mount_cb_data)); |
4431 | if (data == NULL) { | |
4432 | return -1; | |
4433 | } | |
ea98629a | 4434 | memset(data, 0, sizeof(struct mount_cb_data)); |
7f0242ca RS |
4435 | data->cb = cb; |
4436 | data->private_data = private_data; | |
4437 | data->server = strdup(server); | |
4438 | if (data->server == NULL) { | |
4439 | free_mount_cb_data(data); | |
4440 | return -1; | |
4b1ae88a | 4441 | } |
7f0242ca RS |
4442 | if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) { |
4443 | free_mount_cb_data(data); | |
4444 | return -1; | |
4445 | } | |
4446 | ||
4447 | return 0; | |
4448 | } | |
4449 | ||
df5af25f RS |
4450 | struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs) |
4451 | { | |
f3a75078 | 4452 | assert(nfs->rpc->magic == RPC_CONTEXT_MAGIC); |
df5af25f RS |
4453 | return nfs->rpc; |
4454 | } | |
4455 | ||
b077fdeb RS |
4456 | const char *nfs_get_server(struct nfs_context *nfs) { |
4457 | return nfs->server; | |
4458 | } | |
4459 | ||
4460 | const char *nfs_get_export(struct nfs_context *nfs) { | |
4461 | return nfs->export; | |
4462 | } | |
0ad9a1f1 | 4463 | |
8724c833 RS |
4464 | const struct nfs_fh3 *nfs_get_rootfh(struct nfs_context *nfs) { |
4465 | return &nfs->rootfh; | |
4466 | } | |
fa3c25be RS |
4467 | |
4468 | struct nfs_fh3 *nfs_get_fh(struct nfsfh *nfsfh) { | |
4469 | return &nfsfh->fh; | |
4470 | } |