xref: /linux/fs/nfsd/vfs.c (revision 52e380e0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * File operations used by nfsd. Some of these have been ripped from
31da177e4SLinus Torvalds  * other parts of the kernel because they weren't exported, others
41da177e4SLinus Torvalds  * are partial duplicates with added or changed functionality.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Note that several functions dget() the dentry upon which they want
71da177e4SLinus Torvalds  * to act, most notably those that create directory entries. Response
81da177e4SLinus Torvalds  * dentry's are dput()'d if necessary in the release callback.
91da177e4SLinus Torvalds  * So if you notice code paths that apparently fail to dput() the
101da177e4SLinus Torvalds  * dentry, don't worry--they have been taken care of.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
131da177e4SLinus Torvalds  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/fs.h>
171da177e4SLinus Torvalds #include <linux/file.h>
18d6b29d7cSJens Axboe #include <linux/splice.h>
1995d871f0SAnna Schumaker #include <linux/falloc.h>
201da177e4SLinus Torvalds #include <linux/fcntl.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/delay.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/posix_acl_xattr.h>
251da177e4SLinus Torvalds #include <linux/xattr.h>
269a74af21SBoaz Harrosh #include <linux/jhash.h>
279a74af21SBoaz Harrosh #include <linux/ima.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
297c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
30f501912aSBen Myers #include <linux/exportfs.h>
31f501912aSBen Myers #include <linux/writeback.h>
3218032ca0SDavid Quigley #include <linux/security.h>
339a74af21SBoaz Harrosh 
349a74af21SBoaz Harrosh #ifdef CONFIG_NFSD_V3
359a74af21SBoaz Harrosh #include "xdr3.h"
369a74af21SBoaz Harrosh #endif /* CONFIG_NFSD_V3 */
379a74af21SBoaz Harrosh 
385be196e5SChristoph Hellwig #ifdef CONFIG_NFSD_V4
39ffa0160aSChristoph Hellwig #include "../internal.h"
402ca72e17SJ. Bruce Fields #include "acl.h"
412ca72e17SJ. Bruce Fields #include "idmap.h"
421da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V4 */
431da177e4SLinus Torvalds 
449a74af21SBoaz Harrosh #include "nfsd.h"
459a74af21SBoaz Harrosh #include "vfs.h"
466e8b50d1SJeff Layton #include "trace.h"
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_FILEOP
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds /*
521da177e4SLinus Torvalds  * This is a cache of readahead params that help us choose the proper
531da177e4SLinus Torvalds  * readahead strategy. Initially, we set all readahead parameters to 0
541da177e4SLinus Torvalds  * and let the VFS handle things.
551da177e4SLinus Torvalds  * If you increase the number of cached files very much, you'll need to
561da177e4SLinus Torvalds  * add a hash table here.
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds struct raparms {
591da177e4SLinus Torvalds 	struct raparms		*p_next;
601da177e4SLinus Torvalds 	unsigned int		p_count;
611da177e4SLinus Torvalds 	ino_t			p_ino;
621da177e4SLinus Torvalds 	dev_t			p_dev;
631da177e4SLinus Torvalds 	int			p_set;
641da177e4SLinus Torvalds 	struct file_ra_state	p_ra;
65fce1456aSGreg Banks 	unsigned int		p_hindex;
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
68fce1456aSGreg Banks struct raparm_hbucket {
69fce1456aSGreg Banks 	struct raparms		*pb_head;
70fce1456aSGreg Banks 	spinlock_t		pb_lock;
71fce1456aSGreg Banks } ____cacheline_aligned_in_smp;
72fce1456aSGreg Banks 
73fce1456aSGreg Banks #define RAPARM_HASH_BITS	4
74fce1456aSGreg Banks #define RAPARM_HASH_SIZE	(1<<RAPARM_HASH_BITS)
75fce1456aSGreg Banks #define RAPARM_HASH_MASK	(RAPARM_HASH_SIZE-1)
76fce1456aSGreg Banks static struct raparm_hbucket	raparm_hash[RAPARM_HASH_SIZE];
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /*
791da177e4SLinus Torvalds  * Called from nfsd_lookup and encode_dirent. Check if we have crossed
801da177e4SLinus Torvalds  * a mount point.
81e0bb89efSJ.Bruce Fields  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
821da177e4SLinus Torvalds  *  or nfs_ok having possibly changed *dpp and *expp
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds int
851da177e4SLinus Torvalds nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
861da177e4SLinus Torvalds 		        struct svc_export **expp)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	struct svc_export *exp = *expp, *exp2 = NULL;
891da177e4SLinus Torvalds 	struct dentry *dentry = *dpp;
9091c9fa8fSAl Viro 	struct path path = {.mnt = mntget(exp->ex_path.mnt),
9191c9fa8fSAl Viro 			    .dentry = dget(dentry)};
926264d69dSAl Viro 	int err = 0;
931da177e4SLinus Torvalds 
947cc90cc3SAl Viro 	err = follow_down(&path);
95cc53ce53SDavid Howells 	if (err < 0)
96cc53ce53SDavid Howells 		goto out;
971da177e4SLinus Torvalds 
9891c9fa8fSAl Viro 	exp2 = rqst_exp_get_by_name(rqstp, &path);
991da177e4SLinus Torvalds 	if (IS_ERR(exp2)) {
1001da177e4SLinus Torvalds 		err = PTR_ERR(exp2);
1013b6cee7bSJ. Bruce Fields 		/*
1023b6cee7bSJ. Bruce Fields 		 * We normally allow NFS clients to continue
1033b6cee7bSJ. Bruce Fields 		 * "underneath" a mountpoint that is not exported.
1043b6cee7bSJ. Bruce Fields 		 * The exception is V4ROOT, where no traversal is ever
1053b6cee7bSJ. Bruce Fields 		 * allowed without an explicit export of the new
1063b6cee7bSJ. Bruce Fields 		 * directory.
1073b6cee7bSJ. Bruce Fields 		 */
1083b6cee7bSJ. Bruce Fields 		if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
1093b6cee7bSJ. Bruce Fields 			err = 0;
11091c9fa8fSAl Viro 		path_put(&path);
1111da177e4SLinus Torvalds 		goto out;
1121da177e4SLinus Torvalds 	}
1133c394ddaSSteve Dickson 	if (nfsd_v4client(rqstp) ||
1143c394ddaSSteve Dickson 		(exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
1151da177e4SLinus Torvalds 		/* successfully crossed mount point */
1161644ccc8SAl Viro 		/*
11791c9fa8fSAl Viro 		 * This is subtle: path.dentry is *not* on path.mnt
11891c9fa8fSAl Viro 		 * at this point.  The only reason we are safe is that
11991c9fa8fSAl Viro 		 * original mnt is pinned down by exp, so we should
12091c9fa8fSAl Viro 		 * put path *before* putting exp
1211644ccc8SAl Viro 		 */
12291c9fa8fSAl Viro 		*dpp = path.dentry;
12391c9fa8fSAl Viro 		path.dentry = dentry;
1241644ccc8SAl Viro 		*expp = exp2;
12591c9fa8fSAl Viro 		exp2 = exp;
1261da177e4SLinus Torvalds 	}
12791c9fa8fSAl Viro 	path_put(&path);
12891c9fa8fSAl Viro 	exp_put(exp2);
1291da177e4SLinus Torvalds out:
1301da177e4SLinus Torvalds 	return err;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
133289ede45SJ. Bruce Fields static void follow_to_parent(struct path *path)
134289ede45SJ. Bruce Fields {
135289ede45SJ. Bruce Fields 	struct dentry *dp;
136289ede45SJ. Bruce Fields 
137289ede45SJ. Bruce Fields 	while (path->dentry == path->mnt->mnt_root && follow_up(path))
138289ede45SJ. Bruce Fields 		;
139289ede45SJ. Bruce Fields 	dp = dget_parent(path->dentry);
140289ede45SJ. Bruce Fields 	dput(path->dentry);
141289ede45SJ. Bruce Fields 	path->dentry = dp;
142289ede45SJ. Bruce Fields }
143289ede45SJ. Bruce Fields 
144289ede45SJ. Bruce Fields static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
145289ede45SJ. Bruce Fields {
146289ede45SJ. Bruce Fields 	struct svc_export *exp2;
147289ede45SJ. Bruce Fields 	struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
148289ede45SJ. Bruce Fields 			    .dentry = dget(dparent)};
149289ede45SJ. Bruce Fields 
150289ede45SJ. Bruce Fields 	follow_to_parent(&path);
151289ede45SJ. Bruce Fields 
152289ede45SJ. Bruce Fields 	exp2 = rqst_exp_parent(rqstp, &path);
153289ede45SJ. Bruce Fields 	if (PTR_ERR(exp2) == -ENOENT) {
154289ede45SJ. Bruce Fields 		*dentryp = dget(dparent);
155289ede45SJ. Bruce Fields 	} else if (IS_ERR(exp2)) {
156289ede45SJ. Bruce Fields 		path_put(&path);
157289ede45SJ. Bruce Fields 		return PTR_ERR(exp2);
158289ede45SJ. Bruce Fields 	} else {
159289ede45SJ. Bruce Fields 		*dentryp = dget(path.dentry);
160289ede45SJ. Bruce Fields 		exp_put(*exp);
161289ede45SJ. Bruce Fields 		*exp = exp2;
162289ede45SJ. Bruce Fields 	}
163289ede45SJ. Bruce Fields 	path_put(&path);
164289ede45SJ. Bruce Fields 	return 0;
165289ede45SJ. Bruce Fields }
166289ede45SJ. Bruce Fields 
16782ead7feSJ. Bruce Fields /*
16882ead7feSJ. Bruce Fields  * For nfsd purposes, we treat V4ROOT exports as though there was an
16982ead7feSJ. Bruce Fields  * export at *every* directory.
17082ead7feSJ. Bruce Fields  */
1713227fa41SJ. Bruce Fields int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
17282ead7feSJ. Bruce Fields {
17382ead7feSJ. Bruce Fields 	if (d_mountpoint(dentry))
17482ead7feSJ. Bruce Fields 		return 1;
17511fcee02STrond Myklebust 	if (nfsd4_is_junction(dentry))
17611fcee02STrond Myklebust 		return 1;
17782ead7feSJ. Bruce Fields 	if (!(exp->ex_flags & NFSEXP_V4ROOT))
17882ead7feSJ. Bruce Fields 		return 0;
1792b0143b5SDavid Howells 	return d_inode(dentry) != NULL;
18082ead7feSJ. Bruce Fields }
18182ead7feSJ. Bruce Fields 
1826264d69dSAl Viro __be32
1836c0a654dSJ. Bruce Fields nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
1845a022fc8SChuck Lever 		   const char *name, unsigned int len,
1856c0a654dSJ. Bruce Fields 		   struct svc_export **exp_ret, struct dentry **dentry_ret)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	struct svc_export	*exp;
1881da177e4SLinus Torvalds 	struct dentry		*dparent;
1891da177e4SLinus Torvalds 	struct dentry		*dentry;
1906264d69dSAl Viro 	int			host_err;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	dparent = fhp->fh_dentry;
195bf18f163SKinglong Mee 	exp = exp_get(fhp->fh_export);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	/* Lookup the name, but don't follow links */
1981da177e4SLinus Torvalds 	if (isdotent(name, len)) {
1991da177e4SLinus Torvalds 		if (len==1)
2001da177e4SLinus Torvalds 			dentry = dget(dparent);
20154775491SJan Blunck 		else if (dparent != exp->ex_path.dentry)
2021da177e4SLinus Torvalds 			dentry = dget_parent(dparent);
203fed83811SJ. Bruce Fields 		else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
2041da177e4SLinus Torvalds 			dentry = dget(dparent); /* .. == . just like at / */
2051da177e4SLinus Torvalds 		else {
2061da177e4SLinus Torvalds 			/* checking mountpoint crossing is very different when stepping up */
207289ede45SJ. Bruce Fields 			host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
208289ede45SJ. Bruce Fields 			if (host_err)
2091da177e4SLinus Torvalds 				goto out_nfserr;
2101da177e4SLinus Torvalds 		}
2111da177e4SLinus Torvalds 	} else {
2124335723eSJ. Bruce Fields 		/*
2134335723eSJ. Bruce Fields 		 * In the nfsd4_open() case, this may be held across
2144335723eSJ. Bruce Fields 		 * subsequent open and delegation acquisition which may
2154335723eSJ. Bruce Fields 		 * need to take the child's i_mutex:
2164335723eSJ. Bruce Fields 		 */
2174335723eSJ. Bruce Fields 		fh_lock_nested(fhp, I_MUTEX_PARENT);
2181da177e4SLinus Torvalds 		dentry = lookup_one_len(name, dparent, len);
2196264d69dSAl Viro 		host_err = PTR_ERR(dentry);
2201da177e4SLinus Torvalds 		if (IS_ERR(dentry))
2211da177e4SLinus Torvalds 			goto out_nfserr;
22282ead7feSJ. Bruce Fields 		if (nfsd_mountpoint(dentry, exp)) {
223bbddca8eSNeilBrown 			/*
224bbddca8eSNeilBrown 			 * We don't need the i_mutex after all.  It's
225bbddca8eSNeilBrown 			 * still possible we could open this (regular
226bbddca8eSNeilBrown 			 * files can be mountpoints too), but the
227bbddca8eSNeilBrown 			 * i_mutex is just there to prevent renames of
228bbddca8eSNeilBrown 			 * something that we might be about to delegate,
229bbddca8eSNeilBrown 			 * and a mountpoint won't be renamed:
230bbddca8eSNeilBrown 			 */
231bbddca8eSNeilBrown 			fh_unlock(fhp);
2326264d69dSAl Viro 			if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
2331da177e4SLinus Torvalds 				dput(dentry);
2341da177e4SLinus Torvalds 				goto out_nfserr;
2351da177e4SLinus Torvalds 			}
2361da177e4SLinus Torvalds 		}
2371da177e4SLinus Torvalds 	}
2386c0a654dSJ. Bruce Fields 	*dentry_ret = dentry;
2396c0a654dSJ. Bruce Fields 	*exp_ret = exp;
2406c0a654dSJ. Bruce Fields 	return 0;
2416c0a654dSJ. Bruce Fields 
2426c0a654dSJ. Bruce Fields out_nfserr:
2436c0a654dSJ. Bruce Fields 	exp_put(exp);
2446c0a654dSJ. Bruce Fields 	return nfserrno(host_err);
2456c0a654dSJ. Bruce Fields }
2466c0a654dSJ. Bruce Fields 
2476c0a654dSJ. Bruce Fields /*
2486c0a654dSJ. Bruce Fields  * Look up one component of a pathname.
2496c0a654dSJ. Bruce Fields  * N.B. After this call _both_ fhp and resfh need an fh_put
2506c0a654dSJ. Bruce Fields  *
2516c0a654dSJ. Bruce Fields  * If the lookup would cross a mountpoint, and the mounted filesystem
2526c0a654dSJ. Bruce Fields  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
2536c0a654dSJ. Bruce Fields  * accepted as it stands and the mounted directory is
2546c0a654dSJ. Bruce Fields  * returned. Otherwise the covered directory is returned.
2556c0a654dSJ. Bruce Fields  * NOTE: this mountpoint crossing is not supported properly by all
2566c0a654dSJ. Bruce Fields  *   clients and is explicitly disallowed for NFSv3
2576c0a654dSJ. Bruce Fields  *      NeilBrown <neilb@cse.unsw.edu.au>
2586c0a654dSJ. Bruce Fields  */
2596c0a654dSJ. Bruce Fields __be32
2606c0a654dSJ. Bruce Fields nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
2615a022fc8SChuck Lever 				unsigned int len, struct svc_fh *resfh)
2626c0a654dSJ. Bruce Fields {
2636c0a654dSJ. Bruce Fields 	struct svc_export	*exp;
2646c0a654dSJ. Bruce Fields 	struct dentry		*dentry;
2656c0a654dSJ. Bruce Fields 	__be32 err;
2666c0a654dSJ. Bruce Fields 
26729a78a3eSJ. Bruce Fields 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
26829a78a3eSJ. Bruce Fields 	if (err)
26929a78a3eSJ. Bruce Fields 		return err;
2706c0a654dSJ. Bruce Fields 	err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
2716c0a654dSJ. Bruce Fields 	if (err)
2726c0a654dSJ. Bruce Fields 		return err;
27332c1eb0cSAndy Adamson 	err = check_nfsd_access(exp, rqstp);
27432c1eb0cSAndy Adamson 	if (err)
27532c1eb0cSAndy Adamson 		goto out;
2761da177e4SLinus Torvalds 	/*
2771da177e4SLinus Torvalds 	 * Note: we compose the file handle now, but as the
2781da177e4SLinus Torvalds 	 * dentry may be negative, it may need to be updated.
2791da177e4SLinus Torvalds 	 */
2801da177e4SLinus Torvalds 	err = fh_compose(resfh, exp, dentry, fhp);
2812b0143b5SDavid Howells 	if (!err && d_really_is_negative(dentry))
2821da177e4SLinus Torvalds 		err = nfserr_noent;
28332c1eb0cSAndy Adamson out:
2841da177e4SLinus Torvalds 	dput(dentry);
2851da177e4SLinus Torvalds 	exp_put(exp);
2861da177e4SLinus Torvalds 	return err;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
289f501912aSBen Myers /*
290f501912aSBen Myers  * Commit metadata changes to stable storage.
291f501912aSBen Myers  */
292f501912aSBen Myers static int
293f501912aSBen Myers commit_metadata(struct svc_fh *fhp)
294f501912aSBen Myers {
2952b0143b5SDavid Howells 	struct inode *inode = d_inode(fhp->fh_dentry);
296f501912aSBen Myers 	const struct export_operations *export_ops = inode->i_sb->s_export_op;
297f501912aSBen Myers 
298f501912aSBen Myers 	if (!EX_ISSYNC(fhp->fh_export))
299f501912aSBen Myers 		return 0;
300f501912aSBen Myers 
301c3765016SChristoph Hellwig 	if (export_ops->commit_metadata)
302c3765016SChristoph Hellwig 		return export_ops->commit_metadata(inode);
303c3765016SChristoph Hellwig 	return sync_inode_metadata(inode, 1);
304f501912aSBen Myers }
3056c0a654dSJ. Bruce Fields 
3061da177e4SLinus Torvalds /*
307818e5a22SChristoph Hellwig  * Go over the attributes and take care of the small differences between
308818e5a22SChristoph Hellwig  * NFS semantics and what Linux expects.
309818e5a22SChristoph Hellwig  */
310818e5a22SChristoph Hellwig static void
311818e5a22SChristoph Hellwig nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
312818e5a22SChristoph Hellwig {
313818e5a22SChristoph Hellwig 	/* sanitize the mode change */
314818e5a22SChristoph Hellwig 	if (iap->ia_valid & ATTR_MODE) {
315818e5a22SChristoph Hellwig 		iap->ia_mode &= S_IALLUGO;
316818e5a22SChristoph Hellwig 		iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
317818e5a22SChristoph Hellwig 	}
318818e5a22SChristoph Hellwig 
319818e5a22SChristoph Hellwig 	/* Revoke setuid/setgid on chown */
320818e5a22SChristoph Hellwig 	if (!S_ISDIR(inode->i_mode) &&
321c4fa6d7cSStanislav Kholmanskikh 	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
322818e5a22SChristoph Hellwig 		iap->ia_valid |= ATTR_KILL_PRIV;
323818e5a22SChristoph Hellwig 		if (iap->ia_valid & ATTR_MODE) {
324818e5a22SChristoph Hellwig 			/* we're setting mode too, just clear the s*id bits */
325818e5a22SChristoph Hellwig 			iap->ia_mode &= ~S_ISUID;
326818e5a22SChristoph Hellwig 			if (iap->ia_mode & S_IXGRP)
327818e5a22SChristoph Hellwig 				iap->ia_mode &= ~S_ISGID;
328818e5a22SChristoph Hellwig 		} else {
329818e5a22SChristoph Hellwig 			/* set ATTR_KILL_* bits and let VFS handle it */
330818e5a22SChristoph Hellwig 			iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
331818e5a22SChristoph Hellwig 		}
332818e5a22SChristoph Hellwig 	}
333818e5a22SChristoph Hellwig }
334818e5a22SChristoph Hellwig 
335818e5a22SChristoph Hellwig /*
336818e5a22SChristoph Hellwig  * Set various file attributes.  After this call fhp needs an fh_put.
3371da177e4SLinus Torvalds  */
3386264d69dSAl Viro __be32
3391da177e4SLinus Torvalds nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
3401da177e4SLinus Torvalds 	     int check_guard, time_t guardtime)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	struct dentry	*dentry;
3431da177e4SLinus Torvalds 	struct inode	*inode;
3448837abcaSMiklos Szeredi 	int		accmode = NFSD_MAY_SATTR;
345175a4eb7SAl Viro 	umode_t		ftype = 0;
3466264d69dSAl Viro 	__be32		err;
3476264d69dSAl Viro 	int		host_err;
3489f67f189SJ. Bruce Fields 	bool		get_write_count;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
3518837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
3521da177e4SLinus Torvalds 	if (iap->ia_valid & ATTR_SIZE)
3531da177e4SLinus Torvalds 		ftype = S_IFREG;
3541da177e4SLinus Torvalds 
3559f67f189SJ. Bruce Fields 	/* Callers that do fh_verify should do the fh_want_write: */
3569f67f189SJ. Bruce Fields 	get_write_count = !fhp->fh_dentry;
3579f67f189SJ. Bruce Fields 
3581da177e4SLinus Torvalds 	/* Get inode */
3591da177e4SLinus Torvalds 	err = fh_verify(rqstp, fhp, ftype, accmode);
36015b7a1b8SNeilBrown 	if (err)
36141f53350SChristoph Hellwig 		return err;
3629f67f189SJ. Bruce Fields 	if (get_write_count) {
3639f67f189SJ. Bruce Fields 		host_err = fh_want_write(fhp);
3649f67f189SJ. Bruce Fields 		if (host_err)
36541f53350SChristoph Hellwig 			goto out_host_err;
3669f67f189SJ. Bruce Fields 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
3692b0143b5SDavid Howells 	inode = d_inode(dentry);
3701da177e4SLinus Torvalds 
37115b7a1b8SNeilBrown 	/* Ignore any mode updates on symlinks */
37215b7a1b8SNeilBrown 	if (S_ISLNK(inode->i_mode))
37315b7a1b8SNeilBrown 		iap->ia_valid &= ~ATTR_MODE;
37415b7a1b8SNeilBrown 
37515b7a1b8SNeilBrown 	if (!iap->ia_valid)
37641f53350SChristoph Hellwig 		return 0;
37715b7a1b8SNeilBrown 
378818e5a22SChristoph Hellwig 	nfsd_sanitize_attrs(inode, iap);
3791da177e4SLinus Torvalds 
38041f53350SChristoph Hellwig 	if (check_guard && guardtime != inode->i_ctime.tv_sec)
38141f53350SChristoph Hellwig 		return nfserr_notsync;
382f0c63124SChristoph Hellwig 
383f0c63124SChristoph Hellwig 	/*
38441f53350SChristoph Hellwig 	 * The size case is special, it changes the file in addition to the
38541f53350SChristoph Hellwig 	 * attributes, and file systems don't expect it to be mixed with
38641f53350SChristoph Hellwig 	 * "random" attribute changes.  We thus split out the size change
38741f53350SChristoph Hellwig 	 * into a separate call for vfs_truncate, and do the rest as a
38841f53350SChristoph Hellwig 	 * a separate setattr call.
389f0c63124SChristoph Hellwig 	 */
39041f53350SChristoph Hellwig 	if (iap->ia_valid & ATTR_SIZE) {
39141f53350SChristoph Hellwig 		struct path path = {
39241f53350SChristoph Hellwig 			.mnt	= fhp->fh_export->ex_path.mnt,
39341f53350SChristoph Hellwig 			.dentry	= dentry,
39441f53350SChristoph Hellwig 		};
39541f53350SChristoph Hellwig 		bool implicit_mtime = false;
39641f53350SChristoph Hellwig 
39741f53350SChristoph Hellwig 		/*
39841f53350SChristoph Hellwig 		 * vfs_truncate implicity updates the mtime IFF the file size
39941f53350SChristoph Hellwig 		 * actually changes.  Avoid the additional seattr call below if
40041f53350SChristoph Hellwig 		 * the only other attribute that the client sends is the mtime.
40141f53350SChristoph Hellwig 		 */
40241f53350SChristoph Hellwig 		if (iap->ia_size != i_size_read(inode) &&
40341f53350SChristoph Hellwig 		    ((iap->ia_valid & ~(ATTR_SIZE | ATTR_MTIME)) == 0))
40441f53350SChristoph Hellwig 			implicit_mtime = true;
40541f53350SChristoph Hellwig 
40641f53350SChristoph Hellwig 		host_err = vfs_truncate(&path, iap->ia_size);
40741f53350SChristoph Hellwig 		if (host_err)
40841f53350SChristoph Hellwig 			goto out_host_err;
40941f53350SChristoph Hellwig 
41041f53350SChristoph Hellwig 		iap->ia_valid &= ~ATTR_SIZE;
41141f53350SChristoph Hellwig 		if (implicit_mtime)
41241f53350SChristoph Hellwig 			iap->ia_valid &= ~ATTR_MTIME;
41341f53350SChristoph Hellwig 		if (!iap->ia_valid)
41441f53350SChristoph Hellwig 			goto done;
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	iap->ia_valid |= ATTR_CTIME;
4181da177e4SLinus Torvalds 
419987da479SChristoph Hellwig 	fh_lock(fhp);
42027ac0ffeSJ. Bruce Fields 	host_err = notify_change(dentry, iap, NULL);
4211da177e4SLinus Torvalds 	fh_unlock(fhp);
42241f53350SChristoph Hellwig 	if (host_err)
42341f53350SChristoph Hellwig 		goto out_host_err;
424987da479SChristoph Hellwig 
42541f53350SChristoph Hellwig done:
42641f53350SChristoph Hellwig 	host_err = commit_metadata(fhp);
42741f53350SChristoph Hellwig out_host_err:
42841f53350SChristoph Hellwig 	return nfserrno(host_err);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4315be196e5SChristoph Hellwig #if defined(CONFIG_NFSD_V4)
4329b4146e8SChuck Lever /*
4339b4146e8SChuck Lever  * NFS junction information is stored in an extended attribute.
4349b4146e8SChuck Lever  */
4359b4146e8SChuck Lever #define NFSD_JUNCTION_XATTR_NAME	XATTR_TRUSTED_PREFIX "junction.nfs"
4369b4146e8SChuck Lever 
4379b4146e8SChuck Lever /**
4389b4146e8SChuck Lever  * nfsd4_is_junction - Test if an object could be an NFS junction
4399b4146e8SChuck Lever  *
4409b4146e8SChuck Lever  * @dentry: object to test
4419b4146e8SChuck Lever  *
4429b4146e8SChuck Lever  * Returns 1 if "dentry" appears to contain NFS junction information.
4439b4146e8SChuck Lever  * Otherwise 0 is returned.
4449b4146e8SChuck Lever  */
44511fcee02STrond Myklebust int nfsd4_is_junction(struct dentry *dentry)
44611fcee02STrond Myklebust {
4472b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
44811fcee02STrond Myklebust 
44911fcee02STrond Myklebust 	if (inode == NULL)
45011fcee02STrond Myklebust 		return 0;
45111fcee02STrond Myklebust 	if (inode->i_mode & S_IXUGO)
45211fcee02STrond Myklebust 		return 0;
45311fcee02STrond Myklebust 	if (!(inode->i_mode & S_ISVTX))
45411fcee02STrond Myklebust 		return 0;
4559b4146e8SChuck Lever 	if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
45611fcee02STrond Myklebust 		return 0;
45711fcee02STrond Myklebust 	return 1;
45811fcee02STrond Myklebust }
45918032ca0SDavid Quigley #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
46018032ca0SDavid Quigley __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
46118032ca0SDavid Quigley 		struct xdr_netobj *label)
46218032ca0SDavid Quigley {
46318032ca0SDavid Quigley 	__be32 error;
46418032ca0SDavid Quigley 	int host_error;
46518032ca0SDavid Quigley 	struct dentry *dentry;
46618032ca0SDavid Quigley 
46718032ca0SDavid Quigley 	error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
46818032ca0SDavid Quigley 	if (error)
46918032ca0SDavid Quigley 		return error;
47018032ca0SDavid Quigley 
47118032ca0SDavid Quigley 	dentry = fhp->fh_dentry;
47218032ca0SDavid Quigley 
4735955102cSAl Viro 	inode_lock(d_inode(dentry));
47418032ca0SDavid Quigley 	host_error = security_inode_setsecctx(dentry, label->data, label->len);
4755955102cSAl Viro 	inode_unlock(d_inode(dentry));
47618032ca0SDavid Quigley 	return nfserrno(host_error);
47718032ca0SDavid Quigley }
47818032ca0SDavid Quigley #else
47918032ca0SDavid Quigley __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
48018032ca0SDavid Quigley 		struct xdr_netobj *label)
48118032ca0SDavid Quigley {
48218032ca0SDavid Quigley 	return nfserr_notsupp;
48318032ca0SDavid Quigley }
48418032ca0SDavid Quigley #endif
48518032ca0SDavid Quigley 
486ffa0160aSChristoph Hellwig __be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
487ffa0160aSChristoph Hellwig 		u64 dst_pos, u64 count)
488ffa0160aSChristoph Hellwig {
489031a072aSAmir Goldstein 	return nfserrno(do_clone_file_range(src, src_pos, dst, dst_pos, count));
490ffa0160aSChristoph Hellwig }
491ffa0160aSChristoph Hellwig 
49229ae7f9dSAnna Schumaker ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
49329ae7f9dSAnna Schumaker 			     u64 dst_pos, u64 count)
49429ae7f9dSAnna Schumaker {
49529ae7f9dSAnna Schumaker 
49629ae7f9dSAnna Schumaker 	/*
49729ae7f9dSAnna Schumaker 	 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
49829ae7f9dSAnna Schumaker 	 * thread and client rpc slot.  The choice of 4MB is somewhat
49929ae7f9dSAnna Schumaker 	 * arbitrary.  We might instead base this on r/wsize, or make it
50029ae7f9dSAnna Schumaker 	 * tunable, or use a time instead of a byte limit, or implement
50129ae7f9dSAnna Schumaker 	 * asynchronous copy.  In theory a client could also recognize a
50229ae7f9dSAnna Schumaker 	 * limit like this and pipeline multiple COPY requests.
50329ae7f9dSAnna Schumaker 	 */
50429ae7f9dSAnna Schumaker 	count = min_t(u64, count, 1 << 22);
50529ae7f9dSAnna Schumaker 	return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
50629ae7f9dSAnna Schumaker }
50729ae7f9dSAnna Schumaker 
50895d871f0SAnna Schumaker __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
50995d871f0SAnna Schumaker 			   struct file *file, loff_t offset, loff_t len,
51095d871f0SAnna Schumaker 			   int flags)
51195d871f0SAnna Schumaker {
51295d871f0SAnna Schumaker 	int error;
51395d871f0SAnna Schumaker 
51495d871f0SAnna Schumaker 	if (!S_ISREG(file_inode(file)->i_mode))
51595d871f0SAnna Schumaker 		return nfserr_inval;
51695d871f0SAnna Schumaker 
51795d871f0SAnna Schumaker 	error = vfs_fallocate(file, flags, offset, len);
51895d871f0SAnna Schumaker 	if (!error)
51995d871f0SAnna Schumaker 		error = commit_metadata(fhp);
52095d871f0SAnna Schumaker 
52195d871f0SAnna Schumaker 	return nfserrno(error);
52295d871f0SAnna Schumaker }
5236a85d6c7SJ. Bruce Fields #endif /* defined(CONFIG_NFSD_V4) */
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
5261da177e4SLinus Torvalds /*
5271da177e4SLinus Torvalds  * Check server access rights to a file system object
5281da177e4SLinus Torvalds  */
5291da177e4SLinus Torvalds struct accessmap {
5301da177e4SLinus Torvalds 	u32		access;
5311da177e4SLinus Torvalds 	int		how;
5321da177e4SLinus Torvalds };
5331da177e4SLinus Torvalds static struct accessmap	nfs3_regaccess[] = {
5348837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
5358837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
5368837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_TRUNC	},
5378837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE			},
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds     {	0,			0				}
5401da177e4SLinus Torvalds };
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds static struct accessmap	nfs3_diraccess[] = {
5438837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
5448837abcaSMiklos Szeredi     {	NFS3_ACCESS_LOOKUP,	NFSD_MAY_EXEC			},
5458837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
5468837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_EXEC|NFSD_MAY_WRITE	},
5478837abcaSMiklos Szeredi     {	NFS3_ACCESS_DELETE,	NFSD_MAY_REMOVE			},
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds     {	0,			0				}
5501da177e4SLinus Torvalds };
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds static struct accessmap	nfs3_anyaccess[] = {
5531da177e4SLinus Torvalds 	/* Some clients - Solaris 2.6 at least, make an access call
5541da177e4SLinus Torvalds 	 * to the server to check for access for things like /dev/null
5551da177e4SLinus Torvalds 	 * (which really, the server doesn't care about).  So
5561da177e4SLinus Torvalds 	 * We provide simple access checking for them, looking
5571da177e4SLinus Torvalds 	 * mainly at mode bits, and we make sure to ignore read-only
5581da177e4SLinus Torvalds 	 * filesystem checks
5591da177e4SLinus Torvalds 	 */
5608837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
5618837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
5628837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
5638837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds     {	0,			0				}
5661da177e4SLinus Torvalds };
5671da177e4SLinus Torvalds 
5686264d69dSAl Viro __be32
5691da177e4SLinus Torvalds nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	struct accessmap	*map;
5721da177e4SLinus Torvalds 	struct svc_export	*export;
5731da177e4SLinus Torvalds 	struct dentry		*dentry;
5741da177e4SLinus Torvalds 	u32			query, result = 0, sresult = 0;
5756264d69dSAl Viro 	__be32			error;
5761da177e4SLinus Torvalds 
5778837abcaSMiklos Szeredi 	error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
5781da177e4SLinus Torvalds 	if (error)
5791da177e4SLinus Torvalds 		goto out;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	export = fhp->fh_export;
5821da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
5831da177e4SLinus Torvalds 
584e36cb0b8SDavid Howells 	if (d_is_reg(dentry))
5851da177e4SLinus Torvalds 		map = nfs3_regaccess;
586e36cb0b8SDavid Howells 	else if (d_is_dir(dentry))
5871da177e4SLinus Torvalds 		map = nfs3_diraccess;
5881da177e4SLinus Torvalds 	else
5891da177e4SLinus Torvalds 		map = nfs3_anyaccess;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	query = *access;
5931da177e4SLinus Torvalds 	for  (; map->access; map++) {
5941da177e4SLinus Torvalds 		if (map->access & query) {
5956264d69dSAl Viro 			__be32 err2;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 			sresult |= map->access;
5981da177e4SLinus Torvalds 
5990ec757dfSJ. Bruce Fields 			err2 = nfsd_permission(rqstp, export, dentry, map->how);
6001da177e4SLinus Torvalds 			switch (err2) {
6011da177e4SLinus Torvalds 			case nfs_ok:
6021da177e4SLinus Torvalds 				result |= map->access;
6031da177e4SLinus Torvalds 				break;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 			/* the following error codes just mean the access was not allowed,
6061da177e4SLinus Torvalds 			 * rather than an error occurred */
6071da177e4SLinus Torvalds 			case nfserr_rofs:
6081da177e4SLinus Torvalds 			case nfserr_acces:
6091da177e4SLinus Torvalds 			case nfserr_perm:
6101da177e4SLinus Torvalds 				/* simply don't "or" in the access bit. */
6111da177e4SLinus Torvalds 				break;
6121da177e4SLinus Torvalds 			default:
6131da177e4SLinus Torvalds 				error = err2;
6141da177e4SLinus Torvalds 				goto out;
6151da177e4SLinus Torvalds 			}
6161da177e4SLinus Torvalds 		}
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 	*access = result;
6191da177e4SLinus Torvalds 	if (supported)
6201da177e4SLinus Torvalds 		*supported = sresult;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds  out:
6231da177e4SLinus Torvalds 	return error;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
6261da177e4SLinus Torvalds 
627105f4622SJ. Bruce Fields static int nfsd_open_break_lease(struct inode *inode, int access)
628105f4622SJ. Bruce Fields {
629105f4622SJ. Bruce Fields 	unsigned int mode;
6301da177e4SLinus Torvalds 
631105f4622SJ. Bruce Fields 	if (access & NFSD_MAY_NOT_BREAK_LEASE)
632105f4622SJ. Bruce Fields 		return 0;
633105f4622SJ. Bruce Fields 	mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
634105f4622SJ. Bruce Fields 	return break_lease(inode, mode | O_NONBLOCK);
635105f4622SJ. Bruce Fields }
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds /*
6381da177e4SLinus Torvalds  * Open an existing file or directory.
639999448a8SBernd Schubert  * The may_flags argument indicates the type of open (read/write/lock)
640999448a8SBernd Schubert  * and additional flags.
6411da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
6421da177e4SLinus Torvalds  */
6436264d69dSAl Viro __be32
644175a4eb7SAl Viro nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
645999448a8SBernd Schubert 			int may_flags, struct file **filp)
6461da177e4SLinus Torvalds {
647765927b2SAl Viro 	struct path	path;
6481da177e4SLinus Torvalds 	struct inode	*inode;
6498519f994SKinglong Mee 	struct file	*file;
6506264d69dSAl Viro 	int		flags = O_RDONLY|O_LARGEFILE;
6516264d69dSAl Viro 	__be32		err;
65291885258SJeff Layton 	int		host_err = 0;
6531da177e4SLinus Torvalds 
654e0e81739SDavid Howells 	validate_process_creds();
655e0e81739SDavid Howells 
6561da177e4SLinus Torvalds 	/*
6571da177e4SLinus Torvalds 	 * If we get here, then the client has already done an "open",
6581da177e4SLinus Torvalds 	 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
6591da177e4SLinus Torvalds 	 * in case a chmod has now revoked permission.
660d91d0b56SJ. Bruce Fields 	 *
661d91d0b56SJ. Bruce Fields 	 * Arguably we should also allow the owner override for
662d91d0b56SJ. Bruce Fields 	 * directories, but we never have and it doesn't seem to have
663d91d0b56SJ. Bruce Fields 	 * caused anyone a problem.  If we were to change this, note
664d91d0b56SJ. Bruce Fields 	 * also that our filldir callbacks would need a variant of
665d91d0b56SJ. Bruce Fields 	 * lookup_one_len that doesn't check permissions.
6661da177e4SLinus Torvalds 	 */
667d91d0b56SJ. Bruce Fields 	if (type == S_IFREG)
668d91d0b56SJ. Bruce Fields 		may_flags |= NFSD_MAY_OWNER_OVERRIDE;
669d91d0b56SJ. Bruce Fields 	err = fh_verify(rqstp, fhp, type, may_flags);
6701da177e4SLinus Torvalds 	if (err)
6711da177e4SLinus Torvalds 		goto out;
6721da177e4SLinus Torvalds 
673765927b2SAl Viro 	path.mnt = fhp->fh_export->ex_path.mnt;
674765927b2SAl Viro 	path.dentry = fhp->fh_dentry;
6752b0143b5SDavid Howells 	inode = d_inode(path.dentry);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	/* Disallow write access to files with the append-only bit set
6781da177e4SLinus Torvalds 	 * or any access when mandatory locking enabled
6791da177e4SLinus Torvalds 	 */
6801da177e4SLinus Torvalds 	err = nfserr_perm;
681999448a8SBernd Schubert 	if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
6821da177e4SLinus Torvalds 		goto out;
6835e7fc436SJ. Bruce Fields 	/*
6845e7fc436SJ. Bruce Fields 	 * We must ignore files (but only files) which might have mandatory
6855e7fc436SJ. Bruce Fields 	 * locks on them because there is no way to know if the accesser has
6865e7fc436SJ. Bruce Fields 	 * the lock.
6875e7fc436SJ. Bruce Fields 	 */
6885e7fc436SJ. Bruce Fields 	if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
6891da177e4SLinus Torvalds 		goto out;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	if (!inode->i_fop)
6921da177e4SLinus Torvalds 		goto out;
6931da177e4SLinus Torvalds 
694999448a8SBernd Schubert 	host_err = nfsd_open_break_lease(inode, may_flags);
6956264d69dSAl Viro 	if (host_err) /* NOMEM or WOULDBLOCK */
6961da177e4SLinus Torvalds 		goto out_nfserr;
6971da177e4SLinus Torvalds 
698999448a8SBernd Schubert 	if (may_flags & NFSD_MAY_WRITE) {
699999448a8SBernd Schubert 		if (may_flags & NFSD_MAY_READ)
7009ecb6a08SJ. Bruce Fields 			flags = O_RDWR|O_LARGEFILE;
7019ecb6a08SJ. Bruce Fields 		else
7021da177e4SLinus Torvalds 			flags = O_WRONLY|O_LARGEFILE;
7031da177e4SLinus Torvalds 	}
704999448a8SBernd Schubert 
7058519f994SKinglong Mee 	file = dentry_open(&path, flags, current_cred());
7068519f994SKinglong Mee 	if (IS_ERR(file)) {
7078519f994SKinglong Mee 		host_err = PTR_ERR(file);
7088519f994SKinglong Mee 		goto out_nfserr;
70906effdbbSBernd Schubert 	}
71006effdbbSBernd Schubert 
7115e40d331SLinus Torvalds 	host_err = ima_file_check(file, may_flags, 0);
7128519f994SKinglong Mee 	if (host_err) {
713fd891454SChristoph Hellwig 		fput(file);
7148519f994SKinglong Mee 		goto out_nfserr;
7158519f994SKinglong Mee 	}
7168519f994SKinglong Mee 
7178519f994SKinglong Mee 	if (may_flags & NFSD_MAY_64BIT_COOKIE)
7188519f994SKinglong Mee 		file->f_mode |= FMODE_64BITHASH;
7198519f994SKinglong Mee 	else
7208519f994SKinglong Mee 		file->f_mode |= FMODE_32BITHASH;
7218519f994SKinglong Mee 
7228519f994SKinglong Mee 	*filp = file;
7231da177e4SLinus Torvalds out_nfserr:
7246264d69dSAl Viro 	err = nfserrno(host_err);
7251da177e4SLinus Torvalds out:
726e0e81739SDavid Howells 	validate_process_creds();
7271da177e4SLinus Torvalds 	return err;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
730e749a462SChristoph Hellwig struct raparms *
731e749a462SChristoph Hellwig nfsd_init_raparms(struct file *file)
7321da177e4SLinus Torvalds {
733e749a462SChristoph Hellwig 	struct inode *inode = file_inode(file);
734e749a462SChristoph Hellwig 	dev_t dev = inode->i_sb->s_dev;
735e749a462SChristoph Hellwig 	ino_t ino = inode->i_ino;
7361da177e4SLinus Torvalds 	struct raparms	*ra, **rap, **frap = NULL;
7371da177e4SLinus Torvalds 	int depth = 0;
738fce1456aSGreg Banks 	unsigned int hash;
739fce1456aSGreg Banks 	struct raparm_hbucket *rab;
7401da177e4SLinus Torvalds 
741fce1456aSGreg Banks 	hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
742fce1456aSGreg Banks 	rab = &raparm_hash[hash];
743fce1456aSGreg Banks 
744fce1456aSGreg Banks 	spin_lock(&rab->pb_lock);
745fce1456aSGreg Banks 	for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
7461da177e4SLinus Torvalds 		if (ra->p_ino == ino && ra->p_dev == dev)
7471da177e4SLinus Torvalds 			goto found;
7481da177e4SLinus Torvalds 		depth++;
7491da177e4SLinus Torvalds 		if (ra->p_count == 0)
7501da177e4SLinus Torvalds 			frap = rap;
7511da177e4SLinus Torvalds 	}
7523aa6e0aaSKonstantin Khorenko 	depth = nfsdstats.ra_size;
7531da177e4SLinus Torvalds 	if (!frap) {
754fce1456aSGreg Banks 		spin_unlock(&rab->pb_lock);
7551da177e4SLinus Torvalds 		return NULL;
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 	rap = frap;
7581da177e4SLinus Torvalds 	ra = *frap;
7591da177e4SLinus Torvalds 	ra->p_dev = dev;
7601da177e4SLinus Torvalds 	ra->p_ino = ino;
7611da177e4SLinus Torvalds 	ra->p_set = 0;
762fce1456aSGreg Banks 	ra->p_hindex = hash;
7631da177e4SLinus Torvalds found:
764fce1456aSGreg Banks 	if (rap != &rab->pb_head) {
7651da177e4SLinus Torvalds 		*rap = ra->p_next;
766fce1456aSGreg Banks 		ra->p_next   = rab->pb_head;
767fce1456aSGreg Banks 		rab->pb_head = ra;
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 	ra->p_count++;
7701da177e4SLinus Torvalds 	nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
771fce1456aSGreg Banks 	spin_unlock(&rab->pb_lock);
772e749a462SChristoph Hellwig 
773e749a462SChristoph Hellwig 	if (ra->p_set)
774e749a462SChristoph Hellwig 		file->f_ra = ra->p_ra;
7751da177e4SLinus Torvalds 	return ra;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
778e749a462SChristoph Hellwig void nfsd_put_raparams(struct file *file, struct raparms *ra)
779e749a462SChristoph Hellwig {
780e749a462SChristoph Hellwig 	struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
781e749a462SChristoph Hellwig 
782e749a462SChristoph Hellwig 	spin_lock(&rab->pb_lock);
783e749a462SChristoph Hellwig 	ra->p_ra = file->f_ra;
784e749a462SChristoph Hellwig 	ra->p_set = 1;
785e749a462SChristoph Hellwig 	ra->p_count--;
786e749a462SChristoph Hellwig 	spin_unlock(&rab->pb_lock);
787e749a462SChristoph Hellwig }
788e749a462SChristoph Hellwig 
7891da177e4SLinus Torvalds /*
790cf8208d0SJens Axboe  * Grab and keep cached pages associated with a file in the svc_rqst
791cf8208d0SJens Axboe  * so that they can be passed to the network sendmsg/sendpage routines
792cf8208d0SJens Axboe  * directly. They will be released after the sending has completed.
7931da177e4SLinus Torvalds  */
7941da177e4SLinus Torvalds static int
795cf8208d0SJens Axboe nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
796cf8208d0SJens Axboe 		  struct splice_desc *sd)
7971da177e4SLinus Torvalds {
798cf8208d0SJens Axboe 	struct svc_rqst *rqstp = sd->u.data;
799afc59400SJ. Bruce Fields 	struct page **pp = rqstp->rq_next_page;
800cf8208d0SJens Axboe 	struct page *page = buf->page;
801cf8208d0SJens Axboe 	size_t size;
802cf8208d0SJens Axboe 
803cf8208d0SJens Axboe 	size = sd->len;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	if (rqstp->rq_res.page_len == 0) {
8061da177e4SLinus Torvalds 		get_page(page);
807afc59400SJ. Bruce Fields 		put_page(*rqstp->rq_next_page);
808afc59400SJ. Bruce Fields 		*(rqstp->rq_next_page++) = page;
809cf8208d0SJens Axboe 		rqstp->rq_res.page_base = buf->offset;
8101da177e4SLinus Torvalds 		rqstp->rq_res.page_len = size;
81144524359SNeilBrown 	} else if (page != pp[-1]) {
8121da177e4SLinus Torvalds 		get_page(page);
813afc59400SJ. Bruce Fields 		if (*rqstp->rq_next_page)
814afc59400SJ. Bruce Fields 			put_page(*rqstp->rq_next_page);
815afc59400SJ. Bruce Fields 		*(rqstp->rq_next_page++) = page;
8161da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
81744524359SNeilBrown 	} else
8181da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	return size;
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
823cf8208d0SJens Axboe static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
824cf8208d0SJens Axboe 				    struct splice_desc *sd)
825cf8208d0SJens Axboe {
826cf8208d0SJens Axboe 	return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
827cf8208d0SJens Axboe }
828cf8208d0SJens Axboe 
829e2afc819SJeff Layton static __be32
830e2afc819SJeff Layton nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
8311da177e4SLinus Torvalds {
832dc97618dSJ. Bruce Fields 	if (host_err >= 0) {
833dc97618dSJ. Bruce Fields 		nfsdstats.io_read += host_err;
834dc97618dSJ. Bruce Fields 		*count = host_err;
835dc97618dSJ. Bruce Fields 		fsnotify_access(file);
836dc97618dSJ. Bruce Fields 		return 0;
837dc97618dSJ. Bruce Fields 	} else
838dc97618dSJ. Bruce Fields 		return nfserrno(host_err);
839dc97618dSJ. Bruce Fields }
8401da177e4SLinus Torvalds 
841e2afc819SJeff Layton __be32 nfsd_splice_read(struct svc_rqst *rqstp,
842dc97618dSJ. Bruce Fields 		     struct file *file, loff_t offset, unsigned long *count)
843dc97618dSJ. Bruce Fields {
844cf8208d0SJens Axboe 	struct splice_desc sd = {
845cf8208d0SJens Axboe 		.len		= 0,
846cf8208d0SJens Axboe 		.total_len	= *count,
847cf8208d0SJens Axboe 		.pos		= offset,
848cf8208d0SJens Axboe 		.u.data		= rqstp,
849cf8208d0SJens Axboe 	};
850dc97618dSJ. Bruce Fields 	int host_err;
851cf8208d0SJens Axboe 
852afc59400SJ. Bruce Fields 	rqstp->rq_next_page = rqstp->rq_respages + 1;
853cf8208d0SJens Axboe 	host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
854dc97618dSJ. Bruce Fields 	return nfsd_finish_read(file, count, host_err);
855dc97618dSJ. Bruce Fields }
856dc97618dSJ. Bruce Fields 
857e2afc819SJeff Layton __be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
858dc97618dSJ. Bruce Fields 		unsigned long *count)
859dc97618dSJ. Bruce Fields {
860dc97618dSJ. Bruce Fields 	mm_segment_t oldfs;
861dc97618dSJ. Bruce Fields 	int host_err;
862dc97618dSJ. Bruce Fields 
8631da177e4SLinus Torvalds 	oldfs = get_fs();
8641da177e4SLinus Torvalds 	set_fs(KERNEL_DS);
865793b80efSChristoph Hellwig 	host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset, 0);
8661da177e4SLinus Torvalds 	set_fs(oldfs);
867dc97618dSJ. Bruce Fields 	return nfsd_finish_read(file, count, host_err);
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
870dc97618dSJ. Bruce Fields static __be32
871dc97618dSJ. Bruce Fields nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
872dc97618dSJ. Bruce Fields 	      loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
873dc97618dSJ. Bruce Fields {
874779fb0f3SJeff Layton 	if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
875dc97618dSJ. Bruce Fields 		return nfsd_splice_read(rqstp, file, offset, count);
876dc97618dSJ. Bruce Fields 	else
877dc97618dSJ. Bruce Fields 		return nfsd_readv(file, offset, vec, vlen, count);
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
880d911df7bSJ. Bruce Fields /*
881d911df7bSJ. Bruce Fields  * Gathered writes: If another process is currently writing to the file,
882d911df7bSJ. Bruce Fields  * there's a high chance this is another nfsd (triggered by a bulk write
883d911df7bSJ. Bruce Fields  * from a client's biod). Rather than syncing the file with each write
884d911df7bSJ. Bruce Fields  * request, we sleep for 10 msec.
885d911df7bSJ. Bruce Fields  *
886d911df7bSJ. Bruce Fields  * I don't know if this roughly approximates C. Juszak's idea of
887d911df7bSJ. Bruce Fields  * gathered writes, but it's a nice and simple solution (IMHO), and it
888d911df7bSJ. Bruce Fields  * seems to work:-)
889d911df7bSJ. Bruce Fields  *
890d911df7bSJ. Bruce Fields  * Note: we do this only in the NFSv2 case, since v3 and higher have a
891d911df7bSJ. Bruce Fields  * better tool (separate unstable writes and commits) for solving this
892d911df7bSJ. Bruce Fields  * problem.
893d911df7bSJ. Bruce Fields  */
894d911df7bSJ. Bruce Fields static int wait_for_concurrent_writes(struct file *file)
895d911df7bSJ. Bruce Fields {
896496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
897d911df7bSJ. Bruce Fields 	static ino_t last_ino;
898d911df7bSJ. Bruce Fields 	static dev_t last_dev;
899d911df7bSJ. Bruce Fields 	int err = 0;
900d911df7bSJ. Bruce Fields 
901d911df7bSJ. Bruce Fields 	if (atomic_read(&inode->i_writecount) > 1
902d911df7bSJ. Bruce Fields 	    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
903d911df7bSJ. Bruce Fields 		dprintk("nfsd: write defer %d\n", task_pid_nr(current));
904d911df7bSJ. Bruce Fields 		msleep(10);
905d911df7bSJ. Bruce Fields 		dprintk("nfsd: write resume %d\n", task_pid_nr(current));
906d911df7bSJ. Bruce Fields 	}
907d911df7bSJ. Bruce Fields 
908d911df7bSJ. Bruce Fields 	if (inode->i_state & I_DIRTY) {
909d911df7bSJ. Bruce Fields 		dprintk("nfsd: write sync %d\n", task_pid_nr(current));
9108018ab05SChristoph Hellwig 		err = vfs_fsync(file, 0);
911d911df7bSJ. Bruce Fields 	}
912d911df7bSJ. Bruce Fields 	last_ino = inode->i_ino;
913d911df7bSJ. Bruce Fields 	last_dev = inode->i_sb->s_dev;
914d911df7bSJ. Bruce Fields 	return err;
915d911df7bSJ. Bruce Fields }
916d911df7bSJ. Bruce Fields 
917af90f707SChristoph Hellwig __be32
9181da177e4SLinus Torvalds nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
9191da177e4SLinus Torvalds 				loff_t offset, struct kvec *vec, int vlen,
92054bbb7d2SKinglong Mee 				unsigned long *cnt, int stable)
9211da177e4SLinus Torvalds {
9221da177e4SLinus Torvalds 	struct svc_export	*exp;
9231da177e4SLinus Torvalds 	struct inode		*inode;
9241da177e4SLinus Torvalds 	mm_segment_t		oldfs;
9256264d69dSAl Viro 	__be32			err = 0;
9266264d69dSAl Viro 	int			host_err;
92748e03bc5STrond Myklebust 	int			use_wgather;
928e49dbbf3SKent Overstreet 	loff_t			pos = offset;
9298658452eSNeilBrown 	unsigned int		pflags = current->flags;
93024368aadSChristoph Hellwig 	int			flags = 0;
9318658452eSNeilBrown 
9327501cc2bSJeff Layton 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
9338658452eSNeilBrown 		/*
9348658452eSNeilBrown 		 * We want less throttling in balance_dirty_pages()
9358658452eSNeilBrown 		 * and shrink_inactive_list() so that nfs to
9368658452eSNeilBrown 		 * localhost doesn't cause nfsd to lock up due to all
9378658452eSNeilBrown 		 * the client's dirty pages or its congested queue.
9388658452eSNeilBrown 		 */
9398658452eSNeilBrown 		current->flags |= PF_LESS_THROTTLE;
9401da177e4SLinus Torvalds 
9416f4e0d5aSAl Viro 	inode = file_inode(file);
9421da177e4SLinus Torvalds 	exp   = fhp->fh_export;
9431da177e4SLinus Torvalds 
94448e03bc5STrond Myklebust 	use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	if (!EX_ISSYNC(exp))
94754bbb7d2SKinglong Mee 		stable = NFS_UNSTABLE;
9481da177e4SLinus Torvalds 
94924368aadSChristoph Hellwig 	if (stable && !use_wgather)
95024368aadSChristoph Hellwig 		flags |= RWF_SYNC;
95124368aadSChristoph Hellwig 
9521da177e4SLinus Torvalds 	/* Write the data. */
9531da177e4SLinus Torvalds 	oldfs = get_fs(); set_fs(KERNEL_DS);
95424368aadSChristoph Hellwig 	host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos, flags);
9551da177e4SLinus Torvalds 	set_fs(oldfs);
956e4636d53SJ. Bruce Fields 	if (host_err < 0)
957e4636d53SJ. Bruce Fields 		goto out_nfserr;
958a0d24b29SWei Yongjun 	*cnt = host_err;
95931dec253SDavid Shaw 	nfsdstats.io_write += host_err;
9602a12a9d7SEric Paris 	fsnotify_modify(file);
9611da177e4SLinus Torvalds 
96224368aadSChristoph Hellwig 	if (stable && use_wgather)
963d911df7bSJ. Bruce Fields 		host_err = wait_for_concurrent_writes(file);
9641da177e4SLinus Torvalds 
965e4636d53SJ. Bruce Fields out_nfserr:
9666264d69dSAl Viro 	dprintk("nfsd: write complete host_err=%d\n", host_err);
967a0d24b29SWei Yongjun 	if (host_err >= 0)
9681da177e4SLinus Torvalds 		err = 0;
969a0d24b29SWei Yongjun 	else
9706264d69dSAl Viro 		err = nfserrno(host_err);
9717501cc2bSJeff Layton 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
9728658452eSNeilBrown 		tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
9731da177e4SLinus Torvalds 	return err;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
976dc97618dSJ. Bruce Fields /*
977dc97618dSJ. Bruce Fields  * Read data from a file. count must contain the requested read count
978dc97618dSJ. Bruce Fields  * on entry. On return, *count contains the number of bytes actually read.
979dc97618dSJ. Bruce Fields  * N.B. After this call fhp needs an fh_put
980dc97618dSJ. Bruce Fields  */
981dc97618dSJ. Bruce Fields __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
982dc97618dSJ. Bruce Fields 	loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
983dc97618dSJ. Bruce Fields {
984dc97618dSJ. Bruce Fields 	struct file *file;
985dc97618dSJ. Bruce Fields 	struct raparms	*ra;
986dc97618dSJ. Bruce Fields 	__be32 err;
987dc97618dSJ. Bruce Fields 
9886e8b50d1SJeff Layton 	trace_read_start(rqstp, fhp, offset, vlen);
989e749a462SChristoph Hellwig 	err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
990dc97618dSJ. Bruce Fields 	if (err)
991dc97618dSJ. Bruce Fields 		return err;
992dc97618dSJ. Bruce Fields 
993e749a462SChristoph Hellwig 	ra = nfsd_init_raparms(file);
9946e8b50d1SJeff Layton 
9956e8b50d1SJeff Layton 	trace_read_opened(rqstp, fhp, offset, vlen);
996dc97618dSJ. Bruce Fields 	err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
9976e8b50d1SJeff Layton 	trace_read_io_done(rqstp, fhp, offset, vlen);
9986e8b50d1SJeff Layton 
999e749a462SChristoph Hellwig 	if (ra)
1000e749a462SChristoph Hellwig 		nfsd_put_raparams(file, ra);
1001e749a462SChristoph Hellwig 	fput(file);
1002dc97618dSJ. Bruce Fields 
10036e8b50d1SJeff Layton 	trace_read_done(rqstp, fhp, offset, vlen);
10046e8b50d1SJeff Layton 
1005fa0a2126SJ. Bruce Fields 	return err;
1006fa0a2126SJ. Bruce Fields }
1007fa0a2126SJ. Bruce Fields 
10081da177e4SLinus Torvalds /*
10091da177e4SLinus Torvalds  * Write data to a file.
10101da177e4SLinus Torvalds  * The stable flag requests synchronous writes.
10111da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
10121da177e4SLinus Torvalds  */
10136264d69dSAl Viro __be32
1014*52e380e0SKinglong Mee nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1015*52e380e0SKinglong Mee 	   struct kvec *vec, int vlen, unsigned long *cnt, int stable)
10161da177e4SLinus Torvalds {
1017*52e380e0SKinglong Mee 	struct file *file = NULL;
10186264d69dSAl Viro 	__be32 err = 0;
10191da177e4SLinus Torvalds 
10206e8b50d1SJeff Layton 	trace_write_start(rqstp, fhp, offset, vlen);
10216e8b50d1SJeff Layton 
10228837abcaSMiklos Szeredi 	err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
10231da177e4SLinus Torvalds 	if (err)
10241da177e4SLinus Torvalds 		goto out;
10251da177e4SLinus Torvalds 
10266e8b50d1SJeff Layton 	trace_write_opened(rqstp, fhp, offset, vlen);
1027*52e380e0SKinglong Mee 	err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt, stable);
10286e8b50d1SJeff Layton 	trace_write_io_done(rqstp, fhp, offset, vlen);
1029fd891454SChristoph Hellwig 	fput(file);
10301da177e4SLinus Torvalds out:
10316e8b50d1SJeff Layton 	trace_write_done(rqstp, fhp, offset, vlen);
10321da177e4SLinus Torvalds 	return err;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
10361da177e4SLinus Torvalds /*
10371da177e4SLinus Torvalds  * Commit all pending writes to stable storage.
1038aa696a6fSTrond Myklebust  *
1039aa696a6fSTrond Myklebust  * Note: we only guarantee that data that lies within the range specified
1040aa696a6fSTrond Myklebust  * by the 'offset' and 'count' parameters will be synced.
10411da177e4SLinus Torvalds  *
10421da177e4SLinus Torvalds  * Unfortunately we cannot lock the file to make sure we return full WCC
10431da177e4SLinus Torvalds  * data to the client, as locking happens lower down in the filesystem.
10441da177e4SLinus Torvalds  */
10456264d69dSAl Viro __be32
10461da177e4SLinus Torvalds nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
10471da177e4SLinus Torvalds                loff_t offset, unsigned long count)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct file	*file;
1050aa696a6fSTrond Myklebust 	loff_t		end = LLONG_MAX;
1051aa696a6fSTrond Myklebust 	__be32		err = nfserr_inval;
10521da177e4SLinus Torvalds 
1053aa696a6fSTrond Myklebust 	if (offset < 0)
1054aa696a6fSTrond Myklebust 		goto out;
1055aa696a6fSTrond Myklebust 	if (count != 0) {
1056aa696a6fSTrond Myklebust 		end = offset + (loff_t)count - 1;
1057aa696a6fSTrond Myklebust 		if (end < offset)
1058aa696a6fSTrond Myklebust 			goto out;
1059aa696a6fSTrond Myklebust 	}
10601da177e4SLinus Torvalds 
106191885258SJeff Layton 	err = nfsd_open(rqstp, fhp, S_IFREG,
106291885258SJeff Layton 			NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
10638837abcaSMiklos Szeredi 	if (err)
1064aa696a6fSTrond Myklebust 		goto out;
10651da177e4SLinus Torvalds 	if (EX_ISSYNC(fhp->fh_export)) {
10668018ab05SChristoph Hellwig 		int err2 = vfs_fsync_range(file, offset, end, 0);
1067aa696a6fSTrond Myklebust 
1068aa696a6fSTrond Myklebust 		if (err2 != -EINVAL)
1069aa696a6fSTrond Myklebust 			err = nfserrno(err2);
1070aa696a6fSTrond Myklebust 		else
10711da177e4SLinus Torvalds 			err = nfserr_notsupp;
10721da177e4SLinus Torvalds 	}
10731da177e4SLinus Torvalds 
1074fd891454SChristoph Hellwig 	fput(file);
1075aa696a6fSTrond Myklebust out:
10761da177e4SLinus Torvalds 	return err;
10771da177e4SLinus Torvalds }
10781da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
10791da177e4SLinus Torvalds 
1080f2b0dee2SAdrian Bunk static __be32
10815c002b3bSJ. Bruce Fields nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
10825c002b3bSJ. Bruce Fields 			struct iattr *iap)
10835c002b3bSJ. Bruce Fields {
10845c002b3bSJ. Bruce Fields 	/*
10855c002b3bSJ. Bruce Fields 	 * Mode has already been set earlier in create:
10865c002b3bSJ. Bruce Fields 	 */
10875c002b3bSJ. Bruce Fields 	iap->ia_valid &= ~ATTR_MODE;
10885c002b3bSJ. Bruce Fields 	/*
10895c002b3bSJ. Bruce Fields 	 * Setting uid/gid works only for root.  Irix appears to
10905c002b3bSJ. Bruce Fields 	 * send along the gid on create when it tries to implement
10915c002b3bSJ. Bruce Fields 	 * setgid directories via NFS:
10925c002b3bSJ. Bruce Fields 	 */
10936fab8779SEric W. Biederman 	if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
10945c002b3bSJ. Bruce Fields 		iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
10955c002b3bSJ. Bruce Fields 	if (iap->ia_valid)
10965c002b3bSJ. Bruce Fields 		return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
10970f3a24b4STrond Myklebust 	/* Callers expect file metadata to be committed here */
1098722b620dSJeff Layton 	return nfserrno(commit_metadata(resfhp));
10995c002b3bSJ. Bruce Fields }
11005c002b3bSJ. Bruce Fields 
11014ac35c2fSwengang wang /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
11024ac35c2fSwengang wang  * setting size to 0 may fail for some specific file systems by the permission
11034ac35c2fSwengang wang  * checking which requires WRITE permission but the mode is 000.
11044ac35c2fSwengang wang  * we ignore the resizing(to 0) on the just new created file, since the size is
11054ac35c2fSwengang wang  * 0 after file created.
11064ac35c2fSwengang wang  *
11074ac35c2fSwengang wang  * call this only after vfs_create() is called.
11084ac35c2fSwengang wang  * */
11094ac35c2fSwengang wang static void
11104ac35c2fSwengang wang nfsd_check_ignore_resizing(struct iattr *iap)
11114ac35c2fSwengang wang {
11124ac35c2fSwengang wang 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
11134ac35c2fSwengang wang 		iap->ia_valid &= ~ATTR_SIZE;
11144ac35c2fSwengang wang }
11154ac35c2fSwengang wang 
1116b44061d0SJ. Bruce Fields /* The parent directory should already be locked: */
11176264d69dSAl Viro __be32
1118b44061d0SJ. Bruce Fields nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
11191da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
11201da177e4SLinus Torvalds 		int type, dev_t rdev, struct svc_fh *resfhp)
11211da177e4SLinus Torvalds {
11222b118859SDan Carpenter 	struct dentry	*dentry, *dchild;
11231da177e4SLinus Torvalds 	struct inode	*dirp;
11246264d69dSAl Viro 	__be32		err;
11255c002b3bSJ. Bruce Fields 	__be32		err2;
11266264d69dSAl Viro 	int		host_err;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
11292b0143b5SDavid Howells 	dirp = d_inode(dentry);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	dchild = dget(resfhp->fh_dentry);
11321da177e4SLinus Torvalds 	if (!fhp->fh_locked) {
1133b44061d0SJ. Bruce Fields 		WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1134a6a9f18fSAl Viro 				dentry);
1135d75f2b9fSAl Viro 		err = nfserr_io;
11361da177e4SLinus Torvalds 		goto out;
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds 
11397eed34f1SOleg Drokin 	err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
11407eed34f1SOleg Drokin 	if (err)
11417eed34f1SOleg Drokin 		goto out;
11427eed34f1SOleg Drokin 
11431da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
11441da177e4SLinus Torvalds 		iap->ia_mode = 0;
11451da177e4SLinus Torvalds 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
11461da177e4SLinus Torvalds 
1147088406bcSJ. Bruce Fields 	err = 0;
11484a55c101SJan Kara 	host_err = 0;
11491da177e4SLinus Torvalds 	switch (type) {
11501da177e4SLinus Torvalds 	case S_IFREG:
1151312b63fbSAl Viro 		host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
11524ac35c2fSwengang wang 		if (!host_err)
11534ac35c2fSwengang wang 			nfsd_check_ignore_resizing(iap);
11541da177e4SLinus Torvalds 		break;
11551da177e4SLinus Torvalds 	case S_IFDIR:
11566264d69dSAl Viro 		host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
11571da177e4SLinus Torvalds 		break;
11581da177e4SLinus Torvalds 	case S_IFCHR:
11591da177e4SLinus Torvalds 	case S_IFBLK:
11601da177e4SLinus Torvalds 	case S_IFIFO:
11611da177e4SLinus Torvalds 	case S_IFSOCK:
11626264d69dSAl Viro 		host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
11631da177e4SLinus Torvalds 		break;
116471423274SJ. Bruce Fields 	default:
116571423274SJ. Bruce Fields 		printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
116671423274SJ. Bruce Fields 		       type);
116771423274SJ. Bruce Fields 		host_err = -EINVAL;
1168463c3197SDave Hansen 	}
11694a55c101SJan Kara 	if (host_err < 0)
1170463c3197SDave Hansen 		goto out_nfserr;
11711da177e4SLinus Torvalds 
1172f501912aSBen Myers 	err = nfsd_create_setattr(rqstp, resfhp, iap);
11731da177e4SLinus Torvalds 
1174f501912aSBen Myers 	/*
11750f3a24b4STrond Myklebust 	 * nfsd_create_setattr already committed the child.  Transactional
11760f3a24b4STrond Myklebust 	 * filesystems had a chance to commit changes for both parent and
1177b44061d0SJ. Bruce Fields 	 * child simultaneously making the following commit_metadata a
11780f3a24b4STrond Myklebust 	 * noop.
1179f501912aSBen Myers 	 */
1180f501912aSBen Myers 	err2 = nfserrno(commit_metadata(fhp));
1181f193fbabSYAMAMOTO Takashi 	if (err2)
1182f193fbabSYAMAMOTO Takashi 		err = err2;
11831da177e4SLinus Torvalds 	/*
11841da177e4SLinus Torvalds 	 * Update the file handle to get the new inode info.
11851da177e4SLinus Torvalds 	 */
11861da177e4SLinus Torvalds 	if (!err)
11871da177e4SLinus Torvalds 		err = fh_update(resfhp);
11881da177e4SLinus Torvalds out:
11891da177e4SLinus Torvalds 	dput(dchild);
11901da177e4SLinus Torvalds 	return err;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds out_nfserr:
11936264d69dSAl Viro 	err = nfserrno(host_err);
11941da177e4SLinus Torvalds 	goto out;
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
1197b44061d0SJ. Bruce Fields /*
1198b44061d0SJ. Bruce Fields  * Create a filesystem object (regular, directory, special).
1199b44061d0SJ. Bruce Fields  * Note that the parent directory is left locked.
1200b44061d0SJ. Bruce Fields  *
1201b44061d0SJ. Bruce Fields  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1202b44061d0SJ. Bruce Fields  */
1203b44061d0SJ. Bruce Fields __be32
1204b44061d0SJ. Bruce Fields nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1205b44061d0SJ. Bruce Fields 		char *fname, int flen, struct iattr *iap,
1206b44061d0SJ. Bruce Fields 		int type, dev_t rdev, struct svc_fh *resfhp)
1207b44061d0SJ. Bruce Fields {
1208b44061d0SJ. Bruce Fields 	struct dentry	*dentry, *dchild = NULL;
1209b44061d0SJ. Bruce Fields 	struct inode	*dirp;
1210b44061d0SJ. Bruce Fields 	__be32		err;
1211b44061d0SJ. Bruce Fields 	int		host_err;
1212b44061d0SJ. Bruce Fields 
1213b44061d0SJ. Bruce Fields 	if (isdotent(fname, flen))
1214b44061d0SJ. Bruce Fields 		return nfserr_exist;
1215b44061d0SJ. Bruce Fields 
1216fa08139dSJ. Bruce Fields 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1217b44061d0SJ. Bruce Fields 	if (err)
1218b44061d0SJ. Bruce Fields 		return err;
1219b44061d0SJ. Bruce Fields 
1220b44061d0SJ. Bruce Fields 	dentry = fhp->fh_dentry;
1221b44061d0SJ. Bruce Fields 	dirp = d_inode(dentry);
1222b44061d0SJ. Bruce Fields 
1223b44061d0SJ. Bruce Fields 	host_err = fh_want_write(fhp);
1224b44061d0SJ. Bruce Fields 	if (host_err)
1225b44061d0SJ. Bruce Fields 		return nfserrno(host_err);
1226b44061d0SJ. Bruce Fields 
1227b44061d0SJ. Bruce Fields 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1228b44061d0SJ. Bruce Fields 	dchild = lookup_one_len(fname, dentry, flen);
1229b44061d0SJ. Bruce Fields 	host_err = PTR_ERR(dchild);
1230b44061d0SJ. Bruce Fields 	if (IS_ERR(dchild))
1231b44061d0SJ. Bruce Fields 		return nfserrno(host_err);
1232b44061d0SJ. Bruce Fields 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1233502aa0a5SJosef Bacik 	/*
1234502aa0a5SJosef Bacik 	 * We unconditionally drop our ref to dchild as fh_compose will have
1235502aa0a5SJosef Bacik 	 * already grabbed its own ref for it.
1236502aa0a5SJosef Bacik 	 */
1237b44061d0SJ. Bruce Fields 	dput(dchild);
1238502aa0a5SJosef Bacik 	if (err)
1239b44061d0SJ. Bruce Fields 		return err;
1240b44061d0SJ. Bruce Fields 	return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1241b44061d0SJ. Bruce Fields 					rdev, resfhp);
1242b44061d0SJ. Bruce Fields }
1243b44061d0SJ. Bruce Fields 
12441da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
1245ac6721a1SMi Jinlong 
12461da177e4SLinus Torvalds /*
1247ac6721a1SMi Jinlong  * NFSv3 and NFSv4 version of nfsd_create
12481da177e4SLinus Torvalds  */
12496264d69dSAl Viro __be32
1250ac6721a1SMi Jinlong do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
12511da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
12521da177e4SLinus Torvalds 		struct svc_fh *resfhp, int createmode, u32 *verifier,
1253856121b2SJ. Bruce Fields 	        bool *truncp, bool *created)
12541da177e4SLinus Torvalds {
12551da177e4SLinus Torvalds 	struct dentry	*dentry, *dchild = NULL;
12561da177e4SLinus Torvalds 	struct inode	*dirp;
12576264d69dSAl Viro 	__be32		err;
12586264d69dSAl Viro 	int		host_err;
12591da177e4SLinus Torvalds 	__u32		v_mtime=0, v_atime=0;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	err = nfserr_perm;
12621da177e4SLinus Torvalds 	if (!flen)
12631da177e4SLinus Torvalds 		goto out;
12641da177e4SLinus Torvalds 	err = nfserr_exist;
12651da177e4SLinus Torvalds 	if (isdotent(fname, flen))
12661da177e4SLinus Torvalds 		goto out;
12671da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
12681da177e4SLinus Torvalds 		iap->ia_mode = 0;
12691574dff8SSachin Prabhu 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
12701da177e4SLinus Torvalds 	if (err)
12711da177e4SLinus Torvalds 		goto out;
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
12742b0143b5SDavid Howells 	dirp = d_inode(dentry);
12751da177e4SLinus Torvalds 
12764a55c101SJan Kara 	host_err = fh_want_write(fhp);
12774a55c101SJan Kara 	if (host_err)
12784a55c101SJan Kara 		goto out_nfserr;
12794a55c101SJan Kara 
128012fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	/*
12831da177e4SLinus Torvalds 	 * Compose the response file handle.
12841da177e4SLinus Torvalds 	 */
12851da177e4SLinus Torvalds 	dchild = lookup_one_len(fname, dentry, flen);
12866264d69dSAl Viro 	host_err = PTR_ERR(dchild);
12871da177e4SLinus Torvalds 	if (IS_ERR(dchild))
12881da177e4SLinus Torvalds 		goto out_nfserr;
12891da177e4SLinus Torvalds 
12901574dff8SSachin Prabhu 	/* If file doesn't exist, check for permissions to create one */
12912b0143b5SDavid Howells 	if (d_really_is_negative(dchild)) {
12921574dff8SSachin Prabhu 		err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
12931574dff8SSachin Prabhu 		if (err)
12941574dff8SSachin Prabhu 			goto out;
12951574dff8SSachin Prabhu 	}
12961574dff8SSachin Prabhu 
12971da177e4SLinus Torvalds 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
12981da177e4SLinus Torvalds 	if (err)
12991da177e4SLinus Torvalds 		goto out;
13001da177e4SLinus Torvalds 
1301ac6721a1SMi Jinlong 	if (nfsd_create_is_exclusive(createmode)) {
1302c397852cSPeter Staubach 		/* solaris7 gets confused (bugid 4218508) if these have
1303749997e5SJeff Layton 		 * the high bit set, so just clear the high bits. If this is
1304749997e5SJeff Layton 		 * ever changed to use different attrs for storing the
1305749997e5SJeff Layton 		 * verifier, then do_open_lookup() will also need to be fixed
1306749997e5SJeff Layton 		 * accordingly.
13071da177e4SLinus Torvalds 		 */
13081da177e4SLinus Torvalds 		v_mtime = verifier[0]&0x7fffffff;
13091da177e4SLinus Torvalds 		v_atime = verifier[1]&0x7fffffff;
13101da177e4SLinus Torvalds 	}
13111da177e4SLinus Torvalds 
13122b0143b5SDavid Howells 	if (d_really_is_positive(dchild)) {
13131da177e4SLinus Torvalds 		err = 0;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 		switch (createmode) {
13161da177e4SLinus Torvalds 		case NFS3_CREATE_UNCHECKED:
1317e36cb0b8SDavid Howells 			if (! d_is_reg(dchild))
13189dc4e6c4SJ. Bruce Fields 				goto out;
13191da177e4SLinus Torvalds 			else if (truncp) {
13201da177e4SLinus Torvalds 				/* in nfsv4, we need to treat this case a little
13211da177e4SLinus Torvalds 				 * differently.  we don't want to truncate the
13221da177e4SLinus Torvalds 				 * file now; this would be wrong if the OPEN
13231da177e4SLinus Torvalds 				 * fails for some other reason.  furthermore,
13241da177e4SLinus Torvalds 				 * if the size is nonzero, we should ignore it
13251da177e4SLinus Torvalds 				 * according to spec!
13261da177e4SLinus Torvalds 				 */
13271da177e4SLinus Torvalds 				*truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
13281da177e4SLinus Torvalds 			}
13291da177e4SLinus Torvalds 			else {
13301da177e4SLinus Torvalds 				iap->ia_valid &= ATTR_SIZE;
13311da177e4SLinus Torvalds 				goto set_attr;
13321da177e4SLinus Torvalds 			}
13331da177e4SLinus Torvalds 			break;
13341da177e4SLinus Torvalds 		case NFS3_CREATE_EXCLUSIVE:
13352b0143b5SDavid Howells 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
13362b0143b5SDavid Howells 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
13372b0143b5SDavid Howells 			    && d_inode(dchild)->i_size  == 0 ) {
13387007c90fSNeil Brown 				if (created)
13397007c90fSNeil Brown 					*created = 1;
13401da177e4SLinus Torvalds 				break;
13417007c90fSNeil Brown 			}
1342ac6721a1SMi Jinlong 		case NFS4_CREATE_EXCLUSIVE4_1:
13432b0143b5SDavid Howells 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
13442b0143b5SDavid Howells 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
13452b0143b5SDavid Howells 			    && d_inode(dchild)->i_size  == 0 ) {
13467007c90fSNeil Brown 				if (created)
13477007c90fSNeil Brown 					*created = 1;
1348ac6721a1SMi Jinlong 				goto set_attr;
13497007c90fSNeil Brown 			}
13501da177e4SLinus Torvalds 			 /* fallthru */
13511da177e4SLinus Torvalds 		case NFS3_CREATE_GUARDED:
13521da177e4SLinus Torvalds 			err = nfserr_exist;
13531da177e4SLinus Torvalds 		}
1354bad0dcffSAl Viro 		fh_drop_write(fhp);
13551da177e4SLinus Torvalds 		goto out;
13561da177e4SLinus Torvalds 	}
13571da177e4SLinus Torvalds 
1358312b63fbSAl Viro 	host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1359463c3197SDave Hansen 	if (host_err < 0) {
1360bad0dcffSAl Viro 		fh_drop_write(fhp);
13611da177e4SLinus Torvalds 		goto out_nfserr;
1362463c3197SDave Hansen 	}
136381ac95c5SJ. Bruce Fields 	if (created)
136481ac95c5SJ. Bruce Fields 		*created = 1;
13651da177e4SLinus Torvalds 
13664ac35c2fSwengang wang 	nfsd_check_ignore_resizing(iap);
13674ac35c2fSwengang wang 
1368ac6721a1SMi Jinlong 	if (nfsd_create_is_exclusive(createmode)) {
1369c397852cSPeter Staubach 		/* Cram the verifier into atime/mtime */
13701da177e4SLinus Torvalds 		iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1371c397852cSPeter Staubach 			| ATTR_MTIME_SET|ATTR_ATIME_SET;
13721da177e4SLinus Torvalds 		/* XXX someone who knows this better please fix it for nsec */
13731da177e4SLinus Torvalds 		iap->ia_mtime.tv_sec = v_mtime;
13741da177e4SLinus Torvalds 		iap->ia_atime.tv_sec = v_atime;
13751da177e4SLinus Torvalds 		iap->ia_mtime.tv_nsec = 0;
13761da177e4SLinus Torvalds 		iap->ia_atime.tv_nsec = 0;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds  set_attr:
1380f501912aSBen Myers 	err = nfsd_create_setattr(rqstp, resfhp, iap);
1381f501912aSBen Myers 
1382f501912aSBen Myers 	/*
13830f3a24b4STrond Myklebust 	 * nfsd_create_setattr already committed the child
13840f3a24b4STrond Myklebust 	 * (and possibly also the parent).
1385f501912aSBen Myers 	 */
1386f501912aSBen Myers 	if (!err)
1387f501912aSBen Myers 		err = nfserrno(commit_metadata(fhp));
1388f193fbabSYAMAMOTO Takashi 
1389f193fbabSYAMAMOTO Takashi 	/*
1390f193fbabSYAMAMOTO Takashi 	 * Update the filehandle to get the new inode info.
1391f193fbabSYAMAMOTO Takashi 	 */
1392f193fbabSYAMAMOTO Takashi 	if (!err)
1393f193fbabSYAMAMOTO Takashi 		err = fh_update(resfhp);
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds  out:
13961da177e4SLinus Torvalds 	fh_unlock(fhp);
13971da177e4SLinus Torvalds 	if (dchild && !IS_ERR(dchild))
13981da177e4SLinus Torvalds 		dput(dchild);
13994a55c101SJan Kara 	fh_drop_write(fhp);
14001da177e4SLinus Torvalds  	return err;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds  out_nfserr:
14036264d69dSAl Viro 	err = nfserrno(host_err);
14041da177e4SLinus Torvalds 	goto out;
14051da177e4SLinus Torvalds }
14061da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds /*
14091da177e4SLinus Torvalds  * Read a symlink. On entry, *lenp must contain the maximum path length that
14101da177e4SLinus Torvalds  * fits into the buffer. On return, it contains the true length.
14111da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
14121da177e4SLinus Torvalds  */
14136264d69dSAl Viro __be32
14141da177e4SLinus Torvalds nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
14151da177e4SLinus Torvalds {
14161da177e4SLinus Torvalds 	mm_segment_t	oldfs;
14176264d69dSAl Viro 	__be32		err;
14186264d69dSAl Viro 	int		host_err;
141968ac1234SAl Viro 	struct path path;
14201da177e4SLinus Torvalds 
14218837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
14221da177e4SLinus Torvalds 	if (err)
14231da177e4SLinus Torvalds 		goto out;
14241da177e4SLinus Torvalds 
142568ac1234SAl Viro 	path.mnt = fhp->fh_export->ex_path.mnt;
142668ac1234SAl Viro 	path.dentry = fhp->fh_dentry;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	err = nfserr_inval;
1429fd4a0edfSMiklos Szeredi 	if (!d_is_symlink(path.dentry))
14301da177e4SLinus Torvalds 		goto out;
14311da177e4SLinus Torvalds 
143268ac1234SAl Viro 	touch_atime(&path);
14331da177e4SLinus Torvalds 	/* N.B. Why does this call need a get_fs()??
14341da177e4SLinus Torvalds 	 * Remove the set_fs and watch the fireworks:-) --okir
14351da177e4SLinus Torvalds 	 */
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	oldfs = get_fs(); set_fs(KERNEL_DS);
1438fd4a0edfSMiklos Szeredi 	host_err = vfs_readlink(path.dentry, (char __user *)buf, *lenp);
14391da177e4SLinus Torvalds 	set_fs(oldfs);
14401da177e4SLinus Torvalds 
14416264d69dSAl Viro 	if (host_err < 0)
14421da177e4SLinus Torvalds 		goto out_nfserr;
14436264d69dSAl Viro 	*lenp = host_err;
14441da177e4SLinus Torvalds 	err = 0;
14451da177e4SLinus Torvalds out:
14461da177e4SLinus Torvalds 	return err;
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds out_nfserr:
14496264d69dSAl Viro 	err = nfserrno(host_err);
14501da177e4SLinus Torvalds 	goto out;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds /*
14541da177e4SLinus Torvalds  * Create a symlink and look up its inode
14551da177e4SLinus Torvalds  * N.B. After this call _both_ fhp and resfhp need an fh_put
14561da177e4SLinus Torvalds  */
14576264d69dSAl Viro __be32
14581da177e4SLinus Torvalds nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
14591da177e4SLinus Torvalds 				char *fname, int flen,
146052ee0433SJ. Bruce Fields 				char *path,
14611e444f5bSKinglong Mee 				struct svc_fh *resfhp)
14621da177e4SLinus Torvalds {
14631da177e4SLinus Torvalds 	struct dentry	*dentry, *dnew;
14646264d69dSAl Viro 	__be32		err, cerr;
14656264d69dSAl Viro 	int		host_err;
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	err = nfserr_noent;
146852ee0433SJ. Bruce Fields 	if (!flen || path[0] == '\0')
14691da177e4SLinus Torvalds 		goto out;
14701da177e4SLinus Torvalds 	err = nfserr_exist;
14711da177e4SLinus Torvalds 	if (isdotent(fname, flen))
14721da177e4SLinus Torvalds 		goto out;
14731da177e4SLinus Torvalds 
14748837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
14751da177e4SLinus Torvalds 	if (err)
14761da177e4SLinus Torvalds 		goto out;
14774a55c101SJan Kara 
14784a55c101SJan Kara 	host_err = fh_want_write(fhp);
14794a55c101SJan Kara 	if (host_err)
14804a55c101SJan Kara 		goto out_nfserr;
14814a55c101SJan Kara 
14821da177e4SLinus Torvalds 	fh_lock(fhp);
14831da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
14841da177e4SLinus Torvalds 	dnew = lookup_one_len(fname, dentry, flen);
14856264d69dSAl Viro 	host_err = PTR_ERR(dnew);
14861da177e4SLinus Torvalds 	if (IS_ERR(dnew))
14871da177e4SLinus Torvalds 		goto out_nfserr;
14881da177e4SLinus Torvalds 
14892b0143b5SDavid Howells 	host_err = vfs_symlink(d_inode(dentry), dnew, path);
14906264d69dSAl Viro 	err = nfserrno(host_err);
1491f501912aSBen Myers 	if (!err)
1492f501912aSBen Myers 		err = nfserrno(commit_metadata(fhp));
14931da177e4SLinus Torvalds 	fh_unlock(fhp);
14941da177e4SLinus Torvalds 
1495bad0dcffSAl Viro 	fh_drop_write(fhp);
149675c3f29dSDave Hansen 
14971da177e4SLinus Torvalds 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
14981da177e4SLinus Torvalds 	dput(dnew);
14991da177e4SLinus Torvalds 	if (err==0) err = cerr;
15001da177e4SLinus Torvalds out:
15011da177e4SLinus Torvalds 	return err;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds out_nfserr:
15046264d69dSAl Viro 	err = nfserrno(host_err);
15051da177e4SLinus Torvalds 	goto out;
15061da177e4SLinus Torvalds }
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds /*
15091da177e4SLinus Torvalds  * Create a hardlink
15101da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
15111da177e4SLinus Torvalds  */
15126264d69dSAl Viro __be32
15131da177e4SLinus Torvalds nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
15141da177e4SLinus Torvalds 				char *name, int len, struct svc_fh *tfhp)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	struct dentry	*ddir, *dnew, *dold;
151755b13354SJ. Bruce Fields 	struct inode	*dirp;
15186264d69dSAl Viro 	__be32		err;
15196264d69dSAl Viro 	int		host_err;
15201da177e4SLinus Torvalds 
15218837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
15221da177e4SLinus Torvalds 	if (err)
15231da177e4SLinus Torvalds 		goto out;
15247d818a7bSJ. Bruce Fields 	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
15251da177e4SLinus Torvalds 	if (err)
15261da177e4SLinus Torvalds 		goto out;
15277d818a7bSJ. Bruce Fields 	err = nfserr_isdir;
1528e36cb0b8SDavid Howells 	if (d_is_dir(tfhp->fh_dentry))
15297d818a7bSJ. Bruce Fields 		goto out;
15301da177e4SLinus Torvalds 	err = nfserr_perm;
15311da177e4SLinus Torvalds 	if (!len)
15321da177e4SLinus Torvalds 		goto out;
15331da177e4SLinus Torvalds 	err = nfserr_exist;
15341da177e4SLinus Torvalds 	if (isdotent(name, len))
15351da177e4SLinus Torvalds 		goto out;
15361da177e4SLinus Torvalds 
15374a55c101SJan Kara 	host_err = fh_want_write(tfhp);
15384a55c101SJan Kara 	if (host_err) {
15394a55c101SJan Kara 		err = nfserrno(host_err);
15404a55c101SJan Kara 		goto out;
15414a55c101SJan Kara 	}
15424a55c101SJan Kara 
154312fd3520SPeter Zijlstra 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
15441da177e4SLinus Torvalds 	ddir = ffhp->fh_dentry;
15452b0143b5SDavid Howells 	dirp = d_inode(ddir);
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	dnew = lookup_one_len(name, ddir, len);
15486264d69dSAl Viro 	host_err = PTR_ERR(dnew);
15491da177e4SLinus Torvalds 	if (IS_ERR(dnew))
15501da177e4SLinus Torvalds 		goto out_nfserr;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	dold = tfhp->fh_dentry;
15531da177e4SLinus Torvalds 
15544795bb37SJ. Bruce Fields 	err = nfserr_noent;
15552b0143b5SDavid Howells 	if (d_really_is_negative(dold))
15564a55c101SJan Kara 		goto out_dput;
1557146a8595SJ. Bruce Fields 	host_err = vfs_link(dold, dirp, dnew, NULL);
15586264d69dSAl Viro 	if (!host_err) {
1559f501912aSBen Myers 		err = nfserrno(commit_metadata(ffhp));
1560f501912aSBen Myers 		if (!err)
1561f501912aSBen Myers 			err = nfserrno(commit_metadata(tfhp));
15621da177e4SLinus Torvalds 	} else {
15636264d69dSAl Viro 		if (host_err == -EXDEV && rqstp->rq_vers == 2)
15641da177e4SLinus Torvalds 			err = nfserr_acces;
15651da177e4SLinus Torvalds 		else
15666264d69dSAl Viro 			err = nfserrno(host_err);
15671da177e4SLinus Torvalds 	}
156875c3f29dSDave Hansen out_dput:
15691da177e4SLinus Torvalds 	dput(dnew);
1570270d56e5SDavid M. Richter out_unlock:
1571270d56e5SDavid M. Richter 	fh_unlock(ffhp);
15724a55c101SJan Kara 	fh_drop_write(tfhp);
15731da177e4SLinus Torvalds out:
15741da177e4SLinus Torvalds 	return err;
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds out_nfserr:
15776264d69dSAl Viro 	err = nfserrno(host_err);
1578270d56e5SDavid M. Richter 	goto out_unlock;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds /*
15821da177e4SLinus Torvalds  * Rename a file
15831da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
15841da177e4SLinus Torvalds  */
15856264d69dSAl Viro __be32
15861da177e4SLinus Torvalds nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
15871da177e4SLinus Torvalds 			    struct svc_fh *tfhp, char *tname, int tlen)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
15901da177e4SLinus Torvalds 	struct inode	*fdir, *tdir;
15916264d69dSAl Viro 	__be32		err;
15926264d69dSAl Viro 	int		host_err;
15931da177e4SLinus Torvalds 
15948837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
15951da177e4SLinus Torvalds 	if (err)
15961da177e4SLinus Torvalds 		goto out;
15978837abcaSMiklos Szeredi 	err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
15981da177e4SLinus Torvalds 	if (err)
15991da177e4SLinus Torvalds 		goto out;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	fdentry = ffhp->fh_dentry;
16022b0143b5SDavid Howells 	fdir = d_inode(fdentry);
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	tdentry = tfhp->fh_dentry;
16052b0143b5SDavid Howells 	tdir = d_inode(tdentry);
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	err = nfserr_perm;
16081da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
16091da177e4SLinus Torvalds 		goto out;
16101da177e4SLinus Torvalds 
16114a55c101SJan Kara 	host_err = fh_want_write(ffhp);
16124a55c101SJan Kara 	if (host_err) {
16134a55c101SJan Kara 		err = nfserrno(host_err);
16144a55c101SJan Kara 		goto out;
16154a55c101SJan Kara 	}
16164a55c101SJan Kara 
16171da177e4SLinus Torvalds 	/* cannot use fh_lock as we need deadlock protective ordering
16181da177e4SLinus Torvalds 	 * so do it by hand */
16191da177e4SLinus Torvalds 	trap = lock_rename(tdentry, fdentry);
1620aaf91ec1SJeff Layton 	ffhp->fh_locked = tfhp->fh_locked = true;
16211da177e4SLinus Torvalds 	fill_pre_wcc(ffhp);
16221da177e4SLinus Torvalds 	fill_pre_wcc(tfhp);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	odentry = lookup_one_len(fname, fdentry, flen);
16256264d69dSAl Viro 	host_err = PTR_ERR(odentry);
16261da177e4SLinus Torvalds 	if (IS_ERR(odentry))
16271da177e4SLinus Torvalds 		goto out_nfserr;
16281da177e4SLinus Torvalds 
16296264d69dSAl Viro 	host_err = -ENOENT;
16302b0143b5SDavid Howells 	if (d_really_is_negative(odentry))
16311da177e4SLinus Torvalds 		goto out_dput_old;
16326264d69dSAl Viro 	host_err = -EINVAL;
16331da177e4SLinus Torvalds 	if (odentry == trap)
16341da177e4SLinus Torvalds 		goto out_dput_old;
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	ndentry = lookup_one_len(tname, tdentry, tlen);
16376264d69dSAl Viro 	host_err = PTR_ERR(ndentry);
16381da177e4SLinus Torvalds 	if (IS_ERR(ndentry))
16391da177e4SLinus Torvalds 		goto out_dput_old;
16406264d69dSAl Viro 	host_err = -ENOTEMPTY;
16411da177e4SLinus Torvalds 	if (ndentry == trap)
16421da177e4SLinus Torvalds 		goto out_dput_new;
16431da177e4SLinus Torvalds 
16449079b1ebSDave Hansen 	host_err = -EXDEV;
16459079b1ebSDave Hansen 	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
16469079b1ebSDave Hansen 		goto out_dput_new;
1647aa387d6cSJ. Bruce Fields 	if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1648aa387d6cSJ. Bruce Fields 		goto out_dput_new;
16499079b1ebSDave Hansen 
1650520c8b16SMiklos Szeredi 	host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
1651f501912aSBen Myers 	if (!host_err) {
1652f501912aSBen Myers 		host_err = commit_metadata(tfhp);
16536264d69dSAl Viro 		if (!host_err)
1654f501912aSBen Myers 			host_err = commit_metadata(ffhp);
16551da177e4SLinus Torvalds 	}
16561da177e4SLinus Torvalds  out_dput_new:
16571da177e4SLinus Torvalds 	dput(ndentry);
16581da177e4SLinus Torvalds  out_dput_old:
16591da177e4SLinus Torvalds 	dput(odentry);
16601da177e4SLinus Torvalds  out_nfserr:
16616264d69dSAl Viro 	err = nfserrno(host_err);
1662fbb74a34SJ. Bruce Fields 	/*
1663fbb74a34SJ. Bruce Fields 	 * We cannot rely on fh_unlock on the two filehandles,
16641da177e4SLinus Torvalds 	 * as that would do the wrong thing if the two directories
1665fbb74a34SJ. Bruce Fields 	 * were the same, so again we do it by hand.
16661da177e4SLinus Torvalds 	 */
16671da177e4SLinus Torvalds 	fill_post_wcc(ffhp);
16681da177e4SLinus Torvalds 	fill_post_wcc(tfhp);
16691da177e4SLinus Torvalds 	unlock_rename(tdentry, fdentry);
1670aaf91ec1SJeff Layton 	ffhp->fh_locked = tfhp->fh_locked = false;
16714a55c101SJan Kara 	fh_drop_write(ffhp);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds out:
16741da177e4SLinus Torvalds 	return err;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds /*
16781da177e4SLinus Torvalds  * Unlink a file or directory
16791da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
16801da177e4SLinus Torvalds  */
16816264d69dSAl Viro __be32
16821da177e4SLinus Torvalds nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
16831da177e4SLinus Torvalds 				char *fname, int flen)
16841da177e4SLinus Torvalds {
16851da177e4SLinus Torvalds 	struct dentry	*dentry, *rdentry;
16861da177e4SLinus Torvalds 	struct inode	*dirp;
16876264d69dSAl Viro 	__be32		err;
16886264d69dSAl Viro 	int		host_err;
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 	err = nfserr_acces;
16911da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen))
16921da177e4SLinus Torvalds 		goto out;
16938837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
16941da177e4SLinus Torvalds 	if (err)
16951da177e4SLinus Torvalds 		goto out;
16961da177e4SLinus Torvalds 
16974a55c101SJan Kara 	host_err = fh_want_write(fhp);
16984a55c101SJan Kara 	if (host_err)
16994a55c101SJan Kara 		goto out_nfserr;
17004a55c101SJan Kara 
170112fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
17021da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
17032b0143b5SDavid Howells 	dirp = d_inode(dentry);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	rdentry = lookup_one_len(fname, dentry, flen);
17066264d69dSAl Viro 	host_err = PTR_ERR(rdentry);
17071da177e4SLinus Torvalds 	if (IS_ERR(rdentry))
17081da177e4SLinus Torvalds 		goto out_nfserr;
17091da177e4SLinus Torvalds 
17102b0143b5SDavid Howells 	if (d_really_is_negative(rdentry)) {
17111da177e4SLinus Torvalds 		dput(rdentry);
17121da177e4SLinus Torvalds 		err = nfserr_noent;
17131da177e4SLinus Torvalds 		goto out;
17141da177e4SLinus Torvalds 	}
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	if (!type)
17172b0143b5SDavid Howells 		type = d_inode(rdentry)->i_mode & S_IFMT;
17181da177e4SLinus Torvalds 
17199ce137eeSJ. Bruce Fields 	if (type != S_IFDIR)
1720b21996e3SJ. Bruce Fields 		host_err = vfs_unlink(dirp, rdentry, NULL);
17219ce137eeSJ. Bruce Fields 	else
17226264d69dSAl Viro 		host_err = vfs_rmdir(dirp, rdentry);
1723541ce98cSJ. Bruce Fields 	if (!host_err)
1724541ce98cSJ. Bruce Fields 		host_err = commit_metadata(fhp);
17251da177e4SLinus Torvalds 	dput(rdentry);
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds out_nfserr:
17286264d69dSAl Viro 	err = nfserrno(host_err);
1729f193fbabSYAMAMOTO Takashi out:
1730f193fbabSYAMAMOTO Takashi 	return err;
17311da177e4SLinus Torvalds }
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds /*
173414f7dd63SDavid Woodhouse  * We do this buffering because we must not call back into the file
173514f7dd63SDavid Woodhouse  * system's ->lookup() method from the filldir callback. That may well
173614f7dd63SDavid Woodhouse  * deadlock a number of file systems.
173714f7dd63SDavid Woodhouse  *
173814f7dd63SDavid Woodhouse  * This is based heavily on the implementation of same in XFS.
173914f7dd63SDavid Woodhouse  */
174014f7dd63SDavid Woodhouse struct buffered_dirent {
174114f7dd63SDavid Woodhouse 	u64		ino;
174214f7dd63SDavid Woodhouse 	loff_t		offset;
174314f7dd63SDavid Woodhouse 	int		namlen;
174414f7dd63SDavid Woodhouse 	unsigned int	d_type;
174514f7dd63SDavid Woodhouse 	char		name[];
174614f7dd63SDavid Woodhouse };
174714f7dd63SDavid Woodhouse 
174814f7dd63SDavid Woodhouse struct readdir_data {
17495c0ba4e0SAl Viro 	struct dir_context ctx;
175014f7dd63SDavid Woodhouse 	char		*dirent;
175114f7dd63SDavid Woodhouse 	size_t		used;
175253c9c5c0SAl Viro 	int		full;
175314f7dd63SDavid Woodhouse };
175414f7dd63SDavid Woodhouse 
1755ac7576f4SMiklos Szeredi static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1756ac7576f4SMiklos Szeredi 				 int namlen, loff_t offset, u64 ino,
1757ac7576f4SMiklos Szeredi 				 unsigned int d_type)
175814f7dd63SDavid Woodhouse {
1759ac7576f4SMiklos Szeredi 	struct readdir_data *buf =
1760ac7576f4SMiklos Szeredi 		container_of(ctx, struct readdir_data, ctx);
176114f7dd63SDavid Woodhouse 	struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
176214f7dd63SDavid Woodhouse 	unsigned int reclen;
176314f7dd63SDavid Woodhouse 
176414f7dd63SDavid Woodhouse 	reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
176553c9c5c0SAl Viro 	if (buf->used + reclen > PAGE_SIZE) {
176653c9c5c0SAl Viro 		buf->full = 1;
176714f7dd63SDavid Woodhouse 		return -EINVAL;
176853c9c5c0SAl Viro 	}
176914f7dd63SDavid Woodhouse 
177014f7dd63SDavid Woodhouse 	de->namlen = namlen;
177114f7dd63SDavid Woodhouse 	de->offset = offset;
177214f7dd63SDavid Woodhouse 	de->ino = ino;
177314f7dd63SDavid Woodhouse 	de->d_type = d_type;
177414f7dd63SDavid Woodhouse 	memcpy(de->name, name, namlen);
177514f7dd63SDavid Woodhouse 	buf->used += reclen;
177614f7dd63SDavid Woodhouse 
177714f7dd63SDavid Woodhouse 	return 0;
177814f7dd63SDavid Woodhouse }
177914f7dd63SDavid Woodhouse 
1780ac7576f4SMiklos Szeredi static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
17812628b766SDavid Woodhouse 				    struct readdir_cd *cdp, loff_t *offsetp)
17822628b766SDavid Woodhouse {
178314f7dd63SDavid Woodhouse 	struct buffered_dirent *de;
17842628b766SDavid Woodhouse 	int host_err;
178514f7dd63SDavid Woodhouse 	int size;
178614f7dd63SDavid Woodhouse 	loff_t offset;
1787ac6614b7SAl Viro 	struct readdir_data buf = {
1788ac6614b7SAl Viro 		.ctx.actor = nfsd_buffered_filldir,
1789ac6614b7SAl Viro 		.dirent = (void *)__get_free_page(GFP_KERNEL)
1790ac6614b7SAl Viro 	};
17912628b766SDavid Woodhouse 
179214f7dd63SDavid Woodhouse 	if (!buf.dirent)
17932f9092e1SDavid Woodhouse 		return nfserrno(-ENOMEM);
179414f7dd63SDavid Woodhouse 
179514f7dd63SDavid Woodhouse 	offset = *offsetp;
17962628b766SDavid Woodhouse 
179714f7dd63SDavid Woodhouse 	while (1) {
179814f7dd63SDavid Woodhouse 		unsigned int reclen;
179914f7dd63SDavid Woodhouse 
1800b726e923SDoug Nazar 		cdp->err = nfserr_eof; /* will be cleared on successful read */
180114f7dd63SDavid Woodhouse 		buf.used = 0;
180253c9c5c0SAl Viro 		buf.full = 0;
180314f7dd63SDavid Woodhouse 
18045c0ba4e0SAl Viro 		host_err = iterate_dir(file, &buf.ctx);
180553c9c5c0SAl Viro 		if (buf.full)
180653c9c5c0SAl Viro 			host_err = 0;
180753c9c5c0SAl Viro 
180853c9c5c0SAl Viro 		if (host_err < 0)
180914f7dd63SDavid Woodhouse 			break;
181014f7dd63SDavid Woodhouse 
181114f7dd63SDavid Woodhouse 		size = buf.used;
181214f7dd63SDavid Woodhouse 
181314f7dd63SDavid Woodhouse 		if (!size)
181414f7dd63SDavid Woodhouse 			break;
181514f7dd63SDavid Woodhouse 
181614f7dd63SDavid Woodhouse 		de = (struct buffered_dirent *)buf.dirent;
181714f7dd63SDavid Woodhouse 		while (size > 0) {
181814f7dd63SDavid Woodhouse 			offset = de->offset;
181914f7dd63SDavid Woodhouse 
182014f7dd63SDavid Woodhouse 			if (func(cdp, de->name, de->namlen, de->offset,
182114f7dd63SDavid Woodhouse 				 de->ino, de->d_type))
18222f9092e1SDavid Woodhouse 				break;
182314f7dd63SDavid Woodhouse 
182414f7dd63SDavid Woodhouse 			if (cdp->err != nfs_ok)
18252f9092e1SDavid Woodhouse 				break;
182614f7dd63SDavid Woodhouse 
182714f7dd63SDavid Woodhouse 			reclen = ALIGN(sizeof(*de) + de->namlen,
182814f7dd63SDavid Woodhouse 				       sizeof(u64));
182914f7dd63SDavid Woodhouse 			size -= reclen;
183014f7dd63SDavid Woodhouse 			de = (struct buffered_dirent *)((char *)de + reclen);
183114f7dd63SDavid Woodhouse 		}
18322f9092e1SDavid Woodhouse 		if (size > 0) /* We bailed out early */
18332f9092e1SDavid Woodhouse 			break;
18342f9092e1SDavid Woodhouse 
1835c002a6c7SDavid Woodhouse 		offset = vfs_llseek(file, 0, SEEK_CUR);
183614f7dd63SDavid Woodhouse 	}
183714f7dd63SDavid Woodhouse 
183814f7dd63SDavid Woodhouse 	free_page((unsigned long)(buf.dirent));
18392628b766SDavid Woodhouse 
18402628b766SDavid Woodhouse 	if (host_err)
18412628b766SDavid Woodhouse 		return nfserrno(host_err);
184214f7dd63SDavid Woodhouse 
184314f7dd63SDavid Woodhouse 	*offsetp = offset;
18442628b766SDavid Woodhouse 	return cdp->err;
18452628b766SDavid Woodhouse }
18462628b766SDavid Woodhouse 
18471da177e4SLinus Torvalds /*
18481da177e4SLinus Torvalds  * Read entries from a directory.
18491da177e4SLinus Torvalds  * The  NFSv3/4 verifier we ignore for now.
18501da177e4SLinus Torvalds  */
18516264d69dSAl Viro __be32
18521da177e4SLinus Torvalds nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1853ac7576f4SMiklos Szeredi 	     struct readdir_cd *cdp, nfsd_filldir_t func)
18541da177e4SLinus Torvalds {
18556264d69dSAl Viro 	__be32		err;
18561da177e4SLinus Torvalds 	struct file	*file;
18571da177e4SLinus Torvalds 	loff_t		offset = *offsetp;
185806effdbbSBernd Schubert 	int             may_flags = NFSD_MAY_READ;
18591da177e4SLinus Torvalds 
186006effdbbSBernd Schubert 	/* NFSv2 only supports 32 bit cookies */
186106effdbbSBernd Schubert 	if (rqstp->rq_vers > 2)
186206effdbbSBernd Schubert 		may_flags |= NFSD_MAY_64BIT_COOKIE;
186306effdbbSBernd Schubert 
186406effdbbSBernd Schubert 	err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
18651da177e4SLinus Torvalds 	if (err)
18661da177e4SLinus Torvalds 		goto out;
18671da177e4SLinus Torvalds 
1868b108fe6bSJeff Layton 	offset = vfs_llseek(file, offset, SEEK_SET);
18691da177e4SLinus Torvalds 	if (offset < 0) {
18701da177e4SLinus Torvalds 		err = nfserrno((int)offset);
18711da177e4SLinus Torvalds 		goto out_close;
18721da177e4SLinus Torvalds 	}
18731da177e4SLinus Torvalds 
187414f7dd63SDavid Woodhouse 	err = nfsd_buffered_readdir(file, func, cdp, offsetp);
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	if (err == nfserr_eof || err == nfserr_toosmall)
18771da177e4SLinus Torvalds 		err = nfs_ok; /* can still be found in ->err */
18781da177e4SLinus Torvalds out_close:
1879fd891454SChristoph Hellwig 	fput(file);
18801da177e4SLinus Torvalds out:
18811da177e4SLinus Torvalds 	return err;
18821da177e4SLinus Torvalds }
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds /*
18851da177e4SLinus Torvalds  * Get file system stats
18861da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
18871da177e4SLinus Torvalds  */
18886264d69dSAl Viro __be32
188904716e66SJ. Bruce Fields nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
18901da177e4SLinus Torvalds {
1891f6360efbSTakashi Iwai 	__be32 err;
1892f6360efbSTakashi Iwai 
1893f6360efbSTakashi Iwai 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
1894f6360efbSTakashi Iwai 	if (!err) {
1895ebabe9a9SChristoph Hellwig 		struct path path = {
1896ebabe9a9SChristoph Hellwig 			.mnt	= fhp->fh_export->ex_path.mnt,
1897ebabe9a9SChristoph Hellwig 			.dentry	= fhp->fh_dentry,
1898ebabe9a9SChristoph Hellwig 		};
1899f6360efbSTakashi Iwai 		if (vfs_statfs(&path, stat))
19001da177e4SLinus Torvalds 			err = nfserr_io;
1901f6360efbSTakashi Iwai 	}
19021da177e4SLinus Torvalds 	return err;
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds 
1905c7d51402SJ. Bruce Fields static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
1906e22841c6SJ. Bruce Fields {
1907c7d51402SJ. Bruce Fields 	return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
1908e22841c6SJ. Bruce Fields }
1909e22841c6SJ. Bruce Fields 
19101da177e4SLinus Torvalds /*
19111da177e4SLinus Torvalds  * Check for a user's access permissions to this inode.
19121da177e4SLinus Torvalds  */
19136264d69dSAl Viro __be32
19140ec757dfSJ. Bruce Fields nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
19150ec757dfSJ. Bruce Fields 					struct dentry *dentry, int acc)
19161da177e4SLinus Torvalds {
19172b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
19181da177e4SLinus Torvalds 	int		err;
19191da177e4SLinus Torvalds 
1920aea93397SJ. Bruce Fields 	if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
19211da177e4SLinus Torvalds 		return 0;
19221da177e4SLinus Torvalds #if 0
19231da177e4SLinus Torvalds 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
19241da177e4SLinus Torvalds 		acc,
19258837abcaSMiklos Szeredi 		(acc & NFSD_MAY_READ)?	" read"  : "",
19268837abcaSMiklos Szeredi 		(acc & NFSD_MAY_WRITE)?	" write" : "",
19278837abcaSMiklos Szeredi 		(acc & NFSD_MAY_EXEC)?	" exec"  : "",
19288837abcaSMiklos Szeredi 		(acc & NFSD_MAY_SATTR)?	" sattr" : "",
19298837abcaSMiklos Szeredi 		(acc & NFSD_MAY_TRUNC)?	" trunc" : "",
19308837abcaSMiklos Szeredi 		(acc & NFSD_MAY_LOCK)?	" lock"  : "",
19318837abcaSMiklos Szeredi 		(acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
19321da177e4SLinus Torvalds 		inode->i_mode,
19331da177e4SLinus Torvalds 		IS_IMMUTABLE(inode)?	" immut" : "",
19341da177e4SLinus Torvalds 		IS_APPEND(inode)?	" append" : "",
19352c463e95SDave Hansen 		__mnt_is_readonly(exp->ex_path.mnt)?	" ro" : "");
19361da177e4SLinus Torvalds 	dprintk("      owner %d/%d user %d/%d\n",
19375cc0a840SDavid Howells 		inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
19381da177e4SLinus Torvalds #endif
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	/* Normally we reject any write/sattr etc access on a read-only file
19411da177e4SLinus Torvalds 	 * system.  But if it is IRIX doing check on write-access for a
19421da177e4SLinus Torvalds 	 * device special file, we ignore rofs.
19431da177e4SLinus Torvalds 	 */
19448837abcaSMiklos Szeredi 	if (!(acc & NFSD_MAY_LOCAL_ACCESS))
19458837abcaSMiklos Szeredi 		if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
19462c463e95SDave Hansen 			if (exp_rdonly(rqstp, exp) ||
19472c463e95SDave Hansen 			    __mnt_is_readonly(exp->ex_path.mnt))
19481da177e4SLinus Torvalds 				return nfserr_rofs;
19498837abcaSMiklos Szeredi 			if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
19501da177e4SLinus Torvalds 				return nfserr_perm;
19511da177e4SLinus Torvalds 		}
19528837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
19531da177e4SLinus Torvalds 		return nfserr_perm;
19541da177e4SLinus Torvalds 
19558837abcaSMiklos Szeredi 	if (acc & NFSD_MAY_LOCK) {
19561da177e4SLinus Torvalds 		/* If we cannot rely on authentication in NLM requests,
19571da177e4SLinus Torvalds 		 * just allow locks, otherwise require read permission, or
19581da177e4SLinus Torvalds 		 * ownership
19591da177e4SLinus Torvalds 		 */
19601da177e4SLinus Torvalds 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
19611da177e4SLinus Torvalds 			return 0;
19621da177e4SLinus Torvalds 		else
19638837abcaSMiklos Szeredi 			acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
19641da177e4SLinus Torvalds 	}
19651da177e4SLinus Torvalds 	/*
19661da177e4SLinus Torvalds 	 * The file owner always gets access permission for accesses that
19671da177e4SLinus Torvalds 	 * would normally be checked at open time. This is to make
19681da177e4SLinus Torvalds 	 * file access work even when the client has done a fchmod(fd, 0).
19691da177e4SLinus Torvalds 	 *
19701da177e4SLinus Torvalds 	 * However, `cp foo bar' should fail nevertheless when bar is
19711da177e4SLinus Torvalds 	 * readonly. A sensible way to do this might be to reject all
19721da177e4SLinus Torvalds 	 * attempts to truncate a read-only file, because a creat() call
19731da177e4SLinus Torvalds 	 * always implies file truncation.
19741da177e4SLinus Torvalds 	 * ... but this isn't really fair.  A process may reasonably call
19751da177e4SLinus Torvalds 	 * ftruncate on an open file descriptor on a file with perm 000.
19761da177e4SLinus Torvalds 	 * We must trust the client to do permission checking - using "ACCESS"
19771da177e4SLinus Torvalds 	 * with NFSv3.
19781da177e4SLinus Torvalds 	 */
19798837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
19806fab8779SEric W. Biederman 	    uid_eq(inode->i_uid, current_fsuid()))
19811da177e4SLinus Torvalds 		return 0;
19821da177e4SLinus Torvalds 
19838837abcaSMiklos Szeredi 	/* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
1984f419a2e3SAl Viro 	err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 	/* Allow read access to binaries even when mode 111 */
19871da177e4SLinus Torvalds 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
1988a043226bSJ. Bruce Fields 	     (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
1989a043226bSJ. Bruce Fields 	      acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
1990f419a2e3SAl Viro 		err = inode_permission(inode, MAY_EXEC);
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	return err? nfserrno(err) : 0;
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds void
19961da177e4SLinus Torvalds nfsd_racache_shutdown(void)
19971da177e4SLinus Torvalds {
199854a66e54SJeff Layton 	struct raparms *raparm, *last_raparm;
199954a66e54SJeff Layton 	unsigned int i;
200054a66e54SJeff Layton 
20011da177e4SLinus Torvalds 	dprintk("nfsd: freeing readahead buffers.\n");
200254a66e54SJeff Layton 
200354a66e54SJeff Layton 	for (i = 0; i < RAPARM_HASH_SIZE; i++) {
200454a66e54SJeff Layton 		raparm = raparm_hash[i].pb_head;
200554a66e54SJeff Layton 		while(raparm) {
200654a66e54SJeff Layton 			last_raparm = raparm;
200754a66e54SJeff Layton 			raparm = raparm->p_next;
200854a66e54SJeff Layton 			kfree(last_raparm);
200954a66e54SJeff Layton 		}
201054a66e54SJeff Layton 		raparm_hash[i].pb_head = NULL;
201154a66e54SJeff Layton 	}
20121da177e4SLinus Torvalds }
20131da177e4SLinus Torvalds /*
20141da177e4SLinus Torvalds  * Initialize readahead param cache
20151da177e4SLinus Torvalds  */
20161da177e4SLinus Torvalds int
20171da177e4SLinus Torvalds nfsd_racache_init(int cache_size)
20181da177e4SLinus Torvalds {
20191da177e4SLinus Torvalds 	int	i;
2020fce1456aSGreg Banks 	int	j = 0;
2021fce1456aSGreg Banks 	int	nperbucket;
202254a66e54SJeff Layton 	struct raparms **raparm = NULL;
20231da177e4SLinus Torvalds 
2024fce1456aSGreg Banks 
202554a66e54SJeff Layton 	if (raparm_hash[0].pb_head)
20261da177e4SLinus Torvalds 		return 0;
202754a66e54SJeff Layton 	nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
20283c7aa15dSKinglong Mee 	nperbucket = max(2, nperbucket);
202954a66e54SJeff Layton 	cache_size = nperbucket * RAPARM_HASH_SIZE;
20304b3bb06bSYan Burman 
20314b3bb06bSYan Burman 	dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
203254a66e54SJeff Layton 
2033fce1456aSGreg Banks 	for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2034fce1456aSGreg Banks 		spin_lock_init(&raparm_hash[i].pb_lock);
203554a66e54SJeff Layton 
203654a66e54SJeff Layton 		raparm = &raparm_hash[i].pb_head;
203754a66e54SJeff Layton 		for (j = 0; j < nperbucket; j++) {
203854a66e54SJeff Layton 			*raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
203954a66e54SJeff Layton 			if (!*raparm)
204054a66e54SJeff Layton 				goto out_nomem;
204154a66e54SJeff Layton 			raparm = &(*raparm)->p_next;
2042fce1456aSGreg Banks 		}
204354a66e54SJeff Layton 		*raparm = NULL;
20441da177e4SLinus Torvalds 	}
20454b3bb06bSYan Burman 
20461da177e4SLinus Torvalds 	nfsdstats.ra_size = cache_size;
20471da177e4SLinus Torvalds 	return 0;
204854a66e54SJeff Layton 
204954a66e54SJeff Layton out_nomem:
205054a66e54SJeff Layton 	dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
205154a66e54SJeff Layton 	nfsd_racache_shutdown();
205254a66e54SJeff Layton 	return -ENOMEM;
20531da177e4SLinus Torvalds }
2054