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