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