xref: /linux/fs/nfsd/nfs4xdr.c (revision 8eefed8f65cc17c31fdf4ab32292b794b34893ad)
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 	supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3379 	supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3380 	supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3381 
3382 	return nfsd4_encode_bitmap4(xdr, supp[0], supp[1], supp[2]);
3383 }
3384 
3385 /*
3386  * Copied from generic_remap_checks/generic_remap_file_range_prep.
3387  *
3388  * These generic functions use the file system's s_blocksize, but
3389  * individual file systems aren't required to use
3390  * generic_remap_file_range_prep. Until there is a mechanism for
3391  * determining a particular file system's (or file's) clone block
3392  * size, this is the best NFSD can do.
3393  */
nfsd4_encode_fattr4_clone_blksize(struct xdr_stream * xdr,const struct nfsd4_fattr_args * args)3394 static __be32 nfsd4_encode_fattr4_clone_blksize(struct xdr_stream *xdr,
3395 						const struct nfsd4_fattr_args *args)
3396 {
3397 	struct inode *inode = d_inode(args->dentry);
3398 
3399 	return nfsd4_encode_uint32_t(xdr, inode->i_sb->s_blocksize);
3400 }
3401 
3402 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
nfsd4_encode_fattr4_sec_label(struct xdr_stream * xdr,const struct nfsd4_fattr_args * args)3403 static __be32 nfsd4_encode_fattr4_sec_label(struct xdr_stream *xdr,
3404 					    const struct nfsd4_fattr_args *args)
3405 {
3406 	return nfsd4_encode_security_label(xdr, args->rqstp, &args->context);
3407 }
3408 #endif
3409 
nfsd4_encode_fattr4_xattr_support(struct xdr_stream * xdr,const struct nfsd4_fattr_args * args)3410 static __be32 nfsd4_encode_fattr4_xattr_support(struct xdr_stream *xdr,
3411 						const struct nfsd4_fattr_args *args)
3412 {
3413 	int err = xattr_supports_user_prefix(d_inode(args->dentry));
3414 
3415 	return nfsd4_encode_bool(xdr, err == 0);
3416 }
3417 
3418 #define NFSD_OA_SHARE_ACCESS	(BIT(OPEN_ARGS_SHARE_ACCESS_READ)	| \
3419 				 BIT(OPEN_ARGS_SHARE_ACCESS_WRITE)	| \
3420 				 BIT(OPEN_ARGS_SHARE_ACCESS_BOTH))
3421 
3422 #define NFSD_OA_SHARE_DENY	(BIT(OPEN_ARGS_SHARE_DENY_NONE)		| \
3423 				 BIT(OPEN_ARGS_SHARE_DENY_READ)		| \
3424 				 BIT(OPEN_ARGS_SHARE_DENY_WRITE)	| \
3425 				 BIT(OPEN_ARGS_SHARE_DENY_BOTH))
3426 
3427 #define NFSD_OA_SHARE_ACCESS_WANT	(BIT(OPEN_ARGS_SHARE_ACCESS_WANT_ANY_DELEG)		| \
3428 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_NO_DELEG)		| \
3429 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_CANCEL)		| \
3430 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS)	| \
3431 					 BIT(OPEN_ARGS_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION))
3432 
3433 #define NFSD_OA_OPEN_CLAIM	(BIT(OPEN_ARGS_OPEN_CLAIM_NULL)		| \
3434 				 BIT(OPEN_ARGS_OPEN_CLAIM_PREVIOUS)	| \
3435 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEGATE_CUR)	| \
3436 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEGATE_PREV)| \
3437 				 BIT(OPEN_ARGS_OPEN_CLAIM_FH)		| \
3438 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEG_CUR_FH)	| \
3439 				 BIT(OPEN_ARGS_OPEN_CLAIM_DELEG_PREV_FH))
3440 
3441 #define NFSD_OA_CREATE_MODE	(BIT(OPEN_ARGS_CREATEMODE_UNCHECKED4)	| \
3442 				 BIT(OPEN_ARGS_CREATE_MODE_GUARDED)	| \
3443 				 BIT(OPEN_ARGS_CREATEMODE_EXCLUSIVE4)	| \
3444 				 BIT(OPEN_ARGS_CREATE_MODE_EXCLUSIVE4_1))
3445 
3446 static uint32_t oa_share_access = NFSD_OA_SHARE_ACCESS;
3447 static uint32_t oa_share_deny = NFSD_OA_SHARE_DENY;
3448 static uint32_t oa_share_access_want = NFSD_OA_SHARE_ACCESS_WANT;
3449 static uint32_t oa_open_claim = NFSD_OA_OPEN_CLAIM;
3450 static uint32_t oa_create_mode = NFSD_OA_CREATE_MODE;
3451 
3452 static const struct open_arguments4 nfsd_open_arguments = {
3453 	.oa_share_access = { .count = 1, .element = &oa_share_access },
3454 	.oa_share_deny = { .count = 1, .element = &oa_share_deny },
3455 	.oa_share_access_want = { .count = 1, .element = &oa_share_access_want },
3456 	.oa_open_claim = { .count = 1, .element = &oa_open_claim },
3457 	.oa_create_mode = { .count = 1, .element = &oa_create_mode },
3458 };
3459 
nfsd4_encode_fattr4_open_arguments(struct xdr_stream * xdr,const struct nfsd4_fattr_args * args)3460 static __be32 nfsd4_encode_fattr4_open_arguments(struct xdr_stream *xdr,
3461 						 const struct nfsd4_fattr_args *args)
3462 {
3463 	if (!xdrgen_encode_fattr4_open_arguments(xdr, &nfsd_open_arguments))
3464 		return nfserr_resource;
3465 	return nfs_ok;
3466 }
3467 
3468 static const nfsd4_enc_attr nfsd4_enc_fattr4_encode_ops[] = {
3469 	[FATTR4_SUPPORTED_ATTRS]	= nfsd4_encode_fattr4_supported_attrs,
3470 	[FATTR4_TYPE]			= nfsd4_encode_fattr4_type,
3471 	[FATTR4_FH_EXPIRE_TYPE]		= nfsd4_encode_fattr4_fh_expire_type,
3472 	[FATTR4_CHANGE]			= nfsd4_encode_fattr4_change,
3473 	[FATTR4_SIZE]			= nfsd4_encode_fattr4_size,
3474 	[FATTR4_LINK_SUPPORT]		= nfsd4_encode_fattr4__true,
3475 	[FATTR4_SYMLINK_SUPPORT]	= nfsd4_encode_fattr4__true,
3476 	[FATTR4_NAMED_ATTR]		= nfsd4_encode_fattr4__false,
3477 	[FATTR4_FSID]			= nfsd4_encode_fattr4_fsid,
3478 	[FATTR4_UNIQUE_HANDLES]		= nfsd4_encode_fattr4__true,
3479 	[FATTR4_LEASE_TIME]		= nfsd4_encode_fattr4_lease_time,
3480 	[FATTR4_RDATTR_ERROR]		= nfsd4_encode_fattr4_rdattr_error,
3481 	[FATTR4_ACL]			= nfsd4_encode_fattr4_acl,
3482 	[FATTR4_ACLSUPPORT]		= nfsd4_encode_fattr4_aclsupport,
3483 	[FATTR4_ARCHIVE]		= nfsd4_encode_fattr4__noop,
3484 	[FATTR4_CANSETTIME]		= nfsd4_encode_fattr4__true,
3485 	[FATTR4_CASE_INSENSITIVE]	= nfsd4_encode_fattr4__false,
3486 	[FATTR4_CASE_PRESERVING]	= nfsd4_encode_fattr4__true,
3487 	[FATTR4_CHOWN_RESTRICTED]	= nfsd4_encode_fattr4__true,
3488 	[FATTR4_FILEHANDLE]		= nfsd4_encode_fattr4_filehandle,
3489 	[FATTR4_FILEID]			= nfsd4_encode_fattr4_fileid,
3490 	[FATTR4_FILES_AVAIL]		= nfsd4_encode_fattr4_files_avail,
3491 	[FATTR4_FILES_FREE]		= nfsd4_encode_fattr4_files_free,
3492 	[FATTR4_FILES_TOTAL]		= nfsd4_encode_fattr4_files_total,
3493 	[FATTR4_FS_LOCATIONS]		= nfsd4_encode_fattr4_fs_locations,
3494 	[FATTR4_HIDDEN]			= nfsd4_encode_fattr4__noop,
3495 	[FATTR4_HOMOGENEOUS]		= nfsd4_encode_fattr4__true,
3496 	[FATTR4_MAXFILESIZE]		= nfsd4_encode_fattr4_maxfilesize,
3497 	[FATTR4_MAXLINK]		= nfsd4_encode_fattr4_maxlink,
3498 	[FATTR4_MAXNAME]		= nfsd4_encode_fattr4_maxname,
3499 	[FATTR4_MAXREAD]		= nfsd4_encode_fattr4_maxread,
3500 	[FATTR4_MAXWRITE]		= nfsd4_encode_fattr4_maxwrite,
3501 	[FATTR4_MIMETYPE]		= nfsd4_encode_fattr4__noop,
3502 	[FATTR4_MODE]			= nfsd4_encode_fattr4_mode,
3503 	[FATTR4_NO_TRUNC]		= nfsd4_encode_fattr4__true,
3504 	[FATTR4_NUMLINKS]		= nfsd4_encode_fattr4_numlinks,
3505 	[FATTR4_OWNER]			= nfsd4_encode_fattr4_owner,
3506 	[FATTR4_OWNER_GROUP]		= nfsd4_encode_fattr4_owner_group,
3507 	[FATTR4_QUOTA_AVAIL_HARD]	= nfsd4_encode_fattr4__noop,
3508 	[FATTR4_QUOTA_AVAIL_SOFT]	= nfsd4_encode_fattr4__noop,
3509 	[FATTR4_QUOTA_USED]		= nfsd4_encode_fattr4__noop,
3510 	[FATTR4_RAWDEV]			= nfsd4_encode_fattr4_rawdev,
3511 	[FATTR4_SPACE_AVAIL]		= nfsd4_encode_fattr4_space_avail,
3512 	[FATTR4_SPACE_FREE]		= nfsd4_encode_fattr4_space_free,
3513 	[FATTR4_SPACE_TOTAL]		= nfsd4_encode_fattr4_space_total,
3514 	[FATTR4_SPACE_USED]		= nfsd4_encode_fattr4_space_used,
3515 	[FATTR4_SYSTEM]			= nfsd4_encode_fattr4__noop,
3516 	[FATTR4_TIME_ACCESS]		= nfsd4_encode_fattr4_time_access,
3517 	[FATTR4_TIME_ACCESS_SET]	= nfsd4_encode_fattr4__noop,
3518 	[FATTR4_TIME_BACKUP]		= nfsd4_encode_fattr4__noop,
3519 	[FATTR4_TIME_CREATE]		= nfsd4_encode_fattr4_time_create,
3520 	[FATTR4_TIME_DELTA]		= nfsd4_encode_fattr4_time_delta,
3521 	[FATTR4_TIME_METADATA]		= nfsd4_encode_fattr4_time_metadata,
3522 	[FATTR4_TIME_MODIFY]		= nfsd4_encode_fattr4_time_modify,
3523 	[FATTR4_TIME_MODIFY_SET]	= nfsd4_encode_fattr4__noop,
3524 	[FATTR4_MOUNTED_ON_FILEID]	= nfsd4_encode_fattr4_mounted_on_fileid,
3525 	[FATTR4_DIR_NOTIF_DELAY]	= nfsd4_encode_fattr4__noop,
3526 	[FATTR4_DIRENT_NOTIF_DELAY]	= nfsd4_encode_fattr4__noop,
3527 	[FATTR4_DACL]			= nfsd4_encode_fattr4__noop,
3528 	[FATTR4_SACL]			= nfsd4_encode_fattr4__noop,
3529 	[FATTR4_CHANGE_POLICY]		= nfsd4_encode_fattr4__noop,
3530 	[FATTR4_FS_STATUS]		= nfsd4_encode_fattr4__noop,
3531 
3532 #ifdef CONFIG_NFSD_PNFS
3533 	[FATTR4_FS_LAYOUT_TYPES]	= nfsd4_encode_fattr4_fs_layout_types,
3534 	[FATTR4_LAYOUT_HINT]		= nfsd4_encode_fattr4__noop,
3535 	[FATTR4_LAYOUT_TYPES]		= nfsd4_encode_fattr4_layout_types,
3536 	[FATTR4_LAYOUT_BLKSIZE]		= nfsd4_encode_fattr4_layout_blksize,
3537 	[FATTR4_LAYOUT_ALIGNMENT]	= nfsd4_encode_fattr4__noop,
3538 #else
3539 	[FATTR4_FS_LAYOUT_TYPES]	= nfsd4_encode_fattr4__noop,
3540 	[FATTR4_LAYOUT_HINT]		= nfsd4_encode_fattr4__noop,
3541 	[FATTR4_LAYOUT_TYPES]		= nfsd4_encode_fattr4__noop,
3542 	[FATTR4_LAYOUT_BLKSIZE]		= nfsd4_encode_fattr4__noop,
3543 	[FATTR4_LAYOUT_ALIGNMENT]	= nfsd4_encode_fattr4__noop,
3544 #endif
3545 
3546 	[FATTR4_FS_LOCATIONS_INFO]	= nfsd4_encode_fattr4__noop,
3547 	[FATTR4_MDSTHRESHOLD]		= nfsd4_encode_fattr4__noop,
3548 	[FATTR4_RETENTION_GET]		= nfsd4_encode_fattr4__noop,
3549 	[FATTR4_RETENTION_SET]		= nfsd4_encode_fattr4__noop,
3550 	[FATTR4_RETENTEVT_GET]		= nfsd4_encode_fattr4__noop,
3551 	[FATTR4_RETENTEVT_SET]		= nfsd4_encode_fattr4__noop,
3552 	[FATTR4_RETENTION_HOLD]		= nfsd4_encode_fattr4__noop,
3553 	[FATTR4_MODE_SET_MASKED]	= nfsd4_encode_fattr4__noop,
3554 	[FATTR4_SUPPATTR_EXCLCREAT]	= nfsd4_encode_fattr4_suppattr_exclcreat,
3555 	[FATTR4_FS_CHARSET_CAP]		= nfsd4_encode_fattr4__noop,
3556 	[FATTR4_CLONE_BLKSIZE]		= nfsd4_encode_fattr4_clone_blksize,
3557 	[FATTR4_SPACE_FREED]		= nfsd4_encode_fattr4__noop,
3558 	[FATTR4_CHANGE_ATTR_TYPE]	= nfsd4_encode_fattr4__noop,
3559 
3560 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3561 	[FATTR4_SEC_LABEL]		= nfsd4_encode_fattr4_sec_label,
3562 #else
3563 	[FATTR4_SEC_LABEL]		= nfsd4_encode_fattr4__noop,
3564 #endif
3565 
3566 	[FATTR4_MODE_UMASK]		= nfsd4_encode_fattr4__noop,
3567 	[FATTR4_XATTR_SUPPORT]		= nfsd4_encode_fattr4_xattr_support,
3568 	[FATTR4_TIME_DELEG_ACCESS]	= nfsd4_encode_fattr4__inval,
3569 	[FATTR4_TIME_DELEG_MODIFY]	= nfsd4_encode_fattr4__inval,
3570 	[FATTR4_OPEN_ARGUMENTS]		= nfsd4_encode_fattr4_open_arguments,
3571 };
3572 
3573 /*
3574  * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
3575  * ourselves.
3576  */
3577 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)3578 nfsd4_encode_fattr4(struct svc_rqst *rqstp, struct xdr_stream *xdr,
3579 		    struct svc_fh *fhp, struct svc_export *exp,
3580 		    struct dentry *dentry, const u32 *bmval,
3581 		    int ignore_crossmnt)
3582 {
3583 	DECLARE_BITMAP(attr_bitmap, ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops));
3584 	struct nfs4_delegation *dp = NULL;
3585 	struct nfsd4_fattr_args args;
3586 	struct svc_fh *tempfh = NULL;
3587 	int starting_len = xdr->buf->len;
3588 	unsigned int attrlen_offset;
3589 	__be32 attrlen, status;
3590 	u32 attrmask[3];
3591 	int err;
3592 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3593 	u32 minorversion = resp->cstate.minorversion;
3594 	struct path path = {
3595 		.mnt	= exp->ex_path.mnt,
3596 		.dentry	= dentry,
3597 	};
3598 	unsigned long bit;
3599 
3600 	WARN_ON_ONCE(bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1);
3601 	WARN_ON_ONCE(!nfsd_attrs_supported(minorversion, bmval));
3602 
3603 	args.rqstp = rqstp;
3604 	args.exp = exp;
3605 	args.dentry = dentry;
3606 	args.ignore_crossmnt = (ignore_crossmnt != 0);
3607 	args.acl = NULL;
3608 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3609 	args.context.context = NULL;
3610 #endif
3611 
3612 	/*
3613 	 * Make a local copy of the attribute bitmap that can be modified.
3614 	 */
3615 	attrmask[0] = bmval[0];
3616 	attrmask[1] = bmval[1];
3617 	attrmask[2] = bmval[2];
3618 
3619 	args.rdattr_err = 0;
3620 	if (exp->ex_fslocs.migrated) {
3621 		status = fattr_handle_absent_fs(&attrmask[0], &attrmask[1],
3622 						&attrmask[2], &args.rdattr_err);
3623 		if (status)
3624 			goto out;
3625 	}
3626 	if ((attrmask[0] & (FATTR4_WORD0_CHANGE |
3627 			    FATTR4_WORD0_SIZE)) ||
3628 	    (attrmask[1] & (FATTR4_WORD1_TIME_ACCESS |
3629 			    FATTR4_WORD1_TIME_MODIFY |
3630 			    FATTR4_WORD1_TIME_METADATA))) {
3631 		status = nfsd4_deleg_getattr_conflict(rqstp, dentry, &dp);
3632 		if (status)
3633 			goto out;
3634 	}
3635 
3636 	err = vfs_getattr(&path, &args.stat,
3637 			  STATX_BASIC_STATS | STATX_BTIME | STATX_CHANGE_COOKIE,
3638 			  AT_STATX_SYNC_AS_STAT);
3639 	if (dp) {
3640 		struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
3641 
3642 		if (ncf->ncf_file_modified) {
3643 			++ncf->ncf_initial_cinfo;
3644 			args.stat.size = ncf->ncf_cur_fsize;
3645 			if (!timespec64_is_epoch(&ncf->ncf_cb_mtime))
3646 				args.stat.mtime = ncf->ncf_cb_mtime;
3647 		}
3648 		args.change_attr = ncf->ncf_initial_cinfo;
3649 
3650 		if (!timespec64_is_epoch(&ncf->ncf_cb_atime))
3651 			args.stat.atime = ncf->ncf_cb_atime;
3652 
3653 		nfs4_put_stid(&dp->dl_stid);
3654 	} else {
3655 		args.change_attr = nfsd4_change_attribute(&args.stat);
3656 	}
3657 
3658 	if (err)
3659 		goto out_nfserr;
3660 
3661 	if (!(args.stat.result_mask & STATX_BTIME))
3662 		/* underlying FS does not offer btime so we can't share it */
3663 		attrmask[1] &= ~FATTR4_WORD1_TIME_CREATE;
3664 	if ((attrmask[0] & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
3665 			FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
3666 	    (attrmask[1] & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
3667 		       FATTR4_WORD1_SPACE_TOTAL))) {
3668 		err = vfs_statfs(&path, &args.statfs);
3669 		if (err)
3670 			goto out_nfserr;
3671 	}
3672 	if ((attrmask[0] & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) &&
3673 	    !fhp) {
3674 		tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
3675 		status = nfserr_jukebox;
3676 		if (!tempfh)
3677 			goto out;
3678 		fh_init(tempfh, NFS4_FHSIZE);
3679 		status = fh_compose(tempfh, exp, dentry, NULL);
3680 		if (status)
3681 			goto out;
3682 		args.fhp = tempfh;
3683 	} else
3684 		args.fhp = fhp;
3685 
3686 	if (attrmask[0] & FATTR4_WORD0_ACL) {
3687 		err = nfsd4_get_nfs4_acl(rqstp, dentry, &args.acl);
3688 		if (err == -EOPNOTSUPP)
3689 			attrmask[0] &= ~FATTR4_WORD0_ACL;
3690 		else if (err == -EINVAL) {
3691 			status = nfserr_attrnotsupp;
3692 			goto out;
3693 		} else if (err != 0)
3694 			goto out_nfserr;
3695 	}
3696 
3697 	args.contextsupport = false;
3698 
3699 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3700 	if ((attrmask[2] & FATTR4_WORD2_SECURITY_LABEL) ||
3701 	     attrmask[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
3702 		if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
3703 			err = security_inode_getsecctx(d_inode(dentry),
3704 						&args.context);
3705 		else
3706 			err = -EOPNOTSUPP;
3707 		args.contextsupport = (err == 0);
3708 		if (attrmask[2] & FATTR4_WORD2_SECURITY_LABEL) {
3709 			if (err == -EOPNOTSUPP)
3710 				attrmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3711 			else if (err)
3712 				goto out_nfserr;
3713 		}
3714 	}
3715 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
3716 
3717 	/* attrmask */
3718 	status = nfsd4_encode_bitmap4(xdr, attrmask[0], attrmask[1],
3719 				      attrmask[2]);
3720 	if (status)
3721 		goto out;
3722 
3723 	/* attr_vals */
3724 	attrlen_offset = xdr->buf->len;
3725 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
3726 		goto out_resource;
3727 	bitmap_from_arr32(attr_bitmap, attrmask,
3728 			  ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops));
3729 	for_each_set_bit(bit, attr_bitmap,
3730 			 ARRAY_SIZE(nfsd4_enc_fattr4_encode_ops)) {
3731 		status = nfsd4_enc_fattr4_encode_ops[bit](xdr, &args);
3732 		if (status != nfs_ok)
3733 			goto out;
3734 	}
3735 	attrlen = cpu_to_be32(xdr->buf->len - attrlen_offset - XDR_UNIT);
3736 	write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, XDR_UNIT);
3737 	status = nfs_ok;
3738 
3739 out:
3740 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3741 	if (args.context.context)
3742 		security_release_secctx(&args.context);
3743 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
3744 	kfree(args.acl);
3745 	if (tempfh) {
3746 		fh_put(tempfh);
3747 		kfree(tempfh);
3748 	}
3749 	if (status)
3750 		xdr_truncate_encode(xdr, starting_len);
3751 	return status;
3752 out_nfserr:
3753 	status = nfserrno(err);
3754 	goto out;
3755 out_resource:
3756 	status = nfserr_resource;
3757 	goto out;
3758 }
3759 
svcxdr_init_encode_from_buffer(struct xdr_stream * xdr,struct xdr_buf * buf,__be32 * p,int bytes)3760 static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3761 				struct xdr_buf *buf, __be32 *p, int bytes)
3762 {
3763 	xdr->scratch.iov_len = 0;
3764 	memset(buf, 0, sizeof(struct xdr_buf));
3765 	buf->head[0].iov_base = p;
3766 	buf->head[0].iov_len = 0;
3767 	buf->len = 0;
3768 	xdr->buf = buf;
3769 	xdr->iov = buf->head;
3770 	xdr->p = p;
3771 	xdr->end = (void *)p + bytes;
3772 	buf->buflen = bytes;
3773 }
3774 
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)3775 __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3776 			struct svc_fh *fhp, struct svc_export *exp,
3777 			struct dentry *dentry, u32 *bmval,
3778 			struct svc_rqst *rqstp, int ignore_crossmnt)
3779 {
3780 	struct xdr_buf dummy;
3781 	struct xdr_stream xdr;
3782 	__be32 ret;
3783 
3784 	svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
3785 	ret = nfsd4_encode_fattr4(rqstp, &xdr, fhp, exp, dentry, bmval,
3786 				  ignore_crossmnt);
3787 	*p = xdr.p;
3788 	return ret;
3789 }
3790 
3791 /*
3792  * The buffer space for this field was reserved during a previous
3793  * call to nfsd4_encode_entry4().
3794  */
nfsd4_encode_entry4_nfs_cookie4(const struct nfsd4_readdir * readdir,u64 offset)3795 static void nfsd4_encode_entry4_nfs_cookie4(const struct nfsd4_readdir *readdir,
3796 					    u64 offset)
3797 {
3798 	__be64 cookie = cpu_to_be64(offset);
3799 	struct xdr_stream *xdr = readdir->xdr;
3800 
3801 	if (!readdir->cookie_offset)
3802 		return;
3803 	write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset, &cookie,
3804 			       sizeof(cookie));
3805 }
3806 
attributes_need_mount(u32 * bmval)3807 static inline int attributes_need_mount(u32 *bmval)
3808 {
3809 	if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3810 		return 1;
3811 	if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3812 		return 1;
3813 	return 0;
3814 }
3815 
3816 static __be32
nfsd4_encode_entry4_fattr(struct nfsd4_readdir * cd,const char * name,int namlen)3817 nfsd4_encode_entry4_fattr(struct nfsd4_readdir *cd, const char *name,
3818 			  int namlen)
3819 {
3820 	struct svc_export *exp = cd->rd_fhp->fh_export;
3821 	struct dentry *dentry;
3822 	__be32 nfserr;
3823 	int ignore_crossmnt = 0;
3824 
3825 	dentry = lookup_one_positive_unlocked(&nop_mnt_idmap,
3826 					      &QSTR_LEN(name, namlen),
3827 					      cd->rd_fhp->fh_dentry);
3828 	if (IS_ERR(dentry))
3829 		return nfserrno(PTR_ERR(dentry));
3830 
3831 	exp_get(exp);
3832 	/*
3833 	 * In the case of a mountpoint, the client may be asking for
3834 	 * attributes that are only properties of the underlying filesystem
3835 	 * as opposed to the cross-mounted file system. In such a case,
3836 	 * we will not follow the cross mount and will fill the attribtutes
3837 	 * directly from the mountpoint dentry.
3838 	 */
3839 	if (nfsd_mountpoint(dentry, exp)) {
3840 		int err;
3841 
3842 		if (!(exp->ex_flags & NFSEXP_V4ROOT)
3843 				&& !attributes_need_mount(cd->rd_bmval)) {
3844 			ignore_crossmnt = 1;
3845 			goto out_encode;
3846 		}
3847 		/*
3848 		 * Why the heck aren't we just using nfsd_lookup??
3849 		 * Different "."/".." handling?  Something else?
3850 		 * At least, add a comment here to explain....
3851 		 */
3852 		err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3853 		if (err) {
3854 			nfserr = nfserrno(err);
3855 			goto out_put;
3856 		}
3857 		nfserr = check_nfsd_access(exp, cd->rd_rqstp, false);
3858 		if (nfserr)
3859 			goto out_put;
3860 
3861 	}
3862 out_encode:
3863 	nfserr = nfsd4_encode_fattr4(cd->rd_rqstp, cd->xdr, NULL, exp, dentry,
3864 				     cd->rd_bmval, ignore_crossmnt);
3865 out_put:
3866 	dput(dentry);
3867 	exp_put(exp);
3868 	return nfserr;
3869 }
3870 
3871 static __be32
nfsd4_encode_entry4_rdattr_error(struct xdr_stream * xdr,__be32 nfserr)3872 nfsd4_encode_entry4_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
3873 {
3874 	__be32 status;
3875 
3876 	/* attrmask */
3877 	status = nfsd4_encode_bitmap4(xdr, FATTR4_WORD0_RDATTR_ERROR, 0, 0);
3878 	if (status != nfs_ok)
3879 		return status;
3880 	/* attr_vals */
3881 	if (xdr_stream_encode_u32(xdr, XDR_UNIT) != XDR_UNIT)
3882 		return nfserr_resource;
3883 	/* rdattr_error */
3884 	if (xdr_stream_encode_be32(xdr, nfserr) != XDR_UNIT)
3885 		return nfserr_resource;
3886 	return nfs_ok;
3887 }
3888 
3889 static int
nfsd4_encode_entry4(void * ccdv,const char * name,int namlen,loff_t offset,u64 ino,unsigned int d_type)3890 nfsd4_encode_entry4(void *ccdv, const char *name, int namlen,
3891 		    loff_t offset, u64 ino, unsigned int d_type)
3892 {
3893 	struct readdir_cd *ccd = ccdv;
3894 	struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
3895 	struct xdr_stream *xdr = cd->xdr;
3896 	int start_offset = xdr->buf->len;
3897 	int cookie_offset;
3898 	u32 name_and_cookie;
3899 	int entry_bytes;
3900 	__be32 nfserr = nfserr_toosmall;
3901 
3902 	/* In nfsv4, "." and ".." never make it onto the wire.. */
3903 	if (name && isdotent(name, namlen)) {
3904 		cd->common.err = nfs_ok;
3905 		return 0;
3906 	}
3907 
3908 	/* Encode the previous entry's cookie value */
3909 	nfsd4_encode_entry4_nfs_cookie4(cd, offset);
3910 
3911 	if (xdr_stream_encode_item_present(xdr) != XDR_UNIT)
3912 		goto fail;
3913 
3914 	/* Reserve send buffer space for this entry's cookie value. */
3915 	cookie_offset = xdr->buf->len;
3916 	if (nfsd4_encode_nfs_cookie4(xdr, OFFSET_MAX) != nfs_ok)
3917 		goto fail;
3918 	if (nfsd4_encode_component4(xdr, name, namlen) != nfs_ok)
3919 		goto fail;
3920 	nfserr = nfsd4_encode_entry4_fattr(cd, name, namlen);
3921 	switch (nfserr) {
3922 	case nfs_ok:
3923 		break;
3924 	case nfserr_resource:
3925 		nfserr = nfserr_toosmall;
3926 		goto fail;
3927 	case nfserr_noent:
3928 		xdr_truncate_encode(xdr, start_offset);
3929 		goto skip_entry;
3930 	case nfserr_jukebox:
3931 		/*
3932 		 * The pseudoroot should only display dentries that lead to
3933 		 * exports. If we get EJUKEBOX here, then we can't tell whether
3934 		 * this entry should be included. Just fail the whole READDIR
3935 		 * with NFS4ERR_DELAY in that case, and hope that the situation
3936 		 * will resolve itself by the client's next attempt.
3937 		 */
3938 		if (cd->rd_fhp->fh_export->ex_flags & NFSEXP_V4ROOT)
3939 			goto fail;
3940 		fallthrough;
3941 	default:
3942 		/*
3943 		 * If the client requested the RDATTR_ERROR attribute,
3944 		 * we stuff the error code into this attribute
3945 		 * and continue.  If this attribute was not requested,
3946 		 * then in accordance with the spec, we fail the
3947 		 * entire READDIR operation(!)
3948 		 */
3949 		if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3950 			goto fail;
3951 		if (nfsd4_encode_entry4_rdattr_error(xdr, nfserr)) {
3952 			nfserr = nfserr_toosmall;
3953 			goto fail;
3954 		}
3955 	}
3956 	nfserr = nfserr_toosmall;
3957 	entry_bytes = xdr->buf->len - start_offset;
3958 	if (entry_bytes > cd->rd_maxcount)
3959 		goto fail;
3960 	cd->rd_maxcount -= entry_bytes;
3961 	/*
3962 	 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", and
3963 	 * notes that it could be zero. If it is zero, then the server
3964 	 * should enforce only the rd_maxcount value.
3965 	 */
3966 	if (cd->rd_dircount) {
3967 		name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
3968 		if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3969 			goto fail;
3970 		cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
3971 		if (!cd->rd_dircount)
3972 			cd->rd_maxcount = 0;
3973 	}
3974 
3975 	cd->cookie_offset = cookie_offset;
3976 skip_entry:
3977 	cd->common.err = nfs_ok;
3978 	return 0;
3979 fail:
3980 	xdr_truncate_encode(xdr, start_offset);
3981 	cd->common.err = nfserr;
3982 	return -EINVAL;
3983 }
3984 
3985 static __be32
nfsd4_encode_verifier4(struct xdr_stream * xdr,const nfs4_verifier * verf)3986 nfsd4_encode_verifier4(struct xdr_stream *xdr, const nfs4_verifier *verf)
3987 {
3988 	__be32 *p;
3989 
3990 	p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3991 	if (!p)
3992 		return nfserr_resource;
3993 	memcpy(p, verf->data, sizeof(verf->data));
3994 	return nfs_ok;
3995 }
3996 
3997 static __be32
nfsd4_encode_clientid4(struct xdr_stream * xdr,const clientid_t * clientid)3998 nfsd4_encode_clientid4(struct xdr_stream *xdr, const clientid_t *clientid)
3999 {
4000 	__be32 *p;
4001 
4002 	p = xdr_reserve_space(xdr, sizeof(__be64));
4003 	if (!p)
4004 		return nfserr_resource;
4005 	memcpy(p, clientid, sizeof(*clientid));
4006 	return nfs_ok;
4007 }
4008 
4009 /* This is a frequently-encoded item; open-coded for speed */
4010 static __be32
nfsd4_encode_stateid4(struct xdr_stream * xdr,const stateid_t * sid)4011 nfsd4_encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
4012 {
4013 	__be32 *p;
4014 
4015 	p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
4016 	if (!p)
4017 		return nfserr_resource;
4018 	*p++ = cpu_to_be32(sid->si_generation);
4019 	memcpy(p, &sid->si_opaque, sizeof(sid->si_opaque));
4020 	return nfs_ok;
4021 }
4022 
4023 static __be32
nfsd4_encode_sessionid4(struct xdr_stream * xdr,const struct nfs4_sessionid * sessionid)4024 nfsd4_encode_sessionid4(struct xdr_stream *xdr,
4025 			const struct nfs4_sessionid *sessionid)
4026 {
4027 	return nfsd4_encode_opaque_fixed(xdr, sessionid->data,
4028 					 NFS4_MAX_SESSIONID_LEN);
4029 }
4030 
4031 static __be32
nfsd4_encode_access(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4032 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr,
4033 		    union nfsd4_op_u *u)
4034 {
4035 	struct nfsd4_access *access = &u->access;
4036 	struct xdr_stream *xdr = resp->xdr;
4037 	__be32 status;
4038 
4039 	/* supported */
4040 	status = nfsd4_encode_uint32_t(xdr, access->ac_supported);
4041 	if (status != nfs_ok)
4042 		return status;
4043 	/* access */
4044 	return nfsd4_encode_uint32_t(xdr, access->ac_resp_access);
4045 }
4046 
nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4047 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr,
4048 						union nfsd4_op_u *u)
4049 {
4050 	struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
4051 	struct xdr_stream *xdr = resp->xdr;
4052 
4053 	/* bctsr_sessid */
4054 	nfserr = nfsd4_encode_sessionid4(xdr, &bcts->sessionid);
4055 	if (nfserr != nfs_ok)
4056 		return nfserr;
4057 	/* bctsr_dir */
4058 	if (xdr_stream_encode_u32(xdr, bcts->dir) != XDR_UNIT)
4059 		return nfserr_resource;
4060 	/* bctsr_use_conn_in_rdma_mode */
4061 	return nfsd4_encode_bool(xdr, false);
4062 }
4063 
4064 static __be32
nfsd4_encode_close(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4065 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr,
4066 		   union nfsd4_op_u *u)
4067 {
4068 	struct nfsd4_close *close = &u->close;
4069 	struct xdr_stream *xdr = resp->xdr;
4070 
4071 	/* open_stateid */
4072 	return nfsd4_encode_stateid4(xdr, &close->cl_stateid);
4073 }
4074 
4075 
4076 static __be32
nfsd4_encode_commit(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4077 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr,
4078 		    union nfsd4_op_u *u)
4079 {
4080 	struct nfsd4_commit *commit = &u->commit;
4081 
4082 	return nfsd4_encode_verifier4(resp->xdr, &commit->co_verf);
4083 }
4084 
4085 static __be32
nfsd4_encode_create(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4086 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr,
4087 		    union nfsd4_op_u *u)
4088 {
4089 	struct nfsd4_create *create = &u->create;
4090 	struct xdr_stream *xdr = resp->xdr;
4091 
4092 	/* cinfo */
4093 	nfserr = nfsd4_encode_change_info4(xdr, &create->cr_cinfo);
4094 	if (nfserr)
4095 		return nfserr;
4096 	/* attrset */
4097 	return nfsd4_encode_bitmap4(xdr, create->cr_bmval[0],
4098 				    create->cr_bmval[1], create->cr_bmval[2]);
4099 }
4100 
4101 static __be32
nfsd4_encode_getattr(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4102 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4103 		     union nfsd4_op_u *u)
4104 {
4105 	struct nfsd4_getattr *getattr = &u->getattr;
4106 	struct svc_fh *fhp = getattr->ga_fhp;
4107 	struct xdr_stream *xdr = resp->xdr;
4108 
4109 	/* obj_attributes */
4110 	return nfsd4_encode_fattr4(resp->rqstp, xdr, fhp, fhp->fh_export,
4111 				   fhp->fh_dentry, getattr->ga_bmval, 0);
4112 }
4113 
4114 static __be32
nfsd4_encode_getfh(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4115 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr,
4116 		   union nfsd4_op_u *u)
4117 {
4118 	struct xdr_stream *xdr = resp->xdr;
4119 	struct svc_fh *fhp = u->getfh;
4120 
4121 	/* object */
4122 	return nfsd4_encode_nfs_fh4(xdr, &fhp->fh_handle);
4123 }
4124 
4125 static __be32
nfsd4_encode_lock_owner4(struct xdr_stream * xdr,const clientid_t * clientid,const struct xdr_netobj * owner)4126 nfsd4_encode_lock_owner4(struct xdr_stream *xdr, const clientid_t *clientid,
4127 			 const struct xdr_netobj *owner)
4128 {
4129 	__be32 status;
4130 
4131 	/* clientid */
4132 	status = nfsd4_encode_clientid4(xdr, clientid);
4133 	if (status != nfs_ok)
4134 		return status;
4135 	/* owner */
4136 	return nfsd4_encode_opaque(xdr, owner->data, owner->len);
4137 }
4138 
4139 static __be32
nfsd4_encode_lock4denied(struct xdr_stream * xdr,const struct nfsd4_lock_denied * ld)4140 nfsd4_encode_lock4denied(struct xdr_stream *xdr,
4141 			 const struct nfsd4_lock_denied *ld)
4142 {
4143 	__be32 status;
4144 
4145 	/* offset */
4146 	status = nfsd4_encode_offset4(xdr, ld->ld_start);
4147 	if (status != nfs_ok)
4148 		return status;
4149 	/* length */
4150 	status = nfsd4_encode_length4(xdr, ld->ld_length);
4151 	if (status != nfs_ok)
4152 		return status;
4153 	/* locktype */
4154 	if (xdr_stream_encode_u32(xdr, ld->ld_type) != XDR_UNIT)
4155 		return nfserr_resource;
4156 	/* owner */
4157 	return nfsd4_encode_lock_owner4(xdr, &ld->ld_clientid,
4158 					&ld->ld_owner);
4159 }
4160 
4161 static __be32
nfsd4_encode_lock(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4162 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr,
4163 		  union nfsd4_op_u *u)
4164 {
4165 	struct nfsd4_lock *lock = &u->lock;
4166 	struct xdr_stream *xdr = resp->xdr;
4167 	__be32 status;
4168 
4169 	switch (nfserr) {
4170 	case nfs_ok:
4171 		/* resok4 */
4172 		status = nfsd4_encode_stateid4(xdr, &lock->lk_resp_stateid);
4173 		break;
4174 	case nfserr_denied:
4175 		/* denied */
4176 		status = nfsd4_encode_lock4denied(xdr, &lock->lk_denied);
4177 		break;
4178 	default:
4179 		return nfserr;
4180 	}
4181 	return status != nfs_ok ? status : nfserr;
4182 }
4183 
4184 static __be32
nfsd4_encode_lockt(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4185 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr,
4186 		   union nfsd4_op_u *u)
4187 {
4188 	struct nfsd4_lockt *lockt = &u->lockt;
4189 	struct xdr_stream *xdr = resp->xdr;
4190 	__be32 status;
4191 
4192 	if (nfserr == nfserr_denied) {
4193 		/* denied */
4194 		status = nfsd4_encode_lock4denied(xdr, &lockt->lt_denied);
4195 		if (status != nfs_ok)
4196 			return status;
4197 	}
4198 	return nfserr;
4199 }
4200 
4201 static __be32
nfsd4_encode_locku(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4202 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr,
4203 		   union nfsd4_op_u *u)
4204 {
4205 	struct nfsd4_locku *locku = &u->locku;
4206 	struct xdr_stream *xdr = resp->xdr;
4207 
4208 	/* lock_stateid */
4209 	return nfsd4_encode_stateid4(xdr, &locku->lu_stateid);
4210 }
4211 
4212 
4213 static __be32
nfsd4_encode_link(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4214 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr,
4215 		  union nfsd4_op_u *u)
4216 {
4217 	struct nfsd4_link *link = &u->link;
4218 	struct xdr_stream *xdr = resp->xdr;
4219 
4220 	return nfsd4_encode_change_info4(xdr, &link->li_cinfo);
4221 }
4222 
4223 /*
4224  * This implementation does not yet support returning an ACE in an
4225  * OPEN that offers a delegation.
4226  */
4227 static __be32
nfsd4_encode_open_nfsace4(struct xdr_stream * xdr)4228 nfsd4_encode_open_nfsace4(struct xdr_stream *xdr)
4229 {
4230 	__be32 status;
4231 
4232 	/* type */
4233 	status = nfsd4_encode_acetype4(xdr, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
4234 	if (status != nfs_ok)
4235 		return nfserr_resource;
4236 	/* flag */
4237 	status = nfsd4_encode_aceflag4(xdr, 0);
4238 	if (status != nfs_ok)
4239 		return nfserr_resource;
4240 	/* access mask */
4241 	status = nfsd4_encode_acemask4(xdr, 0);
4242 	if (status != nfs_ok)
4243 		return nfserr_resource;
4244 	/* who - empty for now */
4245 	if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
4246 		return nfserr_resource;
4247 	return nfs_ok;
4248 }
4249 
4250 static __be32
nfsd4_encode_open_read_delegation4(struct xdr_stream * xdr,struct nfsd4_open * open)4251 nfsd4_encode_open_read_delegation4(struct xdr_stream *xdr, struct nfsd4_open *open)
4252 {
4253 	__be32 status;
4254 
4255 	/* stateid */
4256 	status = nfsd4_encode_stateid4(xdr, &open->op_delegate_stateid);
4257 	if (status != nfs_ok)
4258 		return status;
4259 	/* recall */
4260 	status = nfsd4_encode_bool(xdr, open->op_recall);
4261 	if (status != nfs_ok)
4262 		return status;
4263 	/* permissions */
4264 	return nfsd4_encode_open_nfsace4(xdr);
4265 }
4266 
4267 static __be32
nfsd4_encode_nfs_space_limit4(struct xdr_stream * xdr,u64 filesize)4268 nfsd4_encode_nfs_space_limit4(struct xdr_stream *xdr, u64 filesize)
4269 {
4270 	/* limitby */
4271 	if (xdr_stream_encode_u32(xdr, NFS4_LIMIT_SIZE) != XDR_UNIT)
4272 		return nfserr_resource;
4273 	/* filesize */
4274 	return nfsd4_encode_uint64_t(xdr, filesize);
4275 }
4276 
4277 static __be32
nfsd4_encode_open_write_delegation4(struct xdr_stream * xdr,struct nfsd4_open * open)4278 nfsd4_encode_open_write_delegation4(struct xdr_stream *xdr,
4279 				    struct nfsd4_open *open)
4280 {
4281 	__be32 status;
4282 
4283 	/* stateid */
4284 	status = nfsd4_encode_stateid4(xdr, &open->op_delegate_stateid);
4285 	if (status != nfs_ok)
4286 		return status;
4287 	/* recall */
4288 	status = nfsd4_encode_bool(xdr, open->op_recall);
4289 	if (status != nfs_ok)
4290 		return status;
4291 	/* space_limit */
4292 	status = nfsd4_encode_nfs_space_limit4(xdr, 0);
4293 	if (status != nfs_ok)
4294 		return status;
4295 	return nfsd4_encode_open_nfsace4(xdr);
4296 }
4297 
4298 static __be32
nfsd4_encode_open_none_delegation4(struct xdr_stream * xdr,struct nfsd4_open * open)4299 nfsd4_encode_open_none_delegation4(struct xdr_stream *xdr,
4300 				   struct nfsd4_open *open)
4301 {
4302 	__be32 status = nfs_ok;
4303 
4304 	/* ond_why */
4305 	if (xdr_stream_encode_u32(xdr, open->op_why_no_deleg) != XDR_UNIT)
4306 		return nfserr_resource;
4307 	switch (open->op_why_no_deleg) {
4308 	case WND4_CONTENTION:
4309 		/* ond_server_will_push_deleg */
4310 		status = nfsd4_encode_bool(xdr, false);
4311 		break;
4312 	case WND4_RESOURCE:
4313 		/* ond_server_will_signal_avail */
4314 		status = nfsd4_encode_bool(xdr, false);
4315 	}
4316 	return status;
4317 }
4318 
4319 static __be32
nfsd4_encode_open_delegation4(struct xdr_stream * xdr,struct nfsd4_open * open)4320 nfsd4_encode_open_delegation4(struct xdr_stream *xdr, struct nfsd4_open *open)
4321 {
4322 	__be32 status;
4323 
4324 	/* delegation_type */
4325 	if (xdr_stream_encode_u32(xdr, open->op_delegate_type) != XDR_UNIT)
4326 		return nfserr_resource;
4327 	switch (open->op_delegate_type) {
4328 	case OPEN_DELEGATE_NONE:
4329 		status = nfs_ok;
4330 		break;
4331 	case OPEN_DELEGATE_READ:
4332 	case OPEN_DELEGATE_READ_ATTRS_DELEG:
4333 		/* read */
4334 		status = nfsd4_encode_open_read_delegation4(xdr, open);
4335 		break;
4336 	case OPEN_DELEGATE_WRITE:
4337 	case OPEN_DELEGATE_WRITE_ATTRS_DELEG:
4338 		/* write */
4339 		status = nfsd4_encode_open_write_delegation4(xdr, open);
4340 		break;
4341 	case OPEN_DELEGATE_NONE_EXT:
4342 		/* od_whynone */
4343 		status = nfsd4_encode_open_none_delegation4(xdr, open);
4344 		break;
4345 	default:
4346 		status = nfserr_serverfault;
4347 	}
4348 
4349 	return status;
4350 }
4351 
4352 static __be32
nfsd4_encode_open(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4353 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr,
4354 		  union nfsd4_op_u *u)
4355 {
4356 	struct nfsd4_open *open = &u->open;
4357 	struct xdr_stream *xdr = resp->xdr;
4358 
4359 	/* stateid */
4360 	nfserr = nfsd4_encode_stateid4(xdr, &open->op_stateid);
4361 	if (nfserr != nfs_ok)
4362 		return nfserr;
4363 	/* cinfo */
4364 	nfserr = nfsd4_encode_change_info4(xdr, &open->op_cinfo);
4365 	if (nfserr != nfs_ok)
4366 		return nfserr;
4367 	/* rflags */
4368 	nfserr = nfsd4_encode_uint32_t(xdr, open->op_rflags);
4369 	if (nfserr != nfs_ok)
4370 		return nfserr;
4371 	/* attrset */
4372 	nfserr = nfsd4_encode_bitmap4(xdr, open->op_bmval[0],
4373 				      open->op_bmval[1], open->op_bmval[2]);
4374 	if (nfserr != nfs_ok)
4375 		return nfserr;
4376 	/* delegation */
4377 	return nfsd4_encode_open_delegation4(xdr, open);
4378 }
4379 
4380 static __be32
nfsd4_encode_open_confirm(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4381 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr,
4382 			  union nfsd4_op_u *u)
4383 {
4384 	struct nfsd4_open_confirm *oc = &u->open_confirm;
4385 	struct xdr_stream *xdr = resp->xdr;
4386 
4387 	/* open_stateid */
4388 	return nfsd4_encode_stateid4(xdr, &oc->oc_resp_stateid);
4389 }
4390 
4391 static __be32
nfsd4_encode_open_downgrade(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4392 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr,
4393 			    union nfsd4_op_u *u)
4394 {
4395 	struct nfsd4_open_downgrade *od = &u->open_downgrade;
4396 	struct xdr_stream *xdr = resp->xdr;
4397 
4398 	/* open_stateid */
4399 	return nfsd4_encode_stateid4(xdr, &od->od_stateid);
4400 }
4401 
4402 /*
4403  * The operation of this function assumes that this is the only
4404  * READ operation in the COMPOUND. If there are multiple READs,
4405  * we use nfsd4_encode_readv().
4406  */
nfsd4_encode_splice_read(struct nfsd4_compoundres * resp,struct nfsd4_read * read,struct file * file,unsigned long maxcount)4407 static __be32 nfsd4_encode_splice_read(
4408 				struct nfsd4_compoundres *resp,
4409 				struct nfsd4_read *read,
4410 				struct file *file, unsigned long maxcount)
4411 {
4412 	struct xdr_stream *xdr = resp->xdr;
4413 	struct xdr_buf *buf = xdr->buf;
4414 	int status, space_left;
4415 	__be32 nfserr;
4416 
4417 	/*
4418 	 * Splice read doesn't work if encoding has already wandered
4419 	 * into the XDR buf's page array.
4420 	 */
4421 	if (unlikely(xdr->buf->page_len)) {
4422 		WARN_ON_ONCE(1);
4423 		return nfserr_serverfault;
4424 	}
4425 
4426 	/*
4427 	 * Make sure there is room at the end of buf->head for
4428 	 * svcxdr_encode_opaque_pages() to create a tail buffer
4429 	 * to XDR-pad the payload.
4430 	 */
4431 	if (xdr->iov != xdr->buf->head || xdr->end - xdr->p < 1)
4432 		return nfserr_resource;
4433 
4434 	nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
4435 				  file, read->rd_offset, &maxcount,
4436 				  &read->rd_eof);
4437 	read->rd_length = maxcount;
4438 	if (nfserr)
4439 		goto out_err;
4440 	svcxdr_encode_opaque_pages(read->rd_rqstp, xdr, buf->pages,
4441 				   buf->page_base, maxcount);
4442 	status = svc_encode_result_payload(read->rd_rqstp,
4443 					   buf->head[0].iov_len, maxcount);
4444 	if (status) {
4445 		nfserr = nfserrno(status);
4446 		goto out_err;
4447 	}
4448 
4449 	/*
4450 	 * Prepare to encode subsequent operations.
4451 	 *
4452 	 * xdr_truncate_encode() is not safe to use after a successful
4453 	 * splice read has been done, so the following stream
4454 	 * manipulations are open-coded.
4455 	 */
4456 	space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
4457 				buf->buflen - buf->len);
4458 	buf->buflen = buf->len + space_left;
4459 	xdr->end = (__be32 *)((void *)xdr->end + space_left);
4460 
4461 	return nfs_ok;
4462 
4463 out_err:
4464 	/*
4465 	 * nfsd_splice_actor may have already messed with the
4466 	 * page length; reset it so as not to confuse
4467 	 * xdr_truncate_encode in our caller.
4468 	 */
4469 	buf->page_len = 0;
4470 	return nfserr;
4471 }
4472 
nfsd4_encode_readv(struct nfsd4_compoundres * resp,struct nfsd4_read * read,struct file * file,unsigned long maxcount)4473 static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
4474 				 struct nfsd4_read *read,
4475 				 struct file *file, unsigned long maxcount)
4476 {
4477 	struct xdr_stream *xdr = resp->xdr;
4478 	unsigned int base = xdr->buf->page_len & ~PAGE_MASK;
4479 	unsigned int starting_len = xdr->buf->len;
4480 	__be32 zero = xdr_zero;
4481 	__be32 nfserr;
4482 
4483 	if (xdr_reserve_space_vec(xdr, maxcount) < 0)
4484 		return nfserr_resource;
4485 
4486 	nfserr = nfsd_iter_read(resp->rqstp, read->rd_fhp, file,
4487 				read->rd_offset, &maxcount, base,
4488 				&read->rd_eof);
4489 	read->rd_length = maxcount;
4490 	if (nfserr)
4491 		return nfserr;
4492 	if (svc_encode_result_payload(resp->rqstp, starting_len, maxcount))
4493 		return nfserr_io;
4494 	xdr_truncate_encode(xdr, starting_len + xdr_align_size(maxcount));
4495 
4496 	write_bytes_to_xdr_buf(xdr->buf, starting_len + maxcount, &zero,
4497 			       xdr_pad_size(maxcount));
4498 	return nfs_ok;
4499 }
4500 
4501 static __be32
nfsd4_encode_read(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4502 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
4503 		  union nfsd4_op_u *u)
4504 {
4505 	struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
4506 	struct nfsd4_read *read = &u->read;
4507 	struct xdr_stream *xdr = resp->xdr;
4508 	bool splice_ok = argp->splice_ok;
4509 	unsigned int eof_offset;
4510 	unsigned long maxcount;
4511 	__be32 wire_data[2];
4512 	struct file *file;
4513 
4514 	if (nfserr)
4515 		return nfserr;
4516 
4517 	eof_offset = xdr->buf->len;
4518 	file = read->rd_nf->nf_file;
4519 
4520 	/* Reserve space for the eof flag and byte count */
4521 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2))) {
4522 		WARN_ON_ONCE(splice_ok);
4523 		return nfserr_resource;
4524 	}
4525 	xdr_commit_encode(xdr);
4526 
4527 	maxcount = min_t(unsigned long, read->rd_length,
4528 			 (xdr->buf->buflen - xdr->buf->len));
4529 
4530 	if (file->f_op->splice_read && splice_ok)
4531 		nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
4532 	else
4533 		nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
4534 	if (nfserr) {
4535 		xdr_truncate_encode(xdr, eof_offset);
4536 		return nfserr;
4537 	}
4538 
4539 	wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
4540 	wire_data[1] = cpu_to_be32(read->rd_length);
4541 	write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
4542 	return nfs_ok;
4543 }
4544 
4545 static __be32
nfsd4_encode_readlink(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4546 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr,
4547 		      union nfsd4_op_u *u)
4548 {
4549 	struct nfsd4_readlink *readlink = &u->readlink;
4550 	__be32 *p, wire_count, zero = xdr_zero;
4551 	struct xdr_stream *xdr = resp->xdr;
4552 	unsigned int length_offset;
4553 	int maxcount, status;
4554 
4555 	/* linktext4.count */
4556 	length_offset = xdr->buf->len;
4557 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
4558 		return nfserr_resource;
4559 
4560 	/* linktext4.data */
4561 	maxcount = PAGE_SIZE;
4562 	p = xdr_reserve_space(xdr, maxcount);
4563 	if (!p)
4564 		return nfserr_resource;
4565 	nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4566 						(char *)p, &maxcount);
4567 	if (nfserr == nfserr_isdir)
4568 		nfserr = nfserr_inval;
4569 	if (nfserr)
4570 		goto out_err;
4571 	status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4572 					   maxcount);
4573 	if (status) {
4574 		nfserr = nfserrno(status);
4575 		goto out_err;
4576 	}
4577 
4578 	wire_count = cpu_to_be32(maxcount);
4579 	write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, XDR_UNIT);
4580 	xdr_truncate_encode(xdr, length_offset + 4 + xdr_align_size(maxcount));
4581 	write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount, &zero,
4582 			       xdr_pad_size(maxcount));
4583 	return nfs_ok;
4584 
4585 out_err:
4586 	xdr_truncate_encode(xdr, length_offset);
4587 	return nfserr;
4588 }
4589 
nfsd4_encode_dirlist4(struct xdr_stream * xdr,struct nfsd4_readdir * readdir,u32 max_payload)4590 static __be32 nfsd4_encode_dirlist4(struct xdr_stream *xdr,
4591 				    struct nfsd4_readdir *readdir,
4592 				    u32 max_payload)
4593 {
4594 	int bytes_left, maxcount, starting_len = xdr->buf->len;
4595 	loff_t offset;
4596 	__be32 status;
4597 
4598 	/*
4599 	 * Number of bytes left for directory entries allowing for the
4600 	 * final 8 bytes of the readdir and a following failed op.
4601 	 */
4602 	bytes_left = xdr->buf->buflen - xdr->buf->len -
4603 		COMPOUND_ERR_SLACK_SPACE - XDR_UNIT * 2;
4604 	if (bytes_left < 0)
4605 		return nfserr_resource;
4606 	maxcount = min_t(u32, readdir->rd_maxcount, max_payload);
4607 
4608 	/*
4609 	 * The RFC defines rd_maxcount as the size of the
4610 	 * READDIR4resok structure, which includes the verifier
4611 	 * and the 8 bytes encoded at the end of this function.
4612 	 */
4613 	if (maxcount < XDR_UNIT * 4)
4614 		return nfserr_toosmall;
4615 	maxcount = min_t(int, maxcount - XDR_UNIT * 4, bytes_left);
4616 
4617 	/* RFC 3530 14.2.24 allows us to ignore dircount when it's 0 */
4618 	if (!readdir->rd_dircount)
4619 		readdir->rd_dircount = max_payload;
4620 
4621 	/* *entries */
4622 	readdir->xdr = xdr;
4623 	readdir->rd_maxcount = maxcount;
4624 	readdir->common.err = 0;
4625 	readdir->cookie_offset = 0;
4626 	offset = readdir->rd_cookie;
4627 	status = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp, &offset,
4628 			      &readdir->common, nfsd4_encode_entry4);
4629 	if (status)
4630 		return status;
4631 	if (readdir->common.err == nfserr_toosmall &&
4632 	    xdr->buf->len == starting_len) {
4633 		/* No entries were encoded. Which limit did we hit? */
4634 		if (maxcount - XDR_UNIT * 4 < bytes_left)
4635 			/* It was the fault of rd_maxcount */
4636 			return nfserr_toosmall;
4637 		/* We ran out of buffer space */
4638 		return nfserr_resource;
4639 	}
4640 	/* Encode the final entry's cookie value */
4641 	nfsd4_encode_entry4_nfs_cookie4(readdir, offset);
4642 	/* No entries follow */
4643 	if (xdr_stream_encode_item_absent(xdr) != XDR_UNIT)
4644 		return nfserr_resource;
4645 
4646 	/* eof */
4647 	return nfsd4_encode_bool(xdr, readdir->common.err == nfserr_eof);
4648 }
4649 
4650 static __be32
nfsd4_encode_readdir(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4651 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr,
4652 		     union nfsd4_op_u *u)
4653 {
4654 	struct nfsd4_readdir *readdir = &u->readdir;
4655 	struct xdr_stream *xdr = resp->xdr;
4656 	int starting_len = xdr->buf->len;
4657 
4658 	/* cookieverf */
4659 	nfserr = nfsd4_encode_verifier4(xdr, &readdir->rd_verf);
4660 	if (nfserr != nfs_ok)
4661 		return nfserr;
4662 
4663 	/* reply */
4664 	nfserr = nfsd4_encode_dirlist4(xdr, readdir, svc_max_payload(resp->rqstp));
4665 	if (nfserr != nfs_ok)
4666 		xdr_truncate_encode(xdr, starting_len);
4667 	return nfserr;
4668 }
4669 
4670 static __be32
nfsd4_encode_remove(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4671 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr,
4672 		    union nfsd4_op_u *u)
4673 {
4674 	struct nfsd4_remove *remove = &u->remove;
4675 	struct xdr_stream *xdr = resp->xdr;
4676 
4677 	return nfsd4_encode_change_info4(xdr, &remove->rm_cinfo);
4678 }
4679 
4680 static __be32
nfsd4_encode_rename(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4681 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr,
4682 		    union nfsd4_op_u *u)
4683 {
4684 	struct nfsd4_rename *rename = &u->rename;
4685 	struct xdr_stream *xdr = resp->xdr;
4686 
4687 	nfserr = nfsd4_encode_change_info4(xdr, &rename->rn_sinfo);
4688 	if (nfserr)
4689 		return nfserr;
4690 	return nfsd4_encode_change_info4(xdr, &rename->rn_tinfo);
4691 }
4692 
4693 static __be32
nfsd4_encode_rpcsec_gss_info(struct xdr_stream * xdr,struct rpcsec_gss_info * info)4694 nfsd4_encode_rpcsec_gss_info(struct xdr_stream *xdr,
4695 			     struct rpcsec_gss_info *info)
4696 {
4697 	__be32 status;
4698 
4699 	/* oid */
4700 	if (xdr_stream_encode_opaque(xdr, info->oid.data, info->oid.len) < 0)
4701 		return nfserr_resource;
4702 	/* qop */
4703 	status = nfsd4_encode_qop4(xdr, info->qop);
4704 	if (status != nfs_ok)
4705 		return status;
4706 	/* service */
4707 	if (xdr_stream_encode_u32(xdr, info->service) != XDR_UNIT)
4708 		return nfserr_resource;
4709 
4710 	return nfs_ok;
4711 }
4712 
4713 static __be32
nfsd4_encode_secinfo4(struct xdr_stream * xdr,rpc_authflavor_t pf,u32 * supported)4714 nfsd4_encode_secinfo4(struct xdr_stream *xdr, rpc_authflavor_t pf,
4715 		      u32 *supported)
4716 {
4717 	struct rpcsec_gss_info info;
4718 	__be32 status;
4719 
4720 	if (rpcauth_get_gssinfo(pf, &info) == 0) {
4721 		(*supported)++;
4722 
4723 		/* flavor */
4724 		status = nfsd4_encode_uint32_t(xdr, RPC_AUTH_GSS);
4725 		if (status != nfs_ok)
4726 			return status;
4727 		/* flavor_info */
4728 		status = nfsd4_encode_rpcsec_gss_info(xdr, &info);
4729 		if (status != nfs_ok)
4730 			return status;
4731 	} else if (pf < RPC_AUTH_MAXFLAVOR) {
4732 		(*supported)++;
4733 
4734 		/* flavor */
4735 		status = nfsd4_encode_uint32_t(xdr, pf);
4736 		if (status != nfs_ok)
4737 			return status;
4738 	}
4739 	return nfs_ok;
4740 }
4741 
4742 static __be32
nfsd4_encode_SECINFO4resok(struct xdr_stream * xdr,struct svc_export * exp)4743 nfsd4_encode_SECINFO4resok(struct xdr_stream *xdr, struct svc_export *exp)
4744 {
4745 	u32 i, nflavs, supported;
4746 	struct exp_flavor_info *flavs;
4747 	struct exp_flavor_info def_flavs[2];
4748 	unsigned int count_offset;
4749 	__be32 status, wire_count;
4750 
4751 	if (exp->ex_nflavors) {
4752 		flavs = exp->ex_flavors;
4753 		nflavs = exp->ex_nflavors;
4754 	} else { /* Handling of some defaults in absence of real secinfo: */
4755 		flavs = def_flavs;
4756 		if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4757 			nflavs = 2;
4758 			flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4759 			flavs[1].pseudoflavor = RPC_AUTH_NULL;
4760 		} else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4761 			nflavs = 1;
4762 			flavs[0].pseudoflavor
4763 					= svcauth_gss_flavor(exp->ex_client);
4764 		} else {
4765 			nflavs = 1;
4766 			flavs[0].pseudoflavor
4767 					= exp->ex_client->flavour->flavour;
4768 		}
4769 	}
4770 
4771 	count_offset = xdr->buf->len;
4772 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT)))
4773 		return nfserr_resource;
4774 
4775 	for (i = 0, supported = 0; i < nflavs; i++) {
4776 		status = nfsd4_encode_secinfo4(xdr, flavs[i].pseudoflavor,
4777 					       &supported);
4778 		if (status != nfs_ok)
4779 			return status;
4780 	}
4781 
4782 	wire_count = cpu_to_be32(supported);
4783 	write_bytes_to_xdr_buf(xdr->buf, count_offset, &wire_count,
4784 			       XDR_UNIT);
4785 	return 0;
4786 }
4787 
4788 static __be32
nfsd4_encode_secinfo(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4789 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4790 		     union nfsd4_op_u *u)
4791 {
4792 	struct nfsd4_secinfo *secinfo = &u->secinfo;
4793 	struct xdr_stream *xdr = resp->xdr;
4794 
4795 	return nfsd4_encode_SECINFO4resok(xdr, secinfo->si_exp);
4796 }
4797 
4798 static __be32
nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4799 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4800 		     union nfsd4_op_u *u)
4801 {
4802 	struct nfsd4_secinfo_no_name *secinfo = &u->secinfo_no_name;
4803 	struct xdr_stream *xdr = resp->xdr;
4804 
4805 	return nfsd4_encode_SECINFO4resok(xdr, secinfo->sin_exp);
4806 }
4807 
4808 static __be32
nfsd4_encode_setattr(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4809 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4810 		     union nfsd4_op_u *u)
4811 {
4812 	struct nfsd4_setattr *setattr = &u->setattr;
4813 	__be32 status;
4814 
4815 	switch (nfserr) {
4816 	case nfs_ok:
4817 		/* attrsset */
4818 		status = nfsd4_encode_bitmap4(resp->xdr, setattr->sa_bmval[0],
4819 					      setattr->sa_bmval[1],
4820 					      setattr->sa_bmval[2]);
4821 		break;
4822 	default:
4823 		/* attrsset */
4824 		status = nfsd4_encode_bitmap4(resp->xdr, 0, 0, 0);
4825 	}
4826 	return status != nfs_ok ? status : nfserr;
4827 }
4828 
4829 static __be32
nfsd4_encode_setclientid(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4830 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr,
4831 			 union nfsd4_op_u *u)
4832 {
4833 	struct nfsd4_setclientid *scd = &u->setclientid;
4834 	struct xdr_stream *xdr = resp->xdr;
4835 
4836 	if (!nfserr) {
4837 		nfserr = nfsd4_encode_clientid4(xdr, &scd->se_clientid);
4838 		if (nfserr != nfs_ok)
4839 			goto out;
4840 		nfserr = nfsd4_encode_verifier4(xdr, &scd->se_confirm);
4841 	} else if (nfserr == nfserr_clid_inuse) {
4842 		/* empty network id */
4843 		if (xdr_stream_encode_u32(xdr, 0) < 0) {
4844 			nfserr = nfserr_resource;
4845 			goto out;
4846 		}
4847 		/* empty universal address */
4848 		if (xdr_stream_encode_u32(xdr, 0) < 0) {
4849 			nfserr = nfserr_resource;
4850 			goto out;
4851 		}
4852 	}
4853 out:
4854 	return nfserr;
4855 }
4856 
4857 static __be32
nfsd4_encode_write(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4858 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr,
4859 		   union nfsd4_op_u *u)
4860 {
4861 	struct nfsd4_write *write = &u->write;
4862 	struct xdr_stream *xdr = resp->xdr;
4863 
4864 	/* count */
4865 	nfserr = nfsd4_encode_count4(xdr, write->wr_bytes_written);
4866 	if (nfserr)
4867 		return nfserr;
4868 	/* committed */
4869 	if (xdr_stream_encode_u32(xdr, write->wr_how_written) != XDR_UNIT)
4870 		return nfserr_resource;
4871 	/* writeverf */
4872 	return nfsd4_encode_verifier4(xdr, &write->wr_verifier);
4873 }
4874 
4875 static __be32
nfsd4_encode_state_protect_ops4(struct xdr_stream * xdr,struct nfsd4_exchange_id * exid)4876 nfsd4_encode_state_protect_ops4(struct xdr_stream *xdr,
4877 				struct nfsd4_exchange_id *exid)
4878 {
4879 	__be32 status;
4880 
4881 	/* spo_must_enforce */
4882 	status = nfsd4_encode_bitmap4(xdr, exid->spo_must_enforce[0],
4883 				      exid->spo_must_enforce[1],
4884 				      exid->spo_must_enforce[2]);
4885 	if (status != nfs_ok)
4886 		return status;
4887 	/* spo_must_allow */
4888 	return nfsd4_encode_bitmap4(xdr, exid->spo_must_allow[0],
4889 				    exid->spo_must_allow[1],
4890 				    exid->spo_must_allow[2]);
4891 }
4892 
4893 static __be32
nfsd4_encode_state_protect4_r(struct xdr_stream * xdr,struct nfsd4_exchange_id * exid)4894 nfsd4_encode_state_protect4_r(struct xdr_stream *xdr, struct nfsd4_exchange_id *exid)
4895 {
4896 	__be32 status;
4897 
4898 	if (xdr_stream_encode_u32(xdr, exid->spa_how) != XDR_UNIT)
4899 		return nfserr_resource;
4900 	switch (exid->spa_how) {
4901 	case SP4_NONE:
4902 		status = nfs_ok;
4903 		break;
4904 	case SP4_MACH_CRED:
4905 		/* spr_mach_ops */
4906 		status = nfsd4_encode_state_protect_ops4(xdr, exid);
4907 		break;
4908 	default:
4909 		status = nfserr_serverfault;
4910 	}
4911 	return status;
4912 }
4913 
4914 static __be32
nfsd4_encode_server_owner4(struct xdr_stream * xdr,struct svc_rqst * rqstp)4915 nfsd4_encode_server_owner4(struct xdr_stream *xdr, struct svc_rqst *rqstp)
4916 {
4917 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4918 	__be32 status;
4919 
4920 	/* so_minor_id */
4921 	status = nfsd4_encode_uint64_t(xdr, 0);
4922 	if (status != nfs_ok)
4923 		return status;
4924 	/* so_major_id */
4925 	return nfsd4_encode_opaque(xdr, nn->nfsd_name, strlen(nn->nfsd_name));
4926 }
4927 
4928 static __be32
nfsd4_encode_nfs_impl_id4(struct xdr_stream * xdr,struct nfsd4_exchange_id * exid)4929 nfsd4_encode_nfs_impl_id4(struct xdr_stream *xdr, struct nfsd4_exchange_id *exid)
4930 {
4931 	__be32 status;
4932 
4933 	/* nii_domain */
4934 	status = nfsd4_encode_opaque(xdr, exid->nii_domain.data,
4935 				     exid->nii_domain.len);
4936 	if (status != nfs_ok)
4937 		return status;
4938 	/* nii_name */
4939 	status = nfsd4_encode_opaque(xdr, exid->nii_name.data,
4940 				     exid->nii_name.len);
4941 	if (status != nfs_ok)
4942 		return status;
4943 	/* nii_time */
4944 	return nfsd4_encode_nfstime4(xdr, &exid->nii_time);
4945 }
4946 
4947 static __be32
nfsd4_encode_exchange_id(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)4948 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
4949 			 union nfsd4_op_u *u)
4950 {
4951 	struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
4952 	struct nfsd4_exchange_id *exid = &u->exchange_id;
4953 	struct xdr_stream *xdr = resp->xdr;
4954 
4955 	/* eir_clientid */
4956 	nfserr = nfsd4_encode_clientid4(xdr, &exid->clientid);
4957 	if (nfserr != nfs_ok)
4958 		return nfserr;
4959 	/* eir_sequenceid */
4960 	nfserr = nfsd4_encode_sequenceid4(xdr, exid->seqid);
4961 	if (nfserr != nfs_ok)
4962 		return nfserr;
4963 	/* eir_flags */
4964 	nfserr = nfsd4_encode_uint32_t(xdr, exid->flags);
4965 	if (nfserr != nfs_ok)
4966 		return nfserr;
4967 	/* eir_state_protect */
4968 	nfserr = nfsd4_encode_state_protect4_r(xdr, exid);
4969 	if (nfserr != nfs_ok)
4970 		return nfserr;
4971 	/* eir_server_owner */
4972 	nfserr = nfsd4_encode_server_owner4(xdr, resp->rqstp);
4973 	if (nfserr != nfs_ok)
4974 		return nfserr;
4975 	/* eir_server_scope */
4976 	nfserr = nfsd4_encode_opaque(xdr, nn->nfsd_name,
4977 				     strlen(nn->nfsd_name));
4978 	if (nfserr != nfs_ok)
4979 		return nfserr;
4980 	/* eir_server_impl_id<1> */
4981 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
4982 		return nfserr_resource;
4983 	nfserr = nfsd4_encode_nfs_impl_id4(xdr, exid);
4984 	if (nfserr != nfs_ok)
4985 		return nfserr;
4986 
4987 	return nfs_ok;
4988 }
4989 
4990 static __be32
nfsd4_encode_channel_attrs4(struct xdr_stream * xdr,const struct nfsd4_channel_attrs * attrs)4991 nfsd4_encode_channel_attrs4(struct xdr_stream *xdr,
4992 			    const struct nfsd4_channel_attrs *attrs)
4993 {
4994 	__be32 status;
4995 
4996 	/* ca_headerpadsize */
4997 	status = nfsd4_encode_count4(xdr, 0);
4998 	if (status != nfs_ok)
4999 		return status;
5000 	/* ca_maxrequestsize */
5001 	status = nfsd4_encode_count4(xdr, attrs->maxreq_sz);
5002 	if (status != nfs_ok)
5003 		return status;
5004 	/* ca_maxresponsesize */
5005 	status = nfsd4_encode_count4(xdr, attrs->maxresp_sz);
5006 	if (status != nfs_ok)
5007 		return status;
5008 	/* ca_maxresponsesize_cached */
5009 	status = nfsd4_encode_count4(xdr, attrs->maxresp_cached);
5010 	if (status != nfs_ok)
5011 		return status;
5012 	/* ca_maxoperations */
5013 	status = nfsd4_encode_count4(xdr, attrs->maxops);
5014 	if (status != nfs_ok)
5015 		return status;
5016 	/* ca_maxrequests */
5017 	status = nfsd4_encode_count4(xdr, attrs->maxreqs);
5018 	if (status != nfs_ok)
5019 		return status;
5020 	/* ca_rdma_ird<1> */
5021 	if (xdr_stream_encode_u32(xdr, attrs->nr_rdma_attrs) != XDR_UNIT)
5022 		return nfserr_resource;
5023 	if (attrs->nr_rdma_attrs)
5024 		return nfsd4_encode_uint32_t(xdr, attrs->rdma_attrs);
5025 	return nfs_ok;
5026 }
5027 
5028 static __be32
nfsd4_encode_create_session(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5029 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
5030 			    union nfsd4_op_u *u)
5031 {
5032 	struct nfsd4_create_session *sess = &u->create_session;
5033 	struct xdr_stream *xdr = resp->xdr;
5034 
5035 	/* csr_sessionid */
5036 	nfserr = nfsd4_encode_sessionid4(xdr, &sess->sessionid);
5037 	if (nfserr != nfs_ok)
5038 		return nfserr;
5039 	/* csr_sequence */
5040 	nfserr = nfsd4_encode_sequenceid4(xdr, sess->seqid);
5041 	if (nfserr != nfs_ok)
5042 		return nfserr;
5043 	/* csr_flags */
5044 	nfserr = nfsd4_encode_uint32_t(xdr, sess->flags);
5045 	if (nfserr != nfs_ok)
5046 		return nfserr;
5047 	/* csr_fore_chan_attrs */
5048 	nfserr = nfsd4_encode_channel_attrs4(xdr, &sess->fore_channel);
5049 	if (nfserr != nfs_ok)
5050 		return nfserr;
5051 	/* csr_back_chan_attrs */
5052 	return nfsd4_encode_channel_attrs4(xdr, &sess->back_channel);
5053 }
5054 
5055 static __be32
nfsd4_encode_sequence(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5056 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
5057 		      union nfsd4_op_u *u)
5058 {
5059 	struct nfsd4_sequence *seq = &u->sequence;
5060 	struct xdr_stream *xdr = resp->xdr;
5061 
5062 	/* sr_sessionid */
5063 	nfserr = nfsd4_encode_sessionid4(xdr, &seq->sessionid);
5064 	if (nfserr != nfs_ok)
5065 		return nfserr;
5066 	/* sr_sequenceid */
5067 	nfserr = nfsd4_encode_sequenceid4(xdr, seq->seqid);
5068 	if (nfserr != nfs_ok)
5069 		return nfserr;
5070 	/* sr_slotid */
5071 	nfserr = nfsd4_encode_slotid4(xdr, seq->slotid);
5072 	if (nfserr != nfs_ok)
5073 		return nfserr;
5074 	/* Note slotid's are numbered from zero: */
5075 	/* sr_highest_slotid */
5076 	nfserr = nfsd4_encode_slotid4(xdr, seq->maxslots - 1);
5077 	if (nfserr != nfs_ok)
5078 		return nfserr;
5079 	/* sr_target_highest_slotid */
5080 	nfserr = nfsd4_encode_slotid4(xdr, seq->target_maxslots - 1);
5081 	if (nfserr != nfs_ok)
5082 		return nfserr;
5083 	/* sr_status_flags */
5084 	nfserr = nfsd4_encode_uint32_t(xdr, seq->status_flags);
5085 	if (nfserr != nfs_ok)
5086 		return nfserr;
5087 
5088 	resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
5089 	return nfs_ok;
5090 }
5091 
5092 static __be32
nfsd4_encode_test_stateid(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5093 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
5094 			  union nfsd4_op_u *u)
5095 {
5096 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5097 	struct nfsd4_test_stateid_id *stateid, *next;
5098 	struct xdr_stream *xdr = resp->xdr;
5099 
5100 	/* tsr_status_codes<> */
5101 	if (xdr_stream_encode_u32(xdr, test_stateid->ts_num_ids) != XDR_UNIT)
5102 		return nfserr_resource;
5103 	list_for_each_entry_safe(stateid, next,
5104 				 &test_stateid->ts_stateid_list, ts_id_list) {
5105 		if (xdr_stream_encode_be32(xdr, stateid->ts_id_status) != XDR_UNIT)
5106 			return nfserr_resource;
5107 	}
5108 	return nfs_ok;
5109 }
5110 
5111 static __be32
nfsd4_encode_get_dir_delegation(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5112 nfsd4_encode_get_dir_delegation(struct nfsd4_compoundres *resp, __be32 nfserr,
5113 				union nfsd4_op_u *u)
5114 {
5115 	struct nfsd4_get_dir_delegation *gdd = &u->get_dir_delegation;
5116 	struct xdr_stream *xdr = resp->xdr;
5117 	__be32 status = nfserr_resource;
5118 
5119 	switch(gdd->gddrnf_status) {
5120 	case GDD4_OK:
5121 		if (xdr_stream_encode_u32(xdr, GDD4_OK) != XDR_UNIT)
5122 			break;
5123 		status = nfsd4_encode_verifier4(xdr, &gdd->gddr_cookieverf);
5124 		if (status)
5125 			break;
5126 		status = nfsd4_encode_stateid4(xdr, &gdd->gddr_stateid);
5127 		if (status)
5128 			break;
5129 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_notification[0], 0, 0);
5130 		if (status)
5131 			break;
5132 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_child_attributes[0],
5133 						   gdd->gddr_child_attributes[1],
5134 						   gdd->gddr_child_attributes[2]);
5135 		if (status)
5136 			break;
5137 		status = nfsd4_encode_bitmap4(xdr, gdd->gddr_dir_attributes[0],
5138 						   gdd->gddr_dir_attributes[1],
5139 						   gdd->gddr_dir_attributes[2]);
5140 		break;
5141 	default:
5142 		pr_warn("nfsd: bad gddrnf_status (%u)\n", gdd->gddrnf_status);
5143 		gdd->gddrnf_will_signal_deleg_avail = 0;
5144 		fallthrough;
5145 	case GDD4_UNAVAIL:
5146 		if (xdr_stream_encode_u32(xdr, GDD4_UNAVAIL) != XDR_UNIT)
5147 			break;
5148 		status = nfsd4_encode_bool(xdr, gdd->gddrnf_will_signal_deleg_avail);
5149 		break;
5150 	}
5151 	return status;
5152 }
5153 
5154 #ifdef CONFIG_NFSD_PNFS
5155 static __be32
nfsd4_encode_device_addr4(struct xdr_stream * xdr,const struct nfsd4_getdeviceinfo * gdev)5156 nfsd4_encode_device_addr4(struct xdr_stream *xdr,
5157 			  const struct nfsd4_getdeviceinfo *gdev)
5158 {
5159 	u32 needed_len, starting_len = xdr->buf->len;
5160 	const struct nfsd4_layout_ops *ops;
5161 	__be32 status;
5162 
5163 	/* da_layout_type */
5164 	if (xdr_stream_encode_u32(xdr, gdev->gd_layout_type) != XDR_UNIT)
5165 		return nfserr_resource;
5166 	/* da_addr_body */
5167 	ops = nfsd4_layout_ops[gdev->gd_layout_type];
5168 	status = ops->encode_getdeviceinfo(xdr, gdev);
5169 	if (status != nfs_ok) {
5170 		/*
5171 		 * Don't burden the layout drivers with enforcing
5172 		 * gd_maxcount. Just tell the client to come back
5173 		 * with a bigger buffer if it's not enough.
5174 		 */
5175 		if (xdr->buf->len + XDR_UNIT > gdev->gd_maxcount)
5176 			goto toosmall;
5177 		return status;
5178 	}
5179 
5180 	return nfs_ok;
5181 
5182 toosmall:
5183 	needed_len = xdr->buf->len + XDR_UNIT;	/* notifications */
5184 	xdr_truncate_encode(xdr, starting_len);
5185 
5186 	status = nfsd4_encode_count4(xdr, needed_len);
5187 	if (status != nfs_ok)
5188 		return status;
5189 	return nfserr_toosmall;
5190 }
5191 
5192 static __be32
nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5193 nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
5194 		union nfsd4_op_u *u)
5195 {
5196 	struct nfsd4_getdeviceinfo *gdev = &u->getdeviceinfo;
5197 	struct xdr_stream *xdr = resp->xdr;
5198 
5199 	/* gdir_device_addr */
5200 	nfserr = nfsd4_encode_device_addr4(xdr, gdev);
5201 	if (nfserr)
5202 		return nfserr;
5203 	/* gdir_notification */
5204 	return nfsd4_encode_bitmap4(xdr, gdev->gd_notify_types, 0, 0);
5205 }
5206 
5207 static __be32
nfsd4_encode_layout4(struct xdr_stream * xdr,const struct nfsd4_layoutget * lgp)5208 nfsd4_encode_layout4(struct xdr_stream *xdr, const struct nfsd4_layoutget *lgp)
5209 {
5210 	const struct nfsd4_layout_ops *ops = nfsd4_layout_ops[lgp->lg_layout_type];
5211 	__be32 status;
5212 
5213 	/* lo_offset */
5214 	status = nfsd4_encode_offset4(xdr, lgp->lg_seg.offset);
5215 	if (status != nfs_ok)
5216 		return status;
5217 	/* lo_length */
5218 	status = nfsd4_encode_length4(xdr, lgp->lg_seg.length);
5219 	if (status != nfs_ok)
5220 		return status;
5221 	/* lo_iomode */
5222 	if (xdr_stream_encode_u32(xdr, lgp->lg_seg.iomode) != XDR_UNIT)
5223 		return nfserr_resource;
5224 	/* lo_content */
5225 	if (xdr_stream_encode_u32(xdr, lgp->lg_layout_type) != XDR_UNIT)
5226 		return nfserr_resource;
5227 	return ops->encode_layoutget(xdr, lgp);
5228 }
5229 
5230 static __be32
nfsd4_encode_layoutget(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5231 nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
5232 		union nfsd4_op_u *u)
5233 {
5234 	struct nfsd4_layoutget *lgp = &u->layoutget;
5235 	struct xdr_stream *xdr = resp->xdr;
5236 
5237 	/* logr_return_on_close */
5238 	nfserr = nfsd4_encode_bool(xdr, true);
5239 	if (nfserr != nfs_ok)
5240 		return nfserr;
5241 	/* logr_stateid */
5242 	nfserr = nfsd4_encode_stateid4(xdr, &lgp->lg_sid);
5243 	if (nfserr != nfs_ok)
5244 		return nfserr;
5245 	/* logr_layout<> */
5246 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5247 		return nfserr_resource;
5248 	return nfsd4_encode_layout4(xdr, lgp);
5249 }
5250 
5251 static __be32
nfsd4_encode_layoutcommit(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5252 nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
5253 			  union nfsd4_op_u *u)
5254 {
5255 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
5256 	struct xdr_stream *xdr = resp->xdr;
5257 
5258 	/* ns_sizechanged */
5259 	nfserr = nfsd4_encode_bool(xdr, lcp->lc_size_chg);
5260 	if (nfserr != nfs_ok)
5261 		return nfserr;
5262 	if (lcp->lc_size_chg)
5263 		/* ns_size */
5264 		return nfsd4_encode_length4(xdr, lcp->lc_newsize);
5265 	return nfs_ok;
5266 }
5267 
5268 static __be32
nfsd4_encode_layoutreturn(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5269 nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
5270 		union nfsd4_op_u *u)
5271 {
5272 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
5273 	struct xdr_stream *xdr = resp->xdr;
5274 
5275 	/* lrs_present */
5276 	nfserr = nfsd4_encode_bool(xdr, lrp->lrs_present);
5277 	if (nfserr != nfs_ok)
5278 		return nfserr;
5279 	if (lrp->lrs_present)
5280 		/* lrs_stateid */
5281 		return nfsd4_encode_stateid4(xdr, &lrp->lr_sid);
5282 	return nfs_ok;
5283 }
5284 #endif /* CONFIG_NFSD_PNFS */
5285 
5286 static __be32
nfsd4_encode_write_response4(struct xdr_stream * xdr,const struct nfsd4_copy * copy)5287 nfsd4_encode_write_response4(struct xdr_stream *xdr,
5288 			     const struct nfsd4_copy *copy)
5289 {
5290 	const struct nfsd42_write_res *write = &copy->cp_res;
5291 	u32 count = nfsd4_copy_is_sync(copy) ? 0 : 1;
5292 	__be32 status;
5293 
5294 	/* wr_callback_id<1> */
5295 	if (xdr_stream_encode_u32(xdr, count) != XDR_UNIT)
5296 		return nfserr_resource;
5297 	if (count) {
5298 		status = nfsd4_encode_stateid4(xdr, &write->cb_stateid);
5299 		if (status != nfs_ok)
5300 			return status;
5301 	}
5302 
5303 	/* wr_count */
5304 	status = nfsd4_encode_length4(xdr, write->wr_bytes_written);
5305 	if (status != nfs_ok)
5306 		return status;
5307 	/* wr_committed */
5308 	if (xdr_stream_encode_u32(xdr, write->wr_stable_how) != XDR_UNIT)
5309 		return nfserr_resource;
5310 	/* wr_writeverf */
5311 	return nfsd4_encode_verifier4(xdr, &write->wr_verifier);
5312 }
5313 
nfsd4_encode_copy_requirements4(struct xdr_stream * xdr,const struct nfsd4_copy * copy)5314 static __be32 nfsd4_encode_copy_requirements4(struct xdr_stream *xdr,
5315 					      const struct nfsd4_copy *copy)
5316 {
5317 	__be32 status;
5318 
5319 	/* cr_consecutive */
5320 	status = nfsd4_encode_bool(xdr, true);
5321 	if (status != nfs_ok)
5322 		return status;
5323 	/* cr_synchronous */
5324 	return nfsd4_encode_bool(xdr, nfsd4_copy_is_sync(copy));
5325 }
5326 
5327 static __be32
nfsd4_encode_copy(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5328 nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
5329 		  union nfsd4_op_u *u)
5330 {
5331 	struct nfsd4_copy *copy = &u->copy;
5332 
5333 	nfserr = nfsd4_encode_write_response4(resp->xdr, copy);
5334 	if (nfserr != nfs_ok)
5335 		return nfserr;
5336 	return nfsd4_encode_copy_requirements4(resp->xdr, copy);
5337 }
5338 
5339 static __be32
nfsd4_encode_netloc4(struct xdr_stream * xdr,const struct nl4_server * ns)5340 nfsd4_encode_netloc4(struct xdr_stream *xdr, const struct nl4_server *ns)
5341 {
5342 	__be32 status;
5343 
5344 	if (xdr_stream_encode_u32(xdr, ns->nl4_type) != XDR_UNIT)
5345 		return nfserr_resource;
5346 	switch (ns->nl4_type) {
5347 	case NL4_NETADDR:
5348 		/* nl_addr */
5349 		status = nfsd4_encode_netaddr4(xdr, &ns->u.nl4_addr);
5350 		break;
5351 	default:
5352 		status = nfserr_serverfault;
5353 	}
5354 	return status;
5355 }
5356 
5357 static __be32
nfsd4_encode_copy_notify(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5358 nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
5359 			 union nfsd4_op_u *u)
5360 {
5361 	struct nfsd4_copy_notify *cn = &u->copy_notify;
5362 	struct xdr_stream *xdr = resp->xdr;
5363 
5364 	/* cnr_lease_time */
5365 	nfserr = nfsd4_encode_nfstime4(xdr, &cn->cpn_lease_time);
5366 	if (nfserr)
5367 		return nfserr;
5368 	/* cnr_stateid */
5369 	nfserr = nfsd4_encode_stateid4(xdr, &cn->cpn_cnr_stateid);
5370 	if (nfserr)
5371 		return nfserr;
5372 	/* cnr_source_server<> */
5373 	if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5374 		return nfserr_resource;
5375 	return nfsd4_encode_netloc4(xdr, cn->cpn_src);
5376 }
5377 
5378 static __be32
nfsd4_encode_offload_status(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5379 nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
5380 			    union nfsd4_op_u *u)
5381 {
5382 	struct nfsd4_offload_status *os = &u->offload_status;
5383 	struct xdr_stream *xdr = resp->xdr;
5384 
5385 	/* osr_count */
5386 	nfserr = nfsd4_encode_length4(xdr, os->count);
5387 	if (nfserr != nfs_ok)
5388 		return nfserr;
5389 	/* osr_complete<1> */
5390 	if (os->completed) {
5391 		if (xdr_stream_encode_u32(xdr, 1) != XDR_UNIT)
5392 			return nfserr_resource;
5393 		if (xdr_stream_encode_be32(xdr, os->status) != XDR_UNIT)
5394 			return nfserr_resource;
5395 	} else if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
5396 		return nfserr_resource;
5397 	return nfs_ok;
5398 }
5399 
5400 static __be32
nfsd4_encode_read_plus_data(struct nfsd4_compoundres * resp,struct nfsd4_read * read)5401 nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
5402 			    struct nfsd4_read *read)
5403 {
5404 	struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
5405 	struct file *file = read->rd_nf->nf_file;
5406 	struct xdr_stream *xdr = resp->xdr;
5407 	bool splice_ok = argp->splice_ok;
5408 	unsigned int offset_offset;
5409 	__be32 nfserr, wire_count;
5410 	unsigned long maxcount;
5411 	__be64 wire_offset;
5412 
5413 	if (xdr_stream_encode_u32(xdr, NFS4_CONTENT_DATA) != XDR_UNIT)
5414 		return nfserr_io;
5415 
5416 	offset_offset = xdr->buf->len;
5417 
5418 	/* Reserve space for the byte offset and count */
5419 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 3)))
5420 		return nfserr_io;
5421 	xdr_commit_encode(xdr);
5422 
5423 	maxcount = min_t(unsigned long, read->rd_length,
5424 			 (xdr->buf->buflen - xdr->buf->len));
5425 
5426 	if (file->f_op->splice_read && splice_ok)
5427 		nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
5428 	else
5429 		nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
5430 	if (nfserr)
5431 		return nfserr;
5432 
5433 	wire_offset = cpu_to_be64(read->rd_offset);
5434 	write_bytes_to_xdr_buf(xdr->buf, offset_offset, &wire_offset,
5435 			       XDR_UNIT * 2);
5436 	wire_count = cpu_to_be32(read->rd_length);
5437 	write_bytes_to_xdr_buf(xdr->buf, offset_offset + XDR_UNIT * 2,
5438 			       &wire_count, XDR_UNIT);
5439 	return nfs_ok;
5440 }
5441 
5442 static __be32
nfsd4_encode_read_plus(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5443 nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
5444 		       union nfsd4_op_u *u)
5445 {
5446 	struct nfsd4_read *read = &u->read;
5447 	struct file *file = read->rd_nf->nf_file;
5448 	struct xdr_stream *xdr = resp->xdr;
5449 	unsigned int eof_offset;
5450 	__be32 wire_data[2];
5451 	u32 segments = 0;
5452 
5453 	if (nfserr)
5454 		return nfserr;
5455 
5456 	eof_offset = xdr->buf->len;
5457 
5458 	/* Reserve space for the eof flag and segment count */
5459 	if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2)))
5460 		return nfserr_io;
5461 	xdr_commit_encode(xdr);
5462 
5463 	read->rd_eof = read->rd_offset >= i_size_read(file_inode(file));
5464 	if (read->rd_eof)
5465 		goto out;
5466 
5467 	nfserr = nfsd4_encode_read_plus_data(resp, read);
5468 	if (nfserr) {
5469 		xdr_truncate_encode(xdr, eof_offset);
5470 		return nfserr;
5471 	}
5472 
5473 	segments++;
5474 
5475 out:
5476 	wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
5477 	wire_data[1] = cpu_to_be32(segments);
5478 	write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
5479 	return nfserr;
5480 }
5481 
5482 static __be32
nfsd4_encode_seek(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5483 nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
5484 		  union nfsd4_op_u *u)
5485 {
5486 	struct nfsd4_seek *seek = &u->seek;
5487 	struct xdr_stream *xdr = resp->xdr;
5488 
5489 	/* sr_eof */
5490 	nfserr = nfsd4_encode_bool(xdr, seek->seek_eof);
5491 	if (nfserr != nfs_ok)
5492 		return nfserr;
5493 	/* sr_offset */
5494 	return nfsd4_encode_offset4(xdr, seek->seek_pos);
5495 }
5496 
5497 static __be32
nfsd4_encode_noop(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * p)5498 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr,
5499 		  union nfsd4_op_u *p)
5500 {
5501 	return nfserr;
5502 }
5503 
5504 /*
5505  * Encode kmalloc-ed buffer in to XDR stream.
5506  */
5507 static __be32
nfsd4_vbuf_to_stream(struct xdr_stream * xdr,char * buf,u32 buflen)5508 nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
5509 {
5510 	u32 cplen;
5511 	__be32 *p;
5512 
5513 	cplen = min_t(unsigned long, buflen,
5514 		      ((void *)xdr->end - (void *)xdr->p));
5515 	p = xdr_reserve_space(xdr, cplen);
5516 	if (!p)
5517 		return nfserr_resource;
5518 
5519 	memcpy(p, buf, cplen);
5520 	buf += cplen;
5521 	buflen -= cplen;
5522 
5523 	while (buflen) {
5524 		cplen = min_t(u32, buflen, PAGE_SIZE);
5525 		p = xdr_reserve_space(xdr, cplen);
5526 		if (!p)
5527 			return nfserr_resource;
5528 
5529 		memcpy(p, buf, cplen);
5530 
5531 		if (cplen < PAGE_SIZE) {
5532 			/*
5533 			 * We're done, with a length that wasn't page
5534 			 * aligned, so possibly not word aligned. Pad
5535 			 * any trailing bytes with 0.
5536 			 */
5537 			xdr_encode_opaque_fixed(p, NULL, cplen);
5538 			break;
5539 		}
5540 
5541 		buflen -= PAGE_SIZE;
5542 		buf += PAGE_SIZE;
5543 	}
5544 
5545 	return 0;
5546 }
5547 
5548 static __be32
nfsd4_encode_getxattr(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5549 nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5550 		      union nfsd4_op_u *u)
5551 {
5552 	struct nfsd4_getxattr *getxattr = &u->getxattr;
5553 	struct xdr_stream *xdr = resp->xdr;
5554 	__be32 *p, err;
5555 
5556 	p = xdr_reserve_space(xdr, 4);
5557 	if (!p)
5558 		return nfserr_resource;
5559 
5560 	*p = cpu_to_be32(getxattr->getxa_len);
5561 
5562 	if (getxattr->getxa_len == 0)
5563 		return 0;
5564 
5565 	err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
5566 				    getxattr->getxa_len);
5567 
5568 	kvfree(getxattr->getxa_buf);
5569 
5570 	return err;
5571 }
5572 
5573 static __be32
nfsd4_encode_setxattr(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5574 nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5575 		      union nfsd4_op_u *u)
5576 {
5577 	struct nfsd4_setxattr *setxattr = &u->setxattr;
5578 	struct xdr_stream *xdr = resp->xdr;
5579 
5580 	return nfsd4_encode_change_info4(xdr, &setxattr->setxa_cinfo);
5581 }
5582 
5583 /*
5584  * See if there are cookie values that can be rejected outright.
5585  */
5586 static __be32
nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs * listxattrs,u32 * offsetp)5587 nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5588 				u32 *offsetp)
5589 {
5590 	u64 cookie = listxattrs->lsxa_cookie;
5591 
5592 	/*
5593 	 * If the cookie is larger than the maximum number we can fit
5594 	 * in the buffer we just got back from vfs_listxattr, it's invalid.
5595 	 */
5596 	if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5597 		return nfserr_badcookie;
5598 
5599 	*offsetp = (u32)cookie;
5600 	return 0;
5601 }
5602 
5603 static __be32
nfsd4_encode_listxattrs(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5604 nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5605 			union nfsd4_op_u *u)
5606 {
5607 	struct nfsd4_listxattrs *listxattrs = &u->listxattrs;
5608 	struct xdr_stream *xdr = resp->xdr;
5609 	u32 cookie_offset, count_offset, eof;
5610 	u32 left, xdrleft, slen, count;
5611 	u32 xdrlen, offset;
5612 	u64 cookie;
5613 	char *sp;
5614 	__be32 status, tmp;
5615 	__be64 wire_cookie;
5616 	__be32 *p;
5617 	u32 nuser;
5618 
5619 	eof = 1;
5620 
5621 	status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5622 	if (status)
5623 		goto out;
5624 
5625 	/*
5626 	 * Reserve space for the cookie and the name array count. Record
5627 	 * the offsets to save them later.
5628 	 */
5629 	cookie_offset = xdr->buf->len;
5630 	count_offset = cookie_offset + 8;
5631 	p = xdr_reserve_space(xdr, XDR_UNIT * 3);
5632 	if (!p) {
5633 		status = nfserr_resource;
5634 		goto out;
5635 	}
5636 
5637 	count = 0;
5638 	left = listxattrs->lsxa_len;
5639 	sp = listxattrs->lsxa_buf;
5640 	nuser = 0;
5641 
5642 	/* Bytes left is maxcount - 8 (cookie) - 4 (array count) */
5643 	xdrleft = listxattrs->lsxa_maxcount - XDR_UNIT * 3;
5644 
5645 	while (left > 0 && xdrleft > 0) {
5646 		slen = strlen(sp);
5647 
5648 		/*
5649 		 * Check if this is a "user." attribute, skip it if not.
5650 		 */
5651 		if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5652 			goto contloop;
5653 
5654 		slen -= XATTR_USER_PREFIX_LEN;
5655 		xdrlen = 4 + ((slen + 3) & ~3);
5656 		/* Check if both entry and eof can fit in the XDR buffer */
5657 		if (xdrlen + XDR_UNIT > xdrleft) {
5658 			if (count == 0) {
5659 				/*
5660 				 * Can't even fit the first attribute name.
5661 				 */
5662 				status = nfserr_toosmall;
5663 				goto out;
5664 			}
5665 			eof = 0;
5666 			goto wreof;
5667 		}
5668 
5669 		left -= XATTR_USER_PREFIX_LEN;
5670 		sp += XATTR_USER_PREFIX_LEN;
5671 		if (nuser++ < offset)
5672 			goto contloop;
5673 
5674 
5675 		p = xdr_reserve_space(xdr, xdrlen);
5676 		if (!p) {
5677 			status = nfserr_resource;
5678 			goto out;
5679 		}
5680 
5681 		xdr_encode_opaque(p, sp, slen);
5682 
5683 		xdrleft -= xdrlen;
5684 		count++;
5685 contloop:
5686 		sp += slen + 1;
5687 		left -= slen + 1;
5688 	}
5689 
5690 	/*
5691 	 * If there were user attributes to copy, but we didn't copy
5692 	 * any, the offset was too large (e.g. the cookie was invalid).
5693 	 */
5694 	if (nuser > 0 && count == 0) {
5695 		status = nfserr_badcookie;
5696 		goto out;
5697 	}
5698 
5699 wreof:
5700 	p = xdr_reserve_space(xdr, 4);
5701 	if (!p) {
5702 		status = nfserr_resource;
5703 		goto out;
5704 	}
5705 	*p = cpu_to_be32(eof);
5706 
5707 	cookie = offset + count;
5708 
5709 	wire_cookie = cpu_to_be64(cookie);
5710 	write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &wire_cookie, 8);
5711 	tmp = cpu_to_be32(count);
5712 	write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
5713 out:
5714 	if (listxattrs->lsxa_len)
5715 		kvfree(listxattrs->lsxa_buf);
5716 	return status;
5717 }
5718 
5719 static __be32
nfsd4_encode_removexattr(struct nfsd4_compoundres * resp,__be32 nfserr,union nfsd4_op_u * u)5720 nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5721 			 union nfsd4_op_u *u)
5722 {
5723 	struct nfsd4_removexattr *removexattr = &u->removexattr;
5724 	struct xdr_stream *xdr = resp->xdr;
5725 
5726 	return nfsd4_encode_change_info4(xdr, &removexattr->rmxa_cinfo);
5727 }
5728 
5729 typedef __be32(*nfsd4_enc)(struct nfsd4_compoundres *, __be32, union nfsd4_op_u *u);
5730 
5731 /*
5732  * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5733  * since we don't need to filter out obsolete ops as this is
5734  * done in the decoding phase.
5735  */
5736 static const nfsd4_enc nfsd4_enc_ops[] = {
5737 	[OP_ACCESS]		= nfsd4_encode_access,
5738 	[OP_CLOSE]		= nfsd4_encode_close,
5739 	[OP_COMMIT]		= nfsd4_encode_commit,
5740 	[OP_CREATE]		= nfsd4_encode_create,
5741 	[OP_DELEGPURGE]		= nfsd4_encode_noop,
5742 	[OP_DELEGRETURN]	= nfsd4_encode_noop,
5743 	[OP_GETATTR]		= nfsd4_encode_getattr,
5744 	[OP_GETFH]		= nfsd4_encode_getfh,
5745 	[OP_LINK]		= nfsd4_encode_link,
5746 	[OP_LOCK]		= nfsd4_encode_lock,
5747 	[OP_LOCKT]		= nfsd4_encode_lockt,
5748 	[OP_LOCKU]		= nfsd4_encode_locku,
5749 	[OP_LOOKUP]		= nfsd4_encode_noop,
5750 	[OP_LOOKUPP]		= nfsd4_encode_noop,
5751 	[OP_NVERIFY]		= nfsd4_encode_noop,
5752 	[OP_OPEN]		= nfsd4_encode_open,
5753 	[OP_OPENATTR]		= nfsd4_encode_noop,
5754 	[OP_OPEN_CONFIRM]	= nfsd4_encode_open_confirm,
5755 	[OP_OPEN_DOWNGRADE]	= nfsd4_encode_open_downgrade,
5756 	[OP_PUTFH]		= nfsd4_encode_noop,
5757 	[OP_PUTPUBFH]		= nfsd4_encode_noop,
5758 	[OP_PUTROOTFH]		= nfsd4_encode_noop,
5759 	[OP_READ]		= nfsd4_encode_read,
5760 	[OP_READDIR]		= nfsd4_encode_readdir,
5761 	[OP_READLINK]		= nfsd4_encode_readlink,
5762 	[OP_REMOVE]		= nfsd4_encode_remove,
5763 	[OP_RENAME]		= nfsd4_encode_rename,
5764 	[OP_RENEW]		= nfsd4_encode_noop,
5765 	[OP_RESTOREFH]		= nfsd4_encode_noop,
5766 	[OP_SAVEFH]		= nfsd4_encode_noop,
5767 	[OP_SECINFO]		= nfsd4_encode_secinfo,
5768 	[OP_SETATTR]		= nfsd4_encode_setattr,
5769 	[OP_SETCLIENTID]	= nfsd4_encode_setclientid,
5770 	[OP_SETCLIENTID_CONFIRM] = nfsd4_encode_noop,
5771 	[OP_VERIFY]		= nfsd4_encode_noop,
5772 	[OP_WRITE]		= nfsd4_encode_write,
5773 	[OP_RELEASE_LOCKOWNER]	= nfsd4_encode_noop,
5774 
5775 	/* NFSv4.1 operations */
5776 	[OP_BACKCHANNEL_CTL]	= nfsd4_encode_noop,
5777 	[OP_BIND_CONN_TO_SESSION] = nfsd4_encode_bind_conn_to_session,
5778 	[OP_EXCHANGE_ID]	= nfsd4_encode_exchange_id,
5779 	[OP_CREATE_SESSION]	= nfsd4_encode_create_session,
5780 	[OP_DESTROY_SESSION]	= nfsd4_encode_noop,
5781 	[OP_FREE_STATEID]	= nfsd4_encode_noop,
5782 	[OP_GET_DIR_DELEGATION]	= nfsd4_encode_get_dir_delegation,
5783 #ifdef CONFIG_NFSD_PNFS
5784 	[OP_GETDEVICEINFO]	= nfsd4_encode_getdeviceinfo,
5785 	[OP_GETDEVICELIST]	= nfsd4_encode_noop,
5786 	[OP_LAYOUTCOMMIT]	= nfsd4_encode_layoutcommit,
5787 	[OP_LAYOUTGET]		= nfsd4_encode_layoutget,
5788 	[OP_LAYOUTRETURN]	= nfsd4_encode_layoutreturn,
5789 #else
5790 	[OP_GETDEVICEINFO]	= nfsd4_encode_noop,
5791 	[OP_GETDEVICELIST]	= nfsd4_encode_noop,
5792 	[OP_LAYOUTCOMMIT]	= nfsd4_encode_noop,
5793 	[OP_LAYOUTGET]		= nfsd4_encode_noop,
5794 	[OP_LAYOUTRETURN]	= nfsd4_encode_noop,
5795 #endif
5796 	[OP_SECINFO_NO_NAME]	= nfsd4_encode_secinfo_no_name,
5797 	[OP_SEQUENCE]		= nfsd4_encode_sequence,
5798 	[OP_SET_SSV]		= nfsd4_encode_noop,
5799 	[OP_TEST_STATEID]	= nfsd4_encode_test_stateid,
5800 	[OP_WANT_DELEGATION]	= nfsd4_encode_noop,
5801 	[OP_DESTROY_CLIENTID]	= nfsd4_encode_noop,
5802 	[OP_RECLAIM_COMPLETE]	= nfsd4_encode_noop,
5803 
5804 	/* NFSv4.2 operations */
5805 	[OP_ALLOCATE]		= nfsd4_encode_noop,
5806 	[OP_COPY]		= nfsd4_encode_copy,
5807 	[OP_COPY_NOTIFY]	= nfsd4_encode_copy_notify,
5808 	[OP_DEALLOCATE]		= nfsd4_encode_noop,
5809 	[OP_IO_ADVISE]		= nfsd4_encode_noop,
5810 	[OP_LAYOUTERROR]	= nfsd4_encode_noop,
5811 	[OP_LAYOUTSTATS]	= nfsd4_encode_noop,
5812 	[OP_OFFLOAD_CANCEL]	= nfsd4_encode_noop,
5813 	[OP_OFFLOAD_STATUS]	= nfsd4_encode_offload_status,
5814 	[OP_READ_PLUS]		= nfsd4_encode_read_plus,
5815 	[OP_SEEK]		= nfsd4_encode_seek,
5816 	[OP_WRITE_SAME]		= nfsd4_encode_noop,
5817 	[OP_CLONE]		= nfsd4_encode_noop,
5818 
5819 	/* RFC 8276 extended atributes operations */
5820 	[OP_GETXATTR]		= nfsd4_encode_getxattr,
5821 	[OP_SETXATTR]		= nfsd4_encode_setxattr,
5822 	[OP_LISTXATTRS]		= nfsd4_encode_listxattrs,
5823 	[OP_REMOVEXATTR]	= nfsd4_encode_removexattr,
5824 };
5825 
5826 /*
5827  * Calculate whether we still have space to encode repsize bytes.
5828  * There are two considerations:
5829  *     - For NFS versions >=4.1, the size of the reply must stay within
5830  *       session limits
5831  *     - For all NFS versions, we must stay within limited preallocated
5832  *       buffer space.
5833  *
5834  * This is called before the operation is processed, so can only provide
5835  * an upper estimate.  For some nonidempotent operations (such as
5836  * getattr), it's not necessarily a problem if that estimate is wrong,
5837  * as we can fail it after processing without significant side effects.
5838  */
nfsd4_check_resp_size(struct nfsd4_compoundres * resp,u32 respsize)5839 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
5840 {
5841 	struct xdr_buf *buf = &resp->rqstp->rq_res;
5842 	struct nfsd4_slot *slot = resp->cstate.slot;
5843 
5844 	if (buf->len + respsize <= buf->buflen)
5845 		return nfs_ok;
5846 	if (!nfsd4_has_session(&resp->cstate))
5847 		return nfserr_resource;
5848 	if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5849 		WARN_ON_ONCE(1);
5850 		return nfserr_rep_too_big_to_cache;
5851 	}
5852 	return nfserr_rep_too_big;
5853 }
5854 
nfsd4_map_status(__be32 status,u32 minor)5855 static __be32 nfsd4_map_status(__be32 status, u32 minor)
5856 {
5857 	switch (status) {
5858 	case nfs_ok:
5859 		break;
5860 	case nfserr_wrong_type:
5861 		/* RFC 8881 - 15.1.2.9 */
5862 		if (minor == 0)
5863 			status = nfserr_inval;
5864 		break;
5865 	case nfserr_symlink_not_dir:
5866 		status = nfserr_symlink;
5867 		break;
5868 	}
5869 	return status;
5870 }
5871 
5872 void
nfsd4_encode_operation(struct nfsd4_compoundres * resp,struct nfsd4_op * op)5873 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5874 {
5875 	struct xdr_stream *xdr = resp->xdr;
5876 	struct nfs4_stateowner *so = resp->cstate.replay_owner;
5877 	struct svc_rqst *rqstp = resp->rqstp;
5878 	const struct nfsd4_operation *opdesc = op->opdesc;
5879 	unsigned int op_status_offset;
5880 	nfsd4_enc encoder;
5881 
5882 	if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
5883 		goto release;
5884 	op_status_offset = xdr->buf->len;
5885 	if (!xdr_reserve_space(xdr, XDR_UNIT))
5886 		goto release;
5887 
5888 	if (op->opnum == OP_ILLEGAL)
5889 		goto status;
5890 	if (op->status && opdesc &&
5891 			!(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5892 		goto status;
5893 	BUG_ON(op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5894 	       !nfsd4_enc_ops[op->opnum]);
5895 	encoder = nfsd4_enc_ops[op->opnum];
5896 	op->status = encoder(resp, op->status, &op->u);
5897 	if (op->status)
5898 		trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
5899 	xdr_commit_encode(xdr);
5900 
5901 	/* nfsd4_check_resp_size guarantees enough room for error status */
5902 	if (!op->status) {
5903 		int space_needed = 0;
5904 		if (!nfsd4_last_compound_op(rqstp))
5905 			space_needed = COMPOUND_ERR_SLACK_SPACE;
5906 		op->status = nfsd4_check_resp_size(resp, space_needed);
5907 	}
5908 	if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5909 		struct nfsd4_slot *slot = resp->cstate.slot;
5910 
5911 		if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5912 			op->status = nfserr_rep_too_big_to_cache;
5913 		else
5914 			op->status = nfserr_rep_too_big;
5915 	}
5916 	if (op->status == nfserr_resource ||
5917 	    op->status == nfserr_rep_too_big ||
5918 	    op->status == nfserr_rep_too_big_to_cache) {
5919 		/*
5920 		 * The operation may have already been encoded or
5921 		 * partially encoded.  No op returns anything additional
5922 		 * in the case of one of these three errors, so we can
5923 		 * just truncate back to after the status.  But it's a
5924 		 * bug if we had to do this on a non-idempotent op:
5925 		 */
5926 		warn_on_nonidempotent_op(op);
5927 		xdr_truncate_encode(xdr, op_status_offset + XDR_UNIT);
5928 	}
5929 	if (so) {
5930 		int len = xdr->buf->len - (op_status_offset + XDR_UNIT);
5931 
5932 		so->so_replay.rp_status = op->status;
5933 		so->so_replay.rp_buflen = len;
5934 		read_bytes_from_xdr_buf(xdr->buf, op_status_offset + XDR_UNIT,
5935 						so->so_replay.rp_buf, len);
5936 	}
5937 status:
5938 	op->status = nfsd4_map_status(op->status,
5939 				      resp->cstate.minorversion);
5940 	write_bytes_to_xdr_buf(xdr->buf, op_status_offset,
5941 			       &op->status, XDR_UNIT);
5942 release:
5943 	if (opdesc && opdesc->op_release)
5944 		opdesc->op_release(&op->u);
5945 
5946 	/*
5947 	 * Account for pages consumed while encoding this operation.
5948 	 * The xdr_stream primitives don't manage rq_next_page.
5949 	 */
5950 	rqstp->rq_next_page = xdr->page_ptr + 1;
5951 }
5952 
5953 /**
5954  * nfsd4_encode_replay - encode a result stored in the stateowner reply cache
5955  * @xdr: send buffer's XDR stream
5956  * @op: operation being replayed
5957  *
5958  * @op->replay->rp_buf contains the previously-sent already-encoded result.
5959  */
nfsd4_encode_replay(struct xdr_stream * xdr,struct nfsd4_op * op)5960 void nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
5961 {
5962 	struct nfs4_replay *rp = op->replay;
5963 
5964 	trace_nfsd_stateowner_replay(op->opnum, rp);
5965 
5966 	if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
5967 		return;
5968 	if (xdr_stream_encode_be32(xdr, rp->rp_status) != XDR_UNIT)
5969 		return;
5970 	xdr_stream_encode_opaque_fixed(xdr, rp->rp_buf, rp->rp_buflen);
5971 }
5972 
nfsd4_release_compoundargs(struct svc_rqst * rqstp)5973 void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
5974 {
5975 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
5976 
5977 	if (args->ops != args->iops) {
5978 		vfree(args->ops);
5979 		args->ops = args->iops;
5980 	}
5981 	while (args->to_free) {
5982 		struct svcxdr_tmpbuf *tb = args->to_free;
5983 		args->to_free = tb->next;
5984 		kfree(tb);
5985 	}
5986 }
5987 
5988 bool
nfs4svc_decode_compoundargs(struct svc_rqst * rqstp,struct xdr_stream * xdr)5989 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
5990 {
5991 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
5992 
5993 	/* svcxdr_tmp_alloc */
5994 	args->to_free = NULL;
5995 
5996 	args->xdr = xdr;
5997 	args->ops = args->iops;
5998 	args->rqstp = rqstp;
5999 
6000 	return nfsd4_decode_compound(args);
6001 }
6002 
6003 bool
nfs4svc_encode_compoundres(struct svc_rqst * rqstp,struct xdr_stream * xdr)6004 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
6005 {
6006 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
6007 	__be32 *p;
6008 
6009 	/*
6010 	 * Send buffer space for the following items is reserved
6011 	 * at the top of nfsd4_proc_compound().
6012 	 */
6013 	p = resp->statusp;
6014 
6015 	*p++ = resp->cstate.status;
6016 	*p++ = htonl(resp->taglen);
6017 	memcpy(p, resp->tag, resp->taglen);
6018 	p += XDR_QUADLEN(resp->taglen);
6019 	*p++ = htonl(resp->opcnt);
6020 
6021 	nfsd4_sequence_done(resp);
6022 	return true;
6023 }
6024