xref: /openbmc/linux/fs/nfsd/vfs.c (revision bbf2f098838aa86cee240a7a11486e0601d56a3f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * File operations used by nfsd. Some of these have been ripped from
41da177e4SLinus Torvalds  * other parts of the kernel because they weren't exported, others
51da177e4SLinus Torvalds  * are partial duplicates with added or changed functionality.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Note that several functions dget() the dentry upon which they want
81da177e4SLinus Torvalds  * to act, most notably those that create directory entries. Response
91da177e4SLinus Torvalds  * dentry's are dput()'d if necessary in the release callback.
101da177e4SLinus Torvalds  * So if you notice code paths that apparently fail to dput() the
111da177e4SLinus Torvalds  * dentry, don't worry--they have been taken care of.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
141da177e4SLinus Torvalds  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/fs.h>
181da177e4SLinus Torvalds #include <linux/file.h>
19d6b29d7cSJens Axboe #include <linux/splice.h>
2095d871f0SAnna Schumaker #include <linux/falloc.h>
211da177e4SLinus Torvalds #include <linux/fcntl.h>
221da177e4SLinus Torvalds #include <linux/namei.h>
231da177e4SLinus Torvalds #include <linux/delay.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
251da177e4SLinus Torvalds #include <linux/posix_acl_xattr.h>
261da177e4SLinus Torvalds #include <linux/xattr.h>
279a74af21SBoaz Harrosh #include <linux/jhash.h>
289a74af21SBoaz Harrosh #include <linux/ima.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
31f501912aSBen Myers #include <linux/exportfs.h>
32f501912aSBen Myers #include <linux/writeback.h>
3318032ca0SDavid Quigley #include <linux/security.h>
349a74af21SBoaz Harrosh 
359a74af21SBoaz Harrosh #ifdef CONFIG_NFSD_V3
369a74af21SBoaz Harrosh #include "xdr3.h"
379a74af21SBoaz Harrosh #endif /* CONFIG_NFSD_V3 */
389a74af21SBoaz Harrosh 
395be196e5SChristoph Hellwig #ifdef CONFIG_NFSD_V4
40ffa0160aSChristoph Hellwig #include "../internal.h"
412ca72e17SJ. Bruce Fields #include "acl.h"
422ca72e17SJ. Bruce Fields #include "idmap.h"
431da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V4 */
441da177e4SLinus Torvalds 
459a74af21SBoaz Harrosh #include "nfsd.h"
469a74af21SBoaz Harrosh #include "vfs.h"
47b4935239SJeff Layton #include "filecache.h"
486e8b50d1SJeff Layton #include "trace.h"
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_FILEOP
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * Called from nfsd_lookup and encode_dirent. Check if we have crossed
541da177e4SLinus Torvalds  * a mount point.
55e0bb89efSJ.Bruce Fields  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
561da177e4SLinus Torvalds  *  or nfs_ok having possibly changed *dpp and *expp
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds int
591da177e4SLinus Torvalds nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
601da177e4SLinus Torvalds 		        struct svc_export **expp)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	struct svc_export *exp = *expp, *exp2 = NULL;
631da177e4SLinus Torvalds 	struct dentry *dentry = *dpp;
6491c9fa8fSAl Viro 	struct path path = {.mnt = mntget(exp->ex_path.mnt),
6591c9fa8fSAl Viro 			    .dentry = dget(dentry)};
666264d69dSAl Viro 	int err = 0;
671da177e4SLinus Torvalds 
687cc90cc3SAl Viro 	err = follow_down(&path);
69cc53ce53SDavid Howells 	if (err < 0)
70cc53ce53SDavid Howells 		goto out;
7199bbf6ecSNeilBrown 	if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
7299bbf6ecSNeilBrown 	    nfsd_mountpoint(dentry, exp) == 2) {
7399bbf6ecSNeilBrown 		/* This is only a mountpoint in some other namespace */
7499bbf6ecSNeilBrown 		path_put(&path);
7599bbf6ecSNeilBrown 		goto out;
7699bbf6ecSNeilBrown 	}
771da177e4SLinus Torvalds 
7891c9fa8fSAl Viro 	exp2 = rqst_exp_get_by_name(rqstp, &path);
791da177e4SLinus Torvalds 	if (IS_ERR(exp2)) {
801da177e4SLinus Torvalds 		err = PTR_ERR(exp2);
813b6cee7bSJ. Bruce Fields 		/*
823b6cee7bSJ. Bruce Fields 		 * We normally allow NFS clients to continue
833b6cee7bSJ. Bruce Fields 		 * "underneath" a mountpoint that is not exported.
843b6cee7bSJ. Bruce Fields 		 * The exception is V4ROOT, where no traversal is ever
853b6cee7bSJ. Bruce Fields 		 * allowed without an explicit export of the new
863b6cee7bSJ. Bruce Fields 		 * directory.
873b6cee7bSJ. Bruce Fields 		 */
883b6cee7bSJ. Bruce Fields 		if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
893b6cee7bSJ. Bruce Fields 			err = 0;
9091c9fa8fSAl Viro 		path_put(&path);
911da177e4SLinus Torvalds 		goto out;
921da177e4SLinus Torvalds 	}
933c394ddaSSteve Dickson 	if (nfsd_v4client(rqstp) ||
943c394ddaSSteve Dickson 		(exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
951da177e4SLinus Torvalds 		/* successfully crossed mount point */
961644ccc8SAl Viro 		/*
9791c9fa8fSAl Viro 		 * This is subtle: path.dentry is *not* on path.mnt
9891c9fa8fSAl Viro 		 * at this point.  The only reason we are safe is that
9991c9fa8fSAl Viro 		 * original mnt is pinned down by exp, so we should
10091c9fa8fSAl Viro 		 * put path *before* putting exp
1011644ccc8SAl Viro 		 */
10291c9fa8fSAl Viro 		*dpp = path.dentry;
10391c9fa8fSAl Viro 		path.dentry = dentry;
1041644ccc8SAl Viro 		*expp = exp2;
10591c9fa8fSAl Viro 		exp2 = exp;
1061da177e4SLinus Torvalds 	}
10791c9fa8fSAl Viro 	path_put(&path);
10891c9fa8fSAl Viro 	exp_put(exp2);
1091da177e4SLinus Torvalds out:
1101da177e4SLinus Torvalds 	return err;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
113289ede45SJ. Bruce Fields static void follow_to_parent(struct path *path)
114289ede45SJ. Bruce Fields {
115289ede45SJ. Bruce Fields 	struct dentry *dp;
116289ede45SJ. Bruce Fields 
117289ede45SJ. Bruce Fields 	while (path->dentry == path->mnt->mnt_root && follow_up(path))
118289ede45SJ. Bruce Fields 		;
119289ede45SJ. Bruce Fields 	dp = dget_parent(path->dentry);
120289ede45SJ. Bruce Fields 	dput(path->dentry);
121289ede45SJ. Bruce Fields 	path->dentry = dp;
122289ede45SJ. Bruce Fields }
123289ede45SJ. Bruce Fields 
124289ede45SJ. Bruce Fields static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
125289ede45SJ. Bruce Fields {
126289ede45SJ. Bruce Fields 	struct svc_export *exp2;
127289ede45SJ. Bruce Fields 	struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
128289ede45SJ. Bruce Fields 			    .dentry = dget(dparent)};
129289ede45SJ. Bruce Fields 
130289ede45SJ. Bruce Fields 	follow_to_parent(&path);
131289ede45SJ. Bruce Fields 
132289ede45SJ. Bruce Fields 	exp2 = rqst_exp_parent(rqstp, &path);
133289ede45SJ. Bruce Fields 	if (PTR_ERR(exp2) == -ENOENT) {
134289ede45SJ. Bruce Fields 		*dentryp = dget(dparent);
135289ede45SJ. Bruce Fields 	} else if (IS_ERR(exp2)) {
136289ede45SJ. Bruce Fields 		path_put(&path);
137289ede45SJ. Bruce Fields 		return PTR_ERR(exp2);
138289ede45SJ. Bruce Fields 	} else {
139289ede45SJ. Bruce Fields 		*dentryp = dget(path.dentry);
140289ede45SJ. Bruce Fields 		exp_put(*exp);
141289ede45SJ. Bruce Fields 		*exp = exp2;
142289ede45SJ. Bruce Fields 	}
143289ede45SJ. Bruce Fields 	path_put(&path);
144289ede45SJ. Bruce Fields 	return 0;
145289ede45SJ. Bruce Fields }
146289ede45SJ. Bruce Fields 
14782ead7feSJ. Bruce Fields /*
14882ead7feSJ. Bruce Fields  * For nfsd purposes, we treat V4ROOT exports as though there was an
14982ead7feSJ. Bruce Fields  * export at *every* directory.
15099bbf6ecSNeilBrown  * We return:
15199bbf6ecSNeilBrown  * '1' if this dentry *must* be an export point,
15299bbf6ecSNeilBrown  * '2' if it might be, if there is really a mount here, and
15399bbf6ecSNeilBrown  * '0' if there is no chance of an export point here.
15482ead7feSJ. Bruce Fields  */
1553227fa41SJ. Bruce Fields int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
15682ead7feSJ. Bruce Fields {
15799bbf6ecSNeilBrown 	if (!d_inode(dentry))
15899bbf6ecSNeilBrown 		return 0;
15999bbf6ecSNeilBrown 	if (exp->ex_flags & NFSEXP_V4ROOT)
16082ead7feSJ. Bruce Fields 		return 1;
16111fcee02STrond Myklebust 	if (nfsd4_is_junction(dentry))
16211fcee02STrond Myklebust 		return 1;
16399bbf6ecSNeilBrown 	if (d_mountpoint(dentry))
16499bbf6ecSNeilBrown 		/*
16599bbf6ecSNeilBrown 		 * Might only be a mountpoint in a different namespace,
16699bbf6ecSNeilBrown 		 * but we need to check.
16799bbf6ecSNeilBrown 		 */
16899bbf6ecSNeilBrown 		return 2;
16982ead7feSJ. Bruce Fields 	return 0;
17082ead7feSJ. Bruce Fields }
17182ead7feSJ. Bruce Fields 
1726264d69dSAl Viro __be32
1736c0a654dSJ. Bruce Fields nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
1745a022fc8SChuck Lever 		   const char *name, unsigned int len,
1756c0a654dSJ. Bruce Fields 		   struct svc_export **exp_ret, struct dentry **dentry_ret)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct svc_export	*exp;
1781da177e4SLinus Torvalds 	struct dentry		*dparent;
1791da177e4SLinus Torvalds 	struct dentry		*dentry;
1806264d69dSAl Viro 	int			host_err;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	dparent = fhp->fh_dentry;
185bf18f163SKinglong Mee 	exp = exp_get(fhp->fh_export);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	/* Lookup the name, but don't follow links */
1881da177e4SLinus Torvalds 	if (isdotent(name, len)) {
1891da177e4SLinus Torvalds 		if (len==1)
1901da177e4SLinus Torvalds 			dentry = dget(dparent);
19154775491SJan Blunck 		else if (dparent != exp->ex_path.dentry)
1921da177e4SLinus Torvalds 			dentry = dget_parent(dparent);
193fed83811SJ. Bruce Fields 		else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
1941da177e4SLinus Torvalds 			dentry = dget(dparent); /* .. == . just like at / */
1951da177e4SLinus Torvalds 		else {
1961da177e4SLinus Torvalds 			/* checking mountpoint crossing is very different when stepping up */
197289ede45SJ. Bruce Fields 			host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
198289ede45SJ. Bruce Fields 			if (host_err)
1991da177e4SLinus Torvalds 				goto out_nfserr;
2001da177e4SLinus Torvalds 		}
2011da177e4SLinus Torvalds 	} else {
2024335723eSJ. Bruce Fields 		/*
2034335723eSJ. Bruce Fields 		 * In the nfsd4_open() case, this may be held across
2044335723eSJ. Bruce Fields 		 * subsequent open and delegation acquisition which may
2054335723eSJ. Bruce Fields 		 * need to take the child's i_mutex:
2064335723eSJ. Bruce Fields 		 */
2074335723eSJ. Bruce Fields 		fh_lock_nested(fhp, I_MUTEX_PARENT);
2081da177e4SLinus Torvalds 		dentry = lookup_one_len(name, dparent, len);
2096264d69dSAl Viro 		host_err = PTR_ERR(dentry);
2101da177e4SLinus Torvalds 		if (IS_ERR(dentry))
2111da177e4SLinus Torvalds 			goto out_nfserr;
21282ead7feSJ. Bruce Fields 		if (nfsd_mountpoint(dentry, exp)) {
213bbddca8eSNeilBrown 			/*
214bbddca8eSNeilBrown 			 * We don't need the i_mutex after all.  It's
215bbddca8eSNeilBrown 			 * still possible we could open this (regular
216bbddca8eSNeilBrown 			 * files can be mountpoints too), but the
217bbddca8eSNeilBrown 			 * i_mutex is just there to prevent renames of
218bbddca8eSNeilBrown 			 * something that we might be about to delegate,
219bbddca8eSNeilBrown 			 * and a mountpoint won't be renamed:
220bbddca8eSNeilBrown 			 */
221bbddca8eSNeilBrown 			fh_unlock(fhp);
2226264d69dSAl Viro 			if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
2231da177e4SLinus Torvalds 				dput(dentry);
2241da177e4SLinus Torvalds 				goto out_nfserr;
2251da177e4SLinus Torvalds 			}
2261da177e4SLinus Torvalds 		}
2271da177e4SLinus Torvalds 	}
2286c0a654dSJ. Bruce Fields 	*dentry_ret = dentry;
2296c0a654dSJ. Bruce Fields 	*exp_ret = exp;
2306c0a654dSJ. Bruce Fields 	return 0;
2316c0a654dSJ. Bruce Fields 
2326c0a654dSJ. Bruce Fields out_nfserr:
2336c0a654dSJ. Bruce Fields 	exp_put(exp);
2346c0a654dSJ. Bruce Fields 	return nfserrno(host_err);
2356c0a654dSJ. Bruce Fields }
2366c0a654dSJ. Bruce Fields 
2376c0a654dSJ. Bruce Fields /*
2386c0a654dSJ. Bruce Fields  * Look up one component of a pathname.
2396c0a654dSJ. Bruce Fields  * N.B. After this call _both_ fhp and resfh need an fh_put
2406c0a654dSJ. Bruce Fields  *
2416c0a654dSJ. Bruce Fields  * If the lookup would cross a mountpoint, and the mounted filesystem
2426c0a654dSJ. Bruce Fields  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
2436c0a654dSJ. Bruce Fields  * accepted as it stands and the mounted directory is
2446c0a654dSJ. Bruce Fields  * returned. Otherwise the covered directory is returned.
2456c0a654dSJ. Bruce Fields  * NOTE: this mountpoint crossing is not supported properly by all
2466c0a654dSJ. Bruce Fields  *   clients and is explicitly disallowed for NFSv3
2476c0a654dSJ. Bruce Fields  *      NeilBrown <neilb@cse.unsw.edu.au>
2486c0a654dSJ. Bruce Fields  */
2496c0a654dSJ. Bruce Fields __be32
2506c0a654dSJ. Bruce Fields nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
2515a022fc8SChuck Lever 				unsigned int len, struct svc_fh *resfh)
2526c0a654dSJ. Bruce Fields {
2536c0a654dSJ. Bruce Fields 	struct svc_export	*exp;
2546c0a654dSJ. Bruce Fields 	struct dentry		*dentry;
2556c0a654dSJ. Bruce Fields 	__be32 err;
2566c0a654dSJ. Bruce Fields 
25729a78a3eSJ. Bruce Fields 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
25829a78a3eSJ. Bruce Fields 	if (err)
25929a78a3eSJ. Bruce Fields 		return err;
2606c0a654dSJ. Bruce Fields 	err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
2616c0a654dSJ. Bruce Fields 	if (err)
2626c0a654dSJ. Bruce Fields 		return err;
26332c1eb0cSAndy Adamson 	err = check_nfsd_access(exp, rqstp);
26432c1eb0cSAndy Adamson 	if (err)
26532c1eb0cSAndy Adamson 		goto out;
2661da177e4SLinus Torvalds 	/*
2671da177e4SLinus Torvalds 	 * Note: we compose the file handle now, but as the
2681da177e4SLinus Torvalds 	 * dentry may be negative, it may need to be updated.
2691da177e4SLinus Torvalds 	 */
2701da177e4SLinus Torvalds 	err = fh_compose(resfh, exp, dentry, fhp);
2712b0143b5SDavid Howells 	if (!err && d_really_is_negative(dentry))
2721da177e4SLinus Torvalds 		err = nfserr_noent;
27332c1eb0cSAndy Adamson out:
2741da177e4SLinus Torvalds 	dput(dentry);
2751da177e4SLinus Torvalds 	exp_put(exp);
2761da177e4SLinus Torvalds 	return err;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
279f501912aSBen Myers /*
280f501912aSBen Myers  * Commit metadata changes to stable storage.
281f501912aSBen Myers  */
282f501912aSBen Myers static int
283f501912aSBen Myers commit_metadata(struct svc_fh *fhp)
284f501912aSBen Myers {
2852b0143b5SDavid Howells 	struct inode *inode = d_inode(fhp->fh_dentry);
286f501912aSBen Myers 	const struct export_operations *export_ops = inode->i_sb->s_export_op;
287f501912aSBen Myers 
288f501912aSBen Myers 	if (!EX_ISSYNC(fhp->fh_export))
289f501912aSBen Myers 		return 0;
290f501912aSBen Myers 
291c3765016SChristoph Hellwig 	if (export_ops->commit_metadata)
292c3765016SChristoph Hellwig 		return export_ops->commit_metadata(inode);
293c3765016SChristoph Hellwig 	return sync_inode_metadata(inode, 1);
294f501912aSBen Myers }
2956c0a654dSJ. Bruce Fields 
2961da177e4SLinus Torvalds /*
297818e5a22SChristoph Hellwig  * Go over the attributes and take care of the small differences between
298818e5a22SChristoph Hellwig  * NFS semantics and what Linux expects.
299818e5a22SChristoph Hellwig  */
300818e5a22SChristoph Hellwig static void
301818e5a22SChristoph Hellwig nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
302818e5a22SChristoph Hellwig {
303818e5a22SChristoph Hellwig 	/* sanitize the mode change */
304818e5a22SChristoph Hellwig 	if (iap->ia_valid & ATTR_MODE) {
305818e5a22SChristoph Hellwig 		iap->ia_mode &= S_IALLUGO;
306818e5a22SChristoph Hellwig 		iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
307818e5a22SChristoph Hellwig 	}
308818e5a22SChristoph Hellwig 
309818e5a22SChristoph Hellwig 	/* Revoke setuid/setgid on chown */
310818e5a22SChristoph Hellwig 	if (!S_ISDIR(inode->i_mode) &&
311c4fa6d7cSStanislav Kholmanskikh 	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
312818e5a22SChristoph Hellwig 		iap->ia_valid |= ATTR_KILL_PRIV;
313818e5a22SChristoph Hellwig 		if (iap->ia_valid & ATTR_MODE) {
314818e5a22SChristoph Hellwig 			/* we're setting mode too, just clear the s*id bits */
315818e5a22SChristoph Hellwig 			iap->ia_mode &= ~S_ISUID;
316818e5a22SChristoph Hellwig 			if (iap->ia_mode & S_IXGRP)
317818e5a22SChristoph Hellwig 				iap->ia_mode &= ~S_ISGID;
318818e5a22SChristoph Hellwig 		} else {
319818e5a22SChristoph Hellwig 			/* set ATTR_KILL_* bits and let VFS handle it */
320818e5a22SChristoph Hellwig 			iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
321818e5a22SChristoph Hellwig 		}
322818e5a22SChristoph Hellwig 	}
323818e5a22SChristoph Hellwig }
324818e5a22SChristoph Hellwig 
3250839ffb8SJ. Bruce Fields static __be32
3260839ffb8SJ. Bruce Fields nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
3270839ffb8SJ. Bruce Fields 		struct iattr *iap)
3280839ffb8SJ. Bruce Fields {
3290839ffb8SJ. Bruce Fields 	struct inode *inode = d_inode(fhp->fh_dentry);
3300839ffb8SJ. Bruce Fields 	int host_err;
3310839ffb8SJ. Bruce Fields 
3320839ffb8SJ. Bruce Fields 	if (iap->ia_size < inode->i_size) {
3330839ffb8SJ. Bruce Fields 		__be32 err;
3340839ffb8SJ. Bruce Fields 
3350839ffb8SJ. Bruce Fields 		err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
3360839ffb8SJ. Bruce Fields 				NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
3370839ffb8SJ. Bruce Fields 		if (err)
3380839ffb8SJ. Bruce Fields 			return err;
3390839ffb8SJ. Bruce Fields 	}
3400839ffb8SJ. Bruce Fields 
3410839ffb8SJ. Bruce Fields 	host_err = get_write_access(inode);
3420839ffb8SJ. Bruce Fields 	if (host_err)
3430839ffb8SJ. Bruce Fields 		goto out_nfserrno;
3440839ffb8SJ. Bruce Fields 
3450839ffb8SJ. Bruce Fields 	host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
3460839ffb8SJ. Bruce Fields 	if (host_err)
3470839ffb8SJ. Bruce Fields 		goto out_put_write_access;
3480839ffb8SJ. Bruce Fields 	return 0;
3490839ffb8SJ. Bruce Fields 
3500839ffb8SJ. Bruce Fields out_put_write_access:
3510839ffb8SJ. Bruce Fields 	put_write_access(inode);
3520839ffb8SJ. Bruce Fields out_nfserrno:
3530839ffb8SJ. Bruce Fields 	return nfserrno(host_err);
3540839ffb8SJ. Bruce Fields }
3550839ffb8SJ. Bruce Fields 
356818e5a22SChristoph Hellwig /*
357818e5a22SChristoph Hellwig  * Set various file attributes.  After this call fhp needs an fh_put.
3581da177e4SLinus Torvalds  */
3596264d69dSAl Viro __be32
3601da177e4SLinus Torvalds nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
3611da177e4SLinus Torvalds 	     int check_guard, time_t guardtime)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	struct dentry	*dentry;
3641da177e4SLinus Torvalds 	struct inode	*inode;
3658837abcaSMiklos Szeredi 	int		accmode = NFSD_MAY_SATTR;
366175a4eb7SAl Viro 	umode_t		ftype = 0;
3676264d69dSAl Viro 	__be32		err;
3686264d69dSAl Viro 	int		host_err;
3699f67f189SJ. Bruce Fields 	bool		get_write_count;
370758e99feSChristoph Hellwig 	bool		size_change = (iap->ia_valid & ATTR_SIZE);
3711da177e4SLinus Torvalds 
372255fbca6Szhengbin 	if (iap->ia_valid & ATTR_SIZE) {
3738837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
3741da177e4SLinus Torvalds 		ftype = S_IFREG;
375255fbca6Szhengbin 	}
376255fbca6Szhengbin 
377255fbca6Szhengbin 	/*
378255fbca6Szhengbin 	 * If utimes(2) and friends are called with times not NULL, we should
379255fbca6Szhengbin 	 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
380e977cc83SGeert Uytterhoeven 	 * will return EACCES, when the caller's effective UID does not match
381255fbca6Szhengbin 	 * the owner of the file, and the caller is not privileged. In this
382255fbca6Szhengbin 	 * situation, we should return EPERM(notify_change will return this).
383255fbca6Szhengbin 	 */
384255fbca6Szhengbin 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
385255fbca6Szhengbin 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
386255fbca6Szhengbin 		if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
387255fbca6Szhengbin 			accmode |= NFSD_MAY_WRITE;
388255fbca6Szhengbin 	}
3891da177e4SLinus Torvalds 
3909f67f189SJ. Bruce Fields 	/* Callers that do fh_verify should do the fh_want_write: */
3919f67f189SJ. Bruce Fields 	get_write_count = !fhp->fh_dentry;
3929f67f189SJ. Bruce Fields 
3931da177e4SLinus Torvalds 	/* Get inode */
3941da177e4SLinus Torvalds 	err = fh_verify(rqstp, fhp, ftype, accmode);
39515b7a1b8SNeilBrown 	if (err)
396758e99feSChristoph Hellwig 		return err;
3979f67f189SJ. Bruce Fields 	if (get_write_count) {
3989f67f189SJ. Bruce Fields 		host_err = fh_want_write(fhp);
3999f67f189SJ. Bruce Fields 		if (host_err)
400758e99feSChristoph Hellwig 			goto out;
4019f67f189SJ. Bruce Fields 	}
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
4042b0143b5SDavid Howells 	inode = d_inode(dentry);
4051da177e4SLinus Torvalds 
40615b7a1b8SNeilBrown 	/* Ignore any mode updates on symlinks */
40715b7a1b8SNeilBrown 	if (S_ISLNK(inode->i_mode))
40815b7a1b8SNeilBrown 		iap->ia_valid &= ~ATTR_MODE;
40915b7a1b8SNeilBrown 
41015b7a1b8SNeilBrown 	if (!iap->ia_valid)
411758e99feSChristoph Hellwig 		return 0;
41215b7a1b8SNeilBrown 
413818e5a22SChristoph Hellwig 	nfsd_sanitize_attrs(inode, iap);
4141da177e4SLinus Torvalds 
415758e99feSChristoph Hellwig 	if (check_guard && guardtime != inode->i_ctime.tv_sec)
416758e99feSChristoph Hellwig 		return nfserr_notsync;
417758e99feSChristoph Hellwig 
418f0c63124SChristoph Hellwig 	/*
41941f53350SChristoph Hellwig 	 * The size case is special, it changes the file in addition to the
420783112f7SChristoph Hellwig 	 * attributes, and file systems don't expect it to be mixed with
421783112f7SChristoph Hellwig 	 * "random" attribute changes.  We thus split out the size change
422783112f7SChristoph Hellwig 	 * into a separate call to ->setattr, and do the rest as a separate
423783112f7SChristoph Hellwig 	 * setattr call.
424f0c63124SChristoph Hellwig 	 */
425758e99feSChristoph Hellwig 	if (size_change) {
4260839ffb8SJ. Bruce Fields 		err = nfsd_get_write_access(rqstp, fhp, iap);
4270839ffb8SJ. Bruce Fields 		if (err)
428758e99feSChristoph Hellwig 			return err;
429783112f7SChristoph Hellwig 	}
43041f53350SChristoph Hellwig 
431783112f7SChristoph Hellwig 	fh_lock(fhp);
432783112f7SChristoph Hellwig 	if (size_change) {
43341f53350SChristoph Hellwig 		/*
4340839ffb8SJ. Bruce Fields 		 * RFC5661, Section 18.30.4:
4350839ffb8SJ. Bruce Fields 		 *   Changing the size of a file with SETATTR indirectly
4360839ffb8SJ. Bruce Fields 		 *   changes the time_modify and change attributes.
4370839ffb8SJ. Bruce Fields 		 *
4380839ffb8SJ. Bruce Fields 		 * (and similar for the older RFCs)
43941f53350SChristoph Hellwig 		 */
440783112f7SChristoph Hellwig 		struct iattr size_attr = {
441783112f7SChristoph Hellwig 			.ia_valid	= ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
442783112f7SChristoph Hellwig 			.ia_size	= iap->ia_size,
443783112f7SChristoph Hellwig 		};
444783112f7SChristoph Hellwig 
445783112f7SChristoph Hellwig 		host_err = notify_change(dentry, &size_attr, NULL);
446783112f7SChristoph Hellwig 		if (host_err)
447783112f7SChristoph Hellwig 			goto out_unlock;
448783112f7SChristoph Hellwig 		iap->ia_valid &= ~ATTR_SIZE;
449783112f7SChristoph Hellwig 
450783112f7SChristoph Hellwig 		/*
451783112f7SChristoph Hellwig 		 * Avoid the additional setattr call below if the only other
452783112f7SChristoph Hellwig 		 * attribute that the client sends is the mtime, as we update
453783112f7SChristoph Hellwig 		 * it as part of the size change above.
454783112f7SChristoph Hellwig 		 */
455783112f7SChristoph Hellwig 		if ((iap->ia_valid & ~ATTR_MTIME) == 0)
456783112f7SChristoph Hellwig 			goto out_unlock;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	iap->ia_valid |= ATTR_CTIME;
46027ac0ffeSJ. Bruce Fields 	host_err = notify_change(dentry, iap, NULL);
461987da479SChristoph Hellwig 
462783112f7SChristoph Hellwig out_unlock:
463783112f7SChristoph Hellwig 	fh_unlock(fhp);
4640839ffb8SJ. Bruce Fields 	if (size_change)
4650839ffb8SJ. Bruce Fields 		put_write_access(inode);
4660839ffb8SJ. Bruce Fields out:
467758e99feSChristoph Hellwig 	if (!host_err)
468758e99feSChristoph Hellwig 		host_err = commit_metadata(fhp);
469758e99feSChristoph Hellwig 	return nfserrno(host_err);
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4725be196e5SChristoph Hellwig #if defined(CONFIG_NFSD_V4)
4739b4146e8SChuck Lever /*
4749b4146e8SChuck Lever  * NFS junction information is stored in an extended attribute.
4759b4146e8SChuck Lever  */
4769b4146e8SChuck Lever #define NFSD_JUNCTION_XATTR_NAME	XATTR_TRUSTED_PREFIX "junction.nfs"
4779b4146e8SChuck Lever 
4789b4146e8SChuck Lever /**
4799b4146e8SChuck Lever  * nfsd4_is_junction - Test if an object could be an NFS junction
4809b4146e8SChuck Lever  *
4819b4146e8SChuck Lever  * @dentry: object to test
4829b4146e8SChuck Lever  *
4839b4146e8SChuck Lever  * Returns 1 if "dentry" appears to contain NFS junction information.
4849b4146e8SChuck Lever  * Otherwise 0 is returned.
4859b4146e8SChuck Lever  */
48611fcee02STrond Myklebust int nfsd4_is_junction(struct dentry *dentry)
48711fcee02STrond Myklebust {
4882b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
48911fcee02STrond Myklebust 
49011fcee02STrond Myklebust 	if (inode == NULL)
49111fcee02STrond Myklebust 		return 0;
49211fcee02STrond Myklebust 	if (inode->i_mode & S_IXUGO)
49311fcee02STrond Myklebust 		return 0;
49411fcee02STrond Myklebust 	if (!(inode->i_mode & S_ISVTX))
49511fcee02STrond Myklebust 		return 0;
4969b4146e8SChuck Lever 	if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
49711fcee02STrond Myklebust 		return 0;
49811fcee02STrond Myklebust 	return 1;
49911fcee02STrond Myklebust }
50018032ca0SDavid Quigley #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
50118032ca0SDavid Quigley __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
50218032ca0SDavid Quigley 		struct xdr_netobj *label)
50318032ca0SDavid Quigley {
50418032ca0SDavid Quigley 	__be32 error;
50518032ca0SDavid Quigley 	int host_error;
50618032ca0SDavid Quigley 	struct dentry *dentry;
50718032ca0SDavid Quigley 
50818032ca0SDavid Quigley 	error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
50918032ca0SDavid Quigley 	if (error)
51018032ca0SDavid Quigley 		return error;
51118032ca0SDavid Quigley 
51218032ca0SDavid Quigley 	dentry = fhp->fh_dentry;
51318032ca0SDavid Quigley 
5145955102cSAl Viro 	inode_lock(d_inode(dentry));
51518032ca0SDavid Quigley 	host_error = security_inode_setsecctx(dentry, label->data, label->len);
5165955102cSAl Viro 	inode_unlock(d_inode(dentry));
51718032ca0SDavid Quigley 	return nfserrno(host_error);
51818032ca0SDavid Quigley }
51918032ca0SDavid Quigley #else
52018032ca0SDavid Quigley __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
52118032ca0SDavid Quigley 		struct xdr_netobj *label)
52218032ca0SDavid Quigley {
52318032ca0SDavid Quigley 	return nfserr_notsupp;
52418032ca0SDavid Quigley }
52518032ca0SDavid Quigley #endif
52618032ca0SDavid Quigley 
527ffa0160aSChristoph Hellwig __be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
528ffa0160aSChristoph Hellwig 		u64 dst_pos, u64 count)
529ffa0160aSChristoph Hellwig {
53042ec3d4cSDarrick J. Wong 	loff_t cloned;
53142ec3d4cSDarrick J. Wong 
532452ce659SDarrick J. Wong 	cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
533e3fdc89cSTrond Myklebust 	if (cloned < 0)
534e3fdc89cSTrond Myklebust 		return nfserrno(cloned);
53542ec3d4cSDarrick J. Wong 	if (count && cloned != count)
536e3fdc89cSTrond Myklebust 		return nfserrno(-EINVAL);
537e3fdc89cSTrond Myklebust 	return 0;
538ffa0160aSChristoph Hellwig }
539ffa0160aSChristoph Hellwig 
54029ae7f9dSAnna Schumaker ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
54129ae7f9dSAnna Schumaker 			     u64 dst_pos, u64 count)
54229ae7f9dSAnna Schumaker {
54329ae7f9dSAnna Schumaker 
54429ae7f9dSAnna Schumaker 	/*
54529ae7f9dSAnna Schumaker 	 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
54629ae7f9dSAnna Schumaker 	 * thread and client rpc slot.  The choice of 4MB is somewhat
54729ae7f9dSAnna Schumaker 	 * arbitrary.  We might instead base this on r/wsize, or make it
54829ae7f9dSAnna Schumaker 	 * tunable, or use a time instead of a byte limit, or implement
54929ae7f9dSAnna Schumaker 	 * asynchronous copy.  In theory a client could also recognize a
55029ae7f9dSAnna Schumaker 	 * limit like this and pipeline multiple COPY requests.
55129ae7f9dSAnna Schumaker 	 */
55229ae7f9dSAnna Schumaker 	count = min_t(u64, count, 1 << 22);
55329ae7f9dSAnna Schumaker 	return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
55429ae7f9dSAnna Schumaker }
55529ae7f9dSAnna Schumaker 
55695d871f0SAnna Schumaker __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
55795d871f0SAnna Schumaker 			   struct file *file, loff_t offset, loff_t len,
55895d871f0SAnna Schumaker 			   int flags)
55995d871f0SAnna Schumaker {
56095d871f0SAnna Schumaker 	int error;
56195d871f0SAnna Schumaker 
56295d871f0SAnna Schumaker 	if (!S_ISREG(file_inode(file)->i_mode))
56395d871f0SAnna Schumaker 		return nfserr_inval;
56495d871f0SAnna Schumaker 
56595d871f0SAnna Schumaker 	error = vfs_fallocate(file, flags, offset, len);
56695d871f0SAnna Schumaker 	if (!error)
56795d871f0SAnna Schumaker 		error = commit_metadata(fhp);
56895d871f0SAnna Schumaker 
56995d871f0SAnna Schumaker 	return nfserrno(error);
57095d871f0SAnna Schumaker }
5716a85d6c7SJ. Bruce Fields #endif /* defined(CONFIG_NFSD_V4) */
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
5741da177e4SLinus Torvalds /*
5751da177e4SLinus Torvalds  * Check server access rights to a file system object
5761da177e4SLinus Torvalds  */
5771da177e4SLinus Torvalds struct accessmap {
5781da177e4SLinus Torvalds 	u32		access;
5791da177e4SLinus Torvalds 	int		how;
5801da177e4SLinus Torvalds };
5811da177e4SLinus Torvalds static struct accessmap	nfs3_regaccess[] = {
5828837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
5838837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
5848837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_TRUNC	},
5858837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE			},
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds     {	0,			0				}
5881da177e4SLinus Torvalds };
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds static struct accessmap	nfs3_diraccess[] = {
5918837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
5928837abcaSMiklos Szeredi     {	NFS3_ACCESS_LOOKUP,	NFSD_MAY_EXEC			},
5938837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
5948837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_EXEC|NFSD_MAY_WRITE	},
5958837abcaSMiklos Szeredi     {	NFS3_ACCESS_DELETE,	NFSD_MAY_REMOVE			},
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds     {	0,			0				}
5981da177e4SLinus Torvalds };
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds static struct accessmap	nfs3_anyaccess[] = {
6011da177e4SLinus Torvalds 	/* Some clients - Solaris 2.6 at least, make an access call
6021da177e4SLinus Torvalds 	 * to the server to check for access for things like /dev/null
6031da177e4SLinus Torvalds 	 * (which really, the server doesn't care about).  So
6041da177e4SLinus Torvalds 	 * We provide simple access checking for them, looking
6051da177e4SLinus Torvalds 	 * mainly at mode bits, and we make sure to ignore read-only
6061da177e4SLinus Torvalds 	 * filesystem checks
6071da177e4SLinus Torvalds 	 */
6088837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
6098837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
6108837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
6118837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds     {	0,			0				}
6141da177e4SLinus Torvalds };
6151da177e4SLinus Torvalds 
6166264d69dSAl Viro __be32
6171da177e4SLinus Torvalds nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	struct accessmap	*map;
6201da177e4SLinus Torvalds 	struct svc_export	*export;
6211da177e4SLinus Torvalds 	struct dentry		*dentry;
6221da177e4SLinus Torvalds 	u32			query, result = 0, sresult = 0;
6236264d69dSAl Viro 	__be32			error;
6241da177e4SLinus Torvalds 
6258837abcaSMiklos Szeredi 	error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
6261da177e4SLinus Torvalds 	if (error)
6271da177e4SLinus Torvalds 		goto out;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	export = fhp->fh_export;
6301da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
6311da177e4SLinus Torvalds 
632e36cb0b8SDavid Howells 	if (d_is_reg(dentry))
6331da177e4SLinus Torvalds 		map = nfs3_regaccess;
634e36cb0b8SDavid Howells 	else if (d_is_dir(dentry))
6351da177e4SLinus Torvalds 		map = nfs3_diraccess;
6361da177e4SLinus Torvalds 	else
6371da177e4SLinus Torvalds 		map = nfs3_anyaccess;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	query = *access;
6411da177e4SLinus Torvalds 	for  (; map->access; map++) {
6421da177e4SLinus Torvalds 		if (map->access & query) {
6436264d69dSAl Viro 			__be32 err2;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 			sresult |= map->access;
6461da177e4SLinus Torvalds 
6470ec757dfSJ. Bruce Fields 			err2 = nfsd_permission(rqstp, export, dentry, map->how);
6481da177e4SLinus Torvalds 			switch (err2) {
6491da177e4SLinus Torvalds 			case nfs_ok:
6501da177e4SLinus Torvalds 				result |= map->access;
6511da177e4SLinus Torvalds 				break;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 			/* the following error codes just mean the access was not allowed,
6541da177e4SLinus Torvalds 			 * rather than an error occurred */
6551da177e4SLinus Torvalds 			case nfserr_rofs:
6561da177e4SLinus Torvalds 			case nfserr_acces:
6571da177e4SLinus Torvalds 			case nfserr_perm:
6581da177e4SLinus Torvalds 				/* simply don't "or" in the access bit. */
6591da177e4SLinus Torvalds 				break;
6601da177e4SLinus Torvalds 			default:
6611da177e4SLinus Torvalds 				error = err2;
6621da177e4SLinus Torvalds 				goto out;
6631da177e4SLinus Torvalds 			}
6641da177e4SLinus Torvalds 		}
6651da177e4SLinus Torvalds 	}
6661da177e4SLinus Torvalds 	*access = result;
6671da177e4SLinus Torvalds 	if (supported)
6681da177e4SLinus Torvalds 		*supported = sresult;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds  out:
6711da177e4SLinus Torvalds 	return error;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
6741da177e4SLinus Torvalds 
67565294c1fSJeff Layton int nfsd_open_break_lease(struct inode *inode, int access)
676105f4622SJ. Bruce Fields {
677105f4622SJ. Bruce Fields 	unsigned int mode;
6781da177e4SLinus Torvalds 
679105f4622SJ. Bruce Fields 	if (access & NFSD_MAY_NOT_BREAK_LEASE)
680105f4622SJ. Bruce Fields 		return 0;
681105f4622SJ. Bruce Fields 	mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
682105f4622SJ. Bruce Fields 	return break_lease(inode, mode | O_NONBLOCK);
683105f4622SJ. Bruce Fields }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /*
6861da177e4SLinus Torvalds  * Open an existing file or directory.
687999448a8SBernd Schubert  * The may_flags argument indicates the type of open (read/write/lock)
688999448a8SBernd Schubert  * and additional flags.
6891da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
6901da177e4SLinus Torvalds  */
69165294c1fSJeff Layton static __be32
69265294c1fSJeff Layton __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
693999448a8SBernd Schubert 			int may_flags, struct file **filp)
6941da177e4SLinus Torvalds {
695765927b2SAl Viro 	struct path	path;
6961da177e4SLinus Torvalds 	struct inode	*inode;
6978519f994SKinglong Mee 	struct file	*file;
6986264d69dSAl Viro 	int		flags = O_RDONLY|O_LARGEFILE;
6996264d69dSAl Viro 	__be32		err;
70091885258SJeff Layton 	int		host_err = 0;
7011da177e4SLinus Torvalds 
702765927b2SAl Viro 	path.mnt = fhp->fh_export->ex_path.mnt;
703765927b2SAl Viro 	path.dentry = fhp->fh_dentry;
7042b0143b5SDavid Howells 	inode = d_inode(path.dentry);
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	/* Disallow write access to files with the append-only bit set
7071da177e4SLinus Torvalds 	 * or any access when mandatory locking enabled
7081da177e4SLinus Torvalds 	 */
7091da177e4SLinus Torvalds 	err = nfserr_perm;
710999448a8SBernd Schubert 	if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
7111da177e4SLinus Torvalds 		goto out;
7125e7fc436SJ. Bruce Fields 	/*
7135e7fc436SJ. Bruce Fields 	 * We must ignore files (but only files) which might have mandatory
7145e7fc436SJ. Bruce Fields 	 * locks on them because there is no way to know if the accesser has
7155e7fc436SJ. Bruce Fields 	 * the lock.
7165e7fc436SJ. Bruce Fields 	 */
7175e7fc436SJ. Bruce Fields 	if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
7181da177e4SLinus Torvalds 		goto out;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	if (!inode->i_fop)
7211da177e4SLinus Torvalds 		goto out;
7221da177e4SLinus Torvalds 
723999448a8SBernd Schubert 	host_err = nfsd_open_break_lease(inode, may_flags);
7246264d69dSAl Viro 	if (host_err) /* NOMEM or WOULDBLOCK */
7251da177e4SLinus Torvalds 		goto out_nfserr;
7261da177e4SLinus Torvalds 
727999448a8SBernd Schubert 	if (may_flags & NFSD_MAY_WRITE) {
728999448a8SBernd Schubert 		if (may_flags & NFSD_MAY_READ)
7299ecb6a08SJ. Bruce Fields 			flags = O_RDWR|O_LARGEFILE;
7309ecb6a08SJ. Bruce Fields 		else
7311da177e4SLinus Torvalds 			flags = O_WRONLY|O_LARGEFILE;
7321da177e4SLinus Torvalds 	}
733999448a8SBernd Schubert 
7348519f994SKinglong Mee 	file = dentry_open(&path, flags, current_cred());
7358519f994SKinglong Mee 	if (IS_ERR(file)) {
7368519f994SKinglong Mee 		host_err = PTR_ERR(file);
7378519f994SKinglong Mee 		goto out_nfserr;
73806effdbbSBernd Schubert 	}
73906effdbbSBernd Schubert 
7406035a27bSAl Viro 	host_err = ima_file_check(file, may_flags);
7418519f994SKinglong Mee 	if (host_err) {
742fd891454SChristoph Hellwig 		fput(file);
7438519f994SKinglong Mee 		goto out_nfserr;
7448519f994SKinglong Mee 	}
7458519f994SKinglong Mee 
7468519f994SKinglong Mee 	if (may_flags & NFSD_MAY_64BIT_COOKIE)
7478519f994SKinglong Mee 		file->f_mode |= FMODE_64BITHASH;
7488519f994SKinglong Mee 	else
7498519f994SKinglong Mee 		file->f_mode |= FMODE_32BITHASH;
7508519f994SKinglong Mee 
7518519f994SKinglong Mee 	*filp = file;
7521da177e4SLinus Torvalds out_nfserr:
7536264d69dSAl Viro 	err = nfserrno(host_err);
7541da177e4SLinus Torvalds out:
75565294c1fSJeff Layton 	return err;
75665294c1fSJeff Layton }
75765294c1fSJeff Layton 
75865294c1fSJeff Layton __be32
75965294c1fSJeff Layton nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
76065294c1fSJeff Layton 		int may_flags, struct file **filp)
76165294c1fSJeff Layton {
76265294c1fSJeff Layton 	__be32 err;
76365294c1fSJeff Layton 
76465294c1fSJeff Layton 	validate_process_creds();
76565294c1fSJeff Layton 	/*
76665294c1fSJeff Layton 	 * If we get here, then the client has already done an "open",
76765294c1fSJeff Layton 	 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
76865294c1fSJeff Layton 	 * in case a chmod has now revoked permission.
76965294c1fSJeff Layton 	 *
77065294c1fSJeff Layton 	 * Arguably we should also allow the owner override for
77165294c1fSJeff Layton 	 * directories, but we never have and it doesn't seem to have
77265294c1fSJeff Layton 	 * caused anyone a problem.  If we were to change this, note
77365294c1fSJeff Layton 	 * also that our filldir callbacks would need a variant of
77465294c1fSJeff Layton 	 * lookup_one_len that doesn't check permissions.
77565294c1fSJeff Layton 	 */
77665294c1fSJeff Layton 	if (type == S_IFREG)
77765294c1fSJeff Layton 		may_flags |= NFSD_MAY_OWNER_OVERRIDE;
77865294c1fSJeff Layton 	err = fh_verify(rqstp, fhp, type, may_flags);
77965294c1fSJeff Layton 	if (!err)
78065294c1fSJeff Layton 		err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
781e0e81739SDavid Howells 	validate_process_creds();
7821da177e4SLinus Torvalds 	return err;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
78565294c1fSJeff Layton __be32
78665294c1fSJeff Layton nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
78765294c1fSJeff Layton 		int may_flags, struct file **filp)
78865294c1fSJeff Layton {
78965294c1fSJeff Layton 	__be32 err;
79065294c1fSJeff Layton 
79165294c1fSJeff Layton 	validate_process_creds();
79265294c1fSJeff Layton 	err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
79365294c1fSJeff Layton 	validate_process_creds();
79465294c1fSJeff Layton 	return err;
79565294c1fSJeff Layton }
79665294c1fSJeff Layton 
7971da177e4SLinus Torvalds /*
798cf8208d0SJens Axboe  * Grab and keep cached pages associated with a file in the svc_rqst
799cf8208d0SJens Axboe  * so that they can be passed to the network sendmsg/sendpage routines
800cf8208d0SJens Axboe  * directly. They will be released after the sending has completed.
8011da177e4SLinus Torvalds  */
8021da177e4SLinus Torvalds static int
803cf8208d0SJens Axboe nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
804cf8208d0SJens Axboe 		  struct splice_desc *sd)
8051da177e4SLinus Torvalds {
806cf8208d0SJens Axboe 	struct svc_rqst *rqstp = sd->u.data;
807afc59400SJ. Bruce Fields 	struct page **pp = rqstp->rq_next_page;
808cf8208d0SJens Axboe 	struct page *page = buf->page;
809cf8208d0SJens Axboe 	size_t size;
810cf8208d0SJens Axboe 
811cf8208d0SJens Axboe 	size = sd->len;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	if (rqstp->rq_res.page_len == 0) {
8141da177e4SLinus Torvalds 		get_page(page);
815afc59400SJ. Bruce Fields 		put_page(*rqstp->rq_next_page);
816afc59400SJ. Bruce Fields 		*(rqstp->rq_next_page++) = page;
817cf8208d0SJens Axboe 		rqstp->rq_res.page_base = buf->offset;
8181da177e4SLinus Torvalds 		rqstp->rq_res.page_len = size;
81944524359SNeilBrown 	} else if (page != pp[-1]) {
8201da177e4SLinus Torvalds 		get_page(page);
821afc59400SJ. Bruce Fields 		if (*rqstp->rq_next_page)
822afc59400SJ. Bruce Fields 			put_page(*rqstp->rq_next_page);
823afc59400SJ. Bruce Fields 		*(rqstp->rq_next_page++) = page;
8241da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
82544524359SNeilBrown 	} else
8261da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	return size;
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds 
831cf8208d0SJens Axboe static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
832cf8208d0SJens Axboe 				    struct splice_desc *sd)
833cf8208d0SJens Axboe {
834cf8208d0SJens Axboe 	return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
835cf8208d0SJens Axboe }
836cf8208d0SJens Axboe 
83787c5942eSChuck Lever static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
83887c5942eSChuck Lever 			       struct file *file, loff_t offset,
83987c5942eSChuck Lever 			       unsigned long *count, int host_err)
8401da177e4SLinus Torvalds {
841dc97618dSJ. Bruce Fields 	if (host_err >= 0) {
842dc97618dSJ. Bruce Fields 		nfsdstats.io_read += host_err;
843dc97618dSJ. Bruce Fields 		*count = host_err;
844dc97618dSJ. Bruce Fields 		fsnotify_access(file);
84587c5942eSChuck Lever 		trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
846dc97618dSJ. Bruce Fields 		return 0;
84787c5942eSChuck Lever 	} else {
84887c5942eSChuck Lever 		trace_nfsd_read_err(rqstp, fhp, offset, host_err);
849dc97618dSJ. Bruce Fields 		return nfserrno(host_err);
850dc97618dSJ. Bruce Fields 	}
85187c5942eSChuck Lever }
8521da177e4SLinus Torvalds 
85387c5942eSChuck Lever __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
854dc97618dSJ. Bruce Fields 			struct file *file, loff_t offset, unsigned long *count)
855dc97618dSJ. Bruce Fields {
856cf8208d0SJens Axboe 	struct splice_desc sd = {
857cf8208d0SJens Axboe 		.len		= 0,
858cf8208d0SJens Axboe 		.total_len	= *count,
859cf8208d0SJens Axboe 		.pos		= offset,
860cf8208d0SJens Axboe 		.u.data		= rqstp,
861cf8208d0SJens Axboe 	};
862dc97618dSJ. Bruce Fields 	int host_err;
863cf8208d0SJens Axboe 
86487c5942eSChuck Lever 	trace_nfsd_read_splice(rqstp, fhp, offset, *count);
865afc59400SJ. Bruce Fields 	rqstp->rq_next_page = rqstp->rq_respages + 1;
866cf8208d0SJens Axboe 	host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
86787c5942eSChuck Lever 	return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
868dc97618dSJ. Bruce Fields }
869dc97618dSJ. Bruce Fields 
87087c5942eSChuck Lever __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
87187c5942eSChuck Lever 		  struct file *file, loff_t offset,
87287c5942eSChuck Lever 		  struct kvec *vec, int vlen, unsigned long *count)
873dc97618dSJ. Bruce Fields {
87473da852eSChristoph Hellwig 	struct iov_iter iter;
875dc97618dSJ. Bruce Fields 	int host_err;
876dc97618dSJ. Bruce Fields 
87787c5942eSChuck Lever 	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
878aa563d7bSDavid Howells 	iov_iter_kvec(&iter, READ, vec, vlen, *count);
87973da852eSChristoph Hellwig 	host_err = vfs_iter_read(file, &iter, &offset, 0);
88087c5942eSChuck Lever 	return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
883d911df7bSJ. Bruce Fields /*
884d911df7bSJ. Bruce Fields  * Gathered writes: If another process is currently writing to the file,
885d911df7bSJ. Bruce Fields  * there's a high chance this is another nfsd (triggered by a bulk write
886d911df7bSJ. Bruce Fields  * from a client's biod). Rather than syncing the file with each write
887d911df7bSJ. Bruce Fields  * request, we sleep for 10 msec.
888d911df7bSJ. Bruce Fields  *
889d911df7bSJ. Bruce Fields  * I don't know if this roughly approximates C. Juszak's idea of
890d911df7bSJ. Bruce Fields  * gathered writes, but it's a nice and simple solution (IMHO), and it
891d911df7bSJ. Bruce Fields  * seems to work:-)
892d911df7bSJ. Bruce Fields  *
893d911df7bSJ. Bruce Fields  * Note: we do this only in the NFSv2 case, since v3 and higher have a
894d911df7bSJ. Bruce Fields  * better tool (separate unstable writes and commits) for solving this
895d911df7bSJ. Bruce Fields  * problem.
896d911df7bSJ. Bruce Fields  */
897d911df7bSJ. Bruce Fields static int wait_for_concurrent_writes(struct file *file)
898d911df7bSJ. Bruce Fields {
899496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
900d911df7bSJ. Bruce Fields 	static ino_t last_ino;
901d911df7bSJ. Bruce Fields 	static dev_t last_dev;
902d911df7bSJ. Bruce Fields 	int err = 0;
903d911df7bSJ. Bruce Fields 
904d911df7bSJ. Bruce Fields 	if (atomic_read(&inode->i_writecount) > 1
905d911df7bSJ. Bruce Fields 	    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
906d911df7bSJ. Bruce Fields 		dprintk("nfsd: write defer %d\n", task_pid_nr(current));
907d911df7bSJ. Bruce Fields 		msleep(10);
908d911df7bSJ. Bruce Fields 		dprintk("nfsd: write resume %d\n", task_pid_nr(current));
909d911df7bSJ. Bruce Fields 	}
910d911df7bSJ. Bruce Fields 
911d911df7bSJ. Bruce Fields 	if (inode->i_state & I_DIRTY) {
912d911df7bSJ. Bruce Fields 		dprintk("nfsd: write sync %d\n", task_pid_nr(current));
9138018ab05SChristoph Hellwig 		err = vfs_fsync(file, 0);
914d911df7bSJ. Bruce Fields 	}
915d911df7bSJ. Bruce Fields 	last_ino = inode->i_ino;
916d911df7bSJ. Bruce Fields 	last_dev = inode->i_sb->s_dev;
917d911df7bSJ. Bruce Fields 	return err;
918d911df7bSJ. Bruce Fields }
919d911df7bSJ. Bruce Fields 
920af90f707SChristoph Hellwig __be32
9211da177e4SLinus Torvalds nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
9221da177e4SLinus Torvalds 				loff_t offset, struct kvec *vec, int vlen,
92354bbb7d2SKinglong Mee 				unsigned long *cnt, int stable)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct svc_export	*exp;
92673da852eSChristoph Hellwig 	struct iov_iter		iter;
927d890be15SChuck Lever 	__be32			nfserr;
9286264d69dSAl Viro 	int			host_err;
92948e03bc5STrond Myklebust 	int			use_wgather;
930e49dbbf3SKent Overstreet 	loff_t			pos = offset;
9318658452eSNeilBrown 	unsigned int		pflags = current->flags;
932ddef7ed2SChristoph Hellwig 	rwf_t			flags = 0;
9338658452eSNeilBrown 
934d890be15SChuck Lever 	trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
935d890be15SChuck Lever 
9367501cc2bSJeff Layton 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
9378658452eSNeilBrown 		/*
9388658452eSNeilBrown 		 * We want less throttling in balance_dirty_pages()
9398658452eSNeilBrown 		 * and shrink_inactive_list() so that nfs to
9408658452eSNeilBrown 		 * localhost doesn't cause nfsd to lock up due to all
9418658452eSNeilBrown 		 * the client's dirty pages or its congested queue.
9428658452eSNeilBrown 		 */
9438658452eSNeilBrown 		current->flags |= PF_LESS_THROTTLE;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	exp = fhp->fh_export;
94648e03bc5STrond Myklebust 	use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	if (!EX_ISSYNC(exp))
94954bbb7d2SKinglong Mee 		stable = NFS_UNSTABLE;
9501da177e4SLinus Torvalds 
95124368aadSChristoph Hellwig 	if (stable && !use_wgather)
95224368aadSChristoph Hellwig 		flags |= RWF_SYNC;
95324368aadSChristoph Hellwig 
954aa563d7bSDavid Howells 	iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
95573da852eSChristoph Hellwig 	host_err = vfs_iter_write(file, &iter, &pos, flags);
956e4636d53SJ. Bruce Fields 	if (host_err < 0)
957e4636d53SJ. Bruce Fields 		goto out_nfserr;
958d890be15SChuck Lever 	nfsdstats.io_write += *cnt;
9592a12a9d7SEric Paris 	fsnotify_modify(file);
9601da177e4SLinus Torvalds 
961*bbf2f098STrond Myklebust 	if (stable && use_wgather) {
962d911df7bSJ. Bruce Fields 		host_err = wait_for_concurrent_writes(file);
963*bbf2f098STrond Myklebust 		if (host_err < 0)
964*bbf2f098STrond Myklebust 			nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
965*bbf2f098STrond Myklebust 						 nfsd_net_id));
966*bbf2f098STrond Myklebust 	}
9671da177e4SLinus Torvalds 
968e4636d53SJ. Bruce Fields out_nfserr:
969d890be15SChuck Lever 	if (host_err >= 0) {
970d890be15SChuck Lever 		trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
971d890be15SChuck Lever 		nfserr = nfs_ok;
972d890be15SChuck Lever 	} else {
973d890be15SChuck Lever 		trace_nfsd_write_err(rqstp, fhp, offset, host_err);
974d890be15SChuck Lever 		nfserr = nfserrno(host_err);
975d890be15SChuck Lever 	}
9767501cc2bSJeff Layton 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
977717a94b5SNeilBrown 		current_restore_flags(pflags, PF_LESS_THROTTLE);
978d890be15SChuck Lever 	return nfserr;
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
981dc97618dSJ. Bruce Fields /*
982dc97618dSJ. Bruce Fields  * Read data from a file. count must contain the requested read count
983dc97618dSJ. Bruce Fields  * on entry. On return, *count contains the number of bytes actually read.
984dc97618dSJ. Bruce Fields  * N.B. After this call fhp needs an fh_put
985dc97618dSJ. Bruce Fields  */
986dc97618dSJ. Bruce Fields __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
987dc97618dSJ. Bruce Fields 	loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
988dc97618dSJ. Bruce Fields {
98948cd7b51SJeff Layton 	struct nfsd_file	*nf;
990dc97618dSJ. Bruce Fields 	struct file *file;
991dc97618dSJ. Bruce Fields 	__be32 err;
992dc97618dSJ. Bruce Fields 
993f394b62bSChuck Lever 	trace_nfsd_read_start(rqstp, fhp, offset, *count);
99448cd7b51SJeff Layton 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
995dc97618dSJ. Bruce Fields 	if (err)
996dc97618dSJ. Bruce Fields 		return err;
997dc97618dSJ. Bruce Fields 
99848cd7b51SJeff Layton 	file = nf->nf_file;
999a4058c5bSChristoph Hellwig 	if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
100087c5942eSChuck Lever 		err = nfsd_splice_read(rqstp, fhp, file, offset, count);
1001a4058c5bSChristoph Hellwig 	else
100287c5942eSChuck Lever 		err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count);
10036e8b50d1SJeff Layton 
100448cd7b51SJeff Layton 	nfsd_file_put(nf);
1005dc97618dSJ. Bruce Fields 
1006f394b62bSChuck Lever 	trace_nfsd_read_done(rqstp, fhp, offset, *count);
10076e8b50d1SJeff Layton 
1008fa0a2126SJ. Bruce Fields 	return err;
1009fa0a2126SJ. Bruce Fields }
1010fa0a2126SJ. Bruce Fields 
10111da177e4SLinus Torvalds /*
10121da177e4SLinus Torvalds  * Write data to a file.
10131da177e4SLinus Torvalds  * The stable flag requests synchronous writes.
10141da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
10151da177e4SLinus Torvalds  */
10166264d69dSAl Viro __be32
101752e380e0SKinglong Mee nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
101852e380e0SKinglong Mee 	   struct kvec *vec, int vlen, unsigned long *cnt, int stable)
10191da177e4SLinus Torvalds {
1020b4935239SJeff Layton 	struct nfsd_file *nf;
1021b4935239SJeff Layton 	__be32 err;
10221da177e4SLinus Torvalds 
1023f394b62bSChuck Lever 	trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
10246e8b50d1SJeff Layton 
1025b4935239SJeff Layton 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
10261da177e4SLinus Torvalds 	if (err)
10271da177e4SLinus Torvalds 		goto out;
10281da177e4SLinus Torvalds 
1029b4935239SJeff Layton 	err = nfsd_vfs_write(rqstp, fhp, nf->nf_file, offset, vec,
1030b4935239SJeff Layton 			vlen, cnt, stable);
1031b4935239SJeff Layton 	nfsd_file_put(nf);
10321da177e4SLinus Torvalds out:
1033f394b62bSChuck Lever 	trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
10341da177e4SLinus Torvalds 	return err;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
10381da177e4SLinus Torvalds /*
10391da177e4SLinus Torvalds  * Commit all pending writes to stable storage.
1040aa696a6fSTrond Myklebust  *
1041aa696a6fSTrond Myklebust  * Note: we only guarantee that data that lies within the range specified
1042aa696a6fSTrond Myklebust  * by the 'offset' and 'count' parameters will be synced.
10431da177e4SLinus Torvalds  *
10441da177e4SLinus Torvalds  * Unfortunately we cannot lock the file to make sure we return full WCC
10451da177e4SLinus Torvalds  * data to the client, as locking happens lower down in the filesystem.
10461da177e4SLinus Torvalds  */
10476264d69dSAl Viro __be32
10481da177e4SLinus Torvalds nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
10491da177e4SLinus Torvalds                loff_t offset, unsigned long count)
10501da177e4SLinus Torvalds {
10515920afa3SJeff Layton 	struct nfsd_file	*nf;
1052aa696a6fSTrond Myklebust 	loff_t			end = LLONG_MAX;
1053aa696a6fSTrond Myklebust 	__be32			err = nfserr_inval;
10541da177e4SLinus Torvalds 
1055aa696a6fSTrond Myklebust 	if (offset < 0)
1056aa696a6fSTrond Myklebust 		goto out;
1057aa696a6fSTrond Myklebust 	if (count != 0) {
1058aa696a6fSTrond Myklebust 		end = offset + (loff_t)count - 1;
1059aa696a6fSTrond Myklebust 		if (end < offset)
1060aa696a6fSTrond Myklebust 			goto out;
1061aa696a6fSTrond Myklebust 	}
10621da177e4SLinus Torvalds 
10635920afa3SJeff Layton 	err = nfsd_file_acquire(rqstp, fhp,
10645920afa3SJeff Layton 			NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
10658837abcaSMiklos Szeredi 	if (err)
1066aa696a6fSTrond Myklebust 		goto out;
10671da177e4SLinus Torvalds 	if (EX_ISSYNC(fhp->fh_export)) {
10685920afa3SJeff Layton 		int err2 = vfs_fsync_range(nf->nf_file, offset, end, 0);
1069aa696a6fSTrond Myklebust 
1070*bbf2f098STrond Myklebust 		switch (err2) {
1071*bbf2f098STrond Myklebust 		case 0:
1072*bbf2f098STrond Myklebust 			break;
1073*bbf2f098STrond Myklebust 		case -EINVAL:
10741da177e4SLinus Torvalds 			err = nfserr_notsupp;
1075*bbf2f098STrond Myklebust 			break;
1076*bbf2f098STrond Myklebust 		default:
1077*bbf2f098STrond Myklebust 			err = nfserrno(err2);
1078*bbf2f098STrond Myklebust 			nfsd_reset_boot_verifier(net_generic(nf->nf_net,
1079*bbf2f098STrond Myklebust 						 nfsd_net_id));
1080*bbf2f098STrond Myklebust 		}
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 
10835920afa3SJeff Layton 	nfsd_file_put(nf);
1084aa696a6fSTrond Myklebust out:
10851da177e4SLinus Torvalds 	return err;
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
10881da177e4SLinus Torvalds 
1089f2b0dee2SAdrian Bunk static __be32
10905c002b3bSJ. Bruce Fields nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
10915c002b3bSJ. Bruce Fields 			struct iattr *iap)
10925c002b3bSJ. Bruce Fields {
10935c002b3bSJ. Bruce Fields 	/*
10945c002b3bSJ. Bruce Fields 	 * Mode has already been set earlier in create:
10955c002b3bSJ. Bruce Fields 	 */
10965c002b3bSJ. Bruce Fields 	iap->ia_valid &= ~ATTR_MODE;
10975c002b3bSJ. Bruce Fields 	/*
10985c002b3bSJ. Bruce Fields 	 * Setting uid/gid works only for root.  Irix appears to
10995c002b3bSJ. Bruce Fields 	 * send along the gid on create when it tries to implement
11005c002b3bSJ. Bruce Fields 	 * setgid directories via NFS:
11015c002b3bSJ. Bruce Fields 	 */
11026fab8779SEric W. Biederman 	if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
11035c002b3bSJ. Bruce Fields 		iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
11045c002b3bSJ. Bruce Fields 	if (iap->ia_valid)
11055c002b3bSJ. Bruce Fields 		return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
11060f3a24b4STrond Myklebust 	/* Callers expect file metadata to be committed here */
1107722b620dSJeff Layton 	return nfserrno(commit_metadata(resfhp));
11085c002b3bSJ. Bruce Fields }
11095c002b3bSJ. Bruce Fields 
11104ac35c2fSwengang wang /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
11114ac35c2fSwengang wang  * setting size to 0 may fail for some specific file systems by the permission
11124ac35c2fSwengang wang  * checking which requires WRITE permission but the mode is 000.
11134ac35c2fSwengang wang  * we ignore the resizing(to 0) on the just new created file, since the size is
11144ac35c2fSwengang wang  * 0 after file created.
11154ac35c2fSwengang wang  *
11164ac35c2fSwengang wang  * call this only after vfs_create() is called.
11174ac35c2fSwengang wang  * */
11184ac35c2fSwengang wang static void
11194ac35c2fSwengang wang nfsd_check_ignore_resizing(struct iattr *iap)
11204ac35c2fSwengang wang {
11214ac35c2fSwengang wang 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
11224ac35c2fSwengang wang 		iap->ia_valid &= ~ATTR_SIZE;
11234ac35c2fSwengang wang }
11244ac35c2fSwengang wang 
1125b44061d0SJ. Bruce Fields /* The parent directory should already be locked: */
11266264d69dSAl Viro __be32
1127b44061d0SJ. Bruce Fields nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
11281da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
11291da177e4SLinus Torvalds 		int type, dev_t rdev, struct svc_fh *resfhp)
11301da177e4SLinus Torvalds {
11312b118859SDan Carpenter 	struct dentry	*dentry, *dchild;
11321da177e4SLinus Torvalds 	struct inode	*dirp;
11336264d69dSAl Viro 	__be32		err;
11345c002b3bSJ. Bruce Fields 	__be32		err2;
11356264d69dSAl Viro 	int		host_err;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
11382b0143b5SDavid Howells 	dirp = d_inode(dentry);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	dchild = dget(resfhp->fh_dentry);
11411da177e4SLinus Torvalds 	if (!fhp->fh_locked) {
1142b44061d0SJ. Bruce Fields 		WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1143a6a9f18fSAl Viro 				dentry);
1144d75f2b9fSAl Viro 		err = nfserr_io;
11451da177e4SLinus Torvalds 		goto out;
11461da177e4SLinus Torvalds 	}
11471da177e4SLinus Torvalds 
11487eed34f1SOleg Drokin 	err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
11497eed34f1SOleg Drokin 	if (err)
11507eed34f1SOleg Drokin 		goto out;
11517eed34f1SOleg Drokin 
11521da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
11531da177e4SLinus Torvalds 		iap->ia_mode = 0;
11541da177e4SLinus Torvalds 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
11551da177e4SLinus Torvalds 
1156088406bcSJ. Bruce Fields 	err = 0;
11574a55c101SJan Kara 	host_err = 0;
11581da177e4SLinus Torvalds 	switch (type) {
11591da177e4SLinus Torvalds 	case S_IFREG:
1160312b63fbSAl Viro 		host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
11614ac35c2fSwengang wang 		if (!host_err)
11624ac35c2fSwengang wang 			nfsd_check_ignore_resizing(iap);
11631da177e4SLinus Torvalds 		break;
11641da177e4SLinus Torvalds 	case S_IFDIR:
11656264d69dSAl Viro 		host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
11663819bb0dSAl Viro 		if (!host_err && unlikely(d_unhashed(dchild))) {
11673819bb0dSAl Viro 			struct dentry *d;
11683819bb0dSAl Viro 			d = lookup_one_len(dchild->d_name.name,
11693819bb0dSAl Viro 					   dchild->d_parent,
11703819bb0dSAl Viro 					   dchild->d_name.len);
11713819bb0dSAl Viro 			if (IS_ERR(d)) {
11723819bb0dSAl Viro 				host_err = PTR_ERR(d);
11733819bb0dSAl Viro 				break;
11743819bb0dSAl Viro 			}
11753819bb0dSAl Viro 			if (unlikely(d_is_negative(d))) {
11763819bb0dSAl Viro 				dput(d);
11773819bb0dSAl Viro 				err = nfserr_serverfault;
11783819bb0dSAl Viro 				goto out;
11793819bb0dSAl Viro 			}
11803819bb0dSAl Viro 			dput(resfhp->fh_dentry);
11813819bb0dSAl Viro 			resfhp->fh_dentry = dget(d);
11823819bb0dSAl Viro 			err = fh_update(resfhp);
11833819bb0dSAl Viro 			dput(dchild);
11843819bb0dSAl Viro 			dchild = d;
11853819bb0dSAl Viro 			if (err)
11863819bb0dSAl Viro 				goto out;
11873819bb0dSAl Viro 		}
11881da177e4SLinus Torvalds 		break;
11891da177e4SLinus Torvalds 	case S_IFCHR:
11901da177e4SLinus Torvalds 	case S_IFBLK:
11911da177e4SLinus Torvalds 	case S_IFIFO:
11921da177e4SLinus Torvalds 	case S_IFSOCK:
11936264d69dSAl Viro 		host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
11941da177e4SLinus Torvalds 		break;
119571423274SJ. Bruce Fields 	default:
119671423274SJ. Bruce Fields 		printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
119771423274SJ. Bruce Fields 		       type);
119871423274SJ. Bruce Fields 		host_err = -EINVAL;
1199463c3197SDave Hansen 	}
12004a55c101SJan Kara 	if (host_err < 0)
1201463c3197SDave Hansen 		goto out_nfserr;
12021da177e4SLinus Torvalds 
1203f501912aSBen Myers 	err = nfsd_create_setattr(rqstp, resfhp, iap);
12041da177e4SLinus Torvalds 
1205f501912aSBen Myers 	/*
12060f3a24b4STrond Myklebust 	 * nfsd_create_setattr already committed the child.  Transactional
12070f3a24b4STrond Myklebust 	 * filesystems had a chance to commit changes for both parent and
1208b44061d0SJ. Bruce Fields 	 * child simultaneously making the following commit_metadata a
12090f3a24b4STrond Myklebust 	 * noop.
1210f501912aSBen Myers 	 */
1211f501912aSBen Myers 	err2 = nfserrno(commit_metadata(fhp));
1212f193fbabSYAMAMOTO Takashi 	if (err2)
1213f193fbabSYAMAMOTO Takashi 		err = err2;
12141da177e4SLinus Torvalds 	/*
12151da177e4SLinus Torvalds 	 * Update the file handle to get the new inode info.
12161da177e4SLinus Torvalds 	 */
12171da177e4SLinus Torvalds 	if (!err)
12181da177e4SLinus Torvalds 		err = fh_update(resfhp);
12191da177e4SLinus Torvalds out:
12201da177e4SLinus Torvalds 	dput(dchild);
12211da177e4SLinus Torvalds 	return err;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds out_nfserr:
12246264d69dSAl Viro 	err = nfserrno(host_err);
12251da177e4SLinus Torvalds 	goto out;
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
1228b44061d0SJ. Bruce Fields /*
1229b44061d0SJ. Bruce Fields  * Create a filesystem object (regular, directory, special).
1230b44061d0SJ. Bruce Fields  * Note that the parent directory is left locked.
1231b44061d0SJ. Bruce Fields  *
1232b44061d0SJ. Bruce Fields  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1233b44061d0SJ. Bruce Fields  */
1234b44061d0SJ. Bruce Fields __be32
1235b44061d0SJ. Bruce Fields nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1236b44061d0SJ. Bruce Fields 		char *fname, int flen, struct iattr *iap,
1237b44061d0SJ. Bruce Fields 		int type, dev_t rdev, struct svc_fh *resfhp)
1238b44061d0SJ. Bruce Fields {
1239b44061d0SJ. Bruce Fields 	struct dentry	*dentry, *dchild = NULL;
1240b44061d0SJ. Bruce Fields 	__be32		err;
1241b44061d0SJ. Bruce Fields 	int		host_err;
1242b44061d0SJ. Bruce Fields 
1243b44061d0SJ. Bruce Fields 	if (isdotent(fname, flen))
1244b44061d0SJ. Bruce Fields 		return nfserr_exist;
1245b44061d0SJ. Bruce Fields 
1246fa08139dSJ. Bruce Fields 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1247b44061d0SJ. Bruce Fields 	if (err)
1248b44061d0SJ. Bruce Fields 		return err;
1249b44061d0SJ. Bruce Fields 
1250b44061d0SJ. Bruce Fields 	dentry = fhp->fh_dentry;
1251b44061d0SJ. Bruce Fields 
1252b44061d0SJ. Bruce Fields 	host_err = fh_want_write(fhp);
1253b44061d0SJ. Bruce Fields 	if (host_err)
1254b44061d0SJ. Bruce Fields 		return nfserrno(host_err);
1255b44061d0SJ. Bruce Fields 
1256b44061d0SJ. Bruce Fields 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1257b44061d0SJ. Bruce Fields 	dchild = lookup_one_len(fname, dentry, flen);
1258b44061d0SJ. Bruce Fields 	host_err = PTR_ERR(dchild);
1259b44061d0SJ. Bruce Fields 	if (IS_ERR(dchild))
1260b44061d0SJ. Bruce Fields 		return nfserrno(host_err);
1261b44061d0SJ. Bruce Fields 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1262502aa0a5SJosef Bacik 	/*
1263502aa0a5SJosef Bacik 	 * We unconditionally drop our ref to dchild as fh_compose will have
1264502aa0a5SJosef Bacik 	 * already grabbed its own ref for it.
1265502aa0a5SJosef Bacik 	 */
1266b44061d0SJ. Bruce Fields 	dput(dchild);
1267502aa0a5SJosef Bacik 	if (err)
1268b44061d0SJ. Bruce Fields 		return err;
1269b44061d0SJ. Bruce Fields 	return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1270b44061d0SJ. Bruce Fields 					rdev, resfhp);
1271b44061d0SJ. Bruce Fields }
1272b44061d0SJ. Bruce Fields 
12731da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
1274ac6721a1SMi Jinlong 
12751da177e4SLinus Torvalds /*
1276ac6721a1SMi Jinlong  * NFSv3 and NFSv4 version of nfsd_create
12771da177e4SLinus Torvalds  */
12786264d69dSAl Viro __be32
1279ac6721a1SMi Jinlong do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
12801da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
12811da177e4SLinus Torvalds 		struct svc_fh *resfhp, int createmode, u32 *verifier,
1282856121b2SJ. Bruce Fields 	        bool *truncp, bool *created)
12831da177e4SLinus Torvalds {
12841da177e4SLinus Torvalds 	struct dentry	*dentry, *dchild = NULL;
12851da177e4SLinus Torvalds 	struct inode	*dirp;
12866264d69dSAl Viro 	__be32		err;
12876264d69dSAl Viro 	int		host_err;
12881da177e4SLinus Torvalds 	__u32		v_mtime=0, v_atime=0;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	err = nfserr_perm;
12911da177e4SLinus Torvalds 	if (!flen)
12921da177e4SLinus Torvalds 		goto out;
12931da177e4SLinus Torvalds 	err = nfserr_exist;
12941da177e4SLinus Torvalds 	if (isdotent(fname, flen))
12951da177e4SLinus Torvalds 		goto out;
12961da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
12971da177e4SLinus Torvalds 		iap->ia_mode = 0;
12981574dff8SSachin Prabhu 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
12991da177e4SLinus Torvalds 	if (err)
13001da177e4SLinus Torvalds 		goto out;
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
13032b0143b5SDavid Howells 	dirp = d_inode(dentry);
13041da177e4SLinus Torvalds 
13054a55c101SJan Kara 	host_err = fh_want_write(fhp);
13064a55c101SJan Kara 	if (host_err)
13074a55c101SJan Kara 		goto out_nfserr;
13084a55c101SJan Kara 
130912fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	/*
13121da177e4SLinus Torvalds 	 * Compose the response file handle.
13131da177e4SLinus Torvalds 	 */
13141da177e4SLinus Torvalds 	dchild = lookup_one_len(fname, dentry, flen);
13156264d69dSAl Viro 	host_err = PTR_ERR(dchild);
13161da177e4SLinus Torvalds 	if (IS_ERR(dchild))
13171da177e4SLinus Torvalds 		goto out_nfserr;
13181da177e4SLinus Torvalds 
13191574dff8SSachin Prabhu 	/* If file doesn't exist, check for permissions to create one */
13202b0143b5SDavid Howells 	if (d_really_is_negative(dchild)) {
13211574dff8SSachin Prabhu 		err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
13221574dff8SSachin Prabhu 		if (err)
13231574dff8SSachin Prabhu 			goto out;
13241574dff8SSachin Prabhu 	}
13251574dff8SSachin Prabhu 
13261da177e4SLinus Torvalds 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
13271da177e4SLinus Torvalds 	if (err)
13281da177e4SLinus Torvalds 		goto out;
13291da177e4SLinus Torvalds 
1330ac6721a1SMi Jinlong 	if (nfsd_create_is_exclusive(createmode)) {
1331c397852cSPeter Staubach 		/* solaris7 gets confused (bugid 4218508) if these have
1332749997e5SJeff Layton 		 * the high bit set, so just clear the high bits. If this is
1333749997e5SJeff Layton 		 * ever changed to use different attrs for storing the
1334749997e5SJeff Layton 		 * verifier, then do_open_lookup() will also need to be fixed
1335749997e5SJeff Layton 		 * accordingly.
13361da177e4SLinus Torvalds 		 */
13371da177e4SLinus Torvalds 		v_mtime = verifier[0]&0x7fffffff;
13381da177e4SLinus Torvalds 		v_atime = verifier[1]&0x7fffffff;
13391da177e4SLinus Torvalds 	}
13401da177e4SLinus Torvalds 
13412b0143b5SDavid Howells 	if (d_really_is_positive(dchild)) {
13421da177e4SLinus Torvalds 		err = 0;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 		switch (createmode) {
13451da177e4SLinus Torvalds 		case NFS3_CREATE_UNCHECKED:
1346e36cb0b8SDavid Howells 			if (! d_is_reg(dchild))
13479dc4e6c4SJ. Bruce Fields 				goto out;
13481da177e4SLinus Torvalds 			else if (truncp) {
13491da177e4SLinus Torvalds 				/* in nfsv4, we need to treat this case a little
13501da177e4SLinus Torvalds 				 * differently.  we don't want to truncate the
13511da177e4SLinus Torvalds 				 * file now; this would be wrong if the OPEN
13521da177e4SLinus Torvalds 				 * fails for some other reason.  furthermore,
13531da177e4SLinus Torvalds 				 * if the size is nonzero, we should ignore it
13541da177e4SLinus Torvalds 				 * according to spec!
13551da177e4SLinus Torvalds 				 */
13561da177e4SLinus Torvalds 				*truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
13571da177e4SLinus Torvalds 			}
13581da177e4SLinus Torvalds 			else {
13591da177e4SLinus Torvalds 				iap->ia_valid &= ATTR_SIZE;
13601da177e4SLinus Torvalds 				goto set_attr;
13611da177e4SLinus Torvalds 			}
13621da177e4SLinus Torvalds 			break;
13631da177e4SLinus Torvalds 		case NFS3_CREATE_EXCLUSIVE:
13642b0143b5SDavid Howells 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
13652b0143b5SDavid Howells 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
13662b0143b5SDavid Howells 			    && d_inode(dchild)->i_size  == 0 ) {
13677007c90fSNeil Brown 				if (created)
13687007c90fSNeil Brown 					*created = 1;
13691da177e4SLinus Torvalds 				break;
13707007c90fSNeil Brown 			}
13710ac203cbSGustavo A. R. Silva 			/* fall through */
1372ac6721a1SMi Jinlong 		case NFS4_CREATE_EXCLUSIVE4_1:
13732b0143b5SDavid Howells 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
13742b0143b5SDavid Howells 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
13752b0143b5SDavid Howells 			    && d_inode(dchild)->i_size  == 0 ) {
13767007c90fSNeil Brown 				if (created)
13777007c90fSNeil Brown 					*created = 1;
1378ac6721a1SMi Jinlong 				goto set_attr;
13797007c90fSNeil Brown 			}
13800ac203cbSGustavo A. R. Silva 			/* fall through */
13811da177e4SLinus Torvalds 		case NFS3_CREATE_GUARDED:
13821da177e4SLinus Torvalds 			err = nfserr_exist;
13831da177e4SLinus Torvalds 		}
1384bad0dcffSAl Viro 		fh_drop_write(fhp);
13851da177e4SLinus Torvalds 		goto out;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 
1388312b63fbSAl Viro 	host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1389463c3197SDave Hansen 	if (host_err < 0) {
1390bad0dcffSAl Viro 		fh_drop_write(fhp);
13911da177e4SLinus Torvalds 		goto out_nfserr;
1392463c3197SDave Hansen 	}
139381ac95c5SJ. Bruce Fields 	if (created)
139481ac95c5SJ. Bruce Fields 		*created = 1;
13951da177e4SLinus Torvalds 
13964ac35c2fSwengang wang 	nfsd_check_ignore_resizing(iap);
13974ac35c2fSwengang wang 
1398ac6721a1SMi Jinlong 	if (nfsd_create_is_exclusive(createmode)) {
1399c397852cSPeter Staubach 		/* Cram the verifier into atime/mtime */
14001da177e4SLinus Torvalds 		iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1401c397852cSPeter Staubach 			| ATTR_MTIME_SET|ATTR_ATIME_SET;
14021da177e4SLinus Torvalds 		/* XXX someone who knows this better please fix it for nsec */
14031da177e4SLinus Torvalds 		iap->ia_mtime.tv_sec = v_mtime;
14041da177e4SLinus Torvalds 		iap->ia_atime.tv_sec = v_atime;
14051da177e4SLinus Torvalds 		iap->ia_mtime.tv_nsec = 0;
14061da177e4SLinus Torvalds 		iap->ia_atime.tv_nsec = 0;
14071da177e4SLinus Torvalds 	}
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds  set_attr:
1410f501912aSBen Myers 	err = nfsd_create_setattr(rqstp, resfhp, iap);
1411f501912aSBen Myers 
1412f501912aSBen Myers 	/*
14130f3a24b4STrond Myklebust 	 * nfsd_create_setattr already committed the child
14140f3a24b4STrond Myklebust 	 * (and possibly also the parent).
1415f501912aSBen Myers 	 */
1416f501912aSBen Myers 	if (!err)
1417f501912aSBen Myers 		err = nfserrno(commit_metadata(fhp));
1418f193fbabSYAMAMOTO Takashi 
1419f193fbabSYAMAMOTO Takashi 	/*
1420f193fbabSYAMAMOTO Takashi 	 * Update the filehandle to get the new inode info.
1421f193fbabSYAMAMOTO Takashi 	 */
1422f193fbabSYAMAMOTO Takashi 	if (!err)
1423f193fbabSYAMAMOTO Takashi 		err = fh_update(resfhp);
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds  out:
14261da177e4SLinus Torvalds 	fh_unlock(fhp);
14271da177e4SLinus Torvalds 	if (dchild && !IS_ERR(dchild))
14281da177e4SLinus Torvalds 		dput(dchild);
14294a55c101SJan Kara 	fh_drop_write(fhp);
14301da177e4SLinus Torvalds  	return err;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds  out_nfserr:
14336264d69dSAl Viro 	err = nfserrno(host_err);
14341da177e4SLinus Torvalds 	goto out;
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds /*
14391da177e4SLinus Torvalds  * Read a symlink. On entry, *lenp must contain the maximum path length that
14401da177e4SLinus Torvalds  * fits into the buffer. On return, it contains the true length.
14411da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
14421da177e4SLinus Torvalds  */
14436264d69dSAl Viro __be32
14441da177e4SLinus Torvalds nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
14451da177e4SLinus Torvalds {
14466264d69dSAl Viro 	__be32		err;
14474d7edbc3SAl Viro 	const char *link;
144868ac1234SAl Viro 	struct path path;
14494d7edbc3SAl Viro 	DEFINE_DELAYED_CALL(done);
14504d7edbc3SAl Viro 	int len;
14511da177e4SLinus Torvalds 
14528837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
14534d7edbc3SAl Viro 	if (unlikely(err))
14544d7edbc3SAl Viro 		return err;
14551da177e4SLinus Torvalds 
145668ac1234SAl Viro 	path.mnt = fhp->fh_export->ex_path.mnt;
145768ac1234SAl Viro 	path.dentry = fhp->fh_dentry;
14581da177e4SLinus Torvalds 
14594d7edbc3SAl Viro 	if (unlikely(!d_is_symlink(path.dentry)))
14604d7edbc3SAl Viro 		return nfserr_inval;
14611da177e4SLinus Torvalds 
146268ac1234SAl Viro 	touch_atime(&path);
14631da177e4SLinus Torvalds 
14644d7edbc3SAl Viro 	link = vfs_get_link(path.dentry, &done);
14654d7edbc3SAl Viro 	if (IS_ERR(link))
14664d7edbc3SAl Viro 		return nfserrno(PTR_ERR(link));
14671da177e4SLinus Torvalds 
14684d7edbc3SAl Viro 	len = strlen(link);
14694d7edbc3SAl Viro 	if (len < *lenp)
14704d7edbc3SAl Viro 		*lenp = len;
14714d7edbc3SAl Viro 	memcpy(buf, link, *lenp);
14724d7edbc3SAl Viro 	do_delayed_call(&done);
14734d7edbc3SAl Viro 	return 0;
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds /*
14771da177e4SLinus Torvalds  * Create a symlink and look up its inode
14781da177e4SLinus Torvalds  * N.B. After this call _both_ fhp and resfhp need an fh_put
14791da177e4SLinus Torvalds  */
14806264d69dSAl Viro __be32
14811da177e4SLinus Torvalds nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
14821da177e4SLinus Torvalds 				char *fname, int flen,
148352ee0433SJ. Bruce Fields 				char *path,
14841e444f5bSKinglong Mee 				struct svc_fh *resfhp)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	struct dentry	*dentry, *dnew;
14876264d69dSAl Viro 	__be32		err, cerr;
14886264d69dSAl Viro 	int		host_err;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	err = nfserr_noent;
149152ee0433SJ. Bruce Fields 	if (!flen || path[0] == '\0')
14921da177e4SLinus Torvalds 		goto out;
14931da177e4SLinus Torvalds 	err = nfserr_exist;
14941da177e4SLinus Torvalds 	if (isdotent(fname, flen))
14951da177e4SLinus Torvalds 		goto out;
14961da177e4SLinus Torvalds 
14978837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
14981da177e4SLinus Torvalds 	if (err)
14991da177e4SLinus Torvalds 		goto out;
15004a55c101SJan Kara 
15014a55c101SJan Kara 	host_err = fh_want_write(fhp);
15024a55c101SJan Kara 	if (host_err)
15034a55c101SJan Kara 		goto out_nfserr;
15044a55c101SJan Kara 
15051da177e4SLinus Torvalds 	fh_lock(fhp);
15061da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
15071da177e4SLinus Torvalds 	dnew = lookup_one_len(fname, dentry, flen);
15086264d69dSAl Viro 	host_err = PTR_ERR(dnew);
15091da177e4SLinus Torvalds 	if (IS_ERR(dnew))
15101da177e4SLinus Torvalds 		goto out_nfserr;
15111da177e4SLinus Torvalds 
15122b0143b5SDavid Howells 	host_err = vfs_symlink(d_inode(dentry), dnew, path);
15136264d69dSAl Viro 	err = nfserrno(host_err);
1514f501912aSBen Myers 	if (!err)
1515f501912aSBen Myers 		err = nfserrno(commit_metadata(fhp));
15161da177e4SLinus Torvalds 	fh_unlock(fhp);
15171da177e4SLinus Torvalds 
1518bad0dcffSAl Viro 	fh_drop_write(fhp);
151975c3f29dSDave Hansen 
15201da177e4SLinus Torvalds 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
15211da177e4SLinus Torvalds 	dput(dnew);
15221da177e4SLinus Torvalds 	if (err==0) err = cerr;
15231da177e4SLinus Torvalds out:
15241da177e4SLinus Torvalds 	return err;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds out_nfserr:
15276264d69dSAl Viro 	err = nfserrno(host_err);
15281da177e4SLinus Torvalds 	goto out;
15291da177e4SLinus Torvalds }
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds /*
15321da177e4SLinus Torvalds  * Create a hardlink
15331da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
15341da177e4SLinus Torvalds  */
15356264d69dSAl Viro __be32
15361da177e4SLinus Torvalds nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
15371da177e4SLinus Torvalds 				char *name, int len, struct svc_fh *tfhp)
15381da177e4SLinus Torvalds {
15391da177e4SLinus Torvalds 	struct dentry	*ddir, *dnew, *dold;
154055b13354SJ. Bruce Fields 	struct inode	*dirp;
15416264d69dSAl Viro 	__be32		err;
15426264d69dSAl Viro 	int		host_err;
15431da177e4SLinus Torvalds 
15448837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
15451da177e4SLinus Torvalds 	if (err)
15461da177e4SLinus Torvalds 		goto out;
15477d818a7bSJ. Bruce Fields 	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
15481da177e4SLinus Torvalds 	if (err)
15491da177e4SLinus Torvalds 		goto out;
15507d818a7bSJ. Bruce Fields 	err = nfserr_isdir;
1551e36cb0b8SDavid Howells 	if (d_is_dir(tfhp->fh_dentry))
15527d818a7bSJ. Bruce Fields 		goto out;
15531da177e4SLinus Torvalds 	err = nfserr_perm;
15541da177e4SLinus Torvalds 	if (!len)
15551da177e4SLinus Torvalds 		goto out;
15561da177e4SLinus Torvalds 	err = nfserr_exist;
15571da177e4SLinus Torvalds 	if (isdotent(name, len))
15581da177e4SLinus Torvalds 		goto out;
15591da177e4SLinus Torvalds 
15604a55c101SJan Kara 	host_err = fh_want_write(tfhp);
15614a55c101SJan Kara 	if (host_err) {
15624a55c101SJan Kara 		err = nfserrno(host_err);
15634a55c101SJan Kara 		goto out;
15644a55c101SJan Kara 	}
15654a55c101SJan Kara 
156612fd3520SPeter Zijlstra 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
15671da177e4SLinus Torvalds 	ddir = ffhp->fh_dentry;
15682b0143b5SDavid Howells 	dirp = d_inode(ddir);
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	dnew = lookup_one_len(name, ddir, len);
15716264d69dSAl Viro 	host_err = PTR_ERR(dnew);
15721da177e4SLinus Torvalds 	if (IS_ERR(dnew))
15731da177e4SLinus Torvalds 		goto out_nfserr;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	dold = tfhp->fh_dentry;
15761da177e4SLinus Torvalds 
15774795bb37SJ. Bruce Fields 	err = nfserr_noent;
15782b0143b5SDavid Howells 	if (d_really_is_negative(dold))
15794a55c101SJan Kara 		goto out_dput;
1580146a8595SJ. Bruce Fields 	host_err = vfs_link(dold, dirp, dnew, NULL);
15816264d69dSAl Viro 	if (!host_err) {
1582f501912aSBen Myers 		err = nfserrno(commit_metadata(ffhp));
1583f501912aSBen Myers 		if (!err)
1584f501912aSBen Myers 			err = nfserrno(commit_metadata(tfhp));
15851da177e4SLinus Torvalds 	} else {
15866264d69dSAl Viro 		if (host_err == -EXDEV && rqstp->rq_vers == 2)
15871da177e4SLinus Torvalds 			err = nfserr_acces;
15881da177e4SLinus Torvalds 		else
15896264d69dSAl Viro 			err = nfserrno(host_err);
15901da177e4SLinus Torvalds 	}
159175c3f29dSDave Hansen out_dput:
15921da177e4SLinus Torvalds 	dput(dnew);
1593270d56e5SDavid M. Richter out_unlock:
1594270d56e5SDavid M. Richter 	fh_unlock(ffhp);
15954a55c101SJan Kara 	fh_drop_write(tfhp);
15961da177e4SLinus Torvalds out:
15971da177e4SLinus Torvalds 	return err;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds out_nfserr:
16006264d69dSAl Viro 	err = nfserrno(host_err);
1601270d56e5SDavid M. Richter 	goto out_unlock;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
16047775ec57SJeff Layton static void
16057775ec57SJeff Layton nfsd_close_cached_files(struct dentry *dentry)
16067775ec57SJeff Layton {
16077775ec57SJeff Layton 	struct inode *inode = d_inode(dentry);
16087775ec57SJeff Layton 
16097775ec57SJeff Layton 	if (inode && S_ISREG(inode->i_mode))
16107775ec57SJeff Layton 		nfsd_file_close_inode_sync(inode);
16117775ec57SJeff Layton }
16127775ec57SJeff Layton 
16137775ec57SJeff Layton static bool
16147775ec57SJeff Layton nfsd_has_cached_files(struct dentry *dentry)
16157775ec57SJeff Layton {
16167775ec57SJeff Layton 	bool		ret = false;
16177775ec57SJeff Layton 	struct inode *inode = d_inode(dentry);
16187775ec57SJeff Layton 
16197775ec57SJeff Layton 	if (inode && S_ISREG(inode->i_mode))
16207775ec57SJeff Layton 		ret = nfsd_file_is_cached(inode);
16217775ec57SJeff Layton 	return ret;
16227775ec57SJeff Layton }
16237775ec57SJeff Layton 
16241da177e4SLinus Torvalds /*
16251da177e4SLinus Torvalds  * Rename a file
16261da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
16271da177e4SLinus Torvalds  */
16286264d69dSAl Viro __be32
16291da177e4SLinus Torvalds nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
16301da177e4SLinus Torvalds 			    struct svc_fh *tfhp, char *tname, int tlen)
16311da177e4SLinus Torvalds {
16321da177e4SLinus Torvalds 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
16331da177e4SLinus Torvalds 	struct inode	*fdir, *tdir;
16346264d69dSAl Viro 	__be32		err;
16356264d69dSAl Viro 	int		host_err;
16367775ec57SJeff Layton 	bool		has_cached = false;
16371da177e4SLinus Torvalds 
16388837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
16391da177e4SLinus Torvalds 	if (err)
16401da177e4SLinus Torvalds 		goto out;
16418837abcaSMiklos Szeredi 	err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
16421da177e4SLinus Torvalds 	if (err)
16431da177e4SLinus Torvalds 		goto out;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	fdentry = ffhp->fh_dentry;
16462b0143b5SDavid Howells 	fdir = d_inode(fdentry);
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	tdentry = tfhp->fh_dentry;
16492b0143b5SDavid Howells 	tdir = d_inode(tdentry);
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	err = nfserr_perm;
16521da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
16531da177e4SLinus Torvalds 		goto out;
16541da177e4SLinus Torvalds 
16557775ec57SJeff Layton retry:
16564a55c101SJan Kara 	host_err = fh_want_write(ffhp);
16574a55c101SJan Kara 	if (host_err) {
16584a55c101SJan Kara 		err = nfserrno(host_err);
16594a55c101SJan Kara 		goto out;
16604a55c101SJan Kara 	}
16614a55c101SJan Kara 
16621da177e4SLinus Torvalds 	/* cannot use fh_lock as we need deadlock protective ordering
16631da177e4SLinus Torvalds 	 * so do it by hand */
16641da177e4SLinus Torvalds 	trap = lock_rename(tdentry, fdentry);
1665aaf91ec1SJeff Layton 	ffhp->fh_locked = tfhp->fh_locked = true;
16661da177e4SLinus Torvalds 	fill_pre_wcc(ffhp);
16671da177e4SLinus Torvalds 	fill_pre_wcc(tfhp);
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	odentry = lookup_one_len(fname, fdentry, flen);
16706264d69dSAl Viro 	host_err = PTR_ERR(odentry);
16711da177e4SLinus Torvalds 	if (IS_ERR(odentry))
16721da177e4SLinus Torvalds 		goto out_nfserr;
16731da177e4SLinus Torvalds 
16746264d69dSAl Viro 	host_err = -ENOENT;
16752b0143b5SDavid Howells 	if (d_really_is_negative(odentry))
16761da177e4SLinus Torvalds 		goto out_dput_old;
16776264d69dSAl Viro 	host_err = -EINVAL;
16781da177e4SLinus Torvalds 	if (odentry == trap)
16791da177e4SLinus Torvalds 		goto out_dput_old;
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	ndentry = lookup_one_len(tname, tdentry, tlen);
16826264d69dSAl Viro 	host_err = PTR_ERR(ndentry);
16831da177e4SLinus Torvalds 	if (IS_ERR(ndentry))
16841da177e4SLinus Torvalds 		goto out_dput_old;
16856264d69dSAl Viro 	host_err = -ENOTEMPTY;
16861da177e4SLinus Torvalds 	if (ndentry == trap)
16871da177e4SLinus Torvalds 		goto out_dput_new;
16881da177e4SLinus Torvalds 
16899079b1ebSDave Hansen 	host_err = -EXDEV;
16909079b1ebSDave Hansen 	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
16919079b1ebSDave Hansen 		goto out_dput_new;
1692aa387d6cSJ. Bruce Fields 	if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1693aa387d6cSJ. Bruce Fields 		goto out_dput_new;
16949079b1ebSDave Hansen 
16957775ec57SJeff Layton 	if (nfsd_has_cached_files(ndentry)) {
16967775ec57SJeff Layton 		has_cached = true;
16977775ec57SJeff Layton 		goto out_dput_old;
16987775ec57SJeff Layton 	} else {
1699520c8b16SMiklos Szeredi 		host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
1700f501912aSBen Myers 		if (!host_err) {
1701f501912aSBen Myers 			host_err = commit_metadata(tfhp);
17026264d69dSAl Viro 			if (!host_err)
1703f501912aSBen Myers 				host_err = commit_metadata(ffhp);
17041da177e4SLinus Torvalds 		}
17057775ec57SJeff Layton 	}
17061da177e4SLinus Torvalds  out_dput_new:
17071da177e4SLinus Torvalds 	dput(ndentry);
17081da177e4SLinus Torvalds  out_dput_old:
17091da177e4SLinus Torvalds 	dput(odentry);
17101da177e4SLinus Torvalds  out_nfserr:
17116264d69dSAl Viro 	err = nfserrno(host_err);
1712fbb74a34SJ. Bruce Fields 	/*
1713fbb74a34SJ. Bruce Fields 	 * We cannot rely on fh_unlock on the two filehandles,
17141da177e4SLinus Torvalds 	 * as that would do the wrong thing if the two directories
1715fbb74a34SJ. Bruce Fields 	 * were the same, so again we do it by hand.
17161da177e4SLinus Torvalds 	 */
17177775ec57SJeff Layton 	if (!has_cached) {
17181da177e4SLinus Torvalds 		fill_post_wcc(ffhp);
17191da177e4SLinus Torvalds 		fill_post_wcc(tfhp);
17207775ec57SJeff Layton 	}
17211da177e4SLinus Torvalds 	unlock_rename(tdentry, fdentry);
1722aaf91ec1SJeff Layton 	ffhp->fh_locked = tfhp->fh_locked = false;
17234a55c101SJan Kara 	fh_drop_write(ffhp);
17241da177e4SLinus Torvalds 
17257775ec57SJeff Layton 	/*
17267775ec57SJeff Layton 	 * If the target dentry has cached open files, then we need to try to
17277775ec57SJeff Layton 	 * close them prior to doing the rename. Flushing delayed fput
17287775ec57SJeff Layton 	 * shouldn't be done with locks held however, so we delay it until this
17297775ec57SJeff Layton 	 * point and then reattempt the whole shebang.
17307775ec57SJeff Layton 	 */
17317775ec57SJeff Layton 	if (has_cached) {
17327775ec57SJeff Layton 		has_cached = false;
17337775ec57SJeff Layton 		nfsd_close_cached_files(ndentry);
17347775ec57SJeff Layton 		dput(ndentry);
17357775ec57SJeff Layton 		goto retry;
17367775ec57SJeff Layton 	}
17371da177e4SLinus Torvalds out:
17381da177e4SLinus Torvalds 	return err;
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds /*
17421da177e4SLinus Torvalds  * Unlink a file or directory
17431da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
17441da177e4SLinus Torvalds  */
17456264d69dSAl Viro __be32
17461da177e4SLinus Torvalds nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
17471da177e4SLinus Torvalds 				char *fname, int flen)
17481da177e4SLinus Torvalds {
17491da177e4SLinus Torvalds 	struct dentry	*dentry, *rdentry;
17501da177e4SLinus Torvalds 	struct inode	*dirp;
17516264d69dSAl Viro 	__be32		err;
17526264d69dSAl Viro 	int		host_err;
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	err = nfserr_acces;
17551da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen))
17561da177e4SLinus Torvalds 		goto out;
17578837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
17581da177e4SLinus Torvalds 	if (err)
17591da177e4SLinus Torvalds 		goto out;
17601da177e4SLinus Torvalds 
17614a55c101SJan Kara 	host_err = fh_want_write(fhp);
17624a55c101SJan Kara 	if (host_err)
17634a55c101SJan Kara 		goto out_nfserr;
17644a55c101SJan Kara 
176512fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
17661da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
17672b0143b5SDavid Howells 	dirp = d_inode(dentry);
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	rdentry = lookup_one_len(fname, dentry, flen);
17706264d69dSAl Viro 	host_err = PTR_ERR(rdentry);
17711da177e4SLinus Torvalds 	if (IS_ERR(rdentry))
17720ca0c9d7SJ. Bruce Fields 		goto out_drop_write;
17731da177e4SLinus Torvalds 
17742b0143b5SDavid Howells 	if (d_really_is_negative(rdentry)) {
17751da177e4SLinus Torvalds 		dput(rdentry);
17760ca0c9d7SJ. Bruce Fields 		host_err = -ENOENT;
17770ca0c9d7SJ. Bruce Fields 		goto out_drop_write;
17781da177e4SLinus Torvalds 	}
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	if (!type)
17812b0143b5SDavid Howells 		type = d_inode(rdentry)->i_mode & S_IFMT;
17821da177e4SLinus Torvalds 
17837775ec57SJeff Layton 	if (type != S_IFDIR) {
17847775ec57SJeff Layton 		nfsd_close_cached_files(rdentry);
1785b21996e3SJ. Bruce Fields 		host_err = vfs_unlink(dirp, rdentry, NULL);
17867775ec57SJeff Layton 	} else {
17876264d69dSAl Viro 		host_err = vfs_rmdir(dirp, rdentry);
17887775ec57SJeff Layton 	}
17897775ec57SJeff Layton 
1790541ce98cSJ. Bruce Fields 	if (!host_err)
1791541ce98cSJ. Bruce Fields 		host_err = commit_metadata(fhp);
17921da177e4SLinus Torvalds 	dput(rdentry);
17931da177e4SLinus Torvalds 
17940ca0c9d7SJ. Bruce Fields out_drop_write:
17950ca0c9d7SJ. Bruce Fields 	fh_drop_write(fhp);
17961da177e4SLinus Torvalds out_nfserr:
17976264d69dSAl Viro 	err = nfserrno(host_err);
1798f193fbabSYAMAMOTO Takashi out:
1799f193fbabSYAMAMOTO Takashi 	return err;
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds /*
180314f7dd63SDavid Woodhouse  * We do this buffering because we must not call back into the file
180414f7dd63SDavid Woodhouse  * system's ->lookup() method from the filldir callback. That may well
180514f7dd63SDavid Woodhouse  * deadlock a number of file systems.
180614f7dd63SDavid Woodhouse  *
180714f7dd63SDavid Woodhouse  * This is based heavily on the implementation of same in XFS.
180814f7dd63SDavid Woodhouse  */
180914f7dd63SDavid Woodhouse struct buffered_dirent {
181014f7dd63SDavid Woodhouse 	u64		ino;
181114f7dd63SDavid Woodhouse 	loff_t		offset;
181214f7dd63SDavid Woodhouse 	int		namlen;
181314f7dd63SDavid Woodhouse 	unsigned int	d_type;
181414f7dd63SDavid Woodhouse 	char		name[];
181514f7dd63SDavid Woodhouse };
181614f7dd63SDavid Woodhouse 
181714f7dd63SDavid Woodhouse struct readdir_data {
18185c0ba4e0SAl Viro 	struct dir_context ctx;
181914f7dd63SDavid Woodhouse 	char		*dirent;
182014f7dd63SDavid Woodhouse 	size_t		used;
182153c9c5c0SAl Viro 	int		full;
182214f7dd63SDavid Woodhouse };
182314f7dd63SDavid Woodhouse 
1824ac7576f4SMiklos Szeredi static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1825ac7576f4SMiklos Szeredi 				 int namlen, loff_t offset, u64 ino,
1826ac7576f4SMiklos Szeredi 				 unsigned int d_type)
182714f7dd63SDavid Woodhouse {
1828ac7576f4SMiklos Szeredi 	struct readdir_data *buf =
1829ac7576f4SMiklos Szeredi 		container_of(ctx, struct readdir_data, ctx);
183014f7dd63SDavid Woodhouse 	struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
183114f7dd63SDavid Woodhouse 	unsigned int reclen;
183214f7dd63SDavid Woodhouse 
183314f7dd63SDavid Woodhouse 	reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
183453c9c5c0SAl Viro 	if (buf->used + reclen > PAGE_SIZE) {
183553c9c5c0SAl Viro 		buf->full = 1;
183614f7dd63SDavid Woodhouse 		return -EINVAL;
183753c9c5c0SAl Viro 	}
183814f7dd63SDavid Woodhouse 
183914f7dd63SDavid Woodhouse 	de->namlen = namlen;
184014f7dd63SDavid Woodhouse 	de->offset = offset;
184114f7dd63SDavid Woodhouse 	de->ino = ino;
184214f7dd63SDavid Woodhouse 	de->d_type = d_type;
184314f7dd63SDavid Woodhouse 	memcpy(de->name, name, namlen);
184414f7dd63SDavid Woodhouse 	buf->used += reclen;
184514f7dd63SDavid Woodhouse 
184614f7dd63SDavid Woodhouse 	return 0;
184714f7dd63SDavid Woodhouse }
184814f7dd63SDavid Woodhouse 
1849ac7576f4SMiklos Szeredi static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
18502628b766SDavid Woodhouse 				    struct readdir_cd *cdp, loff_t *offsetp)
18512628b766SDavid Woodhouse {
185214f7dd63SDavid Woodhouse 	struct buffered_dirent *de;
18532628b766SDavid Woodhouse 	int host_err;
185414f7dd63SDavid Woodhouse 	int size;
185514f7dd63SDavid Woodhouse 	loff_t offset;
1856ac6614b7SAl Viro 	struct readdir_data buf = {
1857ac6614b7SAl Viro 		.ctx.actor = nfsd_buffered_filldir,
1858ac6614b7SAl Viro 		.dirent = (void *)__get_free_page(GFP_KERNEL)
1859ac6614b7SAl Viro 	};
18602628b766SDavid Woodhouse 
186114f7dd63SDavid Woodhouse 	if (!buf.dirent)
18622f9092e1SDavid Woodhouse 		return nfserrno(-ENOMEM);
186314f7dd63SDavid Woodhouse 
186414f7dd63SDavid Woodhouse 	offset = *offsetp;
18652628b766SDavid Woodhouse 
186614f7dd63SDavid Woodhouse 	while (1) {
186714f7dd63SDavid Woodhouse 		unsigned int reclen;
186814f7dd63SDavid Woodhouse 
1869b726e923SDoug Nazar 		cdp->err = nfserr_eof; /* will be cleared on successful read */
187014f7dd63SDavid Woodhouse 		buf.used = 0;
187153c9c5c0SAl Viro 		buf.full = 0;
187214f7dd63SDavid Woodhouse 
18735c0ba4e0SAl Viro 		host_err = iterate_dir(file, &buf.ctx);
187453c9c5c0SAl Viro 		if (buf.full)
187553c9c5c0SAl Viro 			host_err = 0;
187653c9c5c0SAl Viro 
187753c9c5c0SAl Viro 		if (host_err < 0)
187814f7dd63SDavid Woodhouse 			break;
187914f7dd63SDavid Woodhouse 
188014f7dd63SDavid Woodhouse 		size = buf.used;
188114f7dd63SDavid Woodhouse 
188214f7dd63SDavid Woodhouse 		if (!size)
188314f7dd63SDavid Woodhouse 			break;
188414f7dd63SDavid Woodhouse 
188514f7dd63SDavid Woodhouse 		de = (struct buffered_dirent *)buf.dirent;
188614f7dd63SDavid Woodhouse 		while (size > 0) {
188714f7dd63SDavid Woodhouse 			offset = de->offset;
188814f7dd63SDavid Woodhouse 
188914f7dd63SDavid Woodhouse 			if (func(cdp, de->name, de->namlen, de->offset,
189014f7dd63SDavid Woodhouse 				 de->ino, de->d_type))
18912f9092e1SDavid Woodhouse 				break;
189214f7dd63SDavid Woodhouse 
189314f7dd63SDavid Woodhouse 			if (cdp->err != nfs_ok)
18942f9092e1SDavid Woodhouse 				break;
189514f7dd63SDavid Woodhouse 
189614f7dd63SDavid Woodhouse 			reclen = ALIGN(sizeof(*de) + de->namlen,
189714f7dd63SDavid Woodhouse 				       sizeof(u64));
189814f7dd63SDavid Woodhouse 			size -= reclen;
189914f7dd63SDavid Woodhouse 			de = (struct buffered_dirent *)((char *)de + reclen);
190014f7dd63SDavid Woodhouse 		}
19012f9092e1SDavid Woodhouse 		if (size > 0) /* We bailed out early */
19022f9092e1SDavid Woodhouse 			break;
19032f9092e1SDavid Woodhouse 
1904c002a6c7SDavid Woodhouse 		offset = vfs_llseek(file, 0, SEEK_CUR);
190514f7dd63SDavid Woodhouse 	}
190614f7dd63SDavid Woodhouse 
190714f7dd63SDavid Woodhouse 	free_page((unsigned long)(buf.dirent));
19082628b766SDavid Woodhouse 
19092628b766SDavid Woodhouse 	if (host_err)
19102628b766SDavid Woodhouse 		return nfserrno(host_err);
191114f7dd63SDavid Woodhouse 
191214f7dd63SDavid Woodhouse 	*offsetp = offset;
19132628b766SDavid Woodhouse 	return cdp->err;
19142628b766SDavid Woodhouse }
19152628b766SDavid Woodhouse 
19161da177e4SLinus Torvalds /*
19171da177e4SLinus Torvalds  * Read entries from a directory.
19181da177e4SLinus Torvalds  * The  NFSv3/4 verifier we ignore for now.
19191da177e4SLinus Torvalds  */
19206264d69dSAl Viro __be32
19211da177e4SLinus Torvalds nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1922ac7576f4SMiklos Szeredi 	     struct readdir_cd *cdp, nfsd_filldir_t func)
19231da177e4SLinus Torvalds {
19246264d69dSAl Viro 	__be32		err;
19251da177e4SLinus Torvalds 	struct file	*file;
19261da177e4SLinus Torvalds 	loff_t		offset = *offsetp;
192706effdbbSBernd Schubert 	int             may_flags = NFSD_MAY_READ;
19281da177e4SLinus Torvalds 
192906effdbbSBernd Schubert 	/* NFSv2 only supports 32 bit cookies */
193006effdbbSBernd Schubert 	if (rqstp->rq_vers > 2)
193106effdbbSBernd Schubert 		may_flags |= NFSD_MAY_64BIT_COOKIE;
193206effdbbSBernd Schubert 
193306effdbbSBernd Schubert 	err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
19341da177e4SLinus Torvalds 	if (err)
19351da177e4SLinus Torvalds 		goto out;
19361da177e4SLinus Torvalds 
1937b108fe6bSJeff Layton 	offset = vfs_llseek(file, offset, SEEK_SET);
19381da177e4SLinus Torvalds 	if (offset < 0) {
19391da177e4SLinus Torvalds 		err = nfserrno((int)offset);
19401da177e4SLinus Torvalds 		goto out_close;
19411da177e4SLinus Torvalds 	}
19421da177e4SLinus Torvalds 
194314f7dd63SDavid Woodhouse 	err = nfsd_buffered_readdir(file, func, cdp, offsetp);
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds 	if (err == nfserr_eof || err == nfserr_toosmall)
19461da177e4SLinus Torvalds 		err = nfs_ok; /* can still be found in ->err */
19471da177e4SLinus Torvalds out_close:
1948fd891454SChristoph Hellwig 	fput(file);
19491da177e4SLinus Torvalds out:
19501da177e4SLinus Torvalds 	return err;
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds /*
19541da177e4SLinus Torvalds  * Get file system stats
19551da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
19561da177e4SLinus Torvalds  */
19576264d69dSAl Viro __be32
195804716e66SJ. Bruce Fields nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
19591da177e4SLinus Torvalds {
1960f6360efbSTakashi Iwai 	__be32 err;
1961f6360efbSTakashi Iwai 
1962f6360efbSTakashi Iwai 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
1963f6360efbSTakashi Iwai 	if (!err) {
1964ebabe9a9SChristoph Hellwig 		struct path path = {
1965ebabe9a9SChristoph Hellwig 			.mnt	= fhp->fh_export->ex_path.mnt,
1966ebabe9a9SChristoph Hellwig 			.dentry	= fhp->fh_dentry,
1967ebabe9a9SChristoph Hellwig 		};
1968f6360efbSTakashi Iwai 		if (vfs_statfs(&path, stat))
19691da177e4SLinus Torvalds 			err = nfserr_io;
1970f6360efbSTakashi Iwai 	}
19711da177e4SLinus Torvalds 	return err;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
1974c7d51402SJ. Bruce Fields static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
1975e22841c6SJ. Bruce Fields {
1976c7d51402SJ. Bruce Fields 	return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
1977e22841c6SJ. Bruce Fields }
1978e22841c6SJ. Bruce Fields 
19791da177e4SLinus Torvalds /*
19801da177e4SLinus Torvalds  * Check for a user's access permissions to this inode.
19811da177e4SLinus Torvalds  */
19826264d69dSAl Viro __be32
19830ec757dfSJ. Bruce Fields nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
19840ec757dfSJ. Bruce Fields 					struct dentry *dentry, int acc)
19851da177e4SLinus Torvalds {
19862b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
19871da177e4SLinus Torvalds 	int		err;
19881da177e4SLinus Torvalds 
1989aea93397SJ. Bruce Fields 	if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
19901da177e4SLinus Torvalds 		return 0;
19911da177e4SLinus Torvalds #if 0
19921da177e4SLinus Torvalds 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
19931da177e4SLinus Torvalds 		acc,
19948837abcaSMiklos Szeredi 		(acc & NFSD_MAY_READ)?	" read"  : "",
19958837abcaSMiklos Szeredi 		(acc & NFSD_MAY_WRITE)?	" write" : "",
19968837abcaSMiklos Szeredi 		(acc & NFSD_MAY_EXEC)?	" exec"  : "",
19978837abcaSMiklos Szeredi 		(acc & NFSD_MAY_SATTR)?	" sattr" : "",
19988837abcaSMiklos Szeredi 		(acc & NFSD_MAY_TRUNC)?	" trunc" : "",
19998837abcaSMiklos Szeredi 		(acc & NFSD_MAY_LOCK)?	" lock"  : "",
20008837abcaSMiklos Szeredi 		(acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
20011da177e4SLinus Torvalds 		inode->i_mode,
20021da177e4SLinus Torvalds 		IS_IMMUTABLE(inode)?	" immut" : "",
20031da177e4SLinus Torvalds 		IS_APPEND(inode)?	" append" : "",
20042c463e95SDave Hansen 		__mnt_is_readonly(exp->ex_path.mnt)?	" ro" : "");
20051da177e4SLinus Torvalds 	dprintk("      owner %d/%d user %d/%d\n",
20065cc0a840SDavid Howells 		inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
20071da177e4SLinus Torvalds #endif
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds 	/* Normally we reject any write/sattr etc access on a read-only file
20101da177e4SLinus Torvalds 	 * system.  But if it is IRIX doing check on write-access for a
20111da177e4SLinus Torvalds 	 * device special file, we ignore rofs.
20121da177e4SLinus Torvalds 	 */
20138837abcaSMiklos Szeredi 	if (!(acc & NFSD_MAY_LOCAL_ACCESS))
20148837abcaSMiklos Szeredi 		if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
20152c463e95SDave Hansen 			if (exp_rdonly(rqstp, exp) ||
20162c463e95SDave Hansen 			    __mnt_is_readonly(exp->ex_path.mnt))
20171da177e4SLinus Torvalds 				return nfserr_rofs;
20188837abcaSMiklos Szeredi 			if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
20191da177e4SLinus Torvalds 				return nfserr_perm;
20201da177e4SLinus Torvalds 		}
20218837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
20221da177e4SLinus Torvalds 		return nfserr_perm;
20231da177e4SLinus Torvalds 
20248837abcaSMiklos Szeredi 	if (acc & NFSD_MAY_LOCK) {
20251da177e4SLinus Torvalds 		/* If we cannot rely on authentication in NLM requests,
20261da177e4SLinus Torvalds 		 * just allow locks, otherwise require read permission, or
20271da177e4SLinus Torvalds 		 * ownership
20281da177e4SLinus Torvalds 		 */
20291da177e4SLinus Torvalds 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
20301da177e4SLinus Torvalds 			return 0;
20311da177e4SLinus Torvalds 		else
20328837abcaSMiklos Szeredi 			acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
20331da177e4SLinus Torvalds 	}
20341da177e4SLinus Torvalds 	/*
20351da177e4SLinus Torvalds 	 * The file owner always gets access permission for accesses that
20361da177e4SLinus Torvalds 	 * would normally be checked at open time. This is to make
20371da177e4SLinus Torvalds 	 * file access work even when the client has done a fchmod(fd, 0).
20381da177e4SLinus Torvalds 	 *
20391da177e4SLinus Torvalds 	 * However, `cp foo bar' should fail nevertheless when bar is
20401da177e4SLinus Torvalds 	 * readonly. A sensible way to do this might be to reject all
20411da177e4SLinus Torvalds 	 * attempts to truncate a read-only file, because a creat() call
20421da177e4SLinus Torvalds 	 * always implies file truncation.
20431da177e4SLinus Torvalds 	 * ... but this isn't really fair.  A process may reasonably call
20441da177e4SLinus Torvalds 	 * ftruncate on an open file descriptor on a file with perm 000.
20451da177e4SLinus Torvalds 	 * We must trust the client to do permission checking - using "ACCESS"
20461da177e4SLinus Torvalds 	 * with NFSv3.
20471da177e4SLinus Torvalds 	 */
20488837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
20496fab8779SEric W. Biederman 	    uid_eq(inode->i_uid, current_fsuid()))
20501da177e4SLinus Torvalds 		return 0;
20511da177e4SLinus Torvalds 
20528837abcaSMiklos Szeredi 	/* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2053f419a2e3SAl Viro 	err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	/* Allow read access to binaries even when mode 111 */
20561da177e4SLinus Torvalds 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
2057a043226bSJ. Bruce Fields 	     (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2058a043226bSJ. Bruce Fields 	      acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2059f419a2e3SAl Viro 		err = inode_permission(inode, MAY_EXEC);
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	return err? nfserrno(err) : 0;
20621da177e4SLinus Torvalds }
2063