• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Server-side procedures 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 #include <linux/fs_struct.h>
36 #include <linux/file.h>
37 #include <linux/falloc.h>
38 #include <linux/slab.h>
39 #include <linux/kthread.h>
40 #include <linux/namei.h>
41 
42 #include <linux/sunrpc/addr.h>
43 #include <linux/nfs_ssc.h>
44 
45 #include "idmap.h"
46 #include "cache.h"
47 #include "xdr4.h"
48 #include "vfs.h"
49 #include "current_stateid.h"
50 #include "netns.h"
51 #include "acl.h"
52 #include "pnfs.h"
53 #include "trace.h"
54 
55 static bool inter_copy_offload_enable;
56 module_param(inter_copy_offload_enable, bool, 0644);
57 MODULE_PARM_DESC(inter_copy_offload_enable,
58 		 "Enable inter server to server copy offload. Default: false");
59 
60 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
61 static int nfsd4_ssc_umount_timeout = 900000;		/* default to 15 mins */
62 module_param(nfsd4_ssc_umount_timeout, int, 0644);
63 MODULE_PARM_DESC(nfsd4_ssc_umount_timeout,
64 		"idle msecs before unmount export from source server");
65 #endif
66 
67 #define NFSDDBG_FACILITY		NFSDDBG_PROC
68 
69 static u32 nfsd_attrmask[] = {
70 	NFSD_WRITEABLE_ATTRS_WORD0,
71 	NFSD_WRITEABLE_ATTRS_WORD1,
72 	NFSD_WRITEABLE_ATTRS_WORD2
73 };
74 
75 static u32 nfsd41_ex_attrmask[] = {
76 	NFSD_SUPPATTR_EXCLCREAT_WORD0,
77 	NFSD_SUPPATTR_EXCLCREAT_WORD1,
78 	NFSD_SUPPATTR_EXCLCREAT_WORD2
79 };
80 
81 static __be32
check_attr_support(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,u32 * bmval,u32 * writable)82 check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
83 		   u32 *bmval, u32 *writable)
84 {
85 	struct dentry *dentry = cstate->current_fh.fh_dentry;
86 	struct svc_export *exp = cstate->current_fh.fh_export;
87 
88 	if (!nfsd_attrs_supported(cstate->minorversion, bmval))
89 		return nfserr_attrnotsupp;
90 	if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
91 		return nfserr_attrnotsupp;
92 	if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
93 			!(exp->ex_flags & NFSEXP_SECURITY_LABEL))
94 		return nfserr_attrnotsupp;
95 	if (writable && !bmval_is_subset(bmval, writable))
96 		return nfserr_inval;
97 	if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
98 			(bmval[1] & FATTR4_WORD1_MODE))
99 		return nfserr_inval;
100 	return nfs_ok;
101 }
102 
103 static __be32
nfsd4_check_open_attributes(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_open * open)104 nfsd4_check_open_attributes(struct svc_rqst *rqstp,
105 	struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
106 {
107 	__be32 status = nfs_ok;
108 
109 	if (open->op_create == NFS4_OPEN_CREATE) {
110 		if (open->op_createmode == NFS4_CREATE_UNCHECKED
111 		    || open->op_createmode == NFS4_CREATE_GUARDED)
112 			status = check_attr_support(rqstp, cstate,
113 					open->op_bmval, nfsd_attrmask);
114 		else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
115 			status = check_attr_support(rqstp, cstate,
116 					open->op_bmval, nfsd41_ex_attrmask);
117 	}
118 
119 	return status;
120 }
121 
122 static int
is_create_with_attrs(struct nfsd4_open * open)123 is_create_with_attrs(struct nfsd4_open *open)
124 {
125 	return open->op_create == NFS4_OPEN_CREATE
126 		&& (open->op_createmode == NFS4_CREATE_UNCHECKED
127 		    || open->op_createmode == NFS4_CREATE_GUARDED
128 		    || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
129 }
130 
131 static inline void
fh_dup2(struct svc_fh * dst,struct svc_fh * src)132 fh_dup2(struct svc_fh *dst, struct svc_fh *src)
133 {
134 	fh_put(dst);
135 	dget(src->fh_dentry);
136 	if (src->fh_export)
137 		exp_get(src->fh_export);
138 	*dst = *src;
139 }
140 
141 static __be32
do_open_permission(struct svc_rqst * rqstp,struct svc_fh * current_fh,struct nfsd4_open * open,int accmode)142 do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
143 {
144 
145 	if (open->op_truncate &&
146 		!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
147 		return nfserr_inval;
148 
149 	accmode |= NFSD_MAY_READ_IF_EXEC;
150 
151 	if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
152 		accmode |= NFSD_MAY_READ;
153 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
154 		accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
155 	if (open->op_share_deny & NFS4_SHARE_DENY_READ)
156 		accmode |= NFSD_MAY_WRITE;
157 
158 	return fh_verify(rqstp, current_fh, S_IFREG, accmode);
159 }
160 
nfsd_check_obj_isreg(struct svc_fh * fh,u32 minor_version)161 static __be32 nfsd_check_obj_isreg(struct svc_fh *fh, u32 minor_version)
162 {
163 	umode_t mode = d_inode(fh->fh_dentry)->i_mode;
164 
165 	if (S_ISREG(mode))
166 		return nfs_ok;
167 	if (S_ISDIR(mode))
168 		return nfserr_isdir;
169 	if (S_ISLNK(mode))
170 		return nfserr_symlink;
171 
172 	/* RFC 7530 - 16.16.6 */
173 	if (minor_version == 0)
174 		return nfserr_symlink;
175 	else
176 		return nfserr_wrong_type;
177 
178 }
179 
nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct svc_fh * resfh)180 static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
181 {
182 	if (nfsd4_has_session(cstate))
183 		return;
184 	fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
185 			&resfh->fh_handle);
186 }
187 
nfsd4_create_is_exclusive(int createmode)188 static inline bool nfsd4_create_is_exclusive(int createmode)
189 {
190 	return createmode == NFS4_CREATE_EXCLUSIVE ||
191 		createmode == NFS4_CREATE_EXCLUSIVE4_1;
192 }
193 
194 static __be32
nfsd4_vfs_create(struct svc_fh * fhp,struct dentry * child,struct nfsd4_open * open)195 nfsd4_vfs_create(struct svc_fh *fhp, struct dentry *child,
196 		 struct nfsd4_open *open)
197 {
198 	struct file *filp;
199 	struct path path;
200 	int oflags;
201 
202 	oflags = O_CREAT | O_LARGEFILE;
203 	switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
204 	case NFS4_SHARE_ACCESS_WRITE:
205 		oflags |= O_WRONLY;
206 		break;
207 	case NFS4_SHARE_ACCESS_BOTH:
208 		oflags |= O_RDWR;
209 		break;
210 	default:
211 		oflags |= O_RDONLY;
212 	}
213 
214 	path.mnt = fhp->fh_export->ex_path.mnt;
215 	path.dentry = child;
216 	filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
217 			     current_cred());
218 	if (IS_ERR(filp))
219 		return nfserrno(PTR_ERR(filp));
220 
221 	open->op_filp = filp;
222 	return nfs_ok;
223 }
224 
225 /*
226  * Implement NFSv4's unchecked, guarded, and exclusive create
227  * semantics for regular files. Open state for this new file is
228  * subsequently fabricated in nfsd4_process_open2().
229  *
230  * Upon return, caller must release @fhp and @resfhp.
231  */
232 static __be32
nfsd4_create_file(struct svc_rqst * rqstp,struct svc_fh * fhp,struct svc_fh * resfhp,struct nfsd4_open * open)233 nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
234 		  struct svc_fh *resfhp, struct nfsd4_open *open)
235 {
236 	struct iattr *iap = &open->op_iattr;
237 	struct nfsd_attrs attrs = {
238 		.na_iattr	= iap,
239 		.na_seclabel	= &open->op_label,
240 	};
241 	struct dentry *parent, *child;
242 	__u32 v_mtime, v_atime;
243 	struct inode *inode;
244 	__be32 status;
245 	int host_err;
246 
247 	if (isdotent(open->op_fname, open->op_fnamelen))
248 		return nfserr_exist;
249 	if (!(iap->ia_valid & ATTR_MODE))
250 		iap->ia_mode = 0;
251 
252 	status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
253 	if (status != nfs_ok)
254 		return status;
255 	parent = fhp->fh_dentry;
256 	inode = d_inode(parent);
257 
258 	host_err = fh_want_write(fhp);
259 	if (host_err)
260 		return nfserrno(host_err);
261 
262 	if (is_create_with_attrs(open))
263 		nfsd4_acl_to_attr(NF4REG, open->op_acl, &attrs);
264 
265 	inode_lock_nested(inode, I_MUTEX_PARENT);
266 
267 	child = lookup_one_len(open->op_fname, parent, open->op_fnamelen);
268 	if (IS_ERR(child)) {
269 		status = nfserrno(PTR_ERR(child));
270 		goto out;
271 	}
272 
273 	if (d_really_is_negative(child)) {
274 		status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
275 		if (status != nfs_ok)
276 			goto out;
277 	}
278 
279 	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
280 	if (status != nfs_ok)
281 		goto out;
282 
283 	v_mtime = 0;
284 	v_atime = 0;
285 	if (nfsd4_create_is_exclusive(open->op_createmode)) {
286 		u32 *verifier = (u32 *)open->op_verf.data;
287 
288 		/*
289 		 * Solaris 7 gets confused (bugid 4218508) if these have
290 		 * the high bit set, as do xfs filesystems without the
291 		 * "bigtime" feature. So just clear the high bits. If this
292 		 * is ever changed to use different attrs for storing the
293 		 * verifier, then do_open_lookup() will also need to be
294 		 * fixed accordingly.
295 		 */
296 		v_mtime = verifier[0] & 0x7fffffff;
297 		v_atime = verifier[1] & 0x7fffffff;
298 	}
299 
300 	if (d_really_is_positive(child)) {
301 		/* NFSv4 protocol requires change attributes even though
302 		 * no change happened.
303 		 */
304 		status = fh_fill_both_attrs(fhp);
305 		if (status != nfs_ok)
306 			goto out;
307 
308 		switch (open->op_createmode) {
309 		case NFS4_CREATE_UNCHECKED:
310 			if (!d_is_reg(child))
311 				break;
312 
313 			/*
314 			 * In NFSv4, we don't want to truncate the file
315 			 * now. This would be wrong if the OPEN fails for
316 			 * some other reason. Furthermore, if the size is
317 			 * nonzero, we should ignore it according to spec!
318 			 */
319 			open->op_truncate = (iap->ia_valid & ATTR_SIZE) &&
320 						!iap->ia_size;
321 			break;
322 		case NFS4_CREATE_GUARDED:
323 			status = nfserr_exist;
324 			break;
325 		case NFS4_CREATE_EXCLUSIVE:
326 			if (inode_get_mtime_sec(d_inode(child)) == v_mtime &&
327 			    inode_get_atime_sec(d_inode(child)) == v_atime &&
328 			    d_inode(child)->i_size == 0) {
329 				open->op_created = true;
330 				break;		/* subtle */
331 			}
332 			status = nfserr_exist;
333 			break;
334 		case NFS4_CREATE_EXCLUSIVE4_1:
335 			if (inode_get_mtime_sec(d_inode(child)) == v_mtime &&
336 			    inode_get_atime_sec(d_inode(child)) == v_atime &&
337 			    d_inode(child)->i_size == 0) {
338 				open->op_created = true;
339 				goto set_attr;	/* subtle */
340 			}
341 			status = nfserr_exist;
342 		}
343 		goto out;
344 	}
345 
346 	if (!IS_POSIXACL(inode))
347 		iap->ia_mode &= ~current_umask();
348 
349 	status = fh_fill_pre_attrs(fhp);
350 	if (status != nfs_ok)
351 		goto out;
352 	status = nfsd4_vfs_create(fhp, child, open);
353 	if (status != nfs_ok)
354 		goto out;
355 	open->op_created = true;
356 	fh_fill_post_attrs(fhp);
357 
358 	/* A newly created file already has a file size of zero. */
359 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
360 		iap->ia_valid &= ~ATTR_SIZE;
361 	if (nfsd4_create_is_exclusive(open->op_createmode)) {
362 		iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
363 				ATTR_MTIME_SET|ATTR_ATIME_SET;
364 		iap->ia_mtime.tv_sec = v_mtime;
365 		iap->ia_atime.tv_sec = v_atime;
366 		iap->ia_mtime.tv_nsec = 0;
367 		iap->ia_atime.tv_nsec = 0;
368 	}
369 
370 set_attr:
371 	status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
372 
373 	if (attrs.na_labelerr)
374 		open->op_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
375 	if (attrs.na_aclerr)
376 		open->op_bmval[0] &= ~FATTR4_WORD0_ACL;
377 out:
378 	inode_unlock(inode);
379 	nfsd_attrs_free(&attrs);
380 	if (child && !IS_ERR(child))
381 		dput(child);
382 	fh_drop_write(fhp);
383 	return status;
384 }
385 
386 /**
387  * set_change_info - set up the change_info4 for a reply
388  * @cinfo: pointer to nfsd4_change_info to be populated
389  * @fhp: pointer to svc_fh to use as source
390  *
391  * Many operations in NFSv4 require change_info4 in the reply. This function
392  * populates that from the info that we (should!) have already collected. In
393  * the event that we didn't get any pre-attrs, just zero out both.
394  */
395 static void
set_change_info(struct nfsd4_change_info * cinfo,struct svc_fh * fhp)396 set_change_info(struct nfsd4_change_info *cinfo, struct svc_fh *fhp)
397 {
398 	cinfo->atomic = (u32)(fhp->fh_pre_saved && fhp->fh_post_saved && !fhp->fh_no_atomic_attr);
399 	cinfo->before_change = fhp->fh_pre_change;
400 	cinfo->after_change = fhp->fh_post_change;
401 
402 	/*
403 	 * If fetching the pre-change attributes failed, then we should
404 	 * have already failed the whole operation. We could have still
405 	 * failed to fetch post-change attributes however.
406 	 *
407 	 * If we didn't get post-op attrs, just zero-out the after
408 	 * field since we don't know what it should be. If the pre_saved
409 	 * field isn't set for some reason, throw warning and just copy
410 	 * whatever is in the after field.
411 	 */
412 	if (WARN_ON_ONCE(!fhp->fh_pre_saved))
413 		cinfo->before_change = 0;
414 	if (!fhp->fh_post_saved)
415 		cinfo->after_change = cinfo->before_change + 1;
416 }
417 
418 static __be32
do_open_lookup(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct svc_fh ** resfh)419 do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
420 {
421 	struct svc_fh *current_fh = &cstate->current_fh;
422 	int accmode;
423 	__be32 status;
424 
425 	*resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
426 	if (!*resfh)
427 		return nfserr_jukebox;
428 	fh_init(*resfh, NFS4_FHSIZE);
429 	open->op_truncate = false;
430 
431 	if (open->op_create) {
432 		/* FIXME: check session persistence and pnfs flags.
433 		 * The nfsv4.1 spec requires the following semantics:
434 		 *
435 		 * Persistent   | pNFS   | Server REQUIRED | Client Allowed
436 		 * Reply Cache  | server |                 |
437 		 * -------------+--------+-----------------+--------------------
438 		 * no           | no     | EXCLUSIVE4_1    | EXCLUSIVE4_1
439 		 *              |        |                 | (SHOULD)
440 		 *              |        | and EXCLUSIVE4  | or EXCLUSIVE4
441 		 *              |        |                 | (SHOULD NOT)
442 		 * no           | yes    | EXCLUSIVE4_1    | EXCLUSIVE4_1
443 		 * yes          | no     | GUARDED4        | GUARDED4
444 		 * yes          | yes    | GUARDED4        | GUARDED4
445 		 */
446 
447 		current->fs->umask = open->op_umask;
448 		status = nfsd4_create_file(rqstp, current_fh, *resfh, open);
449 		current->fs->umask = 0;
450 
451 		/*
452 		 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
453 		 * use the returned bitmask to indicate which attributes
454 		 * we used to store the verifier:
455 		 */
456 		if (nfsd4_create_is_exclusive(open->op_createmode) && status == 0)
457 			open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
458 						FATTR4_WORD1_TIME_MODIFY);
459 	} else {
460 		status = nfsd_lookup(rqstp, current_fh,
461 				     open->op_fname, open->op_fnamelen, *resfh);
462 		if (status == nfs_ok)
463 			/* NFSv4 protocol requires change attributes even though
464 			 * no change happened.
465 			 */
466 			status = fh_fill_both_attrs(current_fh);
467 	}
468 	if (status)
469 		goto out;
470 	status = nfsd_check_obj_isreg(*resfh, cstate->minorversion);
471 	if (status)
472 		goto out;
473 
474 	nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
475 	accmode = NFSD_MAY_NOP;
476 	if (open->op_created ||
477 			open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
478 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
479 	status = do_open_permission(rqstp, *resfh, open, accmode);
480 	set_change_info(&open->op_cinfo, current_fh);
481 out:
482 	return status;
483 }
484 
485 static __be32
do_open_fhandle(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_open * open)486 do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
487 {
488 	struct svc_fh *current_fh = &cstate->current_fh;
489 	int accmode = 0;
490 
491 	/* We don't know the target directory, and therefore can not
492 	* set the change info
493 	*/
494 
495 	memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
496 
497 	nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
498 
499 	open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
500 		(open->op_iattr.ia_size == 0);
501 	/*
502 	 * In the delegation case, the client is telling us about an
503 	 * open that it *already* performed locally, some time ago.  We
504 	 * should let it succeed now if possible.
505 	 *
506 	 * In the case of a CLAIM_FH open, on the other hand, the client
507 	 * may be counting on us to enforce permissions (the Linux 4.1
508 	 * client uses this for normal opens, for example).
509 	 */
510 	if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
511 		accmode = NFSD_MAY_OWNER_OVERRIDE;
512 
513 	return do_open_permission(rqstp, current_fh, open, accmode);
514 }
515 
516 static void
copy_clientid(clientid_t * clid,struct nfsd4_session * session)517 copy_clientid(clientid_t *clid, struct nfsd4_session *session)
518 {
519 	struct nfsd4_sessionid *sid =
520 			(struct nfsd4_sessionid *)session->se_sessionid.data;
521 
522 	clid->cl_boot = sid->clientid.cl_boot;
523 	clid->cl_id = sid->clientid.cl_id;
524 }
525 
526 static __be32
nfsd4_open(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)527 nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
528 	   union nfsd4_op_u *u)
529 {
530 	struct nfsd4_open *open = &u->open;
531 	__be32 status;
532 	struct svc_fh *resfh = NULL;
533 	struct net *net = SVC_NET(rqstp);
534 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
535 	bool reclaim = false;
536 
537 	dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
538 		(int)open->op_fnamelen, open->op_fname,
539 		open->op_openowner);
540 
541 	open->op_filp = NULL;
542 	open->op_rqstp = rqstp;
543 
544 	/* This check required by spec. */
545 	if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
546 		return nfserr_inval;
547 
548 	open->op_created = false;
549 	/*
550 	 * RFC5661 18.51.3
551 	 * Before RECLAIM_COMPLETE done, server should deny new lock
552 	 */
553 	if (nfsd4_has_session(cstate) &&
554 	    !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags) &&
555 	    open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
556 		return nfserr_grace;
557 
558 	if (nfsd4_has_session(cstate))
559 		copy_clientid(&open->op_clientid, cstate->session);
560 
561 	/* check seqid for replay. set nfs4_owner */
562 	status = nfsd4_process_open1(cstate, open, nn);
563 	if (status == nfserr_replay_me) {
564 		struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
565 		fh_put(&cstate->current_fh);
566 		fh_copy_shallow(&cstate->current_fh.fh_handle,
567 				&rp->rp_openfh);
568 		status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
569 		if (status)
570 			dprintk("nfsd4_open: replay failed"
571 				" restoring previous filehandle\n");
572 		else
573 			status = nfserr_replay_me;
574 	}
575 	if (status)
576 		goto out;
577 	if (open->op_xdr_error) {
578 		status = open->op_xdr_error;
579 		goto out;
580 	}
581 
582 	status = nfsd4_check_open_attributes(rqstp, cstate, open);
583 	if (status)
584 		goto out;
585 
586 	/* Openowner is now set, so sequence id will get bumped.  Now we need
587 	 * these checks before we do any creates: */
588 	status = nfserr_grace;
589 	if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
590 		goto out;
591 	status = nfserr_no_grace;
592 	if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
593 		goto out;
594 
595 	switch (open->op_claim_type) {
596 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
597 	case NFS4_OPEN_CLAIM_NULL:
598 		status = do_open_lookup(rqstp, cstate, open, &resfh);
599 		if (status)
600 			goto out;
601 		break;
602 	case NFS4_OPEN_CLAIM_PREVIOUS:
603 		status = nfs4_check_open_reclaim(cstate->clp);
604 		if (status)
605 			goto out;
606 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
607 		reclaim = true;
608 		fallthrough;
609 	case NFS4_OPEN_CLAIM_FH:
610 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
611 		status = do_open_fhandle(rqstp, cstate, open);
612 		if (status)
613 			goto out;
614 		resfh = &cstate->current_fh;
615 		break;
616 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
617 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
618 		status = nfserr_notsupp;
619 		goto out;
620 	default:
621 		status = nfserr_inval;
622 		goto out;
623 	}
624 
625 	status = nfsd4_process_open2(rqstp, resfh, open);
626 	if (status && open->op_created)
627 		pr_warn("nfsd4_process_open2 failed to open newly-created file: status=%u\n",
628 			be32_to_cpu(status));
629 	if (reclaim && !status)
630 		nn->somebody_reclaimed = true;
631 out:
632 	if (open->op_filp) {
633 		fput(open->op_filp);
634 		open->op_filp = NULL;
635 	}
636 	if (resfh && resfh != &cstate->current_fh) {
637 		fh_dup2(&cstate->current_fh, resfh);
638 		fh_put(resfh);
639 		kfree(resfh);
640 	}
641 	nfsd4_cleanup_open_state(cstate, open);
642 	nfsd4_bump_seqid(cstate, status);
643 	return status;
644 }
645 
646 /*
647  * OPEN is the only seqid-mutating operation whose decoding can fail
648  * with a seqid-mutating error (specifically, decoding of user names in
649  * the attributes).  Therefore we have to do some processing to look up
650  * the stateowner so that we can bump the seqid.
651  */
nfsd4_open_omfg(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_op * op)652 static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
653 {
654 	struct nfsd4_open *open = &op->u.open;
655 
656 	if (!seqid_mutating_err(ntohl(op->status)))
657 		return op->status;
658 	if (nfsd4_has_session(cstate))
659 		return op->status;
660 	open->op_xdr_error = op->status;
661 	return nfsd4_open(rqstp, cstate, &op->u);
662 }
663 
664 /*
665  * filehandle-manipulating ops.
666  */
667 static __be32
nfsd4_getfh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)668 nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
669 	    union nfsd4_op_u *u)
670 {
671 	u->getfh = &cstate->current_fh;
672 	return nfs_ok;
673 }
674 
675 static __be32
nfsd4_putfh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)676 nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
677 	    union nfsd4_op_u *u)
678 {
679 	struct nfsd4_putfh *putfh = &u->putfh;
680 	__be32 ret;
681 
682 	fh_put(&cstate->current_fh);
683 	cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
684 	memcpy(&cstate->current_fh.fh_handle.fh_raw, putfh->pf_fhval,
685 	       putfh->pf_fhlen);
686 	ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
687 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
688 	if (ret == nfserr_stale && putfh->no_verify) {
689 		SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
690 		ret = 0;
691 	}
692 #endif
693 	return ret;
694 }
695 
696 static __be32
nfsd4_putrootfh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)697 nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
698 		union nfsd4_op_u *u)
699 {
700 	fh_put(&cstate->current_fh);
701 
702 	return exp_pseudoroot(rqstp, &cstate->current_fh);
703 }
704 
705 static __be32
nfsd4_restorefh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)706 nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
707 		union nfsd4_op_u *u)
708 {
709 	if (!cstate->save_fh.fh_dentry)
710 		return nfserr_restorefh;
711 
712 	fh_dup2(&cstate->current_fh, &cstate->save_fh);
713 	if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
714 		memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
715 		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
716 	}
717 	return nfs_ok;
718 }
719 
720 static __be32
nfsd4_savefh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)721 nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
722 	     union nfsd4_op_u *u)
723 {
724 	fh_dup2(&cstate->save_fh, &cstate->current_fh);
725 	if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
726 		memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
727 		SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
728 	}
729 	return nfs_ok;
730 }
731 
732 /*
733  * misc nfsv4 ops
734  */
735 static __be32
nfsd4_access(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)736 nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
737 	     union nfsd4_op_u *u)
738 {
739 	struct nfsd4_access *access = &u->access;
740 	u32 access_full;
741 
742 	access_full = NFS3_ACCESS_FULL;
743 	if (cstate->minorversion >= 2)
744 		access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
745 			       NFS4_ACCESS_XAWRITE;
746 
747 	if (access->ac_req_access & ~access_full)
748 		return nfserr_inval;
749 
750 	access->ac_resp_access = access->ac_req_access;
751 	return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
752 			   &access->ac_supported);
753 }
754 
755 static __be32
nfsd4_commit(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)756 nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
757 	     union nfsd4_op_u *u)
758 {
759 	struct nfsd4_commit *commit = &u->commit;
760 	struct nfsd_file *nf;
761 	__be32 status;
762 
763 	status = nfsd_file_acquire(rqstp, &cstate->current_fh, NFSD_MAY_WRITE |
764 				   NFSD_MAY_NOT_BREAK_LEASE, &nf);
765 	if (status != nfs_ok)
766 		return status;
767 
768 	status = nfsd_commit(rqstp, &cstate->current_fh, nf, commit->co_offset,
769 			     commit->co_count,
770 			     (__be32 *)commit->co_verf.data);
771 	nfsd_file_put(nf);
772 	return status;
773 }
774 
775 static __be32
nfsd4_create(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)776 nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
777 	     union nfsd4_op_u *u)
778 {
779 	struct nfsd4_create *create = &u->create;
780 	struct nfsd_attrs attrs = {
781 		.na_iattr	= &create->cr_iattr,
782 		.na_seclabel	= &create->cr_label,
783 	};
784 	struct svc_fh resfh;
785 	__be32 status;
786 	dev_t rdev;
787 
788 	fh_init(&resfh, NFS4_FHSIZE);
789 
790 	status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
791 	if (status)
792 		return status;
793 
794 	status = check_attr_support(rqstp, cstate, create->cr_bmval,
795 				    nfsd_attrmask);
796 	if (status)
797 		return status;
798 
799 	status = nfsd4_acl_to_attr(create->cr_type, create->cr_acl, &attrs);
800 	current->fs->umask = create->cr_umask;
801 	switch (create->cr_type) {
802 	case NF4LNK:
803 		status = nfsd_symlink(rqstp, &cstate->current_fh,
804 				      create->cr_name, create->cr_namelen,
805 				      create->cr_data, &attrs, &resfh);
806 		break;
807 
808 	case NF4BLK:
809 		status = nfserr_inval;
810 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
811 		if (MAJOR(rdev) != create->cr_specdata1 ||
812 		    MINOR(rdev) != create->cr_specdata2)
813 			goto out_umask;
814 		status = nfsd_create(rqstp, &cstate->current_fh,
815 				     create->cr_name, create->cr_namelen,
816 				     &attrs, S_IFBLK, rdev, &resfh);
817 		break;
818 
819 	case NF4CHR:
820 		status = nfserr_inval;
821 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
822 		if (MAJOR(rdev) != create->cr_specdata1 ||
823 		    MINOR(rdev) != create->cr_specdata2)
824 			goto out_umask;
825 		status = nfsd_create(rqstp, &cstate->current_fh,
826 				     create->cr_name, create->cr_namelen,
827 				     &attrs, S_IFCHR, rdev, &resfh);
828 		break;
829 
830 	case NF4SOCK:
831 		status = nfsd_create(rqstp, &cstate->current_fh,
832 				     create->cr_name, create->cr_namelen,
833 				     &attrs, S_IFSOCK, 0, &resfh);
834 		break;
835 
836 	case NF4FIFO:
837 		status = nfsd_create(rqstp, &cstate->current_fh,
838 				     create->cr_name, create->cr_namelen,
839 				     &attrs, S_IFIFO, 0, &resfh);
840 		break;
841 
842 	case NF4DIR:
843 		create->cr_iattr.ia_valid &= ~ATTR_SIZE;
844 		status = nfsd_create(rqstp, &cstate->current_fh,
845 				     create->cr_name, create->cr_namelen,
846 				     &attrs, S_IFDIR, 0, &resfh);
847 		break;
848 
849 	default:
850 		status = nfserr_badtype;
851 	}
852 
853 	if (status)
854 		goto out;
855 
856 	if (attrs.na_labelerr)
857 		create->cr_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
858 	if (attrs.na_aclerr)
859 		create->cr_bmval[0] &= ~FATTR4_WORD0_ACL;
860 	set_change_info(&create->cr_cinfo, &cstate->current_fh);
861 	fh_dup2(&cstate->current_fh, &resfh);
862 out:
863 	fh_put(&resfh);
864 out_umask:
865 	current->fs->umask = 0;
866 	nfsd_attrs_free(&attrs);
867 	return status;
868 }
869 
870 static __be32
nfsd4_getattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)871 nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
872 	      union nfsd4_op_u *u)
873 {
874 	struct nfsd4_getattr *getattr = &u->getattr;
875 	__be32 status;
876 
877 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
878 	if (status)
879 		return status;
880 
881 	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
882 		return nfserr_inval;
883 
884 	getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
885 	getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
886 	getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
887 
888 	getattr->ga_fhp = &cstate->current_fh;
889 	return nfs_ok;
890 }
891 
892 static __be32
nfsd4_link(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)893 nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
894 	   union nfsd4_op_u *u)
895 {
896 	struct nfsd4_link *link = &u->link;
897 	__be32 status;
898 
899 	status = nfsd_link(rqstp, &cstate->current_fh,
900 			   link->li_name, link->li_namelen, &cstate->save_fh);
901 	if (!status)
902 		set_change_info(&link->li_cinfo, &cstate->current_fh);
903 	return status;
904 }
905 
nfsd4_do_lookupp(struct svc_rqst * rqstp,struct svc_fh * fh)906 static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
907 {
908 	struct svc_fh tmp_fh;
909 	__be32 ret;
910 
911 	fh_init(&tmp_fh, NFS4_FHSIZE);
912 	ret = exp_pseudoroot(rqstp, &tmp_fh);
913 	if (ret)
914 		return ret;
915 	if (tmp_fh.fh_dentry == fh->fh_dentry) {
916 		fh_put(&tmp_fh);
917 		return nfserr_noent;
918 	}
919 	fh_put(&tmp_fh);
920 	return nfsd_lookup(rqstp, fh, "..", 2, fh);
921 }
922 
923 static __be32
nfsd4_lookupp(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)924 nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
925 	      union nfsd4_op_u *u)
926 {
927 	return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
928 }
929 
930 static __be32
nfsd4_lookup(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)931 nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
932 	     union nfsd4_op_u *u)
933 {
934 	return nfsd_lookup(rqstp, &cstate->current_fh,
935 			   u->lookup.lo_name, u->lookup.lo_len,
936 			   &cstate->current_fh);
937 }
938 
939 static __be32
nfsd4_read(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)940 nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
941 	   union nfsd4_op_u *u)
942 {
943 	struct nfsd4_read *read = &u->read;
944 	__be32 status;
945 
946 	read->rd_nf = NULL;
947 
948 	trace_nfsd_read_start(rqstp, &cstate->current_fh,
949 			      read->rd_offset, read->rd_length);
950 
951 	read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp));
952 	if (read->rd_offset > (u64)OFFSET_MAX)
953 		read->rd_offset = (u64)OFFSET_MAX;
954 	if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX)
955 		read->rd_length = (u64)OFFSET_MAX - read->rd_offset;
956 
957 	/*
958 	 * If we do a zero copy read, then a client will see read data
959 	 * that reflects the state of the file *after* performing the
960 	 * following compound.
961 	 *
962 	 * To ensure proper ordering, we therefore turn off zero copy if
963 	 * the client wants us to do more in this compound:
964 	 */
965 	if (!nfsd4_last_compound_op(rqstp)) {
966 		struct nfsd4_compoundargs *argp = rqstp->rq_argp;
967 
968 		argp->splice_ok = false;
969 	}
970 
971 	/* check stateid */
972 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
973 					&read->rd_stateid, RD_STATE,
974 					&read->rd_nf, NULL);
975 
976 	read->rd_rqstp = rqstp;
977 	read->rd_fhp = &cstate->current_fh;
978 	return status;
979 }
980 
981 
982 static void
nfsd4_read_release(union nfsd4_op_u * u)983 nfsd4_read_release(union nfsd4_op_u *u)
984 {
985 	if (u->read.rd_nf)
986 		nfsd_file_put(u->read.rd_nf);
987 	trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
988 			     u->read.rd_offset, u->read.rd_length);
989 }
990 
991 static __be32
nfsd4_readdir(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)992 nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
993 	      union nfsd4_op_u *u)
994 {
995 	struct nfsd4_readdir *readdir = &u->readdir;
996 	u64 cookie = readdir->rd_cookie;
997 	static const nfs4_verifier zeroverf;
998 
999 	/* no need to check permission - this will be done in nfsd_readdir() */
1000 
1001 	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
1002 		return nfserr_inval;
1003 
1004 	readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
1005 	readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
1006 	readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
1007 
1008 	if ((cookie == 1) || (cookie == 2) ||
1009 	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
1010 		return nfserr_bad_cookie;
1011 
1012 	readdir->rd_rqstp = rqstp;
1013 	readdir->rd_fhp = &cstate->current_fh;
1014 	return nfs_ok;
1015 }
1016 
1017 static __be32
nfsd4_readlink(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1018 nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1019 	       union nfsd4_op_u *u)
1020 {
1021 	u->readlink.rl_rqstp = rqstp;
1022 	u->readlink.rl_fhp = &cstate->current_fh;
1023 	return nfs_ok;
1024 }
1025 
1026 static __be32
nfsd4_remove(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1027 nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1028 	     union nfsd4_op_u *u)
1029 {
1030 	struct nfsd4_remove *remove = &u->remove;
1031 	__be32 status;
1032 
1033 	if (opens_in_grace(SVC_NET(rqstp)))
1034 		return nfserr_grace;
1035 	status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
1036 			     remove->rm_name, remove->rm_namelen);
1037 	if (!status)
1038 		set_change_info(&remove->rm_cinfo, &cstate->current_fh);
1039 	return status;
1040 }
1041 
1042 static __be32
nfsd4_rename(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1043 nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1044 	     union nfsd4_op_u *u)
1045 {
1046 	struct nfsd4_rename *rename = &u->rename;
1047 	__be32 status;
1048 
1049 	if (opens_in_grace(SVC_NET(rqstp)))
1050 		return nfserr_grace;
1051 	status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
1052 			     rename->rn_snamelen, &cstate->current_fh,
1053 			     rename->rn_tname, rename->rn_tnamelen);
1054 	if (status)
1055 		return status;
1056 	set_change_info(&rename->rn_sinfo, &cstate->save_fh);
1057 	set_change_info(&rename->rn_tinfo, &cstate->current_fh);
1058 	return nfs_ok;
1059 }
1060 
1061 static __be32
nfsd4_secinfo(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1062 nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1063 	      union nfsd4_op_u *u)
1064 {
1065 	struct nfsd4_secinfo *secinfo = &u->secinfo;
1066 	struct svc_export *exp;
1067 	struct dentry *dentry;
1068 	__be32 err;
1069 
1070 	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
1071 	if (err)
1072 		return err;
1073 	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
1074 				    secinfo->si_name, secinfo->si_namelen,
1075 				    &exp, &dentry);
1076 	if (err)
1077 		return err;
1078 	if (d_really_is_negative(dentry)) {
1079 		exp_put(exp);
1080 		err = nfserr_noent;
1081 	} else
1082 		secinfo->si_exp = exp;
1083 	dput(dentry);
1084 	if (cstate->minorversion)
1085 		/* See rfc 5661 section 2.6.3.1.1.8 */
1086 		fh_put(&cstate->current_fh);
1087 	return err;
1088 }
1089 
1090 static __be32
nfsd4_secinfo_no_name(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1091 nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1092 		union nfsd4_op_u *u)
1093 {
1094 	__be32 err;
1095 
1096 	switch (u->secinfo_no_name.sin_style) {
1097 	case NFS4_SECINFO_STYLE4_CURRENT_FH:
1098 		break;
1099 	case NFS4_SECINFO_STYLE4_PARENT:
1100 		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
1101 		if (err)
1102 			return err;
1103 		break;
1104 	default:
1105 		return nfserr_inval;
1106 	}
1107 
1108 	u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
1109 	fh_put(&cstate->current_fh);
1110 	return nfs_ok;
1111 }
1112 
1113 static void
nfsd4_secinfo_release(union nfsd4_op_u * u)1114 nfsd4_secinfo_release(union nfsd4_op_u *u)
1115 {
1116 	if (u->secinfo.si_exp)
1117 		exp_put(u->secinfo.si_exp);
1118 }
1119 
1120 static void
nfsd4_secinfo_no_name_release(union nfsd4_op_u * u)1121 nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
1122 {
1123 	if (u->secinfo_no_name.sin_exp)
1124 		exp_put(u->secinfo_no_name.sin_exp);
1125 }
1126 
1127 static __be32
nfsd4_setattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1128 nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1129 	      union nfsd4_op_u *u)
1130 {
1131 	struct nfsd4_setattr *setattr = &u->setattr;
1132 	struct nfsd_attrs attrs = {
1133 		.na_iattr	= &setattr->sa_iattr,
1134 		.na_seclabel	= &setattr->sa_label,
1135 	};
1136 	struct inode *inode;
1137 	__be32 status = nfs_ok;
1138 	bool save_no_wcc;
1139 	int err;
1140 
1141 	if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
1142 		status = nfs4_preprocess_stateid_op(rqstp, cstate,
1143 				&cstate->current_fh, &setattr->sa_stateid,
1144 				WR_STATE, NULL, NULL);
1145 		if (status)
1146 			return status;
1147 	}
1148 	err = fh_want_write(&cstate->current_fh);
1149 	if (err)
1150 		return nfserrno(err);
1151 	status = nfs_ok;
1152 
1153 	status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
1154 				    nfsd_attrmask);
1155 	if (status)
1156 		goto out;
1157 
1158 	inode = cstate->current_fh.fh_dentry->d_inode;
1159 	status = nfsd4_acl_to_attr(S_ISDIR(inode->i_mode) ? NF4DIR : NF4REG,
1160 				   setattr->sa_acl, &attrs);
1161 
1162 	if (status)
1163 		goto out;
1164 	save_no_wcc = cstate->current_fh.fh_no_wcc;
1165 	cstate->current_fh.fh_no_wcc = true;
1166 	status = nfsd_setattr(rqstp, &cstate->current_fh, &attrs, NULL);
1167 	cstate->current_fh.fh_no_wcc = save_no_wcc;
1168 	if (!status)
1169 		status = nfserrno(attrs.na_labelerr);
1170 	if (!status)
1171 		status = nfserrno(attrs.na_aclerr);
1172 out:
1173 	nfsd_attrs_free(&attrs);
1174 	fh_drop_write(&cstate->current_fh);
1175 	return status;
1176 }
1177 
1178 static __be32
nfsd4_write(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1179 nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1180 	    union nfsd4_op_u *u)
1181 {
1182 	struct nfsd4_write *write = &u->write;
1183 	stateid_t *stateid = &write->wr_stateid;
1184 	struct nfsd_file *nf = NULL;
1185 	__be32 status = nfs_ok;
1186 	unsigned long cnt;
1187 	int nvecs;
1188 
1189 	if (write->wr_offset > (u64)OFFSET_MAX ||
1190 	    write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX)
1191 		return nfserr_fbig;
1192 
1193 	cnt = write->wr_buflen;
1194 	trace_nfsd_write_start(rqstp, &cstate->current_fh,
1195 			       write->wr_offset, cnt);
1196 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1197 						stateid, WR_STATE, &nf, NULL);
1198 	if (status)
1199 		return status;
1200 
1201 	write->wr_how_written = write->wr_stable_how;
1202 
1203 	nvecs = svc_fill_write_vector(rqstp, &write->wr_payload);
1204 	WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1205 
1206 	status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
1207 				write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
1208 				write->wr_how_written,
1209 				(__be32 *)write->wr_verifier.data);
1210 	nfsd_file_put(nf);
1211 
1212 	write->wr_bytes_written = cnt;
1213 	trace_nfsd_write_done(rqstp, &cstate->current_fh,
1214 			      write->wr_offset, cnt);
1215 	return status;
1216 }
1217 
1218 static __be32
nfsd4_verify_copy(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,stateid_t * src_stateid,struct nfsd_file ** src,stateid_t * dst_stateid,struct nfsd_file ** dst)1219 nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1220 		  stateid_t *src_stateid, struct nfsd_file **src,
1221 		  stateid_t *dst_stateid, struct nfsd_file **dst)
1222 {
1223 	__be32 status;
1224 
1225 	if (!cstate->save_fh.fh_dentry)
1226 		return nfserr_nofilehandle;
1227 
1228 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
1229 					    src_stateid, RD_STATE, src, NULL);
1230 	if (status)
1231 		goto out;
1232 
1233 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1234 					    dst_stateid, WR_STATE, dst, NULL);
1235 	if (status)
1236 		goto out_put_src;
1237 
1238 	/* fix up for NFS-specific error code */
1239 	if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
1240 	    !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
1241 		status = nfserr_wrong_type;
1242 		goto out_put_dst;
1243 	}
1244 
1245 out:
1246 	return status;
1247 out_put_dst:
1248 	nfsd_file_put(*dst);
1249 	*dst = NULL;
1250 out_put_src:
1251 	nfsd_file_put(*src);
1252 	*src = NULL;
1253 	goto out;
1254 }
1255 
1256 static __be32
nfsd4_clone(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1257 nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1258 		union nfsd4_op_u *u)
1259 {
1260 	struct nfsd4_clone *clone = &u->clone;
1261 	struct nfsd_file *src, *dst;
1262 	__be32 status;
1263 
1264 	status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
1265 				   &clone->cl_dst_stateid, &dst);
1266 	if (status)
1267 		goto out;
1268 
1269 	status = nfsd4_clone_file_range(rqstp, src, clone->cl_src_pos,
1270 			dst, clone->cl_dst_pos, clone->cl_count,
1271 			EX_ISSYNC(cstate->current_fh.fh_export));
1272 
1273 	nfsd_file_put(dst);
1274 	nfsd_file_put(src);
1275 out:
1276 	return status;
1277 }
1278 
nfs4_put_copy(struct nfsd4_copy * copy)1279 static void nfs4_put_copy(struct nfsd4_copy *copy)
1280 {
1281 	if (!refcount_dec_and_test(&copy->refcount))
1282 		return;
1283 	atomic_dec(&copy->cp_nn->pending_async_copies);
1284 	kfree(copy->cp_src);
1285 	kfree(copy);
1286 }
1287 
nfsd4_stop_copy(struct nfsd4_copy * copy)1288 static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1289 {
1290 	if (!test_and_set_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags))
1291 		kthread_stop(copy->copy_task);
1292 	nfs4_put_copy(copy);
1293 }
1294 
nfsd4_unhash_copy(struct nfs4_client * clp)1295 static struct nfsd4_copy *nfsd4_unhash_copy(struct nfs4_client *clp)
1296 {
1297 	struct nfsd4_copy *copy = NULL;
1298 
1299 	spin_lock(&clp->async_lock);
1300 	if (!list_empty(&clp->async_copies)) {
1301 		copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1302 					copies);
1303 		refcount_inc(&copy->refcount);
1304 		copy->cp_clp = NULL;
1305 		if (!list_empty(&copy->copies))
1306 			list_del_init(&copy->copies);
1307 	}
1308 	spin_unlock(&clp->async_lock);
1309 	return copy;
1310 }
1311 
nfsd4_shutdown_copy(struct nfs4_client * clp)1312 void nfsd4_shutdown_copy(struct nfs4_client *clp)
1313 {
1314 	struct nfsd4_copy *copy;
1315 
1316 	while ((copy = nfsd4_unhash_copy(clp)) != NULL)
1317 		nfsd4_stop_copy(copy);
1318 }
1319 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
1320 
1321 extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1322 				   struct nfs_fh *src_fh,
1323 				   nfs4_stateid *stateid);
1324 extern void nfs42_ssc_close(struct file *filep);
1325 
1326 extern void nfs_sb_deactive(struct super_block *sb);
1327 
1328 #define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1329 
1330 /*
1331  * setup a work entry in the ssc delayed unmount list.
1332  */
nfsd4_ssc_setup_dul(struct nfsd_net * nn,char * ipaddr,struct nfsd4_ssc_umount_item ** nsui,struct svc_rqst * rqstp)1333 static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
1334 				  struct nfsd4_ssc_umount_item **nsui,
1335 				  struct svc_rqst *rqstp)
1336 {
1337 	struct nfsd4_ssc_umount_item *ni = NULL;
1338 	struct nfsd4_ssc_umount_item *work = NULL;
1339 	struct nfsd4_ssc_umount_item *tmp;
1340 	DEFINE_WAIT(wait);
1341 	__be32 status = 0;
1342 
1343 	*nsui = NULL;
1344 	work = kzalloc(sizeof(*work), GFP_KERNEL);
1345 try_again:
1346 	spin_lock(&nn->nfsd_ssc_lock);
1347 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1348 		if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr)))
1349 			continue;
1350 		/* found a match */
1351 		if (ni->nsui_busy) {
1352 			/*  wait - and try again */
1353 			prepare_to_wait(&nn->nfsd_ssc_waitq, &wait, TASK_IDLE);
1354 			spin_unlock(&nn->nfsd_ssc_lock);
1355 
1356 			/* allow 20secs for mount/unmount for now - revisit */
1357 			if (svc_thread_should_stop(rqstp) ||
1358 					(schedule_timeout(20*HZ) == 0)) {
1359 				finish_wait(&nn->nfsd_ssc_waitq, &wait);
1360 				kfree(work);
1361 				return nfserr_eagain;
1362 			}
1363 			finish_wait(&nn->nfsd_ssc_waitq, &wait);
1364 			goto try_again;
1365 		}
1366 		*nsui = ni;
1367 		refcount_inc(&ni->nsui_refcnt);
1368 		spin_unlock(&nn->nfsd_ssc_lock);
1369 		kfree(work);
1370 
1371 		/* return vfsmount in (*nsui)->nsui_vfsmount */
1372 		return 0;
1373 	}
1374 	if (work) {
1375 		strscpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr) - 1);
1376 		refcount_set(&work->nsui_refcnt, 2);
1377 		work->nsui_busy = true;
1378 		list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list);
1379 		*nsui = work;
1380 	} else
1381 		status = nfserr_resource;
1382 	spin_unlock(&nn->nfsd_ssc_lock);
1383 	return status;
1384 }
1385 
nfsd4_ssc_update_dul(struct nfsd_net * nn,struct nfsd4_ssc_umount_item * nsui,struct vfsmount * ss_mnt)1386 static void nfsd4_ssc_update_dul(struct nfsd_net *nn,
1387 				 struct nfsd4_ssc_umount_item *nsui,
1388 				 struct vfsmount *ss_mnt)
1389 {
1390 	spin_lock(&nn->nfsd_ssc_lock);
1391 	nsui->nsui_vfsmount = ss_mnt;
1392 	nsui->nsui_busy = false;
1393 	wake_up_all(&nn->nfsd_ssc_waitq);
1394 	spin_unlock(&nn->nfsd_ssc_lock);
1395 }
1396 
nfsd4_ssc_cancel_dul(struct nfsd_net * nn,struct nfsd4_ssc_umount_item * nsui)1397 static void nfsd4_ssc_cancel_dul(struct nfsd_net *nn,
1398 				 struct nfsd4_ssc_umount_item *nsui)
1399 {
1400 	spin_lock(&nn->nfsd_ssc_lock);
1401 	list_del(&nsui->nsui_list);
1402 	wake_up_all(&nn->nfsd_ssc_waitq);
1403 	spin_unlock(&nn->nfsd_ssc_lock);
1404 	kfree(nsui);
1405 }
1406 
1407 /*
1408  * Support one copy source server for now.
1409  */
1410 static __be32
nfsd4_interssc_connect(struct nl4_server * nss,struct svc_rqst * rqstp,struct nfsd4_ssc_umount_item ** nsui)1411 nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1412 		       struct nfsd4_ssc_umount_item **nsui)
1413 {
1414 	struct file_system_type *type;
1415 	struct vfsmount *ss_mnt;
1416 	struct nfs42_netaddr *naddr;
1417 	struct sockaddr_storage tmp_addr;
1418 	size_t tmp_addrlen, match_netid_len = 3;
1419 	char *startsep = "", *endsep = "", *match_netid = "tcp";
1420 	char *ipaddr, *dev_name, *raw_data;
1421 	int len, raw_len;
1422 	__be32 status = nfserr_inval;
1423 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1424 
1425 	naddr = &nss->u.nl4_addr;
1426 	tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1427 					 naddr->addr_len,
1428 					 (struct sockaddr *)&tmp_addr,
1429 					 sizeof(tmp_addr));
1430 	*nsui = NULL;
1431 	if (tmp_addrlen == 0)
1432 		goto out_err;
1433 
1434 	if (tmp_addr.ss_family == AF_INET6) {
1435 		startsep = "[";
1436 		endsep = "]";
1437 		match_netid = "tcp6";
1438 		match_netid_len = 4;
1439 	}
1440 
1441 	if (naddr->netid_len != match_netid_len ||
1442 		strncmp(naddr->netid, match_netid, naddr->netid_len))
1443 		goto out_err;
1444 
1445 	/* Construct the raw data for the vfs_kern_mount call */
1446 	len = RPC_MAX_ADDRBUFLEN + 1;
1447 	ipaddr = kzalloc(len, GFP_KERNEL);
1448 	if (!ipaddr)
1449 		goto out_err;
1450 
1451 	rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1452 
1453 	/* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1454 
1455 	raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1456 	raw_data = kzalloc(raw_len, GFP_KERNEL);
1457 	if (!raw_data)
1458 		goto out_free_ipaddr;
1459 
1460 	snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1461 
1462 	status = nfserr_nodev;
1463 	type = get_fs_type("nfs");
1464 	if (!type)
1465 		goto out_free_rawdata;
1466 
1467 	/* Set the server:<export> for the vfs_kern_mount call */
1468 	dev_name = kzalloc(len + 5, GFP_KERNEL);
1469 	if (!dev_name)
1470 		goto out_free_rawdata;
1471 	snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1472 
1473 	status = nfsd4_ssc_setup_dul(nn, ipaddr, nsui, rqstp);
1474 	if (status)
1475 		goto out_free_devname;
1476 	if ((*nsui)->nsui_vfsmount)
1477 		goto out_done;
1478 
1479 	/* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1480 	ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1481 	module_put(type->owner);
1482 	if (IS_ERR(ss_mnt)) {
1483 		status = nfserr_nodev;
1484 		nfsd4_ssc_cancel_dul(nn, *nsui);
1485 		goto out_free_devname;
1486 	}
1487 	nfsd4_ssc_update_dul(nn, *nsui, ss_mnt);
1488 out_done:
1489 	status = 0;
1490 
1491 out_free_devname:
1492 	kfree(dev_name);
1493 out_free_rawdata:
1494 	kfree(raw_data);
1495 out_free_ipaddr:
1496 	kfree(ipaddr);
1497 out_err:
1498 	return status;
1499 }
1500 
1501 /*
1502  * Verify COPY destination stateid.
1503  *
1504  * Connect to the source server with NFSv4.1.
1505  * Create the source struct file for nfsd_copy_range.
1506  * Called with COPY cstate:
1507  *    SAVED_FH: source filehandle
1508  *    CURRENT_FH: destination filehandle
1509  */
1510 static __be32
nfsd4_setup_inter_ssc(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_copy * copy)1511 nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1512 		      struct nfsd4_compound_state *cstate,
1513 		      struct nfsd4_copy *copy)
1514 {
1515 	struct svc_fh *s_fh = NULL;
1516 	stateid_t *s_stid = &copy->cp_src_stateid;
1517 	__be32 status = nfserr_inval;
1518 
1519 	/* Verify the destination stateid and set dst struct file*/
1520 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1521 					    &copy->cp_dst_stateid,
1522 					    WR_STATE, &copy->nf_dst, NULL);
1523 	if (status)
1524 		goto out;
1525 
1526 	status = nfsd4_interssc_connect(copy->cp_src, rqstp, &copy->ss_nsui);
1527 	if (status)
1528 		goto out;
1529 
1530 	s_fh = &cstate->save_fh;
1531 
1532 	copy->c_fh.size = s_fh->fh_handle.fh_size;
1533 	memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size);
1534 	copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
1535 	memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1536 	       sizeof(stateid_opaque_t));
1537 
1538 	status = 0;
1539 out:
1540 	return status;
1541 }
1542 
1543 static void
nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item * nsui,struct file * filp,struct nfsd_file * dst)1544 nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
1545 			struct nfsd_file *dst)
1546 {
1547 	struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id);
1548 	long timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
1549 
1550 	nfs42_ssc_close(filp);
1551 	fput(filp);
1552 
1553 	spin_lock(&nn->nfsd_ssc_lock);
1554 	list_del(&nsui->nsui_list);
1555 	/*
1556 	 * vfsmount can be shared by multiple exports,
1557 	 * decrement refcnt. If the count drops to 1 it
1558 	 * will be unmounted when nsui_expire expires.
1559 	 */
1560 	refcount_dec(&nsui->nsui_refcnt);
1561 	nsui->nsui_expire = jiffies + timeout;
1562 	list_add_tail(&nsui->nsui_list, &nn->nfsd_ssc_mount_list);
1563 	spin_unlock(&nn->nfsd_ssc_lock);
1564 }
1565 
1566 #else /* CONFIG_NFSD_V4_2_INTER_SSC */
1567 
1568 static __be32
nfsd4_setup_inter_ssc(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_copy * copy)1569 nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1570 		      struct nfsd4_compound_state *cstate,
1571 		      struct nfsd4_copy *copy)
1572 {
1573 	return nfserr_inval;
1574 }
1575 
1576 static void
nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item * nsui,struct file * filp,struct nfsd_file * dst)1577 nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
1578 			struct nfsd_file *dst)
1579 {
1580 }
1581 
nfs42_ssc_open(struct vfsmount * ss_mnt,struct nfs_fh * src_fh,nfs4_stateid * stateid)1582 static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1583 				   struct nfs_fh *src_fh,
1584 				   nfs4_stateid *stateid)
1585 {
1586 	return NULL;
1587 }
1588 #endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1589 
1590 static __be32
nfsd4_setup_intra_ssc(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_copy * copy)1591 nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1592 		      struct nfsd4_compound_state *cstate,
1593 		      struct nfsd4_copy *copy)
1594 {
1595 	return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1596 				 &copy->nf_src, &copy->cp_dst_stateid,
1597 				 &copy->nf_dst);
1598 }
1599 
nfsd4_cb_offload_release(struct nfsd4_callback * cb)1600 static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1601 {
1602 	struct nfsd4_cb_offload *cbo =
1603 		container_of(cb, struct nfsd4_cb_offload, co_cb);
1604 
1605 	kfree(cbo);
1606 }
1607 
nfsd4_cb_offload_done(struct nfsd4_callback * cb,struct rpc_task * task)1608 static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1609 				 struct rpc_task *task)
1610 {
1611 	struct nfsd4_cb_offload *cbo =
1612 		container_of(cb, struct nfsd4_cb_offload, co_cb);
1613 
1614 	trace_nfsd_cb_offload_done(&cbo->co_res.cb_stateid, task);
1615 	return 1;
1616 }
1617 
1618 static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1619 	.release = nfsd4_cb_offload_release,
1620 	.done = nfsd4_cb_offload_done,
1621 	.opcode = OP_CB_OFFLOAD,
1622 };
1623 
nfsd4_init_copy_res(struct nfsd4_copy * copy,bool sync)1624 static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1625 {
1626 	copy->cp_res.wr_stable_how =
1627 		test_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags) ?
1628 			NFS_FILE_SYNC : NFS_UNSTABLE;
1629 	nfsd4_copy_set_sync(copy, sync);
1630 }
1631 
_nfsd_copy_file_range(struct nfsd4_copy * copy,struct file * dst,struct file * src)1632 static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy,
1633 				     struct file *dst,
1634 				     struct file *src)
1635 {
1636 	errseq_t since;
1637 	ssize_t bytes_copied = 0;
1638 	u64 bytes_total = copy->cp_count;
1639 	u64 src_pos = copy->cp_src_pos;
1640 	u64 dst_pos = copy->cp_dst_pos;
1641 	int status;
1642 	loff_t end;
1643 
1644 	/* See RFC 7862 p.67: */
1645 	if (bytes_total == 0)
1646 		bytes_total = ULLONG_MAX;
1647 	do {
1648 		/* Only async copies can be stopped here */
1649 		if (kthread_should_stop())
1650 			break;
1651 		bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos,
1652 						    bytes_total);
1653 		if (bytes_copied <= 0)
1654 			break;
1655 		bytes_total -= bytes_copied;
1656 		copy->cp_res.wr_bytes_written += bytes_copied;
1657 		src_pos += bytes_copied;
1658 		dst_pos += bytes_copied;
1659 	} while (bytes_total > 0 && nfsd4_copy_is_async(copy));
1660 	/* for a non-zero asynchronous copy do a commit of data */
1661 	if (nfsd4_copy_is_async(copy) && copy->cp_res.wr_bytes_written > 0) {
1662 		since = READ_ONCE(dst->f_wb_err);
1663 		end = copy->cp_dst_pos + copy->cp_res.wr_bytes_written - 1;
1664 		status = vfs_fsync_range(dst, copy->cp_dst_pos, end, 0);
1665 		if (!status)
1666 			status = filemap_check_wb_err(dst->f_mapping, since);
1667 		if (!status)
1668 			set_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags);
1669 	}
1670 	return bytes_copied;
1671 }
1672 
nfsd4_do_copy(struct nfsd4_copy * copy,struct file * src,struct file * dst,bool sync)1673 static __be32 nfsd4_do_copy(struct nfsd4_copy *copy,
1674 			    struct file *src, struct file *dst,
1675 			    bool sync)
1676 {
1677 	__be32 status;
1678 	ssize_t bytes;
1679 
1680 	bytes = _nfsd_copy_file_range(copy, dst, src);
1681 
1682 	/* for async copy, we ignore the error, client can always retry
1683 	 * to get the error
1684 	 */
1685 	if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1686 		status = nfserrno(bytes);
1687 	else {
1688 		nfsd4_init_copy_res(copy, sync);
1689 		status = nfs_ok;
1690 	}
1691 	return status;
1692 }
1693 
dup_copy_fields(struct nfsd4_copy * src,struct nfsd4_copy * dst)1694 static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
1695 {
1696 	dst->cp_src_pos = src->cp_src_pos;
1697 	dst->cp_dst_pos = src->cp_dst_pos;
1698 	dst->cp_count = src->cp_count;
1699 	dst->cp_flags = src->cp_flags;
1700 	memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1701 	memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1702 	dst->cp_clp = src->cp_clp;
1703 	dst->nf_dst = nfsd_file_get(src->nf_dst);
1704 	/* for inter, nf_src doesn't exist yet */
1705 	if (!nfsd4_ssc_is_inter(src))
1706 		dst->nf_src = nfsd_file_get(src->nf_src);
1707 
1708 	memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
1709 	memcpy(dst->cp_src, src->cp_src, sizeof(struct nl4_server));
1710 	memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1711 	memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1712 	dst->ss_nsui = src->ss_nsui;
1713 }
1714 
release_copy_files(struct nfsd4_copy * copy)1715 static void release_copy_files(struct nfsd4_copy *copy)
1716 {
1717 	if (copy->nf_src)
1718 		nfsd_file_put(copy->nf_src);
1719 	if (copy->nf_dst)
1720 		nfsd_file_put(copy->nf_dst);
1721 }
1722 
cleanup_async_copy(struct nfsd4_copy * copy)1723 static void cleanup_async_copy(struct nfsd4_copy *copy)
1724 {
1725 	nfs4_free_copy_state(copy);
1726 	release_copy_files(copy);
1727 	if (copy->cp_clp) {
1728 		spin_lock(&copy->cp_clp->async_lock);
1729 		if (!list_empty(&copy->copies))
1730 			list_del_init(&copy->copies);
1731 		spin_unlock(&copy->cp_clp->async_lock);
1732 	}
1733 	nfs4_put_copy(copy);
1734 }
1735 
nfsd4_send_cb_offload(struct nfsd4_copy * copy)1736 static void nfsd4_send_cb_offload(struct nfsd4_copy *copy)
1737 {
1738 	struct nfsd4_cb_offload *cbo;
1739 
1740 	cbo = kzalloc(sizeof(*cbo), GFP_KERNEL);
1741 	if (!cbo)
1742 		return;
1743 
1744 	memcpy(&cbo->co_res, &copy->cp_res, sizeof(copy->cp_res));
1745 	memcpy(&cbo->co_fh, &copy->fh, sizeof(copy->fh));
1746 	cbo->co_nfserr = copy->nfserr;
1747 
1748 	nfsd4_init_cb(&cbo->co_cb, copy->cp_clp, &nfsd4_cb_offload_ops,
1749 		      NFSPROC4_CLNT_CB_OFFLOAD);
1750 	trace_nfsd_cb_offload(copy->cp_clp, &cbo->co_res.cb_stateid,
1751 			      &cbo->co_fh, copy->cp_count, copy->nfserr);
1752 	nfsd4_run_cb(&cbo->co_cb);
1753 }
1754 
1755 /**
1756  * nfsd4_do_async_copy - kthread function for background server-side COPY
1757  * @data: arguments for COPY operation
1758  *
1759  * Return values:
1760  *   %0: Copy operation is done.
1761  */
nfsd4_do_async_copy(void * data)1762 static int nfsd4_do_async_copy(void *data)
1763 {
1764 	struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1765 
1766 	trace_nfsd_copy_async(copy);
1767 	if (nfsd4_ssc_is_inter(copy)) {
1768 		struct file *filp;
1769 
1770 		filp = nfs42_ssc_open(copy->ss_nsui->nsui_vfsmount,
1771 				      &copy->c_fh, &copy->stateid);
1772 		if (IS_ERR(filp)) {
1773 			switch (PTR_ERR(filp)) {
1774 			case -EBADF:
1775 				copy->nfserr = nfserr_wrong_type;
1776 				break;
1777 			default:
1778 				copy->nfserr = nfserr_offload_denied;
1779 			}
1780 			/* ss_mnt will be unmounted by the laundromat */
1781 			goto do_callback;
1782 		}
1783 		copy->nfserr = nfsd4_do_copy(copy, filp, copy->nf_dst->nf_file,
1784 					     false);
1785 		nfsd4_cleanup_inter_ssc(copy->ss_nsui, filp, copy->nf_dst);
1786 	} else {
1787 		copy->nfserr = nfsd4_do_copy(copy, copy->nf_src->nf_file,
1788 					     copy->nf_dst->nf_file, false);
1789 	}
1790 
1791 do_callback:
1792 	set_bit(NFSD4_COPY_F_COMPLETED, &copy->cp_flags);
1793 	trace_nfsd_copy_async_done(copy);
1794 	nfsd4_send_cb_offload(copy);
1795 	cleanup_async_copy(copy);
1796 	return 0;
1797 }
1798 
1799 static __be32
nfsd4_copy(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1800 nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1801 		union nfsd4_op_u *u)
1802 {
1803 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1804 	struct nfsd4_copy *async_copy = NULL;
1805 	struct nfsd4_copy *copy = &u->copy;
1806 	struct nfsd42_write_res *result;
1807 	__be32 status;
1808 
1809 	/*
1810 	 * Currently, async COPY is not reliable. Force all COPY
1811 	 * requests to be synchronous to avoid client application
1812 	 * hangs waiting for COPY completion.
1813 	 */
1814 	nfsd4_copy_set_sync(copy, true);
1815 
1816 	result = &copy->cp_res;
1817 	nfsd_copy_write_verifier((__be32 *)&result->wr_verifier.data, nn);
1818 
1819 	copy->cp_clp = cstate->clp;
1820 	if (nfsd4_ssc_is_inter(copy)) {
1821 		trace_nfsd_copy_inter(copy);
1822 		if (!inter_copy_offload_enable || nfsd4_copy_is_sync(copy)) {
1823 			status = nfserr_notsupp;
1824 			goto out;
1825 		}
1826 		status = nfsd4_setup_inter_ssc(rqstp, cstate, copy);
1827 		if (status) {
1828 			trace_nfsd_copy_done(copy, status);
1829 			return nfserr_offload_denied;
1830 		}
1831 	} else {
1832 		trace_nfsd_copy_intra(copy);
1833 		status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1834 		if (status) {
1835 			trace_nfsd_copy_done(copy, status);
1836 			return status;
1837 		}
1838 	}
1839 
1840 	memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1841 		sizeof(struct knfsd_fh));
1842 	if (nfsd4_copy_is_async(copy)) {
1843 		async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1844 		if (!async_copy)
1845 			goto out_err;
1846 		async_copy->cp_nn = nn;
1847 		INIT_LIST_HEAD(&async_copy->copies);
1848 		refcount_set(&async_copy->refcount, 1);
1849 		/* Arbitrary cap on number of pending async copy operations */
1850 		if (atomic_inc_return(&nn->pending_async_copies) >
1851 				(int)rqstp->rq_pool->sp_nrthreads)
1852 			goto out_err;
1853 		async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL);
1854 		if (!async_copy->cp_src)
1855 			goto out_err;
1856 		if (!nfs4_init_copy_state(nn, copy))
1857 			goto out_err;
1858 		memcpy(&result->cb_stateid, &copy->cp_stateid.cs_stid,
1859 			sizeof(result->cb_stateid));
1860 		dup_copy_fields(copy, async_copy);
1861 		async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1862 				async_copy, "%s", "copy thread");
1863 		if (IS_ERR(async_copy->copy_task))
1864 			goto out_err;
1865 		spin_lock(&async_copy->cp_clp->async_lock);
1866 		list_add(&async_copy->copies,
1867 				&async_copy->cp_clp->async_copies);
1868 		spin_unlock(&async_copy->cp_clp->async_lock);
1869 		wake_up_process(async_copy->copy_task);
1870 		status = nfs_ok;
1871 	} else {
1872 		status = nfsd4_do_copy(copy, copy->nf_src->nf_file,
1873 				       copy->nf_dst->nf_file, true);
1874 	}
1875 out:
1876 	trace_nfsd_copy_done(copy, status);
1877 	release_copy_files(copy);
1878 	return status;
1879 out_err:
1880 	if (nfsd4_ssc_is_inter(copy)) {
1881 		/*
1882 		 * Source's vfsmount of inter-copy will be unmounted
1883 		 * by the laundromat. Use copy instead of async_copy
1884 		 * since async_copy->ss_nsui might not be set yet.
1885 		 */
1886 		refcount_dec(&copy->ss_nsui->nsui_refcnt);
1887 	}
1888 	if (async_copy)
1889 		cleanup_async_copy(async_copy);
1890 	status = nfserr_jukebox;
1891 	goto out;
1892 }
1893 
1894 static struct nfsd4_copy *
find_async_copy_locked(struct nfs4_client * clp,stateid_t * stateid)1895 find_async_copy_locked(struct nfs4_client *clp, stateid_t *stateid)
1896 {
1897 	struct nfsd4_copy *copy;
1898 
1899 	lockdep_assert_held(&clp->async_lock);
1900 
1901 	list_for_each_entry(copy, &clp->async_copies, copies) {
1902 		if (memcmp(&copy->cp_stateid.cs_stid, stateid, NFS4_STATEID_SIZE))
1903 			continue;
1904 		return copy;
1905 	}
1906 	return NULL;
1907 }
1908 
1909 static struct nfsd4_copy *
find_async_copy(struct nfs4_client * clp,stateid_t * stateid)1910 find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1911 {
1912 	struct nfsd4_copy *copy;
1913 
1914 	spin_lock(&clp->async_lock);
1915 	copy = find_async_copy_locked(clp, stateid);
1916 	if (copy)
1917 		refcount_inc(&copy->refcount);
1918 	spin_unlock(&clp->async_lock);
1919 	return copy;
1920 }
1921 
1922 static __be32
nfsd4_offload_cancel(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1923 nfsd4_offload_cancel(struct svc_rqst *rqstp,
1924 		     struct nfsd4_compound_state *cstate,
1925 		     union nfsd4_op_u *u)
1926 {
1927 	struct nfsd4_offload_status *os = &u->offload_status;
1928 	struct nfsd4_copy *copy;
1929 	struct nfs4_client *clp = cstate->clp;
1930 
1931 	copy = find_async_copy(clp, &os->stateid);
1932 	if (!copy) {
1933 		struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1934 
1935 		return manage_cpntf_state(nn, &os->stateid, clp, NULL);
1936 	} else
1937 		nfsd4_stop_copy(copy);
1938 
1939 	return nfs_ok;
1940 }
1941 
1942 static __be32
nfsd4_copy_notify(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1943 nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1944 		  union nfsd4_op_u *u)
1945 {
1946 	struct nfsd4_copy_notify *cn = &u->copy_notify;
1947 	__be32 status;
1948 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1949 	struct nfs4_stid *stid = NULL;
1950 	struct nfs4_cpntf_state *cps;
1951 	struct nfs4_client *clp = cstate->clp;
1952 
1953 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1954 					&cn->cpn_src_stateid, RD_STATE, NULL,
1955 					&stid);
1956 	if (status)
1957 		return status;
1958 	if (!stid)
1959 		return nfserr_bad_stateid;
1960 
1961 	cn->cpn_lease_time.tv_sec = nn->nfsd4_lease;
1962 	cn->cpn_lease_time.tv_nsec = 0;
1963 
1964 	status = nfserrno(-ENOMEM);
1965 	cps = nfs4_alloc_init_cpntf_state(nn, stid);
1966 	if (!cps)
1967 		goto out;
1968 	memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.cs_stid, sizeof(stateid_t));
1969 	memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
1970 	memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
1971 
1972 	/* For now, only return one server address in cpn_src, the
1973 	 * address used by the client to connect to this server.
1974 	 */
1975 	cn->cpn_src->nl4_type = NL4_NETADDR;
1976 	status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
1977 				 &cn->cpn_src->u.nl4_addr);
1978 	WARN_ON_ONCE(status);
1979 	if (status) {
1980 		nfs4_put_cpntf_state(nn, cps);
1981 		goto out;
1982 	}
1983 out:
1984 	nfs4_put_stid(stid);
1985 	return status;
1986 }
1987 
1988 static __be32
nfsd4_fallocate(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_fallocate * fallocate,int flags)1989 nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1990 		struct nfsd4_fallocate *fallocate, int flags)
1991 {
1992 	__be32 status;
1993 	struct nfsd_file *nf;
1994 
1995 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1996 					    &fallocate->falloc_stateid,
1997 					    WR_STATE, &nf, NULL);
1998 	if (status != nfs_ok)
1999 		return status;
2000 
2001 	status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
2002 				     fallocate->falloc_offset,
2003 				     fallocate->falloc_length,
2004 				     flags);
2005 	nfsd_file_put(nf);
2006 	return status;
2007 }
2008 
2009 static __be32
nfsd4_offload_status(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2010 nfsd4_offload_status(struct svc_rqst *rqstp,
2011 		     struct nfsd4_compound_state *cstate,
2012 		     union nfsd4_op_u *u)
2013 {
2014 	struct nfsd4_offload_status *os = &u->offload_status;
2015 	__be32 status = nfs_ok;
2016 	struct nfsd4_copy *copy;
2017 	struct nfs4_client *clp = cstate->clp;
2018 
2019 	os->completed = false;
2020 	spin_lock(&clp->async_lock);
2021 	copy = find_async_copy_locked(clp, &os->stateid);
2022 	if (copy) {
2023 		os->count = copy->cp_res.wr_bytes_written;
2024 		if (test_bit(NFSD4_COPY_F_COMPLETED, &copy->cp_flags)) {
2025 			os->completed = true;
2026 			os->status = copy->nfserr;
2027 		}
2028 	} else
2029 		status = nfserr_bad_stateid;
2030 	spin_unlock(&clp->async_lock);
2031 
2032 	return status;
2033 }
2034 
2035 static __be32
nfsd4_allocate(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2036 nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2037 	       union nfsd4_op_u *u)
2038 {
2039 	return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
2040 }
2041 
2042 static __be32
nfsd4_deallocate(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2043 nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2044 		 union nfsd4_op_u *u)
2045 {
2046 	return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
2047 			       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2048 }
2049 
2050 static __be32
nfsd4_seek(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2051 nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2052 	   union nfsd4_op_u *u)
2053 {
2054 	struct nfsd4_seek *seek = &u->seek;
2055 	int whence;
2056 	__be32 status;
2057 	struct nfsd_file *nf;
2058 
2059 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
2060 					    &seek->seek_stateid,
2061 					    RD_STATE, &nf, NULL);
2062 	if (status)
2063 		return status;
2064 
2065 	switch (seek->seek_whence) {
2066 	case NFS4_CONTENT_DATA:
2067 		whence = SEEK_DATA;
2068 		break;
2069 	case NFS4_CONTENT_HOLE:
2070 		whence = SEEK_HOLE;
2071 		break;
2072 	default:
2073 		status = nfserr_union_notsupp;
2074 		goto out;
2075 	}
2076 
2077 	/*
2078 	 * Note:  This call does change file->f_pos, but nothing in NFSD
2079 	 *        should ever file->f_pos.
2080 	 */
2081 	seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
2082 	if (seek->seek_pos < 0)
2083 		status = nfserrno(seek->seek_pos);
2084 	else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
2085 		seek->seek_eof = true;
2086 
2087 out:
2088 	nfsd_file_put(nf);
2089 	return status;
2090 }
2091 
2092 /* This routine never returns NFS_OK!  If there are no other errors, it
2093  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
2094  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
2095  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
2096  */
2097 static __be32
_nfsd4_verify(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_verify * verify)2098 _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2099 	     struct nfsd4_verify *verify)
2100 {
2101 	__be32 *buf, *p;
2102 	int count;
2103 	__be32 status;
2104 
2105 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
2106 	if (status)
2107 		return status;
2108 
2109 	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
2110 	if (status)
2111 		return status;
2112 
2113 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
2114 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
2115 		return nfserr_inval;
2116 	if (verify->ve_attrlen & 3)
2117 		return nfserr_inval;
2118 
2119 	/* count in words:
2120 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
2121 	 */
2122 	count = 4 + (verify->ve_attrlen >> 2);
2123 	buf = kmalloc(count << 2, GFP_KERNEL);
2124 	if (!buf)
2125 		return nfserr_jukebox;
2126 
2127 	p = buf;
2128 	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
2129 				    cstate->current_fh.fh_export,
2130 				    cstate->current_fh.fh_dentry,
2131 				    verify->ve_bmval,
2132 				    rqstp, 0);
2133 	/*
2134 	 * If nfsd4_encode_fattr() ran out of space, assume that's because
2135 	 * the attributes are longer (hence different) than those given:
2136 	 */
2137 	if (status == nfserr_resource)
2138 		status = nfserr_not_same;
2139 	if (status)
2140 		goto out_kfree;
2141 
2142 	/* skip bitmap */
2143 	p = buf + 1 + ntohl(buf[0]);
2144 	status = nfserr_not_same;
2145 	if (ntohl(*p++) != verify->ve_attrlen)
2146 		goto out_kfree;
2147 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
2148 		status = nfserr_same;
2149 
2150 out_kfree:
2151 	kfree(buf);
2152 	return status;
2153 }
2154 
2155 static __be32
nfsd4_nverify(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2156 nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2157 	      union nfsd4_op_u *u)
2158 {
2159 	__be32 status;
2160 
2161 	status = _nfsd4_verify(rqstp, cstate, &u->verify);
2162 	return status == nfserr_not_same ? nfs_ok : status;
2163 }
2164 
2165 static __be32
nfsd4_verify(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2166 nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2167 	     union nfsd4_op_u *u)
2168 {
2169 	__be32 status;
2170 
2171 	status = _nfsd4_verify(rqstp, cstate, &u->nverify);
2172 	return status == nfserr_same ? nfs_ok : status;
2173 }
2174 
2175 static __be32
nfsd4_get_dir_delegation(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2176 nfsd4_get_dir_delegation(struct svc_rqst *rqstp,
2177 			 struct nfsd4_compound_state *cstate,
2178 			 union nfsd4_op_u *u)
2179 {
2180 	struct nfsd4_get_dir_delegation *gdd = &u->get_dir_delegation;
2181 
2182 	/*
2183 	 * RFC 8881, section 18.39.3 says:
2184 	 *
2185 	 * "The server may refuse to grant the delegation. In that case, the
2186 	 *  server will return NFS4ERR_DIRDELEG_UNAVAIL."
2187 	 *
2188 	 * This is sub-optimal, since it means that the server would need to
2189 	 * abort compound processing just because the delegation wasn't
2190 	 * available. RFC8881bis should change this to allow the server to
2191 	 * return NFS4_OK with a non-fatal status of GDD4_UNAVAIL in this
2192 	 * situation.
2193 	 */
2194 	gdd->gddrnf_status = GDD4_UNAVAIL;
2195 	return nfs_ok;
2196 }
2197 
2198 #ifdef CONFIG_NFSD_PNFS
2199 static const struct nfsd4_layout_ops *
nfsd4_layout_verify(struct svc_export * exp,unsigned int layout_type)2200 nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
2201 {
2202 	if (!exp->ex_layout_types) {
2203 		dprintk("%s: export does not support pNFS\n", __func__);
2204 		return NULL;
2205 	}
2206 
2207 	if (layout_type >= LAYOUT_TYPE_MAX ||
2208 	    !(exp->ex_layout_types & (1 << layout_type))) {
2209 		dprintk("%s: layout type %d not supported\n",
2210 			__func__, layout_type);
2211 		return NULL;
2212 	}
2213 
2214 	return nfsd4_layout_ops[layout_type];
2215 }
2216 
2217 static __be32
nfsd4_getdeviceinfo(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2218 nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
2219 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2220 {
2221 	struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
2222 	const struct nfsd4_layout_ops *ops;
2223 	struct nfsd4_deviceid_map *map;
2224 	struct svc_export *exp;
2225 	__be32 nfserr;
2226 
2227 	dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
2228 	       __func__,
2229 	       gdp->gd_layout_type,
2230 	       gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
2231 	       gdp->gd_maxcount);
2232 
2233 	map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
2234 	if (!map) {
2235 		dprintk("%s: couldn't find device ID to export mapping!\n",
2236 			__func__);
2237 		return nfserr_noent;
2238 	}
2239 
2240 	exp = rqst_exp_find(&rqstp->rq_chandle, SVC_NET(rqstp),
2241 			    rqstp->rq_client, rqstp->rq_gssclient,
2242 			    map->fsid_type, map->fsid);
2243 	if (IS_ERR(exp)) {
2244 		dprintk("%s: could not find device id\n", __func__);
2245 		return nfserr_noent;
2246 	}
2247 
2248 	nfserr = nfserr_layoutunavailable;
2249 	ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
2250 	if (!ops)
2251 		goto out;
2252 
2253 	nfserr = nfs_ok;
2254 	if (gdp->gd_maxcount != 0) {
2255 		nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
2256 				rqstp, cstate->clp, gdp);
2257 	}
2258 
2259 	gdp->gd_notify_types &= ops->notify_types;
2260 out:
2261 	exp_put(exp);
2262 	return nfserr;
2263 }
2264 
2265 static void
nfsd4_getdeviceinfo_release(union nfsd4_op_u * u)2266 nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
2267 {
2268 	kfree(u->getdeviceinfo.gd_device);
2269 }
2270 
2271 static __be32
nfsd4_layoutget(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2272 nfsd4_layoutget(struct svc_rqst *rqstp,
2273 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2274 {
2275 	struct nfsd4_layoutget *lgp = &u->layoutget;
2276 	struct svc_fh *current_fh = &cstate->current_fh;
2277 	const struct nfsd4_layout_ops *ops;
2278 	struct nfs4_layout_stateid *ls;
2279 	__be32 nfserr;
2280 	int accmode = NFSD_MAY_READ_IF_EXEC | NFSD_MAY_OWNER_OVERRIDE;
2281 
2282 	switch (lgp->lg_seg.iomode) {
2283 	case IOMODE_READ:
2284 		accmode |= NFSD_MAY_READ;
2285 		break;
2286 	case IOMODE_RW:
2287 		accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
2288 		break;
2289 	default:
2290 		dprintk("%s: invalid iomode %d\n",
2291 			__func__, lgp->lg_seg.iomode);
2292 		nfserr = nfserr_badiomode;
2293 		goto out;
2294 	}
2295 
2296 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
2297 	if (nfserr)
2298 		goto out;
2299 
2300 	nfserr = nfserr_layoutunavailable;
2301 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
2302 	if (!ops)
2303 		goto out;
2304 
2305 	/*
2306 	 * Verify minlength and range as per RFC5661:
2307 	 *  o  If loga_length is less than loga_minlength,
2308 	 *     the metadata server MUST return NFS4ERR_INVAL.
2309 	 *  o  If the sum of loga_offset and loga_minlength exceeds
2310 	 *     NFS4_UINT64_MAX, and loga_minlength is not
2311 	 *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
2312 	 *  o  If the sum of loga_offset and loga_length exceeds
2313 	 *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
2314 	 *     the error NFS4ERR_INVAL MUST result.
2315 	 */
2316 	nfserr = nfserr_inval;
2317 	if (lgp->lg_seg.length < lgp->lg_minlength ||
2318 	    (lgp->lg_minlength != NFS4_MAX_UINT64 &&
2319 	     lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
2320 	    (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
2321 	     lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
2322 		goto out;
2323 	if (lgp->lg_seg.length == 0)
2324 		goto out;
2325 
2326 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
2327 						true, lgp->lg_layout_type, &ls);
2328 	if (nfserr) {
2329 		trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
2330 		goto out;
2331 	}
2332 
2333 	nfserr = nfserr_recallconflict;
2334 	if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
2335 		goto out_put_stid;
2336 
2337 	nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
2338 				     current_fh, lgp);
2339 	if (nfserr)
2340 		goto out_put_stid;
2341 
2342 	nfserr = nfsd4_insert_layout(lgp, ls);
2343 
2344 out_put_stid:
2345 	mutex_unlock(&ls->ls_mutex);
2346 	nfs4_put_stid(&ls->ls_stid);
2347 out:
2348 	return nfserr;
2349 }
2350 
2351 static void
nfsd4_layoutget_release(union nfsd4_op_u * u)2352 nfsd4_layoutget_release(union nfsd4_op_u *u)
2353 {
2354 	kfree(u->layoutget.lg_content);
2355 }
2356 
2357 static __be32
nfsd4_layoutcommit(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2358 nfsd4_layoutcommit(struct svc_rqst *rqstp,
2359 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2360 {
2361 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
2362 	const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
2363 	struct svc_fh *current_fh = &cstate->current_fh;
2364 	const struct nfsd4_layout_ops *ops;
2365 	loff_t new_size = lcp->lc_last_wr + 1;
2366 	struct inode *inode;
2367 	struct nfs4_layout_stateid *ls;
2368 	__be32 nfserr;
2369 
2370 	nfserr = fh_verify(rqstp, current_fh, 0,
2371 			   NFSD_MAY_WRITE | NFSD_MAY_OWNER_OVERRIDE);
2372 	if (nfserr)
2373 		goto out;
2374 
2375 	nfserr = nfserr_layoutunavailable;
2376 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
2377 	if (!ops)
2378 		goto out;
2379 	inode = d_inode(current_fh->fh_dentry);
2380 
2381 	nfserr = nfserr_inval;
2382 	if (new_size <= seg->offset) {
2383 		dprintk("pnfsd: last write before layout segment\n");
2384 		goto out;
2385 	}
2386 	if (new_size > seg->offset + seg->length) {
2387 		dprintk("pnfsd: last write beyond layout segment\n");
2388 		goto out;
2389 	}
2390 	if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
2391 		dprintk("pnfsd: layoutcommit beyond EOF\n");
2392 		goto out;
2393 	}
2394 
2395 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
2396 						false, lcp->lc_layout_type,
2397 						&ls);
2398 	if (nfserr) {
2399 		trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
2400 		/* fixup error code as per RFC5661 */
2401 		if (nfserr == nfserr_bad_stateid)
2402 			nfserr = nfserr_badlayout;
2403 		goto out;
2404 	}
2405 
2406 	/* LAYOUTCOMMIT does not require any serialization */
2407 	mutex_unlock(&ls->ls_mutex);
2408 
2409 	if (new_size > i_size_read(inode)) {
2410 		lcp->lc_size_chg = true;
2411 		lcp->lc_newsize = new_size;
2412 	} else {
2413 		lcp->lc_size_chg = false;
2414 	}
2415 
2416 	nfserr = ops->proc_layoutcommit(inode, lcp);
2417 	nfs4_put_stid(&ls->ls_stid);
2418 out:
2419 	return nfserr;
2420 }
2421 
2422 static __be32
nfsd4_layoutreturn(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2423 nfsd4_layoutreturn(struct svc_rqst *rqstp,
2424 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2425 {
2426 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
2427 	struct svc_fh *current_fh = &cstate->current_fh;
2428 	__be32 nfserr;
2429 
2430 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
2431 	if (nfserr)
2432 		goto out;
2433 
2434 	nfserr = nfserr_layoutunavailable;
2435 	if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
2436 		goto out;
2437 
2438 	switch (lrp->lr_seg.iomode) {
2439 	case IOMODE_READ:
2440 	case IOMODE_RW:
2441 	case IOMODE_ANY:
2442 		break;
2443 	default:
2444 		dprintk("%s: invalid iomode %d\n", __func__,
2445 			lrp->lr_seg.iomode);
2446 		nfserr = nfserr_inval;
2447 		goto out;
2448 	}
2449 
2450 	switch (lrp->lr_return_type) {
2451 	case RETURN_FILE:
2452 		nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
2453 		break;
2454 	case RETURN_FSID:
2455 	case RETURN_ALL:
2456 		nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
2457 		break;
2458 	default:
2459 		dprintk("%s: invalid return_type %d\n", __func__,
2460 			lrp->lr_return_type);
2461 		nfserr = nfserr_inval;
2462 		break;
2463 	}
2464 out:
2465 	return nfserr;
2466 }
2467 #endif /* CONFIG_NFSD_PNFS */
2468 
2469 static __be32
nfsd4_getxattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2470 nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2471 	       union nfsd4_op_u *u)
2472 {
2473 	struct nfsd4_getxattr *getxattr = &u->getxattr;
2474 
2475 	return nfsd_getxattr(rqstp, &cstate->current_fh,
2476 			     getxattr->getxa_name, &getxattr->getxa_buf,
2477 			     &getxattr->getxa_len);
2478 }
2479 
2480 static __be32
nfsd4_setxattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2481 nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2482 	   union nfsd4_op_u *u)
2483 {
2484 	struct nfsd4_setxattr *setxattr = &u->setxattr;
2485 	__be32 ret;
2486 
2487 	if (opens_in_grace(SVC_NET(rqstp)))
2488 		return nfserr_grace;
2489 
2490 	ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
2491 			    setxattr->setxa_buf, setxattr->setxa_len,
2492 			    setxattr->setxa_flags);
2493 
2494 	if (!ret)
2495 		set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
2496 
2497 	return ret;
2498 }
2499 
2500 static __be32
nfsd4_listxattrs(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2501 nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2502 	   union nfsd4_op_u *u)
2503 {
2504 	/*
2505 	 * Get the entire list, then copy out only the user attributes
2506 	 * in the encode function.
2507 	 */
2508 	return nfsd_listxattr(rqstp, &cstate->current_fh,
2509 			     &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
2510 }
2511 
2512 static __be32
nfsd4_removexattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2513 nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2514 	   union nfsd4_op_u *u)
2515 {
2516 	struct nfsd4_removexattr *removexattr = &u->removexattr;
2517 	__be32 ret;
2518 
2519 	if (opens_in_grace(SVC_NET(rqstp)))
2520 		return nfserr_grace;
2521 
2522 	ret = nfsd_removexattr(rqstp, &cstate->current_fh,
2523 	    removexattr->rmxa_name);
2524 
2525 	if (!ret)
2526 		set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
2527 
2528 	return ret;
2529 }
2530 
2531 /*
2532  * NULL call.
2533  */
2534 static __be32
nfsd4_proc_null(struct svc_rqst * rqstp)2535 nfsd4_proc_null(struct svc_rqst *rqstp)
2536 {
2537 	return rpc_success;
2538 }
2539 
nfsd4_increment_op_stats(struct nfsd_net * nn,u32 opnum)2540 static inline void nfsd4_increment_op_stats(struct nfsd_net *nn, u32 opnum)
2541 {
2542 	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
2543 		percpu_counter_inc(&nn->counter[NFSD_STATS_NFS4_OP(opnum)]);
2544 }
2545 
2546 static const struct nfsd4_operation nfsd4_ops[];
2547 
2548 static const char *nfsd4_op_name(unsigned opnum);
2549 
2550 /*
2551  * Enforce NFSv4.1 COMPOUND ordering rules:
2552  *
2553  * Also note, enforced elsewhere:
2554  *	- SEQUENCE other than as first op results in
2555  *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
2556  *	- BIND_CONN_TO_SESSION must be the only op in its compound.
2557  *	  (Enforced in nfsd4_bind_conn_to_session().)
2558  *	- DESTROY_SESSION must be the final operation in a compound, if
2559  *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
2560  *	  (Enforced in nfsd4_destroy_session().)
2561  */
nfs41_check_op_ordering(struct nfsd4_compoundargs * args)2562 static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
2563 {
2564 	struct nfsd4_op *first_op = &args->ops[0];
2565 
2566 	/* These ordering requirements don't apply to NFSv4.0: */
2567 	if (args->minorversion == 0)
2568 		return nfs_ok;
2569 	/* This is weird, but OK, not our problem: */
2570 	if (args->opcnt == 0)
2571 		return nfs_ok;
2572 	if (first_op->status == nfserr_op_illegal)
2573 		return nfs_ok;
2574 	if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
2575 		return nfserr_op_not_in_session;
2576 	if (first_op->opnum == OP_SEQUENCE)
2577 		return nfs_ok;
2578 	/*
2579 	 * So first_op is something allowed outside a session, like
2580 	 * EXCHANGE_ID; but then it has to be the only op in the
2581 	 * compound:
2582 	 */
2583 	if (args->opcnt != 1)
2584 		return nfserr_not_only_op;
2585 	return nfs_ok;
2586 }
2587 
OPDESC(struct nfsd4_op * op)2588 const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
2589 {
2590 	return &nfsd4_ops[op->opnum];
2591 }
2592 
nfsd4_cache_this_op(struct nfsd4_op * op)2593 bool nfsd4_cache_this_op(struct nfsd4_op *op)
2594 {
2595 	if (op->opnum == OP_ILLEGAL)
2596 		return false;
2597 	return OPDESC(op)->op_flags & OP_CACHEME;
2598 }
2599 
need_wrongsec_check(struct svc_rqst * rqstp)2600 static bool need_wrongsec_check(struct svc_rqst *rqstp)
2601 {
2602 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
2603 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2604 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2605 	struct nfsd4_op *next = &argp->ops[resp->opcnt];
2606 	const struct nfsd4_operation *thisd = OPDESC(this);
2607 	const struct nfsd4_operation *nextd;
2608 
2609 	/*
2610 	 * Most ops check wronsec on our own; only the putfh-like ops
2611 	 * have special rules.
2612 	 */
2613 	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
2614 		return false;
2615 	/*
2616 	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
2617 	 * put-filehandle operation if we're not going to use the
2618 	 * result:
2619 	 */
2620 	if (argp->opcnt == resp->opcnt)
2621 		return false;
2622 	if (next->opnum == OP_ILLEGAL)
2623 		return false;
2624 	nextd = OPDESC(next);
2625 	/*
2626 	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
2627 	 * errors themselves as necessary; others should check for them
2628 	 * now:
2629 	 */
2630 	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
2631 }
2632 
2633 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
2634 static void
check_if_stalefh_allowed(struct nfsd4_compoundargs * args)2635 check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2636 {
2637 	struct nfsd4_op	*op, *current_op = NULL, *saved_op = NULL;
2638 	struct nfsd4_copy *copy;
2639 	struct nfsd4_putfh *putfh;
2640 	int i;
2641 
2642 	/* traverse all operation and if it's a COPY compound, mark the
2643 	 * source filehandle to skip verification
2644 	 */
2645 	for (i = 0; i < args->opcnt; i++) {
2646 		op = &args->ops[i];
2647 		if (op->opnum == OP_PUTFH)
2648 			current_op = op;
2649 		else if (op->opnum == OP_SAVEFH)
2650 			saved_op = current_op;
2651 		else if (op->opnum == OP_RESTOREFH)
2652 			current_op = saved_op;
2653 		else if (op->opnum == OP_COPY) {
2654 			copy = (struct nfsd4_copy *)&op->u;
2655 			if (!saved_op) {
2656 				op->status = nfserr_nofilehandle;
2657 				return;
2658 			}
2659 			putfh = (struct nfsd4_putfh *)&saved_op->u;
2660 			if (nfsd4_ssc_is_inter(copy))
2661 				putfh->no_verify = true;
2662 		}
2663 	}
2664 }
2665 #else
2666 static void
check_if_stalefh_allowed(struct nfsd4_compoundargs * args)2667 check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2668 {
2669 }
2670 #endif
2671 
2672 /*
2673  * COMPOUND call.
2674  */
2675 static __be32
nfsd4_proc_compound(struct svc_rqst * rqstp)2676 nfsd4_proc_compound(struct svc_rqst *rqstp)
2677 {
2678 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
2679 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
2680 	struct nfsd4_op	*op;
2681 	struct nfsd4_compound_state *cstate = &resp->cstate;
2682 	struct svc_fh *current_fh = &cstate->current_fh;
2683 	struct svc_fh *save_fh = &cstate->save_fh;
2684 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2685 	__be32		status;
2686 
2687 	resp->xdr = &rqstp->rq_res_stream;
2688 	resp->statusp = resp->xdr->p;
2689 
2690 	/* reserve space for: NFS status code */
2691 	xdr_reserve_space(resp->xdr, XDR_UNIT);
2692 
2693 	/* reserve space for: taglen, tag, and opcnt */
2694 	xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen);
2695 	resp->taglen = args->taglen;
2696 	resp->tag = args->tag;
2697 	resp->rqstp = rqstp;
2698 	cstate->minorversion = args->minorversion;
2699 	fh_init(current_fh, NFS4_FHSIZE);
2700 	fh_init(save_fh, NFS4_FHSIZE);
2701 	/*
2702 	 * Don't use the deferral mechanism for NFSv4; compounds make it
2703 	 * too hard to avoid non-idempotency problems.
2704 	 */
2705 	clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2706 
2707 	/*
2708 	 * According to RFC3010, this takes precedence over all other errors.
2709 	 */
2710 	status = nfserr_minor_vers_mismatch;
2711 	if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
2712 		goto out;
2713 
2714 	status = nfs41_check_op_ordering(args);
2715 	if (status) {
2716 		op = &args->ops[0];
2717 		op->status = status;
2718 		resp->opcnt = 1;
2719 		goto encode_op;
2720 	}
2721 	check_if_stalefh_allowed(args);
2722 
2723 	rqstp->rq_lease_breaker = (void **)&cstate->clp;
2724 
2725 	trace_nfsd_compound(rqstp, args->tag, args->taglen, args->client_opcnt);
2726 	while (!status && resp->opcnt < args->opcnt) {
2727 		op = &args->ops[resp->opcnt++];
2728 
2729 		if (unlikely(resp->opcnt == NFSD_MAX_OPS_PER_COMPOUND)) {
2730 			/* If there are still more operations to process,
2731 			 * stop here and report NFS4ERR_RESOURCE. */
2732 			if (cstate->minorversion == 0 &&
2733 			    args->client_opcnt > resp->opcnt) {
2734 				op->status = nfserr_resource;
2735 				goto encode_op;
2736 			}
2737 		}
2738 
2739 		/*
2740 		 * The XDR decode routines may have pre-set op->status;
2741 		 * for example, if there is a miscellaneous XDR error
2742 		 * it will be set to nfserr_bad_xdr.
2743 		 */
2744 		if (op->status) {
2745 			if (op->opnum == OP_OPEN)
2746 				op->status = nfsd4_open_omfg(rqstp, cstate, op);
2747 			goto encode_op;
2748 		}
2749 		if (!current_fh->fh_dentry &&
2750 				!HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
2751 			if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
2752 				op->status = nfserr_nofilehandle;
2753 				goto encode_op;
2754 			}
2755 		} else if (current_fh->fh_export &&
2756 			   current_fh->fh_export->ex_fslocs.migrated &&
2757 			  !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
2758 			op->status = nfserr_moved;
2759 			goto encode_op;
2760 		}
2761 
2762 		fh_clear_pre_post_attrs(current_fh);
2763 
2764 		/* If op is non-idempotent */
2765 		if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
2766 			/*
2767 			 * Don't execute this op if we couldn't encode a
2768 			 * successful reply:
2769 			 */
2770 			u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
2771 			/*
2772 			 * Plus if there's another operation, make sure
2773 			 * we'll have space to at least encode an error:
2774 			 */
2775 			if (resp->opcnt < args->opcnt)
2776 				plen += COMPOUND_ERR_SLACK_SPACE;
2777 			op->status = nfsd4_check_resp_size(resp, plen);
2778 		}
2779 
2780 		if (op->status)
2781 			goto encode_op;
2782 
2783 		if (op->opdesc->op_get_currentstateid)
2784 			op->opdesc->op_get_currentstateid(cstate, &op->u);
2785 		op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
2786 
2787 		/* Only from SEQUENCE */
2788 		if (cstate->status == nfserr_replay_cache) {
2789 			dprintk("%s NFS4.1 replay from cache\n", __func__);
2790 			status = op->status;
2791 			goto out;
2792 		}
2793 		if (!op->status) {
2794 			if (op->opdesc->op_set_currentstateid)
2795 				op->opdesc->op_set_currentstateid(cstate, &op->u);
2796 
2797 			if (op->opdesc->op_flags & OP_CLEAR_STATEID)
2798 				clear_current_stateid(cstate);
2799 
2800 			if (current_fh->fh_export &&
2801 					need_wrongsec_check(rqstp))
2802 				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
2803 		}
2804 encode_op:
2805 		if (op->status == nfserr_replay_me) {
2806 			op->replay = &cstate->replay_owner->so_replay;
2807 			nfsd4_encode_replay(resp->xdr, op);
2808 			status = op->status = op->replay->rp_status;
2809 		} else {
2810 			nfsd4_encode_operation(resp, op);
2811 			status = op->status;
2812 		}
2813 
2814 		trace_nfsd_compound_status(args->client_opcnt, resp->opcnt,
2815 					   status, nfsd4_op_name(op->opnum));
2816 
2817 		nfsd4_cstate_clear_replay(cstate);
2818 		nfsd4_increment_op_stats(nn, op->opnum);
2819 	}
2820 
2821 	fh_put(current_fh);
2822 	fh_put(save_fh);
2823 	BUG_ON(cstate->replay_owner);
2824 out:
2825 	cstate->status = status;
2826 	/* Reset deferral mechanism for RPC deferrals */
2827 	set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2828 	return rpc_success;
2829 }
2830 
2831 #define op_encode_hdr_size		(2)
2832 #define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
2833 #define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
2834 #define op_encode_change_info_maxsz	(5)
2835 #define nfs4_fattr_bitmap_maxsz		(4)
2836 
2837 /* We'll fall back on returning no lockowner if run out of space: */
2838 #define op_encode_lockowner_maxsz	(0)
2839 #define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
2840 
2841 #define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
2842 
2843 #define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
2844 #define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
2845 					 op_encode_ace_maxsz)
2846 
2847 #define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
2848 
2849 /*
2850  * The _rsize() helpers are invoked by the NFSv4 COMPOUND decoder, which
2851  * is called before sunrpc sets rq_res.buflen. Thus we have to compute
2852  * the maximum payload size here, based on transport limits and the size
2853  * of the remaining space in the rq_pages array.
2854  */
nfsd4_max_payload(const struct svc_rqst * rqstp)2855 static u32 nfsd4_max_payload(const struct svc_rqst *rqstp)
2856 {
2857 	u32 buflen;
2858 
2859 	buflen = (rqstp->rq_page_end - rqstp->rq_next_page) * PAGE_SIZE;
2860 	buflen -= rqstp->rq_auth_slack;
2861 	buflen -= rqstp->rq_res.head[0].iov_len;
2862 	return min_t(u32, buflen, svc_max_payload(rqstp));
2863 }
2864 
nfsd4_only_status_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2865 static u32 nfsd4_only_status_rsize(const struct svc_rqst *rqstp,
2866 				   const struct nfsd4_op *op)
2867 {
2868 	return (op_encode_hdr_size) * sizeof(__be32);
2869 }
2870 
nfsd4_status_stateid_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2871 static u32 nfsd4_status_stateid_rsize(const struct svc_rqst *rqstp,
2872 				      const struct nfsd4_op *op)
2873 {
2874 	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
2875 }
2876 
nfsd4_access_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2877 static u32 nfsd4_access_rsize(const struct svc_rqst *rqstp,
2878 			      const struct nfsd4_op *op)
2879 {
2880 	/* ac_supported, ac_resp_access */
2881 	return (op_encode_hdr_size + 2)* sizeof(__be32);
2882 }
2883 
nfsd4_commit_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2884 static u32 nfsd4_commit_rsize(const struct svc_rqst *rqstp,
2885 			      const struct nfsd4_op *op)
2886 {
2887 	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
2888 }
2889 
nfsd4_create_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2890 static u32 nfsd4_create_rsize(const struct svc_rqst *rqstp,
2891 			      const struct nfsd4_op *op)
2892 {
2893 	return (op_encode_hdr_size + op_encode_change_info_maxsz
2894 		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2895 }
2896 
2897 /*
2898  * Note since this is an idempotent operation we won't insist on failing
2899  * the op prematurely if the estimate is too large.  We may turn off splice
2900  * reads unnecessarily.
2901  */
nfsd4_getattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2902 static u32 nfsd4_getattr_rsize(const struct svc_rqst *rqstp,
2903 			       const struct nfsd4_op *op)
2904 {
2905 	const u32 *bmap = op->u.getattr.ga_bmval;
2906 	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2907 	u32 ret = 0;
2908 
2909 	if (bmap0 & FATTR4_WORD0_ACL)
2910 		return nfsd4_max_payload(rqstp);
2911 	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
2912 		return nfsd4_max_payload(rqstp);
2913 
2914 	if (bmap1 & FATTR4_WORD1_OWNER) {
2915 		ret += IDMAP_NAMESZ + 4;
2916 		bmap1 &= ~FATTR4_WORD1_OWNER;
2917 	}
2918 	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2919 		ret += IDMAP_NAMESZ + 4;
2920 		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2921 	}
2922 	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2923 		ret += NFS4_FHSIZE + 4;
2924 		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2925 	}
2926 	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
2927 		ret += NFS4_MAXLABELLEN + 12;
2928 		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2929 	}
2930 	/*
2931 	 * Largest of remaining attributes are 16 bytes (e.g.,
2932 	 * supported_attributes)
2933 	 */
2934 	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2935 	/* bitmask, length */
2936 	ret += 20;
2937 	return ret;
2938 }
2939 
nfsd4_getfh_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2940 static u32 nfsd4_getfh_rsize(const struct svc_rqst *rqstp,
2941 			     const struct nfsd4_op *op)
2942 {
2943 	return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
2944 }
2945 
nfsd4_link_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2946 static u32 nfsd4_link_rsize(const struct svc_rqst *rqstp,
2947 			    const struct nfsd4_op *op)
2948 {
2949 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
2950 		* sizeof(__be32);
2951 }
2952 
nfsd4_lock_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2953 static u32 nfsd4_lock_rsize(const struct svc_rqst *rqstp,
2954 			    const struct nfsd4_op *op)
2955 {
2956 	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
2957 		* sizeof(__be32);
2958 }
2959 
nfsd4_open_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2960 static u32 nfsd4_open_rsize(const struct svc_rqst *rqstp,
2961 			    const struct nfsd4_op *op)
2962 {
2963 	return (op_encode_hdr_size + op_encode_stateid_maxsz
2964 		+ op_encode_change_info_maxsz + 1
2965 		+ nfs4_fattr_bitmap_maxsz
2966 		+ op_encode_delegation_maxsz) * sizeof(__be32);
2967 }
2968 
nfsd4_read_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2969 static u32 nfsd4_read_rsize(const struct svc_rqst *rqstp,
2970 			    const struct nfsd4_op *op)
2971 {
2972 	u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp));
2973 
2974 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
2975 }
2976 
nfsd4_read_plus_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2977 static u32 nfsd4_read_plus_rsize(const struct svc_rqst *rqstp,
2978 				 const struct nfsd4_op *op)
2979 {
2980 	u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp));
2981 	/*
2982 	 * If we detect that the file changed during hole encoding, then we
2983 	 * recover by encoding the remaining reply as data. This means we need
2984 	 * to set aside enough room to encode two data segments.
2985 	 */
2986 	u32 seg_len = 2 * (1 + 2 + 1);
2987 
2988 	return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
2989 }
2990 
nfsd4_readdir_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)2991 static u32 nfsd4_readdir_rsize(const struct svc_rqst *rqstp,
2992 			       const struct nfsd4_op *op)
2993 {
2994 	u32 rlen = min(op->u.readdir.rd_maxcount, nfsd4_max_payload(rqstp));
2995 
2996 	return (op_encode_hdr_size + op_encode_verifier_maxsz +
2997 		XDR_QUADLEN(rlen)) * sizeof(__be32);
2998 }
2999 
nfsd4_readlink_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3000 static u32 nfsd4_readlink_rsize(const struct svc_rqst *rqstp,
3001 				const struct nfsd4_op *op)
3002 {
3003 	return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
3004 }
3005 
nfsd4_remove_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3006 static u32 nfsd4_remove_rsize(const struct svc_rqst *rqstp,
3007 			      const struct nfsd4_op *op)
3008 {
3009 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
3010 		* sizeof(__be32);
3011 }
3012 
nfsd4_rename_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3013 static u32 nfsd4_rename_rsize(const struct svc_rqst *rqstp,
3014 			      const struct nfsd4_op *op)
3015 {
3016 	return (op_encode_hdr_size + op_encode_change_info_maxsz
3017 		+ op_encode_change_info_maxsz) * sizeof(__be32);
3018 }
3019 
nfsd4_sequence_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3020 static u32 nfsd4_sequence_rsize(const struct svc_rqst *rqstp,
3021 				const struct nfsd4_op *op)
3022 {
3023 	return (op_encode_hdr_size
3024 		+ XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
3025 }
3026 
nfsd4_test_stateid_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3027 static u32 nfsd4_test_stateid_rsize(const struct svc_rqst *rqstp,
3028 				    const struct nfsd4_op *op)
3029 {
3030 	return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
3031 		* sizeof(__be32);
3032 }
3033 
nfsd4_setattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3034 static u32 nfsd4_setattr_rsize(const struct svc_rqst *rqstp,
3035 			       const struct nfsd4_op *op)
3036 {
3037 	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
3038 }
3039 
nfsd4_secinfo_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3040 static u32 nfsd4_secinfo_rsize(const struct svc_rqst *rqstp,
3041 			       const struct nfsd4_op *op)
3042 {
3043 	return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
3044 		(4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
3045 }
3046 
nfsd4_setclientid_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3047 static u32 nfsd4_setclientid_rsize(const struct svc_rqst *rqstp,
3048 				   const struct nfsd4_op *op)
3049 {
3050 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
3051 								sizeof(__be32);
3052 }
3053 
nfsd4_write_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3054 static u32 nfsd4_write_rsize(const struct svc_rqst *rqstp,
3055 			     const struct nfsd4_op *op)
3056 {
3057 	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
3058 }
3059 
nfsd4_exchange_id_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3060 static u32 nfsd4_exchange_id_rsize(const struct svc_rqst *rqstp,
3061 				   const struct nfsd4_op *op)
3062 {
3063 	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
3064 		1 + 1 + /* eir_flags, spr_how */\
3065 		4 + /* spo_must_enforce & _allow with bitmap */\
3066 		2 + /*eir_server_owner.so_minor_id */\
3067 		/* eir_server_owner.so_major_id<> */\
3068 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
3069 		/* eir_server_scope<> */\
3070 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
3071 		1 + /* eir_server_impl_id array length */\
3072 		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
3073 }
3074 
nfsd4_bind_conn_to_session_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3075 static u32 nfsd4_bind_conn_to_session_rsize(const struct svc_rqst *rqstp,
3076 					    const struct nfsd4_op *op)
3077 {
3078 	return (op_encode_hdr_size + \
3079 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
3080 		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
3081 }
3082 
nfsd4_create_session_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3083 static u32 nfsd4_create_session_rsize(const struct svc_rqst *rqstp,
3084 				      const struct nfsd4_op *op)
3085 {
3086 	return (op_encode_hdr_size + \
3087 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
3088 		2 + /* csr_sequence, csr_flags */\
3089 		op_encode_channel_attrs_maxsz + \
3090 		op_encode_channel_attrs_maxsz) * sizeof(__be32);
3091 }
3092 
nfsd4_copy_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3093 static u32 nfsd4_copy_rsize(const struct svc_rqst *rqstp,
3094 			    const struct nfsd4_op *op)
3095 {
3096 	return (op_encode_hdr_size +
3097 		1 /* wr_callback */ +
3098 		op_encode_stateid_maxsz /* wr_callback */ +
3099 		2 /* wr_count */ +
3100 		1 /* wr_committed */ +
3101 		op_encode_verifier_maxsz +
3102 		1 /* cr_consecutive */ +
3103 		1 /* cr_synchronous */) * sizeof(__be32);
3104 }
3105 
nfsd4_offload_status_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3106 static u32 nfsd4_offload_status_rsize(const struct svc_rqst *rqstp,
3107 				      const struct nfsd4_op *op)
3108 {
3109 	return (op_encode_hdr_size +
3110 		2 /* osr_count */ +
3111 		1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
3112 }
3113 
nfsd4_copy_notify_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3114 static u32 nfsd4_copy_notify_rsize(const struct svc_rqst *rqstp,
3115 				   const struct nfsd4_op *op)
3116 {
3117 	return (op_encode_hdr_size +
3118 		3 /* cnr_lease_time */ +
3119 		1 /* We support one cnr_source_server */ +
3120 		1 /* cnr_stateid seq */ +
3121 		op_encode_stateid_maxsz /* cnr_stateid */ +
3122 		1 /* num cnr_source_server*/ +
3123 		1 /* nl4_type */ +
3124 		1 /* nl4 size */ +
3125 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
3126 		* sizeof(__be32);
3127 }
3128 
nfsd4_get_dir_delegation_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3129 static u32 nfsd4_get_dir_delegation_rsize(const struct svc_rqst *rqstp,
3130 					  const struct nfsd4_op *op)
3131 {
3132 	return (op_encode_hdr_size +
3133 		1 /* gddr_status */ +
3134 		op_encode_verifier_maxsz +
3135 		op_encode_stateid_maxsz +
3136 		2 /* gddr_notification */ +
3137 		2 /* gddr_child_attributes */ +
3138 		2 /* gddr_dir_attributes */);
3139 }
3140 
3141 #ifdef CONFIG_NFSD_PNFS
nfsd4_getdeviceinfo_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3142 static u32 nfsd4_getdeviceinfo_rsize(const struct svc_rqst *rqstp,
3143 				     const struct nfsd4_op *op)
3144 {
3145 	u32 rlen = min(op->u.getdeviceinfo.gd_maxcount, nfsd4_max_payload(rqstp));
3146 
3147 	return (op_encode_hdr_size +
3148 		1 /* gd_layout_type*/ +
3149 		XDR_QUADLEN(rlen) +
3150 		2 /* gd_notify_types */) * sizeof(__be32);
3151 }
3152 
3153 /*
3154  * At this stage we don't really know what layout driver will handle the request,
3155  * so we need to define an arbitrary upper bound here.
3156  */
3157 #define MAX_LAYOUT_SIZE		128
nfsd4_layoutget_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3158 static u32 nfsd4_layoutget_rsize(const struct svc_rqst *rqstp,
3159 				 const struct nfsd4_op *op)
3160 {
3161 	return (op_encode_hdr_size +
3162 		1 /* logr_return_on_close */ +
3163 		op_encode_stateid_maxsz +
3164 		1 /* nr of layouts */ +
3165 		MAX_LAYOUT_SIZE) * sizeof(__be32);
3166 }
3167 
nfsd4_layoutcommit_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3168 static u32 nfsd4_layoutcommit_rsize(const struct svc_rqst *rqstp,
3169 				    const struct nfsd4_op *op)
3170 {
3171 	return (op_encode_hdr_size +
3172 		1 /* locr_newsize */ +
3173 		2 /* ns_size */) * sizeof(__be32);
3174 }
3175 
nfsd4_layoutreturn_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3176 static u32 nfsd4_layoutreturn_rsize(const struct svc_rqst *rqstp,
3177 				    const struct nfsd4_op *op)
3178 {
3179 	return (op_encode_hdr_size +
3180 		1 /* lrs_stateid */ +
3181 		op_encode_stateid_maxsz) * sizeof(__be32);
3182 }
3183 #endif /* CONFIG_NFSD_PNFS */
3184 
3185 
nfsd4_seek_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3186 static u32 nfsd4_seek_rsize(const struct svc_rqst *rqstp,
3187 			    const struct nfsd4_op *op)
3188 {
3189 	return (op_encode_hdr_size + 3) * sizeof(__be32);
3190 }
3191 
nfsd4_getxattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3192 static u32 nfsd4_getxattr_rsize(const struct svc_rqst *rqstp,
3193 				const struct nfsd4_op *op)
3194 {
3195 	u32 rlen = min_t(u32, XATTR_SIZE_MAX, nfsd4_max_payload(rqstp));
3196 
3197 	return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3198 }
3199 
nfsd4_setxattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3200 static u32 nfsd4_setxattr_rsize(const struct svc_rqst *rqstp,
3201 				const struct nfsd4_op *op)
3202 {
3203 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
3204 		* sizeof(__be32);
3205 }
nfsd4_listxattrs_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3206 static u32 nfsd4_listxattrs_rsize(const struct svc_rqst *rqstp,
3207 				  const struct nfsd4_op *op)
3208 {
3209 	u32 rlen = min(op->u.listxattrs.lsxa_maxcount, nfsd4_max_payload(rqstp));
3210 
3211 	return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3212 }
3213 
nfsd4_removexattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)3214 static u32 nfsd4_removexattr_rsize(const struct svc_rqst *rqstp,
3215 				   const struct nfsd4_op *op)
3216 {
3217 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
3218 		* sizeof(__be32);
3219 }
3220 
3221 
3222 static const struct nfsd4_operation nfsd4_ops[] = {
3223 	[OP_ACCESS] = {
3224 		.op_func = nfsd4_access,
3225 		.op_name = "OP_ACCESS",
3226 		.op_rsize_bop = nfsd4_access_rsize,
3227 	},
3228 	[OP_CLOSE] = {
3229 		.op_func = nfsd4_close,
3230 		.op_flags = OP_MODIFIES_SOMETHING,
3231 		.op_name = "OP_CLOSE",
3232 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3233 		.op_get_currentstateid = nfsd4_get_closestateid,
3234 		.op_set_currentstateid = nfsd4_set_closestateid,
3235 	},
3236 	[OP_COMMIT] = {
3237 		.op_func = nfsd4_commit,
3238 		.op_flags = OP_MODIFIES_SOMETHING,
3239 		.op_name = "OP_COMMIT",
3240 		.op_rsize_bop = nfsd4_commit_rsize,
3241 	},
3242 	[OP_CREATE] = {
3243 		.op_func = nfsd4_create,
3244 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
3245 		.op_name = "OP_CREATE",
3246 		.op_rsize_bop = nfsd4_create_rsize,
3247 	},
3248 	[OP_DELEGRETURN] = {
3249 		.op_func = nfsd4_delegreturn,
3250 		.op_flags = OP_MODIFIES_SOMETHING,
3251 		.op_name = "OP_DELEGRETURN",
3252 		.op_rsize_bop = nfsd4_only_status_rsize,
3253 		.op_get_currentstateid = nfsd4_get_delegreturnstateid,
3254 	},
3255 	[OP_GETATTR] = {
3256 		.op_func = nfsd4_getattr,
3257 		.op_flags = ALLOWED_ON_ABSENT_FS,
3258 		.op_rsize_bop = nfsd4_getattr_rsize,
3259 		.op_name = "OP_GETATTR",
3260 	},
3261 	[OP_GETFH] = {
3262 		.op_func = nfsd4_getfh,
3263 		.op_name = "OP_GETFH",
3264 		.op_rsize_bop = nfsd4_getfh_rsize,
3265 	},
3266 	[OP_LINK] = {
3267 		.op_func = nfsd4_link,
3268 		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
3269 				| OP_CACHEME,
3270 		.op_name = "OP_LINK",
3271 		.op_rsize_bop = nfsd4_link_rsize,
3272 	},
3273 	[OP_LOCK] = {
3274 		.op_func = nfsd4_lock,
3275 		.op_release = nfsd4_lock_release,
3276 		.op_flags = OP_MODIFIES_SOMETHING |
3277 				OP_NONTRIVIAL_ERROR_ENCODE,
3278 		.op_name = "OP_LOCK",
3279 		.op_rsize_bop = nfsd4_lock_rsize,
3280 		.op_set_currentstateid = nfsd4_set_lockstateid,
3281 	},
3282 	[OP_LOCKT] = {
3283 		.op_func = nfsd4_lockt,
3284 		.op_release = nfsd4_lockt_release,
3285 		.op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
3286 		.op_name = "OP_LOCKT",
3287 		.op_rsize_bop = nfsd4_lock_rsize,
3288 	},
3289 	[OP_LOCKU] = {
3290 		.op_func = nfsd4_locku,
3291 		.op_flags = OP_MODIFIES_SOMETHING,
3292 		.op_name = "OP_LOCKU",
3293 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3294 		.op_get_currentstateid = nfsd4_get_lockustateid,
3295 	},
3296 	[OP_LOOKUP] = {
3297 		.op_func = nfsd4_lookup,
3298 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3299 		.op_name = "OP_LOOKUP",
3300 		.op_rsize_bop = nfsd4_only_status_rsize,
3301 	},
3302 	[OP_LOOKUPP] = {
3303 		.op_func = nfsd4_lookupp,
3304 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3305 		.op_name = "OP_LOOKUPP",
3306 		.op_rsize_bop = nfsd4_only_status_rsize,
3307 	},
3308 	[OP_NVERIFY] = {
3309 		.op_func = nfsd4_nverify,
3310 		.op_name = "OP_NVERIFY",
3311 		.op_rsize_bop = nfsd4_only_status_rsize,
3312 	},
3313 	[OP_OPEN] = {
3314 		.op_func = nfsd4_open,
3315 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3316 		.op_name = "OP_OPEN",
3317 		.op_rsize_bop = nfsd4_open_rsize,
3318 		.op_set_currentstateid = nfsd4_set_openstateid,
3319 	},
3320 	[OP_OPEN_CONFIRM] = {
3321 		.op_func = nfsd4_open_confirm,
3322 		.op_flags = OP_MODIFIES_SOMETHING,
3323 		.op_name = "OP_OPEN_CONFIRM",
3324 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3325 	},
3326 	[OP_OPEN_DOWNGRADE] = {
3327 		.op_func = nfsd4_open_downgrade,
3328 		.op_flags = OP_MODIFIES_SOMETHING,
3329 		.op_name = "OP_OPEN_DOWNGRADE",
3330 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3331 		.op_get_currentstateid = nfsd4_get_opendowngradestateid,
3332 		.op_set_currentstateid = nfsd4_set_opendowngradestateid,
3333 	},
3334 	[OP_PUTFH] = {
3335 		.op_func = nfsd4_putfh,
3336 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3337 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3338 		.op_name = "OP_PUTFH",
3339 		.op_rsize_bop = nfsd4_only_status_rsize,
3340 	},
3341 	[OP_PUTPUBFH] = {
3342 		.op_func = nfsd4_putrootfh,
3343 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3344 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3345 		.op_name = "OP_PUTPUBFH",
3346 		.op_rsize_bop = nfsd4_only_status_rsize,
3347 	},
3348 	[OP_PUTROOTFH] = {
3349 		.op_func = nfsd4_putrootfh,
3350 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3351 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3352 		.op_name = "OP_PUTROOTFH",
3353 		.op_rsize_bop = nfsd4_only_status_rsize,
3354 	},
3355 	[OP_READ] = {
3356 		.op_func = nfsd4_read,
3357 		.op_release = nfsd4_read_release,
3358 		.op_name = "OP_READ",
3359 		.op_rsize_bop = nfsd4_read_rsize,
3360 		.op_get_currentstateid = nfsd4_get_readstateid,
3361 	},
3362 	[OP_READDIR] = {
3363 		.op_func = nfsd4_readdir,
3364 		.op_name = "OP_READDIR",
3365 		.op_rsize_bop = nfsd4_readdir_rsize,
3366 	},
3367 	[OP_READLINK] = {
3368 		.op_func = nfsd4_readlink,
3369 		.op_name = "OP_READLINK",
3370 		.op_rsize_bop = nfsd4_readlink_rsize,
3371 	},
3372 	[OP_REMOVE] = {
3373 		.op_func = nfsd4_remove,
3374 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3375 		.op_name = "OP_REMOVE",
3376 		.op_rsize_bop = nfsd4_remove_rsize,
3377 	},
3378 	[OP_RENAME] = {
3379 		.op_func = nfsd4_rename,
3380 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3381 		.op_name = "OP_RENAME",
3382 		.op_rsize_bop = nfsd4_rename_rsize,
3383 	},
3384 	[OP_RENEW] = {
3385 		.op_func = nfsd4_renew,
3386 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3387 				| OP_MODIFIES_SOMETHING,
3388 		.op_name = "OP_RENEW",
3389 		.op_rsize_bop = nfsd4_only_status_rsize,
3390 
3391 	},
3392 	[OP_RESTOREFH] = {
3393 		.op_func = nfsd4_restorefh,
3394 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3395 				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
3396 		.op_name = "OP_RESTOREFH",
3397 		.op_rsize_bop = nfsd4_only_status_rsize,
3398 	},
3399 	[OP_SAVEFH] = {
3400 		.op_func = nfsd4_savefh,
3401 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3402 		.op_name = "OP_SAVEFH",
3403 		.op_rsize_bop = nfsd4_only_status_rsize,
3404 	},
3405 	[OP_SECINFO] = {
3406 		.op_func = nfsd4_secinfo,
3407 		.op_release = nfsd4_secinfo_release,
3408 		.op_flags = OP_HANDLES_WRONGSEC,
3409 		.op_name = "OP_SECINFO",
3410 		.op_rsize_bop = nfsd4_secinfo_rsize,
3411 	},
3412 	[OP_SETATTR] = {
3413 		.op_func = nfsd4_setattr,
3414 		.op_name = "OP_SETATTR",
3415 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3416 				| OP_NONTRIVIAL_ERROR_ENCODE,
3417 		.op_rsize_bop = nfsd4_setattr_rsize,
3418 		.op_get_currentstateid = nfsd4_get_setattrstateid,
3419 	},
3420 	[OP_SETCLIENTID] = {
3421 		.op_func = nfsd4_setclientid,
3422 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3423 				| OP_MODIFIES_SOMETHING | OP_CACHEME
3424 				| OP_NONTRIVIAL_ERROR_ENCODE,
3425 		.op_name = "OP_SETCLIENTID",
3426 		.op_rsize_bop = nfsd4_setclientid_rsize,
3427 	},
3428 	[OP_SETCLIENTID_CONFIRM] = {
3429 		.op_func = nfsd4_setclientid_confirm,
3430 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3431 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
3432 		.op_name = "OP_SETCLIENTID_CONFIRM",
3433 		.op_rsize_bop = nfsd4_only_status_rsize,
3434 	},
3435 	[OP_VERIFY] = {
3436 		.op_func = nfsd4_verify,
3437 		.op_name = "OP_VERIFY",
3438 		.op_rsize_bop = nfsd4_only_status_rsize,
3439 	},
3440 	[OP_WRITE] = {
3441 		.op_func = nfsd4_write,
3442 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3443 		.op_name = "OP_WRITE",
3444 		.op_rsize_bop = nfsd4_write_rsize,
3445 		.op_get_currentstateid = nfsd4_get_writestateid,
3446 	},
3447 	[OP_RELEASE_LOCKOWNER] = {
3448 		.op_func = nfsd4_release_lockowner,
3449 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3450 				| OP_MODIFIES_SOMETHING,
3451 		.op_name = "OP_RELEASE_LOCKOWNER",
3452 		.op_rsize_bop = nfsd4_only_status_rsize,
3453 	},
3454 
3455 	/* NFSv4.1 operations */
3456 	[OP_EXCHANGE_ID] = {
3457 		.op_func = nfsd4_exchange_id,
3458 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3459 				| OP_MODIFIES_SOMETHING,
3460 		.op_name = "OP_EXCHANGE_ID",
3461 		.op_rsize_bop = nfsd4_exchange_id_rsize,
3462 	},
3463 	[OP_BACKCHANNEL_CTL] = {
3464 		.op_func = nfsd4_backchannel_ctl,
3465 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3466 		.op_name = "OP_BACKCHANNEL_CTL",
3467 		.op_rsize_bop = nfsd4_only_status_rsize,
3468 	},
3469 	[OP_BIND_CONN_TO_SESSION] = {
3470 		.op_func = nfsd4_bind_conn_to_session,
3471 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3472 				| OP_MODIFIES_SOMETHING,
3473 		.op_name = "OP_BIND_CONN_TO_SESSION",
3474 		.op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
3475 	},
3476 	[OP_CREATE_SESSION] = {
3477 		.op_func = nfsd4_create_session,
3478 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3479 				| OP_MODIFIES_SOMETHING,
3480 		.op_name = "OP_CREATE_SESSION",
3481 		.op_rsize_bop = nfsd4_create_session_rsize,
3482 	},
3483 	[OP_DESTROY_SESSION] = {
3484 		.op_func = nfsd4_destroy_session,
3485 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3486 				| OP_MODIFIES_SOMETHING,
3487 		.op_name = "OP_DESTROY_SESSION",
3488 		.op_rsize_bop = nfsd4_only_status_rsize,
3489 	},
3490 	[OP_SEQUENCE] = {
3491 		.op_func = nfsd4_sequence,
3492 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
3493 		.op_name = "OP_SEQUENCE",
3494 		.op_rsize_bop = nfsd4_sequence_rsize,
3495 	},
3496 	[OP_DESTROY_CLIENTID] = {
3497 		.op_func = nfsd4_destroy_clientid,
3498 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3499 				| OP_MODIFIES_SOMETHING,
3500 		.op_name = "OP_DESTROY_CLIENTID",
3501 		.op_rsize_bop = nfsd4_only_status_rsize,
3502 	},
3503 	[OP_RECLAIM_COMPLETE] = {
3504 		.op_func = nfsd4_reclaim_complete,
3505 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3506 		.op_name = "OP_RECLAIM_COMPLETE",
3507 		.op_rsize_bop = nfsd4_only_status_rsize,
3508 	},
3509 	[OP_SECINFO_NO_NAME] = {
3510 		.op_func = nfsd4_secinfo_no_name,
3511 		.op_release = nfsd4_secinfo_no_name_release,
3512 		.op_flags = OP_HANDLES_WRONGSEC,
3513 		.op_name = "OP_SECINFO_NO_NAME",
3514 		.op_rsize_bop = nfsd4_secinfo_rsize,
3515 	},
3516 	[OP_TEST_STATEID] = {
3517 		.op_func = nfsd4_test_stateid,
3518 		.op_flags = ALLOWED_WITHOUT_FH,
3519 		.op_name = "OP_TEST_STATEID",
3520 		.op_rsize_bop = nfsd4_test_stateid_rsize,
3521 	},
3522 	[OP_FREE_STATEID] = {
3523 		.op_func = nfsd4_free_stateid,
3524 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3525 		.op_name = "OP_FREE_STATEID",
3526 		.op_get_currentstateid = nfsd4_get_freestateid,
3527 		.op_rsize_bop = nfsd4_only_status_rsize,
3528 	},
3529 	[OP_GET_DIR_DELEGATION] = {
3530 		.op_func = nfsd4_get_dir_delegation,
3531 		.op_flags = OP_MODIFIES_SOMETHING,
3532 		.op_name = "OP_GET_DIR_DELEGATION",
3533 		.op_rsize_bop = nfsd4_get_dir_delegation_rsize,
3534 	},
3535 #ifdef CONFIG_NFSD_PNFS
3536 	[OP_GETDEVICEINFO] = {
3537 		.op_func = nfsd4_getdeviceinfo,
3538 		.op_release = nfsd4_getdeviceinfo_release,
3539 		.op_flags = ALLOWED_WITHOUT_FH,
3540 		.op_name = "OP_GETDEVICEINFO",
3541 		.op_rsize_bop = nfsd4_getdeviceinfo_rsize,
3542 	},
3543 	[OP_LAYOUTGET] = {
3544 		.op_func = nfsd4_layoutget,
3545 		.op_release = nfsd4_layoutget_release,
3546 		.op_flags = OP_MODIFIES_SOMETHING,
3547 		.op_name = "OP_LAYOUTGET",
3548 		.op_rsize_bop = nfsd4_layoutget_rsize,
3549 	},
3550 	[OP_LAYOUTCOMMIT] = {
3551 		.op_func = nfsd4_layoutcommit,
3552 		.op_flags = OP_MODIFIES_SOMETHING,
3553 		.op_name = "OP_LAYOUTCOMMIT",
3554 		.op_rsize_bop = nfsd4_layoutcommit_rsize,
3555 	},
3556 	[OP_LAYOUTRETURN] = {
3557 		.op_func = nfsd4_layoutreturn,
3558 		.op_flags = OP_MODIFIES_SOMETHING,
3559 		.op_name = "OP_LAYOUTRETURN",
3560 		.op_rsize_bop = nfsd4_layoutreturn_rsize,
3561 	},
3562 #endif /* CONFIG_NFSD_PNFS */
3563 
3564 	/* NFSv4.2 operations */
3565 	[OP_ALLOCATE] = {
3566 		.op_func = nfsd4_allocate,
3567 		.op_flags = OP_MODIFIES_SOMETHING,
3568 		.op_name = "OP_ALLOCATE",
3569 		.op_rsize_bop = nfsd4_only_status_rsize,
3570 	},
3571 	[OP_DEALLOCATE] = {
3572 		.op_func = nfsd4_deallocate,
3573 		.op_flags = OP_MODIFIES_SOMETHING,
3574 		.op_name = "OP_DEALLOCATE",
3575 		.op_rsize_bop = nfsd4_only_status_rsize,
3576 	},
3577 	[OP_CLONE] = {
3578 		.op_func = nfsd4_clone,
3579 		.op_flags = OP_MODIFIES_SOMETHING,
3580 		.op_name = "OP_CLONE",
3581 		.op_rsize_bop = nfsd4_only_status_rsize,
3582 	},
3583 	[OP_COPY] = {
3584 		.op_func = nfsd4_copy,
3585 		.op_flags = OP_MODIFIES_SOMETHING,
3586 		.op_name = "OP_COPY",
3587 		.op_rsize_bop = nfsd4_copy_rsize,
3588 	},
3589 	[OP_READ_PLUS] = {
3590 		.op_func = nfsd4_read,
3591 		.op_release = nfsd4_read_release,
3592 		.op_name = "OP_READ_PLUS",
3593 		.op_rsize_bop = nfsd4_read_plus_rsize,
3594 		.op_get_currentstateid = nfsd4_get_readstateid,
3595 	},
3596 	[OP_SEEK] = {
3597 		.op_func = nfsd4_seek,
3598 		.op_name = "OP_SEEK",
3599 		.op_rsize_bop = nfsd4_seek_rsize,
3600 	},
3601 	[OP_OFFLOAD_STATUS] = {
3602 		.op_func = nfsd4_offload_status,
3603 		.op_name = "OP_OFFLOAD_STATUS",
3604 		.op_rsize_bop = nfsd4_offload_status_rsize,
3605 	},
3606 	[OP_OFFLOAD_CANCEL] = {
3607 		.op_func = nfsd4_offload_cancel,
3608 		.op_flags = OP_MODIFIES_SOMETHING,
3609 		.op_name = "OP_OFFLOAD_CANCEL",
3610 		.op_rsize_bop = nfsd4_only_status_rsize,
3611 	},
3612 	[OP_COPY_NOTIFY] = {
3613 		.op_func = nfsd4_copy_notify,
3614 		.op_flags = OP_MODIFIES_SOMETHING,
3615 		.op_name = "OP_COPY_NOTIFY",
3616 		.op_rsize_bop = nfsd4_copy_notify_rsize,
3617 	},
3618 	[OP_GETXATTR] = {
3619 		.op_func = nfsd4_getxattr,
3620 		.op_name = "OP_GETXATTR",
3621 		.op_rsize_bop = nfsd4_getxattr_rsize,
3622 	},
3623 	[OP_SETXATTR] = {
3624 		.op_func = nfsd4_setxattr,
3625 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3626 		.op_name = "OP_SETXATTR",
3627 		.op_rsize_bop = nfsd4_setxattr_rsize,
3628 	},
3629 	[OP_LISTXATTRS] = {
3630 		.op_func = nfsd4_listxattrs,
3631 		.op_name = "OP_LISTXATTRS",
3632 		.op_rsize_bop = nfsd4_listxattrs_rsize,
3633 	},
3634 	[OP_REMOVEXATTR] = {
3635 		.op_func = nfsd4_removexattr,
3636 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3637 		.op_name = "OP_REMOVEXATTR",
3638 		.op_rsize_bop = nfsd4_removexattr_rsize,
3639 	},
3640 };
3641 
3642 /**
3643  * nfsd4_spo_must_allow - Determine if the compound op contains an
3644  * operation that is allowed to be sent with machine credentials
3645  *
3646  * @rqstp: a pointer to the struct svc_rqst
3647  *
3648  * Checks to see if the compound contains a spo_must_allow op
3649  * and confirms that it was sent with the proper machine creds.
3650  */
3651 
nfsd4_spo_must_allow(struct svc_rqst * rqstp)3652 bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3653 {
3654 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3655 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3656 	struct nfsd4_op *this;
3657 	struct nfsd4_compound_state *cstate = &resp->cstate;
3658 	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3659 	u32 opiter;
3660 
3661 	if (rqstp->rq_procinfo != &nfsd_version4.vs_proc[NFSPROC4_COMPOUND] ||
3662 	    cstate->minorversion == 0)
3663 		return false;
3664 
3665 	if (cstate->spo_must_allowed)
3666 		return true;
3667 
3668 	opiter = resp->opcnt;
3669 	while (opiter < argp->opcnt) {
3670 		this = &argp->ops[opiter++];
3671 		if (test_bit(this->opnum, allow->u.longs) &&
3672 			cstate->clp->cl_mach_cred &&
3673 			nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3674 			cstate->spo_must_allowed = true;
3675 			return true;
3676 		}
3677 	}
3678 	cstate->spo_must_allowed = false;
3679 	return false;
3680 }
3681 
nfsd4_max_reply(struct svc_rqst * rqstp,struct nfsd4_op * op)3682 int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
3683 {
3684 	if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
3685 		return op_encode_hdr_size * sizeof(__be32);
3686 
3687 	BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
3688 	return OPDESC(op)->op_rsize_bop(rqstp, op);
3689 }
3690 
warn_on_nonidempotent_op(struct nfsd4_op * op)3691 void warn_on_nonidempotent_op(struct nfsd4_op *op)
3692 {
3693 	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
3694 		pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
3695 			op->opnum, nfsd4_op_name(op->opnum));
3696 		WARN_ON_ONCE(1);
3697 	}
3698 }
3699 
nfsd4_op_name(unsigned opnum)3700 static const char *nfsd4_op_name(unsigned opnum)
3701 {
3702 	if (opnum < ARRAY_SIZE(nfsd4_ops))
3703 		return nfsd4_ops[opnum].op_name;
3704 	return "unknown_operation";
3705 }
3706 
3707 static const struct svc_procedure nfsd_procedures4[2] = {
3708 	[NFSPROC4_NULL] = {
3709 		.pc_func = nfsd4_proc_null,
3710 		.pc_decode = nfssvc_decode_voidarg,
3711 		.pc_encode = nfssvc_encode_voidres,
3712 		.pc_argsize = sizeof(struct nfsd_voidargs),
3713 		.pc_argzero = sizeof(struct nfsd_voidargs),
3714 		.pc_ressize = sizeof(struct nfsd_voidres),
3715 		.pc_cachetype = RC_NOCACHE,
3716 		.pc_xdrressize = 1,
3717 		.pc_name = "NULL",
3718 	},
3719 	[NFSPROC4_COMPOUND] = {
3720 		.pc_func = nfsd4_proc_compound,
3721 		.pc_decode = nfs4svc_decode_compoundargs,
3722 		.pc_encode = nfs4svc_encode_compoundres,
3723 		.pc_argsize = sizeof(struct nfsd4_compoundargs),
3724 		.pc_argzero = offsetof(struct nfsd4_compoundargs, iops),
3725 		.pc_ressize = sizeof(struct nfsd4_compoundres),
3726 		.pc_release = nfsd4_release_compoundargs,
3727 		.pc_cachetype = RC_NOCACHE,
3728 		.pc_xdrressize = NFSD_BUFSIZE/4,
3729 		.pc_name = "COMPOUND",
3730 	},
3731 };
3732 
3733 static DEFINE_PER_CPU_ALIGNED(unsigned long,
3734 			      nfsd_count4[ARRAY_SIZE(nfsd_procedures4)]);
3735 const struct svc_version nfsd_version4 = {
3736 	.vs_vers		= 4,
3737 	.vs_nproc		= ARRAY_SIZE(nfsd_procedures4),
3738 	.vs_proc		= nfsd_procedures4,
3739 	.vs_count		= nfsd_count4,
3740 	.vs_dispatch		= nfsd_dispatch,
3741 	.vs_xdrsize		= NFS4_SVC_XDRSIZE,
3742 	.vs_rpcb_optnl		= true,
3743 	.vs_need_cong_ctrl	= true,
3744 };
3745