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