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