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