Line | Count | Source |
1 | | /* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */ |
2 | | /* |
3 | | Copyright (C) 2017 by Ronnie Sahlberg <ronniesahlberg@gmail.com> |
4 | | |
5 | | This program is free software; you can redistribute it and/or modify |
6 | | it under the terms of the GNU Lesser General Public License as published by |
7 | | the Free Software Foundation; either version 2.1 of the License, or |
8 | | (at your option) any later version. |
9 | | |
10 | | This program is distributed in the hope that it will be useful, |
11 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | GNU Lesser General Public License for more details. |
14 | | |
15 | | You should have received a copy of the GNU Lesser General Public License |
16 | | along with this program; if not, see <http://www.gnu.org/licenses/>. |
17 | | */ |
18 | | /* |
19 | | * High level api to nfsv4 filesystems |
20 | | */ |
21 | | #ifndef _GNU_SOURCE |
22 | | #define _GNU_SOURCE |
23 | | #endif |
24 | | |
25 | | #ifdef HAVE_CONFIG_H |
26 | | #include "config.h" |
27 | | #endif |
28 | | |
29 | | #ifdef AROS |
30 | | #include "aros_compat.h" |
31 | | #endif |
32 | | |
33 | | #ifdef PS2_EE |
34 | | #include "ps2_compat.h" |
35 | | #endif |
36 | | |
37 | | #ifdef PS3_PPU |
38 | | #include "ps3_compat.h" |
39 | | #endif |
40 | | |
41 | | #ifdef WIN32 |
42 | | #include <win32/win32_compat.h> |
43 | | #endif |
44 | | |
45 | | #ifdef HAVE_INTTYPES_H |
46 | | #include <inttypes.h> |
47 | | #else |
48 | | #define PRIu64 "llu" |
49 | | #endif |
50 | | |
51 | | #ifdef HAVE_UTIME_H |
52 | | #include <utime.h> |
53 | | #endif |
54 | | |
55 | | #ifdef HAVE_UNISTD_H |
56 | | #include <unistd.h> |
57 | | #endif |
58 | | |
59 | | #ifdef HAVE_SYS_VFS_H |
60 | | #include <sys/vfs.h> |
61 | | #endif |
62 | | |
63 | | #ifdef HAVE_SYS_STATVFS_H |
64 | | #include <sys/statvfs.h> |
65 | | #endif |
66 | | |
67 | | #if defined(__ANDROID__) && !defined(HAVE_SYS_STATVFS_H) |
68 | | #define statvfs statfs |
69 | | #endif |
70 | | |
71 | | #ifdef HAVE_NETINET_IN_H |
72 | | #include <netinet/in.h> |
73 | | #endif |
74 | | |
75 | | #ifdef HAVE_STRINGS_H |
76 | | #include <strings.h> |
77 | | #endif |
78 | | |
79 | | #ifdef MAJOR_IN_MKDEV |
80 | | #include <sys/mkdev.h> |
81 | | #endif |
82 | | |
83 | | #ifdef HAVE_SYS_SYSMACROS_H |
84 | | #include <sys/sysmacros.h> |
85 | | #endif |
86 | | |
87 | | #ifdef WIN32 |
88 | | #include <win32/win32_compat.h> |
89 | | #endif |
90 | | |
91 | | #include <ctype.h> |
92 | | #include <stdio.h> |
93 | | #include <stdarg.h> |
94 | | #include <stdlib.h> |
95 | | #include <string.h> |
96 | | #include <assert.h> |
97 | | #include <errno.h> |
98 | | #include <time.h> |
99 | | #include <sys/types.h> |
100 | | #include <sys/stat.h> |
101 | | #include <fcntl.h> |
102 | | #include "libnfs-zdr.h" |
103 | | #include "slist.h" |
104 | | #include "libnfs.h" |
105 | | #include "libnfs-raw.h" |
106 | | #include "libnfs-raw-nfs4.h" |
107 | | #include "libnfs-private.h" |
108 | | |
109 | | /* |
110 | | * Naming convention in this file: |
111 | | * |
112 | | * nfs4_* works for every NFSv4 minor version |
113 | | * nfs40_* an NFSv4.0 only operation. SETCLIENTID, SETCLIENTID_CONFIRM and |
114 | | * OPEN_CONFIRM do not exist from minor version 1 onwards. Internal |
115 | | * callbacks are not marked, only the operations themselves. |
116 | | * nfs42_* NFSv4.2 only, built only when HAVE_NFS4_2 is defined. |
117 | | * |
118 | | * Every op array in this file is deliberately declared one element larger |
119 | | * than the operations put into it. From minor version 1 onwards a COMPOUND |
120 | | * must lead with SEQUENCE, and nfs4_start_compound() places it there; the |
121 | | * spare element is what it uses. It simply goes unused under NFSv4.0, which |
122 | | * costs one stack slot and keeps the sizes uniform rather than conditional. |
123 | | */ |
124 | | |
125 | | #ifndef discard_const |
126 | 0 | #define discard_const(ptr) ((void *)((intptr_t)(ptr))) |
127 | | #endif |
128 | | |
129 | | struct nfs4_cb_data; |
130 | | typedef int (*op_filler)(struct nfs4_cb_data *data, nfs_argop4 *op); |
131 | | |
132 | | struct lookup_link_data { |
133 | | unsigned int idx; |
134 | | unsigned int count; |
135 | | }; |
136 | | |
137 | | typedef void (*blob_free)(void *); |
138 | | |
139 | | struct nfs4_blob { |
140 | | int len; |
141 | | void *val; |
142 | | blob_free free; |
143 | | }; |
144 | | |
145 | | /* Function and arguments to append the requested operations we want |
146 | | * for the resolved path. |
147 | | */ |
148 | | struct lookup_filler { |
149 | | op_filler func; |
150 | | int max_op; |
151 | | int flags; |
152 | | void *data; /* Freed by nfs4_cb_data destructor */ |
153 | | |
154 | | struct nfs4_blob blob0; |
155 | | struct nfs4_blob blob1; |
156 | | struct nfs4_blob blob2; |
157 | | struct nfs4_blob blob3; |
158 | | }; |
159 | | |
160 | | struct rw_data { |
161 | | uint64_t offset; |
162 | | uint32_t count; |
163 | | int update_pos; |
164 | | }; |
165 | | |
166 | | struct nfs4_cb_data { |
167 | | struct nfs_context *nfs; |
168 | | /* Do not follow symlinks for the final component on a lookup. |
169 | | * I.e. stat vs lstat |
170 | | */ |
171 | 0 | #define LOOKUP_FLAG_NO_FOLLOW 0x0001 |
172 | 0 | #define LOOKUP_FLAG_IS_STATVFS64 0x0002 |
173 | | /* |
174 | | * Operate on the named attribute directory of the file the path names, |
175 | | * rather than on the file itself. OPENATTR is placed after the path has been |
176 | | * resolved, which leaves the attribute directory as the current filehandle, |
177 | | * so every operation that follows applies to it. _CREATE asks the server to |
178 | | * create the directory if the file does not have one yet. |
179 | | */ |
180 | 0 | #define LOOKUP_FLAG_OPENATTR 0x0010 |
181 | 0 | #define LOOKUP_FLAG_OPENATTR_CREATE 0x0020 |
182 | | /* |
183 | | * Every flag above lives in the same word as this one, so none of them may |
184 | | * reuse its bit. It used to share 0x0004 with LOOKUP_FLAG_OPENATTR, which |
185 | | * made an open taken under the open semaphore look like a request for the |
186 | | * named attribute directory, and made a named attribute call post a |
187 | | * semaphore it never waited on. |
188 | | */ |
189 | | #define MUTEX_HELD 0x0004 |
190 | | /* Return a struct nfs4_stat_64 to the callback instead of nfs_stat_64. */ |
191 | 0 | #define STAT64_FLAG_NFS4 0x0040 |
192 | | int flags; |
193 | | |
194 | | /* Internal callback for open-with-continuation use */ |
195 | | rpc_cb open_cb; |
196 | | |
197 | | /* Application callback and data */ |
198 | | nfs_cb cb; |
199 | | void *private_data; |
200 | | |
201 | | /* Used to track open_owner during open() */ |
202 | | uint32_t open_owner; |
203 | | |
204 | | /* internal callback */ |
205 | | rpc_cb continue_cb; |
206 | | |
207 | | char *path; /* path to lookup */ |
208 | | struct lookup_filler filler; |
209 | | |
210 | | /* Data we need when resolving a symlink in the path */ |
211 | | struct lookup_link_data link; |
212 | | |
213 | | /* Data we need for updating offset in read/write */ |
214 | | struct rw_data rw_data; |
215 | | }; |
216 | | |
217 | | static uint32_t standard_attributes[2] = { |
218 | | (1 << FATTR4_TYPE | |
219 | | 1 << FATTR4_SIZE | |
220 | | 1 << FATTR4_FSID | |
221 | | 1 << FATTR4_FILEID), |
222 | | (1 << (FATTR4_MODE - 32) | |
223 | | 1 << (FATTR4_NUMLINKS - 32) | |
224 | | 1 << (FATTR4_OWNER - 32) | |
225 | | 1 << (FATTR4_OWNER_GROUP - 32) | |
226 | | 1 << (FATTR4_RAWDEV - 32) | |
227 | | 1 << (FATTR4_SPACE_USED - 32) | |
228 | | 1 << (FATTR4_TIME_ACCESS - 32) | |
229 | | 1 << (FATTR4_TIME_METADATA - 32) | |
230 | | 1 << (FATTR4_TIME_MODIFY - 32)) |
231 | | }; |
232 | | static uint32_t statvfs_attributes[2] = { |
233 | | (1 << FATTR4_FSID | |
234 | | 1 << FATTR4_FILES_AVAIL | |
235 | | 1 << FATTR4_FILES_FREE | |
236 | | 1 << FATTR4_FILES_TOTAL | |
237 | | 1 << FATTR4_MAXNAME), |
238 | | (1 << (FATTR4_SPACE_AVAIL - 32) | |
239 | | 1 << (FATTR4_SPACE_FREE - 32) | |
240 | | 1 << (FATTR4_SPACE_TOTAL - 32)) |
241 | | }; |
242 | | |
243 | | static uint32_t getacl_attributes[1] = { |
244 | | (1 << FATTR4_ACL ) |
245 | | }; |
246 | | |
247 | | static uint32_t rwmax_attributes[1] = { |
248 | | (1u << FATTR4_MAXREAD | |
249 | | 1u << FATTR4_MAXWRITE ) |
250 | | }; |
251 | | static uint32_t rwmax_lease_attributes[1] = { |
252 | | (1u << FATTR4_LEASE_TIME | |
253 | | 1u << FATTR4_MAXREAD | |
254 | | 1u << FATTR4_MAXWRITE) |
255 | | }; |
256 | | |
257 | | static int |
258 | | nfs4_open_async_internal(struct nfs_context *nfs, struct nfs4_cb_data *data, |
259 | | int flags, int mode); |
260 | | |
261 | | /* Caller will free the returned path. */ |
262 | | static char * |
263 | | nfs4_resolve_path(struct nfs_context *nfs, const char *path) |
264 | 0 | { |
265 | 0 | char *new_path = NULL; |
266 | | |
267 | | /* Absolute paths we just use as is. |
268 | | * Relateive paths have cwd prepended to them and then become |
269 | | * absolute paths too. |
270 | | */ |
271 | 0 | if (path[0] == '/') { |
272 | 0 | new_path = strdup(path); |
273 | 0 | } else { |
274 | 0 | new_path = malloc(strlen(path) + strlen(nfs->nfsi->cwd) + 2); |
275 | 0 | if (new_path != NULL) { |
276 | 0 | sprintf(new_path, "%s/%s", nfs->nfsi->cwd, path); |
277 | 0 | } |
278 | 0 | } |
279 | 0 | if (new_path == NULL) { |
280 | 0 | nfs_set_error(nfs, "Out of memory: failed to " |
281 | 0 | "allocate path string"); |
282 | 0 | return NULL; |
283 | 0 | } |
284 | | |
285 | 0 | if (nfs_normalize_path(nfs, new_path)) { |
286 | 0 | nfs_set_error(nfs, "Failed to normalize real path. %s", |
287 | 0 | nfs_get_error(nfs)); |
288 | 0 | free(new_path); |
289 | 0 | return NULL; |
290 | 0 | } |
291 | | /* "/." and "/x/.." normalize to "" */ |
292 | 0 | if (new_path[0] == '\0') { |
293 | 0 | new_path[0] = '/'; |
294 | 0 | new_path[1] = '\0'; |
295 | 0 | } |
296 | |
|
297 | 0 | return new_path; |
298 | 0 | } |
299 | | |
300 | | static void |
301 | | free_nfs4_cb_data(struct nfs4_cb_data *data) |
302 | 0 | { |
303 | | #ifdef HAVE_MULTITHREADING |
304 | | if (data->flags & MUTEX_HELD) { |
305 | | nfs_mt_sem_post(&data->nfs->nfsi->nfs4_open_call_sem); |
306 | | } |
307 | | #endif |
308 | 0 | free(data->path); |
309 | 0 | free(data->filler.data); |
310 | 0 | if (data->filler.blob0.val && data->filler.blob0.free) { |
311 | 0 | data->filler.blob0.free(data->filler.blob0.val); |
312 | 0 | } |
313 | 0 | if (data->filler.blob1.val && data->filler.blob1.free) { |
314 | 0 | data->filler.blob1.free(data->filler.blob1.val); |
315 | 0 | } |
316 | 0 | if (data->filler.blob2.val && data->filler.blob2.free) { |
317 | 0 | data->filler.blob2.free(data->filler.blob2.val); |
318 | 0 | } |
319 | 0 | if (data->filler.blob3.val && data->filler.blob3.free) { |
320 | 0 | data->filler.blob3.free(data->filler.blob3.val); |
321 | 0 | } |
322 | 0 | free(data); |
323 | 0 | } |
324 | | |
325 | | static struct nfs4_cb_data * |
326 | | init_cb_data_full_path(struct nfs_context *nfs, const char *path) |
327 | 0 | { |
328 | 0 | struct nfs4_cb_data *data; |
329 | |
|
330 | 0 | data = calloc(1, sizeof(*data)); |
331 | 0 | if (data == NULL) { |
332 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
333 | 0 | "cb data"); |
334 | 0 | return NULL; |
335 | 0 | } |
336 | | |
337 | 0 | data->nfs = nfs; |
338 | 0 | data->path = nfs4_resolve_path(nfs, path); |
339 | 0 | if (data->path == NULL) { |
340 | 0 | free_nfs4_cb_data(data); |
341 | 0 | return NULL; |
342 | 0 | } |
343 | | |
344 | 0 | return data; |
345 | 0 | } |
346 | | |
347 | | static void |
348 | | data_split_path(struct nfs4_cb_data *data) |
349 | 0 | { |
350 | 0 | char *path; |
351 | 0 | path = strrchr(data->path, '/'); |
352 | 0 | if (path == data->path) { |
353 | 0 | char *ptr; |
354 | |
|
355 | 0 | for (ptr = data->path; *ptr; ptr++) { |
356 | 0 | *ptr = *(ptr + 1); |
357 | 0 | } |
358 | | /* No path to lookup */ |
359 | 0 | data->filler.data = data->path; |
360 | 0 | data->path = strdup("/"); |
361 | 0 | } else { |
362 | 0 | *path++ = 0; |
363 | 0 | data->filler.data = strdup(path); |
364 | 0 | } |
365 | 0 | } |
366 | | |
367 | | static struct nfs4_cb_data * |
368 | | init_cb_data_split_path(struct nfs_context *nfs, const char *orig_path) |
369 | 0 | { |
370 | 0 | struct nfs4_cb_data *data; |
371 | |
|
372 | 0 | data = init_cb_data_full_path(nfs, orig_path); |
373 | 0 | if (data == NULL) { |
374 | 0 | return NULL; |
375 | 0 | } |
376 | | |
377 | 0 | data_split_path(data); |
378 | 0 | return data; |
379 | 0 | } |
380 | | |
381 | | static int |
382 | | check_nfs4_error(struct nfs_context *nfs, int status, |
383 | | struct nfs4_cb_data *data, void *command_data, |
384 | | char *op_name) |
385 | 0 | { |
386 | 0 | COMPOUND4res *res = command_data; |
387 | |
|
388 | 0 | if (status == RPC_STATUS_ERROR) { |
389 | 0 | data->cb(-EFAULT, nfs, res, data->private_data); |
390 | 0 | free_nfs4_cb_data(data); |
391 | 0 | return 1; |
392 | 0 | } |
393 | 0 | if (status == RPC_STATUS_CANCEL) { |
394 | 0 | data->cb(-EINTR, nfs, "Command was cancelled", |
395 | 0 | data->private_data); |
396 | 0 | free_nfs4_cb_data(data); |
397 | 0 | return 1; |
398 | 0 | } |
399 | 0 | if (status == RPC_STATUS_TIMEOUT) { |
400 | 0 | data->cb(-EINTR, nfs, "Command timed out", |
401 | 0 | data->private_data); |
402 | 0 | free_nfs4_cb_data(data); |
403 | 0 | return 1; |
404 | 0 | } |
405 | 0 | if (res && res->status != NFS4_OK) { |
406 | 0 | if (nfs->nfsi->version == NFS_V4 && |
407 | 0 | (res->status == NFS4ERR_STALE_CLIENTID || |
408 | 0 | res->status == NFS4ERR_EXPIRED)) |
409 | 0 | nfs->rpc->nfs40_state_error = res->status; |
410 | 0 | nfs_set_error(nfs, "NFS4: %s (path %s) failed with " |
411 | 0 | "%s(%d)", op_name, |
412 | 0 | data->path, |
413 | 0 | nfsstat4_to_str(res->status), |
414 | 0 | nfsstat4_to_errno(res->status)); |
415 | 0 | data->cb(nfsstat4_to_errno(res->status), nfs, |
416 | 0 | nfs_get_error(nfs), data->private_data); |
417 | 0 | free_nfs4_cb_data(data); |
418 | 0 | return 1; |
419 | 0 | } |
420 | | |
421 | 0 | return 0; |
422 | 0 | } |
423 | | |
424 | | static int |
425 | | nfs4_find_op(struct nfs_context *nfs, struct nfs4_cb_data *data, |
426 | | COMPOUND4res *res, int op, const char *op_name) |
427 | 0 | { |
428 | 0 | int i; |
429 | |
|
430 | 0 | for (i = 0; i < (int)res->resarray.resarray_len; i++) { |
431 | 0 | if (res->resarray.resarray_val[i].resop == op) { |
432 | 0 | break; |
433 | 0 | } |
434 | 0 | } |
435 | 0 | if (i == res->resarray.resarray_len) { |
436 | 0 | nfs_set_error(nfs, "No %s result.", op_name); |
437 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
438 | 0 | free_nfs4_cb_data(data); |
439 | 0 | return -1; |
440 | 0 | } |
441 | | |
442 | 0 | return i; |
443 | 0 | } |
444 | | |
445 | | static GETFH4resok * |
446 | | nfs4_find_getfh(struct nfs_context *nfs, struct nfs4_cb_data *data, |
447 | | COMPOUND4res *res) |
448 | 0 | { |
449 | 0 | GETFH4resok *resok; |
450 | 0 | int i; |
451 | |
|
452 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_GETFH, "GETFH")) < 0) { |
453 | 0 | return NULL; |
454 | 0 | } |
455 | 0 | resok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetfh.GETFH4res_u.resok4; |
456 | 0 | if (resok->object.nfs_fh4_len == 0) { |
457 | 0 | nfs_set_error(nfs, "Server returned an empty filehandle."); |
458 | 0 | data->cb(-EIO, nfs, nfs_get_error(nfs), data->private_data); |
459 | 0 | free_nfs4_cb_data(data); |
460 | 0 | return NULL; |
461 | 0 | } |
462 | | |
463 | 0 | return resok; |
464 | 0 | } |
465 | | |
466 | | static uint64_t |
467 | | nfs_hton64(uint64_t val) |
468 | 0 | { |
469 | 0 | int i; |
470 | 0 | uint64_t res; |
471 | 0 | unsigned char *ptr = (void *)&res; |
472 | |
|
473 | 0 | for (i = 0; i < 8; i++) { |
474 | 0 | ptr[7 - i] = val & 0xff; |
475 | 0 | val >>= 8; |
476 | 0 | } |
477 | 0 | return res; |
478 | 0 | } |
479 | | |
480 | | static uint64_t |
481 | | nfs_ntoh64(uint64_t val) |
482 | 0 | { |
483 | 0 | int i; |
484 | 0 | uint64_t res; |
485 | 0 | unsigned char *ptr = (void *)&res; |
486 | |
|
487 | 0 | for (i = 0; i < 8; i++) { |
488 | 0 | ptr[7 - i] = val & 0xff; |
489 | 0 | val >>= 8; |
490 | 0 | } |
491 | 0 | return res; |
492 | 0 | } |
493 | | |
494 | | static uint64_t |
495 | | nfs_pntoh64(const uint32_t *buf) |
496 | 0 | { |
497 | 0 | uint64_t val; |
498 | |
|
499 | 0 | val = ntohl(*(uint32_t *)(void *)buf); |
500 | 0 | buf++; |
501 | 0 | val <<= 32; |
502 | 0 | val |= ntohl(*(uint32_t *)(void *)buf); |
503 | |
|
504 | 0 | return val; |
505 | 0 | } |
506 | | |
507 | | /* A numeric owner/group is the id itself, a name has no id and becomes -1. */ |
508 | | static uint64_t |
509 | | nfs_get_ugid(const char *buf, int slen) |
510 | 0 | { |
511 | 0 | uint64_t ugid = 0; |
512 | 0 | int i; |
513 | |
|
514 | 0 | if (slen <= 0) { |
515 | 0 | return (uint64_t)-1; |
516 | 0 | } |
517 | | |
518 | 0 | for (i = 0; i < slen; i++) { |
519 | | /* isdigit() takes an unsigned char value or EOF. */ |
520 | 0 | if (!isdigit((unsigned char)buf[i])) { |
521 | 0 | return (uint64_t)-1; |
522 | 0 | } |
523 | 0 | ugid = ugid * 10 + (uint64_t)(buf[i] - '0'); |
524 | | /* Out of range for a uid/gid, so it is not an id at all. */ |
525 | 0 | if (ugid > UINT32_MAX) { |
526 | 0 | return (uint64_t)-1; |
527 | 0 | } |
528 | 0 | } |
529 | | |
530 | 0 | return ugid; |
531 | 0 | } |
532 | | |
533 | | #define CHECK_GETATTR_BUF_SPACE(len, size) \ |
534 | 0 | if (len < size) { \ |
535 | 0 | nfs_set_error(nfs, "Not enough data in fattr4"); \ |
536 | 0 | return -1; \ |
537 | 0 | } |
538 | | |
539 | | static uint64_t |
540 | | specdata4_to_rdev(uint32_t *specdata) |
541 | 0 | { |
542 | 0 | uint64_t rdev = ntohl(specdata[0]); |
543 | 0 | return (rdev << 32) | ntohl(specdata[1]); |
544 | 0 | } |
545 | | |
546 | | /* |
547 | | * The server returns the attributes it chose to return, which need not be the |
548 | | * ones we asked for, and it tells us which in the attrmask. Decoding the blob |
549 | | * as if it always held the full standard set silently shifted every later |
550 | | * field when a server returned fewer. |
551 | | */ |
552 | | static int |
553 | | nfs4_attr_is_set(const bitmap4 *mask, int bit) |
554 | 0 | { |
555 | 0 | if ((uint32_t)(bit / 32) >= mask->bitmap4_len) { |
556 | 0 | return 0; |
557 | 0 | } |
558 | 0 | return !!(mask->bitmap4_val[bit / 32] & (1U << (bit % 32))); |
559 | 0 | } |
560 | | |
561 | | static int |
562 | | nfs_parse_attributes(struct nfs_context *nfs, struct nfs4_cb_data *data, |
563 | | struct nfs_stat_64 *st, char **user, char **group, |
564 | | const bitmap4 *mask, const char *buf, int len) |
565 | 0 | { |
566 | 0 | int type = 0, slen, pad; |
567 | | |
568 | | /* Type */ |
569 | 0 | if (nfs4_attr_is_set(mask, FATTR4_TYPE)) { |
570 | 0 | CHECK_GETATTR_BUF_SPACE(len, 4); |
571 | 0 | type = ntohl(*(uint32_t *)(void *)buf); |
572 | 0 | buf += 4; |
573 | 0 | len -= 4; |
574 | 0 | } |
575 | | /* Size */ |
576 | 0 | if (nfs4_attr_is_set(mask, FATTR4_SIZE)) { |
577 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
578 | 0 | st->nfs_size = nfs_pntoh64((uint32_t *)(void *)buf); |
579 | 0 | buf += 8; |
580 | 0 | len -= 8; |
581 | 0 | } |
582 | | /* FSID */ |
583 | 0 | if (nfs4_attr_is_set(mask, FATTR4_FSID)) { |
584 | 0 | uint64_t fsid[2]; |
585 | |
|
586 | 0 | CHECK_GETATTR_BUF_SPACE(len, 16); |
587 | 0 | memcpy(fsid, buf, sizeof(fsid)); |
588 | 0 | st->nfs_dev = fsid[0] ^ fsid[1]; |
589 | 0 | buf += 16; |
590 | 0 | len -= 16; |
591 | 0 | } |
592 | | /* Inode */ |
593 | 0 | if (nfs4_attr_is_set(mask, FATTR4_FILEID)) { |
594 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
595 | 0 | st->nfs_ino = nfs_pntoh64((uint32_t *)(void *)buf); |
596 | 0 | buf += 8; |
597 | 0 | len -= 8; |
598 | 0 | } |
599 | | /* Mode */ |
600 | 0 | if (nfs4_attr_is_set(mask, FATTR4_MODE)) { |
601 | 0 | CHECK_GETATTR_BUF_SPACE(len, 4); |
602 | 0 | st->nfs_mode = ntohl(*(uint32_t *)(void *)buf); |
603 | 0 | buf += 4; |
604 | 0 | len -= 4; |
605 | 0 | } |
606 | 0 | switch (type) { |
607 | 0 | case NF4REG: |
608 | 0 | st->nfs_mode |= S_IFREG; |
609 | 0 | break; |
610 | 0 | case NF4DIR: |
611 | 0 | st->nfs_mode |= S_IFDIR; |
612 | 0 | break; |
613 | 0 | case NF4BLK: |
614 | 0 | st->nfs_mode |= S_IFBLK; |
615 | 0 | break; |
616 | 0 | case NF4CHR: |
617 | 0 | st->nfs_mode |= S_IFCHR; |
618 | 0 | break; |
619 | 0 | case NF4LNK: |
620 | 0 | st->nfs_mode |= S_IFLNK; |
621 | 0 | break; |
622 | 0 | case NF4SOCK: |
623 | 0 | st->nfs_mode |= S_IFSOCK; |
624 | 0 | break; |
625 | 0 | case NF4FIFO: |
626 | 0 | st->nfs_mode |= S_IFIFO; |
627 | 0 | break; |
628 | 0 | default: |
629 | 0 | break; |
630 | 0 | } |
631 | | /* Num Links */ |
632 | 0 | if (nfs4_attr_is_set(mask, FATTR4_NUMLINKS)) { |
633 | 0 | CHECK_GETATTR_BUF_SPACE(len, 4); |
634 | 0 | st->nfs_nlink = ntohl(*(uint32_t *)(void *)buf); |
635 | 0 | buf += 4; |
636 | 0 | len -= 4; |
637 | 0 | } |
638 | | /* Owner */ |
639 | 0 | if (nfs4_attr_is_set(mask, FATTR4_OWNER)) { |
640 | 0 | CHECK_GETATTR_BUF_SPACE(len, 4); |
641 | 0 | slen = ntohl(*(uint32_t *)(void *)buf); |
642 | 0 | if (slen < 0) { |
643 | 0 | return -1; |
644 | 0 | } |
645 | 0 | buf += 4; |
646 | 0 | len -= 4; |
647 | 0 | pad = (4 - (slen & 0x03)) & 0x03; |
648 | 0 | CHECK_GETATTR_BUF_SPACE(len, slen); |
649 | 0 | st->nfs_uid = nfs_get_ugid(buf, slen); |
650 | 0 | if (user) { |
651 | 0 | *user = strndup(buf, slen); |
652 | 0 | if (*user == NULL) { |
653 | 0 | nfs_set_error(nfs, "Out of memory."); |
654 | 0 | return -1; |
655 | 0 | } |
656 | 0 | } |
657 | 0 | buf += slen; |
658 | 0 | len -= slen; |
659 | 0 | CHECK_GETATTR_BUF_SPACE(len, pad); |
660 | 0 | buf += pad; |
661 | 0 | len -= pad; |
662 | 0 | } |
663 | | /* Group */ |
664 | 0 | if (nfs4_attr_is_set(mask, FATTR4_OWNER_GROUP)) { |
665 | 0 | CHECK_GETATTR_BUF_SPACE(len, 4); |
666 | 0 | slen = ntohl(*(uint32_t *)(void *)buf); |
667 | 0 | if (slen < 0) { |
668 | 0 | return -1; |
669 | 0 | } |
670 | 0 | buf += 4; |
671 | 0 | len -= 4; |
672 | 0 | pad = (4 - (slen & 0x03)) & 0x03; |
673 | 0 | CHECK_GETATTR_BUF_SPACE(len, slen); |
674 | 0 | st->nfs_gid = nfs_get_ugid(buf, slen); |
675 | 0 | if (group) { |
676 | 0 | *group = strndup(buf, slen); |
677 | 0 | if (*group == NULL) { |
678 | 0 | nfs_set_error(nfs, "Out of memory."); |
679 | 0 | return -1; |
680 | 0 | } |
681 | 0 | } |
682 | 0 | buf += slen; |
683 | 0 | len -= slen; |
684 | 0 | CHECK_GETATTR_BUF_SPACE(len, pad); |
685 | 0 | buf += pad; |
686 | 0 | len -= pad; |
687 | 0 | } |
688 | | /* raw device */ |
689 | 0 | if (nfs4_attr_is_set(mask, FATTR4_RAWDEV)) { |
690 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
691 | 0 | st->nfs_rdev = specdata4_to_rdev((uint32_t *)(void *)buf); |
692 | 0 | buf += 8; |
693 | 0 | len -= 8; |
694 | 0 | } |
695 | | /* Space Used */ |
696 | 0 | if (nfs4_attr_is_set(mask, FATTR4_SPACE_USED)) { |
697 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
698 | 0 | st->nfs_used = nfs_pntoh64((uint32_t *)(void *)buf); |
699 | 0 | buf += 8; |
700 | 0 | len -= 8; |
701 | 0 | } |
702 | | /* ATime */ |
703 | 0 | if (nfs4_attr_is_set(mask, FATTR4_TIME_ACCESS)) { |
704 | 0 | CHECK_GETATTR_BUF_SPACE(len, 12); |
705 | 0 | st->nfs_atime = nfs_pntoh64((uint32_t *)(void *)buf); |
706 | 0 | buf += 8; |
707 | 0 | len -= 8; |
708 | 0 | st->nfs_atime_nsec = ntohl(*(uint32_t *)(void *)buf); |
709 | 0 | buf += 4; |
710 | 0 | len -= 4; |
711 | 0 | } |
712 | | /* CTime */ |
713 | 0 | if (nfs4_attr_is_set(mask, FATTR4_TIME_METADATA)) { |
714 | 0 | CHECK_GETATTR_BUF_SPACE(len, 12); |
715 | 0 | st->nfs_ctime = nfs_pntoh64((uint32_t *)(void *)buf); |
716 | 0 | buf += 8; |
717 | 0 | len -= 8; |
718 | 0 | st->nfs_ctime_nsec = ntohl(*(uint32_t *)(void *)buf); |
719 | 0 | buf += 4; |
720 | 0 | len -= 4; |
721 | 0 | } |
722 | | /* MTime */ |
723 | 0 | if (nfs4_attr_is_set(mask, FATTR4_TIME_MODIFY)) { |
724 | 0 | CHECK_GETATTR_BUF_SPACE(len, 12); |
725 | 0 | st->nfs_mtime = nfs_pntoh64((uint32_t *)(void *)buf); |
726 | 0 | buf += 8; |
727 | 0 | len -= 8; |
728 | 0 | st->nfs_mtime_nsec = ntohl(*(uint32_t *)(void *)buf); |
729 | 0 | buf += 4; |
730 | 0 | len -= 4; |
731 | 0 | } |
732 | | |
733 | 0 | st->nfs_blksize = NFS_BLKSIZE; |
734 | 0 | st->nfs_blocks = (st->nfs_used + NFS_BLKSIZE -1) / NFS_BLKSIZE; |
735 | |
|
736 | 0 | return 0; |
737 | 0 | } |
738 | | |
739 | | static int |
740 | | nfs4_num_path_components(struct nfs_context *nfs, const char *path) |
741 | 0 | { |
742 | 0 | int i; |
743 | |
|
744 | 0 | for (i = 0; (path = strchr(path, '/')); path++, i++) |
745 | 0 | ; |
746 | |
|
747 | 0 | return i; |
748 | 0 | } |
749 | | |
750 | | static int |
751 | | nfs4_op_create(struct nfs_context *nfs, nfs_argop4 *op, const char *name, |
752 | | nfs_ftype4 type, struct nfs4_blob *attrmask, |
753 | | struct nfs4_blob *attr_vals, const char *linkdata, int dev) |
754 | 0 | { |
755 | 0 | CREATE4args *cargs; |
756 | |
|
757 | 0 | op[0].argop = OP_CREATE; |
758 | 0 | cargs = &op[0].nfs_argop4_u.opcreate; |
759 | 0 | memset(cargs, 0, sizeof(*cargs)); |
760 | 0 | cargs->objtype.type = type; |
761 | 0 | cargs->objname.utf8string_len = strlen(name); |
762 | 0 | cargs->objname.utf8string_val = discard_const(name); |
763 | 0 | if (attrmask) { |
764 | 0 | cargs->createattrs.attrmask.bitmap4_len = attrmask->len; |
765 | 0 | cargs->createattrs.attrmask.bitmap4_val = attrmask->val; |
766 | 0 | } |
767 | 0 | if (attr_vals) { |
768 | 0 | cargs->createattrs.attr_vals.attrlist4_len = attr_vals->len; |
769 | 0 | cargs->createattrs.attr_vals.attrlist4_val = attr_vals->val; |
770 | 0 | } |
771 | 0 | if (linkdata) { |
772 | 0 | cargs->objtype.createtype4_u.linkdata.utf8string_len = |
773 | 0 | strlen(linkdata); |
774 | 0 | cargs->objtype.createtype4_u.linkdata.utf8string_val = |
775 | 0 | discard_const(linkdata); |
776 | 0 | } |
777 | 0 | switch (type) { |
778 | 0 | case NF4CHR: |
779 | 0 | cargs->objtype.type = NF4CHR; |
780 | 0 | cargs->objtype.createtype4_u.devdata.specdata1 = major(dev); |
781 | 0 | cargs->objtype.createtype4_u.devdata.specdata2 = minor(dev); |
782 | 0 | break; |
783 | 0 | case NF4BLK: |
784 | 0 | cargs->objtype.type = NF4BLK; |
785 | 0 | cargs->objtype.createtype4_u.devdata.specdata1 = major(dev); |
786 | 0 | cargs->objtype.createtype4_u.devdata.specdata2 = minor(dev); |
787 | 0 | break; |
788 | 0 | default: |
789 | 0 | ; |
790 | 0 | } |
791 | 0 | return 1; |
792 | 0 | } |
793 | | |
794 | | static int |
795 | | nfs4_op_commit(struct nfs_context *nfs, nfs_argop4 *op) |
796 | 0 | { |
797 | 0 | COMMIT4args *coargs; |
798 | |
|
799 | 0 | op[0].argop = OP_COMMIT; |
800 | 0 | coargs = &op[0].nfs_argop4_u.opcommit; |
801 | 0 | coargs->offset = 0; |
802 | 0 | coargs->count = 0; |
803 | |
|
804 | 0 | return 1; |
805 | 0 | } |
806 | | |
807 | | static int |
808 | | nfs4_op_close(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh) |
809 | 0 | { |
810 | 0 | CLOSE4args *clargs; |
811 | 0 | int i = 0; |
812 | |
|
813 | 0 | if (fh->is_dirty) { |
814 | 0 | i += nfs4_op_commit(nfs, &op[i]); |
815 | 0 | } |
816 | |
|
817 | 0 | op[i].argop = OP_CLOSE; |
818 | 0 | clargs = &op[i++].nfs_argop4_u.opclose; |
819 | 0 | clargs->seqid = fh->open_seqid; |
820 | 0 | clargs->open_stateid.seqid = fh->stateid.seqid; |
821 | 0 | memcpy(clargs->open_stateid.other, fh->stateid.other, 12); |
822 | |
|
823 | 0 | return i; |
824 | 0 | } |
825 | | |
826 | | static int |
827 | | nfs4_op_access(struct nfs_context *nfs, nfs_argop4 *op, uint32_t access_mask) |
828 | 0 | { |
829 | 0 | ACCESS4args *aargs; |
830 | |
|
831 | 0 | op[0].argop = OP_ACCESS; |
832 | 0 | aargs = &op[0].nfs_argop4_u.opaccess; |
833 | 0 | memset(aargs, 0, sizeof(*aargs)); |
834 | 0 | aargs->access = access_mask; |
835 | |
|
836 | 0 | return 1; |
837 | 0 | } |
838 | | |
839 | | static int |
840 | | nfs40_op_setclientid(struct nfs_context *nfs, nfs_argop4 *op, verifier4 verifier, |
841 | | const char *client_name) |
842 | 0 | { |
843 | 0 | SETCLIENTID4args *scidargs; |
844 | |
|
845 | 0 | op[0].argop = OP_SETCLIENTID; |
846 | 0 | scidargs = &op[0].nfs_argop4_u.opsetclientid; |
847 | 0 | memcpy(scidargs->client.verifier, verifier, sizeof(verifier4)); |
848 | 0 | scidargs->client.id.id_len = strlen(client_name); |
849 | 0 | scidargs->client.id.id_val = discard_const(client_name); |
850 | | /* TODO: Decide what we should do here. As long as we only |
851 | | * expose a single FD to the application we will not be able to |
852 | | * do NFSv4 callbacks easily. |
853 | | * Just give it garbage for now until we figure out how we should |
854 | | * solve this. Until then we will just have to avoid doing things |
855 | | * that require a callback. |
856 | | * ( Clients (i.e. Linux) ignore this anyway and just call back to |
857 | | * the originating address and program anyway. ) |
858 | | */ |
859 | 0 | scidargs->callback.cb_program = 0; /* NFS4_CALLBACK */ |
860 | 0 | scidargs->callback.cb_location.r_netid = "tcp"; |
861 | 0 | scidargs->callback.cb_location.r_addr = "0.0.0.0.0.0"; |
862 | 0 | scidargs->callback_ident = 0x00000001; |
863 | |
|
864 | 0 | return 1; |
865 | 0 | } |
866 | | |
867 | | static int |
868 | | nfs40_op_open_confirm(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh) |
869 | 0 | { |
870 | 0 | OPEN_CONFIRM4args *ocargs; |
871 | |
|
872 | 0 | op[0].argop = OP_OPEN_CONFIRM; |
873 | 0 | ocargs = &op[0].nfs_argop4_u.opopen_confirm; |
874 | 0 | ocargs->open_stateid.seqid = fh->stateid.seqid; |
875 | 0 | memcpy(&ocargs->open_stateid.other, fh->stateid.other, 12); |
876 | 0 | ocargs->seqid = fh->open_seqid; |
877 | |
|
878 | 0 | return 1; |
879 | 0 | } |
880 | | |
881 | | static int |
882 | | nfs4_op_truncate(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
883 | | void *sabuf) |
884 | 0 | { |
885 | 0 | SETATTR4args *saargs; |
886 | 0 | static uint32_t mask[2] = {1 << (FATTR4_SIZE), |
887 | 0 | 1 << (FATTR4_TIME_MODIFY_SET - 32)}; |
888 | |
|
889 | 0 | op[0].argop = OP_SETATTR; |
890 | 0 | saargs = &op[0].nfs_argop4_u.opsetattr; |
891 | 0 | saargs->stateid.seqid = fh->stateid.seqid; |
892 | 0 | memcpy(saargs->stateid.other, fh->stateid.other, 12); |
893 | |
|
894 | 0 | saargs->obj_attributes.attrmask.bitmap4_len = 2; |
895 | 0 | saargs->obj_attributes.attrmask.bitmap4_val = mask; |
896 | |
|
897 | 0 | saargs->obj_attributes.attr_vals.attrlist4_len = 12; |
898 | 0 | saargs->obj_attributes.attr_vals.attrlist4_val = sabuf; |
899 | |
|
900 | 0 | return 1; |
901 | 0 | } |
902 | | |
903 | | static int |
904 | | nfs4_op_chmod(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
905 | | void *sabuf) |
906 | 0 | { |
907 | 0 | SETATTR4args *saargs; |
908 | 0 | static uint32_t mask[2] = {0, |
909 | 0 | 1 << (FATTR4_MODE - 32)}; |
910 | |
|
911 | 0 | op[0].argop = OP_SETATTR; |
912 | 0 | saargs = &op[0].nfs_argop4_u.opsetattr; |
913 | 0 | if (fh) { |
914 | 0 | saargs->stateid.seqid = fh->stateid.seqid; |
915 | 0 | memcpy(saargs->stateid.other, fh->stateid.other, 12); |
916 | 0 | } |
917 | |
|
918 | 0 | saargs->obj_attributes.attrmask.bitmap4_len = 2; |
919 | 0 | saargs->obj_attributes.attrmask.bitmap4_val = mask; |
920 | |
|
921 | 0 | saargs->obj_attributes.attr_vals.attrlist4_len = 4; |
922 | 0 | saargs->obj_attributes.attr_vals.attrlist4_val = sabuf; |
923 | |
|
924 | 0 | return 1; |
925 | 0 | } |
926 | | |
927 | | static int |
928 | | nfs4_op_chown(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
929 | | void *sabuf, int len) |
930 | 0 | { |
931 | 0 | SETATTR4args *saargs; |
932 | 0 | static uint32_t mask[2] = {0, |
933 | 0 | 1 << (FATTR4_OWNER - 32) | |
934 | 0 | 1 << (FATTR4_OWNER_GROUP - 32)}; |
935 | |
|
936 | 0 | op[0].argop = OP_SETATTR; |
937 | 0 | saargs = &op[0].nfs_argop4_u.opsetattr; |
938 | 0 | if (fh) { |
939 | 0 | saargs->stateid.seqid = fh->stateid.seqid; |
940 | 0 | memcpy(saargs->stateid.other, fh->stateid.other, 12); |
941 | 0 | } |
942 | |
|
943 | 0 | saargs->obj_attributes.attrmask.bitmap4_len = 2; |
944 | 0 | saargs->obj_attributes.attrmask.bitmap4_val = mask; |
945 | |
|
946 | 0 | saargs->obj_attributes.attr_vals.attrlist4_len = len; |
947 | 0 | saargs->obj_attributes.attr_vals.attrlist4_val = sabuf; |
948 | |
|
949 | 0 | return 1; |
950 | 0 | } |
951 | | |
952 | | static int |
953 | | nfs4_op_utimes(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
954 | | void *sabuf, int len) |
955 | 0 | { |
956 | 0 | SETATTR4args *saargs; |
957 | 0 | static uint32_t mask[2] = {0, |
958 | 0 | 1 << (FATTR4_TIME_ACCESS_SET - 32) | |
959 | 0 | 1 << (FATTR4_TIME_MODIFY_SET - 32)}; |
960 | |
|
961 | 0 | op[0].argop = OP_SETATTR; |
962 | 0 | saargs = &op[0].nfs_argop4_u.opsetattr; |
963 | 0 | if (fh) { |
964 | 0 | saargs->stateid.seqid = fh->stateid.seqid; |
965 | 0 | memcpy(saargs->stateid.other, fh->stateid.other, 12); |
966 | 0 | } |
967 | |
|
968 | 0 | saargs->obj_attributes.attrmask.bitmap4_len = 2; |
969 | 0 | saargs->obj_attributes.attrmask.bitmap4_val = mask; |
970 | |
|
971 | 0 | saargs->obj_attributes.attr_vals.attrlist4_len = len; |
972 | 0 | saargs->obj_attributes.attr_vals.attrlist4_val = sabuf; |
973 | |
|
974 | 0 | return 1; |
975 | 0 | } |
976 | | |
977 | | static int |
978 | | nfs4_op_readdir(struct nfs_context *nfs, nfs_argop4 *op, uint64_t cookie) |
979 | 0 | { |
980 | 0 | READDIR4args *rdargs; |
981 | |
|
982 | 0 | op[0].argop = OP_READDIR; |
983 | 0 | rdargs = &op[0].nfs_argop4_u.opreaddir; |
984 | 0 | memset(rdargs, 0, sizeof(*rdargs)); |
985 | |
|
986 | 0 | rdargs->cookie = cookie; |
987 | 0 | rdargs->dircount = nfs->nfsi->readdir_dircount; |
988 | 0 | rdargs->maxcount = nfs->nfsi->readdir_maxcount; |
989 | 0 | rdargs->attr_request.bitmap4_len = 2; |
990 | 0 | rdargs->attr_request.bitmap4_val = standard_attributes; |
991 | |
|
992 | 0 | return 1; |
993 | 0 | } |
994 | | |
995 | | static int |
996 | | nfs4_op_rename(struct nfs_context *nfs, nfs_argop4 *op, const char *oldname, |
997 | | const char *newname) |
998 | 0 | { |
999 | 0 | RENAME4args *rargs; |
1000 | |
|
1001 | 0 | op[0].argop = OP_RENAME; |
1002 | 0 | rargs = &op[0].nfs_argop4_u.oprename; |
1003 | 0 | memset(rargs, 0, sizeof(*rargs)); |
1004 | 0 | rargs->oldname.utf8string_len = strlen(oldname); |
1005 | 0 | rargs->oldname.utf8string_val = discard_const(oldname); |
1006 | 0 | rargs->newname.utf8string_len = strlen(newname); |
1007 | 0 | rargs->newname.utf8string_val = discard_const(newname); |
1008 | |
|
1009 | 0 | return 1; |
1010 | 0 | } |
1011 | | |
1012 | | static int |
1013 | | nfs4_op_read(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
1014 | | uint64_t offset, size_t count) |
1015 | 0 | { |
1016 | 0 | READ4args *rargs; |
1017 | |
|
1018 | 0 | op[0].argop = OP_READ; |
1019 | 0 | rargs = &op[0].nfs_argop4_u.opread; |
1020 | 0 | rargs->stateid.seqid = fh->stateid.seqid; |
1021 | 0 | memcpy(rargs->stateid.other, fh->stateid.other, 12); |
1022 | 0 | rargs->offset = offset; |
1023 | 0 | rargs->count = count; |
1024 | |
|
1025 | 0 | return 1; |
1026 | 0 | } |
1027 | | |
1028 | | static int |
1029 | | nfs4_op_write(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
1030 | | uint64_t offset, size_t count, const char *buf) |
1031 | 0 | { |
1032 | 0 | WRITE4args *wargs; |
1033 | |
|
1034 | 0 | op[0].argop = OP_WRITE; |
1035 | 0 | wargs = &op[0].nfs_argop4_u.opwrite; |
1036 | 0 | wargs->stateid.seqid = fh->stateid.seqid; |
1037 | 0 | memcpy(wargs->stateid.other, fh->stateid.other, 12); |
1038 | 0 | wargs->offset = offset; |
1039 | 0 | if (fh->is_sync) { |
1040 | 0 | wargs->stable = DATA_SYNC4; |
1041 | 0 | } else { |
1042 | 0 | wargs->stable = UNSTABLE4; |
1043 | 0 | fh->is_dirty = 1; |
1044 | 0 | } |
1045 | 0 | wargs->data.data_len = count; |
1046 | 0 | wargs->data.data_val = discard_const(buf); |
1047 | |
|
1048 | 0 | return 1; |
1049 | 0 | } |
1050 | | |
1051 | | static int |
1052 | | nfs4_op_openattr(struct nfs_context *nfs _U_, nfs_argop4 *op, int createdir) |
1053 | 0 | { |
1054 | 0 | op[0].argop = OP_OPENATTR; |
1055 | 0 | op[0].nfs_argop4_u.opopenattr.createdir = createdir; |
1056 | |
|
1057 | 0 | return 1; |
1058 | 0 | } |
1059 | | |
1060 | | static int |
1061 | | nfs4_op_getfh(struct nfs_context *nfs, nfs_argop4 *op) |
1062 | 0 | { |
1063 | 0 | op[0].argop = OP_GETFH; |
1064 | |
|
1065 | 0 | return 1; |
1066 | 0 | } |
1067 | | |
1068 | | static int |
1069 | | nfs4_op_savefh(struct nfs_context *nfs, nfs_argop4 *op) |
1070 | 0 | { |
1071 | 0 | op[0].argop = OP_SAVEFH; |
1072 | |
|
1073 | 0 | return 1; |
1074 | 0 | } |
1075 | | |
1076 | | static int |
1077 | | nfs4_op_link(struct nfs_context *nfs, nfs_argop4 *op, const char *newname) |
1078 | 0 | { |
1079 | 0 | LINK4args *largs; |
1080 | |
|
1081 | 0 | op[0].argop = OP_LINK; |
1082 | 0 | largs = &op[0].nfs_argop4_u.oplink; |
1083 | 0 | memset(largs, 0, sizeof(*largs)); |
1084 | 0 | largs->newname.utf8string_len = strlen(newname); |
1085 | 0 | largs->newname.utf8string_val = discard_const(newname); |
1086 | |
|
1087 | 0 | return 1; |
1088 | 0 | } |
1089 | | |
1090 | | static int |
1091 | | nfs4_op_putfh(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *nfsfh) |
1092 | 0 | { |
1093 | 0 | PUTFH4args *pfargs; |
1094 | 0 | op[0].argop = OP_PUTFH; |
1095 | |
|
1096 | 0 | pfargs = &op[0].nfs_argop4_u.opputfh; |
1097 | 0 | pfargs->object.nfs_fh4_len = nfsfh->fh.len; |
1098 | 0 | pfargs->object.nfs_fh4_val = nfsfh->fh.val; |
1099 | |
|
1100 | 0 | return 1; |
1101 | 0 | } |
1102 | | |
1103 | | static int |
1104 | | nfs4_op_lock(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
1105 | | nfs_opnum4 cmd, nfs_lock_type4 locktype, |
1106 | | int reclaim, uint64_t offset, length4 length) |
1107 | 0 | { |
1108 | 0 | LOCK4args *largs; |
1109 | 0 | op[0].argop = cmd; |
1110 | |
|
1111 | 0 | largs = &op[0].nfs_argop4_u.oplock; |
1112 | 0 | largs->locktype = locktype; |
1113 | 0 | largs->reclaim = reclaim; |
1114 | 0 | largs->offset = offset; |
1115 | 0 | largs->length = length; |
1116 | |
|
1117 | 0 | if (nfs->nfsi->has_lock_owner) { |
1118 | 0 | largs->locker.new_lock_owner = 0; |
1119 | 0 | largs->locker.locker4_u.lock_owner.lock_stateid.seqid = |
1120 | 0 | fh->lock_stateid.seqid; |
1121 | 0 | memcpy(largs->locker.locker4_u.lock_owner.lock_stateid.other, |
1122 | 0 | fh->lock_stateid.other, 12); |
1123 | 0 | largs->locker.locker4_u.lock_owner.lock_seqid = |
1124 | 0 | fh->lock_seqid; |
1125 | 0 | } else { |
1126 | 0 | largs->locker.new_lock_owner = 1; |
1127 | 0 | largs->locker.locker4_u.open_owner.open_seqid = |
1128 | 0 | fh->open_seqid; |
1129 | 0 | largs->locker.locker4_u.open_owner.open_stateid.seqid = |
1130 | 0 | fh->stateid.seqid; |
1131 | 0 | memcpy(largs->locker.locker4_u.open_owner.open_stateid.other, |
1132 | 0 | fh->stateid.other, 12); |
1133 | 0 | largs->locker.locker4_u.open_owner.lock_owner.clientid = |
1134 | 0 | nfs->nfsi->clientid; |
1135 | 0 | largs->locker.locker4_u.open_owner.lock_owner.owner.owner_len = |
1136 | 0 | strlen(nfs->nfsi->client_name); |
1137 | 0 | largs->locker.locker4_u.open_owner.lock_owner.owner.owner_val = |
1138 | 0 | nfs->nfsi->client_name; |
1139 | 0 | largs->locker.locker4_u.open_owner.lock_seqid = |
1140 | 0 | fh->lock_seqid; |
1141 | 0 | } |
1142 | 0 | fh->lock_seqid++; |
1143 | |
|
1144 | 0 | return 1; |
1145 | 0 | } |
1146 | | |
1147 | | static int |
1148 | | nfs4_op_locku(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
1149 | | nfs_lock_type4 locktype, uint64_t offset, length4 length) |
1150 | 0 | { |
1151 | 0 | LOCKU4args *luargs; |
1152 | 0 | op[0].argop = OP_LOCKU; |
1153 | |
|
1154 | 0 | luargs = &op[0].nfs_argop4_u.oplocku; |
1155 | 0 | luargs->locktype = locktype; |
1156 | 0 | luargs->offset = offset; |
1157 | 0 | luargs->length = length; |
1158 | |
|
1159 | 0 | luargs->seqid = fh->lock_seqid; |
1160 | 0 | luargs->lock_stateid.seqid = fh->lock_stateid.seqid; |
1161 | 0 | memcpy(luargs->lock_stateid.other, fh->lock_stateid.other, 12); |
1162 | |
|
1163 | 0 | fh->lock_seqid++; |
1164 | |
|
1165 | 0 | return 1; |
1166 | 0 | } |
1167 | | |
1168 | | static int |
1169 | | nfs4_op_lockt(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh, |
1170 | | nfs_lock_type4 locktype, uint64_t offset, length4 length) |
1171 | 0 | { |
1172 | 0 | LOCKT4args *ltargs; |
1173 | 0 | op[0].argop = OP_LOCKT; |
1174 | |
|
1175 | 0 | ltargs = &op[0].nfs_argop4_u.oplockt; |
1176 | 0 | ltargs->locktype = locktype; |
1177 | 0 | ltargs->offset = offset; |
1178 | 0 | ltargs->length = length; |
1179 | |
|
1180 | 0 | ltargs->owner.clientid = nfs->nfsi->clientid; |
1181 | 0 | ltargs->owner.owner.owner_len = strlen(nfs->nfsi->client_name); |
1182 | 0 | ltargs->owner.owner.owner_val = nfs->nfsi->client_name; |
1183 | |
|
1184 | 0 | return 1; |
1185 | 0 | } |
1186 | | |
1187 | | static int |
1188 | | nfs4_op_lookup(struct nfs_context *nfs, nfs_argop4 *op, const char *path) |
1189 | 0 | { |
1190 | 0 | LOOKUP4args *largs; |
1191 | |
|
1192 | 0 | op[0].argop = OP_LOOKUP; |
1193 | 0 | largs = &op[0].nfs_argop4_u.oplookup; |
1194 | 0 | largs->objname.utf8string_len = strlen(path); |
1195 | 0 | largs->objname.utf8string_val = discard_const(path); |
1196 | |
|
1197 | 0 | return 1; |
1198 | 0 | } |
1199 | | |
1200 | | static int |
1201 | | nfs40_op_setclientid_confirm(struct nfs_context *nfs, struct nfs_argop4 *op, |
1202 | | uint64_t clientid, verifier4 verifier) |
1203 | 0 | { |
1204 | 0 | SETCLIENTID_CONFIRM4args *scidcargs; |
1205 | |
|
1206 | 0 | op[0].argop = OP_SETCLIENTID_CONFIRM; |
1207 | 0 | scidcargs = &op[0].nfs_argop4_u.opsetclientid_confirm; |
1208 | 0 | scidcargs->clientid = clientid; |
1209 | 0 | memcpy(scidcargs->setclientid_confirm, verifier, NFS4_VERIFIER_SIZE); |
1210 | |
|
1211 | 0 | return 1; |
1212 | 0 | } |
1213 | | |
1214 | | static int |
1215 | | nfs4_op_putrootfh(struct nfs_context *nfs, nfs_argop4 *op) |
1216 | 0 | { |
1217 | 0 | op[0].argop = OP_PUTROOTFH; |
1218 | |
|
1219 | 0 | return 1; |
1220 | 0 | } |
1221 | | |
1222 | | static int |
1223 | | nfs4_op_readlink(struct nfs_context *nfs, nfs_argop4 *op) |
1224 | 0 | { |
1225 | 0 | op[0].argop = OP_READLINK; |
1226 | |
|
1227 | 0 | return 1; |
1228 | 0 | } |
1229 | | |
1230 | | static int |
1231 | | nfs4_op_remove(struct nfs_context *nfs, nfs_argop4 *op, const char *name) |
1232 | 0 | { |
1233 | 0 | REMOVE4args *rmargs; |
1234 | |
|
1235 | 0 | op[0].argop = OP_REMOVE; |
1236 | 0 | rmargs = &op[0].nfs_argop4_u.opremove; |
1237 | 0 | memset(rmargs, 0, sizeof(*rmargs)); |
1238 | 0 | rmargs->target.utf8string_len = strlen(name); |
1239 | 0 | rmargs->target.utf8string_val = discard_const(name); |
1240 | |
|
1241 | 0 | return 1; |
1242 | 0 | } |
1243 | | |
1244 | | static int |
1245 | | nfs4_op_getattr(struct nfs_context *nfs, nfs_argop4 *op, |
1246 | | uint32_t *attributes, int count) |
1247 | 0 | { |
1248 | 0 | GETATTR4args *gaargs; |
1249 | |
|
1250 | 0 | op[0].argop = OP_GETATTR; |
1251 | 0 | gaargs = &op[0].nfs_argop4_u.opgetattr; |
1252 | 0 | memset(gaargs, 0, sizeof(*gaargs)); |
1253 | |
|
1254 | 0 | gaargs->attr_request.bitmap4_val = attributes; |
1255 | 0 | gaargs->attr_request.bitmap4_len = count; |
1256 | |
|
1257 | 0 | return 1; |
1258 | 0 | } |
1259 | | |
1260 | | /* |
1261 | | * Allocate op and populate the path components. |
1262 | | * Will mutate path. |
1263 | | * |
1264 | | * Returns: |
1265 | | * -1 : On error. |
1266 | | * <idx> : On success. Idx represents the next free index in op. |
1267 | | * Caller must free op. |
1268 | | */ |
1269 | | #ifdef HAVE_NFS4_2 |
1270 | | /* |
1271 | | * NFSv4.2 session establishment. |
1272 | | * |
1273 | | * From minor version 1 onwards a client does not SETCLIENTID. It presents a |
1274 | | * client owner to EXCHANGE_ID to get a clientid, turns that into a session |
1275 | | * with CREATE_SESSION, and tells the server it has no state to reclaim with |
1276 | | * RECLAIM_COMPLETE. Only then can it send ordinary COMPOUNDs, each of which |
1277 | | * must lead with SEQUENCE. |
1278 | | */ |
1279 | | static int |
1280 | | nfs42_op_exchange_id(struct nfs_context *nfs, nfs_argop4 *op) |
1281 | 0 | { |
1282 | 0 | EXCHANGE_ID4args *eiargs; |
1283 | |
|
1284 | 0 | op[0].argop = OP_EXCHANGE_ID; |
1285 | 0 | eiargs = &op[0].nfs_argop4_u.opexchangeid; |
1286 | |
|
1287 | 0 | memcpy(eiargs->eia_clientowner.co_verifier, nfs->nfsi->verifier, |
1288 | 0 | sizeof(verifier4)); |
1289 | 0 | eiargs->eia_clientowner.co_ownerid.co_ownerid_len = |
1290 | 0 | strlen(nfs->nfsi->client_name); |
1291 | 0 | eiargs->eia_clientowner.co_ownerid.co_ownerid_val = |
1292 | 0 | nfs->nfsi->client_name; |
1293 | | |
1294 | | /* |
1295 | | * We are not a pNFS client and we do not do state protection, so the |
1296 | | * flags are empty and spa_how is SP4_NONE. |
1297 | | */ |
1298 | 0 | eiargs->eia_flags = 0; |
1299 | 0 | eiargs->eia_state_protect.spa_how = SP4_NONE; |
1300 | 0 | eiargs->eia_client_impl_id.eia_client_impl_id_len = 0; |
1301 | 0 | eiargs->eia_client_impl_id.eia_client_impl_id_val = NULL; |
1302 | |
|
1303 | 0 | return 1; |
1304 | 0 | } |
1305 | | |
1306 | | static int |
1307 | | nfs42_op_create_session(struct nfs_context *nfs, nfs_argop4 *op, |
1308 | | clientid4 clientid, sequenceid4 seqid) |
1309 | 0 | { |
1310 | 0 | CREATE_SESSION4args *csargs; |
1311 | |
|
1312 | 0 | op[0].argop = OP_CREATE_SESSION; |
1313 | 0 | csargs = &op[0].nfs_argop4_u.opcreatesession; |
1314 | |
|
1315 | 0 | csargs->csa_clientid = clientid; |
1316 | 0 | csargs->csa_sequence = seqid; |
1317 | 0 | csargs->csa_flags = 0; |
1318 | | |
1319 | | /* |
1320 | | * ca_maxrequests is the size of the slot table, which is how many |
1321 | | * COMPOUNDs may be in flight at once. Ask for the most we are willing |
1322 | | * to track; the server may grant fewer and we use what it grants. |
1323 | | */ |
1324 | 0 | csargs->csa_fore_chan_attrs.ca_headerpadsize = 0; |
1325 | | /* |
1326 | | * These bound a single RPC on the wire, so they are the record limits |
1327 | | * rather than NFS_MAX_XFER_SIZE, which is the ceiling on a WRITE |
1328 | | * payload assembled from the caller's own buffers. |
1329 | | */ |
1330 | 0 | csargs->csa_fore_chan_attrs.ca_maxrequestsize = MAX_FRAGMENT_SIZE; |
1331 | 0 | csargs->csa_fore_chan_attrs.ca_maxresponsesize = NFS_MAX_RECV_XFER_SIZE; |
1332 | 0 | csargs->csa_fore_chan_attrs.ca_maxresponsesize_cached = 0; |
1333 | 0 | csargs->csa_fore_chan_attrs.ca_maxoperations = 64; |
1334 | 0 | csargs->csa_fore_chan_attrs.ca_maxrequests = NFS4_MAX_SLOTS; |
1335 | 0 | csargs->csa_fore_chan_attrs.ca_rdma_ird.ca_rdma_ird_len = 0; |
1336 | 0 | csargs->csa_fore_chan_attrs.ca_rdma_ird.ca_rdma_ird_val = NULL; |
1337 | | |
1338 | | /* |
1339 | | * The server validates the back channel sizes even when we run no |
1340 | | * back channel, so they have to be plausible rather than zero. |
1341 | | */ |
1342 | 0 | csargs->csa_back_chan_attrs = csargs->csa_fore_chan_attrs; |
1343 | |
|
1344 | 0 | csargs->csa_cb_program = 0; |
1345 | 0 | csargs->csa_sec_parms.csa_sec_parms_len = 0; |
1346 | 0 | csargs->csa_sec_parms.csa_sec_parms_val = NULL; |
1347 | |
|
1348 | 0 | if (nfs->rpc->nfs4_cb_fn && !rpc_is_rdma(nfs->rpc)) { |
1349 | 0 | static callback_sec_parms4 none = { AUTH_NONE, { { 0 } } }; |
1350 | |
|
1351 | 0 | csargs->csa_flags = CREATE_SESSION4_FLAG_CONN_BACK_CHAN; |
1352 | 0 | csargs->csa_back_chan_attrs.ca_maxrequests = 1; |
1353 | 0 | csargs->csa_back_chan_attrs.ca_maxoperations = 8; |
1354 | 0 | csargs->csa_back_chan_attrs.ca_maxresponsesize = 16384; |
1355 | 0 | csargs->csa_back_chan_attrs.ca_maxresponsesize_cached = 16384; |
1356 | 0 | csargs->csa_cb_program = NFS4_CALLBACK; |
1357 | 0 | csargs->csa_sec_parms.csa_sec_parms_len = 1; |
1358 | 0 | csargs->csa_sec_parms.csa_sec_parms_val = &none; |
1359 | 0 | } |
1360 | |
|
1361 | 0 | return 1; |
1362 | 0 | } |
1363 | | |
1364 | | static int |
1365 | | nfs42_op_reclaim_complete(struct nfs_context *nfs _U_, nfs_argop4 *op) |
1366 | 0 | { |
1367 | 0 | op[0].argop = OP_RECLAIM_COMPLETE; |
1368 | | /* Nothing to reclaim, and this is about the whole client, not one fs */ |
1369 | 0 | op[0].nfs_argop4_u.opreclaimcomplete.rca_one_fs = 0; |
1370 | |
|
1371 | 0 | return 1; |
1372 | 0 | } |
1373 | | |
1374 | | static int |
1375 | | nfs42_op_allocate(struct nfs_context *nfs _U_, nfs_argop4 *op, |
1376 | | struct nfsfh *fh, uint64_t offset, uint64_t length) |
1377 | 0 | { |
1378 | 0 | ALLOCATE4args *aaargs; |
1379 | |
|
1380 | 0 | op[0].argop = OP_ALLOCATE; |
1381 | 0 | aaargs = &op[0].nfs_argop4_u.opallocate; |
1382 | |
|
1383 | 0 | aaargs->aa_stateid.seqid = fh->stateid.seqid; |
1384 | 0 | memcpy(aaargs->aa_stateid.other, fh->stateid.other, 12); |
1385 | 0 | aaargs->aa_offset = offset; |
1386 | 0 | aaargs->aa_length = length; |
1387 | |
|
1388 | 0 | return 1; |
1389 | 0 | } |
1390 | | |
1391 | | static int |
1392 | | nfs42_op_deallocate(struct nfs_context *nfs _U_, nfs_argop4 *op, |
1393 | | struct nfsfh *fh, uint64_t offset, uint64_t length) |
1394 | 0 | { |
1395 | 0 | DEALLOCATE4args *daargs; |
1396 | |
|
1397 | 0 | op[0].argop = OP_DEALLOCATE; |
1398 | 0 | daargs = &op[0].nfs_argop4_u.opdeallocate; |
1399 | |
|
1400 | 0 | daargs->da_stateid.seqid = fh->stateid.seqid; |
1401 | 0 | memcpy(daargs->da_stateid.other, fh->stateid.other, 12); |
1402 | 0 | daargs->da_offset = offset; |
1403 | 0 | daargs->da_length = length; |
1404 | |
|
1405 | 0 | return 1; |
1406 | 0 | } |
1407 | | |
1408 | | static int |
1409 | | nfs42_op_read_plus(struct nfs_context *nfs _U_, nfs_argop4 *op, |
1410 | | struct nfsfh *fh, uint64_t offset, uint32_t count) |
1411 | 0 | { |
1412 | 0 | READ_PLUS4args *rpargs; |
1413 | |
|
1414 | 0 | op[0].argop = OP_READ_PLUS; |
1415 | 0 | rpargs = &op[0].nfs_argop4_u.opreadplus; |
1416 | |
|
1417 | 0 | rpargs->rpa_stateid.seqid = fh->stateid.seqid; |
1418 | 0 | memcpy(rpargs->rpa_stateid.other, fh->stateid.other, 12); |
1419 | 0 | rpargs->rpa_offset = offset; |
1420 | 0 | rpargs->rpa_count = count; |
1421 | |
|
1422 | 0 | return 1; |
1423 | 0 | } |
1424 | | |
1425 | | static int |
1426 | | nfs42_op_write_same(struct nfs_context *nfs _U_, nfs_argop4 *op, |
1427 | | struct nfsfh *fh, app_data_block4 *adb, int stable) |
1428 | 0 | { |
1429 | 0 | WRITE_SAME4args *wsargs; |
1430 | |
|
1431 | 0 | op[0].argop = OP_WRITE_SAME; |
1432 | 0 | wsargs = &op[0].nfs_argop4_u.opwritesame; |
1433 | |
|
1434 | 0 | wsargs->wsa_stateid.seqid = fh->stateid.seqid; |
1435 | 0 | memcpy(wsargs->wsa_stateid.other, fh->stateid.other, 12); |
1436 | 0 | wsargs->wsa_stable = stable ? FILE_SYNC4 : UNSTABLE4; |
1437 | 0 | wsargs->wsa_adb = *adb; |
1438 | |
|
1439 | 0 | return 1; |
1440 | 0 | } |
1441 | | |
1442 | | static int |
1443 | | nfs42_op_copy(struct nfs_context *nfs _U_, nfs_argop4 *op, |
1444 | | struct nfsfh *src, uint64_t src_offset, |
1445 | | struct nfsfh *dst, uint64_t dst_offset, |
1446 | | uint64_t count, int synchronous, COPY_NOTIFY4resok *notify) |
1447 | 0 | { |
1448 | 0 | COPY4args *cargs; |
1449 | |
|
1450 | 0 | op[0].argop = OP_COPY; |
1451 | 0 | cargs = &op[0].nfs_argop4_u.opcopy; |
1452 | |
|
1453 | 0 | if (notify) { |
1454 | 0 | cargs->ca_src_stateid = notify->cnr_stateid; |
1455 | 0 | cargs->ca_source_server.ca_source_server_len = |
1456 | 0 | notify->cnr_source_server.cnr_source_server_len; |
1457 | 0 | cargs->ca_source_server.ca_source_server_val = |
1458 | 0 | notify->cnr_source_server.cnr_source_server_val; |
1459 | 0 | } else { |
1460 | 0 | cargs->ca_src_stateid.seqid = src->stateid.seqid; |
1461 | 0 | memcpy(cargs->ca_src_stateid.other, src->stateid.other, 12); |
1462 | 0 | } |
1463 | 0 | cargs->ca_dst_stateid.seqid = dst->stateid.seqid; |
1464 | 0 | memcpy(cargs->ca_dst_stateid.other, dst->stateid.other, 12); |
1465 | 0 | cargs->ca_src_offset = src_offset; |
1466 | 0 | cargs->ca_dst_offset = dst_offset; |
1467 | 0 | cargs->ca_count = count; |
1468 | 0 | cargs->ca_consecutive = 1; |
1469 | 0 | cargs->ca_synchronous = synchronous ? 1 : 0; |
1470 | |
|
1471 | 0 | return 1; |
1472 | 0 | } |
1473 | | |
1474 | | static int |
1475 | | nfs42_op_copy_notify(struct nfs_context *nfs _U_, nfs_argop4 *op, |
1476 | | struct nfsfh *src, netloc4 *destination) |
1477 | 0 | { |
1478 | 0 | COPY_NOTIFY4args *cnargs; |
1479 | |
|
1480 | 0 | op[0].argop = OP_COPY_NOTIFY; |
1481 | 0 | cnargs = &op[0].nfs_argop4_u.opcopynotify; |
1482 | |
|
1483 | 0 | cnargs->cna_src_stateid.seqid = src->stateid.seqid; |
1484 | 0 | memcpy(cnargs->cna_src_stateid.other, src->stateid.other, 12); |
1485 | 0 | cnargs->cna_destination_server = *destination; |
1486 | |
|
1487 | 0 | return 1; |
1488 | 0 | } |
1489 | | |
1490 | | static int |
1491 | | nfs42_op_offload(struct nfs_context *nfs _U_, nfs_argop4 *op, |
1492 | | nfs_opnum4 opnum, const stateid4 *stateid) |
1493 | 0 | { |
1494 | 0 | op[0].argop = opnum; |
1495 | 0 | if (opnum == OP_OFFLOAD_STATUS) { |
1496 | 0 | op[0].nfs_argop4_u.opoffloadstatus.osa_stateid = *stateid; |
1497 | 0 | } else { |
1498 | 0 | op[0].nfs_argop4_u.opoffloadcancel.oca_stateid = *stateid; |
1499 | 0 | } |
1500 | |
|
1501 | 0 | return 1; |
1502 | 0 | } |
1503 | | |
1504 | | static int |
1505 | | nfs42_op_seek(struct nfs_context *nfs _U_, nfs_argop4 *op, struct nfsfh *fh, |
1506 | | uint64_t offset, data_content4 what) |
1507 | 0 | { |
1508 | 0 | SEEK4args *sargs; |
1509 | |
|
1510 | 0 | op[0].argop = OP_SEEK; |
1511 | 0 | sargs = &op[0].nfs_argop4_u.opseek; |
1512 | |
|
1513 | 0 | sargs->sa_stateid.seqid = fh->stateid.seqid; |
1514 | 0 | memcpy(sargs->sa_stateid.other, fh->stateid.other, 12); |
1515 | 0 | sargs->sa_offset = offset; |
1516 | 0 | sargs->sa_what = what; |
1517 | |
|
1518 | 0 | return 1; |
1519 | 0 | } |
1520 | | |
1521 | | /* |
1522 | | * Reserve the leading SEQUENCE. Which slot it uses and what sequence id goes |
1523 | | * in it are decided later, by nfs4_pdu_take_slot() in nfs4/nfs4.c, as the |
1524 | | * COMPOUND is written to the socket. A slot is held for as long as its |
1525 | | * request is outstanding, so binding one here would cap how many requests an |
1526 | | * application may have queued at the number of slots the session has. |
1527 | | */ |
1528 | | static int |
1529 | | nfs42_op_sequence(struct nfs_context *nfs _U_, nfs_argop4 *op) |
1530 | 0 | { |
1531 | 0 | op[0].argop = OP_SEQUENCE; |
1532 | 0 | memset(&op[0].nfs_argop4_u.opsequence, 0, sizeof(SEQUENCE4args)); |
1533 | |
|
1534 | 0 | return 1; |
1535 | 0 | } |
1536 | | #endif /* HAVE_NFS4_2 */ |
1537 | | |
1538 | | /* |
1539 | | * Open a COMPOUND. |
1540 | | * |
1541 | | * From minor version 1 onwards every COMPOUND must lead with SEQUENCE, and |
1542 | | * NFSv4.0 has no such operation. Callers begin their op array with this and |
1543 | | * use the returned count as their starting index, so the difference lives |
1544 | | * here instead of at each of the two dozen places that build a COMPOUND. |
1545 | | * |
1546 | | * The bootstrap COMPOUNDs are the exception and deliberately do not call |
1547 | | * this: SETCLIENTID and SETCLIENTID_CONFIRM in 4.0, and EXCHANGE_ID and |
1548 | | * CREATE_SESSION in 4.2, all run before there is a session to sequence |
1549 | | * against. |
1550 | | */ |
1551 | | static int |
1552 | | nfs4_start_compound(struct nfs_context *nfs, nfs_argop4 *op) |
1553 | 0 | { |
1554 | 0 | #ifdef HAVE_NFS4_2 |
1555 | | /* |
1556 | | * Only once the session exists. The COMPOUNDs that build it run |
1557 | | * before there is anything to sequence against. |
1558 | | */ |
1559 | 0 | if (nfs_has_sessions(nfs) && nfs->rpc->nfs4_session_valid) { |
1560 | 0 | return nfs42_op_sequence(nfs, op); |
1561 | 0 | } |
1562 | 0 | #endif /* HAVE_NFS4_2 */ |
1563 | 0 | (void)nfs; |
1564 | 0 | (void)op; |
1565 | 0 | return 0; |
1566 | 0 | } |
1567 | | |
1568 | | static int |
1569 | | nfs4_allocate_op(struct nfs_context *nfs, nfs_argop4 **op, |
1570 | | char *path, int num_extra) |
1571 | 0 | { |
1572 | 0 | char *ptr; |
1573 | 0 | int i, count; |
1574 | |
|
1575 | 0 | *op = NULL; |
1576 | |
|
1577 | 0 | count = nfs4_num_path_components(nfs, path); |
1578 | | |
1579 | | /* |
1580 | | * The array holds a leading SEQUENCE for NFSv4.2, the PUTFH or |
1581 | | * PUTROOTFH, one LOOKUP per path component, the trailing GETATTR, and |
1582 | | * whatever the caller said it would add on top. That is exact rather |
1583 | | * than generous: NFSv4.0 leaves one entry spare where 4.2 puts its |
1584 | | * SEQUENCE, so a caller that adds more than num_extra operations |
1585 | | * overruns this. |
1586 | | */ |
1587 | 0 | *op = malloc(sizeof(**op) * (3 + count + num_extra)); |
1588 | 0 | if (*op == NULL) { |
1589 | 0 | nfs_set_error(nfs, "Failed to allocate op array"); |
1590 | 0 | return -1; |
1591 | 0 | } |
1592 | | |
1593 | 0 | i = nfs4_start_compound(nfs, *op); |
1594 | 0 | if (nfs->nfsi->rootfh.len) { |
1595 | 0 | struct nfsfh fh; |
1596 | |
|
1597 | 0 | fh.fh.len = nfs->nfsi->rootfh.len; |
1598 | 0 | fh.fh.val = nfs->nfsi->rootfh.val; |
1599 | 0 | i += nfs4_op_putfh(nfs, &(*op)[i], &fh); |
1600 | 0 | } else { |
1601 | 0 | i += nfs4_op_putrootfh(nfs, &(*op)[i]); |
1602 | 0 | } |
1603 | |
|
1604 | 0 | ptr = &path[1]; |
1605 | 0 | while (ptr && *ptr != 0) { |
1606 | 0 | char *tmp; |
1607 | |
|
1608 | 0 | tmp = strchr(ptr, '/'); |
1609 | 0 | if (tmp) { |
1610 | 0 | *tmp = 0; |
1611 | 0 | tmp = tmp + 1; |
1612 | 0 | } |
1613 | 0 | i += nfs4_op_lookup(nfs, &(*op)[i], ptr); |
1614 | |
|
1615 | 0 | ptr = tmp; |
1616 | 0 | } |
1617 | |
|
1618 | 0 | i += nfs4_op_getattr(nfs, &(*op)[i], standard_attributes, 2); |
1619 | |
|
1620 | 0 | return i; |
1621 | 0 | } |
1622 | | |
1623 | | static int |
1624 | | nfs4_lookup_path_async(struct nfs_context *nfs, |
1625 | | struct nfs4_cb_data *data, |
1626 | | rpc_cb cb); |
1627 | | |
1628 | | /* Fails and completes data once a lookup has followed too many symlinks */ |
1629 | | static int |
1630 | | nfs4_follow_link(struct nfs_context *nfs, struct nfs4_cb_data *data) |
1631 | 0 | { |
1632 | 0 | if (data->link.count++ < MAX_LINK_COUNT) { |
1633 | 0 | return 0; |
1634 | 0 | } |
1635 | 0 | nfs_set_error(nfs, "Too many levels of symbolic links"); |
1636 | 0 | data->cb(-ELOOP, nfs, nfs_get_error(nfs), data->private_data); |
1637 | 0 | free_nfs4_cb_data(data); |
1638 | 0 | return -1; |
1639 | 0 | } |
1640 | | |
1641 | | static void |
1642 | | nfs4_lookup_path_2_cb(struct rpc_context *rpc, int status, void *command_data, |
1643 | | void *private_data) |
1644 | 0 | { |
1645 | 0 | struct nfs4_cb_data *data = private_data; |
1646 | 0 | struct nfs_context *nfs = data->nfs; |
1647 | 0 | COMPOUND4res *res = command_data; |
1648 | 0 | READLINK4res *rlres = NULL; |
1649 | 0 | char *path, *tmp, *end; |
1650 | 0 | const char *link_val; |
1651 | 0 | uint32_t link_len; |
1652 | 0 | int i; |
1653 | |
|
1654 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1655 | |
|
1656 | 0 | if (check_nfs4_error(nfs, status, data, res, "READLINK")) { |
1657 | 0 | return; |
1658 | 0 | } |
1659 | 0 | if (nfs4_follow_link(nfs, data) < 0) { |
1660 | 0 | return; |
1661 | 0 | } |
1662 | | |
1663 | 0 | path = strdup(data->path); |
1664 | 0 | if (path == NULL) { |
1665 | 0 | nfs_set_error(nfs, "Out of memory duplicating path."); |
1666 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
1667 | 0 | data->private_data); |
1668 | 0 | free_nfs4_cb_data(data); |
1669 | 0 | return; |
1670 | 0 | } |
1671 | | |
1672 | 0 | tmp = &path[0]; |
1673 | 0 | while (data->link.idx-- > 1) { |
1674 | 0 | tmp = strchr(tmp + 1, '/'); |
1675 | | /* |
1676 | | * link.idx is counted from the server's result array, so it |
1677 | | * can name more components than the path actually has. |
1678 | | */ |
1679 | 0 | if (tmp == NULL) { |
1680 | 0 | nfs_set_error(nfs, "Symlink index does not match path."); |
1681 | 0 | data->cb(-EFAULT, nfs, nfs_get_error(nfs), |
1682 | 0 | data->private_data); |
1683 | 0 | free_nfs4_cb_data(data); |
1684 | 0 | free(path); |
1685 | 0 | return; |
1686 | 0 | } |
1687 | 0 | } |
1688 | 0 | *tmp++ = 0; |
1689 | 0 | end = strchr(tmp, '/'); |
1690 | 0 | if (end == NULL) { |
1691 | | /* Symlink was the last component. */ |
1692 | 0 | end = ""; |
1693 | 0 | } else { |
1694 | 0 | *end++ = 0; |
1695 | 0 | } |
1696 | |
|
1697 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_READLINK, "READLINK")) < 0) { |
1698 | 0 | free(path); |
1699 | 0 | return; |
1700 | 0 | } |
1701 | 0 | rlres = &res->resarray.resarray_val[i].nfs_resop4_u.opreadlink; |
1702 | 0 | link_val = rlres->READLINK4res_u.resok4.link.utf8string_val; |
1703 | 0 | link_len = rlres->READLINK4res_u.resok4.link.utf8string_len; |
1704 | |
|
1705 | 0 | if (link_len > 0 && link_val[0] == '/') { |
1706 | 0 | int ret = nfs_absolute_link_path(nfs, link_val, link_len, |
1707 | 0 | end, &tmp); |
1708 | 0 | if (ret < 0) { |
1709 | 0 | data->cb(ret, nfs, nfs_get_error(nfs), |
1710 | 0 | data->private_data); |
1711 | 0 | free_nfs4_cb_data(data); |
1712 | 0 | free(path); |
1713 | 0 | return; |
1714 | 0 | } |
1715 | 0 | } else { |
1716 | 0 | tmp = malloc(strlen(data->path) + 3 + link_len); |
1717 | 0 | if (tmp == NULL) { |
1718 | 0 | nfs_set_error(nfs, "Out of memory duplicating path."); |
1719 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
1720 | 0 | data->private_data); |
1721 | 0 | free_nfs4_cb_data(data); |
1722 | 0 | free(path); |
1723 | 0 | return; |
1724 | 0 | } |
1725 | | |
1726 | 0 | sprintf(tmp, "%s/%.*s/%s", path, (int)link_len, link_val, |
1727 | 0 | end); |
1728 | 0 | } |
1729 | 0 | free(path); |
1730 | 0 | free(data->path); |
1731 | 0 | data->path = tmp; |
1732 | |
|
1733 | 0 | if (nfs4_lookup_path_async(nfs, data, data->continue_cb) < 0) { |
1734 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
1735 | 0 | free_nfs4_cb_data(data); |
1736 | 0 | return; |
1737 | 0 | } |
1738 | 0 | } |
1739 | | |
1740 | | static int |
1741 | | nfs4_open_readlink(struct rpc_context *rpc, COMPOUND4res *res, |
1742 | | struct nfs4_cb_data *data); |
1743 | | |
1744 | | static void |
1745 | | nfs4_lookup_path_1_cb(struct rpc_context *rpc, int status, void *command_data, |
1746 | | void *private_data) |
1747 | 0 | { |
1748 | 0 | struct nfs4_cb_data *data = private_data; |
1749 | 0 | struct nfs_context *nfs = data->nfs; |
1750 | 0 | COMPOUND4args args; |
1751 | 0 | nfs_argop4 *op; |
1752 | 0 | COMPOUND4res *res = command_data; |
1753 | 0 | int i; |
1754 | 0 | int resolve_link = 0; |
1755 | 0 | char *path, *tmp; |
1756 | |
|
1757 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
1758 | |
|
1759 | 0 | if (status == RPC_STATUS_ERROR) { |
1760 | 0 | data->cb(-EFAULT, nfs, res, data->private_data); |
1761 | 0 | free_nfs4_cb_data(data); |
1762 | 0 | return; |
1763 | 0 | } |
1764 | 0 | if (status == RPC_STATUS_CANCEL) { |
1765 | 0 | data->cb(-EINTR, nfs, "Command was cancelled", |
1766 | 0 | data->private_data); |
1767 | 0 | free_nfs4_cb_data(data); |
1768 | 0 | return; |
1769 | 0 | } |
1770 | 0 | if (status == RPC_STATUS_TIMEOUT) { |
1771 | 0 | data->cb(-EINTR, nfs, "Command timed out", |
1772 | 0 | data->private_data); |
1773 | 0 | free_nfs4_cb_data(data); |
1774 | 0 | return; |
1775 | 0 | } |
1776 | 0 | if (res->status != NFS4_OK && |
1777 | 0 | res->status != NFS4ERR_SYMLINK) { |
1778 | 0 | if (nfs->nfsi->version == NFS_V4 && |
1779 | 0 | (res->status == NFS4ERR_STALE_CLIENTID || |
1780 | 0 | res->status == NFS4ERR_EXPIRED)) |
1781 | 0 | nfs->rpc->nfs40_state_error = res->status; |
1782 | 0 | nfs_set_error(nfs, "NFS4: (path %s) failed with " |
1783 | 0 | "%s(%d)", |
1784 | 0 | data->path, |
1785 | 0 | nfsstat4_to_str(res->status), |
1786 | 0 | nfsstat4_to_errno(res->status)); |
1787 | 0 | data->cb(nfsstat4_to_errno(res->status), nfs, |
1788 | 0 | nfs_get_error(nfs), data->private_data); |
1789 | 0 | free_nfs4_cb_data(data); |
1790 | 0 | return; |
1791 | 0 | } |
1792 | | |
1793 | 0 | for (i = 0; i < (int)res->resarray.resarray_len; i++) { |
1794 | 0 | if (res->resarray.resarray_val[i].resop == OP_GETATTR) { |
1795 | 0 | GETATTR4resok *garesok; |
1796 | 0 | struct nfs_stat_64 st; |
1797 | |
|
1798 | 0 | garesok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetattr.GETATTR4res_u.resok4; |
1799 | |
|
1800 | 0 | memset(&st, 0, sizeof(st)); |
1801 | 0 | if (nfs_parse_attributes(nfs, data, &st, NULL, NULL, |
1802 | 0 | &garesok->obj_attributes.attrmask, |
1803 | 0 | garesok->obj_attributes.attr_vals.attrlist4_val, |
1804 | 0 | garesok->obj_attributes.attr_vals.attrlist4_len) < 0) { |
1805 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
1806 | 0 | free_nfs4_cb_data(data); |
1807 | 0 | return; |
1808 | 0 | } |
1809 | 0 | if ((st.nfs_mode & S_IFMT) == S_IFLNK) { |
1810 | | /* The final component of the path was a |
1811 | | * symlink so we may need to resolve it. |
1812 | | */ |
1813 | 0 | resolve_link = 1; |
1814 | 0 | } |
1815 | 0 | } |
1816 | 0 | } |
1817 | | |
1818 | | /* Open/create is special since the final component for the file |
1819 | | * object is sent as part of the OP_OPEN command. So even if the |
1820 | | * directory path is all good and resolved, we still need to check |
1821 | | * the attributes for the final component and resolve it if it too |
1822 | | * is a symlink. |
1823 | | */ |
1824 | 0 | if (!resolve_link) { |
1825 | 0 | if (nfs4_open_readlink(rpc, res, data) < 0) { |
1826 | | /* It was a symlink and we have started trying to |
1827 | | * resolve it. Nothing more to do here. |
1828 | | */ |
1829 | 0 | return; |
1830 | 0 | } |
1831 | 0 | } |
1832 | | |
1833 | 0 | if (data->flags & LOOKUP_FLAG_NO_FOLLOW) { |
1834 | | /* Do not resolve the final component of the path |
1835 | | * if it is a symlink. |
1836 | | */ |
1837 | 0 | resolve_link = 0; |
1838 | 0 | } |
1839 | | |
1840 | | /* Everything is good so we can just pass it on to the next |
1841 | | * phase. |
1842 | | */ |
1843 | 0 | if (res->status == NFS4_OK && !resolve_link) { |
1844 | 0 | data->continue_cb(rpc, NFS4_OK, res, data); |
1845 | 0 | return; |
1846 | 0 | } |
1847 | | |
1848 | | /* Find the lookup that failed and the associated fh */ |
1849 | 0 | data->link.idx = 0; |
1850 | 0 | for (i = 0; i < (int)res->resarray.resarray_len; i++) { |
1851 | 0 | if (res->resarray.resarray_val[i].resop == OP_LOOKUP) { |
1852 | 0 | if (res->resarray.resarray_val[i].nfs_resop4_u.oplookup.status == NFS4ERR_SYMLINK) { |
1853 | 0 | break; |
1854 | 0 | } |
1855 | 0 | data->link.idx++; |
1856 | 0 | } |
1857 | 0 | } |
1858 | |
|
1859 | 0 | if (!resolve_link && i == res->resarray.resarray_len) { |
1860 | 0 | nfs_set_error(nfs, "Symlink not found during lookup."); |
1861 | 0 | data->cb(-EFAULT, nfs, nfs_get_error(nfs), data->private_data); |
1862 | 0 | free_nfs4_cb_data(data); |
1863 | 0 | return; |
1864 | 0 | } |
1865 | | |
1866 | | /* Build a new path that strips of everything after the symlink. */ |
1867 | 0 | path = strdup(data->path); |
1868 | 0 | if (path == NULL) { |
1869 | 0 | nfs_set_error(nfs, "Out of memory. Failed to duplicate " |
1870 | 0 | "path."); |
1871 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
1872 | 0 | data->private_data); |
1873 | 0 | free_nfs4_cb_data(data); |
1874 | 0 | return; |
1875 | 0 | } |
1876 | | |
1877 | | /* The symlink is not the last component, so find the '/' before |
1878 | | * the symlink and zero it out. |
1879 | | */ |
1880 | 0 | if (!resolve_link) { |
1881 | | /* The root can not be a symlink */ |
1882 | 0 | if (data->link.idx == 0) { |
1883 | 0 | nfs_set_error(nfs, "Symlink index does not " |
1884 | 0 | "match path."); |
1885 | 0 | data->cb(-EFAULT, nfs, nfs_get_error(nfs), |
1886 | 0 | data->private_data); |
1887 | 0 | free_nfs4_cb_data(data); |
1888 | 0 | free(path); |
1889 | 0 | return; |
1890 | 0 | } |
1891 | 0 | tmp = path; |
1892 | 0 | for (i = 0; i < (int)data->link.idx; i++) { |
1893 | 0 | tmp = strchr(tmp + 1, '/'); |
1894 | | /* |
1895 | | * link.idx is counted from the server's result array, |
1896 | | * so it can name more components than the path has. |
1897 | | */ |
1898 | 0 | if (tmp == NULL) { |
1899 | 0 | nfs_set_error(nfs, "Symlink index does not " |
1900 | 0 | "match path."); |
1901 | 0 | data->cb(-EFAULT, nfs, nfs_get_error(nfs), |
1902 | 0 | data->private_data); |
1903 | 0 | free_nfs4_cb_data(data); |
1904 | 0 | free(path); |
1905 | 0 | return; |
1906 | 0 | } |
1907 | 0 | } |
1908 | 0 | *tmp = 0; |
1909 | 0 | } |
1910 | | |
1911 | | /* We need to resolve the symlink */ |
1912 | 0 | if ((i = nfs4_allocate_op(nfs, &op, path, 1)) < 0) { |
1913 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
1914 | 0 | free_nfs4_cb_data(data); |
1915 | 0 | free(path); |
1916 | 0 | return; |
1917 | 0 | } |
1918 | | |
1919 | | /* Append a READLINK command */ |
1920 | 0 | i += nfs4_op_readlink(nfs, &op[i]); |
1921 | |
|
1922 | 0 | memset(&args, 0, sizeof(args)); |
1923 | 0 | args.argarray.argarray_len = i; |
1924 | 0 | args.argarray.argarray_val = op; |
1925 | |
|
1926 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_lookup_path_2_cb, &args, |
1927 | 0 | data) == NULL) { |
1928 | 0 | nfs_set_error(nfs, "Failed to queue READLINK command. %s", |
1929 | 0 | nfs_get_error(nfs)); |
1930 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
1931 | 0 | free_nfs4_cb_data(data); |
1932 | 0 | free(path); |
1933 | 0 | free(op); |
1934 | 0 | return; |
1935 | 0 | } |
1936 | 0 | free(path); |
1937 | 0 | free(op); |
1938 | 0 | } |
1939 | | |
1940 | | static int |
1941 | | nfs4_lookup_path_async(struct nfs_context *nfs, |
1942 | | struct nfs4_cb_data *data, |
1943 | | rpc_cb cb) |
1944 | 0 | { |
1945 | 0 | COMPOUND4args args; |
1946 | 0 | nfs_argop4 *op; |
1947 | 0 | char *path; |
1948 | 0 | int i, num_op; |
1949 | |
|
1950 | 0 | path = nfs4_resolve_path(nfs, data->path); |
1951 | 0 | if (path == NULL) { |
1952 | 0 | return -1; |
1953 | 0 | } |
1954 | 0 | free(data->path); |
1955 | 0 | data->path = path; |
1956 | |
|
1957 | 0 | path = strdup(path); |
1958 | 0 | if (path == NULL) { |
1959 | 0 | return -1; |
1960 | 0 | } |
1961 | | |
1962 | 0 | if ((i = nfs4_allocate_op(nfs, &op, path, |
1963 | 0 | data->filler.max_op + |
1964 | 0 | ((data->flags & LOOKUP_FLAG_OPENATTR) ? |
1965 | 0 | 1 : 0))) < 0) { |
1966 | 0 | free(path); |
1967 | 0 | return -1; |
1968 | 0 | } |
1969 | | |
1970 | | /* |
1971 | | * Switch to the named attribute directory of whatever the path |
1972 | | * resolved to, so the operations the filler adds land there. |
1973 | | */ |
1974 | 0 | if (data->flags & LOOKUP_FLAG_OPENATTR) { |
1975 | 0 | i += nfs4_op_openattr(nfs, &op[i], |
1976 | 0 | !!(data->flags & |
1977 | 0 | LOOKUP_FLAG_OPENATTR_CREATE)); |
1978 | 0 | } |
1979 | |
|
1980 | 0 | num_op = data->filler.func(data, &op[i]); |
1981 | | /* |
1982 | | * nfs4_allocate_op() sized the array from max_op, so a filler that |
1983 | | * returns more than it promised has already written past the end of |
1984 | | * it. Catch that here rather than letting it corrupt the heap. |
1985 | | */ |
1986 | 0 | assert(num_op <= data->filler.max_op); |
1987 | 0 | data->continue_cb = cb; |
1988 | |
|
1989 | 0 | memset(&args, 0, sizeof(args)); |
1990 | 0 | args.argarray.argarray_len = i + num_op; |
1991 | 0 | args.argarray.argarray_val = op; |
1992 | |
|
1993 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_lookup_path_1_cb, &args, |
1994 | 0 | data) == NULL) { |
1995 | 0 | nfs_set_error(nfs, "Failed to queue LOOKUP command. %s", |
1996 | 0 | rpc_get_error(nfs->rpc)); |
1997 | 0 | free(path); |
1998 | 0 | free(op); |
1999 | 0 | return -1; |
2000 | 0 | } |
2001 | | |
2002 | 0 | free(path); |
2003 | 0 | free(op); |
2004 | 0 | return 0; |
2005 | 0 | } |
2006 | | |
2007 | | static int |
2008 | | nfs4_populate_getfh(struct nfs4_cb_data *data, nfs_argop4 *op) |
2009 | 0 | { |
2010 | 0 | return nfs4_op_getfh(data->nfs, op); |
2011 | 0 | } |
2012 | | |
2013 | | static int |
2014 | | nfs4_populate_getattr(struct nfs4_cb_data *data, nfs_argop4 *op) |
2015 | 0 | { |
2016 | 0 | return nfs4_op_getfh(data->nfs, op); |
2017 | 0 | } |
2018 | | |
2019 | | static int |
2020 | | nfs4_populate_access(struct nfs4_cb_data *data, nfs_argop4 *op) |
2021 | 0 | { |
2022 | 0 | uint32_t mode; |
2023 | |
|
2024 | 0 | memcpy(&mode, data->filler.blob3.val, sizeof(uint32_t)); |
2025 | |
|
2026 | 0 | return nfs4_op_access(data->nfs, op, mode); |
2027 | 0 | } |
2028 | | |
2029 | | |
2030 | | int |
2031 | | nfs4_parse_mount_rwmax(struct nfs_context *nfs, GETATTR4resok *attrs, |
2032 | | uint32_t *lease_seconds) |
2033 | 0 | { |
2034 | 0 | const char *buf = attrs->obj_attributes.attr_vals.attrlist4_val; |
2035 | 0 | int len = attrs->obj_attributes.attr_vals.attrlist4_len; |
2036 | 0 | uint32_t lease; |
2037 | |
|
2038 | 0 | if (buf == NULL) { |
2039 | 0 | nfs_set_error(nfs, "NFSv4 mount: missing transfer attributes"); |
2040 | 0 | return -1; |
2041 | 0 | } |
2042 | | |
2043 | 0 | if (nfs->nfsi->version == NFS_V4) { |
2044 | 0 | bitmap4 *mask = &attrs->obj_attributes.attrmask; |
2045 | 0 | uint32_t wire_lease; |
2046 | 0 | uint32_t w; |
2047 | |
|
2048 | 0 | if (mask->bitmap4_len < 1 || mask->bitmap4_val == NULL) { |
2049 | 0 | nfs_set_error(nfs, "NFSv4.0 mount: invalid lease_time attributes"); |
2050 | 0 | return -1; |
2051 | 0 | } |
2052 | | /* Trailing zero words are legal padding. */ |
2053 | 0 | for (w = 1; w < mask->bitmap4_len; w++) { |
2054 | 0 | if (mask->bitmap4_val[w]) { |
2055 | 0 | break; |
2056 | 0 | } |
2057 | 0 | } |
2058 | 0 | if (w < mask->bitmap4_len || |
2059 | 0 | mask->bitmap4_val[0] != rwmax_lease_attributes[0] || |
2060 | 0 | len != 20) { |
2061 | 0 | nfs_set_error(nfs, "NFSv4.0 mount: invalid lease_time attributes"); |
2062 | 0 | return -1; |
2063 | 0 | } |
2064 | 0 | memcpy(&wire_lease, buf, sizeof(wire_lease)); |
2065 | 0 | lease = ntohl(wire_lease); |
2066 | 0 | if (lease == 0) { |
2067 | 0 | nfs_set_error(nfs, "NFSv4.0 mount: zero lease_time"); |
2068 | 0 | return -1; |
2069 | 0 | } |
2070 | 0 | *lease_seconds = lease; |
2071 | 0 | buf += 4; |
2072 | 0 | len -= 4; |
2073 | 0 | } |
2074 | | /* READ MAX */ |
2075 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
2076 | 0 | nfs_set_readmax(nfs, nfs_pntoh64((uint32_t *)(void *)buf)); |
2077 | 0 | buf += 8; |
2078 | 0 | len -= 8; |
2079 | | /* WRITE MAX */ |
2080 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
2081 | 0 | nfs_set_writemax(nfs, nfs_pntoh64((uint32_t *)(void *)buf)); |
2082 | 0 | buf += 8; |
2083 | 0 | len -= 8; |
2084 | |
|
2085 | 0 | return 0; |
2086 | 0 | } |
2087 | | |
2088 | | static void |
2089 | | nfs4_mount_5_cb(struct rpc_context *rpc, int status, void *command_data, |
2090 | | void *private_data) |
2091 | 0 | { |
2092 | 0 | struct nfs4_cb_data *data = private_data; |
2093 | 0 | struct nfs_context *nfs = data->nfs; |
2094 | 0 | COMPOUND4res *res = command_data; |
2095 | 0 | GETATTR4resok *garesok; |
2096 | 0 | uint32_t lease_seconds = 0; |
2097 | 0 | int i; |
2098 | |
|
2099 | 0 | if (check_nfs4_error(nfs, status, data, res, "RWMAX")) { |
2100 | 0 | return; |
2101 | 0 | } |
2102 | | |
2103 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_GETATTR, "GETATTR")) < 0) { |
2104 | 0 | return; |
2105 | 0 | } |
2106 | 0 | garesok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetattr.GETATTR4res_u.resok4; |
2107 | 0 | if (nfs4_parse_mount_rwmax(nfs, garesok, &lease_seconds) < 0) { |
2108 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
2109 | 0 | free_nfs4_cb_data(data); |
2110 | 0 | return; |
2111 | 0 | } |
2112 | | |
2113 | | /* |
2114 | | * Now the entire mount process (including the NFS FSINFO and GETATTR) |
2115 | | * has completed. Any RPC failure till now would have caused the mount |
2116 | | * process to fail. Note that it's desirable for the mount process to |
2117 | | * fail upfront if it encounters any errors (TCP or RPC), rather than |
2118 | | * keep trying indefinitely causing the mount process to "hang", but |
2119 | | * from now on the RPC transport will be used to carry NFS RPCs issued |
2120 | | * by the application which may not be equipped to handle TCP connection |
2121 | | * failure and RPC timeouts, so we set the resiliency parameters of the |
2122 | | * rpc_context as selected by the user using the mount options. The |
2123 | | * default resiliency parameters emulate the common "hard" mount and |
2124 | | * we are resilient to any TCP or RPC connectivity issues. |
2125 | | */ |
2126 | 0 | rpc_set_resiliency(rpc, |
2127 | 0 | nfs->nfsi->auto_reconnect, |
2128 | 0 | nfs->nfsi->timeout, |
2129 | 0 | nfs->nfsi->retrans); |
2130 | 0 | if (nfs->nfsi->version == NFS_V4) { |
2131 | 0 | rpc_nfs40_start_renew(rpc, nfs->nfsi->clientid, lease_seconds); |
2132 | 0 | } |
2133 | | |
2134 | 0 | data->cb(0, nfs, NULL, data->private_data); |
2135 | 0 | free_nfs4_cb_data(data); |
2136 | 0 | } |
2137 | | |
2138 | | static void |
2139 | | nfs4_mount_4_cb(struct rpc_context *rpc, int status, void *command_data, |
2140 | | void *private_data) |
2141 | 0 | { |
2142 | 0 | struct nfs4_cb_data *data = private_data; |
2143 | 0 | struct nfs_context *nfs = data->nfs; |
2144 | 0 | COMPOUND4res *res = command_data; |
2145 | 0 | GETFH4resok *gfhresok; |
2146 | 0 | COMPOUND4args args; |
2147 | 0 | nfs_argop4 op[2 + 1]; |
2148 | 0 | struct nfsfh nfsfh; |
2149 | 0 | int i; |
2150 | |
|
2151 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2152 | |
|
2153 | 0 | if (check_nfs4_error(nfs, status, data, res, "GETFH")) { |
2154 | 0 | return; |
2155 | 0 | } |
2156 | | |
2157 | 0 | if ((gfhresok = nfs4_find_getfh(nfs, data, res)) == NULL) { |
2158 | 0 | return; |
2159 | 0 | } |
2160 | | |
2161 | 0 | nfs->nfsi->rootfh.len = gfhresok->object.nfs_fh4_len; |
2162 | 0 | nfs->nfsi->rootfh.val = malloc(nfs->nfsi->rootfh.len); |
2163 | 0 | if (nfs->nfsi->rootfh.val == NULL) { |
2164 | 0 | nfs_set_error(nfs, "%s: %s", __FUNCTION__, nfs_get_error(nfs)); |
2165 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2166 | 0 | free_nfs4_cb_data(data); |
2167 | 0 | return; |
2168 | 0 | } |
2169 | 0 | memcpy(nfs->nfsi->rootfh.val, |
2170 | 0 | gfhresok->object.nfs_fh4_val, |
2171 | 0 | nfs->nfsi->rootfh.len); |
2172 | |
|
2173 | 0 | memset(op, 0, sizeof(op)); |
2174 | 0 | nfsfh.fh = nfs->nfsi->rootfh; |
2175 | 0 | i = nfs4_start_compound(nfs, op); |
2176 | 0 | i += nfs4_op_putfh(nfs, &op[i], &nfsfh); |
2177 | 0 | i += nfs4_op_getattr(nfs, &op[i], |
2178 | 0 | nfs->nfsi->version == NFS_V4 ? |
2179 | 0 | rwmax_lease_attributes : rwmax_attributes, 1); |
2180 | | |
2181 | 0 | memset(&args, 0, sizeof(args)); |
2182 | 0 | args.argarray.argarray_len = i; |
2183 | 0 | args.argarray.argarray_val = op; |
2184 | |
|
2185 | 0 | if (rpc_nfs4_compound_task(rpc, nfs4_mount_5_cb, &args, |
2186 | 0 | private_data) == NULL) { |
2187 | 0 | nfs_set_error(nfs, "Failed to queue GETATTR rwmax. %s", |
2188 | 0 | rpc_get_error(rpc)); |
2189 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2190 | 0 | free_nfs4_cb_data(data); |
2191 | 0 | return; |
2192 | 0 | } |
2193 | 0 | } |
2194 | | |
2195 | | /* |
2196 | | * The bootstrap is done and the client is usable. Everything from here is the |
2197 | | * same whatever established the state, so both the 4.0 SETCLIENTID chain and |
2198 | | * the 4.2 session chain converge here to fetch the root filehandle. |
2199 | | */ |
2200 | | static void |
2201 | | nfs4_mount_start_getfh(struct nfs_context *nfs, struct nfs4_cb_data *data, |
2202 | | void *res) |
2203 | 0 | { |
2204 | 0 | data->filler.func = nfs4_populate_getfh; |
2205 | 0 | data->filler.max_op = 1; |
2206 | 0 | data->filler.data = calloc(2, sizeof(uint32_t)); |
2207 | 0 | if (data->filler.data == NULL) { |
2208 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
2209 | 0 | "data structure."); |
2210 | 0 | data->cb(-ENOMEM, nfs, res, data->private_data); |
2211 | 0 | free_nfs4_cb_data(data); |
2212 | 0 | return; |
2213 | 0 | } |
2214 | | |
2215 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_mount_4_cb) < 0) { |
2216 | 0 | data->cb(-ENOMEM, nfs, res, data->private_data); |
2217 | 0 | free_nfs4_cb_data(data); |
2218 | 0 | return; |
2219 | 0 | } |
2220 | 0 | } |
2221 | | |
2222 | | static void |
2223 | | nfs4_mount_3_cb(struct rpc_context *rpc, int status, void *command_data, |
2224 | | void *private_data) |
2225 | 0 | { |
2226 | 0 | struct nfs4_cb_data *data = private_data; |
2227 | 0 | struct nfs_context *nfs = data->nfs; |
2228 | 0 | COMPOUND4res *res = command_data; |
2229 | |
|
2230 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2231 | |
|
2232 | 0 | if (check_nfs4_error(nfs, status, data, res, "SETCLIENTID_CONFIRM")) { |
2233 | 0 | return; |
2234 | 0 | } |
2235 | | |
2236 | 0 | nfs4_mount_start_getfh(nfs, data, res); |
2237 | 0 | } |
2238 | | |
2239 | | static void |
2240 | | nfs4_mount_2_cb(struct rpc_context *rpc, int status, void *command_data, |
2241 | | void *private_data) |
2242 | 0 | { |
2243 | 0 | struct nfs4_cb_data *data = private_data; |
2244 | 0 | struct nfs_context *nfs = data->nfs; |
2245 | 0 | COMPOUND4res *res = command_data; |
2246 | 0 | COMPOUND4args args; |
2247 | 0 | nfs_argop4 op[1 + 1]; |
2248 | 0 | SETCLIENTID4resok *scidresok; |
2249 | 0 | int i; |
2250 | |
|
2251 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2252 | |
|
2253 | 0 | if (check_nfs4_error(nfs, status, data, res, "SETCLIENTID")) { |
2254 | 0 | return; |
2255 | 0 | } |
2256 | | |
2257 | | /* |
2258 | | * A reply can carry NFS4_OK with an empty resarray, and |
2259 | | * check_nfs4_error() only looks at the status, so indexing element 0 |
2260 | | * blind would read off the end of the decoded array. Locate the op |
2261 | | * the same way every other result in this file is located. |
2262 | | */ |
2263 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_SETCLIENTID, |
2264 | 0 | "SETCLIENTID")) < 0) { |
2265 | 0 | return; |
2266 | 0 | } |
2267 | | |
2268 | 0 | scidresok = &res->resarray.resarray_val[i].nfs_resop4_u.opsetclientid.SETCLIENTID4res_u.resok4; |
2269 | 0 | nfs->nfsi->clientid = scidresok->clientid; |
2270 | 0 | memcpy(nfs->nfsi->setclientid_confirm, |
2271 | 0 | scidresok->setclientid_confirm, |
2272 | 0 | NFS4_VERIFIER_SIZE); |
2273 | |
|
2274 | 0 | memset(op, 0, sizeof(op)); |
2275 | |
|
2276 | 0 | i = nfs40_op_setclientid_confirm(nfs, &op[0], nfs->nfsi->clientid, |
2277 | 0 | nfs->nfsi->setclientid_confirm); |
2278 | | |
2279 | 0 | memset(&args, 0, sizeof(args)); |
2280 | 0 | args.argarray.argarray_len = i; |
2281 | 0 | args.argarray.argarray_val = op; |
2282 | |
|
2283 | 0 | if (rpc_nfs4_compound_task(rpc, nfs4_mount_3_cb, &args, |
2284 | 0 | private_data) == NULL) { |
2285 | 0 | nfs_set_error(nfs, "Failed to queue SETCLIENTID_CONFIRM. %s", |
2286 | 0 | rpc_get_error(rpc)); |
2287 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2288 | 0 | free_nfs4_cb_data(data); |
2289 | 0 | return; |
2290 | 0 | } |
2291 | 0 | } |
2292 | | |
2293 | | static void |
2294 | | nfs4_mount_1_cb(struct rpc_context *rpc, int status, void *command_data, |
2295 | | void *private_data) |
2296 | 0 | { |
2297 | 0 | struct nfs4_cb_data *data = private_data; |
2298 | 0 | struct nfs_context *nfs = data->nfs; |
2299 | 0 | COMPOUND4args args; |
2300 | 0 | nfs_argop4 op[1 + 1]; |
2301 | 0 | int i, ret; |
2302 | |
|
2303 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2304 | |
|
2305 | 0 | if (check_nfs4_error(nfs, status, data, NULL, "CONNECT")) { |
2306 | 0 | return; |
2307 | 0 | } |
2308 | | |
2309 | 0 | ret = rpc_nfs40_save_renew_auth(rpc); |
2310 | 0 | if (ret < 0) { |
2311 | 0 | nfs_set_error(nfs, "Failed to save NFSv4.0 renewal " |
2312 | 0 | "credentials. %s", rpc_get_error(rpc)); |
2313 | 0 | data->cb(ret, nfs, nfs_get_error(nfs), data->private_data); |
2314 | 0 | free_nfs4_cb_data(data); |
2315 | 0 | return; |
2316 | 0 | } |
2317 | | |
2318 | 0 | memset(op, 0, sizeof(op)); |
2319 | |
|
2320 | 0 | i = nfs40_op_setclientid(nfs, &op[0], nfs->nfsi->verifier, nfs->nfsi->client_name); |
2321 | | |
2322 | 0 | memset(&args, 0, sizeof(args)); |
2323 | 0 | args.argarray.argarray_len = i; |
2324 | 0 | args.argarray.argarray_val = op; |
2325 | |
|
2326 | 0 | if (rpc_nfs4_compound_task(rpc, nfs4_mount_2_cb, &args, data) == NULL) { |
2327 | 0 | nfs_set_error(nfs, "Failed to queue SETCLIENTID. %s", |
2328 | 0 | rpc_get_error(rpc)); |
2329 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2330 | 0 | free_nfs4_cb_data(data); |
2331 | 0 | return; |
2332 | 0 | } |
2333 | 0 | } |
2334 | | |
2335 | | #ifdef HAVE_NFS4_2 |
2336 | | /* |
2337 | | * Third step: RECLAIM_COMPLETE has been acknowledged, so the session is fully |
2338 | | * usable and ordinary COMPOUNDs can start flowing. |
2339 | | */ |
2340 | | static void |
2341 | | nfs42_mount_3_cb(struct rpc_context *rpc, int status, void *command_data, |
2342 | | void *private_data) |
2343 | 0 | { |
2344 | 0 | struct nfs4_cb_data *data = private_data; |
2345 | 0 | struct nfs_context *nfs = data->nfs; |
2346 | 0 | COMPOUND4res *res = command_data; |
2347 | |
|
2348 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2349 | |
|
2350 | 0 | if (check_nfs4_error(nfs, status, data, res, "RECLAIM_COMPLETE")) { |
2351 | 0 | return; |
2352 | 0 | } |
2353 | | |
2354 | 0 | nfs4_mount_start_getfh(nfs, data, res); |
2355 | 0 | } |
2356 | | |
2357 | | /* |
2358 | | * Second step: CREATE_SESSION has returned a session id. Record it, mark the |
2359 | | * session usable so that nfs4_start_compound() starts emitting SEQUENCE, and |
2360 | | * declare that we have no state to reclaim. |
2361 | | */ |
2362 | | static void |
2363 | | nfs42_mount_2_cb(struct rpc_context *rpc, int status, void *command_data, |
2364 | | void *private_data) |
2365 | 0 | { |
2366 | 0 | struct nfs4_cb_data *data = private_data; |
2367 | 0 | struct nfs_context *nfs = data->nfs; |
2368 | 0 | COMPOUND4res *res = command_data; |
2369 | 0 | COMPOUND4args args; |
2370 | 0 | nfs_argop4 op[1 + 1]; |
2371 | 0 | CREATE_SESSION4resok *csresok; |
2372 | 0 | int i; |
2373 | |
|
2374 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2375 | |
|
2376 | 0 | if (check_nfs4_error(nfs, status, data, res, "CREATE_SESSION")) { |
2377 | 0 | return; |
2378 | 0 | } |
2379 | | |
2380 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_CREATE_SESSION, |
2381 | 0 | "CREATE_SESSION")) < 0) { |
2382 | 0 | return; |
2383 | 0 | } |
2384 | 0 | csresok = &res->resarray.resarray_val[i].nfs_resop4_u.opcreatesession.CREATE_SESSION4res_u.csr_resok4; |
2385 | | |
2386 | | /* |
2387 | | * A server may hand back a smaller fore channel than we asked for, so |
2388 | | * the slot table is sized by what it granted rather than what we |
2389 | | * wanted. One slot is the minimum a session can have; none at all is |
2390 | | * a server we cannot talk to. |
2391 | | */ |
2392 | 0 | if (nfs4_session_init(rpc, csresok->csr_sessionid, |
2393 | 0 | csresok->csr_fore_chan_attrs.ca_maxrequests)) { |
2394 | 0 | nfs_set_error(nfs, "Server granted a session with no usable " |
2395 | 0 | "slots (%d requested, %d granted)", |
2396 | 0 | NFS4_MAX_SLOTS, |
2397 | 0 | csresok->csr_fore_chan_attrs.ca_maxrequests); |
2398 | 0 | data->cb(-EIO, nfs, nfs_get_error(nfs), data->private_data); |
2399 | 0 | free_nfs4_cb_data(data); |
2400 | 0 | return; |
2401 | 0 | } |
2402 | 0 | rpc->nfs4_cb_up = !!(csresok->csr_flags & |
2403 | 0 | CREATE_SESSION4_FLAG_CONN_BACK_CHAN); |
2404 | |
|
2405 | 0 | memset(op, 0, sizeof(op)); |
2406 | 0 | i = nfs4_start_compound(nfs, op); |
2407 | 0 | i += nfs42_op_reclaim_complete(nfs, &op[i]); |
2408 | |
|
2409 | 0 | memset(&args, 0, sizeof(args)); |
2410 | 0 | args.argarray.argarray_len = i; |
2411 | 0 | args.argarray.argarray_val = op; |
2412 | |
|
2413 | 0 | if (rpc_nfs4_compound_task(rpc, nfs42_mount_3_cb, &args, data) == NULL) { |
2414 | 0 | nfs_set_error(nfs, "Failed to queue RECLAIM_COMPLETE. %s", |
2415 | 0 | rpc_get_error(rpc)); |
2416 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2417 | 0 | free_nfs4_cb_data(data); |
2418 | 0 | return; |
2419 | 0 | } |
2420 | 0 | } |
2421 | | |
2422 | | /* |
2423 | | * First step: EXCHANGE_ID has returned a clientid. Turn it into a session. |
2424 | | */ |
2425 | | static void |
2426 | | nfs42_mount_1_cb(struct rpc_context *rpc, int status, void *command_data, |
2427 | | void *private_data) |
2428 | 0 | { |
2429 | 0 | struct nfs4_cb_data *data = private_data; |
2430 | 0 | struct nfs_context *nfs = data->nfs; |
2431 | 0 | COMPOUND4res *res = command_data; |
2432 | 0 | COMPOUND4args args; |
2433 | 0 | nfs_argop4 op[1 + 1]; |
2434 | 0 | EXCHANGE_ID4resok *eiresok; |
2435 | 0 | int i; |
2436 | |
|
2437 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2438 | |
|
2439 | 0 | if (check_nfs4_error(nfs, status, data, res, "EXCHANGE_ID")) { |
2440 | 0 | return; |
2441 | 0 | } |
2442 | | |
2443 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_EXCHANGE_ID, |
2444 | 0 | "EXCHANGE_ID")) < 0) { |
2445 | 0 | return; |
2446 | 0 | } |
2447 | 0 | eiresok = &res->resarray.resarray_val[i].nfs_resop4_u.opexchangeid.EXCHANGE_ID4res_u.eir_resok4; |
2448 | |
|
2449 | 0 | nfs->nfsi->clientid = eiresok->eir_clientid; |
2450 | |
|
2451 | 0 | memset(op, 0, sizeof(op)); |
2452 | | /* |
2453 | | * CREATE_SESSION is part of the bootstrap and carries no SEQUENCE, so |
2454 | | * it does not go through nfs4_start_compound(). |
2455 | | */ |
2456 | 0 | i = nfs42_op_create_session(nfs, &op[0], eiresok->eir_clientid, |
2457 | 0 | eiresok->eir_sequenceid); |
2458 | |
|
2459 | 0 | memset(&args, 0, sizeof(args)); |
2460 | 0 | args.argarray.argarray_len = i; |
2461 | 0 | args.argarray.argarray_val = op; |
2462 | |
|
2463 | 0 | if (rpc_nfs4_compound_task(rpc, nfs42_mount_2_cb, &args, data) == NULL) { |
2464 | 0 | nfs_set_error(nfs, "Failed to queue CREATE_SESSION. %s", |
2465 | 0 | rpc_get_error(rpc)); |
2466 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2467 | 0 | free_nfs4_cb_data(data); |
2468 | 0 | return; |
2469 | 0 | } |
2470 | 0 | } |
2471 | | |
2472 | | /* |
2473 | | * Entry point for the 4.2 bootstrap, reached once the socket is connected. |
2474 | | */ |
2475 | | static void |
2476 | | nfs42_mount_start(struct rpc_context *rpc, int status, void *command_data _U_, |
2477 | | void *private_data) |
2478 | 0 | { |
2479 | 0 | struct nfs4_cb_data *data = private_data; |
2480 | 0 | struct nfs_context *nfs = data->nfs; |
2481 | 0 | COMPOUND4args args; |
2482 | 0 | nfs_argop4 op[1 + 1]; |
2483 | 0 | int i; |
2484 | |
|
2485 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2486 | |
|
2487 | 0 | if (check_nfs4_error(nfs, status, data, NULL, "CONNECT")) { |
2488 | 0 | return; |
2489 | 0 | } |
2490 | | |
2491 | | /* |
2492 | | * Any session from an earlier connection is gone with that |
2493 | | * connection, so start from none. The COMPOUNDs below run before |
2494 | | * there is a new one, and therefore carry no SEQUENCE. |
2495 | | */ |
2496 | 0 | nfs4_session_destroy(rpc); |
2497 | |
|
2498 | 0 | memset(op, 0, sizeof(op)); |
2499 | | /* EXCHANGE_ID is what creates the state, so it carries no SEQUENCE. */ |
2500 | 0 | i = nfs42_op_exchange_id(nfs, &op[0]); |
2501 | |
|
2502 | 0 | memset(&args, 0, sizeof(args)); |
2503 | 0 | args.argarray.argarray_len = i; |
2504 | 0 | args.argarray.argarray_val = op; |
2505 | |
|
2506 | 0 | if (rpc_nfs4_compound_task(rpc, nfs42_mount_1_cb, &args, data) == NULL) { |
2507 | 0 | nfs_set_error(nfs, "Failed to queue EXCHANGE_ID. %s", |
2508 | 0 | rpc_get_error(rpc)); |
2509 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2510 | 0 | free_nfs4_cb_data(data); |
2511 | 0 | return; |
2512 | 0 | } |
2513 | 0 | } |
2514 | | |
2515 | | /* |
2516 | | * Release the session on umount. |
2517 | | * |
2518 | | * DESTROY_SESSION carries no SEQUENCE and so needs no slot, which matters |
2519 | | * here because it is the last thing sent and anything still outstanding is |
2520 | | * holding one. The reply is only worth waiting for so that we do not tear the |
2521 | | * connection down underneath it; a failure changes nothing we can act on, the |
2522 | | * session is being abandoned either way. |
2523 | | */ |
2524 | | static void |
2525 | | nfs42_umount_cb(struct rpc_context *rpc, int status, void *command_data, |
2526 | | void *private_data) |
2527 | 0 | { |
2528 | 0 | struct nfs4_cb_data *data = private_data; |
2529 | 0 | struct nfs_context *nfs = data->nfs; |
2530 | 0 | COMPOUND4res *res = command_data; |
2531 | |
|
2532 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2533 | |
|
2534 | 0 | if (status != RPC_STATUS_SUCCESS || res == NULL || |
2535 | 0 | res->status != NFS4_OK) { |
2536 | 0 | RPC_LOG(rpc, 2, "DESTROY_SESSION failed, abandoning the " |
2537 | 0 | "session anyway"); |
2538 | 0 | } else { |
2539 | 0 | RPC_LOG(rpc, 2, "NFSv4 session destroyed"); |
2540 | 0 | } |
2541 | |
|
2542 | 0 | nfs4_session_destroy(rpc); |
2543 | 0 | data->cb(0, nfs, NULL, data->private_data); |
2544 | 0 | free_nfs4_cb_data(data); |
2545 | 0 | } |
2546 | | |
2547 | | int |
2548 | | nfs42_umount_async(struct nfs_context *nfs, nfs_cb cb, void *private_data) |
2549 | 0 | { |
2550 | 0 | struct nfs4_cb_data *data; |
2551 | |
|
2552 | 0 | if (!rpc_nfs4_session_is_valid(nfs->rpc)) { |
2553 | 0 | cb(0, nfs, NULL, private_data); |
2554 | 0 | return 0; |
2555 | 0 | } |
2556 | | |
2557 | 0 | data = calloc(1, sizeof(*data)); |
2558 | 0 | if (data == NULL) { |
2559 | | /* Nothing to clean up but our own idea of the session. */ |
2560 | 0 | nfs4_session_destroy(nfs->rpc); |
2561 | 0 | cb(0, nfs, NULL, private_data); |
2562 | 0 | return 0; |
2563 | 0 | } |
2564 | 0 | data->nfs = nfs; |
2565 | 0 | data->cb = cb; |
2566 | 0 | data->private_data = private_data; |
2567 | |
|
2568 | 0 | if (rpc_nfs4_destroy_session_task(nfs->rpc, nfs42_umount_cb, |
2569 | 0 | data) == NULL) { |
2570 | 0 | nfs4_session_destroy(nfs->rpc); |
2571 | 0 | free_nfs4_cb_data(data); |
2572 | 0 | cb(0, nfs, NULL, private_data); |
2573 | 0 | return 0; |
2574 | 0 | } |
2575 | | |
2576 | 0 | return 0; |
2577 | 0 | } |
2578 | | |
2579 | | #endif /* HAVE_NFS4_2 */ |
2580 | | |
2581 | | int |
2582 | | nfs4_mount_async(struct nfs_context *nfs, const char *server, |
2583 | | const char *export, nfs_cb cb, void *private_data) |
2584 | 0 | { |
2585 | 0 | struct nfs4_cb_data *data; |
2586 | 0 | char *new_server, *new_export; |
2587 | 0 | int port; |
2588 | 0 | rpc_cb connect_cb = nfs4_mount_1_cb; |
2589 | |
|
2590 | 0 | rpc_nfs40_stop_renew(nfs->rpc); |
2591 | |
|
2592 | 0 | new_server = strdup(server); |
2593 | 0 | if (new_server == NULL) { |
2594 | 0 | nfs_set_error(nfs, "out of memory. failed to allocate " |
2595 | 0 | "memory for nfs server string"); |
2596 | 0 | return -1; |
2597 | 0 | } |
2598 | 0 | free(nfs->nfsi->server); |
2599 | 0 | nfs->nfsi->server = new_server; |
2600 | |
|
2601 | 0 | free(nfs->rpc->server); |
2602 | 0 | nfs->rpc->server = strdup(nfs->nfsi->server); |
2603 | |
|
2604 | 0 | new_export = strdup(export); |
2605 | 0 | if (new_export == NULL) { |
2606 | 0 | nfs_set_error(nfs, "out of memory. failed to allocate " |
2607 | 0 | "memory for nfs export string"); |
2608 | 0 | return -1; |
2609 | 0 | } |
2610 | 0 | if (nfs_normalize_path(nfs, new_export)) { |
2611 | 0 | nfs_set_error(nfs, "Bad export path. %s", |
2612 | 0 | nfs_get_error(nfs)); |
2613 | 0 | free(new_export); |
2614 | 0 | return -1; |
2615 | 0 | } |
2616 | 0 | free(nfs->nfsi->export); |
2617 | 0 | nfs->nfsi->export = new_export; |
2618 | | |
2619 | |
|
2620 | 0 | data = calloc(1, sizeof(*data)); |
2621 | 0 | if (data == NULL) { |
2622 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
2623 | 0 | "memory for nfs mount data"); |
2624 | 0 | return -1; |
2625 | 0 | } |
2626 | | |
2627 | 0 | data->nfs = nfs; |
2628 | 0 | data->cb = cb; |
2629 | 0 | data->private_data = private_data; |
2630 | 0 | data->path = strdup(new_export); |
2631 | |
|
2632 | 0 | port = nfs_nfsport(nfs) ? nfs_nfsport(nfs) : 2049; |
2633 | 0 | #ifdef HAVE_NFS4_2 |
2634 | 0 | if (nfs_has_sessions(nfs)) { |
2635 | 0 | connect_cb = nfs42_mount_start; |
2636 | 0 | } |
2637 | 0 | #endif |
2638 | 0 | if (rpc_connect_port_async(nfs->rpc, server, port, |
2639 | 0 | NFS4_PROGRAM, NFS_V4, |
2640 | 0 | connect_cb, data) != 0) { |
2641 | 0 | nfs_set_error(nfs, "Failed to start connection. %s", |
2642 | 0 | rpc_get_error(nfs->rpc)); |
2643 | 0 | free_nfs4_cb_data(data); |
2644 | 0 | return -1; |
2645 | 0 | } |
2646 | 0 | return 0; |
2647 | 0 | } |
2648 | | |
2649 | | static void |
2650 | | nfs4_chdir_1_cb(struct rpc_context *rpc, int status, void *command_data, |
2651 | | void *private_data) |
2652 | 0 | { |
2653 | 0 | struct nfs4_cb_data *data = private_data; |
2654 | 0 | struct nfs_context *nfs = data->nfs; |
2655 | 0 | COMPOUND4res *res = command_data; |
2656 | |
|
2657 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2658 | |
|
2659 | 0 | if (check_nfs4_error(nfs, status, data, res, "CHDIR")) { |
2660 | 0 | return; |
2661 | 0 | } |
2662 | | |
2663 | | /* Ok, all good. Lets steal the path string. */ |
2664 | | #ifdef HAVE_MULTITHREADING |
2665 | | if (nfs->rpc->multithreading_enabled) { |
2666 | | nfs_mt_mutex_lock(&nfs->rpc->rpc_mutex); |
2667 | | } |
2668 | | #endif |
2669 | 0 | free(nfs->nfsi->cwd); |
2670 | 0 | nfs->nfsi->cwd = data->path; |
2671 | | #ifdef HAVE_MULTITHREADING |
2672 | | if (nfs->rpc->multithreading_enabled) { |
2673 | | nfs_mt_mutex_unlock(&nfs->rpc->rpc_mutex); |
2674 | | } |
2675 | | #endif |
2676 | |
|
2677 | 0 | data->path = NULL; |
2678 | |
|
2679 | 0 | data->cb(0, nfs, NULL, data->private_data); |
2680 | 0 | free_nfs4_cb_data(data); |
2681 | 0 | } |
2682 | | |
2683 | | int nfs4_chdir_async(struct nfs_context *nfs, const char *path, |
2684 | | nfs_cb cb, void *private_data) |
2685 | 0 | { |
2686 | 0 | struct nfs4_cb_data *data; |
2687 | |
|
2688 | 0 | data = init_cb_data_full_path(nfs, path); |
2689 | 0 | if (data == NULL) { |
2690 | 0 | return -1; |
2691 | 0 | } |
2692 | | |
2693 | 0 | data->cb = cb; |
2694 | 0 | data->private_data = private_data; |
2695 | 0 | data->filler.func = nfs4_populate_getattr; |
2696 | 0 | data->filler.max_op = 1; |
2697 | 0 | data->filler.data = calloc(2, sizeof(uint32_t)); |
2698 | 0 | if (data->filler.data == NULL) { |
2699 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
2700 | 0 | "data structure."); |
2701 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2702 | 0 | free_nfs4_cb_data(data); |
2703 | 0 | return -1; |
2704 | 0 | } |
2705 | | |
2706 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_chdir_1_cb) < 0) { |
2707 | 0 | free_nfs4_cb_data(data); |
2708 | 0 | return -1; |
2709 | 0 | } |
2710 | | |
2711 | 0 | return 0; |
2712 | 0 | } |
2713 | | |
2714 | | static void |
2715 | | nfs4_xstat64_cb(struct rpc_context *rpc, int status, void *command_data, |
2716 | | void *private_data) |
2717 | 0 | { |
2718 | 0 | struct nfs4_cb_data *data = private_data; |
2719 | 0 | struct nfs_context *nfs = data->nfs; |
2720 | 0 | COMPOUND4res *res = command_data; |
2721 | 0 | GETATTR4resok *garesok; |
2722 | 0 | struct nfs4_stat_64 stx; |
2723 | 0 | int nfs4_stat = data->flags & STAT64_FLAG_NFS4; |
2724 | 0 | int i; |
2725 | |
|
2726 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2727 | |
|
2728 | 0 | if (check_nfs4_error(nfs, status, data, res, "STAT64")) { |
2729 | 0 | return; |
2730 | 0 | } |
2731 | | |
2732 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_GETATTR, "GETATTR")) < 0) { |
2733 | 0 | return; |
2734 | 0 | } |
2735 | 0 | garesok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetattr.GETATTR4res_u.resok4; |
2736 | |
|
2737 | 0 | memset(&stx, 0, sizeof(stx)); |
2738 | 0 | if (nfs_parse_attributes(nfs, data, &stx.st, |
2739 | 0 | nfs4_stat ? &stx.nfs_user : NULL, |
2740 | 0 | nfs4_stat ? &stx.nfs_group : NULL, |
2741 | 0 | &garesok->obj_attributes.attrmask, |
2742 | 0 | garesok->obj_attributes.attr_vals.attrlist4_val, |
2743 | 0 | garesok->obj_attributes.attr_vals.attrlist4_len) < 0) { |
2744 | 0 | nfs4_free_stat64(&stx); |
2745 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
2746 | 0 | free_nfs4_cb_data(data); |
2747 | 0 | return; |
2748 | 0 | } |
2749 | | |
2750 | 0 | data->cb(0, nfs, nfs4_stat ? (void *)&stx : (void *)&stx.st, |
2751 | 0 | data->private_data); |
2752 | 0 | nfs4_free_stat64(&stx); |
2753 | 0 | free_nfs4_cb_data(data); |
2754 | 0 | } |
2755 | | |
2756 | | int |
2757 | | nfs4_xstat64_async(struct nfs_context *nfs, const char *path, int no_follow, |
2758 | | int nfs4_stat, nfs_cb cb, void *private_data) |
2759 | 0 | { |
2760 | 0 | struct nfs4_cb_data *data; |
2761 | |
|
2762 | 0 | data = init_cb_data_full_path(nfs, path); |
2763 | 0 | if (data == NULL) { |
2764 | 0 | return -1; |
2765 | 0 | } |
2766 | | |
2767 | 0 | if (no_follow) { |
2768 | 0 | data->flags |= LOOKUP_FLAG_NO_FOLLOW; |
2769 | 0 | } |
2770 | 0 | if (nfs4_stat) { |
2771 | 0 | data->flags |= STAT64_FLAG_NFS4; |
2772 | 0 | } |
2773 | 0 | data->cb = cb; |
2774 | 0 | data->private_data = private_data; |
2775 | 0 | data->filler.func = nfs4_populate_getattr; |
2776 | 0 | data->filler.max_op = 1; |
2777 | 0 | data->filler.data = calloc(2, sizeof(uint32_t)); |
2778 | 0 | if (data->filler.data == NULL) { |
2779 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
2780 | 0 | "data structure."); |
2781 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
2782 | 0 | free_nfs4_cb_data(data); |
2783 | 0 | return -1; |
2784 | 0 | } |
2785 | | |
2786 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_xstat64_cb) < 0) { |
2787 | 0 | free_nfs4_cb_data(data); |
2788 | 0 | return -1; |
2789 | 0 | } |
2790 | | |
2791 | 0 | return 0; |
2792 | 0 | } |
2793 | | |
2794 | | /* Takes object name as filler.data |
2795 | | * blob0 as the fattr4 attribute mask |
2796 | | * blob1 as the fattr4 attribute list |
2797 | | */ |
2798 | | static int |
2799 | | nfs4_populate_mkdir(struct nfs4_cb_data *data, nfs_argop4 *op) |
2800 | 0 | { |
2801 | 0 | struct nfs_context *nfs = data->nfs; |
2802 | |
|
2803 | 0 | return nfs4_op_create(nfs, op, data->filler.data, NF4DIR, |
2804 | 0 | &data->filler.blob0, &data->filler.blob1, |
2805 | 0 | NULL, 0); |
2806 | 0 | } |
2807 | | |
2808 | | static void |
2809 | | nfs4_mkdir_cb(struct rpc_context *rpc, int status, void *command_data, |
2810 | | void *private_data) |
2811 | 0 | { |
2812 | 0 | struct nfs4_cb_data *data = private_data; |
2813 | 0 | struct nfs_context *nfs = data->nfs; |
2814 | 0 | COMPOUND4res *res = command_data; |
2815 | |
|
2816 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2817 | |
|
2818 | 0 | if (check_nfs4_error(nfs, status, data, res, "MKDIR")) { |
2819 | 0 | return; |
2820 | 0 | } |
2821 | | |
2822 | 0 | data->cb(0, nfs, NULL, data->private_data); |
2823 | 0 | free_nfs4_cb_data(data); |
2824 | 0 | } |
2825 | | |
2826 | | int |
2827 | | nfs4_mkdir2_async(struct nfs_context *nfs, const char *path, int mode, |
2828 | | nfs_cb cb, void *private_data) |
2829 | 0 | { |
2830 | 0 | struct nfs4_cb_data *data; |
2831 | 0 | uint32_t *u32ptr; |
2832 | |
|
2833 | 0 | data = init_cb_data_split_path(nfs, path); |
2834 | 0 | if (data == NULL) { |
2835 | 0 | return -1; |
2836 | 0 | } |
2837 | | |
2838 | 0 | data->cb = cb; |
2839 | 0 | data->private_data = private_data; |
2840 | 0 | data->filler.func = nfs4_populate_mkdir; |
2841 | 0 | data->filler.max_op = 1; |
2842 | | |
2843 | | /* attribute mask */ |
2844 | 0 | u32ptr = malloc(2 * sizeof(uint32_t)); |
2845 | 0 | if (u32ptr == NULL) { |
2846 | 0 | nfs_set_error(nfs, "Out of memory allocating bitmap"); |
2847 | 0 | free_nfs4_cb_data(data); |
2848 | 0 | return -1; |
2849 | 0 | } |
2850 | 0 | u32ptr[0] = 0; |
2851 | 0 | u32ptr[1] = 1 << (FATTR4_MODE - 32); |
2852 | 0 | data->filler.blob0.len = 2; |
2853 | 0 | data->filler.blob0.val = u32ptr; |
2854 | 0 | data->filler.blob0.free = free; |
2855 | | |
2856 | | /* attribute values */ |
2857 | 0 | u32ptr = malloc(1 * sizeof(uint32_t)); |
2858 | 0 | if (u32ptr == NULL) { |
2859 | 0 | nfs_set_error(nfs, "Out of memory allocating attributes"); |
2860 | 0 | free_nfs4_cb_data(data); |
2861 | 0 | return -1; |
2862 | 0 | } |
2863 | 0 | u32ptr[0] = htonl(mode); |
2864 | 0 | data->filler.blob1.len = 4; |
2865 | 0 | data->filler.blob1.val = u32ptr; |
2866 | 0 | data->filler.blob1.free = free; |
2867 | |
|
2868 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_mkdir_cb) < 0) { |
2869 | 0 | free_nfs4_cb_data(data); |
2870 | 0 | return -1; |
2871 | 0 | } |
2872 | | |
2873 | 0 | return 0; |
2874 | 0 | } |
2875 | | |
2876 | | /* Takes object name as filler.data |
2877 | | */ |
2878 | | static int |
2879 | | nfs4_populate_remove(struct nfs4_cb_data *data, nfs_argop4 *op) |
2880 | 0 | { |
2881 | 0 | struct nfs_context *nfs = data->nfs; |
2882 | |
|
2883 | 0 | return nfs4_op_remove(nfs, op, data->filler.data); |
2884 | 0 | } |
2885 | | |
2886 | | static void |
2887 | | nfs4_remove_cb(struct rpc_context *rpc, int status, void *command_data, |
2888 | | void *private_data) |
2889 | 0 | { |
2890 | 0 | struct nfs4_cb_data *data = private_data; |
2891 | 0 | struct nfs_context *nfs = data->nfs; |
2892 | 0 | COMPOUND4res *res = command_data; |
2893 | |
|
2894 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2895 | |
|
2896 | 0 | if (check_nfs4_error(nfs, status, data, res, "REMOVE")) { |
2897 | 0 | return; |
2898 | 0 | } |
2899 | | |
2900 | 0 | data->cb(0, nfs, NULL, data->private_data); |
2901 | 0 | free_nfs4_cb_data(data); |
2902 | 0 | } |
2903 | | |
2904 | | static int |
2905 | | nfs4_remove_async(struct nfs_context *nfs, const char *path, |
2906 | | nfs_cb cb, void *private_data) |
2907 | 0 | { |
2908 | 0 | struct nfs4_cb_data *data; |
2909 | |
|
2910 | 0 | data = init_cb_data_split_path(nfs, path); |
2911 | 0 | if (data == NULL) { |
2912 | 0 | return -1; |
2913 | 0 | } |
2914 | | |
2915 | 0 | data->cb = cb; |
2916 | 0 | data->private_data = private_data; |
2917 | 0 | data->filler.func = nfs4_populate_remove; |
2918 | 0 | data->filler.max_op = 1; |
2919 | |
|
2920 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_remove_cb) < 0) { |
2921 | 0 | free_nfs4_cb_data(data); |
2922 | 0 | return -1; |
2923 | 0 | } |
2924 | | |
2925 | 0 | return 0; |
2926 | 0 | } |
2927 | | |
2928 | | int |
2929 | | nfs4_rmdir_async(struct nfs_context *nfs, const char *path, |
2930 | | nfs_cb cb, void *private_data) |
2931 | 0 | { |
2932 | 0 | return nfs4_remove_async(nfs, path, cb, private_data); |
2933 | 0 | } |
2934 | | |
2935 | | static void |
2936 | | nfs_increment_seqid(struct nfsfh *nfsfh, uint32_t status) |
2937 | 0 | { |
2938 | | /* RFC3530 8.1.5 */ |
2939 | 0 | switch (status) { |
2940 | 0 | case NFS4ERR_STALE_CLIENTID: |
2941 | 0 | case NFS4ERR_STALE_STATEID: |
2942 | 0 | case NFS4ERR_BAD_STATEID: |
2943 | 0 | case NFS4ERR_BAD_SEQID: |
2944 | 0 | case NFS4ERR_BADZDR: |
2945 | 0 | case NFS4ERR_RESOURCE: |
2946 | 0 | case NFS4ERR_NOFILEHANDLE: |
2947 | 0 | break; |
2948 | 0 | default: |
2949 | 0 | nfsfh->open_seqid++; |
2950 | 0 | } |
2951 | 0 | } |
2952 | | |
2953 | | static void |
2954 | | nfs4_open_setattr_cb(struct rpc_context *rpc, int status, void *command_data, |
2955 | | void *private_data) |
2956 | 0 | { |
2957 | 0 | struct nfs4_cb_data *data = private_data; |
2958 | 0 | struct nfs_context *nfs = data->nfs; |
2959 | 0 | COMPOUND4res *res = command_data; |
2960 | 0 | struct nfsfh *fh; |
2961 | |
|
2962 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
2963 | |
|
2964 | 0 | if (check_nfs4_error(nfs, status, data, res, "SETATTR")) { |
2965 | 0 | return; |
2966 | 0 | } |
2967 | | |
2968 | 0 | fh = data->filler.blob0.val; |
2969 | 0 | data->filler.blob0.val = NULL; |
2970 | 0 | data->cb(0, nfs, fh, data->private_data); |
2971 | 0 | free_nfs4_cb_data(data); |
2972 | 0 | } |
2973 | | |
2974 | | static void |
2975 | | nfs4_open_truncate_cb(struct rpc_context *rpc, int status, void *command_data, |
2976 | | void *private_data) |
2977 | 0 | { |
2978 | 0 | struct nfs4_cb_data *data = private_data; |
2979 | 0 | struct nfs_context *nfs = data->nfs; |
2980 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
2981 | 0 | COMPOUND4res *res = command_data; |
2982 | 0 | COMPOUND4args args; |
2983 | 0 | nfs_argop4 op[2 + 1]; |
2984 | 0 | int i; |
2985 | |
|
2986 | 0 | if (check_nfs4_error(nfs, status, data, res, "OPEN")) { |
2987 | 0 | return; |
2988 | 0 | } |
2989 | | |
2990 | 0 | i = nfs4_start_compound(nfs, op); |
2991 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
2992 | 0 | i += nfs4_op_truncate(nfs, &op[i], fh, data->filler.blob3.val); |
2993 | |
|
2994 | 0 | memset(&args, 0, sizeof(args)); |
2995 | 0 | args.argarray.argarray_len = i; |
2996 | 0 | args.argarray.argarray_val = op; |
2997 | |
|
2998 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_open_setattr_cb, &args, |
2999 | 0 | data) == NULL) { |
3000 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
3001 | 0 | free_nfs4_cb_data(data); |
3002 | 0 | return; |
3003 | 0 | } |
3004 | 0 | } |
3005 | | |
3006 | | static void |
3007 | | nfs4_open_chmod_cb(struct rpc_context *rpc, int status, void *command_data, |
3008 | | void *private_data) |
3009 | 0 | { |
3010 | 0 | struct nfs4_cb_data *data = private_data; |
3011 | 0 | struct nfs_context *nfs = data->nfs; |
3012 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
3013 | 0 | COMPOUND4res *res = command_data; |
3014 | 0 | COMPOUND4args args; |
3015 | 0 | nfs_argop4 op[2 + 1]; |
3016 | 0 | int i; |
3017 | |
|
3018 | 0 | if (check_nfs4_error(nfs, status, data, res, "OPEN")) { |
3019 | 0 | return; |
3020 | 0 | } |
3021 | | |
3022 | 0 | i = nfs4_start_compound(nfs, op); |
3023 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
3024 | 0 | i += nfs4_op_chmod(nfs, &op[i], fh, data->filler.blob3.val); |
3025 | |
|
3026 | 0 | memset(&args, 0, sizeof(args)); |
3027 | 0 | args.argarray.argarray_len = i; |
3028 | 0 | args.argarray.argarray_val = op; |
3029 | |
|
3030 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_open_setattr_cb, &args, |
3031 | 0 | data) == NULL) { |
3032 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
3033 | 0 | free_nfs4_cb_data(data); |
3034 | 0 | return; |
3035 | 0 | } |
3036 | 0 | } |
3037 | | |
3038 | | static void |
3039 | | nfs4_open_confirm_cb(struct rpc_context *rpc, int status, void *command_data, |
3040 | | void *private_data) |
3041 | 0 | { |
3042 | 0 | struct nfs4_cb_data *data = private_data; |
3043 | 0 | struct nfs_context *nfs = data->nfs; |
3044 | 0 | COMPOUND4res *res = command_data; |
3045 | 0 | OPEN_CONFIRM4resok *ocresok; |
3046 | 0 | int i; |
3047 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
3048 | |
|
3049 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3050 | |
|
3051 | 0 | if (res) { |
3052 | 0 | nfs_increment_seqid(fh, res->status); |
3053 | 0 | } |
3054 | |
|
3055 | 0 | if (check_nfs4_error(nfs, status, data, res, "OPEN_CONFIRM")) { |
3056 | 0 | return; |
3057 | 0 | } |
3058 | | |
3059 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_OPEN_CONFIRM, |
3060 | 0 | "OPEN_CONFIRM")) < 0) { |
3061 | 0 | return; |
3062 | 0 | } |
3063 | 0 | ocresok = &res->resarray.resarray_val[i].nfs_resop4_u.opopen_confirm.OPEN_CONFIRM4res_u.resok4; |
3064 | |
|
3065 | 0 | fh = data->filler.blob0.val; |
3066 | |
|
3067 | 0 | fh->stateid.seqid = ocresok->open_stateid.seqid; |
3068 | 0 | memcpy(fh->stateid.other, ocresok->open_stateid.other, 12); |
3069 | |
|
3070 | 0 | if (data->open_cb) { |
3071 | 0 | data->open_cb(rpc, status, command_data, private_data); |
3072 | 0 | return; |
3073 | 0 | } |
3074 | 0 | data->filler.blob0.val = NULL; |
3075 | 0 | data->cb(0, nfs, fh, data->private_data); |
3076 | 0 | free_nfs4_cb_data(data); |
3077 | 0 | } |
3078 | | |
3079 | | static void |
3080 | | nfs4_open_cb(struct rpc_context *rpc, int status, void *command_data, |
3081 | | void *private_data) |
3082 | 0 | { |
3083 | 0 | struct nfs4_cb_data *data = private_data; |
3084 | 0 | struct nfs_context *nfs = data->nfs; |
3085 | 0 | COMPOUND4res *res = command_data; |
3086 | 0 | ACCESS4resok *aresok; |
3087 | 0 | OPEN4resok *oresok; |
3088 | 0 | GETFH4resok *gresok; |
3089 | 0 | int i; |
3090 | 0 | struct nfsfh *fh; |
3091 | |
|
3092 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3093 | |
|
3094 | 0 | if (check_nfs4_error(nfs, status, data, res, "OPEN")) { |
3095 | 0 | return; |
3096 | 0 | } |
3097 | | |
3098 | | /* Parse Access and check that we have the access that we need */ |
3099 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_ACCESS, "ACCESS")) < 0) { |
3100 | 0 | return; |
3101 | 0 | } |
3102 | 0 | aresok = &res->resarray.resarray_val[i].nfs_resop4_u.opaccess.ACCESS4res_u.resok4; |
3103 | 0 | if (aresok->supported != aresok->access) { |
3104 | 0 | nfs_set_error(nfs, "Insufficient ACCESS. Wanted %08x but " |
3105 | 0 | "got %08x.", (int)aresok->access, (int)aresok->supported); |
3106 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
3107 | 0 | free_nfs4_cb_data(data); |
3108 | 0 | return; |
3109 | 0 | } |
3110 | | |
3111 | 0 | if ((gresok = nfs4_find_getfh(nfs, data, res)) == NULL) { |
3112 | 0 | return; |
3113 | 0 | } |
3114 | | |
3115 | 0 | fh = calloc(1, sizeof(*fh)); |
3116 | 0 | if (fh == NULL) { |
3117 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
3118 | 0 | "nfsfh"); |
3119 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
3120 | 0 | free_nfs4_cb_data(data); |
3121 | 0 | return; |
3122 | 0 | } |
3123 | | |
3124 | 0 | data->filler.blob0.val = fh; |
3125 | 0 | data->filler.blob0.free = (blob_free)nfs_free_nfsfh; |
3126 | |
|
3127 | 0 | fh->fh.len = gresok->object.nfs_fh4_len; |
3128 | 0 | fh->fh.val = malloc(fh->fh.len); |
3129 | 0 | if (fh->fh.val == NULL) { |
3130 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
3131 | 0 | "nfsfh"); |
3132 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
3133 | 0 | free_nfs4_cb_data(data); |
3134 | 0 | return; |
3135 | 0 | } |
3136 | 0 | memcpy(fh->fh.val, gresok->object.nfs_fh4_val, fh->fh.len); |
3137 | 0 | fh->open_seqid = 1; |
3138 | 0 | fh->open_owner = data->open_owner; |
3139 | | |
3140 | 0 | if (data->filler.flags & O_SYNC) { |
3141 | 0 | fh->is_sync = 1; |
3142 | 0 | } |
3143 | |
|
3144 | 0 | if (data->filler.flags & O_APPEND) { |
3145 | 0 | fh->is_append = 1; |
3146 | 0 | } |
3147 | |
|
3148 | 0 | if (!(data->filler.flags & (O_WRONLY|O_RDWR))) { |
3149 | 0 | fh->is_readonly = 1; |
3150 | 0 | } |
3151 | | |
3152 | | /* Parse Open */ |
3153 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_OPEN, "OPEN")) < 0) { |
3154 | 0 | return; |
3155 | 0 | } |
3156 | 0 | oresok = &res->resarray.resarray_val[i].nfs_resop4_u.opopen.OPEN4res_u.resok4; |
3157 | 0 | fh->stateid.seqid = oresok->stateid.seqid; |
3158 | 0 | memcpy(fh->stateid.other, oresok->stateid.other, 12); |
3159 | | |
3160 | |
|
3161 | 0 | if (oresok->rflags & OPEN4_RESULT_CONFIRM) { |
3162 | 0 | COMPOUND4args args; |
3163 | 0 | nfs_argop4 op[2 + 1]; |
3164 | |
|
3165 | 0 | memset(op, 0, sizeof(op)); |
3166 | 0 | i = nfs4_start_compound(nfs, op); |
3167 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
3168 | 0 | i += nfs40_op_open_confirm(nfs, &op[i], fh); |
3169 | |
|
3170 | 0 | memset(&args, 0, sizeof(args)); |
3171 | 0 | args.argarray.argarray_len = i; |
3172 | 0 | args.argarray.argarray_val = op; |
3173 | |
|
3174 | 0 | if (rpc_nfs4_compound_task(rpc, nfs4_open_confirm_cb, &args, |
3175 | 0 | private_data) == NULL) { |
3176 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
3177 | 0 | data->private_data); |
3178 | 0 | free_nfs4_cb_data(data); |
3179 | 0 | return; |
3180 | 0 | } |
3181 | 0 | return; |
3182 | 0 | } |
3183 | | |
3184 | 0 | if (data->open_cb) { |
3185 | 0 | data->open_cb(rpc, status, command_data, private_data); |
3186 | 0 | return; |
3187 | 0 | } |
3188 | 0 | data->filler.blob0.val = NULL; |
3189 | 0 | data->cb(0, nfs, fh, data->private_data); |
3190 | 0 | free_nfs4_cb_data(data); |
3191 | 0 | } |
3192 | | |
3193 | | static void |
3194 | | nfs4_init_random_verifier(char *verifier) |
3195 | 0 | { |
3196 | 0 | static uint64_t seed = 0, v; |
3197 | 0 | int i; |
3198 | |
|
3199 | 0 | if (seed == 0) { |
3200 | 0 | seed = ~rpc_current_time() << 32 | getpid(); |
3201 | 0 | } else { |
3202 | 0 | seed *= 1337; |
3203 | 0 | } |
3204 | |
|
3205 | 0 | v = seed; |
3206 | 0 | for (i = 0; i < NFS4_VERIFIER_SIZE; i++) { |
3207 | 0 | verifier[i] = v & 0xff; |
3208 | 0 | v >>= 8; |
3209 | 0 | } |
3210 | 0 | } |
3211 | | |
3212 | | /* filler.flags are the open flags |
3213 | | * filler.data is the object name |
3214 | | */ |
3215 | | static int |
3216 | | nfs4_populate_open(struct nfs4_cb_data *data, nfs_argop4 *op) |
3217 | 0 | { |
3218 | 0 | struct nfs_context *nfs = data->nfs; |
3219 | 0 | OPEN4args *oargs; |
3220 | 0 | uint32_t access_mask = 0; |
3221 | 0 | verifier4 verifier; |
3222 | 0 | int i; |
3223 | |
|
3224 | 0 | if (data->filler.flags & O_WRONLY) { |
3225 | 0 | access_mask |= ACCESS4_MODIFY; |
3226 | 0 | } |
3227 | 0 | if (data->filler.flags & O_RDWR) { |
3228 | 0 | access_mask |= ACCESS4_READ|ACCESS4_MODIFY; |
3229 | 0 | } |
3230 | 0 | if (!(data->filler.flags & O_WRONLY)) { |
3231 | 0 | access_mask |= ACCESS4_READ; |
3232 | 0 | } |
3233 | | |
3234 | | /* Access */ |
3235 | 0 | i = nfs4_op_access(nfs, &op[0], access_mask); |
3236 | | |
3237 | | /* Open */ |
3238 | 0 | op[i].argop = OP_OPEN; |
3239 | 0 | oargs = &op[i++].nfs_argop4_u.opopen; |
3240 | 0 | memset(oargs, 0, sizeof(*oargs)); |
3241 | |
|
3242 | 0 | if (access_mask & ACCESS4_READ) { |
3243 | 0 | oargs->share_access |= OPEN4_SHARE_ACCESS_READ; |
3244 | 0 | } |
3245 | 0 | if (access_mask & ACCESS4_MODIFY) { |
3246 | 0 | oargs->share_access |= OPEN4_SHARE_ACCESS_WRITE; |
3247 | 0 | } |
3248 | 0 | #ifdef HAVE_NFS4_2 |
3249 | | /* With a back channel the server could grant us one; we can't hold it. */ |
3250 | 0 | if (nfs_has_sessions(nfs) && nfs->rpc->nfs4_cb_fn) { |
3251 | 0 | oargs->share_access |= OPEN4_SHARE_ACCESS_WANT_NO_DELEG; |
3252 | 0 | } |
3253 | 0 | #endif |
3254 | 0 | oargs->share_deny = OPEN4_SHARE_DENY_NONE; |
3255 | 0 | oargs->owner.clientid = nfs->nfsi->clientid; |
3256 | 0 | oargs->owner.owner.owner_len = 4; |
3257 | 0 | oargs->owner.owner.owner_val = (char *)&data->open_owner; |
3258 | 0 | oargs->seqid = 0; |
3259 | 0 | if (data->filler.flags & O_CREAT) { |
3260 | 0 | createhow4 *ch; |
3261 | 0 | fattr4 *fa; |
3262 | |
|
3263 | 0 | ch = &oargs->openhow.openflag4_u.how; |
3264 | 0 | fa = &ch->createhow4_u.createattrs; |
3265 | |
|
3266 | 0 | oargs->openhow.opentype = OPEN4_CREATE; |
3267 | 0 | if (data->filler.flags & O_EXCL) { |
3268 | 0 | ch->mode = EXCLUSIVE4; |
3269 | |
|
3270 | 0 | nfs4_init_random_verifier(&verifier[0]); |
3271 | 0 | memcpy(ch->createhow4_u.createverf, verifier, |
3272 | 0 | sizeof(verifier4)); |
3273 | 0 | } else { |
3274 | 0 | ch->mode = UNCHECKED4; |
3275 | 0 | fa->attrmask.bitmap4_len = data->filler.blob1.len; |
3276 | 0 | fa->attrmask.bitmap4_val = data->filler.blob1.val; |
3277 | |
|
3278 | 0 | fa->attr_vals.attrlist4_len = data->filler.blob2.len; |
3279 | 0 | fa->attr_vals.attrlist4_val = data->filler.blob2.val; |
3280 | 0 | } |
3281 | 0 | } else { |
3282 | 0 | oargs->openhow.opentype = OPEN4_NOCREATE; |
3283 | 0 | } |
3284 | 0 | oargs->claim.claim = CLAIM_NULL; |
3285 | 0 | oargs->claim.open_claim4_u.file.utf8string_len = |
3286 | 0 | strlen(data->filler.data); |
3287 | 0 | oargs->claim.open_claim4_u.file.utf8string_val = |
3288 | 0 | data->filler.data; |
3289 | | |
3290 | | /* GetFH */ |
3291 | 0 | i += nfs4_op_getfh(nfs, &op[i]); |
3292 | |
|
3293 | 0 | return i; |
3294 | 0 | } |
3295 | | |
3296 | | static void |
3297 | | nfs4_open_readlink_cb(struct rpc_context *rpc, int status, void *command_data, |
3298 | | void *private_data) |
3299 | 0 | { |
3300 | 0 | struct nfs4_cb_data *data = private_data; |
3301 | 0 | struct nfs_context *nfs = data->nfs; |
3302 | 0 | COMPOUND4res *res = command_data; |
3303 | 0 | READLINK4resok *rlresok; |
3304 | 0 | int i; |
3305 | 0 | char *path; |
3306 | |
|
3307 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3308 | |
|
3309 | 0 | if (check_nfs4_error(nfs, status, data, res, "READLINK")) { |
3310 | 0 | return; |
3311 | 0 | } |
3312 | 0 | if (nfs4_follow_link(nfs, data) < 0) { |
3313 | 0 | return; |
3314 | 0 | } |
3315 | | |
3316 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_READLINK, "READLINK")) < 0) { |
3317 | 0 | return; |
3318 | 0 | } |
3319 | | |
3320 | 0 | rlresok = &res->resarray.resarray_val[i].nfs_resop4_u.opreadlink.READLINK4res_u.resok4; |
3321 | |
|
3322 | 0 | path = malloc(2 + strlen(data->path) + |
3323 | 0 | rlresok->link.utf8string_len); |
3324 | 0 | if (path == NULL) { |
3325 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
3326 | 0 | "path"); |
3327 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
3328 | 0 | data->private_data); |
3329 | 0 | free_nfs4_cb_data(data); |
3330 | 0 | return; |
3331 | 0 | } |
3332 | 0 | sprintf(path, "%s/%.*s", data->path, |
3333 | 0 | (int)rlresok->link.utf8string_len, rlresok->link.utf8string_val); |
3334 | | |
3335 | |
|
3336 | 0 | free(data->path); |
3337 | 0 | data->path = NULL; |
3338 | 0 | free(data->filler.data); |
3339 | 0 | data->filler.data = NULL; |
3340 | |
|
3341 | 0 | data->path = nfs4_resolve_path(nfs, path); |
3342 | 0 | free(path); |
3343 | 0 | if (data->path == NULL) { |
3344 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), |
3345 | 0 | data->private_data); |
3346 | 0 | free_nfs4_cb_data(data); |
3347 | 0 | return; |
3348 | 0 | } |
3349 | | |
3350 | 0 | data_split_path(data); |
3351 | |
|
3352 | | #ifdef HAVE_MULTITHREADING |
3353 | | if (nfs->rpc->multithreading_enabled) { |
3354 | | nfs_mt_mutex_lock(&nfs->nfsi->nfs4_open_counter_mutex); |
3355 | | } |
3356 | | #endif |
3357 | 0 | data->open_owner = nfs->nfsi->open_counter++; |
3358 | | #ifdef HAVE_MULTITHREADING |
3359 | | if (nfs->rpc->multithreading_enabled) { |
3360 | | nfs_mt_mutex_unlock(&nfs->nfsi->nfs4_open_counter_mutex); |
3361 | | } |
3362 | | #endif |
3363 | | |
3364 | 0 | data->filler.func = nfs4_populate_open; |
3365 | 0 | data->filler.max_op = 3; |
3366 | | |
3367 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_open_cb) < 0) { |
3368 | 0 | data->cb(-ENOMEM, nfs, res, data->private_data); |
3369 | 0 | free_nfs4_cb_data(data); |
3370 | 0 | return; |
3371 | 0 | } |
3372 | 0 | } |
3373 | | |
3374 | | static int |
3375 | | nfs4_populate_lookup_readlink(struct nfs4_cb_data *data, nfs_argop4 *op) |
3376 | 0 | { |
3377 | 0 | struct nfs_context *nfs = data->nfs; |
3378 | 0 | int i; |
3379 | |
|
3380 | 0 | i = nfs4_op_lookup(nfs, &op[0], data->filler.data); |
3381 | 0 | i += nfs4_op_readlink(nfs, &op[i]); |
3382 | |
|
3383 | 0 | return i; |
3384 | 0 | } |
3385 | | |
3386 | | /* If the final component in the open was a symlink we need to resolve it and |
3387 | | * re-try the nfs4_open_async() |
3388 | | */ |
3389 | | static int |
3390 | | nfs4_open_readlink(struct rpc_context *rpc, COMPOUND4res *res, |
3391 | | struct nfs4_cb_data *data) |
3392 | 0 | { |
3393 | 0 | struct nfs_context *nfs = data->nfs; |
3394 | 0 | int i; |
3395 | |
|
3396 | 0 | if (data->filler.func != nfs4_populate_open) { |
3397 | 0 | return 0; |
3398 | 0 | } |
3399 | | |
3400 | 0 | for (i = 0; i < (int)res->resarray.resarray_len; i++) { |
3401 | 0 | OPEN4res *ores; |
3402 | |
|
3403 | 0 | if (res->resarray.resarray_val[i].resop != OP_OPEN) { |
3404 | 0 | continue; |
3405 | 0 | } |
3406 | 0 | ores = &res->resarray.resarray_val[i].nfs_resop4_u.opopen; |
3407 | |
|
3408 | 0 | if (ores->status != NFS4ERR_SYMLINK) { |
3409 | 0 | continue; |
3410 | 0 | } |
3411 | | |
3412 | 0 | if (data->filler.flags & O_NOFOLLOW) { |
3413 | 0 | nfs_set_error(nfs, "Symlink encountered during " |
3414 | 0 | "open(O_NOFOLLOW)"); |
3415 | 0 | data->cb(-ELOOP, nfs, nfs_get_error(nfs), |
3416 | 0 | data->private_data); |
3417 | 0 | free_nfs4_cb_data(data); |
3418 | 0 | return -1; |
3419 | 0 | } |
3420 | | |
3421 | | /* The object we need to do readlink on is already stored in |
3422 | | * data->filler.data so *populate* can just grab it from there. |
3423 | | */ |
3424 | 0 | data->filler.func = nfs4_populate_lookup_readlink; |
3425 | 0 | data->filler.max_op = 2; |
3426 | |
|
3427 | 0 | if (nfs4_lookup_path_async(nfs, data, |
3428 | 0 | nfs4_open_readlink_cb) < 0) { |
3429 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
3430 | 0 | data->private_data); |
3431 | 0 | free_nfs4_cb_data(data); |
3432 | 0 | return -1; |
3433 | 0 | } |
3434 | 0 | return -1; |
3435 | 0 | } |
3436 | | |
3437 | 0 | return 0; |
3438 | 0 | } |
3439 | | |
3440 | | /* |
3441 | | * data.blob0 is used for nfsfh |
3442 | | * data.blob1 is used for the attribute mask in case on O_CREAT |
3443 | | * data.blob2 is the attribute value in case of O_CREAT |
3444 | | */ |
3445 | | static int |
3446 | | nfs4_open_async_internal(struct nfs_context *nfs, struct nfs4_cb_data *data, |
3447 | | int flags, int mode) |
3448 | 0 | { |
3449 | 0 | if (flags & O_APPEND && !(flags & (O_RDWR|O_WRONLY))) { |
3450 | 0 | flags &= ~O_APPEND; |
3451 | 0 | } |
3452 | |
|
3453 | 0 | if (flags & O_CREAT) { |
3454 | 0 | uint32_t *d; |
3455 | | |
3456 | | /* Attribute mask */ |
3457 | 0 | d = malloc(2 * sizeof(uint32_t)); |
3458 | 0 | if (d == NULL) { |
3459 | 0 | nfs_set_error(nfs, "Out of memory"); |
3460 | 0 | free_nfs4_cb_data(data); |
3461 | 0 | return -1; |
3462 | 0 | } |
3463 | 0 | d[0] = 0; |
3464 | 0 | d[1] = 1 << (FATTR4_MODE - 32); |
3465 | |
|
3466 | 0 | data->filler.blob1.val = d; |
3467 | 0 | data->filler.blob1.len = 2; |
3468 | 0 | data->filler.blob1.free = free; |
3469 | | |
3470 | | /* Attribute value */ |
3471 | 0 | d = malloc(sizeof(uint32_t)); |
3472 | 0 | if (d == NULL) { |
3473 | 0 | nfs_set_error(nfs, "Out of memory"); |
3474 | 0 | free_nfs4_cb_data(data); |
3475 | 0 | return -1; |
3476 | 0 | } |
3477 | | |
3478 | 0 | *d = htonl(mode); |
3479 | |
|
3480 | 0 | data->filler.blob2.val = d; |
3481 | 0 | data->filler.blob2.len = 4; |
3482 | 0 | data->filler.blob2.free = free; |
3483 | 0 | } |
3484 | | |
3485 | | #ifdef HAVE_MULTITHREADING |
3486 | | if (nfs->rpc->multithreading_enabled) { |
3487 | | nfs_mt_mutex_lock(&nfs->nfsi->nfs4_open_counter_mutex); |
3488 | | } |
3489 | | #endif |
3490 | 0 | data->open_owner = nfs->nfsi->open_counter++; |
3491 | | #ifdef HAVE_MULTITHREADING |
3492 | | if (nfs->rpc->multithreading_enabled) { |
3493 | | nfs_mt_mutex_unlock(&nfs->nfsi->nfs4_open_counter_mutex); |
3494 | | } |
3495 | | #endif |
3496 | 0 | data->filler.func = nfs4_populate_open; |
3497 | 0 | data->filler.max_op = 3; |
3498 | 0 | data->filler.flags = flags; |
3499 | |
|
3500 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_open_cb) < 0) { |
3501 | 0 | free_nfs4_cb_data(data); |
3502 | 0 | return -1; |
3503 | 0 | } |
3504 | | |
3505 | 0 | return 0; |
3506 | 0 | } |
3507 | | |
3508 | | int |
3509 | | nfs4_open_async(struct nfs_context *nfs, const char *path, int flags, |
3510 | | int mode, nfs_cb cb, void *private_data) |
3511 | 0 | { |
3512 | 0 | struct nfs4_cb_data *data; |
3513 | 0 | uint32_t m; |
3514 | 0 | int ret; |
3515 | | |
3516 | 0 | data = init_cb_data_split_path(nfs, path); |
3517 | 0 | if (data == NULL) { |
3518 | 0 | return -1; |
3519 | 0 | } |
3520 | | |
3521 | 0 | data->cb = cb; |
3522 | 0 | data->private_data = private_data; |
3523 | | |
3524 | | /* O_TRUNC is only valid for O_RDWR or O_WRONLY */ |
3525 | 0 | if (flags & O_TRUNC && !(flags & (O_RDWR|O_WRONLY))) { |
3526 | 0 | flags &= ~O_TRUNC; |
3527 | 0 | } |
3528 | | /* Successful O_EXCL means the file is 0 size already. */ |
3529 | 0 | if (flags & O_EXCL) { |
3530 | 0 | flags &= ~O_TRUNC; |
3531 | 0 | } |
3532 | |
|
3533 | 0 | if (flags & O_TRUNC) { |
3534 | 0 | data->open_cb = nfs4_open_truncate_cb; |
3535 | |
|
3536 | 0 | data->filler.blob3.val = malloc(12); |
3537 | 0 | if (data->filler.blob3.val == NULL) { |
3538 | 0 | nfs_set_error(nfs, "Out of memory"); |
3539 | 0 | free_nfs4_cb_data(data); |
3540 | 0 | return -1; |
3541 | 0 | } |
3542 | 0 | data->filler.blob3.free = free; |
3543 | |
|
3544 | 0 | memset(data->filler.blob3.val, 0, 12); |
3545 | 0 | } |
3546 | 0 | if (flags & O_EXCL) { |
3547 | 0 | data->open_cb = nfs4_open_chmod_cb; |
3548 | |
|
3549 | 0 | data->filler.blob3.val = malloc(4); |
3550 | 0 | if (data->filler.blob3.val == NULL) { |
3551 | 0 | nfs_set_error(nfs, "Out of memory"); |
3552 | 0 | free_nfs4_cb_data(data); |
3553 | 0 | return -1; |
3554 | 0 | } |
3555 | 0 | data->filler.blob3.free = free; |
3556 | |
|
3557 | 0 | m = htonl(mode); |
3558 | 0 | memcpy(data->filler.blob3.val, &m, sizeof(uint32_t)); |
3559 | 0 | } |
3560 | | |
3561 | | #ifdef HAVE_MULTITHREADING |
3562 | | if (nfs->rpc->multithreading_enabled) { |
3563 | | nfs_mt_sem_wait(&nfs->nfsi->nfs4_open_call_sem); |
3564 | | data->flags |= MUTEX_HELD; |
3565 | | } |
3566 | | #endif |
3567 | 0 | ret = nfs4_open_async_internal(nfs, data, flags, mode); |
3568 | 0 | return ret; |
3569 | 0 | } |
3570 | | |
3571 | | int |
3572 | | nfs4_xfstat64_async(struct nfs_context *nfs, struct nfsfh *nfsfh, |
3573 | | int nfs4_stat, nfs_cb cb, void *private_data) |
3574 | 0 | { |
3575 | 0 | COMPOUND4args args; |
3576 | 0 | nfs_argop4 op[2 + 1]; |
3577 | 0 | struct nfs4_cb_data *data; |
3578 | 0 | int i; |
3579 | |
|
3580 | 0 | data = calloc(1, sizeof(*data)); |
3581 | 0 | if (data == NULL) { |
3582 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
3583 | 0 | "cb data"); |
3584 | 0 | return -1; |
3585 | 0 | } |
3586 | | |
3587 | 0 | data->nfs = nfs; |
3588 | 0 | data->cb = cb; |
3589 | 0 | data->private_data = private_data; |
3590 | 0 | if (nfs4_stat) { |
3591 | 0 | data->flags |= STAT64_FLAG_NFS4; |
3592 | 0 | } |
3593 | |
|
3594 | 0 | i = nfs4_start_compound(nfs, op); |
3595 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
3596 | 0 | i += nfs4_op_getattr(nfs, &op[i], standard_attributes, 2); |
3597 | |
|
3598 | 0 | memset(&args, 0, sizeof(args)); |
3599 | 0 | args.argarray.argarray_len = i; |
3600 | 0 | args.argarray.argarray_val = op; |
3601 | |
|
3602 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_xstat64_cb, &args, |
3603 | 0 | data) == NULL) { |
3604 | 0 | free_nfs4_cb_data(data); |
3605 | 0 | return -1; |
3606 | 0 | } |
3607 | | |
3608 | 0 | return 0; |
3609 | 0 | } |
3610 | | |
3611 | | static void |
3612 | | nfs4_getacl_cb(struct rpc_context *rpc, int status, void *command_data, |
3613 | | void *private_data) |
3614 | 0 | { |
3615 | 0 | struct nfs4_cb_data *data = private_data; |
3616 | 0 | struct nfs_context *nfs = data->nfs; |
3617 | 0 | COMPOUND4res *res = command_data; |
3618 | 0 | GETATTR4resok *garesok; |
3619 | 0 | fattr4_acl acl; |
3620 | 0 | ZDR zdr; |
3621 | 0 | int i; |
3622 | |
|
3623 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3624 | |
|
3625 | 0 | if (check_nfs4_error(nfs, status, data, res, "GETACL")) { |
3626 | 0 | return; |
3627 | 0 | } |
3628 | | |
3629 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_GETATTR, "GETATTR")) < 0) { |
3630 | 0 | return; |
3631 | 0 | } |
3632 | 0 | garesok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetattr.GETATTR4res_u.resok4; |
3633 | |
|
3634 | 0 | memset(&acl, 0, sizeof(acl)); |
3635 | 0 | zdrmem_create(&zdr, |
3636 | 0 | garesok->obj_attributes.attr_vals.attrlist4_val, |
3637 | 0 | garesok->obj_attributes.attr_vals.attrlist4_len, |
3638 | 0 | ZDR_DECODE); |
3639 | 0 | if (zdr_fattr4_acl(&zdr, &acl)) { |
3640 | 0 | data->cb(0, nfs, &acl, data->private_data); |
3641 | 0 | } else { |
3642 | 0 | data->cb(-EIO, nfs, "Failed to unmarshall fattr4_acl", data->private_data); |
3643 | 0 | } |
3644 | |
|
3645 | 0 | zdr_destroy(&zdr); |
3646 | 0 | free_nfs4_cb_data(data); |
3647 | 0 | } |
3648 | | |
3649 | | int |
3650 | | nfs4_getacl_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, |
3651 | | void *private_data) |
3652 | 0 | { |
3653 | 0 | COMPOUND4args args; |
3654 | 0 | nfs_argop4 op[2 + 1]; |
3655 | 0 | struct nfs4_cb_data *data; |
3656 | 0 | int i; |
3657 | |
|
3658 | 0 | data = calloc(1, sizeof(*data)); |
3659 | 0 | if (data == NULL) { |
3660 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
3661 | 0 | "cb data"); |
3662 | 0 | return -1; |
3663 | 0 | } |
3664 | | |
3665 | 0 | data->nfs = nfs; |
3666 | 0 | data->cb = cb; |
3667 | 0 | data->private_data = private_data; |
3668 | |
|
3669 | 0 | i = nfs4_start_compound(nfs, op); |
3670 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
3671 | 0 | i += nfs4_op_getattr(nfs, &op[i], getacl_attributes, 1); |
3672 | |
|
3673 | 0 | memset(&args, 0, sizeof(args)); |
3674 | 0 | args.argarray.argarray_len = i; |
3675 | 0 | args.argarray.argarray_val = op; |
3676 | |
|
3677 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_getacl_cb, &args, |
3678 | 0 | data) == NULL) { |
3679 | 0 | free_nfs4_cb_data(data); |
3680 | 0 | return -1; |
3681 | 0 | } |
3682 | | |
3683 | 0 | return 0; |
3684 | 0 | } |
3685 | | |
3686 | | |
3687 | | static void |
3688 | | nfs4_close_cb(struct rpc_context *rpc, int status, void *command_data, |
3689 | | void *private_data) |
3690 | 0 | { |
3691 | 0 | struct nfs4_cb_data *data = private_data; |
3692 | 0 | struct nfs_context *nfs = data->nfs; |
3693 | 0 | COMPOUND4res *res = command_data; |
3694 | 0 | struct nfsfh *nfsfh = data->filler.blob0.val; |
3695 | |
|
3696 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3697 | |
|
3698 | 0 | if (res) { |
3699 | 0 | nfs_increment_seqid(nfsfh, res->status); |
3700 | 0 | } |
3701 | |
|
3702 | 0 | if (check_nfs4_error(nfs, status, data, res, "CLOSE")) { |
3703 | 0 | return; |
3704 | 0 | } |
3705 | | |
3706 | 0 | data->cb(0, nfs, NULL, data->private_data); |
3707 | 0 | free_nfs4_cb_data(data); |
3708 | 0 | } |
3709 | | |
3710 | | int |
3711 | | nfs4_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, |
3712 | | void *private_data) |
3713 | 0 | { |
3714 | 0 | COMPOUND4args args; |
3715 | 0 | nfs_argop4 op[3 + 1]; |
3716 | 0 | struct nfs4_cb_data *data; |
3717 | 0 | int i; |
3718 | |
|
3719 | 0 | data = calloc(1, sizeof(*data)); |
3720 | 0 | if (data == NULL) { |
3721 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
3722 | 0 | "cb data"); |
3723 | 0 | return -1; |
3724 | 0 | } |
3725 | | |
3726 | | #ifdef HAVE_MULTITHREADING |
3727 | | if (nfs->rpc->multithreading_enabled) { |
3728 | | nfs_mt_sem_wait(&nfs->nfsi->nfs4_open_call_sem); |
3729 | | data->flags |= MUTEX_HELD; |
3730 | | } |
3731 | | #endif |
3732 | 0 | data->nfs = nfs; |
3733 | 0 | data->cb = cb; |
3734 | 0 | data->private_data = private_data; |
3735 | |
|
3736 | 0 | memset(op, 0, sizeof(op)); |
3737 | |
|
3738 | 0 | i = nfs4_start_compound(nfs, op); |
3739 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
3740 | 0 | i += nfs4_op_close(nfs, &op[i], nfsfh); |
3741 | |
|
3742 | 0 | data->filler.blob0.val = nfsfh; |
3743 | 0 | data->filler.blob0.free = (blob_free)nfs_free_nfsfh; |
3744 | |
|
3745 | 0 | memset(&args, 0, sizeof(args)); |
3746 | 0 | args.argarray.argarray_len = i; |
3747 | 0 | args.argarray.argarray_val = op; |
3748 | |
|
3749 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_close_cb, &args, |
3750 | 0 | data) == NULL) { |
3751 | 0 | nfs_set_error(nfs, "Failed to queue CLOSE. %s", |
3752 | 0 | rpc_get_error(nfs->rpc)); |
3753 | 0 | data->filler.blob0.val = NULL; |
3754 | 0 | free_nfs4_cb_data(data); |
3755 | 0 | return -1; |
3756 | 0 | } |
3757 | | |
3758 | 0 | return 0; |
3759 | 0 | } |
3760 | | |
3761 | | static void |
3762 | | nfs4_pread_cb(struct rpc_context *rpc, int status, void *command_data, |
3763 | | void *private_data) |
3764 | 0 | { |
3765 | 0 | struct nfs4_cb_data *data = private_data; |
3766 | 0 | struct nfs_context *nfs = data->nfs; |
3767 | 0 | COMPOUND4res *res = command_data; |
3768 | 0 | READ4resok *rres = NULL; |
3769 | 0 | struct nfsfh *nfsfh; |
3770 | 0 | uint32_t count; |
3771 | 0 | int i; |
3772 | |
|
3773 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3774 | |
|
3775 | 0 | nfsfh = data->filler.blob0.val; |
3776 | |
|
3777 | 0 | if (check_nfs4_error(nfs, status, data, res, "READ")) { |
3778 | 0 | return; |
3779 | 0 | } |
3780 | | |
3781 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_READ, "READ")) < 0) { |
3782 | 0 | return; |
3783 | 0 | } |
3784 | 0 | rres = &res->resarray.resarray_val[i].nfs_resop4_u.opread.READ4res_u.resok4; |
3785 | |
|
3786 | 0 | count = rres->data.data_len; |
3787 | 0 | if (count > rpc->pdu->requested_read_count) { |
3788 | 0 | count = rpc->pdu->requested_read_count; |
3789 | 0 | } |
3790 | 0 | if (data->rw_data.update_pos) { |
3791 | 0 | nfsfh->offset = data->rw_data.offset + count; |
3792 | 0 | } |
3793 | |
|
3794 | 0 | data->cb(count, nfs, NULL, data->private_data); |
3795 | 0 | free_nfs4_cb_data(data); |
3796 | 0 | } |
3797 | | |
3798 | | int |
3799 | | nfs4_pread_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh, |
3800 | | void *buf, size_t count, uint64_t offset, |
3801 | | nfs_cb cb, void *private_data, int update_pos) |
3802 | 0 | { |
3803 | 0 | COMPOUND4args args; |
3804 | 0 | nfs_argop4 op[2 + 1]; |
3805 | 0 | struct nfs4_cb_data *data; |
3806 | 0 | int i; |
3807 | 0 | struct rpc_pdu *pdu; |
3808 | |
|
3809 | 0 | data = calloc(1, sizeof(*data)); |
3810 | 0 | if (data == NULL) { |
3811 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
3812 | 0 | "cb data"); |
3813 | 0 | return -1; |
3814 | 0 | } |
3815 | | |
3816 | 0 | data->nfs = nfs; |
3817 | 0 | data->cb = cb; |
3818 | 0 | data->private_data = private_data; |
3819 | |
|
3820 | 0 | data->filler.blob0.val = nfsfh; |
3821 | 0 | data->filler.blob0.free = NULL; |
3822 | 0 | data->rw_data.offset = offset; |
3823 | 0 | data->rw_data.update_pos = update_pos; |
3824 | | |
3825 | 0 | memset(op, 0, sizeof(op)); |
3826 | |
|
3827 | 0 | i = nfs4_start_compound(nfs, op); |
3828 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
3829 | 0 | i += nfs4_op_read(nfs, &op[i], nfsfh, offset, count); |
3830 | |
|
3831 | 0 | memset(&args, 0, sizeof(args)); |
3832 | 0 | args.argarray.argarray_len = i; |
3833 | 0 | args.argarray.argarray_val = op; |
3834 | |
|
3835 | 0 | pdu = rpc_nfs4_read_task(nfs->rpc, nfs4_pread_cb, buf, count, &args, |
3836 | 0 | data); |
3837 | 0 | if (pdu == NULL) { |
3838 | 0 | nfs_set_error(nfs, "Failed to queue READ. %s", |
3839 | 0 | rpc_get_error(nfs->rpc)); |
3840 | 0 | free_nfs4_cb_data(data); |
3841 | 0 | return -1; |
3842 | 0 | } |
3843 | | |
3844 | 0 | return 0; |
3845 | 0 | } |
3846 | | |
3847 | | int |
3848 | | nfs4_preadv_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh, |
3849 | | const struct iovec *iov, int iovcnt, uint64_t offset, |
3850 | | nfs_cb cb, void *private_data, int update_pos) |
3851 | 0 | { |
3852 | 0 | COMPOUND4args args; |
3853 | 0 | nfs_argop4 op[2 + 1]; |
3854 | 0 | struct nfs4_cb_data *data; |
3855 | 0 | int i; |
3856 | 0 | struct rpc_pdu *pdu; |
3857 | 0 | size_t count = 0; |
3858 | | |
3859 | 0 | for (i = 0; i < iovcnt; i++) { |
3860 | 0 | count += iov[i].iov_len; |
3861 | 0 | } |
3862 | | |
3863 | 0 | data = calloc(1, sizeof(*data)); |
3864 | 0 | if (data == NULL) { |
3865 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
3866 | 0 | "cb data"); |
3867 | 0 | return -1; |
3868 | 0 | } |
3869 | | |
3870 | 0 | data->nfs = nfs; |
3871 | 0 | data->cb = cb; |
3872 | 0 | data->private_data = private_data; |
3873 | |
|
3874 | 0 | data->filler.blob0.val = nfsfh; |
3875 | 0 | data->filler.blob0.free = NULL; |
3876 | 0 | data->rw_data.offset = offset; |
3877 | 0 | data->rw_data.update_pos = update_pos; |
3878 | | |
3879 | 0 | memset(op, 0, sizeof(op)); |
3880 | |
|
3881 | 0 | i = nfs4_start_compound(nfs, op); |
3882 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
3883 | 0 | i += nfs4_op_read(nfs, &op[i], nfsfh, offset, count); |
3884 | |
|
3885 | 0 | memset(&args, 0, sizeof(args)); |
3886 | 0 | args.argarray.argarray_len = i; |
3887 | 0 | args.argarray.argarray_val = op; |
3888 | |
|
3889 | 0 | pdu = rpc_nfs4_readv_task(nfs->rpc, nfs4_pread_cb, iov, iovcnt, &args, |
3890 | 0 | data); |
3891 | 0 | if (pdu == NULL) { |
3892 | 0 | nfs_set_error(nfs, "Failed to queue READ. %s", |
3893 | 0 | rpc_get_error(nfs->rpc)); |
3894 | 0 | free_nfs4_cb_data(data); |
3895 | 0 | return -1; |
3896 | 0 | } |
3897 | | |
3898 | 0 | return 0; |
3899 | 0 | } |
3900 | | |
3901 | | static void |
3902 | | nfs4_symlink_cb(struct rpc_context *rpc, int status, void *command_data, |
3903 | | void *private_data) |
3904 | 0 | { |
3905 | 0 | struct nfs4_cb_data *data = private_data; |
3906 | 0 | struct nfs_context *nfs = data->nfs; |
3907 | 0 | COMPOUND4res *res = command_data; |
3908 | |
|
3909 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3910 | |
|
3911 | 0 | if (check_nfs4_error(nfs, status, data, res, "SYMLINK")) { |
3912 | 0 | return; |
3913 | 0 | } |
3914 | | |
3915 | 0 | data->cb(0, nfs, NULL, data->private_data); |
3916 | 0 | free_nfs4_cb_data(data); |
3917 | 0 | } |
3918 | | |
3919 | | /* Takes object name as filler.data |
3920 | | * blob0 as the target |
3921 | | */ |
3922 | | static int |
3923 | | nfs4_populate_symlink(struct nfs4_cb_data *data, nfs_argop4 *op) |
3924 | 0 | { |
3925 | 0 | struct nfs_context *nfs = data->nfs; |
3926 | |
|
3927 | 0 | return nfs4_op_create(nfs, op, data->filler.data, NF4LNK, |
3928 | 0 | NULL, NULL, data->filler.blob0.val, 0); |
3929 | | |
3930 | 0 | return 1; |
3931 | 0 | } |
3932 | | |
3933 | | int |
3934 | | nfs4_symlink_async(struct nfs_context *nfs, const char *target, |
3935 | | const char *linkname, nfs_cb cb, void *private_data) |
3936 | 0 | { |
3937 | 0 | struct nfs4_cb_data *data; |
3938 | |
|
3939 | 0 | data = init_cb_data_split_path(nfs, linkname); |
3940 | 0 | if (data == NULL) { |
3941 | 0 | return -1; |
3942 | 0 | } |
3943 | | |
3944 | 0 | data->cb = cb; |
3945 | 0 | data->private_data = private_data; |
3946 | 0 | data->filler.func = nfs4_populate_symlink; |
3947 | 0 | data->filler.max_op = 1; |
3948 | |
|
3949 | 0 | data->filler.blob0.val = strdup(target); |
3950 | 0 | data->filler.blob0.free = free; |
3951 | |
|
3952 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_symlink_cb) < 0) { |
3953 | 0 | free_nfs4_cb_data(data); |
3954 | 0 | return -1; |
3955 | 0 | } |
3956 | | |
3957 | 0 | return 0; |
3958 | 0 | } |
3959 | | |
3960 | | static void |
3961 | | nfs4_readlink_cb(struct rpc_context *rpc, int status, void *command_data, |
3962 | | void *private_data) |
3963 | 0 | { |
3964 | 0 | struct nfs4_cb_data *data = private_data; |
3965 | 0 | struct nfs_context *nfs = data->nfs; |
3966 | 0 | COMPOUND4res *res = command_data; |
3967 | 0 | READLINK4resok *rlresok; |
3968 | 0 | char* target; |
3969 | 0 | int i; |
3970 | |
|
3971 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
3972 | |
|
3973 | 0 | if (check_nfs4_error(nfs, status, data, res, "READLINK")) { |
3974 | 0 | return; |
3975 | 0 | } |
3976 | | |
3977 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_READLINK, "READLINK")) < 0) { |
3978 | 0 | return; |
3979 | 0 | } |
3980 | | |
3981 | 0 | rlresok = &res->resarray.resarray_val[i].nfs_resop4_u.opreadlink.READLINK4res_u.resok4; |
3982 | |
|
3983 | 0 | if (rlresok->link.utf8string_len == 0) { |
3984 | 0 | target = strdup(""); |
3985 | 0 | } else { |
3986 | 0 | target = strndup(rlresok->link.utf8string_val, |
3987 | 0 | rlresok->link.utf8string_len); |
3988 | 0 | } |
3989 | 0 | if (target == NULL) { |
3990 | 0 | data->cb(-ENOMEM, nfs, "Failed to allocate memory", |
3991 | 0 | data->private_data); |
3992 | 0 | free_nfs4_cb_data(data); |
3993 | 0 | return; |
3994 | 0 | } |
3995 | 0 | data->filler.blob0.val = target; |
3996 | 0 | data->filler.blob0.free = free; |
3997 | 0 | data->cb(0, nfs, target, data->private_data); |
3998 | 0 | free_nfs4_cb_data(data); |
3999 | 0 | } |
4000 | | |
4001 | | static int |
4002 | | nfs4_populate_readlink(struct nfs4_cb_data *data, nfs_argop4 *op) |
4003 | 0 | { |
4004 | 0 | struct nfs_context *nfs = data->nfs; |
4005 | 0 | int i; |
4006 | |
|
4007 | 0 | i = nfs4_op_readlink(nfs, &op[0]); |
4008 | |
|
4009 | 0 | return i; |
4010 | 0 | } |
4011 | | |
4012 | | int |
4013 | | nfs4_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, |
4014 | | void *private_data) |
4015 | 0 | { |
4016 | 0 | struct nfs4_cb_data *data; |
4017 | |
|
4018 | 0 | data = init_cb_data_full_path(nfs, path); |
4019 | 0 | if (data == NULL) { |
4020 | 0 | return -1; |
4021 | 0 | } |
4022 | | |
4023 | 0 | data->cb = cb; |
4024 | 0 | data->private_data = private_data; |
4025 | 0 | data->filler.func = nfs4_populate_readlink; |
4026 | 0 | data->filler.max_op = 1; |
4027 | 0 | data->flags |= LOOKUP_FLAG_NO_FOLLOW; |
4028 | |
|
4029 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_readlink_cb) < 0) { |
4030 | 0 | free_nfs4_cb_data(data); |
4031 | 0 | return -1; |
4032 | 0 | } |
4033 | | |
4034 | 0 | return 0; |
4035 | 0 | } |
4036 | | |
4037 | | static void |
4038 | | nfs4_pwrite_cb(struct rpc_context *rpc, int status, void *command_data, |
4039 | | void *private_data) |
4040 | 0 | { |
4041 | 0 | struct nfs4_cb_data *data = private_data; |
4042 | 0 | struct nfs_context *nfs = data->nfs; |
4043 | 0 | COMPOUND4res *res = command_data; |
4044 | 0 | WRITE4resok *wres = NULL; |
4045 | 0 | struct nfsfh *nfsfh; |
4046 | 0 | uint32_t count; |
4047 | 0 | int i; |
4048 | |
|
4049 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4050 | |
|
4051 | 0 | nfsfh = data->filler.blob0.val; |
4052 | |
|
4053 | 0 | if (check_nfs4_error(nfs, status, data, res, "WRITE")) { |
4054 | 0 | return; |
4055 | 0 | } |
4056 | | |
4057 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_WRITE, "WRITE")) < 0) { |
4058 | 0 | return; |
4059 | 0 | } |
4060 | 0 | wres = &res->resarray.resarray_val[i].nfs_resop4_u.opwrite.WRITE4res_u.resok4; |
4061 | |
|
4062 | 0 | count = wres->count; |
4063 | 0 | if (count > data->rw_data.count) { |
4064 | 0 | count = data->rw_data.count; |
4065 | 0 | } |
4066 | 0 | if (data->rw_data.update_pos) { |
4067 | 0 | nfsfh->offset = data->rw_data.offset + count; |
4068 | 0 | } |
4069 | |
|
4070 | 0 | data->cb(count, nfs, NULL, data->private_data); |
4071 | 0 | free_nfs4_cb_data(data); |
4072 | 0 | } |
4073 | | |
4074 | | int |
4075 | | nfs4_pwrite_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh, |
4076 | | const void *buf, size_t count, uint64_t offset, |
4077 | | nfs_cb cb, void *private_data, int update_pos) |
4078 | 0 | { |
4079 | 0 | COMPOUND4args args; |
4080 | 0 | nfs_argop4 op[2 + 1]; |
4081 | 0 | struct nfs4_cb_data *data; |
4082 | 0 | int i; |
4083 | |
|
4084 | 0 | data = calloc(1, sizeof(*data)); |
4085 | 0 | if (data == NULL) { |
4086 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
4087 | 0 | "cb data"); |
4088 | 0 | return -1; |
4089 | 0 | } |
4090 | | |
4091 | 0 | data->nfs = nfs; |
4092 | 0 | data->cb = cb; |
4093 | 0 | data->private_data = private_data; |
4094 | |
|
4095 | 0 | data->filler.blob0.val = nfsfh; |
4096 | 0 | data->filler.blob0.free = NULL; |
4097 | 0 | data->rw_data.offset = offset; |
4098 | 0 | data->rw_data.count = (uint32_t)count; |
4099 | 0 | data->rw_data.update_pos = update_pos; |
4100 | |
|
4101 | 0 | memset(op, 0, sizeof(op)); |
4102 | |
|
4103 | 0 | i = nfs4_start_compound(nfs, op); |
4104 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
4105 | 0 | i += nfs4_op_write(nfs, &op[i], nfsfh, offset, count, buf); |
4106 | |
|
4107 | 0 | memset(&args, 0, sizeof(args)); |
4108 | 0 | args.argarray.argarray_len = i; |
4109 | 0 | args.argarray.argarray_val = op; |
4110 | |
|
4111 | 0 | if (rpc_nfs4_write_task(nfs->rpc, nfs4_pwrite_cb, buf, count, |
4112 | 0 | &args, data) == NULL) { |
4113 | 0 | nfs_set_error(nfs, "PWRITE " |
4114 | 0 | "failed: %s", rpc_get_error(nfs->rpc)); |
4115 | 0 | free_nfs4_cb_data(data); |
4116 | 0 | return -EIO; |
4117 | 0 | } |
4118 | | |
4119 | 0 | return 0; |
4120 | 0 | } |
4121 | | |
4122 | | static void |
4123 | | nfs4_write_append_cb(struct rpc_context *rpc, int status, void *command_data, |
4124 | | void *private_data) |
4125 | 0 | { |
4126 | 0 | struct nfs4_cb_data *data = private_data; |
4127 | 0 | struct nfs_context *nfs = data->nfs; |
4128 | 0 | COMPOUND4res *res = command_data; |
4129 | 0 | GETATTR4resok *garesok = NULL; |
4130 | 0 | struct nfsfh *nfsfh; |
4131 | 0 | int i; |
4132 | 0 | uint64_t offset; |
4133 | 0 | char *buf; |
4134 | 0 | uint32_t count; |
4135 | 0 | struct nfs_stat_64 st; |
4136 | |
|
4137 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4138 | |
|
4139 | 0 | nfsfh = data->filler.blob0.val; |
4140 | |
|
4141 | 0 | buf = data->filler.blob1.val; |
4142 | 0 | count = data->filler.blob1.len; |
4143 | |
|
4144 | 0 | if (check_nfs4_error(nfs, status, data, res, "GETATTR")) { |
4145 | 0 | return; |
4146 | 0 | } |
4147 | | |
4148 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_GETATTR, "GETATTR")) < 0) { |
4149 | 0 | return; |
4150 | 0 | } |
4151 | | |
4152 | | |
4153 | 0 | garesok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetattr.GETATTR4res_u.resok4; |
4154 | 0 | if (garesok->obj_attributes.attr_vals.attrlist4_len < 8) { |
4155 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
4156 | 0 | free_nfs4_cb_data(data); |
4157 | 0 | return; |
4158 | 0 | } |
4159 | | |
4160 | 0 | memset(&st, 0, sizeof(st)); |
4161 | 0 | nfs_parse_attributes(nfs, data, &st, NULL, NULL, |
4162 | 0 | &garesok->obj_attributes.attrmask, |
4163 | 0 | garesok->obj_attributes.attr_vals.attrlist4_val, |
4164 | 0 | garesok->obj_attributes.attr_vals.attrlist4_len); |
4165 | 0 | offset = st.nfs_size; |
4166 | |
|
4167 | 0 | if (nfs4_pwrite_async_internal(nfs, nfsfh, |
4168 | 0 | buf, (size_t)count, offset, |
4169 | 0 | data->cb, data->private_data, 1) < 0) { |
4170 | 0 | free_nfs4_cb_data(data); |
4171 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4172 | 0 | return; |
4173 | 0 | } |
4174 | | |
4175 | 0 | free_nfs4_cb_data(data); |
4176 | 0 | } |
4177 | | |
4178 | | int |
4179 | | nfs4_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count, |
4180 | | const void *buf, nfs_cb cb, void *private_data) |
4181 | 0 | { |
4182 | 0 | if (nfsfh->is_append) { |
4183 | 0 | COMPOUND4args args; |
4184 | 0 | nfs_argop4 op[2 + 1]; |
4185 | 0 | struct nfs4_cb_data *data; |
4186 | 0 | int i; |
4187 | |
|
4188 | 0 | data = calloc(1, sizeof(*data)); |
4189 | 0 | if (data == NULL) { |
4190 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
4191 | 0 | "cb data"); |
4192 | 0 | return -1; |
4193 | 0 | } |
4194 | | |
4195 | 0 | data->nfs = nfs; |
4196 | 0 | data->cb = cb; |
4197 | 0 | data->private_data = private_data; |
4198 | |
|
4199 | 0 | data->filler.blob0.val = nfsfh; |
4200 | 0 | data->filler.blob0.free = NULL; |
4201 | |
|
4202 | 0 | memset(op, 0, sizeof(op)); |
4203 | |
|
4204 | 0 | i = nfs4_start_compound(nfs, op); |
4205 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
4206 | 0 | i += nfs4_op_getattr(nfs, &op[i], standard_attributes, 2); |
4207 | |
|
4208 | 0 | memset(&args, 0, sizeof(args)); |
4209 | 0 | args.argarray.argarray_len = i; |
4210 | 0 | args.argarray.argarray_val = op; |
4211 | |
|
4212 | 0 | data->filler.blob0.val = nfsfh; |
4213 | 0 | data->filler.blob0.free = NULL; |
4214 | |
|
4215 | 0 | data->filler.blob1.val = discard_const(buf); |
4216 | 0 | data->filler.blob1.len = (int)count; |
4217 | 0 | data->filler.blob1.free = NULL; |
4218 | |
|
4219 | 0 | if (rpc_nfs4_compound_task2(nfs->rpc, nfs4_write_append_cb, |
4220 | 0 | &args, data, count) == NULL) { |
4221 | 0 | nfs_set_error(nfs, "PWRITE " |
4222 | 0 | "failed: %s", rpc_get_error(nfs->rpc)); |
4223 | 0 | free_nfs4_cb_data(data); |
4224 | 0 | return -EIO; |
4225 | 0 | } |
4226 | | |
4227 | 0 | return 0; |
4228 | 0 | } |
4229 | | |
4230 | 0 | return nfs4_pwrite_async_internal(nfs, nfsfh, |
4231 | 0 | buf, (size_t)count, nfsfh->offset, |
4232 | 0 | cb, private_data, 1); |
4233 | 0 | } |
4234 | | |
4235 | | int |
4236 | | nfs4_creat_async(struct nfs_context *nfs, const char *path, |
4237 | | int mode, nfs_cb cb, void *private_data) |
4238 | 0 | { |
4239 | 0 | return nfs4_open_async(nfs, path, O_CREAT|O_WRONLY|O_TRUNC, mode, |
4240 | 0 | cb, private_data); |
4241 | 0 | } |
4242 | | |
4243 | | int |
4244 | | nfs4_unlink_async(struct nfs_context *nfs, const char *path, |
4245 | | nfs_cb cb, void *private_data) |
4246 | 0 | { |
4247 | 0 | return nfs4_remove_async(nfs, path, cb, private_data); |
4248 | 0 | } |
4249 | | |
4250 | | static void |
4251 | | nfs4_link_2_cb(struct rpc_context *rpc, int status, void *command_data, |
4252 | | void *private_data) |
4253 | 0 | { |
4254 | 0 | struct nfs4_cb_data *data = private_data; |
4255 | 0 | struct nfs_context *nfs = data->nfs; |
4256 | 0 | COMPOUND4res *res = command_data; |
4257 | |
|
4258 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4259 | |
|
4260 | 0 | if (check_nfs4_error(nfs, status, data, res, "LINK")) { |
4261 | 0 | return; |
4262 | 0 | } |
4263 | | |
4264 | 0 | data->cb(0, nfs, NULL, data->private_data); |
4265 | 0 | free_nfs4_cb_data(data); |
4266 | 0 | } |
4267 | | |
4268 | | static int |
4269 | | nfs4_populate_link(struct nfs4_cb_data *data, nfs_argop4 *op) |
4270 | 0 | { |
4271 | 0 | struct nfs_context *nfs = data->nfs; |
4272 | 0 | struct nfsfh *nfsfh = data->filler.blob0.val; |
4273 | |
|
4274 | 0 | int i; |
4275 | |
|
4276 | 0 | i = nfs4_op_savefh(nfs, &op[0]); |
4277 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
4278 | 0 | i += nfs4_op_link(nfs, &op[i], data->filler.data); |
4279 | |
|
4280 | 0 | return i; |
4281 | 0 | } |
4282 | | |
4283 | | static void |
4284 | | nfs4_link_1_cb(struct rpc_context *rpc, int status, void *command_data, |
4285 | | void *private_data) |
4286 | 0 | { |
4287 | 0 | struct nfs4_cb_data *data = private_data; |
4288 | 0 | struct nfs_context *nfs = data->nfs; |
4289 | 0 | COMPOUND4res *res = command_data; |
4290 | 0 | GETFH4resok *gfhresok; |
4291 | 0 | struct nfsfh *fh; |
4292 | |
|
4293 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4294 | |
|
4295 | 0 | if (check_nfs4_error(nfs, status, data, res, "LINK")) { |
4296 | 0 | return; |
4297 | 0 | } |
4298 | | |
4299 | 0 | if ((gfhresok = nfs4_find_getfh(nfs, data, res)) == NULL) { |
4300 | 0 | return; |
4301 | 0 | } |
4302 | | |
4303 | | /* oldpath fh */ |
4304 | 0 | fh = calloc(1, sizeof(*fh)); |
4305 | 0 | if (fh == NULL) { |
4306 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
4307 | 0 | "nfsfh"); |
4308 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4309 | 0 | free_nfs4_cb_data(data); |
4310 | 0 | return; |
4311 | 0 | } |
4312 | 0 | data->filler.blob0.val = fh; |
4313 | 0 | data->filler.blob0.free = (blob_free)nfs_free_nfsfh; |
4314 | |
|
4315 | 0 | fh->fh.len = gfhresok->object.nfs_fh4_len; |
4316 | 0 | fh->fh.val = malloc(fh->fh.len); |
4317 | 0 | if (fh->fh.val == NULL) { |
4318 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
4319 | 0 | "nfsfh"); |
4320 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4321 | 0 | free_nfs4_cb_data(data); |
4322 | 0 | return; |
4323 | 0 | } |
4324 | 0 | memcpy(fh->fh.val, gfhresok->object.nfs_fh4_val, fh->fh.len); |
4325 | | |
4326 | |
|
4327 | 0 | data->filler.func = nfs4_populate_link; |
4328 | 0 | data->filler.max_op = 3; |
4329 | |
|
4330 | 0 | free(data->path); |
4331 | 0 | data->path = data->filler.blob1.val; |
4332 | 0 | data->filler.blob1.val = NULL; |
4333 | 0 | data->filler.blob1.free = NULL; |
4334 | |
|
4335 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_link_2_cb) < 0) { |
4336 | 0 | data->cb(-EFAULT, nfs, nfs_get_error(nfs), data->private_data); |
4337 | 0 | free_nfs4_cb_data(data); |
4338 | 0 | return; |
4339 | 0 | } |
4340 | 0 | } |
4341 | | |
4342 | | /* |
4343 | | * filler.data is the name of the new object |
4344 | | * blob0 is the filehandle for newpath parent directory. |
4345 | | * blob1 is oldpath. |
4346 | | */ |
4347 | | int |
4348 | | nfs4_link_async(struct nfs_context *nfs, const char *oldpath, |
4349 | | const char *newpath, nfs_cb cb, void *private_data) |
4350 | 0 | { |
4351 | 0 | struct nfs4_cb_data *data; |
4352 | |
|
4353 | 0 | data = init_cb_data_split_path(nfs, newpath); |
4354 | 0 | if (data == NULL) { |
4355 | 0 | return -1; |
4356 | 0 | } |
4357 | | |
4358 | 0 | data->cb = cb; |
4359 | 0 | data->private_data = private_data; |
4360 | 0 | data->filler.func = nfs4_populate_getfh; |
4361 | 0 | data->filler.max_op = 1; |
4362 | | |
4363 | | /* oldpath */ |
4364 | 0 | data->filler.blob1.val = strdup(oldpath); |
4365 | 0 | if (data->filler.blob1.val == NULL) { |
4366 | 0 | nfs_set_error(nfs, "Out of memory"); |
4367 | 0 | free_nfs4_cb_data(data); |
4368 | 0 | return -1; |
4369 | 0 | } |
4370 | 0 | data->filler.blob1.free = free; |
4371 | |
|
4372 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_link_1_cb) < 0) { |
4373 | 0 | free_nfs4_cb_data(data); |
4374 | 0 | return -1; |
4375 | 0 | } |
4376 | | |
4377 | 0 | return 0; |
4378 | 0 | } |
4379 | | |
4380 | | static void |
4381 | | nfs4_rename_2_cb(struct rpc_context *rpc, int status, void *command_data, |
4382 | | void *private_data) |
4383 | 0 | { |
4384 | 0 | struct nfs4_cb_data *data = private_data; |
4385 | 0 | struct nfs_context *nfs = data->nfs; |
4386 | 0 | COMPOUND4res *res = command_data; |
4387 | |
|
4388 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4389 | |
|
4390 | 0 | if (check_nfs4_error(nfs, status, data, res, "RENAME")) { |
4391 | 0 | return; |
4392 | 0 | } |
4393 | | |
4394 | 0 | data->cb(0, nfs, NULL, data->private_data); |
4395 | 0 | free_nfs4_cb_data(data); |
4396 | 0 | } |
4397 | | |
4398 | | static int |
4399 | | nfs4_populate_rename(struct nfs4_cb_data *data, nfs_argop4 *op) |
4400 | 0 | { |
4401 | 0 | struct nfs_context *nfs = data->nfs; |
4402 | 0 | struct nfsfh *nfsfh = data->filler.blob0.val; |
4403 | 0 | int i; |
4404 | |
|
4405 | 0 | i = nfs4_op_savefh(nfs, &op[0]); |
4406 | 0 | i += nfs4_op_putfh(nfs, &op[i], nfsfh); |
4407 | 0 | i += nfs4_op_rename(nfs, &op[i], data->filler.data, |
4408 | 0 | data->filler.blob1.val); |
4409 | |
|
4410 | 0 | return i; |
4411 | 0 | } |
4412 | | |
4413 | | static void |
4414 | | nfs4_rename_1_cb(struct rpc_context *rpc, int status, void *command_data, |
4415 | | void *private_data) |
4416 | 0 | { |
4417 | 0 | struct nfs4_cb_data *data = private_data; |
4418 | 0 | struct nfs_context *nfs = data->nfs; |
4419 | 0 | COMPOUND4res *res = command_data; |
4420 | 0 | GETFH4resok *gfhresok; |
4421 | 0 | struct nfsfh *fh; |
4422 | |
|
4423 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4424 | |
|
4425 | 0 | if (check_nfs4_error(nfs, status, data, res, "RENAME")) { |
4426 | 0 | return; |
4427 | 0 | } |
4428 | | |
4429 | 0 | if ((gfhresok = nfs4_find_getfh(nfs, data, res)) == NULL) { |
4430 | 0 | return; |
4431 | 0 | } |
4432 | | |
4433 | | /* newpath fh */ |
4434 | 0 | fh = calloc(1, sizeof(*fh)); |
4435 | 0 | if (fh == NULL) { |
4436 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
4437 | 0 | "nfsfh"); |
4438 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4439 | 0 | free_nfs4_cb_data(data); |
4440 | 0 | return; |
4441 | 0 | } |
4442 | 0 | data->filler.blob0.val = fh; |
4443 | 0 | data->filler.blob0.free = (blob_free)nfs_free_nfsfh; |
4444 | |
|
4445 | 0 | fh->fh.len = gfhresok->object.nfs_fh4_len; |
4446 | 0 | fh->fh.val = malloc(fh->fh.len); |
4447 | 0 | if (fh->fh.val == NULL) { |
4448 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
4449 | 0 | "nfsfh"); |
4450 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4451 | 0 | free_nfs4_cb_data(data); |
4452 | 0 | return; |
4453 | 0 | } |
4454 | 0 | memcpy(fh->fh.val, gfhresok->object.nfs_fh4_val, fh->fh.len); |
4455 | |
|
4456 | 0 | data->filler.blob1.val = data->filler.data; |
4457 | 0 | data->filler.blob1.free = free; |
4458 | 0 | data->filler.data = NULL; |
4459 | | |
4460 | | /* Update path and data to point to the old path/name */ |
4461 | 0 | free(data->path); |
4462 | 0 | data->path = nfs4_resolve_path(nfs, data->filler.blob2.val); |
4463 | 0 | if (data->path == NULL) { |
4464 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
4465 | 0 | free_nfs4_cb_data(data); |
4466 | 0 | return; |
4467 | 0 | } |
4468 | 0 | data_split_path(data); |
4469 | |
|
4470 | 0 | data->filler.func = nfs4_populate_rename; |
4471 | 0 | data->filler.max_op = 3; |
4472 | |
|
4473 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_rename_2_cb) < 0) { |
4474 | 0 | nfs_set_error(nfs, "Out of memory."); |
4475 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4476 | 0 | free_nfs4_cb_data(data); |
4477 | 0 | return; |
4478 | 0 | } |
4479 | 0 | } |
4480 | | |
4481 | | /* |
4482 | | * blob0 is the filehandle for newpath parent directory. |
4483 | | * blob1 is the new name |
4484 | | * blob2 is oldpath. |
4485 | | */ |
4486 | | int |
4487 | | nfs4_rename_async(struct nfs_context *nfs, const char *oldpath, |
4488 | | const char *newpath, nfs_cb cb, void *private_data) |
4489 | 0 | { |
4490 | 0 | struct nfs4_cb_data *data; |
4491 | |
|
4492 | 0 | data = init_cb_data_split_path(nfs, newpath); |
4493 | 0 | if (data == NULL) { |
4494 | 0 | return -1; |
4495 | 0 | } |
4496 | | |
4497 | 0 | data->cb = cb; |
4498 | 0 | data->private_data = private_data; |
4499 | 0 | data->filler.func = nfs4_populate_getfh; |
4500 | 0 | data->filler.max_op = 1; |
4501 | | |
4502 | | /* oldpath */ |
4503 | 0 | data->filler.blob2.val = strdup(oldpath); |
4504 | 0 | if (data->filler.blob2.val == NULL) { |
4505 | 0 | nfs_set_error(nfs, "Out of memory"); |
4506 | 0 | free_nfs4_cb_data(data); |
4507 | 0 | return -1; |
4508 | 0 | } |
4509 | 0 | data->filler.blob2.free = free; |
4510 | |
|
4511 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_rename_1_cb) < 0) { |
4512 | 0 | free_nfs4_cb_data(data); |
4513 | 0 | return -1; |
4514 | 0 | } |
4515 | | |
4516 | 0 | return 0; |
4517 | 0 | } |
4518 | | |
4519 | | static void |
4520 | | nfs4_mknod_cb(struct rpc_context *rpc, int status, void *command_data, |
4521 | | void *private_data) |
4522 | 0 | { |
4523 | 0 | struct nfs4_cb_data *data = private_data; |
4524 | 0 | struct nfs_context *nfs = data->nfs; |
4525 | 0 | COMPOUND4res *res = command_data; |
4526 | |
|
4527 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4528 | |
|
4529 | 0 | if (check_nfs4_error(nfs, status, data, res, "MKNOD")) { |
4530 | 0 | return; |
4531 | 0 | } |
4532 | | |
4533 | 0 | data->cb(0, nfs, NULL, data->private_data); |
4534 | 0 | free_nfs4_cb_data(data); |
4535 | 0 | } |
4536 | | |
4537 | | static int |
4538 | | nfs4_populate_mknod(struct nfs4_cb_data *data, nfs_argop4 *op) |
4539 | 0 | { |
4540 | 0 | struct nfs_context *nfs = data->nfs; |
4541 | 0 | uint32_t mode, *ptr; |
4542 | 0 | int dev; |
4543 | | |
4544 | | /* Strip off the file type before we marshall it */ |
4545 | 0 | ptr = (void *)data->filler.blob1.val; |
4546 | 0 | mode = *ptr; |
4547 | 0 | *ptr = htonl(mode & ~S_IFMT); |
4548 | |
|
4549 | 0 | dev = data->filler.blob2.len; |
4550 | |
|
4551 | 0 | switch (mode & S_IFMT) { |
4552 | 0 | case S_IFBLK: |
4553 | 0 | return nfs4_op_create(nfs, op, data->filler.data, NF4BLK, |
4554 | 0 | &data->filler.blob0, &data->filler.blob1, |
4555 | 0 | NULL, dev); |
4556 | 0 | case S_IFCHR: |
4557 | 0 | return nfs4_op_create(nfs, op, data->filler.data, NF4CHR, |
4558 | 0 | &data->filler.blob0, &data->filler.blob1, |
4559 | 0 | NULL, dev); |
4560 | 0 | } |
4561 | | |
4562 | 0 | return 1; |
4563 | 0 | } |
4564 | | |
4565 | | /* Takes object name as filler.data |
4566 | | * blob0 as attribute mask |
4567 | | * blob1 as attribute value |
4568 | | * blob2.len as dev |
4569 | | */ |
4570 | | int |
4571 | | nfs4_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev, |
4572 | | nfs_cb cb, void *private_data) |
4573 | 0 | { |
4574 | 0 | struct nfs4_cb_data *data; |
4575 | 0 | uint32_t *u32ptr; |
4576 | |
|
4577 | 0 | switch (mode & S_IFMT) { |
4578 | 0 | case S_IFCHR: |
4579 | 0 | case S_IFBLK: |
4580 | 0 | break; |
4581 | 0 | default: |
4582 | 0 | nfs_set_error(nfs, "Invalid file type for " |
4583 | 0 | "MKNOD call"); |
4584 | 0 | return -1; |
4585 | 0 | } |
4586 | | |
4587 | 0 | data = init_cb_data_split_path(nfs, path); |
4588 | 0 | if (data == NULL) { |
4589 | 0 | return -1; |
4590 | 0 | } |
4591 | | |
4592 | 0 | data->cb = cb; |
4593 | 0 | data->private_data = private_data; |
4594 | 0 | data->filler.func = nfs4_populate_mknod; |
4595 | 0 | data->filler.max_op = 1; |
4596 | | |
4597 | | /* attribute mask */ |
4598 | 0 | u32ptr = malloc(2 * sizeof(uint32_t)); |
4599 | 0 | if (u32ptr == NULL) { |
4600 | 0 | nfs_set_error(nfs, "Out of memory allocating bitmap"); |
4601 | 0 | return 0; |
4602 | 0 | } |
4603 | 0 | u32ptr[0] = 0; |
4604 | 0 | u32ptr[1] = 1 << (FATTR4_MODE - 32); |
4605 | 0 | data->filler.blob0.len = 2; |
4606 | 0 | data->filler.blob0.val = u32ptr; |
4607 | 0 | data->filler.blob0.free = free; |
4608 | | |
4609 | | /* attribute values */ |
4610 | 0 | u32ptr = malloc(1 * sizeof(uint32_t)); |
4611 | 0 | if (u32ptr == NULL) { |
4612 | 0 | nfs_set_error(nfs, "Out of memory allocating attributes"); |
4613 | 0 | free_nfs4_cb_data(data); |
4614 | 0 | return -1; |
4615 | 0 | } |
4616 | 0 | u32ptr[0] = mode; |
4617 | 0 | data->filler.blob1.len = 4; |
4618 | 0 | data->filler.blob1.val = u32ptr; |
4619 | 0 | data->filler.blob1.free = free; |
4620 | |
|
4621 | 0 | data->filler.blob2.len = dev; |
4622 | |
|
4623 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_mknod_cb) < 0) { |
4624 | 0 | free_nfs4_cb_data(data); |
4625 | 0 | return -1; |
4626 | 0 | } |
4627 | | |
4628 | 0 | return 0; |
4629 | 0 | } |
4630 | | |
4631 | | static void |
4632 | | nfs4_parse_readdir(struct nfs_context *nfs, struct nfs4_cb_data *data, |
4633 | | READDIR4resok *res); |
4634 | | |
4635 | | static void |
4636 | | nfs4_opendir_2_cb(struct rpc_context *rpc, int status, void *command_data, |
4637 | | void *private_data) |
4638 | 0 | { |
4639 | 0 | struct nfs4_cb_data *data = private_data; |
4640 | 0 | struct nfs_context *nfs = data->nfs; |
4641 | 0 | COMPOUND4res *res = command_data; |
4642 | 0 | READDIR4resok *rdresok; |
4643 | 0 | int i; |
4644 | |
|
4645 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4646 | |
|
4647 | 0 | if (check_nfs4_error(nfs, status, data, res, "READDIR")) { |
4648 | 0 | return; |
4649 | 0 | } |
4650 | | |
4651 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_READDIR, "READDIR")) < 0) { |
4652 | 0 | return; |
4653 | 0 | } |
4654 | 0 | rdresok = &res->resarray.resarray_val[i].nfs_resop4_u.opreaddir.READDIR4res_u.resok4; |
4655 | 0 | nfs4_parse_readdir(nfs, data, rdresok); |
4656 | 0 | } |
4657 | | |
4658 | | static void |
4659 | | nfs4_opendir_continue(struct nfs_context *nfs, struct nfs4_cb_data *data) |
4660 | 0 | { |
4661 | 0 | COMPOUND4args args; |
4662 | 0 | nfs_argop4 op[2 + 1]; |
4663 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
4664 | 0 | uint64_t cookie; |
4665 | 0 | int i; |
4666 | |
|
4667 | 0 | memcpy(&cookie, data->filler.blob2.val, sizeof(uint64_t)); |
4668 | |
|
4669 | 0 | memset(op, 0, sizeof(op)); |
4670 | |
|
4671 | 0 | i = nfs4_start_compound(nfs, op); |
4672 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
4673 | 0 | i += nfs4_op_readdir(nfs, &op[i], cookie); |
4674 | |
|
4675 | 0 | memset(&args, 0, sizeof(args)); |
4676 | 0 | args.argarray.argarray_len = i; |
4677 | 0 | args.argarray.argarray_val = op; |
4678 | |
|
4679 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_opendir_2_cb, &args, |
4680 | 0 | data) == NULL) { |
4681 | 0 | nfs_set_error(nfs, "Failed to queue READDIR command. %s", |
4682 | 0 | rpc_get_error(nfs->rpc)); |
4683 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4684 | 0 | free_nfs4_cb_data(data); |
4685 | 0 | return; |
4686 | 0 | } |
4687 | 0 | } |
4688 | | |
4689 | | static void |
4690 | | nfs4_parse_readdir(struct nfs_context *nfs, struct nfs4_cb_data *data, |
4691 | | READDIR4resok *res) |
4692 | 0 | { |
4693 | 0 | struct nfsdir *nfsdir = data->filler.blob1.val; |
4694 | 0 | struct entry4 *e; |
4695 | 0 | uint64_t cookie; |
4696 | | |
4697 | | /* The cookie this reply is answering. */ |
4698 | 0 | memcpy(&cookie, data->filler.blob2.val, sizeof(uint64_t)); |
4699 | |
|
4700 | 0 | e = res->reply.entries; |
4701 | 0 | while (e) { |
4702 | 0 | struct nfsdirent *nfsdirent; |
4703 | 0 | struct nfs_stat_64 st; |
4704 | |
|
4705 | 0 | memcpy(data->filler.blob2.val, &e->cookie, sizeof(uint64_t)); |
4706 | |
|
4707 | 0 | nfsdirent = malloc(sizeof(struct nfsdirent)); |
4708 | 0 | if (nfsdirent == NULL) { |
4709 | 0 | nfs_set_error(nfs, "Out of memory."); |
4710 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
4711 | 0 | data->private_data); |
4712 | 0 | free_nfs4_cb_data(data); |
4713 | 0 | return; |
4714 | 0 | } |
4715 | | /* |
4716 | | * component4 is decoded with zdr_bytes, so the value points |
4717 | | * into the receive buffer with a separate length and no |
4718 | | * terminator. strdup() would run on into whatever follows the |
4719 | | * name in the reply. Every other user of a utf8string in this |
4720 | | * tree already respects the length; this one did not. |
4721 | | */ |
4722 | 0 | if (e->name.utf8string_len == 0) { |
4723 | 0 | nfsdirent->name = strdup(""); |
4724 | 0 | } else { |
4725 | 0 | nfsdirent->name = strndup(e->name.utf8string_val, |
4726 | 0 | e->name.utf8string_len); |
4727 | 0 | } |
4728 | 0 | if (nfsdirent->name == NULL) { |
4729 | 0 | nfs_set_error(nfs, "Out of memory."); |
4730 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
4731 | 0 | data->private_data); |
4732 | 0 | free_nfs4_cb_data(data); |
4733 | 0 | free(nfsdirent); |
4734 | 0 | return; |
4735 | 0 | } |
4736 | | |
4737 | 0 | memset(&st, 0, sizeof(st)); |
4738 | 0 | if (nfs_parse_attributes(nfs, data, &st, NULL, NULL, |
4739 | 0 | &e->attrs.attrmask, |
4740 | 0 | e->attrs.attr_vals.attrlist4_val, |
4741 | 0 | e->attrs.attr_vals.attrlist4_len) < 0) { |
4742 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), |
4743 | 0 | data->private_data); |
4744 | 0 | free_nfs4_cb_data(data); |
4745 | 0 | free(nfsdirent->name); |
4746 | 0 | free(nfsdirent); |
4747 | 0 | return; |
4748 | 0 | } |
4749 | | |
4750 | 0 | nfsdirent->inode = st.nfs_ino; |
4751 | 0 | nfsdirent->mode = (uint32_t)st.nfs_mode; |
4752 | 0 | switch (st.nfs_mode & S_IFMT) { |
4753 | 0 | case S_IFREG: |
4754 | 0 | nfsdirent->type = NF4REG; |
4755 | 0 | break; |
4756 | 0 | case S_IFDIR: |
4757 | 0 | nfsdirent->type = NF4DIR; |
4758 | 0 | break; |
4759 | 0 | case S_IFBLK: |
4760 | 0 | nfsdirent->type = NF4BLK; |
4761 | 0 | break; |
4762 | 0 | case S_IFCHR: |
4763 | 0 | nfsdirent->type = NF4CHR; |
4764 | 0 | break; |
4765 | 0 | case S_IFLNK: |
4766 | 0 | nfsdirent->type = NF4LNK; |
4767 | 0 | break; |
4768 | 0 | case S_IFSOCK: |
4769 | 0 | nfsdirent->type = NF4SOCK; |
4770 | 0 | break; |
4771 | 0 | case S_IFIFO: |
4772 | 0 | nfsdirent->type = NF4FIFO; |
4773 | 0 | break; |
4774 | 0 | } |
4775 | 0 | nfsdirent->size = st.nfs_size; |
4776 | 0 | nfsdirent->atime.tv_sec = (long)st.nfs_atime; |
4777 | 0 | nfsdirent->atime.tv_usec = (long)(st.nfs_atime_nsec/1000); |
4778 | 0 | nfsdirent->atime_nsec = (uint32_t)st.nfs_atime_nsec; |
4779 | 0 | nfsdirent->mtime.tv_sec = (long)st.nfs_mtime; |
4780 | 0 | nfsdirent->mtime.tv_usec = (long)(st.nfs_mtime_nsec/1000); |
4781 | 0 | nfsdirent->mtime_nsec = (uint32_t)st.nfs_mtime_nsec; |
4782 | 0 | nfsdirent->ctime.tv_sec = (long)st.nfs_ctime; |
4783 | 0 | nfsdirent->ctime.tv_usec = (long)(st.nfs_ctime_nsec/1000); |
4784 | 0 | nfsdirent->ctime_nsec = (uint32_t)st.nfs_ctime_nsec; |
4785 | 0 | nfsdirent->uid = (uint32_t)st.nfs_uid; |
4786 | 0 | nfsdirent->gid = (uint32_t)st.nfs_gid; |
4787 | 0 | nfsdirent->nlink = (uint32_t)st.nfs_nlink; |
4788 | 0 | nfsdirent->dev = st.nfs_dev; |
4789 | 0 | nfsdirent->rdev = st.nfs_rdev; |
4790 | 0 | nfsdirent->blksize = NFS_BLKSIZE; |
4791 | 0 | nfsdirent->blocks = st.nfs_blocks; |
4792 | 0 | nfsdirent->used = st.nfs_used; |
4793 | |
|
4794 | 0 | nfsdirent->next = nfsdir->entries; |
4795 | 0 | nfsdir->entries = nfsdirent; |
4796 | 0 | e = e->nextentry; |
4797 | 0 | } |
4798 | | |
4799 | 0 | if (res->reply.eof == 0) { |
4800 | 0 | uint64_t next; |
4801 | | |
4802 | | /* |
4803 | | * The server says there is more to come, so the next request |
4804 | | * must ask for something different from the last one. A reply |
4805 | | * that leaves the cookie where it was, either by returning no |
4806 | | * entries at all or by repeating the one we sent, would have |
4807 | | * us reissue an identical READDIR forever. |
4808 | | */ |
4809 | 0 | memcpy(&next, data->filler.blob2.val, sizeof(uint64_t)); |
4810 | 0 | if (next == cookie) { |
4811 | 0 | nfs_set_error(nfs, "NFS: server did not advance the " |
4812 | 0 | "readdir cookie for %s, giving up to " |
4813 | 0 | "avoid looping", data->path); |
4814 | 0 | data->cb(-EIO, nfs, nfs_get_error(nfs), |
4815 | 0 | data->private_data); |
4816 | 0 | free_nfs4_cb_data(data); |
4817 | 0 | return; |
4818 | 0 | } |
4819 | 0 | nfs4_opendir_continue(nfs, data); |
4820 | 0 | return; |
4821 | 0 | } |
4822 | | |
4823 | 0 | nfsdir->current = nfsdir->entries; |
4824 | 0 | data->filler.blob1.val = NULL; |
4825 | 0 | data->cb(0, nfs, nfsdir, data->private_data); |
4826 | 0 | free_nfs4_cb_data(data); |
4827 | 0 | } |
4828 | | |
4829 | | static void |
4830 | | nfs4_opendir_cb(struct rpc_context *rpc, int status, void *command_data, |
4831 | | void *private_data) |
4832 | 0 | { |
4833 | 0 | struct nfs4_cb_data *data = private_data; |
4834 | 0 | struct nfs_context *nfs = data->nfs; |
4835 | 0 | COMPOUND4res *res = command_data; |
4836 | 0 | struct nfsfh *fh; |
4837 | 0 | GETFH4resok *gresok; |
4838 | 0 | READDIR4resok *rdresok; |
4839 | 0 | int i; |
4840 | |
|
4841 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
4842 | |
|
4843 | 0 | if (check_nfs4_error(nfs, status, data, res, "READDIR")) { |
4844 | 0 | return; |
4845 | 0 | } |
4846 | | |
4847 | 0 | if ((gresok = nfs4_find_getfh(nfs, data, res)) == NULL) { |
4848 | 0 | return; |
4849 | 0 | } |
4850 | | |
4851 | 0 | fh = calloc(1, sizeof(*fh)); |
4852 | 0 | if (fh == NULL) { |
4853 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
4854 | 0 | "nfsfh"); |
4855 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4856 | 0 | free_nfs4_cb_data(data); |
4857 | 0 | return; |
4858 | 0 | } |
4859 | | |
4860 | 0 | data->filler.blob0.val = fh; |
4861 | 0 | data->filler.blob0.free = (blob_free)nfs_free_nfsfh; |
4862 | |
|
4863 | 0 | fh->fh.len = gresok->object.nfs_fh4_len; |
4864 | 0 | fh->fh.val = malloc(fh->fh.len); |
4865 | 0 | if (fh->fh.val == NULL) { |
4866 | 0 | nfs_set_error(nfs, "Out of memory. Failed to allocate " |
4867 | 0 | "nfsfh"); |
4868 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
4869 | 0 | free_nfs4_cb_data(data); |
4870 | 0 | return; |
4871 | 0 | } |
4872 | 0 | memcpy(fh->fh.val, gresok->object.nfs_fh4_val, fh->fh.len); |
4873 | |
|
4874 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_READDIR, "READDIR")) < 0) { |
4875 | 0 | return; |
4876 | 0 | } |
4877 | 0 | rdresok = &res->resarray.resarray_val[i].nfs_resop4_u.opreaddir.READDIR4res_u.resok4; |
4878 | 0 | nfs4_parse_readdir(nfs, data, rdresok); |
4879 | 0 | } |
4880 | | |
4881 | | static int |
4882 | | nfs4_populate_readdir(struct nfs4_cb_data *data, nfs_argop4 *op) |
4883 | 0 | { |
4884 | 0 | struct nfs_context *nfs = data->nfs; |
4885 | 0 | uint64_t cookie; |
4886 | 0 | int i; |
4887 | |
|
4888 | 0 | memcpy(&cookie, data->filler.blob2.val, sizeof(uint64_t)); |
4889 | |
|
4890 | 0 | i = nfs4_op_getfh(nfs, &op[0]); |
4891 | 0 | i += nfs4_op_readdir(nfs, &op[i], cookie); |
4892 | |
|
4893 | 0 | return i; |
4894 | 0 | } |
4895 | | |
4896 | | |
4897 | | /* blob0 is the directory filehandle |
4898 | | * blob1 is nfsdir |
4899 | | * blob2 is the cookie |
4900 | | */ |
4901 | | /* |
4902 | | * NFSv4 named attributes. |
4903 | | * |
4904 | | * Every file and directory can carry a hidden directory of named attributes, |
4905 | | * which are themselves ordinary files. Windows clients use these to hold |
4906 | | * alternate data streams. The three calls below reach that directory by |
4907 | | * setting LOOKUP_FLAG_OPENATTR, which puts an OPENATTR after the path lookup |
4908 | | * so that the ordinary readdir, open and remove machinery operates on the |
4909 | | * attribute directory instead of on the file. |
4910 | | * |
4911 | | * This is NFSv4, not NFSv4.2, so it works for every minor version. A server |
4912 | | * that does not implement named attributes answers NFS4ERR_NOTSUPP. |
4913 | | */ |
4914 | | int |
4915 | | nfs4_opendir_namedattr_async(struct nfs_context *nfs, const char *path, |
4916 | | nfs_cb cb, void *private_data) |
4917 | 0 | { |
4918 | 0 | struct nfs4_cb_data *data; |
4919 | 0 | struct nfsdir *nfsdir; |
4920 | |
|
4921 | 0 | data = init_cb_data_full_path(nfs, path); |
4922 | 0 | if (data == NULL) { |
4923 | 0 | return -1; |
4924 | 0 | } |
4925 | | |
4926 | 0 | data->cb = cb; |
4927 | 0 | data->private_data = private_data; |
4928 | 0 | data->filler.func = nfs4_populate_readdir; |
4929 | 0 | data->filler.max_op = 2; |
4930 | 0 | data->flags |= LOOKUP_FLAG_OPENATTR; |
4931 | |
|
4932 | 0 | nfsdir = calloc(1, sizeof(struct nfsdir)); |
4933 | 0 | if (nfsdir == NULL) { |
4934 | 0 | free_nfs4_cb_data(data); |
4935 | 0 | nfs_set_error(nfs, "failed to allocate buffer for nfsdir"); |
4936 | 0 | return -1; |
4937 | 0 | } |
4938 | 0 | data->filler.blob1.val = nfsdir; |
4939 | 0 | data->filler.blob1.free = (blob_free)nfs_free_nfsdir; |
4940 | |
|
4941 | 0 | data->filler.blob2.val = calloc(1, sizeof(uint64_t)); |
4942 | 0 | if (data->filler.blob2.val == NULL) { |
4943 | 0 | free_nfs4_cb_data(data); |
4944 | 0 | nfs_set_error(nfs, "failed to allocate buffer for cookie"); |
4945 | 0 | return -1; |
4946 | 0 | } |
4947 | 0 | data->filler.blob2.free = (blob_free)free; |
4948 | |
|
4949 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_opendir_cb) < 0) { |
4950 | 0 | free_nfs4_cb_data(data); |
4951 | 0 | return -1; |
4952 | 0 | } |
4953 | | |
4954 | 0 | return 0; |
4955 | 0 | } |
4956 | | |
4957 | | int |
4958 | | nfs4_open_namedattr_async(struct nfs_context *nfs, const char *path, |
4959 | | const char *attrname, int flags, nfs_cb cb, |
4960 | | void *private_data) |
4961 | 0 | { |
4962 | 0 | struct nfs4_cb_data *data; |
4963 | |
|
4964 | 0 | if (attrname == NULL || *attrname == 0) { |
4965 | 0 | nfs_set_error(nfs, "No named attribute given"); |
4966 | 0 | return -1; |
4967 | 0 | } |
4968 | 0 | if (strchr(attrname, '/')) { |
4969 | 0 | nfs_set_error(nfs, "A named attribute cannot contain '/'"); |
4970 | 0 | return -1; |
4971 | 0 | } |
4972 | | |
4973 | | /* |
4974 | | * The whole path names the file that owns the attributes, and the |
4975 | | * attribute itself is the name looked up inside the attribute |
4976 | | * directory, which is where data->filler.data goes. |
4977 | | */ |
4978 | 0 | data = init_cb_data_full_path(nfs, path); |
4979 | 0 | if (data == NULL) { |
4980 | 0 | return -1; |
4981 | 0 | } |
4982 | | |
4983 | 0 | data->cb = cb; |
4984 | 0 | data->private_data = private_data; |
4985 | 0 | data->filler.data = strdup(attrname); |
4986 | 0 | if (data->filler.data == NULL) { |
4987 | 0 | nfs_set_error(nfs, "Out of memory"); |
4988 | 0 | free_nfs4_cb_data(data); |
4989 | 0 | return -1; |
4990 | 0 | } |
4991 | 0 | data->flags |= LOOKUP_FLAG_OPENATTR; |
4992 | | /* |
4993 | | * A file that has never had an attribute written has no attribute |
4994 | | * directory, so creating one is part of creating the first |
4995 | | * attribute. |
4996 | | */ |
4997 | 0 | if (flags & O_CREAT) { |
4998 | 0 | data->flags |= LOOKUP_FLAG_OPENATTR_CREATE; |
4999 | 0 | } |
5000 | | |
5001 | | /* O_TRUNC is only valid for O_RDWR or O_WRONLY */ |
5002 | 0 | if (flags & O_TRUNC && !(flags & (O_RDWR|O_WRONLY))) { |
5003 | 0 | flags &= ~O_TRUNC; |
5004 | 0 | } |
5005 | 0 | if (flags & O_EXCL) { |
5006 | 0 | flags &= ~O_TRUNC; |
5007 | 0 | } |
5008 | 0 | if (flags & O_TRUNC) { |
5009 | 0 | data->open_cb = nfs4_open_truncate_cb; |
5010 | |
|
5011 | 0 | data->filler.blob3.val = malloc(12); |
5012 | 0 | if (data->filler.blob3.val == NULL) { |
5013 | 0 | nfs_set_error(nfs, "Out of memory"); |
5014 | 0 | free_nfs4_cb_data(data); |
5015 | 0 | return -1; |
5016 | 0 | } |
5017 | 0 | data->filler.blob3.free = free; |
5018 | 0 | memset(data->filler.blob3.val, 0, 12); |
5019 | 0 | } |
5020 | | |
5021 | | #ifdef HAVE_MULTITHREADING |
5022 | | if (nfs->rpc->multithreading_enabled) { |
5023 | | nfs_mt_sem_wait(&nfs->nfsi->nfs4_open_call_sem); |
5024 | | data->flags |= MUTEX_HELD; |
5025 | | } |
5026 | | #endif |
5027 | 0 | return nfs4_open_async_internal(nfs, data, flags, 0666); |
5028 | 0 | } |
5029 | | |
5030 | | int |
5031 | | nfs4_remove_namedattr_async(struct nfs_context *nfs, const char *path, |
5032 | | const char *attrname, nfs_cb cb, |
5033 | | void *private_data) |
5034 | 0 | { |
5035 | 0 | struct nfs4_cb_data *data; |
5036 | |
|
5037 | 0 | if (attrname == NULL || *attrname == 0) { |
5038 | 0 | nfs_set_error(nfs, "No named attribute given"); |
5039 | 0 | return -1; |
5040 | 0 | } |
5041 | 0 | if (strchr(attrname, '/')) { |
5042 | 0 | nfs_set_error(nfs, "A named attribute cannot contain '/'"); |
5043 | 0 | return -1; |
5044 | 0 | } |
5045 | | |
5046 | 0 | data = init_cb_data_full_path(nfs, path); |
5047 | 0 | if (data == NULL) { |
5048 | 0 | return -1; |
5049 | 0 | } |
5050 | | |
5051 | 0 | data->cb = cb; |
5052 | 0 | data->private_data = private_data; |
5053 | 0 | data->filler.func = nfs4_populate_remove; |
5054 | 0 | data->filler.max_op = 1; |
5055 | 0 | data->filler.data = strdup(attrname); |
5056 | 0 | if (data->filler.data == NULL) { |
5057 | 0 | nfs_set_error(nfs, "Out of memory"); |
5058 | 0 | free_nfs4_cb_data(data); |
5059 | 0 | return -1; |
5060 | 0 | } |
5061 | 0 | data->flags |= LOOKUP_FLAG_OPENATTR; |
5062 | |
|
5063 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_remove_cb) < 0) { |
5064 | 0 | free_nfs4_cb_data(data); |
5065 | 0 | return -1; |
5066 | 0 | } |
5067 | | |
5068 | 0 | return 0; |
5069 | 0 | } |
5070 | | |
5071 | | int |
5072 | | nfs4_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, |
5073 | | void *private_data) |
5074 | 0 | { |
5075 | 0 | struct nfs4_cb_data *data; |
5076 | 0 | struct nfsdir *nfsdir; |
5077 | |
|
5078 | 0 | data = init_cb_data_full_path(nfs, path); |
5079 | 0 | if (data == NULL) { |
5080 | 0 | return -1; |
5081 | 0 | } |
5082 | | |
5083 | 0 | data->cb = cb; |
5084 | 0 | data->private_data = private_data; |
5085 | 0 | data->filler.func = nfs4_populate_readdir; |
5086 | 0 | data->filler.max_op = 2; |
5087 | |
|
5088 | 0 | nfsdir = calloc(1, sizeof(struct nfsdir)); |
5089 | 0 | if (nfsdir == NULL) { |
5090 | 0 | free_nfs4_cb_data(data); |
5091 | 0 | nfs_set_error(nfs, "failed to allocate buffer for nfsdir"); |
5092 | 0 | return -1; |
5093 | 0 | } |
5094 | | |
5095 | 0 | data->filler.blob1.val = nfsdir; |
5096 | 0 | data->filler.blob1.free = (blob_free)nfs_free_nfsdir; |
5097 | |
|
5098 | 0 | data->filler.blob2.val = calloc(1, sizeof(uint64_t)); |
5099 | 0 | if (data->filler.blob2.val == NULL) { |
5100 | 0 | free_nfs4_cb_data(data); |
5101 | 0 | nfs_set_error(nfs, "failed to allocate buffer for cookie"); |
5102 | 0 | return -1; |
5103 | 0 | } |
5104 | 0 | data->filler.blob2.free = (blob_free)free; |
5105 | |
|
5106 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_opendir_cb) < 0) { |
5107 | 0 | free_nfs4_cb_data(data); |
5108 | 0 | return -1; |
5109 | 0 | } |
5110 | | |
5111 | 0 | return 0; |
5112 | 0 | } |
5113 | | |
5114 | | static void |
5115 | | nfs4_truncate_close_cb(struct rpc_context *rpc, int status, void *command_data, |
5116 | | void *private_data) |
5117 | 0 | { |
5118 | 0 | struct nfs4_cb_data *data = private_data; |
5119 | 0 | struct nfs_context *nfs = data->nfs; |
5120 | 0 | COMPOUND4res *res = command_data; |
5121 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
5122 | | |
5123 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
5124 | |
|
5125 | 0 | if (res) { |
5126 | 0 | nfs_increment_seqid(fh, res->status); |
5127 | 0 | } |
5128 | |
|
5129 | 0 | if (check_nfs4_error(nfs, status, data, res, "CLOSE")) { |
5130 | 0 | return; |
5131 | 0 | } |
5132 | | |
5133 | 0 | data->cb(0, nfs, NULL, data->private_data); |
5134 | 0 | free_nfs4_cb_data(data); |
5135 | 0 | } |
5136 | | |
5137 | | static void |
5138 | | nfs4_truncate_open_cb(struct rpc_context *rpc, int status, void *command_data, |
5139 | | void *private_data) |
5140 | 0 | { |
5141 | 0 | struct nfs4_cb_data *data = private_data; |
5142 | 0 | struct nfs_context *nfs = data->nfs; |
5143 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
5144 | 0 | COMPOUND4res *res = command_data; |
5145 | 0 | COMPOUND4args args; |
5146 | 0 | nfs_argop4 op[4 + 1]; |
5147 | 0 | int i; |
5148 | |
|
5149 | 0 | if (check_nfs4_error(nfs, status, data, res, "OPEN")) { |
5150 | 0 | return; |
5151 | 0 | } |
5152 | | |
5153 | 0 | i = nfs4_start_compound(nfs, op); |
5154 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5155 | 0 | i += nfs4_op_truncate(nfs, &op[i], fh, data->filler.blob3.val); |
5156 | 0 | i += nfs4_op_close(nfs, &op[i], fh); |
5157 | |
|
5158 | 0 | memset(&args, 0, sizeof(args)); |
5159 | 0 | args.argarray.argarray_len = i; |
5160 | 0 | args.argarray.argarray_val = op; |
5161 | |
|
5162 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_truncate_close_cb, &args, |
5163 | 0 | data) == NULL) { |
5164 | | /* Not much we can do but leak one fd on the server :( */ |
5165 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data); |
5166 | 0 | free_nfs4_cb_data(data); |
5167 | 0 | return; |
5168 | 0 | } |
5169 | 0 | } |
5170 | | |
5171 | | /* |
5172 | | * data.blob3.val is a 12 byte SETATTR buffer for length+update_mtime |
5173 | | */ |
5174 | | int |
5175 | | nfs4_truncate_async(struct nfs_context *nfs, const char *path, uint64_t length, |
5176 | | nfs_cb cb, void *private_data) |
5177 | 0 | { |
5178 | 0 | struct nfs4_cb_data *data; |
5179 | |
|
5180 | 0 | data = init_cb_data_split_path(nfs, path); |
5181 | 0 | if (data == NULL) { |
5182 | 0 | return -1; |
5183 | 0 | } |
5184 | | |
5185 | 0 | data->cb = cb; |
5186 | 0 | data->private_data = private_data; |
5187 | 0 | data->open_cb = nfs4_truncate_open_cb; |
5188 | |
|
5189 | 0 | data->filler.blob3.val = malloc(12); |
5190 | 0 | if (data->filler.blob3.val == NULL) { |
5191 | 0 | nfs_set_error(nfs, "Out of memory"); |
5192 | 0 | free_nfs4_cb_data(data); |
5193 | 0 | return -1; |
5194 | 0 | } |
5195 | 0 | data->filler.blob3.free = free; |
5196 | |
|
5197 | 0 | memset(data->filler.blob3.val, 0, 12); |
5198 | 0 | length = nfs_hton64(length); |
5199 | 0 | memcpy(data->filler.blob3.val, &length, sizeof(uint64_t)); |
5200 | |
|
5201 | | #ifdef HAVE_MULTITHREADING |
5202 | | if (nfs->rpc->multithreading_enabled) { |
5203 | | nfs_mt_sem_wait(&nfs->nfsi->nfs4_open_call_sem); |
5204 | | data->flags |= MUTEX_HELD; |
5205 | | } |
5206 | | #endif |
5207 | 0 | if (nfs4_open_async_internal(nfs, data, O_WRONLY, 0) < 0) { |
5208 | 0 | return -1; |
5209 | 0 | } |
5210 | | |
5211 | 0 | return 0; |
5212 | 0 | } |
5213 | | |
5214 | | static void |
5215 | | nfs4_fsync_cb(struct rpc_context *rpc, int status, void *command_data, |
5216 | | void *private_data) |
5217 | 0 | { |
5218 | 0 | struct nfs4_cb_data *data = private_data; |
5219 | 0 | struct nfs_context *nfs = data->nfs; |
5220 | 0 | COMPOUND4res *res = command_data; |
5221 | |
|
5222 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
5223 | |
|
5224 | 0 | if (check_nfs4_error(nfs, status, data, res, "FSYNC")) { |
5225 | 0 | return; |
5226 | 0 | } |
5227 | | |
5228 | 0 | data->cb(0, nfs, NULL, data->private_data); |
5229 | 0 | free_nfs4_cb_data(data); |
5230 | 0 | } |
5231 | | |
5232 | | int |
5233 | | nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *fh, nfs_cb cb, |
5234 | | void *private_data) |
5235 | 0 | { |
5236 | 0 | COMPOUND4args args; |
5237 | 0 | nfs_argop4 op[2 + 1]; |
5238 | 0 | struct nfs4_cb_data *data; |
5239 | 0 | int i; |
5240 | |
|
5241 | 0 | data = calloc(1, sizeof(*data)); |
5242 | 0 | if (data == NULL) { |
5243 | 0 | nfs_set_error(nfs, "Out of memory."); |
5244 | 0 | return -1; |
5245 | 0 | } |
5246 | | |
5247 | 0 | data->nfs = nfs; |
5248 | 0 | data->cb = cb; |
5249 | 0 | data->private_data = private_data; |
5250 | |
|
5251 | 0 | memset(op, 0, sizeof(op)); |
5252 | |
|
5253 | 0 | i = nfs4_start_compound(nfs, op); |
5254 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5255 | 0 | i += nfs4_op_commit(nfs, &op[i]); |
5256 | |
|
5257 | 0 | memset(&args, 0, sizeof(args)); |
5258 | 0 | args.argarray.argarray_len = i; |
5259 | 0 | args.argarray.argarray_val = op; |
5260 | |
|
5261 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_fsync_cb, &args, |
5262 | 0 | data) == NULL) { |
5263 | 0 | data->filler.blob0.val = NULL; |
5264 | 0 | free_nfs4_cb_data(data); |
5265 | 0 | return -1; |
5266 | 0 | } |
5267 | | |
5268 | 0 | return 0; |
5269 | 0 | } |
5270 | | |
5271 | | int |
5272 | | nfs4_ftruncate_async(struct nfs_context *nfs, struct nfsfh *fh, |
5273 | | uint64_t length, nfs_cb cb, void *private_data) |
5274 | 0 | { |
5275 | 0 | COMPOUND4args args; |
5276 | 0 | nfs_argop4 op[2 + 1]; |
5277 | 0 | struct nfs4_cb_data *data; |
5278 | 0 | int i; |
5279 | |
|
5280 | 0 | data = calloc(1, sizeof(*data)); |
5281 | 0 | if (data == NULL) { |
5282 | 0 | nfs_set_error(nfs, "Out of memory."); |
5283 | 0 | return -1; |
5284 | 0 | } |
5285 | | |
5286 | 0 | data->nfs = nfs; |
5287 | 0 | data->cb = cb; |
5288 | 0 | data->private_data = private_data; |
5289 | |
|
5290 | 0 | data->filler.blob3.val = calloc(1, 12); |
5291 | 0 | if (data->filler.blob3.val == NULL) { |
5292 | 0 | nfs_set_error(nfs, "Out of memory"); |
5293 | 0 | free_nfs4_cb_data(data); |
5294 | 0 | return -1; |
5295 | 0 | } |
5296 | 0 | data->filler.blob3.free = free; |
5297 | |
|
5298 | 0 | length = nfs_hton64(length); |
5299 | 0 | memcpy(data->filler.blob3.val, &length, sizeof(uint64_t)); |
5300 | | |
5301 | 0 | memset(op, 0, sizeof(op)); |
5302 | |
|
5303 | 0 | i = nfs4_start_compound(nfs, op); |
5304 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5305 | 0 | i += nfs4_op_truncate(nfs, &op[i], fh, data->filler.blob3.val); |
5306 | |
|
5307 | 0 | memset(&args, 0, sizeof(args)); |
5308 | 0 | args.argarray.argarray_len = i; |
5309 | 0 | args.argarray.argarray_val = op; |
5310 | |
|
5311 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_fsync_cb, &args, |
5312 | 0 | data) == NULL) { |
5313 | 0 | data->filler.blob0.val = NULL; |
5314 | 0 | free_nfs4_cb_data(data); |
5315 | 0 | return -1; |
5316 | 0 | } |
5317 | | |
5318 | 0 | return 0; |
5319 | 0 | } |
5320 | | |
5321 | | static void |
5322 | | nfs4_lseek_cb(struct rpc_context *rpc, int status, void *command_data, |
5323 | | void *private_data) |
5324 | 0 | { |
5325 | 0 | struct nfs4_cb_data *data = private_data; |
5326 | 0 | struct nfs_context *nfs = data->nfs; |
5327 | 0 | COMPOUND4res *res = command_data; |
5328 | 0 | GETATTR4resok *garesok = NULL; |
5329 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
5330 | 0 | struct nfs_stat_64 st; |
5331 | 0 | int64_t offset; |
5332 | 0 | int i; |
5333 | |
|
5334 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
5335 | |
|
5336 | 0 | memcpy(&offset, data->filler.blob1.val, sizeof(int64_t)); |
5337 | | |
5338 | 0 | if (check_nfs4_error(nfs, status, data, res, "LSEEK")) { |
5339 | 0 | return; |
5340 | 0 | } |
5341 | | |
5342 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_GETATTR, "GETATTR")) < 0) { |
5343 | 0 | return; |
5344 | 0 | } |
5345 | 0 | garesok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetattr.GETATTR4res_u.resok4; |
5346 | |
|
5347 | 0 | memset(&st, 0, sizeof(st)); |
5348 | 0 | nfs_parse_attributes(nfs, data, &st, NULL, NULL, |
5349 | 0 | &garesok->obj_attributes.attrmask, |
5350 | 0 | garesok->obj_attributes.attr_vals.attrlist4_val, |
5351 | 0 | garesok->obj_attributes.attr_vals.attrlist4_len); |
5352 | |
|
5353 | 0 | if (offset < 0 && |
5354 | 0 | -offset > (int64_t)st.nfs_size) { |
5355 | 0 | nfs_set_error(nfs, "Negative offset for lseek(" |
5356 | 0 | "SEET_END)"); |
5357 | 0 | data->cb(-EINVAL, nfs, &fh->offset, |
5358 | 0 | data->private_data); |
5359 | 0 | } else { |
5360 | 0 | fh->offset = offset + st.nfs_size; |
5361 | 0 | data->cb(0, nfs, &fh->offset, data->private_data); |
5362 | 0 | } |
5363 | |
|
5364 | 0 | free_nfs4_cb_data(data); |
5365 | 0 | } |
5366 | | |
5367 | | #ifdef HAVE_NFS4_2 |
5368 | | /* What the COMPOUND for this fallocate mode is made of, for error messages */ |
5369 | | static char * |
5370 | | nfs42_fallocate_opname(int mode) |
5371 | 0 | { |
5372 | 0 | if (mode & FALLOC_FL_ZERO_RANGE) { |
5373 | 0 | return "DEALLOCATE+ALLOCATE"; |
5374 | 0 | } |
5375 | 0 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
5376 | 0 | return "DEALLOCATE"; |
5377 | 0 | } |
5378 | 0 | return "ALLOCATE"; |
5379 | 0 | } |
5380 | | |
5381 | | static void |
5382 | | nfs42_fallocate_cb(struct rpc_context *rpc, int status, void *command_data, |
5383 | | void *private_data) |
5384 | 0 | { |
5385 | 0 | struct nfs4_cb_data *data = private_data; |
5386 | 0 | struct nfs_context *nfs = data->nfs; |
5387 | 0 | COMPOUND4res *res = command_data; |
5388 | |
|
5389 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
5390 | | |
5391 | | /* blob1.len carries the mode, so the error names the right op */ |
5392 | 0 | if (check_nfs4_error(nfs, status, data, res, |
5393 | 0 | nfs42_fallocate_opname(data->filler.blob1.len))) { |
5394 | 0 | return; |
5395 | 0 | } |
5396 | | |
5397 | 0 | data->cb(0, nfs, NULL, data->private_data); |
5398 | 0 | free_nfs4_cb_data(data); |
5399 | 0 | } |
5400 | | |
5401 | | int |
5402 | | nfs42_fallocate_async(struct nfs_context *nfs, struct nfsfh *fh, int mode, |
5403 | | uint64_t offset, uint64_t length, nfs_cb cb, |
5404 | | void *private_data) |
5405 | 0 | { |
5406 | 0 | COMPOUND4args args; |
5407 | | /* PUTFH plus at most DEALLOCATE and ALLOCATE for ZERO_RANGE */ |
5408 | 0 | nfs_argop4 op[3 + 1]; |
5409 | 0 | struct nfs4_cb_data *data; |
5410 | 0 | int i; |
5411 | |
|
5412 | 0 | if (fh->is_readonly) { |
5413 | 0 | nfs_set_error(nfs, "Trying to fallocate a read-only file"); |
5414 | 0 | return -1; |
5415 | 0 | } |
5416 | | |
5417 | | /* |
5418 | | * Three modes are implemented. |
5419 | | * |
5420 | | * 0 reserves space, which is ALLOCATE. NFS grows the file when the |
5421 | | * range runs past the end, exactly as fallocate(2) does without |
5422 | | * FALLOC_FL_KEEP_SIZE. There is no way to ask NFS to reserve without |
5423 | | * growing, so KEEP_SIZE on its own is refused rather than quietly |
5424 | | * ignored. |
5425 | | * |
5426 | | * FALLOC_FL_PUNCH_HOLE deallocates, which is DEALLOCATE, and must be |
5427 | | * paired with FALLOC_FL_KEEP_SIZE as Linux requires. A hole never |
5428 | | * changes the file size and neither does DEALLOCATE. |
5429 | | * |
5430 | | * FALLOC_FL_ZERO_RANGE has no single operation of its own. It is a |
5431 | | * DEALLOCATE, which makes the range read as zeroes, followed by an |
5432 | | * ALLOCATE that puts the space back, both in one COMPOUND so that |
5433 | | * the range is never left merely sparse. KEEP_SIZE cannot be honoured |
5434 | | * with it for the same reason as above, so it is refused too. |
5435 | | */ |
5436 | 0 | if (mode != 0 && |
5437 | 0 | mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE) && |
5438 | 0 | mode != FALLOC_FL_ZERO_RANGE) { |
5439 | 0 | nfs_set_error(nfs, "nfs_fallocate only supports mode 0, " |
5440 | 0 | "FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE and " |
5441 | 0 | "FALLOC_FL_ZERO_RANGE"); |
5442 | 0 | return -EINVAL; |
5443 | 0 | } |
5444 | 0 | if (length == 0) { |
5445 | 0 | nfs_set_error(nfs, "nfs_fallocate: length must be non-zero"); |
5446 | 0 | return -EINVAL; |
5447 | 0 | } |
5448 | | |
5449 | 0 | data = calloc(1, sizeof(*data)); |
5450 | 0 | if (data == NULL) { |
5451 | 0 | nfs_set_error(nfs, "Out of memory."); |
5452 | 0 | return -1; |
5453 | 0 | } |
5454 | 0 | data->nfs = nfs; |
5455 | 0 | data->cb = cb; |
5456 | 0 | data->private_data = private_data; |
5457 | 0 | data->filler.blob1.len = mode; |
5458 | |
|
5459 | 0 | memset(op, 0, sizeof(op)); |
5460 | 0 | i = nfs4_start_compound(nfs, op); |
5461 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5462 | 0 | if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) { |
5463 | 0 | i += nfs42_op_deallocate(nfs, &op[i], fh, offset, length); |
5464 | 0 | } |
5465 | 0 | if (!(mode & FALLOC_FL_PUNCH_HOLE)) { |
5466 | 0 | i += nfs42_op_allocate(nfs, &op[i], fh, offset, length); |
5467 | 0 | } |
5468 | |
|
5469 | 0 | memset(&args, 0, sizeof(args)); |
5470 | 0 | args.argarray.argarray_len = i; |
5471 | 0 | args.argarray.argarray_val = op; |
5472 | |
|
5473 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs42_fallocate_cb, &args, |
5474 | 0 | data) == NULL) { |
5475 | 0 | nfs_set_error(nfs, "Failed to queue %s. %s", |
5476 | 0 | nfs42_fallocate_opname(mode), |
5477 | 0 | rpc_get_error(nfs->rpc)); |
5478 | 0 | free_nfs4_cb_data(data); |
5479 | 0 | return -1; |
5480 | 0 | } |
5481 | | |
5482 | 0 | return 0; |
5483 | 0 | } |
5484 | | |
5485 | | static void |
5486 | | nfs42_read_plus_cb(struct rpc_context *rpc, int status, void *command_data, |
5487 | | void *private_data) |
5488 | 0 | { |
5489 | 0 | struct nfs4_cb_data *data = private_data; |
5490 | 0 | struct nfs_context *nfs = data->nfs; |
5491 | 0 | COMPOUND4res *res = command_data; |
5492 | 0 | READ_PLUS4resok *rpresok; |
5493 | 0 | int i; |
5494 | |
|
5495 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
5496 | |
|
5497 | 0 | if (check_nfs4_error(nfs, status, data, res, "READ_PLUS")) { |
5498 | 0 | return; |
5499 | 0 | } |
5500 | | |
5501 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_READ_PLUS, "READ_PLUS")) < 0) { |
5502 | 0 | return; |
5503 | 0 | } |
5504 | 0 | rpresok = &res->resarray.resarray_val[i].nfs_resop4_u.opreadplus.READ_PLUS4res_u.rp_resok4; |
5505 | | |
5506 | | /* |
5507 | | * The contents point into the reply, which is released once we |
5508 | | * return, so a caller that wants to keep them must copy them. |
5509 | | */ |
5510 | 0 | data->cb(0, nfs, rpresok, data->private_data); |
5511 | 0 | free_nfs4_cb_data(data); |
5512 | 0 | } |
5513 | | |
5514 | | int |
5515 | | nfs42_read_plus_async(struct nfs_context *nfs, struct nfsfh *fh, |
5516 | | uint64_t offset, uint32_t count, nfs_cb cb, |
5517 | | void *private_data) |
5518 | 0 | { |
5519 | 0 | COMPOUND4args args; |
5520 | 0 | nfs_argop4 op[2 + 1]; |
5521 | 0 | struct nfs4_cb_data *data; |
5522 | 0 | int i; |
5523 | |
|
5524 | 0 | if (!nfs_is_v4_2(nfs)) { |
5525 | 0 | nfs_set_error(nfs, "%s requires NFSv4.2", __FUNCTION__); |
5526 | 0 | return -ENOTSUP; |
5527 | 0 | } |
5528 | | |
5529 | 0 | data = calloc(1, sizeof(*data)); |
5530 | 0 | if (data == NULL) { |
5531 | 0 | nfs_set_error(nfs, "Out of memory."); |
5532 | 0 | return -1; |
5533 | 0 | } |
5534 | 0 | data->nfs = nfs; |
5535 | 0 | data->cb = cb; |
5536 | 0 | data->private_data = private_data; |
5537 | |
|
5538 | 0 | memset(op, 0, sizeof(op)); |
5539 | 0 | i = nfs4_start_compound(nfs, op); |
5540 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5541 | 0 | i += nfs42_op_read_plus(nfs, &op[i], fh, offset, count); |
5542 | |
|
5543 | 0 | memset(&args, 0, sizeof(args)); |
5544 | 0 | args.argarray.argarray_len = i; |
5545 | 0 | args.argarray.argarray_val = op; |
5546 | |
|
5547 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs42_read_plus_cb, &args, |
5548 | 0 | data) == NULL) { |
5549 | 0 | nfs_set_error(nfs, "Failed to queue READ_PLUS. %s", |
5550 | 0 | rpc_get_error(nfs->rpc)); |
5551 | 0 | free_nfs4_cb_data(data); |
5552 | 0 | return -1; |
5553 | 0 | } |
5554 | | |
5555 | 0 | return 0; |
5556 | 0 | } |
5557 | | |
5558 | | static void |
5559 | | nfs42_write_same_cb(struct rpc_context *rpc, int status, void *command_data, |
5560 | | void *private_data) |
5561 | 0 | { |
5562 | 0 | struct nfs4_cb_data *data = private_data; |
5563 | 0 | struct nfs_context *nfs = data->nfs; |
5564 | 0 | COMPOUND4res *res = command_data; |
5565 | 0 | write_response4 *wsresok; |
5566 | 0 | int i; |
5567 | |
|
5568 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
5569 | |
|
5570 | 0 | if (check_nfs4_error(nfs, status, data, res, "WRITE_SAME")) { |
5571 | 0 | return; |
5572 | 0 | } |
5573 | | |
5574 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_WRITE_SAME, |
5575 | 0 | "WRITE_SAME")) < 0) { |
5576 | 0 | return; |
5577 | 0 | } |
5578 | 0 | wsresok = &res->resarray.resarray_val[i].nfs_resop4_u.opwritesame.WRITE_SAME4res_u.wsr_resok4; |
5579 | |
|
5580 | 0 | data->cb(0, nfs, wsresok, data->private_data); |
5581 | 0 | free_nfs4_cb_data(data); |
5582 | 0 | } |
5583 | | |
5584 | | int |
5585 | | nfs42_write_same_async(struct nfs_context *nfs, struct nfsfh *fh, |
5586 | | app_data_block4 *adb, int stable, nfs_cb cb, |
5587 | | void *private_data) |
5588 | 0 | { |
5589 | 0 | COMPOUND4args args; |
5590 | 0 | nfs_argop4 op[2 + 1]; |
5591 | 0 | struct nfs4_cb_data *data; |
5592 | 0 | int i; |
5593 | |
|
5594 | 0 | if (!nfs_is_v4_2(nfs)) { |
5595 | 0 | nfs_set_error(nfs, "%s requires NFSv4.2", __FUNCTION__); |
5596 | 0 | return -ENOTSUP; |
5597 | 0 | } |
5598 | 0 | if (fh->is_readonly) { |
5599 | 0 | nfs_set_error(nfs, "Trying to write to a read-only file"); |
5600 | 0 | return -1; |
5601 | 0 | } |
5602 | | |
5603 | 0 | data = calloc(1, sizeof(*data)); |
5604 | 0 | if (data == NULL) { |
5605 | 0 | nfs_set_error(nfs, "Out of memory."); |
5606 | 0 | return -1; |
5607 | 0 | } |
5608 | 0 | data->nfs = nfs; |
5609 | 0 | data->cb = cb; |
5610 | 0 | data->private_data = private_data; |
5611 | |
|
5612 | 0 | memset(op, 0, sizeof(op)); |
5613 | 0 | i = nfs4_start_compound(nfs, op); |
5614 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5615 | 0 | i += nfs42_op_write_same(nfs, &op[i], fh, adb, stable); |
5616 | |
|
5617 | 0 | memset(&args, 0, sizeof(args)); |
5618 | 0 | args.argarray.argarray_len = i; |
5619 | 0 | args.argarray.argarray_val = op; |
5620 | |
|
5621 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs42_write_same_cb, &args, |
5622 | 0 | data) == NULL) { |
5623 | 0 | nfs_set_error(nfs, "Failed to queue WRITE_SAME. %s", |
5624 | 0 | rpc_get_error(nfs->rpc)); |
5625 | 0 | free_nfs4_cb_data(data); |
5626 | 0 | return -1; |
5627 | 0 | } |
5628 | | |
5629 | 0 | return 0; |
5630 | 0 | } |
5631 | | |
5632 | | /* |
5633 | | * COPY, COPY_NOTIFY, OFFLOAD_STATUS and OFFLOAD_CANCEL. filler.flags is the |
5634 | | * operation the callback looks for. |
5635 | | */ |
5636 | | static const char * |
5637 | | nfs42_copy_op_name(nfs_opnum4 opnum) |
5638 | 0 | { |
5639 | 0 | switch (opnum) { |
5640 | 0 | case OP_COPY: return "COPY"; |
5641 | 0 | case OP_COPY_NOTIFY: return "COPY_NOTIFY"; |
5642 | 0 | case OP_OFFLOAD_STATUS: return "OFFLOAD_STATUS"; |
5643 | 0 | default: return "OFFLOAD_CANCEL"; |
5644 | 0 | } |
5645 | 0 | } |
5646 | | |
5647 | | static void |
5648 | | nfs42_copy_cb(struct rpc_context *rpc, int status, void *command_data, |
5649 | | void *private_data) |
5650 | 0 | { |
5651 | 0 | struct nfs4_cb_data *data = private_data; |
5652 | 0 | struct nfs_context *nfs = data->nfs; |
5653 | 0 | COMPOUND4res *res = command_data; |
5654 | 0 | nfs_opnum4 opnum = data->filler.flags; |
5655 | 0 | const char *name = nfs42_copy_op_name(opnum); |
5656 | 0 | nfs_resop4 *r; |
5657 | 0 | void *result = NULL; |
5658 | 0 | int i; |
5659 | |
|
5660 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
5661 | |
|
5662 | 0 | if (check_nfs4_error(nfs, status, data, res, discard_const(name))) { |
5663 | 0 | return; |
5664 | 0 | } |
5665 | 0 | if ((i = nfs4_find_op(nfs, data, res, opnum, name)) < 0) { |
5666 | 0 | return; |
5667 | 0 | } |
5668 | 0 | r = &res->resarray.resarray_val[i]; |
5669 | |
|
5670 | 0 | switch (opnum) { |
5671 | 0 | case OP_COPY: |
5672 | 0 | result = &r->nfs_resop4_u.opcopy.COPY4res_u.cr_resok4; |
5673 | 0 | break; |
5674 | 0 | case OP_COPY_NOTIFY: |
5675 | 0 | result = &r->nfs_resop4_u.opcopynotify.COPY_NOTIFY4res_u.resok4; |
5676 | 0 | break; |
5677 | 0 | case OP_OFFLOAD_STATUS: |
5678 | 0 | result = &r->nfs_resop4_u.opoffloadstatus.OFFLOAD_STATUS4res_u.osr_resok4; |
5679 | 0 | break; |
5680 | 0 | default: |
5681 | 0 | break; |
5682 | 0 | } |
5683 | | |
5684 | 0 | data->cb(0, nfs, result, data->private_data); |
5685 | 0 | free_nfs4_cb_data(data); |
5686 | 0 | } |
5687 | | |
5688 | | static int |
5689 | | nfs42_copy_send(struct nfs_context *nfs, nfs_argop4 *op, int i, |
5690 | | nfs_opnum4 opnum, nfs_cb cb, void *private_data) |
5691 | 0 | { |
5692 | 0 | COMPOUND4args args; |
5693 | 0 | struct nfs4_cb_data *data; |
5694 | |
|
5695 | 0 | data = calloc(1, sizeof(*data)); |
5696 | 0 | if (data == NULL) { |
5697 | 0 | nfs_set_error(nfs, "Out of memory."); |
5698 | 0 | return -1; |
5699 | 0 | } |
5700 | 0 | data->nfs = nfs; |
5701 | 0 | data->cb = cb; |
5702 | 0 | data->private_data = private_data; |
5703 | 0 | data->filler.flags = opnum; |
5704 | |
|
5705 | 0 | memset(&args, 0, sizeof(args)); |
5706 | 0 | args.argarray.argarray_len = i; |
5707 | 0 | args.argarray.argarray_val = op; |
5708 | |
|
5709 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs42_copy_cb, &args, |
5710 | 0 | data) == NULL) { |
5711 | 0 | nfs_set_error(nfs, "Failed to queue %s. %s", |
5712 | 0 | nfs42_copy_op_name(opnum), |
5713 | 0 | rpc_get_error(nfs->rpc)); |
5714 | 0 | free_nfs4_cb_data(data); |
5715 | 0 | return -1; |
5716 | 0 | } |
5717 | | |
5718 | 0 | return 0; |
5719 | 0 | } |
5720 | | |
5721 | | int |
5722 | | nfs42_copy_async(struct nfs_context *nfs, |
5723 | | struct nfsfh *src, uint64_t src_offset, |
5724 | | struct nfsfh *dst, uint64_t dst_offset, |
5725 | | uint64_t count, int synchronous, |
5726 | | COPY_NOTIFY4resok *notify, nfs_cb cb, void *private_data) |
5727 | 0 | { |
5728 | 0 | nfs_argop4 op[5]; |
5729 | 0 | int i; |
5730 | |
|
5731 | 0 | if (!nfs_is_v4_2(nfs)) { |
5732 | 0 | nfs_set_error(nfs, "%s requires NFSv4.2", __FUNCTION__); |
5733 | 0 | return -ENOTSUP; |
5734 | 0 | } |
5735 | 0 | if (dst->is_readonly) { |
5736 | 0 | nfs_set_error(nfs, "Trying to write to a read-only file"); |
5737 | 0 | return -1; |
5738 | 0 | } |
5739 | | |
5740 | 0 | memset(op, 0, sizeof(op)); |
5741 | 0 | i = nfs4_start_compound(nfs, op); |
5742 | 0 | i += nfs4_op_putfh(nfs, &op[i], src); |
5743 | 0 | i += nfs4_op_savefh(nfs, &op[i]); |
5744 | 0 | i += nfs4_op_putfh(nfs, &op[i], dst); |
5745 | 0 | i += nfs42_op_copy(nfs, &op[i], src, src_offset, dst, dst_offset, |
5746 | 0 | count, synchronous, notify); |
5747 | |
|
5748 | 0 | return nfs42_copy_send(nfs, op, i, OP_COPY, cb, private_data); |
5749 | 0 | } |
5750 | | |
5751 | | int |
5752 | | nfs42_copy_notify_async(struct nfs_context *nfs, struct nfsfh *src, |
5753 | | netloc4 *destination, nfs_cb cb, void *private_data) |
5754 | 0 | { |
5755 | 0 | nfs_argop4 op[3]; |
5756 | 0 | int i; |
5757 | |
|
5758 | 0 | if (!nfs_is_v4_2(nfs)) { |
5759 | 0 | nfs_set_error(nfs, "%s requires NFSv4.2", __FUNCTION__); |
5760 | 0 | return -ENOTSUP; |
5761 | 0 | } |
5762 | | |
5763 | 0 | memset(op, 0, sizeof(op)); |
5764 | 0 | i = nfs4_start_compound(nfs, op); |
5765 | 0 | i += nfs4_op_putfh(nfs, &op[i], src); |
5766 | 0 | i += nfs42_op_copy_notify(nfs, &op[i], src, destination); |
5767 | |
|
5768 | 0 | return nfs42_copy_send(nfs, op, i, OP_COPY_NOTIFY, cb, private_data); |
5769 | 0 | } |
5770 | | |
5771 | | static int |
5772 | | nfs42_offload_async(struct nfs_context *nfs, struct nfsfh *fh, |
5773 | | const stateid4 *stateid, nfs_opnum4 opnum, |
5774 | | nfs_cb cb, void *private_data) |
5775 | 0 | { |
5776 | 0 | nfs_argop4 op[3]; |
5777 | 0 | int i; |
5778 | |
|
5779 | 0 | if (!nfs_is_v4_2(nfs)) { |
5780 | 0 | nfs_set_error(nfs, "%s requires NFSv4.2", __FUNCTION__); |
5781 | 0 | return -ENOTSUP; |
5782 | 0 | } |
5783 | | |
5784 | 0 | memset(op, 0, sizeof(op)); |
5785 | 0 | i = nfs4_start_compound(nfs, op); |
5786 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5787 | 0 | i += nfs42_op_offload(nfs, &op[i], opnum, stateid); |
5788 | |
|
5789 | 0 | return nfs42_copy_send(nfs, op, i, opnum, cb, private_data); |
5790 | 0 | } |
5791 | | |
5792 | | int |
5793 | | nfs42_offload_status_async(struct nfs_context *nfs, struct nfsfh *fh, |
5794 | | const stateid4 *stateid, nfs_cb cb, |
5795 | | void *private_data) |
5796 | 0 | { |
5797 | 0 | return nfs42_offload_async(nfs, fh, stateid, OP_OFFLOAD_STATUS, cb, |
5798 | 0 | private_data); |
5799 | 0 | } |
5800 | | |
5801 | | int |
5802 | | nfs42_offload_cancel_async(struct nfs_context *nfs, struct nfsfh *fh, |
5803 | | const stateid4 *stateid, nfs_cb cb, |
5804 | | void *private_data) |
5805 | 0 | { |
5806 | 0 | return nfs42_offload_async(nfs, fh, stateid, OP_OFFLOAD_CANCEL, cb, |
5807 | 0 | private_data); |
5808 | 0 | } |
5809 | | |
5810 | | /* |
5811 | | * SEEK_HOLE / SEEK_DATA. blob0.val is the nfsfh. |
5812 | | */ |
5813 | | static void |
5814 | | nfs42_seek_cb(struct rpc_context *rpc, int status, void *command_data, |
5815 | | void *private_data) |
5816 | 0 | { |
5817 | 0 | struct nfs4_cb_data *data = private_data; |
5818 | 0 | struct nfs_context *nfs = data->nfs; |
5819 | 0 | COMPOUND4res *res = command_data; |
5820 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
5821 | 0 | SEEK4resok *sresok; |
5822 | 0 | int i; |
5823 | |
|
5824 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
5825 | |
|
5826 | 0 | if (check_nfs4_error(nfs, status, data, res, "SEEK")) { |
5827 | 0 | return; |
5828 | 0 | } |
5829 | | |
5830 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_SEEK, "SEEK")) < 0) { |
5831 | 0 | return; |
5832 | 0 | } |
5833 | 0 | sresok = &res->resarray.resarray_val[i].nfs_resop4_u.opseek.SEEK4res_u.resok4; |
5834 | |
|
5835 | 0 | fh->offset = sresok->sr_offset; |
5836 | 0 | data->cb(0, nfs, &fh->offset, data->private_data); |
5837 | 0 | free_nfs4_cb_data(data); |
5838 | 0 | } |
5839 | | |
5840 | | static int |
5841 | | nfs42_lseek_content_async(struct nfs_context *nfs, struct nfsfh *fh, |
5842 | | int64_t offset, data_content4 what, nfs_cb cb, |
5843 | | void *private_data) |
5844 | 0 | { |
5845 | 0 | COMPOUND4args args; |
5846 | 0 | nfs_argop4 op[2 + 1]; |
5847 | 0 | struct nfs4_cb_data *data; |
5848 | 0 | int i; |
5849 | |
|
5850 | 0 | if (offset < 0) { |
5851 | 0 | nfs_set_error(nfs, "Negative offset for lseek(SEEK_%s)", |
5852 | 0 | what == NFS4_CONTENT_HOLE ? "HOLE" : "DATA"); |
5853 | 0 | return -EINVAL; |
5854 | 0 | } |
5855 | | |
5856 | 0 | data = calloc(1, sizeof(*data)); |
5857 | 0 | if (data == NULL) { |
5858 | 0 | nfs_set_error(nfs, "Out of memory."); |
5859 | 0 | return -1; |
5860 | 0 | } |
5861 | 0 | data->nfs = nfs; |
5862 | 0 | data->cb = cb; |
5863 | 0 | data->private_data = private_data; |
5864 | 0 | data->filler.blob0.val = fh; |
5865 | 0 | data->filler.blob0.free = NULL; |
5866 | |
|
5867 | 0 | memset(op, 0, sizeof(op)); |
5868 | 0 | i = nfs4_start_compound(nfs, op); |
5869 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5870 | 0 | i += nfs42_op_seek(nfs, &op[i], fh, (uint64_t)offset, what); |
5871 | |
|
5872 | 0 | memset(&args, 0, sizeof(args)); |
5873 | 0 | args.argarray.argarray_len = i; |
5874 | 0 | args.argarray.argarray_val = op; |
5875 | |
|
5876 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs42_seek_cb, &args, |
5877 | 0 | data) == NULL) { |
5878 | 0 | nfs_set_error(nfs, "Failed to queue SEEK. %s", |
5879 | 0 | rpc_get_error(nfs->rpc)); |
5880 | 0 | free_nfs4_cb_data(data); |
5881 | 0 | return -1; |
5882 | 0 | } |
5883 | | |
5884 | 0 | return 0; |
5885 | 0 | } |
5886 | | #endif /* HAVE_NFS4_2 */ |
5887 | | |
5888 | | /* blob0.val is nfsfh |
5889 | | * blob1.val is offset |
5890 | | */ |
5891 | | int |
5892 | | nfs4_lseek_async(struct nfs_context *nfs, struct nfsfh *fh, int64_t offset, |
5893 | | int whence, nfs_cb cb, void *private_data) |
5894 | 0 | { |
5895 | 0 | COMPOUND4args args; |
5896 | 0 | nfs_argop4 op[2 + 1]; |
5897 | 0 | struct nfs4_cb_data *data; |
5898 | 0 | int i; |
5899 | |
|
5900 | 0 | if (whence == SEEK_SET) { |
5901 | 0 | if (offset < 0) { |
5902 | 0 | nfs_set_error(nfs, "Negative offset for lseek(" |
5903 | 0 | "SEET_SET)"); |
5904 | 0 | cb(-EINVAL, nfs, &fh->offset, private_data); |
5905 | 0 | } else { |
5906 | 0 | fh->offset = offset; |
5907 | 0 | cb(0, nfs, &fh->offset, private_data); |
5908 | 0 | } |
5909 | 0 | return 0; |
5910 | 0 | } |
5911 | 0 | #ifdef HAVE_NFS4_2 |
5912 | | /* |
5913 | | * Finding the next hole or the next data is a server side question, |
5914 | | * and only NFSv4.2 can ask it. |
5915 | | */ |
5916 | 0 | if (whence == SEEK_HOLE || whence == SEEK_DATA) { |
5917 | 0 | if (!nfs_is_v4_2(nfs)) { |
5918 | 0 | nfs_set_error(nfs, "lseek(SEEK_%s) requires NFSv4.2", |
5919 | 0 | whence == SEEK_HOLE ? "HOLE" : "DATA"); |
5920 | 0 | return -ENOTSUP; |
5921 | 0 | } |
5922 | 0 | return nfs42_lseek_content_async(nfs, fh, offset, |
5923 | 0 | whence == SEEK_HOLE ? |
5924 | 0 | NFS4_CONTENT_HOLE : |
5925 | 0 | NFS4_CONTENT_DATA, |
5926 | 0 | cb, private_data); |
5927 | 0 | } |
5928 | 0 | #endif /* HAVE_NFS4_2 */ |
5929 | 0 | if (whence == SEEK_CUR) { |
5930 | 0 | if (offset < 0 && |
5931 | 0 | fh->offset < (uint64_t)(-offset)) { |
5932 | 0 | nfs_set_error(nfs, "Negative offset for lseek(" |
5933 | 0 | "SEET_CUR)"); |
5934 | 0 | cb(-EINVAL, nfs, &fh->offset, private_data); |
5935 | 0 | } else { |
5936 | 0 | fh->offset += offset; |
5937 | 0 | cb(0, nfs, &fh->offset, private_data); |
5938 | 0 | } |
5939 | 0 | return 0; |
5940 | 0 | } |
5941 | | |
5942 | | /* |
5943 | | * Anything left has to be SEEK_END. Falling through with an |
5944 | | * unrecognised whence silently treated it as SEEK_END. |
5945 | | */ |
5946 | 0 | if (whence != SEEK_END) { |
5947 | 0 | nfs_set_error(nfs, "Invalid whence %d for lseek()", whence); |
5948 | 0 | return -EINVAL; |
5949 | 0 | } |
5950 | | |
5951 | 0 | data = calloc(1, sizeof(*data)); |
5952 | 0 | if (data == NULL) { |
5953 | 0 | nfs_set_error(nfs, "Out of memory."); |
5954 | 0 | return -1; |
5955 | 0 | } |
5956 | | |
5957 | 0 | data->nfs = nfs; |
5958 | 0 | data->cb = cb; |
5959 | 0 | data->private_data = private_data; |
5960 | |
|
5961 | 0 | data->filler.blob0.val = fh; |
5962 | 0 | data->filler.blob0.free = NULL; |
5963 | |
|
5964 | 0 | data->filler.blob1.val = malloc(sizeof(uint64_t)); |
5965 | 0 | if (data->filler.blob1.val == NULL) { |
5966 | 0 | nfs_set_error(nfs, "Out of memory."); |
5967 | 0 | free_nfs4_cb_data(data); |
5968 | 0 | return -1; |
5969 | 0 | } |
5970 | | /* |
5971 | | * free_nfs4_cb_data() only releases a blob that carries a free |
5972 | | * function, so without this the offset buffer leaks on every |
5973 | | * NFSv4 lseek that has to ask the server. |
5974 | | */ |
5975 | 0 | data->filler.blob1.free = free; |
5976 | 0 | memcpy(data->filler.blob1.val, &offset, sizeof(uint64_t)); |
5977 | |
|
5978 | 0 | i = nfs4_start_compound(nfs, op); |
5979 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
5980 | 0 | i += nfs4_op_getattr(nfs, &op[i], standard_attributes, 2); |
5981 | |
|
5982 | 0 | memset(&args, 0, sizeof(args)); |
5983 | 0 | args.argarray.argarray_len = i; |
5984 | 0 | args.argarray.argarray_val = op; |
5985 | |
|
5986 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_lseek_cb, &args, |
5987 | 0 | data) == NULL) { |
5988 | 0 | free_nfs4_cb_data(data); |
5989 | 0 | return -1; |
5990 | 0 | } |
5991 | | |
5992 | 0 | return 0; |
5993 | 0 | } |
5994 | | |
5995 | | static void |
5996 | | nfs4_lockf_cb(struct rpc_context *rpc, int status, void *command_data, |
5997 | | void *private_data) |
5998 | 0 | { |
5999 | 0 | struct nfs4_cb_data *data = private_data; |
6000 | 0 | struct nfs_context *nfs = data->nfs; |
6001 | 0 | COMPOUND4res *res = command_data; |
6002 | 0 | LOCK4resok *lresok = NULL; |
6003 | 0 | LOCKU4res *lures = NULL; |
6004 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
6005 | 0 | enum nfs4_lock_op cmd; |
6006 | 0 | int i; |
6007 | |
|
6008 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
6009 | |
|
6010 | 0 | cmd = data->filler.blob1.len; |
6011 | |
|
6012 | 0 | if (check_nfs4_error(nfs, status, data, res, "LOCKF")) { |
6013 | 0 | return; |
6014 | 0 | } |
6015 | | |
6016 | 0 | switch (cmd) { |
6017 | 0 | case NFS4_F_LOCK: |
6018 | 0 | case NFS4_F_TLOCK: |
6019 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_LOCK, "LOCK")) < 0) { |
6020 | 0 | return; |
6021 | 0 | } |
6022 | | |
6023 | 0 | lresok = &res->resarray.resarray_val[i].nfs_resop4_u.oplock.LOCK4res_u.resok4; |
6024 | 0 | nfs->nfsi->has_lock_owner = 1; |
6025 | 0 | fh->lock_stateid.seqid = lresok->lock_stateid.seqid; |
6026 | 0 | memcpy(fh->lock_stateid.other, lresok->lock_stateid.other, 12); |
6027 | 0 | break; |
6028 | 0 | case NFS4_F_ULOCK: |
6029 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_LOCKU, "LOCKU")) < 0) { |
6030 | 0 | return; |
6031 | 0 | } |
6032 | 0 | lures = &res->resarray.resarray_val[i].nfs_resop4_u.oplocku; |
6033 | 0 | fh->lock_stateid.seqid = lures->LOCKU4res_u.lock_stateid.seqid; |
6034 | 0 | memcpy(fh->lock_stateid.other, |
6035 | 0 | lures->LOCKU4res_u.lock_stateid.other, 12); |
6036 | 0 | break; |
6037 | 0 | case NFS4_F_TEST: |
6038 | 0 | break; |
6039 | 0 | } |
6040 | | |
6041 | 0 | data->cb(0, nfs, NULL, data->private_data); |
6042 | |
|
6043 | 0 | free_nfs4_cb_data(data); |
6044 | 0 | } |
6045 | | |
6046 | | /* blob0.val is nfsfh |
6047 | | * blob1.len is cmd |
6048 | | */ |
6049 | | int |
6050 | | nfs4_lockf_async(struct nfs_context *nfs, struct nfsfh *fh, |
6051 | | enum nfs4_lock_op cmd, uint64_t count, |
6052 | | nfs_cb cb, void *private_data) |
6053 | 0 | { |
6054 | 0 | COMPOUND4args args; |
6055 | 0 | nfs_argop4 op[2 + 1]; |
6056 | 0 | struct nfs4_cb_data *data; |
6057 | 0 | int i; |
6058 | |
|
6059 | 0 | data = calloc(1, sizeof(*data)); |
6060 | 0 | if (data == NULL) { |
6061 | 0 | nfs_set_error(nfs, "Out of memory."); |
6062 | 0 | return -1; |
6063 | 0 | } |
6064 | | |
6065 | 0 | data->nfs = nfs; |
6066 | 0 | data->cb = cb; |
6067 | 0 | data->private_data = private_data; |
6068 | |
|
6069 | 0 | data->filler.blob0.val = fh; |
6070 | 0 | data->filler.blob0.free = NULL; |
6071 | |
|
6072 | 0 | data->filler.blob1.len = cmd; |
6073 | |
|
6074 | 0 | i = nfs4_start_compound(nfs, op); |
6075 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
6076 | 0 | switch (cmd) { |
6077 | 0 | case NFS4_F_LOCK: |
6078 | 0 | i += nfs4_op_lock(nfs, &op[i], fh, OP_LOCK, WRITEW_LT, |
6079 | 0 | 0, fh->offset, count); |
6080 | 0 | break; |
6081 | 0 | case NFS4_F_TLOCK: |
6082 | 0 | i += nfs4_op_lock(nfs, &op[i], fh, OP_LOCK, WRITE_LT, |
6083 | 0 | 0, fh->offset, count); |
6084 | 0 | break; |
6085 | 0 | case NFS4_F_ULOCK: |
6086 | 0 | i += nfs4_op_locku(nfs, &op[i], fh, WRITE_LT, |
6087 | 0 | fh->offset, count); |
6088 | 0 | break; |
6089 | 0 | case NFS4_F_TEST: |
6090 | 0 | i += nfs4_op_lockt(nfs, &op[i], fh, WRITEW_LT, |
6091 | 0 | fh->offset, count); |
6092 | 0 | break; |
6093 | 0 | } |
6094 | | |
6095 | 0 | memset(&args, 0, sizeof(args)); |
6096 | 0 | args.argarray.argarray_len = i; |
6097 | 0 | args.argarray.argarray_val = op; |
6098 | |
|
6099 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_lockf_cb, &args, |
6100 | 0 | data) == NULL) { |
6101 | 0 | free_nfs4_cb_data(data); |
6102 | 0 | return -1; |
6103 | 0 | } |
6104 | | |
6105 | 0 | return 0; |
6106 | 0 | } |
6107 | | |
6108 | | static void |
6109 | | nfs4_fcntl_cb(struct rpc_context *rpc, int status, void *command_data, |
6110 | | void *private_data) |
6111 | 0 | { |
6112 | 0 | struct nfs4_cb_data *data = private_data; |
6113 | 0 | struct nfs_context *nfs = data->nfs; |
6114 | 0 | COMPOUND4res *res = command_data; |
6115 | 0 | LOCK4resok *lresok = NULL; |
6116 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
6117 | 0 | enum nfs4_fcntl_op cmd; |
6118 | 0 | struct nfs4_flock *fl; |
6119 | 0 | int i; |
6120 | |
|
6121 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
6122 | |
|
6123 | 0 | cmd = data->filler.blob1.len; |
6124 | |
|
6125 | 0 | if (check_nfs4_error(nfs, status, data, res, "FCNTL")) { |
6126 | 0 | return; |
6127 | 0 | } |
6128 | | |
6129 | 0 | switch (cmd) { |
6130 | 0 | case NFS4_F_SETLK: |
6131 | 0 | case NFS4_F_SETLKW: |
6132 | 0 | fl = (struct nfs4_flock *)data->filler.blob1.val; |
6133 | |
|
6134 | 0 | switch (fl->l_type) { |
6135 | 0 | case F_RDLCK: |
6136 | 0 | case F_WRLCK: |
6137 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_LOCK, |
6138 | 0 | "LOCK")) < 0) { |
6139 | 0 | return; |
6140 | 0 | } |
6141 | | |
6142 | 0 | lresok = &res->resarray.resarray_val[i].nfs_resop4_u.oplock.LOCK4res_u.resok4; |
6143 | 0 | nfs->nfsi->has_lock_owner = 1; |
6144 | 0 | fh->lock_stateid.seqid = lresok->lock_stateid.seqid; |
6145 | 0 | memcpy(fh->lock_stateid.other, |
6146 | 0 | lresok->lock_stateid.other, 12); |
6147 | 0 | break; |
6148 | 0 | case F_UNLCK: |
6149 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_LOCKU, |
6150 | 0 | "UNLOCK")) < 0) { |
6151 | 0 | return; |
6152 | 0 | } |
6153 | 0 | break; |
6154 | 0 | } |
6155 | 0 | break; |
6156 | 0 | } |
6157 | | |
6158 | 0 | data->cb(0, nfs, NULL, data->private_data); |
6159 | |
|
6160 | 0 | free_nfs4_cb_data(data); |
6161 | 0 | } |
6162 | | |
6163 | | static int |
6164 | | nfs4_fcntl_async_internal(struct nfs_context *nfs, struct nfsfh *fh, |
6165 | | struct nfs4_cb_data *data) |
6166 | 0 | { |
6167 | 0 | COMPOUND4args args; |
6168 | 0 | nfs_argop4 op[2 + 1]; |
6169 | 0 | struct nfs4_flock *fl; |
6170 | 0 | enum nfs4_fcntl_op cmd; |
6171 | 0 | int i, lock_type; |
6172 | |
|
6173 | 0 | cmd = data->filler.blob1.len; |
6174 | |
|
6175 | 0 | i = nfs4_start_compound(nfs, op); |
6176 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
6177 | 0 | switch (cmd) { |
6178 | 0 | case NFS4_F_SETLK: |
6179 | 0 | case NFS4_F_SETLKW: |
6180 | 0 | fl = data->filler.blob1.val; |
6181 | |
|
6182 | 0 | switch (fl->l_type) { |
6183 | 0 | case F_RDLCK: |
6184 | 0 | lock_type = cmd == NFS4_F_SETLK ? READ_LT : READW_LT; |
6185 | 0 | i += nfs4_op_lock(nfs, &op[i], fh, OP_LOCK, lock_type, |
6186 | 0 | 0, fl->l_start, fl->l_len); |
6187 | 0 | break; |
6188 | 0 | case F_WRLCK: |
6189 | 0 | lock_type = cmd == NFS4_F_SETLK ? WRITE_LT : WRITEW_LT; |
6190 | 0 | i += nfs4_op_lock(nfs, &op[i], fh, OP_LOCK, lock_type, |
6191 | 0 | 0, fl->l_start, fl->l_len); |
6192 | 0 | break; |
6193 | 0 | case F_UNLCK: |
6194 | 0 | i += nfs4_op_locku(nfs, &op[i], fh, WRITE_LT, |
6195 | 0 | fl->l_start, fl->l_len); |
6196 | 0 | break; |
6197 | 0 | } |
6198 | 0 | break; |
6199 | 0 | } |
6200 | | |
6201 | 0 | memset(&args, 0, sizeof(args)); |
6202 | 0 | args.argarray.argarray_len = i; |
6203 | 0 | args.argarray.argarray_val = op; |
6204 | |
|
6205 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_fcntl_cb, &args, |
6206 | 0 | data) == NULL) { |
6207 | 0 | free_nfs4_cb_data(data); |
6208 | 0 | return -1; |
6209 | 0 | } |
6210 | | |
6211 | 0 | return 0; |
6212 | 0 | } |
6213 | | |
6214 | | static void |
6215 | | nfs4_fcntl_stat_cb(struct rpc_context *rpc, int status, void *command_data, |
6216 | | void *private_data) |
6217 | 0 | { |
6218 | 0 | struct nfs4_cb_data *data = private_data; |
6219 | 0 | struct nfs_context *nfs = data->nfs; |
6220 | 0 | struct nfsfh *fh = data->filler.blob0.val; |
6221 | 0 | enum nfs4_fcntl_op cmd = data->filler.blob1.len; |
6222 | 0 | COMPOUND4res *res = command_data; |
6223 | 0 | GETATTR4resok *garesok; |
6224 | 0 | struct nfs4_flock *fl; |
6225 | 0 | struct nfs_stat_64 st; |
6226 | 0 | int i; |
6227 | |
|
6228 | 0 | if (check_nfs4_error(nfs, status, data, res, "STAT64")) { |
6229 | 0 | return; |
6230 | 0 | } |
6231 | | |
6232 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_GETATTR, "GETATTR")) < 0) { |
6233 | 0 | return; |
6234 | 0 | } |
6235 | 0 | garesok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetattr.GETATTR4res_u.resok4; |
6236 | 0 | memset(&st, 0, sizeof(st)); |
6237 | 0 | if (nfs_parse_attributes(nfs, data, &st, NULL, NULL, |
6238 | 0 | &garesok->obj_attributes.attrmask, |
6239 | 0 | garesok->obj_attributes.attr_vals.attrlist4_val, |
6240 | 0 | garesok->obj_attributes.attr_vals.attrlist4_len) < 0) { |
6241 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
6242 | 0 | free_nfs4_cb_data(data); |
6243 | 0 | return; |
6244 | 0 | } |
6245 | | |
6246 | 0 | switch (cmd) { |
6247 | 0 | case NFS4_F_SETLK: |
6248 | 0 | case NFS4_F_SETLKW: |
6249 | 0 | fl = data->filler.blob1.val; |
6250 | |
|
6251 | 0 | fl->l_whence = SEEK_SET; |
6252 | 0 | fl->l_start = st.nfs_size + fl->l_start; |
6253 | 0 | if (nfs4_fcntl_async_internal(nfs, fh, data)) { |
6254 | 0 | data->cb(-ENOMEM, nfs, nfs_get_error(nfs), |
6255 | 0 | data->private_data); |
6256 | 0 | free_nfs4_cb_data(data); |
6257 | 0 | } |
6258 | 0 | } |
6259 | 0 | } |
6260 | | |
6261 | | /* blob0.val is nfsfh |
6262 | | * blob1.len is cmd |
6263 | | * blob1.val is arg |
6264 | | */ |
6265 | | int |
6266 | | nfs4_fcntl_async(struct nfs_context *nfs, struct nfsfh *fh, |
6267 | | enum nfs4_fcntl_op cmd, void *arg, |
6268 | | nfs_cb cb, void *private_data) |
6269 | 0 | { |
6270 | 0 | struct nfs4_cb_data *data; |
6271 | 0 | struct nfs4_flock *fl; |
6272 | 0 | COMPOUND4args args; |
6273 | 0 | nfs_argop4 op[2 + 1]; |
6274 | 0 | int i; |
6275 | |
|
6276 | 0 | data = calloc(1, sizeof(*data)); |
6277 | 0 | if (data == NULL) { |
6278 | 0 | nfs_set_error(nfs, "Out of memory."); |
6279 | 0 | return -1; |
6280 | 0 | } |
6281 | | |
6282 | 0 | data->nfs = nfs; |
6283 | 0 | data->cb = cb; |
6284 | 0 | data->private_data = private_data; |
6285 | |
|
6286 | 0 | data->filler.blob0.val = fh; |
6287 | 0 | data->filler.blob0.free = NULL; |
6288 | |
|
6289 | 0 | data->filler.blob1.len = cmd; |
6290 | 0 | data->filler.blob1.val = arg; |
6291 | 0 | data->filler.blob1.free = NULL; |
6292 | |
|
6293 | 0 | switch (cmd) { |
6294 | 0 | case NFS4_F_SETLK: |
6295 | 0 | case NFS4_F_SETLKW: |
6296 | 0 | fl = arg; |
6297 | 0 | switch (fl->l_whence) { |
6298 | 0 | case SEEK_SET: |
6299 | 0 | return nfs4_fcntl_async_internal(nfs, fh, data); |
6300 | 0 | case SEEK_CUR: |
6301 | 0 | fl->l_whence = SEEK_SET; |
6302 | 0 | fl->l_start = fh->offset + fl->l_start; |
6303 | 0 | return nfs4_fcntl_async_internal(nfs, fh, data); |
6304 | 0 | case SEEK_END: |
6305 | 0 | i = nfs4_start_compound(nfs, op); |
6306 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
6307 | 0 | i += nfs4_op_getattr(nfs, &op[i], standard_attributes, |
6308 | 0 | 2); |
6309 | |
|
6310 | 0 | memset(&args, 0, sizeof(args)); |
6311 | 0 | args.argarray.argarray_len = i; |
6312 | 0 | args.argarray.argarray_val = op; |
6313 | |
|
6314 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, |
6315 | 0 | nfs4_fcntl_stat_cb, |
6316 | 0 | &args, data) == NULL) { |
6317 | 0 | free_nfs4_cb_data(data); |
6318 | 0 | return -1; |
6319 | 0 | } |
6320 | 0 | return 0; |
6321 | 0 | } |
6322 | 0 | nfs_set_error(nfs, "fcntl: unknown fl->whence:%d\n", |
6323 | 0 | fl->l_whence); |
6324 | 0 | free_nfs4_cb_data(data); |
6325 | 0 | return -1; |
6326 | 0 | } |
6327 | 0 | nfs_set_error(nfs, "fcntl: unknown cmd:%d\n", cmd); |
6328 | 0 | free_nfs4_cb_data(data); |
6329 | 0 | return -1; |
6330 | 0 | } |
6331 | | |
6332 | | static int |
6333 | | nfs_parse_statvfs(struct nfs_context *nfs, struct nfs4_cb_data *data, |
6334 | | struct statvfs *svfs, const char *buf, int len) |
6335 | 0 | { |
6336 | 0 | uint64_t u64; |
6337 | 0 | uint32_t u32; |
6338 | |
|
6339 | 0 | svfs->f_bsize = NFS_BLKSIZE; |
6340 | 0 | svfs->f_frsize = NFS_BLKSIZE; |
6341 | |
|
6342 | 0 | #if !defined(__ANDROID__) |
6343 | 0 | svfs->f_flag = 0; |
6344 | 0 | #endif |
6345 | | |
6346 | | /* FSID |
6347 | | * NFSv4 FSID is 2*64 bit but statvfs fsid is just an |
6348 | | * unsigmed long. Mix the 2*64 bits and hope for the best. |
6349 | | */ |
6350 | 0 | CHECK_GETATTR_BUF_SPACE(len, 16); |
6351 | 0 | memcpy(&u64, buf, 8); |
6352 | 0 | svfs->f_fsid = (unsigned long)nfs_ntoh64(u64); |
6353 | 0 | buf += 8; |
6354 | 0 | len -= 8; |
6355 | 0 | memcpy(&u64, buf, 8); |
6356 | 0 | svfs->f_fsid |= (unsigned long)nfs_ntoh64(u64); |
6357 | 0 | buf += 8; |
6358 | 0 | len -= 8; |
6359 | | |
6360 | | /* Files Avail */ |
6361 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6362 | 0 | memcpy(&u64, buf, 8); |
6363 | 0 | #if !defined(__ANDROID__) |
6364 | 0 | svfs->f_favail = (fsfilcnt_t)nfs_ntoh64(u64); |
6365 | 0 | #endif |
6366 | 0 | buf += 8; |
6367 | 0 | len -= 8; |
6368 | | |
6369 | | /* Files Free */ |
6370 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6371 | 0 | memcpy(&u64, buf, 8); |
6372 | 0 | svfs->f_ffree = (fsfilcnt_t)nfs_ntoh64(u64); |
6373 | 0 | buf += 8; |
6374 | 0 | len -= 8; |
6375 | | |
6376 | | /* Files Total */ |
6377 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6378 | 0 | memcpy(&u64, buf, 8); |
6379 | 0 | svfs->f_files = (fsfilcnt_t)nfs_ntoh64(u64); |
6380 | 0 | buf += 8; |
6381 | 0 | len -= 8; |
6382 | | |
6383 | | /* Max Name */ |
6384 | 0 | CHECK_GETATTR_BUF_SPACE(len, 4); |
6385 | 0 | memcpy(&u32, buf, 4); |
6386 | 0 | #if !defined(__ANDROID__) |
6387 | 0 | svfs->f_namemax = ntohl(u32); |
6388 | 0 | #endif |
6389 | 0 | buf += 4; |
6390 | 0 | len -= 4; |
6391 | | |
6392 | | /* Space Avail */ |
6393 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6394 | 0 | memcpy(&u64, buf, 8); |
6395 | 0 | svfs->f_bavail = (fsblkcnt_t)(nfs_ntoh64(u64) / svfs->f_frsize); |
6396 | 0 | buf += 8; |
6397 | 0 | len -= 8; |
6398 | | |
6399 | | /* Space Free */ |
6400 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6401 | 0 | memcpy(&u64, buf, 8); |
6402 | 0 | svfs->f_bfree = (fsblkcnt_t)(nfs_ntoh64(u64) / svfs->f_frsize); |
6403 | 0 | buf += 8; |
6404 | 0 | len -= 8; |
6405 | | |
6406 | | /* Space Total */ |
6407 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6408 | 0 | memcpy(&u64, buf, 8); |
6409 | 0 | svfs->f_blocks = (fsblkcnt_t)(nfs_ntoh64(u64) / svfs->f_frsize); |
6410 | 0 | buf += 8; |
6411 | 0 | len -= 8; |
6412 | |
|
6413 | 0 | return 0; |
6414 | 0 | } |
6415 | | |
6416 | | static int |
6417 | | nfs_parse_statvfs64(struct nfs_context *nfs, struct nfs4_cb_data *data, |
6418 | | struct nfs_statvfs_64 *svfs64, const char *buf, int len) |
6419 | 0 | { |
6420 | 0 | uint64_t u64; |
6421 | 0 | uint32_t u32; |
6422 | |
|
6423 | 0 | svfs64->f_bsize = NFS_BLKSIZE; |
6424 | 0 | svfs64->f_frsize = NFS_BLKSIZE; |
6425 | 0 | svfs64->f_flag = 0; |
6426 | | |
6427 | | /* FSID |
6428 | | * NFSv4 FSID is 2*64 bit but statvfs fsid is just an |
6429 | | * unsigmed long. Mix the 2*64 bits and hope for the best. |
6430 | | */ |
6431 | 0 | CHECK_GETATTR_BUF_SPACE(len, 16); |
6432 | 0 | memcpy(&u64, buf, 8); |
6433 | 0 | svfs64->f_fsid = nfs_ntoh64(u64); |
6434 | 0 | buf += 8; |
6435 | 0 | len -= 8; |
6436 | 0 | memcpy(&u64, buf, 8); |
6437 | 0 | svfs64->f_fsid |= nfs_ntoh64(u64); |
6438 | 0 | buf += 8; |
6439 | 0 | len -= 8; |
6440 | | |
6441 | | /* Files Avail */ |
6442 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6443 | 0 | memcpy(&u64, buf, 8); |
6444 | 0 | svfs64->f_favail = nfs_ntoh64(u64); |
6445 | 0 | buf += 8; |
6446 | 0 | len -= 8; |
6447 | | |
6448 | | /* Files Free */ |
6449 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6450 | 0 | memcpy(&u64, buf, 8); |
6451 | 0 | svfs64->f_ffree = nfs_ntoh64(u64); |
6452 | 0 | buf += 8; |
6453 | 0 | len -= 8; |
6454 | | |
6455 | | /* Files Total */ |
6456 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6457 | 0 | memcpy(&u64, buf, 8); |
6458 | 0 | svfs64->f_files = nfs_ntoh64(u64); |
6459 | 0 | buf += 8; |
6460 | 0 | len -= 8; |
6461 | | |
6462 | | /* Max Name */ |
6463 | 0 | CHECK_GETATTR_BUF_SPACE(len, 4); |
6464 | 0 | memcpy(&u32, buf, 4); |
6465 | 0 | svfs64->f_namemax = ntohl(u32); |
6466 | 0 | buf += 4; |
6467 | 0 | len -= 4; |
6468 | | |
6469 | | /* Space Avail */ |
6470 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6471 | 0 | memcpy(&u64, buf, 8); |
6472 | 0 | svfs64->f_bavail = nfs_ntoh64(u64) / svfs64->f_frsize; |
6473 | 0 | buf += 8; |
6474 | 0 | len -= 8; |
6475 | | |
6476 | | /* Space Free */ |
6477 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6478 | 0 | memcpy(&u64, buf, 8); |
6479 | 0 | svfs64->f_bfree = nfs_ntoh64(u64) / svfs64->f_frsize; |
6480 | 0 | buf += 8; |
6481 | 0 | len -= 8; |
6482 | | |
6483 | | /* Space Total */ |
6484 | 0 | CHECK_GETATTR_BUF_SPACE(len, 8); |
6485 | 0 | memcpy(&u64, buf, 8); |
6486 | 0 | svfs64->f_blocks = nfs_ntoh64(u64) / svfs64->f_frsize; |
6487 | 0 | buf += 8; |
6488 | 0 | len -= 8; |
6489 | |
|
6490 | 0 | return 0; |
6491 | 0 | } |
6492 | | |
6493 | | static void |
6494 | | nfs4_statvfs_cb(struct rpc_context *rpc, int status, void *command_data, |
6495 | | void *private_data) |
6496 | 0 | { |
6497 | 0 | struct nfs4_cb_data *data = private_data; |
6498 | 0 | struct nfs_context *nfs = data->nfs; |
6499 | 0 | COMPOUND4res *res = command_data; |
6500 | 0 | GETATTR4resok *garesok; |
6501 | 0 | struct statvfs svfs; |
6502 | 0 | struct nfs_statvfs_64 svfs64; |
6503 | 0 | int i; |
6504 | |
|
6505 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
6506 | |
|
6507 | 0 | if (check_nfs4_error(nfs, status, data, res, "STATVFS")) { |
6508 | 0 | return; |
6509 | 0 | } |
6510 | | |
6511 | 0 | memset(&svfs, 0, sizeof(svfs)); |
6512 | 0 | memset(&svfs64, 0, sizeof(svfs64)); |
6513 | |
|
6514 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_GETATTR, "GETATTR")) < 0) { |
6515 | 0 | return; |
6516 | 0 | } |
6517 | 0 | garesok = &res->resarray.resarray_val[i].nfs_resop4_u.opgetattr.GETATTR4res_u.resok4; |
6518 | |
|
6519 | 0 | if (data->flags & LOOKUP_FLAG_IS_STATVFS64) { |
6520 | | /* statvfs64 */ |
6521 | 0 | if (nfs_parse_statvfs64(nfs, data, &svfs64, |
6522 | 0 | garesok->obj_attributes.attr_vals.attrlist4_val, |
6523 | 0 | garesok->obj_attributes.attr_vals.attrlist4_len) < 0) { |
6524 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
6525 | 0 | free_nfs4_cb_data(data); |
6526 | 0 | return; |
6527 | 0 | } |
6528 | | |
6529 | 0 | data->cb(0, nfs, &svfs64, data->private_data); |
6530 | 0 | } else { |
6531 | | /* statvfs */ |
6532 | 0 | if (nfs_parse_statvfs(nfs, data, &svfs, |
6533 | 0 | garesok->obj_attributes.attr_vals.attrlist4_val, |
6534 | 0 | garesok->obj_attributes.attr_vals.attrlist4_len) < 0) { |
6535 | 0 | data->cb(-EINVAL, nfs, nfs_get_error(nfs), data->private_data); |
6536 | 0 | free_nfs4_cb_data(data); |
6537 | 0 | return; |
6538 | 0 | } |
6539 | | |
6540 | 0 | data->cb(0, nfs, &svfs, data->private_data); |
6541 | 0 | } |
6542 | | |
6543 | 0 | free_nfs4_cb_data(data); |
6544 | 0 | } |
6545 | | |
6546 | | static int |
6547 | | nfs4_statvfs_async_internal(struct nfs_context *nfs, const char *path, |
6548 | | int is_statvfs64, |
6549 | | nfs_cb cb, void *private_data) |
6550 | 0 | { |
6551 | 0 | struct nfs4_cb_data *data; |
6552 | 0 | COMPOUND4args args; |
6553 | 0 | struct nfsfh fh; |
6554 | 0 | nfs_argop4 op[2 + 1]; |
6555 | 0 | int i; |
6556 | |
|
6557 | 0 | data = calloc(1, sizeof(*data)); |
6558 | 0 | if (data == NULL) { |
6559 | 0 | nfs_set_error(nfs, "Out of memory."); |
6560 | 0 | return -1; |
6561 | 0 | } |
6562 | | |
6563 | 0 | data->nfs = nfs; |
6564 | 0 | data->cb = cb; |
6565 | 0 | data->private_data = private_data; |
6566 | 0 | if (is_statvfs64) { |
6567 | 0 | data->flags |= LOOKUP_FLAG_IS_STATVFS64; |
6568 | 0 | } |
6569 | |
|
6570 | 0 | fh.fh.len = nfs->nfsi->rootfh.len; |
6571 | 0 | fh.fh.val = nfs->nfsi->rootfh.val; |
6572 | |
|
6573 | 0 | i = nfs4_start_compound(nfs, op); |
6574 | 0 | i += nfs4_op_putfh(nfs, &op[i], &fh); |
6575 | 0 | i += nfs4_op_getattr(nfs, &op[i], statvfs_attributes, 2); |
6576 | |
|
6577 | 0 | memset(&args, 0, sizeof(args)); |
6578 | 0 | args.argarray.argarray_len = i; |
6579 | 0 | args.argarray.argarray_val = op; |
6580 | |
|
6581 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_statvfs_cb, &args, |
6582 | 0 | data) == NULL) { |
6583 | 0 | free_nfs4_cb_data(data); |
6584 | 0 | return -1; |
6585 | 0 | } |
6586 | | |
6587 | 0 | return 0; |
6588 | 0 | } |
6589 | | |
6590 | | int |
6591 | | nfs4_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, |
6592 | | void *private_data) |
6593 | 0 | { |
6594 | 0 | return nfs4_statvfs_async_internal(nfs, path, 0, |
6595 | 0 | cb, private_data); |
6596 | 0 | } |
6597 | | |
6598 | | int |
6599 | | nfs4_statvfs64_async(struct nfs_context *nfs, const char *path, nfs_cb cb, |
6600 | | void *private_data) |
6601 | 0 | { |
6602 | 0 | return nfs4_statvfs_async_internal(nfs, path, 1, |
6603 | 0 | cb, private_data); |
6604 | 0 | } |
6605 | | |
6606 | | static void |
6607 | | nfs4_chmod_cb(struct rpc_context *rpc, int status, void *command_data, |
6608 | | void *private_data) |
6609 | 0 | { |
6610 | 0 | struct nfs4_cb_data *data = private_data; |
6611 | 0 | struct nfs_context *nfs = data->nfs; |
6612 | 0 | COMPOUND4res *res = command_data; |
6613 | |
|
6614 | 0 | if (check_nfs4_error(nfs, status, data, res, "CHMOD")) { |
6615 | 0 | return; |
6616 | 0 | } |
6617 | | |
6618 | 0 | data->cb(0, nfs, NULL, data->private_data); |
6619 | 0 | free_nfs4_cb_data(data); |
6620 | 0 | } |
6621 | | |
6622 | | static int |
6623 | | nfs4_populate_chmod(struct nfs4_cb_data *data, nfs_argop4 *op) |
6624 | 0 | { |
6625 | 0 | return nfs4_op_chmod(data->nfs, op, NULL, data->filler.blob3.val); |
6626 | 0 | } |
6627 | | |
6628 | | int |
6629 | | nfs4_chmod_async_internal(struct nfs_context *nfs, const char *path, |
6630 | | int no_follow, int mode, nfs_cb cb, |
6631 | | void *private_data) |
6632 | 0 | { |
6633 | 0 | struct nfs4_cb_data *data; |
6634 | 0 | uint32_t m; |
6635 | |
|
6636 | 0 | data = init_cb_data_full_path(nfs, path); |
6637 | 0 | if (data == NULL) { |
6638 | 0 | return -1; |
6639 | 0 | } |
6640 | | |
6641 | 0 | data->cb = cb; |
6642 | 0 | data->private_data = private_data; |
6643 | 0 | data->filler.func = nfs4_populate_chmod; |
6644 | 0 | data->filler.max_op = 1; |
6645 | |
|
6646 | 0 | if (no_follow) { |
6647 | 0 | data->flags |= LOOKUP_FLAG_NO_FOLLOW; |
6648 | 0 | } |
6649 | |
|
6650 | 0 | data->filler.blob3.val = malloc(sizeof(uint32_t)); |
6651 | 0 | if (data->filler.blob3.val == NULL) { |
6652 | 0 | nfs_set_error(nfs, "Out of memory"); |
6653 | 0 | free_nfs4_cb_data(data); |
6654 | 0 | return -1; |
6655 | 0 | } |
6656 | 0 | data->filler.blob3.free = free; |
6657 | |
|
6658 | 0 | m = htonl(mode); |
6659 | 0 | memcpy(data->filler.blob3.val, &m, sizeof(uint32_t)); |
6660 | |
|
6661 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_chmod_cb) < 0) { |
6662 | 0 | return -1; |
6663 | 0 | } |
6664 | | |
6665 | 0 | return 0; |
6666 | 0 | } |
6667 | | |
6668 | | int |
6669 | | nfs4_fchmod_async(struct nfs_context *nfs, struct nfsfh *fh, int mode, |
6670 | | nfs_cb cb, void *private_data) |
6671 | 0 | { |
6672 | 0 | COMPOUND4args args; |
6673 | 0 | nfs_argop4 op[2 + 1]; |
6674 | 0 | struct nfs4_cb_data *data; |
6675 | 0 | uint32_t m; |
6676 | 0 | int i; |
6677 | |
|
6678 | 0 | data = calloc(1, sizeof(*data)); |
6679 | 0 | if (data == NULL) { |
6680 | 0 | nfs_set_error(nfs, "Out of memory."); |
6681 | 0 | return -1; |
6682 | 0 | } |
6683 | | |
6684 | 0 | data->nfs = nfs; |
6685 | 0 | data->cb = cb; |
6686 | 0 | data->private_data = private_data; |
6687 | |
|
6688 | 0 | data->filler.blob3.val = malloc(sizeof(uint32_t)); |
6689 | 0 | if (data->filler.blob3.val == NULL) { |
6690 | 0 | nfs_set_error(nfs, "Out of memory"); |
6691 | 0 | free_nfs4_cb_data(data); |
6692 | 0 | return -1; |
6693 | 0 | } |
6694 | 0 | data->filler.blob3.free = free; |
6695 | |
|
6696 | 0 | m = htonl(mode); |
6697 | 0 | memcpy(data->filler.blob3.val, &m, sizeof(uint32_t)); |
6698 | | |
6699 | 0 | memset(op, 0, sizeof(op)); |
6700 | |
|
6701 | 0 | i = nfs4_start_compound(nfs, op); |
6702 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
6703 | 0 | i += nfs4_op_chmod(nfs, &op[i], fh, data->filler.blob3.val); |
6704 | |
|
6705 | 0 | memset(&args, 0, sizeof(args)); |
6706 | 0 | args.argarray.argarray_len = i; |
6707 | 0 | args.argarray.argarray_val = op; |
6708 | |
|
6709 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_fsync_cb, &args, |
6710 | 0 | data) == NULL) { |
6711 | 0 | data->filler.blob0.val = NULL; |
6712 | 0 | free_nfs4_cb_data(data); |
6713 | 0 | return -1; |
6714 | 0 | } |
6715 | | |
6716 | 0 | return 0; |
6717 | 0 | } |
6718 | | |
6719 | 0 | #define CHOWN_BLOB_SIZE 64 |
6720 | | static int |
6721 | | nfs4_create_chown_buffer(struct nfs_context *nfs, struct nfs4_cb_data *data, |
6722 | | int uid, int gid) |
6723 | 0 | { |
6724 | 0 | char *str; |
6725 | 0 | int i, l; |
6726 | 0 | uint32_t len; |
6727 | |
|
6728 | 0 | data->filler.blob3.val = calloc(1, CHOWN_BLOB_SIZE); |
6729 | 0 | if (data->filler.blob3.val == NULL) { |
6730 | 0 | nfs_set_error(nfs, "Out of memory"); |
6731 | 0 | return -1; |
6732 | 0 | } |
6733 | 0 | data->filler.blob3.free = free; |
6734 | | |
6735 | 0 | i = 0; |
6736 | 0 | str = data->filler.blob3.val; |
6737 | | /* UID */ |
6738 | 0 | l = snprintf(&str[i + 4], CHOWN_BLOB_SIZE - 4 - i, |
6739 | 0 | "%d", uid); |
6740 | 0 | if (l < 0) { |
6741 | 0 | nfs_set_error(nfs, "snprintf failed"); |
6742 | 0 | return -1; |
6743 | 0 | } |
6744 | 0 | len = htonl(l); |
6745 | | /* UID length prefix */ |
6746 | 0 | memcpy(&str[i], &len, sizeof(uint32_t)); |
6747 | 0 | i += 4 + l; |
6748 | 0 | i = (i + 3) & ~0x03; |
6749 | | |
6750 | | /* GID */ |
6751 | 0 | l = snprintf(&str[i + 4], CHOWN_BLOB_SIZE - 4 - i, |
6752 | 0 | "%d", gid); |
6753 | 0 | if (l < 0) { |
6754 | 0 | nfs_set_error(nfs, "snprintf failed"); |
6755 | 0 | return -1; |
6756 | 0 | } |
6757 | 0 | len = htonl(l); |
6758 | | /* GID length prefix */ |
6759 | 0 | memcpy(&str[i], &len, sizeof(uint32_t)); |
6760 | 0 | i += 4 + l; |
6761 | 0 | i = (i + 3) & ~0x03; |
6762 | |
|
6763 | 0 | data->filler.blob3.len = i; |
6764 | |
|
6765 | 0 | return 0; |
6766 | 0 | } |
6767 | | |
6768 | | static void |
6769 | | nfs4_chown_cb(struct rpc_context *rpc, int status, void *command_data, |
6770 | | void *private_data) |
6771 | 0 | { |
6772 | 0 | struct nfs4_cb_data *data = private_data; |
6773 | 0 | struct nfs_context *nfs = data->nfs; |
6774 | 0 | COMPOUND4res *res = command_data; |
6775 | |
|
6776 | 0 | if (check_nfs4_error(nfs, status, data, res, "OPEN")) { |
6777 | 0 | return; |
6778 | 0 | } |
6779 | | |
6780 | 0 | data->cb(0, nfs, NULL, data->private_data); |
6781 | 0 | free_nfs4_cb_data(data); |
6782 | 0 | } |
6783 | | |
6784 | | static int |
6785 | | nfs4_populate_chown(struct nfs4_cb_data *data, nfs_argop4 *op) |
6786 | 0 | { |
6787 | 0 | return nfs4_op_chown(data->nfs, op, NULL, data->filler.blob3.val, |
6788 | 0 | data->filler.blob3.len); |
6789 | 0 | } |
6790 | | |
6791 | | int |
6792 | | nfs4_chown_async_internal(struct nfs_context *nfs, const char *path, |
6793 | | int no_follow, int uid, int gid, |
6794 | | nfs_cb cb, void *private_data) |
6795 | 0 | { |
6796 | 0 | struct nfs4_cb_data *data; |
6797 | | |
6798 | | /* |
6799 | | * SETATTR applies to the filehandle the compound is left on, so the |
6800 | | * path has to name the file itself. Splitting the last component off |
6801 | | * left the compound sitting on the parent directory and chowned that |
6802 | | * instead, silently and successfully. chmod and utimes, which build |
6803 | | * the same shape of compound, already use the full path. |
6804 | | */ |
6805 | 0 | data = init_cb_data_full_path(nfs, path); |
6806 | 0 | if (data == NULL) { |
6807 | 0 | return -1; |
6808 | 0 | } |
6809 | | |
6810 | 0 | data->cb = cb; |
6811 | 0 | data->private_data = private_data; |
6812 | 0 | data->filler.func = nfs4_populate_chown; |
6813 | 0 | data->filler.max_op = 1; |
6814 | |
|
6815 | 0 | if (no_follow) { |
6816 | 0 | data->flags |= LOOKUP_FLAG_NO_FOLLOW; |
6817 | 0 | } |
6818 | |
|
6819 | 0 | if (nfs4_create_chown_buffer(nfs, data, uid, gid) < 0) { |
6820 | 0 | free_nfs4_cb_data(data); |
6821 | 0 | return -1; |
6822 | 0 | } |
6823 | | |
6824 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_chown_cb) < 0) { |
6825 | 0 | return -1; |
6826 | 0 | } |
6827 | | |
6828 | 0 | return 0; |
6829 | 0 | } |
6830 | | |
6831 | | int |
6832 | | nfs4_fchown_async(struct nfs_context *nfs, struct nfsfh *fh, int uid, int gid, |
6833 | | nfs_cb cb, void *private_data) |
6834 | 0 | { |
6835 | 0 | COMPOUND4args args; |
6836 | 0 | nfs_argop4 op[2 + 1]; |
6837 | 0 | struct nfs4_cb_data *data; |
6838 | 0 | int i; |
6839 | |
|
6840 | 0 | data = calloc(1, sizeof(*data)); |
6841 | 0 | if (data == NULL) { |
6842 | 0 | nfs_set_error(nfs, "Out of memory."); |
6843 | 0 | return -1; |
6844 | 0 | } |
6845 | | |
6846 | 0 | data->nfs = nfs; |
6847 | 0 | data->cb = cb; |
6848 | 0 | data->private_data = private_data; |
6849 | |
|
6850 | 0 | if (nfs4_create_chown_buffer(nfs, data, uid, gid) < 0) { |
6851 | 0 | free_nfs4_cb_data(data); |
6852 | 0 | return -1; |
6853 | 0 | } |
6854 | | |
6855 | 0 | memset(op, 0, sizeof(op)); |
6856 | |
|
6857 | 0 | i = nfs4_start_compound(nfs, op); |
6858 | 0 | i += nfs4_op_putfh(nfs, &op[i], fh); |
6859 | 0 | i += nfs4_op_chown(nfs, &op[i], fh, data->filler.blob3.val, |
6860 | 0 | data->filler.blob3.len); |
6861 | |
|
6862 | 0 | memset(&args, 0, sizeof(args)); |
6863 | 0 | args.argarray.argarray_len = i; |
6864 | 0 | args.argarray.argarray_val = op; |
6865 | |
|
6866 | 0 | if (rpc_nfs4_compound_task(nfs->rpc, nfs4_fsync_cb, &args, |
6867 | 0 | data) == NULL) { |
6868 | 0 | data->filler.blob0.val = NULL; |
6869 | 0 | free_nfs4_cb_data(data); |
6870 | 0 | return -1; |
6871 | 0 | } |
6872 | | |
6873 | 0 | return 0; |
6874 | 0 | } |
6875 | | |
6876 | | static void |
6877 | | nfs4_access_cb(struct rpc_context *rpc, int status, void *command_data, |
6878 | | void *private_data) |
6879 | 0 | { |
6880 | 0 | struct nfs4_cb_data *data = private_data; |
6881 | 0 | struct nfs_context *nfs = data->nfs; |
6882 | 0 | COMPOUND4res *res = command_data; |
6883 | 0 | ACCESS4resok *aresok; |
6884 | 0 | int i; |
6885 | |
|
6886 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
6887 | |
|
6888 | 0 | if (check_nfs4_error(nfs, status, data, res, "ACCESS")) { |
6889 | 0 | return; |
6890 | 0 | } |
6891 | | |
6892 | 0 | if ((i = nfs4_find_op(nfs, data, res, OP_ACCESS, "ACCESS")) < 0) { |
6893 | 0 | return; |
6894 | 0 | } |
6895 | | |
6896 | 0 | aresok = &res->resarray.resarray_val[i].nfs_resop4_u.opaccess.ACCESS4res_u.resok4; |
6897 | | |
6898 | | /* access2 */ |
6899 | 0 | if (data->filler.flags) { |
6900 | 0 | int mode = 0; |
6901 | |
|
6902 | 0 | if (aresok->access & ACCESS4_READ) { |
6903 | 0 | mode |= R_OK; |
6904 | 0 | } |
6905 | 0 | if (aresok->access & ACCESS4_MODIFY) { |
6906 | 0 | mode |= W_OK; |
6907 | 0 | } |
6908 | 0 | if (aresok->access & ACCESS4_EXECUTE) { |
6909 | 0 | mode |= X_OK; |
6910 | 0 | } |
6911 | 0 | data->cb(mode, nfs, NULL, data->private_data); |
6912 | 0 | free_nfs4_cb_data(data); |
6913 | 0 | return; |
6914 | 0 | } |
6915 | | |
6916 | 0 | if (aresok->supported != aresok->access) { |
6917 | 0 | data->cb(-EACCES, nfs, NULL, data->private_data); |
6918 | 0 | free_nfs4_cb_data(data); |
6919 | 0 | return; |
6920 | 0 | } |
6921 | | |
6922 | 0 | data->cb(0, nfs, NULL, data->private_data); |
6923 | 0 | free_nfs4_cb_data(data); |
6924 | 0 | } |
6925 | | |
6926 | | static int |
6927 | | nfs4_access_internal(struct nfs_context *nfs, const char *path, int mode, |
6928 | | int is_access2, nfs_cb cb, void *private_data) |
6929 | 0 | { |
6930 | 0 | struct nfs4_cb_data *data; |
6931 | 0 | uint32_t m; |
6932 | |
|
6933 | 0 | data = init_cb_data_full_path(nfs, path); |
6934 | 0 | if (data == NULL) { |
6935 | 0 | return -1; |
6936 | 0 | } |
6937 | | |
6938 | 0 | data->cb = cb; |
6939 | 0 | data->private_data = private_data; |
6940 | 0 | data->filler.func = nfs4_populate_access; |
6941 | 0 | data->filler.max_op = 1; |
6942 | 0 | data->filler.flags = is_access2; |
6943 | |
|
6944 | 0 | data->filler.blob3.val = malloc(sizeof(uint32_t)); |
6945 | 0 | if (data->filler.blob3.val == NULL) { |
6946 | 0 | nfs_set_error(nfs, "Out of memory"); |
6947 | 0 | return -1; |
6948 | 0 | } |
6949 | 0 | data->filler.blob3.free = free; |
6950 | |
|
6951 | 0 | m = 0; |
6952 | 0 | if (mode & R_OK) { |
6953 | 0 | m |= ACCESS4_READ; |
6954 | 0 | } |
6955 | 0 | if (mode & W_OK) { |
6956 | 0 | m |= ACCESS4_MODIFY; |
6957 | 0 | } |
6958 | 0 | if (mode & X_OK) { |
6959 | 0 | m |= ACCESS4_EXECUTE; |
6960 | 0 | } |
6961 | 0 | memcpy(data->filler.blob3.val, &m, sizeof(uint32_t)); |
6962 | |
|
6963 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_access_cb) < 0) { |
6964 | 0 | free_nfs4_cb_data(data); |
6965 | 0 | return -1; |
6966 | 0 | } |
6967 | | |
6968 | 0 | return 0; |
6969 | 0 | } |
6970 | | |
6971 | | int |
6972 | | nfs4_access_async(struct nfs_context *nfs, const char *path, int mode, |
6973 | | nfs_cb cb, void *private_data) |
6974 | 0 | { |
6975 | 0 | return nfs4_access_internal(nfs, path, mode, 0, |
6976 | 0 | cb, private_data); |
6977 | 0 | } |
6978 | | |
6979 | | int |
6980 | | nfs4_access2_async(struct nfs_context *nfs, const char *path, nfs_cb cb, |
6981 | | void *private_data) |
6982 | 0 | { |
6983 | 0 | return nfs4_access_internal(nfs, path, R_OK|W_OK|X_OK, 1, |
6984 | 0 | cb, private_data); |
6985 | 0 | } |
6986 | | |
6987 | | static void |
6988 | | nfs4_utimes_cb(struct rpc_context *rpc, int status, void *command_data, |
6989 | | void *private_data) |
6990 | 0 | { |
6991 | 0 | struct nfs4_cb_data *data = private_data; |
6992 | 0 | struct nfs_context *nfs = data->nfs; |
6993 | 0 | COMPOUND4res *res = command_data; |
6994 | |
|
6995 | 0 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
6996 | |
|
6997 | 0 | if (check_nfs4_error(nfs, status, data, res, "UTIMES")) { |
6998 | 0 | return; |
6999 | 0 | } |
7000 | | |
7001 | 0 | data->cb(0, nfs, NULL, data->private_data); |
7002 | 0 | free_nfs4_cb_data(data); |
7003 | 0 | } |
7004 | | |
7005 | | static int |
7006 | | nfs4_populate_utimes(struct nfs4_cb_data *data, nfs_argop4 *op) |
7007 | 0 | { |
7008 | 0 | return nfs4_op_utimes(data->nfs, op, NULL, data->filler.blob3.val, |
7009 | 0 | data->filler.blob3.len); |
7010 | 0 | } |
7011 | | |
7012 | | int |
7013 | | nfs4_utimes_async_internal(struct nfs_context *nfs, const char *path, |
7014 | | int no_follow, struct timeval *times, |
7015 | | nfs_cb cb, void *private_data) |
7016 | 0 | { |
7017 | 0 | struct nfs4_cb_data *data; |
7018 | 0 | char *buf; |
7019 | 0 | uint32_t u32; |
7020 | 0 | uint64_t u64; |
7021 | |
|
7022 | 0 | data = init_cb_data_full_path(nfs, path); |
7023 | 0 | if (data == NULL) { |
7024 | 0 | return -1; |
7025 | 0 | } |
7026 | | |
7027 | 0 | data->cb = cb; |
7028 | 0 | data->private_data = private_data; |
7029 | 0 | data->filler.func = nfs4_populate_utimes; |
7030 | 0 | data->filler.max_op = 1; |
7031 | |
|
7032 | 0 | if (no_follow) { |
7033 | 0 | data->flags |= LOOKUP_FLAG_NO_FOLLOW; |
7034 | 0 | } |
7035 | |
|
7036 | 0 | data->filler.blob3.len = 2 * (4 + 8 + 4); |
7037 | 0 | buf = data->filler.blob3.val = calloc(1, data->filler.blob3.len); |
7038 | 0 | if (data->filler.blob3.val == NULL) { |
7039 | 0 | nfs_set_error(nfs, "Out of memory"); |
7040 | 0 | return -1; |
7041 | 0 | } |
7042 | 0 | data->filler.blob3.free = free; |
7043 | |
|
7044 | 0 | if (times != NULL) { |
7045 | | /* atime */ |
7046 | 0 | u32 = htonl(SET_TO_CLIENT_TIME4); |
7047 | 0 | memcpy(buf, &u32, sizeof(uint32_t)); |
7048 | 0 | u64 = nfs_hton64(times[0].tv_sec); |
7049 | 0 | memcpy(buf + 4, &u64, sizeof(uint64_t)); |
7050 | 0 | u32 = htonl(times[0].tv_usec * 1000); |
7051 | 0 | memcpy(buf + 12, &u32, sizeof(uint32_t)); |
7052 | 0 | buf += 16; |
7053 | | /* mtime */ |
7054 | 0 | u32 = htonl(SET_TO_CLIENT_TIME4); |
7055 | 0 | memcpy(buf, &u32, sizeof(uint32_t)); |
7056 | 0 | u64 = nfs_hton64(times[1].tv_sec); |
7057 | 0 | memcpy(buf + 4, &u64, sizeof(uint64_t)); |
7058 | 0 | u32 = htonl(times[1].tv_usec * 1000); |
7059 | 0 | memcpy(buf + 12, &u32, sizeof(uint32_t)); |
7060 | 0 | } else { |
7061 | | /* atime */ |
7062 | 0 | u32 = htonl(SET_TO_SERVER_TIME4); |
7063 | 0 | memcpy(buf, &u32, sizeof(uint32_t)); |
7064 | | /* mtime */ |
7065 | 0 | u32 = htonl(SET_TO_SERVER_TIME4); |
7066 | 0 | memcpy(buf + 4, &u32, sizeof(uint32_t)); |
7067 | 0 | data->filler.blob3.len = 8; |
7068 | 0 | } |
7069 | |
|
7070 | 0 | if (nfs4_lookup_path_async(nfs, data, nfs4_utimes_cb) < 0) { |
7071 | 0 | free_nfs4_cb_data(data); |
7072 | 0 | return -1; |
7073 | 0 | } |
7074 | | |
7075 | 0 | return 0; |
7076 | 0 | } |
7077 | | |
7078 | | int |
7079 | | nfs4_utime_async(struct nfs_context *nfs, const char *path, |
7080 | | struct utimbuf *times, nfs_cb cb, void *private_data) |
7081 | 0 | { |
7082 | 0 | if (times == NULL) { |
7083 | 0 | return nfs4_utimes_async_internal(nfs, path, 0, NULL, |
7084 | 0 | cb, private_data); |
7085 | 0 | } |
7086 | 0 | struct timeval new_times[2]; |
7087 | |
|
7088 | 0 | new_times[0].tv_sec = times->actime; |
7089 | 0 | new_times[0].tv_usec = 0; |
7090 | 0 | new_times[1].tv_sec = times->modtime; |
7091 | 0 | new_times[1].tv_usec = 0; |
7092 | |
|
7093 | 0 | return nfs4_utimes_async_internal(nfs, path, 0, new_times, |
7094 | 0 | cb, private_data); |
7095 | 0 | } |