xref: /linux/fs/nfsd/nfs4xdr.c (revision cfff672ffcf9a74b560c2002739729f91812e398)
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 	if (xdr_stream_decode_u32(argp->xdr, &sin->sin_style) < 0)
2012 		return nfserr_bad_xdr;
2013 
2014 	sin->sin_exp = NULL;
2015 	return nfs_ok;
2016 }
2017 
2018 static __be32
2019 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
2020 		      union nfsd4_op_u *u)
2021 {
2022 	struct nfsd4_sequence *seq = &u->sequence;
2023 	__be32 *p, status;
2024 
2025 	status = nfsd4_decode_sessionid4(argp, &seq->sessionid);
2026 	if (status)
2027 		return status;
2028 	p = xdr_inline_decode(argp->xdr, XDR_UNIT * 4);
2029 	if (!p)
2030 		return nfserr_bad_xdr;
2031 	seq->seqid = be32_to_cpup(p++);
2032 	seq->slotid = be32_to_cpup(p++);
2033 	/* sa_highest_slotid counts from 0 but maxslots  counts from 1 ... */
2034 	seq->maxslots = be32_to_cpup(p++) + 1;
2035 	seq->cachethis = be32_to_cpup(p);
2036 
2037 	seq->status_flags = 0;
2038 	return nfs_ok;
2039 }
2040 
2041 static __be32
2042 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp,
2043 			  union nfsd4_op_u *u)
2044 {
2045 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
2046 	struct nfsd4_test_stateid_id *stateid;
2047 	__be32 status;
2048 	u32 i;
2049 
2050 	memset(test_stateid, 0, sizeof(*test_stateid));
2051 	if (xdr_stream_decode_u32(argp->xdr, &test_stateid->ts_num_ids) < 0)
2052 		return nfserr_bad_xdr;
2053 
2054 	INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
2055 	for (i = 0; i < test_stateid->ts_num_ids; i++) {
2056 		stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
2057 		if (!stateid)
2058 			return nfserr_jukebox;
2059 		INIT_LIST_HEAD(&stateid->ts_id_list);
2060 		list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
2061 		status = nfsd4_decode_stateid4(argp, &stateid->ts_id_stateid);
2062 		if (status)
2063 			return status;
2064 	}
2065 
2066 	return nfs_ok;
2067 }
2068 
2069 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp,
2070 					    union nfsd4_op_u *u)
2071 {
2072 	struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
2073 	return nfsd4_decode_clientid4(argp, &dc->clientid);
2074 }
2075 
2076 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp,
2077 					    union nfsd4_op_u *u)
2078 {
2079 	struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
2080 	if (xdr_stream_decode_bool(argp->xdr, &rc->rca_one_fs) < 0)
2081 		return nfserr_bad_xdr;
2082 	return nfs_ok;
2083 }
2084 
2085 static __be32
2086 nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
2087 		       union nfsd4_op_u *u)
2088 {
2089 	struct nfsd4_fallocate *fallocate = &u->allocate;
2090 	__be32 status;
2091 
2092 	status = nfsd4_decode_stateid4(argp, &fallocate->falloc_stateid);
2093 	if (status)
2094 		return status;
2095 	if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_offset) < 0)
2096 		return nfserr_bad_xdr;
2097 	if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_length) < 0)
2098 		return nfserr_bad_xdr;
2099 
2100 	return nfs_ok;
2101 }
2102 
2103 static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
2104 				      struct nl4_server *ns)
2105 {
2106 	struct nfs42_netaddr *naddr;
2107 	__be32 *p;
2108 
2109 	if (xdr_stream_decode_u32(argp->xdr, &ns->nl4_type) < 0)
2110 		return nfserr_bad_xdr;
2111 
2112 	/* currently support for 1 inter-server source server */
2113 	switch (ns->nl4_type) {
2114 	case NL4_NETADDR:
2115 		naddr = &ns->u.nl4_addr;
2116 
2117 		if (xdr_stream_decode_u32(argp->xdr, &naddr->netid_len) < 0)
2118 			return nfserr_bad_xdr;
2119 		if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
2120 			return nfserr_bad_xdr;
2121 
2122 		p = xdr_inline_decode(argp->xdr, naddr->netid_len);
2123 		if (!p)
2124 			return nfserr_bad_xdr;
2125 		memcpy(naddr->netid, p, naddr->netid_len);
2126 
2127 		if (xdr_stream_decode_u32(argp->xdr, &naddr->addr_len) < 0)
2128 			return nfserr_bad_xdr;
2129 		if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
2130 			return nfserr_bad_xdr;
2131 
2132 		p = xdr_inline_decode(argp->xdr, naddr->addr_len);
2133 		if (!p)
2134 			return nfserr_bad_xdr;
2135 		memcpy(naddr->addr, p, naddr->addr_len);
2136 		break;
2137 	default:
2138 		return nfserr_bad_xdr;
2139 	}
2140 
2141 	return nfs_ok;
2142 }
2143 
2144 static __be32
2145 nfsd4_decode_copy(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
2146 {
2147 	struct nfsd4_copy *copy = &u->copy;
2148 	u32 consecutive, i, count, sync;
2149 	struct nl4_server *ns_dummy;
2150 	__be32 status;
2151 
2152 	memset(copy, 0, sizeof(*copy));
2153 	status = nfsd4_decode_stateid4(argp, &copy->cp_src_stateid);
2154 	if (status)
2155 		return status;
2156 	status = nfsd4_decode_stateid4(argp, &copy->cp_dst_stateid);
2157 	if (status)
2158 		return status;
2159 	if (xdr_stream_decode_u64(argp->xdr, &copy->cp_src_pos) < 0)
2160 		return nfserr_bad_xdr;
2161 	if (xdr_stream_decode_u64(argp->xdr, &copy->cp_dst_pos) < 0)
2162 		return nfserr_bad_xdr;
2163 	if (xdr_stream_decode_u64(argp->xdr, &copy->cp_count) < 0)
2164 		return nfserr_bad_xdr;
2165 	/* ca_consecutive: we always do consecutive copies */
2166 	if (xdr_stream_decode_u32(argp->xdr, &consecutive) < 0)
2167 		return nfserr_bad_xdr;
2168 	if (xdr_stream_decode_bool(argp->xdr, &sync) < 0)
2169 		return nfserr_bad_xdr;
2170 	nfsd4_copy_set_sync(copy, sync);
2171 
2172 	if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
2173 		return nfserr_bad_xdr;
2174 	copy->cp_src = svcxdr_tmpalloc(argp, sizeof(*copy->cp_src));
2175 	if (copy->cp_src == NULL)
2176 		return nfserr_jukebox;
2177 	if (count == 0) { /* intra-server copy */
2178 		__set_bit(NFSD4_COPY_F_INTRA, &copy->cp_flags);
2179 		return nfs_ok;
2180 	}
2181 
2182 	/* decode all the supplied server addresses but use only the first */
2183 	status = nfsd4_decode_nl4_server(argp, copy->cp_src);
2184 	if (status)
2185 		return status;
2186 
2187 	ns_dummy = kmalloc_obj(struct nl4_server);
2188 	if (ns_dummy == NULL)
2189 		return nfserr_jukebox;
2190 	for (i = 0; i < count - 1; i++) {
2191 		status = nfsd4_decode_nl4_server(argp, ns_dummy);
2192 		if (status) {
2193 			kfree(ns_dummy);
2194 			return status;
2195 		}
2196 	}
2197 	kfree(ns_dummy);
2198 
2199 	return nfs_ok;
2200 }
2201 
2202 static __be32
2203 nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
2204 			 union nfsd4_op_u *u)
2205 {
2206 	struct nfsd4_copy_notify *cn = &u->copy_notify;
2207 	__be32 status;
2208 
2209 	memset(cn, 0, sizeof(*cn));
2210 	cn->cpn_src = svcxdr_tmpalloc(argp, sizeof(*cn->cpn_src));
2211 	if (cn->cpn_src == NULL)
2212 		return nfserr_jukebox;
2213 	cn->cpn_dst = svcxdr_tmpalloc(argp, sizeof(*cn->cpn_dst));
2214 	if (cn->cpn_dst == NULL)
2215 		return nfserr_jukebox;
2216 
2217 	status = nfsd4_decode_stateid4(argp, &cn->cpn_src_stateid);
2218 	if (status)
2219 		return status;
2220 	return nfsd4_decode_nl4_server(argp, cn->cpn_dst);
2221 }
2222 
2223 static __be32
2224 nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
2225 			    union nfsd4_op_u *u)
2226 {
2227 	struct nfsd4_offload_status *os = &u->offload_status;
2228 	os->count = 0;
2229 	os->status = 0;
2230 	return nfsd4_decode_stateid4(argp, &os->stateid);
2231 }
2232 
2233 static __be32
2234 nfsd4_decode_seek(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
2235 {
2236 	struct nfsd4_seek *seek = &u->seek;
2237 	__be32 status;
2238 
2239 	status = nfsd4_decode_stateid4(argp, &seek->seek_stateid);
2240 	if (status)
2241 		return status;
2242 	if (xdr_stream_decode_u64(argp->xdr, &seek->seek_offset) < 0)
2243 		return nfserr_bad_xdr;
2244 	if (xdr_stream_decode_u32(argp->xdr, &seek->seek_whence) < 0)
2245 		return nfserr_bad_xdr;
2246 
2247 	seek->seek_eof = 0;
2248 	seek->seek_pos = 0;
2249 	return nfs_ok;
2250 }
2251 
2252 static __be32
2253 nfsd4_decode_clone(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
2254 {
2255 	struct nfsd4_clone *clone = &u->clone;
2256 	__be32 status;
2257 
2258 	status = nfsd4_decode_stateid4(argp, &clone->cl_src_stateid);
2259 	if (status)
2260 		return status;
2261 	status = nfsd4_decode_stateid4(argp, &clone->cl_dst_stateid);
2262 	if (status)
2263 		return status;
2264 	if (xdr_stream_decode_u64(argp->xdr, &clone->cl_src_pos) < 0)
2265 		return nfserr_bad_xdr;
2266 	if (xdr_stream_decode_u64(argp->xdr, &clone->cl_dst_pos) < 0)
2267 		return nfserr_bad_xdr;
2268 	if (xdr_stream_decode_u64(argp->xdr, &clone->cl_count) < 0)
2269 		return nfserr_bad_xdr;
2270 
2271 	return nfs_ok;
2272 }
2273 
2274 /*
2275  * XDR data that is more than PAGE_SIZE in size is normally part of a
2276  * read or write. However, the size of extended attributes is limited
2277  * by the maximum request size, and then further limited by the underlying
2278  * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
2279  * is 64k). Since there is no kvec- or page-based interface to xattrs,
2280  * and we're not dealing with contiguous pages, we need to do some copying.
2281  */
2282 
2283 /*
2284  * Decode data into buffer.
2285  */
2286 static __be32
2287 nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
2288 		       char **bufp, size_t buflen)
2289 {
2290 	struct page **pages = xdr->pages;
2291 	struct kvec *head = xdr->head;
2292 	char *tmp, *dp;
2293 	u32 len;
2294 
2295 	if (buflen <= head->iov_len) {
2296 		/*
2297 		 * We're in luck, the head has enough space. Just return
2298 		 * the head, no need for copying.
2299 		 */
2300 		*bufp = head->iov_base;
2301 		return 0;
2302 	}
2303 
2304 	tmp = svcxdr_tmpalloc(argp, buflen);
2305 	if (tmp == NULL)
2306 		return nfserr_jukebox;
2307 
2308 	dp = tmp;
2309 	memcpy(dp, head->iov_base, head->iov_len);
2310 	buflen -= head->iov_len;
2311 	dp += head->iov_len;
2312 
2313 	while (buflen > 0) {
2314 		len = min_t(u32, buflen, PAGE_SIZE);
2315 		memcpy(dp, page_address(*pages), len);
2316 
2317 		buflen -= len;
2318 		dp += len;
2319 		pages++;
2320 	}
2321 
2322 	*bufp = tmp;
2323 	return 0;
2324 }
2325 
2326 /*
2327  * Get a user extended attribute name from the XDR buffer.
2328  * It will not have the "user." prefix, so prepend it.
2329  * Lastly, check for nul characters in the name.
2330  */
2331 static __be32
2332 nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2333 {
2334 	char *name, *sp, *dp;
2335 	u32 namelen, cnt;
2336 	__be32 *p;
2337 
2338 	if (xdr_stream_decode_u32(argp->xdr, &namelen) < 0)
2339 		return nfserr_bad_xdr;
2340 	if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2341 		return nfserr_nametoolong;
2342 	if (namelen == 0)
2343 		return nfserr_bad_xdr;
2344 	p = xdr_inline_decode(argp->xdr, namelen);
2345 	if (!p)
2346 		return nfserr_bad_xdr;
2347 	name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2348 	if (!name)
2349 		return nfserr_jukebox;
2350 	memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2351 
2352 	/*
2353 	 * Copy the extended attribute name over while checking for 0
2354 	 * characters.
2355 	 */
2356 	sp = (char *)p;
2357 	dp = name + XATTR_USER_PREFIX_LEN;
2358 	cnt = namelen;
2359 
2360 	while (cnt-- > 0) {
2361 		if (*sp == '\0')
2362 			return nfserr_bad_xdr;
2363 		*dp++ = *sp++;
2364 	}
2365 	*dp = '\0';
2366 
2367 	*namep = name;
2368 
2369 	return nfs_ok;
2370 }
2371 
2372 /*
2373  * A GETXATTR op request comes without a length specifier. We just set the
2374  * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2375  * channel reply size. nfsd_getxattr will probe the length of the xattr,
2376  * check it against getxa_len, and allocate + return the value.
2377  */
2378 static __be32
2379 nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2380 		      union nfsd4_op_u *u)
2381 {
2382 	struct nfsd4_getxattr *getxattr = &u->getxattr;
2383 	__be32 status;
2384 	u32 maxcount;
2385 
2386 	memset(getxattr, 0, sizeof(*getxattr));
2387 	status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2388 	if (status)
2389 		return status;
2390 
2391 	maxcount = svc_max_payload(argp->rqstp);
2392 	maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2393 
2394 	getxattr->getxa_len = maxcount;
2395 	return nfs_ok;
2396 }
2397 
2398 static __be32
2399 nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2400 		      union nfsd4_op_u *u)
2401 {
2402 	struct nfsd4_setxattr *setxattr = &u->setxattr;
2403 	u32 flags, maxcount, size;
2404 	__be32 status;
2405 
2406 	memset(setxattr, 0, sizeof(*setxattr));
2407 
2408 	if (xdr_stream_decode_u32(argp->xdr, &flags) < 0)
2409 		return nfserr_bad_xdr;
2410 
2411 	if (flags > SETXATTR4_REPLACE)
2412 		return nfserr_inval;
2413 	setxattr->setxa_flags = flags;
2414 
2415 	status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2416 	if (status)
2417 		return status;
2418 
2419 	maxcount = svc_max_payload(argp->rqstp);
2420 	maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2421 
2422 	if (xdr_stream_decode_u32(argp->xdr, &size) < 0)
2423 		return nfserr_bad_xdr;
2424 	if (size > maxcount)
2425 		return nfserr_xattr2big;
2426 
2427 	setxattr->setxa_len = size;
2428 	if (size > 0) {
2429 		struct xdr_buf payload;
2430 
2431 		if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2432 			return nfserr_bad_xdr;
2433 		status = nfsd4_vbuf_from_vector(argp, &payload,
2434 						&setxattr->setxa_buf, size);
2435 	}
2436 
2437 	return nfs_ok;
2438 }
2439 
2440 static __be32
2441 nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2442 			union nfsd4_op_u *u)
2443 {
2444 	struct nfsd4_listxattrs *listxattrs = &u->listxattrs;
2445 	u32 maxcount;
2446 
2447 	memset(listxattrs, 0, sizeof(*listxattrs));
2448 
2449 	if (xdr_stream_decode_u64(argp->xdr, &listxattrs->lsxa_cookie) < 0)
2450 		return nfserr_bad_xdr;
2451 
2452 	/*
2453 	 * If the cookie  is too large to have even one user.x attribute
2454 	 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2455 	 */
2456 	if (listxattrs->lsxa_cookie >=
2457 	    (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2458 		return nfserr_badcookie;
2459 
2460 	if (xdr_stream_decode_u32(argp->xdr, &maxcount) < 0)
2461 		return nfserr_bad_xdr;
2462 	if (maxcount < 8)
2463 		/* Always need at least 2 words (length and one character) */
2464 		return nfserr_inval;
2465 
2466 	maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2467 	listxattrs->lsxa_maxcount = maxcount;
2468 
2469 	return nfs_ok;
2470 }
2471 
2472 static __be32
2473 nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2474 			 union nfsd4_op_u *u)
2475 {
2476 	struct nfsd4_removexattr *removexattr = &u->removexattr;
2477 	memset(removexattr, 0, sizeof(*removexattr));
2478 	return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2479 }
2480 
2481 static __be32
2482 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, union nfsd4_op_u *p)
2483 {
2484 	return nfs_ok;
2485 }
2486 
2487 static __be32
2488 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, union nfsd4_op_u *p)
2489 {
2490 	return nfserr_notsupp;
2491 }
2492 
2493 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u);
2494 
2495 static const nfsd4_dec nfsd4_dec_ops[] = {
2496 	[OP_ACCESS]		= nfsd4_decode_access,
2497 	[OP_CLOSE]		= nfsd4_decode_close,
2498 	[OP_COMMIT]		= nfsd4_decode_commit,
2499 	[OP_CREATE]		= nfsd4_decode_create,
2500 	[OP_DELEGPURGE]		= nfsd4_decode_notsupp,
2501 	[OP_DELEGRETURN]	= nfsd4_decode_delegreturn,
2502 	[OP_GETATTR]		= nfsd4_decode_getattr,
2503 	[OP_GETFH]		= nfsd4_decode_noop,
2504 	[OP_LINK]		= nfsd4_decode_link,
2505 	[OP_LOCK]		= nfsd4_decode_lock,
2506 	[OP_LOCKT]		= nfsd4_decode_lockt,
2507 	[OP_LOCKU]		= nfsd4_decode_locku,
2508 	[OP_LOOKUP]		= nfsd4_decode_lookup,
2509 	[OP_LOOKUPP]		= nfsd4_decode_noop,
2510 	[OP_NVERIFY]		= nfsd4_decode_verify,
2511 	[OP_OPEN]		= nfsd4_decode_open,
2512 	[OP_OPENATTR]		= nfsd4_decode_notsupp,
2513 	[OP_OPEN_CONFIRM]	= nfsd4_decode_open_confirm,
2514 	[OP_OPEN_DOWNGRADE]	= nfsd4_decode_open_downgrade,
2515 	[OP_PUTFH]		= nfsd4_decode_putfh,
2516 	[OP_PUTPUBFH]		= nfsd4_decode_noop,
2517 	[OP_PUTROOTFH]		= nfsd4_decode_noop,
2518 	[OP_READ]		= nfsd4_decode_read,
2519 	[OP_READDIR]		= nfsd4_decode_readdir,
2520 	[OP_READLINK]		= nfsd4_decode_noop,
2521 	[OP_REMOVE]		= nfsd4_decode_remove,
2522 	[OP_RENAME]		= nfsd4_decode_rename,
2523 	[OP_RENEW]		= nfsd4_decode_renew,
2524 	[OP_RESTOREFH]		= nfsd4_decode_noop,
2525 	[OP_SAVEFH]		= nfsd4_decode_noop,
2526 	[OP_SECINFO]		= nfsd4_decode_secinfo,
2527 	[OP_SETATTR]		= nfsd4_decode_setattr,
2528 	[OP_SETCLIENTID]	= nfsd4_decode_setclientid,
2529 	[OP_SETCLIENTID_CONFIRM] = nfsd4_decode_setclientid_confirm,
2530 	[OP_VERIFY]		= nfsd4_decode_verify,
2531 	[OP_WRITE]		= nfsd4_decode_write,
2532 	[OP_RELEASE_LOCKOWNER]	= nfsd4_decode_release_lockowner,
2533 
2534 	/* new operations for NFSv4.1 */
2535 	[OP_BACKCHANNEL_CTL]	= nfsd4_decode_backchannel_ctl,
2536 	[OP_BIND_CONN_TO_SESSION] = nfsd4_decode_bind_conn_to_session,
2537 	[OP_EXCHANGE_ID]	= nfsd4_decode_exchange_id,
2538 	[OP_CREATE_SESSION]	= nfsd4_decode_create_session,
2539 	[OP_DESTROY_SESSION]	= nfsd4_decode_destroy_session,
2540 	[OP_FREE_STATEID]	= nfsd4_decode_free_stateid,
2541 	[OP_GET_DIR_DELEGATION]	= nfsd4_decode_get_dir_delegation,
2542 #ifdef CONFIG_NFSD_PNFS
2543 	[OP_GETDEVICEINFO]	= nfsd4_decode_getdeviceinfo,
2544 	[OP_GETDEVICELIST]	= nfsd4_decode_notsupp,
2545 	[OP_LAYOUTCOMMIT]	= nfsd4_decode_layoutcommit,
2546 	[OP_LAYOUTGET]		= nfsd4_decode_layoutget,
2547 	[OP_LAYOUTRETURN]	= nfsd4_decode_layoutreturn,
2548 #else
2549 	[OP_GETDEVICEINFO]	= nfsd4_decode_notsupp,
2550 	[OP_GETDEVICELIST]	= nfsd4_decode_notsupp,
2551 	[OP_LAYOUTCOMMIT]	= nfsd4_decode_notsupp,
2552 	[OP_LAYOUTGET]		= nfsd4_decode_notsupp,
2553 	[OP_LAYOUTRETURN]	= nfsd4_decode_notsupp,
2554 #endif
2555 	[OP_SECINFO_NO_NAME]	= nfsd4_decode_secinfo_no_name,
2556 	[OP_SEQUENCE]		= nfsd4_decode_sequence,
2557 	[OP_SET_SSV]		= nfsd4_decode_notsupp,
2558 	[OP_TEST_STATEID]	= nfsd4_decode_test_stateid,
2559 	[OP_WANT_DELEGATION]	= nfsd4_decode_notsupp,
2560 	[OP_DESTROY_CLIENTID]	= nfsd4_decode_destroy_clientid,
2561 	[OP_RECLAIM_COMPLETE]	= nfsd4_decode_reclaim_complete,
2562 
2563 	/* new operations for NFSv4.2 */
2564 	[OP_ALLOCATE]		= nfsd4_decode_fallocate,
2565 	[OP_COPY]		= nfsd4_decode_copy,
2566 	[OP_COPY_NOTIFY]	= nfsd4_decode_copy_notify,
2567 	[OP_DEALLOCATE]		= nfsd4_decode_fallocate,
2568 	[OP_IO_ADVISE]		= nfsd4_decode_notsupp,
2569 	[OP_LAYOUTERROR]	= nfsd4_decode_notsupp,
2570 	[OP_LAYOUTSTATS]	= nfsd4_decode_notsupp,
2571 	[OP_OFFLOAD_CANCEL]	= nfsd4_decode_offload_status,
2572 	[OP_OFFLOAD_STATUS]	= nfsd4_decode_offload_status,
2573 	[OP_READ_PLUS]		= nfsd4_decode_read,
2574 	[OP_SEEK]		= nfsd4_decode_seek,
2575 	[OP_WRITE_SAME]		= nfsd4_decode_notsupp,
2576 	[OP_CLONE]		= nfsd4_decode_clone,
2577 	/* RFC 8276 extended atributes operations */
2578 	[OP_GETXATTR]		= nfsd4_decode_getxattr,
2579 	[OP_SETXATTR]		= nfsd4_decode_setxattr,
2580 	[OP_LISTXATTRS]		= nfsd4_decode_listxattrs,
2581 	[OP_REMOVEXATTR]	= nfsd4_decode_removexattr,
2582 };
2583 
2584 static inline bool
2585 nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2586 {
2587 	if (op->opnum < FIRST_NFS4_OP)
2588 		return false;
2589 	else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
2590 		return false;
2591 	else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2592 		return false;
2593 	else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
2594 		return false;
2595 	return true;
2596 }
2597 
2598 static bool
2599 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2600 {
2601 	struct nfsd_thread_local_info *ntli = argp->rqstp->rq_private;
2602 	struct nfsd4_op *op;
2603 	bool cachethis = false;
2604 	int auth_slack= argp->rqstp->rq_auth_slack;
2605 	int max_reply = auth_slack + 8; /* opcnt, status */
2606 	int readcount = 0;
2607 	int readbytes = 0;
2608 	__be32 *p;
2609 	int i;
2610 
2611 	if (xdr_stream_decode_u32(argp->xdr, &argp->taglen) < 0)
2612 		return false;
2613 	max_reply += XDR_UNIT;
2614 	argp->tag = NULL;
2615 	if (unlikely(argp->taglen)) {
2616 		if (argp->taglen > NFSD4_MAX_TAGLEN)
2617 			return false;
2618 		p = xdr_inline_decode(argp->xdr, argp->taglen);
2619 		if (!p)
2620 			return false;
2621 		argp->tag = svcxdr_savemem(argp, p, argp->taglen);
2622 		if (!argp->tag)
2623 			return false;
2624 		max_reply += xdr_align_size(argp->taglen);
2625 	}
2626 
2627 	if (xdr_stream_decode_u32(argp->xdr, &argp->minorversion) < 0)
2628 		return false;
2629 	if (xdr_stream_decode_u32(argp->xdr, &argp->client_opcnt) < 0)
2630 		return false;
2631 	argp->opcnt = min_t(u32, argp->client_opcnt,
2632 			    NFSD_MAX_OPS_PER_COMPOUND);
2633 
2634 	if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
2635 		argp->ops = vcalloc(argp->opcnt, sizeof(*argp->ops));
2636 		if (!argp->ops) {
2637 			argp->ops = argp->iops;
2638 			return false;
2639 		}
2640 	}
2641 
2642 	if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
2643 		argp->opcnt = 0;
2644 
2645 	for (i = 0; i < argp->opcnt; i++) {
2646 		op = &argp->ops[i];
2647 		op->replay = NULL;
2648 		op->opdesc = NULL;
2649 
2650 		if (xdr_stream_decode_u32(argp->xdr, &op->opnum) < 0)
2651 			return false;
2652 		if (nfsd4_opnum_in_range(argp, op)) {
2653 			op->opdesc = OPDESC(op);
2654 			op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
2655 			if (op->status != nfs_ok)
2656 				trace_nfsd_compound_decode_err(argp->rqstp,
2657 							       argp->opcnt, i,
2658 							       op->opnum,
2659 							       op->status);
2660 		} else {
2661 			op->opnum = OP_ILLEGAL;
2662 			op->status = nfserr_op_illegal;
2663 		}
2664 
2665 		/*
2666 		 * We'll try to cache the result in the DRC if any one
2667 		 * op in the compound wants to be cached:
2668 		 */
2669 		cachethis |= nfsd4_cache_this_op(op);
2670 
2671 		if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
2672 			readcount++;
2673 			readbytes += nfsd4_max_reply(argp->rqstp, op);
2674 		} else
2675 			max_reply += nfsd4_max_reply(argp->rqstp, op);
2676 		/*
2677 		 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2678 		 * (Special case because it will just skip encoding this
2679 		 * if it runs out of xdr buffer space, and it is the only
2680 		 * operation that behaves this way.)
2681 		 */
2682 		if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
2683 			max_reply += NFS4_OPAQUE_LIMIT;
2684 
2685 		if (op->status) {
2686 			argp->opcnt = i+1;
2687 			break;
2688 		}
2689 	}
2690 	/* Sessions make the DRC unnecessary: */
2691 	if (argp->minorversion)
2692 		cachethis = false;
2693 	svc_reserve_auth(argp->rqstp, max_reply + readbytes);
2694 	ntli->ntli_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
2695 
2696 	argp->splice_ok = nfsd_read_splice_ok(argp->rqstp);
2697 	if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
2698 		argp->splice_ok = false;
2699 
2700 	return true;
2701 }
2702 
2703 static __be32 nfsd4_encode_nfs_fh4(struct xdr_stream *xdr,
2704 				   struct knfsd_fh *fh_handle)
2705 {
2706 	return nfsd4_encode_opaque(xdr, fh_handle->fh_raw, fh_handle->fh_size);
2707 }
2708 
2709 /* This is a frequently-encoded type; open-coded for speed */
2710 static __be32 nfsd4_encode_nfstime4(struct xdr_stream *xdr,
2711 				    const struct timespec64 *tv)
2712 {
2713 	__be32 *p;
2714 
2715 	p = xdr_reserve_space(xdr, XDR_UNIT * 3);
2716 	if (!p)
2717 		return nfserr_resource;
2718 	p = xdr_encode_hyper(p, tv->tv_sec);
2719 	*p = cpu_to_be32(tv->tv_nsec);
2720 	return nfs_ok;
2721 }
2722 
2723 static __be32 nfsd4_encode_specdata4(struct xdr_stream *xdr,
2724 				     unsigned int major, unsigned int minor)
2725 {
2726 	__be32 status;
2727 
2728 	status = nfsd4_encode_uint32_t(xdr, major);
2729 	if (status != nfs_ok)
2730 		return status;
2731 	return nfsd4_encode_uint32_t(xdr, minor);
2732 }
2733 
2734 static __be32
2735 nfsd4_encode_change_info4(struct xdr_stream *xdr, const struct nfsd4_change_info *c)
2736 {
2737 	__be32 status;
2738 
2739 	status = nfsd4_encode_bool(xdr, c->atomic);
2740 	if (status != nfs_ok)
2741 		return status;
2742 	status = nfsd4_encode_changeid4(xdr, c->before_change);
2743 	if (status != nfs_ok)
2744 		return status;
2745 	return nfsd4_encode_changeid4(xdr, c->after_change);
2746 }
2747 
2748 static __be32 nfsd4_encode_netaddr4(struct xdr_stream *xdr,
2749 				    const struct nfs42_netaddr *addr)
2750 {
2751 	__be32 status;
2752 
2753 	/* na_r_netid */
2754 	status = nfsd4_encode_opaque(xdr, addr->netid, addr->netid_len);
2755 	if (status != nfs_ok)
2756 		return status;
2757 	/* na_r_addr */
2758 	return nfsd4_encode_opaque(xdr, addr->addr, addr->addr_len);
2759 }
2760 
2761 /* Encode as an array of strings the string given with components
2762  * separated @sep, escaped with esc_enter and esc_exit.
2763  */
2764 static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2765 					  char *components, char esc_enter,
2766 					  char esc_exit)
2767 {
2768 	__be32 *p;
2769 	__be32 pathlen;
2770 	int pathlen_offset;
2771 	char *str, *end, *next;
2772 	int count = 0;
2773 
2774 	pathlen_offset = xdr->buf->len;
2775 	p = xdr_reserve_space(xdr, 4);
2776 	if (!p)
2777 		return nfserr_resource;
2778 	p++; /* We will fill this in with @count later */
2779 
2780 	end = str = components;
2781 	while (*end) {
2782 		bool found_esc = false;
2783 
2784 		/* try to parse as esc_start, ..., esc_end, sep */
2785 		if (*str == esc_enter) {
2786 			for (; *end && (*end != esc_exit); end++)
2787 				/* find esc_exit or end of string */;
2788 			next = end + 1;
2789 			if (*end && (!*next || *next == sep)) {
2790 				str++;
2791 				found_esc = true;
2792 			}
2793 		}
2794 
2795 		if (!found_esc)
2796 			for (; *end && (*end != sep); end++)
2797 				/* find sep or end of string */;
2798 
2799 		if (end > str) {
2800 			if (xdr_stream_encode_opaque(xdr, str, end - str) < 0)
2801 				return nfserr_resource;
2802 			count++;
2803 		} else
2804 			end++;
2805 		if (found_esc)
2806 			end = next;
2807 
2808 		str = end;
2809 	}
2810 	pathlen = htonl(count);
2811 	write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
2812 	return 0;
2813 }
2814 
2815 /* Encode as an array of strings the string given with components
2816  * separated @sep.
2817  */
2818 static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2819 				      char *components)
2820 {
2821 	return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
2822 }
2823 
2824 static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2825 					struct nfsd4_fs_location *location)
2826 {
2827 	__be32 status;
2828 
2829 	status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
2830 						'[', ']');
2831 	if (status)
2832 		return status;
2833 	status = nfsd4_encode_components(xdr, '/', location->path);
2834 	if (status)
2835 		return status;
2836 	return nfs_ok;
2837 }
2838 
2839 static __be32 nfsd4_encode_pathname4(struct xdr_stream *xdr,
2840 				     const struct path *root,
2841 				     const struct path *path)
2842 {
2843 	struct path cur = *path;
2844 	struct dentry **components = NULL;
2845 	unsigned int ncomponents = 0;
2846 	__be32 err = nfserr_jukebox;
2847 
2848 	dprintk("nfsd4_encode_components(");
2849 
2850 	path_get(&cur);
2851 	/* First walk the path up to the nfsd root, and store the
2852 	 * dentries/path components in an array.
2853 	 */
2854 	for (;;) {
2855 		if (path_equal(&cur, root))
2856 			break;
2857 		if (cur.dentry == cur.mnt->mnt_root) {
2858 			if (follow_up(&cur))
2859 				continue;
2860 			goto out_free;
2861 		}
2862 		if ((ncomponents & 15) == 0) {
2863 			struct dentry **new;
2864 			new = krealloc(components,
2865 					sizeof(*new) * (ncomponents + 16),
2866 					GFP_KERNEL);
2867 			if (!new)
2868 				goto out_free;
2869 			components = new;
2870 		}
2871 		components[ncomponents++] = cur.dentry;
2872 		cur.dentry = dget_parent(cur.dentry);
2873 	}
2874 
2875 	err = nfserr_resource;
2876 	if (xdr_stream_encode_u32(xdr, ncomponents) != XDR_UNIT)
2877 		goto out_free;
2878 	while (ncomponents) {
2879 		struct dentry *dentry = components[ncomponents - 1];
2880 
2881 		spin_lock(&dentry->d_lock);
2882 		if (xdr_stream_encode_opaque(xdr, dentry->d_name.name,
2883 					     dentry->d_name.len) < 0) {
2884 			spin_unlock(&dentry->d_lock);
2885 			goto out_free;
2886 		}
2887 		dprintk("/%pd", dentry);
2888 		spin_unlock(&dentry->d_lock);
2889 		dput(dentry);
2890 		ncomponents--;
2891 	}
2892 
2893 	err = 0;
2894 out_free:
2895 	dprintk(")\n");
2896 	while (ncomponents)
2897 		dput(components[--ncomponents]);
2898 	kfree(components);
2899 	path_put(&cur);
2900 	return err;
2901 }
2902 
2903 static __be32 nfsd4_encode_fs_locations4(struct xdr_stream *xdr,
2904 					 struct svc_rqst *rqstp,
2905 					 struct svc_export *exp)
2906 {
2907 	struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
2908 	struct svc_export *exp_ps;
2909 	unsigned int i;
2910 	__be32 status;
2911 
2912 	/* fs_root */
2913 	exp_ps = rqst_find_fsidzero_export(rqstp);
2914 	if (IS_ERR(exp_ps))
2915 		return nfserrno(PTR_ERR(exp_ps));
2916 	status = nfsd4_encode_pathname4(xdr, &exp_ps->ex_path, &exp->ex_path);
2917 	exp_put(exp_ps);
2918 	if (status != nfs_ok)
2919 		return status;
2920 
2921 	/* locations<> */
2922 	if (xdr_stream_encode_u32(xdr, fslocs->locations_count) != XDR_UNIT)
2923 		return nfserr_resource;
2924 	for (i = 0; i < fslocs->locations_count; i++) {
2925 		status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
2926 		if (status != nfs_ok)
2927 			return status;
2928 	}
2929 
2930 	return nfs_ok;
2931 }
2932 
2933 static __be32 nfsd4_encode_nfsace4(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2934 				   struct nfs4_ace *ace)
2935 {
2936 	__be32 status;
2937 
2938 	/* type */
2939 	status = nfsd4_encode_acetype4(xdr, ace->type);
2940 	if (status != nfs_ok)
2941 		return nfserr_resource;
2942 	/* flag */
2943 	status = nfsd4_encode_aceflag4(xdr, ace->flag);
2944 	if (status != nfs_ok)
2945 		return nfserr_resource;
2946 	/* access mask */
2947 	status = nfsd4_encode_acemask4(xdr, ace->access_mask & NFS4_ACE_MASK_ALL);
2948 	if (status != nfs_ok)
2949 		return nfserr_resource;
2950 	/* who */
2951 	if (ace->whotype != NFS4_ACL_WHO_NAMED)
2952 		return nfs4_acl_write_who(xdr, ace->whotype);
2953 	if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
2954 		return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
2955 	return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
2956 }
2957 
2958 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2959 			      FATTR4_WORD0_RDATTR_ERROR)
2960 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2961 #define WORD2_ABSENT_FS_ATTRS 0
2962 
2963 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2964 static inline __be32
2965 nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2966 			    const struct lsm_context *context)
2967 {
2968 	__be32 *p;
2969 
2970 	p = xdr_reserve_space(xdr, context->len + 4 + 4 + 4);
2971 	if (!p)
2972 		return nfserr_resource;
2973 
2974 	/*
2975 	 * For now we use a 0 here to indicate the null translation; in
2976 	 * the future we may place a call to translation code here.
2977 	 */
2978 	*p++ = cpu_to_be32(0); /* lfs */
2979 	*p++ = cpu_to_be32(0); /* pi */
2980 	p = xdr_encode_opaque(p, context->context, context->len);
2981 	return 0;
2982 }
2983 #else
2984 static inline __be32
2985 nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2986 			    struct lsm_context *context)
2987 { return 0; }
2988 #endif
2989 
2990 #ifdef CONFIG_NFSD_V4_POSIX_ACLS
2991 
2992 static int nfsd4_posix_tagtotype(short tag)
2993 {
2994 	switch (tag) {
2995 	case ACL_USER_OBJ:	return POSIXACE4_TAG_USER_OBJ;
2996 	case ACL_GROUP_OBJ:	return POSIXACE4_TAG_GROUP_OBJ;
2997 	case ACL_USER:		return POSIXACE4_TAG_USER;
2998 	case ACL_GROUP:		return POSIXACE4_TAG_GROUP;
2999 	case ACL_MASK:		return POSIXACE4_TAG_MASK;
3000 	case ACL_OTHER:		return POSIXACE4_TAG_OTHER;
3001 	default:		return -EINVAL;
3002 	}
3003 }
3004 
3005 static __be32
3006 nfsd4_encode_posixace4(struct xdr_stream *xdr, struct svc_rqst *rqstp,
3007 		       struct posix_acl_entry *acep)
3008 {
3009 	__be32 status;
3010 	int type;
3011 
3012 	type = nfsd4_posix_tagtotype(acep->e_tag);
3013 	if (type < 0)
3014 		return nfserr_resource;
3015 	if (!xdrgen_encode_posixacetag4(xdr, type))
3016 		return nfserr_resource;
3017 	if (!xdrgen_encode_posixaceperm4(xdr, acep->e_perm))
3018 		return nfserr_resource;
3019 
3020 	/* who */
3021 	switch (acep->e_tag) {
3022 	case ACL_USER_OBJ:
3023 	case ACL_GROUP_OBJ:
3024 	case ACL_MASK:
3025 	case ACL_OTHER:
3026 		if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
3027 			return nfserr_resource;
3028 		break;
3029 	case ACL_USER:
3030 		status = nfsd4_encode_user(xdr, rqstp, acep->e_uid);
3031 		if (status != nfs_ok)
3032 			return status;
3033 		break;
3034 	case ACL_GROUP:
3035 		status = nfsd4_encode_group(xdr, rqstp, acep->e_gid);
3036 		if (status != nfs_ok)
3037 			return status;
3038 		break;
3039 	default:
3040 		return nfserr_resource;
3041 	}
3042 	return nfs_ok;
3043 }
3044 
3045 static __be32
3046 nfsd4_encode_posixacl(struct xdr_stream *xdr, struct svc_rqst *rqstp,
3047 		      struct posix_acl *acl)
3048 {
3049 	__be32 status;
3050 	int i;
3051 
3052 	if (!acl) {
3053 		if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
3054 			return nfserr_resource;
3055 		return nfs_ok;
3056 	}
3057 
3058 	if (acl->a_count > NFS_ACL_MAX_ENTRIES)
3059 		return nfserr_resource;
3060 	if (xdr_stream_encode_u32(xdr, acl->a_count) != XDR_UNIT)
3061 		return nfserr_resource;
3062 	for (i = 0; i < acl->a_count; i++) {
3063 		status = nfsd4_encode_posixace4(xdr, rqstp, &acl->a_entries[i]);
3064 		if (status != nfs_ok)
3065 			return status;
3066 	}
3067 
3068 	return nfs_ok;
3069 }
3070 
3071 #endif /* CONFIG_NFSD_V4_POSIX_ACL */
3072 
3073 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
3074 {
3075 	/* As per referral draft:  */
3076 	if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
3077 	    *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
3078 		if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
3079 	            *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
3080 			*rdattr_err = NFSERR_MOVED;
3081 		else
3082 			return nfserr_moved;
3083 	}
3084 	*bmval0 &= WORD0_ABSENT_FS_ATTRS;
3085 	*bmval1 &= WORD1_ABSENT_FS_ATTRS;
3086 	*bmval2 &= WORD2_ABSENT_FS_ATTRS;
3087 	return 0;
3088 }
3089 
3090 
3091 static int nfsd4_get_mounted_on_ino(struct svc_export *exp, u64 *pino)
3092 {
3093 	struct path path = exp->ex_path;
3094 	struct kstat stat;
3095 	int err;
3096 
3097 	path_get(&path);
3098 	while (follow_up(&path)) {
3099 		if (path.dentry != path.mnt->mnt_root)
3100 			break;
3101 	}
3102 	err = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
3103 	path_put(&path);
3104 	if (!err)
3105 		*pino = stat.ino;
3106 	return err;
3107 }
3108 
3109 static __be32
3110 nfsd4_encode_bitmap4(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
3111 {
3112 	__be32 *p;
3113 
3114 	if (bmval2) {
3115 		p = xdr_reserve_space(xdr, XDR_UNIT * 4);
3116 		if (!p)
3117 			goto out_resource;
3118 		*p++ = cpu_to_be32(3);
3119 		*p++ = cpu_to_be32(bmval0);
3120 		*p++ = cpu_to_be32(bmval1);
3121 		*p++ = cpu_to_be32(bmval2);
3122 	} else if (bmval1) {
3123 		p = xdr_reserve_space(xdr, XDR_UNIT * 3);
3124 		if (!p)
3125 			goto out_resource;
3126 		*p++ = cpu_to_be32(2);
3127 		*p++ = cpu_to_be32(bmval0);
3128 		*p++ = cpu_to_be32(bmval1);
3129 	} else {
3130 		p = xdr_reserve_space(xdr, XDR_UNIT * 2);
3131 		if (!p)
3132 			goto out_resource;
3133 		*p++ = cpu_to_be32(1);
3134 		*p++ = cpu_to_be32(bmval0);
3135 	}
3136 
3137 	return nfs_ok;
3138 out_resource:
3139 	return nfserr_resource;
3140 }
3141 
3142 struct nfsd4_fattr_args {
3143 	struct svc_rqst		*rqstp;
3144 	struct svc_fh		*fhp;
3145 	struct svc_export	*exp;
3146 	struct dentry		*dentry;
3147 	struct kstat		stat;
3148 	struct kstatfs		statfs;
3149 	struct nfs4_acl		*acl;
3150 	u64			change_attr;
3151 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3152 	struct lsm_context	context;
3153 #endif
3154 #ifdef CONFIG_NFSD_V4_POSIX_ACLS
3155 	struct posix_acl	*dpacl;
3156 	struct posix_acl	*pacl;
3157 #endif
3158 	u32			rdattr_err;
3159 	bool			contextsupport;
3160 	bool			ignore_crossmnt;
3161 	bool			case_insensitive;
3162 	bool			case_preserving;
3163 };
3164 
3165 typedef __be32(*nfsd4_enc_attr)(struct xdr_stream *xdr,
3166 				const struct nfsd4_fattr_args *args);
3167 
3168 static __be32 nfsd4_encode_fattr4__inval(struct xdr_stream *xdr,
3169 					 const struct nfsd4_fattr_args *args)
3170 {
3171 	return nfserr_inval;
3172 }
3173 
3174 static __be32 nfsd4_encode_fattr4__noop(struct xdr_stream *xdr,
3175 					const struct nfsd4_fattr_args *args)
3176 {
3177 	return nfs_ok;
3178 }
3179 
3180 static __be32 nfsd4_encode_fattr4__true(struct xdr_stream *xdr,
3181 					const struct nfsd4_fattr_args *args)
3182 {
3183 	return nfsd4_encode_bool(xdr, true);
3184 }
3185 
3186 static __be32 nfsd4_encode_fattr4__false(struct xdr_stream *xdr,
3187 					 const struct nfsd4_fattr_args *args)
3188 {
3189 	return nfsd4_encode_bool(xdr, false);
3190 }
3191 
3192 static __be32 nfsd4_encode_fattr4_supported_attrs(struct xdr_stream *xdr,
3193 						  const struct nfsd4_fattr_args *args)
3194 {
3195 	struct nfsd4_compoundres *resp = args->rqstp->rq_resp;
3196 	u32 minorversion = resp->cstate.minorversion;
3197 	u32 supp[3];
3198 
3199 	memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3200 	if (!IS_POSIXACL(d_inode(args->dentry)))
3201 		supp[0] &= ~FATTR4_WORD0_ACL;
3202 	if (!args->contextsupport)
3203 		supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3204 
3205 	return nfsd4_encode_bitmap4(xdr, supp[0], supp[1], supp[2]);
3206 }
3207 
3208 static __be32 nfsd4_encode_fattr4_type(struct xdr_stream *xdr,
3209 				       const struct nfsd4_fattr_args *args)
3210 {
3211 	__be32 *p;
3212 
3213 	p = xdr_reserve_space(xdr, XDR_UNIT);
3214 	if (!p)
3215 		return nfserr_resource;
3216 
3217 	switch (args->stat.mode & S_IFMT) {
3218 	case S_IFIFO:
3219 		*p = cpu_to_be32(NF4FIFO);
3220 		break;
3221 	case S_IFCHR:
3222 		*p = cpu_to_be32(NF4CHR);
3223 		break;
3224 	case S_IFDIR:
3225 		*p = cpu_to_be32(NF4DIR);
3226 		break;
3227 	case S_IFBLK:
3228 		*p = cpu_to_be32(NF4BLK);
3229 		break;
3230 	case S_IFLNK:
3231 		*p = cpu_to_be32(NF4LNK);
3232 		break;
3233 	case S_IFREG:
3234 		*p = cpu_to_be32(NF4REG);
3235 		break;
3236 	case S_IFSOCK:
3237 		*p = cpu_to_be32(NF4SOCK);
3238 		break;
3239 	default:
3240 		return nfserr_serverfault;
3241 	}
3242 
3243 	return nfs_ok;
3244 }
3245 
3246 static __be32 nfsd4_encode_fattr4_fh_expire_type(struct xdr_stream *xdr,
3247 						 const struct nfsd4_fattr_args *args)
3248 {
3249 	u32 mask;
3250 
3251 	mask = NFS4_FH_PERSISTENT;
3252 	if (!(args->exp->ex_flags & NFSEXP_NOSUBTREECHECK))
3253 		mask |= NFS4_FH_VOL_RENAME;
3254 	return nfsd4_encode_uint32_t(xdr, mask);
3255 }
3256 
3257 static __be32 nfsd4_encode_fattr4_change(struct xdr_stream *xdr,
3258 					 const struct nfsd4_fattr_args *args)
3259 {
3260 	const struct svc_export *exp = args->exp;
3261 
3262 	if (unlikely(exp->ex_flags & NFSEXP_V4ROOT)) {
3263 		u32 flush_time = convert_to_wallclock(exp->cd->flush_time);
3264 
3265 		if (xdr_stream_encode_u32(xdr, flush_time) != XDR_UNIT)
3266 			return nfserr_resource;
3267 		if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
3268 			return nfserr_resource;
3269 		return nfs_ok;
3270 	}
3271 	return nfsd4_encode_changeid4(xdr, args->change_attr);
3272 }
3273 
3274 static __be32 nfsd4_encode_fattr4_size(struct xdr_stream *xdr,
3275 				       const struct nfsd4_fattr_args *args)
3276 {
3277 	return nfsd4_encode_uint64_t(xdr, args->stat.size);
3278 }
3279 
3280 static __be32 nfsd4_encode_fattr4_fsid(struct xdr_stream *xdr,
3281 				       const struct nfsd4_fattr_args *args)
3282 {
3283 	__be32 *p;
3284 
3285 	p = xdr_reserve_space(xdr, XDR_UNIT * 2 + XDR_UNIT * 2);
3286 	if (!p)
3287 		return nfserr_resource;
3288 
3289 	if (unlikely(args->exp->ex_fslocs.migrated)) {
3290 		p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
3291 		xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
3292 		return nfs_ok;
3293 	}
3294 	switch (fsid_source(args->fhp)) {
3295 	case FSIDSOURCE_FSID:
3296 		p = xdr_encode_hyper(p, (u64)args->exp->ex_fsid);
3297 		xdr_encode_hyper(p, (u64)0);
3298 		break;
3299 	case FSIDSOURCE_DEV:
3300 		*p++ = xdr_zero;
3301 		*p++ = cpu_to_be32(MAJOR(args->stat.dev));
3302 		*p++ = xdr_zero;
3303 		*p   = cpu_to_be32(MINOR(args->stat.dev));
3304 		break;
3305 	case FSIDSOURCE_UUID:
3306 		xdr_encode_opaque_fixed(p, args->exp->ex_uuid, EX_UUID_LEN);
3307 		break;
3308 	}
3309 
3310 	return nfs_ok;
3311 }
3312 
3313 static __be32 nfsd4_encode_fattr4_lease_time(struct xdr_stream *xdr,
3314 					     const struct nfsd4_fattr_args *args)
3315 {
3316 	struct nfsd_net *nn = net_generic(SVC_NET(args->rqstp), nfsd_net_id);
3317 
3318 	return nfsd4_encode_nfs_lease4(xdr, nn->nfsd4_lease);
3319 }
3320 
3321 static __be32 nfsd4_encode_fattr4_rdattr_error(struct xdr_stream *xdr,
3322 					       const struct nfsd4_fattr_args *args)
3323 {
3324 	return nfsd4_encode_uint32_t(xdr, args->rdattr_err);
3325 }
3326 
3327 static __be32 nfsd4_encode_fattr4_aclsupport(struct xdr_stream *xdr,
3328 					     const struct nfsd4_fattr_args *args)
3329 {
3330 	u32 mask;
3331 
3332 	mask = 0;
3333 	if (IS_POSIXACL(d_inode(args->dentry)))
3334 		mask = ACL4_SUPPORT_ALLOW_ACL | ACL4_SUPPORT_DENY_ACL;
3335 	return nfsd4_encode_uint32_t(xdr, mask);
3336 }
3337 
3338 static __be32 nfsd4_encode_fattr4_acl(struct xdr_stream *xdr,
3339 				      const struct nfsd4_fattr_args *args)
3340 {
3341 	struct nfs4_acl *acl = args->acl;
3342 	struct nfs4_ace *ace;
3343 	__be32 status;
3344 
3345 	/* nfsace4<> */
3346 	if (!acl) {
3347 		if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
3348 			return nfserr_resource;
3349 	} else {
3350 		if (xdr_stream_encode_u32(xdr, acl->naces) != XDR_UNIT)
3351 			return nfserr_resource;
3352 		for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
3353 			status = nfsd4_encode_nfsace4(xdr, args->rqstp, ace);
3354 			if (status != nfs_ok)
3355 				return status;
3356 		}
3357 	}
3358 	return nfs_ok;
3359 }
3360 
3361 static __be32 nfsd4_encode_fattr4_case_insensitive(struct xdr_stream *xdr,
3362 					const struct nfsd4_fattr_args *args)
3363 {
3364 	return nfsd4_encode_bool(xdr, args->case_insensitive);
3365 }
3366 
3367 static __be32 nfsd4_encode_fattr4_case_preserving(struct xdr_stream *xdr,
3368 					const struct nfsd4_fattr_args *args)
3369 {
3370 	return nfsd4_encode_bool(xdr, args->case_preserving);
3371 }
3372 
3373 static __be32 nfsd4_encode_fattr4_homogeneous(struct xdr_stream *xdr,
3374 					const struct nfsd4_fattr_args *args)
3375 {
3376 	/*
3377 	 * Casefold-capable filesystems (e.g. ext4 or f2fs with the
3378 	 * casefold feature) attach a Unicode encoding at mount time
3379 	 * but apply case folding per directory.  The per-file-system
3380 	 * case_insensitive and case_preserving values can therefore
3381 	 * legitimately differ across objects that share the same fsid.
3382 	 * Report FATTR4_HOMOGENEOUS = FALSE on such filesystems to
3383 	 * keep that variation consistent with RFC 8881 Section 5.8.2.16.
3384 	 */
3385 	return nfsd4_encode_bool(xdr, !sb_has_encoding(args->dentry->d_sb));
3386 }
3387 
3388 static __be32 nfsd4_encode_fattr4_filehandle(struct xdr_stream *xdr,
3389 					     const struct nfsd4_fattr_args *args)
3390 {
3391 	return nfsd4_encode_nfs_fh4(xdr, &args->fhp->fh_handle);
3392 }
3393 
3394 static __be32 nfsd4_encode_fattr4_fileid(struct xdr_stream *xdr,
3395 					 const struct nfsd4_fattr_args *args)
3396 {
3397 	return nfsd4_encode_uint64_t(xdr, args->stat.ino);
3398 }
3399 
3400 static __be32 nfsd4_encode_fattr4_files_avail(struct xdr_stream *xdr,
3401 					      const struct nfsd4_fattr_args *args)
3402 {
3403 	return nfsd4_encode_uint64_t(xdr, args->statfs.f_ffree);
3404 }
3405 
3406 static __be32 nfsd4_encode_fattr4_files_free(struct xdr_stream *xdr,
3407 					     const struct nfsd4_fattr_args *args)
3408 {
3409 	return nfsd4_encode_uint64_t(xdr, args->statfs.f_ffree);
3410 }
3411 
3412 static __be32 nfsd4_encode_fattr4_files_total(struct xdr_stream *xdr,
3413 					      const struct nfsd4_fattr_args *args)
3414 {
3415 	return nfsd4_encode_uint64_t(xdr, args->statfs.f_files);
3416 }
3417 
3418 static __be32 nfsd4_encode_fattr4_fs_locations(struct xdr_stream *xdr,
3419 					       const struct nfsd4_fattr_args *args)
3420 {
3421 	return nfsd4_encode_fs_locations4(xdr, args->rqstp, args->exp);
3422 }
3423 
3424 static __be32 nfsd4_encode_fattr4_maxfilesize(struct xdr_stream *xdr,
3425 					      const struct nfsd4_fattr_args *args)
3426 {
3427 	struct super_block *sb = args->exp->ex_path.mnt->mnt_sb;
3428 
3429 	return nfsd4_encode_uint64_t(xdr, sb->s_maxbytes);
3430 }
3431 
3432 static __be32 nfsd4_encode_fattr4_maxlink(struct xdr_stream *xdr,
3433 					  const struct nfsd4_fattr_args *args)
3434 {
3435 	return nfsd4_encode_uint32_t(xdr, 255);
3436 }
3437 
3438 static __be32 nfsd4_encode_fattr4_maxname(struct xdr_stream *xdr,
3439 					  const struct nfsd4_fattr_args *args)
3440 {
3441 	return nfsd4_encode_uint32_t(xdr, args->statfs.f_namelen);
3442 }
3443 
3444 static __be32 nfsd4_encode_fattr4_maxread(struct xdr_stream *xdr,
3445 					  const struct nfsd4_fattr_args *args)
3446 {
3447 	return nfsd4_encode_uint64_t(xdr, svc_max_payload(args->rqstp));
3448 }
3449 
3450 static __be32 nfsd4_encode_fattr4_maxwrite(struct xdr_stream *xdr,
3451 					   const struct nfsd4_fattr_args *args)
3452 {
3453 	return nfsd4_encode_uint64_t(xdr, svc_max_payload(args->rqstp));
3454 }
3455 
3456 static __be32 nfsd4_encode_fattr4_mode(struct xdr_stream *xdr,
3457 				       const struct nfsd4_fattr_args *args)
3458 {
3459 	return nfsd4_encode_mode4(xdr, args->stat.mode & S_IALLUGO);
3460 }
3461 
3462 static __be32 nfsd4_encode_fattr4_numlinks(struct xdr_stream *xdr,
3463 					   const struct nfsd4_fattr_args *args)
3464 {
3465 	return nfsd4_encode_uint32_t(xdr, args->stat.nlink);
3466 }
3467 
3468 static __be32 nfsd4_encode_fattr4_owner(struct xdr_stream *xdr,
3469 					const struct nfsd4_fattr_args *args)
3470 {
3471 	return nfsd4_encode_user(xdr, args->rqstp, args->stat.uid);
3472 }
3473 
3474 static __be32 nfsd4_encode_fattr4_owner_group(struct xdr_stream *xdr,
3475 					      const struct nfsd4_fattr_args *args)
3476 {
3477 	return nfsd4_encode_group(xdr, args->rqstp, args->stat.gid);
3478 }
3479 
3480 static __be32 nfsd4_encode_fattr4_rawdev(struct xdr_stream *xdr,
3481 					 const struct nfsd4_fattr_args *args)
3482 {
3483 	return nfsd4_encode_specdata4(xdr, MAJOR(args->stat.rdev),
3484 				      MINOR(args->stat.rdev));
3485 }
3486 
3487 static __be32 nfsd4_encode_fattr4_space_avail(struct xdr_stream *xdr,
3488 					      const struct nfsd4_fattr_args *args)
3489 {
3490 	u64 avail = (u64)args->statfs.f_bavail * (u64)args->statfs.f_bsize;
3491 
3492 	return nfsd4_encode_uint64_t(xdr, avail);
3493 }
3494 
3495 static __be32 nfsd4_encode_fattr4_space_free(struct xdr_stream *xdr,
3496 					     const struct nfsd4_fattr_args *args)
3497 {
3498 	u64 free = (u64)args->statfs.f_bfree * (u64)args->statfs.f_bsize;
3499 
3500 	return nfsd4_encode_uint64_t(xdr, free);
3501 }
3502 
3503 static __be32 nfsd4_encode_fattr4_space_total(struct xdr_stream *xdr,
3504 					      const struct nfsd4_fattr_args *args)
3505 {
3506 	u64 total = (u64)args->statfs.f_blocks * (u64)args->statfs.f_bsize;
3507 
3508 	return nfsd4_encode_uint64_t(xdr, total);
3509 }
3510 
3511 static __be32 nfsd4_encode_fattr4_space_used(struct xdr_stream *xdr,
3512 					     const struct nfsd4_fattr_args *args)
3513 {
3514 	return nfsd4_encode_uint64_t(xdr, (u64)args->stat.blocks << 9);
3515 }
3516 
3517 static __be32 nfsd4_encode_fattr4_time_access(struct xdr_stream *xdr,
3518 					      const struct nfsd4_fattr_args *args)
3519 {
3520 	return nfsd4_encode_nfstime4(xdr, &args->stat.atime);
3521 }
3522 
3523 static __be32 nfsd4_encode_fattr4_time_create(struct xdr_stream *xdr,
3524 					      const struct nfsd4_fattr_args *args)
3525 {
3526 	return nfsd4_encode_nfstime4(xdr, &args->stat.btime);
3527 }
3528 
3529 /*
3530  * ctime (in NFSv4, time_metadata) is not writeable, and the client
3531  * doesn't really care what resolution could theoretically be stored by
3532  * the filesystem.
3533  *
3534  * The client cares how close together changes can be while still
3535  * guaranteeing ctime changes.  For most filesystems (which have
3536  * timestamps with nanosecond fields) that is limited by the resolution
3537  * of the time returned from current_time() (which I'm assuming to be
3538  * 1/HZ).
3539  */
3540 static __be32 nfsd4_encode_fattr4_time_delta(struct xdr_stream *xdr,
3541 					     const struct nfsd4_fattr_args *args)
3542 {
3543 	const struct inode *inode = d_inode(args->dentry);
3544 	u32 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
3545 	struct timespec64 ts = ns_to_timespec64(ns);
3546 
3547 	return nfsd4_encode_nfstime4(xdr, &ts);
3548 }
3549 
3550 static __be32 nfsd4_encode_fattr4_time_metadata(struct xdr_stream *xdr,
3551 						const struct nfsd4_fattr_args *args)
3552 {
3553 	return nfsd4_encode_nfstime4(xdr, &args->stat.ctime);
3554 }
3555 
3556 static __be32 nfsd4_encode_fattr4_time_modify(struct xdr_stream *xdr,
3557 					      const struct nfsd4_fattr_args *args)
3558 {
3559 	return nfsd4_encode_nfstime4(xdr, &args->stat.mtime);
3560 }
3561 
3562 static __be32 nfsd4_encode_fattr4_mounted_on_fileid(struct xdr_stream *xdr,
3563 						    const struct nfsd4_fattr_args *args)
3564 {
3565 	u64 ino;
3566 	int err;
3567 
3568 	if (!args->ignore_crossmnt &&
3569 	    args->dentry == args->exp->ex_path.mnt->mnt_root) {
3570 		err = nfsd4_get_mounted_on_ino(args->exp, &ino);
3571 		if (err)
3572 			return nfserrno(err);
3573 	} else
3574 		ino = args->stat.ino;
3575 
3576 	return nfsd4_encode_uint64_t(xdr, ino);
3577 }
3578 
3579 #ifdef CONFIG_NFSD_PNFS
3580 
3581 static __be32 nfsd4_encode_fattr4_fs_layout_types(struct xdr_stream *xdr,
3582 						  const struct nfsd4_fattr_args *args)
3583 {
3584 	unsigned long mask = args->exp->ex_layout_types;
3585 	int i;
3586 
3587 	/* Hamming weight of @mask is the number of layout types to return */
3588 	if (xdr_stream_encode_u32(xdr, hweight_long(mask)) != XDR_UNIT)
3589 		return nfserr_resource;
3590 	for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
3591 		if (mask & BIT(i)) {
3592 			/* layouttype4 */
3593 			if (xdr_stream_encode_u32(xdr, i) != XDR_UNIT)
3594 				return nfserr_resource;
3595 		}
3596 	return nfs_ok;
3597 }
3598 
3599 static __be32 nfsd4_encode_fattr4_layout_types(struct xdr_stream *xdr,
3600 					       const struct nfsd4_fattr_args *args)
3601 {
3602 	unsigned long mask = args->exp->ex_layout_types;
3603 	int i;
3604 
3605 	/* Hamming weight of @mask is the number of layout types to return */
3606 	if (xdr_stream_encode_u32(xdr, hweight_long(mask)) != XDR_UNIT)
3607 		return nfserr_resource;
3608 	for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
3609 		if (mask & BIT(i)) {
3610 			/* layouttype4 */
3611 			if (xdr_stream_encode_u32(xdr, i) != XDR_UNIT)
3612 				return nfserr_resource;
3613 		}
3614 	return nfs_ok;
3615 }
3616 
3617 static __be32 nfsd4_encode_fattr4_layout_blksize(struct xdr_stream *xdr,
3618 						 const struct nfsd4_fattr_args *args)
3619 {
3620 	return nfsd4_encode_uint32_t(xdr, args->stat.blksize);
3621 }
3622 
3623 #endif
3624 
3625 static __be32 nfsd4_encode_fattr4_suppattr_exclcreat(struct xdr_stream *xdr,
3626 						     const struct nfsd4_fattr_args *args)
3627 {
3628 	struct nfsd4_compoundres *resp = args->rqstp->rq_resp;
3629 	u32 supp[3];
3630 
3631 	memcpy(supp, nfsd_suppattrs[resp->cstate.minorversion], sizeof(supp));
3632 	if (!IS_POSIXACL(d_inode(args->dentry)))
3633 		supp[0] &= ~FATTR4_WORD0_ACL;
3634 	if (!args->contextsupport)
3635 		supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3636 
3637 	supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3638 	supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3639 	supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3640 
3641 	return nfsd4_encode_bitmap4(xdr, supp[0], supp[1], supp[2]);
3642 }
3643 
3644 /*
3645  * Copied from generic_remap_checks/generic_remap_file_range_prep.
3646  *
3647  * These generic functions use the file system's s_blocksize, but
3648  * individual file systems aren't required to use
3649  * generic_remap_file_range_prep. Until there is a mechanism for
3650  * determining a particular file system's (or file's) clone block
3651  * size, this is the best NFSD can do.
3652  */
3653 static __be32 nfsd4_encode_fattr4_clone_blksize(struct xdr_stream *xdr,
3654 						const struct nfsd4_fattr_args *args)
3655 {
3656 	struct inode *inode = d_inode(args->dentry);
3657 
3658 	return nfsd4_encode_uint32_t(xdr, inode->i_sb->s_blocksize);
3659 }
3660 
3661 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3662 static __be32 nfsd4_encode_fattr4_sec_label(struct xdr_stream *xdr,
3663 					    const struct nfsd4_fattr_args *args)
3664 {
3665 	return nfsd4_encode_security_label(xdr, args->rqstp, &args->context);
3666 }
3667 #endif
3668 
3669 static __be32 nfsd4_encode_fattr4_xattr_support(struct xdr_stream *xdr,
3670 						const struct nfsd4_fattr_args *args)
3671 {
3672 	int err = xattr_supports_user_prefix(d_inode(args->dentry));
3673 
3674 	return nfsd4_encode_bool(xdr, err == 0);
3675 }
3676 
3677 #define NFSD_OA_SHARE_ACCESS	(BIT(OPEN_ARGS_SHARE_ACCESS_READ)	| \
3678 				 BIT(OPEN_ARGS_SHARE_ACCESS_WRITE)	| \
3679 				 BIT(OPEN_ARGS_SHARE_ACCESS_BOTH))
3680 
3681 #define NFSD_OA_SHARE_DENY	(BIT(OPEN_ARGS_SHARE_DENY_NONE)		| \
3682 				 BIT(OPEN_ARGS_SHARE_DENY_READ)		| \
3683 				 BIT(OPEN_ARGS_SHARE_DENY_WRITE)	| \
3684 				 BIT(OPEN_ARGS_SHARE_DENY_BOTH))
3685 
3686 #define NFSD_OA_SHARE_ACCESS_WANT	(BIT(OPEN_ARGS_SHARE_ACCESS_WANT_ANY_DELEG)		| \
3687 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_NO_DELEG)		| \
3688 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_CANCEL)		| \
3689 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS)	| \
3690 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION))
3691 
3692 #define NFSD_OA_OPEN_CLAIM	(BIT(OPEN_ARGS_OPEN_CLAIM_NULL)		| \
3693 				 BIT(OPEN_ARGS_OPEN_CLAIM_PREVIOUS)	| \
3694 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEGATE_CUR)	| \
3695 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEGATE_PREV)| \
3696 				 BIT(OPEN_ARGS_OPEN_CLAIM_FH)		| \
3697 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEG_CUR_FH)	| \
3698 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEG_PREV_FH))
3699 
3700 #define NFSD_OA_CREATE_MODE	(BIT(OPEN_ARGS_CREATEMODE_UNCHECKED4)	| \
3701 				 BIT(OPEN_ARGS_CREATE_MODE_GUARDED)	| \
3702 				 BIT(OPEN_ARGS_CREATEMODE_EXCLUSIVE4)	| \
3703 				 BIT(OPEN_ARGS_CREATE_MODE_EXCLUSIVE4_1))
3704 
3705 static uint32_t oa_share_access = NFSD_OA_SHARE_ACCESS;
3706 static uint32_t oa_share_deny = NFSD_OA_SHARE_DENY;
3707 static uint32_t oa_share_access_want = NFSD_OA_SHARE_ACCESS_WANT;
3708 static uint32_t oa_open_claim = NFSD_OA_OPEN_CLAIM;
3709 static uint32_t oa_create_mode = NFSD_OA_CREATE_MODE;
3710 
3711 static const struct open_arguments4 nfsd_open_arguments = {
3712 	.oa_share_access = { .count = 1, .element = &oa_share_access },
3713 	.oa_share_deny = { .count = 1, .element = &oa_share_deny },
3714 	.oa_share_access_want = { .count = 1, .element = &oa_share_access_want },
3715 	.oa_open_claim = { .count = 1, .element = &oa_open_claim },
3716 	.oa_create_mode = { .count = 1, .element = &oa_create_mode },
3717 };
3718 
3719 static __be32 nfsd4_encode_fattr4_open_arguments(struct xdr_stream *xdr,
3720 						 const struct nfsd4_fattr_args *args)
3721 {
3722 	if (!xdrgen_encode_fattr4_open_arguments(xdr, &nfsd_open_arguments))
3723 		return nfserr_resource;
3724 	return nfs_ok;
3725 }
3726 
3727 #ifdef CONFIG_NFSD_V4_POSIX_ACLS
3728 
3729 static __be32 nfsd4_encode_fattr4_acl_trueform(struct xdr_stream *xdr,
3730 					       const struct nfsd4_fattr_args *args)
3731 {
3732 	aclmodel4 trueform = ACL_MODEL_NONE;
3733 
3734 	if (IS_POSIXACL(d_inode(args->dentry)))
3735 		trueform = ACL_MODEL_POSIX_DRAFT;
3736 	if (!xdrgen_encode_aclmodel4(xdr, trueform))
3737 		return nfserr_resource;
3738 	return nfs_ok;
3739 }
3740 
3741 static __be32 nfsd4_encode_fattr4_acl_trueform_scope(struct xdr_stream *xdr,
3742 						     const struct nfsd4_fattr_args *args)
3743 {
3744 	if (!xdrgen_encode_aclscope4(xdr, ACL_SCOPE_FILE_SYSTEM))
3745 		return nfserr_resource;
3746 	return nfs_ok;
3747 }
3748 
3749 static __be32 nfsd4_encode_fattr4_posix_default_acl(struct xdr_stream *xdr,
3750 						    const struct nfsd4_fattr_args *args)
3751 {
3752 	return nfsd4_encode_posixacl(xdr, args->rqstp, args->dpacl);
3753 }
3754 
3755 static __be32 nfsd4_encode_fattr4_posix_access_acl(struct xdr_stream *xdr,
3756 						   const struct nfsd4_fattr_args *args)
3757 {
3758 	return nfsd4_encode_posixacl(xdr, args->rqstp, args->pacl);
3759 }
3760 
3761 #endif /* CONFIG_NFSD_V4_POSIX_ACLS */
3762 
3763 static const nfsd4_enc_attr nfsd4_enc_fattr4_encode_ops[] = {
3764 	[FATTR4_SUPPORTED_ATTRS]	= nfsd4_encode_fattr4_supported_attrs,
3765 	[FATTR4_TYPE]			= nfsd4_encode_fattr4_type,
3766 	[FATTR4_FH_EXPIRE_TYPE]		= nfsd4_encode_fattr4_fh_expire_type,
3767 	[FATTR4_CHANGE]			= nfsd4_encode_fattr4_change,
3768 	[FATTR4_SIZE]			= nfsd4_encode_fattr4_size,
3769 	[FATTR4_LINK_SUPPORT]		= nfsd4_encode_fattr4__true,
3770 	[FATTR4_SYMLINK_SUPPORT]	= nfsd4_encode_fattr4__true,
3771 	[FATTR4_NAMED_ATTR]		= nfsd4_encode_fattr4__false,
3772 	[FATTR4_FSID]			= nfsd4_encode_fattr4_fsid,
3773 	[FATTR4_UNIQUE_HANDLES]		= nfsd4_encode_fattr4__true,
3774 	[FATTR4_LEASE_TIME]		= nfsd4_encode_fattr4_lease_time,
3775 	[FATTR4_RDATTR_ERROR]		= nfsd4_encode_fattr4_rdattr_error,
3776 	[FATTR4_ACL]			= nfsd4_encode_fattr4_acl,
3777 	[FATTR4_ACLSUPPORT]		= nfsd4_encode_fattr4_aclsupport,
3778 	[FATTR4_ARCHIVE]		= nfsd4_encode_fattr4__noop,
3779 	[FATTR4_CANSETTIME]		= nfsd4_encode_fattr4__true,
3780 	[FATTR4_CASE_INSENSITIVE]	= nfsd4_encode_fattr4_case_insensitive,
3781 	[FATTR4_CASE_PRESERVING]	= nfsd4_encode_fattr4_case_preserving,
3782 	[FATTR4_CHOWN_RESTRICTED]	= nfsd4_encode_fattr4__true,
3783 	[FATTR4_FILEHANDLE]		= nfsd4_encode_fattr4_filehandle,
3784 	[FATTR4_FILEID]			= nfsd4_encode_fattr4_fileid,
3785 	[FATTR4_FILES_AVAIL]		= nfsd4_encode_fattr4_files_avail,
3786 	[FATTR4_FILES_FREE]		= nfsd4_encode_fattr4_files_free,
3787 	[FATTR4_FILES_TOTAL]		= nfsd4_encode_fattr4_files_total,
3788 	[FATTR4_FS_LOCATIONS]		= nfsd4_encode_fattr4_fs_locations,
3789 	[FATTR4_HIDDEN]			= nfsd4_encode_fattr4__noop,
3790 	[FATTR4_HOMOGENEOUS]		= nfsd4_encode_fattr4_homogeneous,
3791 	[FATTR4_MAXFILESIZE]		= nfsd4_encode_fattr4_maxfilesize,
3792 	[FATTR4_MAXLINK]		= nfsd4_encode_fattr4_maxlink,
3793 	[FATTR4_MAXNAME]		= nfsd4_encode_fattr4_maxname,
3794 	[FATTR4_MAXREAD]		= nfsd4_encode_fattr4_maxread,
3795 	[FATTR4_MAXWRITE]		= nfsd4_encode_fattr4_maxwrite,
3796 	[FATTR4_MIMETYPE]		= nfsd4_encode_fattr4__noop,
3797 	[FATTR4_MODE]			= nfsd4_encode_fattr4_mode,
3798 	[FATTR4_NO_TRUNC]		= nfsd4_encode_fattr4__true,
3799 	[FATTR4_NUMLINKS]		= nfsd4_encode_fattr4_numlinks,
3800 	[FATTR4_OWNER]			= nfsd4_encode_fattr4_owner,
3801 	[FATTR4_OWNER_GROUP]		= nfsd4_encode_fattr4_owner_group,
3802 	[FATTR4_QUOTA_AVAIL_HARD]	= nfsd4_encode_fattr4__noop,
3803 	[FATTR4_QUOTA_AVAIL_SOFT]	= nfsd4_encode_fattr4__noop,
3804 	[FATTR4_QUOTA_USED]		= nfsd4_encode_fattr4__noop,
3805 	[FATTR4_RAWDEV]			= nfsd4_encode_fattr4_rawdev,
3806 	[FATTR4_SPACE_AVAIL]		= nfsd4_encode_fattr4_space_avail,
3807 	[FATTR4_SPACE_FREE]		= nfsd4_encode_fattr4_space_free,
3808 	[FATTR4_SPACE_TOTAL]		= nfsd4_encode_fattr4_space_total,
3809 	[FATTR4_SPACE_USED]		= nfsd4_encode_fattr4_space_used,
3810 	[FATTR4_SYSTEM]			= nfsd4_encode_fattr4__noop,
3811 	[FATTR4_TIME_ACCESS]		= nfsd4_encode_fattr4_time_access,
3812 	[FATTR4_TIME_ACCESS_SET]	= nfsd4_encode_fattr4__noop,
3813 	[FATTR4_TIME_BACKUP]		= nfsd4_encode_fattr4__noop,
3814 	[FATTR4_TIME_CREATE]		= nfsd4_encode_fattr4_time_create,
3815 	[FATTR4_TIME_DELTA]		= nfsd4_encode_fattr4_time_delta,
3816 	[FATTR4_TIME_METADATA]		= nfsd4_encode_fattr4_time_metadata,
3817 	[FATTR4_TIME_MODIFY]		= nfsd4_encode_fattr4_time_modify,
3818 	[FATTR4_TIME_MODIFY_SET]	= nfsd4_encode_fattr4__noop,
3819 	[FATTR4_MOUNTED_ON_FILEID]	= nfsd4_encode_fattr4_mounted_on_fileid,
3820 	[FATTR4_DIR_NOTIF_DELAY]	= nfsd4_encode_fattr4__noop,
3821 	[FATTR4_DIRENT_NOTIF_DELAY]	= nfsd4_encode_fattr4__noop,
3822 	[FATTR4_DACL]			= nfsd4_encode_fattr4__noop,
3823 	[FATTR4_SACL]			= nfsd4_encode_fattr4__noop,
3824 	[FATTR4_CHANGE_POLICY]		= nfsd4_encode_fattr4__noop,
3825 	[FATTR4_FS_STATUS]		= nfsd4_encode_fattr4__noop,
3826 
3827 #ifdef CONFIG_NFSD_PNFS
3828 	[FATTR4_FS_LAYOUT_TYPES]	= nfsd4_encode_fattr4_fs_layout_types,
3829 	[FATTR4_LAYOUT_HINT]		= nfsd4_encode_fattr4__noop,
3830 	[FATTR4_LAYOUT_TYPES]		= nfsd4_encode_fattr4_layout_types,
3831 	[FATTR4_LAYOUT_BLKSIZE]		= nfsd4_encode_fattr4_layout_blksize,
3832 	[FATTR4_LAYOUT_ALIGNMENT]	= nfsd4_encode_fattr4__noop,
3833 #else
3834 	[FATTR4_FS_LAYOUT_TYPES]	= nfsd4_encode_fattr4__noop,
3835 	[FATTR4_LAYOUT_HINT]		= nfsd4_encode_fattr4__noop,
3836 	[FATTR4_LAYOUT_TYPES]		= nfsd4_encode_fattr4__noop,
3837 	[FATTR4_LAYOUT_BLKSIZE]		= nfsd4_encode_fattr4__noop,
3838 	[FATTR4_LAYOUT_ALIGNMENT]	= nfsd4_encode_fattr4__noop,
3839 #endif
3840 
3841 	[FATTR4_FS_LOCATIONS_INFO]	= nfsd4_encode_fattr4__noop,
3842 	[FATTR4_MDSTHRESHOLD]		= nfsd4_encode_fattr4__noop,
3843 	[FATTR4_RETENTION_GET]		= nfsd4_encode_fattr4__noop,
3844 	[FATTR4_RETENTION_SET]		= nfsd4_encode_fattr4__noop,
3845 	[FATTR4_RETENTEVT_GET]		= nfsd4_encode_fattr4__noop,
3846 	[FATTR4_RETENTEVT_SET]		= nfsd4_encode_fattr4__noop,
3847 	[FATTR4_RETENTION_HOLD]		= nfsd4_encode_fattr4__noop,
3848 	[FATTR4_MODE_SET_MASKED]	= nfsd4_encode_fattr4__noop,
3849 	[FATTR4_SUPPATTR_EXCLCREAT]	= nfsd4_encode_fattr4_suppattr_exclcreat,
3850 	[FATTR4_FS_CHARSET_CAP]		= nfsd4_encode_fattr4__noop,
3851 	[FATTR4_CLONE_BLKSIZE]		= nfsd4_encode_fattr4_clone_blksize,
3852 	[FATTR4_SPACE_FREED]		= nfsd4_encode_fattr4__noop,
3853 	[FATTR4_CHANGE_ATTR_TYPE]	= nfsd4_encode_fattr4__noop,
3854 
3855 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3856 	[FATTR4_SEC_LABEL]		= nfsd4_encode_fattr4_sec_label,
3857 #else
3858 	[FATTR4_SEC_LABEL]		= nfsd4_encode_fattr4__noop,
3859 #endif
3860 
3861 	[FATTR4_MODE_UMASK]		= nfsd4_encode_fattr4__noop,
3862 	[FATTR4_XATTR_SUPPORT]		= nfsd4_encode_fattr4_xattr_support,
3863 	[FATTR4_TIME_DELEG_ACCESS]	= nfsd4_encode_fattr4__inval,
3864 	[FATTR4_TIME_DELEG_MODIFY]	= nfsd4_encode_fattr4__inval,
3865 	[FATTR4_OPEN_ARGUMENTS]		= nfsd4_encode_fattr4_open_arguments,
3866 
3867 	/* Reserved */
3868 	[87]				= nfsd4_encode_fattr4__inval,
3869 	[88]				= nfsd4_encode_fattr4__inval,
3870 
3871 #ifdef CONFIG_NFSD_V4_POSIX_ACLS
3872 	[FATTR4_ACL_TRUEFORM]		= nfsd4_encode_fattr4_acl_trueform,
3873 	[FATTR4_ACL_TRUEFORM_SCOPE]	= nfsd4_encode_fattr4_acl_trueform_scope,
3874 	[FATTR4_POSIX_DEFAULT_ACL]	= nfsd4_encode_fattr4_posix_default_acl,
3875 	[FATTR4_POSIX_ACCESS_ACL]	= nfsd4_encode_fattr4_posix_access_acl,
3876 #else
3877 	[FATTR4_ACL_TRUEFORM]		= nfsd4_encode_fattr4__noop,
3878 	[FATTR4_ACL_TRUEFORM_SCOPE]	= nfsd4_encode_fattr4__noop,
3879 	[FATTR4_POSIX_DEFAULT_ACL]	= nfsd4_encode_fattr4__noop,
3880 	[FATTR4_POSIX_ACCESS_ACL]	= nfsd4_encode_fattr4__noop,
3881 #endif
3882 };
3883 
3884 /*
3885  * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
3886  * ourselves. @case_cache is NULL for callers that encode a single dentry
3887  * (GETATTR, the buffer wrapper); READDIR passes a per-request cache so
3888  * non-directory children share the parent's case-folding probe result.
3889  */
3890 static __be32
3891 nfsd4_encode_fattr4(struct svc_rqst *rqstp, struct xdr_stream *xdr,
3892 		    struct svc_fh *fhp, struct svc_export *exp,
3893 		    struct dentry *dentry, const u32 *bmval,
3894 		    int ignore_crossmnt,
3895 		    struct nfsd_case_attrs_cache *case_cache)
3896 {
3897 	DECLARE_BITMAP(attr_bitmap, ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops));
3898 	struct nfs4_delegation *dp = NULL;
3899 	struct nfsd4_fattr_args args;
3900 	struct svc_fh *tempfh = NULL;
3901 	int starting_len = xdr->buf->len;
3902 	unsigned int attrlen_offset;
3903 	__be32 attrlen, status;
3904 	u32 attrmask[3];
3905 	int err;
3906 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3907 	u32 minorversion = resp->cstate.minorversion;
3908 	struct path path = {
3909 		.mnt	= exp->ex_path.mnt,
3910 		.dentry	= dentry,
3911 	};
3912 	unsigned long bit;
3913 
3914 	WARN_ON_ONCE(bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1);
3915 	WARN_ON_ONCE(!nfsd_attrs_supported(minorversion, bmval));
3916 
3917 	args.rqstp = rqstp;
3918 	args.exp = exp;
3919 	args.dentry = dentry;
3920 	args.ignore_crossmnt = (ignore_crossmnt != 0);
3921 	args.acl = NULL;
3922 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3923 	args.context.context = NULL;
3924 #endif
3925 #ifdef CONFIG_NFSD_V4_POSIX_ACLS
3926 	args.dpacl = NULL;
3927 	args.pacl = NULL;
3928 #endif
3929 
3930 	/*
3931 	 * Make a local copy of the attribute bitmap that can be modified.
3932 	 */
3933 	attrmask[0] = bmval[0];
3934 	attrmask[1] = bmval[1];
3935 	attrmask[2] = bmval[2];
3936 
3937 	args.rdattr_err = 0;
3938 	if (exp->ex_fslocs.migrated) {
3939 		status = fattr_handle_absent_fs(&attrmask[0], &attrmask[1],
3940 						&attrmask[2], &args.rdattr_err);
3941 		if (status)
3942 			goto out;
3943 	}
3944 	if ((attrmask[0] & (FATTR4_WORD0_CHANGE |
3945 			    FATTR4_WORD0_SIZE)) ||
3946 	    (attrmask[1] & (FATTR4_WORD1_TIME_ACCESS |
3947 			    FATTR4_WORD1_TIME_MODIFY |
3948 			    FATTR4_WORD1_TIME_METADATA))) {
3949 		status = nfsd4_deleg_getattr_conflict(rqstp, dentry, &dp);
3950 		if (status)
3951 			goto out;
3952 	}
3953 
3954 	err = vfs_getattr(&path, &args.stat,
3955 			  STATX_BASIC_STATS | STATX_BTIME | STATX_CHANGE_COOKIE,
3956 			  AT_STATX_SYNC_AS_STAT);
3957 	if (dp) {
3958 		struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
3959 
3960 		if (ncf->ncf_file_modified) {
3961 			++ncf->ncf_initial_cinfo;
3962 			args.stat.size = ncf->ncf_cur_fsize;
3963 			if (!timespec64_is_epoch(&ncf->ncf_cb_mtime))
3964 				args.stat.mtime = ncf->ncf_cb_mtime;
3965 		}
3966 		args.change_attr = ncf->ncf_initial_cinfo;
3967 
3968 		if (!timespec64_is_epoch(&ncf->ncf_cb_atime))
3969 			args.stat.atime = ncf->ncf_cb_atime;
3970 
3971 		nfs4_put_stid(&dp->dl_stid);
3972 	} else {
3973 		args.change_attr = nfsd4_change_attribute(&args.stat);
3974 	}
3975 
3976 	if (err)
3977 		goto out_nfserr;
3978 
3979 	if (!(args.stat.result_mask & STATX_BTIME))
3980 		/* underlying FS does not offer btime so we can't share it */
3981 		attrmask[1] &= ~FATTR4_WORD1_TIME_CREATE;
3982 	if ((attrmask[0] & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
3983 			FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
3984 	    (attrmask[1] & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
3985 		       FATTR4_WORD1_SPACE_TOTAL))) {
3986 		err = vfs_statfs(&path, &args.statfs);
3987 		if (err)
3988 			goto out_nfserr;
3989 	}
3990 	if ((attrmask[0] & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) &&
3991 	    !fhp) {
3992 		tempfh = kmalloc_obj(struct svc_fh);
3993 		status = nfserr_jukebox;
3994 		if (!tempfh)
3995 			goto out;
3996 		fh_init(tempfh, NFS4_FHSIZE);
3997 		status = fh_compose(tempfh, exp, dentry, NULL);
3998 		if (status)
3999 			goto out;
4000 		args.fhp = tempfh;
4001 	} else
4002 		args.fhp = fhp;
4003 	if (attrmask[0] & (FATTR4_WORD0_CASE_INSENSITIVE |
4004 			   FATTR4_WORD0_CASE_PRESERVING)) {
4005 		/*
4006 		 * In a batched encoder (READDIR) every non-directory
4007 		 * child shares the same case-folding answer, so the
4008 		 * directory being read is probed once and the result is
4009 		 * cached. The probe targets case_cache->dir, the held
4010 		 * readdir filehandle's dentry, instead of the child's
4011 		 * locklessly-acquired dentry, which a concurrent rename
4012 		 * could move under an unrelated parent. Directory
4013 		 * entries are queried directly because casefold-capable
4014 		 * filesystems answer per directory.
4015 		 *
4016 		 * Per RFC 8881 Section 18.7.3, an attribute advertised
4017 		 * in SUPPORTED_ATTRS must come back with a value or the
4018 		 * GETATTR must fail. nfsd_get_case_info() fills POSIX
4019 		 * defaults and returns -EOPNOTSUPP when the underlying
4020 		 * filesystem does not expose case state; encode those
4021 		 * defaults so the reply agrees with what SUPPORTED_ATTRS
4022 		 * advertises. Other errors fail the operation as the
4023 		 * spec requires.
4024 		 */
4025 		if (case_cache && !d_is_dir(dentry)) {
4026 			if (!case_cache->valid) {
4027 				err = nfsd_get_case_info(case_cache->dir,
4028 							 &case_cache->insensitive,
4029 							 &case_cache->preserving);
4030 				if (err && err != -EOPNOTSUPP)
4031 					goto out_nfserr;
4032 				case_cache->valid = true;
4033 			}
4034 			args.case_insensitive = case_cache->insensitive;
4035 			args.case_preserving = case_cache->preserving;
4036 		} else {
4037 			err = nfsd_get_case_info(dentry,
4038 						 &args.case_insensitive,
4039 						 &args.case_preserving);
4040 			if (err && err != -EOPNOTSUPP)
4041 				goto out_nfserr;
4042 		}
4043 	}
4044 
4045 	if (attrmask[0] & FATTR4_WORD0_ACL) {
4046 		err = nfsd4_get_nfs4_acl(rqstp, dentry, &args.acl);
4047 		if (err == -EOPNOTSUPP)
4048 			attrmask[0] &= ~FATTR4_WORD0_ACL;
4049 		else if (err == -EINVAL) {
4050 			status = nfserr_attrnotsupp;
4051 			goto out;
4052 		} else if (err != 0)
4053 			goto out_nfserr;
4054 	}
4055 
4056 	args.contextsupport = false;
4057 
4058 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
4059 	if ((attrmask[2] & FATTR4_WORD2_SECURITY_LABEL) ||
4060 	     attrmask[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
4061 		if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
4062 			err = security_inode_getsecctx(d_inode(dentry),
4063 						&args.context);
4064 		else
4065 			err = -EOPNOTSUPP;
4066 		args.contextsupport = (err == 0);
4067 		if (attrmask[2] & FATTR4_WORD2_SECURITY_LABEL) {
4068 			if (err == -EOPNOTSUPP)
4069 				attrmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
4070 			else if (err)
4071 				goto out_nfserr;
4072 		}
4073 	}
4074 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
4075 
4076 #ifdef CONFIG_NFSD_V4_POSIX_ACLS
4077 	if (attrmask[2] & FATTR4_WORD2_POSIX_DEFAULT_ACL) {
4078 		struct inode *inode = d_inode(dentry);
4079 		struct posix_acl *dpacl;
4080 
4081 		if (S_ISDIR(inode->i_mode)) {
4082 			dpacl = get_inode_acl(inode, ACL_TYPE_DEFAULT);
4083 			if (IS_ERR(dpacl)) {
4084 				switch (PTR_ERR(dpacl)) {
4085 				case -EOPNOTSUPP:
4086 					attrmask[2] &= ~FATTR4_WORD2_POSIX_DEFAULT_ACL;
4087 					break;
4088 				case -EINVAL:
4089 					status = nfserr_attrnotsupp;
4090 					goto out;
4091 				default:
4092 					err = PTR_ERR(dpacl);
4093 					goto out_nfserr;
4094 				}
4095 			} else {
4096 				args.dpacl = dpacl;
4097 			}
4098 		}
4099 	}
4100 	if (attrmask[2] & FATTR4_WORD2_POSIX_ACCESS_ACL) {
4101 		struct inode *inode = d_inode(dentry);
4102 		struct posix_acl *pacl;
4103 
4104 		pacl = get_inode_acl(inode, ACL_TYPE_ACCESS);
4105 		if (!pacl)
4106 			pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
4107 		if (IS_ERR(pacl)) {
4108 			switch (PTR_ERR(pacl)) {
4109 			case -EOPNOTSUPP:
4110 				attrmask[2] &= ~FATTR4_WORD2_POSIX_ACCESS_ACL;
4111 				break;
4112 			case -EINVAL:
4113 				status = nfserr_attrnotsupp;
4114 				goto out;
4115 			default:
4116 				err = PTR_ERR(pacl);
4117 				goto out_nfserr;
4118 			}
4119 		} else {
4120 			args.pacl = pacl;
4121 		}
4122 	}
4123 #endif /* CONFIG_NFSD_V4_POSIX_ACLS */
4124 
4125 	/* attrmask */
4126 	status = nfsd4_encode_bitmap4(xdr, attrmask[0], attrmask[1],
4127 				      attrmask[2]);
4128 	if (status)
4129 		goto out;
4130 
4131 	/* attr_vals */
4132 	attrlen_offset = xdr->buf->len;
4133 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
4134 		goto out_resource;
4135 	bitmap_from_arr32(attr_bitmap, attrmask,
4136 			  ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops));
4137 	for_each_set_bit(bit, attr_bitmap,
4138 			 ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops)) {
4139 		status = nfsd4_enc_fattr4_encode_ops[bit](xdr, &args);
4140 		if (status != nfs_ok)
4141 			goto out;
4142 	}
4143 	attrlen = cpu_to_be32(xdr->buf->len - attrlen_offset - XDR_UNIT);
4144 	write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, XDR_UNIT);
4145 	status = nfs_ok;
4146 
4147 out:
4148 #ifdef CONFIG_NFSD_V4_POSIX_ACLS
4149 	if (args.dpacl)
4150 		posix_acl_release(args.dpacl);
4151 	if (args.pacl)
4152 		posix_acl_release(args.pacl);
4153 #endif /* CONFIG_NFSD_V4_POSIX_ACLS */
4154 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
4155 	if (args.context.context)
4156 		security_release_secctx(&args.context);
4157 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
4158 	kfree(args.acl);
4159 	if (tempfh) {
4160 		fh_put(tempfh);
4161 		kfree(tempfh);
4162 	}
4163 	if (status)
4164 		xdr_truncate_encode(xdr, starting_len);
4165 	return status;
4166 out_nfserr:
4167 	status = nfserrno(err);
4168 	goto out;
4169 out_resource:
4170 	status = nfserr_resource;
4171 	goto out;
4172 }
4173 
4174 static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
4175 				struct xdr_buf *buf, __be32 *p, int bytes)
4176 {
4177 	xdr->scratch.iov_len = 0;
4178 	memset(buf, 0, sizeof(struct xdr_buf));
4179 	buf->head[0].iov_base = p;
4180 	buf->head[0].iov_len = 0;
4181 	buf->len = 0;
4182 	xdr->buf = buf;
4183 	xdr->iov = buf->head;
4184 	xdr->p = p;
4185 	xdr->end = (void *)p + bytes;
4186 	buf->buflen = bytes;
4187 }
4188 
4189 __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
4190 			struct svc_fh *fhp, struct svc_export *exp,
4191 			struct dentry *dentry, u32 *bmval,
4192 			struct svc_rqst *rqstp, int ignore_crossmnt)
4193 {
4194 	struct xdr_buf dummy;
4195 	struct xdr_stream xdr;
4196 	__be32 ret;
4197 
4198 	svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
4199 	ret = nfsd4_encode_fattr4(rqstp, &xdr, fhp, exp, dentry, bmval,
4200 				  ignore_crossmnt, NULL);
4201 	*p = xdr.p;
4202 	return ret;
4203 }
4204 
4205 /*
4206  * The buffer space for this field was reserved during a previous
4207  * call to nfsd4_encode_entry4().
4208  */
4209 static void nfsd4_encode_entry4_nfs_cookie4(const struct nfsd4_readdir *readdir,
4210 					    u64 offset)
4211 {
4212 	__be64 cookie = cpu_to_be64(offset);
4213 	struct xdr_stream *xdr = readdir->xdr;
4214 
4215 	if (!readdir->cookie_offset)
4216 		return;
4217 	write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset, &cookie,
4218 			       sizeof(cookie));
4219 }
4220 
4221 static inline int attributes_need_mount(u32 *bmval)
4222 {
4223 	if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
4224 		return 1;
4225 	if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
4226 		return 1;
4227 	return 0;
4228 }
4229 
4230 static __be32
4231 nfsd4_encode_entry4_fattr(struct nfsd4_readdir *cd, const char *name,
4232 			  int namlen)
4233 {
4234 	struct svc_export *exp = cd->rd_fhp->fh_export;
4235 	struct dentry *dentry;
4236 	__be32 nfserr;
4237 	int ignore_crossmnt = 0;
4238 	bool crossed = false;
4239 
4240 	dentry = lookup_one_positive_unlocked(&nop_mnt_idmap,
4241 					      &QSTR_LEN(name, namlen),
4242 					      cd->rd_fhp->fh_dentry);
4243 	if (IS_ERR(dentry))
4244 		return nfserrno(PTR_ERR(dentry));
4245 
4246 	exp_get(exp);
4247 	/*
4248 	 * In the case of a mountpoint, the client may be asking for
4249 	 * attributes that are only properties of the underlying filesystem
4250 	 * as opposed to the cross-mounted file system. In such a case,
4251 	 * we will not follow the cross mount and will fill the attribtutes
4252 	 * directly from the mountpoint dentry.
4253 	 */
4254 	if (nfsd_mountpoint(dentry, exp)) {
4255 		int err;
4256 
4257 		if (!(exp->ex_flags & NFSEXP_V4ROOT)
4258 				&& !attributes_need_mount(cd->rd_bmval)) {
4259 			ignore_crossmnt = 1;
4260 			goto out_encode;
4261 		}
4262 		/*
4263 		 * Why the heck aren't we just using nfsd_lookup??
4264 		 * Different "."/".." handling?  Something else?
4265 		 * At least, add a comment here to explain....
4266 		 */
4267 		err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
4268 		if (err) {
4269 			nfserr = nfserrno(err);
4270 			goto out_put;
4271 		}
4272 		nfserr = check_nfsd_access(exp, cd->rd_rqstp, false);
4273 		if (nfserr)
4274 			goto out_put;
4275 		crossed = true;
4276 
4277 	}
4278 out_encode:
4279 	/*
4280 	 * A crossed entry no longer shares a parent with the directory
4281 	 * being read, so it must neither consume nor populate the
4282 	 * per-readdir case-folding cache.
4283 	 */
4284 	nfserr = nfsd4_encode_fattr4(cd->rd_rqstp, cd->xdr, NULL, exp, dentry,
4285 				     cd->rd_bmval, ignore_crossmnt,
4286 				     crossed ? NULL : &cd->rd_case_cache);
4287 out_put:
4288 	dput(dentry);
4289 	exp_put(exp);
4290 	return nfserr;
4291 }
4292 
4293 static __be32
4294 nfsd4_encode_entry4_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
4295 {
4296 	__be32 status;
4297 
4298 	/* attrmask */
4299 	status = nfsd4_encode_bitmap4(xdr, FATTR4_WORD0_RDATTR_ERROR, 0, 0);
4300 	if (status != nfs_ok)
4301 		return status;
4302 	/* attr_vals */
4303 	if (xdr_stream_encode_u32(xdr, XDR_UNIT) != XDR_UNIT)
4304 		return nfserr_resource;
4305 	/* rdattr_error */
4306 	if (xdr_stream_encode_be32(xdr, nfserr) != XDR_UNIT)
4307 		return nfserr_resource;
4308 	return nfs_ok;
4309 }
4310 
4311 static int
4312 nfsd4_encode_entry4(void *ccdv, const char *name, int namlen,
4313 		    loff_t offset, u64 ino, unsigned int d_type)
4314 {
4315 	struct readdir_cd *ccd = ccdv;
4316 	struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
4317 	struct xdr_stream *xdr = cd->xdr;
4318 	int start_offset = xdr->buf->len;
4319 	int cookie_offset;
4320 	u32 name_and_cookie;
4321 	int entry_bytes;
4322 	__be32 nfserr = nfserr_toosmall;
4323 
4324 	/* In nfsv4, "." and ".." never make it onto the wire.. */
4325 	if (name && isdotent(name, namlen)) {
4326 		cd->common.err = nfs_ok;
4327 		return 0;
4328 	}
4329 
4330 	/* Encode the previous entry's cookie value */
4331 	nfsd4_encode_entry4_nfs_cookie4(cd, offset);
4332 
4333 	if (xdr_stream_encode_item_present(xdr) != XDR_UNIT)
4334 		goto fail;
4335 
4336 	/* Reserve send buffer space for this entry's cookie value. */
4337 	cookie_offset = xdr->buf->len;
4338 	if (nfsd4_encode_nfs_cookie4(xdr, OFFSET_MAX) != nfs_ok)
4339 		goto fail;
4340 	if (nfsd4_encode_component4(xdr, name, namlen) != nfs_ok)
4341 		goto fail;
4342 	nfserr = nfsd4_encode_entry4_fattr(cd, name, namlen);
4343 	switch (nfserr) {
4344 	case nfs_ok:
4345 		break;
4346 	case nfserr_resource:
4347 		nfserr = nfserr_toosmall;
4348 		goto fail;
4349 	case nfserr_noent:
4350 		xdr_truncate_encode(xdr, start_offset);
4351 		goto skip_entry;
4352 	case nfserr_jukebox:
4353 		/*
4354 		 * The pseudoroot should only display dentries that lead to
4355 		 * exports. If we get EJUKEBOX here, then we can't tell whether
4356 		 * this entry should be included. Just fail the whole READDIR
4357 		 * with NFS4ERR_DELAY in that case, and hope that the situation
4358 		 * will resolve itself by the client's next attempt.
4359 		 */
4360 		if (cd->rd_fhp->fh_export->ex_flags & NFSEXP_V4ROOT)
4361 			goto fail;
4362 		fallthrough;
4363 	default:
4364 		/*
4365 		 * If the client requested the RDATTR_ERROR attribute,
4366 		 * we stuff the error code into this attribute
4367 		 * and continue.  If this attribute was not requested,
4368 		 * then in accordance with the spec, we fail the
4369 		 * entire READDIR operation(!)
4370 		 */
4371 		if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
4372 			goto fail;
4373 		if (nfsd4_encode_entry4_rdattr_error(xdr, nfserr)) {
4374 			nfserr = nfserr_toosmall;
4375 			goto fail;
4376 		}
4377 	}
4378 	nfserr = nfserr_toosmall;
4379 	entry_bytes = xdr->buf->len - start_offset;
4380 	if (entry_bytes > cd->rd_maxcount)
4381 		goto fail;
4382 	cd->rd_maxcount -= entry_bytes;
4383 	/*
4384 	 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", and
4385 	 * notes that it could be zero. If it is zero, then the server
4386 	 * should enforce only the rd_maxcount value.
4387 	 */
4388 	if (cd->rd_dircount) {
4389 		name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
4390 		if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
4391 			goto fail;
4392 		cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
4393 		if (!cd->rd_dircount)
4394 			cd->rd_maxcount = 0;
4395 	}
4396 
4397 	cd->cookie_offset = cookie_offset;
4398 skip_entry:
4399 	cd->common.err = nfs_ok;
4400 	return 0;
4401 fail:
4402 	xdr_truncate_encode(xdr, start_offset);
4403 	cd->common.err = nfserr;
4404 	return -EINVAL;
4405 }
4406 
4407 static __be32
4408 nfsd4_encode_verifier4(struct xdr_stream *xdr, const nfs4_verifier *verf)
4409 {
4410 	__be32 *p;
4411 
4412 	p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
4413 	if (!p)
4414 		return nfserr_resource;
4415 	memcpy(p, verf->data, sizeof(verf->data));
4416 	return nfs_ok;
4417 }
4418 
4419 static __be32
4420 nfsd4_encode_clientid4(struct xdr_stream *xdr, const clientid_t *clientid)
4421 {
4422 	__be32 *p;
4423 
4424 	p = xdr_reserve_space(xdr, sizeof(__be64));
4425 	if (!p)
4426 		return nfserr_resource;
4427 	memcpy(p, clientid, sizeof(*clientid));
4428 	return nfs_ok;
4429 }
4430 
4431 /* This is a frequently-encoded item; open-coded for speed */
4432 static __be32
4433 nfsd4_encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
4434 {
4435 	__be32 *p;
4436 
4437 	p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
4438 	if (!p)
4439 		return nfserr_resource;
4440 	*p++ = cpu_to_be32(sid->si_generation);
4441 	memcpy(p, &sid->si_opaque, sizeof(sid->si_opaque));
4442 	return nfs_ok;
4443 }
4444 
4445 static __be32
4446 nfsd4_encode_sessionid4(struct xdr_stream *xdr,
4447 			const struct nfs4_sessionid *sessionid)
4448 {
4449 	return nfsd4_encode_opaque_fixed(xdr, sessionid->data,
4450 					 NFS4_MAX_SESSIONID_LEN);
4451 }
4452 
4453 static __be32
4454 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr,
4455 		    union nfsd4_op_u *u)
4456 {
4457 	struct nfsd4_access *access = &u->access;
4458 	struct xdr_stream *xdr = resp->xdr;
4459 	__be32 status;
4460 
4461 	/* supported */
4462 	status = nfsd4_encode_uint32_t(xdr, access->ac_supported);
4463 	if (status != nfs_ok)
4464 		return status;
4465 	/* access */
4466 	return nfsd4_encode_uint32_t(xdr, access->ac_resp_access);
4467 }
4468 
4469 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr,
4470 						union nfsd4_op_u *u)
4471 {
4472 	struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
4473 	struct xdr_stream *xdr = resp->xdr;
4474 
4475 	/* bctsr_sessid */
4476 	nfserr = nfsd4_encode_sessionid4(xdr, &bcts->sessionid);
4477 	if (nfserr != nfs_ok)
4478 		return nfserr;
4479 	/* bctsr_dir */
4480 	if (xdr_stream_encode_u32(xdr, bcts->dir) != XDR_UNIT)
4481 		return nfserr_resource;
4482 	/* bctsr_use_conn_in_rdma_mode */
4483 	return nfsd4_encode_bool(xdr, false);
4484 }
4485 
4486 static __be32
4487 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr,
4488 		   union nfsd4_op_u *u)
4489 {
4490 	struct nfsd4_close *close = &u->close;
4491 	struct xdr_stream *xdr = resp->xdr;
4492 
4493 	/* open_stateid */
4494 	return nfsd4_encode_stateid4(xdr, &close->cl_stateid);
4495 }
4496 
4497 
4498 static __be32
4499 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr,
4500 		    union nfsd4_op_u *u)
4501 {
4502 	struct nfsd4_commit *commit = &u->commit;
4503 
4504 	return nfsd4_encode_verifier4(resp->xdr, &commit->co_verf);
4505 }
4506 
4507 static __be32
4508 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr,
4509 		    union nfsd4_op_u *u)
4510 {
4511 	struct nfsd4_create *create = &u->create;
4512 	struct xdr_stream *xdr = resp->xdr;
4513 
4514 	/* cinfo */
4515 	nfserr = nfsd4_encode_change_info4(xdr, &create->cr_cinfo);
4516 	if (nfserr)
4517 		return nfserr;
4518 	/* attrset */
4519 	return nfsd4_encode_bitmap4(xdr, create->cr_bmval[0],
4520 				    create->cr_bmval[1], create->cr_bmval[2]);
4521 }
4522 
4523 static __be32
4524 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4525 		     union nfsd4_op_u *u)
4526 {
4527 	struct nfsd4_getattr *getattr = &u->getattr;
4528 	struct svc_fh *fhp = getattr->ga_fhp;
4529 	struct xdr_stream *xdr = resp->xdr;
4530 
4531 	/* obj_attributes */
4532 	return nfsd4_encode_fattr4(resp->rqstp, xdr, fhp, fhp->fh_export,
4533 				   fhp->fh_dentry, getattr->ga_bmval, 0, NULL);
4534 }
4535 
4536 static __be32
4537 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr,
4538 		   union nfsd4_op_u *u)
4539 {
4540 	struct xdr_stream *xdr = resp->xdr;
4541 	struct svc_fh *fhp = u->getfh;
4542 
4543 	/* object */
4544 	return nfsd4_encode_nfs_fh4(xdr, &fhp->fh_handle);
4545 }
4546 
4547 static __be32
4548 nfsd4_encode_lock_owner4(struct xdr_stream *xdr, const clientid_t *clientid,
4549 			 const struct xdr_netobj *owner)
4550 {
4551 	__be32 status;
4552 
4553 	/* clientid */
4554 	status = nfsd4_encode_clientid4(xdr, clientid);
4555 	if (status != nfs_ok)
4556 		return status;
4557 	/* owner */
4558 	return nfsd4_encode_opaque(xdr, owner->data, owner->len);
4559 }
4560 
4561 static __be32
4562 nfsd4_encode_lock4denied(struct xdr_stream *xdr,
4563 			 const struct nfsd4_lock_denied *ld)
4564 {
4565 	__be32 status;
4566 
4567 	/* offset */
4568 	status = nfsd4_encode_offset4(xdr, ld->ld_start);
4569 	if (status != nfs_ok)
4570 		return status;
4571 	/* length */
4572 	status = nfsd4_encode_length4(xdr, ld->ld_length);
4573 	if (status != nfs_ok)
4574 		return status;
4575 	/* locktype */
4576 	if (xdr_stream_encode_u32(xdr, ld->ld_type) != XDR_UNIT)
4577 		return nfserr_resource;
4578 	/* owner */
4579 	return nfsd4_encode_lock_owner4(xdr, &ld->ld_clientid,
4580 					&ld->ld_owner);
4581 }
4582 
4583 static __be32
4584 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr,
4585 		  union nfsd4_op_u *u)
4586 {
4587 	struct nfsd4_lock *lock = &u->lock;
4588 	struct xdr_stream *xdr = resp->xdr;
4589 	__be32 status;
4590 
4591 	switch (nfserr) {
4592 	case nfs_ok:
4593 		/* resok4 */
4594 		status = nfsd4_encode_stateid4(xdr, &lock->lk_resp_stateid);
4595 		break;
4596 	case nfserr_denied:
4597 		/* denied */
4598 		status = nfsd4_encode_lock4denied(xdr, &lock->lk_denied);
4599 		break;
4600 	default:
4601 		return nfserr;
4602 	}
4603 	return status != nfs_ok ? status : nfserr;
4604 }
4605 
4606 static __be32
4607 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr,
4608 		   union nfsd4_op_u *u)
4609 {
4610 	struct nfsd4_lockt *lockt = &u->lockt;
4611 	struct xdr_stream *xdr = resp->xdr;
4612 	__be32 status;
4613 
4614 	if (nfserr == nfserr_denied) {
4615 		/* denied */
4616 		status = nfsd4_encode_lock4denied(xdr, &lockt->lt_denied);
4617 		if (status != nfs_ok)
4618 			return status;
4619 	}
4620 	return nfserr;
4621 }
4622 
4623 static __be32
4624 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr,
4625 		   union nfsd4_op_u *u)
4626 {
4627 	struct nfsd4_locku *locku = &u->locku;
4628 	struct xdr_stream *xdr = resp->xdr;
4629 
4630 	/* lock_stateid */
4631 	return nfsd4_encode_stateid4(xdr, &locku->lu_stateid);
4632 }
4633 
4634 
4635 static __be32
4636 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr,
4637 		  union nfsd4_op_u *u)
4638 {
4639 	struct nfsd4_link *link = &u->link;
4640 	struct xdr_stream *xdr = resp->xdr;
4641 
4642 	return nfsd4_encode_change_info4(xdr, &link->li_cinfo);
4643 }
4644 
4645 /*
4646  * This implementation does not yet support returning an ACE in an
4647  * OPEN that offers a delegation.
4648  */
4649 static __be32
4650 nfsd4_encode_open_nfsace4(struct xdr_stream *xdr)
4651 {
4652 	__be32 status;
4653 
4654 	/* type */
4655 	status = nfsd4_encode_acetype4(xdr, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
4656 	if (status != nfs_ok)
4657 		return nfserr_resource;
4658 	/* flag */
4659 	status = nfsd4_encode_aceflag4(xdr, 0);
4660 	if (status != nfs_ok)
4661 		return nfserr_resource;
4662 	/* access mask */
4663 	status = nfsd4_encode_acemask4(xdr, 0);
4664 	if (status != nfs_ok)
4665 		return nfserr_resource;
4666 	/* who - empty for now */
4667 	if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
4668 		return nfserr_resource;
4669 	return nfs_ok;
4670 }
4671 
4672 static __be32
4673 nfsd4_encode_open_read_delegation4(struct xdr_stream *xdr, struct nfsd4_open *open)
4674 {
4675 	__be32 status;
4676 
4677 	/* stateid */
4678 	status = nfsd4_encode_stateid4(xdr, &open->op_delegate_stateid);
4679 	if (status != nfs_ok)
4680 		return status;
4681 	/* recall */
4682 	status = nfsd4_encode_bool(xdr, open->op_recall);
4683 	if (status != nfs_ok)
4684 		return status;
4685 	/* permissions */
4686 	return nfsd4_encode_open_nfsace4(xdr);
4687 }
4688 
4689 static __be32
4690 nfsd4_encode_nfs_space_limit4(struct xdr_stream *xdr, u64 filesize)
4691 {
4692 	/* limitby */
4693 	if (xdr_stream_encode_u32(xdr, NFS4_LIMIT_SIZE) != XDR_UNIT)
4694 		return nfserr_resource;
4695 	/* filesize */
4696 	return nfsd4_encode_uint64_t(xdr, filesize);
4697 }
4698 
4699 static __be32
4700 nfsd4_encode_open_write_delegation4(struct xdr_stream *xdr,
4701 				    struct nfsd4_open *open)
4702 {
4703 	__be32 status;
4704 
4705 	/* stateid */
4706 	status = nfsd4_encode_stateid4(xdr, &open->op_delegate_stateid);
4707 	if (status != nfs_ok)
4708 		return status;
4709 	/* recall */
4710 	status = nfsd4_encode_bool(xdr, open->op_recall);
4711 	if (status != nfs_ok)
4712 		return status;
4713 	/* space_limit */
4714 	status = nfsd4_encode_nfs_space_limit4(xdr, 0);
4715 	if (status != nfs_ok)
4716 		return status;
4717 	return nfsd4_encode_open_nfsace4(xdr);
4718 }
4719 
4720 static __be32
4721 nfsd4_encode_open_none_delegation4(struct xdr_stream *xdr,
4722 				   struct nfsd4_open *open)
4723 {
4724 	__be32 status = nfs_ok;
4725 
4726 	/* ond_why */
4727 	if (xdr_stream_encode_u32(xdr, open->op_why_no_deleg) != XDR_UNIT)
4728 		return nfserr_resource;
4729 	switch (open->op_why_no_deleg) {
4730 	case WND4_CONTENTION:
4731 		/* ond_server_will_push_deleg */
4732 		status = nfsd4_encode_bool(xdr, false);
4733 		break;
4734 	case WND4_RESOURCE:
4735 		/* ond_server_will_signal_avail */
4736 		status = nfsd4_encode_bool(xdr, false);
4737 	}
4738 	return status;
4739 }
4740 
4741 static __be32
4742 nfsd4_encode_open_delegation4(struct xdr_stream *xdr, struct nfsd4_open *open)
4743 {
4744 	__be32 status;
4745 
4746 	/* delegation_type */
4747 	if (xdr_stream_encode_u32(xdr, open->op_delegate_type) != XDR_UNIT)
4748 		return nfserr_resource;
4749 	switch (open->op_delegate_type) {
4750 	case OPEN_DELEGATE_NONE:
4751 		status = nfs_ok;
4752 		break;
4753 	case OPEN_DELEGATE_READ:
4754 	case OPEN_DELEGATE_READ_ATTRS_DELEG:
4755 		/* read */
4756 		status = nfsd4_encode_open_read_delegation4(xdr, open);
4757 		break;
4758 	case OPEN_DELEGATE_WRITE:
4759 	case OPEN_DELEGATE_WRITE_ATTRS_DELEG:
4760 		/* write */
4761 		status = nfsd4_encode_open_write_delegation4(xdr, open);
4762 		break;
4763 	case OPEN_DELEGATE_NONE_EXT:
4764 		/* od_whynone */
4765 		status = nfsd4_encode_open_none_delegation4(xdr, open);
4766 		break;
4767 	default:
4768 		status = nfserr_serverfault;
4769 	}
4770 
4771 	return status;
4772 }
4773 
4774 static __be32
4775 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr,
4776 		  union nfsd4_op_u *u)
4777 {
4778 	struct nfsd4_open *open = &u->open;
4779 	struct xdr_stream *xdr = resp->xdr;
4780 
4781 	/* stateid */
4782 	nfserr = nfsd4_encode_stateid4(xdr, &open->op_stateid);
4783 	if (nfserr != nfs_ok)
4784 		return nfserr;
4785 	/* cinfo */
4786 	nfserr = nfsd4_encode_change_info4(xdr, &open->op_cinfo);
4787 	if (nfserr != nfs_ok)
4788 		return nfserr;
4789 	/* rflags */
4790 	nfserr = nfsd4_encode_uint32_t(xdr, open->op_rflags);
4791 	if (nfserr != nfs_ok)
4792 		return nfserr;
4793 	/* attrset */
4794 	nfserr = nfsd4_encode_bitmap4(xdr, open->op_bmval[0],
4795 				      open->op_bmval[1], open->op_bmval[2]);
4796 	if (nfserr != nfs_ok)
4797 		return nfserr;
4798 	/* delegation */
4799 	return nfsd4_encode_open_delegation4(xdr, open);
4800 }
4801 
4802 static __be32
4803 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr,
4804 			  union nfsd4_op_u *u)
4805 {
4806 	struct nfsd4_open_confirm *oc = &u->open_confirm;
4807 	struct xdr_stream *xdr = resp->xdr;
4808 
4809 	/* open_stateid */
4810 	return nfsd4_encode_stateid4(xdr, &oc->oc_resp_stateid);
4811 }
4812 
4813 static __be32
4814 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr,
4815 			    union nfsd4_op_u *u)
4816 {
4817 	struct nfsd4_open_downgrade *od = &u->open_downgrade;
4818 	struct xdr_stream *xdr = resp->xdr;
4819 
4820 	/* open_stateid */
4821 	return nfsd4_encode_stateid4(xdr, &od->od_stateid);
4822 }
4823 
4824 /*
4825  * The operation of this function assumes that this is the only
4826  * READ operation in the COMPOUND. If there are multiple READs,
4827  * we use nfsd4_encode_readv().
4828  */
4829 static __be32 nfsd4_encode_splice_read(
4830 				struct nfsd4_compoundres *resp,
4831 				struct nfsd4_read *read,
4832 				struct file *file, unsigned long maxcount)
4833 {
4834 	struct xdr_stream *xdr = resp->xdr;
4835 	struct xdr_buf *buf = xdr->buf;
4836 	int status, space_left;
4837 	__be32 nfserr;
4838 
4839 	/*
4840 	 * Splice read doesn't work if encoding has already wandered
4841 	 * into the XDR buf's page array.
4842 	 */
4843 	if (unlikely(xdr->buf->page_len)) {
4844 		WARN_ON_ONCE(1);
4845 		return nfserr_serverfault;
4846 	}
4847 
4848 	/*
4849 	 * Make sure there is room at the end of buf->head for
4850 	 * svcxdr_encode_opaque_pages() to create a tail buffer
4851 	 * to XDR-pad the payload.
4852 	 */
4853 	if (xdr->iov != xdr->buf->head || xdr->end - xdr->p < 1)
4854 		return nfserr_resource;
4855 
4856 	nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
4857 				  file, read->rd_offset, &maxcount,
4858 				  &read->rd_eof);
4859 	read->rd_length = maxcount;
4860 	if (nfserr)
4861 		goto out_err;
4862 	svcxdr_encode_opaque_pages(read->rd_rqstp, xdr, buf->pages,
4863 				   buf->page_base, maxcount);
4864 	status = svc_encode_result_payload(read->rd_rqstp,
4865 					   buf->head[0].iov_len, maxcount);
4866 	if (status) {
4867 		nfserr = nfserrno(status);
4868 		goto out_err;
4869 	}
4870 
4871 	/*
4872 	 * Prepare to encode subsequent operations.
4873 	 *
4874 	 * xdr_truncate_encode() is not safe to use after a successful
4875 	 * splice read has been done, so the following stream
4876 	 * manipulations are open-coded.
4877 	 */
4878 	space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
4879 				buf->buflen - buf->len);
4880 	buf->buflen = buf->len + space_left;
4881 	xdr->end = (__be32 *)((void *)xdr->end + space_left);
4882 
4883 	return nfs_ok;
4884 
4885 out_err:
4886 	/*
4887 	 * nfsd_splice_actor may have already messed with the
4888 	 * page length; reset it so as not to confuse
4889 	 * xdr_truncate_encode in our caller.
4890 	 */
4891 	buf->page_len = 0;
4892 	return nfserr;
4893 }
4894 
4895 static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
4896 				 struct nfsd4_read *read,
4897 				 unsigned long maxcount)
4898 {
4899 	struct xdr_stream *xdr = resp->xdr;
4900 	unsigned int base = xdr->buf->page_len & ~PAGE_MASK;
4901 	unsigned int starting_len = xdr->buf->len;
4902 	__be32 zero = xdr_zero;
4903 	__be32 nfserr;
4904 
4905 	nfserr = nfsd_iter_read(resp->rqstp, read->rd_fhp, read->rd_nf,
4906 				read->rd_offset, &maxcount, base,
4907 				&read->rd_eof);
4908 	read->rd_length = maxcount;
4909 	if (nfserr)
4910 		return nfserr;
4911 
4912 	/*
4913 	 * svcxdr_encode_opaque_pages() is not used here because
4914 	 * we don't want to encode subsequent results in this
4915 	 * COMPOUND into the xdr->buf's tail, but rather those
4916 	 * results should follow the NFS READ payload in the
4917 	 * buf's pages.
4918 	 */
4919 	if (xdr_reserve_space_vec(xdr, maxcount) < 0)
4920 		return nfserr_resource;
4921 
4922 	/*
4923 	 * Mark the buffer location of the NFS READ payload so that
4924 	 * direct placement-capable transports send only the
4925 	 * payload bytes out-of-band.
4926 	 */
4927 	if (svc_encode_result_payload(resp->rqstp, starting_len, maxcount))
4928 		return nfserr_io;
4929 
4930 	write_bytes_to_xdr_buf(xdr->buf, starting_len + maxcount, &zero,
4931 			       xdr_pad_size(maxcount));
4932 	return nfs_ok;
4933 }
4934 
4935 static __be32
4936 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
4937 		  union nfsd4_op_u *u)
4938 {
4939 	struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
4940 	struct nfsd4_read *read = &u->read;
4941 	struct xdr_stream *xdr = resp->xdr;
4942 	bool splice_ok = argp->splice_ok;
4943 	unsigned int eof_offset;
4944 	unsigned long maxcount;
4945 	__be32 wire_data[2];
4946 	struct file *file;
4947 
4948 	if (nfserr)
4949 		return nfserr;
4950 
4951 	eof_offset = xdr->buf->len;
4952 	file = read->rd_nf->nf_file;
4953 
4954 	/* Reserve space for the eof flag and byte count */
4955 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2))) {
4956 		WARN_ON_ONCE(splice_ok);
4957 		return nfserr_resource;
4958 	}
4959 	xdr_commit_encode(xdr);
4960 
4961 	maxcount = min_t(unsigned long, read->rd_length,
4962 			 (xdr->buf->buflen - xdr->buf->len));
4963 
4964 	if (file->f_op->splice_read && splice_ok)
4965 		nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
4966 	else
4967 		nfserr = nfsd4_encode_readv(resp, read, maxcount);
4968 	if (nfserr) {
4969 		xdr_truncate_encode(xdr, eof_offset);
4970 		return nfserr;
4971 	}
4972 
4973 	wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
4974 	wire_data[1] = cpu_to_be32(read->rd_length);
4975 	write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
4976 	return nfs_ok;
4977 }
4978 
4979 static __be32
4980 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr,
4981 		      union nfsd4_op_u *u)
4982 {
4983 	struct nfsd4_readlink *readlink = &u->readlink;
4984 	__be32 *p, wire_count, zero = xdr_zero;
4985 	struct xdr_stream *xdr = resp->xdr;
4986 	unsigned int length_offset;
4987 	int maxcount, status;
4988 
4989 	/* linktext4.count */
4990 	length_offset = xdr->buf->len;
4991 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
4992 		return nfserr_resource;
4993 
4994 	/* linktext4.data */
4995 	maxcount = PAGE_SIZE;
4996 	p = xdr_reserve_space(xdr, maxcount);
4997 	if (!p)
4998 		return nfserr_resource;
4999 	nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
5000 						(char *)p, &maxcount);
5001 	if (nfserr == nfserr_isdir)
5002 		nfserr = nfserr_inval;
5003 	if (nfserr)
5004 		goto out_err;
5005 	status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
5006 					   maxcount);
5007 	if (status) {
5008 		nfserr = nfserrno(status);
5009 		goto out_err;
5010 	}
5011 
5012 	wire_count = cpu_to_be32(maxcount);
5013 	write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, XDR_UNIT);
5014 	xdr_truncate_encode(xdr, length_offset + 4 + xdr_align_size(maxcount));
5015 	write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount, &zero,
5016 			       xdr_pad_size(maxcount));
5017 	return nfs_ok;
5018 
5019 out_err:
5020 	xdr_truncate_encode(xdr, length_offset);
5021 	return nfserr;
5022 }
5023 
5024 static __be32 nfsd4_encode_dirlist4(struct xdr_stream *xdr,
5025 				    struct nfsd4_readdir *readdir,
5026 				    u32 max_payload)
5027 {
5028 	int bytes_left, maxcount, starting_len = xdr->buf->len;
5029 	loff_t offset;
5030 	__be32 status;
5031 
5032 	/*
5033 	 * Number of bytes left for directory entries allowing for the
5034 	 * final 8 bytes of the readdir and a following failed op.
5035 	 */
5036 	bytes_left = xdr->buf->buflen - xdr->buf->len -
5037 		COMPOUND_ERR_SLACK_SPACE - XDR_UNIT * 2;
5038 	if (bytes_left < 0)
5039 		return nfserr_resource;
5040 	maxcount = min_t(u32, readdir->rd_maxcount, max_payload);
5041 
5042 	/*
5043 	 * The RFC defines rd_maxcount as the size of the
5044 	 * READDIR4resok structure, which includes the verifier
5045 	 * and the 8 bytes encoded at the end of this function.
5046 	 */
5047 	if (maxcount < XDR_UNIT * 4)
5048 		return nfserr_toosmall;
5049 	maxcount = min_t(int, maxcount - XDR_UNIT * 4, bytes_left);
5050 
5051 	/* RFC 3530 14.2.24 allows us to ignore dircount when it's 0 */
5052 	if (!readdir->rd_dircount)
5053 		readdir->rd_dircount = max_payload;
5054 
5055 	/* *entries */
5056 	readdir->xdr = xdr;
5057 	readdir->rd_maxcount = maxcount;
5058 	readdir->common.err = 0;
5059 	readdir->cookie_offset = 0;
5060 	readdir->rd_case_cache.dir = readdir->rd_fhp->fh_dentry;
5061 	readdir->rd_case_cache.valid = false;
5062 	offset = readdir->rd_cookie;
5063 	status = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp, &offset,
5064 			      &readdir->common, nfsd4_encode_entry4);
5065 	if (status)
5066 		return status;
5067 	if (readdir->common.err == nfserr_toosmall &&
5068 	    xdr->buf->len == starting_len) {
5069 		/* No entries were encoded. Which limit did we hit? */
5070 		if (maxcount - XDR_UNIT * 4 < bytes_left)
5071 			/* It was the fault of rd_maxcount */
5072 			return nfserr_toosmall;
5073 		/* We ran out of buffer space */
5074 		return nfserr_resource;
5075 	}
5076 	/* Encode the final entry's cookie value */
5077 	nfsd4_encode_entry4_nfs_cookie4(readdir, offset);
5078 	/* No entries follow */
5079 	if (xdr_stream_encode_item_absent(xdr) != XDR_UNIT)
5080 		return nfserr_resource;
5081 
5082 	/* eof */
5083 	return nfsd4_encode_bool(xdr, readdir->common.err == nfserr_eof);
5084 }
5085 
5086 static __be32
5087 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr,
5088 		     union nfsd4_op_u *u)
5089 {
5090 	struct nfsd4_readdir *readdir = &u->readdir;
5091 	struct xdr_stream *xdr = resp->xdr;
5092 	int starting_len = xdr->buf->len;
5093 
5094 	/* cookieverf */
5095 	nfserr = nfsd4_encode_verifier4(xdr, &readdir->rd_verf);
5096 	if (nfserr != nfs_ok)
5097 		return nfserr;
5098 
5099 	/* reply */
5100 	nfserr = nfsd4_encode_dirlist4(xdr, readdir, svc_max_payload(resp->rqstp));
5101 	if (nfserr != nfs_ok)
5102 		xdr_truncate_encode(xdr, starting_len);
5103 	return nfserr;
5104 }
5105 
5106 static __be32
5107 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr,
5108 		    union nfsd4_op_u *u)
5109 {
5110 	struct nfsd4_remove *remove = &u->remove;
5111 	struct xdr_stream *xdr = resp->xdr;
5112 
5113 	return nfsd4_encode_change_info4(xdr, &remove->rm_cinfo);
5114 }
5115 
5116 static __be32
5117 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr,
5118 		    union nfsd4_op_u *u)
5119 {
5120 	struct nfsd4_rename *rename = &u->rename;
5121 	struct xdr_stream *xdr = resp->xdr;
5122 
5123 	nfserr = nfsd4_encode_change_info4(xdr, &rename->rn_sinfo);
5124 	if (nfserr)
5125 		return nfserr;
5126 	return nfsd4_encode_change_info4(xdr, &rename->rn_tinfo);
5127 }
5128 
5129 static __be32
5130 nfsd4_encode_rpcsec_gss_info(struct xdr_stream *xdr,
5131 			     struct rpcsec_gss_info *info)
5132 {
5133 	__be32 status;
5134 
5135 	/* oid */
5136 	if (xdr_stream_encode_opaque(xdr, info->oid.data, info->oid.len) < 0)
5137 		return nfserr_resource;
5138 	/* qop */
5139 	status = nfsd4_encode_qop4(xdr, info->qop);
5140 	if (status != nfs_ok)
5141 		return status;
5142 	/* service */
5143 	if (xdr_stream_encode_u32(xdr, info->service) != XDR_UNIT)
5144 		return nfserr_resource;
5145 
5146 	return nfs_ok;
5147 }
5148 
5149 static __be32
5150 nfsd4_encode_secinfo4(struct xdr_stream *xdr, rpc_authflavor_t pf,
5151 		      u32 *supported)
5152 {
5153 	struct rpcsec_gss_info info;
5154 	__be32 status;
5155 
5156 	if (rpcauth_get_gssinfo(pf, &info) == 0) {
5157 		(*supported)++;
5158 
5159 		/* flavor */
5160 		status = nfsd4_encode_uint32_t(xdr, RPC_AUTH_GSS);
5161 		if (status != nfs_ok)
5162 			return status;
5163 		/* flavor_info */
5164 		status = nfsd4_encode_rpcsec_gss_info(xdr, &info);
5165 		if (status != nfs_ok)
5166 			return status;
5167 	} else if (pf < RPC_AUTH_MAXFLAVOR) {
5168 		(*supported)++;
5169 
5170 		/* flavor */
5171 		status = nfsd4_encode_uint32_t(xdr, pf);
5172 		if (status != nfs_ok)
5173 			return status;
5174 	}
5175 	return nfs_ok;
5176 }
5177 
5178 static __be32
5179 nfsd4_encode_SECINFO4resok(struct xdr_stream *xdr, struct svc_export *exp)
5180 {
5181 	u32 i, nflavs, supported;
5182 	struct exp_flavor_info *flavs;
5183 	struct exp_flavor_info def_flavs[2];
5184 	unsigned int count_offset;
5185 	__be32 status, wire_count;
5186 
5187 	if (exp->ex_nflavors) {
5188 		flavs = exp->ex_flavors;
5189 		nflavs = exp->ex_nflavors;
5190 	} else { /* Handling of some defaults in absence of real secinfo: */
5191 		flavs = def_flavs;
5192 		if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
5193 			nflavs = 2;
5194 			flavs[0].pseudoflavor = RPC_AUTH_UNIX;
5195 			flavs[1].pseudoflavor = RPC_AUTH_NULL;
5196 		} else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
5197 			nflavs = 1;
5198 			flavs[0].pseudoflavor
5199 					= svcauth_gss_flavor(exp->ex_client);
5200 		} else {
5201 			nflavs = 1;
5202 			flavs[0].pseudoflavor
5203 					= exp->ex_client->flavour->flavour;
5204 		}
5205 	}
5206 
5207 	count_offset = xdr->buf->len;
5208 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
5209 		return nfserr_resource;
5210 
5211 	for (i = 0, supported = 0; i < nflavs; i++) {
5212 		status = nfsd4_encode_secinfo4(xdr, flavs[i].pseudoflavor,
5213 					       &supported);
5214 		if (status != nfs_ok)
5215 			return status;
5216 	}
5217 
5218 	wire_count = cpu_to_be32(supported);
5219 	write_bytes_to_xdr_buf(xdr->buf, count_offset, &wire_count,
5220 			       XDR_UNIT);
5221 	return 0;
5222 }
5223 
5224 static __be32
5225 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
5226 		     union nfsd4_op_u *u)
5227 {
5228 	struct nfsd4_secinfo *secinfo = &u->secinfo;
5229 	struct xdr_stream *xdr = resp->xdr;
5230 
5231 	return nfsd4_encode_SECINFO4resok(xdr, secinfo->si_exp);
5232 }
5233 
5234 static __be32
5235 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
5236 		     union nfsd4_op_u *u)
5237 {
5238 	struct nfsd4_secinfo_no_name *secinfo = &u->secinfo_no_name;
5239 	struct xdr_stream *xdr = resp->xdr;
5240 
5241 	return nfsd4_encode_SECINFO4resok(xdr, secinfo->sin_exp);
5242 }
5243 
5244 static __be32
5245 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5246 		     union nfsd4_op_u *u)
5247 {
5248 	struct nfsd4_setattr *setattr = &u->setattr;
5249 	__be32 status;
5250 
5251 	switch (nfserr) {
5252 	case nfs_ok:
5253 		/* attrsset */
5254 		status = nfsd4_encode_bitmap4(resp->xdr, setattr->sa_bmval[0],
5255 					      setattr->sa_bmval[1],
5256 					      setattr->sa_bmval[2]);
5257 		break;
5258 	default:
5259 		/* attrsset */
5260 		status = nfsd4_encode_bitmap4(resp->xdr, 0, 0, 0);
5261 	}
5262 	return status != nfs_ok ? status : nfserr;
5263 }
5264 
5265 static __be32
5266 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr,
5267 			 union nfsd4_op_u *u)
5268 {
5269 	struct nfsd4_setclientid *scd = &u->setclientid;
5270 	struct xdr_stream *xdr = resp->xdr;
5271 
5272 	if (!nfserr) {
5273 		nfserr = nfsd4_encode_clientid4(xdr, &scd->se_clientid);
5274 		if (nfserr != nfs_ok)
5275 			goto out;
5276 		nfserr = nfsd4_encode_verifier4(xdr, &scd->se_confirm);
5277 	} else if (nfserr == nfserr_clid_inuse) {
5278 		/* empty network id */
5279 		if (xdr_stream_encode_u32(xdr, 0) < 0) {
5280 			nfserr = nfserr_resource;
5281 			goto out;
5282 		}
5283 		/* empty universal address */
5284 		if (xdr_stream_encode_u32(xdr, 0) < 0) {
5285 			nfserr = nfserr_resource;
5286 			goto out;
5287 		}
5288 	}
5289 out:
5290 	return nfserr;
5291 }
5292 
5293 static __be32
5294 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr,
5295 		   union nfsd4_op_u *u)
5296 {
5297 	struct nfsd4_write *write = &u->write;
5298 	struct xdr_stream *xdr = resp->xdr;
5299 
5300 	/* count */
5301 	nfserr = nfsd4_encode_count4(xdr, write->wr_bytes_written);
5302 	if (nfserr)
5303 		return nfserr;
5304 	/* committed */
5305 	if (xdr_stream_encode_u32(xdr, write->wr_how_written) != XDR_UNIT)
5306 		return nfserr_resource;
5307 	/* writeverf */
5308 	return nfsd4_encode_verifier4(xdr, &write->wr_verifier);
5309 }
5310 
5311 static __be32
5312 nfsd4_encode_state_protect_ops4(struct xdr_stream *xdr,
5313 				struct nfsd4_exchange_id *exid)
5314 {
5315 	__be32 status;
5316 
5317 	/* spo_must_enforce */
5318 	status = nfsd4_encode_bitmap4(xdr, exid->spo_must_enforce[0],
5319 				      exid->spo_must_enforce[1],
5320 				      exid->spo_must_enforce[2]);
5321 	if (status != nfs_ok)
5322 		return status;
5323 	/* spo_must_allow */
5324 	return nfsd4_encode_bitmap4(xdr, exid->spo_must_allow[0],
5325 				    exid->spo_must_allow[1],
5326 				    exid->spo_must_allow[2]);
5327 }
5328 
5329 static __be32
5330 nfsd4_encode_state_protect4_r(struct xdr_stream *xdr, struct nfsd4_exchange_id *exid)
5331 {
5332 	__be32 status;
5333 
5334 	if (xdr_stream_encode_u32(xdr, exid->spa_how) != XDR_UNIT)
5335 		return nfserr_resource;
5336 	switch (exid->spa_how) {
5337 	case SP4_NONE:
5338 		status = nfs_ok;
5339 		break;
5340 	case SP4_MACH_CRED:
5341 		/* spr_mach_ops */
5342 		status = nfsd4_encode_state_protect_ops4(xdr, exid);
5343 		break;
5344 	default:
5345 		status = nfserr_serverfault;
5346 	}
5347 	return status;
5348 }
5349 
5350 static __be32
5351 nfsd4_encode_server_owner4(struct xdr_stream *xdr, struct svc_rqst *rqstp)
5352 {
5353 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5354 	__be32 status;
5355 
5356 	/* so_minor_id */
5357 	status = nfsd4_encode_uint64_t(xdr, 0);
5358 	if (status != nfs_ok)
5359 		return status;
5360 	/* so_major_id */
5361 	return nfsd4_encode_opaque(xdr, nn->nfsd_name, strlen(nn->nfsd_name));
5362 }
5363 
5364 static __be32
5365 nfsd4_encode_nfs_impl_id4(struct xdr_stream *xdr, struct nfsd4_exchange_id *exid)
5366 {
5367 	__be32 status;
5368 
5369 	/* nii_domain */
5370 	status = nfsd4_encode_opaque(xdr, exid->nii_domain.data,
5371 				     exid->nii_domain.len);
5372 	if (status != nfs_ok)
5373 		return status;
5374 	/* nii_name */
5375 	status = nfsd4_encode_opaque(xdr, exid->nii_name.data,
5376 				     exid->nii_name.len);
5377 	if (status != nfs_ok)
5378 		return status;
5379 	/* nii_time */
5380 	return nfsd4_encode_nfstime4(xdr, &exid->nii_time);
5381 }
5382 
5383 static __be32
5384 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
5385 			 union nfsd4_op_u *u)
5386 {
5387 	struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
5388 	struct nfsd4_exchange_id *exid = &u->exchange_id;
5389 	struct xdr_stream *xdr = resp->xdr;
5390 
5391 	/* eir_clientid */
5392 	nfserr = nfsd4_encode_clientid4(xdr, &exid->clientid);
5393 	if (nfserr != nfs_ok)
5394 		return nfserr;
5395 	/* eir_sequenceid */
5396 	nfserr = nfsd4_encode_sequenceid4(xdr, exid->seqid);
5397 	if (nfserr != nfs_ok)
5398 		return nfserr;
5399 	/* eir_flags */
5400 	nfserr = nfsd4_encode_uint32_t(xdr, exid->flags);
5401 	if (nfserr != nfs_ok)
5402 		return nfserr;
5403 	/* eir_state_protect */
5404 	nfserr = nfsd4_encode_state_protect4_r(xdr, exid);
5405 	if (nfserr != nfs_ok)
5406 		return nfserr;
5407 	/* eir_server_owner */
5408 	nfserr = nfsd4_encode_server_owner4(xdr, resp->rqstp);
5409 	if (nfserr != nfs_ok)
5410 		return nfserr;
5411 	/* eir_server_scope */
5412 	nfserr = nfsd4_encode_opaque(xdr, nn->nfsd_name,
5413 				     strlen(nn->nfsd_name));
5414 	if (nfserr != nfs_ok)
5415 		return nfserr;
5416 	/* eir_server_impl_id<1> */
5417 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5418 		return nfserr_resource;
5419 	nfserr = nfsd4_encode_nfs_impl_id4(xdr, exid);
5420 	if (nfserr != nfs_ok)
5421 		return nfserr;
5422 
5423 	return nfs_ok;
5424 }
5425 
5426 static __be32
5427 nfsd4_encode_channel_attrs4(struct xdr_stream *xdr,
5428 			    const struct nfsd4_channel_attrs *attrs)
5429 {
5430 	__be32 status;
5431 
5432 	/* ca_headerpadsize */
5433 	status = nfsd4_encode_count4(xdr, 0);
5434 	if (status != nfs_ok)
5435 		return status;
5436 	/* ca_maxrequestsize */
5437 	status = nfsd4_encode_count4(xdr, attrs->maxreq_sz);
5438 	if (status != nfs_ok)
5439 		return status;
5440 	/* ca_maxresponsesize */
5441 	status = nfsd4_encode_count4(xdr, attrs->maxresp_sz);
5442 	if (status != nfs_ok)
5443 		return status;
5444 	/* ca_maxresponsesize_cached */
5445 	status = nfsd4_encode_count4(xdr, attrs->maxresp_cached);
5446 	if (status != nfs_ok)
5447 		return status;
5448 	/* ca_maxoperations */
5449 	status = nfsd4_encode_count4(xdr, attrs->maxops);
5450 	if (status != nfs_ok)
5451 		return status;
5452 	/* ca_maxrequests */
5453 	status = nfsd4_encode_count4(xdr, attrs->maxreqs);
5454 	if (status != nfs_ok)
5455 		return status;
5456 	/* ca_rdma_ird<1> */
5457 	if (xdr_stream_encode_u32(xdr, attrs->nr_rdma_attrs) != XDR_UNIT)
5458 		return nfserr_resource;
5459 	if (attrs->nr_rdma_attrs)
5460 		return nfsd4_encode_uint32_t(xdr, attrs->rdma_attrs);
5461 	return nfs_ok;
5462 }
5463 
5464 static __be32
5465 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
5466 			    union nfsd4_op_u *u)
5467 {
5468 	struct nfsd4_create_session *sess = &u->create_session;
5469 	struct xdr_stream *xdr = resp->xdr;
5470 
5471 	/* csr_sessionid */
5472 	nfserr = nfsd4_encode_sessionid4(xdr, &sess->sessionid);
5473 	if (nfserr != nfs_ok)
5474 		return nfserr;
5475 	/* csr_sequence */
5476 	nfserr = nfsd4_encode_sequenceid4(xdr, sess->seqid);
5477 	if (nfserr != nfs_ok)
5478 		return nfserr;
5479 	/* csr_flags */
5480 	nfserr = nfsd4_encode_uint32_t(xdr, sess->flags);
5481 	if (nfserr != nfs_ok)
5482 		return nfserr;
5483 	/* csr_fore_chan_attrs */
5484 	nfserr = nfsd4_encode_channel_attrs4(xdr, &sess->fore_channel);
5485 	if (nfserr != nfs_ok)
5486 		return nfserr;
5487 	/* csr_back_chan_attrs */
5488 	return nfsd4_encode_channel_attrs4(xdr, &sess->back_channel);
5489 }
5490 
5491 static __be32
5492 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
5493 		      union nfsd4_op_u *u)
5494 {
5495 	struct nfsd4_sequence *seq = &u->sequence;
5496 	struct xdr_stream *xdr = resp->xdr;
5497 
5498 	/* sr_sessionid */
5499 	nfserr = nfsd4_encode_sessionid4(xdr, &seq->sessionid);
5500 	if (nfserr != nfs_ok)
5501 		return nfserr;
5502 	/* sr_sequenceid */
5503 	nfserr = nfsd4_encode_sequenceid4(xdr, seq->seqid);
5504 	if (nfserr != nfs_ok)
5505 		return nfserr;
5506 	/* sr_slotid */
5507 	nfserr = nfsd4_encode_slotid4(xdr, seq->slotid);
5508 	if (nfserr != nfs_ok)
5509 		return nfserr;
5510 	/* Note slotid's are numbered from zero: */
5511 	/* sr_highest_slotid */
5512 	nfserr = nfsd4_encode_slotid4(xdr, seq->maxslots_response - 1);
5513 	if (nfserr != nfs_ok)
5514 		return nfserr;
5515 	/* sr_target_highest_slotid */
5516 	nfserr = nfsd4_encode_slotid4(xdr, seq->target_maxslots - 1);
5517 	if (nfserr != nfs_ok)
5518 		return nfserr;
5519 	/* sr_status_flags */
5520 	nfserr = nfsd4_encode_uint32_t(xdr, seq->status_flags);
5521 	if (nfserr != nfs_ok)
5522 		return nfserr;
5523 
5524 	resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
5525 	return nfs_ok;
5526 }
5527 
5528 static __be32
5529 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
5530 			  union nfsd4_op_u *u)
5531 {
5532 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5533 	struct nfsd4_test_stateid_id *stateid, *next;
5534 	struct xdr_stream *xdr = resp->xdr;
5535 
5536 	/* tsr_status_codes<> */
5537 	if (xdr_stream_encode_u32(xdr, test_stateid->ts_num_ids) != XDR_UNIT)
5538 		return nfserr_resource;
5539 	list_for_each_entry_safe(stateid, next,
5540 				 &test_stateid->ts_stateid_list, ts_id_list) {
5541 		if (xdr_stream_encode_be32(xdr, stateid->ts_id_status) != XDR_UNIT)
5542 			return nfserr_resource;
5543 	}
5544 	return nfs_ok;
5545 }
5546 
5547 static __be32
5548 nfsd4_encode_get_dir_delegation(struct nfsd4_compoundres *resp, __be32 nfserr,
5549 				union nfsd4_op_u *u)
5550 {
5551 	struct nfsd4_get_dir_delegation *gdd = &u->get_dir_delegation;
5552 	struct xdr_stream *xdr = resp->xdr;
5553 	__be32 status = nfserr_resource;
5554 
5555 	switch(gdd->gddrnf_status) {
5556 	case GDD4_OK:
5557 		if (xdr_stream_encode_u32(xdr, GDD4_OK) != XDR_UNIT)
5558 			break;
5559 		status = nfsd4_encode_verifier4(xdr, &gdd->gddr_cookieverf);
5560 		if (status)
5561 			break;
5562 		status = nfsd4_encode_stateid4(xdr, &gdd->gddr_stateid);
5563 		if (status)
5564 			break;
5565 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_notification[0], 0, 0);
5566 		if (status)
5567 			break;
5568 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_child_attributes[0],
5569 						   gdd->gddr_child_attributes[1],
5570 						   gdd->gddr_child_attributes[2]);
5571 		if (status)
5572 			break;
5573 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_dir_attributes[0],
5574 						   gdd->gddr_dir_attributes[1],
5575 						   gdd->gddr_dir_attributes[2]);
5576 		break;
5577 	default:
5578 		pr_warn("nfsd: bad gddrnf_status (%u)\n", gdd->gddrnf_status);
5579 		gdd->gddrnf_will_signal_deleg_avail = 0;
5580 		fallthrough;
5581 	case GDD4_UNAVAIL:
5582 		if (xdr_stream_encode_u32(xdr, GDD4_UNAVAIL) != XDR_UNIT)
5583 			break;
5584 		status = nfsd4_encode_bool(xdr, gdd->gddrnf_will_signal_deleg_avail);
5585 		break;
5586 	}
5587 	return status;
5588 }
5589 
5590 #ifdef CONFIG_NFSD_PNFS
5591 static __be32
5592 nfsd4_encode_device_addr4(struct xdr_stream *xdr,
5593 			  const struct nfsd4_getdeviceinfo *gdev)
5594 {
5595 	u32 needed_len, starting_len = xdr->buf->len;
5596 	const struct nfsd4_layout_ops *ops;
5597 	__be32 status;
5598 
5599 	/* da_layout_type */
5600 	if (xdr_stream_encode_u32(xdr, gdev->gd_layout_type) != XDR_UNIT)
5601 		return nfserr_resource;
5602 	/* da_addr_body */
5603 	ops = nfsd4_layout_ops[gdev->gd_layout_type];
5604 	status = ops->encode_getdeviceinfo(xdr, gdev);
5605 	if (status != nfs_ok) {
5606 		/*
5607 		 * Don't burden the layout drivers with enforcing
5608 		 * gd_maxcount. Just tell the client to come back
5609 		 * with a bigger buffer if it's not enough.
5610 		 */
5611 		if (xdr->buf->len + XDR_UNIT > gdev->gd_maxcount)
5612 			goto toosmall;
5613 		return status;
5614 	}
5615 
5616 	return nfs_ok;
5617 
5618 toosmall:
5619 	needed_len = xdr->buf->len + XDR_UNIT;	/* notifications */
5620 	xdr_truncate_encode(xdr, starting_len);
5621 
5622 	status = nfsd4_encode_count4(xdr, needed_len);
5623 	if (status != nfs_ok)
5624 		return status;
5625 	return nfserr_toosmall;
5626 }
5627 
5628 static __be32
5629 nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
5630 		union nfsd4_op_u *u)
5631 {
5632 	struct nfsd4_getdeviceinfo *gdev = &u->getdeviceinfo;
5633 	struct xdr_stream *xdr = resp->xdr;
5634 
5635 	/* gdir_device_addr */
5636 	nfserr = nfsd4_encode_device_addr4(xdr, gdev);
5637 	if (nfserr)
5638 		return nfserr;
5639 	/* gdir_notification */
5640 	return nfsd4_encode_bitmap4(xdr, gdev->gd_notify_types, 0, 0);
5641 }
5642 
5643 static __be32
5644 nfsd4_encode_layout4(struct xdr_stream *xdr, const struct nfsd4_layoutget *lgp)
5645 {
5646 	const struct nfsd4_layout_ops *ops = nfsd4_layout_ops[lgp->lg_layout_type];
5647 	__be32 status;
5648 
5649 	/* lo_offset */
5650 	status = nfsd4_encode_offset4(xdr, lgp->lg_seg.offset);
5651 	if (status != nfs_ok)
5652 		return status;
5653 	/* lo_length */
5654 	status = nfsd4_encode_length4(xdr, lgp->lg_seg.length);
5655 	if (status != nfs_ok)
5656 		return status;
5657 	/* lo_iomode */
5658 	if (xdr_stream_encode_u32(xdr, lgp->lg_seg.iomode) != XDR_UNIT)
5659 		return nfserr_resource;
5660 	/* lo_content */
5661 	if (xdr_stream_encode_u32(xdr, lgp->lg_layout_type) != XDR_UNIT)
5662 		return nfserr_resource;
5663 	return ops->encode_layoutget(xdr, lgp);
5664 }
5665 
5666 static __be32
5667 nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
5668 		union nfsd4_op_u *u)
5669 {
5670 	struct nfsd4_layoutget *lgp = &u->layoutget;
5671 	struct xdr_stream *xdr = resp->xdr;
5672 
5673 	/* logr_return_on_close */
5674 	nfserr = nfsd4_encode_bool(xdr, true);
5675 	if (nfserr != nfs_ok)
5676 		return nfserr;
5677 	/* logr_stateid */
5678 	nfserr = nfsd4_encode_stateid4(xdr, &lgp->lg_sid);
5679 	if (nfserr != nfs_ok)
5680 		return nfserr;
5681 	/* logr_layout<> */
5682 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5683 		return nfserr_resource;
5684 	return nfsd4_encode_layout4(xdr, lgp);
5685 }
5686 
5687 static __be32
5688 nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
5689 			  union nfsd4_op_u *u)
5690 {
5691 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
5692 	struct xdr_stream *xdr = resp->xdr;
5693 
5694 	/* ns_sizechanged */
5695 	nfserr = nfsd4_encode_bool(xdr, lcp->lc_size_chg);
5696 	if (nfserr != nfs_ok)
5697 		return nfserr;
5698 	if (lcp->lc_size_chg)
5699 		/* ns_size */
5700 		return nfsd4_encode_length4(xdr, lcp->lc_newsize);
5701 	return nfs_ok;
5702 }
5703 
5704 static __be32
5705 nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
5706 		union nfsd4_op_u *u)
5707 {
5708 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
5709 	struct xdr_stream *xdr = resp->xdr;
5710 
5711 	/* lrs_present */
5712 	nfserr = nfsd4_encode_bool(xdr, lrp->lrs_present);
5713 	if (nfserr != nfs_ok)
5714 		return nfserr;
5715 	if (lrp->lrs_present)
5716 		/* lrs_stateid */
5717 		return nfsd4_encode_stateid4(xdr, &lrp->lr_sid);
5718 	return nfs_ok;
5719 }
5720 #endif /* CONFIG_NFSD_PNFS */
5721 
5722 static __be32
5723 nfsd4_encode_write_response4(struct xdr_stream *xdr,
5724 			     const struct nfsd4_copy *copy)
5725 {
5726 	const struct nfsd42_write_res *write = &copy->cp_res;
5727 	u32 count = nfsd4_copy_is_sync(copy) ? 0 : 1;
5728 	__be32 status;
5729 
5730 	/* wr_callback_id<1> */
5731 	if (xdr_stream_encode_u32(xdr, count) != XDR_UNIT)
5732 		return nfserr_resource;
5733 	if (count) {
5734 		status = nfsd4_encode_stateid4(xdr, &write->cb_stateid);
5735 		if (status != nfs_ok)
5736 			return status;
5737 	}
5738 
5739 	/* wr_count */
5740 	status = nfsd4_encode_length4(xdr, write->wr_bytes_written);
5741 	if (status != nfs_ok)
5742 		return status;
5743 	/* wr_committed */
5744 	if (xdr_stream_encode_u32(xdr, write->wr_stable_how) != XDR_UNIT)
5745 		return nfserr_resource;
5746 	/* wr_writeverf */
5747 	return nfsd4_encode_verifier4(xdr, &write->wr_verifier);
5748 }
5749 
5750 static __be32 nfsd4_encode_copy_requirements4(struct xdr_stream *xdr,
5751 					      const struct nfsd4_copy *copy)
5752 {
5753 	__be32 status;
5754 
5755 	/* cr_consecutive */
5756 	status = nfsd4_encode_bool(xdr, true);
5757 	if (status != nfs_ok)
5758 		return status;
5759 	/* cr_synchronous */
5760 	return nfsd4_encode_bool(xdr, nfsd4_copy_is_sync(copy));
5761 }
5762 
5763 static __be32
5764 nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
5765 		  union nfsd4_op_u *u)
5766 {
5767 	struct nfsd4_copy *copy = &u->copy;
5768 
5769 	nfserr = nfsd4_encode_write_response4(resp->xdr, copy);
5770 	if (nfserr != nfs_ok)
5771 		return nfserr;
5772 	return nfsd4_encode_copy_requirements4(resp->xdr, copy);
5773 }
5774 
5775 static __be32
5776 nfsd4_encode_netloc4(struct xdr_stream *xdr, const struct nl4_server *ns)
5777 {
5778 	__be32 status;
5779 
5780 	if (xdr_stream_encode_u32(xdr, ns->nl4_type) != XDR_UNIT)
5781 		return nfserr_resource;
5782 	switch (ns->nl4_type) {
5783 	case NL4_NETADDR:
5784 		/* nl_addr */
5785 		status = nfsd4_encode_netaddr4(xdr, &ns->u.nl4_addr);
5786 		break;
5787 	default:
5788 		status = nfserr_serverfault;
5789 	}
5790 	return status;
5791 }
5792 
5793 static __be32
5794 nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
5795 			 union nfsd4_op_u *u)
5796 {
5797 	struct nfsd4_copy_notify *cn = &u->copy_notify;
5798 	struct xdr_stream *xdr = resp->xdr;
5799 
5800 	/* cnr_lease_time */
5801 	nfserr = nfsd4_encode_nfstime4(xdr, &cn->cpn_lease_time);
5802 	if (nfserr)
5803 		return nfserr;
5804 	/* cnr_stateid */
5805 	nfserr = nfsd4_encode_stateid4(xdr, &cn->cpn_cnr_stateid);
5806 	if (nfserr)
5807 		return nfserr;
5808 	/* cnr_source_server<> */
5809 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5810 		return nfserr_resource;
5811 	return nfsd4_encode_netloc4(xdr, cn->cpn_src);
5812 }
5813 
5814 static __be32
5815 nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
5816 			    union nfsd4_op_u *u)
5817 {
5818 	struct nfsd4_offload_status *os = &u->offload_status;
5819 	struct xdr_stream *xdr = resp->xdr;
5820 
5821 	/* osr_count */
5822 	nfserr = nfsd4_encode_length4(xdr, os->count);
5823 	if (nfserr != nfs_ok)
5824 		return nfserr;
5825 	/* osr_complete<1> */
5826 	if (os->completed) {
5827 		if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5828 			return nfserr_resource;
5829 		if (xdr_stream_encode_be32(xdr, os->status) != XDR_UNIT)
5830 			return nfserr_resource;
5831 	} else if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
5832 		return nfserr_resource;
5833 	return nfs_ok;
5834 }
5835 
5836 static __be32
5837 nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
5838 			    struct nfsd4_read *read)
5839 {
5840 	struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
5841 	struct file *file = read->rd_nf->nf_file;
5842 	struct xdr_stream *xdr = resp->xdr;
5843 	bool splice_ok = argp->splice_ok;
5844 	unsigned int offset_offset;
5845 	__be32 nfserr, wire_count;
5846 	unsigned long maxcount;
5847 	__be64 wire_offset;
5848 
5849 	if (xdr_stream_encode_u32(xdr, NFS4_CONTENT_DATA) != XDR_UNIT)
5850 		return nfserr_io;
5851 
5852 	offset_offset = xdr->buf->len;
5853 
5854 	/* Reserve space for the byte offset and count */
5855 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 3)))
5856 		return nfserr_io;
5857 	xdr_commit_encode(xdr);
5858 
5859 	maxcount = min_t(unsigned long, read->rd_length,
5860 			 (xdr->buf->buflen - xdr->buf->len));
5861 
5862 	if (file->f_op->splice_read && splice_ok)
5863 		nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
5864 	else
5865 		nfserr = nfsd4_encode_readv(resp, read, maxcount);
5866 	if (nfserr)
5867 		return nfserr;
5868 
5869 	wire_offset = cpu_to_be64(read->rd_offset);
5870 	write_bytes_to_xdr_buf(xdr->buf, offset_offset, &wire_offset,
5871 			       XDR_UNIT * 2);
5872 	wire_count = cpu_to_be32(read->rd_length);
5873 	write_bytes_to_xdr_buf(xdr->buf, offset_offset + XDR_UNIT * 2,
5874 			       &wire_count, XDR_UNIT);
5875 	return nfs_ok;
5876 }
5877 
5878 static __be32
5879 nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
5880 		       union nfsd4_op_u *u)
5881 {
5882 	struct nfsd4_read *read = &u->read;
5883 	struct file *file = read->rd_nf->nf_file;
5884 	struct xdr_stream *xdr = resp->xdr;
5885 	unsigned int eof_offset;
5886 	__be32 wire_data[2];
5887 	u32 segments = 0;
5888 
5889 	if (nfserr)
5890 		return nfserr;
5891 
5892 	eof_offset = xdr->buf->len;
5893 
5894 	/* Reserve space for the eof flag and segment count */
5895 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2)))
5896 		return nfserr_io;
5897 	xdr_commit_encode(xdr);
5898 
5899 	read->rd_eof = read->rd_offset >= i_size_read(file_inode(file));
5900 	if (read->rd_eof)
5901 		goto out;
5902 
5903 	nfserr = nfsd4_encode_read_plus_data(resp, read);
5904 	if (nfserr) {
5905 		xdr_truncate_encode(xdr, eof_offset);
5906 		return nfserr;
5907 	}
5908 
5909 	segments++;
5910 
5911 out:
5912 	wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
5913 	wire_data[1] = cpu_to_be32(segments);
5914 	write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
5915 	return nfserr;
5916 }
5917 
5918 static __be32
5919 nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
5920 		  union nfsd4_op_u *u)
5921 {
5922 	struct nfsd4_seek *seek = &u->seek;
5923 	struct xdr_stream *xdr = resp->xdr;
5924 
5925 	/* sr_eof */
5926 	nfserr = nfsd4_encode_bool(xdr, seek->seek_eof);
5927 	if (nfserr != nfs_ok)
5928 		return nfserr;
5929 	/* sr_offset */
5930 	return nfsd4_encode_offset4(xdr, seek->seek_pos);
5931 }
5932 
5933 static __be32
5934 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr,
5935 		  union nfsd4_op_u *p)
5936 {
5937 	return nfserr;
5938 }
5939 
5940 /*
5941  * Encode kmalloc-ed buffer in to XDR stream.
5942  */
5943 static __be32
5944 nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
5945 {
5946 	u32 cplen;
5947 	__be32 *p;
5948 
5949 	cplen = min_t(unsigned long, buflen,
5950 		      ((void *)xdr->end - (void *)xdr->p));
5951 	p = xdr_reserve_space(xdr, cplen);
5952 	if (!p)
5953 		return nfserr_resource;
5954 
5955 	memcpy(p, buf, cplen);
5956 	buf += cplen;
5957 	buflen -= cplen;
5958 
5959 	while (buflen) {
5960 		cplen = min_t(u32, buflen, PAGE_SIZE);
5961 		p = xdr_reserve_space(xdr, cplen);
5962 		if (!p)
5963 			return nfserr_resource;
5964 
5965 		memcpy(p, buf, cplen);
5966 
5967 		if (cplen < PAGE_SIZE) {
5968 			/*
5969 			 * We're done, with a length that wasn't page
5970 			 * aligned, so possibly not word aligned. Pad
5971 			 * any trailing bytes with 0.
5972 			 */
5973 			xdr_encode_opaque_fixed(p, NULL, cplen);
5974 			break;
5975 		}
5976 
5977 		buflen -= PAGE_SIZE;
5978 		buf += PAGE_SIZE;
5979 	}
5980 
5981 	return 0;
5982 }
5983 
5984 static __be32
5985 nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5986 		      union nfsd4_op_u *u)
5987 {
5988 	struct nfsd4_getxattr *getxattr = &u->getxattr;
5989 	struct xdr_stream *xdr = resp->xdr;
5990 	__be32 *p, err;
5991 
5992 	p = xdr_reserve_space(xdr, 4);
5993 	if (!p)
5994 		return nfserr_resource;
5995 
5996 	*p = cpu_to_be32(getxattr->getxa_len);
5997 
5998 	if (getxattr->getxa_len == 0)
5999 		return 0;
6000 
6001 	err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
6002 				    getxattr->getxa_len);
6003 
6004 	kvfree(getxattr->getxa_buf);
6005 
6006 	return err;
6007 }
6008 
6009 static __be32
6010 nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
6011 		      union nfsd4_op_u *u)
6012 {
6013 	struct nfsd4_setxattr *setxattr = &u->setxattr;
6014 	struct xdr_stream *xdr = resp->xdr;
6015 
6016 	return nfsd4_encode_change_info4(xdr, &setxattr->setxa_cinfo);
6017 }
6018 
6019 /*
6020  * See if there are cookie values that can be rejected outright.
6021  */
6022 static __be32
6023 nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
6024 				u32 *offsetp)
6025 {
6026 	u64 cookie = listxattrs->lsxa_cookie;
6027 
6028 	/*
6029 	 * If the cookie is larger than the maximum number we can fit
6030 	 * in the buffer we just got back from vfs_listxattr, it's invalid.
6031 	 */
6032 	if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
6033 		return nfserr_badcookie;
6034 
6035 	*offsetp = (u32)cookie;
6036 	return 0;
6037 }
6038 
6039 static __be32
6040 nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
6041 			union nfsd4_op_u *u)
6042 {
6043 	struct nfsd4_listxattrs *listxattrs = &u->listxattrs;
6044 	struct xdr_stream *xdr = resp->xdr;
6045 	u32 cookie_offset, count_offset, eof;
6046 	u32 left, xdrleft, slen, count;
6047 	u32 xdrlen, offset;
6048 	u64 cookie;
6049 	char *sp;
6050 	__be32 status, tmp;
6051 	__be64 wire_cookie;
6052 	__be32 *p;
6053 	u32 nuser;
6054 
6055 	eof = 1;
6056 
6057 	status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
6058 	if (status)
6059 		goto out;
6060 
6061 	/*
6062 	 * Reserve space for the cookie and the name array count. Record
6063 	 * the offsets to save them later.
6064 	 */
6065 	cookie_offset = xdr->buf->len;
6066 	count_offset = cookie_offset + 8;
6067 	p = xdr_reserve_space(xdr, XDR_UNIT * 3);
6068 	if (!p) {
6069 		status = nfserr_resource;
6070 		goto out;
6071 	}
6072 
6073 	count = 0;
6074 	left = listxattrs->lsxa_len;
6075 	sp = listxattrs->lsxa_buf;
6076 	nuser = 0;
6077 
6078 	/* Bytes left is maxcount - 8 (cookie) - 4 (array count) */
6079 	xdrleft = listxattrs->lsxa_maxcount - XDR_UNIT * 3;
6080 
6081 	while (left > 0 && xdrleft > 0) {
6082 		slen = strlen(sp);
6083 
6084 		/*
6085 		 * Check if this is a "user." attribute, skip it if not.
6086 		 */
6087 		if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
6088 			goto contloop;
6089 
6090 		slen -= XATTR_USER_PREFIX_LEN;
6091 		xdrlen = 4 + ((slen + 3) & ~3);
6092 		/* Check if both entry and eof can fit in the XDR buffer */
6093 		if (xdrlen + XDR_UNIT > xdrleft) {
6094 			if (count == 0) {
6095 				/*
6096 				 * Can't even fit the first attribute name.
6097 				 */
6098 				status = nfserr_toosmall;
6099 				goto out;
6100 			}
6101 			eof = 0;
6102 			goto wreof;
6103 		}
6104 
6105 		left -= XATTR_USER_PREFIX_LEN;
6106 		sp += XATTR_USER_PREFIX_LEN;
6107 		if (nuser++ < offset)
6108 			goto contloop;
6109 
6110 
6111 		p = xdr_reserve_space(xdr, xdrlen);
6112 		if (!p) {
6113 			status = nfserr_resource;
6114 			goto out;
6115 		}
6116 
6117 		xdr_encode_opaque(p, sp, slen);
6118 
6119 		xdrleft -= xdrlen;
6120 		count++;
6121 contloop:
6122 		sp += slen + 1;
6123 		left -= slen + 1;
6124 	}
6125 
6126 	/*
6127 	 * If there were user attributes to copy, but we didn't copy
6128 	 * any, the offset was too large (e.g. the cookie was invalid).
6129 	 */
6130 	if (nuser > 0 && count == 0) {
6131 		status = nfserr_badcookie;
6132 		goto out;
6133 	}
6134 
6135 wreof:
6136 	p = xdr_reserve_space(xdr, 4);
6137 	if (!p) {
6138 		status = nfserr_resource;
6139 		goto out;
6140 	}
6141 	*p = cpu_to_be32(eof);
6142 
6143 	cookie = offset + count;
6144 
6145 	wire_cookie = cpu_to_be64(cookie);
6146 	write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &wire_cookie, 8);
6147 	tmp = cpu_to_be32(count);
6148 	write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
6149 out:
6150 	if (listxattrs->lsxa_len)
6151 		kvfree(listxattrs->lsxa_buf);
6152 	return status;
6153 }
6154 
6155 static __be32
6156 nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
6157 			 union nfsd4_op_u *u)
6158 {
6159 	struct nfsd4_removexattr *removexattr = &u->removexattr;
6160 	struct xdr_stream *xdr = resp->xdr;
6161 
6162 	return nfsd4_encode_change_info4(xdr, &removexattr->rmxa_cinfo);
6163 }
6164 
6165 typedef __be32(*nfsd4_enc)(struct nfsd4_compoundres *, __be32, union nfsd4_op_u *u);
6166 
6167 /*
6168  * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
6169  * since we don't need to filter out obsolete ops as this is
6170  * done in the decoding phase.
6171  */
6172 static const nfsd4_enc nfsd4_enc_ops[] = {
6173 	[OP_ACCESS]		= nfsd4_encode_access,
6174 	[OP_CLOSE]		= nfsd4_encode_close,
6175 	[OP_COMMIT]		= nfsd4_encode_commit,
6176 	[OP_CREATE]		= nfsd4_encode_create,
6177 	[OP_DELEGPURGE]		= nfsd4_encode_noop,
6178 	[OP_DELEGRETURN]	= nfsd4_encode_noop,
6179 	[OP_GETATTR]		= nfsd4_encode_getattr,
6180 	[OP_GETFH]		= nfsd4_encode_getfh,
6181 	[OP_LINK]		= nfsd4_encode_link,
6182 	[OP_LOCK]		= nfsd4_encode_lock,
6183 	[OP_LOCKT]		= nfsd4_encode_lockt,
6184 	[OP_LOCKU]		= nfsd4_encode_locku,
6185 	[OP_LOOKUP]		= nfsd4_encode_noop,
6186 	[OP_LOOKUPP]		= nfsd4_encode_noop,
6187 	[OP_NVERIFY]		= nfsd4_encode_noop,
6188 	[OP_OPEN]		= nfsd4_encode_open,
6189 	[OP_OPENATTR]		= nfsd4_encode_noop,
6190 	[OP_OPEN_CONFIRM]	= nfsd4_encode_open_confirm,
6191 	[OP_OPEN_DOWNGRADE]	= nfsd4_encode_open_downgrade,
6192 	[OP_PUTFH]		= nfsd4_encode_noop,
6193 	[OP_PUTPUBFH]		= nfsd4_encode_noop,
6194 	[OP_PUTROOTFH]		= nfsd4_encode_noop,
6195 	[OP_READ]		= nfsd4_encode_read,
6196 	[OP_READDIR]		= nfsd4_encode_readdir,
6197 	[OP_READLINK]		= nfsd4_encode_readlink,
6198 	[OP_REMOVE]		= nfsd4_encode_remove,
6199 	[OP_RENAME]		= nfsd4_encode_rename,
6200 	[OP_RENEW]		= nfsd4_encode_noop,
6201 	[OP_RESTOREFH]		= nfsd4_encode_noop,
6202 	[OP_SAVEFH]		= nfsd4_encode_noop,
6203 	[OP_SECINFO]		= nfsd4_encode_secinfo,
6204 	[OP_SETATTR]		= nfsd4_encode_setattr,
6205 	[OP_SETCLIENTID]	= nfsd4_encode_setclientid,
6206 	[OP_SETCLIENTID_CONFIRM] = nfsd4_encode_noop,
6207 	[OP_VERIFY]		= nfsd4_encode_noop,
6208 	[OP_WRITE]		= nfsd4_encode_write,
6209 	[OP_RELEASE_LOCKOWNER]	= nfsd4_encode_noop,
6210 
6211 	/* NFSv4.1 operations */
6212 	[OP_BACKCHANNEL_CTL]	= nfsd4_encode_noop,
6213 	[OP_BIND_CONN_TO_SESSION] = nfsd4_encode_bind_conn_to_session,
6214 	[OP_EXCHANGE_ID]	= nfsd4_encode_exchange_id,
6215 	[OP_CREATE_SESSION]	= nfsd4_encode_create_session,
6216 	[OP_DESTROY_SESSION]	= nfsd4_encode_noop,
6217 	[OP_FREE_STATEID]	= nfsd4_encode_noop,
6218 	[OP_GET_DIR_DELEGATION]	= nfsd4_encode_get_dir_delegation,
6219 #ifdef CONFIG_NFSD_PNFS
6220 	[OP_GETDEVICEINFO]	= nfsd4_encode_getdeviceinfo,
6221 	[OP_GETDEVICELIST]	= nfsd4_encode_noop,
6222 	[OP_LAYOUTCOMMIT]	= nfsd4_encode_layoutcommit,
6223 	[OP_LAYOUTGET]		= nfsd4_encode_layoutget,
6224 	[OP_LAYOUTRETURN]	= nfsd4_encode_layoutreturn,
6225 #else
6226 	[OP_GETDEVICEINFO]	= nfsd4_encode_noop,
6227 	[OP_GETDEVICELIST]	= nfsd4_encode_noop,
6228 	[OP_LAYOUTCOMMIT]	= nfsd4_encode_noop,
6229 	[OP_LAYOUTGET]		= nfsd4_encode_noop,
6230 	[OP_LAYOUTRETURN]	= nfsd4_encode_noop,
6231 #endif
6232 	[OP_SECINFO_NO_NAME]	= nfsd4_encode_secinfo_no_name,
6233 	[OP_SEQUENCE]		= nfsd4_encode_sequence,
6234 	[OP_SET_SSV]		= nfsd4_encode_noop,
6235 	[OP_TEST_STATEID]	= nfsd4_encode_test_stateid,
6236 	[OP_WANT_DELEGATION]	= nfsd4_encode_noop,
6237 	[OP_DESTROY_CLIENTID]	= nfsd4_encode_noop,
6238 	[OP_RECLAIM_COMPLETE]	= nfsd4_encode_noop,
6239 
6240 	/* NFSv4.2 operations */
6241 	[OP_ALLOCATE]		= nfsd4_encode_noop,
6242 	[OP_COPY]		= nfsd4_encode_copy,
6243 	[OP_COPY_NOTIFY]	= nfsd4_encode_copy_notify,
6244 	[OP_DEALLOCATE]		= nfsd4_encode_noop,
6245 	[OP_IO_ADVISE]		= nfsd4_encode_noop,
6246 	[OP_LAYOUTERROR]	= nfsd4_encode_noop,
6247 	[OP_LAYOUTSTATS]	= nfsd4_encode_noop,
6248 	[OP_OFFLOAD_CANCEL]	= nfsd4_encode_noop,
6249 	[OP_OFFLOAD_STATUS]	= nfsd4_encode_offload_status,
6250 	[OP_READ_PLUS]		= nfsd4_encode_read_plus,
6251 	[OP_SEEK]		= nfsd4_encode_seek,
6252 	[OP_WRITE_SAME]		= nfsd4_encode_noop,
6253 	[OP_CLONE]		= nfsd4_encode_noop,
6254 
6255 	/* RFC 8276 extended atributes operations */
6256 	[OP_GETXATTR]		= nfsd4_encode_getxattr,
6257 	[OP_SETXATTR]		= nfsd4_encode_setxattr,
6258 	[OP_LISTXATTRS]		= nfsd4_encode_listxattrs,
6259 	[OP_REMOVEXATTR]	= nfsd4_encode_removexattr,
6260 };
6261 
6262 /*
6263  * Calculate whether we still have space to encode repsize bytes.
6264  * There are two considerations:
6265  *     - For NFS versions >=4.1, the size of the reply must stay within
6266  *       session limits
6267  *     - For all NFS versions, we must stay within limited preallocated
6268  *       buffer space.
6269  *
6270  * This is called before the operation is processed, so can only provide
6271  * an upper estimate.  For some nonidempotent operations (such as
6272  * getattr), it's not necessarily a problem if that estimate is wrong,
6273  * as we can fail it after processing without significant side effects.
6274  */
6275 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
6276 {
6277 	struct xdr_buf *buf = &resp->rqstp->rq_res;
6278 	struct nfsd4_slot *slot = resp->cstate.slot;
6279 
6280 	if (buf->len + respsize <= buf->buflen)
6281 		return nfs_ok;
6282 	if (!nfsd4_has_session(&resp->cstate))
6283 		return nfserr_resource;
6284 	if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
6285 		WARN_ON_ONCE(1);
6286 		return nfserr_rep_too_big_to_cache;
6287 	}
6288 	return nfserr_rep_too_big;
6289 }
6290 
6291 static __be32 nfsd4_map_status(__be32 status, u32 minor)
6292 {
6293 	switch (status) {
6294 	case nfs_ok:
6295 		break;
6296 	case nfserr_wrong_type:
6297 		/* RFC 8881 - 15.1.2.9 */
6298 		if (minor == 0)
6299 			status = nfserr_inval;
6300 		break;
6301 	case nfserr_symlink_not_dir:
6302 		status = nfserr_symlink;
6303 		break;
6304 	}
6305 	return status;
6306 }
6307 
6308 void
6309 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
6310 {
6311 	struct xdr_stream *xdr = resp->xdr;
6312 	struct nfs4_stateowner *so = resp->cstate.replay_owner;
6313 	struct svc_rqst *rqstp = resp->rqstp;
6314 	const struct nfsd4_operation *opdesc = op->opdesc;
6315 	unsigned int op_status_offset;
6316 	nfsd4_enc encoder;
6317 
6318 	if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
6319 		goto release;
6320 	op_status_offset = xdr->buf->len;
6321 	if (!xdr_reserve_space(xdr, XDR_UNIT))
6322 		goto release;
6323 
6324 	if (op->opnum == OP_ILLEGAL)
6325 		goto status;
6326 	if (op->status && opdesc &&
6327 			!(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
6328 		goto status;
6329 	BUG_ON(op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
6330 	       !nfsd4_enc_ops[op->opnum]);
6331 	encoder = nfsd4_enc_ops[op->opnum];
6332 	op->status = encoder(resp, op->status, &op->u);
6333 	if (op->status)
6334 		trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
6335 	xdr_commit_encode(xdr);
6336 
6337 	/* nfsd4_check_resp_size guarantees enough room for error status */
6338 	if (!op->status) {
6339 		int space_needed = 0;
6340 		if (!nfsd4_last_compound_op(rqstp))
6341 			space_needed = COMPOUND_ERR_SLACK_SPACE;
6342 		op->status = nfsd4_check_resp_size(resp, space_needed);
6343 	}
6344 	if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
6345 		struct nfsd4_slot *slot = resp->cstate.slot;
6346 
6347 		if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
6348 			op->status = nfserr_rep_too_big_to_cache;
6349 		else
6350 			op->status = nfserr_rep_too_big;
6351 	}
6352 	if (op->status == nfserr_resource ||
6353 	    op->status == nfserr_rep_too_big ||
6354 	    op->status == nfserr_rep_too_big_to_cache) {
6355 		/*
6356 		 * The operation may have already been encoded or
6357 		 * partially encoded.  No op returns anything additional
6358 		 * in the case of one of these three errors, so we can
6359 		 * just truncate back to after the status.  But it's a
6360 		 * bug if we had to do this on a non-idempotent op:
6361 		 */
6362 		warn_on_nonidempotent_op(op);
6363 		xdr_truncate_encode(xdr, op_status_offset + XDR_UNIT);
6364 	} else if (so) {
6365 		int len = xdr->buf->len - (op_status_offset + XDR_UNIT);
6366 
6367 		so->so_replay.rp_status = op->status;
6368 		if (len > NFSD4_REPLAY_ISIZE) {
6369 			char *buf = kmalloc(len, GFP_KERNEL);
6370 
6371 			nfs4_replay_free_cache(&so->so_replay);
6372 			if (buf) {
6373 				so->so_replay.rp_buf = buf;
6374 			} else {
6375 				/* rp_buflen already zeroed; skip caching */
6376 				goto status;
6377 			}
6378 		} else if (so->so_replay.rp_buf != so->so_replay.rp_ibuf) {
6379 			nfs4_replay_free_cache(&so->so_replay);
6380 		}
6381 		so->so_replay.rp_buflen = len;
6382 		read_bytes_from_xdr_buf(xdr->buf,
6383 					op_status_offset + XDR_UNIT,
6384 					so->so_replay.rp_buf, len);
6385 	}
6386 status:
6387 	op->status = nfsd4_map_status(op->status,
6388 				      resp->cstate.minorversion);
6389 	write_bytes_to_xdr_buf(xdr->buf, op_status_offset,
6390 			       &op->status, XDR_UNIT);
6391 release:
6392 	if (opdesc && opdesc->op_release)
6393 		opdesc->op_release(&op->u);
6394 
6395 	/*
6396 	 * Account for pages consumed while encoding this operation.
6397 	 * The xdr_stream primitives don't manage rq_next_page.
6398 	 */
6399 	rqstp->rq_next_page = xdr->page_ptr + 1;
6400 }
6401 
6402 /**
6403  * nfsd4_encode_replay - encode a result stored in the stateowner reply cache
6404  * @xdr: send buffer's XDR stream
6405  * @op: operation being replayed
6406  *
6407  * @op->replay->rp_buf contains the previously-sent already-encoded result.
6408  */
6409 void nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
6410 {
6411 	struct nfs4_replay *rp = op->replay;
6412 
6413 	trace_nfsd_stateowner_replay(op->opnum, rp);
6414 
6415 	if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
6416 		return;
6417 	if (xdr_stream_encode_be32(xdr, rp->rp_status) != XDR_UNIT)
6418 		return;
6419 	xdr_stream_encode_opaque_fixed(xdr, rp->rp_buf, rp->rp_buflen);
6420 }
6421 
6422 void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
6423 {
6424 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
6425 
6426 	if (args->ops != args->iops) {
6427 		vfree(args->ops);
6428 		args->ops = args->iops;
6429 	}
6430 	while (args->to_free) {
6431 		struct svcxdr_tmpbuf *tb = args->to_free;
6432 		args->to_free = tb->next;
6433 		kfree(tb);
6434 	}
6435 }
6436 
6437 bool
6438 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
6439 {
6440 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
6441 
6442 	/* svcxdr_tmp_alloc */
6443 	args->to_free = NULL;
6444 
6445 	args->xdr = xdr;
6446 	args->ops = args->iops;
6447 	args->rqstp = rqstp;
6448 
6449 	/*
6450 	 * NFSv4 operation decoders can invoke svc cache lookups
6451 	 * that trigger svc_defer() when RQ_USEDEFERRAL is set,
6452 	 * setting RQ_DROPME. This creates two problems:
6453 	 *
6454 	 * 1. Non-idempotency: Compounds make it too hard to avoid
6455 	 *    problems if a request is deferred and replayed.
6456 	 *
6457 	 * 2. Session slot leakage (NFSv4.1+): If RQ_DROPME is set
6458 	 *    during decode but SEQUENCE executes successfully, the
6459 	 *    session slot will be marked INUSE. The request is then
6460 	 *    dropped before encoding, so the slot is never released,
6461 	 *    rendering it permanently unusable by the client.
6462 	 */
6463 	clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
6464 
6465 	return nfsd4_decode_compound(args);
6466 }
6467 
6468 bool
6469 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
6470 {
6471 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
6472 	__be32 *p;
6473 
6474 	/*
6475 	 * Send buffer space for the following items is reserved
6476 	 * at the top of nfsd4_proc_compound().
6477 	 */
6478 	p = resp->statusp;
6479 
6480 	*p++ = resp->cstate.status;
6481 	*p++ = htonl(resp->taglen);
6482 	memcpy(p, resp->tag, resp->taglen);
6483 	p += XDR_QUADLEN(resp->taglen);
6484 	*p++ = htonl(resp->opcnt);
6485 
6486 	nfsd4_sequence_done(resp);
6487 	return true;
6488 }
6489