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