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