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