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