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