xref: /openbmc/linux/fs/nfsd/vfs.c (revision a37b0715)
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
28357f64034STrond Myklebust commit_inode_metadata(struct inode *inode)
284f501912aSBen Myers {
285f501912aSBen Myers 	const struct export_operations *export_ops = inode->i_sb->s_export_op;
286f501912aSBen Myers 
287c3765016SChristoph Hellwig 	if (export_ops->commit_metadata)
288c3765016SChristoph Hellwig 		return export_ops->commit_metadata(inode);
289c3765016SChristoph Hellwig 	return sync_inode_metadata(inode, 1);
290f501912aSBen Myers }
2916c0a654dSJ. Bruce Fields 
29257f64034STrond Myklebust static int
29357f64034STrond Myklebust commit_metadata(struct svc_fh *fhp)
29457f64034STrond Myklebust {
29557f64034STrond Myklebust 	struct inode *inode = d_inode(fhp->fh_dentry);
29657f64034STrond Myklebust 
29757f64034STrond Myklebust 	if (!EX_ISSYNC(fhp->fh_export))
29857f64034STrond Myklebust 		return 0;
29957f64034STrond Myklebust 	return commit_inode_metadata(inode);
30057f64034STrond Myklebust }
30157f64034STrond Myklebust 
3021da177e4SLinus Torvalds /*
303818e5a22SChristoph Hellwig  * Go over the attributes and take care of the small differences between
304818e5a22SChristoph Hellwig  * NFS semantics and what Linux expects.
305818e5a22SChristoph Hellwig  */
306818e5a22SChristoph Hellwig static void
307818e5a22SChristoph Hellwig nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
308818e5a22SChristoph Hellwig {
309818e5a22SChristoph Hellwig 	/* sanitize the mode change */
310818e5a22SChristoph Hellwig 	if (iap->ia_valid & ATTR_MODE) {
311818e5a22SChristoph Hellwig 		iap->ia_mode &= S_IALLUGO;
312818e5a22SChristoph Hellwig 		iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
313818e5a22SChristoph Hellwig 	}
314818e5a22SChristoph Hellwig 
315818e5a22SChristoph Hellwig 	/* Revoke setuid/setgid on chown */
316818e5a22SChristoph Hellwig 	if (!S_ISDIR(inode->i_mode) &&
317c4fa6d7cSStanislav Kholmanskikh 	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
318818e5a22SChristoph Hellwig 		iap->ia_valid |= ATTR_KILL_PRIV;
319818e5a22SChristoph Hellwig 		if (iap->ia_valid & ATTR_MODE) {
320818e5a22SChristoph Hellwig 			/* we're setting mode too, just clear the s*id bits */
321818e5a22SChristoph Hellwig 			iap->ia_mode &= ~S_ISUID;
322818e5a22SChristoph Hellwig 			if (iap->ia_mode & S_IXGRP)
323818e5a22SChristoph Hellwig 				iap->ia_mode &= ~S_ISGID;
324818e5a22SChristoph Hellwig 		} else {
325818e5a22SChristoph Hellwig 			/* set ATTR_KILL_* bits and let VFS handle it */
326818e5a22SChristoph Hellwig 			iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
327818e5a22SChristoph Hellwig 		}
328818e5a22SChristoph Hellwig 	}
329818e5a22SChristoph Hellwig }
330818e5a22SChristoph Hellwig 
3310839ffb8SJ. Bruce Fields static __be32
3320839ffb8SJ. Bruce Fields nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
3330839ffb8SJ. Bruce Fields 		struct iattr *iap)
3340839ffb8SJ. Bruce Fields {
3350839ffb8SJ. Bruce Fields 	struct inode *inode = d_inode(fhp->fh_dentry);
3360839ffb8SJ. Bruce Fields 	int host_err;
3370839ffb8SJ. Bruce Fields 
3380839ffb8SJ. Bruce Fields 	if (iap->ia_size < inode->i_size) {
3390839ffb8SJ. Bruce Fields 		__be32 err;
3400839ffb8SJ. Bruce Fields 
3410839ffb8SJ. Bruce Fields 		err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
3420839ffb8SJ. Bruce Fields 				NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
3430839ffb8SJ. Bruce Fields 		if (err)
3440839ffb8SJ. Bruce Fields 			return err;
3450839ffb8SJ. Bruce Fields 	}
3460839ffb8SJ. Bruce Fields 
3470839ffb8SJ. Bruce Fields 	host_err = get_write_access(inode);
3480839ffb8SJ. Bruce Fields 	if (host_err)
3490839ffb8SJ. Bruce Fields 		goto out_nfserrno;
3500839ffb8SJ. Bruce Fields 
3510839ffb8SJ. Bruce Fields 	host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
3520839ffb8SJ. Bruce Fields 	if (host_err)
3530839ffb8SJ. Bruce Fields 		goto out_put_write_access;
3540839ffb8SJ. Bruce Fields 	return 0;
3550839ffb8SJ. Bruce Fields 
3560839ffb8SJ. Bruce Fields out_put_write_access:
3570839ffb8SJ. Bruce Fields 	put_write_access(inode);
3580839ffb8SJ. Bruce Fields out_nfserrno:
3590839ffb8SJ. Bruce Fields 	return nfserrno(host_err);
3600839ffb8SJ. Bruce Fields }
3610839ffb8SJ. Bruce Fields 
362818e5a22SChristoph Hellwig /*
363818e5a22SChristoph Hellwig  * Set various file attributes.  After this call fhp needs an fh_put.
3641da177e4SLinus Torvalds  */
3656264d69dSAl Viro __be32
3661da177e4SLinus Torvalds nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
3672a1aa489SArnd Bergmann 	     int check_guard, time64_t guardtime)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct dentry	*dentry;
3701da177e4SLinus Torvalds 	struct inode	*inode;
3718837abcaSMiklos Szeredi 	int		accmode = NFSD_MAY_SATTR;
372175a4eb7SAl Viro 	umode_t		ftype = 0;
3736264d69dSAl Viro 	__be32		err;
3746264d69dSAl Viro 	int		host_err;
3759f67f189SJ. Bruce Fields 	bool		get_write_count;
376758e99feSChristoph Hellwig 	bool		size_change = (iap->ia_valid & ATTR_SIZE);
3771da177e4SLinus Torvalds 
378255fbca6Szhengbin 	if (iap->ia_valid & ATTR_SIZE) {
3798837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
3801da177e4SLinus Torvalds 		ftype = S_IFREG;
381255fbca6Szhengbin 	}
382255fbca6Szhengbin 
383255fbca6Szhengbin 	/*
384255fbca6Szhengbin 	 * If utimes(2) and friends are called with times not NULL, we should
385255fbca6Szhengbin 	 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
386e977cc83SGeert Uytterhoeven 	 * will return EACCES, when the caller's effective UID does not match
387255fbca6Szhengbin 	 * the owner of the file, and the caller is not privileged. In this
388255fbca6Szhengbin 	 * situation, we should return EPERM(notify_change will return this).
389255fbca6Szhengbin 	 */
390255fbca6Szhengbin 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
391255fbca6Szhengbin 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
392255fbca6Szhengbin 		if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
393255fbca6Szhengbin 			accmode |= NFSD_MAY_WRITE;
394255fbca6Szhengbin 	}
3951da177e4SLinus Torvalds 
3969f67f189SJ. Bruce Fields 	/* Callers that do fh_verify should do the fh_want_write: */
3979f67f189SJ. Bruce Fields 	get_write_count = !fhp->fh_dentry;
3989f67f189SJ. Bruce Fields 
3991da177e4SLinus Torvalds 	/* Get inode */
4001da177e4SLinus Torvalds 	err = fh_verify(rqstp, fhp, ftype, accmode);
40115b7a1b8SNeilBrown 	if (err)
402758e99feSChristoph Hellwig 		return err;
4039f67f189SJ. Bruce Fields 	if (get_write_count) {
4049f67f189SJ. Bruce Fields 		host_err = fh_want_write(fhp);
4059f67f189SJ. Bruce Fields 		if (host_err)
406758e99feSChristoph Hellwig 			goto out;
4079f67f189SJ. Bruce Fields 	}
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
4102b0143b5SDavid Howells 	inode = d_inode(dentry);
4111da177e4SLinus Torvalds 
41215b7a1b8SNeilBrown 	/* Ignore any mode updates on symlinks */
41315b7a1b8SNeilBrown 	if (S_ISLNK(inode->i_mode))
41415b7a1b8SNeilBrown 		iap->ia_valid &= ~ATTR_MODE;
41515b7a1b8SNeilBrown 
41615b7a1b8SNeilBrown 	if (!iap->ia_valid)
417758e99feSChristoph Hellwig 		return 0;
41815b7a1b8SNeilBrown 
419818e5a22SChristoph Hellwig 	nfsd_sanitize_attrs(inode, iap);
4201da177e4SLinus Torvalds 
421758e99feSChristoph Hellwig 	if (check_guard && guardtime != inode->i_ctime.tv_sec)
422758e99feSChristoph Hellwig 		return nfserr_notsync;
423758e99feSChristoph Hellwig 
424f0c63124SChristoph Hellwig 	/*
42541f53350SChristoph Hellwig 	 * The size case is special, it changes the file in addition to the
426783112f7SChristoph Hellwig 	 * attributes, and file systems don't expect it to be mixed with
427783112f7SChristoph Hellwig 	 * "random" attribute changes.  We thus split out the size change
428783112f7SChristoph Hellwig 	 * into a separate call to ->setattr, and do the rest as a separate
429783112f7SChristoph Hellwig 	 * setattr call.
430f0c63124SChristoph Hellwig 	 */
431758e99feSChristoph Hellwig 	if (size_change) {
4320839ffb8SJ. Bruce Fields 		err = nfsd_get_write_access(rqstp, fhp, iap);
4330839ffb8SJ. Bruce Fields 		if (err)
434758e99feSChristoph Hellwig 			return err;
435783112f7SChristoph Hellwig 	}
43641f53350SChristoph Hellwig 
437783112f7SChristoph Hellwig 	fh_lock(fhp);
438783112f7SChristoph Hellwig 	if (size_change) {
43941f53350SChristoph Hellwig 		/*
4400839ffb8SJ. Bruce Fields 		 * RFC5661, Section 18.30.4:
4410839ffb8SJ. Bruce Fields 		 *   Changing the size of a file with SETATTR indirectly
4420839ffb8SJ. Bruce Fields 		 *   changes the time_modify and change attributes.
4430839ffb8SJ. Bruce Fields 		 *
4440839ffb8SJ. Bruce Fields 		 * (and similar for the older RFCs)
44541f53350SChristoph Hellwig 		 */
446783112f7SChristoph Hellwig 		struct iattr size_attr = {
447783112f7SChristoph Hellwig 			.ia_valid	= ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
448783112f7SChristoph Hellwig 			.ia_size	= iap->ia_size,
449783112f7SChristoph Hellwig 		};
450783112f7SChristoph Hellwig 
451783112f7SChristoph Hellwig 		host_err = notify_change(dentry, &size_attr, NULL);
452783112f7SChristoph Hellwig 		if (host_err)
453783112f7SChristoph Hellwig 			goto out_unlock;
454783112f7SChristoph Hellwig 		iap->ia_valid &= ~ATTR_SIZE;
455783112f7SChristoph Hellwig 
456783112f7SChristoph Hellwig 		/*
457783112f7SChristoph Hellwig 		 * Avoid the additional setattr call below if the only other
458783112f7SChristoph Hellwig 		 * attribute that the client sends is the mtime, as we update
459783112f7SChristoph Hellwig 		 * it as part of the size change above.
460783112f7SChristoph Hellwig 		 */
461783112f7SChristoph Hellwig 		if ((iap->ia_valid & ~ATTR_MTIME) == 0)
462783112f7SChristoph Hellwig 			goto out_unlock;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	iap->ia_valid |= ATTR_CTIME;
46627ac0ffeSJ. Bruce Fields 	host_err = notify_change(dentry, iap, NULL);
467987da479SChristoph Hellwig 
468783112f7SChristoph Hellwig out_unlock:
469783112f7SChristoph Hellwig 	fh_unlock(fhp);
4700839ffb8SJ. Bruce Fields 	if (size_change)
4710839ffb8SJ. Bruce Fields 		put_write_access(inode);
4720839ffb8SJ. Bruce Fields out:
473758e99feSChristoph Hellwig 	if (!host_err)
474758e99feSChristoph Hellwig 		host_err = commit_metadata(fhp);
475758e99feSChristoph Hellwig 	return nfserrno(host_err);
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
4785be196e5SChristoph Hellwig #if defined(CONFIG_NFSD_V4)
4799b4146e8SChuck Lever /*
4809b4146e8SChuck Lever  * NFS junction information is stored in an extended attribute.
4819b4146e8SChuck Lever  */
4829b4146e8SChuck Lever #define NFSD_JUNCTION_XATTR_NAME	XATTR_TRUSTED_PREFIX "junction.nfs"
4839b4146e8SChuck Lever 
4849b4146e8SChuck Lever /**
4859b4146e8SChuck Lever  * nfsd4_is_junction - Test if an object could be an NFS junction
4869b4146e8SChuck Lever  *
4879b4146e8SChuck Lever  * @dentry: object to test
4889b4146e8SChuck Lever  *
4899b4146e8SChuck Lever  * Returns 1 if "dentry" appears to contain NFS junction information.
4909b4146e8SChuck Lever  * Otherwise 0 is returned.
4919b4146e8SChuck Lever  */
49211fcee02STrond Myklebust int nfsd4_is_junction(struct dentry *dentry)
49311fcee02STrond Myklebust {
4942b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
49511fcee02STrond Myklebust 
49611fcee02STrond Myklebust 	if (inode == NULL)
49711fcee02STrond Myklebust 		return 0;
49811fcee02STrond Myklebust 	if (inode->i_mode & S_IXUGO)
49911fcee02STrond Myklebust 		return 0;
50011fcee02STrond Myklebust 	if (!(inode->i_mode & S_ISVTX))
50111fcee02STrond Myklebust 		return 0;
5029b4146e8SChuck Lever 	if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
50311fcee02STrond Myklebust 		return 0;
50411fcee02STrond Myklebust 	return 1;
50511fcee02STrond Myklebust }
50618032ca0SDavid Quigley #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
50718032ca0SDavid Quigley __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
50818032ca0SDavid Quigley 		struct xdr_netobj *label)
50918032ca0SDavid Quigley {
51018032ca0SDavid Quigley 	__be32 error;
51118032ca0SDavid Quigley 	int host_error;
51218032ca0SDavid Quigley 	struct dentry *dentry;
51318032ca0SDavid Quigley 
51418032ca0SDavid Quigley 	error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
51518032ca0SDavid Quigley 	if (error)
51618032ca0SDavid Quigley 		return error;
51718032ca0SDavid Quigley 
51818032ca0SDavid Quigley 	dentry = fhp->fh_dentry;
51918032ca0SDavid Quigley 
5205955102cSAl Viro 	inode_lock(d_inode(dentry));
52118032ca0SDavid Quigley 	host_error = security_inode_setsecctx(dentry, label->data, label->len);
5225955102cSAl Viro 	inode_unlock(d_inode(dentry));
52318032ca0SDavid Quigley 	return nfserrno(host_error);
52418032ca0SDavid Quigley }
52518032ca0SDavid Quigley #else
52618032ca0SDavid Quigley __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
52718032ca0SDavid Quigley 		struct xdr_netobj *label)
52818032ca0SDavid Quigley {
52918032ca0SDavid Quigley 	return nfserr_notsupp;
53018032ca0SDavid Quigley }
53118032ca0SDavid Quigley #endif
53218032ca0SDavid Quigley 
533b66ae6ddSTrond Myklebust __be32 nfsd4_clone_file_range(struct nfsd_file *nf_src, u64 src_pos,
534b66ae6ddSTrond Myklebust 		struct nfsd_file *nf_dst, u64 dst_pos, u64 count, bool sync)
535ffa0160aSChristoph Hellwig {
536b66ae6ddSTrond Myklebust 	struct file *src = nf_src->nf_file;
537b66ae6ddSTrond Myklebust 	struct file *dst = nf_dst->nf_file;
53842ec3d4cSDarrick J. Wong 	loff_t cloned;
5391b28d756STrond Myklebust 	__be32 ret = 0;
54042ec3d4cSDarrick J. Wong 
5411b28d756STrond Myklebust 	down_write(&nf_dst->nf_rwsem);
542452ce659SDarrick J. Wong 	cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
5431b28d756STrond Myklebust 	if (cloned < 0) {
5441b28d756STrond Myklebust 		ret = nfserrno(cloned);
5451b28d756STrond Myklebust 		goto out_err;
5461b28d756STrond Myklebust 	}
5471b28d756STrond Myklebust 	if (count && cloned != count) {
5481b28d756STrond Myklebust 		ret = nfserrno(-EINVAL);
5491b28d756STrond Myklebust 		goto out_err;
5501b28d756STrond Myklebust 	}
551a25e3726STrond Myklebust 	if (sync) {
552a25e3726STrond Myklebust 		loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
553a25e3726STrond Myklebust 		int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
55457f64034STrond Myklebust 
55557f64034STrond Myklebust 		if (!status)
55657f64034STrond Myklebust 			status = commit_inode_metadata(file_inode(src));
5571b28d756STrond Myklebust 		if (status < 0) {
5581b28d756STrond Myklebust 			nfsd_reset_boot_verifier(net_generic(nf_dst->nf_net,
5591b28d756STrond Myklebust 						 nfsd_net_id));
5601b28d756STrond Myklebust 			ret = nfserrno(status);
561a25e3726STrond Myklebust 		}
5621b28d756STrond Myklebust 	}
5631b28d756STrond Myklebust out_err:
5641b28d756STrond Myklebust 	up_write(&nf_dst->nf_rwsem);
5651b28d756STrond Myklebust 	return ret;
566ffa0160aSChristoph Hellwig }
567ffa0160aSChristoph Hellwig 
56829ae7f9dSAnna Schumaker ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
56929ae7f9dSAnna Schumaker 			     u64 dst_pos, u64 count)
57029ae7f9dSAnna Schumaker {
57129ae7f9dSAnna Schumaker 
57229ae7f9dSAnna Schumaker 	/*
57329ae7f9dSAnna Schumaker 	 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
57429ae7f9dSAnna Schumaker 	 * thread and client rpc slot.  The choice of 4MB is somewhat
57529ae7f9dSAnna Schumaker 	 * arbitrary.  We might instead base this on r/wsize, or make it
57629ae7f9dSAnna Schumaker 	 * tunable, or use a time instead of a byte limit, or implement
57729ae7f9dSAnna Schumaker 	 * asynchronous copy.  In theory a client could also recognize a
57829ae7f9dSAnna Schumaker 	 * limit like this and pipeline multiple COPY requests.
57929ae7f9dSAnna Schumaker 	 */
58029ae7f9dSAnna Schumaker 	count = min_t(u64, count, 1 << 22);
58129ae7f9dSAnna Schumaker 	return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
58229ae7f9dSAnna Schumaker }
58329ae7f9dSAnna Schumaker 
58495d871f0SAnna Schumaker __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
58595d871f0SAnna Schumaker 			   struct file *file, loff_t offset, loff_t len,
58695d871f0SAnna Schumaker 			   int flags)
58795d871f0SAnna Schumaker {
58895d871f0SAnna Schumaker 	int error;
58995d871f0SAnna Schumaker 
59095d871f0SAnna Schumaker 	if (!S_ISREG(file_inode(file)->i_mode))
59195d871f0SAnna Schumaker 		return nfserr_inval;
59295d871f0SAnna Schumaker 
59395d871f0SAnna Schumaker 	error = vfs_fallocate(file, flags, offset, len);
59495d871f0SAnna Schumaker 	if (!error)
59595d871f0SAnna Schumaker 		error = commit_metadata(fhp);
59695d871f0SAnna Schumaker 
59795d871f0SAnna Schumaker 	return nfserrno(error);
59895d871f0SAnna Schumaker }
5996a85d6c7SJ. Bruce Fields #endif /* defined(CONFIG_NFSD_V4) */
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
6021da177e4SLinus Torvalds /*
6031da177e4SLinus Torvalds  * Check server access rights to a file system object
6041da177e4SLinus Torvalds  */
6051da177e4SLinus Torvalds struct accessmap {
6061da177e4SLinus Torvalds 	u32		access;
6071da177e4SLinus Torvalds 	int		how;
6081da177e4SLinus Torvalds };
6091da177e4SLinus Torvalds static struct accessmap	nfs3_regaccess[] = {
6108837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
6118837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
6128837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_TRUNC	},
6138837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE			},
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds     {	0,			0				}
6161da177e4SLinus Torvalds };
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds static struct accessmap	nfs3_diraccess[] = {
6198837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
6208837abcaSMiklos Szeredi     {	NFS3_ACCESS_LOOKUP,	NFSD_MAY_EXEC			},
6218837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
6228837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_EXEC|NFSD_MAY_WRITE	},
6238837abcaSMiklos Szeredi     {	NFS3_ACCESS_DELETE,	NFSD_MAY_REMOVE			},
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds     {	0,			0				}
6261da177e4SLinus Torvalds };
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds static struct accessmap	nfs3_anyaccess[] = {
6291da177e4SLinus Torvalds 	/* Some clients - Solaris 2.6 at least, make an access call
6301da177e4SLinus Torvalds 	 * to the server to check for access for things like /dev/null
6311da177e4SLinus Torvalds 	 * (which really, the server doesn't care about).  So
6321da177e4SLinus Torvalds 	 * We provide simple access checking for them, looking
6331da177e4SLinus Torvalds 	 * mainly at mode bits, and we make sure to ignore read-only
6341da177e4SLinus Torvalds 	 * filesystem checks
6351da177e4SLinus Torvalds 	 */
6368837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
6378837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
6388837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
6398837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds     {	0,			0				}
6421da177e4SLinus Torvalds };
6431da177e4SLinus Torvalds 
6446264d69dSAl Viro __be32
6451da177e4SLinus Torvalds nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 	struct accessmap	*map;
6481da177e4SLinus Torvalds 	struct svc_export	*export;
6491da177e4SLinus Torvalds 	struct dentry		*dentry;
6501da177e4SLinus Torvalds 	u32			query, result = 0, sresult = 0;
6516264d69dSAl Viro 	__be32			error;
6521da177e4SLinus Torvalds 
6538837abcaSMiklos Szeredi 	error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
6541da177e4SLinus Torvalds 	if (error)
6551da177e4SLinus Torvalds 		goto out;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	export = fhp->fh_export;
6581da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
6591da177e4SLinus Torvalds 
660e36cb0b8SDavid Howells 	if (d_is_reg(dentry))
6611da177e4SLinus Torvalds 		map = nfs3_regaccess;
662e36cb0b8SDavid Howells 	else if (d_is_dir(dentry))
6631da177e4SLinus Torvalds 		map = nfs3_diraccess;
6641da177e4SLinus Torvalds 	else
6651da177e4SLinus Torvalds 		map = nfs3_anyaccess;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	query = *access;
6691da177e4SLinus Torvalds 	for  (; map->access; map++) {
6701da177e4SLinus Torvalds 		if (map->access & query) {
6716264d69dSAl Viro 			__be32 err2;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 			sresult |= map->access;
6741da177e4SLinus Torvalds 
6750ec757dfSJ. Bruce Fields 			err2 = nfsd_permission(rqstp, export, dentry, map->how);
6761da177e4SLinus Torvalds 			switch (err2) {
6771da177e4SLinus Torvalds 			case nfs_ok:
6781da177e4SLinus Torvalds 				result |= map->access;
6791da177e4SLinus Torvalds 				break;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 			/* the following error codes just mean the access was not allowed,
6821da177e4SLinus Torvalds 			 * rather than an error occurred */
6831da177e4SLinus Torvalds 			case nfserr_rofs:
6841da177e4SLinus Torvalds 			case nfserr_acces:
6851da177e4SLinus Torvalds 			case nfserr_perm:
6861da177e4SLinus Torvalds 				/* simply don't "or" in the access bit. */
6871da177e4SLinus Torvalds 				break;
6881da177e4SLinus Torvalds 			default:
6891da177e4SLinus Torvalds 				error = err2;
6901da177e4SLinus Torvalds 				goto out;
6911da177e4SLinus Torvalds 			}
6921da177e4SLinus Torvalds 		}
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds 	*access = result;
6951da177e4SLinus Torvalds 	if (supported)
6961da177e4SLinus Torvalds 		*supported = sresult;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds  out:
6991da177e4SLinus Torvalds 	return error;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
7021da177e4SLinus Torvalds 
70365294c1fSJeff Layton int nfsd_open_break_lease(struct inode *inode, int access)
704105f4622SJ. Bruce Fields {
705105f4622SJ. Bruce Fields 	unsigned int mode;
7061da177e4SLinus Torvalds 
707105f4622SJ. Bruce Fields 	if (access & NFSD_MAY_NOT_BREAK_LEASE)
708105f4622SJ. Bruce Fields 		return 0;
709105f4622SJ. Bruce Fields 	mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
710105f4622SJ. Bruce Fields 	return break_lease(inode, mode | O_NONBLOCK);
711105f4622SJ. Bruce Fields }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds /*
7141da177e4SLinus Torvalds  * Open an existing file or directory.
715999448a8SBernd Schubert  * The may_flags argument indicates the type of open (read/write/lock)
716999448a8SBernd Schubert  * and additional flags.
7171da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
7181da177e4SLinus Torvalds  */
71965294c1fSJeff Layton static __be32
72065294c1fSJeff Layton __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
721999448a8SBernd Schubert 			int may_flags, struct file **filp)
7221da177e4SLinus Torvalds {
723765927b2SAl Viro 	struct path	path;
7241da177e4SLinus Torvalds 	struct inode	*inode;
7258519f994SKinglong Mee 	struct file	*file;
7266264d69dSAl Viro 	int		flags = O_RDONLY|O_LARGEFILE;
7276264d69dSAl Viro 	__be32		err;
72891885258SJeff Layton 	int		host_err = 0;
7291da177e4SLinus Torvalds 
730765927b2SAl Viro 	path.mnt = fhp->fh_export->ex_path.mnt;
731765927b2SAl Viro 	path.dentry = fhp->fh_dentry;
7322b0143b5SDavid Howells 	inode = d_inode(path.dentry);
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	/* Disallow write access to files with the append-only bit set
7351da177e4SLinus Torvalds 	 * or any access when mandatory locking enabled
7361da177e4SLinus Torvalds 	 */
7371da177e4SLinus Torvalds 	err = nfserr_perm;
738999448a8SBernd Schubert 	if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
7391da177e4SLinus Torvalds 		goto out;
7405e7fc436SJ. Bruce Fields 	/*
7415e7fc436SJ. Bruce Fields 	 * We must ignore files (but only files) which might have mandatory
7425e7fc436SJ. Bruce Fields 	 * locks on them because there is no way to know if the accesser has
7435e7fc436SJ. Bruce Fields 	 * the lock.
7445e7fc436SJ. Bruce Fields 	 */
7455e7fc436SJ. Bruce Fields 	if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
7461da177e4SLinus Torvalds 		goto out;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	if (!inode->i_fop)
7491da177e4SLinus Torvalds 		goto out;
7501da177e4SLinus Torvalds 
751999448a8SBernd Schubert 	host_err = nfsd_open_break_lease(inode, may_flags);
7526264d69dSAl Viro 	if (host_err) /* NOMEM or WOULDBLOCK */
7531da177e4SLinus Torvalds 		goto out_nfserr;
7541da177e4SLinus Torvalds 
755999448a8SBernd Schubert 	if (may_flags & NFSD_MAY_WRITE) {
756999448a8SBernd Schubert 		if (may_flags & NFSD_MAY_READ)
7579ecb6a08SJ. Bruce Fields 			flags = O_RDWR|O_LARGEFILE;
7589ecb6a08SJ. Bruce Fields 		else
7591da177e4SLinus Torvalds 			flags = O_WRONLY|O_LARGEFILE;
7601da177e4SLinus Torvalds 	}
761999448a8SBernd Schubert 
7628519f994SKinglong Mee 	file = dentry_open(&path, flags, current_cred());
7638519f994SKinglong Mee 	if (IS_ERR(file)) {
7648519f994SKinglong Mee 		host_err = PTR_ERR(file);
7658519f994SKinglong Mee 		goto out_nfserr;
76606effdbbSBernd Schubert 	}
76706effdbbSBernd Schubert 
7686035a27bSAl Viro 	host_err = ima_file_check(file, may_flags);
7698519f994SKinglong Mee 	if (host_err) {
770fd891454SChristoph Hellwig 		fput(file);
7718519f994SKinglong Mee 		goto out_nfserr;
7728519f994SKinglong Mee 	}
7738519f994SKinglong Mee 
7748519f994SKinglong Mee 	if (may_flags & NFSD_MAY_64BIT_COOKIE)
7758519f994SKinglong Mee 		file->f_mode |= FMODE_64BITHASH;
7768519f994SKinglong Mee 	else
7778519f994SKinglong Mee 		file->f_mode |= FMODE_32BITHASH;
7788519f994SKinglong Mee 
7798519f994SKinglong Mee 	*filp = file;
7801da177e4SLinus Torvalds out_nfserr:
7816264d69dSAl Viro 	err = nfserrno(host_err);
7821da177e4SLinus Torvalds out:
78365294c1fSJeff Layton 	return err;
78465294c1fSJeff Layton }
78565294c1fSJeff Layton 
78665294c1fSJeff Layton __be32
78765294c1fSJeff Layton nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
78865294c1fSJeff Layton 		int may_flags, struct file **filp)
78965294c1fSJeff Layton {
79065294c1fSJeff Layton 	__be32 err;
79165294c1fSJeff Layton 
79265294c1fSJeff Layton 	validate_process_creds();
79365294c1fSJeff Layton 	/*
79465294c1fSJeff Layton 	 * If we get here, then the client has already done an "open",
79565294c1fSJeff Layton 	 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
79665294c1fSJeff Layton 	 * in case a chmod has now revoked permission.
79765294c1fSJeff Layton 	 *
79865294c1fSJeff Layton 	 * Arguably we should also allow the owner override for
79965294c1fSJeff Layton 	 * directories, but we never have and it doesn't seem to have
80065294c1fSJeff Layton 	 * caused anyone a problem.  If we were to change this, note
80165294c1fSJeff Layton 	 * also that our filldir callbacks would need a variant of
80265294c1fSJeff Layton 	 * lookup_one_len that doesn't check permissions.
80365294c1fSJeff Layton 	 */
80465294c1fSJeff Layton 	if (type == S_IFREG)
80565294c1fSJeff Layton 		may_flags |= NFSD_MAY_OWNER_OVERRIDE;
80665294c1fSJeff Layton 	err = fh_verify(rqstp, fhp, type, may_flags);
80765294c1fSJeff Layton 	if (!err)
80865294c1fSJeff Layton 		err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
809e0e81739SDavid Howells 	validate_process_creds();
8101da177e4SLinus Torvalds 	return err;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
81365294c1fSJeff Layton __be32
81465294c1fSJeff Layton nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
81565294c1fSJeff Layton 		int may_flags, struct file **filp)
81665294c1fSJeff Layton {
81765294c1fSJeff Layton 	__be32 err;
81865294c1fSJeff Layton 
81965294c1fSJeff Layton 	validate_process_creds();
82065294c1fSJeff Layton 	err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
82165294c1fSJeff Layton 	validate_process_creds();
82265294c1fSJeff Layton 	return err;
82365294c1fSJeff Layton }
82465294c1fSJeff Layton 
8251da177e4SLinus Torvalds /*
826cf8208d0SJens Axboe  * Grab and keep cached pages associated with a file in the svc_rqst
827cf8208d0SJens Axboe  * so that they can be passed to the network sendmsg/sendpage routines
828cf8208d0SJens Axboe  * directly. They will be released after the sending has completed.
8291da177e4SLinus Torvalds  */
8301da177e4SLinus Torvalds static int
831cf8208d0SJens Axboe nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
832cf8208d0SJens Axboe 		  struct splice_desc *sd)
8331da177e4SLinus Torvalds {
834cf8208d0SJens Axboe 	struct svc_rqst *rqstp = sd->u.data;
835afc59400SJ. Bruce Fields 	struct page **pp = rqstp->rq_next_page;
836cf8208d0SJens Axboe 	struct page *page = buf->page;
837cf8208d0SJens Axboe 	size_t size;
838cf8208d0SJens Axboe 
839cf8208d0SJens Axboe 	size = sd->len;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	if (rqstp->rq_res.page_len == 0) {
8421da177e4SLinus Torvalds 		get_page(page);
843afc59400SJ. Bruce Fields 		put_page(*rqstp->rq_next_page);
844afc59400SJ. Bruce Fields 		*(rqstp->rq_next_page++) = page;
845cf8208d0SJens Axboe 		rqstp->rq_res.page_base = buf->offset;
8461da177e4SLinus Torvalds 		rqstp->rq_res.page_len = size;
84744524359SNeilBrown 	} else if (page != pp[-1]) {
8481da177e4SLinus Torvalds 		get_page(page);
849afc59400SJ. Bruce Fields 		if (*rqstp->rq_next_page)
850afc59400SJ. Bruce Fields 			put_page(*rqstp->rq_next_page);
851afc59400SJ. Bruce Fields 		*(rqstp->rq_next_page++) = page;
8521da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
85344524359SNeilBrown 	} else
8541da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	return size;
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
859cf8208d0SJens Axboe static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
860cf8208d0SJens Axboe 				    struct splice_desc *sd)
861cf8208d0SJens Axboe {
862cf8208d0SJens Axboe 	return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
863cf8208d0SJens Axboe }
864cf8208d0SJens Axboe 
86583a63072STrond Myklebust static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
86683a63072STrond Myklebust 		size_t expected)
86783a63072STrond Myklebust {
86883a63072STrond Myklebust 	if (expected != 0 && len == 0)
86983a63072STrond Myklebust 		return 1;
87083a63072STrond Myklebust 	if (offset+len >= i_size_read(file_inode(file)))
87183a63072STrond Myklebust 		return 1;
87283a63072STrond Myklebust 	return 0;
87383a63072STrond Myklebust }
87483a63072STrond Myklebust 
87587c5942eSChuck Lever static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
87687c5942eSChuck Lever 			       struct file *file, loff_t offset,
87783a63072STrond Myklebust 			       unsigned long *count, u32 *eof, ssize_t host_err)
8781da177e4SLinus Torvalds {
879dc97618dSJ. Bruce Fields 	if (host_err >= 0) {
880dc97618dSJ. Bruce Fields 		nfsdstats.io_read += host_err;
88183a63072STrond Myklebust 		*eof = nfsd_eof_on_read(file, offset, host_err, *count);
882dc97618dSJ. Bruce Fields 		*count = host_err;
883dc97618dSJ. Bruce Fields 		fsnotify_access(file);
88487c5942eSChuck Lever 		trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
885dc97618dSJ. Bruce Fields 		return 0;
88687c5942eSChuck Lever 	} else {
88787c5942eSChuck Lever 		trace_nfsd_read_err(rqstp, fhp, offset, host_err);
888dc97618dSJ. Bruce Fields 		return nfserrno(host_err);
889dc97618dSJ. Bruce Fields 	}
89087c5942eSChuck Lever }
8911da177e4SLinus Torvalds 
89287c5942eSChuck Lever __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
89383a63072STrond Myklebust 			struct file *file, loff_t offset, unsigned long *count,
89483a63072STrond Myklebust 			u32 *eof)
895dc97618dSJ. Bruce Fields {
896cf8208d0SJens Axboe 	struct splice_desc sd = {
897cf8208d0SJens Axboe 		.len		= 0,
898cf8208d0SJens Axboe 		.total_len	= *count,
899cf8208d0SJens Axboe 		.pos		= offset,
900cf8208d0SJens Axboe 		.u.data		= rqstp,
901cf8208d0SJens Axboe 	};
90283a63072STrond Myklebust 	ssize_t host_err;
903cf8208d0SJens Axboe 
90487c5942eSChuck Lever 	trace_nfsd_read_splice(rqstp, fhp, offset, *count);
905afc59400SJ. Bruce Fields 	rqstp->rq_next_page = rqstp->rq_respages + 1;
906cf8208d0SJens Axboe 	host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
90783a63072STrond Myklebust 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
908dc97618dSJ. Bruce Fields }
909dc97618dSJ. Bruce Fields 
91087c5942eSChuck Lever __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
91187c5942eSChuck Lever 		  struct file *file, loff_t offset,
91283a63072STrond Myklebust 		  struct kvec *vec, int vlen, unsigned long *count,
91383a63072STrond Myklebust 		  u32 *eof)
914dc97618dSJ. Bruce Fields {
91573da852eSChristoph Hellwig 	struct iov_iter iter;
91683a63072STrond Myklebust 	loff_t ppos = offset;
91783a63072STrond Myklebust 	ssize_t host_err;
918dc97618dSJ. Bruce Fields 
91987c5942eSChuck Lever 	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
920aa563d7bSDavid Howells 	iov_iter_kvec(&iter, READ, vec, vlen, *count);
92183a63072STrond Myklebust 	host_err = vfs_iter_read(file, &iter, &ppos, 0);
92283a63072STrond Myklebust 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
925d911df7bSJ. Bruce Fields /*
926d911df7bSJ. Bruce Fields  * Gathered writes: If another process is currently writing to the file,
927d911df7bSJ. Bruce Fields  * there's a high chance this is another nfsd (triggered by a bulk write
928d911df7bSJ. Bruce Fields  * from a client's biod). Rather than syncing the file with each write
929d911df7bSJ. Bruce Fields  * request, we sleep for 10 msec.
930d911df7bSJ. Bruce Fields  *
931d911df7bSJ. Bruce Fields  * I don't know if this roughly approximates C. Juszak's idea of
932d911df7bSJ. Bruce Fields  * gathered writes, but it's a nice and simple solution (IMHO), and it
933d911df7bSJ. Bruce Fields  * seems to work:-)
934d911df7bSJ. Bruce Fields  *
935d911df7bSJ. Bruce Fields  * Note: we do this only in the NFSv2 case, since v3 and higher have a
936d911df7bSJ. Bruce Fields  * better tool (separate unstable writes and commits) for solving this
937d911df7bSJ. Bruce Fields  * problem.
938d911df7bSJ. Bruce Fields  */
939d911df7bSJ. Bruce Fields static int wait_for_concurrent_writes(struct file *file)
940d911df7bSJ. Bruce Fields {
941496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
942d911df7bSJ. Bruce Fields 	static ino_t last_ino;
943d911df7bSJ. Bruce Fields 	static dev_t last_dev;
944d911df7bSJ. Bruce Fields 	int err = 0;
945d911df7bSJ. Bruce Fields 
946d911df7bSJ. Bruce Fields 	if (atomic_read(&inode->i_writecount) > 1
947d911df7bSJ. Bruce Fields 	    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
948d911df7bSJ. Bruce Fields 		dprintk("nfsd: write defer %d\n", task_pid_nr(current));
949d911df7bSJ. Bruce Fields 		msleep(10);
950d911df7bSJ. Bruce Fields 		dprintk("nfsd: write resume %d\n", task_pid_nr(current));
951d911df7bSJ. Bruce Fields 	}
952d911df7bSJ. Bruce Fields 
953d911df7bSJ. Bruce Fields 	if (inode->i_state & I_DIRTY) {
954d911df7bSJ. Bruce Fields 		dprintk("nfsd: write sync %d\n", task_pid_nr(current));
9558018ab05SChristoph Hellwig 		err = vfs_fsync(file, 0);
956d911df7bSJ. Bruce Fields 	}
957d911df7bSJ. Bruce Fields 	last_ino = inode->i_ino;
958d911df7bSJ. Bruce Fields 	last_dev = inode->i_sb->s_dev;
959d911df7bSJ. Bruce Fields 	return err;
960d911df7bSJ. Bruce Fields }
961d911df7bSJ. Bruce Fields 
962af90f707SChristoph Hellwig __be32
96316f8f894STrond Myklebust nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
9641da177e4SLinus Torvalds 				loff_t offset, struct kvec *vec, int vlen,
96519e0663fSTrond Myklebust 				unsigned long *cnt, int stable,
96619e0663fSTrond Myklebust 				__be32 *verf)
9671da177e4SLinus Torvalds {
96816f8f894STrond Myklebust 	struct file		*file = nf->nf_file;
9691da177e4SLinus Torvalds 	struct svc_export	*exp;
97073da852eSChristoph Hellwig 	struct iov_iter		iter;
971d890be15SChuck Lever 	__be32			nfserr;
9726264d69dSAl Viro 	int			host_err;
97348e03bc5STrond Myklebust 	int			use_wgather;
974e49dbbf3SKent Overstreet 	loff_t			pos = offset;
9758658452eSNeilBrown 	unsigned int		pflags = current->flags;
976ddef7ed2SChristoph Hellwig 	rwf_t			flags = 0;
9778658452eSNeilBrown 
978d890be15SChuck Lever 	trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
979d890be15SChuck Lever 
9807501cc2bSJeff Layton 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
9818658452eSNeilBrown 		/*
982*a37b0715SNeilBrown 		 * We want throttling in balance_dirty_pages()
983*a37b0715SNeilBrown 		 * and shrink_inactive_list() to only consider
984*a37b0715SNeilBrown 		 * the backingdev we are writing to, so that nfs to
9858658452eSNeilBrown 		 * localhost doesn't cause nfsd to lock up due to all
9868658452eSNeilBrown 		 * the client's dirty pages or its congested queue.
9878658452eSNeilBrown 		 */
988*a37b0715SNeilBrown 		current->flags |= PF_LOCAL_THROTTLE;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	exp = fhp->fh_export;
99148e03bc5STrond Myklebust 	use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	if (!EX_ISSYNC(exp))
99454bbb7d2SKinglong Mee 		stable = NFS_UNSTABLE;
9951da177e4SLinus Torvalds 
99624368aadSChristoph Hellwig 	if (stable && !use_wgather)
99724368aadSChristoph Hellwig 		flags |= RWF_SYNC;
99824368aadSChristoph Hellwig 
999aa563d7bSDavid Howells 	iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
10005011af4cSTrond Myklebust 	if (flags & RWF_SYNC) {
10015011af4cSTrond Myklebust 		down_write(&nf->nf_rwsem);
100273da852eSChristoph Hellwig 		host_err = vfs_iter_write(file, &iter, &pos, flags);
1003e4636d53SJ. Bruce Fields 		if (host_err < 0)
10045011af4cSTrond Myklebust 			nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
10055011af4cSTrond Myklebust 						 nfsd_net_id));
10065011af4cSTrond Myklebust 		up_write(&nf->nf_rwsem);
10075011af4cSTrond Myklebust 	} else {
10085011af4cSTrond Myklebust 		down_read(&nf->nf_rwsem);
100919e0663fSTrond Myklebust 		if (verf)
101019e0663fSTrond Myklebust 			nfsd_copy_boot_verifier(verf,
101119e0663fSTrond Myklebust 					net_generic(SVC_NET(rqstp),
101219e0663fSTrond Myklebust 					nfsd_net_id));
10135011af4cSTrond Myklebust 		host_err = vfs_iter_write(file, &iter, &pos, flags);
10145011af4cSTrond Myklebust 		up_read(&nf->nf_rwsem);
10155011af4cSTrond Myklebust 	}
10167bf94c6bSTrond Myklebust 	if (host_err < 0) {
10177bf94c6bSTrond Myklebust 		nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
10187bf94c6bSTrond Myklebust 					 nfsd_net_id));
1019e4636d53SJ. Bruce Fields 		goto out_nfserr;
10207bf94c6bSTrond Myklebust 	}
102109a80f2aSTrond Myklebust 	*cnt = host_err;
1022d890be15SChuck Lever 	nfsdstats.io_write += *cnt;
10232a12a9d7SEric Paris 	fsnotify_modify(file);
10241da177e4SLinus Torvalds 
1025bbf2f098STrond Myklebust 	if (stable && use_wgather) {
1026d911df7bSJ. Bruce Fields 		host_err = wait_for_concurrent_writes(file);
1027bbf2f098STrond Myklebust 		if (host_err < 0)
1028bbf2f098STrond Myklebust 			nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
1029bbf2f098STrond Myklebust 						 nfsd_net_id));
1030bbf2f098STrond Myklebust 	}
10311da177e4SLinus Torvalds 
1032e4636d53SJ. Bruce Fields out_nfserr:
1033d890be15SChuck Lever 	if (host_err >= 0) {
1034d890be15SChuck Lever 		trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1035d890be15SChuck Lever 		nfserr = nfs_ok;
1036d890be15SChuck Lever 	} else {
1037d890be15SChuck Lever 		trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1038d890be15SChuck Lever 		nfserr = nfserrno(host_err);
1039d890be15SChuck Lever 	}
10407501cc2bSJeff Layton 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
1041*a37b0715SNeilBrown 		current_restore_flags(pflags, PF_LOCAL_THROTTLE);
1042d890be15SChuck Lever 	return nfserr;
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
1045dc97618dSJ. Bruce Fields /*
1046dc97618dSJ. Bruce Fields  * Read data from a file. count must contain the requested read count
1047dc97618dSJ. Bruce Fields  * on entry. On return, *count contains the number of bytes actually read.
1048dc97618dSJ. Bruce Fields  * N.B. After this call fhp needs an fh_put
1049dc97618dSJ. Bruce Fields  */
1050dc97618dSJ. Bruce Fields __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
105183a63072STrond Myklebust 	loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
105283a63072STrond Myklebust 	u32 *eof)
1053dc97618dSJ. Bruce Fields {
105448cd7b51SJeff Layton 	struct nfsd_file	*nf;
1055dc97618dSJ. Bruce Fields 	struct file *file;
1056dc97618dSJ. Bruce Fields 	__be32 err;
1057dc97618dSJ. Bruce Fields 
1058f394b62bSChuck Lever 	trace_nfsd_read_start(rqstp, fhp, offset, *count);
105948cd7b51SJeff Layton 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
1060dc97618dSJ. Bruce Fields 	if (err)
1061dc97618dSJ. Bruce Fields 		return err;
1062dc97618dSJ. Bruce Fields 
106348cd7b51SJeff Layton 	file = nf->nf_file;
1064a4058c5bSChristoph Hellwig 	if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
106583a63072STrond Myklebust 		err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
1066a4058c5bSChristoph Hellwig 	else
106783a63072STrond Myklebust 		err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
10686e8b50d1SJeff Layton 
106948cd7b51SJeff Layton 	nfsd_file_put(nf);
1070dc97618dSJ. Bruce Fields 
1071f394b62bSChuck Lever 	trace_nfsd_read_done(rqstp, fhp, offset, *count);
10726e8b50d1SJeff Layton 
1073fa0a2126SJ. Bruce Fields 	return err;
1074fa0a2126SJ. Bruce Fields }
1075fa0a2126SJ. Bruce Fields 
10761da177e4SLinus Torvalds /*
10771da177e4SLinus Torvalds  * Write data to a file.
10781da177e4SLinus Torvalds  * The stable flag requests synchronous writes.
10791da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
10801da177e4SLinus Torvalds  */
10816264d69dSAl Viro __be32
108252e380e0SKinglong Mee nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
108319e0663fSTrond Myklebust 	   struct kvec *vec, int vlen, unsigned long *cnt, int stable,
108419e0663fSTrond Myklebust 	   __be32 *verf)
10851da177e4SLinus Torvalds {
1086b4935239SJeff Layton 	struct nfsd_file *nf;
1087b4935239SJeff Layton 	__be32 err;
10881da177e4SLinus Torvalds 
1089f394b62bSChuck Lever 	trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
10906e8b50d1SJeff Layton 
1091b4935239SJeff Layton 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
10921da177e4SLinus Torvalds 	if (err)
10931da177e4SLinus Torvalds 		goto out;
10941da177e4SLinus Torvalds 
109516f8f894STrond Myklebust 	err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
109619e0663fSTrond Myklebust 			vlen, cnt, stable, verf);
1097b4935239SJeff Layton 	nfsd_file_put(nf);
10981da177e4SLinus Torvalds out:
1099f394b62bSChuck Lever 	trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
11001da177e4SLinus Torvalds 	return err;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
11041da177e4SLinus Torvalds /*
11051da177e4SLinus Torvalds  * Commit all pending writes to stable storage.
1106aa696a6fSTrond Myklebust  *
1107aa696a6fSTrond Myklebust  * Note: we only guarantee that data that lies within the range specified
1108aa696a6fSTrond Myklebust  * by the 'offset' and 'count' parameters will be synced.
11091da177e4SLinus Torvalds  *
11101da177e4SLinus Torvalds  * Unfortunately we cannot lock the file to make sure we return full WCC
11111da177e4SLinus Torvalds  * data to the client, as locking happens lower down in the filesystem.
11121da177e4SLinus Torvalds  */
11136264d69dSAl Viro __be32
11141da177e4SLinus Torvalds nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1115524ff1afSTrond Myklebust                loff_t offset, unsigned long count, __be32 *verf)
11161da177e4SLinus Torvalds {
11175920afa3SJeff Layton 	struct nfsd_file	*nf;
1118aa696a6fSTrond Myklebust 	loff_t			end = LLONG_MAX;
1119aa696a6fSTrond Myklebust 	__be32			err = nfserr_inval;
11201da177e4SLinus Torvalds 
1121aa696a6fSTrond Myklebust 	if (offset < 0)
1122aa696a6fSTrond Myklebust 		goto out;
1123aa696a6fSTrond Myklebust 	if (count != 0) {
1124aa696a6fSTrond Myklebust 		end = offset + (loff_t)count - 1;
1125aa696a6fSTrond Myklebust 		if (end < offset)
1126aa696a6fSTrond Myklebust 			goto out;
1127aa696a6fSTrond Myklebust 	}
11281da177e4SLinus Torvalds 
11295920afa3SJeff Layton 	err = nfsd_file_acquire(rqstp, fhp,
11305920afa3SJeff Layton 			NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
11318837abcaSMiklos Szeredi 	if (err)
1132aa696a6fSTrond Myklebust 		goto out;
11331da177e4SLinus Torvalds 	if (EX_ISSYNC(fhp->fh_export)) {
11345011af4cSTrond Myklebust 		int err2;
1135aa696a6fSTrond Myklebust 
11365011af4cSTrond Myklebust 		down_write(&nf->nf_rwsem);
11375011af4cSTrond Myklebust 		err2 = vfs_fsync_range(nf->nf_file, offset, end, 0);
1138bbf2f098STrond Myklebust 		switch (err2) {
1139bbf2f098STrond Myklebust 		case 0:
1140524ff1afSTrond Myklebust 			nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
1141524ff1afSTrond Myklebust 						nfsd_net_id));
1142bbf2f098STrond Myklebust 			break;
1143bbf2f098STrond Myklebust 		case -EINVAL:
11441da177e4SLinus Torvalds 			err = nfserr_notsupp;
1145bbf2f098STrond Myklebust 			break;
1146bbf2f098STrond Myklebust 		default:
1147bbf2f098STrond Myklebust 			err = nfserrno(err2);
1148bbf2f098STrond Myklebust 			nfsd_reset_boot_verifier(net_generic(nf->nf_net,
1149bbf2f098STrond Myklebust 						 nfsd_net_id));
1150bbf2f098STrond Myklebust 		}
11515011af4cSTrond Myklebust 		up_write(&nf->nf_rwsem);
1152524ff1afSTrond Myklebust 	} else
1153524ff1afSTrond Myklebust 		nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
1154524ff1afSTrond Myklebust 					nfsd_net_id));
11551da177e4SLinus Torvalds 
11565920afa3SJeff Layton 	nfsd_file_put(nf);
1157aa696a6fSTrond Myklebust out:
11581da177e4SLinus Torvalds 	return err;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
11611da177e4SLinus Torvalds 
1162f2b0dee2SAdrian Bunk static __be32
11635c002b3bSJ. Bruce Fields nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
11645c002b3bSJ. Bruce Fields 			struct iattr *iap)
11655c002b3bSJ. Bruce Fields {
11665c002b3bSJ. Bruce Fields 	/*
11675c002b3bSJ. Bruce Fields 	 * Mode has already been set earlier in create:
11685c002b3bSJ. Bruce Fields 	 */
11695c002b3bSJ. Bruce Fields 	iap->ia_valid &= ~ATTR_MODE;
11705c002b3bSJ. Bruce Fields 	/*
11715c002b3bSJ. Bruce Fields 	 * Setting uid/gid works only for root.  Irix appears to
11725c002b3bSJ. Bruce Fields 	 * send along the gid on create when it tries to implement
11735c002b3bSJ. Bruce Fields 	 * setgid directories via NFS:
11745c002b3bSJ. Bruce Fields 	 */
11756fab8779SEric W. Biederman 	if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
11765c002b3bSJ. Bruce Fields 		iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
11775c002b3bSJ. Bruce Fields 	if (iap->ia_valid)
11782a1aa489SArnd Bergmann 		return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0);
11790f3a24b4STrond Myklebust 	/* Callers expect file metadata to be committed here */
1180722b620dSJeff Layton 	return nfserrno(commit_metadata(resfhp));
11815c002b3bSJ. Bruce Fields }
11825c002b3bSJ. Bruce Fields 
11834ac35c2fSwengang wang /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
11844ac35c2fSwengang wang  * setting size to 0 may fail for some specific file systems by the permission
11854ac35c2fSwengang wang  * checking which requires WRITE permission but the mode is 000.
11864ac35c2fSwengang wang  * we ignore the resizing(to 0) on the just new created file, since the size is
11874ac35c2fSwengang wang  * 0 after file created.
11884ac35c2fSwengang wang  *
11894ac35c2fSwengang wang  * call this only after vfs_create() is called.
11904ac35c2fSwengang wang  * */
11914ac35c2fSwengang wang static void
11924ac35c2fSwengang wang nfsd_check_ignore_resizing(struct iattr *iap)
11934ac35c2fSwengang wang {
11944ac35c2fSwengang wang 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
11954ac35c2fSwengang wang 		iap->ia_valid &= ~ATTR_SIZE;
11964ac35c2fSwengang wang }
11974ac35c2fSwengang wang 
1198b44061d0SJ. Bruce Fields /* The parent directory should already be locked: */
11996264d69dSAl Viro __be32
1200b44061d0SJ. Bruce Fields nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
12011da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
12021da177e4SLinus Torvalds 		int type, dev_t rdev, struct svc_fh *resfhp)
12031da177e4SLinus Torvalds {
12042b118859SDan Carpenter 	struct dentry	*dentry, *dchild;
12051da177e4SLinus Torvalds 	struct inode	*dirp;
12066264d69dSAl Viro 	__be32		err;
12075c002b3bSJ. Bruce Fields 	__be32		err2;
12086264d69dSAl Viro 	int		host_err;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
12112b0143b5SDavid Howells 	dirp = d_inode(dentry);
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	dchild = dget(resfhp->fh_dentry);
12141da177e4SLinus Torvalds 	if (!fhp->fh_locked) {
1215b44061d0SJ. Bruce Fields 		WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1216a6a9f18fSAl Viro 				dentry);
1217d75f2b9fSAl Viro 		err = nfserr_io;
12181da177e4SLinus Torvalds 		goto out;
12191da177e4SLinus Torvalds 	}
12201da177e4SLinus Torvalds 
12217eed34f1SOleg Drokin 	err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
12227eed34f1SOleg Drokin 	if (err)
12237eed34f1SOleg Drokin 		goto out;
12247eed34f1SOleg Drokin 
12251da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
12261da177e4SLinus Torvalds 		iap->ia_mode = 0;
12271da177e4SLinus Torvalds 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
12281da177e4SLinus Torvalds 
1229088406bcSJ. Bruce Fields 	err = 0;
12304a55c101SJan Kara 	host_err = 0;
12311da177e4SLinus Torvalds 	switch (type) {
12321da177e4SLinus Torvalds 	case S_IFREG:
1233312b63fbSAl Viro 		host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
12344ac35c2fSwengang wang 		if (!host_err)
12354ac35c2fSwengang wang 			nfsd_check_ignore_resizing(iap);
12361da177e4SLinus Torvalds 		break;
12371da177e4SLinus Torvalds 	case S_IFDIR:
12386264d69dSAl Viro 		host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
12393819bb0dSAl Viro 		if (!host_err && unlikely(d_unhashed(dchild))) {
12403819bb0dSAl Viro 			struct dentry *d;
12413819bb0dSAl Viro 			d = lookup_one_len(dchild->d_name.name,
12423819bb0dSAl Viro 					   dchild->d_parent,
12433819bb0dSAl Viro 					   dchild->d_name.len);
12443819bb0dSAl Viro 			if (IS_ERR(d)) {
12453819bb0dSAl Viro 				host_err = PTR_ERR(d);
12463819bb0dSAl Viro 				break;
12473819bb0dSAl Viro 			}
12483819bb0dSAl Viro 			if (unlikely(d_is_negative(d))) {
12493819bb0dSAl Viro 				dput(d);
12503819bb0dSAl Viro 				err = nfserr_serverfault;
12513819bb0dSAl Viro 				goto out;
12523819bb0dSAl Viro 			}
12533819bb0dSAl Viro 			dput(resfhp->fh_dentry);
12543819bb0dSAl Viro 			resfhp->fh_dentry = dget(d);
12553819bb0dSAl Viro 			err = fh_update(resfhp);
12563819bb0dSAl Viro 			dput(dchild);
12573819bb0dSAl Viro 			dchild = d;
12583819bb0dSAl Viro 			if (err)
12593819bb0dSAl Viro 				goto out;
12603819bb0dSAl Viro 		}
12611da177e4SLinus Torvalds 		break;
12621da177e4SLinus Torvalds 	case S_IFCHR:
12631da177e4SLinus Torvalds 	case S_IFBLK:
12641da177e4SLinus Torvalds 	case S_IFIFO:
12651da177e4SLinus Torvalds 	case S_IFSOCK:
12666264d69dSAl Viro 		host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
12671da177e4SLinus Torvalds 		break;
126871423274SJ. Bruce Fields 	default:
126971423274SJ. Bruce Fields 		printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
127071423274SJ. Bruce Fields 		       type);
127171423274SJ. Bruce Fields 		host_err = -EINVAL;
1272463c3197SDave Hansen 	}
12734a55c101SJan Kara 	if (host_err < 0)
1274463c3197SDave Hansen 		goto out_nfserr;
12751da177e4SLinus Torvalds 
1276f501912aSBen Myers 	err = nfsd_create_setattr(rqstp, resfhp, iap);
12771da177e4SLinus Torvalds 
1278f501912aSBen Myers 	/*
12790f3a24b4STrond Myklebust 	 * nfsd_create_setattr already committed the child.  Transactional
12800f3a24b4STrond Myklebust 	 * filesystems had a chance to commit changes for both parent and
1281b44061d0SJ. Bruce Fields 	 * child simultaneously making the following commit_metadata a
12820f3a24b4STrond Myklebust 	 * noop.
1283f501912aSBen Myers 	 */
1284f501912aSBen Myers 	err2 = nfserrno(commit_metadata(fhp));
1285f193fbabSYAMAMOTO Takashi 	if (err2)
1286f193fbabSYAMAMOTO Takashi 		err = err2;
12871da177e4SLinus Torvalds 	/*
12881da177e4SLinus Torvalds 	 * Update the file handle to get the new inode info.
12891da177e4SLinus Torvalds 	 */
12901da177e4SLinus Torvalds 	if (!err)
12911da177e4SLinus Torvalds 		err = fh_update(resfhp);
12921da177e4SLinus Torvalds out:
12931da177e4SLinus Torvalds 	dput(dchild);
12941da177e4SLinus Torvalds 	return err;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds out_nfserr:
12976264d69dSAl Viro 	err = nfserrno(host_err);
12981da177e4SLinus Torvalds 	goto out;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
1301b44061d0SJ. Bruce Fields /*
1302b44061d0SJ. Bruce Fields  * Create a filesystem object (regular, directory, special).
1303b44061d0SJ. Bruce Fields  * Note that the parent directory is left locked.
1304b44061d0SJ. Bruce Fields  *
1305b44061d0SJ. Bruce Fields  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1306b44061d0SJ. Bruce Fields  */
1307b44061d0SJ. Bruce Fields __be32
1308b44061d0SJ. Bruce Fields nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1309b44061d0SJ. Bruce Fields 		char *fname, int flen, struct iattr *iap,
1310b44061d0SJ. Bruce Fields 		int type, dev_t rdev, struct svc_fh *resfhp)
1311b44061d0SJ. Bruce Fields {
1312b44061d0SJ. Bruce Fields 	struct dentry	*dentry, *dchild = NULL;
1313b44061d0SJ. Bruce Fields 	__be32		err;
1314b44061d0SJ. Bruce Fields 	int		host_err;
1315b44061d0SJ. Bruce Fields 
1316b44061d0SJ. Bruce Fields 	if (isdotent(fname, flen))
1317b44061d0SJ. Bruce Fields 		return nfserr_exist;
1318b44061d0SJ. Bruce Fields 
1319fa08139dSJ. Bruce Fields 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1320b44061d0SJ. Bruce Fields 	if (err)
1321b44061d0SJ. Bruce Fields 		return err;
1322b44061d0SJ. Bruce Fields 
1323b44061d0SJ. Bruce Fields 	dentry = fhp->fh_dentry;
1324b44061d0SJ. Bruce Fields 
1325b44061d0SJ. Bruce Fields 	host_err = fh_want_write(fhp);
1326b44061d0SJ. Bruce Fields 	if (host_err)
1327b44061d0SJ. Bruce Fields 		return nfserrno(host_err);
1328b44061d0SJ. Bruce Fields 
1329b44061d0SJ. Bruce Fields 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1330b44061d0SJ. Bruce Fields 	dchild = lookup_one_len(fname, dentry, flen);
1331b44061d0SJ. Bruce Fields 	host_err = PTR_ERR(dchild);
1332b44061d0SJ. Bruce Fields 	if (IS_ERR(dchild))
1333b44061d0SJ. Bruce Fields 		return nfserrno(host_err);
1334b44061d0SJ. Bruce Fields 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1335502aa0a5SJosef Bacik 	/*
1336502aa0a5SJosef Bacik 	 * We unconditionally drop our ref to dchild as fh_compose will have
1337502aa0a5SJosef Bacik 	 * already grabbed its own ref for it.
1338502aa0a5SJosef Bacik 	 */
1339b44061d0SJ. Bruce Fields 	dput(dchild);
1340502aa0a5SJosef Bacik 	if (err)
1341b44061d0SJ. Bruce Fields 		return err;
1342b44061d0SJ. Bruce Fields 	return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1343b44061d0SJ. Bruce Fields 					rdev, resfhp);
1344b44061d0SJ. Bruce Fields }
1345b44061d0SJ. Bruce Fields 
13461da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
1347ac6721a1SMi Jinlong 
13481da177e4SLinus Torvalds /*
1349ac6721a1SMi Jinlong  * NFSv3 and NFSv4 version of nfsd_create
13501da177e4SLinus Torvalds  */
13516264d69dSAl Viro __be32
1352ac6721a1SMi Jinlong do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
13531da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
13541da177e4SLinus Torvalds 		struct svc_fh *resfhp, int createmode, u32 *verifier,
1355856121b2SJ. Bruce Fields 	        bool *truncp, bool *created)
13561da177e4SLinus Torvalds {
13571da177e4SLinus Torvalds 	struct dentry	*dentry, *dchild = NULL;
13581da177e4SLinus Torvalds 	struct inode	*dirp;
13596264d69dSAl Viro 	__be32		err;
13606264d69dSAl Viro 	int		host_err;
13611da177e4SLinus Torvalds 	__u32		v_mtime=0, v_atime=0;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	err = nfserr_perm;
13641da177e4SLinus Torvalds 	if (!flen)
13651da177e4SLinus Torvalds 		goto out;
13661da177e4SLinus Torvalds 	err = nfserr_exist;
13671da177e4SLinus Torvalds 	if (isdotent(fname, flen))
13681da177e4SLinus Torvalds 		goto out;
13691da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
13701da177e4SLinus Torvalds 		iap->ia_mode = 0;
13711574dff8SSachin Prabhu 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
13721da177e4SLinus Torvalds 	if (err)
13731da177e4SLinus Torvalds 		goto out;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
13762b0143b5SDavid Howells 	dirp = d_inode(dentry);
13771da177e4SLinus Torvalds 
13784a55c101SJan Kara 	host_err = fh_want_write(fhp);
13794a55c101SJan Kara 	if (host_err)
13804a55c101SJan Kara 		goto out_nfserr;
13814a55c101SJan Kara 
138212fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	/*
13851da177e4SLinus Torvalds 	 * Compose the response file handle.
13861da177e4SLinus Torvalds 	 */
13871da177e4SLinus Torvalds 	dchild = lookup_one_len(fname, dentry, flen);
13886264d69dSAl Viro 	host_err = PTR_ERR(dchild);
13891da177e4SLinus Torvalds 	if (IS_ERR(dchild))
13901da177e4SLinus Torvalds 		goto out_nfserr;
13911da177e4SLinus Torvalds 
13921574dff8SSachin Prabhu 	/* If file doesn't exist, check for permissions to create one */
13932b0143b5SDavid Howells 	if (d_really_is_negative(dchild)) {
13941574dff8SSachin Prabhu 		err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
13951574dff8SSachin Prabhu 		if (err)
13961574dff8SSachin Prabhu 			goto out;
13971574dff8SSachin Prabhu 	}
13981574dff8SSachin Prabhu 
13991da177e4SLinus Torvalds 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
14001da177e4SLinus Torvalds 	if (err)
14011da177e4SLinus Torvalds 		goto out;
14021da177e4SLinus Torvalds 
1403ac6721a1SMi Jinlong 	if (nfsd_create_is_exclusive(createmode)) {
1404c397852cSPeter Staubach 		/* solaris7 gets confused (bugid 4218508) if these have
1405749997e5SJeff Layton 		 * the high bit set, so just clear the high bits. If this is
1406749997e5SJeff Layton 		 * ever changed to use different attrs for storing the
1407749997e5SJeff Layton 		 * verifier, then do_open_lookup() will also need to be fixed
1408749997e5SJeff Layton 		 * accordingly.
14091da177e4SLinus Torvalds 		 */
14101da177e4SLinus Torvalds 		v_mtime = verifier[0]&0x7fffffff;
14111da177e4SLinus Torvalds 		v_atime = verifier[1]&0x7fffffff;
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds 
14142b0143b5SDavid Howells 	if (d_really_is_positive(dchild)) {
14151da177e4SLinus Torvalds 		err = 0;
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 		switch (createmode) {
14181da177e4SLinus Torvalds 		case NFS3_CREATE_UNCHECKED:
1419e36cb0b8SDavid Howells 			if (! d_is_reg(dchild))
14209dc4e6c4SJ. Bruce Fields 				goto out;
14211da177e4SLinus Torvalds 			else if (truncp) {
14221da177e4SLinus Torvalds 				/* in nfsv4, we need to treat this case a little
14231da177e4SLinus Torvalds 				 * differently.  we don't want to truncate the
14241da177e4SLinus Torvalds 				 * file now; this would be wrong if the OPEN
14251da177e4SLinus Torvalds 				 * fails for some other reason.  furthermore,
14261da177e4SLinus Torvalds 				 * if the size is nonzero, we should ignore it
14271da177e4SLinus Torvalds 				 * according to spec!
14281da177e4SLinus Torvalds 				 */
14291da177e4SLinus Torvalds 				*truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
14301da177e4SLinus Torvalds 			}
14311da177e4SLinus Torvalds 			else {
14321da177e4SLinus Torvalds 				iap->ia_valid &= ATTR_SIZE;
14331da177e4SLinus Torvalds 				goto set_attr;
14341da177e4SLinus Torvalds 			}
14351da177e4SLinus Torvalds 			break;
14361da177e4SLinus Torvalds 		case NFS3_CREATE_EXCLUSIVE:
14372b0143b5SDavid Howells 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
14382b0143b5SDavid Howells 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
14392b0143b5SDavid Howells 			    && d_inode(dchild)->i_size  == 0 ) {
14407007c90fSNeil Brown 				if (created)
1441384a7ccaSzhengbin 					*created = true;
14421da177e4SLinus Torvalds 				break;
14437007c90fSNeil Brown 			}
14440ac203cbSGustavo A. R. Silva 			/* fall through */
1445ac6721a1SMi Jinlong 		case NFS4_CREATE_EXCLUSIVE4_1:
14462b0143b5SDavid Howells 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
14472b0143b5SDavid Howells 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
14482b0143b5SDavid Howells 			    && d_inode(dchild)->i_size  == 0 ) {
14497007c90fSNeil Brown 				if (created)
1450384a7ccaSzhengbin 					*created = true;
1451ac6721a1SMi Jinlong 				goto set_attr;
14527007c90fSNeil Brown 			}
14530ac203cbSGustavo A. R. Silva 			/* fall through */
14541da177e4SLinus Torvalds 		case NFS3_CREATE_GUARDED:
14551da177e4SLinus Torvalds 			err = nfserr_exist;
14561da177e4SLinus Torvalds 		}
1457bad0dcffSAl Viro 		fh_drop_write(fhp);
14581da177e4SLinus Torvalds 		goto out;
14591da177e4SLinus Torvalds 	}
14601da177e4SLinus Torvalds 
1461312b63fbSAl Viro 	host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1462463c3197SDave Hansen 	if (host_err < 0) {
1463bad0dcffSAl Viro 		fh_drop_write(fhp);
14641da177e4SLinus Torvalds 		goto out_nfserr;
1465463c3197SDave Hansen 	}
146681ac95c5SJ. Bruce Fields 	if (created)
1467384a7ccaSzhengbin 		*created = true;
14681da177e4SLinus Torvalds 
14694ac35c2fSwengang wang 	nfsd_check_ignore_resizing(iap);
14704ac35c2fSwengang wang 
1471ac6721a1SMi Jinlong 	if (nfsd_create_is_exclusive(createmode)) {
1472c397852cSPeter Staubach 		/* Cram the verifier into atime/mtime */
14731da177e4SLinus Torvalds 		iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1474c397852cSPeter Staubach 			| ATTR_MTIME_SET|ATTR_ATIME_SET;
14751da177e4SLinus Torvalds 		/* XXX someone who knows this better please fix it for nsec */
14761da177e4SLinus Torvalds 		iap->ia_mtime.tv_sec = v_mtime;
14771da177e4SLinus Torvalds 		iap->ia_atime.tv_sec = v_atime;
14781da177e4SLinus Torvalds 		iap->ia_mtime.tv_nsec = 0;
14791da177e4SLinus Torvalds 		iap->ia_atime.tv_nsec = 0;
14801da177e4SLinus Torvalds 	}
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds  set_attr:
1483f501912aSBen Myers 	err = nfsd_create_setattr(rqstp, resfhp, iap);
1484f501912aSBen Myers 
1485f501912aSBen Myers 	/*
14860f3a24b4STrond Myklebust 	 * nfsd_create_setattr already committed the child
14870f3a24b4STrond Myklebust 	 * (and possibly also the parent).
1488f501912aSBen Myers 	 */
1489f501912aSBen Myers 	if (!err)
1490f501912aSBen Myers 		err = nfserrno(commit_metadata(fhp));
1491f193fbabSYAMAMOTO Takashi 
1492f193fbabSYAMAMOTO Takashi 	/*
1493f193fbabSYAMAMOTO Takashi 	 * Update the filehandle to get the new inode info.
1494f193fbabSYAMAMOTO Takashi 	 */
1495f193fbabSYAMAMOTO Takashi 	if (!err)
1496f193fbabSYAMAMOTO Takashi 		err = fh_update(resfhp);
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds  out:
14991da177e4SLinus Torvalds 	fh_unlock(fhp);
15001da177e4SLinus Torvalds 	if (dchild && !IS_ERR(dchild))
15011da177e4SLinus Torvalds 		dput(dchild);
15024a55c101SJan Kara 	fh_drop_write(fhp);
15031da177e4SLinus Torvalds  	return err;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds  out_nfserr:
15066264d69dSAl Viro 	err = nfserrno(host_err);
15071da177e4SLinus Torvalds 	goto out;
15081da177e4SLinus Torvalds }
15091da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds /*
15121da177e4SLinus Torvalds  * Read a symlink. On entry, *lenp must contain the maximum path length that
15131da177e4SLinus Torvalds  * fits into the buffer. On return, it contains the true length.
15141da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
15151da177e4SLinus Torvalds  */
15166264d69dSAl Viro __be32
15171da177e4SLinus Torvalds nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
15181da177e4SLinus Torvalds {
15196264d69dSAl Viro 	__be32		err;
15204d7edbc3SAl Viro 	const char *link;
152168ac1234SAl Viro 	struct path path;
15224d7edbc3SAl Viro 	DEFINE_DELAYED_CALL(done);
15234d7edbc3SAl Viro 	int len;
15241da177e4SLinus Torvalds 
15258837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
15264d7edbc3SAl Viro 	if (unlikely(err))
15274d7edbc3SAl Viro 		return err;
15281da177e4SLinus Torvalds 
152968ac1234SAl Viro 	path.mnt = fhp->fh_export->ex_path.mnt;
153068ac1234SAl Viro 	path.dentry = fhp->fh_dentry;
15311da177e4SLinus Torvalds 
15324d7edbc3SAl Viro 	if (unlikely(!d_is_symlink(path.dentry)))
15334d7edbc3SAl Viro 		return nfserr_inval;
15341da177e4SLinus Torvalds 
153568ac1234SAl Viro 	touch_atime(&path);
15361da177e4SLinus Torvalds 
15374d7edbc3SAl Viro 	link = vfs_get_link(path.dentry, &done);
15384d7edbc3SAl Viro 	if (IS_ERR(link))
15394d7edbc3SAl Viro 		return nfserrno(PTR_ERR(link));
15401da177e4SLinus Torvalds 
15414d7edbc3SAl Viro 	len = strlen(link);
15424d7edbc3SAl Viro 	if (len < *lenp)
15434d7edbc3SAl Viro 		*lenp = len;
15444d7edbc3SAl Viro 	memcpy(buf, link, *lenp);
15454d7edbc3SAl Viro 	do_delayed_call(&done);
15464d7edbc3SAl Viro 	return 0;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds /*
15501da177e4SLinus Torvalds  * Create a symlink and look up its inode
15511da177e4SLinus Torvalds  * N.B. After this call _both_ fhp and resfhp need an fh_put
15521da177e4SLinus Torvalds  */
15536264d69dSAl Viro __be32
15541da177e4SLinus Torvalds nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
15551da177e4SLinus Torvalds 				char *fname, int flen,
155652ee0433SJ. Bruce Fields 				char *path,
15571e444f5bSKinglong Mee 				struct svc_fh *resfhp)
15581da177e4SLinus Torvalds {
15591da177e4SLinus Torvalds 	struct dentry	*dentry, *dnew;
15606264d69dSAl Viro 	__be32		err, cerr;
15616264d69dSAl Viro 	int		host_err;
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	err = nfserr_noent;
156452ee0433SJ. Bruce Fields 	if (!flen || path[0] == '\0')
15651da177e4SLinus Torvalds 		goto out;
15661da177e4SLinus Torvalds 	err = nfserr_exist;
15671da177e4SLinus Torvalds 	if (isdotent(fname, flen))
15681da177e4SLinus Torvalds 		goto out;
15691da177e4SLinus Torvalds 
15708837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
15711da177e4SLinus Torvalds 	if (err)
15721da177e4SLinus Torvalds 		goto out;
15734a55c101SJan Kara 
15744a55c101SJan Kara 	host_err = fh_want_write(fhp);
15754a55c101SJan Kara 	if (host_err)
15764a55c101SJan Kara 		goto out_nfserr;
15774a55c101SJan Kara 
15781da177e4SLinus Torvalds 	fh_lock(fhp);
15791da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
15801da177e4SLinus Torvalds 	dnew = lookup_one_len(fname, dentry, flen);
15816264d69dSAl Viro 	host_err = PTR_ERR(dnew);
15821da177e4SLinus Torvalds 	if (IS_ERR(dnew))
15831da177e4SLinus Torvalds 		goto out_nfserr;
15841da177e4SLinus Torvalds 
15852b0143b5SDavid Howells 	host_err = vfs_symlink(d_inode(dentry), dnew, path);
15866264d69dSAl Viro 	err = nfserrno(host_err);
1587f501912aSBen Myers 	if (!err)
1588f501912aSBen Myers 		err = nfserrno(commit_metadata(fhp));
15891da177e4SLinus Torvalds 	fh_unlock(fhp);
15901da177e4SLinus Torvalds 
1591bad0dcffSAl Viro 	fh_drop_write(fhp);
159275c3f29dSDave Hansen 
15931da177e4SLinus Torvalds 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
15941da177e4SLinus Torvalds 	dput(dnew);
15951da177e4SLinus Torvalds 	if (err==0) err = cerr;
15961da177e4SLinus Torvalds out:
15971da177e4SLinus Torvalds 	return err;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds out_nfserr:
16006264d69dSAl Viro 	err = nfserrno(host_err);
16011da177e4SLinus Torvalds 	goto out;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds /*
16051da177e4SLinus Torvalds  * Create a hardlink
16061da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
16071da177e4SLinus Torvalds  */
16086264d69dSAl Viro __be32
16091da177e4SLinus Torvalds nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
16101da177e4SLinus Torvalds 				char *name, int len, struct svc_fh *tfhp)
16111da177e4SLinus Torvalds {
16121da177e4SLinus Torvalds 	struct dentry	*ddir, *dnew, *dold;
161355b13354SJ. Bruce Fields 	struct inode	*dirp;
16146264d69dSAl Viro 	__be32		err;
16156264d69dSAl Viro 	int		host_err;
16161da177e4SLinus Torvalds 
16178837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
16181da177e4SLinus Torvalds 	if (err)
16191da177e4SLinus Torvalds 		goto out;
16207d818a7bSJ. Bruce Fields 	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
16211da177e4SLinus Torvalds 	if (err)
16221da177e4SLinus Torvalds 		goto out;
16237d818a7bSJ. Bruce Fields 	err = nfserr_isdir;
1624e36cb0b8SDavid Howells 	if (d_is_dir(tfhp->fh_dentry))
16257d818a7bSJ. Bruce Fields 		goto out;
16261da177e4SLinus Torvalds 	err = nfserr_perm;
16271da177e4SLinus Torvalds 	if (!len)
16281da177e4SLinus Torvalds 		goto out;
16291da177e4SLinus Torvalds 	err = nfserr_exist;
16301da177e4SLinus Torvalds 	if (isdotent(name, len))
16311da177e4SLinus Torvalds 		goto out;
16321da177e4SLinus Torvalds 
16334a55c101SJan Kara 	host_err = fh_want_write(tfhp);
16344a55c101SJan Kara 	if (host_err) {
16354a55c101SJan Kara 		err = nfserrno(host_err);
16364a55c101SJan Kara 		goto out;
16374a55c101SJan Kara 	}
16384a55c101SJan Kara 
163912fd3520SPeter Zijlstra 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
16401da177e4SLinus Torvalds 	ddir = ffhp->fh_dentry;
16412b0143b5SDavid Howells 	dirp = d_inode(ddir);
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	dnew = lookup_one_len(name, ddir, len);
16446264d69dSAl Viro 	host_err = PTR_ERR(dnew);
16451da177e4SLinus Torvalds 	if (IS_ERR(dnew))
16461da177e4SLinus Torvalds 		goto out_nfserr;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	dold = tfhp->fh_dentry;
16491da177e4SLinus Torvalds 
16504795bb37SJ. Bruce Fields 	err = nfserr_noent;
16512b0143b5SDavid Howells 	if (d_really_is_negative(dold))
16524a55c101SJan Kara 		goto out_dput;
1653146a8595SJ. Bruce Fields 	host_err = vfs_link(dold, dirp, dnew, NULL);
16546264d69dSAl Viro 	if (!host_err) {
1655f501912aSBen Myers 		err = nfserrno(commit_metadata(ffhp));
1656f501912aSBen Myers 		if (!err)
1657f501912aSBen Myers 			err = nfserrno(commit_metadata(tfhp));
16581da177e4SLinus Torvalds 	} else {
16596264d69dSAl Viro 		if (host_err == -EXDEV && rqstp->rq_vers == 2)
16601da177e4SLinus Torvalds 			err = nfserr_acces;
16611da177e4SLinus Torvalds 		else
16626264d69dSAl Viro 			err = nfserrno(host_err);
16631da177e4SLinus Torvalds 	}
166475c3f29dSDave Hansen out_dput:
16651da177e4SLinus Torvalds 	dput(dnew);
1666270d56e5SDavid M. Richter out_unlock:
1667270d56e5SDavid M. Richter 	fh_unlock(ffhp);
16684a55c101SJan Kara 	fh_drop_write(tfhp);
16691da177e4SLinus Torvalds out:
16701da177e4SLinus Torvalds 	return err;
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds out_nfserr:
16736264d69dSAl Viro 	err = nfserrno(host_err);
1674270d56e5SDavid M. Richter 	goto out_unlock;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
16777775ec57SJeff Layton static void
16787775ec57SJeff Layton nfsd_close_cached_files(struct dentry *dentry)
16797775ec57SJeff Layton {
16807775ec57SJeff Layton 	struct inode *inode = d_inode(dentry);
16817775ec57SJeff Layton 
16827775ec57SJeff Layton 	if (inode && S_ISREG(inode->i_mode))
16837775ec57SJeff Layton 		nfsd_file_close_inode_sync(inode);
16847775ec57SJeff Layton }
16857775ec57SJeff Layton 
16867775ec57SJeff Layton static bool
16877775ec57SJeff Layton nfsd_has_cached_files(struct dentry *dentry)
16887775ec57SJeff Layton {
16897775ec57SJeff Layton 	bool		ret = false;
16907775ec57SJeff Layton 	struct inode *inode = d_inode(dentry);
16917775ec57SJeff Layton 
16927775ec57SJeff Layton 	if (inode && S_ISREG(inode->i_mode))
16937775ec57SJeff Layton 		ret = nfsd_file_is_cached(inode);
16947775ec57SJeff Layton 	return ret;
16957775ec57SJeff Layton }
16967775ec57SJeff Layton 
16971da177e4SLinus Torvalds /*
16981da177e4SLinus Torvalds  * Rename a file
16991da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
17001da177e4SLinus Torvalds  */
17016264d69dSAl Viro __be32
17021da177e4SLinus Torvalds nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
17031da177e4SLinus Torvalds 			    struct svc_fh *tfhp, char *tname, int tlen)
17041da177e4SLinus Torvalds {
17051da177e4SLinus Torvalds 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
17061da177e4SLinus Torvalds 	struct inode	*fdir, *tdir;
17076264d69dSAl Viro 	__be32		err;
17086264d69dSAl Viro 	int		host_err;
17097775ec57SJeff Layton 	bool		has_cached = false;
17101da177e4SLinus Torvalds 
17118837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
17121da177e4SLinus Torvalds 	if (err)
17131da177e4SLinus Torvalds 		goto out;
17148837abcaSMiklos Szeredi 	err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
17151da177e4SLinus Torvalds 	if (err)
17161da177e4SLinus Torvalds 		goto out;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	fdentry = ffhp->fh_dentry;
17192b0143b5SDavid Howells 	fdir = d_inode(fdentry);
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	tdentry = tfhp->fh_dentry;
17222b0143b5SDavid Howells 	tdir = d_inode(tdentry);
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	err = nfserr_perm;
17251da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
17261da177e4SLinus Torvalds 		goto out;
17271da177e4SLinus Torvalds 
17287775ec57SJeff Layton retry:
17294a55c101SJan Kara 	host_err = fh_want_write(ffhp);
17304a55c101SJan Kara 	if (host_err) {
17314a55c101SJan Kara 		err = nfserrno(host_err);
17324a55c101SJan Kara 		goto out;
17334a55c101SJan Kara 	}
17344a55c101SJan Kara 
17351da177e4SLinus Torvalds 	/* cannot use fh_lock as we need deadlock protective ordering
17361da177e4SLinus Torvalds 	 * so do it by hand */
17371da177e4SLinus Torvalds 	trap = lock_rename(tdentry, fdentry);
1738aaf91ec1SJeff Layton 	ffhp->fh_locked = tfhp->fh_locked = true;
17391da177e4SLinus Torvalds 	fill_pre_wcc(ffhp);
17401da177e4SLinus Torvalds 	fill_pre_wcc(tfhp);
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 	odentry = lookup_one_len(fname, fdentry, flen);
17436264d69dSAl Viro 	host_err = PTR_ERR(odentry);
17441da177e4SLinus Torvalds 	if (IS_ERR(odentry))
17451da177e4SLinus Torvalds 		goto out_nfserr;
17461da177e4SLinus Torvalds 
17476264d69dSAl Viro 	host_err = -ENOENT;
17482b0143b5SDavid Howells 	if (d_really_is_negative(odentry))
17491da177e4SLinus Torvalds 		goto out_dput_old;
17506264d69dSAl Viro 	host_err = -EINVAL;
17511da177e4SLinus Torvalds 	if (odentry == trap)
17521da177e4SLinus Torvalds 		goto out_dput_old;
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	ndentry = lookup_one_len(tname, tdentry, tlen);
17556264d69dSAl Viro 	host_err = PTR_ERR(ndentry);
17561da177e4SLinus Torvalds 	if (IS_ERR(ndentry))
17571da177e4SLinus Torvalds 		goto out_dput_old;
17586264d69dSAl Viro 	host_err = -ENOTEMPTY;
17591da177e4SLinus Torvalds 	if (ndentry == trap)
17601da177e4SLinus Torvalds 		goto out_dput_new;
17611da177e4SLinus Torvalds 
17629079b1ebSDave Hansen 	host_err = -EXDEV;
17639079b1ebSDave Hansen 	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
17649079b1ebSDave Hansen 		goto out_dput_new;
1765aa387d6cSJ. Bruce Fields 	if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1766aa387d6cSJ. Bruce Fields 		goto out_dput_new;
17679079b1ebSDave Hansen 
17687775ec57SJeff Layton 	if (nfsd_has_cached_files(ndentry)) {
17697775ec57SJeff Layton 		has_cached = true;
17707775ec57SJeff Layton 		goto out_dput_old;
17717775ec57SJeff Layton 	} else {
1772520c8b16SMiklos Szeredi 		host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
1773f501912aSBen Myers 		if (!host_err) {
1774f501912aSBen Myers 			host_err = commit_metadata(tfhp);
17756264d69dSAl Viro 			if (!host_err)
1776f501912aSBen Myers 				host_err = commit_metadata(ffhp);
17771da177e4SLinus Torvalds 		}
17787775ec57SJeff Layton 	}
17791da177e4SLinus Torvalds  out_dput_new:
17801da177e4SLinus Torvalds 	dput(ndentry);
17811da177e4SLinus Torvalds  out_dput_old:
17821da177e4SLinus Torvalds 	dput(odentry);
17831da177e4SLinus Torvalds  out_nfserr:
17846264d69dSAl Viro 	err = nfserrno(host_err);
1785fbb74a34SJ. Bruce Fields 	/*
1786fbb74a34SJ. Bruce Fields 	 * We cannot rely on fh_unlock on the two filehandles,
17871da177e4SLinus Torvalds 	 * as that would do the wrong thing if the two directories
1788fbb74a34SJ. Bruce Fields 	 * were the same, so again we do it by hand.
17891da177e4SLinus Torvalds 	 */
17907775ec57SJeff Layton 	if (!has_cached) {
17911da177e4SLinus Torvalds 		fill_post_wcc(ffhp);
17921da177e4SLinus Torvalds 		fill_post_wcc(tfhp);
17937775ec57SJeff Layton 	}
17941da177e4SLinus Torvalds 	unlock_rename(tdentry, fdentry);
1795aaf91ec1SJeff Layton 	ffhp->fh_locked = tfhp->fh_locked = false;
17964a55c101SJan Kara 	fh_drop_write(ffhp);
17971da177e4SLinus Torvalds 
17987775ec57SJeff Layton 	/*
17997775ec57SJeff Layton 	 * If the target dentry has cached open files, then we need to try to
18007775ec57SJeff Layton 	 * close them prior to doing the rename. Flushing delayed fput
18017775ec57SJeff Layton 	 * shouldn't be done with locks held however, so we delay it until this
18027775ec57SJeff Layton 	 * point and then reattempt the whole shebang.
18037775ec57SJeff Layton 	 */
18047775ec57SJeff Layton 	if (has_cached) {
18057775ec57SJeff Layton 		has_cached = false;
18067775ec57SJeff Layton 		nfsd_close_cached_files(ndentry);
18077775ec57SJeff Layton 		dput(ndentry);
18087775ec57SJeff Layton 		goto retry;
18097775ec57SJeff Layton 	}
18101da177e4SLinus Torvalds out:
18111da177e4SLinus Torvalds 	return err;
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds /*
18151da177e4SLinus Torvalds  * Unlink a file or directory
18161da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
18171da177e4SLinus Torvalds  */
18186264d69dSAl Viro __be32
18191da177e4SLinus Torvalds nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
18201da177e4SLinus Torvalds 				char *fname, int flen)
18211da177e4SLinus Torvalds {
18221da177e4SLinus Torvalds 	struct dentry	*dentry, *rdentry;
18231da177e4SLinus Torvalds 	struct inode	*dirp;
18246264d69dSAl Viro 	__be32		err;
18256264d69dSAl Viro 	int		host_err;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	err = nfserr_acces;
18281da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen))
18291da177e4SLinus Torvalds 		goto out;
18308837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
18311da177e4SLinus Torvalds 	if (err)
18321da177e4SLinus Torvalds 		goto out;
18331da177e4SLinus Torvalds 
18344a55c101SJan Kara 	host_err = fh_want_write(fhp);
18354a55c101SJan Kara 	if (host_err)
18364a55c101SJan Kara 		goto out_nfserr;
18374a55c101SJan Kara 
183812fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
18391da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
18402b0143b5SDavid Howells 	dirp = d_inode(dentry);
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	rdentry = lookup_one_len(fname, dentry, flen);
18436264d69dSAl Viro 	host_err = PTR_ERR(rdentry);
18441da177e4SLinus Torvalds 	if (IS_ERR(rdentry))
18450ca0c9d7SJ. Bruce Fields 		goto out_drop_write;
18461da177e4SLinus Torvalds 
18472b0143b5SDavid Howells 	if (d_really_is_negative(rdentry)) {
18481da177e4SLinus Torvalds 		dput(rdentry);
18490ca0c9d7SJ. Bruce Fields 		host_err = -ENOENT;
18500ca0c9d7SJ. Bruce Fields 		goto out_drop_write;
18511da177e4SLinus Torvalds 	}
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	if (!type)
18542b0143b5SDavid Howells 		type = d_inode(rdentry)->i_mode & S_IFMT;
18551da177e4SLinus Torvalds 
18567775ec57SJeff Layton 	if (type != S_IFDIR) {
18577775ec57SJeff Layton 		nfsd_close_cached_files(rdentry);
1858b21996e3SJ. Bruce Fields 		host_err = vfs_unlink(dirp, rdentry, NULL);
18597775ec57SJeff Layton 	} else {
18606264d69dSAl Viro 		host_err = vfs_rmdir(dirp, rdentry);
18617775ec57SJeff Layton 	}
18627775ec57SJeff Layton 
1863541ce98cSJ. Bruce Fields 	if (!host_err)
1864541ce98cSJ. Bruce Fields 		host_err = commit_metadata(fhp);
18651da177e4SLinus Torvalds 	dput(rdentry);
18661da177e4SLinus Torvalds 
18670ca0c9d7SJ. Bruce Fields out_drop_write:
18680ca0c9d7SJ. Bruce Fields 	fh_drop_write(fhp);
18691da177e4SLinus Torvalds out_nfserr:
1870466e16f0SNeilBrown 	if (host_err == -EBUSY) {
1871466e16f0SNeilBrown 		/* name is mounted-on. There is no perfect
1872466e16f0SNeilBrown 		 * error status.
1873466e16f0SNeilBrown 		 */
1874466e16f0SNeilBrown 		if (nfsd_v4client(rqstp))
1875466e16f0SNeilBrown 			err = nfserr_file_open;
1876466e16f0SNeilBrown 		else
1877466e16f0SNeilBrown 			err = nfserr_acces;
1878466e16f0SNeilBrown 	} else {
18796264d69dSAl Viro 		err = nfserrno(host_err);
1880466e16f0SNeilBrown 	}
1881f193fbabSYAMAMOTO Takashi out:
1882f193fbabSYAMAMOTO Takashi 	return err;
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds /*
188614f7dd63SDavid Woodhouse  * We do this buffering because we must not call back into the file
188714f7dd63SDavid Woodhouse  * system's ->lookup() method from the filldir callback. That may well
188814f7dd63SDavid Woodhouse  * deadlock a number of file systems.
188914f7dd63SDavid Woodhouse  *
189014f7dd63SDavid Woodhouse  * This is based heavily on the implementation of same in XFS.
189114f7dd63SDavid Woodhouse  */
189214f7dd63SDavid Woodhouse struct buffered_dirent {
189314f7dd63SDavid Woodhouse 	u64		ino;
189414f7dd63SDavid Woodhouse 	loff_t		offset;
189514f7dd63SDavid Woodhouse 	int		namlen;
189614f7dd63SDavid Woodhouse 	unsigned int	d_type;
189714f7dd63SDavid Woodhouse 	char		name[];
189814f7dd63SDavid Woodhouse };
189914f7dd63SDavid Woodhouse 
190014f7dd63SDavid Woodhouse struct readdir_data {
19015c0ba4e0SAl Viro 	struct dir_context ctx;
190214f7dd63SDavid Woodhouse 	char		*dirent;
190314f7dd63SDavid Woodhouse 	size_t		used;
190453c9c5c0SAl Viro 	int		full;
190514f7dd63SDavid Woodhouse };
190614f7dd63SDavid Woodhouse 
1907ac7576f4SMiklos Szeredi static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1908ac7576f4SMiklos Szeredi 				 int namlen, loff_t offset, u64 ino,
1909ac7576f4SMiklos Szeredi 				 unsigned int d_type)
191014f7dd63SDavid Woodhouse {
1911ac7576f4SMiklos Szeredi 	struct readdir_data *buf =
1912ac7576f4SMiklos Szeredi 		container_of(ctx, struct readdir_data, ctx);
191314f7dd63SDavid Woodhouse 	struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
191414f7dd63SDavid Woodhouse 	unsigned int reclen;
191514f7dd63SDavid Woodhouse 
191614f7dd63SDavid Woodhouse 	reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
191753c9c5c0SAl Viro 	if (buf->used + reclen > PAGE_SIZE) {
191853c9c5c0SAl Viro 		buf->full = 1;
191914f7dd63SDavid Woodhouse 		return -EINVAL;
192053c9c5c0SAl Viro 	}
192114f7dd63SDavid Woodhouse 
192214f7dd63SDavid Woodhouse 	de->namlen = namlen;
192314f7dd63SDavid Woodhouse 	de->offset = offset;
192414f7dd63SDavid Woodhouse 	de->ino = ino;
192514f7dd63SDavid Woodhouse 	de->d_type = d_type;
192614f7dd63SDavid Woodhouse 	memcpy(de->name, name, namlen);
192714f7dd63SDavid Woodhouse 	buf->used += reclen;
192814f7dd63SDavid Woodhouse 
192914f7dd63SDavid Woodhouse 	return 0;
193014f7dd63SDavid Woodhouse }
193114f7dd63SDavid Woodhouse 
1932ac7576f4SMiklos Szeredi static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
19332628b766SDavid Woodhouse 				    struct readdir_cd *cdp, loff_t *offsetp)
19342628b766SDavid Woodhouse {
193514f7dd63SDavid Woodhouse 	struct buffered_dirent *de;
19362628b766SDavid Woodhouse 	int host_err;
193714f7dd63SDavid Woodhouse 	int size;
193814f7dd63SDavid Woodhouse 	loff_t offset;
1939ac6614b7SAl Viro 	struct readdir_data buf = {
1940ac6614b7SAl Viro 		.ctx.actor = nfsd_buffered_filldir,
1941ac6614b7SAl Viro 		.dirent = (void *)__get_free_page(GFP_KERNEL)
1942ac6614b7SAl Viro 	};
19432628b766SDavid Woodhouse 
194414f7dd63SDavid Woodhouse 	if (!buf.dirent)
19452f9092e1SDavid Woodhouse 		return nfserrno(-ENOMEM);
194614f7dd63SDavid Woodhouse 
194714f7dd63SDavid Woodhouse 	offset = *offsetp;
19482628b766SDavid Woodhouse 
194914f7dd63SDavid Woodhouse 	while (1) {
195014f7dd63SDavid Woodhouse 		unsigned int reclen;
195114f7dd63SDavid Woodhouse 
1952b726e923SDoug Nazar 		cdp->err = nfserr_eof; /* will be cleared on successful read */
195314f7dd63SDavid Woodhouse 		buf.used = 0;
195453c9c5c0SAl Viro 		buf.full = 0;
195514f7dd63SDavid Woodhouse 
19565c0ba4e0SAl Viro 		host_err = iterate_dir(file, &buf.ctx);
195753c9c5c0SAl Viro 		if (buf.full)
195853c9c5c0SAl Viro 			host_err = 0;
195953c9c5c0SAl Viro 
196053c9c5c0SAl Viro 		if (host_err < 0)
196114f7dd63SDavid Woodhouse 			break;
196214f7dd63SDavid Woodhouse 
196314f7dd63SDavid Woodhouse 		size = buf.used;
196414f7dd63SDavid Woodhouse 
196514f7dd63SDavid Woodhouse 		if (!size)
196614f7dd63SDavid Woodhouse 			break;
196714f7dd63SDavid Woodhouse 
196814f7dd63SDavid Woodhouse 		de = (struct buffered_dirent *)buf.dirent;
196914f7dd63SDavid Woodhouse 		while (size > 0) {
197014f7dd63SDavid Woodhouse 			offset = de->offset;
197114f7dd63SDavid Woodhouse 
197214f7dd63SDavid Woodhouse 			if (func(cdp, de->name, de->namlen, de->offset,
197314f7dd63SDavid Woodhouse 				 de->ino, de->d_type))
19742f9092e1SDavid Woodhouse 				break;
197514f7dd63SDavid Woodhouse 
197614f7dd63SDavid Woodhouse 			if (cdp->err != nfs_ok)
19772f9092e1SDavid Woodhouse 				break;
197814f7dd63SDavid Woodhouse 
197914f7dd63SDavid Woodhouse 			reclen = ALIGN(sizeof(*de) + de->namlen,
198014f7dd63SDavid Woodhouse 				       sizeof(u64));
198114f7dd63SDavid Woodhouse 			size -= reclen;
198214f7dd63SDavid Woodhouse 			de = (struct buffered_dirent *)((char *)de + reclen);
198314f7dd63SDavid Woodhouse 		}
19842f9092e1SDavid Woodhouse 		if (size > 0) /* We bailed out early */
19852f9092e1SDavid Woodhouse 			break;
19862f9092e1SDavid Woodhouse 
1987c002a6c7SDavid Woodhouse 		offset = vfs_llseek(file, 0, SEEK_CUR);
198814f7dd63SDavid Woodhouse 	}
198914f7dd63SDavid Woodhouse 
199014f7dd63SDavid Woodhouse 	free_page((unsigned long)(buf.dirent));
19912628b766SDavid Woodhouse 
19922628b766SDavid Woodhouse 	if (host_err)
19932628b766SDavid Woodhouse 		return nfserrno(host_err);
199414f7dd63SDavid Woodhouse 
199514f7dd63SDavid Woodhouse 	*offsetp = offset;
19962628b766SDavid Woodhouse 	return cdp->err;
19972628b766SDavid Woodhouse }
19982628b766SDavid Woodhouse 
19991da177e4SLinus Torvalds /*
20001da177e4SLinus Torvalds  * Read entries from a directory.
20011da177e4SLinus Torvalds  * The  NFSv3/4 verifier we ignore for now.
20021da177e4SLinus Torvalds  */
20036264d69dSAl Viro __be32
20041da177e4SLinus Torvalds nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2005ac7576f4SMiklos Szeredi 	     struct readdir_cd *cdp, nfsd_filldir_t func)
20061da177e4SLinus Torvalds {
20076264d69dSAl Viro 	__be32		err;
20081da177e4SLinus Torvalds 	struct file	*file;
20091da177e4SLinus Torvalds 	loff_t		offset = *offsetp;
201006effdbbSBernd Schubert 	int             may_flags = NFSD_MAY_READ;
20111da177e4SLinus Torvalds 
201206effdbbSBernd Schubert 	/* NFSv2 only supports 32 bit cookies */
201306effdbbSBernd Schubert 	if (rqstp->rq_vers > 2)
201406effdbbSBernd Schubert 		may_flags |= NFSD_MAY_64BIT_COOKIE;
201506effdbbSBernd Schubert 
201606effdbbSBernd Schubert 	err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
20171da177e4SLinus Torvalds 	if (err)
20181da177e4SLinus Torvalds 		goto out;
20191da177e4SLinus Torvalds 
2020b108fe6bSJeff Layton 	offset = vfs_llseek(file, offset, SEEK_SET);
20211da177e4SLinus Torvalds 	if (offset < 0) {
20221da177e4SLinus Torvalds 		err = nfserrno((int)offset);
20231da177e4SLinus Torvalds 		goto out_close;
20241da177e4SLinus Torvalds 	}
20251da177e4SLinus Torvalds 
202614f7dd63SDavid Woodhouse 	err = nfsd_buffered_readdir(file, func, cdp, offsetp);
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	if (err == nfserr_eof || err == nfserr_toosmall)
20291da177e4SLinus Torvalds 		err = nfs_ok; /* can still be found in ->err */
20301da177e4SLinus Torvalds out_close:
2031fd891454SChristoph Hellwig 	fput(file);
20321da177e4SLinus Torvalds out:
20331da177e4SLinus Torvalds 	return err;
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds /*
20371da177e4SLinus Torvalds  * Get file system stats
20381da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
20391da177e4SLinus Torvalds  */
20406264d69dSAl Viro __be32
204104716e66SJ. Bruce Fields nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
20421da177e4SLinus Torvalds {
2043f6360efbSTakashi Iwai 	__be32 err;
2044f6360efbSTakashi Iwai 
2045f6360efbSTakashi Iwai 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2046f6360efbSTakashi Iwai 	if (!err) {
2047ebabe9a9SChristoph Hellwig 		struct path path = {
2048ebabe9a9SChristoph Hellwig 			.mnt	= fhp->fh_export->ex_path.mnt,
2049ebabe9a9SChristoph Hellwig 			.dentry	= fhp->fh_dentry,
2050ebabe9a9SChristoph Hellwig 		};
2051f6360efbSTakashi Iwai 		if (vfs_statfs(&path, stat))
20521da177e4SLinus Torvalds 			err = nfserr_io;
2053f6360efbSTakashi Iwai 	}
20541da177e4SLinus Torvalds 	return err;
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
2057c7d51402SJ. Bruce Fields static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2058e22841c6SJ. Bruce Fields {
2059c7d51402SJ. Bruce Fields 	return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2060e22841c6SJ. Bruce Fields }
2061e22841c6SJ. Bruce Fields 
20621da177e4SLinus Torvalds /*
20631da177e4SLinus Torvalds  * Check for a user's access permissions to this inode.
20641da177e4SLinus Torvalds  */
20656264d69dSAl Viro __be32
20660ec757dfSJ. Bruce Fields nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
20670ec757dfSJ. Bruce Fields 					struct dentry *dentry, int acc)
20681da177e4SLinus Torvalds {
20692b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
20701da177e4SLinus Torvalds 	int		err;
20711da177e4SLinus Torvalds 
2072aea93397SJ. Bruce Fields 	if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
20731da177e4SLinus Torvalds 		return 0;
20741da177e4SLinus Torvalds #if 0
20751da177e4SLinus Torvalds 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
20761da177e4SLinus Torvalds 		acc,
20778837abcaSMiklos Szeredi 		(acc & NFSD_MAY_READ)?	" read"  : "",
20788837abcaSMiklos Szeredi 		(acc & NFSD_MAY_WRITE)?	" write" : "",
20798837abcaSMiklos Szeredi 		(acc & NFSD_MAY_EXEC)?	" exec"  : "",
20808837abcaSMiklos Szeredi 		(acc & NFSD_MAY_SATTR)?	" sattr" : "",
20818837abcaSMiklos Szeredi 		(acc & NFSD_MAY_TRUNC)?	" trunc" : "",
20828837abcaSMiklos Szeredi 		(acc & NFSD_MAY_LOCK)?	" lock"  : "",
20838837abcaSMiklos Szeredi 		(acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
20841da177e4SLinus Torvalds 		inode->i_mode,
20851da177e4SLinus Torvalds 		IS_IMMUTABLE(inode)?	" immut" : "",
20861da177e4SLinus Torvalds 		IS_APPEND(inode)?	" append" : "",
20872c463e95SDave Hansen 		__mnt_is_readonly(exp->ex_path.mnt)?	" ro" : "");
20881da177e4SLinus Torvalds 	dprintk("      owner %d/%d user %d/%d\n",
20895cc0a840SDavid Howells 		inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
20901da177e4SLinus Torvalds #endif
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	/* Normally we reject any write/sattr etc access on a read-only file
20931da177e4SLinus Torvalds 	 * system.  But if it is IRIX doing check on write-access for a
20941da177e4SLinus Torvalds 	 * device special file, we ignore rofs.
20951da177e4SLinus Torvalds 	 */
20968837abcaSMiklos Szeredi 	if (!(acc & NFSD_MAY_LOCAL_ACCESS))
20978837abcaSMiklos Szeredi 		if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
20982c463e95SDave Hansen 			if (exp_rdonly(rqstp, exp) ||
20992c463e95SDave Hansen 			    __mnt_is_readonly(exp->ex_path.mnt))
21001da177e4SLinus Torvalds 				return nfserr_rofs;
21018837abcaSMiklos Szeredi 			if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
21021da177e4SLinus Torvalds 				return nfserr_perm;
21031da177e4SLinus Torvalds 		}
21048837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
21051da177e4SLinus Torvalds 		return nfserr_perm;
21061da177e4SLinus Torvalds 
21078837abcaSMiklos Szeredi 	if (acc & NFSD_MAY_LOCK) {
21081da177e4SLinus Torvalds 		/* If we cannot rely on authentication in NLM requests,
21091da177e4SLinus Torvalds 		 * just allow locks, otherwise require read permission, or
21101da177e4SLinus Torvalds 		 * ownership
21111da177e4SLinus Torvalds 		 */
21121da177e4SLinus Torvalds 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
21131da177e4SLinus Torvalds 			return 0;
21141da177e4SLinus Torvalds 		else
21158837abcaSMiklos Szeredi 			acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
21161da177e4SLinus Torvalds 	}
21171da177e4SLinus Torvalds 	/*
21181da177e4SLinus Torvalds 	 * The file owner always gets access permission for accesses that
21191da177e4SLinus Torvalds 	 * would normally be checked at open time. This is to make
21201da177e4SLinus Torvalds 	 * file access work even when the client has done a fchmod(fd, 0).
21211da177e4SLinus Torvalds 	 *
21221da177e4SLinus Torvalds 	 * However, `cp foo bar' should fail nevertheless when bar is
21231da177e4SLinus Torvalds 	 * readonly. A sensible way to do this might be to reject all
21241da177e4SLinus Torvalds 	 * attempts to truncate a read-only file, because a creat() call
21251da177e4SLinus Torvalds 	 * always implies file truncation.
21261da177e4SLinus Torvalds 	 * ... but this isn't really fair.  A process may reasonably call
21271da177e4SLinus Torvalds 	 * ftruncate on an open file descriptor on a file with perm 000.
21281da177e4SLinus Torvalds 	 * We must trust the client to do permission checking - using "ACCESS"
21291da177e4SLinus Torvalds 	 * with NFSv3.
21301da177e4SLinus Torvalds 	 */
21318837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
21326fab8779SEric W. Biederman 	    uid_eq(inode->i_uid, current_fsuid()))
21331da177e4SLinus Torvalds 		return 0;
21341da177e4SLinus Torvalds 
21358837abcaSMiklos Szeredi 	/* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2136f419a2e3SAl Viro 	err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	/* Allow read access to binaries even when mode 111 */
21391da177e4SLinus Torvalds 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
2140a043226bSJ. Bruce Fields 	     (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2141a043226bSJ. Bruce Fields 	      acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2142f419a2e3SAl Viro 		err = inode_permission(inode, MAY_EXEC);
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	return err? nfserrno(err) : 0;
21451da177e4SLinus Torvalds }
2146