xref: /linux/fs/nfsd/nfs4xdr.c (revision 15ecd83dc06277385ad71dc7ea26911d9a79acaf)
1 /*
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.
34  */
35 
36 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/statfs.h>
40 #include <linux/utsname.h>
41 #include <linux/pagemap.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/xattr.h>
45 #include <linux/vmalloc.h>
46 
47 #include <uapi/linux/xattr.h>
48 
49 #include "idmap.h"
50 #include "acl.h"
51 #include "xdr4.h"
52 #include "vfs.h"
53 #include "state.h"
54 #include "cache.h"
55 #include "netns.h"
56 #include "pnfs.h"
57 #include "filecache.h"
58 #include "nfs4xdr_gen.h"
59 
60 #include "trace.h"
61 
62 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
63 #include <linux/security.h>
64 #endif
65 
66 
67 #define NFSDDBG_FACILITY		NFSDDBG_XDR
68 
69 const u32 nfsd_suppattrs[3][3] = {
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 
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 
91 static __be32
92 check_filename(char *str, int len)
93 {
94 	int i;
95 
96 	if (len == 0)
97 		return nfserr_inval;
98 	if (len > NFS4_MAXNAMLEN)
99 		return nfserr_nametoolong;
100 	if (isdotent(str, len))
101 		return nfserr_badname;
102 	for (i = 0; i < len; i++)
103 		if (str[i] == '/')
104 			return nfserr_badname;
105 	return 0;
106 }
107 
108 static int zero_clientid(clientid_t *clid)
109 {
110 	return (clid->cl_boot == 0) && (clid->cl_id == 0);
111 }
112 
113 /**
114  * svcxdr_tmpalloc - allocate memory to be freed after compound processing
115  * @argp: NFSv4 compound argument structure
116  * @len: length of buffer to allocate
117  *
118  * Allocates a buffer of size @len to be freed when processing the compound
119  * operation described in @argp finishes.
120  */
121 static void *
122 svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, size_t len)
123 {
124 	struct svcxdr_tmpbuf *tb;
125 
126 	tb = kmalloc(struct_size(tb, buf, len), GFP_KERNEL);
127 	if (!tb)
128 		return NULL;
129 	tb->next = argp->to_free;
130 	argp->to_free = tb;
131 	return tb->buf;
132 }
133 
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  */
141 static char *
142 svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, size_t len)
143 {
144 	char *p = svcxdr_tmpalloc(argp, size_add(len, 1));
145 
146 	if (!p)
147 		return NULL;
148 	memcpy(p, buf, len);
149 	p[len] = '\0';
150 	return p;
151 }
152 
153 static void *
154 svcxdr_savemem(struct nfsd4_compoundargs *argp, __be32 *p, size_t len)
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 
172 /*
173  * NFSv4 basic data type decoders
174  */
175 
176 /*
177  * This helper handles variable-length opaques which belong to protocol
178  * elements that this implementation does not support.
179  */
180 static __be32
181 nfsd4_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 
195 static __be32
196 nfsd4_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;
208 	o->data = svcxdr_savemem(argp, p, len);
209 	if (!o->data)
210 		return nfserr_jukebox;
211 	o->len = len;
212 
213 	return nfs_ok;
214 }
215 
216 static __be32
217 nfsd4_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;
229 	*namp = svcxdr_savemem(argp, p, *lenp);
230 	if (!*namp)
231 		return nfserr_jukebox;
232 
233 	return nfs_ok;
234 }
235 
236 static __be32
237 nfsd4_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 
251 static __be32
252 nfsd4_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 
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  */
280 static __be32
281 nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
282 {
283 	ssize_t status;
284 
285 	status = xdr_stream_decode_uint32_array(argp->xdr, bmval, bmlen);
286 	return status == -EBADMSG ? nfserr_bad_xdr : nfs_ok;
287 }
288 
289 static __be32
290 nfsd4_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 */
321 static noinline __be32
322 nfsd4_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 
353 static noinline __be32
354 nfsd4_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 
380 static __be32
381 nfsd4_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)
384 {
385 	unsigned int starting_pos;
386 	u32 attrlist4_count;
387 	__be32 *p, status;
388 
389 	iattr->ia_valid = 0;
390 	status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
391 	if (status)
392 		return nfserr_bad_xdr;
393 
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 
402 	if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
403 		return nfserr_bad_xdr;
404 	starting_pos = xdr_stream_pos(argp->xdr);
405 
406 	if (bmval[0] & FATTR4_WORD0_SIZE) {
407 		u64 size;
408 
409 		if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
410 			return nfserr_bad_xdr;
411 		iattr->ia_size = size;
412 		iattr->ia_valid |= ATTR_SIZE;
413 	}
414 	if (bmval[0] & FATTR4_WORD0_ACL) {
415 		status = nfsd4_decode_acl(argp, acl);
416 		if (status)
417 			return status;
418 	} else
419 		*acl = NULL;
420 	if (bmval[1] & FATTR4_WORD1_MODE) {
421 		u32 mode;
422 
423 		if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
424 			return nfserr_bad_xdr;
425 		iattr->ia_mode = mode;
426 		iattr->ia_mode &= (S_IFMT | S_IALLUGO);
427 		iattr->ia_valid |= ATTR_MODE;
428 	}
429 	if (bmval[1] & FATTR4_WORD1_OWNER) {
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)
440 			return status;
441 		iattr->ia_valid |= ATTR_UID;
442 	}
443 	if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
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)
454 			return status;
455 		iattr->ia_valid |= ATTR_GID;
456 	}
457 	if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
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) {
463 		case NFS4_SET_TO_CLIENT_TIME:
464 			status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
465 			if (status)
466 				return status;
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:
473 			return nfserr_bad_xdr;
474 		}
475 	}
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 	}
485 	if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
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) {
491 		case NFS4_SET_TO_CLIENT_TIME:
492 			status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
493 			if (status)
494 				return status;
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:
501 			return nfserr_bad_xdr;
502 		}
503 	}
504 	label->len = 0;
505 	if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
506 	    bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
507 		status = nfsd4_decode_security_label(argp, label);
508 		if (status)
509 			return status;
510 	}
511 	if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
512 		u32 mode, mask;
513 
514 		if (!umask)
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;
522 		iattr->ia_valid |= ATTR_MODE;
523 	}
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 	}
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;
548 
549 	return nfs_ok;
550 }
551 
552 static __be32
553 nfsd4_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 
565 static __be32
566 nfsd4_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 
577 static __be32
578 nfsd4_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 
589 #ifdef CONFIG_NFSD_PNFS
590 static __be32
591 nfsd4_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 }
602 
603 static __be32
604 nfsd4_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 
625 static __be32
626 nfsd4_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 
662 #endif /* CONFIG_NFSD_PNFS */
663 
664 static __be32
665 nfsd4_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 
677 /* Defined in Appendix A of RFC 5531 */
678 static __be32
679 nfsd4_decode_authsys_parms(struct nfsd4_compoundargs *argp,
680 			   struct nfsd4_cb_sec *cbs)
681 {
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 
719 static __be32
720 nfsd4_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 */
745 static __be32
746 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
747 {
748 	u32 i, secflavor, nr_secflavs;
749 	__be32 status;
750 
751 	/* callback_sec_params4 */
752 	if (xdr_stream_decode_u32(argp->xdr, &nr_secflavs) < 0)
753 		return nfserr_bad_xdr;
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;
759 
760 	for (i = 0; i < nr_secflavs; ++i) {
761 		if (xdr_stream_decode_u32(argp->xdr, &secflavor) < 0)
762 			return nfserr_bad_xdr;
763 		switch (secflavor) {
764 		case RPC_AUTH_NULL:
765 			/* void */
766 			if (cbs->flavor == (u32)(-1))
767 				cbs->flavor = RPC_AUTH_NULL;
768 			break;
769 		case RPC_AUTH_UNIX:
770 			status = nfsd4_decode_authsys_parms(argp, cbs);
771 			if (status)
772 				return status;
773 			break;
774 		case RPC_AUTH_GSS:
775 			status = nfsd4_decode_gss_cb_handles4(argp, cbs);
776 			if (status)
777 				return status;
778 			break;
779 		default:
780 			return nfserr_inval;
781 		}
782 	}
783 
784 	return nfs_ok;
785 }
786 
787 
788 /*
789  * NFSv4 operation argument decoders
790  */
791 
792 static __be32
793 nfsd4_decode_access(struct nfsd4_compoundargs *argp,
794 		    union nfsd4_op_u *u)
795 {
796 	struct nfsd4_access *access = &u->access;
797 	if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
798 		return nfserr_bad_xdr;
799 	return nfs_ok;
800 }
801 
802 static __be32
803 nfsd4_decode_close(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
804 {
805 	struct nfsd4_close *close = &u->close;
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);
809 }
810 
811 
812 static __be32
813 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
814 {
815 	struct nfsd4_commit *commit = &u->commit;
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;
820 	memset(&commit->co_verf, 0, sizeof(commit->co_verf));
821 	return nfs_ok;
822 }
823 
824 static __be32
825 nfsd4_decode_create(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
826 {
827 	struct nfsd4_create *create = &u->create;
828 	__be32 *p, status;
829 
830 	memset(create, 0, sizeof(*create));
831 	if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
832 		return nfserr_bad_xdr;
833 	switch (create->cr_type) {
834 	case NF4LNK:
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;
840 		create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
841 		if (!create->cr_data)
842 			return nfserr_jukebox;
843 		break;
844 	case NF4BLK:
845 	case NF4CHR:
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;
850 		break;
851 	case NF4SOCK:
852 	case NF4FIFO:
853 	case NF4DIR:
854 	default:
855 		break;
856 	}
857 	status = nfsd4_decode_component4(argp, &create->cr_name,
858 					 &create->cr_namelen);
859 	if (status)
860 		return status;
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);
865 	if (status)
866 		return status;
867 
868 	return nfs_ok;
869 }
870 
871 static inline __be32
872 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
873 {
874 	struct nfsd4_delegreturn *dr = &u->delegreturn;
875 	return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
876 }
877 
878 static inline __be32
879 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
880 {
881 	struct nfsd4_getattr *getattr = &u->getattr;
882 	memset(getattr, 0, sizeof(*getattr));
883 	return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
884 				    ARRAY_SIZE(getattr->ga_bmval));
885 }
886 
887 static __be32
888 nfsd4_decode_link(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
889 {
890 	struct nfsd4_link *link = &u->link;
891 	memset(link, 0, sizeof(*link));
892 	return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
893 }
894 
895 static __be32
896 nfsd4_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 
912 static __be32
913 nfsd4_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 
927 static __be32
928 nfsd4_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 
937 static __be32
938 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
939 {
940 	struct nfsd4_lock *lock = &u->lock;
941 	memset(lock, 0, sizeof(*lock));
942 	if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
943 		return nfserr_bad_xdr;
944 	if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
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);
953 }
954 
955 static __be32
956 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
957 {
958 	struct nfsd4_lockt *lockt = &u->lockt;
959 	memset(lockt, 0, sizeof(*lockt));
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);
970 }
971 
972 static __be32
973 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
974 {
975 	struct nfsd4_locku *locku = &u->locku;
976 	__be32 status;
977 
978 	if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
979 		return nfserr_bad_xdr;
980 	if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
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);
985 	if (status)
986 		return status;
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;
991 
992 	return nfs_ok;
993 }
994 
995 static __be32
996 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
997 {
998 	struct nfsd4_lookup *lookup = &u->lookup;
999 	return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
1000 }
1001 
1002 static __be32
1003 nfsd4_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 
1044 static __be32
1045 nfsd4_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 
1066 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
1067 {
1068 	u32 w;
1069 
1070 	if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
1071 		return nfserr_bad_xdr;
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 
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 	}
1085 	w &= ~NFS4_SHARE_ACCESS_MASK;
1086 	if (!w)
1087 		return nfs_ok;
1088 	if (!argp->minorversion)
1089 		return nfserr_bad_xdr;
1090 	switch (w & NFS4_SHARE_WANT_TYPE_MASK) {
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:
1097 		break;
1098 	default:
1099 		return nfserr_bad_xdr;
1100 	}
1101 	w &= ~NFS4_SHARE_WANT_MASK;
1102 	if (!w)
1103 		return nfs_ok;
1104 
1105 	if (!deleg_when)	/* open_downgrade */
1106 		return nfserr_inval;
1107 	switch (w) {
1108 	case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
1109 	case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
1110 	case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
1111 	      NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
1112 		return nfs_ok;
1113 	}
1114 	return nfserr_bad_xdr;
1115 }
1116 
1117 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
1118 {
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. */
1122 	if (*x & ~NFS4_SHARE_DENY_BOTH)
1123 		return nfserr_bad_xdr;
1124 
1125 	return nfs_ok;
1126 }
1127 
1128 static __be32
1129 nfsd4_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 
1177 static __be32
1178 nfsd4_decode_open(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1179 {
1180 	struct nfsd4_open *open = &u->open;
1181 	__be32 status;
1182 	u32 dummy;
1183 
1184 	memset(open, 0, sizeof(*open));
1185 
1186 	if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
1187 		return nfserr_bad_xdr;
1188 	/* deleg_want is ignored */
1189 	status = nfsd4_decode_share_access(argp, &open->op_share_access,
1190 					   &open->op_deleg_want, &dummy);
1191 	if (status)
1192 		return status;
1193 	status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
1194 	if (status)
1195 		return status;
1196 	status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
1197 					   &open->op_owner);
1198 	if (status)
1199 		return status;
1200 	status = nfsd4_decode_openflag4(argp, open);
1201 	if (status)
1202 		return status;
1203 	return nfsd4_decode_open_claim4(argp, open);
1204 }
1205 
1206 static __be32
1207 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp,
1208 			  union nfsd4_op_u *u)
1209 {
1210 	struct nfsd4_open_confirm *open_conf = &u->open_confirm;
1211 	__be32 status;
1212 
1213 	if (argp->minorversion >= 1)
1214 		return nfserr_notsupp;
1215 
1216 	status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
1217 	if (status)
1218 		return status;
1219 	if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
1220 		return nfserr_bad_xdr;
1221 
1222 	memset(&open_conf->oc_resp_stateid, 0,
1223 	       sizeof(open_conf->oc_resp_stateid));
1224 	return nfs_ok;
1225 }
1226 
1227 static __be32
1228 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp,
1229 			    union nfsd4_op_u *u)
1230 {
1231 	struct nfsd4_open_downgrade *open_down = &u->open_downgrade;
1232 	__be32 status;
1233 
1234 	memset(open_down, 0, sizeof(*open_down));
1235 	status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
1236 	if (status)
1237 		return status;
1238 	if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
1239 		return nfserr_bad_xdr;
1240 	/* deleg_want is ignored */
1241 	status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1242 					   &open_down->od_deleg_want, NULL);
1243 	if (status)
1244 		return status;
1245 	return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1246 }
1247 
1248 static __be32
1249 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1250 {
1251 	struct nfsd4_putfh *putfh = &u->putfh;
1252 	__be32 *p;
1253 
1254 	if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
1255 		return nfserr_bad_xdr;
1256 	if (putfh->pf_fhlen > NFS4_FHSIZE)
1257 		return nfserr_bad_xdr;
1258 	p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
1259 	if (!p)
1260 		return nfserr_bad_xdr;
1261 	putfh->pf_fhval = svcxdr_savemem(argp, p, putfh->pf_fhlen);
1262 	if (!putfh->pf_fhval)
1263 		return nfserr_jukebox;
1264 
1265 	putfh->no_verify = false;
1266 	return nfs_ok;
1267 }
1268 
1269 static __be32
1270 nfsd4_decode_read(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1271 {
1272 	struct nfsd4_read *read = &u->read;
1273 	__be32 status;
1274 
1275 	memset(read, 0, sizeof(*read));
1276 	status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
1277 	if (status)
1278 		return status;
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;
1283 
1284 	return nfs_ok;
1285 }
1286 
1287 static __be32
1288 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1289 {
1290 	struct nfsd4_readdir *readdir = &u->readdir;
1291 	__be32 status;
1292 
1293 	memset(readdir, 0, sizeof(*readdir));
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;
1306 
1307 	return nfs_ok;
1308 }
1309 
1310 static __be32
1311 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1312 {
1313 	struct nfsd4_remove *remove = &u->remove;
1314 	memset(&remove->rm_cinfo, 0, sizeof(remove->rm_cinfo));
1315 	return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
1316 }
1317 
1318 static __be32
1319 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1320 {
1321 	struct nfsd4_rename *rename = &u->rename;
1322 	__be32 status;
1323 
1324 	memset(rename, 0, sizeof(*rename));
1325 	status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
1326 	if (status)
1327 		return status;
1328 	return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
1329 }
1330 
1331 static __be32
1332 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1333 {
1334 	clientid_t *clientid = &u->renew;
1335 	return nfsd4_decode_clientid4(argp, clientid);
1336 }
1337 
1338 static __be32
1339 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1340 		     union nfsd4_op_u *u)
1341 {
1342 	struct nfsd4_secinfo *secinfo = &u->secinfo;
1343 	secinfo->si_exp = NULL;
1344 	return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
1345 }
1346 
1347 static __be32
1348 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1349 {
1350 	struct nfsd4_setattr *setattr = &u->setattr;
1351 	__be32 status;
1352 
1353 	memset(setattr, 0, sizeof(*setattr));
1354 	status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
1355 	if (status)
1356 		return status;
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);
1361 }
1362 
1363 static __be32
1364 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1365 {
1366 	struct nfsd4_setclientid *setclientid = &u->setclientid;
1367 	__be32 *p, status;
1368 
1369 	memset(setclientid, 0, sizeof(*setclientid));
1370 
1371 	if (argp->minorversion >= 1)
1372 		return nfserr_notsupp;
1373 
1374 	status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
1375 	if (status)
1376 		return status;
1377 	status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1378 	if (status)
1379 		return status;
1380 	if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
1381 		return nfserr_bad_xdr;
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;
1387 	setclientid->se_callback_netid_val = svcxdr_savemem(argp, p,
1388 						setclientid->se_callback_netid_len);
1389 	if (!setclientid->se_callback_netid_val)
1390 		return nfserr_jukebox;
1391 
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;
1397 	setclientid->se_callback_addr_val = svcxdr_savemem(argp, p,
1398 						setclientid->se_callback_addr_len);
1399 	if (!setclientid->se_callback_addr_val)
1400 		return nfserr_jukebox;
1401 	if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
1402 		return nfserr_bad_xdr;
1403 
1404 	return nfs_ok;
1405 }
1406 
1407 static __be32
1408 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp,
1409 				 union nfsd4_op_u *u)
1410 {
1411 	struct nfsd4_setclientid_confirm *scd_c = &u->setclientid_confirm;
1412 	__be32 status;
1413 
1414 	if (argp->minorversion >= 1)
1415 		return nfserr_notsupp;
1416 
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);
1421 }
1422 
1423 /* Also used for NVERIFY */
1424 static __be32
1425 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1426 {
1427 	struct nfsd4_verify *verify = &u->verify;
1428 	__be32 *p, status;
1429 
1430 	memset(verify, 0, sizeof(*verify));
1431 
1432 	status = nfsd4_decode_bitmap4(argp, verify->ve_bmval,
1433 				      ARRAY_SIZE(verify->ve_bmval));
1434 	if (status)
1435 		return status;
1436 
1437 	/* For convenience's sake, we compare raw xdr'd attributes in
1438 	 * nfsd4_proc_verify */
1439 
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;
1445 	verify->ve_attrval = svcxdr_savemem(argp, p, verify->ve_attrlen);
1446 	if (!verify->ve_attrval)
1447 		return nfserr_jukebox;
1448 
1449 	return nfs_ok;
1450 }
1451 
1452 static __be32
1453 nfsd4_decode_write(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
1454 {
1455 	struct nfsd4_write *write = &u->write;
1456 	__be32 status;
1457 
1458 	status = nfsd4_decode_stateid4(argp, &write->wr_stateid);
1459 	if (status)
1460 		return status;
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;
1465 	if (write->wr_stable_how > NFS_FILE_SYNC)
1466 		return nfserr_bad_xdr;
1467 	if (xdr_stream_decode_u32(argp->xdr, &write->wr_buflen) < 0)
1468 		return nfserr_bad_xdr;
1469 	if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
1470 		return nfserr_bad_xdr;
1471 
1472 	write->wr_bytes_written = 0;
1473 	write->wr_how_written = 0;
1474 	memset(&write->wr_verifier, 0, sizeof(write->wr_verifier));
1475 	return nfs_ok;
1476 }
1477 
1478 static __be32
1479 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp,
1480 			       union nfsd4_op_u *u)
1481 {
1482 	struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
1483 	__be32 status;
1484 
1485 	if (argp->minorversion >= 1)
1486 		return nfserr_notsupp;
1487 
1488 	status = nfsd4_decode_state_owner4(argp, &rlockowner->rl_clientid,
1489 					   &rlockowner->rl_owner);
1490 	if (status)
1491 		return status;
1492 
1493 	if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1494 		return nfserr_inval;
1495 
1496 	return nfs_ok;
1497 }
1498 
1499 static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp,
1500 					   union nfsd4_op_u *u)
1501 {
1502 	struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
1503 	memset(bc, 0, sizeof(*bc));
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 
1509 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp,
1510 						union nfsd4_op_u *u)
1511 {
1512 	struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
1513 	u32 use_conn_in_rdma_mode;
1514 	__be32 status;
1515 
1516 	memset(bcts, 0, sizeof(*bcts));
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 
1528 static __be32
1529 nfsd4_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 
1546 /*
1547  * This implementation currently does not support SP4_SSV.
1548  * This decoder simply skips over these arguments.
1549  */
1550 static noinline __be32
1551 nfsd4_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 
1588 static __be32
1589 nfsd4_decode_state_protect4_a(struct nfsd4_compoundargs *argp,
1590 			      struct nfsd4_exchange_id *exid)
1591 {
1592 	__be32 status;
1593 
1594 	if (xdr_stream_decode_u32(argp->xdr, &exid->spa_how) < 0)
1595 		return nfserr_bad_xdr;
1596 	switch (exid->spa_how) {
1597 	case SP4_NONE:
1598 		break;
1599 	case SP4_MACH_CRED:
1600 		status = nfsd4_decode_state_protect_ops(argp, exid);
1601 		if (status)
1602 			return status;
1603 		break;
1604 	case SP4_SSV:
1605 		status = nfsd4_decode_ssv_sp_parms(argp, exid);
1606 		if (status)
1607 			return status;
1608 		break;
1609 	default:
1610 		return nfserr_bad_xdr;
1611 	}
1612 
1613 	return nfs_ok;
1614 }
1615 
1616 static __be32
1617 nfsd4_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 
1652 static __be32
1653 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1654 			 union nfsd4_op_u *u)
1655 {
1656 	struct nfsd4_exchange_id *exid = &u->exchange_id;
1657 	__be32 status;
1658 
1659 	memset(exid, 0, sizeof(*exid));
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;
1671 	return nfsd4_decode_nfs_impl_id4(argp, exid);
1672 }
1673 
1674 static __be32
1675 nfsd4_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 
1706 static __be32
1707 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1708 			    union nfsd4_op_u *u)
1709 {
1710 	struct nfsd4_create_session *sess = &u->create_session;
1711 	__be32 status;
1712 
1713 	memset(sess, 0, sizeof(*sess));
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;
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);
1725 	if (status)
1726 		return status;
1727 	if (xdr_stream_decode_u32(argp->xdr, &sess->callback_prog) < 0)
1728 		return nfserr_bad_xdr;
1729 	return nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1730 }
1731 
1732 static __be32
1733 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1734 			     union nfsd4_op_u *u)
1735 {
1736 	struct nfsd4_destroy_session *destroy_session = &u->destroy_session;
1737 	return nfsd4_decode_sessionid4(argp, &destroy_session->sessionid);
1738 }
1739 
1740 static __be32
1741 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1742 			  union nfsd4_op_u *u)
1743 {
1744 	struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
1745 	return nfsd4_decode_stateid4(argp, &free_stateid->fr_stateid);
1746 }
1747 
1748 static __be32
1749 nfsd4_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 
1777 #ifdef CONFIG_NFSD_PNFS
1778 static __be32
1779 nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1780 		union nfsd4_op_u *u)
1781 {
1782 	struct nfsd4_getdeviceinfo *gdev = &u->getdeviceinfo;
1783 	__be32 status;
1784 
1785 	memset(gdev, 0, sizeof(*gdev));
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;
1798 }
1799 
1800 static __be32
1801 nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1802 			  union nfsd4_op_u *u)
1803 {
1804 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
1805 	__be32 *p, status;
1806 
1807 	memset(lcp, 0, sizeof(*lcp));
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 
1837 static __be32
1838 nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1839 		union nfsd4_op_u *u)
1840 {
1841 	struct nfsd4_layoutget *lgp = &u->layoutget;
1842 	__be32 status;
1843 
1844 	memset(lgp, 0, sizeof(*lgp));
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);
1858 	if (status)
1859 		return status;
1860 	if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_maxcount) < 0)
1861 		return nfserr_bad_xdr;
1862 
1863 	return nfs_ok;
1864 }
1865 
1866 static __be32
1867 nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1868 		union nfsd4_op_u *u)
1869 {
1870 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
1871 	memset(lrp, 0, sizeof(*lrp));
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);
1879 }
1880 #endif /* CONFIG_NFSD_PNFS */
1881 
1882 static __be32 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1883 					   union nfsd4_op_u *u)
1884 {
1885 	struct nfsd4_secinfo_no_name *sin = &u->secinfo_no_name;
1886 	if (xdr_stream_decode_u32(argp->xdr, &sin->sin_style) < 0)
1887 		return nfserr_bad_xdr;
1888 
1889 	sin->sin_exp = NULL;
1890 	return nfs_ok;
1891 }
1892 
1893 static __be32
1894 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1895 		      union nfsd4_op_u *u)
1896 {
1897 	struct nfsd4_sequence *seq = &u->sequence;
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++);
1908 	/* sa_highest_slotid counts from 0 but maxslots  counts from 1 ... */
1909 	seq->maxslots = be32_to_cpup(p++) + 1;
1910 	seq->cachethis = be32_to_cpup(p);
1911 
1912 	seq->status_flags = 0;
1913 	return nfs_ok;
1914 }
1915 
1916 static __be32
1917 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp,
1918 			  union nfsd4_op_u *u)
1919 {
1920 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
1921 	struct nfsd4_test_stateid_id *stateid;
1922 	__be32 status;
1923 	u32 i;
1924 
1925 	memset(test_stateid, 0, sizeof(*test_stateid));
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)
1933 			return nfserr_jukebox;
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 
1944 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp,
1945 					    union nfsd4_op_u *u)
1946 {
1947 	struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
1948 	return nfsd4_decode_clientid4(argp, &dc->clientid);
1949 }
1950 
1951 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp,
1952 					    union nfsd4_op_u *u)
1953 {
1954 	struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
1955 	if (xdr_stream_decode_bool(argp->xdr, &rc->rca_one_fs) < 0)
1956 		return nfserr_bad_xdr;
1957 	return nfs_ok;
1958 }
1959 
1960 static __be32
1961 nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1962 		       union nfsd4_op_u *u)
1963 {
1964 	struct nfsd4_fallocate *fallocate = &u->allocate;
1965 	__be32 status;
1966 
1967 	status = nfsd4_decode_stateid4(argp, &fallocate->falloc_stateid);
1968 	if (status)
1969 		return status;
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;
1974 
1975 	return nfs_ok;
1976 }
1977 
1978 static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1979 				      struct nl4_server *ns)
1980 {
1981 	struct nfs42_netaddr *naddr;
1982 	__be32 *p;
1983 
1984 	if (xdr_stream_decode_u32(argp->xdr, &ns->nl4_type) < 0)
1985 		return nfserr_bad_xdr;
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 
1992 		if (xdr_stream_decode_u32(argp->xdr, &naddr->netid_len) < 0)
1993 			return nfserr_bad_xdr;
1994 		if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1995 			return nfserr_bad_xdr;
1996 
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);
2001 
2002 		if (xdr_stream_decode_u32(argp->xdr, &naddr->addr_len) < 0)
2003 			return nfserr_bad_xdr;
2004 		if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
2005 			return nfserr_bad_xdr;
2006 
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);
2011 		break;
2012 	default:
2013 		return nfserr_bad_xdr;
2014 	}
2015 
2016 	return nfs_ok;
2017 }
2018 
2019 static __be32
2020 nfsd4_decode_copy(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
2021 {
2022 	struct nfsd4_copy *copy = &u->copy;
2023 	u32 consecutive, i, count, sync;
2024 	struct nl4_server *ns_dummy;
2025 	__be32 status;
2026 
2027 	memset(copy, 0, sizeof(*copy));
2028 	status = nfsd4_decode_stateid4(argp, &copy->cp_src_stateid);
2029 	if (status)
2030 		return status;
2031 	status = nfsd4_decode_stateid4(argp, &copy->cp_dst_stateid);
2032 	if (status)
2033 		return status;
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;
2043 	if (xdr_stream_decode_bool(argp->xdr, &sync) < 0)
2044 		return nfserr_bad_xdr;
2045 	nfsd4_copy_set_sync(copy, sync);
2046 
2047 	if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
2048 		return nfserr_bad_xdr;
2049 	copy->cp_src = svcxdr_tmpalloc(argp, sizeof(*copy->cp_src));
2050 	if (copy->cp_src == NULL)
2051 		return nfserr_jukebox;
2052 	if (count == 0) { /* intra-server copy */
2053 		__set_bit(NFSD4_COPY_F_INTRA, &copy->cp_flags);
2054 		return nfs_ok;
2055 	}
2056 
2057 	/* decode all the supplied server addresses but use only the first */
2058 	status = nfsd4_decode_nl4_server(argp, copy->cp_src);
2059 	if (status)
2060 		return status;
2061 
2062 	ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
2063 	if (ns_dummy == NULL)
2064 		return nfserr_jukebox;
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);
2073 
2074 	return nfs_ok;
2075 }
2076 
2077 static __be32
2078 nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
2079 			 union nfsd4_op_u *u)
2080 {
2081 	struct nfsd4_copy_notify *cn = &u->copy_notify;
2082 	__be32 status;
2083 
2084 	memset(cn, 0, sizeof(*cn));
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 
2092 	status = nfsd4_decode_stateid4(argp, &cn->cpn_src_stateid);
2093 	if (status)
2094 		return status;
2095 	return nfsd4_decode_nl4_server(argp, cn->cpn_dst);
2096 }
2097 
2098 static __be32
2099 nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
2100 			    union nfsd4_op_u *u)
2101 {
2102 	struct nfsd4_offload_status *os = &u->offload_status;
2103 	os->count = 0;
2104 	os->status = 0;
2105 	return nfsd4_decode_stateid4(argp, &os->stateid);
2106 }
2107 
2108 static __be32
2109 nfsd4_decode_seek(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
2110 {
2111 	struct nfsd4_seek *seek = &u->seek;
2112 	__be32 status;
2113 
2114 	status = nfsd4_decode_stateid4(argp, &seek->seek_stateid);
2115 	if (status)
2116 		return status;
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;
2121 
2122 	seek->seek_eof = 0;
2123 	seek->seek_pos = 0;
2124 	return nfs_ok;
2125 }
2126 
2127 static __be32
2128 nfsd4_decode_clone(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
2129 {
2130 	struct nfsd4_clone *clone = &u->clone;
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 
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 /*
2159  * Decode data into buffer.
2160  */
2161 static __be32
2162 nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
2163 		       char **bufp, size_t buflen)
2164 {
2165 	struct page **pages = xdr->pages;
2166 	struct kvec *head = xdr->head;
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  */
2206 static __be32
2207 nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2208 {
2209 	char *name, *sp, *dp;
2210 	u32 namelen, cnt;
2211 	__be32 *p;
2212 
2213 	if (xdr_stream_decode_u32(argp->xdr, &namelen) < 0)
2214 		return nfserr_bad_xdr;
2215 	if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2216 		return nfserr_nametoolong;
2217 	if (namelen == 0)
2218 		return nfserr_bad_xdr;
2219 	p = xdr_inline_decode(argp->xdr, namelen);
2220 	if (!p)
2221 		return nfserr_bad_xdr;
2222 	name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2223 	if (!name)
2224 		return nfserr_jukebox;
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')
2237 			return nfserr_bad_xdr;
2238 		*dp++ = *sp++;
2239 	}
2240 	*dp = '\0';
2241 
2242 	*namep = name;
2243 
2244 	return nfs_ok;
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  */
2253 static __be32
2254 nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2255 		      union nfsd4_op_u *u)
2256 {
2257 	struct nfsd4_getxattr *getxattr = &u->getxattr;
2258 	__be32 status;
2259 	u32 maxcount;
2260 
2261 	memset(getxattr, 0, sizeof(*getxattr));
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;
2270 	return nfs_ok;
2271 }
2272 
2273 static __be32
2274 nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2275 		      union nfsd4_op_u *u)
2276 {
2277 	struct nfsd4_setxattr *setxattr = &u->setxattr;
2278 	u32 flags, maxcount, size;
2279 	__be32 status;
2280 
2281 	memset(setxattr, 0, sizeof(*setxattr));
2282 
2283 	if (xdr_stream_decode_u32(argp->xdr, &flags) < 0)
2284 		return nfserr_bad_xdr;
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 
2297 	if (xdr_stream_decode_u32(argp->xdr, &size) < 0)
2298 		return nfserr_bad_xdr;
2299 	if (size > maxcount)
2300 		return nfserr_xattr2big;
2301 
2302 	setxattr->setxa_len = size;
2303 	if (size > 0) {
2304 		struct xdr_buf payload;
2305 
2306 		if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2307 			return nfserr_bad_xdr;
2308 		status = nfsd4_vbuf_from_vector(argp, &payload,
2309 						&setxattr->setxa_buf, size);
2310 	}
2311 
2312 	return nfs_ok;
2313 }
2314 
2315 static __be32
2316 nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2317 			union nfsd4_op_u *u)
2318 {
2319 	struct nfsd4_listxattrs *listxattrs = &u->listxattrs;
2320 	u32 maxcount;
2321 
2322 	memset(listxattrs, 0, sizeof(*listxattrs));
2323 
2324 	if (xdr_stream_decode_u64(argp->xdr, &listxattrs->lsxa_cookie) < 0)
2325 		return nfserr_bad_xdr;
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 
2335 	if (xdr_stream_decode_u32(argp->xdr, &maxcount) < 0)
2336 		return nfserr_bad_xdr;
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 
2344 	return nfs_ok;
2345 }
2346 
2347 static __be32
2348 nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2349 			 union nfsd4_op_u *u)
2350 {
2351 	struct nfsd4_removexattr *removexattr = &u->removexattr;
2352 	memset(removexattr, 0, sizeof(*removexattr));
2353 	return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2354 }
2355 
2356 static __be32
2357 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, union nfsd4_op_u *p)
2358 {
2359 	return nfs_ok;
2360 }
2361 
2362 static __be32
2363 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, union nfsd4_op_u *p)
2364 {
2365 	return nfserr_notsupp;
2366 }
2367 
2368 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u);
2369 
2370 static const nfsd4_dec nfsd4_dec_ops[] = {
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,
2391 	[OP_PUTPUBFH]		= nfsd4_decode_noop,
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,
2408 
2409 	/* new operations for NFSv4.1 */
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,
2416 	[OP_GET_DIR_DELEGATION]	= nfsd4_decode_get_dir_delegation,
2417 #ifdef CONFIG_NFSD_PNFS
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,
2423 #else
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,
2429 #endif
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,
2437 
2438 	/* new operations for NFSv4.2 */
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,
2452 	/* RFC 8276 extended atributes operations */
2453 	[OP_GETXATTR]		= nfsd4_decode_getxattr,
2454 	[OP_SETXATTR]		= nfsd4_decode_setxattr,
2455 	[OP_LISTXATTRS]		= nfsd4_decode_listxattrs,
2456 	[OP_REMOVEXATTR]	= nfsd4_decode_removexattr,
2457 };
2458 
2459 static inline bool
2460 nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2461 {
2462 	if (op->opnum < FIRST_NFS4_OP)
2463 		return false;
2464 	else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
2465 		return false;
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)
2469 		return false;
2470 	return true;
2471 }
2472 
2473 static bool
2474 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2475 {
2476 	struct nfsd4_op *op;
2477 	bool cachethis = false;
2478 	int auth_slack= argp->rqstp->rq_auth_slack;
2479 	int max_reply = auth_slack + 8; /* opcnt, status */
2480 	int readcount = 0;
2481 	int readbytes = 0;
2482 	__be32 *p;
2483 	int i;
2484 
2485 	if (xdr_stream_decode_u32(argp->xdr, &argp->taglen) < 0)
2486 		return false;
2487 	max_reply += XDR_UNIT;
2488 	argp->tag = NULL;
2489 	if (unlikely(argp->taglen)) {
2490 		if (argp->taglen > NFSD4_MAX_TAGLEN)
2491 			return false;
2492 		p = xdr_inline_decode(argp->xdr, argp->taglen);
2493 		if (!p)
2494 			return false;
2495 		argp->tag = svcxdr_savemem(argp, p, argp->taglen);
2496 		if (!argp->tag)
2497 			return false;
2498 		max_reply += xdr_align_size(argp->taglen);
2499 	}
2500 
2501 	if (xdr_stream_decode_u32(argp->xdr, &argp->minorversion) < 0)
2502 		return false;
2503 	if (xdr_stream_decode_u32(argp->xdr, &argp->client_opcnt) < 0)
2504 		return false;
2505 	argp->opcnt = min_t(u32, argp->client_opcnt,
2506 			    NFSD_MAX_OPS_PER_COMPOUND);
2507 
2508 	if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
2509 		argp->ops = vcalloc(argp->opcnt, sizeof(*argp->ops));
2510 		if (!argp->ops) {
2511 			argp->ops = argp->iops;
2512 			return false;
2513 		}
2514 	}
2515 
2516 	if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
2517 		argp->opcnt = 0;
2518 
2519 	for (i = 0; i < argp->opcnt; i++) {
2520 		op = &argp->ops[i];
2521 		op->replay = NULL;
2522 		op->opdesc = NULL;
2523 
2524 		if (xdr_stream_decode_u32(argp->xdr, &op->opnum) < 0)
2525 			return false;
2526 		if (nfsd4_opnum_in_range(argp, op)) {
2527 			op->opdesc = OPDESC(op);
2528 			op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
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 {
2535 			op->opnum = OP_ILLEGAL;
2536 			op->status = nfserr_op_illegal;
2537 		}
2538 
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);
2544 
2545 		if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
2546 			readcount++;
2547 			readbytes += nfsd4_max_reply(argp->rqstp, op);
2548 		} else
2549 			max_reply += nfsd4_max_reply(argp->rqstp, op);
2550 		/*
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.)
2555 		 */
2556 		if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
2557 			max_reply += NFS4_OPAQUE_LIMIT;
2558 
2559 		if (op->status) {
2560 			argp->opcnt = i+1;
2561 			break;
2562 		}
2563 	}
2564 	/* Sessions make the DRC unnecessary: */
2565 	if (argp->minorversion)
2566 		cachethis = false;
2567 	svc_reserve(argp->rqstp, max_reply + readbytes);
2568 	argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
2569 
2570 	argp->splice_ok = nfsd_read_splice_ok(argp->rqstp);
2571 	if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
2572 		argp->splice_ok = false;
2573 
2574 	return true;
2575 }
2576 
2577 static __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 
2583 /* This is a frequently-encoded type; open-coded for speed */
2584 static __be32 nfsd4_encode_nfstime4(struct xdr_stream *xdr,
2585 				    const struct timespec64 *tv)
2586 {
2587 	__be32 *p;
2588 
2589 	p = xdr_reserve_space(xdr, XDR_UNIT * 3);
2590 	if (!p)
2591 		return nfserr_resource;
2592 	p = xdr_encode_hyper(p, tv->tv_sec);
2593 	*p = cpu_to_be32(tv->tv_nsec);
2594 	return nfs_ok;
2595 }
2596 
2597 static __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 
2608 static __be32
2609 nfsd4_encode_change_info4(struct xdr_stream *xdr, const struct nfsd4_change_info *c)
2610 {
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);
2620 }
2621 
2622 static __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 
2635 /* Encode as an array of strings the string given with components
2636  * separated @sep, escaped with esc_enter and esc_exit.
2637  */
2638 static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2639 					  char *components, char esc_enter,
2640 					  char esc_exit)
2641 {
2642 	__be32 *p;
2643 	__be32 pathlen;
2644 	int pathlen_offset;
2645 	int strlen, count=0;
2646 	char *str, *end, *next;
2647 
2648 	dprintk("nfsd4_encode_components(%s)\n", components);
2649 
2650 	pathlen_offset = xdr->buf->len;
2651 	p = xdr_reserve_space(xdr, 4);
2652 	if (!p)
2653 		return nfserr_resource;
2654 	p++; /* We will fill this in with @count later */
2655 
2656 	end = str = components;
2657 	while (*end) {
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 
2675 		strlen = end - str;
2676 		if (strlen) {
2677 			if (xdr_stream_encode_opaque(xdr, str, strlen) < 0)
2678 				return nfserr_resource;
2679 			count++;
2680 		} else
2681 			end++;
2682 		if (found_esc)
2683 			end = next;
2684 
2685 		str = end;
2686 	}
2687 	pathlen = htonl(count);
2688 	write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
2689 	return 0;
2690 }
2691 
2692 /* Encode as an array of strings the string given with components
2693  * separated @sep.
2694  */
2695 static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2696 				      char *components)
2697 {
2698 	return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
2699 }
2700 
2701 static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2702 					struct nfsd4_fs_location *location)
2703 {
2704 	__be32 status;
2705 
2706 	status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
2707 						'[', ']');
2708 	if (status)
2709 		return status;
2710 	status = nfsd4_encode_components(xdr, '/', location->path);
2711 	if (status)
2712 		return status;
2713 	return nfs_ok;
2714 }
2715 
2716 static __be32 nfsd4_encode_pathname4(struct xdr_stream *xdr,
2717 				     const struct path *root,
2718 				     const struct path *path)
2719 {
2720 	struct path cur = *path;
2721 	struct dentry **components = NULL;
2722 	unsigned int ncomponents = 0;
2723 	__be32 err = nfserr_jukebox;
2724 
2725 	dprintk("nfsd4_encode_components(");
2726 
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 (;;) {
2732 		if (path_equal(&cur, root))
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 	}
2751 
2752 	err = nfserr_resource;
2753 	if (xdr_stream_encode_u32(xdr, ncomponents) != XDR_UNIT)
2754 		goto out_free;
2755 	while (ncomponents) {
2756 		struct dentry *dentry = components[ncomponents - 1];
2757 
2758 		spin_lock(&dentry->d_lock);
2759 		if (xdr_stream_encode_opaque(xdr, dentry->d_name.name,
2760 					     dentry->d_name.len) < 0) {
2761 			spin_unlock(&dentry->d_lock);
2762 			goto out_free;
2763 		}
2764 		dprintk("/%pd", dentry);
2765 		spin_unlock(&dentry->d_lock);
2766 		dput(dentry);
2767 		ncomponents--;
2768 	}
2769 
2770 	err = 0;
2771 out_free:
2772 	dprintk(")\n");
2773 	while (ncomponents)
2774 		dput(components[--ncomponents]);
2775 	kfree(components);
2776 	path_put(&cur);
2777 	return err;
2778 }
2779 
2780 static __be32 nfsd4_encode_fs_locations4(struct xdr_stream *xdr,
2781 					 struct svc_rqst *rqstp,
2782 					 struct svc_export *exp)
2783 {
2784 	struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
2785 	struct svc_export *exp_ps;
2786 	unsigned int i;
2787 	__be32 status;
2788 
2789 	/* fs_root */
2790 	exp_ps = rqst_find_fsidzero_export(rqstp);
2791 	if (IS_ERR(exp_ps))
2792 		return nfserrno(PTR_ERR(exp_ps));
2793 	status = nfsd4_encode_pathname4(xdr, &exp_ps->ex_path, &exp->ex_path);
2794 	exp_put(exp_ps);
2795 	if (status != nfs_ok)
2796 		return status;
2797 
2798 	/* locations<> */
2799 	if (xdr_stream_encode_u32(xdr, fslocs->locations_count) != XDR_UNIT)
2800 		return nfserr_resource;
2801 	for (i = 0; i < fslocs->locations_count; i++) {
2802 		status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
2803 		if (status != nfs_ok)
2804 			return status;
2805 	}
2806 
2807 	return nfs_ok;
2808 }
2809 
2810 static __be32 nfsd4_encode_nfsace4(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2811 				   struct nfs4_ace *ace)
2812 {
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 */
2828 	if (ace->whotype != NFS4_ACL_WHO_NAMED)
2829 		return nfs4_acl_write_who(xdr, ace->whotype);
2830 	if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
2831 		return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
2832 	return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
2833 }
2834 
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
2838 #define WORD2_ABSENT_FS_ATTRS 0
2839 
2840 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2841 static inline __be32
2842 nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2843 			    const struct lsm_context *context)
2844 {
2845 	__be32 *p;
2846 
2847 	p = xdr_reserve_space(xdr, context->len + 4 + 4 + 4);
2848 	if (!p)
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 	 */
2855 	*p++ = cpu_to_be32(0); /* lfs */
2856 	*p++ = cpu_to_be32(0); /* pi */
2857 	p = xdr_encode_opaque(p, context->context, context->len);
2858 	return 0;
2859 }
2860 #else
2861 static inline __be32
2862 nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2863 			    struct lsm_context *context)
2864 { return 0; }
2865 #endif
2866 
2867 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
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;
2880 	*bmval2 &= WORD2_ABSENT_FS_ATTRS;
2881 	return 0;
2882 }
2883 
2884 
2885 static int nfsd4_get_mounted_on_ino(struct svc_export *exp, u64 *pino)
2886 {
2887 	struct path path = exp->ex_path;
2888 	struct kstat stat;
2889 	int err;
2890 
2891 	path_get(&path);
2892 	while (follow_up(&path)) {
2893 		if (path.dentry != path.mnt->mnt_root)
2894 			break;
2895 	}
2896 	err = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
2897 	path_put(&path);
2898 	if (!err)
2899 		*pino = stat.ino;
2900 	return err;
2901 }
2902 
2903 static __be32
2904 nfsd4_encode_bitmap4(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2905 {
2906 	__be32 *p;
2907 
2908 	if (bmval2) {
2909 		p = xdr_reserve_space(xdr, XDR_UNIT * 4);
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) {
2917 		p = xdr_reserve_space(xdr, XDR_UNIT * 3);
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 {
2924 		p = xdr_reserve_space(xdr, XDR_UNIT * 2);
2925 		if (!p)
2926 			goto out_resource;
2927 		*p++ = cpu_to_be32(1);
2928 		*p++ = cpu_to_be32(bmval0);
2929 	}
2930 
2931 	return nfs_ok;
2932 out_resource:
2933 	return nfserr_resource;
2934 }
2935 
2936 struct nfsd4_fattr_args {
2937 	struct svc_rqst		*rqstp;
2938 	struct svc_fh		*fhp;
2939 	struct svc_export	*exp;
2940 	struct dentry		*dentry;
2941 	struct kstat		stat;
2942 	struct kstatfs		statfs;
2943 	struct nfs4_acl		*acl;
2944 	u64			change_attr;
2945 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2946 	struct lsm_context	context;
2947 #endif
2948 	u32			rdattr_err;
2949 	bool			contextsupport;
2950 	bool			ignore_crossmnt;
2951 };
2952 
2953 typedef __be32(*nfsd4_enc_attr)(struct xdr_stream *xdr,
2954 				const struct nfsd4_fattr_args *args);
2955 
2956 static __be32 nfsd4_encode_fattr4__noop(struct xdr_stream *xdr,
2957 					const struct nfsd4_fattr_args *args)
2958 {
2959 	return nfs_ok;
2960 }
2961 
2962 static __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 
2968 static __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 
2974 static __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 
2990 static __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 
3028 static __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 
3039 static __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;
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 	}
3053 	return nfsd4_encode_changeid4(xdr, args->change_attr);
3054 }
3055 
3056 static __be32 nfsd4_encode_fattr4_size(struct xdr_stream *xdr,
3057 				       const struct nfsd4_fattr_args *args)
3058 {
3059 	return nfsd4_encode_uint64_t(xdr, args->stat.size);
3060 }
3061 
3062 static __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 
3095 static __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 
3103 static __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 
3109 static __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 
3120 static __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 
3143 static __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 
3149 static __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 
3155 static __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 
3161 static __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 
3167 static __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 
3173 static __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 
3179 static __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 
3187 static __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 
3193 static __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 
3199 static __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 
3205 static __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 
3211 static __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 
3217 static __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 
3223 static __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 
3229 static __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 
3235 static __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 
3242 static __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 
3250 static __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 
3258 static __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 
3266 static __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 
3272 static __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 
3278 static __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 
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  */
3295 static __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 
3305 static __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 
3311 static __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 
3317 static __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 
3334 #ifdef CONFIG_NFSD_PNFS
3335 
3336 static __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 
3354 static __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 
3372 static __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 
3378 #endif
3379 
3380 static __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 
3394 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3395 static __be32 nfsd4_encode_fattr4_sec_label(struct xdr_stream *xdr,
3396 					    const struct nfsd4_fattr_args *args)
3397 {
3398 	return nfsd4_encode_security_label(xdr, args->rqstp, &args->context);
3399 }
3400 #endif
3401 
3402 static __be32 nfsd4_encode_fattr4_xattr_support(struct xdr_stream *xdr,
3403 						const struct nfsd4_fattr_args *args)
3404 {
3405 	int err = xattr_supports_user_prefix(d_inode(args->dentry));
3406 
3407 	return nfsd4_encode_bool(xdr, err == 0);
3408 }
3409 
3410 #define NFSD_OA_SHARE_ACCESS	(BIT(OPEN_ARGS_SHARE_ACCESS_READ)	| \
3411 				 BIT(OPEN_ARGS_SHARE_ACCESS_WRITE)	| \
3412 				 BIT(OPEN_ARGS_SHARE_ACCESS_BOTH))
3413 
3414 #define NFSD_OA_SHARE_DENY	(BIT(OPEN_ARGS_SHARE_DENY_NONE)		| \
3415 				 BIT(OPEN_ARGS_SHARE_DENY_READ)		| \
3416 				 BIT(OPEN_ARGS_SHARE_DENY_WRITE)	| \
3417 				 BIT(OPEN_ARGS_SHARE_DENY_BOTH))
3418 
3419 #define NFSD_OA_SHARE_ACCESS_WANT	(BIT(OPEN_ARGS_SHARE_ACCESS_WANT_ANY_DELEG)		| \
3420 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_NO_DELEG)		| \
3421 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_CANCEL)		| \
3422 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS)	| \
3423 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION))
3424 
3425 #define NFSD_OA_OPEN_CLAIM	(BIT(OPEN_ARGS_OPEN_CLAIM_NULL)		| \
3426 				 BIT(OPEN_ARGS_OPEN_CLAIM_PREVIOUS)	| \
3427 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEGATE_CUR)	| \
3428 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEGATE_PREV)| \
3429 				 BIT(OPEN_ARGS_OPEN_CLAIM_FH)		| \
3430 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEG_CUR_FH)	| \
3431 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEG_PREV_FH))
3432 
3433 #define NFSD_OA_CREATE_MODE	(BIT(OPEN_ARGS_CREATEMODE_UNCHECKED4)	| \
3434 				 BIT(OPEN_ARGS_CREATE_MODE_GUARDED)	| \
3435 				 BIT(OPEN_ARGS_CREATEMODE_EXCLUSIVE4)	| \
3436 				 BIT(OPEN_ARGS_CREATE_MODE_EXCLUSIVE4_1))
3437 
3438 static uint32_t oa_share_access = NFSD_OA_SHARE_ACCESS;
3439 static uint32_t oa_share_deny = NFSD_OA_SHARE_DENY;
3440 static uint32_t oa_share_access_want = NFSD_OA_SHARE_ACCESS_WANT;
3441 static uint32_t oa_open_claim = NFSD_OA_OPEN_CLAIM;
3442 static uint32_t oa_create_mode = NFSD_OA_CREATE_MODE;
3443 
3444 static const struct open_arguments4 nfsd_open_arguments = {
3445 	.oa_share_access = { .count = 1, .element = &oa_share_access },
3446 	.oa_share_deny = { .count = 1, .element = &oa_share_deny },
3447 	.oa_share_access_want = { .count = 1, .element = &oa_share_access_want },
3448 	.oa_open_claim = { .count = 1, .element = &oa_open_claim },
3449 	.oa_create_mode = { .count = 1, .element = &oa_create_mode },
3450 };
3451 
3452 static __be32 nfsd4_encode_fattr4_open_arguments(struct xdr_stream *xdr,
3453 						 const struct nfsd4_fattr_args *args)
3454 {
3455 	if (!xdrgen_encode_fattr4_open_arguments(xdr, &nfsd_open_arguments))
3456 		return nfserr_resource;
3457 	return nfs_ok;
3458 }
3459 
3460 static const nfsd4_enc_attr nfsd4_enc_fattr4_encode_ops[] = {
3461 	[FATTR4_SUPPORTED_ATTRS]	= nfsd4_encode_fattr4_supported_attrs,
3462 	[FATTR4_TYPE]			= nfsd4_encode_fattr4_type,
3463 	[FATTR4_FH_EXPIRE_TYPE]		= nfsd4_encode_fattr4_fh_expire_type,
3464 	[FATTR4_CHANGE]			= nfsd4_encode_fattr4_change,
3465 	[FATTR4_SIZE]			= nfsd4_encode_fattr4_size,
3466 	[FATTR4_LINK_SUPPORT]		= nfsd4_encode_fattr4__true,
3467 	[FATTR4_SYMLINK_SUPPORT]	= nfsd4_encode_fattr4__true,
3468 	[FATTR4_NAMED_ATTR]		= nfsd4_encode_fattr4__false,
3469 	[FATTR4_FSID]			= nfsd4_encode_fattr4_fsid,
3470 	[FATTR4_UNIQUE_HANDLES]		= nfsd4_encode_fattr4__true,
3471 	[FATTR4_LEASE_TIME]		= nfsd4_encode_fattr4_lease_time,
3472 	[FATTR4_RDATTR_ERROR]		= nfsd4_encode_fattr4_rdattr_error,
3473 	[FATTR4_ACL]			= nfsd4_encode_fattr4_acl,
3474 	[FATTR4_ACLSUPPORT]		= nfsd4_encode_fattr4_aclsupport,
3475 	[FATTR4_ARCHIVE]		= nfsd4_encode_fattr4__noop,
3476 	[FATTR4_CANSETTIME]		= nfsd4_encode_fattr4__true,
3477 	[FATTR4_CASE_INSENSITIVE]	= nfsd4_encode_fattr4__false,
3478 	[FATTR4_CASE_PRESERVING]	= nfsd4_encode_fattr4__true,
3479 	[FATTR4_CHOWN_RESTRICTED]	= nfsd4_encode_fattr4__true,
3480 	[FATTR4_FILEHANDLE]		= nfsd4_encode_fattr4_filehandle,
3481 	[FATTR4_FILEID]			= nfsd4_encode_fattr4_fileid,
3482 	[FATTR4_FILES_AVAIL]		= nfsd4_encode_fattr4_files_avail,
3483 	[FATTR4_FILES_FREE]		= nfsd4_encode_fattr4_files_free,
3484 	[FATTR4_FILES_TOTAL]		= nfsd4_encode_fattr4_files_total,
3485 	[FATTR4_FS_LOCATIONS]		= nfsd4_encode_fattr4_fs_locations,
3486 	[FATTR4_HIDDEN]			= nfsd4_encode_fattr4__noop,
3487 	[FATTR4_HOMOGENEOUS]		= nfsd4_encode_fattr4__true,
3488 	[FATTR4_MAXFILESIZE]		= nfsd4_encode_fattr4_maxfilesize,
3489 	[FATTR4_MAXLINK]		= nfsd4_encode_fattr4_maxlink,
3490 	[FATTR4_MAXNAME]		= nfsd4_encode_fattr4_maxname,
3491 	[FATTR4_MAXREAD]		= nfsd4_encode_fattr4_maxread,
3492 	[FATTR4_MAXWRITE]		= nfsd4_encode_fattr4_maxwrite,
3493 	[FATTR4_MIMETYPE]		= nfsd4_encode_fattr4__noop,
3494 	[FATTR4_MODE]			= nfsd4_encode_fattr4_mode,
3495 	[FATTR4_NO_TRUNC]		= nfsd4_encode_fattr4__true,
3496 	[FATTR4_NUMLINKS]		= nfsd4_encode_fattr4_numlinks,
3497 	[FATTR4_OWNER]			= nfsd4_encode_fattr4_owner,
3498 	[FATTR4_OWNER_GROUP]		= nfsd4_encode_fattr4_owner_group,
3499 	[FATTR4_QUOTA_AVAIL_HARD]	= nfsd4_encode_fattr4__noop,
3500 	[FATTR4_QUOTA_AVAIL_SOFT]	= nfsd4_encode_fattr4__noop,
3501 	[FATTR4_QUOTA_USED]		= nfsd4_encode_fattr4__noop,
3502 	[FATTR4_RAWDEV]			= nfsd4_encode_fattr4_rawdev,
3503 	[FATTR4_SPACE_AVAIL]		= nfsd4_encode_fattr4_space_avail,
3504 	[FATTR4_SPACE_FREE]		= nfsd4_encode_fattr4_space_free,
3505 	[FATTR4_SPACE_TOTAL]		= nfsd4_encode_fattr4_space_total,
3506 	[FATTR4_SPACE_USED]		= nfsd4_encode_fattr4_space_used,
3507 	[FATTR4_SYSTEM]			= nfsd4_encode_fattr4__noop,
3508 	[FATTR4_TIME_ACCESS]		= nfsd4_encode_fattr4_time_access,
3509 	[FATTR4_TIME_ACCESS_SET]	= nfsd4_encode_fattr4__noop,
3510 	[FATTR4_TIME_BACKUP]		= nfsd4_encode_fattr4__noop,
3511 	[FATTR4_TIME_CREATE]		= nfsd4_encode_fattr4_time_create,
3512 	[FATTR4_TIME_DELTA]		= nfsd4_encode_fattr4_time_delta,
3513 	[FATTR4_TIME_METADATA]		= nfsd4_encode_fattr4_time_metadata,
3514 	[FATTR4_TIME_MODIFY]		= nfsd4_encode_fattr4_time_modify,
3515 	[FATTR4_TIME_MODIFY_SET]	= nfsd4_encode_fattr4__noop,
3516 	[FATTR4_MOUNTED_ON_FILEID]	= nfsd4_encode_fattr4_mounted_on_fileid,
3517 	[FATTR4_DIR_NOTIF_DELAY]	= nfsd4_encode_fattr4__noop,
3518 	[FATTR4_DIRENT_NOTIF_DELAY]	= nfsd4_encode_fattr4__noop,
3519 	[FATTR4_DACL]			= nfsd4_encode_fattr4__noop,
3520 	[FATTR4_SACL]			= nfsd4_encode_fattr4__noop,
3521 	[FATTR4_CHANGE_POLICY]		= nfsd4_encode_fattr4__noop,
3522 	[FATTR4_FS_STATUS]		= nfsd4_encode_fattr4__noop,
3523 
3524 #ifdef CONFIG_NFSD_PNFS
3525 	[FATTR4_FS_LAYOUT_TYPES]	= nfsd4_encode_fattr4_fs_layout_types,
3526 	[FATTR4_LAYOUT_HINT]		= nfsd4_encode_fattr4__noop,
3527 	[FATTR4_LAYOUT_TYPES]		= nfsd4_encode_fattr4_layout_types,
3528 	[FATTR4_LAYOUT_BLKSIZE]		= nfsd4_encode_fattr4_layout_blksize,
3529 	[FATTR4_LAYOUT_ALIGNMENT]	= nfsd4_encode_fattr4__noop,
3530 #else
3531 	[FATTR4_FS_LAYOUT_TYPES]	= nfsd4_encode_fattr4__noop,
3532 	[FATTR4_LAYOUT_HINT]		= nfsd4_encode_fattr4__noop,
3533 	[FATTR4_LAYOUT_TYPES]		= nfsd4_encode_fattr4__noop,
3534 	[FATTR4_LAYOUT_BLKSIZE]		= nfsd4_encode_fattr4__noop,
3535 	[FATTR4_LAYOUT_ALIGNMENT]	= nfsd4_encode_fattr4__noop,
3536 #endif
3537 
3538 	[FATTR4_FS_LOCATIONS_INFO]	= nfsd4_encode_fattr4__noop,
3539 	[FATTR4_MDSTHRESHOLD]		= nfsd4_encode_fattr4__noop,
3540 	[FATTR4_RETENTION_GET]		= nfsd4_encode_fattr4__noop,
3541 	[FATTR4_RETENTION_SET]		= nfsd4_encode_fattr4__noop,
3542 	[FATTR4_RETENTEVT_GET]		= nfsd4_encode_fattr4__noop,
3543 	[FATTR4_RETENTEVT_SET]		= nfsd4_encode_fattr4__noop,
3544 	[FATTR4_RETENTION_HOLD]		= nfsd4_encode_fattr4__noop,
3545 	[FATTR4_MODE_SET_MASKED]	= nfsd4_encode_fattr4__noop,
3546 	[FATTR4_SUPPATTR_EXCLCREAT]	= nfsd4_encode_fattr4_suppattr_exclcreat,
3547 	[FATTR4_FS_CHARSET_CAP]		= nfsd4_encode_fattr4__noop,
3548 	[FATTR4_CLONE_BLKSIZE]		= nfsd4_encode_fattr4__noop,
3549 	[FATTR4_SPACE_FREED]		= nfsd4_encode_fattr4__noop,
3550 	[FATTR4_CHANGE_ATTR_TYPE]	= nfsd4_encode_fattr4__noop,
3551 
3552 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3553 	[FATTR4_SEC_LABEL]		= nfsd4_encode_fattr4_sec_label,
3554 #else
3555 	[FATTR4_SEC_LABEL]		= nfsd4_encode_fattr4__noop,
3556 #endif
3557 
3558 	[FATTR4_MODE_UMASK]		= nfsd4_encode_fattr4__noop,
3559 	[FATTR4_XATTR_SUPPORT]		= nfsd4_encode_fattr4_xattr_support,
3560 	[FATTR4_OPEN_ARGUMENTS]		= nfsd4_encode_fattr4_open_arguments,
3561 };
3562 
3563 /*
3564  * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
3565  * ourselves.
3566  */
3567 static __be32
3568 nfsd4_encode_fattr4(struct svc_rqst *rqstp, struct xdr_stream *xdr,
3569 		    struct svc_fh *fhp, struct svc_export *exp,
3570 		    struct dentry *dentry, const u32 *bmval,
3571 		    int ignore_crossmnt)
3572 {
3573 	DECLARE_BITMAP(attr_bitmap, ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops));
3574 	struct nfs4_delegation *dp = NULL;
3575 	struct nfsd4_fattr_args args;
3576 	struct svc_fh *tempfh = NULL;
3577 	int starting_len = xdr->buf->len;
3578 	unsigned int attrlen_offset;
3579 	__be32 attrlen, status;
3580 	u32 attrmask[3];
3581 	int err;
3582 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3583 	u32 minorversion = resp->cstate.minorversion;
3584 	struct path path = {
3585 		.mnt	= exp->ex_path.mnt,
3586 		.dentry	= dentry,
3587 	};
3588 	unsigned long bit;
3589 
3590 	WARN_ON_ONCE(bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1);
3591 	WARN_ON_ONCE(!nfsd_attrs_supported(minorversion, bmval));
3592 
3593 	args.rqstp = rqstp;
3594 	args.exp = exp;
3595 	args.dentry = dentry;
3596 	args.ignore_crossmnt = (ignore_crossmnt != 0);
3597 	args.acl = NULL;
3598 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3599 	args.context.context = NULL;
3600 #endif
3601 
3602 	/*
3603 	 * Make a local copy of the attribute bitmap that can be modified.
3604 	 */
3605 	attrmask[0] = bmval[0];
3606 	attrmask[1] = bmval[1];
3607 	attrmask[2] = bmval[2];
3608 
3609 	args.rdattr_err = 0;
3610 	if (exp->ex_fslocs.migrated) {
3611 		status = fattr_handle_absent_fs(&attrmask[0], &attrmask[1],
3612 						&attrmask[2], &args.rdattr_err);
3613 		if (status)
3614 			goto out;
3615 	}
3616 	if ((attrmask[0] & (FATTR4_WORD0_CHANGE |
3617 			    FATTR4_WORD0_SIZE)) ||
3618 	    (attrmask[1] & (FATTR4_WORD1_TIME_ACCESS |
3619 			    FATTR4_WORD1_TIME_MODIFY |
3620 			    FATTR4_WORD1_TIME_METADATA))) {
3621 		status = nfsd4_deleg_getattr_conflict(rqstp, dentry, &dp);
3622 		if (status)
3623 			goto out;
3624 	}
3625 
3626 	err = vfs_getattr(&path, &args.stat,
3627 			  STATX_BASIC_STATS | STATX_BTIME | STATX_CHANGE_COOKIE,
3628 			  AT_STATX_SYNC_AS_STAT);
3629 	if (dp) {
3630 		struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
3631 
3632 		if (ncf->ncf_file_modified) {
3633 			++ncf->ncf_initial_cinfo;
3634 			args.stat.size = ncf->ncf_cur_fsize;
3635 			if (!timespec64_is_epoch(&ncf->ncf_cb_mtime))
3636 				args.stat.mtime = ncf->ncf_cb_mtime;
3637 		}
3638 		args.change_attr = ncf->ncf_initial_cinfo;
3639 
3640 		if (!timespec64_is_epoch(&ncf->ncf_cb_atime))
3641 			args.stat.atime = ncf->ncf_cb_atime;
3642 
3643 		nfs4_put_stid(&dp->dl_stid);
3644 	} else {
3645 		args.change_attr = nfsd4_change_attribute(&args.stat);
3646 	}
3647 
3648 	if (err)
3649 		goto out_nfserr;
3650 
3651 	if (!(args.stat.result_mask & STATX_BTIME))
3652 		/* underlying FS does not offer btime so we can't share it */
3653 		attrmask[1] &= ~FATTR4_WORD1_TIME_CREATE;
3654 	if ((attrmask[0] & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
3655 			FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
3656 	    (attrmask[1] & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
3657 		       FATTR4_WORD1_SPACE_TOTAL))) {
3658 		err = vfs_statfs(&path, &args.statfs);
3659 		if (err)
3660 			goto out_nfserr;
3661 	}
3662 	if ((attrmask[0] & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) &&
3663 	    !fhp) {
3664 		tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
3665 		status = nfserr_jukebox;
3666 		if (!tempfh)
3667 			goto out;
3668 		fh_init(tempfh, NFS4_FHSIZE);
3669 		status = fh_compose(tempfh, exp, dentry, NULL);
3670 		if (status)
3671 			goto out;
3672 		args.fhp = tempfh;
3673 	} else
3674 		args.fhp = fhp;
3675 
3676 	if (attrmask[0] & FATTR4_WORD0_ACL) {
3677 		err = nfsd4_get_nfs4_acl(rqstp, dentry, &args.acl);
3678 		if (err == -EOPNOTSUPP)
3679 			attrmask[0] &= ~FATTR4_WORD0_ACL;
3680 		else if (err == -EINVAL) {
3681 			status = nfserr_attrnotsupp;
3682 			goto out;
3683 		} else if (err != 0)
3684 			goto out_nfserr;
3685 	}
3686 
3687 	args.contextsupport = false;
3688 
3689 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3690 	if ((attrmask[2] & FATTR4_WORD2_SECURITY_LABEL) ||
3691 	     attrmask[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
3692 		if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
3693 			err = security_inode_getsecctx(d_inode(dentry),
3694 						&args.context);
3695 		else
3696 			err = -EOPNOTSUPP;
3697 		args.contextsupport = (err == 0);
3698 		if (attrmask[2] & FATTR4_WORD2_SECURITY_LABEL) {
3699 			if (err == -EOPNOTSUPP)
3700 				attrmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3701 			else if (err)
3702 				goto out_nfserr;
3703 		}
3704 	}
3705 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
3706 
3707 	/* attrmask */
3708 	status = nfsd4_encode_bitmap4(xdr, attrmask[0], attrmask[1],
3709 				      attrmask[2]);
3710 	if (status)
3711 		goto out;
3712 
3713 	/* attr_vals */
3714 	attrlen_offset = xdr->buf->len;
3715 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
3716 		goto out_resource;
3717 	bitmap_from_arr32(attr_bitmap, attrmask,
3718 			  ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops));
3719 	for_each_set_bit(bit, attr_bitmap,
3720 			 ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops)) {
3721 		status = nfsd4_enc_fattr4_encode_ops[bit](xdr, &args);
3722 		if (status != nfs_ok)
3723 			goto out;
3724 	}
3725 	attrlen = cpu_to_be32(xdr->buf->len - attrlen_offset - XDR_UNIT);
3726 	write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, XDR_UNIT);
3727 	status = nfs_ok;
3728 
3729 out:
3730 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3731 	if (args.context.context)
3732 		security_release_secctx(&args.context);
3733 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
3734 	kfree(args.acl);
3735 	if (tempfh) {
3736 		fh_put(tempfh);
3737 		kfree(tempfh);
3738 	}
3739 	if (status)
3740 		xdr_truncate_encode(xdr, starting_len);
3741 	return status;
3742 out_nfserr:
3743 	status = nfserrno(err);
3744 	goto out;
3745 out_resource:
3746 	status = nfserr_resource;
3747 	goto out;
3748 }
3749 
3750 static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3751 				struct xdr_buf *buf, __be32 *p, int bytes)
3752 {
3753 	xdr->scratch.iov_len = 0;
3754 	memset(buf, 0, sizeof(struct xdr_buf));
3755 	buf->head[0].iov_base = p;
3756 	buf->head[0].iov_len = 0;
3757 	buf->len = 0;
3758 	xdr->buf = buf;
3759 	xdr->iov = buf->head;
3760 	xdr->p = p;
3761 	xdr->end = (void *)p + bytes;
3762 	buf->buflen = bytes;
3763 }
3764 
3765 __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3766 			struct svc_fh *fhp, struct svc_export *exp,
3767 			struct dentry *dentry, u32 *bmval,
3768 			struct svc_rqst *rqstp, int ignore_crossmnt)
3769 {
3770 	struct xdr_buf dummy;
3771 	struct xdr_stream xdr;
3772 	__be32 ret;
3773 
3774 	svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
3775 	ret = nfsd4_encode_fattr4(rqstp, &xdr, fhp, exp, dentry, bmval,
3776 				  ignore_crossmnt);
3777 	*p = xdr.p;
3778 	return ret;
3779 }
3780 
3781 /*
3782  * The buffer space for this field was reserved during a previous
3783  * call to nfsd4_encode_entry4().
3784  */
3785 static void nfsd4_encode_entry4_nfs_cookie4(const struct nfsd4_readdir *readdir,
3786 					    u64 offset)
3787 {
3788 	__be64 cookie = cpu_to_be64(offset);
3789 	struct xdr_stream *xdr = readdir->xdr;
3790 
3791 	if (!readdir->cookie_offset)
3792 		return;
3793 	write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset, &cookie,
3794 			       sizeof(cookie));
3795 }
3796 
3797 static inline int attributes_need_mount(u32 *bmval)
3798 {
3799 	if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3800 		return 1;
3801 	if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3802 		return 1;
3803 	return 0;
3804 }
3805 
3806 static __be32
3807 nfsd4_encode_entry4_fattr(struct nfsd4_readdir *cd, const char *name,
3808 			  int namlen)
3809 {
3810 	struct svc_export *exp = cd->rd_fhp->fh_export;
3811 	struct dentry *dentry;
3812 	__be32 nfserr;
3813 	int ignore_crossmnt = 0;
3814 
3815 	dentry = lookup_one_positive_unlocked(&nop_mnt_idmap,
3816 					      &QSTR_LEN(name, namlen),
3817 					      cd->rd_fhp->fh_dentry);
3818 	if (IS_ERR(dentry))
3819 		return nfserrno(PTR_ERR(dentry));
3820 
3821 	exp_get(exp);
3822 	/*
3823 	 * In the case of a mountpoint, the client may be asking for
3824 	 * attributes that are only properties of the underlying filesystem
3825 	 * as opposed to the cross-mounted file system. In such a case,
3826 	 * we will not follow the cross mount and will fill the attribtutes
3827 	 * directly from the mountpoint dentry.
3828 	 */
3829 	if (nfsd_mountpoint(dentry, exp)) {
3830 		int err;
3831 
3832 		if (!(exp->ex_flags & NFSEXP_V4ROOT)
3833 				&& !attributes_need_mount(cd->rd_bmval)) {
3834 			ignore_crossmnt = 1;
3835 			goto out_encode;
3836 		}
3837 		/*
3838 		 * Why the heck aren't we just using nfsd_lookup??
3839 		 * Different "."/".." handling?  Something else?
3840 		 * At least, add a comment here to explain....
3841 		 */
3842 		err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3843 		if (err) {
3844 			nfserr = nfserrno(err);
3845 			goto out_put;
3846 		}
3847 		nfserr = check_nfsd_access(exp, cd->rd_rqstp, false);
3848 		if (nfserr)
3849 			goto out_put;
3850 
3851 	}
3852 out_encode:
3853 	nfserr = nfsd4_encode_fattr4(cd->rd_rqstp, cd->xdr, NULL, exp, dentry,
3854 				     cd->rd_bmval, ignore_crossmnt);
3855 out_put:
3856 	dput(dentry);
3857 	exp_put(exp);
3858 	return nfserr;
3859 }
3860 
3861 static __be32
3862 nfsd4_encode_entry4_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
3863 {
3864 	__be32 status;
3865 
3866 	/* attrmask */
3867 	status = nfsd4_encode_bitmap4(xdr, FATTR4_WORD0_RDATTR_ERROR, 0, 0);
3868 	if (status != nfs_ok)
3869 		return status;
3870 	/* attr_vals */
3871 	if (xdr_stream_encode_u32(xdr, XDR_UNIT) != XDR_UNIT)
3872 		return nfserr_resource;
3873 	/* rdattr_error */
3874 	if (xdr_stream_encode_be32(xdr, nfserr) != XDR_UNIT)
3875 		return nfserr_resource;
3876 	return nfs_ok;
3877 }
3878 
3879 static int
3880 nfsd4_encode_entry4(void *ccdv, const char *name, int namlen,
3881 		    loff_t offset, u64 ino, unsigned int d_type)
3882 {
3883 	struct readdir_cd *ccd = ccdv;
3884 	struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
3885 	struct xdr_stream *xdr = cd->xdr;
3886 	int start_offset = xdr->buf->len;
3887 	int cookie_offset;
3888 	u32 name_and_cookie;
3889 	int entry_bytes;
3890 	__be32 nfserr = nfserr_toosmall;
3891 
3892 	/* In nfsv4, "." and ".." never make it onto the wire.. */
3893 	if (name && isdotent(name, namlen)) {
3894 		cd->common.err = nfs_ok;
3895 		return 0;
3896 	}
3897 
3898 	/* Encode the previous entry's cookie value */
3899 	nfsd4_encode_entry4_nfs_cookie4(cd, offset);
3900 
3901 	if (xdr_stream_encode_item_present(xdr) != XDR_UNIT)
3902 		goto fail;
3903 
3904 	/* Reserve send buffer space for this entry's cookie value. */
3905 	cookie_offset = xdr->buf->len;
3906 	if (nfsd4_encode_nfs_cookie4(xdr, OFFSET_MAX) != nfs_ok)
3907 		goto fail;
3908 	if (nfsd4_encode_component4(xdr, name, namlen) != nfs_ok)
3909 		goto fail;
3910 	nfserr = nfsd4_encode_entry4_fattr(cd, name, namlen);
3911 	switch (nfserr) {
3912 	case nfs_ok:
3913 		break;
3914 	case nfserr_resource:
3915 		nfserr = nfserr_toosmall;
3916 		goto fail;
3917 	case nfserr_noent:
3918 		xdr_truncate_encode(xdr, start_offset);
3919 		goto skip_entry;
3920 	case nfserr_jukebox:
3921 		/*
3922 		 * The pseudoroot should only display dentries that lead to
3923 		 * exports. If we get EJUKEBOX here, then we can't tell whether
3924 		 * this entry should be included. Just fail the whole READDIR
3925 		 * with NFS4ERR_DELAY in that case, and hope that the situation
3926 		 * will resolve itself by the client's next attempt.
3927 		 */
3928 		if (cd->rd_fhp->fh_export->ex_flags & NFSEXP_V4ROOT)
3929 			goto fail;
3930 		fallthrough;
3931 	default:
3932 		/*
3933 		 * If the client requested the RDATTR_ERROR attribute,
3934 		 * we stuff the error code into this attribute
3935 		 * and continue.  If this attribute was not requested,
3936 		 * then in accordance with the spec, we fail the
3937 		 * entire READDIR operation(!)
3938 		 */
3939 		if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3940 			goto fail;
3941 		if (nfsd4_encode_entry4_rdattr_error(xdr, nfserr)) {
3942 			nfserr = nfserr_toosmall;
3943 			goto fail;
3944 		}
3945 	}
3946 	nfserr = nfserr_toosmall;
3947 	entry_bytes = xdr->buf->len - start_offset;
3948 	if (entry_bytes > cd->rd_maxcount)
3949 		goto fail;
3950 	cd->rd_maxcount -= entry_bytes;
3951 	/*
3952 	 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", and
3953 	 * notes that it could be zero. If it is zero, then the server
3954 	 * should enforce only the rd_maxcount value.
3955 	 */
3956 	if (cd->rd_dircount) {
3957 		name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
3958 		if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3959 			goto fail;
3960 		cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
3961 		if (!cd->rd_dircount)
3962 			cd->rd_maxcount = 0;
3963 	}
3964 
3965 	cd->cookie_offset = cookie_offset;
3966 skip_entry:
3967 	cd->common.err = nfs_ok;
3968 	return 0;
3969 fail:
3970 	xdr_truncate_encode(xdr, start_offset);
3971 	cd->common.err = nfserr;
3972 	return -EINVAL;
3973 }
3974 
3975 static __be32
3976 nfsd4_encode_verifier4(struct xdr_stream *xdr, const nfs4_verifier *verf)
3977 {
3978 	__be32 *p;
3979 
3980 	p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3981 	if (!p)
3982 		return nfserr_resource;
3983 	memcpy(p, verf->data, sizeof(verf->data));
3984 	return nfs_ok;
3985 }
3986 
3987 static __be32
3988 nfsd4_encode_clientid4(struct xdr_stream *xdr, const clientid_t *clientid)
3989 {
3990 	__be32 *p;
3991 
3992 	p = xdr_reserve_space(xdr, sizeof(__be64));
3993 	if (!p)
3994 		return nfserr_resource;
3995 	memcpy(p, clientid, sizeof(*clientid));
3996 	return nfs_ok;
3997 }
3998 
3999 /* This is a frequently-encoded item; open-coded for speed */
4000 static __be32
4001 nfsd4_encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
4002 {
4003 	__be32 *p;
4004 
4005 	p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
4006 	if (!p)
4007 		return nfserr_resource;
4008 	*p++ = cpu_to_be32(sid->si_generation);
4009 	memcpy(p, &sid->si_opaque, sizeof(sid->si_opaque));
4010 	return nfs_ok;
4011 }
4012 
4013 static __be32
4014 nfsd4_encode_sessionid4(struct xdr_stream *xdr,
4015 			const struct nfs4_sessionid *sessionid)
4016 {
4017 	return nfsd4_encode_opaque_fixed(xdr, sessionid->data,
4018 					 NFS4_MAX_SESSIONID_LEN);
4019 }
4020 
4021 static __be32
4022 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr,
4023 		    union nfsd4_op_u *u)
4024 {
4025 	struct nfsd4_access *access = &u->access;
4026 	struct xdr_stream *xdr = resp->xdr;
4027 	__be32 status;
4028 
4029 	/* supported */
4030 	status = nfsd4_encode_uint32_t(xdr, access->ac_supported);
4031 	if (status != nfs_ok)
4032 		return status;
4033 	/* access */
4034 	return nfsd4_encode_uint32_t(xdr, access->ac_resp_access);
4035 }
4036 
4037 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr,
4038 						union nfsd4_op_u *u)
4039 {
4040 	struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
4041 	struct xdr_stream *xdr = resp->xdr;
4042 
4043 	/* bctsr_sessid */
4044 	nfserr = nfsd4_encode_sessionid4(xdr, &bcts->sessionid);
4045 	if (nfserr != nfs_ok)
4046 		return nfserr;
4047 	/* bctsr_dir */
4048 	if (xdr_stream_encode_u32(xdr, bcts->dir) != XDR_UNIT)
4049 		return nfserr_resource;
4050 	/* bctsr_use_conn_in_rdma_mode */
4051 	return nfsd4_encode_bool(xdr, false);
4052 }
4053 
4054 static __be32
4055 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr,
4056 		   union nfsd4_op_u *u)
4057 {
4058 	struct nfsd4_close *close = &u->close;
4059 	struct xdr_stream *xdr = resp->xdr;
4060 
4061 	/* open_stateid */
4062 	return nfsd4_encode_stateid4(xdr, &close->cl_stateid);
4063 }
4064 
4065 
4066 static __be32
4067 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr,
4068 		    union nfsd4_op_u *u)
4069 {
4070 	struct nfsd4_commit *commit = &u->commit;
4071 
4072 	return nfsd4_encode_verifier4(resp->xdr, &commit->co_verf);
4073 }
4074 
4075 static __be32
4076 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr,
4077 		    union nfsd4_op_u *u)
4078 {
4079 	struct nfsd4_create *create = &u->create;
4080 	struct xdr_stream *xdr = resp->xdr;
4081 
4082 	/* cinfo */
4083 	nfserr = nfsd4_encode_change_info4(xdr, &create->cr_cinfo);
4084 	if (nfserr)
4085 		return nfserr;
4086 	/* attrset */
4087 	return nfsd4_encode_bitmap4(xdr, create->cr_bmval[0],
4088 				    create->cr_bmval[1], create->cr_bmval[2]);
4089 }
4090 
4091 static __be32
4092 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4093 		     union nfsd4_op_u *u)
4094 {
4095 	struct nfsd4_getattr *getattr = &u->getattr;
4096 	struct svc_fh *fhp = getattr->ga_fhp;
4097 	struct xdr_stream *xdr = resp->xdr;
4098 
4099 	/* obj_attributes */
4100 	return nfsd4_encode_fattr4(resp->rqstp, xdr, fhp, fhp->fh_export,
4101 				   fhp->fh_dentry, getattr->ga_bmval, 0);
4102 }
4103 
4104 static __be32
4105 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr,
4106 		   union nfsd4_op_u *u)
4107 {
4108 	struct xdr_stream *xdr = resp->xdr;
4109 	struct svc_fh *fhp = u->getfh;
4110 
4111 	/* object */
4112 	return nfsd4_encode_nfs_fh4(xdr, &fhp->fh_handle);
4113 }
4114 
4115 static __be32
4116 nfsd4_encode_lock_owner4(struct xdr_stream *xdr, const clientid_t *clientid,
4117 			 const struct xdr_netobj *owner)
4118 {
4119 	__be32 status;
4120 
4121 	/* clientid */
4122 	status = nfsd4_encode_clientid4(xdr, clientid);
4123 	if (status != nfs_ok)
4124 		return status;
4125 	/* owner */
4126 	return nfsd4_encode_opaque(xdr, owner->data, owner->len);
4127 }
4128 
4129 static __be32
4130 nfsd4_encode_lock4denied(struct xdr_stream *xdr,
4131 			 const struct nfsd4_lock_denied *ld)
4132 {
4133 	__be32 status;
4134 
4135 	/* offset */
4136 	status = nfsd4_encode_offset4(xdr, ld->ld_start);
4137 	if (status != nfs_ok)
4138 		return status;
4139 	/* length */
4140 	status = nfsd4_encode_length4(xdr, ld->ld_length);
4141 	if (status != nfs_ok)
4142 		return status;
4143 	/* locktype */
4144 	if (xdr_stream_encode_u32(xdr, ld->ld_type) != XDR_UNIT)
4145 		return nfserr_resource;
4146 	/* owner */
4147 	return nfsd4_encode_lock_owner4(xdr, &ld->ld_clientid,
4148 					&ld->ld_owner);
4149 }
4150 
4151 static __be32
4152 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr,
4153 		  union nfsd4_op_u *u)
4154 {
4155 	struct nfsd4_lock *lock = &u->lock;
4156 	struct xdr_stream *xdr = resp->xdr;
4157 	__be32 status;
4158 
4159 	switch (nfserr) {
4160 	case nfs_ok:
4161 		/* resok4 */
4162 		status = nfsd4_encode_stateid4(xdr, &lock->lk_resp_stateid);
4163 		break;
4164 	case nfserr_denied:
4165 		/* denied */
4166 		status = nfsd4_encode_lock4denied(xdr, &lock->lk_denied);
4167 		break;
4168 	default:
4169 		return nfserr;
4170 	}
4171 	return status != nfs_ok ? status : nfserr;
4172 }
4173 
4174 static __be32
4175 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr,
4176 		   union nfsd4_op_u *u)
4177 {
4178 	struct nfsd4_lockt *lockt = &u->lockt;
4179 	struct xdr_stream *xdr = resp->xdr;
4180 	__be32 status;
4181 
4182 	if (nfserr == nfserr_denied) {
4183 		/* denied */
4184 		status = nfsd4_encode_lock4denied(xdr, &lockt->lt_denied);
4185 		if (status != nfs_ok)
4186 			return status;
4187 	}
4188 	return nfserr;
4189 }
4190 
4191 static __be32
4192 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr,
4193 		   union nfsd4_op_u *u)
4194 {
4195 	struct nfsd4_locku *locku = &u->locku;
4196 	struct xdr_stream *xdr = resp->xdr;
4197 
4198 	/* lock_stateid */
4199 	return nfsd4_encode_stateid4(xdr, &locku->lu_stateid);
4200 }
4201 
4202 
4203 static __be32
4204 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr,
4205 		  union nfsd4_op_u *u)
4206 {
4207 	struct nfsd4_link *link = &u->link;
4208 	struct xdr_stream *xdr = resp->xdr;
4209 
4210 	return nfsd4_encode_change_info4(xdr, &link->li_cinfo);
4211 }
4212 
4213 /*
4214  * This implementation does not yet support returning an ACE in an
4215  * OPEN that offers a delegation.
4216  */
4217 static __be32
4218 nfsd4_encode_open_nfsace4(struct xdr_stream *xdr)
4219 {
4220 	__be32 status;
4221 
4222 	/* type */
4223 	status = nfsd4_encode_acetype4(xdr, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
4224 	if (status != nfs_ok)
4225 		return nfserr_resource;
4226 	/* flag */
4227 	status = nfsd4_encode_aceflag4(xdr, 0);
4228 	if (status != nfs_ok)
4229 		return nfserr_resource;
4230 	/* access mask */
4231 	status = nfsd4_encode_acemask4(xdr, 0);
4232 	if (status != nfs_ok)
4233 		return nfserr_resource;
4234 	/* who - empty for now */
4235 	if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
4236 		return nfserr_resource;
4237 	return nfs_ok;
4238 }
4239 
4240 static __be32
4241 nfsd4_encode_open_read_delegation4(struct xdr_stream *xdr, struct nfsd4_open *open)
4242 {
4243 	__be32 status;
4244 
4245 	/* stateid */
4246 	status = nfsd4_encode_stateid4(xdr, &open->op_delegate_stateid);
4247 	if (status != nfs_ok)
4248 		return status;
4249 	/* recall */
4250 	status = nfsd4_encode_bool(xdr, open->op_recall);
4251 	if (status != nfs_ok)
4252 		return status;
4253 	/* permissions */
4254 	return nfsd4_encode_open_nfsace4(xdr);
4255 }
4256 
4257 static __be32
4258 nfsd4_encode_nfs_space_limit4(struct xdr_stream *xdr, u64 filesize)
4259 {
4260 	/* limitby */
4261 	if (xdr_stream_encode_u32(xdr, NFS4_LIMIT_SIZE) != XDR_UNIT)
4262 		return nfserr_resource;
4263 	/* filesize */
4264 	return nfsd4_encode_uint64_t(xdr, filesize);
4265 }
4266 
4267 static __be32
4268 nfsd4_encode_open_write_delegation4(struct xdr_stream *xdr,
4269 				    struct nfsd4_open *open)
4270 {
4271 	__be32 status;
4272 
4273 	/* stateid */
4274 	status = nfsd4_encode_stateid4(xdr, &open->op_delegate_stateid);
4275 	if (status != nfs_ok)
4276 		return status;
4277 	/* recall */
4278 	status = nfsd4_encode_bool(xdr, open->op_recall);
4279 	if (status != nfs_ok)
4280 		return status;
4281 	/* space_limit */
4282 	status = nfsd4_encode_nfs_space_limit4(xdr, 0);
4283 	if (status != nfs_ok)
4284 		return status;
4285 	return nfsd4_encode_open_nfsace4(xdr);
4286 }
4287 
4288 static __be32
4289 nfsd4_encode_open_none_delegation4(struct xdr_stream *xdr,
4290 				   struct nfsd4_open *open)
4291 {
4292 	__be32 status = nfs_ok;
4293 
4294 	/* ond_why */
4295 	if (xdr_stream_encode_u32(xdr, open->op_why_no_deleg) != XDR_UNIT)
4296 		return nfserr_resource;
4297 	switch (open->op_why_no_deleg) {
4298 	case WND4_CONTENTION:
4299 		/* ond_server_will_push_deleg */
4300 		status = nfsd4_encode_bool(xdr, false);
4301 		break;
4302 	case WND4_RESOURCE:
4303 		/* ond_server_will_signal_avail */
4304 		status = nfsd4_encode_bool(xdr, false);
4305 	}
4306 	return status;
4307 }
4308 
4309 static __be32
4310 nfsd4_encode_open_delegation4(struct xdr_stream *xdr, struct nfsd4_open *open)
4311 {
4312 	__be32 status;
4313 
4314 	/* delegation_type */
4315 	if (xdr_stream_encode_u32(xdr, open->op_delegate_type) != XDR_UNIT)
4316 		return nfserr_resource;
4317 	switch (open->op_delegate_type) {
4318 	case OPEN_DELEGATE_NONE:
4319 		status = nfs_ok;
4320 		break;
4321 	case OPEN_DELEGATE_READ:
4322 	case OPEN_DELEGATE_READ_ATTRS_DELEG:
4323 		/* read */
4324 		status = nfsd4_encode_open_read_delegation4(xdr, open);
4325 		break;
4326 	case OPEN_DELEGATE_WRITE:
4327 	case OPEN_DELEGATE_WRITE_ATTRS_DELEG:
4328 		/* write */
4329 		status = nfsd4_encode_open_write_delegation4(xdr, open);
4330 		break;
4331 	case OPEN_DELEGATE_NONE_EXT:
4332 		/* od_whynone */
4333 		status = nfsd4_encode_open_none_delegation4(xdr, open);
4334 		break;
4335 	default:
4336 		status = nfserr_serverfault;
4337 	}
4338 
4339 	return status;
4340 }
4341 
4342 static __be32
4343 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr,
4344 		  union nfsd4_op_u *u)
4345 {
4346 	struct nfsd4_open *open = &u->open;
4347 	struct xdr_stream *xdr = resp->xdr;
4348 
4349 	/* stateid */
4350 	nfserr = nfsd4_encode_stateid4(xdr, &open->op_stateid);
4351 	if (nfserr != nfs_ok)
4352 		return nfserr;
4353 	/* cinfo */
4354 	nfserr = nfsd4_encode_change_info4(xdr, &open->op_cinfo);
4355 	if (nfserr != nfs_ok)
4356 		return nfserr;
4357 	/* rflags */
4358 	nfserr = nfsd4_encode_uint32_t(xdr, open->op_rflags);
4359 	if (nfserr != nfs_ok)
4360 		return nfserr;
4361 	/* attrset */
4362 	nfserr = nfsd4_encode_bitmap4(xdr, open->op_bmval[0],
4363 				      open->op_bmval[1], open->op_bmval[2]);
4364 	if (nfserr != nfs_ok)
4365 		return nfserr;
4366 	/* delegation */
4367 	return nfsd4_encode_open_delegation4(xdr, open);
4368 }
4369 
4370 static __be32
4371 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr,
4372 			  union nfsd4_op_u *u)
4373 {
4374 	struct nfsd4_open_confirm *oc = &u->open_confirm;
4375 	struct xdr_stream *xdr = resp->xdr;
4376 
4377 	/* open_stateid */
4378 	return nfsd4_encode_stateid4(xdr, &oc->oc_resp_stateid);
4379 }
4380 
4381 static __be32
4382 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr,
4383 			    union nfsd4_op_u *u)
4384 {
4385 	struct nfsd4_open_downgrade *od = &u->open_downgrade;
4386 	struct xdr_stream *xdr = resp->xdr;
4387 
4388 	/* open_stateid */
4389 	return nfsd4_encode_stateid4(xdr, &od->od_stateid);
4390 }
4391 
4392 /*
4393  * The operation of this function assumes that this is the only
4394  * READ operation in the COMPOUND. If there are multiple READs,
4395  * we use nfsd4_encode_readv().
4396  */
4397 static __be32 nfsd4_encode_splice_read(
4398 				struct nfsd4_compoundres *resp,
4399 				struct nfsd4_read *read,
4400 				struct file *file, unsigned long maxcount)
4401 {
4402 	struct xdr_stream *xdr = resp->xdr;
4403 	struct xdr_buf *buf = xdr->buf;
4404 	int status, space_left;
4405 	__be32 nfserr;
4406 
4407 	/*
4408 	 * Splice read doesn't work if encoding has already wandered
4409 	 * into the XDR buf's page array.
4410 	 */
4411 	if (unlikely(xdr->buf->page_len)) {
4412 		WARN_ON_ONCE(1);
4413 		return nfserr_serverfault;
4414 	}
4415 
4416 	/*
4417 	 * Make sure there is room at the end of buf->head for
4418 	 * svcxdr_encode_opaque_pages() to create a tail buffer
4419 	 * to XDR-pad the payload.
4420 	 */
4421 	if (xdr->iov != xdr->buf->head || xdr->end - xdr->p < 1)
4422 		return nfserr_resource;
4423 
4424 	nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
4425 				  file, read->rd_offset, &maxcount,
4426 				  &read->rd_eof);
4427 	read->rd_length = maxcount;
4428 	if (nfserr)
4429 		goto out_err;
4430 	svcxdr_encode_opaque_pages(read->rd_rqstp, xdr, buf->pages,
4431 				   buf->page_base, maxcount);
4432 	status = svc_encode_result_payload(read->rd_rqstp,
4433 					   buf->head[0].iov_len, maxcount);
4434 	if (status) {
4435 		nfserr = nfserrno(status);
4436 		goto out_err;
4437 	}
4438 
4439 	/*
4440 	 * Prepare to encode subsequent operations.
4441 	 *
4442 	 * xdr_truncate_encode() is not safe to use after a successful
4443 	 * splice read has been done, so the following stream
4444 	 * manipulations are open-coded.
4445 	 */
4446 	space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
4447 				buf->buflen - buf->len);
4448 	buf->buflen = buf->len + space_left;
4449 	xdr->end = (__be32 *)((void *)xdr->end + space_left);
4450 
4451 	return nfs_ok;
4452 
4453 out_err:
4454 	/*
4455 	 * nfsd_splice_actor may have already messed with the
4456 	 * page length; reset it so as not to confuse
4457 	 * xdr_truncate_encode in our caller.
4458 	 */
4459 	buf->page_len = 0;
4460 	return nfserr;
4461 }
4462 
4463 static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
4464 				 struct nfsd4_read *read,
4465 				 struct file *file, unsigned long maxcount)
4466 {
4467 	struct xdr_stream *xdr = resp->xdr;
4468 	unsigned int base = xdr->buf->page_len & ~PAGE_MASK;
4469 	unsigned int starting_len = xdr->buf->len;
4470 	__be32 zero = xdr_zero;
4471 	__be32 nfserr;
4472 
4473 	if (xdr_reserve_space_vec(xdr, maxcount) < 0)
4474 		return nfserr_resource;
4475 
4476 	nfserr = nfsd_iter_read(resp->rqstp, read->rd_fhp, file,
4477 				read->rd_offset, &maxcount, base,
4478 				&read->rd_eof);
4479 	read->rd_length = maxcount;
4480 	if (nfserr)
4481 		return nfserr;
4482 	if (svc_encode_result_payload(resp->rqstp, starting_len, maxcount))
4483 		return nfserr_io;
4484 	xdr_truncate_encode(xdr, starting_len + xdr_align_size(maxcount));
4485 
4486 	write_bytes_to_xdr_buf(xdr->buf, starting_len + maxcount, &zero,
4487 			       xdr_pad_size(maxcount));
4488 	return nfs_ok;
4489 }
4490 
4491 static __be32
4492 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
4493 		  union nfsd4_op_u *u)
4494 {
4495 	struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
4496 	struct nfsd4_read *read = &u->read;
4497 	struct xdr_stream *xdr = resp->xdr;
4498 	bool splice_ok = argp->splice_ok;
4499 	unsigned int eof_offset;
4500 	unsigned long maxcount;
4501 	__be32 wire_data[2];
4502 	struct file *file;
4503 
4504 	if (nfserr)
4505 		return nfserr;
4506 
4507 	eof_offset = xdr->buf->len;
4508 	file = read->rd_nf->nf_file;
4509 
4510 	/* Reserve space for the eof flag and byte count */
4511 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2))) {
4512 		WARN_ON_ONCE(splice_ok);
4513 		return nfserr_resource;
4514 	}
4515 	xdr_commit_encode(xdr);
4516 
4517 	maxcount = min_t(unsigned long, read->rd_length,
4518 			 (xdr->buf->buflen - xdr->buf->len));
4519 
4520 	if (file->f_op->splice_read && splice_ok)
4521 		nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
4522 	else
4523 		nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
4524 	if (nfserr) {
4525 		xdr_truncate_encode(xdr, eof_offset);
4526 		return nfserr;
4527 	}
4528 
4529 	wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
4530 	wire_data[1] = cpu_to_be32(read->rd_length);
4531 	write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
4532 	return nfs_ok;
4533 }
4534 
4535 static __be32
4536 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr,
4537 		      union nfsd4_op_u *u)
4538 {
4539 	struct nfsd4_readlink *readlink = &u->readlink;
4540 	__be32 *p, wire_count, zero = xdr_zero;
4541 	struct xdr_stream *xdr = resp->xdr;
4542 	unsigned int length_offset;
4543 	int maxcount, status;
4544 
4545 	/* linktext4.count */
4546 	length_offset = xdr->buf->len;
4547 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
4548 		return nfserr_resource;
4549 
4550 	/* linktext4.data */
4551 	maxcount = PAGE_SIZE;
4552 	p = xdr_reserve_space(xdr, maxcount);
4553 	if (!p)
4554 		return nfserr_resource;
4555 	nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4556 						(char *)p, &maxcount);
4557 	if (nfserr == nfserr_isdir)
4558 		nfserr = nfserr_inval;
4559 	if (nfserr)
4560 		goto out_err;
4561 	status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4562 					   maxcount);
4563 	if (status) {
4564 		nfserr = nfserrno(status);
4565 		goto out_err;
4566 	}
4567 
4568 	wire_count = cpu_to_be32(maxcount);
4569 	write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, XDR_UNIT);
4570 	xdr_truncate_encode(xdr, length_offset + 4 + xdr_align_size(maxcount));
4571 	write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount, &zero,
4572 			       xdr_pad_size(maxcount));
4573 	return nfs_ok;
4574 
4575 out_err:
4576 	xdr_truncate_encode(xdr, length_offset);
4577 	return nfserr;
4578 }
4579 
4580 static __be32 nfsd4_encode_dirlist4(struct xdr_stream *xdr,
4581 				    struct nfsd4_readdir *readdir,
4582 				    u32 max_payload)
4583 {
4584 	int bytes_left, maxcount, starting_len = xdr->buf->len;
4585 	loff_t offset;
4586 	__be32 status;
4587 
4588 	/*
4589 	 * Number of bytes left for directory entries allowing for the
4590 	 * final 8 bytes of the readdir and a following failed op.
4591 	 */
4592 	bytes_left = xdr->buf->buflen - xdr->buf->len -
4593 		COMPOUND_ERR_SLACK_SPACE - XDR_UNIT * 2;
4594 	if (bytes_left < 0)
4595 		return nfserr_resource;
4596 	maxcount = min_t(u32, readdir->rd_maxcount, max_payload);
4597 
4598 	/*
4599 	 * The RFC defines rd_maxcount as the size of the
4600 	 * READDIR4resok structure, which includes the verifier
4601 	 * and the 8 bytes encoded at the end of this function.
4602 	 */
4603 	if (maxcount < XDR_UNIT * 4)
4604 		return nfserr_toosmall;
4605 	maxcount = min_t(int, maxcount - XDR_UNIT * 4, bytes_left);
4606 
4607 	/* RFC 3530 14.2.24 allows us to ignore dircount when it's 0 */
4608 	if (!readdir->rd_dircount)
4609 		readdir->rd_dircount = max_payload;
4610 
4611 	/* *entries */
4612 	readdir->xdr = xdr;
4613 	readdir->rd_maxcount = maxcount;
4614 	readdir->common.err = 0;
4615 	readdir->cookie_offset = 0;
4616 	offset = readdir->rd_cookie;
4617 	status = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp, &offset,
4618 			      &readdir->common, nfsd4_encode_entry4);
4619 	if (status)
4620 		return status;
4621 	if (readdir->common.err == nfserr_toosmall &&
4622 	    xdr->buf->len == starting_len) {
4623 		/* No entries were encoded. Which limit did we hit? */
4624 		if (maxcount - XDR_UNIT * 4 < bytes_left)
4625 			/* It was the fault of rd_maxcount */
4626 			return nfserr_toosmall;
4627 		/* We ran out of buffer space */
4628 		return nfserr_resource;
4629 	}
4630 	/* Encode the final entry's cookie value */
4631 	nfsd4_encode_entry4_nfs_cookie4(readdir, offset);
4632 	/* No entries follow */
4633 	if (xdr_stream_encode_item_absent(xdr) != XDR_UNIT)
4634 		return nfserr_resource;
4635 
4636 	/* eof */
4637 	return nfsd4_encode_bool(xdr, readdir->common.err == nfserr_eof);
4638 }
4639 
4640 static __be32
4641 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr,
4642 		     union nfsd4_op_u *u)
4643 {
4644 	struct nfsd4_readdir *readdir = &u->readdir;
4645 	struct xdr_stream *xdr = resp->xdr;
4646 	int starting_len = xdr->buf->len;
4647 
4648 	/* cookieverf */
4649 	nfserr = nfsd4_encode_verifier4(xdr, &readdir->rd_verf);
4650 	if (nfserr != nfs_ok)
4651 		return nfserr;
4652 
4653 	/* reply */
4654 	nfserr = nfsd4_encode_dirlist4(xdr, readdir, svc_max_payload(resp->rqstp));
4655 	if (nfserr != nfs_ok)
4656 		xdr_truncate_encode(xdr, starting_len);
4657 	return nfserr;
4658 }
4659 
4660 static __be32
4661 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr,
4662 		    union nfsd4_op_u *u)
4663 {
4664 	struct nfsd4_remove *remove = &u->remove;
4665 	struct xdr_stream *xdr = resp->xdr;
4666 
4667 	return nfsd4_encode_change_info4(xdr, &remove->rm_cinfo);
4668 }
4669 
4670 static __be32
4671 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr,
4672 		    union nfsd4_op_u *u)
4673 {
4674 	struct nfsd4_rename *rename = &u->rename;
4675 	struct xdr_stream *xdr = resp->xdr;
4676 
4677 	nfserr = nfsd4_encode_change_info4(xdr, &rename->rn_sinfo);
4678 	if (nfserr)
4679 		return nfserr;
4680 	return nfsd4_encode_change_info4(xdr, &rename->rn_tinfo);
4681 }
4682 
4683 static __be32
4684 nfsd4_encode_rpcsec_gss_info(struct xdr_stream *xdr,
4685 			     struct rpcsec_gss_info *info)
4686 {
4687 	__be32 status;
4688 
4689 	/* oid */
4690 	if (xdr_stream_encode_opaque(xdr, info->oid.data, info->oid.len) < 0)
4691 		return nfserr_resource;
4692 	/* qop */
4693 	status = nfsd4_encode_qop4(xdr, info->qop);
4694 	if (status != nfs_ok)
4695 		return status;
4696 	/* service */
4697 	if (xdr_stream_encode_u32(xdr, info->service) != XDR_UNIT)
4698 		return nfserr_resource;
4699 
4700 	return nfs_ok;
4701 }
4702 
4703 static __be32
4704 nfsd4_encode_secinfo4(struct xdr_stream *xdr, rpc_authflavor_t pf,
4705 		      u32 *supported)
4706 {
4707 	struct rpcsec_gss_info info;
4708 	__be32 status;
4709 
4710 	if (rpcauth_get_gssinfo(pf, &info) == 0) {
4711 		(*supported)++;
4712 
4713 		/* flavor */
4714 		status = nfsd4_encode_uint32_t(xdr, RPC_AUTH_GSS);
4715 		if (status != nfs_ok)
4716 			return status;
4717 		/* flavor_info */
4718 		status = nfsd4_encode_rpcsec_gss_info(xdr, &info);
4719 		if (status != nfs_ok)
4720 			return status;
4721 	} else if (pf < RPC_AUTH_MAXFLAVOR) {
4722 		(*supported)++;
4723 
4724 		/* flavor */
4725 		status = nfsd4_encode_uint32_t(xdr, pf);
4726 		if (status != nfs_ok)
4727 			return status;
4728 	}
4729 	return nfs_ok;
4730 }
4731 
4732 static __be32
4733 nfsd4_encode_SECINFO4resok(struct xdr_stream *xdr, struct svc_export *exp)
4734 {
4735 	u32 i, nflavs, supported;
4736 	struct exp_flavor_info *flavs;
4737 	struct exp_flavor_info def_flavs[2];
4738 	unsigned int count_offset;
4739 	__be32 status, wire_count;
4740 
4741 	if (exp->ex_nflavors) {
4742 		flavs = exp->ex_flavors;
4743 		nflavs = exp->ex_nflavors;
4744 	} else { /* Handling of some defaults in absence of real secinfo: */
4745 		flavs = def_flavs;
4746 		if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4747 			nflavs = 2;
4748 			flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4749 			flavs[1].pseudoflavor = RPC_AUTH_NULL;
4750 		} else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4751 			nflavs = 1;
4752 			flavs[0].pseudoflavor
4753 					= svcauth_gss_flavor(exp->ex_client);
4754 		} else {
4755 			nflavs = 1;
4756 			flavs[0].pseudoflavor
4757 					= exp->ex_client->flavour->flavour;
4758 		}
4759 	}
4760 
4761 	count_offset = xdr->buf->len;
4762 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
4763 		return nfserr_resource;
4764 
4765 	for (i = 0, supported = 0; i < nflavs; i++) {
4766 		status = nfsd4_encode_secinfo4(xdr, flavs[i].pseudoflavor,
4767 					       &supported);
4768 		if (status != nfs_ok)
4769 			return status;
4770 	}
4771 
4772 	wire_count = cpu_to_be32(supported);
4773 	write_bytes_to_xdr_buf(xdr->buf, count_offset, &wire_count,
4774 			       XDR_UNIT);
4775 	return 0;
4776 }
4777 
4778 static __be32
4779 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4780 		     union nfsd4_op_u *u)
4781 {
4782 	struct nfsd4_secinfo *secinfo = &u->secinfo;
4783 	struct xdr_stream *xdr = resp->xdr;
4784 
4785 	return nfsd4_encode_SECINFO4resok(xdr, secinfo->si_exp);
4786 }
4787 
4788 static __be32
4789 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4790 		     union nfsd4_op_u *u)
4791 {
4792 	struct nfsd4_secinfo_no_name *secinfo = &u->secinfo_no_name;
4793 	struct xdr_stream *xdr = resp->xdr;
4794 
4795 	return nfsd4_encode_SECINFO4resok(xdr, secinfo->sin_exp);
4796 }
4797 
4798 static __be32
4799 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4800 		     union nfsd4_op_u *u)
4801 {
4802 	struct nfsd4_setattr *setattr = &u->setattr;
4803 	__be32 status;
4804 
4805 	switch (nfserr) {
4806 	case nfs_ok:
4807 		/* attrsset */
4808 		status = nfsd4_encode_bitmap4(resp->xdr, setattr->sa_bmval[0],
4809 					      setattr->sa_bmval[1],
4810 					      setattr->sa_bmval[2]);
4811 		break;
4812 	default:
4813 		/* attrsset */
4814 		status = nfsd4_encode_bitmap4(resp->xdr, 0, 0, 0);
4815 	}
4816 	return status != nfs_ok ? status : nfserr;
4817 }
4818 
4819 static __be32
4820 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr,
4821 			 union nfsd4_op_u *u)
4822 {
4823 	struct nfsd4_setclientid *scd = &u->setclientid;
4824 	struct xdr_stream *xdr = resp->xdr;
4825 
4826 	if (!nfserr) {
4827 		nfserr = nfsd4_encode_clientid4(xdr, &scd->se_clientid);
4828 		if (nfserr != nfs_ok)
4829 			goto out;
4830 		nfserr = nfsd4_encode_verifier4(xdr, &scd->se_confirm);
4831 	} else if (nfserr == nfserr_clid_inuse) {
4832 		/* empty network id */
4833 		if (xdr_stream_encode_u32(xdr, 0) < 0) {
4834 			nfserr = nfserr_resource;
4835 			goto out;
4836 		}
4837 		/* empty universal address */
4838 		if (xdr_stream_encode_u32(xdr, 0) < 0) {
4839 			nfserr = nfserr_resource;
4840 			goto out;
4841 		}
4842 	}
4843 out:
4844 	return nfserr;
4845 }
4846 
4847 static __be32
4848 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr,
4849 		   union nfsd4_op_u *u)
4850 {
4851 	struct nfsd4_write *write = &u->write;
4852 	struct xdr_stream *xdr = resp->xdr;
4853 
4854 	/* count */
4855 	nfserr = nfsd4_encode_count4(xdr, write->wr_bytes_written);
4856 	if (nfserr)
4857 		return nfserr;
4858 	/* committed */
4859 	if (xdr_stream_encode_u32(xdr, write->wr_how_written) != XDR_UNIT)
4860 		return nfserr_resource;
4861 	/* writeverf */
4862 	return nfsd4_encode_verifier4(xdr, &write->wr_verifier);
4863 }
4864 
4865 static __be32
4866 nfsd4_encode_state_protect_ops4(struct xdr_stream *xdr,
4867 				struct nfsd4_exchange_id *exid)
4868 {
4869 	__be32 status;
4870 
4871 	/* spo_must_enforce */
4872 	status = nfsd4_encode_bitmap4(xdr, exid->spo_must_enforce[0],
4873 				      exid->spo_must_enforce[1],
4874 				      exid->spo_must_enforce[2]);
4875 	if (status != nfs_ok)
4876 		return status;
4877 	/* spo_must_allow */
4878 	return nfsd4_encode_bitmap4(xdr, exid->spo_must_allow[0],
4879 				    exid->spo_must_allow[1],
4880 				    exid->spo_must_allow[2]);
4881 }
4882 
4883 static __be32
4884 nfsd4_encode_state_protect4_r(struct xdr_stream *xdr, struct nfsd4_exchange_id *exid)
4885 {
4886 	__be32 status;
4887 
4888 	if (xdr_stream_encode_u32(xdr, exid->spa_how) != XDR_UNIT)
4889 		return nfserr_resource;
4890 	switch (exid->spa_how) {
4891 	case SP4_NONE:
4892 		status = nfs_ok;
4893 		break;
4894 	case SP4_MACH_CRED:
4895 		/* spr_mach_ops */
4896 		status = nfsd4_encode_state_protect_ops4(xdr, exid);
4897 		break;
4898 	default:
4899 		status = nfserr_serverfault;
4900 	}
4901 	return status;
4902 }
4903 
4904 static __be32
4905 nfsd4_encode_server_owner4(struct xdr_stream *xdr, struct svc_rqst *rqstp)
4906 {
4907 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4908 	__be32 status;
4909 
4910 	/* so_minor_id */
4911 	status = nfsd4_encode_uint64_t(xdr, 0);
4912 	if (status != nfs_ok)
4913 		return status;
4914 	/* so_major_id */
4915 	return nfsd4_encode_opaque(xdr, nn->nfsd_name, strlen(nn->nfsd_name));
4916 }
4917 
4918 static __be32
4919 nfsd4_encode_nfs_impl_id4(struct xdr_stream *xdr, struct nfsd4_exchange_id *exid)
4920 {
4921 	__be32 status;
4922 
4923 	/* nii_domain */
4924 	status = nfsd4_encode_opaque(xdr, exid->nii_domain.data,
4925 				     exid->nii_domain.len);
4926 	if (status != nfs_ok)
4927 		return status;
4928 	/* nii_name */
4929 	status = nfsd4_encode_opaque(xdr, exid->nii_name.data,
4930 				     exid->nii_name.len);
4931 	if (status != nfs_ok)
4932 		return status;
4933 	/* nii_time */
4934 	return nfsd4_encode_nfstime4(xdr, &exid->nii_time);
4935 }
4936 
4937 static __be32
4938 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
4939 			 union nfsd4_op_u *u)
4940 {
4941 	struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
4942 	struct nfsd4_exchange_id *exid = &u->exchange_id;
4943 	struct xdr_stream *xdr = resp->xdr;
4944 
4945 	/* eir_clientid */
4946 	nfserr = nfsd4_encode_clientid4(xdr, &exid->clientid);
4947 	if (nfserr != nfs_ok)
4948 		return nfserr;
4949 	/* eir_sequenceid */
4950 	nfserr = nfsd4_encode_sequenceid4(xdr, exid->seqid);
4951 	if (nfserr != nfs_ok)
4952 		return nfserr;
4953 	/* eir_flags */
4954 	nfserr = nfsd4_encode_uint32_t(xdr, exid->flags);
4955 	if (nfserr != nfs_ok)
4956 		return nfserr;
4957 	/* eir_state_protect */
4958 	nfserr = nfsd4_encode_state_protect4_r(xdr, exid);
4959 	if (nfserr != nfs_ok)
4960 		return nfserr;
4961 	/* eir_server_owner */
4962 	nfserr = nfsd4_encode_server_owner4(xdr, resp->rqstp);
4963 	if (nfserr != nfs_ok)
4964 		return nfserr;
4965 	/* eir_server_scope */
4966 	nfserr = nfsd4_encode_opaque(xdr, nn->nfsd_name,
4967 				     strlen(nn->nfsd_name));
4968 	if (nfserr != nfs_ok)
4969 		return nfserr;
4970 	/* eir_server_impl_id<1> */
4971 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
4972 		return nfserr_resource;
4973 	nfserr = nfsd4_encode_nfs_impl_id4(xdr, exid);
4974 	if (nfserr != nfs_ok)
4975 		return nfserr;
4976 
4977 	return nfs_ok;
4978 }
4979 
4980 static __be32
4981 nfsd4_encode_channel_attrs4(struct xdr_stream *xdr,
4982 			    const struct nfsd4_channel_attrs *attrs)
4983 {
4984 	__be32 status;
4985 
4986 	/* ca_headerpadsize */
4987 	status = nfsd4_encode_count4(xdr, 0);
4988 	if (status != nfs_ok)
4989 		return status;
4990 	/* ca_maxrequestsize */
4991 	status = nfsd4_encode_count4(xdr, attrs->maxreq_sz);
4992 	if (status != nfs_ok)
4993 		return status;
4994 	/* ca_maxresponsesize */
4995 	status = nfsd4_encode_count4(xdr, attrs->maxresp_sz);
4996 	if (status != nfs_ok)
4997 		return status;
4998 	/* ca_maxresponsesize_cached */
4999 	status = nfsd4_encode_count4(xdr, attrs->maxresp_cached);
5000 	if (status != nfs_ok)
5001 		return status;
5002 	/* ca_maxoperations */
5003 	status = nfsd4_encode_count4(xdr, attrs->maxops);
5004 	if (status != nfs_ok)
5005 		return status;
5006 	/* ca_maxrequests */
5007 	status = nfsd4_encode_count4(xdr, attrs->maxreqs);
5008 	if (status != nfs_ok)
5009 		return status;
5010 	/* ca_rdma_ird<1> */
5011 	if (xdr_stream_encode_u32(xdr, attrs->nr_rdma_attrs) != XDR_UNIT)
5012 		return nfserr_resource;
5013 	if (attrs->nr_rdma_attrs)
5014 		return nfsd4_encode_uint32_t(xdr, attrs->rdma_attrs);
5015 	return nfs_ok;
5016 }
5017 
5018 static __be32
5019 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
5020 			    union nfsd4_op_u *u)
5021 {
5022 	struct nfsd4_create_session *sess = &u->create_session;
5023 	struct xdr_stream *xdr = resp->xdr;
5024 
5025 	/* csr_sessionid */
5026 	nfserr = nfsd4_encode_sessionid4(xdr, &sess->sessionid);
5027 	if (nfserr != nfs_ok)
5028 		return nfserr;
5029 	/* csr_sequence */
5030 	nfserr = nfsd4_encode_sequenceid4(xdr, sess->seqid);
5031 	if (nfserr != nfs_ok)
5032 		return nfserr;
5033 	/* csr_flags */
5034 	nfserr = nfsd4_encode_uint32_t(xdr, sess->flags);
5035 	if (nfserr != nfs_ok)
5036 		return nfserr;
5037 	/* csr_fore_chan_attrs */
5038 	nfserr = nfsd4_encode_channel_attrs4(xdr, &sess->fore_channel);
5039 	if (nfserr != nfs_ok)
5040 		return nfserr;
5041 	/* csr_back_chan_attrs */
5042 	return nfsd4_encode_channel_attrs4(xdr, &sess->back_channel);
5043 }
5044 
5045 static __be32
5046 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
5047 		      union nfsd4_op_u *u)
5048 {
5049 	struct nfsd4_sequence *seq = &u->sequence;
5050 	struct xdr_stream *xdr = resp->xdr;
5051 
5052 	/* sr_sessionid */
5053 	nfserr = nfsd4_encode_sessionid4(xdr, &seq->sessionid);
5054 	if (nfserr != nfs_ok)
5055 		return nfserr;
5056 	/* sr_sequenceid */
5057 	nfserr = nfsd4_encode_sequenceid4(xdr, seq->seqid);
5058 	if (nfserr != nfs_ok)
5059 		return nfserr;
5060 	/* sr_slotid */
5061 	nfserr = nfsd4_encode_slotid4(xdr, seq->slotid);
5062 	if (nfserr != nfs_ok)
5063 		return nfserr;
5064 	/* Note slotid's are numbered from zero: */
5065 	/* sr_highest_slotid */
5066 	nfserr = nfsd4_encode_slotid4(xdr, seq->maxslots - 1);
5067 	if (nfserr != nfs_ok)
5068 		return nfserr;
5069 	/* sr_target_highest_slotid */
5070 	nfserr = nfsd4_encode_slotid4(xdr, seq->target_maxslots - 1);
5071 	if (nfserr != nfs_ok)
5072 		return nfserr;
5073 	/* sr_status_flags */
5074 	nfserr = nfsd4_encode_uint32_t(xdr, seq->status_flags);
5075 	if (nfserr != nfs_ok)
5076 		return nfserr;
5077 
5078 	resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
5079 	return nfs_ok;
5080 }
5081 
5082 static __be32
5083 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
5084 			  union nfsd4_op_u *u)
5085 {
5086 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5087 	struct nfsd4_test_stateid_id *stateid, *next;
5088 	struct xdr_stream *xdr = resp->xdr;
5089 
5090 	/* tsr_status_codes<> */
5091 	if (xdr_stream_encode_u32(xdr, test_stateid->ts_num_ids) != XDR_UNIT)
5092 		return nfserr_resource;
5093 	list_for_each_entry_safe(stateid, next,
5094 				 &test_stateid->ts_stateid_list, ts_id_list) {
5095 		if (xdr_stream_encode_be32(xdr, stateid->ts_id_status) != XDR_UNIT)
5096 			return nfserr_resource;
5097 	}
5098 	return nfs_ok;
5099 }
5100 
5101 static __be32
5102 nfsd4_encode_get_dir_delegation(struct nfsd4_compoundres *resp, __be32 nfserr,
5103 				union nfsd4_op_u *u)
5104 {
5105 	struct nfsd4_get_dir_delegation *gdd = &u->get_dir_delegation;
5106 	struct xdr_stream *xdr = resp->xdr;
5107 	__be32 status = nfserr_resource;
5108 
5109 	switch(gdd->gddrnf_status) {
5110 	case GDD4_OK:
5111 		if (xdr_stream_encode_u32(xdr, GDD4_OK) != XDR_UNIT)
5112 			break;
5113 		status = nfsd4_encode_verifier4(xdr, &gdd->gddr_cookieverf);
5114 		if (status)
5115 			break;
5116 		status = nfsd4_encode_stateid4(xdr, &gdd->gddr_stateid);
5117 		if (status)
5118 			break;
5119 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_notification[0], 0, 0);
5120 		if (status)
5121 			break;
5122 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_child_attributes[0],
5123 						   gdd->gddr_child_attributes[1],
5124 						   gdd->gddr_child_attributes[2]);
5125 		if (status)
5126 			break;
5127 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_dir_attributes[0],
5128 						   gdd->gddr_dir_attributes[1],
5129 						   gdd->gddr_dir_attributes[2]);
5130 		break;
5131 	default:
5132 		pr_warn("nfsd: bad gddrnf_status (%u)\n", gdd->gddrnf_status);
5133 		gdd->gddrnf_will_signal_deleg_avail = 0;
5134 		fallthrough;
5135 	case GDD4_UNAVAIL:
5136 		if (xdr_stream_encode_u32(xdr, GDD4_UNAVAIL) != XDR_UNIT)
5137 			break;
5138 		status = nfsd4_encode_bool(xdr, gdd->gddrnf_will_signal_deleg_avail);
5139 		break;
5140 	}
5141 	return status;
5142 }
5143 
5144 #ifdef CONFIG_NFSD_PNFS
5145 static __be32
5146 nfsd4_encode_device_addr4(struct xdr_stream *xdr,
5147 			  const struct nfsd4_getdeviceinfo *gdev)
5148 {
5149 	u32 needed_len, starting_len = xdr->buf->len;
5150 	const struct nfsd4_layout_ops *ops;
5151 	__be32 status;
5152 
5153 	/* da_layout_type */
5154 	if (xdr_stream_encode_u32(xdr, gdev->gd_layout_type) != XDR_UNIT)
5155 		return nfserr_resource;
5156 	/* da_addr_body */
5157 	ops = nfsd4_layout_ops[gdev->gd_layout_type];
5158 	status = ops->encode_getdeviceinfo(xdr, gdev);
5159 	if (status != nfs_ok) {
5160 		/*
5161 		 * Don't burden the layout drivers with enforcing
5162 		 * gd_maxcount. Just tell the client to come back
5163 		 * with a bigger buffer if it's not enough.
5164 		 */
5165 		if (xdr->buf->len + XDR_UNIT > gdev->gd_maxcount)
5166 			goto toosmall;
5167 		return status;
5168 	}
5169 
5170 	return nfs_ok;
5171 
5172 toosmall:
5173 	needed_len = xdr->buf->len + XDR_UNIT;	/* notifications */
5174 	xdr_truncate_encode(xdr, starting_len);
5175 
5176 	status = nfsd4_encode_count4(xdr, needed_len);
5177 	if (status != nfs_ok)
5178 		return status;
5179 	return nfserr_toosmall;
5180 }
5181 
5182 static __be32
5183 nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
5184 		union nfsd4_op_u *u)
5185 {
5186 	struct nfsd4_getdeviceinfo *gdev = &u->getdeviceinfo;
5187 	struct xdr_stream *xdr = resp->xdr;
5188 
5189 	/* gdir_device_addr */
5190 	nfserr = nfsd4_encode_device_addr4(xdr, gdev);
5191 	if (nfserr)
5192 		return nfserr;
5193 	/* gdir_notification */
5194 	return nfsd4_encode_bitmap4(xdr, gdev->gd_notify_types, 0, 0);
5195 }
5196 
5197 static __be32
5198 nfsd4_encode_layout4(struct xdr_stream *xdr, const struct nfsd4_layoutget *lgp)
5199 {
5200 	const struct nfsd4_layout_ops *ops = nfsd4_layout_ops[lgp->lg_layout_type];
5201 	__be32 status;
5202 
5203 	/* lo_offset */
5204 	status = nfsd4_encode_offset4(xdr, lgp->lg_seg.offset);
5205 	if (status != nfs_ok)
5206 		return status;
5207 	/* lo_length */
5208 	status = nfsd4_encode_length4(xdr, lgp->lg_seg.length);
5209 	if (status != nfs_ok)
5210 		return status;
5211 	/* lo_iomode */
5212 	if (xdr_stream_encode_u32(xdr, lgp->lg_seg.iomode) != XDR_UNIT)
5213 		return nfserr_resource;
5214 	/* lo_content */
5215 	if (xdr_stream_encode_u32(xdr, lgp->lg_layout_type) != XDR_UNIT)
5216 		return nfserr_resource;
5217 	return ops->encode_layoutget(xdr, lgp);
5218 }
5219 
5220 static __be32
5221 nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
5222 		union nfsd4_op_u *u)
5223 {
5224 	struct nfsd4_layoutget *lgp = &u->layoutget;
5225 	struct xdr_stream *xdr = resp->xdr;
5226 
5227 	/* logr_return_on_close */
5228 	nfserr = nfsd4_encode_bool(xdr, true);
5229 	if (nfserr != nfs_ok)
5230 		return nfserr;
5231 	/* logr_stateid */
5232 	nfserr = nfsd4_encode_stateid4(xdr, &lgp->lg_sid);
5233 	if (nfserr != nfs_ok)
5234 		return nfserr;
5235 	/* logr_layout<> */
5236 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5237 		return nfserr_resource;
5238 	return nfsd4_encode_layout4(xdr, lgp);
5239 }
5240 
5241 static __be32
5242 nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
5243 			  union nfsd4_op_u *u)
5244 {
5245 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
5246 	struct xdr_stream *xdr = resp->xdr;
5247 
5248 	/* ns_sizechanged */
5249 	nfserr = nfsd4_encode_bool(xdr, lcp->lc_size_chg);
5250 	if (nfserr != nfs_ok)
5251 		return nfserr;
5252 	if (lcp->lc_size_chg)
5253 		/* ns_size */
5254 		return nfsd4_encode_length4(xdr, lcp->lc_newsize);
5255 	return nfs_ok;
5256 }
5257 
5258 static __be32
5259 nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
5260 		union nfsd4_op_u *u)
5261 {
5262 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
5263 	struct xdr_stream *xdr = resp->xdr;
5264 
5265 	/* lrs_present */
5266 	nfserr = nfsd4_encode_bool(xdr, lrp->lrs_present);
5267 	if (nfserr != nfs_ok)
5268 		return nfserr;
5269 	if (lrp->lrs_present)
5270 		/* lrs_stateid */
5271 		return nfsd4_encode_stateid4(xdr, &lrp->lr_sid);
5272 	return nfs_ok;
5273 }
5274 #endif /* CONFIG_NFSD_PNFS */
5275 
5276 static __be32
5277 nfsd4_encode_write_response4(struct xdr_stream *xdr,
5278 			     const struct nfsd4_copy *copy)
5279 {
5280 	const struct nfsd42_write_res *write = &copy->cp_res;
5281 	u32 count = nfsd4_copy_is_sync(copy) ? 0 : 1;
5282 	__be32 status;
5283 
5284 	/* wr_callback_id<1> */
5285 	if (xdr_stream_encode_u32(xdr, count) != XDR_UNIT)
5286 		return nfserr_resource;
5287 	if (count) {
5288 		status = nfsd4_encode_stateid4(xdr, &write->cb_stateid);
5289 		if (status != nfs_ok)
5290 			return status;
5291 	}
5292 
5293 	/* wr_count */
5294 	status = nfsd4_encode_length4(xdr, write->wr_bytes_written);
5295 	if (status != nfs_ok)
5296 		return status;
5297 	/* wr_committed */
5298 	if (xdr_stream_encode_u32(xdr, write->wr_stable_how) != XDR_UNIT)
5299 		return nfserr_resource;
5300 	/* wr_writeverf */
5301 	return nfsd4_encode_verifier4(xdr, &write->wr_verifier);
5302 }
5303 
5304 static __be32 nfsd4_encode_copy_requirements4(struct xdr_stream *xdr,
5305 					      const struct nfsd4_copy *copy)
5306 {
5307 	__be32 status;
5308 
5309 	/* cr_consecutive */
5310 	status = nfsd4_encode_bool(xdr, true);
5311 	if (status != nfs_ok)
5312 		return status;
5313 	/* cr_synchronous */
5314 	return nfsd4_encode_bool(xdr, nfsd4_copy_is_sync(copy));
5315 }
5316 
5317 static __be32
5318 nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
5319 		  union nfsd4_op_u *u)
5320 {
5321 	struct nfsd4_copy *copy = &u->copy;
5322 
5323 	nfserr = nfsd4_encode_write_response4(resp->xdr, copy);
5324 	if (nfserr != nfs_ok)
5325 		return nfserr;
5326 	return nfsd4_encode_copy_requirements4(resp->xdr, copy);
5327 }
5328 
5329 static __be32
5330 nfsd4_encode_netloc4(struct xdr_stream *xdr, const struct nl4_server *ns)
5331 {
5332 	__be32 status;
5333 
5334 	if (xdr_stream_encode_u32(xdr, ns->nl4_type) != XDR_UNIT)
5335 		return nfserr_resource;
5336 	switch (ns->nl4_type) {
5337 	case NL4_NETADDR:
5338 		/* nl_addr */
5339 		status = nfsd4_encode_netaddr4(xdr, &ns->u.nl4_addr);
5340 		break;
5341 	default:
5342 		status = nfserr_serverfault;
5343 	}
5344 	return status;
5345 }
5346 
5347 static __be32
5348 nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
5349 			 union nfsd4_op_u *u)
5350 {
5351 	struct nfsd4_copy_notify *cn = &u->copy_notify;
5352 	struct xdr_stream *xdr = resp->xdr;
5353 
5354 	/* cnr_lease_time */
5355 	nfserr = nfsd4_encode_nfstime4(xdr, &cn->cpn_lease_time);
5356 	if (nfserr)
5357 		return nfserr;
5358 	/* cnr_stateid */
5359 	nfserr = nfsd4_encode_stateid4(xdr, &cn->cpn_cnr_stateid);
5360 	if (nfserr)
5361 		return nfserr;
5362 	/* cnr_source_server<> */
5363 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5364 		return nfserr_resource;
5365 	return nfsd4_encode_netloc4(xdr, cn->cpn_src);
5366 }
5367 
5368 static __be32
5369 nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
5370 			    union nfsd4_op_u *u)
5371 {
5372 	struct nfsd4_offload_status *os = &u->offload_status;
5373 	struct xdr_stream *xdr = resp->xdr;
5374 
5375 	/* osr_count */
5376 	nfserr = nfsd4_encode_length4(xdr, os->count);
5377 	if (nfserr != nfs_ok)
5378 		return nfserr;
5379 	/* osr_complete<1> */
5380 	if (os->completed) {
5381 		if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5382 			return nfserr_resource;
5383 		if (xdr_stream_encode_be32(xdr, os->status) != XDR_UNIT)
5384 			return nfserr_resource;
5385 	} else if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
5386 		return nfserr_resource;
5387 	return nfs_ok;
5388 }
5389 
5390 static __be32
5391 nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
5392 			    struct nfsd4_read *read)
5393 {
5394 	struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
5395 	struct file *file = read->rd_nf->nf_file;
5396 	struct xdr_stream *xdr = resp->xdr;
5397 	bool splice_ok = argp->splice_ok;
5398 	unsigned int offset_offset;
5399 	__be32 nfserr, wire_count;
5400 	unsigned long maxcount;
5401 	__be64 wire_offset;
5402 
5403 	if (xdr_stream_encode_u32(xdr, NFS4_CONTENT_DATA) != XDR_UNIT)
5404 		return nfserr_io;
5405 
5406 	offset_offset = xdr->buf->len;
5407 
5408 	/* Reserve space for the byte offset and count */
5409 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 3)))
5410 		return nfserr_io;
5411 	xdr_commit_encode(xdr);
5412 
5413 	maxcount = min_t(unsigned long, read->rd_length,
5414 			 (xdr->buf->buflen - xdr->buf->len));
5415 
5416 	if (file->f_op->splice_read && splice_ok)
5417 		nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
5418 	else
5419 		nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
5420 	if (nfserr)
5421 		return nfserr;
5422 
5423 	wire_offset = cpu_to_be64(read->rd_offset);
5424 	write_bytes_to_xdr_buf(xdr->buf, offset_offset, &wire_offset,
5425 			       XDR_UNIT * 2);
5426 	wire_count = cpu_to_be32(read->rd_length);
5427 	write_bytes_to_xdr_buf(xdr->buf, offset_offset + XDR_UNIT * 2,
5428 			       &wire_count, XDR_UNIT);
5429 	return nfs_ok;
5430 }
5431 
5432 static __be32
5433 nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
5434 		       union nfsd4_op_u *u)
5435 {
5436 	struct nfsd4_read *read = &u->read;
5437 	struct file *file = read->rd_nf->nf_file;
5438 	struct xdr_stream *xdr = resp->xdr;
5439 	unsigned int eof_offset;
5440 	__be32 wire_data[2];
5441 	u32 segments = 0;
5442 
5443 	if (nfserr)
5444 		return nfserr;
5445 
5446 	eof_offset = xdr->buf->len;
5447 
5448 	/* Reserve space for the eof flag and segment count */
5449 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2)))
5450 		return nfserr_io;
5451 	xdr_commit_encode(xdr);
5452 
5453 	read->rd_eof = read->rd_offset >= i_size_read(file_inode(file));
5454 	if (read->rd_eof)
5455 		goto out;
5456 
5457 	nfserr = nfsd4_encode_read_plus_data(resp, read);
5458 	if (nfserr) {
5459 		xdr_truncate_encode(xdr, eof_offset);
5460 		return nfserr;
5461 	}
5462 
5463 	segments++;
5464 
5465 out:
5466 	wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
5467 	wire_data[1] = cpu_to_be32(segments);
5468 	write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
5469 	return nfserr;
5470 }
5471 
5472 static __be32
5473 nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
5474 		  union nfsd4_op_u *u)
5475 {
5476 	struct nfsd4_seek *seek = &u->seek;
5477 	struct xdr_stream *xdr = resp->xdr;
5478 
5479 	/* sr_eof */
5480 	nfserr = nfsd4_encode_bool(xdr, seek->seek_eof);
5481 	if (nfserr != nfs_ok)
5482 		return nfserr;
5483 	/* sr_offset */
5484 	return nfsd4_encode_offset4(xdr, seek->seek_pos);
5485 }
5486 
5487 static __be32
5488 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr,
5489 		  union nfsd4_op_u *p)
5490 {
5491 	return nfserr;
5492 }
5493 
5494 /*
5495  * Encode kmalloc-ed buffer in to XDR stream.
5496  */
5497 static __be32
5498 nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
5499 {
5500 	u32 cplen;
5501 	__be32 *p;
5502 
5503 	cplen = min_t(unsigned long, buflen,
5504 		      ((void *)xdr->end - (void *)xdr->p));
5505 	p = xdr_reserve_space(xdr, cplen);
5506 	if (!p)
5507 		return nfserr_resource;
5508 
5509 	memcpy(p, buf, cplen);
5510 	buf += cplen;
5511 	buflen -= cplen;
5512 
5513 	while (buflen) {
5514 		cplen = min_t(u32, buflen, PAGE_SIZE);
5515 		p = xdr_reserve_space(xdr, cplen);
5516 		if (!p)
5517 			return nfserr_resource;
5518 
5519 		memcpy(p, buf, cplen);
5520 
5521 		if (cplen < PAGE_SIZE) {
5522 			/*
5523 			 * We're done, with a length that wasn't page
5524 			 * aligned, so possibly not word aligned. Pad
5525 			 * any trailing bytes with 0.
5526 			 */
5527 			xdr_encode_opaque_fixed(p, NULL, cplen);
5528 			break;
5529 		}
5530 
5531 		buflen -= PAGE_SIZE;
5532 		buf += PAGE_SIZE;
5533 	}
5534 
5535 	return 0;
5536 }
5537 
5538 static __be32
5539 nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5540 		      union nfsd4_op_u *u)
5541 {
5542 	struct nfsd4_getxattr *getxattr = &u->getxattr;
5543 	struct xdr_stream *xdr = resp->xdr;
5544 	__be32 *p, err;
5545 
5546 	p = xdr_reserve_space(xdr, 4);
5547 	if (!p)
5548 		return nfserr_resource;
5549 
5550 	*p = cpu_to_be32(getxattr->getxa_len);
5551 
5552 	if (getxattr->getxa_len == 0)
5553 		return 0;
5554 
5555 	err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
5556 				    getxattr->getxa_len);
5557 
5558 	kvfree(getxattr->getxa_buf);
5559 
5560 	return err;
5561 }
5562 
5563 static __be32
5564 nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5565 		      union nfsd4_op_u *u)
5566 {
5567 	struct nfsd4_setxattr *setxattr = &u->setxattr;
5568 	struct xdr_stream *xdr = resp->xdr;
5569 
5570 	return nfsd4_encode_change_info4(xdr, &setxattr->setxa_cinfo);
5571 }
5572 
5573 /*
5574  * See if there are cookie values that can be rejected outright.
5575  */
5576 static __be32
5577 nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5578 				u32 *offsetp)
5579 {
5580 	u64 cookie = listxattrs->lsxa_cookie;
5581 
5582 	/*
5583 	 * If the cookie is larger than the maximum number we can fit
5584 	 * in the buffer we just got back from vfs_listxattr, it's invalid.
5585 	 */
5586 	if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5587 		return nfserr_badcookie;
5588 
5589 	*offsetp = (u32)cookie;
5590 	return 0;
5591 }
5592 
5593 static __be32
5594 nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5595 			union nfsd4_op_u *u)
5596 {
5597 	struct nfsd4_listxattrs *listxattrs = &u->listxattrs;
5598 	struct xdr_stream *xdr = resp->xdr;
5599 	u32 cookie_offset, count_offset, eof;
5600 	u32 left, xdrleft, slen, count;
5601 	u32 xdrlen, offset;
5602 	u64 cookie;
5603 	char *sp;
5604 	__be32 status, tmp;
5605 	__be64 wire_cookie;
5606 	__be32 *p;
5607 	u32 nuser;
5608 
5609 	eof = 1;
5610 
5611 	status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5612 	if (status)
5613 		goto out;
5614 
5615 	/*
5616 	 * Reserve space for the cookie and the name array count. Record
5617 	 * the offsets to save them later.
5618 	 */
5619 	cookie_offset = xdr->buf->len;
5620 	count_offset = cookie_offset + 8;
5621 	p = xdr_reserve_space(xdr, XDR_UNIT * 3);
5622 	if (!p) {
5623 		status = nfserr_resource;
5624 		goto out;
5625 	}
5626 
5627 	count = 0;
5628 	left = listxattrs->lsxa_len;
5629 	sp = listxattrs->lsxa_buf;
5630 	nuser = 0;
5631 
5632 	/* Bytes left is maxcount - 8 (cookie) - 4 (array count) */
5633 	xdrleft = listxattrs->lsxa_maxcount - XDR_UNIT * 3;
5634 
5635 	while (left > 0 && xdrleft > 0) {
5636 		slen = strlen(sp);
5637 
5638 		/*
5639 		 * Check if this is a "user." attribute, skip it if not.
5640 		 */
5641 		if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5642 			goto contloop;
5643 
5644 		slen -= XATTR_USER_PREFIX_LEN;
5645 		xdrlen = 4 + ((slen + 3) & ~3);
5646 		/* Check if both entry and eof can fit in the XDR buffer */
5647 		if (xdrlen + XDR_UNIT > xdrleft) {
5648 			if (count == 0) {
5649 				/*
5650 				 * Can't even fit the first attribute name.
5651 				 */
5652 				status = nfserr_toosmall;
5653 				goto out;
5654 			}
5655 			eof = 0;
5656 			goto wreof;
5657 		}
5658 
5659 		left -= XATTR_USER_PREFIX_LEN;
5660 		sp += XATTR_USER_PREFIX_LEN;
5661 		if (nuser++ < offset)
5662 			goto contloop;
5663 
5664 
5665 		p = xdr_reserve_space(xdr, xdrlen);
5666 		if (!p) {
5667 			status = nfserr_resource;
5668 			goto out;
5669 		}
5670 
5671 		xdr_encode_opaque(p, sp, slen);
5672 
5673 		xdrleft -= xdrlen;
5674 		count++;
5675 contloop:
5676 		sp += slen + 1;
5677 		left -= slen + 1;
5678 	}
5679 
5680 	/*
5681 	 * If there were user attributes to copy, but we didn't copy
5682 	 * any, the offset was too large (e.g. the cookie was invalid).
5683 	 */
5684 	if (nuser > 0 && count == 0) {
5685 		status = nfserr_badcookie;
5686 		goto out;
5687 	}
5688 
5689 wreof:
5690 	p = xdr_reserve_space(xdr, 4);
5691 	if (!p) {
5692 		status = nfserr_resource;
5693 		goto out;
5694 	}
5695 	*p = cpu_to_be32(eof);
5696 
5697 	cookie = offset + count;
5698 
5699 	wire_cookie = cpu_to_be64(cookie);
5700 	write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &wire_cookie, 8);
5701 	tmp = cpu_to_be32(count);
5702 	write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
5703 out:
5704 	if (listxattrs->lsxa_len)
5705 		kvfree(listxattrs->lsxa_buf);
5706 	return status;
5707 }
5708 
5709 static __be32
5710 nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5711 			 union nfsd4_op_u *u)
5712 {
5713 	struct nfsd4_removexattr *removexattr = &u->removexattr;
5714 	struct xdr_stream *xdr = resp->xdr;
5715 
5716 	return nfsd4_encode_change_info4(xdr, &removexattr->rmxa_cinfo);
5717 }
5718 
5719 typedef __be32(*nfsd4_enc)(struct nfsd4_compoundres *, __be32, union nfsd4_op_u *u);
5720 
5721 /*
5722  * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5723  * since we don't need to filter out obsolete ops as this is
5724  * done in the decoding phase.
5725  */
5726 static const nfsd4_enc nfsd4_enc_ops[] = {
5727 	[OP_ACCESS]		= nfsd4_encode_access,
5728 	[OP_CLOSE]		= nfsd4_encode_close,
5729 	[OP_COMMIT]		= nfsd4_encode_commit,
5730 	[OP_CREATE]		= nfsd4_encode_create,
5731 	[OP_DELEGPURGE]		= nfsd4_encode_noop,
5732 	[OP_DELEGRETURN]	= nfsd4_encode_noop,
5733 	[OP_GETATTR]		= nfsd4_encode_getattr,
5734 	[OP_GETFH]		= nfsd4_encode_getfh,
5735 	[OP_LINK]		= nfsd4_encode_link,
5736 	[OP_LOCK]		= nfsd4_encode_lock,
5737 	[OP_LOCKT]		= nfsd4_encode_lockt,
5738 	[OP_LOCKU]		= nfsd4_encode_locku,
5739 	[OP_LOOKUP]		= nfsd4_encode_noop,
5740 	[OP_LOOKUPP]		= nfsd4_encode_noop,
5741 	[OP_NVERIFY]		= nfsd4_encode_noop,
5742 	[OP_OPEN]		= nfsd4_encode_open,
5743 	[OP_OPENATTR]		= nfsd4_encode_noop,
5744 	[OP_OPEN_CONFIRM]	= nfsd4_encode_open_confirm,
5745 	[OP_OPEN_DOWNGRADE]	= nfsd4_encode_open_downgrade,
5746 	[OP_PUTFH]		= nfsd4_encode_noop,
5747 	[OP_PUTPUBFH]		= nfsd4_encode_noop,
5748 	[OP_PUTROOTFH]		= nfsd4_encode_noop,
5749 	[OP_READ]		= nfsd4_encode_read,
5750 	[OP_READDIR]		= nfsd4_encode_readdir,
5751 	[OP_READLINK]		= nfsd4_encode_readlink,
5752 	[OP_REMOVE]		= nfsd4_encode_remove,
5753 	[OP_RENAME]		= nfsd4_encode_rename,
5754 	[OP_RENEW]		= nfsd4_encode_noop,
5755 	[OP_RESTOREFH]		= nfsd4_encode_noop,
5756 	[OP_SAVEFH]		= nfsd4_encode_noop,
5757 	[OP_SECINFO]		= nfsd4_encode_secinfo,
5758 	[OP_SETATTR]		= nfsd4_encode_setattr,
5759 	[OP_SETCLIENTID]	= nfsd4_encode_setclientid,
5760 	[OP_SETCLIENTID_CONFIRM] = nfsd4_encode_noop,
5761 	[OP_VERIFY]		= nfsd4_encode_noop,
5762 	[OP_WRITE]		= nfsd4_encode_write,
5763 	[OP_RELEASE_LOCKOWNER]	= nfsd4_encode_noop,
5764 
5765 	/* NFSv4.1 operations */
5766 	[OP_BACKCHANNEL_CTL]	= nfsd4_encode_noop,
5767 	[OP_BIND_CONN_TO_SESSION] = nfsd4_encode_bind_conn_to_session,
5768 	[OP_EXCHANGE_ID]	= nfsd4_encode_exchange_id,
5769 	[OP_CREATE_SESSION]	= nfsd4_encode_create_session,
5770 	[OP_DESTROY_SESSION]	= nfsd4_encode_noop,
5771 	[OP_FREE_STATEID]	= nfsd4_encode_noop,
5772 	[OP_GET_DIR_DELEGATION]	= nfsd4_encode_get_dir_delegation,
5773 #ifdef CONFIG_NFSD_PNFS
5774 	[OP_GETDEVICEINFO]	= nfsd4_encode_getdeviceinfo,
5775 	[OP_GETDEVICELIST]	= nfsd4_encode_noop,
5776 	[OP_LAYOUTCOMMIT]	= nfsd4_encode_layoutcommit,
5777 	[OP_LAYOUTGET]		= nfsd4_encode_layoutget,
5778 	[OP_LAYOUTRETURN]	= nfsd4_encode_layoutreturn,
5779 #else
5780 	[OP_GETDEVICEINFO]	= nfsd4_encode_noop,
5781 	[OP_GETDEVICELIST]	= nfsd4_encode_noop,
5782 	[OP_LAYOUTCOMMIT]	= nfsd4_encode_noop,
5783 	[OP_LAYOUTGET]		= nfsd4_encode_noop,
5784 	[OP_LAYOUTRETURN]	= nfsd4_encode_noop,
5785 #endif
5786 	[OP_SECINFO_NO_NAME]	= nfsd4_encode_secinfo_no_name,
5787 	[OP_SEQUENCE]		= nfsd4_encode_sequence,
5788 	[OP_SET_SSV]		= nfsd4_encode_noop,
5789 	[OP_TEST_STATEID]	= nfsd4_encode_test_stateid,
5790 	[OP_WANT_DELEGATION]	= nfsd4_encode_noop,
5791 	[OP_DESTROY_CLIENTID]	= nfsd4_encode_noop,
5792 	[OP_RECLAIM_COMPLETE]	= nfsd4_encode_noop,
5793 
5794 	/* NFSv4.2 operations */
5795 	[OP_ALLOCATE]		= nfsd4_encode_noop,
5796 	[OP_COPY]		= nfsd4_encode_copy,
5797 	[OP_COPY_NOTIFY]	= nfsd4_encode_copy_notify,
5798 	[OP_DEALLOCATE]		= nfsd4_encode_noop,
5799 	[OP_IO_ADVISE]		= nfsd4_encode_noop,
5800 	[OP_LAYOUTERROR]	= nfsd4_encode_noop,
5801 	[OP_LAYOUTSTATS]	= nfsd4_encode_noop,
5802 	[OP_OFFLOAD_CANCEL]	= nfsd4_encode_noop,
5803 	[OP_OFFLOAD_STATUS]	= nfsd4_encode_offload_status,
5804 	[OP_READ_PLUS]		= nfsd4_encode_read_plus,
5805 	[OP_SEEK]		= nfsd4_encode_seek,
5806 	[OP_WRITE_SAME]		= nfsd4_encode_noop,
5807 	[OP_CLONE]		= nfsd4_encode_noop,
5808 
5809 	/* RFC 8276 extended atributes operations */
5810 	[OP_GETXATTR]		= nfsd4_encode_getxattr,
5811 	[OP_SETXATTR]		= nfsd4_encode_setxattr,
5812 	[OP_LISTXATTRS]		= nfsd4_encode_listxattrs,
5813 	[OP_REMOVEXATTR]	= nfsd4_encode_removexattr,
5814 };
5815 
5816 /*
5817  * Calculate whether we still have space to encode repsize bytes.
5818  * There are two considerations:
5819  *     - For NFS versions >=4.1, the size of the reply must stay within
5820  *       session limits
5821  *     - For all NFS versions, we must stay within limited preallocated
5822  *       buffer space.
5823  *
5824  * This is called before the operation is processed, so can only provide
5825  * an upper estimate.  For some nonidempotent operations (such as
5826  * getattr), it's not necessarily a problem if that estimate is wrong,
5827  * as we can fail it after processing without significant side effects.
5828  */
5829 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
5830 {
5831 	struct xdr_buf *buf = &resp->rqstp->rq_res;
5832 	struct nfsd4_slot *slot = resp->cstate.slot;
5833 
5834 	if (buf->len + respsize <= buf->buflen)
5835 		return nfs_ok;
5836 	if (!nfsd4_has_session(&resp->cstate))
5837 		return nfserr_resource;
5838 	if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5839 		WARN_ON_ONCE(1);
5840 		return nfserr_rep_too_big_to_cache;
5841 	}
5842 	return nfserr_rep_too_big;
5843 }
5844 
5845 static __be32 nfsd4_map_status(__be32 status, u32 minor)
5846 {
5847 	switch (status) {
5848 	case nfs_ok:
5849 		break;
5850 	case nfserr_wrong_type:
5851 		/* RFC 8881 - 15.1.2.9 */
5852 		if (minor == 0)
5853 			status = nfserr_inval;
5854 		break;
5855 	case nfserr_symlink_not_dir:
5856 		status = nfserr_symlink;
5857 		break;
5858 	}
5859 	return status;
5860 }
5861 
5862 void
5863 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5864 {
5865 	struct xdr_stream *xdr = resp->xdr;
5866 	struct nfs4_stateowner *so = resp->cstate.replay_owner;
5867 	struct svc_rqst *rqstp = resp->rqstp;
5868 	const struct nfsd4_operation *opdesc = op->opdesc;
5869 	unsigned int op_status_offset;
5870 	nfsd4_enc encoder;
5871 
5872 	if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
5873 		goto release;
5874 	op_status_offset = xdr->buf->len;
5875 	if (!xdr_reserve_space(xdr, XDR_UNIT))
5876 		goto release;
5877 
5878 	if (op->opnum == OP_ILLEGAL)
5879 		goto status;
5880 	if (op->status && opdesc &&
5881 			!(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5882 		goto status;
5883 	BUG_ON(op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5884 	       !nfsd4_enc_ops[op->opnum]);
5885 	encoder = nfsd4_enc_ops[op->opnum];
5886 	op->status = encoder(resp, op->status, &op->u);
5887 	if (op->status)
5888 		trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
5889 	xdr_commit_encode(xdr);
5890 
5891 	/* nfsd4_check_resp_size guarantees enough room for error status */
5892 	if (!op->status) {
5893 		int space_needed = 0;
5894 		if (!nfsd4_last_compound_op(rqstp))
5895 			space_needed = COMPOUND_ERR_SLACK_SPACE;
5896 		op->status = nfsd4_check_resp_size(resp, space_needed);
5897 	}
5898 	if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5899 		struct nfsd4_slot *slot = resp->cstate.slot;
5900 
5901 		if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5902 			op->status = nfserr_rep_too_big_to_cache;
5903 		else
5904 			op->status = nfserr_rep_too_big;
5905 	}
5906 	if (op->status == nfserr_resource ||
5907 	    op->status == nfserr_rep_too_big ||
5908 	    op->status == nfserr_rep_too_big_to_cache) {
5909 		/*
5910 		 * The operation may have already been encoded or
5911 		 * partially encoded.  No op returns anything additional
5912 		 * in the case of one of these three errors, so we can
5913 		 * just truncate back to after the status.  But it's a
5914 		 * bug if we had to do this on a non-idempotent op:
5915 		 */
5916 		warn_on_nonidempotent_op(op);
5917 		xdr_truncate_encode(xdr, op_status_offset + XDR_UNIT);
5918 	}
5919 	if (so) {
5920 		int len = xdr->buf->len - (op_status_offset + XDR_UNIT);
5921 
5922 		so->so_replay.rp_status = op->status;
5923 		so->so_replay.rp_buflen = len;
5924 		read_bytes_from_xdr_buf(xdr->buf, op_status_offset + XDR_UNIT,
5925 						so->so_replay.rp_buf, len);
5926 	}
5927 status:
5928 	op->status = nfsd4_map_status(op->status,
5929 				      resp->cstate.minorversion);
5930 	write_bytes_to_xdr_buf(xdr->buf, op_status_offset,
5931 			       &op->status, XDR_UNIT);
5932 release:
5933 	if (opdesc && opdesc->op_release)
5934 		opdesc->op_release(&op->u);
5935 
5936 	/*
5937 	 * Account for pages consumed while encoding this operation.
5938 	 * The xdr_stream primitives don't manage rq_next_page.
5939 	 */
5940 	rqstp->rq_next_page = xdr->page_ptr + 1;
5941 }
5942 
5943 /**
5944  * nfsd4_encode_replay - encode a result stored in the stateowner reply cache
5945  * @xdr: send buffer's XDR stream
5946  * @op: operation being replayed
5947  *
5948  * @op->replay->rp_buf contains the previously-sent already-encoded result.
5949  */
5950 void nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
5951 {
5952 	struct nfs4_replay *rp = op->replay;
5953 
5954 	trace_nfsd_stateowner_replay(op->opnum, rp);
5955 
5956 	if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
5957 		return;
5958 	if (xdr_stream_encode_be32(xdr, rp->rp_status) != XDR_UNIT)
5959 		return;
5960 	xdr_stream_encode_opaque_fixed(xdr, rp->rp_buf, rp->rp_buflen);
5961 }
5962 
5963 void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
5964 {
5965 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
5966 
5967 	if (args->ops != args->iops) {
5968 		vfree(args->ops);
5969 		args->ops = args->iops;
5970 	}
5971 	while (args->to_free) {
5972 		struct svcxdr_tmpbuf *tb = args->to_free;
5973 		args->to_free = tb->next;
5974 		kfree(tb);
5975 	}
5976 }
5977 
5978 bool
5979 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
5980 {
5981 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
5982 
5983 	/* svcxdr_tmp_alloc */
5984 	args->to_free = NULL;
5985 
5986 	args->xdr = xdr;
5987 	args->ops = args->iops;
5988 	args->rqstp = rqstp;
5989 
5990 	return nfsd4_decode_compound(args);
5991 }
5992 
5993 bool
5994 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
5995 {
5996 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
5997 	__be32 *p;
5998 
5999 	/*
6000 	 * Send buffer space for the following items is reserved
6001 	 * at the top of nfsd4_proc_compound().
6002 	 */
6003 	p = resp->statusp;
6004 
6005 	*p++ = resp->cstate.status;
6006 	*p++ = htonl(resp->taglen);
6007 	memcpy(p, resp->tag, resp->taglen);
6008 	p += XDR_QUADLEN(resp->taglen);
6009 	*p++ = htonl(resp->opcnt);
6010 
6011 	nfsd4_sequence_done(resp);
6012 	return true;
6013 }
6014