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