]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/nfsd/nfs4xdr.c
Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[thirdparty/linux.git] / fs / nfsd / nfs4xdr.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Server-side XDR for NFSv4
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1da177e4
LT
34 */
35
96bcad50 36#include <linux/file.h>
5a0e3ad6 37#include <linux/slab.h>
1da177e4 38#include <linux/namei.h>
341eb184 39#include <linux/statfs.h>
0733d213 40#include <linux/utsname.h>
17456804 41#include <linux/pagemap.h>
4796f457 42#include <linux/sunrpc/svcauth_gss.h>
84e1b21d 43#include <linux/sunrpc/addr.h>
23e50fe3 44#include <linux/xattr.h>
80e591ce
CL
45#include <linux/vmalloc.h>
46
23e50fe3 47#include <uapi/linux/xattr.h>
9a74af21 48
2ca72e17
BF
49#include "idmap.h"
50#include "acl.h"
9a74af21 51#include "xdr4.h"
0a3adade 52#include "vfs.h"
17456804 53#include "state.h"
1091006c 54#include "cache.h"
3d733711 55#include "netns.h"
9cf514cc 56#include "pnfs.h"
5c4583b2 57#include "filecache.h"
51c0d4f7 58#include "nfs4xdr_gen.h"
2ca72e17 59
08281341
CL
60#include "trace.h"
61
18032ca0
DQ
62#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
63#include <linux/security.h>
64#endif
65
66
1da177e4
LT
67#define NFSDDBG_FACILITY NFSDDBG_XDR
68
5cf23dbb 69const u32 nfsd_suppattrs[3][3] = {
916d2d84
BF
70 {NFSD4_SUPPORTED_ATTRS_WORD0,
71 NFSD4_SUPPORTED_ATTRS_WORD1,
72 NFSD4_SUPPORTED_ATTRS_WORD2},
73
74 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
75 NFSD4_1_SUPPORTED_ATTRS_WORD1,
76 NFSD4_1_SUPPORTED_ATTRS_WORD2},
77
78 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
79 NFSD4_1_SUPPORTED_ATTRS_WORD1,
80 NFSD4_2_SUPPORTED_ATTRS_WORD2},
81};
82
42ca0993
BF
83/*
84 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
85 * directory in order to indicate to the client that a filesystem boundary is present
86 * We use a fixed fsid for a referral
87 */
88#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
89#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
90
b37ad28b 91static __be32
a36b1725 92check_filename(char *str, int len)
1da177e4
LT
93{
94 int i;
95
96 if (len == 0)
97 return nfserr_inval;
000dfa18
CL
98 if (len > NFS4_MAXNAMLEN)
99 return nfserr_nametoolong;
1da177e4 100 if (isdotent(str, len))
a36b1725 101 return nfserr_badname;
1da177e4
LT
102 for (i = 0; i < len; i++)
103 if (str[i] == '/')
a36b1725 104 return nfserr_badname;
1da177e4
LT
105 return 0;
106}
107
60adfc50
AA
108static int zero_clientid(clientid_t *clid)
109{
110 return (clid->cl_boot == 0) && (clid->cl_id == 0);
111}
112
2d8498db 113/**
d5e23383 114 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
ce043ac8 115 * @argp: NFSv4 compound argument structure
ed992753 116 * @len: length of buffer to allocate
2d8498db 117 *
ed992753
TM
118 * Allocates a buffer of size @len to be freed when processing the compound
119 * operation described in @argp finishes.
2d8498db 120 */
d5e23383 121static void *
dbc834e5 122svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, size_t len)
1da177e4 123{
d5e23383 124 struct svcxdr_tmpbuf *tb;
1da177e4 125
dbc834e5 126 tb = kmalloc(struct_size(tb, buf, len), GFP_KERNEL);
1da177e4 127 if (!tb)
d5e23383 128 return NULL;
1da177e4
LT
129 tb->next = argp->to_free;
130 argp->to_free = tb;
d5e23383 131 return tb->buf;
1da177e4
LT
132}
133
29c353b3
BF
134/*
135 * For xdr strings that need to be passed to other kernel api's
136 * as null-terminated strings.
137 *
138 * Note null-terminating in place usually isn't safe since the
139 * buffer might end on a page boundary.
140 */
141static char *
dbc834e5 142svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, size_t len)
29c353b3 143{
dbc834e5 144 char *p = svcxdr_tmpalloc(argp, size_add(len, 1));
29c353b3
BF
145
146 if (!p)
147 return NULL;
148 memcpy(p, buf, len);
149 p[len] = '\0';
29c353b3 150 return p;
1da177e4
LT
151}
152
7b723008 153static void *
dbc834e5 154svcxdr_savemem(struct nfsd4_compoundargs *argp, __be32 *p, size_t len)
7b723008
CL
155{
156 __be32 *tmp;
157
158 /*
159 * The location of the decoded data item is stable,
160 * so @p is OK to use. This is the common case.
161 */
162 if (p != argp->xdr->scratch.iov_base)
163 return p;
164
165 tmp = svcxdr_tmpalloc(argp, len);
166 if (!tmp)
167 return NULL;
168 memcpy(tmp, p, len);
169 return tmp;
170}
171
5dcbfabb
CL
172/*
173 * NFSv4 basic data type decoders
174 */
175
1a994408
CL
176/*
177 * This helper handles variable-length opaques which belong to protocol
178 * elements that this implementation does not support.
179 */
180static __be32
181nfsd4_decode_ignored_string(struct nfsd4_compoundargs *argp, u32 maxlen)
182{
183 u32 len;
184
185 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
186 return nfserr_bad_xdr;
187 if (maxlen && len > maxlen)
188 return nfserr_bad_xdr;
189 if (!xdr_inline_decode(argp->xdr, len))
190 return nfserr_bad_xdr;
191
192 return nfs_ok;
193}
194
5dcbfabb
CL
195static __be32
196nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
197{
198 __be32 *p;
199 u32 len;
200
201 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
202 return nfserr_bad_xdr;
203 if (len == 0 || len > NFS4_OPAQUE_LIMIT)
204 return nfserr_bad_xdr;
205 p = xdr_inline_decode(argp->xdr, len);
206 if (!p)
207 return nfserr_bad_xdr;
7b723008 208 o->data = svcxdr_savemem(argp, p, len);
5dcbfabb
CL
209 if (!o->data)
210 return nfserr_jukebox;
211 o->len = len;
5dcbfabb
CL
212
213 return nfs_ok;
214}
215
000dfa18
CL
216static __be32
217nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
218{
219 __be32 *p, status;
220
221 if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
222 return nfserr_bad_xdr;
223 p = xdr_inline_decode(argp->xdr, *lenp);
224 if (!p)
225 return nfserr_bad_xdr;
226 status = check_filename((char *)p, *lenp);
227 if (status)
228 return status;
7b723008 229 *namp = svcxdr_savemem(argp, p, *lenp);
000dfa18
CL
230 if (!*namp)
231 return nfserr_jukebox;
000dfa18
CL
232
233 return nfs_ok;
234}
235
1c3eff7e
CL
236static __be32
237nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
238{
239 __be32 *p;
240
241 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
242 if (!p)
243 return nfserr_bad_xdr;
244 p = xdr_decode_hyper(p, &tv->tv_sec);
245 tv->tv_nsec = be32_to_cpup(p++);
246 if (tv->tv_nsec >= (u32)1000000000)
247 return nfserr_inval;
248 return nfs_ok;
249}
250
796dd1c6
CL
251static __be32
252nfsd4_decode_verifier4(struct nfsd4_compoundargs *argp, nfs4_verifier *verf)
253{
254 __be32 *p;
255
256 p = xdr_inline_decode(argp->xdr, NFS4_VERIFIER_SIZE);
257 if (!p)
258 return nfserr_bad_xdr;
259 memcpy(verf->data, p, sizeof(verf->data));
260 return nfs_ok;
261}
262
d1c263a0
CL
263/**
264 * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
265 * @argp: NFSv4 compound argument structure
266 * @bmval: pointer to an array of u32's to decode into
267 * @bmlen: size of the @bmval array
268 *
269 * The server needs to return nfs_ok rather than nfserr_bad_xdr when
270 * encountering bitmaps containing bits it does not recognize. This
271 * includes bits in bitmap words past WORDn, where WORDn is the last
272 * bitmap WORD the implementation currently supports. Thus we are
273 * careful here to simply ignore bits in bitmap words that this
274 * implementation has yet to support explicitly.
275 *
276 * Return values:
277 * %nfs_ok: @bmval populated successfully
278 * %nfserr_bad_xdr: the encoded bitmap was invalid
279 */
280static __be32
281nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
282{
cd2e999c 283 ssize_t status;
d1c263a0 284
cd2e999c
CL
285 status = xdr_stream_decode_uint32_array(argp->xdr, bmval, bmlen);
286 return status == -EBADMSG ? nfserr_bad_xdr : nfs_ok;
d1c263a0
CL
287}
288
c941a968
CL
289static __be32
290nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
291{
292 __be32 *p, status;
293 u32 length;
294
295 if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
296 return nfserr_bad_xdr;
297 if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
298 return nfserr_bad_xdr;
299 if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
300 return nfserr_bad_xdr;
301
302 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
303 return nfserr_bad_xdr;
304 p = xdr_inline_decode(argp->xdr, length);
305 if (!p)
306 return nfserr_bad_xdr;
307 ace->whotype = nfs4_acl_get_whotype((char *)p, length);
308 if (ace->whotype != NFS4_ACL_WHO_NAMED)
309 status = nfs_ok;
310 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
311 status = nfsd_map_name_to_gid(argp->rqstp,
312 (char *)p, length, &ace->who_gid);
313 else
314 status = nfsd_map_name_to_uid(argp->rqstp,
315 (char *)p, length, &ace->who_uid);
316
317 return status;
318}
319
320/* A counted array of nfsace4's */
321static noinline __be32
322nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
323{
324 struct nfs4_ace *ace;
325 __be32 status;
326 u32 count;
327
328 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
329 return nfserr_bad_xdr;
330
331 if (count > xdr_stream_remaining(argp->xdr) / 20)
332 /*
333 * Even with 4-byte names there wouldn't be
334 * space for that many aces; something fishy is
335 * going on:
336 */
337 return nfserr_fbig;
338
339 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
340 if (*acl == NULL)
341 return nfserr_jukebox;
342
343 (*acl)->naces = count;
344 for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
345 status = nfsd4_decode_nfsace4(argp, ace);
346 if (status)
347 return status;
348 }
349
350 return nfs_ok;
351}
352
dabe9182
CL
353static noinline __be32
354nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
355 struct xdr_netobj *label)
356{
357 u32 lfs, pi, length;
358 __be32 *p;
359
360 if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
361 return nfserr_bad_xdr;
362 if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
363 return nfserr_bad_xdr;
364
365 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
366 return nfserr_bad_xdr;
367 if (length > NFS4_MAXLABELLEN)
368 return nfserr_badlabel;
369 p = xdr_inline_decode(argp->xdr, length);
370 if (!p)
371 return nfserr_bad_xdr;
372 label->len = length;
373 label->data = svcxdr_dupstr(argp, p, length);
374 if (!label->data)
375 return nfserr_jukebox;
376
377 return nfs_ok;
378}
379
b37ad28b 380static __be32
d1c263a0
CL
381nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
382 struct iattr *iattr, struct nfs4_acl **acl,
383 struct xdr_netobj *label, int *umask)
1da177e4 384{
081d53fe
CL
385 unsigned int starting_pos;
386 u32 attrlist4_count;
d1c263a0 387 __be32 *p, status;
1da177e4 388
1da177e4 389 iattr->ia_valid = 0;
d1c263a0
CL
390 status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
391 if (status)
392 return nfserr_bad_xdr;
1da177e4 393
e864c189
BF
394 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
395 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
396 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
397 if (nfsd_attrs_supported(argp->minorversion, bmval))
398 return nfserr_inval;
399 return nfserr_attrnotsupp;
400 }
401
081d53fe
CL
402 if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
403 return nfserr_bad_xdr;
404 starting_pos = xdr_stream_pos(argp->xdr);
1da177e4
LT
405
406 if (bmval[0] & FATTR4_WORD0_SIZE) {
2ac1b9b2
CL
407 u64 size;
408
409 if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
410 return nfserr_bad_xdr;
411 iattr->ia_size = size;
1da177e4
LT
412 iattr->ia_valid |= ATTR_SIZE;
413 }
414 if (bmval[0] & FATTR4_WORD0_ACL) {
c941a968
CL
415 status = nfsd4_decode_acl(argp, acl);
416 if (status)
417 return status;
1da177e4
LT
418 } else
419 *acl = NULL;
420 if (bmval[1] & FATTR4_WORD1_MODE) {
1c8f0ad7
CL
421 u32 mode;
422
423 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
424 return nfserr_bad_xdr;
425 iattr->ia_mode = mode;
1da177e4
LT
426 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
427 iattr->ia_valid |= ATTR_MODE;
428 }
429 if (bmval[1] & FATTR4_WORD1_OWNER) {
9853a5ac
CL
430 u32 length;
431
432 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
433 return nfserr_bad_xdr;
434 p = xdr_inline_decode(argp->xdr, length);
435 if (!p)
436 return nfserr_bad_xdr;
437 status = nfsd_map_name_to_uid(argp->rqstp, (char *)p, length,
438 &iattr->ia_uid);
439 if (status)
47c85291 440 return status;
1da177e4
LT
441 iattr->ia_valid |= ATTR_UID;
442 }
443 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
393c31dd
CL
444 u32 length;
445
446 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
447 return nfserr_bad_xdr;
448 p = xdr_inline_decode(argp->xdr, length);
449 if (!p)
450 return nfserr_bad_xdr;
451 status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
452 &iattr->ia_gid);
453 if (status)
47c85291 454 return status;
1da177e4
LT
455 iattr->ia_valid |= ATTR_GID;
456 }
457 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
1c3eff7e
CL
458 u32 set_it;
459
460 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
461 return nfserr_bad_xdr;
462 switch (set_it) {
1da177e4 463 case NFS4_SET_TO_CLIENT_TIME:
1c3eff7e 464 status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
4c94e13e
CH
465 if (status)
466 return status;
1da177e4
LT
467 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
468 break;
469 case NFS4_SET_TO_SERVER_TIME:
470 iattr->ia_valid |= ATTR_ATIME;
471 break;
472 default:
1c3eff7e 473 return nfserr_bad_xdr;
1da177e4
LT
474 }
475 }
5b2f3e07
CL
476 if (bmval[1] & FATTR4_WORD1_TIME_CREATE) {
477 struct timespec64 ts;
478
479 /* No Linux filesystem supports setting this attribute. */
480 bmval[1] &= ~FATTR4_WORD1_TIME_CREATE;
481 status = nfsd4_decode_nfstime4(argp, &ts);
482 if (status)
483 return status;
484 }
1da177e4 485 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
1c3eff7e
CL
486 u32 set_it;
487
488 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
489 return nfserr_bad_xdr;
490 switch (set_it) {
1da177e4 491 case NFS4_SET_TO_CLIENT_TIME:
1c3eff7e 492 status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
4c94e13e
CH
493 if (status)
494 return status;
1da177e4
LT
495 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
496 break;
497 case NFS4_SET_TO_SERVER_TIME:
498 iattr->ia_valid |= ATTR_MTIME;
499 break;
500 default:
1c3eff7e 501 return nfserr_bad_xdr;
1da177e4
LT
502 }
503 }
18032ca0 504 label->len = 0;
2285ae76
AB
505 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
506 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
dabe9182
CL
507 status = nfsd4_decode_security_label(argp, label);
508 if (status)
509 return status;
18032ca0 510 }
47057abd 511 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
66f0476c
CL
512 u32 mode, mask;
513
47057abd 514 if (!umask)
66f0476c
CL
515 return nfserr_bad_xdr;
516 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
517 return nfserr_bad_xdr;
518 iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
519 if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
520 return nfserr_bad_xdr;
521 *umask = mask & S_IRWXUGO;
47057abd
AG
522 iattr->ia_valid |= ATTR_MODE;
523 }
7e13f4f8
JL
524 if (bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS) {
525 fattr4_time_deleg_access access;
526
527 if (!xdrgen_decode_fattr4_time_deleg_access(argp->xdr, &access))
528 return nfserr_bad_xdr;
529 iattr->ia_atime.tv_sec = access.seconds;
530 iattr->ia_atime.tv_nsec = access.nseconds;
531 iattr->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET | ATTR_DELEG;
532 }
533 if (bmval[2] & FATTR4_WORD2_TIME_DELEG_MODIFY) {
534 fattr4_time_deleg_modify modify;
535
536 if (!xdrgen_decode_fattr4_time_deleg_modify(argp->xdr, &modify))
537 return nfserr_bad_xdr;
538 iattr->ia_mtime.tv_sec = modify.seconds;
539 iattr->ia_mtime.tv_nsec = modify.nseconds;
540 iattr->ia_ctime.tv_sec = modify.seconds;
541 iattr->ia_ctime.tv_nsec = modify.seconds;
542 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME | ATTR_MTIME_SET | ATTR_DELEG;
543 }
081d53fe
CL
544
545 /* request sanity: did attrlist4 contain the expected number of words? */
546 if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
547 return nfserr_bad_xdr;
1da177e4 548
d1c263a0 549 return nfs_ok;
1da177e4
LT
550}
551
d3d2f381
CL
552static __be32
553nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
554{
555 __be32 *p;
556
557 p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
558 if (!p)
559 return nfserr_bad_xdr;
560 sid->si_generation = be32_to_cpup(p++);
561 memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
562 return nfs_ok;
563}
564
144e8269
CL
565static __be32
566nfsd4_decode_clientid4(struct nfsd4_compoundargs *argp, clientid_t *clientid)
567{
568 __be32 *p;
569
570 p = xdr_inline_decode(argp->xdr, sizeof(__be64));
571 if (!p)
572 return nfserr_bad_xdr;
573 memcpy(clientid, p, sizeof(*clientid));
574 return nfs_ok;
575}
576
577static __be32
578nfsd4_decode_state_owner4(struct nfsd4_compoundargs *argp,
579 clientid_t *clientid, struct xdr_netobj *owner)
580{
581 __be32 status;
582
583 status = nfsd4_decode_clientid4(argp, clientid);
584 if (status)
585 return status;
586 return nfsd4_decode_opaque(argp, owner);
587}
588
04495971
CL
589#ifdef CONFIG_NFSD_PNFS
590static __be32
591nfsd4_decode_deviceid4(struct nfsd4_compoundargs *argp,
592 struct nfsd4_deviceid *devid)
593{
594 __be32 *p;
595
596 p = xdr_inline_decode(argp->xdr, NFS4_DEVICEID4_SIZE);
597 if (!p)
598 return nfserr_bad_xdr;
599 memcpy(devid, p, sizeof(*devid));
600 return nfs_ok;
601}
5185980d
CL
602
603static __be32
604nfsd4_decode_layoutupdate4(struct nfsd4_compoundargs *argp,
605 struct nfsd4_layoutcommit *lcp)
606{
607 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_layout_type) < 0)
608 return nfserr_bad_xdr;
609 if (lcp->lc_layout_type < LAYOUT_NFSV4_1_FILES)
610 return nfserr_bad_xdr;
611 if (lcp->lc_layout_type >= LAYOUT_TYPE_MAX)
612 return nfserr_bad_xdr;
613
614 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_up_len) < 0)
615 return nfserr_bad_xdr;
616 if (lcp->lc_up_len > 0) {
617 lcp->lc_up_layout = xdr_inline_decode(argp->xdr, lcp->lc_up_len);
618 if (!lcp->lc_up_layout)
619 return nfserr_bad_xdr;
620 }
621
622 return nfs_ok;
623}
624
645fcad3
CL
625static __be32
626nfsd4_decode_layoutreturn4(struct nfsd4_compoundargs *argp,
627 struct nfsd4_layoutreturn *lrp)
628{
629 __be32 status;
630
631 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_return_type) < 0)
632 return nfserr_bad_xdr;
633 switch (lrp->lr_return_type) {
634 case RETURN_FILE:
635 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.offset) < 0)
636 return nfserr_bad_xdr;
637 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.length) < 0)
638 return nfserr_bad_xdr;
639 status = nfsd4_decode_stateid4(argp, &lrp->lr_sid);
640 if (status)
641 return status;
642 if (xdr_stream_decode_u32(argp->xdr, &lrp->lrf_body_len) < 0)
643 return nfserr_bad_xdr;
644 if (lrp->lrf_body_len > 0) {
645 lrp->lrf_body = xdr_inline_decode(argp->xdr, lrp->lrf_body_len);
646 if (!lrp->lrf_body)
647 return nfserr_bad_xdr;
648 }
649 break;
650 case RETURN_FSID:
651 case RETURN_ALL:
652 lrp->lr_seg.offset = 0;
653 lrp->lr_seg.length = NFS4_MAX_UINT64;
654 break;
655 default:
656 return nfserr_bad_xdr;
657 }
658
659 return nfs_ok;
660}
661
04495971
CL
662#endif /* CONFIG_NFSD_PNFS */
663
571e0451
CL
664static __be32
665nfsd4_decode_sessionid4(struct nfsd4_compoundargs *argp,
666 struct nfs4_sessionid *sessionid)
667{
668 __be32 *p;
669
670 p = xdr_inline_decode(argp->xdr, NFS4_MAX_SESSIONID_LEN);
671 if (!p)
672 return nfserr_bad_xdr;
673 memcpy(sessionid->data, p, sizeof(sessionid->data));
674 return nfs_ok;
675}
676
1a994408
CL
677/* Defined in Appendix A of RFC 5531 */
678static __be32
679nfsd4_decode_authsys_parms(struct nfsd4_compoundargs *argp,
680 struct nfsd4_cb_sec *cbs)
acb2887e 681{
1a994408
CL
682 u32 stamp, gidcount, uid, gid;
683 __be32 *p, status;
684
685 if (xdr_stream_decode_u32(argp->xdr, &stamp) < 0)
686 return nfserr_bad_xdr;
687 /* machine name */
688 status = nfsd4_decode_ignored_string(argp, 255);
689 if (status)
690 return status;
691 if (xdr_stream_decode_u32(argp->xdr, &uid) < 0)
692 return nfserr_bad_xdr;
693 if (xdr_stream_decode_u32(argp->xdr, &gid) < 0)
694 return nfserr_bad_xdr;
695 if (xdr_stream_decode_u32(argp->xdr, &gidcount) < 0)
696 return nfserr_bad_xdr;
697 if (gidcount > 16)
698 return nfserr_bad_xdr;
699 p = xdr_inline_decode(argp->xdr, gidcount << 2);
700 if (!p)
701 return nfserr_bad_xdr;
702 if (cbs->flavor == (u32)(-1)) {
703 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
704
705 kuid_t kuid = make_kuid(userns, uid);
706 kgid_t kgid = make_kgid(userns, gid);
707 if (uid_valid(kuid) && gid_valid(kgid)) {
708 cbs->uid = kuid;
709 cbs->gid = kgid;
710 cbs->flavor = RPC_AUTH_UNIX;
711 } else {
712 dprintk("RPC_AUTH_UNIX with invalid uid or gid, ignoring!\n");
713 }
714 }
715
716 return nfs_ok;
717}
718
719static __be32
720nfsd4_decode_gss_cb_handles4(struct nfsd4_compoundargs *argp,
721 struct nfsd4_cb_sec *cbs)
722{
723 __be32 status;
724 u32 service;
725
726 dprintk("RPC_AUTH_GSS callback secflavor not supported!\n");
727
728 if (xdr_stream_decode_u32(argp->xdr, &service) < 0)
729 return nfserr_bad_xdr;
730 if (service < RPC_GSS_SVC_NONE || service > RPC_GSS_SVC_PRIVACY)
731 return nfserr_bad_xdr;
732 /* gcbp_handle_from_server */
733 status = nfsd4_decode_ignored_string(argp, 0);
734 if (status)
735 return status;
736 /* gcbp_handle_from_client */
737 status = nfsd4_decode_ignored_string(argp, 0);
738 if (status)
739 return status;
740
741 return nfs_ok;
742}
743
744/* a counted array of callback_sec_parms4 items */
745static __be32
746nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
747{
748 u32 i, secflavor, nr_secflavs;
749 __be32 status;
acb2887e
BF
750
751 /* callback_sec_params4 */
1a994408
CL
752 if (xdr_stream_decode_u32(argp->xdr, &nr_secflavs) < 0)
753 return nfserr_bad_xdr;
57569a70
BF
754 if (nr_secflavs)
755 cbs->flavor = (u32)(-1);
756 else
757 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
758 cbs->flavor = 0;
1a994408 759
acb2887e 760 for (i = 0; i < nr_secflavs; ++i) {
1a994408
CL
761 if (xdr_stream_decode_u32(argp->xdr, &secflavor) < 0)
762 return nfserr_bad_xdr;
763 switch (secflavor) {
acb2887e 764 case RPC_AUTH_NULL:
1a994408 765 /* void */
12fc3e92
BF
766 if (cbs->flavor == (u32)(-1))
767 cbs->flavor = RPC_AUTH_NULL;
acb2887e
BF
768 break;
769 case RPC_AUTH_UNIX:
1a994408
CL
770 status = nfsd4_decode_authsys_parms(argp, cbs);
771 if (status)
772 return status;
acb2887e
BF
773 break;
774 case RPC_AUTH_GSS:
1a994408
CL
775 status = nfsd4_decode_gss_cb_handles4(argp, cbs);
776 if (status)
777 return status;
acb2887e
BF
778 break;
779 default:
acb2887e
BF
780 return nfserr_inval;
781 }
782 }
1a994408
CL
783
784 return nfs_ok;
acb2887e
BF
785}
786
1a994408 787
d169a6a9
CL
788/*
789 * NFSv4 operation argument decoders
790 */
791
792static __be32
793nfsd4_decode_access(struct nfsd4_compoundargs *argp,
e78e274e 794 union nfsd4_op_u *u)
d169a6a9 795{
e78e274e 796 struct nfsd4_access *access = &u->access;
d169a6a9
CL
797 if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
798 return nfserr_bad_xdr;
799 return nfs_ok;
800}
801
b37ad28b 802static __be32
e78e274e 803nfsd4_decode_close(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 804{
e78e274e 805 struct nfsd4_close *close = &u->close;
d3d2f381
CL
806 if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
807 return nfserr_bad_xdr;
808 return nfsd4_decode_stateid4(argp, &close->cl_stateid);
1da177e4
LT
809}
810
811
b37ad28b 812static __be32
e78e274e 813nfsd4_decode_commit(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 814{
e78e274e 815 struct nfsd4_commit *commit = &u->commit;
cbd9abb3
CL
816 if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
817 return nfserr_bad_xdr;
818 if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
819 return nfserr_bad_xdr;
3fdc5464 820 memset(&commit->co_verf, 0, sizeof(commit->co_verf));
cbd9abb3 821 return nfs_ok;
1da177e4
LT
822}
823
b37ad28b 824static __be32
e78e274e 825nfsd4_decode_create(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 826{
e78e274e 827 struct nfsd4_create *create = &u->create;
000dfa18 828 __be32 *p, status;
1da177e4 829
3fdc5464 830 memset(create, 0, sizeof(*create));
000dfa18
CL
831 if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
832 return nfserr_bad_xdr;
1da177e4
LT
833 switch (create->cr_type) {
834 case NF4LNK:
000dfa18
CL
835 if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
836 return nfserr_bad_xdr;
837 p = xdr_inline_decode(argp->xdr, create->cr_datalen);
838 if (!p)
839 return nfserr_bad_xdr;
29c353b3 840 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
7fb84306 841 if (!create->cr_data)
76f47128 842 return nfserr_jukebox;
1da177e4
LT
843 break;
844 case NF4BLK:
845 case NF4CHR:
000dfa18
CL
846 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
847 return nfserr_bad_xdr;
848 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
849 return nfserr_bad_xdr;
1da177e4
LT
850 break;
851 case NF4SOCK:
852 case NF4FIFO:
853 case NF4DIR:
854 default:
855 break;
856 }
000dfa18
CL
857 status = nfsd4_decode_component4(argp, &create->cr_name,
858 &create->cr_namelen);
859 if (status)
1da177e4 860 return status;
d1c263a0
CL
861 status = nfsd4_decode_fattr4(argp, create->cr_bmval,
862 ARRAY_SIZE(create->cr_bmval),
863 &create->cr_iattr, &create->cr_acl,
864 &create->cr_label, &create->cr_umask);
c0d6fc8a 865 if (status)
000dfa18 866 return status;
1da177e4 867
000dfa18 868 return nfs_ok;
1da177e4
LT
869}
870
b37ad28b 871static inline __be32
e78e274e 872nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 873{
e78e274e 874 struct nfsd4_delegreturn *dr = &u->delegreturn;
95e6482c 875 return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
1da177e4
LT
876}
877
b37ad28b 878static inline __be32
e78e274e 879nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 880{
e78e274e 881 struct nfsd4_getattr *getattr = &u->getattr;
3fdc5464 882 memset(getattr, 0, sizeof(*getattr));
f759eff2
CL
883 return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
884 ARRAY_SIZE(getattr->ga_bmval));
1da177e4
LT
885}
886
b37ad28b 887static __be32
e78e274e 888nfsd4_decode_link(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 889{
e78e274e 890 struct nfsd4_link *link = &u->link;
3fdc5464 891 memset(link, 0, sizeof(*link));
5c505d12 892 return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
1da177e4
LT
893}
894
8918cc0d
CL
895static __be32
896nfsd4_decode_open_to_lock_owner4(struct nfsd4_compoundargs *argp,
897 struct nfsd4_lock *lock)
898{
899 __be32 status;
900
901 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_open_seqid) < 0)
902 return nfserr_bad_xdr;
903 status = nfsd4_decode_stateid4(argp, &lock->lk_new_open_stateid);
904 if (status)
905 return status;
906 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_lock_seqid) < 0)
907 return nfserr_bad_xdr;
908 return nfsd4_decode_state_owner4(argp, &lock->lk_new_clientid,
909 &lock->lk_new_owner);
910}
911
912static __be32
913nfsd4_decode_exist_lock_owner4(struct nfsd4_compoundargs *argp,
914 struct nfsd4_lock *lock)
915{
916 __be32 status;
917
918 status = nfsd4_decode_stateid4(argp, &lock->lk_old_lock_stateid);
919 if (status)
920 return status;
921 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_old_lock_seqid) < 0)
922 return nfserr_bad_xdr;
923
924 return nfs_ok;
925}
926
927static __be32
928nfsd4_decode_locker4(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
929{
930 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_is_new) < 0)
931 return nfserr_bad_xdr;
932 if (lock->lk_is_new)
933 return nfsd4_decode_open_to_lock_owner4(argp, lock);
934 return nfsd4_decode_exist_lock_owner4(argp, lock);
935}
936
b37ad28b 937static __be32
e78e274e 938nfsd4_decode_lock(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 939{
e78e274e 940 struct nfsd4_lock *lock = &u->lock;
3fdc5464 941 memset(lock, 0, sizeof(*lock));
7c59deed
CL
942 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
943 return nfserr_bad_xdr;
1da177e4 944 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
7c59deed
CL
945 return nfserr_bad_xdr;
946 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_reclaim) < 0)
947 return nfserr_bad_xdr;
948 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_offset) < 0)
949 return nfserr_bad_xdr;
950 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_length) < 0)
951 return nfserr_bad_xdr;
952 return nfsd4_decode_locker4(argp, lock);
1da177e4
LT
953}
954
b37ad28b 955static __be32
e78e274e 956nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 957{
e78e274e 958 struct nfsd4_lockt *lockt = &u->lockt;
3fdc5464 959 memset(lockt, 0, sizeof(*lockt));
0a146f04
CL
960 if (xdr_stream_decode_u32(argp->xdr, &lockt->lt_type) < 0)
961 return nfserr_bad_xdr;
962 if ((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
963 return nfserr_bad_xdr;
964 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_offset) < 0)
965 return nfserr_bad_xdr;
966 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_length) < 0)
967 return nfserr_bad_xdr;
968 return nfsd4_decode_state_owner4(argp, &lockt->lt_clientid,
969 &lockt->lt_owner);
1da177e4
LT
970}
971
b37ad28b 972static __be32
e78e274e 973nfsd4_decode_locku(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 974{
e78e274e 975 struct nfsd4_locku *locku = &u->locku;
ca9cf9fc 976 __be32 status;
1da177e4 977
ca9cf9fc
CL
978 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
979 return nfserr_bad_xdr;
1da177e4 980 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
ca9cf9fc
CL
981 return nfserr_bad_xdr;
982 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_seqid) < 0)
983 return nfserr_bad_xdr;
984 status = nfsd4_decode_stateid4(argp, &locku->lu_stateid);
e31a1b66
BH
985 if (status)
986 return status;
ca9cf9fc
CL
987 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_offset) < 0)
988 return nfserr_bad_xdr;
989 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_length) < 0)
990 return nfserr_bad_xdr;
1da177e4 991
ca9cf9fc 992 return nfs_ok;
1da177e4
LT
993}
994
b37ad28b 995static __be32
e78e274e 996nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 997{
e78e274e 998 struct nfsd4_lookup *lookup = &u->lookup;
3d5877e8 999 return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
1da177e4
LT
1000}
1001
bf33bab3
CL
1002static __be32
1003nfsd4_decode_createhow4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1004{
1005 __be32 status;
1006
1007 if (xdr_stream_decode_u32(argp->xdr, &open->op_createmode) < 0)
1008 return nfserr_bad_xdr;
1009 switch (open->op_createmode) {
1010 case NFS4_CREATE_UNCHECKED:
1011 case NFS4_CREATE_GUARDED:
1012 status = nfsd4_decode_fattr4(argp, open->op_bmval,
1013 ARRAY_SIZE(open->op_bmval),
1014 &open->op_iattr, &open->op_acl,
1015 &open->op_label, &open->op_umask);
1016 if (status)
1017 return status;
1018 break;
1019 case NFS4_CREATE_EXCLUSIVE:
1020 status = nfsd4_decode_verifier4(argp, &open->op_verf);
1021 if (status)
1022 return status;
1023 break;
1024 case NFS4_CREATE_EXCLUSIVE4_1:
1025 if (argp->minorversion < 1)
1026 return nfserr_bad_xdr;
1027 status = nfsd4_decode_verifier4(argp, &open->op_verf);
1028 if (status)
1029 return status;
1030 status = nfsd4_decode_fattr4(argp, open->op_bmval,
1031 ARRAY_SIZE(open->op_bmval),
1032 &open->op_iattr, &open->op_acl,
1033 &open->op_label, &open->op_umask);
1034 if (status)
1035 return status;
1036 break;
1037 default:
1038 return nfserr_bad_xdr;
1039 }
1040
1041 return nfs_ok;
1042}
1043
e6ec04b2
CL
1044static __be32
1045nfsd4_decode_openflag4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1046{
1047 __be32 status;
1048
1049 if (xdr_stream_decode_u32(argp->xdr, &open->op_create) < 0)
1050 return nfserr_bad_xdr;
1051 switch (open->op_create) {
1052 case NFS4_OPEN_NOCREATE:
1053 break;
1054 case NFS4_OPEN_CREATE:
1055 status = nfsd4_decode_createhow4(argp, open);
1056 if (status)
1057 return status;
1058 break;
1059 default:
1060 return nfserr_bad_xdr;
1061 }
1062
1063 return nfs_ok;
1064}
1065
2c8bd7e0 1066static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
04f9e664 1067{
04f9e664
BF
1068 u32 w;
1069
9aa62f51
CL
1070 if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
1071 return nfserr_bad_xdr;
2c8bd7e0
BH
1072 *share_access = w & NFS4_SHARE_ACCESS_MASK;
1073 *deleg_want = w & NFS4_SHARE_WANT_MASK;
1074 if (deleg_when)
1075 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
1076
04f9e664
BF
1077 switch (w & NFS4_SHARE_ACCESS_MASK) {
1078 case NFS4_SHARE_ACCESS_READ:
1079 case NFS4_SHARE_ACCESS_WRITE:
1080 case NFS4_SHARE_ACCESS_BOTH:
1081 break;
1082 default:
1083 return nfserr_bad_xdr;
1084 }
fc0d14fe 1085 w &= ~NFS4_SHARE_ACCESS_MASK;
04f9e664
BF
1086 if (!w)
1087 return nfs_ok;
1088 if (!argp->minorversion)
1089 return nfserr_bad_xdr;
cee9b4ef 1090 switch (w & NFS4_SHARE_WANT_TYPE_MASK) {
c9c99a33
JL
1091 case OPEN4_SHARE_ACCESS_WANT_NO_PREFERENCE:
1092 case OPEN4_SHARE_ACCESS_WANT_READ_DELEG:
1093 case OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG:
1094 case OPEN4_SHARE_ACCESS_WANT_ANY_DELEG:
1095 case OPEN4_SHARE_ACCESS_WANT_NO_DELEG:
1096 case OPEN4_SHARE_ACCESS_WANT_CANCEL:
04f9e664
BF
1097 break;
1098 default:
1099 return nfserr_bad_xdr;
1100 }
92bac8c5 1101 w &= ~NFS4_SHARE_WANT_MASK;
04f9e664
BF
1102 if (!w)
1103 return nfs_ok;
2c8bd7e0
BH
1104
1105 if (!deleg_when) /* open_downgrade */
1106 return nfserr_inval;
04f9e664
BF
1107 switch (w) {
1108 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
1109 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
c668fc6d
BH
1110 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
1111 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
04f9e664
BF
1112 return nfs_ok;
1113 }
04f9e664
BF
1114 return nfserr_bad_xdr;
1115}
1116
1117static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
1118{
b07bebd9
CL
1119 if (xdr_stream_decode_u32(argp->xdr, x) < 0)
1120 return nfserr_bad_xdr;
1121 /* Note: unlike access bits, deny bits may be zero. */
01cd4afa 1122 if (*x & ~NFS4_SHARE_DENY_BOTH)
04f9e664 1123 return nfserr_bad_xdr;
b07bebd9 1124
04f9e664 1125 return nfs_ok;
04f9e664
BF
1126}
1127
1708e50b
CL
1128static __be32
1129nfsd4_decode_open_claim4(struct nfsd4_compoundargs *argp,
1130 struct nfsd4_open *open)
1131{
1132 __be32 status;
1133
1134 if (xdr_stream_decode_u32(argp->xdr, &open->op_claim_type) < 0)
1135 return nfserr_bad_xdr;
1136 switch (open->op_claim_type) {
1137 case NFS4_OPEN_CLAIM_NULL:
1138 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1139 status = nfsd4_decode_component4(argp, &open->op_fname,
1140 &open->op_fnamelen);
1141 if (status)
1142 return status;
1143 break;
1144 case NFS4_OPEN_CLAIM_PREVIOUS:
1145 if (xdr_stream_decode_u32(argp->xdr, &open->op_delegate_type) < 0)
1146 return nfserr_bad_xdr;
1147 break;
1148 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1149 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1150 if (status)
1151 return status;
1152 status = nfsd4_decode_component4(argp, &open->op_fname,
1153 &open->op_fnamelen);
1154 if (status)
1155 return status;
1156 break;
1157 case NFS4_OPEN_CLAIM_FH:
1158 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1159 if (argp->minorversion < 1)
1160 return nfserr_bad_xdr;
1161 /* void */
1162 break;
1163 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1164 if (argp->minorversion < 1)
1165 return nfserr_bad_xdr;
1166 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1167 if (status)
1168 return status;
1169 break;
1170 default:
1171 return nfserr_bad_xdr;
1172 }
1173
1174 return nfs_ok;
1175}
1176
b37ad28b 1177static __be32
e78e274e 1178nfsd4_decode_open(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1179{
e78e274e 1180 struct nfsd4_open *open = &u->open;
61e5e0b3 1181 __be32 status;
2c8bd7e0 1182 u32 dummy;
1da177e4 1183
3fdc5464 1184 memset(open, 0, sizeof(*open));
1da177e4 1185
61e5e0b3
CL
1186 if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
1187 return nfserr_bad_xdr;
1188 /* deleg_want is ignored */
2c8bd7e0
BH
1189 status = nfsd4_decode_share_access(argp, &open->op_share_access,
1190 &open->op_deleg_want, &dummy);
04f9e664 1191 if (status)
61e5e0b3 1192 return status;
04f9e664
BF
1193 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
1194 if (status)
61e5e0b3
CL
1195 return status;
1196 status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
1197 &open->op_owner);
a084daf5 1198 if (status)
61e5e0b3 1199 return status;
e6ec04b2
CL
1200 status = nfsd4_decode_openflag4(argp, open);
1201 if (status)
1202 return status;
61e5e0b3 1203 return nfsd4_decode_open_claim4(argp, open);
1da177e4
LT
1204}
1205
b37ad28b 1206static __be32
e78e274e
KC
1207nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp,
1208 union nfsd4_op_u *u)
1da177e4 1209{
e78e274e 1210 struct nfsd4_open_confirm *open_conf = &u->open_confirm;
06bee693 1211 __be32 status;
e1a90ebd
AS
1212
1213 if (argp->minorversion >= 1)
1214 return nfserr_notsupp;
1215
06bee693 1216 status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
e31a1b66
BH
1217 if (status)
1218 return status;
06bee693
CL
1219 if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
1220 return nfserr_bad_xdr;
e1a90ebd 1221
3fdc5464
CL
1222 memset(&open_conf->oc_resp_stateid, 0,
1223 sizeof(open_conf->oc_resp_stateid));
06bee693 1224 return nfs_ok;
1da177e4
LT
1225}
1226
b37ad28b 1227static __be32
e78e274e
KC
1228nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp,
1229 union nfsd4_op_u *u)
1da177e4 1230{
e78e274e 1231 struct nfsd4_open_downgrade *open_down = &u->open_downgrade;
dca71651
CL
1232 __be32 status;
1233
3fdc5464 1234 memset(open_down, 0, sizeof(*open_down));
dca71651 1235 status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
e31a1b66
BH
1236 if (status)
1237 return status;
dca71651
CL
1238 if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
1239 return nfserr_bad_xdr;
1240 /* deleg_want is ignored */
2c8bd7e0
BH
1241 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1242 &open_down->od_deleg_want, NULL);
04f9e664
BF
1243 if (status)
1244 return status;
dca71651 1245 return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1da177e4
LT
1246}
1247
b37ad28b 1248static __be32
e78e274e 1249nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1250{
e78e274e 1251 struct nfsd4_putfh *putfh = &u->putfh;
a73bed98 1252 __be32 *p;
1da177e4 1253
a73bed98
CL
1254 if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
1255 return nfserr_bad_xdr;
1da177e4 1256 if (putfh->pf_fhlen > NFS4_FHSIZE)
a73bed98
CL
1257 return nfserr_bad_xdr;
1258 p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
1259 if (!p)
1260 return nfserr_bad_xdr;
7b723008 1261 putfh->pf_fhval = svcxdr_savemem(argp, p, putfh->pf_fhlen);
a73bed98
CL
1262 if (!putfh->pf_fhval)
1263 return nfserr_jukebox;
1da177e4 1264
3fdc5464 1265 putfh->no_verify = false;
a73bed98 1266 return nfs_ok;
1da177e4
LT
1267}
1268
b37ad28b 1269static __be32
e78e274e 1270nfsd4_decode_read(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1271{
e78e274e 1272 struct nfsd4_read *read = &u->read;
3909c3bc 1273 __be32 status;
1da177e4 1274
3fdc5464 1275 memset(read, 0, sizeof(*read));
3909c3bc 1276 status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
e31a1b66
BH
1277 if (status)
1278 return status;
3909c3bc
CL
1279 if (xdr_stream_decode_u64(argp->xdr, &read->rd_offset) < 0)
1280 return nfserr_bad_xdr;
1281 if (xdr_stream_decode_u32(argp->xdr, &read->rd_length) < 0)
1282 return nfserr_bad_xdr;
1da177e4 1283
3909c3bc 1284 return nfs_ok;
1da177e4
LT
1285}
1286
b37ad28b 1287static __be32
e78e274e 1288nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1289{
e78e274e 1290 struct nfsd4_readdir *readdir = &u->readdir;
0dfaf2a3 1291 __be32 status;
1da177e4 1292
3fdc5464 1293 memset(readdir, 0, sizeof(*readdir));
0dfaf2a3
CL
1294 if (xdr_stream_decode_u64(argp->xdr, &readdir->rd_cookie) < 0)
1295 return nfserr_bad_xdr;
1296 status = nfsd4_decode_verifier4(argp, &readdir->rd_verf);
1297 if (status)
1298 return status;
1299 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_dircount) < 0)
1300 return nfserr_bad_xdr;
1301 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_maxcount) < 0)
1302 return nfserr_bad_xdr;
1303 if (xdr_stream_decode_uint32_array(argp->xdr, readdir->rd_bmval,
1304 ARRAY_SIZE(readdir->rd_bmval)) < 0)
1305 return nfserr_bad_xdr;
1da177e4 1306
0dfaf2a3 1307 return nfs_ok;
1da177e4
LT
1308}
1309
b37ad28b 1310static __be32
e78e274e 1311nfsd4_decode_remove(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1312{
e78e274e 1313 struct nfsd4_remove *remove = &u->remove;
3fdc5464 1314 memset(&remove->rm_cinfo, 0, sizeof(remove->rm_cinfo));
b7f5fbf2 1315 return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
1da177e4
LT
1316}
1317
b37ad28b 1318static __be32
e78e274e 1319nfsd4_decode_rename(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1320{
e78e274e 1321 struct nfsd4_rename *rename = &u->rename;
ba881a0a 1322 __be32 status;
1da177e4 1323
3fdc5464 1324 memset(rename, 0, sizeof(*rename));
ba881a0a
CL
1325 status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
1326 if (status)
1da177e4 1327 return status;
ba881a0a 1328 return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
1da177e4
LT
1329}
1330
b37ad28b 1331static __be32
e78e274e 1332nfsd4_decode_renew(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1333{
e78e274e 1334 clientid_t *clientid = &u->renew;
d12f9045 1335 return nfsd4_decode_clientid4(argp, clientid);
1da177e4
LT
1336}
1337
dcb488a3
AA
1338static __be32
1339nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
e78e274e 1340 union nfsd4_op_u *u)
dcb488a3 1341{
e78e274e 1342 struct nfsd4_secinfo *secinfo = &u->secinfo;
3fdc5464 1343 secinfo->si_exp = NULL;
d0abdae5 1344 return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
dcb488a3
AA
1345}
1346
b37ad28b 1347static __be32
e78e274e 1348nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1349{
e78e274e 1350 struct nfsd4_setattr *setattr = &u->setattr;
e31a1b66 1351 __be32 status;
1da177e4 1352
3fdc5464 1353 memset(setattr, 0, sizeof(*setattr));
44592fe9 1354 status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
e31a1b66
BH
1355 if (status)
1356 return status;
d1c263a0
CL
1357 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1358 ARRAY_SIZE(setattr->sa_bmval),
1359 &setattr->sa_iattr, &setattr->sa_acl,
1360 &setattr->sa_label, NULL);
1da177e4
LT
1361}
1362
b37ad28b 1363static __be32
e78e274e 1364nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1365{
e78e274e 1366 struct nfsd4_setclientid *setclientid = &u->setclientid;
92fa6c08 1367 __be32 *p, status;
1da177e4 1368
3fdc5464
CL
1369 memset(setclientid, 0, sizeof(*setclientid));
1370
e1a90ebd
AS
1371 if (argp->minorversion >= 1)
1372 return nfserr_notsupp;
1373
92fa6c08
CL
1374 status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
1375 if (status)
1376 return status;
a084daf5
BF
1377 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1378 if (status)
92fa6c08
CL
1379 return status;
1380 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
a084daf5 1381 return nfserr_bad_xdr;
92fa6c08
CL
1382 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_netid_len) < 0)
1383 return nfserr_bad_xdr;
1384 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_netid_len);
1385 if (!p)
1386 return nfserr_bad_xdr;
7b723008 1387 setclientid->se_callback_netid_val = svcxdr_savemem(argp, p,
92fa6c08
CL
1388 setclientid->se_callback_netid_len);
1389 if (!setclientid->se_callback_netid_val)
1390 return nfserr_jukebox;
1da177e4 1391
92fa6c08
CL
1392 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_addr_len) < 0)
1393 return nfserr_bad_xdr;
1394 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_addr_len);
1395 if (!p)
1396 return nfserr_bad_xdr;
7b723008 1397 setclientid->se_callback_addr_val = svcxdr_savemem(argp, p,
92fa6c08
CL
1398 setclientid->se_callback_addr_len);
1399 if (!setclientid->se_callback_addr_val)
1400 return nfserr_jukebox;
92fa6c08
CL
1401 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
1402 return nfserr_bad_xdr;
1da177e4 1403
92fa6c08 1404 return nfs_ok;
1da177e4
LT
1405}
1406
b37ad28b 1407static __be32
e78e274e
KC
1408nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp,
1409 union nfsd4_op_u *u)
1da177e4 1410{
e78e274e 1411 struct nfsd4_setclientid_confirm *scd_c = &u->setclientid_confirm;
d1ca5514 1412 __be32 status;
1da177e4 1413
e1a90ebd
AS
1414 if (argp->minorversion >= 1)
1415 return nfserr_notsupp;
1416
d1ca5514
CL
1417 status = nfsd4_decode_clientid4(argp, &scd_c->sc_clientid);
1418 if (status)
1419 return status;
1420 return nfsd4_decode_verifier4(argp, &scd_c->sc_confirm);
1da177e4
LT
1421}
1422
1423/* Also used for NVERIFY */
b37ad28b 1424static __be32
e78e274e 1425nfsd4_decode_verify(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1426{
e78e274e 1427 struct nfsd4_verify *verify = &u->verify;
67cd453e 1428 __be32 *p, status;
1da177e4 1429
3fdc5464
CL
1430 memset(verify, 0, sizeof(*verify));
1431
67cd453e
CL
1432 status = nfsd4_decode_bitmap4(argp, verify->ve_bmval,
1433 ARRAY_SIZE(verify->ve_bmval));
1434 if (status)
1435 return status;
1da177e4
LT
1436
1437 /* For convenience's sake, we compare raw xdr'd attributes in
e5f95703
BF
1438 * nfsd4_proc_verify */
1439
67cd453e
CL
1440 if (xdr_stream_decode_u32(argp->xdr, &verify->ve_attrlen) < 0)
1441 return nfserr_bad_xdr;
1442 p = xdr_inline_decode(argp->xdr, verify->ve_attrlen);
1443 if (!p)
1444 return nfserr_bad_xdr;
7b723008 1445 verify->ve_attrval = svcxdr_savemem(argp, p, verify->ve_attrlen);
67cd453e
CL
1446 if (!verify->ve_attrval)
1447 return nfserr_jukebox;
1da177e4 1448
67cd453e 1449 return nfs_ok;
1da177e4
LT
1450}
1451
b37ad28b 1452static __be32
e78e274e 1453nfsd4_decode_write(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1da177e4 1454{
e78e274e 1455 struct nfsd4_write *write = &u->write;
244e2bef 1456 __be32 status;
1da177e4 1457
244e2bef 1458 status = nfsd4_decode_stateid4(argp, &write->wr_stateid);
e31a1b66
BH
1459 if (status)
1460 return status;
244e2bef
CL
1461 if (xdr_stream_decode_u64(argp->xdr, &write->wr_offset) < 0)
1462 return nfserr_bad_xdr;
1463 if (xdr_stream_decode_u32(argp->xdr, &write->wr_stable_how) < 0)
1464 return nfserr_bad_xdr;
54bbb7d2 1465 if (write->wr_stable_how > NFS_FILE_SYNC)
244e2bef
CL
1466 return nfserr_bad_xdr;
1467 if (xdr_stream_decode_u32(argp->xdr, &write->wr_buflen) < 0)
1468 return nfserr_bad_xdr;
c1346a12 1469 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
244e2bef 1470 return nfserr_bad_xdr;
1da177e4 1471
3fdc5464
CL
1472 write->wr_bytes_written = 0;
1473 write->wr_how_written = 0;
1474 memset(&write->wr_verifier, 0, sizeof(write->wr_verifier));
244e2bef 1475 return nfs_ok;
1da177e4
LT
1476}
1477
b37ad28b 1478static __be32
e78e274e
KC
1479nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp,
1480 union nfsd4_op_u *u)
1da177e4 1481{
e78e274e 1482 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
a4a80c15 1483 __be32 status;
1da177e4 1484
e1a90ebd
AS
1485 if (argp->minorversion >= 1)
1486 return nfserr_notsupp;
1487
a4a80c15
CL
1488 status = nfsd4_decode_state_owner4(argp, &rlockowner->rl_clientid,
1489 &rlockowner->rl_owner);
1490 if (status)
1491 return status;
1da177e4 1492
60adfc50
AA
1493 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1494 return nfserr_inval;
a4a80c15
CL
1495
1496 return nfs_ok;
1da177e4
LT
1497}
1498
e78e274e
KC
1499static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp,
1500 union nfsd4_op_u *u)
0f81d960 1501{
e78e274e 1502 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3fdc5464 1503 memset(bc, 0, sizeof(*bc));
0f81d960
CL
1504 if (xdr_stream_decode_u32(argp->xdr, &bc->bc_cb_program) < 0)
1505 return nfserr_bad_xdr;
1506 return nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
1507}
1508
e78e274e
KC
1509static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp,
1510 union nfsd4_op_u *u)
571e0451 1511{
e78e274e 1512 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
571e0451
CL
1513 u32 use_conn_in_rdma_mode;
1514 __be32 status;
1515
3fdc5464 1516 memset(bcts, 0, sizeof(*bcts));
571e0451
CL
1517 status = nfsd4_decode_sessionid4(argp, &bcts->sessionid);
1518 if (status)
1519 return status;
1520 if (xdr_stream_decode_u32(argp->xdr, &bcts->dir) < 0)
1521 return nfserr_bad_xdr;
1522 if (xdr_stream_decode_u32(argp->xdr, &use_conn_in_rdma_mode) < 0)
1523 return nfserr_bad_xdr;
1524
1525 return nfs_ok;
1526}
1527
2548aa78
CL
1528static __be32
1529nfsd4_decode_state_protect_ops(struct nfsd4_compoundargs *argp,
1530 struct nfsd4_exchange_id *exid)
1531{
1532 __be32 status;
1533
1534 status = nfsd4_decode_bitmap4(argp, exid->spo_must_enforce,
1535 ARRAY_SIZE(exid->spo_must_enforce));
1536 if (status)
1537 return nfserr_bad_xdr;
1538 status = nfsd4_decode_bitmap4(argp, exid->spo_must_allow,
1539 ARRAY_SIZE(exid->spo_must_allow));
1540 if (status)
1541 return nfserr_bad_xdr;
1542
1543 return nfs_ok;
1544}
1545
547bfeb4
CL
1546/*
1547 * This implementation currently does not support SP4_SSV.
1548 * This decoder simply skips over these arguments.
1549 */
1550static noinline __be32
1551nfsd4_decode_ssv_sp_parms(struct nfsd4_compoundargs *argp,
1552 struct nfsd4_exchange_id *exid)
1553{
1554 u32 count, window, num_gss_handles;
1555 __be32 status;
1556
1557 /* ssp_ops */
1558 status = nfsd4_decode_state_protect_ops(argp, exid);
1559 if (status)
1560 return status;
1561
1562 /* ssp_hash_algs<> */
1563 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1564 return nfserr_bad_xdr;
1565 while (count--) {
1566 status = nfsd4_decode_ignored_string(argp, 0);
1567 if (status)
1568 return status;
1569 }
1570
1571 /* ssp_encr_algs<> */
1572 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1573 return nfserr_bad_xdr;
1574 while (count--) {
1575 status = nfsd4_decode_ignored_string(argp, 0);
1576 if (status)
1577 return status;
1578 }
1579
1580 if (xdr_stream_decode_u32(argp->xdr, &window) < 0)
1581 return nfserr_bad_xdr;
1582 if (xdr_stream_decode_u32(argp->xdr, &num_gss_handles) < 0)
1583 return nfserr_bad_xdr;
1584
1585 return nfs_ok;
1586}
1587
2db134eb 1588static __be32
523ec6ed
CL
1589nfsd4_decode_state_protect4_a(struct nfsd4_compoundargs *argp,
1590 struct nfsd4_exchange_id *exid)
2db134eb 1591{
523ec6ed 1592 __be32 status;
0733d213 1593
523ec6ed 1594 if (xdr_stream_decode_u32(argp->xdr, &exid->spa_how) < 0)
a084daf5 1595 return nfserr_bad_xdr;
0733d213
AA
1596 switch (exid->spa_how) {
1597 case SP4_NONE:
1598 break;
1599 case SP4_MACH_CRED:
2548aa78 1600 status = nfsd4_decode_state_protect_ops(argp, exid);
ed941643 1601 if (status)
2548aa78 1602 return status;
0733d213
AA
1603 break;
1604 case SP4_SSV:
547bfeb4 1605 status = nfsd4_decode_ssv_sp_parms(argp, exid);
2548aa78
CL
1606 if (status)
1607 return status;
0733d213
AA
1608 break;
1609 default:
523ec6ed 1610 return nfserr_bad_xdr;
0733d213
AA
1611 }
1612
523ec6ed
CL
1613 return nfs_ok;
1614}
1615
10ff8422
CL
1616static __be32
1617nfsd4_decode_nfs_impl_id4(struct nfsd4_compoundargs *argp,
1618 struct nfsd4_exchange_id *exid)
1619{
1620 __be32 status;
1621 u32 count;
1622
1623 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1624 return nfserr_bad_xdr;
1625 switch (count) {
1626 case 0:
1627 break;
1628 case 1:
1629 /* Note that RFC 8881 places no length limit on
1630 * nii_domain, but this implementation permits no
1631 * more than NFS4_OPAQUE_LIMIT bytes */
1632 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1633 if (status)
1634 return status;
1635 /* Note that RFC 8881 places no length limit on
1636 * nii_name, but this implementation permits no
1637 * more than NFS4_OPAQUE_LIMIT bytes */
1638 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1639 if (status)
1640 return status;
1641 status = nfsd4_decode_nfstime4(argp, &exid->nii_time);
1642 if (status)
1643 return status;
1644 break;
1645 default:
1646 return nfserr_bad_xdr;
1647 }
1648
1649 return nfs_ok;
1650}
1651
523ec6ed
CL
1652static __be32
1653nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
e78e274e 1654 union nfsd4_op_u *u)
523ec6ed 1655{
e78e274e 1656 struct nfsd4_exchange_id *exid = &u->exchange_id;
10ff8422 1657 __be32 status;
523ec6ed 1658
3fdc5464 1659 memset(exid, 0, sizeof(*exid));
523ec6ed
CL
1660 status = nfsd4_decode_verifier4(argp, &exid->verifier);
1661 if (status)
1662 return status;
1663 status = nfsd4_decode_opaque(argp, &exid->clname);
1664 if (status)
1665 return status;
1666 if (xdr_stream_decode_u32(argp->xdr, &exid->flags) < 0)
1667 return nfserr_bad_xdr;
1668 status = nfsd4_decode_state_protect4_a(argp, exid);
1669 if (status)
1670 return status;
10ff8422 1671 return nfsd4_decode_nfs_impl_id4(argp, exid);
2db134eb
AA
1672}
1673
3a3f1fba
CL
1674static __be32
1675nfsd4_decode_channel_attrs4(struct nfsd4_compoundargs *argp,
1676 struct nfsd4_channel_attrs *ca)
1677{
1678 __be32 *p;
1679
1680 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 7);
1681 if (!p)
1682 return nfserr_bad_xdr;
1683
1684 /* headerpadsz is ignored */
1685 p++;
1686 ca->maxreq_sz = be32_to_cpup(p++);
1687 ca->maxresp_sz = be32_to_cpup(p++);
1688 ca->maxresp_cached = be32_to_cpup(p++);
1689 ca->maxops = be32_to_cpup(p++);
1690 ca->maxreqs = be32_to_cpup(p++);
1691 ca->nr_rdma_attrs = be32_to_cpup(p);
1692 switch (ca->nr_rdma_attrs) {
1693 case 0:
1694 break;
1695 case 1:
1696 if (xdr_stream_decode_u32(argp->xdr, &ca->rdma_attrs) < 0)
1697 return nfserr_bad_xdr;
1698 break;
1699 default:
1700 return nfserr_bad_xdr;
1701 }
1702
1703 return nfs_ok;
1704}
1705
2db134eb
AA
1706static __be32
1707nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
e78e274e 1708 union nfsd4_op_u *u)
2db134eb 1709{
e78e274e 1710 struct nfsd4_create_session *sess = &u->create_session;
81243e3f 1711 __be32 status;
ec6b5d7b 1712
3fdc5464 1713 memset(sess, 0, sizeof(*sess));
81243e3f
CL
1714 status = nfsd4_decode_clientid4(argp, &sess->clientid);
1715 if (status)
1716 return status;
1717 if (xdr_stream_decode_u32(argp->xdr, &sess->seqid) < 0)
1718 return nfserr_bad_xdr;
1719 if (xdr_stream_decode_u32(argp->xdr, &sess->flags) < 0)
1720 return nfserr_bad_xdr;
3a3f1fba
CL
1721 status = nfsd4_decode_channel_attrs4(argp, &sess->fore_channel);
1722 if (status)
1723 return status;
1724 status = nfsd4_decode_channel_attrs4(argp, &sess->back_channel);
81243e3f
CL
1725 if (status)
1726 return status;
1727 if (xdr_stream_decode_u32(argp->xdr, &sess->callback_prog) < 0)
1728 return nfserr_bad_xdr;
3fdc5464 1729 return nfsd4_decode_cb_sec(argp, &sess->cb_sec);
2db134eb
AA
1730}
1731
1732static __be32
1733nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
e78e274e 1734 union nfsd4_op_u *u)
2db134eb 1735{
e78e274e 1736 struct nfsd4_destroy_session *destroy_session = &u->destroy_session;
94e254af 1737 return nfsd4_decode_sessionid4(argp, &destroy_session->sessionid);
2db134eb
AA
1738}
1739
e1ca12df
BS
1740static __be32
1741nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
e78e274e 1742 union nfsd4_op_u *u)
e1ca12df 1743{
e78e274e 1744 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
aec387d5 1745 return nfsd4_decode_stateid4(argp, &free_stateid->fr_stateid);
e1ca12df
BS
1746}
1747
33a1e6ea
JL
1748static __be32
1749nfsd4_decode_get_dir_delegation(struct nfsd4_compoundargs *argp,
1750 union nfsd4_op_u *u)
1751{
1752 struct nfsd4_get_dir_delegation *gdd = &u->get_dir_delegation;
1753 __be32 status;
1754
1755 memset(gdd, 0, sizeof(*gdd));
1756
1757 if (xdr_stream_decode_bool(argp->xdr, &gdd->gdda_signal_deleg_avail) < 0)
1758 return nfserr_bad_xdr;
1759 status = nfsd4_decode_bitmap4(argp, gdd->gdda_notification_types,
1760 ARRAY_SIZE(gdd->gdda_notification_types));
1761 if (status)
1762 return status;
1763 status = nfsd4_decode_nfstime4(argp, &gdd->gdda_child_attr_delay);
1764 if (status)
1765 return status;
1766 status = nfsd4_decode_nfstime4(argp, &gdd->gdda_dir_attr_delay);
1767 if (status)
1768 return status;
1769 status = nfsd4_decode_bitmap4(argp, gdd->gdda_child_attributes,
1770 ARRAY_SIZE(gdd->gdda_child_attributes));
1771 if (status)
1772 return status;
1773 return nfsd4_decode_bitmap4(argp, gdd->gdda_dir_attributes,
1774 ARRAY_SIZE(gdd->gdda_dir_attributes));
1775}
1776
9cf514cc
CH
1777#ifdef CONFIG_NFSD_PNFS
1778static __be32
1779nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
e78e274e 1780 union nfsd4_op_u *u)
9cf514cc 1781{
e78e274e 1782 struct nfsd4_getdeviceinfo *gdev = &u->getdeviceinfo;
04495971
CL
1783 __be32 status;
1784
3fdc5464 1785 memset(gdev, 0, sizeof(*gdev));
04495971
CL
1786 status = nfsd4_decode_deviceid4(argp, &gdev->gd_devid);
1787 if (status)
1788 return status;
1789 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_layout_type) < 0)
1790 return nfserr_bad_xdr;
1791 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_maxcount) < 0)
1792 return nfserr_bad_xdr;
1793 if (xdr_stream_decode_uint32_array(argp->xdr,
1794 &gdev->gd_notify_types, 1) < 0)
1795 return nfserr_bad_xdr;
1796
1797 return nfs_ok;
9cf514cc
CH
1798}
1799
5185980d
CL
1800static __be32
1801nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
e78e274e 1802 union nfsd4_op_u *u)
5185980d 1803{
e78e274e 1804 struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
5185980d
CL
1805 __be32 *p, status;
1806
3fdc5464 1807 memset(lcp, 0, sizeof(*lcp));
5185980d
CL
1808 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.offset) < 0)
1809 return nfserr_bad_xdr;
1810 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.length) < 0)
1811 return nfserr_bad_xdr;
1812 if (xdr_stream_decode_bool(argp->xdr, &lcp->lc_reclaim) < 0)
1813 return nfserr_bad_xdr;
1814 status = nfsd4_decode_stateid4(argp, &lcp->lc_sid);
1815 if (status)
1816 return status;
1817 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_newoffset) < 0)
1818 return nfserr_bad_xdr;
1819 if (lcp->lc_newoffset) {
1820 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_last_wr) < 0)
1821 return nfserr_bad_xdr;
1822 } else
1823 lcp->lc_last_wr = 0;
1824 p = xdr_inline_decode(argp->xdr, XDR_UNIT);
1825 if (!p)
1826 return nfserr_bad_xdr;
1827 if (xdr_item_is_present(p)) {
1828 status = nfsd4_decode_nfstime4(argp, &lcp->lc_mtime);
1829 if (status)
1830 return status;
1831 } else {
1832 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1833 }
1834 return nfsd4_decode_layoutupdate4(argp, lcp);
1835}
1836
9cf514cc
CH
1837static __be32
1838nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
e78e274e 1839 union nfsd4_op_u *u)
9cf514cc 1840{
e78e274e 1841 struct nfsd4_layoutget *lgp = &u->layoutget;
c8e88e3a 1842 __be32 status;
db59c0ef 1843
3fdc5464 1844 memset(lgp, 0, sizeof(*lgp));
c8e88e3a
CL
1845 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_signal) < 0)
1846 return nfserr_bad_xdr;
1847 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_layout_type) < 0)
1848 return nfserr_bad_xdr;
1849 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_seg.iomode) < 0)
1850 return nfserr_bad_xdr;
1851 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.offset) < 0)
1852 return nfserr_bad_xdr;
1853 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.length) < 0)
1854 return nfserr_bad_xdr;
1855 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_minlength) < 0)
1856 return nfserr_bad_xdr;
1857 status = nfsd4_decode_stateid4(argp, &lgp->lg_sid);
db59c0ef
KM
1858 if (status)
1859 return status;
c8e88e3a
CL
1860 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_maxcount) < 0)
1861 return nfserr_bad_xdr;
db59c0ef 1862
c8e88e3a 1863 return nfs_ok;
9cf514cc
CH
1864}
1865
9cf514cc
CH
1866static __be32
1867nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
e78e274e 1868 union nfsd4_op_u *u)
9cf514cc 1869{
e78e274e 1870 struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
3fdc5464 1871 memset(lrp, 0, sizeof(*lrp));
645fcad3
CL
1872 if (xdr_stream_decode_bool(argp->xdr, &lrp->lr_reclaim) < 0)
1873 return nfserr_bad_xdr;
1874 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_layout_type) < 0)
1875 return nfserr_bad_xdr;
1876 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_seg.iomode) < 0)
1877 return nfserr_bad_xdr;
1878 return nfsd4_decode_layoutreturn4(argp, lrp);
9cf514cc
CH
1879}
1880#endif /* CONFIG_NFSD_PNFS */
1881
53d70873 1882static __be32 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
e78e274e 1883 union nfsd4_op_u *u)
53d70873 1884{
e78e274e 1885 struct nfsd4_secinfo_no_name *sin = &u->secinfo_no_name;
53d70873
CL
1886 if (xdr_stream_decode_u32(argp->xdr, &sin->sin_style) < 0)
1887 return nfserr_bad_xdr;
3fdc5464
CL
1888
1889 sin->sin_exp = NULL;
53d70873
CL
1890 return nfs_ok;
1891}
1892
cf907b11
CL
1893static __be32
1894nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
e78e274e 1895 union nfsd4_op_u *u)
cf907b11 1896{
e78e274e 1897 struct nfsd4_sequence *seq = &u->sequence;
cf907b11
CL
1898 __be32 *p, status;
1899
1900 status = nfsd4_decode_sessionid4(argp, &seq->sessionid);
1901 if (status)
1902 return status;
1903 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 4);
1904 if (!p)
1905 return nfserr_bad_xdr;
1906 seq->seqid = be32_to_cpup(p++);
1907 seq->slotid = be32_to_cpup(p++);
fc8738c6
N
1908 /* sa_highest_slotid counts from 0 but maxslots counts from 1 ... */
1909 seq->maxslots = be32_to_cpup(p++) + 1;
cf907b11
CL
1910 seq->cachethis = be32_to_cpup(p);
1911
3fdc5464 1912 seq->status_flags = 0;
cf907b11
CL
1913 return nfs_ok;
1914}
1915
b7a0c8f6 1916static __be32
e78e274e
KC
1917nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp,
1918 union nfsd4_op_u *u)
b7a0c8f6 1919{
e78e274e 1920 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
b7a0c8f6
CL
1921 struct nfsd4_test_stateid_id *stateid;
1922 __be32 status;
1923 u32 i;
1924
3fdc5464 1925 memset(test_stateid, 0, sizeof(*test_stateid));
b7a0c8f6
CL
1926 if (xdr_stream_decode_u32(argp->xdr, &test_stateid->ts_num_ids) < 0)
1927 return nfserr_bad_xdr;
1928
1929 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1930 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1931 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
1932 if (!stateid)
bb4d8427 1933 return nfserr_jukebox;
b7a0c8f6
CL
1934 INIT_LIST_HEAD(&stateid->ts_id_list);
1935 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1936 status = nfsd4_decode_stateid4(argp, &stateid->ts_id_stateid);
1937 if (status)
1938 return status;
1939 }
1940
1941 return nfs_ok;
1942}
1943
c95f2ec3 1944static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp,
e78e274e 1945 union nfsd4_op_u *u)
c95f2ec3 1946{
e78e274e 1947 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
c95f2ec3
CL
1948 return nfsd4_decode_clientid4(argp, &dc->clientid);
1949}
1950
0d646784 1951static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp,
e78e274e 1952 union nfsd4_op_u *u)
0d646784 1953{
e78e274e 1954 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
0d646784
CL
1955 if (xdr_stream_decode_bool(argp->xdr, &rc->rca_one_fs) < 0)
1956 return nfserr_bad_xdr;
1957 return nfs_ok;
1958}
1959
95d871f0
AS
1960static __be32
1961nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
e78e274e 1962 union nfsd4_op_u *u)
95d871f0 1963{
e78e274e 1964 struct nfsd4_fallocate *fallocate = &u->allocate;
6aef27aa 1965 __be32 status;
95d871f0 1966
6aef27aa 1967 status = nfsd4_decode_stateid4(argp, &fallocate->falloc_stateid);
95d871f0
AS
1968 if (status)
1969 return status;
6aef27aa
CL
1970 if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_offset) < 0)
1971 return nfserr_bad_xdr;
1972 if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_length) < 0)
1973 return nfserr_bad_xdr;
95d871f0 1974
6aef27aa 1975 return nfs_ok;
95d871f0
AS
1976}
1977
84e1b21d
OK
1978static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1979 struct nl4_server *ns)
1980{
84e1b21d 1981 struct nfs42_netaddr *naddr;
f49e4b4d 1982 __be32 *p;
84e1b21d 1983
f49e4b4d
CL
1984 if (xdr_stream_decode_u32(argp->xdr, &ns->nl4_type) < 0)
1985 return nfserr_bad_xdr;
84e1b21d
OK
1986
1987 /* currently support for 1 inter-server source server */
1988 switch (ns->nl4_type) {
1989 case NL4_NETADDR:
1990 naddr = &ns->u.nl4_addr;
1991
f49e4b4d
CL
1992 if (xdr_stream_decode_u32(argp->xdr, &naddr->netid_len) < 0)
1993 return nfserr_bad_xdr;
84e1b21d 1994 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
f49e4b4d 1995 return nfserr_bad_xdr;
84e1b21d 1996
f49e4b4d
CL
1997 p = xdr_inline_decode(argp->xdr, naddr->netid_len);
1998 if (!p)
1999 return nfserr_bad_xdr;
2000 memcpy(naddr->netid, p, naddr->netid_len);
84e1b21d 2001
f49e4b4d
CL
2002 if (xdr_stream_decode_u32(argp->xdr, &naddr->addr_len) < 0)
2003 return nfserr_bad_xdr;
84e1b21d 2004 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
f49e4b4d 2005 return nfserr_bad_xdr;
84e1b21d 2006
f49e4b4d
CL
2007 p = xdr_inline_decode(argp->xdr, naddr->addr_len);
2008 if (!p)
2009 return nfserr_bad_xdr;
2010 memcpy(naddr->addr, p, naddr->addr_len);
84e1b21d
OK
2011 break;
2012 default:
f49e4b4d 2013 return nfserr_bad_xdr;
84e1b21d 2014 }
f49e4b4d
CL
2015
2016 return nfs_ok;
84e1b21d
OK
2017}
2018
29ae7f9d 2019static __be32
e78e274e 2020nfsd4_decode_copy(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
29ae7f9d 2021{
e78e274e 2022 struct nfsd4_copy *copy = &u->copy;
1913cdf5 2023 u32 consecutive, i, count, sync;
84e1b21d 2024 struct nl4_server *ns_dummy;
e8febea7 2025 __be32 status;
29ae7f9d 2026
3fdc5464 2027 memset(copy, 0, sizeof(*copy));
e8febea7 2028 status = nfsd4_decode_stateid4(argp, &copy->cp_src_stateid);
29ae7f9d
AS
2029 if (status)
2030 return status;
e8febea7 2031 status = nfsd4_decode_stateid4(argp, &copy->cp_dst_stateid);
29ae7f9d
AS
2032 if (status)
2033 return status;
e8febea7
CL
2034 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_src_pos) < 0)
2035 return nfserr_bad_xdr;
2036 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_dst_pos) < 0)
2037 return nfserr_bad_xdr;
2038 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_count) < 0)
2039 return nfserr_bad_xdr;
2040 /* ca_consecutive: we always do consecutive copies */
2041 if (xdr_stream_decode_u32(argp->xdr, &consecutive) < 0)
2042 return nfserr_bad_xdr;
1913cdf5 2043 if (xdr_stream_decode_bool(argp->xdr, &sync) < 0)
e8febea7 2044 return nfserr_bad_xdr;
1913cdf5 2045 nfsd4_copy_set_sync(copy, sync);
29ae7f9d 2046
e8febea7
CL
2047 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
2048 return nfserr_bad_xdr;
87689df6
CL
2049 copy->cp_src = svcxdr_tmpalloc(argp, sizeof(*copy->cp_src));
2050 if (copy->cp_src == NULL)
2051 return nfserr_jukebox;
84e1b21d 2052 if (count == 0) { /* intra-server copy */
1913cdf5 2053 __set_bit(NFSD4_COPY_F_INTRA, &copy->cp_flags);
e8febea7 2054 return nfs_ok;
84e1b21d
OK
2055 }
2056
e8febea7 2057 /* decode all the supplied server addresses but use only the first */
87689df6 2058 status = nfsd4_decode_nl4_server(argp, copy->cp_src);
84e1b21d
OK
2059 if (status)
2060 return status;
2061
2062 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
2063 if (ns_dummy == NULL)
bb4d8427 2064 return nfserr_jukebox;
84e1b21d
OK
2065 for (i = 0; i < count - 1; i++) {
2066 status = nfsd4_decode_nl4_server(argp, ns_dummy);
2067 if (status) {
2068 kfree(ns_dummy);
2069 return status;
2070 }
2071 }
2072 kfree(ns_dummy);
29ae7f9d 2073
e8febea7 2074 return nfs_ok;
29ae7f9d
AS
2075}
2076
51911868
OK
2077static __be32
2078nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
e78e274e 2079 union nfsd4_op_u *u)
51911868 2080{
e78e274e 2081 struct nfsd4_copy_notify *cn = &u->copy_notify;
5aff7d08 2082 __be32 status;
51911868 2083
3fdc5464 2084 memset(cn, 0, sizeof(*cn));
09426ef2
CL
2085 cn->cpn_src = svcxdr_tmpalloc(argp, sizeof(*cn->cpn_src));
2086 if (cn->cpn_src == NULL)
2087 return nfserr_jukebox;
2088 cn->cpn_dst = svcxdr_tmpalloc(argp, sizeof(*cn->cpn_dst));
2089 if (cn->cpn_dst == NULL)
2090 return nfserr_jukebox;
2091
f9a953fb 2092 status = nfsd4_decode_stateid4(argp, &cn->cpn_src_stateid);
51911868
OK
2093 if (status)
2094 return status;
09426ef2 2095 return nfsd4_decode_nl4_server(argp, cn->cpn_dst);
51911868
OK
2096}
2097
f9a953fb
CL
2098static __be32
2099nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
e78e274e 2100 union nfsd4_op_u *u)
f9a953fb 2101{
e78e274e 2102 struct nfsd4_offload_status *os = &u->offload_status;
3fdc5464
CL
2103 os->count = 0;
2104 os->status = 0;
2846bb05 2105 return nfsd4_decode_stateid4(argp, &os->stateid);
f9a953fb
CL
2106}
2107
24bab491 2108static __be32
e78e274e 2109nfsd4_decode_seek(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
24bab491 2110{
e78e274e 2111 struct nfsd4_seek *seek = &u->seek;
9d32b412 2112 __be32 status;
24bab491 2113
9d32b412 2114 status = nfsd4_decode_stateid4(argp, &seek->seek_stateid);
24bab491
AS
2115 if (status)
2116 return status;
9d32b412
CL
2117 if (xdr_stream_decode_u64(argp->xdr, &seek->seek_offset) < 0)
2118 return nfserr_bad_xdr;
2119 if (xdr_stream_decode_u32(argp->xdr, &seek->seek_whence) < 0)
2120 return nfserr_bad_xdr;
24bab491 2121
3fdc5464
CL
2122 seek->seek_eof = 0;
2123 seek->seek_pos = 0;
9d32b412 2124 return nfs_ok;
24bab491
AS
2125}
2126
3dfd0b0e 2127static __be32
e78e274e 2128nfsd4_decode_clone(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
3dfd0b0e 2129{
e78e274e 2130 struct nfsd4_clone *clone = &u->clone;
3dfd0b0e
CL
2131 __be32 status;
2132
2133 status = nfsd4_decode_stateid4(argp, &clone->cl_src_stateid);
2134 if (status)
2135 return status;
2136 status = nfsd4_decode_stateid4(argp, &clone->cl_dst_stateid);
2137 if (status)
2138 return status;
2139 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_src_pos) < 0)
2140 return nfserr_bad_xdr;
2141 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_dst_pos) < 0)
2142 return nfserr_bad_xdr;
2143 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_count) < 0)
2144 return nfserr_bad_xdr;
2145
2146 return nfs_ok;
2147}
2148
23e50fe3
FL
2149/*
2150 * XDR data that is more than PAGE_SIZE in size is normally part of a
2151 * read or write. However, the size of extended attributes is limited
2152 * by the maximum request size, and then further limited by the underlying
2153 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
2154 * is 64k). Since there is no kvec- or page-based interface to xattrs,
2155 * and we're not dealing with contiguous pages, we need to do some copying.
2156 */
2157
2158/*
c1346a12 2159 * Decode data into buffer.
23e50fe3
FL
2160 */
2161static __be32
c1346a12 2162nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
dbc834e5 2163 char **bufp, size_t buflen)
23e50fe3 2164{
c1346a12
CL
2165 struct page **pages = xdr->pages;
2166 struct kvec *head = xdr->head;
23e50fe3
FL
2167 char *tmp, *dp;
2168 u32 len;
2169
2170 if (buflen <= head->iov_len) {
2171 /*
2172 * We're in luck, the head has enough space. Just return
2173 * the head, no need for copying.
2174 */
2175 *bufp = head->iov_base;
2176 return 0;
2177 }
2178
2179 tmp = svcxdr_tmpalloc(argp, buflen);
2180 if (tmp == NULL)
2181 return nfserr_jukebox;
2182
2183 dp = tmp;
2184 memcpy(dp, head->iov_base, head->iov_len);
2185 buflen -= head->iov_len;
2186 dp += head->iov_len;
2187
2188 while (buflen > 0) {
2189 len = min_t(u32, buflen, PAGE_SIZE);
2190 memcpy(dp, page_address(*pages), len);
2191
2192 buflen -= len;
2193 dp += len;
2194 pages++;
2195 }
2196
2197 *bufp = tmp;
2198 return 0;
2199}
2200
2201/*
2202 * Get a user extended attribute name from the XDR buffer.
2203 * It will not have the "user." prefix, so prepend it.
2204 * Lastly, check for nul characters in the name.
2205 */
2206static __be32
2207nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2208{
23e50fe3
FL
2209 char *name, *sp, *dp;
2210 u32 namelen, cnt;
830c7150 2211 __be32 *p;
23e50fe3 2212
830c7150
CL
2213 if (xdr_stream_decode_u32(argp->xdr, &namelen) < 0)
2214 return nfserr_bad_xdr;
23e50fe3
FL
2215 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2216 return nfserr_nametoolong;
23e50fe3 2217 if (namelen == 0)
830c7150
CL
2218 return nfserr_bad_xdr;
2219 p = xdr_inline_decode(argp->xdr, namelen);
2220 if (!p)
2221 return nfserr_bad_xdr;
23e50fe3
FL
2222 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2223 if (!name)
2224 return nfserr_jukebox;
23e50fe3
FL
2225 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2226
2227 /*
2228 * Copy the extended attribute name over while checking for 0
2229 * characters.
2230 */
2231 sp = (char *)p;
2232 dp = name + XATTR_USER_PREFIX_LEN;
2233 cnt = namelen;
2234
2235 while (cnt-- > 0) {
2236 if (*sp == '\0')
830c7150 2237 return nfserr_bad_xdr;
23e50fe3
FL
2238 *dp++ = *sp++;
2239 }
2240 *dp = '\0';
2241
2242 *namep = name;
2243
830c7150 2244 return nfs_ok;
23e50fe3
FL
2245}
2246
2247/*
2248 * A GETXATTR op request comes without a length specifier. We just set the
2249 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2250 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2251 * check it against getxa_len, and allocate + return the value.
2252 */
2253static __be32
2254nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
e78e274e 2255 union nfsd4_op_u *u)
23e50fe3 2256{
e78e274e 2257 struct nfsd4_getxattr *getxattr = &u->getxattr;
23e50fe3
FL
2258 __be32 status;
2259 u32 maxcount;
2260
3fdc5464 2261 memset(getxattr, 0, sizeof(*getxattr));
23e50fe3
FL
2262 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2263 if (status)
2264 return status;
2265
2266 maxcount = svc_max_payload(argp->rqstp);
2267 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2268
2269 getxattr->getxa_len = maxcount;
3fdc5464 2270 return nfs_ok;
23e50fe3
FL
2271}
2272
2273static __be32
2274nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
e78e274e 2275 union nfsd4_op_u *u)
23e50fe3 2276{
e78e274e 2277 struct nfsd4_setxattr *setxattr = &u->setxattr;
23e50fe3 2278 u32 flags, maxcount, size;
403366a7 2279 __be32 status;
23e50fe3 2280
3fdc5464
CL
2281 memset(setxattr, 0, sizeof(*setxattr));
2282
403366a7
CL
2283 if (xdr_stream_decode_u32(argp->xdr, &flags) < 0)
2284 return nfserr_bad_xdr;
23e50fe3
FL
2285
2286 if (flags > SETXATTR4_REPLACE)
2287 return nfserr_inval;
2288 setxattr->setxa_flags = flags;
2289
2290 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2291 if (status)
2292 return status;
2293
2294 maxcount = svc_max_payload(argp->rqstp);
2295 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2296
403366a7
CL
2297 if (xdr_stream_decode_u32(argp->xdr, &size) < 0)
2298 return nfserr_bad_xdr;
23e50fe3
FL
2299 if (size > maxcount)
2300 return nfserr_xattr2big;
2301
2302 setxattr->setxa_len = size;
2303 if (size > 0) {
c1346a12 2304 struct xdr_buf payload;
23e50fe3 2305
c1346a12 2306 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
403366a7 2307 return nfserr_bad_xdr;
c1346a12
CL
2308 status = nfsd4_vbuf_from_vector(argp, &payload,
2309 &setxattr->setxa_buf, size);
23e50fe3
FL
2310 }
2311
403366a7 2312 return nfs_ok;
23e50fe3
FL
2313}
2314
2315static __be32
2316nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
e78e274e 2317 union nfsd4_op_u *u)
23e50fe3 2318{
e78e274e 2319 struct nfsd4_listxattrs *listxattrs = &u->listxattrs;
23e50fe3
FL
2320 u32 maxcount;
2321
3fdc5464
CL
2322 memset(listxattrs, 0, sizeof(*listxattrs));
2323
2212036c
CL
2324 if (xdr_stream_decode_u64(argp->xdr, &listxattrs->lsxa_cookie) < 0)
2325 return nfserr_bad_xdr;
23e50fe3
FL
2326
2327 /*
2328 * If the cookie is too large to have even one user.x attribute
2329 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2330 */
2331 if (listxattrs->lsxa_cookie >=
2332 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2333 return nfserr_badcookie;
2334
2212036c
CL
2335 if (xdr_stream_decode_u32(argp->xdr, &maxcount) < 0)
2336 return nfserr_bad_xdr;
23e50fe3
FL
2337 if (maxcount < 8)
2338 /* Always need at least 2 words (length and one character) */
2339 return nfserr_inval;
2340
2341 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2342 listxattrs->lsxa_maxcount = maxcount;
2343
2212036c 2344 return nfs_ok;
23e50fe3
FL
2345}
2346
2347static __be32
2348nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
e78e274e 2349 union nfsd4_op_u *u)
23e50fe3 2350{
e78e274e 2351 struct nfsd4_removexattr *removexattr = &u->removexattr;
3fdc5464 2352 memset(removexattr, 0, sizeof(*removexattr));
23e50fe3
FL
2353 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2354}
2355
347e0ad9 2356static __be32
e78e274e 2357nfsd4_decode_noop(struct nfsd4_compoundargs *argp, union nfsd4_op_u *p)
347e0ad9
BH
2358{
2359 return nfs_ok;
2360}
2361
3c375c6f 2362static __be32
e78e274e 2363nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, union nfsd4_op_u *p)
3c375c6f 2364{
1e685ec2 2365 return nfserr_notsupp;
3c375c6f
BH
2366}
2367
e78e274e 2368typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u);
347e0ad9 2369
c1df609d 2370static const nfsd4_dec nfsd4_dec_ops[] = {
e78e274e
KC
2371 [OP_ACCESS] = nfsd4_decode_access,
2372 [OP_CLOSE] = nfsd4_decode_close,
2373 [OP_COMMIT] = nfsd4_decode_commit,
2374 [OP_CREATE] = nfsd4_decode_create,
2375 [OP_DELEGPURGE] = nfsd4_decode_notsupp,
2376 [OP_DELEGRETURN] = nfsd4_decode_delegreturn,
2377 [OP_GETATTR] = nfsd4_decode_getattr,
2378 [OP_GETFH] = nfsd4_decode_noop,
2379 [OP_LINK] = nfsd4_decode_link,
2380 [OP_LOCK] = nfsd4_decode_lock,
2381 [OP_LOCKT] = nfsd4_decode_lockt,
2382 [OP_LOCKU] = nfsd4_decode_locku,
2383 [OP_LOOKUP] = nfsd4_decode_lookup,
2384 [OP_LOOKUPP] = nfsd4_decode_noop,
2385 [OP_NVERIFY] = nfsd4_decode_verify,
2386 [OP_OPEN] = nfsd4_decode_open,
2387 [OP_OPENATTR] = nfsd4_decode_notsupp,
2388 [OP_OPEN_CONFIRM] = nfsd4_decode_open_confirm,
2389 [OP_OPEN_DOWNGRADE] = nfsd4_decode_open_downgrade,
2390 [OP_PUTFH] = nfsd4_decode_putfh,
202f3903 2391 [OP_PUTPUBFH] = nfsd4_decode_noop,
e78e274e
KC
2392 [OP_PUTROOTFH] = nfsd4_decode_noop,
2393 [OP_READ] = nfsd4_decode_read,
2394 [OP_READDIR] = nfsd4_decode_readdir,
2395 [OP_READLINK] = nfsd4_decode_noop,
2396 [OP_REMOVE] = nfsd4_decode_remove,
2397 [OP_RENAME] = nfsd4_decode_rename,
2398 [OP_RENEW] = nfsd4_decode_renew,
2399 [OP_RESTOREFH] = nfsd4_decode_noop,
2400 [OP_SAVEFH] = nfsd4_decode_noop,
2401 [OP_SECINFO] = nfsd4_decode_secinfo,
2402 [OP_SETATTR] = nfsd4_decode_setattr,
2403 [OP_SETCLIENTID] = nfsd4_decode_setclientid,
2404 [OP_SETCLIENTID_CONFIRM] = nfsd4_decode_setclientid_confirm,
2405 [OP_VERIFY] = nfsd4_decode_verify,
2406 [OP_WRITE] = nfsd4_decode_write,
2407 [OP_RELEASE_LOCKOWNER] = nfsd4_decode_release_lockowner,
2db134eb
AA
2408
2409 /* new operations for NFSv4.1 */
e78e274e
KC
2410 [OP_BACKCHANNEL_CTL] = nfsd4_decode_backchannel_ctl,
2411 [OP_BIND_CONN_TO_SESSION] = nfsd4_decode_bind_conn_to_session,
2412 [OP_EXCHANGE_ID] = nfsd4_decode_exchange_id,
2413 [OP_CREATE_SESSION] = nfsd4_decode_create_session,
2414 [OP_DESTROY_SESSION] = nfsd4_decode_destroy_session,
2415 [OP_FREE_STATEID] = nfsd4_decode_free_stateid,
33a1e6ea 2416 [OP_GET_DIR_DELEGATION] = nfsd4_decode_get_dir_delegation,
9cf514cc 2417#ifdef CONFIG_NFSD_PNFS
e78e274e
KC
2418 [OP_GETDEVICEINFO] = nfsd4_decode_getdeviceinfo,
2419 [OP_GETDEVICELIST] = nfsd4_decode_notsupp,
2420 [OP_LAYOUTCOMMIT] = nfsd4_decode_layoutcommit,
2421 [OP_LAYOUTGET] = nfsd4_decode_layoutget,
2422 [OP_LAYOUTRETURN] = nfsd4_decode_layoutreturn,
9cf514cc 2423#else
e78e274e
KC
2424 [OP_GETDEVICEINFO] = nfsd4_decode_notsupp,
2425 [OP_GETDEVICELIST] = nfsd4_decode_notsupp,
2426 [OP_LAYOUTCOMMIT] = nfsd4_decode_notsupp,
2427 [OP_LAYOUTGET] = nfsd4_decode_notsupp,
2428 [OP_LAYOUTRETURN] = nfsd4_decode_notsupp,
9cf514cc 2429#endif
e78e274e
KC
2430 [OP_SECINFO_NO_NAME] = nfsd4_decode_secinfo_no_name,
2431 [OP_SEQUENCE] = nfsd4_decode_sequence,
2432 [OP_SET_SSV] = nfsd4_decode_notsupp,
2433 [OP_TEST_STATEID] = nfsd4_decode_test_stateid,
2434 [OP_WANT_DELEGATION] = nfsd4_decode_notsupp,
2435 [OP_DESTROY_CLIENTID] = nfsd4_decode_destroy_clientid,
2436 [OP_RECLAIM_COMPLETE] = nfsd4_decode_reclaim_complete,
87a15a80
AS
2437
2438 /* new operations for NFSv4.2 */
e78e274e
KC
2439 [OP_ALLOCATE] = nfsd4_decode_fallocate,
2440 [OP_COPY] = nfsd4_decode_copy,
2441 [OP_COPY_NOTIFY] = nfsd4_decode_copy_notify,
2442 [OP_DEALLOCATE] = nfsd4_decode_fallocate,
2443 [OP_IO_ADVISE] = nfsd4_decode_notsupp,
2444 [OP_LAYOUTERROR] = nfsd4_decode_notsupp,
2445 [OP_LAYOUTSTATS] = nfsd4_decode_notsupp,
2446 [OP_OFFLOAD_CANCEL] = nfsd4_decode_offload_status,
2447 [OP_OFFLOAD_STATUS] = nfsd4_decode_offload_status,
2448 [OP_READ_PLUS] = nfsd4_decode_read,
2449 [OP_SEEK] = nfsd4_decode_seek,
2450 [OP_WRITE_SAME] = nfsd4_decode_notsupp,
2451 [OP_CLONE] = nfsd4_decode_clone,
23e50fe3 2452 /* RFC 8276 extended atributes operations */
e78e274e
KC
2453 [OP_GETXATTR] = nfsd4_decode_getxattr,
2454 [OP_SETXATTR] = nfsd4_decode_setxattr,
2455 [OP_LISTXATTRS] = nfsd4_decode_listxattrs,
2456 [OP_REMOVEXATTR] = nfsd4_decode_removexattr,
2db134eb
AA
2457};
2458
e1a90ebd
AS
2459static inline bool
2460nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2461{
8217d146 2462 if (op->opnum < FIRST_NFS4_OP)
e1a90ebd 2463 return false;
8217d146 2464 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
e1a90ebd 2465 return false;
8217d146
AS
2466 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2467 return false;
2468 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
e1a90ebd
AS
2469 return false;
2470 return true;
2471}
f2feb96b 2472
c44b31c2 2473static bool
1da177e4
LT
2474nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2475{
1da177e4 2476 struct nfsd4_op *op;
1091006c 2477 bool cachethis = false;
a5cddc88
BF
2478 int auth_slack= argp->rqstp->rq_auth_slack;
2479 int max_reply = auth_slack + 8; /* opcnt, status */
b0e35fda
BF
2480 int readcount = 0;
2481 int readbytes = 0;
d9b74bda 2482 __be32 *p;
1da177e4
LT
2483 int i;
2484
d9b74bda 2485 if (xdr_stream_decode_u32(argp->xdr, &argp->taglen) < 0)
c44b31c2 2486 return false;
d9b74bda
CL
2487 max_reply += XDR_UNIT;
2488 argp->tag = NULL;
2489 if (unlikely(argp->taglen)) {
2490 if (argp->taglen > NFSD4_MAX_TAGLEN)
c44b31c2 2491 return false;
d9b74bda
CL
2492 p = xdr_inline_decode(argp->xdr, argp->taglen);
2493 if (!p)
c44b31c2 2494 return false;
7b723008 2495 argp->tag = svcxdr_savemem(argp, p, argp->taglen);
d9b74bda 2496 if (!argp->tag)
c44b31c2 2497 return false;
d9b74bda
CL
2498 max_reply += xdr_align_size(argp->taglen);
2499 }
2500
2501 if (xdr_stream_decode_u32(argp->xdr, &argp->minorversion) < 0)
c44b31c2 2502 return false;
7518a3dc 2503 if (xdr_stream_decode_u32(argp->xdr, &argp->client_opcnt) < 0)
c44b31c2 2504 return false;
7518a3dc
CL
2505 argp->opcnt = min_t(u32, argp->client_opcnt,
2506 NFSD_MAX_OPS_PER_COMPOUND);
1da177e4 2507
e8c96f8c 2508 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
80e591ce 2509 argp->ops = vcalloc(argp->opcnt, sizeof(*argp->ops));
1da177e4
LT
2510 if (!argp->ops) {
2511 argp->ops = argp->iops;
c44b31c2 2512 return false;
1da177e4
LT
2513 }
2514 }
2515
e1a90ebd 2516 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
30cff1ff
BH
2517 argp->opcnt = 0;
2518
1da177e4
LT
2519 for (i = 0; i < argp->opcnt; i++) {
2520 op = &argp->ops[i];
2521 op->replay = NULL;
804d8e0a 2522 op->opdesc = NULL;
1da177e4 2523
3a237b4a 2524 if (xdr_stream_decode_u32(argp->xdr, &op->opnum) < 0)
c44b31c2 2525 return false;
08281341 2526 if (nfsd4_opnum_in_range(argp, op)) {
804d8e0a 2527 op->opdesc = OPDESC(op);
e1a90ebd 2528 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
08281341
CL
2529 if (op->status != nfs_ok)
2530 trace_nfsd_compound_decode_err(argp->rqstp,
2531 argp->opcnt, i,
2532 op->opnum,
2533 op->status);
2534 } else {
1da177e4
LT
2535 op->opnum = OP_ILLEGAL;
2536 op->status = nfserr_op_illegal;
1da177e4 2537 }
804d8e0a 2538
1091006c
BF
2539 /*
2540 * We'll try to cache the result in the DRC if any one
2541 * op in the compound wants to be cached:
2542 */
2543 cachethis |= nfsd4_cache_this_op(op);
6ff40dec 2544
528b8493 2545 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
b0e35fda
BF
2546 readcount++;
2547 readbytes += nfsd4_max_reply(argp->rqstp, op);
2548 } else
2549 max_reply += nfsd4_max_reply(argp->rqstp, op);
f7b43d0c 2550 /*
7323f0d2
KM
2551 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2552 * (Special case because it will just skip encoding this
2553 * if it runs out of xdr buffer space, and it is the only
2554 * operation that behaves this way.)
f7b43d0c 2555 */
7323f0d2 2556 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
f7b43d0c 2557 max_reply += NFS4_OPAQUE_LIMIT;
e372ba60
BF
2558
2559 if (op->status) {
2560 argp->opcnt = i+1;
2561 break;
2562 }
1da177e4 2563 }
1091006c
BF
2564 /* Sessions make the DRC unnecessary: */
2565 if (argp->minorversion)
2566 cachethis = false;
58d72168 2567 svc_reserve_auth(argp->rqstp, max_reply + readbytes);
1091006c 2568 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1da177e4 2569
a2c91753 2570 argp->splice_ok = nfsd_read_splice_ok(argp->rqstp);
a5cddc88 2571 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
a2c91753 2572 argp->splice_ok = false;
b0e35fda 2573
c44b31c2 2574 return true;
1da177e4 2575}
1da177e4 2576
3283bf64
CL
2577static __be32 nfsd4_encode_nfs_fh4(struct xdr_stream *xdr,
2578 struct knfsd_fh *fh_handle)
2579{
2580 return nfsd4_encode_opaque(xdr, fh_handle->fh_raw, fh_handle->fh_size);
2581}
2582
eed4d1ad 2583/* This is a frequently-encoded type; open-coded for speed */
26217679 2584static __be32 nfsd4_encode_nfstime4(struct xdr_stream *xdr,
eed4d1ad 2585 const struct timespec64 *tv)
26217679
CL
2586{
2587 __be32 *p;
2588
2589 p = xdr_reserve_space(xdr, XDR_UNIT * 3);
2590 if (!p)
2591 return nfserr_resource;
eed4d1ad 2592 p = xdr_encode_hyper(p, tv->tv_sec);
26217679
CL
2593 *p = cpu_to_be32(tv->tv_nsec);
2594 return nfs_ok;
2595}
2596
a460cda2
CL
2597static __be32 nfsd4_encode_specdata4(struct xdr_stream *xdr,
2598 unsigned int major, unsigned int minor)
2599{
2600 __be32 status;
2601
2602 status = nfsd4_encode_uint32_t(xdr, major);
2603 if (status != nfs_ok)
2604 return status;
2605 return nfsd4_encode_uint32_t(xdr, minor);
2606}
2607
66a21db7 2608static __be32
263453d9 2609nfsd4_encode_change_info4(struct xdr_stream *xdr, const struct nfsd4_change_info *c)
c654b8a9 2610{
263453d9
CL
2611 __be32 status;
2612
2613 status = nfsd4_encode_bool(xdr, c->atomic);
2614 if (status != nfs_ok)
2615 return status;
2616 status = nfsd4_encode_changeid4(xdr, c->before_change);
2617 if (status != nfs_ok)
2618 return status;
2619 return nfsd4_encode_changeid4(xdr, c->after_change);
c654b8a9 2620}
1da177e4 2621
21d316a7
CL
2622static __be32 nfsd4_encode_netaddr4(struct xdr_stream *xdr,
2623 const struct nfs42_netaddr *addr)
2624{
2625 __be32 status;
2626
2627 /* na_r_netid */
2628 status = nfsd4_encode_opaque(xdr, addr->netid, addr->netid_len);
2629 if (status != nfs_ok)
2630 return status;
2631 /* na_r_addr */
2632 return nfsd4_encode_opaque(xdr, addr->addr, addr->addr_len);
2633}
2634
81c3f413 2635/* Encode as an array of strings the string given with components
e7a0444a 2636 * separated @sep, escaped with esc_enter and esc_exit.
81c3f413 2637 */
ddd1ea56
BF
2638static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2639 char *components, char esc_enter,
2640 char esc_exit)
81c3f413 2641{
ddd1ea56 2642 __be32 *p;
082d4bd7
BF
2643 __be32 pathlen;
2644 int pathlen_offset;
81c3f413 2645 int strlen, count=0;
e7a0444a 2646 char *str, *end, *next;
81c3f413
BF
2647
2648 dprintk("nfsd4_encode_components(%s)\n", components);
082d4bd7
BF
2649
2650 pathlen_offset = xdr->buf->len;
ddd1ea56
BF
2651 p = xdr_reserve_space(xdr, 4);
2652 if (!p)
81c3f413 2653 return nfserr_resource;
082d4bd7
BF
2654 p++; /* We will fill this in with @count later */
2655
81c3f413
BF
2656 end = str = components;
2657 while (*end) {
e7a0444a
WAA
2658 bool found_esc = false;
2659
2660 /* try to parse as esc_start, ..., esc_end, sep */
2661 if (*str == esc_enter) {
2662 for (; *end && (*end != esc_exit); end++)
2663 /* find esc_exit or end of string */;
2664 next = end + 1;
2665 if (*end && (!*next || *next == sep)) {
2666 str++;
2667 found_esc = true;
2668 }
2669 }
2670
2671 if (!found_esc)
2672 for (; *end && (*end != sep); end++)
2673 /* find sep or end of string */;
2674
81c3f413
BF
2675 strlen = end - str;
2676 if (strlen) {
30c1d241 2677 if (xdr_stream_encode_opaque(xdr, str, strlen) < 0)
81c3f413 2678 return nfserr_resource;
81c3f413 2679 count++;
30c1d241 2680 } else
81c3f413 2681 end++;
5a64e569
BC
2682 if (found_esc)
2683 end = next;
2684
81c3f413
BF
2685 str = end;
2686 }
bf7491f1 2687 pathlen = htonl(count);
082d4bd7 2688 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
81c3f413
BF
2689 return 0;
2690}
2691
e7a0444a
WAA
2692/* Encode as an array of strings the string given with components
2693 * separated @sep.
2694 */
ddd1ea56
BF
2695static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2696 char *components)
e7a0444a 2697{
ddd1ea56 2698 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
e7a0444a
WAA
2699}
2700
ddd1ea56
BF
2701static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2702 struct nfsd4_fs_location *location)
81c3f413 2703{
b37ad28b 2704 __be32 status;
81c3f413 2705
ddd1ea56 2706 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
e7a0444a 2707 '[', ']');
81c3f413
BF
2708 if (status)
2709 return status;
ddd1ea56 2710 status = nfsd4_encode_components(xdr, '/', location->path);
81c3f413
BF
2711 if (status)
2712 return status;
a1469a37 2713 return nfs_ok;
81c3f413
BF
2714}
2715
a1469a37
CL
2716static __be32 nfsd4_encode_pathname4(struct xdr_stream *xdr,
2717 const struct path *root,
2718 const struct path *path)
81c3f413 2719{
301f0268 2720 struct path cur = *path;
ed748aac
TM
2721 struct dentry **components = NULL;
2722 unsigned int ncomponents = 0;
2723 __be32 err = nfserr_jukebox;
81c3f413 2724
ed748aac 2725 dprintk("nfsd4_encode_components(");
81c3f413 2726
ed748aac
TM
2727 path_get(&cur);
2728 /* First walk the path up to the nfsd root, and store the
2729 * dentries/path components in an array.
2730 */
2731 for (;;) {
b77a4b2e 2732 if (path_equal(&cur, root))
ed748aac
TM
2733 break;
2734 if (cur.dentry == cur.mnt->mnt_root) {
2735 if (follow_up(&cur))
2736 continue;
2737 goto out_free;
2738 }
2739 if ((ncomponents & 15) == 0) {
2740 struct dentry **new;
2741 new = krealloc(components,
2742 sizeof(*new) * (ncomponents + 16),
2743 GFP_KERNEL);
2744 if (!new)
2745 goto out_free;
2746 components = new;
2747 }
2748 components[ncomponents++] = cur.dentry;
2749 cur.dentry = dget_parent(cur.dentry);
2750 }
6b9c1080 2751
ddd1ea56 2752 err = nfserr_resource;
6b9c1080 2753 if (xdr_stream_encode_u32(xdr, ncomponents) != XDR_UNIT)
ed748aac 2754 goto out_free;
ed748aac
TM
2755 while (ncomponents) {
2756 struct dentry *dentry = components[ncomponents - 1];
ed748aac 2757
301f0268 2758 spin_lock(&dentry->d_lock);
6b9c1080
CL
2759 if (xdr_stream_encode_opaque(xdr, dentry->d_name.name,
2760 dentry->d_name.len) < 0) {
301f0268 2761 spin_unlock(&dentry->d_lock);
ed748aac 2762 goto out_free;
301f0268 2763 }
a455589f 2764 dprintk("/%pd", dentry);
301f0268 2765 spin_unlock(&dentry->d_lock);
ed748aac
TM
2766 dput(dentry);
2767 ncomponents--;
81c3f413 2768 }
ed748aac 2769
ed748aac
TM
2770 err = 0;
2771out_free:
2772 dprintk(")\n");
2773 while (ncomponents)
2774 dput(components[--ncomponents]);
2775 kfree(components);
2776 path_put(&cur);
2777 return err;
2778}
2779
a1469a37
CL
2780static __be32 nfsd4_encode_fs_locations4(struct xdr_stream *xdr,
2781 struct svc_rqst *rqstp,
2782 struct svc_export *exp)
ed748aac 2783{
a1469a37 2784 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
ed748aac 2785 struct svc_export *exp_ps;
a1469a37
CL
2786 unsigned int i;
2787 __be32 status;
ed748aac 2788
a1469a37 2789 /* fs_root */
ed748aac
TM
2790 exp_ps = rqst_find_fsidzero_export(rqstp);
2791 if (IS_ERR(exp_ps))
2792 return nfserrno(PTR_ERR(exp_ps));
a1469a37 2793 status = nfsd4_encode_pathname4(xdr, &exp_ps->ex_path, &exp->ex_path);
ed748aac 2794 exp_put(exp_ps);
a1469a37 2795 if (status != nfs_ok)
81c3f413 2796 return status;
a1469a37
CL
2797
2798 /* locations<> */
2799 if (xdr_stream_encode_u32(xdr, fslocs->locations_count) != XDR_UNIT)
81c3f413 2800 return nfserr_resource;
a1469a37 2801 for (i = 0; i < fslocs->locations_count; i++) {
ddd1ea56 2802 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
a1469a37 2803 if (status != nfs_ok)
81c3f413
BF
2804 return status;
2805 }
a1469a37
CL
2806
2807 return nfs_ok;
81c3f413 2808}
1da177e4 2809
0207ee08
CL
2810static __be32 nfsd4_encode_nfsace4(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2811 struct nfs4_ace *ace)
1da177e4 2812{
0207ee08
CL
2813 __be32 status;
2814
2815 /* type */
2816 status = nfsd4_encode_acetype4(xdr, ace->type);
2817 if (status != nfs_ok)
2818 return nfserr_resource;
2819 /* flag */
2820 status = nfsd4_encode_aceflag4(xdr, ace->flag);
2821 if (status != nfs_ok)
2822 return nfserr_resource;
2823 /* access mask */
2824 status = nfsd4_encode_acemask4(xdr, ace->access_mask & NFS4_ACE_MASK_ALL);
2825 if (status != nfs_ok)
2826 return nfserr_resource;
2827 /* who */
3554116d 2828 if (ace->whotype != NFS4_ACL_WHO_NAMED)
ddd1ea56 2829 return nfs4_acl_write_who(xdr, ace->whotype);
0207ee08 2830 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
ddd1ea56 2831 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
0207ee08 2832 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
1da177e4
LT
2833}
2834
42ca0993
BF
2835#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2836 FATTR4_WORD0_RDATTR_ERROR)
2837#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
c2227a39 2838#define WORD2_ABSENT_FS_ATTRS 0
42ca0993 2839
18032ca0
DQ
2840#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2841static inline __be32
ddd1ea56 2842nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
76ecf306 2843 const struct lsm_context *context)
18032ca0 2844{
ddd1ea56 2845 __be32 *p;
18032ca0 2846
76ecf306 2847 p = xdr_reserve_space(xdr, context->len + 4 + 4 + 4);
ddd1ea56 2848 if (!p)
18032ca0
DQ
2849 return nfserr_resource;
2850
2851 /*
2852 * For now we use a 0 here to indicate the null translation; in
2853 * the future we may place a call to translation code here.
2854 */
c373b0a4
BF
2855 *p++ = cpu_to_be32(0); /* lfs */
2856 *p++ = cpu_to_be32(0); /* pi */
76ecf306 2857 p = xdr_encode_opaque(p, context->context, context->len);
18032ca0
DQ
2858 return 0;
2859}
2860#else
2861static inline __be32
ddd1ea56 2862nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
76ecf306 2863 struct lsm_context *context)
18032ca0
DQ
2864{ return 0; }
2865#endif
2866
c2227a39 2867static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
42ca0993
BF
2868{
2869 /* As per referral draft: */
2870 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2871 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2872 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2873 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2874 *rdattr_err = NFSERR_MOVED;
2875 else
2876 return nfserr_moved;
2877 }
2878 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2879 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
c2227a39 2880 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
42ca0993
BF
2881 return 0;
2882}
1da177e4 2883
ae7095a7 2884
6106d911 2885static int nfsd4_get_mounted_on_ino(struct svc_export *exp, u64 *pino)
ae7095a7
BF
2886{
2887 struct path path = exp->ex_path;
6106d911 2888 struct kstat stat;
ae7095a7
BF
2889 int err;
2890
2891 path_get(&path);
2892 while (follow_up(&path)) {
2893 if (path.dentry != path.mnt->mnt_root)
2894 break;
2895 }
6106d911 2896 err = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
ae7095a7 2897 path_put(&path);
6106d911
JL
2898 if (!err)
2899 *pino = stat.ino;
ae7095a7
BF
2900 return err;
2901}
2902
75976de6 2903static __be32
e64301f5 2904nfsd4_encode_bitmap4(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
75976de6
KM
2905{
2906 __be32 *p;
2907
2908 if (bmval2) {
e64301f5 2909 p = xdr_reserve_space(xdr, XDR_UNIT * 4);
75976de6
KM
2910 if (!p)
2911 goto out_resource;
2912 *p++ = cpu_to_be32(3);
2913 *p++ = cpu_to_be32(bmval0);
2914 *p++ = cpu_to_be32(bmval1);
2915 *p++ = cpu_to_be32(bmval2);
2916 } else if (bmval1) {
e64301f5 2917 p = xdr_reserve_space(xdr, XDR_UNIT * 3);
75976de6
KM
2918 if (!p)
2919 goto out_resource;
2920 *p++ = cpu_to_be32(2);
2921 *p++ = cpu_to_be32(bmval0);
2922 *p++ = cpu_to_be32(bmval1);
2923 } else {
e64301f5 2924 p = xdr_reserve_space(xdr, XDR_UNIT * 2);
75976de6
KM
2925 if (!p)
2926 goto out_resource;
2927 *p++ = cpu_to_be32(1);
2928 *p++ = cpu_to_be32(bmval0);
2929 }
2930
e64301f5 2931 return nfs_ok;
75976de6
KM
2932out_resource:
2933 return nfserr_resource;
2934}
2935
83ab8678 2936struct nfsd4_fattr_args {
c9090e27 2937 struct svc_rqst *rqstp;
83ab8678 2938 struct svc_fh *fhp;
36ed7e64 2939 struct svc_export *exp;
c9090e27 2940 struct dentry *dentry;
83ab8678
CL
2941 struct kstat stat;
2942 struct kstatfs statfs;
2943 struct nfs4_acl *acl;
53150305 2944 u64 change_attr;
f59388a5 2945#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
76ecf306 2946 struct lsm_context context;
f59388a5 2947#endif
83ab8678
CL
2948 u32 rdattr_err;
2949 bool contextsupport;
1b9097e3 2950 bool ignore_crossmnt;
83ab8678
CL
2951};
2952
fce7913b
CL
2953typedef __be32(*nfsd4_enc_attr)(struct xdr_stream *xdr,
2954 const struct nfsd4_fattr_args *args);
2955
2956static __be32 nfsd4_encode_fattr4__noop(struct xdr_stream *xdr,
2957 const struct nfsd4_fattr_args *args)
2958{
2959 return nfs_ok;
2960}
2961
c88cb472
CL
2962static __be32 nfsd4_encode_fattr4__true(struct xdr_stream *xdr,
2963 const struct nfsd4_fattr_args *args)
2964{
2965 return nfsd4_encode_bool(xdr, true);
2966}
2967
8c442288
CL
2968static __be32 nfsd4_encode_fattr4__false(struct xdr_stream *xdr,
2969 const struct nfsd4_fattr_args *args)
2970{
2971 return nfsd4_encode_bool(xdr, false);
2972}
2973
c9090e27
CL
2974static __be32 nfsd4_encode_fattr4_supported_attrs(struct xdr_stream *xdr,
2975 const struct nfsd4_fattr_args *args)
2976{
2977 struct nfsd4_compoundres *resp = args->rqstp->rq_resp;
2978 u32 minorversion = resp->cstate.minorversion;
2979 u32 supp[3];
2980
2981 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
2982 if (!IS_POSIXACL(d_inode(args->dentry)))
2983 supp[0] &= ~FATTR4_WORD0_ACL;
2984 if (!args->contextsupport)
2985 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2986
2987 return nfsd4_encode_bitmap4(xdr, supp[0], supp[1], supp[2]);
2988}
2989
b06cf375
CL
2990static __be32 nfsd4_encode_fattr4_type(struct xdr_stream *xdr,
2991 const struct nfsd4_fattr_args *args)
2992{
2993 __be32 *p;
2994
2995 p = xdr_reserve_space(xdr, XDR_UNIT);
2996 if (!p)
2997 return nfserr_resource;
2998
2999 switch (args->stat.mode & S_IFMT) {
3000 case S_IFIFO:
3001 *p = cpu_to_be32(NF4FIFO);
3002 break;
3003 case S_IFCHR:
3004 *p = cpu_to_be32(NF4CHR);
3005 break;
3006 case S_IFDIR:
3007 *p = cpu_to_be32(NF4DIR);
3008 break;
3009 case S_IFBLK:
3010 *p = cpu_to_be32(NF4BLK);
3011 break;
3012 case S_IFLNK:
3013 *p = cpu_to_be32(NF4LNK);
3014 break;
3015 case S_IFREG:
3016 *p = cpu_to_be32(NF4REG);
3017 break;
3018 case S_IFSOCK:
3019 *p = cpu_to_be32(NF4SOCK);
3020 break;
3021 default:
3022 return nfserr_serverfault;
3023 }
3024
3025 return nfs_ok;
3026}
3027
36ed7e64
CL
3028static __be32 nfsd4_encode_fattr4_fh_expire_type(struct xdr_stream *xdr,
3029 const struct nfsd4_fattr_args *args)
3030{
3031 u32 mask;
3032
3033 mask = NFS4_FH_PERSISTENT;
3034 if (!(args->exp->ex_flags & NFSEXP_NOSUBTREECHECK))
3035 mask |= NFS4_FH_VOL_RENAME;
3036 return nfsd4_encode_uint32_t(xdr, mask);
3037}
3038
263453d9
CL
3039static __be32 nfsd4_encode_fattr4_change(struct xdr_stream *xdr,
3040 const struct nfsd4_fattr_args *args)
3041{
3042 const struct svc_export *exp = args->exp;
263453d9
CL
3043
3044 if (unlikely(exp->ex_flags & NFSEXP_V4ROOT)) {
3045 u32 flush_time = convert_to_wallclock(exp->cd->flush_time);
3046
3047 if (xdr_stream_encode_u32(xdr, flush_time) != XDR_UNIT)
3048 return nfserr_resource;
3049 if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
3050 return nfserr_resource;
3051 return nfs_ok;
3052 }
53150305 3053 return nfsd4_encode_changeid4(xdr, args->change_attr);
263453d9
CL
3054}
3055
d0b28aad
CL
3056static __be32 nfsd4_encode_fattr4_size(struct xdr_stream *xdr,
3057 const struct nfsd4_fattr_args *args)
3058{
3a405432 3059 return nfsd4_encode_uint64_t(xdr, args->stat.size);
d0b28aad
CL
3060}
3061
b6b62595
CL
3062static __be32 nfsd4_encode_fattr4_fsid(struct xdr_stream *xdr,
3063 const struct nfsd4_fattr_args *args)
3064{
3065 __be32 *p;
3066
3067 p = xdr_reserve_space(xdr, XDR_UNIT * 2 + XDR_UNIT * 2);
3068 if (!p)
3069 return nfserr_resource;
3070
3071 if (unlikely(args->exp->ex_fslocs.migrated)) {
3072 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
3073 xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
3074 return nfs_ok;
3075 }
3076 switch (fsid_source(args->fhp)) {
3077 case FSIDSOURCE_FSID:
3078 p = xdr_encode_hyper(p, (u64)args->exp->ex_fsid);
3079 xdr_encode_hyper(p, (u64)0);
3080 break;
3081 case FSIDSOURCE_DEV:
3082 *p++ = xdr_zero;
3083 *p++ = cpu_to_be32(MAJOR(args->stat.dev));
3084 *p++ = xdr_zero;
3085 *p = cpu_to_be32(MINOR(args->stat.dev));
3086 break;
3087 case FSIDSOURCE_UUID:
3088 xdr_encode_opaque_fixed(p, args->exp->ex_uuid, EX_UUID_LEN);
3089 break;
3090 }
3091
3092 return nfs_ok;
3093}
3094
1252b283
CL
3095static __be32 nfsd4_encode_fattr4_lease_time(struct xdr_stream *xdr,
3096 const struct nfsd4_fattr_args *args)
3097{
3098 struct nfsd_net *nn = net_generic(SVC_NET(args->rqstp), nfsd_net_id);
3099
3100 return nfsd4_encode_nfs_lease4(xdr, nn->nfsd4_lease);
3101}
3102
782448e1
CL
3103static __be32 nfsd4_encode_fattr4_rdattr_error(struct xdr_stream *xdr,
3104 const struct nfsd4_fattr_args *args)
3105{
3106 return nfsd4_encode_uint32_t(xdr, args->rdattr_err);
3107}
3108
6515b7d7
CL
3109static __be32 nfsd4_encode_fattr4_aclsupport(struct xdr_stream *xdr,
3110 const struct nfsd4_fattr_args *args)
3111{
3112 u32 mask;
3113
3114 mask = 0;
3115 if (IS_POSIXACL(d_inode(args->dentry)))
3116 mask = ACL4_SUPPORT_ALLOW_ACL | ACL4_SUPPORT_DENY_ACL;
3117 return nfsd4_encode_uint32_t(xdr, mask);
3118}
3119
07455dc4
CL
3120static __be32 nfsd4_encode_fattr4_acl(struct xdr_stream *xdr,
3121 const struct nfsd4_fattr_args *args)
3122{
3123 struct nfs4_acl *acl = args->acl;
3124 struct nfs4_ace *ace;
3125 __be32 status;
3126
3127 /* nfsace4<> */
3128 if (!acl) {
3129 if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
3130 return nfserr_resource;
3131 } else {
3132 if (xdr_stream_encode_u32(xdr, acl->naces) != XDR_UNIT)
3133 return nfserr_resource;
3134 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
3135 status = nfsd4_encode_nfsace4(xdr, args->rqstp, ace);
3136 if (status != nfs_ok)
3137 return status;
3138 }
3139 }
3140 return nfs_ok;
3141}
3142
3283bf64
CL
3143static __be32 nfsd4_encode_fattr4_filehandle(struct xdr_stream *xdr,
3144 const struct nfsd4_fattr_args *args)
3145{
3146 return nfsd4_encode_nfs_fh4(xdr, &args->fhp->fh_handle);
3147}
3148
eb7ece81
CL
3149static __be32 nfsd4_encode_fattr4_fileid(struct xdr_stream *xdr,
3150 const struct nfsd4_fattr_args *args)
3151{
3152 return nfsd4_encode_uint64_t(xdr, args->stat.ino);
3153}
3154
b0c3a5f8
CL
3155static __be32 nfsd4_encode_fattr4_files_avail(struct xdr_stream *xdr,
3156 const struct nfsd4_fattr_args *args)
3157{
3158 return nfsd4_encode_uint64_t(xdr, args->statfs.f_ffree);
3159}
3160
74361e2b
CL
3161static __be32 nfsd4_encode_fattr4_files_free(struct xdr_stream *xdr,
3162 const struct nfsd4_fattr_args *args)
3163{
3164 return nfsd4_encode_uint64_t(xdr, args->statfs.f_ffree);
3165}
3166
b56b7526
CL
3167static __be32 nfsd4_encode_fattr4_files_total(struct xdr_stream *xdr,
3168 const struct nfsd4_fattr_args *args)
3169{
3170 return nfsd4_encode_uint64_t(xdr, args->statfs.f_files);
3171}
3172
a1469a37
CL
3173static __be32 nfsd4_encode_fattr4_fs_locations(struct xdr_stream *xdr,
3174 const struct nfsd4_fattr_args *args)
3175{
3176 return nfsd4_encode_fs_locations4(xdr, args->rqstp, args->exp);
3177}
3178
7c605dcc
CL
3179static __be32 nfsd4_encode_fattr4_maxfilesize(struct xdr_stream *xdr,
3180 const struct nfsd4_fattr_args *args)
3181{
3182 struct super_block *sb = args->exp->ex_path.mnt->mnt_sb;
3183
3184 return nfsd4_encode_uint64_t(xdr, sb->s_maxbytes);
3185}
3186
b066aa5c
CL
3187static __be32 nfsd4_encode_fattr4_maxlink(struct xdr_stream *xdr,
3188 const struct nfsd4_fattr_args *args)
3189{
3190 return nfsd4_encode_uint32_t(xdr, 255);
3191}
3192
9c1adacc
CL
3193static __be32 nfsd4_encode_fattr4_maxname(struct xdr_stream *xdr,
3194 const struct nfsd4_fattr_args *args)
3195{
3196 return nfsd4_encode_uint32_t(xdr, args->statfs.f_namelen);
3197}
3198
c17195c3
CL
3199static __be32 nfsd4_encode_fattr4_maxread(struct xdr_stream *xdr,
3200 const struct nfsd4_fattr_args *args)
3201{
3202 return nfsd4_encode_uint64_t(xdr, svc_max_payload(args->rqstp));
3203}
3204
951378dc
CL
3205static __be32 nfsd4_encode_fattr4_maxwrite(struct xdr_stream *xdr,
3206 const struct nfsd4_fattr_args *args)
3207{
3208 return nfsd4_encode_uint64_t(xdr, svc_max_payload(args->rqstp));
3209}
3210
f4cf5042
CL
3211static __be32 nfsd4_encode_fattr4_mode(struct xdr_stream *xdr,
3212 const struct nfsd4_fattr_args *args)
3213{
3214 return nfsd4_encode_mode4(xdr, args->stat.mode & S_IALLUGO);
3215}
3216
9f329fea
CL
3217static __be32 nfsd4_encode_fattr4_numlinks(struct xdr_stream *xdr,
3218 const struct nfsd4_fattr_args *args)
3219{
3220 return nfsd4_encode_uint32_t(xdr, args->stat.nlink);
3221}
3222
fa51a520
CL
3223static __be32 nfsd4_encode_fattr4_owner(struct xdr_stream *xdr,
3224 const struct nfsd4_fattr_args *args)
3225{
3226 return nfsd4_encode_user(xdr, args->rqstp, args->stat.uid);
3227}
3228
62f31e56
CL
3229static __be32 nfsd4_encode_fattr4_owner_group(struct xdr_stream *xdr,
3230 const struct nfsd4_fattr_args *args)
3231{
3232 return nfsd4_encode_group(xdr, args->rqstp, args->stat.gid);
3233}
3234
a460cda2
CL
3235static __be32 nfsd4_encode_fattr4_rawdev(struct xdr_stream *xdr,
3236 const struct nfsd4_fattr_args *args)
3237{
3238 return nfsd4_encode_specdata4(xdr, MAJOR(args->stat.rdev),
3239 MINOR(args->stat.rdev));
3240}
3241
83afa091
CL
3242static __be32 nfsd4_encode_fattr4_space_avail(struct xdr_stream *xdr,
3243 const struct nfsd4_fattr_args *args)
3244{
3245 u64 avail = (u64)args->statfs.f_bavail * (u64)args->statfs.f_bsize;
3246
3247 return nfsd4_encode_uint64_t(xdr, avail);
3248}
3249
74ebc697
CL
3250static __be32 nfsd4_encode_fattr4_space_free(struct xdr_stream *xdr,
3251 const struct nfsd4_fattr_args *args)
3252{
3253 u64 free = (u64)args->statfs.f_bfree * (u64)args->statfs.f_bsize;
3254
3255 return nfsd4_encode_uint64_t(xdr, free);
3256}
3257
d0cde979
CL
3258static __be32 nfsd4_encode_fattr4_space_total(struct xdr_stream *xdr,
3259 const struct nfsd4_fattr_args *args)
3260{
3261 u64 total = (u64)args->statfs.f_blocks * (u64)args->statfs.f_bsize;
3262
3263 return nfsd4_encode_uint64_t(xdr, total);
3264}
3265
6d37ac3a
CL
3266static __be32 nfsd4_encode_fattr4_space_used(struct xdr_stream *xdr,
3267 const struct nfsd4_fattr_args *args)
3268{
3269 return nfsd4_encode_uint64_t(xdr, (u64)args->stat.blocks << 9);
3270}
3271
eed4d1ad
CL
3272static __be32 nfsd4_encode_fattr4_time_access(struct xdr_stream *xdr,
3273 const struct nfsd4_fattr_args *args)
3274{
3275 return nfsd4_encode_nfstime4(xdr, &args->stat.atime);
3276}
3277
2e38722d
CL
3278static __be32 nfsd4_encode_fattr4_time_create(struct xdr_stream *xdr,
3279 const struct nfsd4_fattr_args *args)
3280{
3281 return nfsd4_encode_nfstime4(xdr, &args->stat.btime);
3282}
3283
993474e8
CL
3284/*
3285 * ctime (in NFSv4, time_metadata) is not writeable, and the client
3286 * doesn't really care what resolution could theoretically be stored by
3287 * the filesystem.
3288 *
3289 * The client cares how close together changes can be while still
3290 * guaranteeing ctime changes. For most filesystems (which have
3291 * timestamps with nanosecond fields) that is limited by the resolution
3292 * of the time returned from current_time() (which I'm assuming to be
3293 * 1/HZ).
3294 */
3295static __be32 nfsd4_encode_fattr4_time_delta(struct xdr_stream *xdr,
3296 const struct nfsd4_fattr_args *args)
3297{
3298 const struct inode *inode = d_inode(args->dentry);
3299 u32 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
3300 struct timespec64 ts = ns_to_timespec64(ns);
3301
3302 return nfsd4_encode_nfstime4(xdr, &ts);
3303}
3304
673720bc
CL
3305static __be32 nfsd4_encode_fattr4_time_metadata(struct xdr_stream *xdr,
3306 const struct nfsd4_fattr_args *args)
3307{
3308 return nfsd4_encode_nfstime4(xdr, &args->stat.ctime);
3309}
3310
d1828611
CL
3311static __be32 nfsd4_encode_fattr4_time_modify(struct xdr_stream *xdr,
3312 const struct nfsd4_fattr_args *args)
3313{
3314 return nfsd4_encode_nfstime4(xdr, &args->stat.mtime);
3315}
3316
1b9097e3
CL
3317static __be32 nfsd4_encode_fattr4_mounted_on_fileid(struct xdr_stream *xdr,
3318 const struct nfsd4_fattr_args *args)
3319{
3320 u64 ino;
3321 int err;
3322
3323 if (!args->ignore_crossmnt &&
3324 args->dentry == args->exp->ex_path.mnt->mnt_root) {
3325 err = nfsd4_get_mounted_on_ino(args->exp, &ino);
3326 if (err)
3327 return nfserrno(err);
3328 } else
3329 ino = args->stat.ino;
3330
3331 return nfsd4_encode_uint64_t(xdr, ino);
3332}
3333
e7a5b1b2
CL
3334#ifdef CONFIG_NFSD_PNFS
3335
3336static __be32 nfsd4_encode_fattr4_fs_layout_types(struct xdr_stream *xdr,
3337 const struct nfsd4_fattr_args *args)
3338{
3339 unsigned long mask = args->exp->ex_layout_types;
3340 int i;
3341
3342 /* Hamming weight of @mask is the number of layout types to return */
3343 if (xdr_stream_encode_u32(xdr, hweight_long(mask)) != XDR_UNIT)
3344 return nfserr_resource;
3345 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
3346 if (mask & BIT(i)) {
3347 /* layouttype4 */
3348 if (xdr_stream_encode_u32(xdr, i) != XDR_UNIT)
3349 return nfserr_resource;
3350 }
3351 return nfs_ok;
3352}
3353
4c15878e
CL
3354static __be32 nfsd4_encode_fattr4_layout_types(struct xdr_stream *xdr,
3355 const struct nfsd4_fattr_args *args)
3356{
3357 unsigned long mask = args->exp->ex_layout_types;
3358 int i;
3359
3360 /* Hamming weight of @mask is the number of layout types to return */
3361 if (xdr_stream_encode_u32(xdr, hweight_long(mask)) != XDR_UNIT)
3362 return nfserr_resource;
3363 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
3364 if (mask & BIT(i)) {
3365 /* layouttype4 */
3366 if (xdr_stream_encode_u32(xdr, i) != XDR_UNIT)
3367 return nfserr_resource;
3368 }
3369 return nfs_ok;
3370}
3371
4c584731
CL
3372static __be32 nfsd4_encode_fattr4_layout_blksize(struct xdr_stream *xdr,
3373 const struct nfsd4_fattr_args *args)
3374{
3375 return nfsd4_encode_uint32_t(xdr, args->stat.blksize);
3376}
3377
e7a5b1b2
CL
3378#endif
3379
345c3877
CL
3380static __be32 nfsd4_encode_fattr4_suppattr_exclcreat(struct xdr_stream *xdr,
3381 const struct nfsd4_fattr_args *args)
3382{
3383 struct nfsd4_compoundres *resp = args->rqstp->rq_resp;
3384 u32 supp[3];
3385
3386 memcpy(supp, nfsd_suppattrs[resp->cstate.minorversion], sizeof(supp));
3387 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3388 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3389 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3390
3391 return nfsd4_encode_bitmap4(xdr, supp[0], supp[1], supp[2]);
3392}
3393
d6ca7d26
CL
3394/*
3395 * Copied from generic_remap_checks/generic_remap_file_range_prep.
3396 *
3397 * These generic functions use the file system's s_blocksize, but
3398 * individual file systems aren't required to use
3399 * generic_remap_file_range_prep. Until there is a mechanism for
3400 * determining a particular file system's (or file's) clone block
3401 * size, this is the best NFSD can do.
3402 */
3403static __be32 nfsd4_encode_fattr4_clone_blksize(struct xdr_stream *xdr,
3404 const struct nfsd4_fattr_args *args)
3405{
3406 struct inode *inode = d_inode(args->dentry);
3407
3408 return nfsd4_encode_uint32_t(xdr, inode->i_sb->s_blocksize);
3409}
3410
f59388a5
CL
3411#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3412static __be32 nfsd4_encode_fattr4_sec_label(struct xdr_stream *xdr,
3413 const struct nfsd4_fattr_args *args)
3414{
76ecf306 3415 return nfsd4_encode_security_label(xdr, args->rqstp, &args->context);
f59388a5
CL
3416}
3417#endif
3418
b3dbf4e4
CL
3419static __be32 nfsd4_encode_fattr4_xattr_support(struct xdr_stream *xdr,
3420 const struct nfsd4_fattr_args *args)
3421{
3422 int err = xattr_supports_user_prefix(d_inode(args->dentry));
3423
3424 return nfsd4_encode_bool(xdr, err == 0);
3425}
3426
51c0d4f7
JL
3427#define NFSD_OA_SHARE_ACCESS (BIT(OPEN_ARGS_SHARE_ACCESS_READ) | \
3428 BIT(OPEN_ARGS_SHARE_ACCESS_WRITE) | \
3429 BIT(OPEN_ARGS_SHARE_ACCESS_BOTH))
3430
3431#define NFSD_OA_SHARE_DENY (BIT(OPEN_ARGS_SHARE_DENY_NONE) | \
3432 BIT(OPEN_ARGS_SHARE_DENY_READ) | \
3433 BIT(OPEN_ARGS_SHARE_DENY_WRITE) | \
3434 BIT(OPEN_ARGS_SHARE_DENY_BOTH))
3435
3436#define NFSD_OA_SHARE_ACCESS_WANT (BIT(OPEN_ARGS_SHARE_ACCESS_WANT_ANY_DELEG) | \
3437 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_NO_DELEG) | \
6ae30d6e 3438 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_CANCEL) | \
d3edfd9e
JL
3439 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS) | \
3440 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION))
51c0d4f7
JL
3441
3442#define NFSD_OA_OPEN_CLAIM (BIT(OPEN_ARGS_OPEN_CLAIM_NULL) | \
3443 BIT(OPEN_ARGS_OPEN_CLAIM_PREVIOUS) | \
3444 BIT(OPEN_ARGS_OPEN_CLAIM_DELEGATE_CUR) | \
3445 BIT(OPEN_ARGS_OPEN_CLAIM_DELEGATE_PREV)| \
3446 BIT(OPEN_ARGS_OPEN_CLAIM_FH) | \
3447 BIT(OPEN_ARGS_OPEN_CLAIM_DELEG_CUR_FH) | \
3448 BIT(OPEN_ARGS_OPEN_CLAIM_DELEG_PREV_FH))
3449
3450#define NFSD_OA_CREATE_MODE (BIT(OPEN_ARGS_CREATEMODE_UNCHECKED4) | \
3451 BIT(OPEN_ARGS_CREATE_MODE_GUARDED) | \
3452 BIT(OPEN_ARGS_CREATEMODE_EXCLUSIVE4) | \
3453 BIT(OPEN_ARGS_CREATE_MODE_EXCLUSIVE4_1))
3454
3455static uint32_t oa_share_access = NFSD_OA_SHARE_ACCESS;
3456static uint32_t oa_share_deny = NFSD_OA_SHARE_DENY;
3457static uint32_t oa_share_access_want = NFSD_OA_SHARE_ACCESS_WANT;
3458static uint32_t oa_open_claim = NFSD_OA_OPEN_CLAIM;
3459static uint32_t oa_create_mode = NFSD_OA_CREATE_MODE;
3460
3461static const struct open_arguments4 nfsd_open_arguments = {
3462 .oa_share_access = { .count = 1, .element = &oa_share_access },
3463 .oa_share_deny = { .count = 1, .element = &oa_share_deny },
3464 .oa_share_access_want = { .count = 1, .element = &oa_share_access_want },
3465 .oa_open_claim = { .count = 1, .element = &oa_open_claim },
3466 .oa_create_mode = { .count = 1, .element = &oa_create_mode },
3467};
3468
3469static __be32 nfsd4_encode_fattr4_open_arguments(struct xdr_stream *xdr,
3470 const struct nfsd4_fattr_args *args)
3471{
3472 if (!xdrgen_encode_fattr4_open_arguments(xdr, &nfsd_open_arguments))
3473 return nfserr_resource;
3474 return nfs_ok;
3475}
3476
fce7913b
CL
3477static const nfsd4_enc_attr nfsd4_enc_fattr4_encode_ops[] = {
3478 [FATTR4_SUPPORTED_ATTRS] = nfsd4_encode_fattr4_supported_attrs,
3479 [FATTR4_TYPE] = nfsd4_encode_fattr4_type,
3480 [FATTR4_FH_EXPIRE_TYPE] = nfsd4_encode_fattr4_fh_expire_type,
3481 [FATTR4_CHANGE] = nfsd4_encode_fattr4_change,
3482 [FATTR4_SIZE] = nfsd4_encode_fattr4_size,
3483 [FATTR4_LINK_SUPPORT] = nfsd4_encode_fattr4__true,
3484 [FATTR4_SYMLINK_SUPPORT] = nfsd4_encode_fattr4__true,
3485 [FATTR4_NAMED_ATTR] = nfsd4_encode_fattr4__false,
3486 [FATTR4_FSID] = nfsd4_encode_fattr4_fsid,
3487 [FATTR4_UNIQUE_HANDLES] = nfsd4_encode_fattr4__true,
3488 [FATTR4_LEASE_TIME] = nfsd4_encode_fattr4_lease_time,
3489 [FATTR4_RDATTR_ERROR] = nfsd4_encode_fattr4_rdattr_error,
3490 [FATTR4_ACL] = nfsd4_encode_fattr4_acl,
3491 [FATTR4_ACLSUPPORT] = nfsd4_encode_fattr4_aclsupport,
3492 [FATTR4_ARCHIVE] = nfsd4_encode_fattr4__noop,
3493 [FATTR4_CANSETTIME] = nfsd4_encode_fattr4__true,
3494 [FATTR4_CASE_INSENSITIVE] = nfsd4_encode_fattr4__false,
3495 [FATTR4_CASE_PRESERVING] = nfsd4_encode_fattr4__true,
3496 [FATTR4_CHOWN_RESTRICTED] = nfsd4_encode_fattr4__true,
3497 [FATTR4_FILEHANDLE] = nfsd4_encode_fattr4_filehandle,
3498 [FATTR4_FILEID] = nfsd4_encode_fattr4_fileid,
3499 [FATTR4_FILES_AVAIL] = nfsd4_encode_fattr4_files_avail,
3500 [FATTR4_FILES_FREE] = nfsd4_encode_fattr4_files_free,
3501 [FATTR4_FILES_TOTAL] = nfsd4_encode_fattr4_files_total,
3502 [FATTR4_FS_LOCATIONS] = nfsd4_encode_fattr4_fs_locations,
3503 [FATTR4_HIDDEN] = nfsd4_encode_fattr4__noop,
3504 [FATTR4_HOMOGENEOUS] = nfsd4_encode_fattr4__true,
3505 [FATTR4_MAXFILESIZE] = nfsd4_encode_fattr4_maxfilesize,
3506 [FATTR4_MAXLINK] = nfsd4_encode_fattr4_maxlink,
3507 [FATTR4_MAXNAME] = nfsd4_encode_fattr4_maxname,
3508 [FATTR4_MAXREAD] = nfsd4_encode_fattr4_maxread,
3509 [FATTR4_MAXWRITE] = nfsd4_encode_fattr4_maxwrite,
3510 [FATTR4_MIMETYPE] = nfsd4_encode_fattr4__noop,
3511 [FATTR4_MODE] = nfsd4_encode_fattr4_mode,
3512 [FATTR4_NO_TRUNC] = nfsd4_encode_fattr4__true,
3513 [FATTR4_NUMLINKS] = nfsd4_encode_fattr4_numlinks,
3514 [FATTR4_OWNER] = nfsd4_encode_fattr4_owner,
3515 [FATTR4_OWNER_GROUP] = nfsd4_encode_fattr4_owner_group,
3516 [FATTR4_QUOTA_AVAIL_HARD] = nfsd4_encode_fattr4__noop,
3517 [FATTR4_QUOTA_AVAIL_SOFT] = nfsd4_encode_fattr4__noop,
3518 [FATTR4_QUOTA_USED] = nfsd4_encode_fattr4__noop,
3519 [FATTR4_RAWDEV] = nfsd4_encode_fattr4_rawdev,
3520 [FATTR4_SPACE_AVAIL] = nfsd4_encode_fattr4_space_avail,
3521 [FATTR4_SPACE_FREE] = nfsd4_encode_fattr4_space_free,
3522 [FATTR4_SPACE_TOTAL] = nfsd4_encode_fattr4_space_total,
3523 [FATTR4_SPACE_USED] = nfsd4_encode_fattr4_space_used,
3524 [FATTR4_SYSTEM] = nfsd4_encode_fattr4__noop,
3525 [FATTR4_TIME_ACCESS] = nfsd4_encode_fattr4_time_access,
3526 [FATTR4_TIME_ACCESS_SET] = nfsd4_encode_fattr4__noop,
3527 [FATTR4_TIME_BACKUP] = nfsd4_encode_fattr4__noop,
3528 [FATTR4_TIME_CREATE] = nfsd4_encode_fattr4_time_create,
3529 [FATTR4_TIME_DELTA] = nfsd4_encode_fattr4_time_delta,
3530 [FATTR4_TIME_METADATA] = nfsd4_encode_fattr4_time_metadata,
3531 [FATTR4_TIME_MODIFY] = nfsd4_encode_fattr4_time_modify,
3532 [FATTR4_TIME_MODIFY_SET] = nfsd4_encode_fattr4__noop,
3533 [FATTR4_MOUNTED_ON_FILEID] = nfsd4_encode_fattr4_mounted_on_fileid,
3534 [FATTR4_DIR_NOTIF_DELAY] = nfsd4_encode_fattr4__noop,
3535 [FATTR4_DIRENT_NOTIF_DELAY] = nfsd4_encode_fattr4__noop,
3536 [FATTR4_DACL] = nfsd4_encode_fattr4__noop,
3537 [FATTR4_SACL] = nfsd4_encode_fattr4__noop,
3538 [FATTR4_CHANGE_POLICY] = nfsd4_encode_fattr4__noop,
3539 [FATTR4_FS_STATUS] = nfsd4_encode_fattr4__noop,
3540
3541#ifdef CONFIG_NFSD_PNFS
3542 [FATTR4_FS_LAYOUT_TYPES] = nfsd4_encode_fattr4_fs_layout_types,
3543 [FATTR4_LAYOUT_HINT] = nfsd4_encode_fattr4__noop,
3544 [FATTR4_LAYOUT_TYPES] = nfsd4_encode_fattr4_layout_types,
3545 [FATTR4_LAYOUT_BLKSIZE] = nfsd4_encode_fattr4_layout_blksize,
3546 [FATTR4_LAYOUT_ALIGNMENT] = nfsd4_encode_fattr4__noop,
3547#else
3548 [FATTR4_FS_LAYOUT_TYPES] = nfsd4_encode_fattr4__noop,
3549 [FATTR4_LAYOUT_HINT] = nfsd4_encode_fattr4__noop,
3550 [FATTR4_LAYOUT_TYPES] = nfsd4_encode_fattr4__noop,
3551 [FATTR4_LAYOUT_BLKSIZE] = nfsd4_encode_fattr4__noop,
3552 [FATTR4_LAYOUT_ALIGNMENT] = nfsd4_encode_fattr4__noop,
3553#endif
3554
3555 [FATTR4_FS_LOCATIONS_INFO] = nfsd4_encode_fattr4__noop,
3556 [FATTR4_MDSTHRESHOLD] = nfsd4_encode_fattr4__noop,
3557 [FATTR4_RETENTION_GET] = nfsd4_encode_fattr4__noop,
3558 [FATTR4_RETENTION_SET] = nfsd4_encode_fattr4__noop,
3559 [FATTR4_RETENTEVT_GET] = nfsd4_encode_fattr4__noop,
3560 [FATTR4_RETENTEVT_SET] = nfsd4_encode_fattr4__noop,
3561 [FATTR4_RETENTION_HOLD] = nfsd4_encode_fattr4__noop,
3562 [FATTR4_MODE_SET_MASKED] = nfsd4_encode_fattr4__noop,
3563 [FATTR4_SUPPATTR_EXCLCREAT] = nfsd4_encode_fattr4_suppattr_exclcreat,
3564 [FATTR4_FS_CHARSET_CAP] = nfsd4_encode_fattr4__noop,
d6ca7d26 3565 [FATTR4_CLONE_BLKSIZE] = nfsd4_encode_fattr4_clone_blksize,
fce7913b
CL
3566 [FATTR4_SPACE_FREED] = nfsd4_encode_fattr4__noop,
3567 [FATTR4_CHANGE_ATTR_TYPE] = nfsd4_encode_fattr4__noop,
3568
3569#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3570 [FATTR4_SEC_LABEL] = nfsd4_encode_fattr4_sec_label,
3571#else
3572 [FATTR4_SEC_LABEL] = nfsd4_encode_fattr4__noop,
3573#endif
3574
3575 [FATTR4_MODE_UMASK] = nfsd4_encode_fattr4__noop,
3576 [FATTR4_XATTR_SUPPORT] = nfsd4_encode_fattr4_xattr_support,
51c0d4f7 3577 [FATTR4_OPEN_ARGUMENTS] = nfsd4_encode_fattr4_open_arguments,
fce7913b
CL
3578};
3579
1da177e4
LT
3580/*
3581 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
3582 * ourselves.
1da177e4 3583 */
da2ebce6 3584static __be32
ae1131d4
CL
3585nfsd4_encode_fattr4(struct svc_rqst *rqstp, struct xdr_stream *xdr,
3586 struct svc_fh *fhp, struct svc_export *exp,
3587 struct dentry *dentry, const u32 *bmval,
3588 int ignore_crossmnt)
1da177e4 3589{
f488138b 3590 DECLARE_BITMAP(attr_bitmap, ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops));
f6259e2e 3591 struct nfs4_delegation *dp = NULL;
83ab8678 3592 struct nfsd4_fattr_args args;
d50e6136 3593 struct svc_fh *tempfh = NULL;
1fcea5b2 3594 int starting_len = xdr->buf->len;
4163ee71
CL
3595 unsigned int attrlen_offset;
3596 __be32 attrlen, status;
f488138b 3597 u32 attrmask[3];
b8dd7b9a 3598 int err;
7e705706
AA
3599 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3600 u32 minorversion = resp->cstate.minorversion;
ebabe9a9
CH
3601 struct path path = {
3602 .mnt = exp->ex_path.mnt,
3603 .dentry = dentry,
3604 };
fce7913b 3605 unsigned long bit;
1da177e4 3606
fce7913b
CL
3607 WARN_ON_ONCE(bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1);
3608 WARN_ON_ONCE(!nfsd_attrs_supported(minorversion, bmval));
1da177e4 3609
c9090e27 3610 args.rqstp = rqstp;
36ed7e64 3611 args.exp = exp;
c9090e27 3612 args.dentry = dentry;
1b9097e3 3613 args.ignore_crossmnt = (ignore_crossmnt != 0);
18180a45 3614 args.acl = NULL;
f58bab6f 3615#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
76ecf306 3616 args.context.context = NULL;
f58bab6f 3617#endif
c9090e27 3618
fce7913b
CL
3619 /*
3620 * Make a local copy of the attribute bitmap that can be modified.
3621 */
f488138b
VG
3622 attrmask[0] = bmval[0];
3623 attrmask[1] = bmval[1];
3624 attrmask[2] = bmval[2];
fce7913b 3625
83ab8678 3626 args.rdattr_err = 0;
42ca0993 3627 if (exp->ex_fslocs.migrated) {
f488138b
VG
3628 status = fattr_handle_absent_fs(&attrmask[0], &attrmask[1],
3629 &attrmask[2], &args.rdattr_err);
42ca0993
BF
3630 if (status)
3631 goto out;
3632 }
6ae30d6e
JL
3633 if ((attrmask[0] & (FATTR4_WORD0_CHANGE |
3634 FATTR4_WORD0_SIZE)) ||
3635 (attrmask[1] & (FATTR4_WORD1_TIME_ACCESS |
3636 FATTR4_WORD1_TIME_MODIFY |
3637 FATTR4_WORD1_TIME_METADATA))) {
f6259e2e 3638 status = nfsd4_deleg_getattr_conflict(rqstp, dentry, &dp);
fd19ca36
DN
3639 if (status)
3640 goto out;
3641 }
42ca0993 3642
83ab8678 3643 err = vfs_getattr(&path, &args.stat,
638e3e7d
JL
3644 STATX_BASIC_STATS | STATX_BTIME | STATX_CHANGE_COOKIE,
3645 AT_STATX_SYNC_AS_STAT);
f6259e2e
JL
3646 if (dp) {
3647 struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
3648
53150305
JL
3649 if (ncf->ncf_file_modified) {
3650 ++ncf->ncf_initial_cinfo;
f6259e2e 3651 args.stat.size = ncf->ncf_cur_fsize;
6ae30d6e
JL
3652 if (!timespec64_is_epoch(&ncf->ncf_cb_mtime))
3653 args.stat.mtime = ncf->ncf_cb_mtime;
53150305
JL
3654 }
3655 args.change_attr = ncf->ncf_initial_cinfo;
6ae30d6e
JL
3656
3657 if (!timespec64_is_epoch(&ncf->ncf_cb_atime))
3658 args.stat.atime = ncf->ncf_cb_atime;
f6259e2e
JL
3659
3660 nfs4_put_stid(&dp->dl_stid);
53150305
JL
3661 } else {
3662 args.change_attr = nfsd4_change_attribute(&args.stat);
f6259e2e 3663 }
53150305 3664
b8dd7b9a 3665 if (err)
1da177e4 3666 goto out_nfserr;
83ab8678
CL
3667
3668 if (!(args.stat.result_mask & STATX_BTIME))
e377a3e6 3669 /* underlying FS does not offer btime so we can't share it */
f488138b
VG
3670 attrmask[1] &= ~FATTR4_WORD1_TIME_CREATE;
3671 if ((attrmask[0] & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
12337901 3672 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
f488138b 3673 (attrmask[1] & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
1da177e4 3674 FATTR4_WORD1_SPACE_TOTAL))) {
83ab8678 3675 err = vfs_statfs(&path, &args.statfs);
b8dd7b9a 3676 if (err)
1da177e4
LT
3677 goto out_nfserr;
3678 }
f488138b 3679 if ((attrmask[0] & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) &&
fce7913b 3680 !fhp) {
d50e6136
BF
3681 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
3682 status = nfserr_jukebox;
3683 if (!tempfh)
3684 goto out;
3685 fh_init(tempfh, NFS4_FHSIZE);
3686 status = fh_compose(tempfh, exp, dentry, NULL);
1da177e4
LT
3687 if (status)
3688 goto out;
83ab8678
CL
3689 args.fhp = tempfh;
3690 } else
3691 args.fhp = fhp;
3692
f488138b 3693 if (attrmask[0] & FATTR4_WORD0_ACL) {
83ab8678 3694 err = nfsd4_get_nfs4_acl(rqstp, dentry, &args.acl);
0c9d65e7 3695 if (err == -EOPNOTSUPP)
f488138b 3696 attrmask[0] &= ~FATTR4_WORD0_ACL;
0c9d65e7
AG
3697 else if (err == -EINVAL) {
3698 status = nfserr_attrnotsupp;
3699 goto out;
3700 } else if (err != 0)
3701 goto out_nfserr;
1da177e4 3702 }
1da177e4 3703
83ab8678
CL
3704 args.contextsupport = false;
3705
18032ca0 3706#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
f488138b
VG
3707 if ((attrmask[2] & FATTR4_WORD2_SECURITY_LABEL) ||
3708 attrmask[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
32ddd944
BF
3709 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
3710 err = security_inode_getsecctx(d_inode(dentry),
76ecf306 3711 &args.context);
32ddd944
BF
3712 else
3713 err = -EOPNOTSUPP;
83ab8678 3714 args.contextsupport = (err == 0);
f488138b 3715 if (attrmask[2] & FATTR4_WORD2_SECURITY_LABEL) {
18032ca0 3716 if (err == -EOPNOTSUPP)
f488138b 3717 attrmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
18032ca0
DQ
3718 else if (err)
3719 goto out_nfserr;
3720 }
3721 }
3722#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
3723
ae1131d4 3724 /* attrmask */
f488138b
VG
3725 status = nfsd4_encode_bitmap4(xdr, attrmask[0], attrmask[1],
3726 attrmask[2]);
75976de6
KM
3727 if (status)
3728 goto out;
082d4bd7 3729
ae1131d4 3730 /* attr_vals */
082d4bd7 3731 attrlen_offset = xdr->buf->len;
4163ee71 3732 if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
ddd1ea56 3733 goto out_resource;
f488138b
VG
3734 bitmap_from_arr32(attr_bitmap, attrmask,
3735 ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops));
3736 for_each_set_bit(bit, attr_bitmap,
fce7913b
CL
3737 ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops)) {
3738 status = nfsd4_enc_fattr4_encode_ops[bit](xdr, &args);
4c584731
CL
3739 if (status != nfs_ok)
3740 goto out;
9cf514cc 3741 }
4163ee71
CL
3742 attrlen = cpu_to_be32(xdr->buf->len - attrlen_offset - XDR_UNIT);
3743 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, XDR_UNIT);
1da177e4
LT
3744 status = nfs_ok;
3745
3746out:
ba4e55bb 3747#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
76ecf306
CS
3748 if (args.context.context)
3749 security_release_secctx(&args.context);
ba4e55bb 3750#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
83ab8678 3751 kfree(args.acl);
18df11d0 3752 if (tempfh) {
d50e6136 3753 fh_put(tempfh);
18df11d0
YZ
3754 kfree(tempfh);
3755 }
1fcea5b2
BF
3756 if (status)
3757 xdr_truncate_encode(xdr, starting_len);
1da177e4
LT
3758 return status;
3759out_nfserr:
b8dd7b9a 3760 status = nfserrno(err);
1da177e4
LT
3761 goto out;
3762out_resource:
1da177e4
LT
3763 status = nfserr_resource;
3764 goto out;
1da177e4
LT
3765}
3766
2825a7f9
BF
3767static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3768 struct xdr_buf *buf, __be32 *p, int bytes)
3769{
3770 xdr->scratch.iov_len = 0;
3771 memset(buf, 0, sizeof(struct xdr_buf));
3772 buf->head[0].iov_base = p;
3773 buf->head[0].iov_len = 0;
3774 buf->len = 0;
3775 xdr->buf = buf;
3776 xdr->iov = buf->head;
3777 xdr->p = p;
3778 xdr->end = (void *)p + bytes;
3779 buf->buflen = bytes;
3780}
3781
d5184658
BF
3782__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3783 struct svc_fh *fhp, struct svc_export *exp,
3784 struct dentry *dentry, u32 *bmval,
3785 struct svc_rqst *rqstp, int ignore_crossmnt)
3786{
2825a7f9 3787 struct xdr_buf dummy;
d5184658
BF
3788 struct xdr_stream xdr;
3789 __be32 ret;
3790
2825a7f9 3791 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
ae1131d4
CL
3792 ret = nfsd4_encode_fattr4(rqstp, &xdr, fhp, exp, dentry, bmval,
3793 ignore_crossmnt);
d5184658
BF
3794 *p = xdr.p;
3795 return ret;
3796}
3797
3fc5048c
CL
3798/*
3799 * The buffer space for this field was reserved during a previous
3800 * call to nfsd4_encode_entry4().
3801 */
3802static void nfsd4_encode_entry4_nfs_cookie4(const struct nfsd4_readdir *readdir,
3803 u64 offset)
3804{
3805 __be64 cookie = cpu_to_be64(offset);
3806 struct xdr_stream *xdr = readdir->xdr;
3807
3808 if (!readdir->cookie_offset)
3809 return;
3810 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset, &cookie,
3811 sizeof(cookie));
3812}
3813
c0ce6ec8
BF
3814static inline int attributes_need_mount(u32 *bmval)
3815{
3816 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3817 return 1;
3818 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3819 return 1;
3820 return 0;
3821}
3822
b37ad28b 3823static __be32
a0f3c835
CL
3824nfsd4_encode_entry4_fattr(struct nfsd4_readdir *cd, const char *name,
3825 int namlen)
1da177e4
LT
3826{
3827 struct svc_export *exp = cd->rd_fhp->fh_export;
3828 struct dentry *dentry;
b37ad28b 3829 __be32 nfserr;
406a7ea9 3830 int ignore_crossmnt = 0;
1da177e4 3831
8ad92484
N
3832 dentry = lookup_one_positive_unlocked(&nop_mnt_idmap,
3833 &QSTR_LEN(name, namlen),
3834 cd->rd_fhp->fh_dentry);
1da177e4
LT
3835 if (IS_ERR(dentry))
3836 return nfserrno(PTR_ERR(dentry));
3837
3838 exp_get(exp);
406a7ea9
FF
3839 /*
3840 * In the case of a mountpoint, the client may be asking for
3841 * attributes that are only properties of the underlying filesystem
3842 * as opposed to the cross-mounted file system. In such a case,
3843 * we will not follow the cross mount and will fill the attribtutes
3844 * directly from the mountpoint dentry.
3845 */
3227fa41 3846 if (nfsd_mountpoint(dentry, exp)) {
021d3a72
BF
3847 int err;
3848
3227fa41
BF
3849 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3850 && !attributes_need_mount(cd->rd_bmval)) {
3851 ignore_crossmnt = 1;
3852 goto out_encode;
3853 }
dcb488a3
AA
3854 /*
3855 * Why the heck aren't we just using nfsd_lookup??
3856 * Different "."/".." handling? Something else?
3857 * At least, add a comment here to explain....
3858 */
021d3a72
BF
3859 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3860 if (err) {
3861 nfserr = nfserrno(err);
1da177e4
LT
3862 goto out_put;
3863 }
bb4f07f2 3864 nfserr = check_nfsd_access(exp, cd->rd_rqstp, false);
dcb488a3
AA
3865 if (nfserr)
3866 goto out_put;
1da177e4
LT
3867
3868 }
3227fa41 3869out_encode:
a0f3c835 3870 nfserr = nfsd4_encode_fattr4(cd->rd_rqstp, cd->xdr, NULL, exp, dentry,
ae1131d4 3871 cd->rd_bmval, ignore_crossmnt);
1da177e4
LT
3872out_put:
3873 dput(dentry);
3874 exp_put(exp);
3875 return nfserr;
3876}
3877
a0d042f8
CL
3878static __be32
3879nfsd4_encode_entry4_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
1da177e4 3880{
a0d042f8 3881 __be32 status;
1da177e4 3882
a0d042f8
CL
3883 /* attrmask */
3884 status = nfsd4_encode_bitmap4(xdr, FATTR4_WORD0_RDATTR_ERROR, 0, 0);
3885 if (status != nfs_ok)
3886 return status;
3887 /* attr_vals */
3888 if (xdr_stream_encode_u32(xdr, XDR_UNIT) != XDR_UNIT)
3889 return nfserr_resource;
3890 /* rdattr_error */
3891 if (xdr_stream_encode_be32(xdr, nfserr) != XDR_UNIT)
3892 return nfserr_resource;
3893 return nfs_ok;
1da177e4
LT
3894}
3895
3896static int
a0f3c835 3897nfsd4_encode_entry4(void *ccdv, const char *name, int namlen,
a0ad13ef 3898 loff_t offset, u64 ino, unsigned int d_type)
1da177e4 3899{
a0ad13ef 3900 struct readdir_cd *ccd = ccdv;
1da177e4 3901 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
561f0ed4
BF
3902 struct xdr_stream *xdr = cd->xdr;
3903 int start_offset = xdr->buf->len;
3904 int cookie_offset;
aee37764 3905 u32 name_and_cookie;
561f0ed4 3906 int entry_bytes;
b37ad28b 3907 __be32 nfserr = nfserr_toosmall;
1da177e4
LT
3908
3909 /* In nfsv4, "." and ".." never make it onto the wire.. */
3910 if (name && isdotent(name, namlen)) {
3911 cd->common.err = nfs_ok;
3912 return 0;
3913 }
3914
3fc5048c
CL
3915 /* Encode the previous entry's cookie value */
3916 nfsd4_encode_entry4_nfs_cookie4(cd, offset);
1da177e4 3917
a1aee9aa 3918 if (xdr_stream_encode_item_present(xdr) != XDR_UNIT)
1da177e4 3919 goto fail;
a1aee9aa
CL
3920
3921 /* Reserve send buffer space for this entry's cookie value. */
561f0ed4 3922 cookie_offset = xdr->buf->len;
a1aee9aa
CL
3923 if (nfsd4_encode_nfs_cookie4(xdr, OFFSET_MAX) != nfs_ok)
3924 goto fail;
3925 if (nfsd4_encode_component4(xdr, name, namlen) != nfs_ok)
561f0ed4 3926 goto fail;
a0f3c835 3927 nfserr = nfsd4_encode_entry4_fattr(cd, name, namlen);
1da177e4
LT
3928 switch (nfserr) {
3929 case nfs_ok:
1da177e4
LT
3930 break;
3931 case nfserr_resource:
3932 nfserr = nfserr_toosmall;
3933 goto fail;
b2c0cea6 3934 case nfserr_noent:
f41c5ad2 3935 xdr_truncate_encode(xdr, start_offset);
b2c0cea6 3936 goto skip_entry;
cad85337
JL
3937 case nfserr_jukebox:
3938 /*
3939 * The pseudoroot should only display dentries that lead to
3940 * exports. If we get EJUKEBOX here, then we can't tell whether
3941 * this entry should be included. Just fail the whole READDIR
3942 * with NFS4ERR_DELAY in that case, and hope that the situation
3943 * will resolve itself by the client's next attempt.
3944 */
3945 if (cd->rd_fhp->fh_export->ex_flags & NFSEXP_V4ROOT)
3946 goto fail;
3947 fallthrough;
1da177e4
LT
3948 default:
3949 /*
3950 * If the client requested the RDATTR_ERROR attribute,
3951 * we stuff the error code into this attribute
3952 * and continue. If this attribute was not requested,
3953 * then in accordance with the spec, we fail the
3954 * entire READDIR operation(!)
3955 */
3956 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3957 goto fail;
a0d042f8 3958 if (nfsd4_encode_entry4_rdattr_error(xdr, nfserr)) {
34081efc 3959 nfserr = nfserr_toosmall;
1da177e4 3960 goto fail;
34081efc 3961 }
1da177e4 3962 }
561f0ed4
BF
3963 nfserr = nfserr_toosmall;
3964 entry_bytes = xdr->buf->len - start_offset;
3965 if (entry_bytes > cd->rd_maxcount)
3966 goto fail;
3967 cd->rd_maxcount -= entry_bytes;
aee37764 3968 /*
f2e717d6
TM
3969 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", and
3970 * notes that it could be zero. If it is zero, then the server
3971 * should enforce only the rd_maxcount value.
aee37764 3972 */
f2e717d6
TM
3973 if (cd->rd_dircount) {
3974 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
3975 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3976 goto fail;
3977 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
3978 if (!cd->rd_dircount)
3979 cd->rd_maxcount = 0;
3980 }
0ec016e3 3981
561f0ed4 3982 cd->cookie_offset = cookie_offset;
b2c0cea6 3983skip_entry:
1da177e4
LT
3984 cd->common.err = nfs_ok;
3985 return 0;
3986fail:
561f0ed4 3987 xdr_truncate_encode(xdr, start_offset);
1da177e4
LT
3988 cd->common.err = nfserr;
3989 return -EINVAL;
3990}
3991
adaa7a50
CL
3992static __be32
3993nfsd4_encode_verifier4(struct xdr_stream *xdr, const nfs4_verifier *verf)
3994{
3995 __be32 *p;
3996
3997 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3998 if (!p)
3999 return nfserr_resource;
4000 memcpy(p, verf->data, sizeof(verf->data));
4001 return nfs_ok;
4002}
4003
4004static __be32
4005nfsd4_encode_clientid4(struct xdr_stream *xdr, const clientid_t *clientid)
4006{
4007 __be32 *p;
4008
4009 p = xdr_reserve_space(xdr, sizeof(__be64));
4010 if (!p)
4011 return nfserr_resource;
4012 memcpy(p, clientid, sizeof(*clientid));
4013 return nfs_ok;
4014}
4015
40bb2baa 4016/* This is a frequently-encoded item; open-coded for speed */
d0a381dd 4017static __be32
40bb2baa 4018nfsd4_encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
e2f282b9 4019{
bc749ca4 4020 __be32 *p;
e2f282b9 4021
40bb2baa 4022 p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
d0a381dd
BF
4023 if (!p)
4024 return nfserr_resource;
c373b0a4 4025 *p++ = cpu_to_be32(sid->si_generation);
40bb2baa
CL
4026 memcpy(p, &sid->si_opaque, sizeof(sid->si_opaque));
4027 return nfs_ok;
e2f282b9
BH
4028}
4029
65baa609
CL
4030static __be32
4031nfsd4_encode_sessionid4(struct xdr_stream *xdr,
4032 const struct nfs4_sessionid *sessionid)
4033{
4034 return nfsd4_encode_opaque_fixed(xdr, sessionid->data,
4035 NFS4_MAX_SESSIONID_LEN);
4036}
4037
695e12f8 4038static __be32
e78e274e
KC
4039nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr,
4040 union nfsd4_op_u *u)
1da177e4 4041{
e78e274e 4042 struct nfsd4_access *access = &u->access;
bddfdbcd 4043 struct xdr_stream *xdr = resp->xdr;
d38e570f 4044 __be32 status;
1da177e4 4045
d38e570f
CL
4046 /* supported */
4047 status = nfsd4_encode_uint32_t(xdr, access->ac_supported);
4048 if (status != nfs_ok)
4049 return status;
4050 /* access */
4051 return nfsd4_encode_uint32_t(xdr, access->ac_resp_access);
1da177e4
LT
4052}
4053
e78e274e
KC
4054static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr,
4055 union nfsd4_op_u *u)
1d1bc8f2 4056{
e78e274e 4057 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
bddfdbcd 4058 struct xdr_stream *xdr = resp->xdr;
1d1bc8f2 4059
65baa609
CL
4060 /* bctsr_sessid */
4061 nfserr = nfsd4_encode_sessionid4(xdr, &bcts->sessionid);
4062 if (nfserr != nfs_ok)
4063 return nfserr;
4064 /* bctsr_dir */
4065 if (xdr_stream_encode_u32(xdr, bcts->dir) != XDR_UNIT)
bac966d6 4066 return nfserr_resource;
65baa609
CL
4067 /* bctsr_use_conn_in_rdma_mode */
4068 return nfsd4_encode_bool(xdr, false);
1d1bc8f2
BF
4069}
4070
695e12f8 4071static __be32
e78e274e
KC
4072nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr,
4073 union nfsd4_op_u *u)
1da177e4 4074{
e78e274e 4075 struct nfsd4_close *close = &u->close;
bddfdbcd 4076 struct xdr_stream *xdr = resp->xdr;
d0a381dd 4077
40bb2baa
CL
4078 /* open_stateid */
4079 return nfsd4_encode_stateid4(xdr, &close->cl_stateid);
1da177e4
LT
4080}
4081
4082
695e12f8 4083static __be32
e78e274e
KC
4084nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr,
4085 union nfsd4_op_u *u)
1da177e4 4086{
e78e274e 4087 struct nfsd4_commit *commit = &u->commit;
1da177e4 4088
adaa7a50 4089 return nfsd4_encode_verifier4(resp->xdr, &commit->co_verf);
1da177e4
LT
4090}
4091
695e12f8 4092static __be32
e78e274e
KC
4093nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr,
4094 union nfsd4_op_u *u)
1da177e4 4095{
e78e274e 4096 struct nfsd4_create *create = &u->create;
bddfdbcd 4097 struct xdr_stream *xdr = resp->xdr;
1da177e4 4098
e64301f5 4099 /* cinfo */
66a21db7
CL
4100 nfserr = nfsd4_encode_change_info4(xdr, &create->cr_cinfo);
4101 if (nfserr)
4102 return nfserr;
e64301f5
CL
4103 /* attrset */
4104 return nfsd4_encode_bitmap4(xdr, create->cr_bmval[0],
4105 create->cr_bmval[1], create->cr_bmval[2]);
1da177e4
LT
4106}
4107
b37ad28b 4108static __be32
e78e274e
KC
4109nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4110 union nfsd4_op_u *u)
1da177e4 4111{
e78e274e 4112 struct nfsd4_getattr *getattr = &u->getattr;
1da177e4 4113 struct svc_fh *fhp = getattr->ga_fhp;
bddfdbcd 4114 struct xdr_stream *xdr = resp->xdr;
1da177e4 4115
ae1131d4
CL
4116 /* obj_attributes */
4117 return nfsd4_encode_fattr4(resp->rqstp, xdr, fhp, fhp->fh_export,
4118 fhp->fh_dentry, getattr->ga_bmval, 0);
1da177e4
LT
4119}
4120
695e12f8 4121static __be32
e78e274e
KC
4122nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr,
4123 union nfsd4_op_u *u)
1da177e4 4124{
bddfdbcd 4125 struct xdr_stream *xdr = resp->xdr;
3283bf64 4126 struct svc_fh *fhp = u->getfh;
1da177e4 4127
3283bf64
CL
4128 /* object */
4129 return nfsd4_encode_nfs_fh4(xdr, &fhp->fh_handle);
1da177e4
LT
4130}
4131
c4a29c52
CL
4132static __be32
4133nfsd4_encode_lock_owner4(struct xdr_stream *xdr, const clientid_t *clientid,
4134 const struct xdr_netobj *owner)
4135{
4136 __be32 status;
4137
4138 /* clientid */
4139 status = nfsd4_encode_clientid4(xdr, clientid);
4140 if (status != nfs_ok)
4141 return status;
4142 /* owner */
4143 return nfsd4_encode_opaque(xdr, owner->data, owner->len);
4144}
4145
d0a381dd 4146static __be32
c5641782
CL
4147nfsd4_encode_lock4denied(struct xdr_stream *xdr,
4148 const struct nfsd4_lock_denied *ld)
1da177e4 4149{
c5641782 4150 __be32 status;
92d82e99 4151
c5641782
CL
4152 /* offset */
4153 status = nfsd4_encode_offset4(xdr, ld->ld_start);
c4a29c52
CL
4154 if (status != nfs_ok)
4155 return status;
c5641782
CL
4156 /* length */
4157 status = nfsd4_encode_length4(xdr, ld->ld_length);
4158 if (status != nfs_ok)
4159 return status;
4160 /* locktype */
4161 if (xdr_stream_encode_u32(xdr, ld->ld_type) != XDR_UNIT)
4162 return nfserr_resource;
4163 /* owner */
4164 return nfsd4_encode_lock_owner4(xdr, &ld->ld_clientid,
4165 &ld->ld_owner);
1da177e4
LT
4166}
4167
695e12f8 4168static __be32
e78e274e
KC
4169nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr,
4170 union nfsd4_op_u *u)
1da177e4 4171{
e78e274e 4172 struct nfsd4_lock *lock = &u->lock;
bddfdbcd 4173 struct xdr_stream *xdr = resp->xdr;
c5641782 4174 __be32 status;
d0a381dd 4175
c5641782
CL
4176 switch (nfserr) {
4177 case nfs_ok:
4178 /* resok4 */
4179 status = nfsd4_encode_stateid4(xdr, &lock->lk_resp_stateid);
4180 break;
4181 case nfserr_denied:
4182 /* denied */
4183 status = nfsd4_encode_lock4denied(xdr, &lock->lk_denied);
4184 break;
4185 default:
4186 return nfserr;
4187 }
4188 return status != nfs_ok ? status : nfserr;
1da177e4
LT
4189}
4190
695e12f8 4191static __be32
e78e274e
KC
4192nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr,
4193 union nfsd4_op_u *u)
1da177e4 4194{
e78e274e 4195 struct nfsd4_lockt *lockt = &u->lockt;
bddfdbcd 4196 struct xdr_stream *xdr = resp->xdr;
c5641782 4197 __be32 status;
d0a381dd 4198
c5641782
CL
4199 if (nfserr == nfserr_denied) {
4200 /* denied */
4201 status = nfsd4_encode_lock4denied(xdr, &lockt->lt_denied);
4202 if (status != nfs_ok)
4203 return status;
4204 }
695e12f8 4205 return nfserr;
1da177e4
LT
4206}
4207
695e12f8 4208static __be32
e78e274e
KC
4209nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr,
4210 union nfsd4_op_u *u)
1da177e4 4211{
e78e274e 4212 struct nfsd4_locku *locku = &u->locku;
bddfdbcd 4213 struct xdr_stream *xdr = resp->xdr;
d0a381dd 4214
40bb2baa
CL
4215 /* lock_stateid */
4216 return nfsd4_encode_stateid4(xdr, &locku->lu_stateid);
1da177e4
LT
4217}
4218
4219
695e12f8 4220static __be32
e78e274e
KC
4221nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr,
4222 union nfsd4_op_u *u)
1da177e4 4223{
e78e274e 4224 struct nfsd4_link *link = &u->link;
bddfdbcd 4225 struct xdr_stream *xdr = resp->xdr;
1da177e4 4226
66a21db7 4227 return nfsd4_encode_change_info4(xdr, &link->li_cinfo);
1da177e4
LT
4228}
4229
e4ad7ce7
CL
4230/*
4231 * This implementation does not yet support returning an ACE in an
4232 * OPEN that offers a delegation.
4233 */
4234static __be32
4235nfsd4_encode_open_nfsace4(struct xdr_stream *xdr)
4236{
4237 __be32 status;
4238
4239 /* type */
4240 status = nfsd4_encode_acetype4(xdr, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
4241 if (status != nfs_ok)
4242 return nfserr_resource;
4243 /* flag */
4244 status = nfsd4_encode_aceflag4(xdr, 0);
4245 if (status != nfs_ok)
4246 return nfserr_resource;
4247 /* access mask */
4248 status = nfsd4_encode_acemask4(xdr, 0);
4249 if (status != nfs_ok)
4250 return nfserr_resource;
4251 /* who - empty for now */
4252 if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
4253 return nfserr_resource;
4254 return nfs_ok;
4255}
4256
4257static __be32
4258nfsd4_encode_open_read_delegation4(struct xdr_stream *xdr, struct nfsd4_open *open)
4259{
4260 __be32 status;
4261
4262 /* stateid */
4263 status = nfsd4_encode_stateid4(xdr, &open->op_delegate_stateid);
4264 if (status != nfs_ok)
4265 return status;
4266 /* recall */
4267 status = nfsd4_encode_bool(xdr, open->op_recall);
4268 if (status != nfs_ok)
4269 return status;
4270 /* permissions */
4271 return nfsd4_encode_open_nfsace4(xdr);
4272}
1da177e4 4273
32efa674
CL
4274static __be32
4275nfsd4_encode_nfs_space_limit4(struct xdr_stream *xdr, u64 filesize)
4276{
4277 /* limitby */
4278 if (xdr_stream_encode_u32(xdr, NFS4_LIMIT_SIZE) != XDR_UNIT)
4279 return nfserr_resource;
4280 /* filesize */
4281 return nfsd4_encode_uint64_t(xdr, filesize);
4282}
4283
4284static __be32
4285nfsd4_encode_open_write_delegation4(struct xdr_stream *xdr,
4286 struct nfsd4_open *open)
4287{
4288 __be32 status;
4289
4290 /* stateid */
4291 status = nfsd4_encode_stateid4(xdr, &open->op_delegate_stateid);
4292 if (status != nfs_ok)
4293 return status;
4294 /* recall */
4295 status = nfsd4_encode_bool(xdr, open->op_recall);
4296 if (status != nfs_ok)
4297 return status;
4298 /* space_limit */
4299 status = nfsd4_encode_nfs_space_limit4(xdr, 0);
4300 if (status != nfs_ok)
4301 return status;
4302 return nfsd4_encode_open_nfsace4(xdr);
4303}
4304
6dd43c6d
CL
4305static __be32
4306nfsd4_encode_open_none_delegation4(struct xdr_stream *xdr,
4307 struct nfsd4_open *open)
4308{
4309 __be32 status = nfs_ok;
4310
4311 /* ond_why */
4312 if (xdr_stream_encode_u32(xdr, open->op_why_no_deleg) != XDR_UNIT)
4313 return nfserr_resource;
4314 switch (open->op_why_no_deleg) {
4315 case WND4_CONTENTION:
4316 /* ond_server_will_push_deleg */
4317 status = nfsd4_encode_bool(xdr, false);
4318 break;
4319 case WND4_RESOURCE:
4320 /* ond_server_will_signal_avail */
4321 status = nfsd4_encode_bool(xdr, false);
4322 }
4323 return status;
4324}
4325
802e1913
CL
4326static __be32
4327nfsd4_encode_open_delegation4(struct xdr_stream *xdr, struct nfsd4_open *open)
4328{
4329 __be32 status;
4330
4331 /* delegation_type */
4332 if (xdr_stream_encode_u32(xdr, open->op_delegate_type) != XDR_UNIT)
4333 return nfserr_resource;
4334 switch (open->op_delegate_type) {
8dfbea8b 4335 case OPEN_DELEGATE_NONE:
802e1913
CL
4336 status = nfs_ok;
4337 break;
8dfbea8b 4338 case OPEN_DELEGATE_READ:
fbd5573d 4339 case OPEN_DELEGATE_READ_ATTRS_DELEG:
802e1913
CL
4340 /* read */
4341 status = nfsd4_encode_open_read_delegation4(xdr, open);
4342 break;
8dfbea8b 4343 case OPEN_DELEGATE_WRITE:
fbd5573d 4344 case OPEN_DELEGATE_WRITE_ATTRS_DELEG:
802e1913
CL
4345 /* write */
4346 status = nfsd4_encode_open_write_delegation4(xdr, open);
4347 break;
8dfbea8b 4348 case OPEN_DELEGATE_NONE_EXT:
802e1913
CL
4349 /* od_whynone */
4350 status = nfsd4_encode_open_none_delegation4(xdr, open);
4351 break;
4352 default:
4353 status = nfserr_serverfault;
4354 }
4355
4356 return status;
4357}
4358
695e12f8 4359static __be32
e78e274e
KC
4360nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr,
4361 union nfsd4_op_u *u)
1da177e4 4362{
e78e274e 4363 struct nfsd4_open *open = &u->open;
bddfdbcd 4364 struct xdr_stream *xdr = resp->xdr;
1da177e4 4365
841735b3 4366 /* stateid */
40bb2baa 4367 nfserr = nfsd4_encode_stateid4(xdr, &open->op_stateid);
841735b3 4368 if (nfserr != nfs_ok)
bac966d6 4369 return nfserr;
841735b3 4370 /* cinfo */
66a21db7 4371 nfserr = nfsd4_encode_change_info4(xdr, &open->op_cinfo);
841735b3 4372 if (nfserr != nfs_ok)
66a21db7 4373 return nfserr;
841735b3
CL
4374 /* rflags */
4375 nfserr = nfsd4_encode_uint32_t(xdr, open->op_rflags);
4376 if (nfserr != nfs_ok)
4377 return nfserr;
4378 /* attrset */
e64301f5
CL
4379 nfserr = nfsd4_encode_bitmap4(xdr, open->op_bmval[0],
4380 open->op_bmval[1], open->op_bmval[2]);
841735b3 4381 if (nfserr != nfs_ok)
bac966d6 4382 return nfserr;
802e1913
CL
4383 /* delegation */
4384 return nfsd4_encode_open_delegation4(xdr, open);
1da177e4
LT
4385}
4386
695e12f8 4387static __be32
e78e274e
KC
4388nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr,
4389 union nfsd4_op_u *u)
1da177e4 4390{
e78e274e 4391 struct nfsd4_open_confirm *oc = &u->open_confirm;
bddfdbcd 4392 struct xdr_stream *xdr = resp->xdr;
d0a381dd 4393
40bb2baa
CL
4394 /* open_stateid */
4395 return nfsd4_encode_stateid4(xdr, &oc->oc_resp_stateid);
1da177e4
LT
4396}
4397
695e12f8 4398static __be32
e78e274e
KC
4399nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr,
4400 union nfsd4_op_u *u)
1da177e4 4401{
e78e274e 4402 struct nfsd4_open_downgrade *od = &u->open_downgrade;
bddfdbcd 4403 struct xdr_stream *xdr = resp->xdr;
d0a381dd 4404
40bb2baa
CL
4405 /* open_stateid */
4406 return nfsd4_encode_stateid4(xdr, &od->od_stateid);
1da177e4
LT
4407}
4408
ba21e20b
CL
4409/*
4410 * The operation of this function assumes that this is the only
4411 * READ operation in the COMPOUND. If there are multiple READs,
4412 * we use nfsd4_encode_readv().
4413 */
dc97618d
BF
4414static __be32 nfsd4_encode_splice_read(
4415 struct nfsd4_compoundres *resp,
4416 struct nfsd4_read *read,
4417 struct file *file, unsigned long maxcount)
1da177e4 4418{
bddfdbcd 4419 struct xdr_stream *xdr = resp->xdr;
34a78b48 4420 struct xdr_buf *buf = xdr->buf;
76e5492b 4421 int status, space_left;
dc97618d 4422 __be32 nfserr;
1da177e4 4423
1a861150
CL
4424 /*
4425 * Splice read doesn't work if encoding has already wandered
4426 * into the XDR buf's page array.
4427 */
4428 if (unlikely(xdr->buf->page_len)) {
4429 WARN_ON_ONCE(1);
4430 return nfserr_serverfault;
4431 }
4432
ba21e20b
CL
4433 /*
4434 * Make sure there is room at the end of buf->head for
4435 * svcxdr_encode_opaque_pages() to create a tail buffer
4436 * to XDR-pad the payload.
4437 */
4438 if (xdr->iov != xdr->buf->head || xdr->end - xdr->p < 1)
d0a381dd 4439 return nfserr_resource;
dc97618d 4440
87c5942e 4441 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
24c7fb85
CL
4442 file, read->rd_offset, &maxcount,
4443 &read->rd_eof);
87c5942e 4444 read->rd_length = maxcount;
76e5492b
CL
4445 if (nfserr)
4446 goto out_err;
ba21e20b
CL
4447 svcxdr_encode_opaque_pages(read->rd_rqstp, xdr, buf->pages,
4448 buf->page_base, maxcount);
76e5492b
CL
4449 status = svc_encode_result_payload(read->rd_rqstp,
4450 buf->head[0].iov_len, maxcount);
4451 if (status) {
4452 nfserr = nfserrno(status);
4453 goto out_err;
b0e35fda 4454 }
1da177e4 4455
ba21e20b
CL
4456 /*
4457 * Prepare to encode subsequent operations.
4458 *
4459 * xdr_truncate_encode() is not safe to use after a successful
4460 * splice read has been done, so the following stream
4461 * manipulations are open-coded.
4462 */
dc97618d 4463 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
34a78b48
BF
4464 buf->buflen - buf->len);
4465 buf->buflen = buf->len + space_left;
dc97618d
BF
4466 xdr->end = (__be32 *)((void *)xdr->end + space_left);
4467
ba21e20b 4468 return nfs_ok;
76e5492b
CL
4469
4470out_err:
4471 /*
4472 * nfsd_splice_actor may have already messed with the
4473 * page length; reset it so as not to confuse
4474 * xdr_truncate_encode in our caller.
4475 */
4476 buf->page_len = 0;
4477 return nfserr;
dc97618d
BF
4478}
4479
4480static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
4481 struct nfsd4_read *read,
4482 struct file *file, unsigned long maxcount)
4483{
bddfdbcd 4484 struct xdr_stream *xdr = resp->xdr;
0d32a6bb 4485 unsigned int base = xdr->buf->page_len & ~PAGE_MASK;
071ae99f 4486 unsigned int starting_len = xdr->buf->len;
5e64d85c 4487 __be32 zero = xdr_zero;
dc97618d 4488 __be32 nfserr;
1da177e4 4489
703d7521 4490 if (xdr_reserve_space_vec(xdr, maxcount) < 0)
403217f3 4491 return nfserr_resource;
1da177e4 4492
703d7521 4493 nfserr = nfsd_iter_read(resp->rqstp, read->rd_fhp, file,
0d32a6bb 4494 read->rd_offset, &maxcount, base,
703d7521 4495 &read->rd_eof);
87c5942e 4496 read->rd_length = maxcount;
dc97618d 4497 if (nfserr)
1da177e4 4498 return nfserr;
071ae99f 4499 if (svc_encode_result_payload(resp->rqstp, starting_len, maxcount))
41205539 4500 return nfserr_io;
071ae99f 4501 xdr_truncate_encode(xdr, starting_len + xdr_align_size(maxcount));
dc97618d 4502
5e64d85c
CL
4503 write_bytes_to_xdr_buf(xdr->buf, starting_len + maxcount, &zero,
4504 xdr_pad_size(maxcount));
4505 return nfs_ok;
dc97618d
BF
4506}
4507
4508static __be32
4509nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 4510 union nfsd4_op_u *u)
dc97618d 4511{
a2c91753 4512 struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
e78e274e 4513 struct nfsd4_read *read = &u->read;
bddfdbcd 4514 struct xdr_stream *xdr = resp->xdr;
a2c91753 4515 bool splice_ok = argp->splice_ok;
1a861150 4516 unsigned int eof_offset;
a2c91753 4517 unsigned long maxcount;
1a861150 4518 __be32 wire_data[2];
a2c91753 4519 struct file *file;
dc97618d 4520
5c4583b2
JL
4521 if (nfserr)
4522 return nfserr;
1a861150
CL
4523
4524 eof_offset = xdr->buf->len;
5c4583b2
JL
4525 file = read->rd_nf->nf_file;
4526
1a861150
CL
4527 /* Reserve space for the eof flag and byte count */
4528 if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2))) {
c738b218 4529 WARN_ON_ONCE(splice_ok);
bac966d6 4530 return nfserr_resource;
dc97618d 4531 }
dc97618d
BF
4532 xdr_commit_encode(xdr);
4533
0cb4d23a 4534 maxcount = min_t(unsigned long, read->rd_length,
68e8bb03 4535 (xdr->buf->buflen - xdr->buf->len));
dc97618d 4536
c738b218 4537 if (file->f_op->splice_read && splice_ok)
96bcad50 4538 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
dc97618d 4539 else
96bcad50 4540 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
28d5bc46 4541 if (nfserr) {
1a861150 4542 xdr_truncate_encode(xdr, eof_offset);
28d5bc46
CL
4543 return nfserr;
4544 }
96bcad50 4545
1a861150
CL
4546 wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
4547 wire_data[1] = cpu_to_be32(read->rd_length);
4548 write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
28d5bc46 4549 return nfs_ok;
1da177e4
LT
4550}
4551
b37ad28b 4552static __be32
e78e274e
KC
4553nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr,
4554 union nfsd4_op_u *u)
1da177e4 4555{
e78e274e 4556 struct nfsd4_readlink *readlink = &u->readlink;
201cb204 4557 __be32 *p, wire_count, zero = xdr_zero;
bddfdbcd 4558 struct xdr_stream *xdr = resp->xdr;
201cb204 4559 unsigned int length_offset;
99b002a1 4560 int maxcount, status;
1da177e4 4561
201cb204
CL
4562 /* linktext4.count */
4563 length_offset = xdr->buf->len;
4564 if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
2825a7f9 4565 return nfserr_resource;
d0a381dd 4566
201cb204
CL
4567 /* linktext4.data */
4568 maxcount = PAGE_SIZE;
476a7b1f
BF
4569 p = xdr_reserve_space(xdr, maxcount);
4570 if (!p)
4e21ac4b 4571 return nfserr_resource;
476a7b1f
BF
4572 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4573 (char *)p, &maxcount);
1da177e4 4574 if (nfserr == nfserr_isdir)
d3f627c8 4575 nfserr = nfserr_inval;
76e5492b
CL
4576 if (nfserr)
4577 goto out_err;
4578 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4579 maxcount);
4580 if (status) {
4581 nfserr = nfserrno(status);
4582 goto out_err;
d3f627c8 4583 }
201cb204
CL
4584
4585 wire_count = cpu_to_be32(maxcount);
4586 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, XDR_UNIT);
99b002a1
CL
4587 xdr_truncate_encode(xdr, length_offset + 4 + xdr_align_size(maxcount));
4588 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount, &zero,
4589 xdr_pad_size(maxcount));
4590 return nfs_ok;
76e5492b
CL
4591
4592out_err:
4593 xdr_truncate_encode(xdr, length_offset);
4594 return nfserr;
1da177e4
LT
4595}
4596
25c307ac
CL
4597static __be32 nfsd4_encode_dirlist4(struct xdr_stream *xdr,
4598 struct nfsd4_readdir *readdir,
4599 u32 max_payload)
1da177e4 4600{
25c307ac 4601 int bytes_left, maxcount, starting_len = xdr->buf->len;
1da177e4 4602 loff_t offset;
25c307ac 4603 __be32 status;
1da177e4
LT
4604
4605 /*
561f0ed4 4606 * Number of bytes left for directory entries allowing for the
25c307ac 4607 * final 8 bytes of the readdir and a following failed op.
561f0ed4 4608 */
25c307ac
CL
4609 bytes_left = xdr->buf->buflen - xdr->buf->len -
4610 COMPOUND_ERR_SLACK_SPACE - XDR_UNIT * 2;
4611 if (bytes_left < 0)
4612 return nfserr_resource;
4613 maxcount = min_t(u32, readdir->rd_maxcount, max_payload);
4614
561f0ed4 4615 /*
25c307ac
CL
4616 * The RFC defines rd_maxcount as the size of the
4617 * READDIR4resok structure, which includes the verifier
4618 * and the 8 bytes encoded at the end of this function.
1da177e4 4619 */
25c307ac
CL
4620 if (maxcount < XDR_UNIT * 4)
4621 return nfserr_toosmall;
4622 maxcount = min_t(int, maxcount - XDR_UNIT * 4, bytes_left);
1da177e4 4623
25c307ac 4624 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0 */
aee37764 4625 if (!readdir->rd_dircount)
25c307ac 4626 readdir->rd_dircount = max_payload;
aee37764 4627
25c307ac 4628 /* *entries */
561f0ed4
BF
4629 readdir->xdr = xdr;
4630 readdir->rd_maxcount = maxcount;
1da177e4 4631 readdir->common.err = 0;
561f0ed4 4632 readdir->cookie_offset = 0;
1da177e4 4633 offset = readdir->rd_cookie;
25c307ac 4634 status = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp, &offset,
a0f3c835 4635 &readdir->common, nfsd4_encode_entry4);
25c307ac
CL
4636 if (status)
4637 return status;
4638 if (readdir->common.err == nfserr_toosmall &&
4639 xdr->buf->len == starting_len) {
4640 /* No entries were encoded. Which limit did we hit? */
4641 if (maxcount - XDR_UNIT * 4 < bytes_left)
4642 /* It was the fault of rd_maxcount */
4643 return nfserr_toosmall;
4644 /* We ran out of buffer space */
4645 return nfserr_resource;
561f0ed4 4646 }
3fc5048c
CL
4647 /* Encode the final entry's cookie value */
4648 nfsd4_encode_entry4_nfs_cookie4(readdir, offset);
25c307ac
CL
4649 /* No entries follow */
4650 if (xdr_stream_encode_item_absent(xdr) != XDR_UNIT)
4651 return nfserr_resource;
1da177e4 4652
25c307ac
CL
4653 /* eof */
4654 return nfsd4_encode_bool(xdr, readdir->common.err == nfserr_eof);
4655}
1da177e4 4656
25c307ac
CL
4657static __be32
4658nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr,
4659 union nfsd4_op_u *u)
4660{
4661 struct nfsd4_readdir *readdir = &u->readdir;
4662 struct xdr_stream *xdr = resp->xdr;
4663 int starting_len = xdr->buf->len;
4664
4665 /* cookieverf */
4666 nfserr = nfsd4_encode_verifier4(xdr, &readdir->rd_verf);
4667 if (nfserr != nfs_ok)
4668 return nfserr;
4669
4670 /* reply */
4671 nfserr = nfsd4_encode_dirlist4(xdr, readdir, svc_max_payload(resp->rqstp));
4672 if (nfserr != nfs_ok)
4673 xdr_truncate_encode(xdr, starting_len);
1da177e4
LT
4674 return nfserr;
4675}
4676
695e12f8 4677static __be32
e78e274e
KC
4678nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr,
4679 union nfsd4_op_u *u)
1da177e4 4680{
e78e274e 4681 struct nfsd4_remove *remove = &u->remove;
bddfdbcd 4682 struct xdr_stream *xdr = resp->xdr;
1da177e4 4683
66a21db7 4684 return nfsd4_encode_change_info4(xdr, &remove->rm_cinfo);
1da177e4
LT
4685}
4686
695e12f8 4687static __be32
e78e274e
KC
4688nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr,
4689 union nfsd4_op_u *u)
1da177e4 4690{
e78e274e 4691 struct nfsd4_rename *rename = &u->rename;
bddfdbcd 4692 struct xdr_stream *xdr = resp->xdr;
1da177e4 4693
66a21db7
CL
4694 nfserr = nfsd4_encode_change_info4(xdr, &rename->rn_sinfo);
4695 if (nfserr)
4696 return nfserr;
4697 return nfsd4_encode_change_info4(xdr, &rename->rn_tinfo);
1da177e4
LT
4698}
4699
91c7a905
CL
4700static __be32
4701nfsd4_encode_rpcsec_gss_info(struct xdr_stream *xdr,
4702 struct rpcsec_gss_info *info)
4703{
4704 __be32 status;
4705
4706 /* oid */
4707 if (xdr_stream_encode_opaque(xdr, info->oid.data, info->oid.len) < 0)
4708 return nfserr_resource;
4709 /* qop */
4710 status = nfsd4_encode_qop4(xdr, info->qop);
4711 if (status != nfs_ok)
4712 return status;
4713 /* service */
4714 if (xdr_stream_encode_u32(xdr, info->service) != XDR_UNIT)
4715 return nfserr_resource;
4716
4717 return nfs_ok;
4718}
4719
695e12f8 4720static __be32
825562bc
CL
4721nfsd4_encode_secinfo4(struct xdr_stream *xdr, rpc_authflavor_t pf,
4722 u32 *supported)
4723{
4724 struct rpcsec_gss_info info;
4725 __be32 status;
4726
4727 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4728 (*supported)++;
4729
4730 /* flavor */
4731 status = nfsd4_encode_uint32_t(xdr, RPC_AUTH_GSS);
4732 if (status != nfs_ok)
4733 return status;
4734 /* flavor_info */
4735 status = nfsd4_encode_rpcsec_gss_info(xdr, &info);
4736 if (status != nfs_ok)
4737 return status;
4738 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4739 (*supported)++;
4740
4741 /* flavor */
4742 status = nfsd4_encode_uint32_t(xdr, pf);
4743 if (status != nfs_ok)
4744 return status;
4745 }
4746 return nfs_ok;
4747}
4748
695e12f8 4749static __be32
b786caa6 4750nfsd4_encode_SECINFO4resok(struct xdr_stream *xdr, struct svc_export *exp)
dcb488a3 4751{
676e4ebd 4752 u32 i, nflavs, supported;
4796f457
BF
4753 struct exp_flavor_info *flavs;
4754 struct exp_flavor_info def_flavs[2];
b786caa6
CL
4755 unsigned int count_offset;
4756 __be32 status, wire_count;
dcb488a3 4757
4796f457
BF
4758 if (exp->ex_nflavors) {
4759 flavs = exp->ex_flavors;
4760 nflavs = exp->ex_nflavors;
4761 } else { /* Handling of some defaults in absence of real secinfo: */
4762 flavs = def_flavs;
4763 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4764 nflavs = 2;
4765 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4766 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4767 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4768 nflavs = 1;
4769 flavs[0].pseudoflavor
4770 = svcauth_gss_flavor(exp->ex_client);
4771 } else {
4772 nflavs = 1;
4773 flavs[0].pseudoflavor
4774 = exp->ex_client->flavour->flavour;
4775 }
4776 }
4777
b786caa6
CL
4778 count_offset = xdr->buf->len;
4779 if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
bac966d6 4780 return nfserr_resource;
676e4ebd 4781
825562bc
CL
4782 for (i = 0, supported = 0; i < nflavs; i++) {
4783 status = nfsd4_encode_secinfo4(xdr, flavs[i].pseudoflavor,
4784 &supported);
4785 if (status != nfs_ok)
4786 return status;
dcb488a3 4787 }
a77c806f 4788
b786caa6
CL
4789 wire_count = cpu_to_be32(supported);
4790 write_bytes_to_xdr_buf(xdr->buf, count_offset, &wire_count,
4791 XDR_UNIT);
bac966d6 4792 return 0;
dcb488a3
AA
4793}
4794
22b6dee8
MJ
4795static __be32
4796nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 4797 union nfsd4_op_u *u)
22b6dee8 4798{
e78e274e 4799 struct nfsd4_secinfo *secinfo = &u->secinfo;
bddfdbcd 4800 struct xdr_stream *xdr = resp->xdr;
d0a381dd 4801
b786caa6 4802 return nfsd4_encode_SECINFO4resok(xdr, secinfo->si_exp);
22b6dee8
MJ
4803}
4804
4805static __be32
4806nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 4807 union nfsd4_op_u *u)
22b6dee8 4808{
e78e274e 4809 struct nfsd4_secinfo_no_name *secinfo = &u->secinfo_no_name;
bddfdbcd 4810 struct xdr_stream *xdr = resp->xdr;
d0a381dd 4811
b786caa6 4812 return nfsd4_encode_SECINFO4resok(xdr, secinfo->sin_exp);
22b6dee8
MJ
4813}
4814
695e12f8 4815static __be32
e78e274e
KC
4816nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4817 union nfsd4_op_u *u)
1da177e4 4818{
e78e274e 4819 struct nfsd4_setattr *setattr = &u->setattr;
c3dcb45b 4820 __be32 status;
1da177e4 4821
c3dcb45b
CL
4822 switch (nfserr) {
4823 case nfs_ok:
4824 /* attrsset */
4825 status = nfsd4_encode_bitmap4(resp->xdr, setattr->sa_bmval[0],
4826 setattr->sa_bmval[1],
4827 setattr->sa_bmval[2]);
4828 break;
4829 default:
4830 /* attrsset */
4831 status = nfsd4_encode_bitmap4(resp->xdr, 0, 0, 0);
1da177e4 4832 }
c3dcb45b 4833 return status != nfs_ok ? status : nfserr;
1da177e4
LT
4834}
4835
695e12f8 4836static __be32
e78e274e
KC
4837nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr,
4838 union nfsd4_op_u *u)
1da177e4 4839{
e78e274e 4840 struct nfsd4_setclientid *scd = &u->setclientid;
bddfdbcd 4841 struct xdr_stream *xdr = resp->xdr;
1da177e4
LT
4842
4843 if (!nfserr) {
adaa7a50
CL
4844 nfserr = nfsd4_encode_clientid4(xdr, &scd->se_clientid);
4845 if (nfserr != nfs_ok)
4846 goto out;
4847 nfserr = nfsd4_encode_verifier4(xdr, &scd->se_confirm);
4848 } else if (nfserr == nfserr_clid_inuse) {
4849 /* empty network id */
4850 if (xdr_stream_encode_u32(xdr, 0) < 0) {
4851 nfserr = nfserr_resource;
4852 goto out;
4853 }
4854 /* empty universal address */
4855 if (xdr_stream_encode_u32(xdr, 0) < 0) {
4856 nfserr = nfserr_resource;
4857 goto out;
4858 }
1da177e4 4859 }
adaa7a50 4860out:
695e12f8 4861 return nfserr;
1da177e4
LT
4862}
4863
695e12f8 4864static __be32
e78e274e
KC
4865nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr,
4866 union nfsd4_op_u *u)
1da177e4 4867{
e78e274e 4868 struct nfsd4_write *write = &u->write;
76bebcc7 4869 struct xdr_stream *xdr = resp->xdr;
1da177e4 4870
76bebcc7
CL
4871 /* count */
4872 nfserr = nfsd4_encode_count4(xdr, write->wr_bytes_written);
4873 if (nfserr)
4874 return nfserr;
4875 /* committed */
4876 if (xdr_stream_encode_u32(xdr, write->wr_how_written) != XDR_UNIT)
adaa7a50 4877 return nfserr_resource;
76bebcc7
CL
4878 /* writeverf */
4879 return nfsd4_encode_verifier4(xdr, &write->wr_verifier);
1da177e4
LT
4880}
4881
2db134eb 4882static __be32
abef972c
CL
4883nfsd4_encode_state_protect_ops4(struct xdr_stream *xdr,
4884 struct nfsd4_exchange_id *exid)
2db134eb 4885{
abef972c 4886 __be32 status;
0733d213 4887
abef972c
CL
4888 /* spo_must_enforce */
4889 status = nfsd4_encode_bitmap4(xdr, exid->spo_must_enforce[0],
4890 exid->spo_must_enforce[1],
4891 exid->spo_must_enforce[2]);
4892 if (status != nfs_ok)
4893 return status;
4894 /* spo_must_allow */
4895 return nfsd4_encode_bitmap4(xdr, exid->spo_must_allow[0],
4896 exid->spo_must_allow[1],
4897 exid->spo_must_allow[2]);
4898}
0733d213 4899
abef972c
CL
4900static __be32
4901nfsd4_encode_state_protect4_r(struct xdr_stream *xdr, struct nfsd4_exchange_id *exid)
4902{
4903 __be32 status;
0733d213 4904
abef972c 4905 if (xdr_stream_encode_u32(xdr, exid->spa_how) != XDR_UNIT)
adaa7a50 4906 return nfserr_resource;
57266a6e
BF
4907 switch (exid->spa_how) {
4908 case SP4_NONE:
abef972c 4909 status = nfs_ok;
57266a6e
BF
4910 break;
4911 case SP4_MACH_CRED:
abef972c
CL
4912 /* spr_mach_ops */
4913 status = nfsd4_encode_state_protect_ops4(xdr, exid);
57266a6e
BF
4914 break;
4915 default:
abef972c 4916 status = nfserr_serverfault;
57266a6e 4917 }
abef972c
CL
4918 return status;
4919}
0733d213 4920
abef972c
CL
4921static __be32
4922nfsd4_encode_server_owner4(struct xdr_stream *xdr, struct svc_rqst *rqstp)
4923{
4924 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4925 __be32 status;
a8bb84bc 4926
abef972c
CL
4927 /* so_minor_id */
4928 status = nfsd4_encode_uint64_t(xdr, 0);
4929 if (status != nfs_ok)
4930 return status;
4931 /* so_major_id */
4932 return nfsd4_encode_opaque(xdr, nn->nfsd_name, strlen(nn->nfsd_name));
4933}
0733d213 4934
60002092
PR
4935static __be32
4936nfsd4_encode_nfs_impl_id4(struct xdr_stream *xdr, struct nfsd4_exchange_id *exid)
4937{
4938 __be32 status;
4939
4940 /* nii_domain */
4941 status = nfsd4_encode_opaque(xdr, exid->nii_domain.data,
4942 exid->nii_domain.len);
4943 if (status != nfs_ok)
4944 return status;
4945 /* nii_name */
4946 status = nfsd4_encode_opaque(xdr, exid->nii_name.data,
4947 exid->nii_name.len);
4948 if (status != nfs_ok)
4949 return status;
4950 /* nii_time */
4951 return nfsd4_encode_nfstime4(xdr, &exid->nii_time);
4952}
4953
abef972c
CL
4954static __be32
4955nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
4956 union nfsd4_op_u *u)
4957{
4958 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
4959 struct nfsd4_exchange_id *exid = &u->exchange_id;
4960 struct xdr_stream *xdr = resp->xdr;
0733d213 4961
abef972c
CL
4962 /* eir_clientid */
4963 nfserr = nfsd4_encode_clientid4(xdr, &exid->clientid);
4964 if (nfserr != nfs_ok)
4965 return nfserr;
4966 /* eir_sequenceid */
4967 nfserr = nfsd4_encode_sequenceid4(xdr, exid->seqid);
4968 if (nfserr != nfs_ok)
4969 return nfserr;
4970 /* eir_flags */
4971 nfserr = nfsd4_encode_uint32_t(xdr, exid->flags);
4972 if (nfserr != nfs_ok)
4973 return nfserr;
4974 /* eir_state_protect */
4975 nfserr = nfsd4_encode_state_protect4_r(xdr, exid);
4976 if (nfserr != nfs_ok)
4977 return nfserr;
4978 /* eir_server_owner */
4979 nfserr = nfsd4_encode_server_owner4(xdr, resp->rqstp);
4980 if (nfserr != nfs_ok)
4981 return nfserr;
4982 /* eir_server_scope */
4983 nfserr = nfsd4_encode_opaque(xdr, nn->nfsd_name,
4984 strlen(nn->nfsd_name));
4985 if (nfserr != nfs_ok)
4986 return nfserr;
4987 /* eir_server_impl_id<1> */
60002092 4988 if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
abef972c 4989 return nfserr_resource;
60002092
PR
4990 nfserr = nfsd4_encode_nfs_impl_id4(xdr, exid);
4991 if (nfserr != nfs_ok)
4992 return nfserr;
abef972c
CL
4993
4994 return nfs_ok;
2db134eb
AA
4995}
4996
150990f4
CL
4997static __be32
4998nfsd4_encode_channel_attrs4(struct xdr_stream *xdr,
4999 const struct nfsd4_channel_attrs *attrs)
5000{
5001 __be32 status;
5002
5003 /* ca_headerpadsize */
5004 status = nfsd4_encode_count4(xdr, 0);
5005 if (status != nfs_ok)
5006 return status;
5007 /* ca_maxrequestsize */
5008 status = nfsd4_encode_count4(xdr, attrs->maxreq_sz);
5009 if (status != nfs_ok)
5010 return status;
5011 /* ca_maxresponsesize */
5012 status = nfsd4_encode_count4(xdr, attrs->maxresp_sz);
5013 if (status != nfs_ok)
5014 return status;
5015 /* ca_maxresponsesize_cached */
5016 status = nfsd4_encode_count4(xdr, attrs->maxresp_cached);
5017 if (status != nfs_ok)
5018 return status;
5019 /* ca_maxoperations */
5020 status = nfsd4_encode_count4(xdr, attrs->maxops);
5021 if (status != nfs_ok)
5022 return status;
5023 /* ca_maxrequests */
5024 status = nfsd4_encode_count4(xdr, attrs->maxreqs);
5025 if (status != nfs_ok)
5026 return status;
5027 /* ca_rdma_ird<1> */
5028 if (xdr_stream_encode_u32(xdr, attrs->nr_rdma_attrs) != XDR_UNIT)
5029 return nfserr_resource;
5030 if (attrs->nr_rdma_attrs)
5031 return nfsd4_encode_uint32_t(xdr, attrs->rdma_attrs);
5032 return nfs_ok;
5033}
5034
2db134eb 5035static __be32
57b7b43b 5036nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5037 union nfsd4_op_u *u)
2db134eb 5038{
e78e274e 5039 struct nfsd4_create_session *sess = &u->create_session;
bddfdbcd 5040 struct xdr_stream *xdr = resp->xdr;
ec6b5d7b 5041
b0c1b1ba
CL
5042 /* csr_sessionid */
5043 nfserr = nfsd4_encode_sessionid4(xdr, &sess->sessionid);
5044 if (nfserr != nfs_ok)
5045 return nfserr;
5046 /* csr_sequence */
5047 nfserr = nfsd4_encode_sequenceid4(xdr, sess->seqid);
5048 if (nfserr != nfs_ok)
5049 return nfserr;
5050 /* csr_flags */
5051 nfserr = nfsd4_encode_uint32_t(xdr, sess->flags);
5052 if (nfserr != nfs_ok)
5053 return nfserr;
150990f4
CL
5054 /* csr_fore_chan_attrs */
5055 nfserr = nfsd4_encode_channel_attrs4(xdr, &sess->fore_channel);
5056 if (nfserr != nfs_ok)
5057 return nfserr;
5058 /* csr_back_chan_attrs */
5059 return nfsd4_encode_channel_attrs4(xdr, &sess->back_channel);
2db134eb
AA
5060}
5061
c47d832b 5062static __be32
57b7b43b 5063nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5064 union nfsd4_op_u *u)
2db134eb 5065{
e78e274e 5066 struct nfsd4_sequence *seq = &u->sequence;
bddfdbcd 5067 struct xdr_stream *xdr = resp->xdr;
b85d4c01 5068
6621b88b
CL
5069 /* sr_sessionid */
5070 nfserr = nfsd4_encode_sessionid4(xdr, &seq->sessionid);
5071 if (nfserr != nfs_ok)
5072 return nfserr;
5073 /* sr_sequenceid */
5074 nfserr = nfsd4_encode_sequenceid4(xdr, seq->seqid);
5075 if (nfserr != nfs_ok)
5076 return nfserr;
5077 /* sr_slotid */
5078 nfserr = nfsd4_encode_slotid4(xdr, seq->slotid);
5079 if (nfserr != nfs_ok)
5080 return nfserr;
b7d7ca35 5081 /* Note slotid's are numbered from zero: */
6621b88b
CL
5082 /* sr_highest_slotid */
5083 nfserr = nfsd4_encode_slotid4(xdr, seq->maxslots - 1);
5084 if (nfserr != nfs_ok)
5085 return nfserr;
5086 /* sr_target_highest_slotid */
fc8738c6 5087 nfserr = nfsd4_encode_slotid4(xdr, seq->target_maxslots - 1);
6621b88b
CL
5088 if (nfserr != nfs_ok)
5089 return nfserr;
5090 /* sr_status_flags */
5091 nfserr = nfsd4_encode_uint32_t(xdr, seq->status_flags);
5092 if (nfserr != nfs_ok)
5093 return nfserr;
b85d4c01 5094
f5236013 5095 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
6621b88b 5096 return nfs_ok;
2db134eb
AA
5097}
5098
2355c596 5099static __be32
57b7b43b 5100nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5101 union nfsd4_op_u *u)
17456804 5102{
e78e274e 5103 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
03cfb420 5104 struct nfsd4_test_stateid_id *stateid, *next;
08b4436a 5105 struct xdr_stream *xdr = resp->xdr;
17456804 5106
08b4436a
CL
5107 /* tsr_status_codes<> */
5108 if (xdr_stream_encode_u32(xdr, test_stateid->ts_num_ids) != XDR_UNIT)
d0a381dd 5109 return nfserr_resource;
08b4436a
CL
5110 list_for_each_entry_safe(stateid, next,
5111 &test_stateid->ts_stateid_list, ts_id_list) {
5112 if (xdr_stream_encode_be32(xdr, stateid->ts_id_status) != XDR_UNIT)
5113 return nfserr_resource;
17456804 5114 }
08b4436a 5115 return nfs_ok;
17456804
BS
5116}
5117
33a1e6ea
JL
5118static __be32
5119nfsd4_encode_get_dir_delegation(struct nfsd4_compoundres *resp, __be32 nfserr,
5120 union nfsd4_op_u *u)
5121{
5122 struct nfsd4_get_dir_delegation *gdd = &u->get_dir_delegation;
5123 struct xdr_stream *xdr = resp->xdr;
5124 __be32 status = nfserr_resource;
5125
5126 switch(gdd->gddrnf_status) {
5127 case GDD4_OK:
5128 if (xdr_stream_encode_u32(xdr, GDD4_OK) != XDR_UNIT)
5129 break;
5130 status = nfsd4_encode_verifier4(xdr, &gdd->gddr_cookieverf);
5131 if (status)
5132 break;
5133 status = nfsd4_encode_stateid4(xdr, &gdd->gddr_stateid);
5134 if (status)
5135 break;
5136 status = nfsd4_encode_bitmap4(xdr, gdd->gddr_notification[0], 0, 0);
5137 if (status)
5138 break;
5139 status = nfsd4_encode_bitmap4(xdr, gdd->gddr_child_attributes[0],
5140 gdd->gddr_child_attributes[1],
5141 gdd->gddr_child_attributes[2]);
5142 if (status)
5143 break;
5144 status = nfsd4_encode_bitmap4(xdr, gdd->gddr_dir_attributes[0],
5145 gdd->gddr_dir_attributes[1],
5146 gdd->gddr_dir_attributes[2]);
5147 break;
5148 default:
5149 pr_warn("nfsd: bad gddrnf_status (%u)\n", gdd->gddrnf_status);
5150 gdd->gddrnf_will_signal_deleg_avail = 0;
5151 fallthrough;
5152 case GDD4_UNAVAIL:
5153 if (xdr_stream_encode_u32(xdr, GDD4_UNAVAIL) != XDR_UNIT)
5154 break;
5155 status = nfsd4_encode_bool(xdr, gdd->gddrnf_will_signal_deleg_avail);
5156 break;
5157 }
5158 return status;
5159}
5160
9cf514cc
CH
5161#ifdef CONFIG_NFSD_PNFS
5162static __be32
4bbe42e8
CL
5163nfsd4_encode_device_addr4(struct xdr_stream *xdr,
5164 const struct nfsd4_getdeviceinfo *gdev)
9cf514cc 5165{
4bbe42e8 5166 u32 needed_len, starting_len = xdr->buf->len;
f961e3f2 5167 const struct nfsd4_layout_ops *ops;
4bbe42e8 5168 __be32 status;
9cf514cc 5169
4bbe42e8
CL
5170 /* da_layout_type */
5171 if (xdr_stream_encode_u32(xdr, gdev->gd_layout_type) != XDR_UNIT)
bac966d6 5172 return nfserr_resource;
4bbe42e8 5173 /* da_addr_body */
6372e2ee 5174 ops = nfsd4_layout_ops[gdev->gd_layout_type];
4bbe42e8
CL
5175 status = ops->encode_getdeviceinfo(xdr, gdev);
5176 if (status != nfs_ok) {
6372e2ee 5177 /*
4bbe42e8
CL
5178 * Don't burden the layout drivers with enforcing
5179 * gd_maxcount. Just tell the client to come back
5180 * with a bigger buffer if it's not enough.
6372e2ee 5181 */
4bbe42e8 5182 if (xdr->buf->len + XDR_UNIT > gdev->gd_maxcount)
6372e2ee 5183 goto toosmall;
4bbe42e8 5184 return status;
9cf514cc
CH
5185 }
5186
4bbe42e8 5187 return nfs_ok;
9cf514cc 5188
9cf514cc 5189toosmall:
4bbe42e8 5190 needed_len = xdr->buf->len + XDR_UNIT; /* notifications */
9cf514cc 5191 xdr_truncate_encode(xdr, starting_len);
4bbe42e8
CL
5192
5193 status = nfsd4_encode_count4(xdr, needed_len);
5194 if (status != nfs_ok)
5195 return status;
bac966d6 5196 return nfserr_toosmall;
9cf514cc
CH
5197}
5198
4bbe42e8
CL
5199static __be32
5200nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
5201 union nfsd4_op_u *u)
5202{
5203 struct nfsd4_getdeviceinfo *gdev = &u->getdeviceinfo;
5204 struct xdr_stream *xdr = resp->xdr;
5205
5206 /* gdir_device_addr */
5207 nfserr = nfsd4_encode_device_addr4(xdr, gdev);
5208 if (nfserr)
5209 return nfserr;
5210 /* gdir_notification */
5211 return nfsd4_encode_bitmap4(xdr, gdev->gd_notify_types, 0, 0);
5212}
5213
69f5f019
CL
5214static __be32
5215nfsd4_encode_layout4(struct xdr_stream *xdr, const struct nfsd4_layoutget *lgp)
5216{
5217 const struct nfsd4_layout_ops *ops = nfsd4_layout_ops[lgp->lg_layout_type];
5218 __be32 status;
5219
5220 /* lo_offset */
5221 status = nfsd4_encode_offset4(xdr, lgp->lg_seg.offset);
5222 if (status != nfs_ok)
5223 return status;
5224 /* lo_length */
5225 status = nfsd4_encode_length4(xdr, lgp->lg_seg.length);
5226 if (status != nfs_ok)
5227 return status;
5228 /* lo_iomode */
5229 if (xdr_stream_encode_u32(xdr, lgp->lg_seg.iomode) != XDR_UNIT)
5230 return nfserr_resource;
5231 /* lo_content */
5232 if (xdr_stream_encode_u32(xdr, lgp->lg_layout_type) != XDR_UNIT)
5233 return nfserr_resource;
5234 return ops->encode_layoutget(xdr, lgp);
5235}
5236
9cf514cc
CH
5237static __be32
5238nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5239 union nfsd4_op_u *u)
9cf514cc 5240{
e78e274e 5241 struct nfsd4_layoutget *lgp = &u->layoutget;
bddfdbcd 5242 struct xdr_stream *xdr = resp->xdr;
9cf514cc 5243
69f5f019
CL
5244 /* logr_return_on_close */
5245 nfserr = nfsd4_encode_bool(xdr, true);
5246 if (nfserr != nfs_ok)
5247 return nfserr;
5248 /* logr_stateid */
5249 nfserr = nfsd4_encode_stateid4(xdr, &lgp->lg_sid);
5250 if (nfserr != nfs_ok)
5251 return nfserr;
5252 /* logr_layout<> */
5253 if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
bac966d6 5254 return nfserr_resource;
69f5f019 5255 return nfsd4_encode_layout4(xdr, lgp);
9cf514cc
CH
5256}
5257
5258static __be32
5259nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5260 union nfsd4_op_u *u)
9cf514cc 5261{
e78e274e 5262 struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
bddfdbcd 5263 struct xdr_stream *xdr = resp->xdr;
9cf514cc 5264
cc313f80
CL
5265 /* ns_sizechanged */
5266 nfserr = nfsd4_encode_bool(xdr, lcp->lc_size_chg);
5267 if (nfserr != nfs_ok)
5268 return nfserr;
5269 if (lcp->lc_size_chg)
5270 /* ns_size */
5271 return nfsd4_encode_length4(xdr, lcp->lc_newsize);
5272 return nfs_ok;
9cf514cc
CH
5273}
5274
5275static __be32
5276nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5277 union nfsd4_op_u *u)
9cf514cc 5278{
e78e274e 5279 struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
bddfdbcd 5280 struct xdr_stream *xdr = resp->xdr;
9cf514cc 5281
85dbc978
CL
5282 /* lrs_present */
5283 nfserr = nfsd4_encode_bool(xdr, lrp->lrs_present);
5284 if (nfserr != nfs_ok)
5285 return nfserr;
9cf514cc 5286 if (lrp->lrs_present)
85dbc978 5287 /* lrs_stateid */
40bb2baa 5288 return nfsd4_encode_stateid4(xdr, &lrp->lr_sid);
85dbc978 5289 return nfs_ok;
9cf514cc
CH
5290}
5291#endif /* CONFIG_NFSD_PNFS */
5292
29ae7f9d 5293static __be32
02e0297f
CL
5294nfsd4_encode_write_response4(struct xdr_stream *xdr,
5295 const struct nfsd4_copy *copy)
29ae7f9d 5296{
02e0297f
CL
5297 const struct nfsd42_write_res *write = &copy->cp_res;
5298 u32 count = nfsd4_copy_is_sync(copy) ? 0 : 1;
5299 __be32 status;
29ae7f9d 5300
02e0297f
CL
5301 /* wr_callback_id<1> */
5302 if (xdr_stream_encode_u32(xdr, count) != XDR_UNIT)
5303 return nfserr_resource;
5304 if (count) {
5305 status = nfsd4_encode_stateid4(xdr, &write->cb_stateid);
5306 if (status != nfs_ok)
5307 return status;
e0639dc5 5308 }
02e0297f
CL
5309
5310 /* wr_count */
5311 status = nfsd4_encode_length4(xdr, write->wr_bytes_written);
5312 if (status != nfs_ok)
5313 return status;
5314 /* wr_committed */
5315 if (xdr_stream_encode_u32(xdr, write->wr_stable_how) != XDR_UNIT)
29ae7f9d 5316 return nfserr_resource;
02e0297f
CL
5317 /* wr_writeverf */
5318 return nfsd4_encode_verifier4(xdr, &write->wr_verifier);
5319}
29ae7f9d 5320
02e0297f
CL
5321static __be32 nfsd4_encode_copy_requirements4(struct xdr_stream *xdr,
5322 const struct nfsd4_copy *copy)
5323{
5324 __be32 status;
5325
5326 /* cr_consecutive */
5327 status = nfsd4_encode_bool(xdr, true);
5328 if (status != nfs_ok)
5329 return status;
5330 /* cr_synchronous */
5331 return nfsd4_encode_bool(xdr, nfsd4_copy_is_sync(copy));
5332}
5333
5334static __be32
5335nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
5336 union nfsd4_op_u *u)
5337{
5338 struct nfsd4_copy *copy = &u->copy;
5339
5340 nfserr = nfsd4_encode_write_response4(resp->xdr, copy);
5341 if (nfserr != nfs_ok)
5342 return nfserr;
5343 return nfsd4_encode_copy_requirements4(resp->xdr, copy);
29ae7f9d
AS
5344}
5345
51911868 5346static __be32
21d316a7 5347nfsd4_encode_netloc4(struct xdr_stream *xdr, const struct nl4_server *ns)
51911868 5348{
21d316a7 5349 __be32 status;
51911868 5350
21d316a7
CL
5351 if (xdr_stream_encode_u32(xdr, ns->nl4_type) != XDR_UNIT)
5352 return nfserr_resource;
51911868
OK
5353 switch (ns->nl4_type) {
5354 case NL4_NETADDR:
21d316a7
CL
5355 /* nl_addr */
5356 status = nfsd4_encode_netaddr4(xdr, &ns->u.nl4_addr);
51911868
OK
5357 break;
5358 default:
21d316a7 5359 status = nfserr_serverfault;
51911868 5360 }
21d316a7
CL
5361 return status;
5362}
51911868 5363
21d316a7
CL
5364static __be32
5365nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
5366 union nfsd4_op_u *u)
5367{
5368 struct nfsd4_copy_notify *cn = &u->copy_notify;
5369 struct xdr_stream *xdr = resp->xdr;
5370
5371 /* cnr_lease_time */
5372 nfserr = nfsd4_encode_nfstime4(xdr, &cn->cpn_lease_time);
5373 if (nfserr)
5374 return nfserr;
5375 /* cnr_stateid */
5376 nfserr = nfsd4_encode_stateid4(xdr, &cn->cpn_cnr_stateid);
5377 if (nfserr)
5378 return nfserr;
5379 /* cnr_source_server<> */
5380 if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5381 return nfserr_resource;
5382 return nfsd4_encode_netloc4(xdr, cn->cpn_src);
51911868
OK
5383}
5384
6308bc98
OK
5385static __be32
5386nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5387 union nfsd4_op_u *u)
6308bc98 5388{
e78e274e 5389 struct nfsd4_offload_status *os = &u->offload_status;
bddfdbcd 5390 struct xdr_stream *xdr = resp->xdr;
6308bc98 5391
b609ad60
CL
5392 /* osr_count */
5393 nfserr = nfsd4_encode_length4(xdr, os->count);
5394 if (nfserr != nfs_ok)
5395 return nfserr;
5396 /* osr_complete<1> */
cc63c216
CL
5397 if (os->completed) {
5398 if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5399 return nfserr_resource;
5400 if (xdr_stream_encode_be32(xdr, os->status) != XDR_UNIT)
5401 return nfserr_resource;
5402 } else if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
6308bc98 5403 return nfserr_resource;
b609ad60 5404 return nfs_ok;
528b8493
AS
5405}
5406
5407static __be32
5408nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
eeadcb75 5409 struct nfsd4_read *read)
528b8493 5410{
a2c91753 5411 struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
528b8493 5412 struct file *file = read->rd_nf->nf_file;
eeadcb75 5413 struct xdr_stream *xdr = resp->xdr;
a2c91753 5414 bool splice_ok = argp->splice_ok;
26ea8163
CL
5415 unsigned int offset_offset;
5416 __be32 nfserr, wire_count;
eeadcb75 5417 unsigned long maxcount;
26ea8163 5418 __be64 wire_offset;
528b8493 5419
26ea8163 5420 if (xdr_stream_encode_u32(xdr, NFS4_CONTENT_DATA) != XDR_UNIT)
eeadcb75 5421 return nfserr_io;
26ea8163
CL
5422
5423 offset_offset = xdr->buf->len;
5424
5425 /* Reserve space for the byte offset and count */
5426 if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 3)))
eeadcb75 5427 return nfserr_io;
26ea8163 5428 xdr_commit_encode(xdr);
528b8493 5429
eeadcb75
AS
5430 maxcount = min_t(unsigned long, read->rd_length,
5431 (xdr->buf->buflen - xdr->buf->len));
528b8493 5432
eeadcb75
AS
5433 if (file->f_op->splice_read && splice_ok)
5434 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
5435 else
5436 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
528b8493
AS
5437 if (nfserr)
5438 return nfserr;
278765ea 5439
26ea8163
CL
5440 wire_offset = cpu_to_be64(read->rd_offset);
5441 write_bytes_to_xdr_buf(xdr->buf, offset_offset, &wire_offset,
5442 XDR_UNIT * 2);
5443 wire_count = cpu_to_be32(read->rd_length);
5444 write_bytes_to_xdr_buf(xdr->buf, offset_offset + XDR_UNIT * 2,
5445 &wire_count, XDR_UNIT);
2db27992
AS
5446 return nfs_ok;
5447}
5448
528b8493
AS
5449static __be32
5450nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5451 union nfsd4_op_u *u)
528b8493 5452{
e78e274e 5453 struct nfsd4_read *read = &u->read;
eeadcb75 5454 struct file *file = read->rd_nf->nf_file;
bddfdbcd 5455 struct xdr_stream *xdr = resp->xdr;
c9fc7772
CL
5456 unsigned int eof_offset;
5457 __be32 wire_data[2];
eeadcb75 5458 u32 segments = 0;
528b8493
AS
5459
5460 if (nfserr)
5461 return nfserr;
528b8493 5462
c9fc7772
CL
5463 eof_offset = xdr->buf->len;
5464
5465 /* Reserve space for the eof flag and segment count */
5466 if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2)))
eeadcb75 5467 return nfserr_io;
528b8493
AS
5468 xdr_commit_encode(xdr);
5469
eeadcb75
AS
5470 read->rd_eof = read->rd_offset >= i_size_read(file_inode(file));
5471 if (read->rd_eof)
2db27992
AS
5472 goto out;
5473
eeadcb75
AS
5474 nfserr = nfsd4_encode_read_plus_data(resp, read);
5475 if (nfserr) {
c9fc7772 5476 xdr_truncate_encode(xdr, eof_offset);
eeadcb75 5477 return nfserr;
528b8493 5478 }
6308bc98 5479
eeadcb75
AS
5480 segments++;
5481
5482out:
c9fc7772
CL
5483 wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
5484 wire_data[1] = cpu_to_be32(segments);
5485 write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
6308bc98
OK
5486 return nfserr;
5487}
5488
24bab491
AS
5489static __be32
5490nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5491 union nfsd4_op_u *u)
24bab491 5492{
e78e274e 5493 struct nfsd4_seek *seek = &u->seek;
1f121e2d 5494 struct xdr_stream *xdr = resp->xdr;
24bab491 5495
1f121e2d
CL
5496 /* sr_eof */
5497 nfserr = nfsd4_encode_bool(xdr, seek->seek_eof);
5498 if (nfserr != nfs_ok)
5499 return nfserr;
5500 /* sr_offset */
5501 return nfsd4_encode_offset4(xdr, seek->seek_pos);
24bab491
AS
5502}
5503
695e12f8 5504static __be32
e78e274e
KC
5505nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr,
5506 union nfsd4_op_u *p)
695e12f8
BH
5507{
5508 return nfserr;
5509}
5510
23e50fe3
FL
5511/*
5512 * Encode kmalloc-ed buffer in to XDR stream.
5513 */
b9a49237 5514static __be32
23e50fe3
FL
5515nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
5516{
5517 u32 cplen;
5518 __be32 *p;
5519
5520 cplen = min_t(unsigned long, buflen,
5521 ((void *)xdr->end - (void *)xdr->p));
5522 p = xdr_reserve_space(xdr, cplen);
5523 if (!p)
5524 return nfserr_resource;
5525
5526 memcpy(p, buf, cplen);
5527 buf += cplen;
5528 buflen -= cplen;
5529
5530 while (buflen) {
5531 cplen = min_t(u32, buflen, PAGE_SIZE);
5532 p = xdr_reserve_space(xdr, cplen);
5533 if (!p)
5534 return nfserr_resource;
5535
5536 memcpy(p, buf, cplen);
5537
5538 if (cplen < PAGE_SIZE) {
5539 /*
5540 * We're done, with a length that wasn't page
5541 * aligned, so possibly not word aligned. Pad
5542 * any trailing bytes with 0.
5543 */
5544 xdr_encode_opaque_fixed(p, NULL, cplen);
5545 break;
5546 }
5547
5548 buflen -= PAGE_SIZE;
5549 buf += PAGE_SIZE;
5550 }
5551
5552 return 0;
5553}
5554
5555static __be32
5556nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5557 union nfsd4_op_u *u)
23e50fe3 5558{
e78e274e 5559 struct nfsd4_getxattr *getxattr = &u->getxattr;
bddfdbcd 5560 struct xdr_stream *xdr = resp->xdr;
23e50fe3
FL
5561 __be32 *p, err;
5562
5563 p = xdr_reserve_space(xdr, 4);
5564 if (!p)
5565 return nfserr_resource;
5566
5567 *p = cpu_to_be32(getxattr->getxa_len);
5568
5569 if (getxattr->getxa_len == 0)
5570 return 0;
5571
5572 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
5573 getxattr->getxa_len);
5574
5575 kvfree(getxattr->getxa_buf);
5576
5577 return err;
5578}
5579
5580static __be32
5581nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5582 union nfsd4_op_u *u)
23e50fe3 5583{
e78e274e 5584 struct nfsd4_setxattr *setxattr = &u->setxattr;
bddfdbcd 5585 struct xdr_stream *xdr = resp->xdr;
23e50fe3 5586
66a21db7 5587 return nfsd4_encode_change_info4(xdr, &setxattr->setxa_cinfo);
23e50fe3
FL
5588}
5589
5590/*
5591 * See if there are cookie values that can be rejected outright.
5592 */
5593static __be32
5594nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5595 u32 *offsetp)
5596{
5597 u64 cookie = listxattrs->lsxa_cookie;
5598
5599 /*
5600 * If the cookie is larger than the maximum number we can fit
52a357db 5601 * in the buffer we just got back from vfs_listxattr, it's invalid.
23e50fe3
FL
5602 */
5603 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5604 return nfserr_badcookie;
5605
23e50fe3
FL
5606 *offsetp = (u32)cookie;
5607 return 0;
5608}
5609
5610static __be32
5611nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5612 union nfsd4_op_u *u)
23e50fe3 5613{
e78e274e 5614 struct nfsd4_listxattrs *listxattrs = &u->listxattrs;
bddfdbcd 5615 struct xdr_stream *xdr = resp->xdr;
23e50fe3
FL
5616 u32 cookie_offset, count_offset, eof;
5617 u32 left, xdrleft, slen, count;
5618 u32 xdrlen, offset;
5619 u64 cookie;
5620 char *sp;
b9a49237 5621 __be32 status, tmp;
61ab5e07 5622 __be64 wire_cookie;
23e50fe3
FL
5623 __be32 *p;
5624 u32 nuser;
5625
5626 eof = 1;
5627
5628 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5629 if (status)
5630 goto out;
5631
5632 /*
5633 * Reserve space for the cookie and the name array count. Record
5634 * the offsets to save them later.
5635 */
5636 cookie_offset = xdr->buf->len;
5637 count_offset = cookie_offset + 8;
31e4bb8f 5638 p = xdr_reserve_space(xdr, XDR_UNIT * 3);
23e50fe3
FL
5639 if (!p) {
5640 status = nfserr_resource;
5641 goto out;
5642 }
5643
5644 count = 0;
5645 left = listxattrs->lsxa_len;
5646 sp = listxattrs->lsxa_buf;
5647 nuser = 0;
5648
31e4bb8f
JM
5649 /* Bytes left is maxcount - 8 (cookie) - 4 (array count) */
5650 xdrleft = listxattrs->lsxa_maxcount - XDR_UNIT * 3;
23e50fe3
FL
5651
5652 while (left > 0 && xdrleft > 0) {
5653 slen = strlen(sp);
5654
5655 /*
4cce11fa 5656 * Check if this is a "user." attribute, skip it if not.
23e50fe3
FL
5657 */
5658 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5659 goto contloop;
5660
5661 slen -= XATTR_USER_PREFIX_LEN;
5662 xdrlen = 4 + ((slen + 3) & ~3);
2f73f37d
JM
5663 /* Check if both entry and eof can fit in the XDR buffer */
5664 if (xdrlen + XDR_UNIT > xdrleft) {
23e50fe3
FL
5665 if (count == 0) {
5666 /*
5667 * Can't even fit the first attribute name.
5668 */
5669 status = nfserr_toosmall;
5670 goto out;
5671 }
5672 eof = 0;
5673 goto wreof;
5674 }
5675
5676 left -= XATTR_USER_PREFIX_LEN;
5677 sp += XATTR_USER_PREFIX_LEN;
5678 if (nuser++ < offset)
5679 goto contloop;
5680
5681
5682 p = xdr_reserve_space(xdr, xdrlen);
5683 if (!p) {
5684 status = nfserr_resource;
5685 goto out;
5686 }
5687
e2a1840e 5688 xdr_encode_opaque(p, sp, slen);
23e50fe3
FL
5689
5690 xdrleft -= xdrlen;
5691 count++;
5692contloop:
5693 sp += slen + 1;
5694 left -= slen + 1;
5695 }
5696
5697 /*
5698 * If there were user attributes to copy, but we didn't copy
5699 * any, the offset was too large (e.g. the cookie was invalid).
5700 */
5701 if (nuser > 0 && count == 0) {
5702 status = nfserr_badcookie;
5703 goto out;
5704 }
5705
5706wreof:
5707 p = xdr_reserve_space(xdr, 4);
5708 if (!p) {
5709 status = nfserr_resource;
5710 goto out;
5711 }
5712 *p = cpu_to_be32(eof);
5713
5714 cookie = offset + count;
5715
61ab5e07
JM
5716 wire_cookie = cpu_to_be64(cookie);
5717 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &wire_cookie, 8);
b9a49237
CL
5718 tmp = cpu_to_be32(count);
5719 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
23e50fe3
FL
5720out:
5721 if (listxattrs->lsxa_len)
5722 kvfree(listxattrs->lsxa_buf);
5723 return status;
5724}
5725
5726static __be32
5727nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
e78e274e 5728 union nfsd4_op_u *u)
23e50fe3 5729{
e78e274e 5730 struct nfsd4_removexattr *removexattr = &u->removexattr;
bddfdbcd 5731 struct xdr_stream *xdr = resp->xdr;
23e50fe3 5732
66a21db7 5733 return nfsd4_encode_change_info4(xdr, &removexattr->rmxa_cinfo);
23e50fe3
FL
5734}
5735
e78e274e 5736typedef __be32(*nfsd4_enc)(struct nfsd4_compoundres *, __be32, union nfsd4_op_u *u);
695e12f8 5737
2db134eb
AA
5738/*
5739 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5740 * since we don't need to filter out obsolete ops as this is
5741 * done in the decoding phase.
5742 */
c1df609d 5743static const nfsd4_enc nfsd4_enc_ops[] = {
e78e274e
KC
5744 [OP_ACCESS] = nfsd4_encode_access,
5745 [OP_CLOSE] = nfsd4_encode_close,
5746 [OP_COMMIT] = nfsd4_encode_commit,
5747 [OP_CREATE] = nfsd4_encode_create,
5748 [OP_DELEGPURGE] = nfsd4_encode_noop,
5749 [OP_DELEGRETURN] = nfsd4_encode_noop,
5750 [OP_GETATTR] = nfsd4_encode_getattr,
5751 [OP_GETFH] = nfsd4_encode_getfh,
5752 [OP_LINK] = nfsd4_encode_link,
5753 [OP_LOCK] = nfsd4_encode_lock,
5754 [OP_LOCKT] = nfsd4_encode_lockt,
5755 [OP_LOCKU] = nfsd4_encode_locku,
5756 [OP_LOOKUP] = nfsd4_encode_noop,
5757 [OP_LOOKUPP] = nfsd4_encode_noop,
5758 [OP_NVERIFY] = nfsd4_encode_noop,
5759 [OP_OPEN] = nfsd4_encode_open,
5760 [OP_OPENATTR] = nfsd4_encode_noop,
5761 [OP_OPEN_CONFIRM] = nfsd4_encode_open_confirm,
5762 [OP_OPEN_DOWNGRADE] = nfsd4_encode_open_downgrade,
5763 [OP_PUTFH] = nfsd4_encode_noop,
5764 [OP_PUTPUBFH] = nfsd4_encode_noop,
5765 [OP_PUTROOTFH] = nfsd4_encode_noop,
5766 [OP_READ] = nfsd4_encode_read,
5767 [OP_READDIR] = nfsd4_encode_readdir,
5768 [OP_READLINK] = nfsd4_encode_readlink,
5769 [OP_REMOVE] = nfsd4_encode_remove,
5770 [OP_RENAME] = nfsd4_encode_rename,
5771 [OP_RENEW] = nfsd4_encode_noop,
5772 [OP_RESTOREFH] = nfsd4_encode_noop,
5773 [OP_SAVEFH] = nfsd4_encode_noop,
5774 [OP_SECINFO] = nfsd4_encode_secinfo,
5775 [OP_SETATTR] = nfsd4_encode_setattr,
5776 [OP_SETCLIENTID] = nfsd4_encode_setclientid,
5777 [OP_SETCLIENTID_CONFIRM] = nfsd4_encode_noop,
5778 [OP_VERIFY] = nfsd4_encode_noop,
5779 [OP_WRITE] = nfsd4_encode_write,
5780 [OP_RELEASE_LOCKOWNER] = nfsd4_encode_noop,
2db134eb
AA
5781
5782 /* NFSv4.1 operations */
e78e274e
KC
5783 [OP_BACKCHANNEL_CTL] = nfsd4_encode_noop,
5784 [OP_BIND_CONN_TO_SESSION] = nfsd4_encode_bind_conn_to_session,
5785 [OP_EXCHANGE_ID] = nfsd4_encode_exchange_id,
5786 [OP_CREATE_SESSION] = nfsd4_encode_create_session,
5787 [OP_DESTROY_SESSION] = nfsd4_encode_noop,
5788 [OP_FREE_STATEID] = nfsd4_encode_noop,
33a1e6ea 5789 [OP_GET_DIR_DELEGATION] = nfsd4_encode_get_dir_delegation,
9cf514cc 5790#ifdef CONFIG_NFSD_PNFS
e78e274e
KC
5791 [OP_GETDEVICEINFO] = nfsd4_encode_getdeviceinfo,
5792 [OP_GETDEVICELIST] = nfsd4_encode_noop,
5793 [OP_LAYOUTCOMMIT] = nfsd4_encode_layoutcommit,
5794 [OP_LAYOUTGET] = nfsd4_encode_layoutget,
5795 [OP_LAYOUTRETURN] = nfsd4_encode_layoutreturn,
9cf514cc 5796#else
e78e274e
KC
5797 [OP_GETDEVICEINFO] = nfsd4_encode_noop,
5798 [OP_GETDEVICELIST] = nfsd4_encode_noop,
5799 [OP_LAYOUTCOMMIT] = nfsd4_encode_noop,
5800 [OP_LAYOUTGET] = nfsd4_encode_noop,
5801 [OP_LAYOUTRETURN] = nfsd4_encode_noop,
9cf514cc 5802#endif
e78e274e
KC
5803 [OP_SECINFO_NO_NAME] = nfsd4_encode_secinfo_no_name,
5804 [OP_SEQUENCE] = nfsd4_encode_sequence,
5805 [OP_SET_SSV] = nfsd4_encode_noop,
5806 [OP_TEST_STATEID] = nfsd4_encode_test_stateid,
5807 [OP_WANT_DELEGATION] = nfsd4_encode_noop,
5808 [OP_DESTROY_CLIENTID] = nfsd4_encode_noop,
5809 [OP_RECLAIM_COMPLETE] = nfsd4_encode_noop,
87a15a80
AS
5810
5811 /* NFSv4.2 operations */
e78e274e
KC
5812 [OP_ALLOCATE] = nfsd4_encode_noop,
5813 [OP_COPY] = nfsd4_encode_copy,
5814 [OP_COPY_NOTIFY] = nfsd4_encode_copy_notify,
5815 [OP_DEALLOCATE] = nfsd4_encode_noop,
5816 [OP_IO_ADVISE] = nfsd4_encode_noop,
5817 [OP_LAYOUTERROR] = nfsd4_encode_noop,
5818 [OP_LAYOUTSTATS] = nfsd4_encode_noop,
5819 [OP_OFFLOAD_CANCEL] = nfsd4_encode_noop,
5820 [OP_OFFLOAD_STATUS] = nfsd4_encode_offload_status,
5821 [OP_READ_PLUS] = nfsd4_encode_read_plus,
5822 [OP_SEEK] = nfsd4_encode_seek,
5823 [OP_WRITE_SAME] = nfsd4_encode_noop,
5824 [OP_CLONE] = nfsd4_encode_noop,
23e50fe3
FL
5825
5826 /* RFC 8276 extended atributes operations */
e78e274e
KC
5827 [OP_GETXATTR] = nfsd4_encode_getxattr,
5828 [OP_SETXATTR] = nfsd4_encode_setxattr,
5829 [OP_LISTXATTRS] = nfsd4_encode_listxattrs,
5830 [OP_REMOVEXATTR] = nfsd4_encode_removexattr,
695e12f8
BH
5831};
5832
496c262c 5833/*
a8095f7e
BF
5834 * Calculate whether we still have space to encode repsize bytes.
5835 * There are two considerations:
5836 * - For NFS versions >=4.1, the size of the reply must stay within
5837 * session limits
5838 * - For all NFS versions, we must stay within limited preallocated
5839 * buffer space.
496c262c 5840 *
a8095f7e
BF
5841 * This is called before the operation is processed, so can only provide
5842 * an upper estimate. For some nonidempotent operations (such as
5843 * getattr), it's not necessarily a problem if that estimate is wrong,
5844 * as we can fail it after processing without significant side effects.
496c262c 5845 */
a8095f7e 5846__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
496c262c 5847{
67492c99 5848 struct xdr_buf *buf = &resp->rqstp->rq_res;
47ee5298 5849 struct nfsd4_slot *slot = resp->cstate.slot;
496c262c 5850
47ee5298
BF
5851 if (buf->len + respsize <= buf->buflen)
5852 return nfs_ok;
5853 if (!nfsd4_has_session(&resp->cstate))
ea8d7720 5854 return nfserr_resource;
47ee5298
BF
5855 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5856 WARN_ON_ONCE(1);
5857 return nfserr_rep_too_big_to_cache;
ea8d7720 5858 }
47ee5298 5859 return nfserr_rep_too_big;
496c262c
AA
5860}
5861
438f81e0
N
5862static __be32 nfsd4_map_status(__be32 status, u32 minor)
5863{
5864 switch (status) {
5865 case nfs_ok:
5866 break;
5867 case nfserr_wrong_type:
5868 /* RFC 8881 - 15.1.2.9 */
5869 if (minor == 0)
5870 status = nfserr_inval;
5871 break;
5872 case nfserr_symlink_not_dir:
5873 status = nfserr_symlink;
5874 break;
5875 }
5876 return status;
5877}
5878
1da177e4
LT
5879void
5880nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5881{
bddfdbcd 5882 struct xdr_stream *xdr = resp->xdr;
9411b1d4 5883 struct nfs4_stateowner *so = resp->cstate.replay_owner;
5f4ab945 5884 struct svc_rqst *rqstp = resp->rqstp;
34b1744c 5885 const struct nfsd4_operation *opdesc = op->opdesc;
ef3675b4 5886 unsigned int op_status_offset;
07d1f802 5887 nfsd4_enc encoder;
1da177e4 5888
ef3675b4
CL
5889 if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
5890 goto release;
5891 op_status_offset = xdr->buf->len;
5892 if (!xdr_reserve_space(xdr, XDR_UNIT))
15a8b55d 5893 goto release;
1da177e4 5894
695e12f8
BH
5895 if (op->opnum == OP_ILLEGAL)
5896 goto status;
b7571e4c
BF
5897 if (op->status && opdesc &&
5898 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5899 goto status;
3a237b4a 5900 BUG_ON(op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
695e12f8 5901 !nfsd4_enc_ops[op->opnum]);
07d1f802
BF
5902 encoder = nfsd4_enc_ops[op->opnum];
5903 op->status = encoder(resp, op->status, &op->u);
08281341
CL
5904 if (op->status)
5905 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
2825a7f9
BF
5906 xdr_commit_encode(xdr);
5907
067e1ace 5908 /* nfsd4_check_resp_size guarantees enough room for error status */
5f4ab945
BF
5909 if (!op->status) {
5910 int space_needed = 0;
5911 if (!nfsd4_last_compound_op(rqstp))
5912 space_needed = COMPOUND_ERR_SLACK_SPACE;
5913 op->status = nfsd4_check_resp_size(resp, space_needed);
5914 }
c8f13d97
BF
5915 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5916 struct nfsd4_slot *slot = resp->cstate.slot;
5917
5918 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5919 op->status = nfserr_rep_too_big_to_cache;
5920 else
5921 op->status = nfserr_rep_too_big;
5922 }
07d1f802
BF
5923 if (op->status == nfserr_resource ||
5924 op->status == nfserr_rep_too_big ||
5925 op->status == nfserr_rep_too_big_to_cache) {
5926 /*
5927 * The operation may have already been encoded or
5928 * partially encoded. No op returns anything additional
5929 * in the case of one of these three errors, so we can
5930 * just truncate back to after the status. But it's a
5931 * bug if we had to do this on a non-idempotent op:
5932 */
5933 warn_on_nonidempotent_op(op);
ef3675b4 5934 xdr_truncate_encode(xdr, op_status_offset + XDR_UNIT);
07d1f802 5935 }
9411b1d4 5936 if (so) {
ef3675b4 5937 int len = xdr->buf->len - (op_status_offset + XDR_UNIT);
082d4bd7 5938
9411b1d4 5939 so->so_replay.rp_status = op->status;
082d4bd7 5940 so->so_replay.rp_buflen = len;
ef3675b4 5941 read_bytes_from_xdr_buf(xdr->buf, op_status_offset + XDR_UNIT,
082d4bd7 5942 so->so_replay.rp_buf, len);
9411b1d4 5943 }
695e12f8 5944status:
438f81e0
N
5945 op->status = nfsd4_map_status(op->status,
5946 resp->cstate.minorversion);
ef3675b4
CL
5947 write_bytes_to_xdr_buf(xdr->buf, op_status_offset,
5948 &op->status, XDR_UNIT);
15a8b55d
JL
5949release:
5950 if (opdesc && opdesc->op_release)
5951 opdesc->op_release(&op->u);
ed4a567a
CL
5952
5953 /*
5954 * Account for pages consumed while encoding this operation.
5955 * The xdr_stream primitives don't manage rq_next_page.
5956 */
5957 rqstp->rq_next_page = xdr->page_ptr + 1;
1da177e4
LT
5958}
5959
9b350d3e
CL
5960/**
5961 * nfsd4_encode_replay - encode a result stored in the stateowner reply cache
5962 * @xdr: send buffer's XDR stream
5963 * @op: operation being replayed
5964 *
5965 * @op->replay->rp_buf contains the previously-sent already-encoded result.
1da177e4 5966 */
9b350d3e 5967void nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
1da177e4 5968{
1da177e4
LT
5969 struct nfs4_replay *rp = op->replay;
5970
9b350d3e 5971 trace_nfsd_stateowner_replay(op->opnum, rp);
1da177e4 5972
9b350d3e
CL
5973 if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
5974 return;
5975 if (xdr_stream_encode_be32(xdr, rp->rp_status) != XDR_UNIT)
5976 return;
5977 xdr_stream_encode_opaque_fixed(xdr, rp->rp_buf, rp->rp_buflen);
1da177e4
LT
5978}
5979
8537488b 5980void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
1da177e4 5981{
3e98abff
BF
5982 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5983
1da177e4 5984 if (args->ops != args->iops) {
80e591ce 5985 vfree(args->ops);
1da177e4
LT
5986 args->ops = args->iops;
5987 }
1da177e4 5988 while (args->to_free) {
d5e23383 5989 struct svcxdr_tmpbuf *tb = args->to_free;
1da177e4 5990 args->to_free = tb->next;
1da177e4
LT
5991 kfree(tb);
5992 }
5993}
5994
c44b31c2 5995bool
16c66364 5996nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 5997{
026fec7e
CH
5998 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5999
c1346a12 6000 /* svcxdr_tmp_alloc */
1da177e4 6001 args->to_free = NULL;
c1346a12 6002
16c66364 6003 args->xdr = xdr;
1da177e4
LT
6004 args->ops = args->iops;
6005 args->rqstp = rqstp;
6006
d9b74bda 6007 return nfsd4_decode_compound(args);
1da177e4
LT
6008}
6009
130e2054 6010bool
fda49441 6011nfs4svc_encode_compoundres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 6012{
63f8de37 6013 struct nfsd4_compoundres *resp = rqstp->rq_resp;
fda49441 6014 __be32 *p;
6ac90391 6015
3b0ebb25
CL
6016 /*
6017 * Send buffer space for the following items is reserved
6018 * at the top of nfsd4_proc_compound().
6019 */
6020 p = resp->statusp;
6021
6022 *p++ = resp->cstate.status;
1da177e4
LT
6023 *p++ = htonl(resp->taglen);
6024 memcpy(p, resp->tag, resp->taglen);
6025 p += XDR_QUADLEN(resp->taglen);
6026 *p++ = htonl(resp->opcnt);
6027
b607664e 6028 nfsd4_sequence_done(resp);
130e2054 6029 return true;
1da177e4 6030}