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 | */ | |
a8a1b858 M |
20 | #ifdef WIN32 |
21 | #include "win32_compat.h" | |
6874f61e | 22 | #define DllExport |
6874f61e RS |
23 | #else |
24 | #include <strings.h> | |
25 | #include <sys/statvfs.h> | |
26 | #include <utime.h> | |
27 | #include <unistd.h> | |
a8a1b858 M |
28 | #endif/*WIN32*/ |
29 | ||
30 | #define _GNU_SOURCE | |
6874f61e | 31 | |
84004dbf | 32 | #include <stdio.h> |
1896d37b | 33 | #include <stdarg.h> |
84004dbf RS |
34 | #include <stdlib.h> |
35 | #include <string.h> | |
84004dbf RS |
36 | #include <errno.h> |
37 | #include <sys/types.h> | |
38 | #include <sys/stat.h> | |
84004dbf RS |
39 | #include <fcntl.h> |
40 | #include "libnfs.h" | |
41 | #include "libnfs-raw.h" | |
42 | #include "libnfs-raw-mount.h" | |
43 | #include "libnfs-raw-nfs.h" | |
1896d37b RS |
44 | #include "libnfs-private.h" |
45 | ||
46 | struct nfsdir { | |
47 | struct nfsdirent *entries; | |
48 | struct nfsdirent *current; | |
49 | }; | |
84004dbf RS |
50 | |
51 | struct nfsfh { | |
52 | struct nfs_fh3 fh; | |
53 | int is_sync; | |
54 | off_t offset; | |
55 | }; | |
56 | ||
1896d37b RS |
57 | struct nfs_context { |
58 | struct rpc_context *rpc; | |
59 | char *server; | |
60 | char *export; | |
61 | struct nfs_fh3 rootfh; | |
62 | size_t readmax; | |
63 | size_t writemax; | |
84004dbf RS |
64 | }; |
65 | ||
66 | void nfs_free_nfsdir(struct nfsdir *nfsdir) | |
67 | { | |
68 | while (nfsdir->entries) { | |
69 | struct nfsdirent *dirent = nfsdir->entries->next; | |
70 | if (nfsdir->entries->name != NULL) { | |
71 | free(nfsdir->entries->name); | |
72 | } | |
73 | free(nfsdir->entries); | |
74 | nfsdir->entries = dirent; | |
75 | } | |
76 | free(nfsdir); | |
77 | } | |
78 | ||
84004dbf RS |
79 | struct nfs_cb_data; |
80 | typedef int (*continue_func)(struct nfs_context *nfs, struct nfs_cb_data *data); | |
81 | ||
82 | struct nfs_cb_data { | |
83 | struct nfs_context *nfs; | |
84 | struct nfsfh *nfsfh; | |
85 | char *saved_path, *path; | |
86 | ||
87 | nfs_cb cb; | |
88 | void *private_data; | |
89 | ||
90 | continue_func continue_cb; | |
91 | void *continue_data; | |
92 | void (*free_continue_data)(void *); | |
93 | int continue_int; | |
94 | ||
95 | struct nfs_fh3 fh; | |
921f877b RS |
96 | |
97 | /* for multi-read/write calls. */ | |
98 | int error; | |
99 | int cancel; | |
100 | int num_calls; | |
101 | off_t start_offset, max_offset; | |
102 | char *buffer; | |
103 | }; | |
104 | ||
105 | struct nfs_mcb_data { | |
106 | struct nfs_cb_data *data; | |
107 | off_t offset; | |
108 | size_t count; | |
84004dbf RS |
109 | }; |
110 | ||
111 | static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh); | |
112 | ||
113 | ||
114 | void nfs_set_auth(struct nfs_context *nfs, struct AUTH *auth) | |
115 | { | |
9896c715 | 116 | rpc_set_auth(nfs->rpc, auth); |
84004dbf RS |
117 | } |
118 | ||
119 | int nfs_get_fd(struct nfs_context *nfs) | |
120 | { | |
121 | return rpc_get_fd(nfs->rpc); | |
122 | } | |
123 | ||
83aa785d RS |
124 | int nfs_queue_length(struct nfs_context *nfs) |
125 | { | |
126 | return rpc_queue_length(nfs->rpc); | |
127 | } | |
128 | ||
84004dbf RS |
129 | int nfs_which_events(struct nfs_context *nfs) |
130 | { | |
131 | return rpc_which_events(nfs->rpc); | |
132 | } | |
133 | ||
134 | int nfs_service(struct nfs_context *nfs, int revents) | |
135 | { | |
136 | return rpc_service(nfs->rpc, revents); | |
137 | } | |
138 | ||
139 | char *nfs_get_error(struct nfs_context *nfs) | |
140 | { | |
141 | return rpc_get_error(nfs->rpc); | |
142 | }; | |
143 | ||
144 | struct nfs_context *nfs_init_context(void) | |
145 | { | |
146 | struct nfs_context *nfs; | |
147 | ||
148 | nfs = malloc(sizeof(struct nfs_context)); | |
149 | if (nfs == NULL) { | |
84004dbf RS |
150 | return NULL; |
151 | } | |
152 | nfs->rpc = rpc_init_context(); | |
153 | if (nfs->rpc == NULL) { | |
84004dbf RS |
154 | free(nfs); |
155 | return NULL; | |
156 | } | |
157 | ||
b077fdeb RS |
158 | nfs->server = NULL; |
159 | nfs->export = NULL; | |
160 | ||
963c2f83 RS |
161 | nfs->rootfh.data.data_len = 0; |
162 | nfs->rootfh.data.data_val = NULL; | |
163 | ||
84004dbf RS |
164 | return nfs; |
165 | } | |
166 | ||
167 | void nfs_destroy_context(struct nfs_context *nfs) | |
168 | { | |
169 | rpc_destroy_context(nfs->rpc); | |
170 | nfs->rpc = NULL; | |
171 | ||
172 | if (nfs->server) { | |
173 | free(nfs->server); | |
174 | nfs->server = NULL; | |
175 | } | |
176 | ||
177 | if (nfs->export) { | |
178 | free(nfs->export); | |
179 | nfs->export = NULL; | |
180 | } | |
181 | ||
182 | if (nfs->rootfh.data.data_val != NULL) { | |
183 | free(nfs->rootfh.data.data_val); | |
184 | nfs->rootfh.data.data_val = NULL; | |
185 | } | |
186 | ||
187 | free(nfs); | |
188 | } | |
189 | ||
190 | void free_nfs_cb_data(struct nfs_cb_data *data) | |
191 | { | |
192 | if (data->saved_path != NULL) { | |
193 | free(data->saved_path); | |
194 | data->saved_path = NULL; | |
195 | } | |
196 | ||
197 | if (data->continue_data != NULL) { | |
198 | data->free_continue_data(data->continue_data); | |
199 | data->continue_data = NULL; | |
200 | } | |
201 | ||
202 | if (data->fh.data.data_val != NULL) { | |
203 | free(data->fh.data.data_val); | |
204 | data->fh.data.data_val = NULL; | |
205 | } | |
206 | ||
921f877b RS |
207 | if (data->buffer != NULL) { |
208 | free(data->buffer); | |
209 | data->buffer = NULL; | |
210 | } | |
211 | ||
84004dbf RS |
212 | free(data); |
213 | } | |
214 | ||
215 | ||
216 | ||
217 | ||
218 | ||
17ef62fa | 219 | static void nfs_mount_10_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) |
84004dbf RS |
220 | { |
221 | struct nfs_cb_data *data = private_data; | |
222 | struct nfs_context *nfs = data->nfs; | |
223 | ||
224 | if (status == RPC_STATUS_ERROR) { | |
225 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
226 | free_nfs_cb_data(data); | |
227 | return; | |
228 | } | |
229 | if (status == RPC_STATUS_CANCEL) { | |
230 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
231 | free_nfs_cb_data(data); | |
232 | return; | |
233 | } | |
234 | ||
235 | data->cb(0, nfs, NULL, data->private_data); | |
236 | free_nfs_cb_data(data); | |
237 | } | |
238 | ||
17ef62fa | 239 | static void nfs_mount_9_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
84004dbf RS |
240 | { |
241 | struct nfs_cb_data *data = private_data; | |
242 | struct nfs_context *nfs = data->nfs; | |
17ef62fa | 243 | FSINFO3res *res = command_data; |
84004dbf RS |
244 | |
245 | if (status == RPC_STATUS_ERROR) { | |
246 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
247 | free_nfs_cb_data(data); | |
248 | return; | |
249 | } | |
250 | if (status == RPC_STATUS_CANCEL) { | |
251 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
252 | free_nfs_cb_data(data); | |
253 | return; | |
254 | } | |
255 | ||
17ef62fa RS |
256 | nfs->readmax = res->FSINFO3res_u.resok.rtmax; |
257 | nfs->writemax = res->FSINFO3res_u.resok.wtmax; | |
84004dbf | 258 | |
17ef62fa | 259 | if (rpc_nfs_getattr_async(rpc, nfs_mount_10_cb, &nfs->rootfh, data) != 0) { |
84004dbf RS |
260 | data->cb(-ENOMEM, nfs, command_data, data->private_data); |
261 | free_nfs_cb_data(data); | |
262 | return; | |
263 | } | |
264 | } | |
265 | ||
17ef62fa RS |
266 | static void nfs_mount_8_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
267 | { | |
268 | struct nfs_cb_data *data = private_data; | |
269 | struct nfs_context *nfs = data->nfs; | |
270 | ||
271 | if (status == RPC_STATUS_ERROR) { | |
272 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
273 | free_nfs_cb_data(data); | |
274 | return; | |
275 | } | |
276 | if (status == RPC_STATUS_CANCEL) { | |
277 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
278 | free_nfs_cb_data(data); | |
279 | return; | |
280 | } | |
281 | ||
282 | if (rpc_nfs_fsinfo_async(rpc, nfs_mount_9_cb, &nfs->rootfh, data) != 0) { | |
283 | data->cb(-ENOMEM, nfs, command_data, data->private_data); | |
284 | free_nfs_cb_data(data); | |
285 | return; | |
286 | } | |
287 | } | |
288 | ||
289 | ||
84004dbf RS |
290 | static void nfs_mount_7_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) |
291 | { | |
292 | struct nfs_cb_data *data = private_data; | |
293 | struct nfs_context *nfs = data->nfs; | |
294 | ||
295 | if (status == RPC_STATUS_ERROR) { | |
296 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
297 | free_nfs_cb_data(data); | |
298 | return; | |
299 | } | |
300 | if (status == RPC_STATUS_CANCEL) { | |
301 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
302 | free_nfs_cb_data(data); | |
303 | return; | |
304 | } | |
305 | ||
306 | if (rpc_nfs_null_async(rpc, nfs_mount_8_cb, data) != 0) { | |
307 | data->cb(-ENOMEM, nfs, command_data, data->private_data); | |
308 | free_nfs_cb_data(data); | |
309 | return; | |
310 | } | |
311 | } | |
312 | ||
313 | ||
314 | static void nfs_mount_6_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
315 | { | |
316 | struct nfs_cb_data *data = private_data; | |
317 | struct nfs_context *nfs = data->nfs; | |
318 | mountres3 *res; | |
319 | ||
320 | if (status == RPC_STATUS_ERROR) { | |
321 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
322 | free_nfs_cb_data(data); | |
323 | return; | |
324 | } | |
325 | if (status == RPC_STATUS_CANCEL) { | |
326 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
327 | free_nfs_cb_data(data); | |
328 | return; | |
329 | } | |
330 | ||
331 | res = command_data; | |
332 | if (res->fhs_status != MNT3_OK) { | |
333 | 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)); | |
334 | data->cb(mountstat3_to_errno(res->fhs_status), nfs, rpc_get_error(rpc), data->private_data); | |
335 | free_nfs_cb_data(data); | |
336 | return; | |
337 | } | |
338 | ||
339 | nfs->rootfh.data.data_len = res->mountres3_u.mountinfo.fhandle.fhandle3_len; | |
340 | nfs->rootfh.data.data_val = malloc(nfs->rootfh.data.data_len); | |
341 | if (nfs->rootfh.data.data_val == NULL) { | |
342 | rpc_set_error(rpc, "Out of memory. Could not allocate memory to store root filehandle"); | |
343 | data->cb(-ENOMEM, nfs, rpc_get_error(rpc), data->private_data); | |
344 | free_nfs_cb_data(data); | |
345 | return; | |
346 | } | |
347 | memcpy(nfs->rootfh.data.data_val, res->mountres3_u.mountinfo.fhandle.fhandle3_val, nfs->rootfh.data.data_len); | |
348 | ||
349 | rpc_disconnect(rpc, "normal disconnect"); | |
350 | if (rpc_connect_async(rpc, nfs->server, 2049, nfs_mount_7_cb, data) != 0) { | |
351 | data->cb(-ENOMEM, nfs, command_data, data->private_data); | |
352 | free_nfs_cb_data(data); | |
353 | return; | |
354 | } | |
355 | } | |
356 | ||
357 | ||
358 | static void nfs_mount_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
359 | { | |
360 | struct nfs_cb_data *data = private_data; | |
361 | struct nfs_context *nfs = data->nfs; | |
362 | ||
363 | if (status == RPC_STATUS_ERROR) { | |
364 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
365 | free_nfs_cb_data(data); | |
366 | return; | |
367 | } | |
368 | if (status == RPC_STATUS_CANCEL) { | |
369 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
370 | free_nfs_cb_data(data); | |
371 | return; | |
372 | } | |
373 | ||
374 | if (rpc_mount_mnt_async(rpc, nfs_mount_6_cb, nfs->export, data) != 0) { | |
375 | data->cb(-ENOMEM, nfs, command_data, data->private_data); | |
376 | free_nfs_cb_data(data); | |
377 | return; | |
378 | } | |
379 | } | |
380 | ||
381 | static void nfs_mount_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
382 | { | |
383 | struct nfs_cb_data *data = private_data; | |
384 | struct nfs_context *nfs = data->nfs; | |
385 | ||
386 | if (status == RPC_STATUS_ERROR) { | |
387 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
388 | free_nfs_cb_data(data); | |
389 | return; | |
390 | } | |
391 | if (status == RPC_STATUS_CANCEL) { | |
392 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
393 | free_nfs_cb_data(data); | |
394 | return; | |
395 | } | |
396 | ||
397 | if (rpc_mount_null_async(rpc, nfs_mount_5_cb, data) != 0) { | |
398 | data->cb(-ENOMEM, nfs, command_data, data->private_data); | |
399 | free_nfs_cb_data(data); | |
400 | return; | |
401 | } | |
402 | } | |
403 | ||
404 | static void nfs_mount_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
405 | { | |
406 | struct nfs_cb_data *data = private_data; | |
407 | struct nfs_context *nfs = data->nfs; | |
408 | uint32_t mount_port; | |
409 | ||
410 | if (status == RPC_STATUS_ERROR) { | |
411 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
412 | free_nfs_cb_data(data); | |
413 | return; | |
414 | } | |
415 | if (status == RPC_STATUS_CANCEL) { | |
416 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
417 | free_nfs_cb_data(data); | |
418 | return; | |
419 | } | |
420 | ||
421 | mount_port = *(uint32_t *)command_data; | |
422 | if (mount_port == 0) { | |
423 | rpc_set_error(rpc, "RPC error. Mount program is not available on %s", nfs->server); | |
424 | data->cb(-ENOENT, nfs, command_data, data->private_data); | |
425 | free_nfs_cb_data(data); | |
426 | return; | |
427 | } | |
428 | ||
429 | rpc_disconnect(rpc, "normal disconnect"); | |
430 | if (rpc_connect_async(rpc, nfs->server, mount_port, nfs_mount_4_cb, data) != 0) { | |
431 | data->cb(-ENOMEM, nfs, command_data, data->private_data); | |
432 | free_nfs_cb_data(data); | |
433 | return; | |
434 | } | |
435 | } | |
436 | ||
437 | ||
438 | static void nfs_mount_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
439 | { | |
440 | struct nfs_cb_data *data = private_data; | |
441 | struct nfs_context *nfs = data->nfs; | |
442 | ||
443 | if (status == RPC_STATUS_ERROR) { | |
444 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
445 | free_nfs_cb_data(data); | |
446 | return; | |
447 | } | |
448 | if (status == RPC_STATUS_CANCEL) { | |
449 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
450 | free_nfs_cb_data(data); | |
451 | return; | |
452 | } | |
453 | ||
454 | if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, nfs_mount_3_cb, private_data) != 0) { | |
455 | data->cb(-ENOMEM, nfs, command_data, data->private_data); | |
456 | free_nfs_cb_data(data); | |
457 | return; | |
458 | } | |
459 | } | |
460 | ||
461 | static void nfs_mount_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
462 | { | |
463 | struct nfs_cb_data *data = private_data; | |
464 | struct nfs_context *nfs = data->nfs; | |
465 | ||
466 | if (status == RPC_STATUS_ERROR) { | |
467 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
468 | free_nfs_cb_data(data); | |
469 | return; | |
470 | } | |
471 | if (status == RPC_STATUS_CANCEL) { | |
472 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
473 | free_nfs_cb_data(data); | |
474 | return; | |
475 | } | |
476 | ||
477 | if (rpc_pmap_null_async(rpc, nfs_mount_2_cb, data) != 0) { | |
478 | data->cb(-ENOMEM, nfs, command_data, data->private_data); | |
479 | free_nfs_cb_data(data); | |
480 | return; | |
481 | } | |
482 | } | |
483 | ||
484 | /* | |
485 | * Async call for mounting an nfs share and geting the root filehandle | |
486 | */ | |
487 | int nfs_mount_async(struct nfs_context *nfs, const char *server, const char *export, nfs_cb cb, void *private_data) | |
488 | { | |
489 | struct nfs_cb_data *data; | |
b077fdeb | 490 | char *new_server, *new_export; |
84004dbf RS |
491 | |
492 | data = malloc(sizeof(struct nfs_cb_data)); | |
493 | if (data == NULL) { | |
cbbf9d3e | 494 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data"); |
84004dbf RS |
495 | return -1; |
496 | } | |
ea98629a | 497 | memset(data, 0, sizeof(struct nfs_cb_data)); |
b077fdeb RS |
498 | new_server = strdup(server); |
499 | new_export = strdup(export); | |
500 | if (nfs->server != NULL) { | |
501 | free(nfs->server); | |
502 | nfs->server = NULL; | |
503 | } | |
504 | nfs->server = new_server; | |
505 | if (nfs->export != NULL) { | |
506 | free(nfs->export); | |
507 | nfs->export = NULL; | |
508 | } | |
509 | nfs->export = new_export; | |
84004dbf RS |
510 | data->nfs = nfs; |
511 | data->cb = cb; | |
512 | data->private_data = private_data; | |
513 | ||
514 | if (rpc_connect_async(nfs->rpc, server, 111, nfs_mount_1_cb, data) != 0) { | |
cbbf9d3e | 515 | rpc_set_error(nfs->rpc, "Failed to start connection"); |
84004dbf | 516 | free_nfs_cb_data(data); |
cbbf9d3e | 517 | return -1; |
84004dbf RS |
518 | } |
519 | ||
520 | return 0; | |
521 | } | |
522 | ||
523 | ||
524 | ||
525 | /* | |
526 | * Functions to first look up a path, component by component, and then finally call a specific function once | |
527 | * the filehandle for the final component is found. | |
528 | */ | |
529 | static void nfs_lookup_path_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
530 | { | |
531 | struct nfs_cb_data *data = private_data; | |
532 | struct nfs_context *nfs = data->nfs; | |
533 | LOOKUP3res *res; | |
534 | ||
535 | if (status == RPC_STATUS_ERROR) { | |
536 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
537 | free_nfs_cb_data(data); | |
538 | return; | |
539 | } | |
540 | if (status == RPC_STATUS_CANCEL) { | |
541 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
542 | free_nfs_cb_data(data); | |
543 | return; | |
544 | } | |
545 | ||
546 | res = command_data; | |
547 | if (res->status != NFS3_OK) { | |
548 | 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)); | |
549 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
550 | free_nfs_cb_data(data); | |
551 | return; | |
552 | } | |
553 | ||
554 | if (nfs_lookup_path_async_internal(nfs, data, &res->LOOKUP3res_u.resok.object) != 0) { | |
555 | rpc_set_error(nfs->rpc, "Failed to create lookup pdu"); | |
556 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
557 | free_nfs_cb_data(data); | |
558 | return; | |
559 | } | |
560 | } | |
561 | ||
562 | static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh) | |
563 | { | |
564 | char *path, *str; | |
565 | ||
566 | while (*data->path == '/') { | |
567 | data->path++; | |
568 | } | |
569 | ||
570 | path = data->path; | |
93e9306f | 571 | str = strchr(path, '/'); |
84004dbf RS |
572 | if (str != NULL) { |
573 | *str = 0; | |
574 | data->path = str+1; | |
575 | } else { | |
576 | while (*data->path != 0) { | |
577 | data->path++; | |
578 | } | |
579 | } | |
580 | ||
581 | if (*path == 0) { | |
582 | data->fh.data.data_len = fh->data.data_len; | |
583 | data->fh.data.data_val = malloc(data->fh.data.data_len); | |
584 | if (data->fh.data.data_val == NULL) { | |
585 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh for %s", data->path); | |
586 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
587 | free_nfs_cb_data(data); | |
588 | return -1; | |
589 | } | |
590 | memcpy(data->fh.data.data_val, fh->data.data_val, data->fh.data.data_len); | |
591 | data->continue_cb(nfs, data); | |
592 | return 0; | |
593 | } | |
594 | ||
595 | if (rpc_nfs_lookup_async(nfs->rpc, nfs_lookup_path_1_cb, fh, path, data) != 0) { | |
596 | rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s", data->path); | |
597 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
598 | free_nfs_cb_data(data); | |
599 | return -1; | |
600 | } | |
601 | return 0; | |
602 | } | |
603 | ||
604 | 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) | |
605 | { | |
606 | struct nfs_cb_data *data; | |
607 | ||
608 | if (path[0] != '/') { | |
609 | rpc_set_error(nfs->rpc, "Pathname is not absulute %s", path); | |
610 | return -1; | |
611 | } | |
612 | ||
613 | data = malloc(sizeof(struct nfs_cb_data)); | |
614 | if (data == NULL) { | |
615 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
cbbf9d3e | 616 | return -1; |
84004dbf | 617 | } |
ea98629a | 618 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
619 | data->nfs = nfs; |
620 | data->cb = cb; | |
621 | data->continue_cb = continue_cb; | |
622 | data->continue_data = continue_data; | |
623 | data->free_continue_data = free_continue_data; | |
624 | data->continue_int = continue_int; | |
625 | data->private_data = private_data; | |
626 | data->saved_path = strdup(path); | |
627 | if (data->saved_path == NULL) { | |
628 | rpc_set_error(nfs->rpc, "out of memory: failed to copy path string"); | |
84004dbf | 629 | free_nfs_cb_data(data); |
cbbf9d3e | 630 | return -1; |
84004dbf RS |
631 | } |
632 | data->path = data->saved_path; | |
633 | ||
634 | if (nfs_lookup_path_async_internal(nfs, data, &nfs->rootfh) != 0) { | |
84004dbf RS |
635 | /* return 0 here since the callback will be invoked if there is a failure */ |
636 | return 0; | |
637 | } | |
638 | return 0; | |
639 | } | |
640 | ||
641 | ||
642 | ||
643 | ||
644 | ||
645 | /* | |
646 | * Async stat() | |
647 | */ | |
648 | static void nfs_stat_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
649 | { | |
650 | GETATTR3res *res; | |
651 | struct nfs_cb_data *data = private_data; | |
652 | struct nfs_context *nfs = data->nfs; | |
653 | struct stat st; | |
654 | ||
655 | if (status == RPC_STATUS_ERROR) { | |
656 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
657 | free_nfs_cb_data(data); | |
658 | return; | |
659 | } | |
660 | if (status == RPC_STATUS_CANCEL) { | |
661 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
662 | free_nfs_cb_data(data); | |
663 | return; | |
664 | } | |
665 | ||
666 | res = command_data; | |
667 | if (res->status != NFS3_OK) { | |
668 | 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)); | |
669 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
670 | free_nfs_cb_data(data); | |
671 | return; | |
672 | } | |
673 | ||
674 | st.st_dev = -1; | |
675 | st.st_ino = res->GETATTR3res_u.resok.obj_attributes.fileid; | |
676 | st.st_mode = res->GETATTR3res_u.resok.obj_attributes.mode; | |
e8cab72a RS |
677 | if (res->GETATTR3res_u.resok.obj_attributes.type == NF3DIR) { |
678 | st.st_mode |= S_IFDIR ; | |
679 | } | |
84004dbf RS |
680 | st.st_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink; |
681 | st.st_uid = res->GETATTR3res_u.resok.obj_attributes.uid; | |
682 | st.st_gid = res->GETATTR3res_u.resok.obj_attributes.gid; | |
683 | st.st_rdev = 0; | |
684 | st.st_size = res->GETATTR3res_u.resok.obj_attributes.size; | |
a8a1b858 | 685 | #ifndef WIN32 |
84004dbf RS |
686 | st.st_blksize = 4096; |
687 | st.st_blocks = res->GETATTR3res_u.resok.obj_attributes.size / 4096; | |
a8a1b858 | 688 | #endif//WIN32 |
84004dbf RS |
689 | st.st_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds; |
690 | st.st_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds; | |
691 | st.st_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds; | |
692 | ||
693 | data->cb(0, nfs, &st, data->private_data); | |
694 | free_nfs_cb_data(data); | |
695 | } | |
696 | ||
697 | static int nfs_stat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
698 | { | |
699 | if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &data->fh, data) != 0) { | |
700 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); | |
701 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
702 | free_nfs_cb_data(data); | |
703 | return -1; | |
704 | } | |
705 | return 0; | |
706 | } | |
707 | ||
708 | int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
709 | { | |
710 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_stat_continue_internal, NULL, NULL, 0) != 0) { | |
cbbf9d3e | 711 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
712 | return -1; |
713 | } | |
714 | ||
715 | return 0; | |
716 | } | |
717 | ||
718 | ||
719 | ||
720 | ||
721 | ||
722 | /* | |
723 | * Async open() | |
724 | */ | |
725 | static void nfs_open_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
726 | { | |
727 | ACCESS3res *res; | |
728 | struct nfs_cb_data *data = private_data; | |
729 | struct nfs_context *nfs = data->nfs; | |
730 | struct nfsfh *nfsfh; | |
731 | unsigned int nfsmode = 0; | |
732 | ||
733 | if (status == RPC_STATUS_ERROR) { | |
734 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
735 | free_nfs_cb_data(data); | |
736 | return; | |
737 | } | |
738 | if (status == RPC_STATUS_CANCEL) { | |
739 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
740 | free_nfs_cb_data(data); | |
741 | return; | |
742 | } | |
743 | ||
744 | res = command_data; | |
745 | if (res->status != NFS3_OK) { | |
746 | 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)); | |
747 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
748 | free_nfs_cb_data(data); | |
749 | return; | |
750 | } | |
751 | ||
752 | if (data->continue_int & O_WRONLY) { | |
753 | nfsmode |= ACCESS3_MODIFY; | |
754 | } | |
755 | if (data->continue_int & O_RDWR) { | |
756 | nfsmode |= ACCESS3_READ|ACCESS3_MODIFY; | |
757 | } | |
758 | if (!(data->continue_int & (O_WRONLY|O_RDWR))) { | |
759 | nfsmode |= ACCESS3_READ; | |
760 | } | |
761 | ||
762 | ||
763 | if (res->ACCESS3res_u.resok.access != nfsmode) { | |
764 | rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c", | |
765 | nfsmode&ACCESS3_READ?'r':'-', | |
766 | nfsmode&ACCESS3_MODIFY?'w':'-', | |
767 | nfsmode&ACCESS3_EXECUTE?'x':'-', | |
768 | res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-', | |
769 | res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-', | |
770 | res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-'); | |
771 | data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
772 | free_nfs_cb_data(data); | |
773 | return; | |
774 | } | |
775 | ||
776 | nfsfh = malloc(sizeof(struct nfsfh)); | |
777 | if (nfsfh == NULL) { | |
778 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); | |
779 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
780 | free_nfs_cb_data(data); | |
781 | return; | |
782 | } | |
ea98629a | 783 | memset(nfsfh, 0, sizeof(struct nfsfh)); |
84004dbf RS |
784 | |
785 | if (data->continue_int & O_SYNC) { | |
786 | nfsfh->is_sync = 1; | |
787 | } | |
788 | ||
789 | /* steal the filehandle */ | |
790 | nfsfh->fh.data.data_len = data->fh.data.data_len; | |
791 | nfsfh->fh.data.data_val = data->fh.data.data_val; | |
792 | data->fh.data.data_val = NULL; | |
793 | ||
794 | data->cb(0, nfs, nfsfh, data->private_data); | |
795 | free_nfs_cb_data(data); | |
796 | } | |
797 | ||
798 | static int nfs_open_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
799 | { | |
800 | int nfsmode = 0; | |
801 | ||
802 | if (data->continue_int & O_WRONLY) { | |
803 | nfsmode |= ACCESS3_MODIFY; | |
804 | } | |
805 | if (data->continue_int & O_RDWR) { | |
806 | nfsmode |= ACCESS3_READ|ACCESS3_MODIFY; | |
807 | } | |
808 | if (!(data->continue_int & (O_WRONLY|O_RDWR))) { | |
809 | nfsmode |= ACCESS3_READ; | |
810 | } | |
811 | ||
812 | if (rpc_nfs_access_async(nfs->rpc, nfs_open_cb, &data->fh, nfsmode, data) != 0) { | |
813 | rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path); | |
814 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
815 | free_nfs_cb_data(data); | |
816 | return -1; | |
817 | } | |
818 | return 0; | |
819 | } | |
820 | ||
821 | int nfs_open_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) | |
822 | { | |
823 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_open_continue_internal, NULL, NULL, mode) != 0) { | |
cbbf9d3e RS |
824 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
825 | return -1; | |
84004dbf RS |
826 | } |
827 | ||
828 | return 0; | |
829 | } | |
830 | ||
831 | ||
832 | ||
833 | ||
834 | ||
835 | /* | |
836 | * Async pread() | |
837 | */ | |
838 | static void nfs_pread_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
839 | { | |
840 | struct nfs_cb_data *data = private_data; | |
841 | struct nfs_context *nfs = data->nfs; | |
842 | READ3res *res; | |
843 | ||
844 | if (status == RPC_STATUS_ERROR) { | |
845 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
846 | free_nfs_cb_data(data); | |
847 | return; | |
848 | } | |
849 | if (status == RPC_STATUS_CANCEL) { | |
850 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
851 | free_nfs_cb_data(data); | |
852 | return; | |
853 | } | |
854 | ||
855 | res = command_data; | |
856 | if (res->status != NFS3_OK) { | |
857 | rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
858 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
859 | free_nfs_cb_data(data); | |
860 | return; | |
861 | } | |
862 | ||
863 | data->nfsfh->offset += res->READ3res_u.resok.count; | |
864 | data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_data); | |
865 | free_nfs_cb_data(data); | |
866 | } | |
867 | ||
921f877b RS |
868 | static void nfs_pread_mcb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) |
869 | { | |
870 | struct nfs_mcb_data *mdata = private_data; | |
871 | struct nfs_cb_data *data = mdata->data; | |
872 | struct nfs_context *nfs = data->nfs; | |
873 | READ3res *res; | |
874 | ||
875 | data->num_calls--; | |
876 | ||
877 | if (status == RPC_STATUS_ERROR) { | |
878 | /* flag the failure but do not invoke callback until we have received all responses */ | |
879 | data->error = 1; | |
880 | } | |
881 | if (status == RPC_STATUS_CANCEL) { | |
882 | /* flag the cancellation but do not invoke callback until we have received all responses */ | |
883 | data->cancel = 1; | |
884 | } | |
885 | ||
886 | /* reassemble the data into the buffer */ | |
887 | if (status == RPC_STATUS_SUCCESS) { | |
888 | res = command_data; | |
889 | if (res->status != NFS3_OK) { | |
890 | rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
891 | data->error = 1; | |
e4a5ba42 RS |
892 | } else { |
893 | if (res->READ3res_u.resok.count > 0) { | |
894 | memcpy(&data->buffer[mdata->offset - data->start_offset], res->READ3res_u.resok.data.data_val, res->READ3res_u.resok.count); | |
895 | if ((unsigned)data->max_offset < mdata->offset + res->READ3res_u.resok.count) { | |
896 | data->max_offset = mdata->offset + res->READ3res_u.resok.count; | |
897 | } | |
921f877b RS |
898 | } |
899 | } | |
900 | } | |
901 | ||
902 | if (data->num_calls > 0) { | |
903 | /* still waiting for more replies */ | |
904 | free(mdata); | |
905 | return; | |
906 | } | |
907 | ||
921f877b RS |
908 | if (data->error != 0) { |
909 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
910 | free_nfs_cb_data(data); | |
911 | free(mdata); | |
912 | return; | |
913 | } | |
914 | if (data->cancel != 0) { | |
915 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
916 | free_nfs_cb_data(data); | |
917 | free(mdata); | |
918 | return; | |
919 | } | |
920 | ||
921 | data->nfsfh->offset = data->max_offset; | |
7ed9d87a RS |
922 | data->cb(data->max_offset - data->start_offset, nfs, data->buffer, data->private_data); |
923 | ||
921f877b RS |
924 | free_nfs_cb_data(data); |
925 | free(mdata); | |
926 | } | |
927 | ||
84004dbf RS |
928 | int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_t count, nfs_cb cb, void *private_data) |
929 | { | |
930 | struct nfs_cb_data *data; | |
931 | ||
932 | data = malloc(sizeof(struct nfs_cb_data)); | |
933 | if (data == NULL) { | |
934 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
935 | return -1; |
936 | } | |
ea98629a | 937 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
938 | data->nfs = nfs; |
939 | data->cb = cb; | |
940 | data->private_data = private_data; | |
941 | data->nfsfh = nfsfh; | |
942 | ||
943 | nfsfh->offset = offset; | |
921f877b RS |
944 | |
945 | if (count <= nfs_get_readmax(nfs)) { | |
946 | if (rpc_nfs_read_async(nfs->rpc, nfs_pread_cb, &nfsfh->fh, offset, count, data) != 0) { | |
947 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path); | |
948 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
949 | free_nfs_cb_data(data); | |
950 | return -1; | |
951 | } | |
952 | return 0; | |
953 | } | |
954 | ||
955 | /* trying to read more than maximum server read size, we has to chop it up into smaller | |
956 | * reads and collect into a reassembly buffer. | |
957 | * we send all reads in parallell so that performance is still good. | |
958 | */ | |
7ed9d87a | 959 | data->max_offset = offset; |
921f877b RS |
960 | data->start_offset = offset; |
961 | ||
962 | data->buffer = malloc(count); | |
963 | if (data->buffer == NULL) { | |
964 | rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count); | |
84004dbf RS |
965 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
966 | free_nfs_cb_data(data); | |
967 | return -1; | |
968 | } | |
921f877b RS |
969 | |
970 | while (count > 0) { | |
971 | size_t readcount = count; | |
972 | struct nfs_mcb_data *mdata; | |
973 | ||
974 | if (readcount > nfs_get_readmax(nfs)) { | |
975 | readcount = nfs_get_readmax(nfs); | |
976 | } | |
977 | ||
978 | mdata = malloc(sizeof(struct nfs_mcb_data)); | |
979 | if (mdata == NULL) { | |
980 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure"); | |
921f877b RS |
981 | return -1; |
982 | } | |
ea98629a | 983 | memset(mdata, 0, sizeof(struct nfs_mcb_data)); |
921f877b RS |
984 | mdata->data = data; |
985 | mdata->offset = offset; | |
986 | mdata->count = readcount; | |
921f877b RS |
987 | if (rpc_nfs_read_async(nfs->rpc, nfs_pread_mcb, &nfsfh->fh, offset, readcount, mdata) != 0) { |
988 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path); | |
989 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
990 | free(mdata); | |
991 | return -1; | |
992 | } | |
993 | ||
994 | count -= readcount; | |
995 | offset += readcount; | |
996 | data->num_calls++; | |
997 | } | |
998 | ||
999 | return 0; | |
84004dbf RS |
1000 | } |
1001 | ||
1002 | /* | |
1003 | * Async read() | |
1004 | */ | |
1005 | int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, size_t count, nfs_cb cb, void *private_data) | |
1006 | { | |
1007 | return nfs_pread_async(nfs, nfsfh, nfsfh->offset, count, cb, private_data); | |
1008 | } | |
1009 | ||
1010 | ||
1011 | ||
1012 | /* | |
1013 | * Async pwrite() | |
1014 | */ | |
1015 | static void nfs_pwrite_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1016 | { | |
1017 | struct nfs_cb_data *data = private_data; | |
1018 | struct nfs_context *nfs = data->nfs; | |
1019 | WRITE3res *res; | |
1020 | ||
1021 | if (status == RPC_STATUS_ERROR) { | |
1022 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1023 | free_nfs_cb_data(data); | |
1024 | return; | |
1025 | } | |
1026 | if (status == RPC_STATUS_CANCEL) { | |
1027 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1028 | free_nfs_cb_data(data); | |
1029 | return; | |
1030 | } | |
1031 | ||
1032 | res = command_data; | |
1033 | if (res->status != NFS3_OK) { | |
1034 | rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1035 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1036 | free_nfs_cb_data(data); | |
1037 | return; | |
1038 | } | |
1039 | ||
1040 | data->nfsfh->offset += res->WRITE3res_u.resok.count; | |
1041 | data->cb(res->WRITE3res_u.resok.count, nfs, NULL, data->private_data); | |
1042 | free_nfs_cb_data(data); | |
1043 | } | |
1044 | ||
18c27b73 RS |
1045 | static void nfs_pwrite_mcb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) |
1046 | { | |
1047 | struct nfs_mcb_data *mdata = private_data; | |
1048 | struct nfs_cb_data *data = mdata->data; | |
1049 | struct nfs_context *nfs = data->nfs; | |
1050 | WRITE3res *res; | |
1051 | ||
1052 | data->num_calls--; | |
1053 | ||
1054 | if (status == RPC_STATUS_ERROR) { | |
1055 | /* flag the failure but do not invoke callback until we have received all responses */ | |
1056 | data->error = 1; | |
1057 | } | |
1058 | if (status == RPC_STATUS_CANCEL) { | |
1059 | /* flag the cancellation but do not invoke callback until we have received all responses */ | |
1060 | data->cancel = 1; | |
1061 | } | |
1062 | ||
1063 | if (status == RPC_STATUS_SUCCESS) { | |
1064 | res = command_data; | |
1065 | if (res->status != NFS3_OK) { | |
1066 | rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1067 | data->error = 1; | |
1068 | } else { | |
1069 | if (res->WRITE3res_u.resok.count > 0) { | |
1070 | if ((unsigned)data->max_offset < mdata->offset + res->WRITE3res_u.resok.count) { | |
1071 | data->max_offset = mdata->offset + res->WRITE3res_u.resok.count; | |
1072 | } | |
1073 | } | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | if (data->num_calls > 0) { | |
1078 | /* still waiting for more replies */ | |
1079 | free(mdata); | |
1080 | return; | |
1081 | } | |
1082 | ||
1083 | if (data->error != 0) { | |
1084 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1085 | free_nfs_cb_data(data); | |
1086 | free(mdata); | |
1087 | return; | |
1088 | } | |
1089 | if (data->cancel != 0) { | |
1090 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1091 | free_nfs_cb_data(data); | |
1092 | free(mdata); | |
1093 | return; | |
1094 | } | |
1095 | ||
1096 | data->nfsfh->offset = data->max_offset; | |
1097 | data->cb(data->max_offset - data->start_offset, nfs, NULL, data->private_data); | |
1098 | ||
1099 | free_nfs_cb_data(data); | |
1100 | free(mdata); | |
1101 | } | |
1102 | ||
1103 | ||
84004dbf RS |
1104 | int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_t count, char *buf, nfs_cb cb, void *private_data) |
1105 | { | |
1106 | struct nfs_cb_data *data; | |
1107 | ||
1108 | data = malloc(sizeof(struct nfs_cb_data)); | |
1109 | if (data == NULL) { | |
1110 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
1111 | return -1; |
1112 | } | |
ea98629a | 1113 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
1114 | data->nfs = nfs; |
1115 | data->cb = cb; | |
1116 | data->private_data = private_data; | |
1117 | data->nfsfh = nfsfh; | |
1118 | ||
1119 | nfsfh->offset = offset; | |
18c27b73 RS |
1120 | |
1121 | if (count <= nfs_get_writemax(nfs)) { | |
1122 | if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_cb, &nfsfh->fh, buf, offset, count, nfsfh->is_sync?FILE_SYNC:UNSTABLE, data) != 0) { | |
1123 | rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path); | |
1124 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1125 | free_nfs_cb_data(data); | |
1126 | return -1; | |
1127 | } | |
1128 | return 0; | |
84004dbf | 1129 | } |
18c27b73 RS |
1130 | |
1131 | /* trying to write more than maximum server write size, we has to chop it up into smaller | |
1132 | * chunks. | |
1133 | * we send all writes in parallell so that performance is still good. | |
1134 | */ | |
1135 | data->max_offset = offset; | |
1136 | data->start_offset = offset; | |
1137 | ||
1138 | while (count > 0) { | |
1139 | size_t writecount = count; | |
1140 | struct nfs_mcb_data *mdata; | |
1141 | ||
1142 | if (writecount > nfs_get_writemax(nfs)) { | |
1143 | writecount = nfs_get_writemax(nfs); | |
1144 | } | |
1145 | ||
1146 | mdata = malloc(sizeof(struct nfs_mcb_data)); | |
1147 | if (mdata == NULL) { | |
1148 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure"); | |
1149 | return -1; | |
1150 | } | |
1151 | memset(mdata, 0, sizeof(struct nfs_mcb_data)); | |
1152 | mdata->data = data; | |
1153 | mdata->offset = offset; | |
1154 | mdata->count = writecount; | |
1155 | ||
1156 | if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_mcb, &nfsfh->fh, &buf[offset - data->start_offset], offset, writecount, nfsfh->is_sync?FILE_SYNC:UNSTABLE, mdata) != 0) { | |
1157 | rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path); | |
1158 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1159 | free(mdata); | |
1160 | return -1; | |
1161 | } | |
1162 | ||
1163 | count -= writecount; | |
1164 | offset += writecount; | |
1165 | data->num_calls++; | |
1166 | } | |
1167 | ||
84004dbf RS |
1168 | return 0; |
1169 | } | |
1170 | ||
1171 | /* | |
1172 | * Async write() | |
1173 | */ | |
1174 | int nfs_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, size_t count, char *buf, nfs_cb cb, void *private_data) | |
1175 | { | |
1176 | return nfs_pwrite_async(nfs, nfsfh, nfsfh->offset, count, buf, cb, private_data); | |
1177 | } | |
1178 | ||
1179 | ||
1180 | ||
1181 | ||
1182 | /* | |
1183 | * close | |
1184 | */ | |
1185 | ||
1186 | int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) | |
1187 | { | |
1188 | if (nfsfh->fh.data.data_val != NULL){ | |
1189 | free(nfsfh->fh.data.data_val); | |
1190 | nfsfh->fh.data.data_val = NULL; | |
1191 | } | |
1192 | free(nfsfh); | |
1193 | ||
1194 | cb(0, nfs, NULL, private_data); | |
1195 | return 0; | |
1196 | }; | |
1197 | ||
1198 | ||
1199 | ||
1200 | ||
1201 | ||
1202 | /* | |
1203 | * Async fstat() | |
1204 | */ | |
1205 | int nfs_fstat_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) | |
1206 | { | |
1207 | struct nfs_cb_data *data; | |
1208 | ||
1209 | data = malloc(sizeof(struct nfs_cb_data)); | |
1210 | if (data == NULL) { | |
1211 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
1212 | return -1; |
1213 | } | |
ea98629a | 1214 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
1215 | data->nfs = nfs; |
1216 | data->cb = cb; | |
1217 | data->private_data = private_data; | |
1218 | ||
1219 | if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &nfsfh->fh, data) != 0) { | |
1220 | rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path); | |
1221 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1222 | free_nfs_cb_data(data); | |
1223 | return -1; | |
1224 | } | |
1225 | return 0; | |
1226 | } | |
1227 | ||
1228 | ||
1229 | ||
1230 | /* | |
1231 | * Async fsync() | |
1232 | */ | |
1233 | static void nfs_fsync_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1234 | { | |
1235 | struct nfs_cb_data *data = private_data; | |
1236 | struct nfs_context *nfs = data->nfs; | |
1237 | COMMIT3res *res; | |
1238 | ||
1239 | if (status == RPC_STATUS_ERROR) { | |
1240 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1241 | free_nfs_cb_data(data); | |
1242 | return; | |
1243 | } | |
1244 | if (status == RPC_STATUS_CANCEL) { | |
1245 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1246 | free_nfs_cb_data(data); | |
1247 | return; | |
1248 | } | |
1249 | ||
1250 | res = command_data; | |
1251 | if (res->status != NFS3_OK) { | |
1252 | rpc_set_error(nfs->rpc, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1253 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1254 | free_nfs_cb_data(data); | |
1255 | return; | |
1256 | } | |
1257 | ||
1258 | data->cb(0, nfs, NULL, data->private_data); | |
1259 | free_nfs_cb_data(data); | |
1260 | } | |
1261 | ||
1262 | int nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data) | |
1263 | { | |
1264 | struct nfs_cb_data *data; | |
1265 | ||
1266 | data = malloc(sizeof(struct nfs_cb_data)); | |
1267 | if (data == NULL) { | |
1268 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
1269 | return -1; |
1270 | } | |
ea98629a | 1271 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
1272 | data->nfs = nfs; |
1273 | data->cb = cb; | |
1274 | data->private_data = private_data; | |
1275 | ||
1276 | if (rpc_nfs_commit_async(nfs->rpc, nfs_fsync_cb, &nfsfh->fh, data) != 0) { | |
1277 | rpc_set_error(nfs->rpc, "RPC error: Failed to send COMMIT call for %s", data->path); | |
1278 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1279 | free_nfs_cb_data(data); | |
1280 | return -1; | |
1281 | } | |
1282 | return 0; | |
1283 | } | |
1284 | ||
1285 | ||
1286 | ||
1287 | ||
1288 | /* | |
1289 | * Async ftruncate() | |
1290 | */ | |
1291 | static void nfs_ftruncate_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1292 | { | |
1293 | struct nfs_cb_data *data = private_data; | |
1294 | struct nfs_context *nfs = data->nfs; | |
1295 | SETATTR3res *res; | |
1296 | ||
1297 | if (status == RPC_STATUS_ERROR) { | |
1298 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1299 | free_nfs_cb_data(data); | |
1300 | return; | |
1301 | } | |
1302 | if (status == RPC_STATUS_CANCEL) { | |
1303 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1304 | free_nfs_cb_data(data); | |
1305 | return; | |
1306 | } | |
1307 | ||
1308 | res = command_data; | |
1309 | if (res->status != NFS3_OK) { | |
1310 | rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1311 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1312 | free_nfs_cb_data(data); | |
1313 | return; | |
1314 | } | |
1315 | ||
1316 | data->cb(0, nfs, NULL, data->private_data); | |
1317 | free_nfs_cb_data(data); | |
1318 | } | |
1319 | ||
1320 | int nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t length, nfs_cb cb, void *private_data) | |
1321 | { | |
1322 | struct nfs_cb_data *data; | |
1323 | SETATTR3args args; | |
1324 | ||
1325 | data = malloc(sizeof(struct nfs_cb_data)); | |
1326 | if (data == NULL) { | |
1327 | rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure"); | |
84004dbf RS |
1328 | return -1; |
1329 | } | |
ea98629a | 1330 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
1331 | data->nfs = nfs; |
1332 | data->cb = cb; | |
1333 | data->private_data = private_data; | |
1334 | ||
ea98629a | 1335 | memset(&args, 0, sizeof(SETATTR3args)); |
84004dbf RS |
1336 | args.object.data.data_len = nfsfh->fh.data.data_len; |
1337 | args.object.data.data_val = nfsfh->fh.data.data_val; | |
1338 | args.new_attributes.size.set_it = 1; | |
1339 | args.new_attributes.size.set_size3_u.size = length; | |
1340 | ||
1341 | if (rpc_nfs_setattr_async(nfs->rpc, nfs_ftruncate_cb, &args, data) != 0) { | |
1342 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); | |
1343 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1344 | free_nfs_cb_data(data); | |
1345 | return -1; | |
1346 | } | |
1347 | return 0; | |
1348 | } | |
1349 | ||
1350 | ||
1351 | /* | |
1352 | * Async truncate() | |
1353 | */ | |
1354 | static int nfs_truncate_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1355 | { | |
1356 | off_t offset = data->continue_int; | |
1357 | struct nfsfh nfsfh; | |
1358 | ||
1359 | nfsfh.fh.data.data_val = data->fh.data.data_val; | |
1360 | nfsfh.fh.data.data_len = data->fh.data.data_len; | |
1361 | ||
1362 | if (nfs_ftruncate_async(nfs, &nfsfh, offset, data->cb, data->private_data) != 0) { | |
1363 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); | |
1364 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1365 | free_nfs_cb_data(data); | |
1366 | return -1; | |
1367 | } | |
1368 | free_nfs_cb_data(data); | |
1369 | return 0; | |
1370 | } | |
1371 | ||
1372 | int nfs_truncate_async(struct nfs_context *nfs, const char *path, off_t length, nfs_cb cb, void *private_data) | |
1373 | { | |
1374 | off_t offset; | |
1375 | ||
1376 | offset = length; | |
1377 | ||
1378 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_truncate_continue_internal, NULL, NULL, offset) != 0) { | |
cbbf9d3e RS |
1379 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
1380 | return -1; | |
84004dbf RS |
1381 | } |
1382 | ||
1383 | return 0; | |
1384 | } | |
1385 | ||
1386 | ||
1387 | ||
1388 | ||
1389 | /* | |
1390 | * Async mkdir() | |
1391 | */ | |
1392 | static void nfs_mkdir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1393 | { | |
1394 | MKDIR3res *res; | |
1395 | struct nfs_cb_data *data = private_data; | |
1396 | struct nfs_context *nfs = data->nfs; | |
1397 | char *str = data->continue_data; | |
1398 | ||
1399 | str = &str[strlen(str) + 1]; | |
1400 | ||
1401 | if (status == RPC_STATUS_ERROR) { | |
1402 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1403 | free_nfs_cb_data(data); | |
1404 | return; | |
1405 | } | |
1406 | if (status == RPC_STATUS_CANCEL) { | |
1407 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1408 | free_nfs_cb_data(data); | |
1409 | return; | |
1410 | } | |
1411 | ||
1412 | res = command_data; | |
1413 | if (res->status != NFS3_OK) { | |
1414 | 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)); | |
1415 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1416 | free_nfs_cb_data(data); | |
1417 | return; | |
1418 | } | |
1419 | ||
1420 | data->cb(0, nfs, NULL, data->private_data); | |
1421 | free_nfs_cb_data(data); | |
1422 | } | |
1423 | ||
1424 | static int nfs_mkdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1425 | { | |
1426 | char *str = data->continue_data; | |
1427 | ||
1428 | str = &str[strlen(str) + 1]; | |
1429 | ||
1430 | if (rpc_nfs_mkdir_async(nfs->rpc, nfs_mkdir_cb, &data->fh, str, data) != 0) { | |
1431 | rpc_set_error(nfs->rpc, "RPC error: Failed to send MKDIR call for %s", data->path); | |
1432 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1433 | free_nfs_cb_data(data); | |
1434 | return -1; | |
1435 | } | |
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | int nfs_mkdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
1440 | { | |
1441 | char *new_path; | |
1442 | char *ptr; | |
1443 | ||
1444 | new_path = strdup(path); | |
1445 | if (new_path == NULL) { | |
cbbf9d3e | 1446 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
84004dbf RS |
1447 | return -1; |
1448 | } | |
1449 | ||
93e9306f | 1450 | ptr = strrchr(new_path, '/'); |
84004dbf | 1451 | if (ptr == NULL) { |
cbbf9d3e RS |
1452 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
1453 | return -1; | |
84004dbf RS |
1454 | } |
1455 | *ptr = 0; | |
1456 | ||
1457 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ | |
1458 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_mkdir_continue_internal, new_path, free, 0) != 0) { | |
cbbf9d3e RS |
1459 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path component"); |
1460 | return -1; | |
84004dbf RS |
1461 | } |
1462 | ||
1463 | return 0; | |
1464 | } | |
1465 | ||
1466 | ||
1467 | ||
1468 | ||
1469 | ||
1470 | /* | |
1471 | * Async rmdir() | |
1472 | */ | |
1473 | static void nfs_rmdir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1474 | { | |
1475 | RMDIR3res *res; | |
1476 | struct nfs_cb_data *data = private_data; | |
1477 | struct nfs_context *nfs = data->nfs; | |
1478 | char *str = data->continue_data; | |
1479 | ||
1480 | str = &str[strlen(str) + 1]; | |
1481 | ||
1482 | if (status == RPC_STATUS_ERROR) { | |
1483 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1484 | free_nfs_cb_data(data); | |
1485 | return; | |
1486 | } | |
1487 | if (status == RPC_STATUS_CANCEL) { | |
1488 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1489 | free_nfs_cb_data(data); | |
1490 | return; | |
1491 | } | |
1492 | ||
1493 | res = command_data; | |
1494 | if (res->status != NFS3_OK) { | |
1495 | 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)); | |
1496 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1497 | free_nfs_cb_data(data); | |
1498 | return; | |
1499 | } | |
1500 | ||
1501 | data->cb(0, nfs, NULL, data->private_data); | |
1502 | free_nfs_cb_data(data); | |
1503 | } | |
1504 | ||
1505 | static int nfs_rmdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1506 | { | |
1507 | char *str = data->continue_data; | |
1508 | ||
1509 | str = &str[strlen(str) + 1]; | |
1510 | ||
1511 | if (rpc_nfs_rmdir_async(nfs->rpc, nfs_rmdir_cb, &data->fh, str, data) != 0) { | |
1512 | rpc_set_error(nfs->rpc, "RPC error: Failed to send RMDIR call for %s", data->path); | |
1513 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1514 | free_nfs_cb_data(data); | |
1515 | return -1; | |
1516 | } | |
1517 | return 0; | |
1518 | } | |
1519 | ||
1520 | int nfs_rmdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
1521 | { | |
1522 | char *new_path; | |
1523 | char *ptr; | |
1524 | ||
1525 | new_path = strdup(path); | |
1526 | if (new_path == NULL) { | |
cbbf9d3e | 1527 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
84004dbf RS |
1528 | return -1; |
1529 | } | |
1530 | ||
93e9306f | 1531 | ptr = strrchr(new_path, '/'); |
84004dbf | 1532 | if (ptr == NULL) { |
cbbf9d3e RS |
1533 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
1534 | return -1; | |
84004dbf RS |
1535 | } |
1536 | *ptr = 0; | |
1537 | ||
1538 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ | |
1539 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_rmdir_continue_internal, new_path, free, 0) != 0) { | |
cbbf9d3e RS |
1540 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
1541 | return -1; | |
84004dbf RS |
1542 | } |
1543 | ||
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | ||
1548 | ||
1549 | ||
1550 | /* | |
1551 | * Async creat() | |
1552 | */ | |
1553 | static void nfs_create_2_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1554 | { | |
1555 | LOOKUP3res *res; | |
1556 | struct nfs_cb_data *data = private_data; | |
1557 | struct nfs_context *nfs = data->nfs; | |
1558 | struct nfsfh *nfsfh; | |
1559 | char *str = data->continue_data; | |
1560 | ||
1561 | if (status == RPC_STATUS_ERROR) { | |
1562 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1563 | free_nfs_cb_data(data); | |
1564 | return; | |
1565 | } | |
1566 | if (status == RPC_STATUS_CANCEL) { | |
1567 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1568 | free_nfs_cb_data(data); | |
1569 | return; | |
1570 | } | |
1571 | ||
1572 | str = &str[strlen(str) + 1]; | |
1573 | res = command_data; | |
1574 | if (res->status != NFS3_OK) { | |
1575 | 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)); | |
1576 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1577 | ||
1578 | return; | |
1579 | } | |
1580 | ||
1581 | nfsfh = malloc(sizeof(struct nfsfh)); | |
1582 | if (nfsfh == NULL) { | |
1583 | rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure"); | |
1584 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1585 | free_nfs_cb_data(data); | |
1586 | return; | |
1587 | } | |
ea98629a | 1588 | memset(nfsfh, 0, sizeof(struct nfsfh)); |
84004dbf RS |
1589 | |
1590 | /* steal the filehandle */ | |
1591 | nfsfh->fh.data.data_len = data->fh.data.data_len; | |
1592 | nfsfh->fh.data.data_val = data->fh.data.data_val; | |
1593 | data->fh.data.data_val = NULL; | |
1594 | ||
1595 | data->cb(0, nfs, nfsfh, data->private_data); | |
1596 | free_nfs_cb_data(data); | |
1597 | } | |
1598 | ||
1599 | ||
1600 | ||
1601 | static void nfs_creat_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1602 | { | |
1603 | CREATE3res *res; | |
1604 | struct nfs_cb_data *data = private_data; | |
1605 | struct nfs_context *nfs = data->nfs; | |
1606 | char *str = data->continue_data; | |
1607 | ||
1608 | if (status == RPC_STATUS_ERROR) { | |
1609 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1610 | free_nfs_cb_data(data); | |
1611 | return; | |
1612 | } | |
1613 | if (status == RPC_STATUS_CANCEL) { | |
1614 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1615 | free_nfs_cb_data(data); | |
1616 | return; | |
1617 | } | |
1618 | ||
1619 | str = &str[strlen(str) + 1]; | |
1620 | res = command_data; | |
1621 | if (res->status != NFS3_OK) { | |
1622 | 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)); | |
1623 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1624 | ||
1625 | return; | |
1626 | } | |
1627 | ||
1628 | if (rpc_nfs_lookup_async(nfs->rpc, nfs_create_2_cb, &data->fh, str, data) != 0) { | |
1629 | rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s/%s", data->saved_path, str); | |
1630 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1631 | free_nfs_cb_data(data); | |
1632 | return; | |
1633 | } | |
1634 | return; | |
1635 | } | |
1636 | ||
1637 | static int nfs_creat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1638 | { | |
1639 | char *str = data->continue_data; | |
1640 | ||
1641 | str = &str[strlen(str) + 1]; | |
1642 | ||
1643 | if (rpc_nfs_create_async(nfs->rpc, nfs_creat_1_cb, &data->fh, str, data->continue_int, data) != 0) { | |
1644 | rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str); | |
1645 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1646 | free_nfs_cb_data(data); | |
1647 | return -1; | |
1648 | } | |
1649 | return 0; | |
1650 | } | |
1651 | ||
1652 | int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) | |
1653 | { | |
1654 | char *new_path; | |
1655 | char *ptr; | |
1656 | ||
1657 | new_path = strdup(path); | |
1658 | if (new_path == NULL) { | |
cbbf9d3e | 1659 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
84004dbf RS |
1660 | return -1; |
1661 | } | |
1662 | ||
93e9306f | 1663 | ptr = strrchr(new_path, '/'); |
84004dbf | 1664 | if (ptr == NULL) { |
cbbf9d3e RS |
1665 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
1666 | return -1; | |
84004dbf RS |
1667 | } |
1668 | *ptr = 0; | |
1669 | ||
1670 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ | |
1671 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_creat_continue_internal, new_path, free, mode) != 0) { | |
cbbf9d3e RS |
1672 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
1673 | return -1; | |
84004dbf RS |
1674 | } |
1675 | ||
1676 | return 0; | |
1677 | } | |
1678 | ||
1679 | ||
1680 | ||
1681 | ||
1682 | /* | |
1683 | * Async unlink() | |
1684 | */ | |
1685 | static void nfs_unlink_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1686 | { | |
1687 | REMOVE3res *res; | |
1688 | struct nfs_cb_data *data = private_data; | |
1689 | struct nfs_context *nfs = data->nfs; | |
1690 | char *str = data->continue_data; | |
1691 | ||
1692 | str = &str[strlen(str) + 1]; | |
1693 | ||
1694 | if (status == RPC_STATUS_ERROR) { | |
1695 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1696 | free_nfs_cb_data(data); | |
1697 | return; | |
1698 | } | |
1699 | if (status == RPC_STATUS_CANCEL) { | |
1700 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1701 | free_nfs_cb_data(data); | |
1702 | return; | |
1703 | } | |
1704 | ||
1705 | res = command_data; | |
1706 | if (res->status != NFS3_OK) { | |
1707 | 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)); | |
1708 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1709 | free_nfs_cb_data(data); | |
1710 | return; | |
1711 | } | |
1712 | ||
1713 | data->cb(0, nfs, NULL, data->private_data); | |
1714 | free_nfs_cb_data(data); | |
1715 | } | |
1716 | ||
1717 | static int nfs_unlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1718 | { | |
1719 | char *str = data->continue_data; | |
1720 | ||
1721 | str = &str[strlen(str) + 1]; | |
1722 | ||
1723 | if (rpc_nfs_remove_async(nfs->rpc, nfs_unlink_cb, &data->fh, str, data) != 0) { | |
1724 | rpc_set_error(nfs->rpc, "RPC error: Failed to send REMOVE call for %s", data->path); | |
1725 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1726 | free_nfs_cb_data(data); | |
1727 | return -1; | |
1728 | } | |
1729 | return 0; | |
1730 | } | |
1731 | ||
1732 | int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
1733 | { | |
1734 | char *new_path; | |
1735 | char *ptr; | |
1736 | ||
1737 | new_path = strdup(path); | |
1738 | if (new_path == NULL) { | |
cbbf9d3e | 1739 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path"); |
84004dbf RS |
1740 | return -1; |
1741 | } | |
1742 | ||
93e9306f | 1743 | ptr = strrchr(new_path, '/'); |
84004dbf | 1744 | if (ptr == NULL) { |
cbbf9d3e RS |
1745 | rpc_set_error(nfs->rpc, "Invalid path %s", path); |
1746 | return -1; | |
84004dbf RS |
1747 | } |
1748 | *ptr = 0; | |
1749 | ||
1750 | /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */ | |
1751 | if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_unlink_continue_internal, new_path, free, 0) != 0) { | |
cbbf9d3e RS |
1752 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
1753 | return -1; | |
84004dbf RS |
1754 | } |
1755 | ||
1756 | return 0; | |
1757 | } | |
1758 | ||
1759 | ||
1760 | ||
1761 | ||
1762 | ||
1763 | /* | |
1764 | * Async opendir() | |
1765 | */ | |
1766 | static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1767 | { | |
f390f181 | 1768 | READDIRPLUS3res *res; |
84004dbf RS |
1769 | struct nfs_cb_data *data = private_data; |
1770 | struct nfs_context *nfs = data->nfs; | |
fb6510bb | 1771 | struct nfsdir *nfsdir = data->continue_data; |
f390f181 | 1772 | struct entryplus3 *entry; |
84004dbf | 1773 | uint64_t cookie; |
f390f181 | 1774 | |
84004dbf RS |
1775 | if (status == RPC_STATUS_ERROR) { |
1776 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1777 | nfs_free_nfsdir(nfsdir); | |
1778 | data->continue_data = NULL; | |
1779 | free_nfs_cb_data(data); | |
1780 | return; | |
1781 | } | |
1782 | if (status == RPC_STATUS_CANCEL) { | |
1783 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1784 | nfs_free_nfsdir(nfsdir); | |
1785 | data->continue_data = NULL; | |
1786 | free_nfs_cb_data(data); | |
1787 | return; | |
1788 | } | |
1789 | ||
1790 | res = command_data; | |
1791 | if (res->status != NFS3_OK) { | |
1792 | 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)); | |
1793 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1794 | nfs_free_nfsdir(nfsdir); | |
1795 | data->continue_data = NULL; | |
1796 | free_nfs_cb_data(data); | |
1797 | return; | |
1798 | } | |
1799 | ||
f390f181 | 1800 | entry =res->READDIRPLUS3res_u.resok.reply.entries; |
84004dbf RS |
1801 | while (entry != NULL) { |
1802 | struct nfsdirent *nfsdirent; | |
1803 | ||
1804 | nfsdirent = malloc(sizeof(struct nfsdirent)); | |
1805 | if (nfsdirent == NULL) { | |
1806 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data); | |
1807 | nfs_free_nfsdir(nfsdir); | |
1808 | data->continue_data = NULL; | |
1809 | free_nfs_cb_data(data); | |
1810 | return; | |
1811 | } | |
ea98629a | 1812 | memset(nfsdirent, 0, sizeof(struct nfsdirent)); |
84004dbf RS |
1813 | nfsdirent->name = strdup(entry->name); |
1814 | if (nfsdirent->name == NULL) { | |
1815 | data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data); | |
1816 | nfs_free_nfsdir(nfsdir); | |
1817 | data->continue_data = NULL; | |
1818 | free_nfs_cb_data(data); | |
1819 | return; | |
1820 | } | |
1821 | nfsdirent->inode = entry->fileid; | |
0804e67d RS |
1822 | if (entry->name_attributes.attributes_follow) { |
1823 | nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type; | |
1824 | nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode; | |
1825 | nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size; | |
1826 | ||
1827 | nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds; | |
1828 | nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000; | |
1829 | nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds; | |
1830 | nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000; | |
1831 | nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds; | |
1832 | nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000; | |
1833 | } | |
1834 | ||
84004dbf RS |
1835 | nfsdirent->next = nfsdir->entries; |
1836 | nfsdir->entries = nfsdirent; | |
1837 | ||
1838 | cookie = entry->cookie; | |
1839 | entry = entry->nextentry; | |
1840 | } | |
1841 | ||
f390f181 RS |
1842 | if (res->READDIRPLUS3res_u.resok.reply.eof == 0) { |
1843 | if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, cookie, res->READDIRPLUS3res_u.resok.cookieverf, 8192, data) != 0) { | |
1844 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path); | |
84004dbf RS |
1845 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
1846 | nfs_free_nfsdir(nfsdir); | |
1847 | data->continue_data = NULL; | |
1848 | free_nfs_cb_data(data); | |
1849 | return; | |
1850 | } | |
1851 | return; | |
1852 | } | |
1853 | ||
1854 | /* steal the dirhandle */ | |
1855 | data->continue_data = NULL; | |
1856 | nfsdir->current = nfsdir->entries; | |
1857 | ||
1858 | data->cb(0, nfs, nfsdir, data->private_data); | |
1859 | free_nfs_cb_data(data); | |
1860 | } | |
1861 | ||
1862 | static int nfs_opendir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
1863 | { | |
1864 | cookieverf3 cv; | |
1865 | ||
ea98629a | 1866 | memset(cv, 0, sizeof(cookieverf3)); |
f390f181 RS |
1867 | if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) { |
1868 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path); | |
84004dbf RS |
1869 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); |
1870 | free_nfs_cb_data(data); | |
1871 | return -1; | |
1872 | } | |
1873 | return 0; | |
1874 | } | |
1875 | ||
1876 | int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
1877 | { | |
1878 | struct nfsdir *nfsdir; | |
1879 | ||
1880 | nfsdir = malloc(sizeof(struct nfsdir)); | |
1881 | if (nfsdir == NULL) { | |
cbbf9d3e | 1882 | rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir"); |
84004dbf RS |
1883 | return -1; |
1884 | } | |
ea98629a | 1885 | memset(nfsdir, 0, sizeof(struct nfsdir)); |
84004dbf RS |
1886 | |
1887 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) { | |
cbbf9d3e RS |
1888 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
1889 | return -1; | |
84004dbf RS |
1890 | } |
1891 | ||
1892 | return 0; | |
1893 | } | |
1894 | ||
1895 | ||
1896 | struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir) | |
1897 | { | |
1898 | struct nfsdirent *nfsdirent = nfsdir->current; | |
1899 | ||
1900 | if (nfsdir->current != NULL) { | |
1901 | nfsdir->current = nfsdir->current->next; | |
1902 | } | |
1903 | return nfsdirent; | |
1904 | } | |
1905 | ||
1906 | ||
1907 | void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir) | |
1908 | { | |
1909 | nfs_free_nfsdir(nfsdir); | |
1910 | } | |
1911 | ||
1912 | ||
1913 | ||
1914 | ||
1915 | ||
1916 | ||
1917 | ||
1918 | /* | |
1919 | * Async lseek() | |
1920 | */ | |
1921 | struct lseek_cb_data { | |
1922 | struct nfs_context *nfs; | |
1923 | struct nfsfh *nfsfh; | |
1924 | off_t offset; | |
1925 | nfs_cb cb; | |
1926 | void *private_data; | |
1927 | }; | |
1928 | ||
1929 | static void nfs_lseek_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
1930 | { | |
1931 | GETATTR3res *res; | |
1932 | struct lseek_cb_data *data = private_data; | |
1933 | struct nfs_context *nfs = data->nfs; | |
1934 | ||
1935 | if (status == RPC_STATUS_ERROR) { | |
1936 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
1937 | free(data); | |
1938 | return; | |
1939 | } | |
1940 | if (status == RPC_STATUS_CANCEL) { | |
1941 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
1942 | free(data); | |
1943 | return; | |
1944 | } | |
1945 | ||
1946 | res = command_data; | |
1947 | if (res->status != NFS3_OK) { | |
1948 | rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
1949 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
1950 | free(data); | |
1951 | return; | |
1952 | } | |
1953 | ||
1954 | data->nfsfh->offset = data->offset + res->GETATTR3res_u.resok.obj_attributes.size; | |
1955 | data->cb(0, nfs, &data->nfsfh->offset, data->private_data); | |
1956 | free(data); | |
1957 | } | |
1958 | ||
1959 | int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, int whence, nfs_cb cb, void *private_data) | |
1960 | { | |
1961 | struct lseek_cb_data *data; | |
1962 | ||
1963 | if (whence == SEEK_SET) { | |
1964 | nfsfh->offset = offset; | |
1965 | cb(0, nfs, &nfsfh->offset, private_data); | |
1966 | return 0; | |
1967 | } | |
1968 | if (whence == SEEK_CUR) { | |
1969 | nfsfh->offset += offset; | |
1970 | cb(0, nfs, &nfsfh->offset, private_data); | |
1971 | return 0; | |
1972 | } | |
1973 | ||
1974 | data = malloc(sizeof(struct lseek_cb_data)); | |
1975 | if (data == NULL) { | |
1976 | rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data"); | |
1977 | return -1; | |
1978 | } | |
1979 | ||
1980 | data->nfs = nfs; | |
1981 | data->nfsfh = nfsfh; | |
1982 | data->offset = offset; | |
1983 | data->cb = cb; | |
1984 | data->private_data = private_data; | |
1985 | ||
1986 | if (rpc_nfs_getattr_async(nfs->rpc, nfs_lseek_1_cb, &nfsfh->fh, data) != 0) { | |
1987 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call"); | |
1988 | free(data); | |
cbbf9d3e | 1989 | return -1; |
84004dbf RS |
1990 | } |
1991 | return 0; | |
1992 | } | |
1993 | ||
1994 | ||
1995 | ||
1996 | ||
1997 | /* | |
1998 | * Async statvfs() | |
1999 | */ | |
2000 | static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2001 | { | |
2002 | FSSTAT3res *res; | |
2003 | struct nfs_cb_data *data = private_data; | |
2004 | struct nfs_context *nfs = data->nfs; | |
2005 | struct statvfs svfs; | |
2006 | ||
2007 | if (status == RPC_STATUS_ERROR) { | |
2008 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2009 | free_nfs_cb_data(data); | |
2010 | return; | |
2011 | } | |
2012 | if (status == RPC_STATUS_CANCEL) { | |
2013 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2014 | free_nfs_cb_data(data); | |
2015 | return; | |
2016 | } | |
2017 | ||
2018 | res = command_data; | |
2019 | if (res->status != NFS3_OK) { | |
2020 | 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)); | |
2021 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2022 | free_nfs_cb_data(data); | |
2023 | return; | |
2024 | } | |
2025 | ||
2026 | svfs.f_bsize = 4096; | |
2027 | svfs.f_frsize = 4096; | |
2028 | svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/4096; | |
2029 | svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/4096; | |
2030 | svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/4096; | |
2031 | svfs.f_files = res->FSSTAT3res_u.resok.tfiles; | |
2032 | svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles; | |
2033 | svfs.f_favail = res->FSSTAT3res_u.resok.afiles; | |
2034 | svfs.f_fsid = 0; | |
2035 | svfs.f_flag = 0; | |
2036 | svfs.f_namemax = 256; | |
2037 | ||
2038 | data->cb(0, nfs, &svfs, data->private_data); | |
2039 | free_nfs_cb_data(data); | |
2040 | } | |
2041 | ||
2042 | static int nfs_statvfs_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2043 | { | |
2044 | if (rpc_nfs_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &data->fh, data) != 0) { | |
2045 | rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT 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 | return 0; | |
2051 | } | |
2052 | ||
2053 | int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
2054 | { | |
2055 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) { | |
cbbf9d3e | 2056 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
2057 | return -1; |
2058 | } | |
2059 | ||
2060 | return 0; | |
2061 | } | |
2062 | ||
2063 | ||
2064 | ||
2065 | ||
2066 | /* | |
2067 | * Async readlink() | |
2068 | */ | |
2069 | static void nfs_readlink_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2070 | { | |
2071 | READLINK3res *res; | |
2072 | struct nfs_cb_data *data = private_data; | |
2073 | struct nfs_context *nfs = data->nfs; | |
2074 | ||
2075 | if (status == RPC_STATUS_ERROR) { | |
2076 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2077 | free_nfs_cb_data(data); | |
2078 | return; | |
2079 | } | |
2080 | if (status == RPC_STATUS_CANCEL) { | |
2081 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2082 | free_nfs_cb_data(data); | |
2083 | return; | |
2084 | } | |
2085 | ||
2086 | res = command_data; | |
2087 | if (res->status != NFS3_OK) { | |
2088 | 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)); | |
2089 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2090 | free_nfs_cb_data(data); | |
2091 | return; | |
2092 | } | |
2093 | ||
2094 | ||
2095 | data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data); | |
2096 | free_nfs_cb_data(data); | |
2097 | } | |
2098 | ||
2099 | static int nfs_readlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2100 | { | |
2101 | if (rpc_nfs_readlink_async(nfs->rpc, nfs_readlink_1_cb, &data->fh, data) != 0) { | |
2102 | rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path); | |
2103 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2104 | free_nfs_cb_data(data); | |
2105 | return -1; | |
2106 | } | |
2107 | return 0; | |
2108 | } | |
2109 | ||
2110 | int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) | |
2111 | { | |
2112 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) { | |
cbbf9d3e | 2113 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
2114 | return -1; |
2115 | } | |
2116 | ||
2117 | return 0; | |
2118 | } | |
2119 | ||
2120 | ||
2121 | ||
2122 | ||
2123 | /* | |
2124 | * Async chmod() | |
2125 | */ | |
2126 | static void nfs_chmod_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2127 | { | |
2128 | struct nfs_cb_data *data = private_data; | |
2129 | struct nfs_context *nfs = data->nfs; | |
2130 | SETATTR3res *res; | |
2131 | ||
2132 | if (status == RPC_STATUS_ERROR) { | |
2133 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2134 | free_nfs_cb_data(data); | |
2135 | return; | |
2136 | } | |
2137 | if (status == RPC_STATUS_CANCEL) { | |
2138 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2139 | free_nfs_cb_data(data); | |
2140 | return; | |
2141 | } | |
2142 | ||
2143 | res = command_data; | |
2144 | if (res->status != NFS3_OK) { | |
2145 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
2146 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2147 | free_nfs_cb_data(data); | |
2148 | return; | |
2149 | } | |
2150 | ||
2151 | data->cb(0, nfs, NULL, data->private_data); | |
2152 | free_nfs_cb_data(data); | |
2153 | } | |
2154 | ||
2155 | static int nfs_chmod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2156 | { | |
2157 | SETATTR3args args; | |
2158 | ||
ea98629a | 2159 | memset(&args, 0, sizeof(SETATTR3args)); |
84004dbf RS |
2160 | args.object.data.data_len = data->fh.data.data_len; |
2161 | args.object.data.data_val = data->fh.data.data_val; | |
2162 | args.new_attributes.mode.set_it = 1; | |
2163 | args.new_attributes.mode.set_mode3_u.mode = data->continue_int; | |
2164 | ||
2165 | if (rpc_nfs_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) { | |
2166 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); | |
2167 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2168 | free_nfs_cb_data(data); | |
2169 | return -1; | |
2170 | } | |
2171 | return 0; | |
2172 | } | |
2173 | ||
2174 | ||
2175 | int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) | |
2176 | { | |
2177 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) { | |
cbbf9d3e | 2178 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
2179 | return -1; |
2180 | } | |
2181 | ||
2182 | return 0; | |
2183 | } | |
2184 | ||
2185 | /* | |
2186 | * Async fchmod() | |
2187 | */ | |
2188 | int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data) | |
2189 | { | |
2190 | struct nfs_cb_data *data; | |
2191 | ||
2192 | data = malloc(sizeof(struct nfs_cb_data)); | |
2193 | if (data == NULL) { | |
cbbf9d3e | 2194 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data"); |
84004dbf RS |
2195 | return -1; |
2196 | } | |
ea98629a | 2197 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
2198 | data->nfs = nfs; |
2199 | data->cb = cb; | |
2200 | data->private_data = private_data; | |
2201 | data->continue_int = mode; | |
2202 | data->fh.data.data_len = nfsfh->fh.data.data_len; | |
2203 | data->fh.data.data_val = malloc(data->fh.data.data_len); | |
2204 | if (data->fh.data.data_val == NULL) { | |
2205 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh"); | |
2206 | free_nfs_cb_data(data); | |
2207 | return -1; | |
2208 | } | |
2209 | memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len); | |
2210 | ||
2211 | if (nfs_chmod_continue_internal(nfs, data) != 0) { | |
2212 | free_nfs_cb_data(data); | |
2213 | return -1; | |
2214 | } | |
2215 | ||
2216 | return 0; | |
2217 | } | |
2218 | ||
2219 | ||
2220 | ||
2221 | /* | |
2222 | * Async chown() | |
2223 | */ | |
2224 | static void nfs_chown_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2225 | { | |
2226 | struct nfs_cb_data *data = private_data; | |
2227 | struct nfs_context *nfs = data->nfs; | |
2228 | SETATTR3res *res; | |
2229 | ||
2230 | if (status == RPC_STATUS_ERROR) { | |
2231 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2232 | free_nfs_cb_data(data); | |
2233 | return; | |
2234 | } | |
2235 | if (status == RPC_STATUS_CANCEL) { | |
2236 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2237 | free_nfs_cb_data(data); | |
2238 | return; | |
2239 | } | |
2240 | ||
2241 | res = command_data; | |
2242 | if (res->status != NFS3_OK) { | |
2243 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
2244 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2245 | free_nfs_cb_data(data); | |
2246 | return; | |
2247 | } | |
2248 | ||
2249 | data->cb(0, nfs, NULL, data->private_data); | |
2250 | free_nfs_cb_data(data); | |
2251 | } | |
2252 | ||
2253 | struct nfs_chown_data { | |
2254 | uid_t uid; | |
2255 | gid_t gid; | |
2256 | }; | |
2257 | ||
2258 | static int nfs_chown_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2259 | { | |
2260 | SETATTR3args args; | |
2261 | struct nfs_chown_data *chown_data = data->continue_data; | |
2262 | ||
ea98629a | 2263 | memset(&args, 0, sizeof(SETATTR3args)); |
84004dbf RS |
2264 | args.object.data.data_len = data->fh.data.data_len; |
2265 | args.object.data.data_val = data->fh.data.data_val; | |
2266 | if (chown_data->uid != (uid_t)-1) { | |
2267 | args.new_attributes.uid.set_it = 1; | |
2268 | args.new_attributes.uid.set_uid3_u.uid = chown_data->uid; | |
2269 | } | |
2270 | if (chown_data->gid != (gid_t)-1) { | |
2271 | args.new_attributes.gid.set_it = 1; | |
2272 | args.new_attributes.gid.set_gid3_u.gid = chown_data->gid; | |
2273 | } | |
2274 | ||
2275 | if (rpc_nfs_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) { | |
2276 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); | |
2277 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2278 | free_nfs_cb_data(data); | |
2279 | return -1; | |
2280 | } | |
2281 | return 0; | |
2282 | } | |
2283 | ||
2284 | ||
2285 | int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data) | |
2286 | { | |
2287 | struct nfs_chown_data *chown_data; | |
2288 | ||
2289 | chown_data = malloc(sizeof(struct nfs_chown_data)); | |
2290 | if (chown_data == NULL) { | |
cbbf9d3e | 2291 | rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure"); |
84004dbf RS |
2292 | return -1; |
2293 | } | |
2294 | ||
2295 | chown_data->uid = uid; | |
2296 | chown_data->gid = gid; | |
2297 | ||
2298 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) { | |
cbbf9d3e | 2299 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
2300 | return -1; |
2301 | } | |
2302 | ||
2303 | return 0; | |
2304 | } | |
2305 | ||
2306 | ||
2307 | /* | |
2308 | * Async fchown() | |
2309 | */ | |
2310 | int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data) | |
2311 | { | |
2312 | struct nfs_cb_data *data; | |
2313 | struct nfs_chown_data *chown_data; | |
2314 | ||
2315 | chown_data = malloc(sizeof(struct nfs_chown_data)); | |
2316 | if (chown_data == NULL) { | |
cbbf9d3e | 2317 | rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure"); |
84004dbf RS |
2318 | return -1; |
2319 | } | |
2320 | ||
2321 | chown_data->uid = uid; | |
2322 | chown_data->gid = gid; | |
2323 | ||
2324 | ||
2325 | data = malloc(sizeof(struct nfs_cb_data)); | |
2326 | if (data == NULL) { | |
cbbf9d3e | 2327 | rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data"); |
84004dbf RS |
2328 | return -1; |
2329 | } | |
ea98629a | 2330 | memset(data, 0, sizeof(struct nfs_cb_data)); |
84004dbf RS |
2331 | data->nfs = nfs; |
2332 | data->cb = cb; | |
2333 | data->private_data = private_data; | |
2334 | data->continue_data = chown_data; | |
2335 | data->fh.data.data_len = nfsfh->fh.data.data_len; | |
2336 | data->fh.data.data_val = malloc(data->fh.data.data_len); | |
2337 | if (data->fh.data.data_val == NULL) { | |
2338 | rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh"); | |
2339 | free_nfs_cb_data(data); | |
2340 | return -1; | |
2341 | } | |
2342 | memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len); | |
2343 | ||
2344 | ||
2345 | if (nfs_chown_continue_internal(nfs, data) != 0) { | |
2346 | free_nfs_cb_data(data); | |
2347 | return -1; | |
2348 | } | |
2349 | ||
2350 | return 0; | |
2351 | } | |
2352 | ||
2353 | ||
2354 | ||
2355 | ||
2356 | ||
2357 | /* | |
2358 | * Async utimes() | |
2359 | */ | |
2360 | static void nfs_utimes_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2361 | { | |
2362 | struct nfs_cb_data *data = private_data; | |
2363 | struct nfs_context *nfs = data->nfs; | |
2364 | SETATTR3res *res; | |
2365 | ||
2366 | if (status == RPC_STATUS_ERROR) { | |
2367 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2368 | free_nfs_cb_data(data); | |
2369 | return; | |
2370 | } | |
2371 | if (status == RPC_STATUS_CANCEL) { | |
2372 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2373 | free_nfs_cb_data(data); | |
2374 | return; | |
2375 | } | |
2376 | ||
2377 | res = command_data; | |
2378 | if (res->status != NFS3_OK) { | |
2379 | rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); | |
2380 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2381 | free_nfs_cb_data(data); | |
2382 | return; | |
2383 | } | |
2384 | ||
2385 | data->cb(0, nfs, NULL, data->private_data); | |
2386 | free_nfs_cb_data(data); | |
2387 | } | |
2388 | ||
2389 | static int nfs_utimes_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2390 | { | |
2391 | SETATTR3args args; | |
2392 | struct timeval *utimes_data = data->continue_data; | |
2393 | ||
ea98629a | 2394 | memset(&args, 0, sizeof(SETATTR3args)); |
84004dbf RS |
2395 | args.object.data.data_len = data->fh.data.data_len; |
2396 | args.object.data.data_val = data->fh.data.data_val; | |
2397 | if (utimes_data != NULL) { | |
2398 | args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME; | |
2399 | args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec; | |
2400 | args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000; | |
2401 | args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME; | |
2402 | args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec; | |
2403 | args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000; | |
2404 | } else { | |
2405 | args.new_attributes.atime.set_it = SET_TO_SERVER_TIME; | |
2406 | args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME; | |
2407 | } | |
2408 | ||
2409 | if (rpc_nfs_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) { | |
2410 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path); | |
2411 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2412 | free_nfs_cb_data(data); | |
2413 | return -1; | |
2414 | } | |
2415 | return 0; | |
2416 | } | |
2417 | ||
2418 | ||
2419 | int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data) | |
2420 | { | |
2421 | struct timeval *new_times = NULL; | |
2422 | ||
2423 | if (times != NULL) { | |
2424 | new_times = malloc(sizeof(struct timeval)*2); | |
2425 | if (new_times == NULL) { | |
cbbf9d3e | 2426 | rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure"); |
84004dbf RS |
2427 | return -1; |
2428 | } | |
2429 | ||
2430 | memcpy(new_times, times, sizeof(struct timeval)*2); | |
2431 | } | |
2432 | ||
2433 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) { | |
cbbf9d3e | 2434 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
2435 | return -1; |
2436 | } | |
2437 | ||
2438 | return 0; | |
2439 | } | |
2440 | ||
2441 | /* | |
2442 | * Async utime() | |
2443 | */ | |
2444 | int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data) | |
2445 | { | |
2446 | struct timeval *new_times = NULL; | |
2447 | ||
2448 | if (times != NULL) { | |
2449 | new_times = malloc(sizeof(struct timeval)*2); | |
2450 | if (new_times == NULL) { | |
cbbf9d3e | 2451 | rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure"); |
84004dbf RS |
2452 | return -1; |
2453 | } | |
2454 | ||
2455 | new_times[0].tv_sec = times->actime; | |
2456 | new_times[0].tv_usec = 0; | |
2457 | new_times[1].tv_sec = times->modtime; | |
2458 | new_times[1].tv_usec = 0; | |
2459 | } | |
2460 | ||
2461 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) { | |
cbbf9d3e | 2462 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
84004dbf RS |
2463 | return -1; |
2464 | } | |
2465 | ||
2466 | return 0; | |
2467 | } | |
2468 | ||
2469 | ||
2470 | ||
2471 | ||
2472 | ||
2473 | /* | |
2474 | * Async access() | |
2475 | */ | |
2476 | static void nfs_access_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2477 | { | |
2478 | ACCESS3res *res; | |
2479 | struct nfs_cb_data *data = private_data; | |
2480 | struct nfs_context *nfs = data->nfs; | |
2481 | unsigned int nfsmode = 0; | |
2482 | ||
2483 | if (status == RPC_STATUS_ERROR) { | |
2484 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2485 | free_nfs_cb_data(data); | |
2486 | return; | |
2487 | } | |
2488 | if (status == RPC_STATUS_CANCEL) { | |
2489 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2490 | free_nfs_cb_data(data); | |
2491 | return; | |
2492 | } | |
2493 | ||
2494 | res = command_data; | |
2495 | if (res->status != NFS3_OK) { | |
2496 | 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)); | |
2497 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2498 | free_nfs_cb_data(data); | |
2499 | return; | |
2500 | } | |
2501 | ||
2502 | if (data->continue_int & R_OK) { | |
2503 | nfsmode |= ACCESS3_READ; | |
2504 | } | |
2505 | if (data->continue_int & W_OK) { | |
2506 | nfsmode |= ACCESS3_MODIFY; | |
2507 | } | |
2508 | if (data->continue_int & X_OK) { | |
2509 | nfsmode |= ACCESS3_EXECUTE; | |
2510 | } | |
2511 | ||
2512 | if (res->ACCESS3res_u.resok.access != nfsmode) { | |
2513 | rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c", | |
2514 | nfsmode&ACCESS3_READ?'r':'-', | |
2515 | nfsmode&ACCESS3_MODIFY?'w':'-', | |
2516 | nfsmode&ACCESS3_EXECUTE?'x':'-', | |
2517 | res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-', | |
2518 | res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-', | |
2519 | res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-'); | |
2520 | data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2521 | free_nfs_cb_data(data); | |
2522 | return; | |
2523 | } | |
2524 | ||
2525 | data->cb(0, nfs, NULL, data->private_data); | |
2526 | free_nfs_cb_data(data); | |
2527 | } | |
2528 | ||
2529 | static int nfs_access_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2530 | { | |
2531 | int nfsmode = 0; | |
2532 | ||
2533 | if (data->continue_int & R_OK) { | |
2534 | nfsmode |= ACCESS3_READ; | |
2535 | } | |
2536 | if (data->continue_int & W_OK) { | |
2537 | nfsmode |= ACCESS3_MODIFY; | |
2538 | } | |
2539 | if (data->continue_int & X_OK) { | |
2540 | nfsmode |= ACCESS3_EXECUTE; | |
2541 | } | |
2542 | ||
2543 | if (rpc_nfs_access_async(nfs->rpc, nfs_access_cb, &data->fh, nfsmode, data) != 0) { | |
2544 | rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path); | |
2545 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2546 | free_nfs_cb_data(data); | |
2547 | return -1; | |
2548 | } | |
2549 | return 0; | |
2550 | } | |
2551 | ||
2552 | int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data) | |
2553 | { | |
2554 | if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) { | |
cbbf9d3e RS |
2555 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
2556 | return -1; | |
84004dbf RS |
2557 | } |
2558 | ||
2559 | return 0; | |
2560 | } | |
2561 | ||
2562 | ||
2563 | ||
2564 | /* | |
2565 | * Async symlink() | |
2566 | */ | |
2567 | struct nfs_symlink_data { | |
2568 | char *oldpath; | |
2569 | char *newpathparent; | |
2570 | char *newpathobject; | |
2571 | }; | |
2572 | ||
2573 | static void free_nfs_symlink_data(void *mem) | |
2574 | { | |
2575 | struct nfs_symlink_data *data = mem; | |
2576 | ||
2577 | if (data->oldpath != NULL) { | |
2578 | free(data->oldpath); | |
2579 | } | |
2580 | if (data->newpathparent != NULL) { | |
2581 | free(data->newpathparent); | |
2582 | } | |
2583 | if (data->newpathobject != NULL) { | |
2584 | free(data->newpathobject); | |
2585 | } | |
2586 | free(data); | |
2587 | } | |
2588 | ||
2589 | static void nfs_symlink_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2590 | { | |
2591 | SYMLINK3res *res; | |
2592 | struct nfs_cb_data *data = private_data; | |
2593 | struct nfs_context *nfs = data->nfs; | |
2594 | struct nfs_symlink_data *symlink_data = data->continue_data; | |
2595 | ||
2596 | if (status == RPC_STATUS_ERROR) { | |
2597 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2598 | free_nfs_cb_data(data); | |
2599 | return; | |
2600 | } | |
2601 | if (status == RPC_STATUS_CANCEL) { | |
2602 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2603 | free_nfs_cb_data(data); | |
2604 | return; | |
2605 | } | |
2606 | ||
2607 | res = command_data; | |
2608 | if (res->status != NFS3_OK) { | |
2609 | 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)); | |
2610 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2611 | free_nfs_cb_data(data); | |
2612 | return; | |
2613 | } | |
2614 | ||
2615 | data->cb(0, nfs, NULL, data->private_data); | |
2616 | free_nfs_cb_data(data); | |
2617 | } | |
2618 | ||
2619 | static int nfs_symlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2620 | { | |
2621 | struct nfs_symlink_data *symlink_data = data->continue_data; | |
2622 | ||
2623 | if (rpc_nfs_symlink_async(nfs->rpc, nfs_symlink_cb, &data->fh, symlink_data->newpathobject, symlink_data->oldpath, data) != 0) { | |
2624 | rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path); | |
2625 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2626 | free_nfs_cb_data(data); | |
2627 | return -1; | |
2628 | } | |
2629 | return 0; | |
2630 | } | |
2631 | ||
2632 | int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) | |
2633 | { | |
2634 | char *ptr; | |
2635 | struct nfs_symlink_data *symlink_data; | |
2636 | ||
2637 | symlink_data = malloc(sizeof(struct nfs_symlink_data)); | |
2638 | if (symlink_data == NULL) { | |
cbbf9d3e | 2639 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data"); |
84004dbf RS |
2640 | return -1; |
2641 | } | |
ea98629a | 2642 | memset(symlink_data, 0, sizeof(struct nfs_symlink_data)); |
84004dbf RS |
2643 | |
2644 | symlink_data->oldpath = strdup(oldpath); | |
2645 | if (symlink_data->oldpath == NULL) { | |
cbbf9d3e | 2646 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
84004dbf | 2647 | free_nfs_symlink_data(symlink_data); |
cbbf9d3e | 2648 | return -1; |
84004dbf RS |
2649 | } |
2650 | ||
2651 | symlink_data->newpathparent = strdup(newpath); | |
2652 | if (symlink_data->newpathparent == NULL) { | |
cbbf9d3e | 2653 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path"); |
84004dbf | 2654 | free_nfs_symlink_data(symlink_data); |
cbbf9d3e | 2655 | return -1; |
84004dbf RS |
2656 | } |
2657 | ||
93e9306f | 2658 | ptr = strrchr(symlink_data->newpathparent, '/'); |
84004dbf | 2659 | if (ptr == NULL) { |
cbbf9d3e | 2660 | rpc_set_error(nfs->rpc, "Invalid path %s", oldpath); |
84004dbf | 2661 | free_nfs_symlink_data(symlink_data); |
cbbf9d3e | 2662 | return -1; |
84004dbf RS |
2663 | } |
2664 | *ptr = 0; | |
2665 | ptr++; | |
2666 | ||
2667 | symlink_data->newpathobject = strdup(ptr); | |
2668 | if (symlink_data->newpathobject == NULL) { | |
cbbf9d3e | 2669 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path"); |
84004dbf | 2670 | free_nfs_symlink_data(symlink_data); |
cbbf9d3e | 2671 | return -1; |
84004dbf RS |
2672 | } |
2673 | ||
2674 | 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 |
2675 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
2676 | return -1; | |
84004dbf RS |
2677 | } |
2678 | ||
2679 | return 0; | |
2680 | } | |
2681 | ||
2682 | ||
2683 | ||
2684 | /* | |
2685 | * Async rename() | |
2686 | */ | |
2687 | struct nfs_rename_data { | |
2688 | char *oldpath; | |
2689 | char *oldobject; | |
2690 | struct nfs_fh3 olddir; | |
2691 | char *newpath; | |
2692 | char *newobject; | |
2693 | struct nfs_fh3 newdir; | |
2694 | }; | |
2695 | ||
2696 | static void free_nfs_rename_data(void *mem) | |
2697 | { | |
2698 | struct nfs_rename_data *data = mem; | |
2699 | ||
2700 | if (data->oldpath != NULL) { | |
2701 | free(data->oldpath); | |
2702 | } | |
2703 | if (data->olddir.data.data_val != NULL) { | |
2704 | free(data->olddir.data.data_val); | |
2705 | } | |
2706 | if (data->newpath != NULL) { | |
2707 | free(data->newpath); | |
2708 | } | |
2709 | if (data->newdir.data.data_val != NULL) { | |
2710 | free(data->newdir.data.data_val); | |
2711 | } | |
2712 | free(data); | |
2713 | } | |
2714 | ||
2715 | static void nfs_rename_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2716 | { | |
2717 | RENAME3res *res; | |
2718 | struct nfs_cb_data *data = private_data; | |
2719 | struct nfs_context *nfs = data->nfs; | |
2720 | struct nfs_rename_data *rename_data = data->continue_data; | |
2721 | ||
2722 | if (status == RPC_STATUS_ERROR) { | |
2723 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2724 | free_nfs_cb_data(data); | |
2725 | return; | |
2726 | } | |
2727 | if (status == RPC_STATUS_CANCEL) { | |
2728 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2729 | free_nfs_cb_data(data); | |
2730 | return; | |
2731 | } | |
2732 | ||
2733 | res = command_data; | |
2734 | if (res->status != NFS3_OK) { | |
2735 | 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)); | |
2736 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2737 | free_nfs_cb_data(data); | |
2738 | return; | |
2739 | } | |
2740 | ||
2741 | data->cb(0, nfs, NULL, data->private_data); | |
2742 | free_nfs_cb_data(data); | |
2743 | } | |
2744 | ||
2745 | static int nfs_rename_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2746 | { | |
2747 | struct nfs_rename_data *rename_data = data->continue_data; | |
2748 | ||
2749 | /* steal the filehandle */ | |
2750 | rename_data->newdir.data.data_len = data->fh.data.data_len; | |
2751 | rename_data->newdir.data.data_val = data->fh.data.data_val; | |
2752 | data->fh.data.data_val = NULL; | |
2753 | ||
2754 | if (rpc_nfs_rename_async(nfs->rpc, nfs_rename_cb, &rename_data->olddir, rename_data->oldobject, &rename_data->newdir, rename_data->newobject, data) != 0) { | |
2755 | rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path); | |
2756 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2757 | free_nfs_cb_data(data); | |
2758 | return -1; | |
2759 | } | |
2760 | return 0; | |
2761 | } | |
2762 | ||
2763 | ||
2764 | static int nfs_rename_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2765 | { | |
2766 | struct nfs_rename_data *rename_data = data->continue_data; | |
2767 | ||
2768 | /* steal the filehandle */ | |
2769 | rename_data->olddir.data.data_len = data->fh.data.data_len; | |
2770 | rename_data->olddir.data.data_val = data->fh.data.data_val; | |
2771 | data->fh.data.data_val = NULL; | |
2772 | ||
2773 | 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) { | |
2774 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", rename_data->newpath); | |
2775 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2776 | free_nfs_cb_data(data); | |
2777 | return -1; | |
2778 | } | |
2779 | data->continue_data = NULL; | |
2780 | free_nfs_cb_data(data); | |
2781 | ||
2782 | return 0; | |
2783 | } | |
2784 | ||
2785 | ||
2786 | int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) | |
2787 | { | |
2788 | char *ptr; | |
2789 | struct nfs_rename_data *rename_data; | |
2790 | ||
2791 | rename_data = malloc(sizeof(struct nfs_rename_data)); | |
2792 | if (rename_data == NULL) { | |
cbbf9d3e | 2793 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data"); |
84004dbf RS |
2794 | return -1; |
2795 | } | |
ea98629a | 2796 | memset(rename_data, 0, sizeof(struct nfs_rename_data)); |
84004dbf RS |
2797 | |
2798 | rename_data->oldpath = strdup(oldpath); | |
2799 | if (rename_data->oldpath == NULL) { | |
cbbf9d3e | 2800 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
84004dbf | 2801 | free_nfs_rename_data(rename_data); |
cbbf9d3e | 2802 | return -1; |
84004dbf | 2803 | } |
93e9306f | 2804 | ptr = strrchr(rename_data->oldpath, '/'); |
84004dbf | 2805 | if (ptr == NULL) { |
cbbf9d3e | 2806 | rpc_set_error(nfs->rpc, "Invalid path %s", oldpath); |
84004dbf | 2807 | free_nfs_rename_data(rename_data); |
cbbf9d3e | 2808 | return -1; |
84004dbf RS |
2809 | } |
2810 | *ptr = 0; | |
2811 | ptr++; | |
2812 | rename_data->oldobject = ptr; | |
2813 | ||
2814 | ||
2815 | rename_data->newpath = strdup(newpath); | |
2816 | if (rename_data->newpath == NULL) { | |
cbbf9d3e | 2817 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath"); |
84004dbf | 2818 | free_nfs_rename_data(rename_data); |
cbbf9d3e | 2819 | return -1; |
84004dbf | 2820 | } |
93e9306f | 2821 | ptr = strrchr(rename_data->newpath, '/'); |
84004dbf | 2822 | if (ptr == NULL) { |
cbbf9d3e | 2823 | rpc_set_error(nfs->rpc, "Invalid path %s", newpath); |
84004dbf | 2824 | free_nfs_rename_data(rename_data); |
cbbf9d3e | 2825 | return -1; |
84004dbf RS |
2826 | } |
2827 | *ptr = 0; | |
2828 | ptr++; | |
2829 | rename_data->newobject = ptr; | |
2830 | ||
2831 | ||
2832 | 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 |
2833 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
2834 | return -1; | |
84004dbf RS |
2835 | } |
2836 | ||
2837 | return 0; | |
2838 | } | |
2839 | ||
2840 | ||
2841 | /* | |
2842 | * Async link() | |
2843 | */ | |
2844 | struct nfs_link_data { | |
2845 | char *oldpath; | |
2846 | struct nfs_fh3 oldfh; | |
2847 | char *newpath; | |
2848 | char *newobject; | |
2849 | struct nfs_fh3 newdir; | |
2850 | }; | |
2851 | ||
2852 | static void free_nfs_link_data(void *mem) | |
2853 | { | |
2854 | struct nfs_link_data *data = mem; | |
2855 | ||
2856 | if (data->oldpath != NULL) { | |
2857 | free(data->oldpath); | |
2858 | } | |
2859 | if (data->oldfh.data.data_val != NULL) { | |
2860 | free(data->oldfh.data.data_val); | |
2861 | } | |
2862 | if (data->newpath != NULL) { | |
2863 | free(data->newpath); | |
2864 | } | |
2865 | if (data->newdir.data.data_val != NULL) { | |
2866 | free(data->newdir.data.data_val); | |
2867 | } | |
2868 | free(data); | |
2869 | } | |
2870 | ||
2871 | static void nfs_link_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data) | |
2872 | { | |
2873 | LINK3res *res; | |
2874 | struct nfs_cb_data *data = private_data; | |
2875 | struct nfs_context *nfs = data->nfs; | |
2876 | struct nfs_link_data *link_data = data->continue_data; | |
2877 | ||
2878 | if (status == RPC_STATUS_ERROR) { | |
2879 | data->cb(-EFAULT, nfs, command_data, data->private_data); | |
2880 | free_nfs_cb_data(data); | |
2881 | return; | |
2882 | } | |
2883 | if (status == RPC_STATUS_CANCEL) { | |
2884 | data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); | |
2885 | free_nfs_cb_data(data); | |
2886 | return; | |
2887 | } | |
2888 | ||
2889 | res = command_data; | |
2890 | if (res->status != NFS3_OK) { | |
2891 | 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)); | |
2892 | data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2893 | free_nfs_cb_data(data); | |
2894 | return; | |
2895 | } | |
2896 | ||
2897 | data->cb(0, nfs, NULL, data->private_data); | |
2898 | free_nfs_cb_data(data); | |
2899 | } | |
2900 | ||
2901 | static int nfs_link_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2902 | { | |
2903 | struct nfs_link_data *link_data = data->continue_data; | |
2904 | ||
2905 | /* steal the filehandle */ | |
2906 | link_data->newdir.data.data_len = data->fh.data.data_len; | |
2907 | link_data->newdir.data.data_val = data->fh.data.data_val; | |
2908 | data->fh.data.data_val = NULL; | |
2909 | ||
2910 | if (rpc_nfs_link_async(nfs->rpc, nfs_link_cb, &link_data->oldfh, &link_data->newdir, link_data->newobject, data) != 0) { | |
2911 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path); | |
2912 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2913 | free_nfs_cb_data(data); | |
2914 | return -1; | |
2915 | } | |
2916 | return 0; | |
2917 | } | |
2918 | ||
2919 | ||
2920 | static int nfs_link_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data) | |
2921 | { | |
2922 | struct nfs_link_data *link_data = data->continue_data; | |
2923 | ||
2924 | /* steal the filehandle */ | |
2925 | link_data->oldfh.data.data_len = data->fh.data.data_len; | |
2926 | link_data->oldfh.data.data_val = data->fh.data.data_val; | |
2927 | data->fh.data.data_val = NULL; | |
2928 | ||
2929 | 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) { | |
2930 | rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath); | |
2931 | data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); | |
2932 | free_nfs_cb_data(data); | |
2933 | return -1; | |
2934 | } | |
2935 | data->continue_data = NULL; | |
2936 | free_nfs_cb_data(data); | |
2937 | ||
2938 | return 0; | |
2939 | } | |
2940 | ||
2941 | ||
2942 | int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data) | |
2943 | { | |
2944 | char *ptr; | |
2945 | struct nfs_link_data *link_data; | |
2946 | ||
2947 | link_data = malloc(sizeof(struct nfs_link_data)); | |
2948 | if (link_data == NULL) { | |
cbbf9d3e | 2949 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data"); |
84004dbf RS |
2950 | return -1; |
2951 | } | |
ea98629a | 2952 | memset(link_data, 0, sizeof(struct nfs_link_data)); |
84004dbf RS |
2953 | |
2954 | link_data->oldpath = strdup(oldpath); | |
2955 | if (link_data->oldpath == NULL) { | |
cbbf9d3e | 2956 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath"); |
84004dbf | 2957 | free_nfs_link_data(link_data); |
cbbf9d3e | 2958 | return -1; |
84004dbf RS |
2959 | } |
2960 | ||
2961 | link_data->newpath = strdup(newpath); | |
2962 | if (link_data->newpath == NULL) { | |
cbbf9d3e | 2963 | rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath"); |
84004dbf | 2964 | free_nfs_link_data(link_data); |
cbbf9d3e | 2965 | return -1; |
84004dbf | 2966 | } |
93e9306f | 2967 | ptr = strrchr(link_data->newpath, '/'); |
84004dbf | 2968 | if (ptr == NULL) { |
cbbf9d3e | 2969 | rpc_set_error(nfs->rpc, "Invalid path %s", newpath); |
84004dbf | 2970 | free_nfs_link_data(link_data); |
cbbf9d3e | 2971 | return -1; |
84004dbf RS |
2972 | } |
2973 | *ptr = 0; | |
2974 | ptr++; | |
2975 | link_data->newobject = ptr; | |
2976 | ||
2977 | ||
2978 | 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 |
2979 | rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); |
2980 | return -1; | |
84004dbf RS |
2981 | } |
2982 | ||
2983 | return 0; | |
2984 | } | |
2985 | ||
2986 | ||
2987 | //qqq replace later with lseek() | |
2988 | off_t nfs_get_current_offset(struct nfsfh *nfsfh) | |
2989 | { | |
2990 | return nfsfh->offset; | |
2991 | } | |
2992 | ||
17ef62fa RS |
2993 | |
2994 | ||
2995 | /* | |
2996 | * Get the maximum supported READ3 size by the server | |
2997 | */ | |
2998 | size_t nfs_get_readmax(struct nfs_context *nfs) | |
2999 | { | |
3000 | return nfs->readmax; | |
3001 | } | |
3002 | ||
3003 | /* | |
3004 | * Get the maximum supported WRITE3 size by the server | |
3005 | */ | |
3006 | size_t nfs_get_writemax(struct nfs_context *nfs) | |
3007 | { | |
18c27b73 RS |
3008 | /* Some XDR libraries can not marshall PDUs bigger than this */ |
3009 | if (nfs->writemax < 32768) { | |
3010 | return nfs->writemax; | |
3011 | } | |
3012 | return 32768; | |
17ef62fa | 3013 | } |
1896d37b RS |
3014 | |
3015 | void nfs_set_error(struct nfs_context *nfs, char *error_string, ...) | |
3016 | { | |
3017 | va_list ap; | |
1e8994af | 3018 | char *str = NULL; |
1896d37b | 3019 | |
1e8994af | 3020 | va_start(ap, error_string); |
2606f9bb RS |
3021 | str = malloc(1024); |
3022 | vsnprintf(str, 1024, error_string, ap); | |
1896d37b RS |
3023 | if (nfs->rpc->error_string != NULL) { |
3024 | free(nfs->rpc->error_string); | |
3025 | } | |
1896d37b | 3026 | nfs->rpc->error_string = str; |
a8a1b858 | 3027 | va_end(ap); |
1896d37b | 3028 | } |
7f0242ca RS |
3029 | |
3030 | ||
3031 | ||
3032 | struct mount_cb_data { | |
3033 | rpc_cb cb; | |
3034 | void *private_data; | |
3035 | char *server; | |
3036 | }; | |
3037 | ||
3038 | static void free_mount_cb_data(struct mount_cb_data *data) | |
3039 | { | |
3040 | if (data->server != NULL) { | |
3041 | free(data->server); | |
3042 | data->server = NULL; | |
3043 | } | |
3044 | ||
3045 | free(data); | |
3046 | } | |
3047 | ||
3048 | static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
3049 | { | |
3050 | struct mount_cb_data *data = private_data; | |
3051 | ||
3052 | if (status == RPC_STATUS_ERROR) { | |
3053 | data->cb(rpc, -EFAULT, command_data, data->private_data); | |
3054 | free_mount_cb_data(data); | |
3055 | return; | |
3056 | } | |
3057 | if (status == RPC_STATUS_CANCEL) { | |
3058 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
3059 | free_mount_cb_data(data); | |
3060 | return; | |
3061 | } | |
3062 | ||
3063 | data->cb(rpc, 0, command_data, data->private_data); | |
3064 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { | |
3065 | rpc_set_error(rpc, "Failed to disconnect\n"); | |
3066 | } | |
3067 | free_mount_cb_data(data); | |
3068 | } | |
3069 | ||
3070 | static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
3071 | { | |
3072 | struct mount_cb_data *data = private_data; | |
3073 | ||
3074 | if (status == RPC_STATUS_ERROR) { | |
3075 | data->cb(rpc, -EFAULT, command_data, data->private_data); | |
3076 | free_mount_cb_data(data); | |
3077 | return; | |
3078 | } | |
3079 | if (status == RPC_STATUS_CANCEL) { | |
3080 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
3081 | free_mount_cb_data(data); | |
3082 | return; | |
3083 | } | |
3084 | ||
3085 | if (rpc_mount_export_async(rpc, mount_export_5_cb, data) != 0) { | |
3086 | data->cb(rpc, -ENOMEM, command_data, data->private_data); | |
3087 | free_mount_cb_data(data); | |
3088 | return; | |
3089 | } | |
3090 | } | |
3091 | ||
3092 | static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
3093 | { | |
3094 | struct mount_cb_data *data = private_data; | |
3095 | uint32_t mount_port; | |
3096 | ||
3097 | if (status == RPC_STATUS_ERROR) { | |
3098 | data->cb(rpc, -EFAULT, command_data, data->private_data); | |
3099 | free_mount_cb_data(data); | |
3100 | return; | |
3101 | } | |
3102 | if (status == RPC_STATUS_CANCEL) { | |
3103 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
3104 | free_mount_cb_data(data); | |
3105 | return; | |
3106 | } | |
3107 | ||
3108 | mount_port = *(uint32_t *)command_data; | |
3109 | if (mount_port == 0) { | |
3110 | rpc_set_error(rpc, "RPC error. Mount program is not available"); | |
3111 | data->cb(rpc, -ENOENT, command_data, data->private_data); | |
3112 | free_mount_cb_data(data); | |
3113 | return; | |
3114 | } | |
3115 | ||
3116 | rpc_disconnect(rpc, "normal disconnect"); | |
3117 | if (rpc_connect_async(rpc, data->server, mount_port, mount_export_4_cb, data) != 0) { | |
3118 | data->cb(rpc, -ENOMEM, command_data, data->private_data); | |
3119 | free_mount_cb_data(data); | |
3120 | return; | |
3121 | } | |
3122 | } | |
3123 | ||
3124 | static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
3125 | { | |
3126 | struct mount_cb_data *data = private_data; | |
3127 | ||
3128 | if (status == RPC_STATUS_ERROR) { | |
3129 | data->cb(rpc, -EFAULT, command_data, data->private_data); | |
3130 | free_mount_cb_data(data); | |
3131 | return; | |
3132 | } | |
3133 | if (status == RPC_STATUS_CANCEL) { | |
3134 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
3135 | free_mount_cb_data(data); | |
3136 | return; | |
3137 | } | |
3138 | ||
3139 | if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, mount_export_3_cb, private_data) != 0) { | |
3140 | data->cb(rpc, -ENOMEM, command_data, data->private_data); | |
3141 | free_mount_cb_data(data); | |
3142 | return; | |
3143 | } | |
3144 | } | |
3145 | ||
3146 | static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) | |
3147 | { | |
3148 | struct mount_cb_data *data = private_data; | |
3149 | ||
3150 | if (status == RPC_STATUS_ERROR) { | |
3151 | data->cb(rpc, -EFAULT, command_data, data->private_data); | |
3152 | free_mount_cb_data(data); | |
3153 | return; | |
3154 | } | |
3155 | if (status == RPC_STATUS_CANCEL) { | |
3156 | data->cb(rpc, -EINTR, "Command was cancelled", data->private_data); | |
3157 | free_mount_cb_data(data); | |
3158 | return; | |
3159 | } | |
3160 | ||
3161 | if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) { | |
3162 | data->cb(rpc, -ENOMEM, command_data, data->private_data); | |
3163 | free_mount_cb_data(data); | |
3164 | return; | |
3165 | } | |
3166 | } | |
3167 | ||
3168 | int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data) | |
3169 | { | |
3170 | struct mount_cb_data *data; | |
3171 | ||
3172 | data = malloc(sizeof(struct mount_cb_data)); | |
3173 | if (data == NULL) { | |
3174 | return -1; | |
3175 | } | |
ea98629a | 3176 | memset(data, 0, sizeof(struct mount_cb_data)); |
7f0242ca RS |
3177 | data->cb = cb; |
3178 | data->private_data = private_data; | |
3179 | data->server = strdup(server); | |
3180 | if (data->server == NULL) { | |
3181 | free_mount_cb_data(data); | |
3182 | return -1; | |
3183 | } | |
3184 | if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) { | |
3185 | free_mount_cb_data(data); | |
3186 | return -1; | |
3187 | } | |
3188 | ||
3189 | return 0; | |
3190 | } | |
3191 | ||
df5af25f RS |
3192 | struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs) |
3193 | { | |
3194 | return nfs->rpc; | |
3195 | } | |
3196 | ||
b077fdeb RS |
3197 | const char *nfs_get_server(struct nfs_context *nfs) { |
3198 | return nfs->server; | |
3199 | } | |
3200 | ||
3201 | const char *nfs_get_export(struct nfs_context *nfs) { | |
3202 | return nfs->export; | |
3203 | } | |
0ad9a1f1 | 3204 |