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