xref: /openbmc/linux/fs/nfsd/vfs.c (revision 7fe2a71dda349a1afa75781f0cc7975be9784d15)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * File operations used by nfsd. Some of these have been ripped from
4  * other parts of the kernel because they weren't exported, others
5  * are partial duplicates with added or changed functionality.
6  *
7  * Note that several functions dget() the dentry upon which they want
8  * to act, most notably those that create directory entries. Response
9  * dentry's are dput()'d if necessary in the release callback.
10  * So if you notice code paths that apparently fail to dput() the
11  * dentry, don't worry--they have been taken care of.
12  *
13  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
15  */
16 
17 #include <linux/fs.h>
18 #include <linux/file.h>
19 #include <linux/splice.h>
20 #include <linux/falloc.h>
21 #include <linux/fcntl.h>
22 #include <linux/namei.h>
23 #include <linux/delay.h>
24 #include <linux/fsnotify.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/xattr.h>
27 #include <linux/jhash.h>
28 #include <linux/ima.h>
29 #include <linux/pagemap.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <linux/exportfs.h>
33 #include <linux/writeback.h>
34 #include <linux/security.h>
35 
36 #include "xdr3.h"
37 
38 #ifdef CONFIG_NFSD_V4
39 #include "../internal.h"
40 #include "acl.h"
41 #include "idmap.h"
42 #include "xdr4.h"
43 #endif /* CONFIG_NFSD_V4 */
44 
45 #include "nfsd.h"
46 #include "vfs.h"
47 #include "filecache.h"
48 #include "trace.h"
49 
50 #define NFSDDBG_FACILITY		NFSDDBG_FILEOP
51 
52 /*
53  * Called from nfsd_lookup and encode_dirent. Check if we have crossed
54  * a mount point.
55  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
56  *  or nfs_ok having possibly changed *dpp and *expp
57  */
58 int
59 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
60 		        struct svc_export **expp)
61 {
62 	struct svc_export *exp = *expp, *exp2 = NULL;
63 	struct dentry *dentry = *dpp;
64 	struct path path = {.mnt = mntget(exp->ex_path.mnt),
65 			    .dentry = dget(dentry)};
66 	int err = 0;
67 
68 	err = follow_down(&path);
69 	if (err < 0)
70 		goto out;
71 	if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
72 	    nfsd_mountpoint(dentry, exp) == 2) {
73 		/* This is only a mountpoint in some other namespace */
74 		path_put(&path);
75 		goto out;
76 	}
77 
78 	exp2 = rqst_exp_get_by_name(rqstp, &path);
79 	if (IS_ERR(exp2)) {
80 		err = PTR_ERR(exp2);
81 		/*
82 		 * We normally allow NFS clients to continue
83 		 * "underneath" a mountpoint that is not exported.
84 		 * The exception is V4ROOT, where no traversal is ever
85 		 * allowed without an explicit export of the new
86 		 * directory.
87 		 */
88 		if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
89 			err = 0;
90 		path_put(&path);
91 		goto out;
92 	}
93 	if (nfsd_v4client(rqstp) ||
94 		(exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
95 		/* successfully crossed mount point */
96 		/*
97 		 * This is subtle: path.dentry is *not* on path.mnt
98 		 * at this point.  The only reason we are safe is that
99 		 * original mnt is pinned down by exp, so we should
100 		 * put path *before* putting exp
101 		 */
102 		*dpp = path.dentry;
103 		path.dentry = dentry;
104 		*expp = exp2;
105 		exp2 = exp;
106 	}
107 	path_put(&path);
108 	exp_put(exp2);
109 out:
110 	return err;
111 }
112 
113 static void follow_to_parent(struct path *path)
114 {
115 	struct dentry *dp;
116 
117 	while (path->dentry == path->mnt->mnt_root && follow_up(path))
118 		;
119 	dp = dget_parent(path->dentry);
120 	dput(path->dentry);
121 	path->dentry = dp;
122 }
123 
124 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
125 {
126 	struct svc_export *exp2;
127 	struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
128 			    .dentry = dget(dparent)};
129 
130 	follow_to_parent(&path);
131 
132 	exp2 = rqst_exp_parent(rqstp, &path);
133 	if (PTR_ERR(exp2) == -ENOENT) {
134 		*dentryp = dget(dparent);
135 	} else if (IS_ERR(exp2)) {
136 		path_put(&path);
137 		return PTR_ERR(exp2);
138 	} else {
139 		*dentryp = dget(path.dentry);
140 		exp_put(*exp);
141 		*exp = exp2;
142 	}
143 	path_put(&path);
144 	return 0;
145 }
146 
147 /*
148  * For nfsd purposes, we treat V4ROOT exports as though there was an
149  * export at *every* directory.
150  * We return:
151  * '1' if this dentry *must* be an export point,
152  * '2' if it might be, if there is really a mount here, and
153  * '0' if there is no chance of an export point here.
154  */
155 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
156 {
157 	if (!d_inode(dentry))
158 		return 0;
159 	if (exp->ex_flags & NFSEXP_V4ROOT)
160 		return 1;
161 	if (nfsd4_is_junction(dentry))
162 		return 1;
163 	if (d_mountpoint(dentry))
164 		/*
165 		 * Might only be a mountpoint in a different namespace,
166 		 * but we need to check.
167 		 */
168 		return 2;
169 	return 0;
170 }
171 
172 __be32
173 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
174 		   const char *name, unsigned int len,
175 		   struct svc_export **exp_ret, struct dentry **dentry_ret)
176 {
177 	struct svc_export	*exp;
178 	struct dentry		*dparent;
179 	struct dentry		*dentry;
180 	int			host_err;
181 
182 	dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
183 
184 	dparent = fhp->fh_dentry;
185 	exp = exp_get(fhp->fh_export);
186 
187 	/* Lookup the name, but don't follow links */
188 	if (isdotent(name, len)) {
189 		if (len==1)
190 			dentry = dget(dparent);
191 		else if (dparent != exp->ex_path.dentry)
192 			dentry = dget_parent(dparent);
193 		else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
194 			dentry = dget(dparent); /* .. == . just like at / */
195 		else {
196 			/* checking mountpoint crossing is very different when stepping up */
197 			host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
198 			if (host_err)
199 				goto out_nfserr;
200 		}
201 	} else {
202 		/*
203 		 * In the nfsd4_open() case, this may be held across
204 		 * subsequent open and delegation acquisition which may
205 		 * need to take the child's i_mutex:
206 		 */
207 		fh_lock_nested(fhp, I_MUTEX_PARENT);
208 		dentry = lookup_one_len(name, dparent, len);
209 		host_err = PTR_ERR(dentry);
210 		if (IS_ERR(dentry))
211 			goto out_nfserr;
212 		if (nfsd_mountpoint(dentry, exp)) {
213 			/*
214 			 * We don't need the i_mutex after all.  It's
215 			 * still possible we could open this (regular
216 			 * files can be mountpoints too), but the
217 			 * i_mutex is just there to prevent renames of
218 			 * something that we might be about to delegate,
219 			 * and a mountpoint won't be renamed:
220 			 */
221 			fh_unlock(fhp);
222 			if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
223 				dput(dentry);
224 				goto out_nfserr;
225 			}
226 		}
227 	}
228 	*dentry_ret = dentry;
229 	*exp_ret = exp;
230 	return 0;
231 
232 out_nfserr:
233 	exp_put(exp);
234 	return nfserrno(host_err);
235 }
236 
237 /*
238  * Look up one component of a pathname.
239  * N.B. After this call _both_ fhp and resfh need an fh_put
240  *
241  * If the lookup would cross a mountpoint, and the mounted filesystem
242  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
243  * accepted as it stands and the mounted directory is
244  * returned. Otherwise the covered directory is returned.
245  * NOTE: this mountpoint crossing is not supported properly by all
246  *   clients and is explicitly disallowed for NFSv3
247  *      NeilBrown <neilb@cse.unsw.edu.au>
248  */
249 __be32
250 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
251 				unsigned int len, struct svc_fh *resfh)
252 {
253 	struct svc_export	*exp;
254 	struct dentry		*dentry;
255 	__be32 err;
256 
257 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
258 	if (err)
259 		return err;
260 	err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
261 	if (err)
262 		return err;
263 	err = check_nfsd_access(exp, rqstp);
264 	if (err)
265 		goto out;
266 	/*
267 	 * Note: we compose the file handle now, but as the
268 	 * dentry may be negative, it may need to be updated.
269 	 */
270 	err = fh_compose(resfh, exp, dentry, fhp);
271 	if (!err && d_really_is_negative(dentry))
272 		err = nfserr_noent;
273 out:
274 	dput(dentry);
275 	exp_put(exp);
276 	return err;
277 }
278 
279 /*
280  * Commit metadata changes to stable storage.
281  */
282 static int
283 commit_inode_metadata(struct inode *inode)
284 {
285 	const struct export_operations *export_ops = inode->i_sb->s_export_op;
286 
287 	if (export_ops->commit_metadata)
288 		return export_ops->commit_metadata(inode);
289 	return sync_inode_metadata(inode, 1);
290 }
291 
292 static int
293 commit_metadata(struct svc_fh *fhp)
294 {
295 	struct inode *inode = d_inode(fhp->fh_dentry);
296 
297 	if (!EX_ISSYNC(fhp->fh_export))
298 		return 0;
299 	return commit_inode_metadata(inode);
300 }
301 
302 /*
303  * Go over the attributes and take care of the small differences between
304  * NFS semantics and what Linux expects.
305  */
306 static void
307 nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
308 {
309 	/* sanitize the mode change */
310 	if (iap->ia_valid & ATTR_MODE) {
311 		iap->ia_mode &= S_IALLUGO;
312 		iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
313 	}
314 
315 	/* Revoke setuid/setgid on chown */
316 	if (!S_ISDIR(inode->i_mode) &&
317 	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
318 		iap->ia_valid |= ATTR_KILL_PRIV;
319 		if (iap->ia_valid & ATTR_MODE) {
320 			/* we're setting mode too, just clear the s*id bits */
321 			iap->ia_mode &= ~S_ISUID;
322 			if (iap->ia_mode & S_IXGRP)
323 				iap->ia_mode &= ~S_ISGID;
324 		} else {
325 			/* set ATTR_KILL_* bits and let VFS handle it */
326 			iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
327 		}
328 	}
329 }
330 
331 static __be32
332 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
333 		struct iattr *iap)
334 {
335 	struct inode *inode = d_inode(fhp->fh_dentry);
336 
337 	if (iap->ia_size < inode->i_size) {
338 		__be32 err;
339 
340 		err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
341 				NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
342 		if (err)
343 			return err;
344 	}
345 	return nfserrno(get_write_access(inode));
346 }
347 
348 /*
349  * Set various file attributes.  After this call fhp needs an fh_put.
350  */
351 __be32
352 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
353 	     struct nfsd_attrs *attr,
354 	     int check_guard, time64_t guardtime)
355 {
356 	struct dentry	*dentry;
357 	struct inode	*inode;
358 	struct iattr	*iap = attr->na_iattr;
359 	int		accmode = NFSD_MAY_SATTR;
360 	umode_t		ftype = 0;
361 	__be32		err;
362 	int		host_err;
363 	bool		get_write_count;
364 	bool		size_change = (iap->ia_valid & ATTR_SIZE);
365 
366 	if (iap->ia_valid & ATTR_SIZE) {
367 		accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
368 		ftype = S_IFREG;
369 	}
370 
371 	/*
372 	 * If utimes(2) and friends are called with times not NULL, we should
373 	 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
374 	 * will return EACCES, when the caller's effective UID does not match
375 	 * the owner of the file, and the caller is not privileged. In this
376 	 * situation, we should return EPERM(notify_change will return this).
377 	 */
378 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
379 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
380 		if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
381 			accmode |= NFSD_MAY_WRITE;
382 	}
383 
384 	/* Callers that do fh_verify should do the fh_want_write: */
385 	get_write_count = !fhp->fh_dentry;
386 
387 	/* Get inode */
388 	err = fh_verify(rqstp, fhp, ftype, accmode);
389 	if (err)
390 		return err;
391 	if (get_write_count) {
392 		host_err = fh_want_write(fhp);
393 		if (host_err)
394 			goto out;
395 	}
396 
397 	dentry = fhp->fh_dentry;
398 	inode = d_inode(dentry);
399 
400 	/* Ignore any mode updates on symlinks */
401 	if (S_ISLNK(inode->i_mode))
402 		iap->ia_valid &= ~ATTR_MODE;
403 
404 	if (!iap->ia_valid)
405 		return 0;
406 
407 	nfsd_sanitize_attrs(inode, iap);
408 
409 	if (check_guard && guardtime != inode->i_ctime.tv_sec)
410 		return nfserr_notsync;
411 
412 	/*
413 	 * The size case is special, it changes the file in addition to the
414 	 * attributes, and file systems don't expect it to be mixed with
415 	 * "random" attribute changes.  We thus split out the size change
416 	 * into a separate call to ->setattr, and do the rest as a separate
417 	 * setattr call.
418 	 */
419 	if (size_change) {
420 		err = nfsd_get_write_access(rqstp, fhp, iap);
421 		if (err)
422 			return err;
423 	}
424 
425 	fh_lock(fhp);
426 	if (size_change) {
427 		/*
428 		 * RFC5661, Section 18.30.4:
429 		 *   Changing the size of a file with SETATTR indirectly
430 		 *   changes the time_modify and change attributes.
431 		 *
432 		 * (and similar for the older RFCs)
433 		 */
434 		struct iattr size_attr = {
435 			.ia_valid	= ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
436 			.ia_size	= iap->ia_size,
437 		};
438 
439 		host_err = -EFBIG;
440 		if (iap->ia_size < 0)
441 			goto out_unlock;
442 
443 		host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL);
444 		if (host_err)
445 			goto out_unlock;
446 		iap->ia_valid &= ~ATTR_SIZE;
447 
448 		/*
449 		 * Avoid the additional setattr call below if the only other
450 		 * attribute that the client sends is the mtime, as we update
451 		 * it as part of the size change above.
452 		 */
453 		if ((iap->ia_valid & ~ATTR_MTIME) == 0)
454 			goto out_unlock;
455 	}
456 
457 	iap->ia_valid |= ATTR_CTIME;
458 	host_err = notify_change(&init_user_ns, dentry, iap, NULL);
459 
460 out_unlock:
461 	fh_unlock(fhp);
462 	if (size_change)
463 		put_write_access(inode);
464 out:
465 	if (!host_err)
466 		host_err = commit_metadata(fhp);
467 	return nfserrno(host_err);
468 }
469 
470 #if defined(CONFIG_NFSD_V4)
471 /*
472  * NFS junction information is stored in an extended attribute.
473  */
474 #define NFSD_JUNCTION_XATTR_NAME	XATTR_TRUSTED_PREFIX "junction.nfs"
475 
476 /**
477  * nfsd4_is_junction - Test if an object could be an NFS junction
478  *
479  * @dentry: object to test
480  *
481  * Returns 1 if "dentry" appears to contain NFS junction information.
482  * Otherwise 0 is returned.
483  */
484 int nfsd4_is_junction(struct dentry *dentry)
485 {
486 	struct inode *inode = d_inode(dentry);
487 
488 	if (inode == NULL)
489 		return 0;
490 	if (inode->i_mode & S_IXUGO)
491 		return 0;
492 	if (!(inode->i_mode & S_ISVTX))
493 		return 0;
494 	if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME,
495 			 NULL, 0) <= 0)
496 		return 0;
497 	return 1;
498 }
499 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
500 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
501 		struct xdr_netobj *label)
502 {
503 	__be32 error;
504 	int host_error;
505 	struct dentry *dentry;
506 
507 	error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
508 	if (error)
509 		return error;
510 
511 	dentry = fhp->fh_dentry;
512 
513 	inode_lock(d_inode(dentry));
514 	host_error = security_inode_setsecctx(dentry, label->data, label->len);
515 	inode_unlock(d_inode(dentry));
516 	return nfserrno(host_error);
517 }
518 #else
519 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
520 		struct xdr_netobj *label)
521 {
522 	return nfserr_notsupp;
523 }
524 #endif
525 
526 static struct nfsd4_compound_state *nfsd4_get_cstate(struct svc_rqst *rqstp)
527 {
528 	return &((struct nfsd4_compoundres *)rqstp->rq_resp)->cstate;
529 }
530 
531 __be32 nfsd4_clone_file_range(struct svc_rqst *rqstp,
532 		struct nfsd_file *nf_src, u64 src_pos,
533 		struct nfsd_file *nf_dst, u64 dst_pos,
534 		u64 count, bool sync)
535 {
536 	struct file *src = nf_src->nf_file;
537 	struct file *dst = nf_dst->nf_file;
538 	errseq_t since;
539 	loff_t cloned;
540 	__be32 ret = 0;
541 
542 	since = READ_ONCE(dst->f_wb_err);
543 	cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
544 	if (cloned < 0) {
545 		ret = nfserrno(cloned);
546 		goto out_err;
547 	}
548 	if (count && cloned != count) {
549 		ret = nfserrno(-EINVAL);
550 		goto out_err;
551 	}
552 	if (sync) {
553 		loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
554 		int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
555 
556 		if (!status)
557 			status = filemap_check_wb_err(dst->f_mapping, since);
558 		if (!status)
559 			status = commit_inode_metadata(file_inode(src));
560 		if (status < 0) {
561 			struct nfsd_net *nn = net_generic(nf_dst->nf_net,
562 							  nfsd_net_id);
563 
564 			trace_nfsd_clone_file_range_err(rqstp,
565 					&nfsd4_get_cstate(rqstp)->save_fh,
566 					src_pos,
567 					&nfsd4_get_cstate(rqstp)->current_fh,
568 					dst_pos,
569 					count, status);
570 			nfsd_reset_write_verifier(nn);
571 			trace_nfsd_writeverf_reset(nn, rqstp, status);
572 			ret = nfserrno(status);
573 		}
574 	}
575 out_err:
576 	return ret;
577 }
578 
579 ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
580 			     u64 dst_pos, u64 count)
581 {
582 	ssize_t ret;
583 
584 	/*
585 	 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
586 	 * thread and client rpc slot.  The choice of 4MB is somewhat
587 	 * arbitrary.  We might instead base this on r/wsize, or make it
588 	 * tunable, or use a time instead of a byte limit, or implement
589 	 * asynchronous copy.  In theory a client could also recognize a
590 	 * limit like this and pipeline multiple COPY requests.
591 	 */
592 	count = min_t(u64, count, 1 << 22);
593 	ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
594 
595 	if (ret == -EOPNOTSUPP || ret == -EXDEV)
596 		ret = generic_copy_file_range(src, src_pos, dst, dst_pos,
597 					      count, 0);
598 	return ret;
599 }
600 
601 __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
602 			   struct file *file, loff_t offset, loff_t len,
603 			   int flags)
604 {
605 	int error;
606 
607 	if (!S_ISREG(file_inode(file)->i_mode))
608 		return nfserr_inval;
609 
610 	error = vfs_fallocate(file, flags, offset, len);
611 	if (!error)
612 		error = commit_metadata(fhp);
613 
614 	return nfserrno(error);
615 }
616 #endif /* defined(CONFIG_NFSD_V4) */
617 
618 /*
619  * Check server access rights to a file system object
620  */
621 struct accessmap {
622 	u32		access;
623 	int		how;
624 };
625 static struct accessmap	nfs3_regaccess[] = {
626     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
627     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
628     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_TRUNC	},
629     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE			},
630 
631 #ifdef CONFIG_NFSD_V4
632     {	NFS4_ACCESS_XAREAD,	NFSD_MAY_READ			},
633     {	NFS4_ACCESS_XAWRITE,	NFSD_MAY_WRITE			},
634     {	NFS4_ACCESS_XALIST,	NFSD_MAY_READ			},
635 #endif
636 
637     {	0,			0				}
638 };
639 
640 static struct accessmap	nfs3_diraccess[] = {
641     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
642     {	NFS3_ACCESS_LOOKUP,	NFSD_MAY_EXEC			},
643     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
644     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_EXEC|NFSD_MAY_WRITE	},
645     {	NFS3_ACCESS_DELETE,	NFSD_MAY_REMOVE			},
646 
647 #ifdef CONFIG_NFSD_V4
648     {	NFS4_ACCESS_XAREAD,	NFSD_MAY_READ			},
649     {	NFS4_ACCESS_XAWRITE,	NFSD_MAY_WRITE			},
650     {	NFS4_ACCESS_XALIST,	NFSD_MAY_READ			},
651 #endif
652 
653     {	0,			0				}
654 };
655 
656 static struct accessmap	nfs3_anyaccess[] = {
657 	/* Some clients - Solaris 2.6 at least, make an access call
658 	 * to the server to check for access for things like /dev/null
659 	 * (which really, the server doesn't care about).  So
660 	 * We provide simple access checking for them, looking
661 	 * mainly at mode bits, and we make sure to ignore read-only
662 	 * filesystem checks
663 	 */
664     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
665     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
666     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
667     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
668 
669     {	0,			0				}
670 };
671 
672 __be32
673 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
674 {
675 	struct accessmap	*map;
676 	struct svc_export	*export;
677 	struct dentry		*dentry;
678 	u32			query, result = 0, sresult = 0;
679 	__be32			error;
680 
681 	error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
682 	if (error)
683 		goto out;
684 
685 	export = fhp->fh_export;
686 	dentry = fhp->fh_dentry;
687 
688 	if (d_is_reg(dentry))
689 		map = nfs3_regaccess;
690 	else if (d_is_dir(dentry))
691 		map = nfs3_diraccess;
692 	else
693 		map = nfs3_anyaccess;
694 
695 
696 	query = *access;
697 	for  (; map->access; map++) {
698 		if (map->access & query) {
699 			__be32 err2;
700 
701 			sresult |= map->access;
702 
703 			err2 = nfsd_permission(rqstp, export, dentry, map->how);
704 			switch (err2) {
705 			case nfs_ok:
706 				result |= map->access;
707 				break;
708 
709 			/* the following error codes just mean the access was not allowed,
710 			 * rather than an error occurred */
711 			case nfserr_rofs:
712 			case nfserr_acces:
713 			case nfserr_perm:
714 				/* simply don't "or" in the access bit. */
715 				break;
716 			default:
717 				error = err2;
718 				goto out;
719 			}
720 		}
721 	}
722 	*access = result;
723 	if (supported)
724 		*supported = sresult;
725 
726  out:
727 	return error;
728 }
729 
730 int nfsd_open_break_lease(struct inode *inode, int access)
731 {
732 	unsigned int mode;
733 
734 	if (access & NFSD_MAY_NOT_BREAK_LEASE)
735 		return 0;
736 	mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
737 	return break_lease(inode, mode | O_NONBLOCK);
738 }
739 
740 /*
741  * Open an existing file or directory.
742  * The may_flags argument indicates the type of open (read/write/lock)
743  * and additional flags.
744  * N.B. After this call fhp needs an fh_put
745  */
746 static __be32
747 __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
748 			int may_flags, struct file **filp)
749 {
750 	struct path	path;
751 	struct inode	*inode;
752 	struct file	*file;
753 	int		flags = O_RDONLY|O_LARGEFILE;
754 	__be32		err;
755 	int		host_err = 0;
756 
757 	path.mnt = fhp->fh_export->ex_path.mnt;
758 	path.dentry = fhp->fh_dentry;
759 	inode = d_inode(path.dentry);
760 
761 	err = nfserr_perm;
762 	if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
763 		goto out;
764 
765 	if (!inode->i_fop)
766 		goto out;
767 
768 	host_err = nfsd_open_break_lease(inode, may_flags);
769 	if (host_err) /* NOMEM or WOULDBLOCK */
770 		goto out_nfserr;
771 
772 	if (may_flags & NFSD_MAY_WRITE) {
773 		if (may_flags & NFSD_MAY_READ)
774 			flags = O_RDWR|O_LARGEFILE;
775 		else
776 			flags = O_WRONLY|O_LARGEFILE;
777 	}
778 
779 	file = dentry_open(&path, flags, current_cred());
780 	if (IS_ERR(file)) {
781 		host_err = PTR_ERR(file);
782 		goto out_nfserr;
783 	}
784 
785 	host_err = ima_file_check(file, may_flags);
786 	if (host_err) {
787 		fput(file);
788 		goto out_nfserr;
789 	}
790 
791 	if (may_flags & NFSD_MAY_64BIT_COOKIE)
792 		file->f_mode |= FMODE_64BITHASH;
793 	else
794 		file->f_mode |= FMODE_32BITHASH;
795 
796 	*filp = file;
797 out_nfserr:
798 	err = nfserrno(host_err);
799 out:
800 	return err;
801 }
802 
803 __be32
804 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
805 		int may_flags, struct file **filp)
806 {
807 	__be32 err;
808 	bool retried = false;
809 
810 	validate_process_creds();
811 	/*
812 	 * If we get here, then the client has already done an "open",
813 	 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
814 	 * in case a chmod has now revoked permission.
815 	 *
816 	 * Arguably we should also allow the owner override for
817 	 * directories, but we never have and it doesn't seem to have
818 	 * caused anyone a problem.  If we were to change this, note
819 	 * also that our filldir callbacks would need a variant of
820 	 * lookup_one_len that doesn't check permissions.
821 	 */
822 	if (type == S_IFREG)
823 		may_flags |= NFSD_MAY_OWNER_OVERRIDE;
824 retry:
825 	err = fh_verify(rqstp, fhp, type, may_flags);
826 	if (!err) {
827 		err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
828 		if (err == nfserr_stale && !retried) {
829 			retried = true;
830 			fh_put(fhp);
831 			goto retry;
832 		}
833 	}
834 	validate_process_creds();
835 	return err;
836 }
837 
838 /**
839  * nfsd_open_verified - Open a regular file for the filecache
840  * @rqstp: RPC request
841  * @fhp: NFS filehandle of the file to open
842  * @may_flags: internal permission flags
843  * @filp: OUT: open "struct file *"
844  *
845  * Returns an nfsstat value in network byte order.
846  */
847 __be32
848 nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, int may_flags,
849 		   struct file **filp)
850 {
851 	__be32 err;
852 
853 	validate_process_creds();
854 	err = __nfsd_open(rqstp, fhp, S_IFREG, may_flags, filp);
855 	validate_process_creds();
856 	return err;
857 }
858 
859 /*
860  * Grab and keep cached pages associated with a file in the svc_rqst
861  * so that they can be passed to the network sendmsg/sendpage routines
862  * directly. They will be released after the sending has completed.
863  */
864 static int
865 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
866 		  struct splice_desc *sd)
867 {
868 	struct svc_rqst *rqstp = sd->u.data;
869 
870 	svc_rqst_replace_page(rqstp, buf->page);
871 	if (rqstp->rq_res.page_len == 0)
872 		rqstp->rq_res.page_base = buf->offset;
873 	rqstp->rq_res.page_len += sd->len;
874 	return sd->len;
875 }
876 
877 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
878 				    struct splice_desc *sd)
879 {
880 	return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
881 }
882 
883 static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
884 		size_t expected)
885 {
886 	if (expected != 0 && len == 0)
887 		return 1;
888 	if (offset+len >= i_size_read(file_inode(file)))
889 		return 1;
890 	return 0;
891 }
892 
893 static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
894 			       struct file *file, loff_t offset,
895 			       unsigned long *count, u32 *eof, ssize_t host_err)
896 {
897 	if (host_err >= 0) {
898 		nfsd_stats_io_read_add(fhp->fh_export, host_err);
899 		*eof = nfsd_eof_on_read(file, offset, host_err, *count);
900 		*count = host_err;
901 		fsnotify_access(file);
902 		trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
903 		return 0;
904 	} else {
905 		trace_nfsd_read_err(rqstp, fhp, offset, host_err);
906 		return nfserrno(host_err);
907 	}
908 }
909 
910 __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
911 			struct file *file, loff_t offset, unsigned long *count,
912 			u32 *eof)
913 {
914 	struct splice_desc sd = {
915 		.len		= 0,
916 		.total_len	= *count,
917 		.pos		= offset,
918 		.u.data		= rqstp,
919 	};
920 	ssize_t host_err;
921 
922 	trace_nfsd_read_splice(rqstp, fhp, offset, *count);
923 	rqstp->rq_next_page = rqstp->rq_respages + 1;
924 	host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
925 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
926 }
927 
928 __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
929 		  struct file *file, loff_t offset,
930 		  struct kvec *vec, int vlen, unsigned long *count,
931 		  u32 *eof)
932 {
933 	struct iov_iter iter;
934 	loff_t ppos = offset;
935 	ssize_t host_err;
936 
937 	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
938 	iov_iter_kvec(&iter, READ, vec, vlen, *count);
939 	host_err = vfs_iter_read(file, &iter, &ppos, 0);
940 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
941 }
942 
943 /*
944  * Gathered writes: If another process is currently writing to the file,
945  * there's a high chance this is another nfsd (triggered by a bulk write
946  * from a client's biod). Rather than syncing the file with each write
947  * request, we sleep for 10 msec.
948  *
949  * I don't know if this roughly approximates C. Juszak's idea of
950  * gathered writes, but it's a nice and simple solution (IMHO), and it
951  * seems to work:-)
952  *
953  * Note: we do this only in the NFSv2 case, since v3 and higher have a
954  * better tool (separate unstable writes and commits) for solving this
955  * problem.
956  */
957 static int wait_for_concurrent_writes(struct file *file)
958 {
959 	struct inode *inode = file_inode(file);
960 	static ino_t last_ino;
961 	static dev_t last_dev;
962 	int err = 0;
963 
964 	if (atomic_read(&inode->i_writecount) > 1
965 	    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
966 		dprintk("nfsd: write defer %d\n", task_pid_nr(current));
967 		msleep(10);
968 		dprintk("nfsd: write resume %d\n", task_pid_nr(current));
969 	}
970 
971 	if (inode->i_state & I_DIRTY) {
972 		dprintk("nfsd: write sync %d\n", task_pid_nr(current));
973 		err = vfs_fsync(file, 0);
974 	}
975 	last_ino = inode->i_ino;
976 	last_dev = inode->i_sb->s_dev;
977 	return err;
978 }
979 
980 __be32
981 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
982 				loff_t offset, struct kvec *vec, int vlen,
983 				unsigned long *cnt, int stable,
984 				__be32 *verf)
985 {
986 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
987 	struct file		*file = nf->nf_file;
988 	struct super_block	*sb = file_inode(file)->i_sb;
989 	struct svc_export	*exp;
990 	struct iov_iter		iter;
991 	errseq_t		since;
992 	__be32			nfserr;
993 	int			host_err;
994 	int			use_wgather;
995 	loff_t			pos = offset;
996 	unsigned long		exp_op_flags = 0;
997 	unsigned int		pflags = current->flags;
998 	rwf_t			flags = 0;
999 	bool			restore_flags = false;
1000 
1001 	trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
1002 
1003 	if (sb->s_export_op)
1004 		exp_op_flags = sb->s_export_op->flags;
1005 
1006 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags) &&
1007 	    !(exp_op_flags & EXPORT_OP_REMOTE_FS)) {
1008 		/*
1009 		 * We want throttling in balance_dirty_pages()
1010 		 * and shrink_inactive_list() to only consider
1011 		 * the backingdev we are writing to, so that nfs to
1012 		 * localhost doesn't cause nfsd to lock up due to all
1013 		 * the client's dirty pages or its congested queue.
1014 		 */
1015 		current->flags |= PF_LOCAL_THROTTLE;
1016 		restore_flags = true;
1017 	}
1018 
1019 	exp = fhp->fh_export;
1020 	use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1021 
1022 	if (!EX_ISSYNC(exp))
1023 		stable = NFS_UNSTABLE;
1024 
1025 	if (stable && !use_wgather)
1026 		flags |= RWF_SYNC;
1027 
1028 	iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
1029 	since = READ_ONCE(file->f_wb_err);
1030 	if (verf)
1031 		nfsd_copy_write_verifier(verf, nn);
1032 	host_err = vfs_iter_write(file, &iter, &pos, flags);
1033 	if (host_err < 0) {
1034 		nfsd_reset_write_verifier(nn);
1035 		trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1036 		goto out_nfserr;
1037 	}
1038 	*cnt = host_err;
1039 	nfsd_stats_io_write_add(exp, *cnt);
1040 	fsnotify_modify(file);
1041 	host_err = filemap_check_wb_err(file->f_mapping, since);
1042 	if (host_err < 0)
1043 		goto out_nfserr;
1044 
1045 	if (stable && use_wgather) {
1046 		host_err = wait_for_concurrent_writes(file);
1047 		if (host_err < 0) {
1048 			nfsd_reset_write_verifier(nn);
1049 			trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1050 		}
1051 	}
1052 
1053 out_nfserr:
1054 	if (host_err >= 0) {
1055 		trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1056 		nfserr = nfs_ok;
1057 	} else {
1058 		trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1059 		nfserr = nfserrno(host_err);
1060 	}
1061 	if (restore_flags)
1062 		current_restore_flags(pflags, PF_LOCAL_THROTTLE);
1063 	return nfserr;
1064 }
1065 
1066 /*
1067  * Read data from a file. count must contain the requested read count
1068  * on entry. On return, *count contains the number of bytes actually read.
1069  * N.B. After this call fhp needs an fh_put
1070  */
1071 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1072 	loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
1073 	u32 *eof)
1074 {
1075 	struct nfsd_file	*nf;
1076 	struct file *file;
1077 	__be32 err;
1078 
1079 	trace_nfsd_read_start(rqstp, fhp, offset, *count);
1080 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
1081 	if (err)
1082 		return err;
1083 
1084 	file = nf->nf_file;
1085 	if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
1086 		err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
1087 	else
1088 		err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
1089 
1090 	nfsd_file_put(nf);
1091 
1092 	trace_nfsd_read_done(rqstp, fhp, offset, *count);
1093 
1094 	return err;
1095 }
1096 
1097 /*
1098  * Write data to a file.
1099  * The stable flag requests synchronous writes.
1100  * N.B. After this call fhp needs an fh_put
1101  */
1102 __be32
1103 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1104 	   struct kvec *vec, int vlen, unsigned long *cnt, int stable,
1105 	   __be32 *verf)
1106 {
1107 	struct nfsd_file *nf;
1108 	__be32 err;
1109 
1110 	trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
1111 
1112 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
1113 	if (err)
1114 		goto out;
1115 
1116 	err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
1117 			vlen, cnt, stable, verf);
1118 	nfsd_file_put(nf);
1119 out:
1120 	trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
1121 	return err;
1122 }
1123 
1124 /**
1125  * nfsd_commit - Commit pending writes to stable storage
1126  * @rqstp: RPC request being processed
1127  * @fhp: NFS filehandle
1128  * @offset: raw offset from beginning of file
1129  * @count: raw count of bytes to sync
1130  * @verf: filled in with the server's current write verifier
1131  *
1132  * Note: we guarantee that data that lies within the range specified
1133  * by the 'offset' and 'count' parameters will be synced. The server
1134  * is permitted to sync data that lies outside this range at the
1135  * same time.
1136  *
1137  * Unfortunately we cannot lock the file to make sure we return full WCC
1138  * data to the client, as locking happens lower down in the filesystem.
1139  *
1140  * Return values:
1141  *   An nfsstat value in network byte order.
1142  */
1143 __be32
1144 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, u64 offset,
1145 	    u32 count, __be32 *verf)
1146 {
1147 	u64			maxbytes;
1148 	loff_t			start, end;
1149 	struct nfsd_net		*nn;
1150 	struct nfsd_file	*nf;
1151 	__be32			err;
1152 
1153 	err = nfsd_file_acquire(rqstp, fhp,
1154 			NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
1155 	if (err)
1156 		goto out;
1157 
1158 	/*
1159 	 * Convert the client-provided (offset, count) range to a
1160 	 * (start, end) range. If the client-provided range falls
1161 	 * outside the maximum file size of the underlying FS,
1162 	 * clamp the sync range appropriately.
1163 	 */
1164 	start = 0;
1165 	end = LLONG_MAX;
1166 	maxbytes = (u64)fhp->fh_dentry->d_sb->s_maxbytes;
1167 	if (offset < maxbytes) {
1168 		start = offset;
1169 		if (count && (offset + count - 1 < maxbytes))
1170 			end = offset + count - 1;
1171 	}
1172 
1173 	nn = net_generic(nf->nf_net, nfsd_net_id);
1174 	if (EX_ISSYNC(fhp->fh_export)) {
1175 		errseq_t since = READ_ONCE(nf->nf_file->f_wb_err);
1176 		int err2;
1177 
1178 		err2 = vfs_fsync_range(nf->nf_file, start, end, 0);
1179 		switch (err2) {
1180 		case 0:
1181 			nfsd_copy_write_verifier(verf, nn);
1182 			err2 = filemap_check_wb_err(nf->nf_file->f_mapping,
1183 						    since);
1184 			err = nfserrno(err2);
1185 			break;
1186 		case -EINVAL:
1187 			err = nfserr_notsupp;
1188 			break;
1189 		default:
1190 			nfsd_reset_write_verifier(nn);
1191 			trace_nfsd_writeverf_reset(nn, rqstp, err2);
1192 			err = nfserrno(err2);
1193 		}
1194 	} else
1195 		nfsd_copy_write_verifier(verf, nn);
1196 
1197 	nfsd_file_put(nf);
1198 out:
1199 	return err;
1200 }
1201 
1202 /**
1203  * nfsd_create_setattr - Set a created file's attributes
1204  * @rqstp: RPC transaction being executed
1205  * @fhp: NFS filehandle of parent directory
1206  * @resfhp: NFS filehandle of new object
1207  * @attrs: requested attributes of new object
1208  *
1209  * Returns nfs_ok on success, or an nfsstat in network byte order.
1210  */
1211 __be32
1212 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
1213 		    struct svc_fh *resfhp, struct nfsd_attrs *attrs)
1214 {
1215 	struct iattr *iap = attrs->na_iattr;
1216 	__be32 status;
1217 
1218 	/*
1219 	 * Mode has already been set by file creation.
1220 	 */
1221 	iap->ia_valid &= ~ATTR_MODE;
1222 
1223 	/*
1224 	 * Setting uid/gid works only for root.  Irix appears to
1225 	 * send along the gid on create when it tries to implement
1226 	 * setgid directories via NFS:
1227 	 */
1228 	if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1229 		iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1230 
1231 	/*
1232 	 * Callers expect new file metadata to be committed even
1233 	 * if the attributes have not changed.
1234 	 */
1235 	if (iap->ia_valid)
1236 		status = nfsd_setattr(rqstp, resfhp, attrs, 0, (time64_t)0);
1237 	else
1238 		status = nfserrno(commit_metadata(resfhp));
1239 
1240 	/*
1241 	 * Transactional filesystems had a chance to commit changes
1242 	 * for both parent and child simultaneously making the
1243 	 * following commit_metadata a noop in many cases.
1244 	 */
1245 	if (!status)
1246 		status = nfserrno(commit_metadata(fhp));
1247 
1248 	/*
1249 	 * Update the new filehandle to pick up the new attributes.
1250 	 */
1251 	if (!status)
1252 		status = fh_update(resfhp);
1253 
1254 	return status;
1255 }
1256 
1257 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1258  * setting size to 0 may fail for some specific file systems by the permission
1259  * checking which requires WRITE permission but the mode is 000.
1260  * we ignore the resizing(to 0) on the just new created file, since the size is
1261  * 0 after file created.
1262  *
1263  * call this only after vfs_create() is called.
1264  * */
1265 static void
1266 nfsd_check_ignore_resizing(struct iattr *iap)
1267 {
1268 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1269 		iap->ia_valid &= ~ATTR_SIZE;
1270 }
1271 
1272 /* The parent directory should already be locked: */
1273 __be32
1274 nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
1275 		   char *fname, int flen, struct nfsd_attrs *attrs,
1276 		   int type, dev_t rdev, struct svc_fh *resfhp)
1277 {
1278 	struct dentry	*dentry, *dchild;
1279 	struct inode	*dirp;
1280 	struct iattr	*iap = attrs->na_iattr;
1281 	__be32		err;
1282 	int		host_err;
1283 
1284 	dentry = fhp->fh_dentry;
1285 	dirp = d_inode(dentry);
1286 
1287 	dchild = dget(resfhp->fh_dentry);
1288 	if (!fhp->fh_locked) {
1289 		WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1290 				dentry);
1291 		err = nfserr_io;
1292 		goto out;
1293 	}
1294 
1295 	err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1296 	if (err)
1297 		goto out;
1298 
1299 	if (!(iap->ia_valid & ATTR_MODE))
1300 		iap->ia_mode = 0;
1301 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1302 
1303 	if (!IS_POSIXACL(dirp))
1304 		iap->ia_mode &= ~current_umask();
1305 
1306 	err = 0;
1307 	host_err = 0;
1308 	switch (type) {
1309 	case S_IFREG:
1310 		host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
1311 		if (!host_err)
1312 			nfsd_check_ignore_resizing(iap);
1313 		break;
1314 	case S_IFDIR:
1315 		host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode);
1316 		if (!host_err && unlikely(d_unhashed(dchild))) {
1317 			struct dentry *d;
1318 			d = lookup_one_len(dchild->d_name.name,
1319 					   dchild->d_parent,
1320 					   dchild->d_name.len);
1321 			if (IS_ERR(d)) {
1322 				host_err = PTR_ERR(d);
1323 				break;
1324 			}
1325 			if (unlikely(d_is_negative(d))) {
1326 				dput(d);
1327 				err = nfserr_serverfault;
1328 				goto out;
1329 			}
1330 			dput(resfhp->fh_dentry);
1331 			resfhp->fh_dentry = dget(d);
1332 			err = fh_update(resfhp);
1333 			dput(dchild);
1334 			dchild = d;
1335 			if (err)
1336 				goto out;
1337 		}
1338 		break;
1339 	case S_IFCHR:
1340 	case S_IFBLK:
1341 	case S_IFIFO:
1342 	case S_IFSOCK:
1343 		host_err = vfs_mknod(&init_user_ns, dirp, dchild,
1344 				     iap->ia_mode, rdev);
1345 		break;
1346 	default:
1347 		printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1348 		       type);
1349 		host_err = -EINVAL;
1350 	}
1351 	if (host_err < 0)
1352 		goto out_nfserr;
1353 
1354 	err = nfsd_create_setattr(rqstp, fhp, resfhp, attrs);
1355 
1356 out:
1357 	dput(dchild);
1358 	return err;
1359 
1360 out_nfserr:
1361 	err = nfserrno(host_err);
1362 	goto out;
1363 }
1364 
1365 /*
1366  * Create a filesystem object (regular, directory, special).
1367  * Note that the parent directory is left locked.
1368  *
1369  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1370  */
1371 __be32
1372 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1373 	    char *fname, int flen, struct nfsd_attrs *attrs,
1374 	    int type, dev_t rdev, struct svc_fh *resfhp)
1375 {
1376 	struct dentry	*dentry, *dchild = NULL;
1377 	__be32		err;
1378 	int		host_err;
1379 
1380 	if (isdotent(fname, flen))
1381 		return nfserr_exist;
1382 
1383 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1384 	if (err)
1385 		return err;
1386 
1387 	dentry = fhp->fh_dentry;
1388 
1389 	host_err = fh_want_write(fhp);
1390 	if (host_err)
1391 		return nfserrno(host_err);
1392 
1393 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1394 	dchild = lookup_one_len(fname, dentry, flen);
1395 	host_err = PTR_ERR(dchild);
1396 	if (IS_ERR(dchild))
1397 		return nfserrno(host_err);
1398 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1399 	/*
1400 	 * We unconditionally drop our ref to dchild as fh_compose will have
1401 	 * already grabbed its own ref for it.
1402 	 */
1403 	dput(dchild);
1404 	if (err)
1405 		return err;
1406 	return nfsd_create_locked(rqstp, fhp, fname, flen, attrs, type,
1407 					rdev, resfhp);
1408 }
1409 
1410 /*
1411  * Read a symlink. On entry, *lenp must contain the maximum path length that
1412  * fits into the buffer. On return, it contains the true length.
1413  * N.B. After this call fhp needs an fh_put
1414  */
1415 __be32
1416 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1417 {
1418 	__be32		err;
1419 	const char *link;
1420 	struct path path;
1421 	DEFINE_DELAYED_CALL(done);
1422 	int len;
1423 
1424 	err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1425 	if (unlikely(err))
1426 		return err;
1427 
1428 	path.mnt = fhp->fh_export->ex_path.mnt;
1429 	path.dentry = fhp->fh_dentry;
1430 
1431 	if (unlikely(!d_is_symlink(path.dentry)))
1432 		return nfserr_inval;
1433 
1434 	touch_atime(&path);
1435 
1436 	link = vfs_get_link(path.dentry, &done);
1437 	if (IS_ERR(link))
1438 		return nfserrno(PTR_ERR(link));
1439 
1440 	len = strlen(link);
1441 	if (len < *lenp)
1442 		*lenp = len;
1443 	memcpy(buf, link, *lenp);
1444 	do_delayed_call(&done);
1445 	return 0;
1446 }
1447 
1448 /*
1449  * Create a symlink and look up its inode
1450  * N.B. After this call _both_ fhp and resfhp need an fh_put
1451  */
1452 __be32
1453 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1454 				char *fname, int flen,
1455 				char *path,
1456 				struct svc_fh *resfhp)
1457 {
1458 	struct dentry	*dentry, *dnew;
1459 	__be32		err, cerr;
1460 	int		host_err;
1461 
1462 	err = nfserr_noent;
1463 	if (!flen || path[0] == '\0')
1464 		goto out;
1465 	err = nfserr_exist;
1466 	if (isdotent(fname, flen))
1467 		goto out;
1468 
1469 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1470 	if (err)
1471 		goto out;
1472 
1473 	host_err = fh_want_write(fhp);
1474 	if (host_err)
1475 		goto out_nfserr;
1476 
1477 	fh_lock(fhp);
1478 	dentry = fhp->fh_dentry;
1479 	dnew = lookup_one_len(fname, dentry, flen);
1480 	host_err = PTR_ERR(dnew);
1481 	if (IS_ERR(dnew))
1482 		goto out_nfserr;
1483 
1484 	host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
1485 	err = nfserrno(host_err);
1486 	fh_unlock(fhp);
1487 	if (!err)
1488 		err = nfserrno(commit_metadata(fhp));
1489 
1490 	fh_drop_write(fhp);
1491 
1492 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1493 	dput(dnew);
1494 	if (err==0) err = cerr;
1495 out:
1496 	return err;
1497 
1498 out_nfserr:
1499 	err = nfserrno(host_err);
1500 	goto out;
1501 }
1502 
1503 /*
1504  * Create a hardlink
1505  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1506  */
1507 __be32
1508 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1509 				char *name, int len, struct svc_fh *tfhp)
1510 {
1511 	struct dentry	*ddir, *dnew, *dold;
1512 	struct inode	*dirp;
1513 	__be32		err;
1514 	int		host_err;
1515 
1516 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1517 	if (err)
1518 		goto out;
1519 	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1520 	if (err)
1521 		goto out;
1522 	err = nfserr_isdir;
1523 	if (d_is_dir(tfhp->fh_dentry))
1524 		goto out;
1525 	err = nfserr_perm;
1526 	if (!len)
1527 		goto out;
1528 	err = nfserr_exist;
1529 	if (isdotent(name, len))
1530 		goto out;
1531 
1532 	host_err = fh_want_write(tfhp);
1533 	if (host_err) {
1534 		err = nfserrno(host_err);
1535 		goto out;
1536 	}
1537 
1538 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
1539 	ddir = ffhp->fh_dentry;
1540 	dirp = d_inode(ddir);
1541 
1542 	dnew = lookup_one_len(name, ddir, len);
1543 	host_err = PTR_ERR(dnew);
1544 	if (IS_ERR(dnew))
1545 		goto out_nfserr;
1546 
1547 	dold = tfhp->fh_dentry;
1548 
1549 	err = nfserr_noent;
1550 	if (d_really_is_negative(dold))
1551 		goto out_dput;
1552 	host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
1553 	fh_unlock(ffhp);
1554 	if (!host_err) {
1555 		err = nfserrno(commit_metadata(ffhp));
1556 		if (!err)
1557 			err = nfserrno(commit_metadata(tfhp));
1558 	} else {
1559 		if (host_err == -EXDEV && rqstp->rq_vers == 2)
1560 			err = nfserr_acces;
1561 		else
1562 			err = nfserrno(host_err);
1563 	}
1564 out_dput:
1565 	dput(dnew);
1566 out_unlock:
1567 	fh_unlock(ffhp);
1568 	fh_drop_write(tfhp);
1569 out:
1570 	return err;
1571 
1572 out_nfserr:
1573 	err = nfserrno(host_err);
1574 	goto out_unlock;
1575 }
1576 
1577 static void
1578 nfsd_close_cached_files(struct dentry *dentry)
1579 {
1580 	struct inode *inode = d_inode(dentry);
1581 
1582 	if (inode && S_ISREG(inode->i_mode))
1583 		nfsd_file_close_inode_sync(inode);
1584 }
1585 
1586 static bool
1587 nfsd_has_cached_files(struct dentry *dentry)
1588 {
1589 	bool		ret = false;
1590 	struct inode *inode = d_inode(dentry);
1591 
1592 	if (inode && S_ISREG(inode->i_mode))
1593 		ret = nfsd_file_is_cached(inode);
1594 	return ret;
1595 }
1596 
1597 /*
1598  * Rename a file
1599  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1600  */
1601 __be32
1602 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1603 			    struct svc_fh *tfhp, char *tname, int tlen)
1604 {
1605 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
1606 	struct inode	*fdir, *tdir;
1607 	__be32		err;
1608 	int		host_err;
1609 	bool		close_cached = false;
1610 
1611 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1612 	if (err)
1613 		goto out;
1614 	err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1615 	if (err)
1616 		goto out;
1617 
1618 	fdentry = ffhp->fh_dentry;
1619 	fdir = d_inode(fdentry);
1620 
1621 	tdentry = tfhp->fh_dentry;
1622 	tdir = d_inode(tdentry);
1623 
1624 	err = nfserr_perm;
1625 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1626 		goto out;
1627 
1628 retry:
1629 	host_err = fh_want_write(ffhp);
1630 	if (host_err) {
1631 		err = nfserrno(host_err);
1632 		goto out;
1633 	}
1634 
1635 	/* cannot use fh_lock as we need deadlock protective ordering
1636 	 * so do it by hand */
1637 	trap = lock_rename(tdentry, fdentry);
1638 	ffhp->fh_locked = tfhp->fh_locked = true;
1639 	fh_fill_pre_attrs(ffhp);
1640 	fh_fill_pre_attrs(tfhp);
1641 
1642 	odentry = lookup_one_len(fname, fdentry, flen);
1643 	host_err = PTR_ERR(odentry);
1644 	if (IS_ERR(odentry))
1645 		goto out_nfserr;
1646 
1647 	host_err = -ENOENT;
1648 	if (d_really_is_negative(odentry))
1649 		goto out_dput_old;
1650 	host_err = -EINVAL;
1651 	if (odentry == trap)
1652 		goto out_dput_old;
1653 
1654 	ndentry = lookup_one_len(tname, tdentry, tlen);
1655 	host_err = PTR_ERR(ndentry);
1656 	if (IS_ERR(ndentry))
1657 		goto out_dput_old;
1658 	host_err = -ENOTEMPTY;
1659 	if (ndentry == trap)
1660 		goto out_dput_new;
1661 
1662 	host_err = -EXDEV;
1663 	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1664 		goto out_dput_new;
1665 	if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1666 		goto out_dput_new;
1667 
1668 	if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) &&
1669 	    nfsd_has_cached_files(ndentry)) {
1670 		close_cached = true;
1671 		goto out_dput_old;
1672 	} else {
1673 		struct renamedata rd = {
1674 			.old_mnt_userns	= &init_user_ns,
1675 			.old_dir	= fdir,
1676 			.old_dentry	= odentry,
1677 			.new_mnt_userns	= &init_user_ns,
1678 			.new_dir	= tdir,
1679 			.new_dentry	= ndentry,
1680 		};
1681 		host_err = vfs_rename(&rd);
1682 		if (!host_err) {
1683 			host_err = commit_metadata(tfhp);
1684 			if (!host_err)
1685 				host_err = commit_metadata(ffhp);
1686 		}
1687 	}
1688  out_dput_new:
1689 	dput(ndentry);
1690  out_dput_old:
1691 	dput(odentry);
1692  out_nfserr:
1693 	err = nfserrno(host_err);
1694 	/*
1695 	 * We cannot rely on fh_unlock on the two filehandles,
1696 	 * as that would do the wrong thing if the two directories
1697 	 * were the same, so again we do it by hand.
1698 	 */
1699 	if (!close_cached) {
1700 		fh_fill_post_attrs(ffhp);
1701 		fh_fill_post_attrs(tfhp);
1702 	}
1703 	unlock_rename(tdentry, fdentry);
1704 	ffhp->fh_locked = tfhp->fh_locked = false;
1705 	fh_drop_write(ffhp);
1706 
1707 	/*
1708 	 * If the target dentry has cached open files, then we need to try to
1709 	 * close them prior to doing the rename. Flushing delayed fput
1710 	 * shouldn't be done with locks held however, so we delay it until this
1711 	 * point and then reattempt the whole shebang.
1712 	 */
1713 	if (close_cached) {
1714 		close_cached = false;
1715 		nfsd_close_cached_files(ndentry);
1716 		dput(ndentry);
1717 		goto retry;
1718 	}
1719 out:
1720 	return err;
1721 }
1722 
1723 /*
1724  * Unlink a file or directory
1725  * N.B. After this call fhp needs an fh_put
1726  */
1727 __be32
1728 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1729 				char *fname, int flen)
1730 {
1731 	struct dentry	*dentry, *rdentry;
1732 	struct inode	*dirp;
1733 	struct inode	*rinode;
1734 	__be32		err;
1735 	int		host_err;
1736 
1737 	err = nfserr_acces;
1738 	if (!flen || isdotent(fname, flen))
1739 		goto out;
1740 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1741 	if (err)
1742 		goto out;
1743 
1744 	host_err = fh_want_write(fhp);
1745 	if (host_err)
1746 		goto out_nfserr;
1747 
1748 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1749 	dentry = fhp->fh_dentry;
1750 	dirp = d_inode(dentry);
1751 
1752 	rdentry = lookup_one_len(fname, dentry, flen);
1753 	host_err = PTR_ERR(rdentry);
1754 	if (IS_ERR(rdentry))
1755 		goto out_drop_write;
1756 
1757 	if (d_really_is_negative(rdentry)) {
1758 		dput(rdentry);
1759 		host_err = -ENOENT;
1760 		goto out_drop_write;
1761 	}
1762 	rinode = d_inode(rdentry);
1763 	ihold(rinode);
1764 
1765 	if (!type)
1766 		type = d_inode(rdentry)->i_mode & S_IFMT;
1767 
1768 	if (type != S_IFDIR) {
1769 		if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK)
1770 			nfsd_close_cached_files(rdentry);
1771 		host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL);
1772 	} else {
1773 		host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
1774 	}
1775 
1776 	fh_unlock(fhp);
1777 	if (!host_err)
1778 		host_err = commit_metadata(fhp);
1779 	dput(rdentry);
1780 	iput(rinode);    /* truncate the inode here */
1781 
1782 out_drop_write:
1783 	fh_drop_write(fhp);
1784 out_nfserr:
1785 	if (host_err == -EBUSY) {
1786 		/* name is mounted-on. There is no perfect
1787 		 * error status.
1788 		 */
1789 		if (nfsd_v4client(rqstp))
1790 			err = nfserr_file_open;
1791 		else
1792 			err = nfserr_acces;
1793 	} else {
1794 		err = nfserrno(host_err);
1795 	}
1796 out:
1797 	return err;
1798 }
1799 
1800 /*
1801  * We do this buffering because we must not call back into the file
1802  * system's ->lookup() method from the filldir callback. That may well
1803  * deadlock a number of file systems.
1804  *
1805  * This is based heavily on the implementation of same in XFS.
1806  */
1807 struct buffered_dirent {
1808 	u64		ino;
1809 	loff_t		offset;
1810 	int		namlen;
1811 	unsigned int	d_type;
1812 	char		name[];
1813 };
1814 
1815 struct readdir_data {
1816 	struct dir_context ctx;
1817 	char		*dirent;
1818 	size_t		used;
1819 	int		full;
1820 };
1821 
1822 static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1823 				 int namlen, loff_t offset, u64 ino,
1824 				 unsigned int d_type)
1825 {
1826 	struct readdir_data *buf =
1827 		container_of(ctx, struct readdir_data, ctx);
1828 	struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1829 	unsigned int reclen;
1830 
1831 	reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1832 	if (buf->used + reclen > PAGE_SIZE) {
1833 		buf->full = 1;
1834 		return -EINVAL;
1835 	}
1836 
1837 	de->namlen = namlen;
1838 	de->offset = offset;
1839 	de->ino = ino;
1840 	de->d_type = d_type;
1841 	memcpy(de->name, name, namlen);
1842 	buf->used += reclen;
1843 
1844 	return 0;
1845 }
1846 
1847 static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp,
1848 				    nfsd_filldir_t func, struct readdir_cd *cdp,
1849 				    loff_t *offsetp)
1850 {
1851 	struct buffered_dirent *de;
1852 	int host_err;
1853 	int size;
1854 	loff_t offset;
1855 	struct readdir_data buf = {
1856 		.ctx.actor = nfsd_buffered_filldir,
1857 		.dirent = (void *)__get_free_page(GFP_KERNEL)
1858 	};
1859 
1860 	if (!buf.dirent)
1861 		return nfserrno(-ENOMEM);
1862 
1863 	offset = *offsetp;
1864 
1865 	while (1) {
1866 		unsigned int reclen;
1867 
1868 		cdp->err = nfserr_eof; /* will be cleared on successful read */
1869 		buf.used = 0;
1870 		buf.full = 0;
1871 
1872 		host_err = iterate_dir(file, &buf.ctx);
1873 		if (buf.full)
1874 			host_err = 0;
1875 
1876 		if (host_err < 0)
1877 			break;
1878 
1879 		size = buf.used;
1880 
1881 		if (!size)
1882 			break;
1883 
1884 		de = (struct buffered_dirent *)buf.dirent;
1885 		while (size > 0) {
1886 			offset = de->offset;
1887 
1888 			if (func(cdp, de->name, de->namlen, de->offset,
1889 				 de->ino, de->d_type))
1890 				break;
1891 
1892 			if (cdp->err != nfs_ok)
1893 				break;
1894 
1895 			trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen);
1896 
1897 			reclen = ALIGN(sizeof(*de) + de->namlen,
1898 				       sizeof(u64));
1899 			size -= reclen;
1900 			de = (struct buffered_dirent *)((char *)de + reclen);
1901 		}
1902 		if (size > 0) /* We bailed out early */
1903 			break;
1904 
1905 		offset = vfs_llseek(file, 0, SEEK_CUR);
1906 	}
1907 
1908 	free_page((unsigned long)(buf.dirent));
1909 
1910 	if (host_err)
1911 		return nfserrno(host_err);
1912 
1913 	*offsetp = offset;
1914 	return cdp->err;
1915 }
1916 
1917 /*
1918  * Read entries from a directory.
1919  * The  NFSv3/4 verifier we ignore for now.
1920  */
1921 __be32
1922 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1923 	     struct readdir_cd *cdp, nfsd_filldir_t func)
1924 {
1925 	__be32		err;
1926 	struct file	*file;
1927 	loff_t		offset = *offsetp;
1928 	int             may_flags = NFSD_MAY_READ;
1929 
1930 	/* NFSv2 only supports 32 bit cookies */
1931 	if (rqstp->rq_vers > 2)
1932 		may_flags |= NFSD_MAY_64BIT_COOKIE;
1933 
1934 	err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
1935 	if (err)
1936 		goto out;
1937 
1938 	offset = vfs_llseek(file, offset, SEEK_SET);
1939 	if (offset < 0) {
1940 		err = nfserrno((int)offset);
1941 		goto out_close;
1942 	}
1943 
1944 	err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp);
1945 
1946 	if (err == nfserr_eof || err == nfserr_toosmall)
1947 		err = nfs_ok; /* can still be found in ->err */
1948 out_close:
1949 	fput(file);
1950 out:
1951 	return err;
1952 }
1953 
1954 /*
1955  * Get file system stats
1956  * N.B. After this call fhp needs an fh_put
1957  */
1958 __be32
1959 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
1960 {
1961 	__be32 err;
1962 
1963 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
1964 	if (!err) {
1965 		struct path path = {
1966 			.mnt	= fhp->fh_export->ex_path.mnt,
1967 			.dentry	= fhp->fh_dentry,
1968 		};
1969 		if (vfs_statfs(&path, stat))
1970 			err = nfserr_io;
1971 	}
1972 	return err;
1973 }
1974 
1975 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
1976 {
1977 	return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
1978 }
1979 
1980 #ifdef CONFIG_NFSD_V4
1981 /*
1982  * Helper function to translate error numbers. In the case of xattr operations,
1983  * some error codes need to be translated outside of the standard translations.
1984  *
1985  * ENODATA needs to be translated to nfserr_noxattr.
1986  * E2BIG to nfserr_xattr2big.
1987  *
1988  * Additionally, vfs_listxattr can return -ERANGE. This means that the
1989  * file has too many extended attributes to retrieve inside an
1990  * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
1991  * filesystems will allow the adding of extended attributes until they hit
1992  * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
1993  * So, at that point, the attributes are present and valid, but can't
1994  * be retrieved using listxattr, since the upper level xattr code enforces
1995  * the XATTR_LIST_MAX limit.
1996  *
1997  * This bug means that we need to deal with listxattr returning -ERANGE. The
1998  * best mapping is to return TOOSMALL.
1999  */
2000 static __be32
2001 nfsd_xattr_errno(int err)
2002 {
2003 	switch (err) {
2004 	case -ENODATA:
2005 		return nfserr_noxattr;
2006 	case -E2BIG:
2007 		return nfserr_xattr2big;
2008 	case -ERANGE:
2009 		return nfserr_toosmall;
2010 	}
2011 	return nfserrno(err);
2012 }
2013 
2014 /*
2015  * Retrieve the specified user extended attribute. To avoid always
2016  * having to allocate the maximum size (since we are not getting
2017  * a maximum size from the RPC), do a probe + alloc. Hold a reader
2018  * lock on i_rwsem to prevent the extended attribute from changing
2019  * size while we're doing this.
2020  */
2021 __be32
2022 nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2023 	      void **bufp, int *lenp)
2024 {
2025 	ssize_t len;
2026 	__be32 err;
2027 	char *buf;
2028 	struct inode *inode;
2029 	struct dentry *dentry;
2030 
2031 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2032 	if (err)
2033 		return err;
2034 
2035 	err = nfs_ok;
2036 	dentry = fhp->fh_dentry;
2037 	inode = d_inode(dentry);
2038 
2039 	inode_lock_shared(inode);
2040 
2041 	len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
2042 
2043 	/*
2044 	 * Zero-length attribute, just return.
2045 	 */
2046 	if (len == 0) {
2047 		*bufp = NULL;
2048 		*lenp = 0;
2049 		goto out;
2050 	}
2051 
2052 	if (len < 0) {
2053 		err = nfsd_xattr_errno(len);
2054 		goto out;
2055 	}
2056 
2057 	if (len > *lenp) {
2058 		err = nfserr_toosmall;
2059 		goto out;
2060 	}
2061 
2062 	buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2063 	if (buf == NULL) {
2064 		err = nfserr_jukebox;
2065 		goto out;
2066 	}
2067 
2068 	len = vfs_getxattr(&init_user_ns, dentry, name, buf, len);
2069 	if (len <= 0) {
2070 		kvfree(buf);
2071 		buf = NULL;
2072 		err = nfsd_xattr_errno(len);
2073 	}
2074 
2075 	*lenp = len;
2076 	*bufp = buf;
2077 
2078 out:
2079 	inode_unlock_shared(inode);
2080 
2081 	return err;
2082 }
2083 
2084 /*
2085  * Retrieve the xattr names. Since we can't know how many are
2086  * user extended attributes, we must get all attributes here,
2087  * and have the XDR encode filter out the "user." ones.
2088  *
2089  * While this could always just allocate an XATTR_LIST_MAX
2090  * buffer, that's a waste, so do a probe + allocate. To
2091  * avoid any changes between the probe and allocate, wrap
2092  * this in inode_lock.
2093  */
2094 __be32
2095 nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
2096 	       int *lenp)
2097 {
2098 	ssize_t len;
2099 	__be32 err;
2100 	char *buf;
2101 	struct inode *inode;
2102 	struct dentry *dentry;
2103 
2104 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2105 	if (err)
2106 		return err;
2107 
2108 	dentry = fhp->fh_dentry;
2109 	inode = d_inode(dentry);
2110 	*lenp = 0;
2111 
2112 	inode_lock_shared(inode);
2113 
2114 	len = vfs_listxattr(dentry, NULL, 0);
2115 	if (len <= 0) {
2116 		err = nfsd_xattr_errno(len);
2117 		goto out;
2118 	}
2119 
2120 	if (len > XATTR_LIST_MAX) {
2121 		err = nfserr_xattr2big;
2122 		goto out;
2123 	}
2124 
2125 	/*
2126 	 * We're holding i_rwsem - use GFP_NOFS.
2127 	 */
2128 	buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2129 	if (buf == NULL) {
2130 		err = nfserr_jukebox;
2131 		goto out;
2132 	}
2133 
2134 	len = vfs_listxattr(dentry, buf, len);
2135 	if (len <= 0) {
2136 		kvfree(buf);
2137 		err = nfsd_xattr_errno(len);
2138 		goto out;
2139 	}
2140 
2141 	*lenp = len;
2142 	*bufp = buf;
2143 
2144 	err = nfs_ok;
2145 out:
2146 	inode_unlock_shared(inode);
2147 
2148 	return err;
2149 }
2150 
2151 /*
2152  * Removexattr and setxattr need to call fh_lock to both lock the inode
2153  * and set the change attribute. Since the top-level vfs_removexattr
2154  * and vfs_setxattr calls already do their own inode_lock calls, call
2155  * the _locked variant. Pass in a NULL pointer for delegated_inode,
2156  * and let the client deal with NFS4ERR_DELAY (same as with e.g.
2157  * setattr and remove).
2158  */
2159 __be32
2160 nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
2161 {
2162 	__be32 err;
2163 	int ret;
2164 
2165 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2166 	if (err)
2167 		return err;
2168 
2169 	ret = fh_want_write(fhp);
2170 	if (ret)
2171 		return nfserrno(ret);
2172 
2173 	fh_lock(fhp);
2174 
2175 	ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
2176 				       name, NULL);
2177 
2178 	fh_unlock(fhp);
2179 	fh_drop_write(fhp);
2180 
2181 	return nfsd_xattr_errno(ret);
2182 }
2183 
2184 __be32
2185 nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2186 	      void *buf, u32 len, u32 flags)
2187 {
2188 	__be32 err;
2189 	int ret;
2190 
2191 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2192 	if (err)
2193 		return err;
2194 
2195 	ret = fh_want_write(fhp);
2196 	if (ret)
2197 		return nfserrno(ret);
2198 	fh_lock(fhp);
2199 
2200 	ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
2201 				    len, flags, NULL);
2202 
2203 	fh_unlock(fhp);
2204 	fh_drop_write(fhp);
2205 
2206 	return nfsd_xattr_errno(ret);
2207 }
2208 #endif
2209 
2210 /*
2211  * Check for a user's access permissions to this inode.
2212  */
2213 __be32
2214 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2215 					struct dentry *dentry, int acc)
2216 {
2217 	struct inode	*inode = d_inode(dentry);
2218 	int		err;
2219 
2220 	if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2221 		return 0;
2222 #if 0
2223 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2224 		acc,
2225 		(acc & NFSD_MAY_READ)?	" read"  : "",
2226 		(acc & NFSD_MAY_WRITE)?	" write" : "",
2227 		(acc & NFSD_MAY_EXEC)?	" exec"  : "",
2228 		(acc & NFSD_MAY_SATTR)?	" sattr" : "",
2229 		(acc & NFSD_MAY_TRUNC)?	" trunc" : "",
2230 		(acc & NFSD_MAY_LOCK)?	" lock"  : "",
2231 		(acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2232 		inode->i_mode,
2233 		IS_IMMUTABLE(inode)?	" immut" : "",
2234 		IS_APPEND(inode)?	" append" : "",
2235 		__mnt_is_readonly(exp->ex_path.mnt)?	" ro" : "");
2236 	dprintk("      owner %d/%d user %d/%d\n",
2237 		inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2238 #endif
2239 
2240 	/* Normally we reject any write/sattr etc access on a read-only file
2241 	 * system.  But if it is IRIX doing check on write-access for a
2242 	 * device special file, we ignore rofs.
2243 	 */
2244 	if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2245 		if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2246 			if (exp_rdonly(rqstp, exp) ||
2247 			    __mnt_is_readonly(exp->ex_path.mnt))
2248 				return nfserr_rofs;
2249 			if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2250 				return nfserr_perm;
2251 		}
2252 	if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2253 		return nfserr_perm;
2254 
2255 	if (acc & NFSD_MAY_LOCK) {
2256 		/* If we cannot rely on authentication in NLM requests,
2257 		 * just allow locks, otherwise require read permission, or
2258 		 * ownership
2259 		 */
2260 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2261 			return 0;
2262 		else
2263 			acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2264 	}
2265 	/*
2266 	 * The file owner always gets access permission for accesses that
2267 	 * would normally be checked at open time. This is to make
2268 	 * file access work even when the client has done a fchmod(fd, 0).
2269 	 *
2270 	 * However, `cp foo bar' should fail nevertheless when bar is
2271 	 * readonly. A sensible way to do this might be to reject all
2272 	 * attempts to truncate a read-only file, because a creat() call
2273 	 * always implies file truncation.
2274 	 * ... but this isn't really fair.  A process may reasonably call
2275 	 * ftruncate on an open file descriptor on a file with perm 000.
2276 	 * We must trust the client to do permission checking - using "ACCESS"
2277 	 * with NFSv3.
2278 	 */
2279 	if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2280 	    uid_eq(inode->i_uid, current_fsuid()))
2281 		return 0;
2282 
2283 	/* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2284 	err = inode_permission(&init_user_ns, inode,
2285 			       acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
2286 
2287 	/* Allow read access to binaries even when mode 111 */
2288 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
2289 	     (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2290 	      acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2291 		err = inode_permission(&init_user_ns, inode, MAY_EXEC);
2292 
2293 	return err? nfserrno(err) : 0;
2294 }
2295