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