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