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