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