xref: /linux/fs/nfsd/vfs.c (revision 255fbca6)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * File operations used by nfsd. Some of these have been ripped from
41da177e4SLinus Torvalds  * other parts of the kernel because they weren't exported, others
51da177e4SLinus Torvalds  * are partial duplicates with added or changed functionality.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Note that several functions dget() the dentry upon which they want
81da177e4SLinus Torvalds  * to act, most notably those that create directory entries. Response
91da177e4SLinus Torvalds  * dentry's are dput()'d if necessary in the release callback.
101da177e4SLinus Torvalds  * So if you notice code paths that apparently fail to dput() the
111da177e4SLinus Torvalds  * dentry, don't worry--they have been taken care of.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
141da177e4SLinus Torvalds  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/fs.h>
181da177e4SLinus Torvalds #include <linux/file.h>
19d6b29d7cSJens Axboe #include <linux/splice.h>
2095d871f0SAnna Schumaker #include <linux/falloc.h>
211da177e4SLinus Torvalds #include <linux/fcntl.h>
221da177e4SLinus Torvalds #include <linux/namei.h>
231da177e4SLinus Torvalds #include <linux/delay.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
251da177e4SLinus Torvalds #include <linux/posix_acl_xattr.h>
261da177e4SLinus Torvalds #include <linux/xattr.h>
279a74af21SBoaz Harrosh #include <linux/jhash.h>
289a74af21SBoaz Harrosh #include <linux/ima.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
31f501912aSBen Myers #include <linux/exportfs.h>
32f501912aSBen Myers #include <linux/writeback.h>
3318032ca0SDavid Quigley #include <linux/security.h>
349a74af21SBoaz Harrosh 
359a74af21SBoaz Harrosh #ifdef CONFIG_NFSD_V3
369a74af21SBoaz Harrosh #include "xdr3.h"
379a74af21SBoaz Harrosh #endif /* CONFIG_NFSD_V3 */
389a74af21SBoaz Harrosh 
395be196e5SChristoph Hellwig #ifdef CONFIG_NFSD_V4
40ffa0160aSChristoph Hellwig #include "../internal.h"
412ca72e17SJ. Bruce Fields #include "acl.h"
422ca72e17SJ. Bruce Fields #include "idmap.h"
431da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V4 */
441da177e4SLinus Torvalds 
459a74af21SBoaz Harrosh #include "nfsd.h"
469a74af21SBoaz Harrosh #include "vfs.h"
476e8b50d1SJeff Layton #include "trace.h"
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_FILEOP
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * This is a cache of readahead params that help us choose the proper
541da177e4SLinus Torvalds  * readahead strategy. Initially, we set all readahead parameters to 0
551da177e4SLinus Torvalds  * and let the VFS handle things.
561da177e4SLinus Torvalds  * If you increase the number of cached files very much, you'll need to
571da177e4SLinus Torvalds  * add a hash table here.
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds struct raparms {
601da177e4SLinus Torvalds 	struct raparms		*p_next;
611da177e4SLinus Torvalds 	unsigned int		p_count;
621da177e4SLinus Torvalds 	ino_t			p_ino;
631da177e4SLinus Torvalds 	dev_t			p_dev;
641da177e4SLinus Torvalds 	int			p_set;
651da177e4SLinus Torvalds 	struct file_ra_state	p_ra;
66fce1456aSGreg Banks 	unsigned int		p_hindex;
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
69fce1456aSGreg Banks struct raparm_hbucket {
70fce1456aSGreg Banks 	struct raparms		*pb_head;
71fce1456aSGreg Banks 	spinlock_t		pb_lock;
72fce1456aSGreg Banks } ____cacheline_aligned_in_smp;
73fce1456aSGreg Banks 
74fce1456aSGreg Banks #define RAPARM_HASH_BITS	4
75fce1456aSGreg Banks #define RAPARM_HASH_SIZE	(1<<RAPARM_HASH_BITS)
76fce1456aSGreg Banks #define RAPARM_HASH_MASK	(RAPARM_HASH_SIZE-1)
77fce1456aSGreg Banks static struct raparm_hbucket	raparm_hash[RAPARM_HASH_SIZE];
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /*
801da177e4SLinus Torvalds  * Called from nfsd_lookup and encode_dirent. Check if we have crossed
811da177e4SLinus Torvalds  * a mount point.
82e0bb89efSJ.Bruce Fields  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
831da177e4SLinus Torvalds  *  or nfs_ok having possibly changed *dpp and *expp
841da177e4SLinus Torvalds  */
851da177e4SLinus Torvalds int
861da177e4SLinus Torvalds nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
871da177e4SLinus Torvalds 		        struct svc_export **expp)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	struct svc_export *exp = *expp, *exp2 = NULL;
901da177e4SLinus Torvalds 	struct dentry *dentry = *dpp;
9191c9fa8fSAl Viro 	struct path path = {.mnt = mntget(exp->ex_path.mnt),
9291c9fa8fSAl Viro 			    .dentry = dget(dentry)};
936264d69dSAl Viro 	int err = 0;
941da177e4SLinus Torvalds 
957cc90cc3SAl Viro 	err = follow_down(&path);
96cc53ce53SDavid Howells 	if (err < 0)
97cc53ce53SDavid Howells 		goto out;
9899bbf6ecSNeilBrown 	if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
9999bbf6ecSNeilBrown 	    nfsd_mountpoint(dentry, exp) == 2) {
10099bbf6ecSNeilBrown 		/* This is only a mountpoint in some other namespace */
10199bbf6ecSNeilBrown 		path_put(&path);
10299bbf6ecSNeilBrown 		goto out;
10399bbf6ecSNeilBrown 	}
1041da177e4SLinus Torvalds 
10591c9fa8fSAl Viro 	exp2 = rqst_exp_get_by_name(rqstp, &path);
1061da177e4SLinus Torvalds 	if (IS_ERR(exp2)) {
1071da177e4SLinus Torvalds 		err = PTR_ERR(exp2);
1083b6cee7bSJ. Bruce Fields 		/*
1093b6cee7bSJ. Bruce Fields 		 * We normally allow NFS clients to continue
1103b6cee7bSJ. Bruce Fields 		 * "underneath" a mountpoint that is not exported.
1113b6cee7bSJ. Bruce Fields 		 * The exception is V4ROOT, where no traversal is ever
1123b6cee7bSJ. Bruce Fields 		 * allowed without an explicit export of the new
1133b6cee7bSJ. Bruce Fields 		 * directory.
1143b6cee7bSJ. Bruce Fields 		 */
1153b6cee7bSJ. Bruce Fields 		if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
1163b6cee7bSJ. Bruce Fields 			err = 0;
11791c9fa8fSAl Viro 		path_put(&path);
1181da177e4SLinus Torvalds 		goto out;
1191da177e4SLinus Torvalds 	}
1203c394ddaSSteve Dickson 	if (nfsd_v4client(rqstp) ||
1213c394ddaSSteve Dickson 		(exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
1221da177e4SLinus Torvalds 		/* successfully crossed mount point */
1231644ccc8SAl Viro 		/*
12491c9fa8fSAl Viro 		 * This is subtle: path.dentry is *not* on path.mnt
12591c9fa8fSAl Viro 		 * at this point.  The only reason we are safe is that
12691c9fa8fSAl Viro 		 * original mnt is pinned down by exp, so we should
12791c9fa8fSAl Viro 		 * put path *before* putting exp
1281644ccc8SAl Viro 		 */
12991c9fa8fSAl Viro 		*dpp = path.dentry;
13091c9fa8fSAl Viro 		path.dentry = dentry;
1311644ccc8SAl Viro 		*expp = exp2;
13291c9fa8fSAl Viro 		exp2 = exp;
1331da177e4SLinus Torvalds 	}
13491c9fa8fSAl Viro 	path_put(&path);
13591c9fa8fSAl Viro 	exp_put(exp2);
1361da177e4SLinus Torvalds out:
1371da177e4SLinus Torvalds 	return err;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
140289ede45SJ. Bruce Fields static void follow_to_parent(struct path *path)
141289ede45SJ. Bruce Fields {
142289ede45SJ. Bruce Fields 	struct dentry *dp;
143289ede45SJ. Bruce Fields 
144289ede45SJ. Bruce Fields 	while (path->dentry == path->mnt->mnt_root && follow_up(path))
145289ede45SJ. Bruce Fields 		;
146289ede45SJ. Bruce Fields 	dp = dget_parent(path->dentry);
147289ede45SJ. Bruce Fields 	dput(path->dentry);
148289ede45SJ. Bruce Fields 	path->dentry = dp;
149289ede45SJ. Bruce Fields }
150289ede45SJ. Bruce Fields 
151289ede45SJ. Bruce Fields static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
152289ede45SJ. Bruce Fields {
153289ede45SJ. Bruce Fields 	struct svc_export *exp2;
154289ede45SJ. Bruce Fields 	struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
155289ede45SJ. Bruce Fields 			    .dentry = dget(dparent)};
156289ede45SJ. Bruce Fields 
157289ede45SJ. Bruce Fields 	follow_to_parent(&path);
158289ede45SJ. Bruce Fields 
159289ede45SJ. Bruce Fields 	exp2 = rqst_exp_parent(rqstp, &path);
160289ede45SJ. Bruce Fields 	if (PTR_ERR(exp2) == -ENOENT) {
161289ede45SJ. Bruce Fields 		*dentryp = dget(dparent);
162289ede45SJ. Bruce Fields 	} else if (IS_ERR(exp2)) {
163289ede45SJ. Bruce Fields 		path_put(&path);
164289ede45SJ. Bruce Fields 		return PTR_ERR(exp2);
165289ede45SJ. Bruce Fields 	} else {
166289ede45SJ. Bruce Fields 		*dentryp = dget(path.dentry);
167289ede45SJ. Bruce Fields 		exp_put(*exp);
168289ede45SJ. Bruce Fields 		*exp = exp2;
169289ede45SJ. Bruce Fields 	}
170289ede45SJ. Bruce Fields 	path_put(&path);
171289ede45SJ. Bruce Fields 	return 0;
172289ede45SJ. Bruce Fields }
173289ede45SJ. Bruce Fields 
17482ead7feSJ. Bruce Fields /*
17582ead7feSJ. Bruce Fields  * For nfsd purposes, we treat V4ROOT exports as though there was an
17682ead7feSJ. Bruce Fields  * export at *every* directory.
17799bbf6ecSNeilBrown  * We return:
17899bbf6ecSNeilBrown  * '1' if this dentry *must* be an export point,
17999bbf6ecSNeilBrown  * '2' if it might be, if there is really a mount here, and
18099bbf6ecSNeilBrown  * '0' if there is no chance of an export point here.
18182ead7feSJ. Bruce Fields  */
1823227fa41SJ. Bruce Fields int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
18382ead7feSJ. Bruce Fields {
18499bbf6ecSNeilBrown 	if (!d_inode(dentry))
18599bbf6ecSNeilBrown 		return 0;
18699bbf6ecSNeilBrown 	if (exp->ex_flags & NFSEXP_V4ROOT)
18782ead7feSJ. Bruce Fields 		return 1;
18811fcee02STrond Myklebust 	if (nfsd4_is_junction(dentry))
18911fcee02STrond Myklebust 		return 1;
19099bbf6ecSNeilBrown 	if (d_mountpoint(dentry))
19199bbf6ecSNeilBrown 		/*
19299bbf6ecSNeilBrown 		 * Might only be a mountpoint in a different namespace,
19399bbf6ecSNeilBrown 		 * but we need to check.
19499bbf6ecSNeilBrown 		 */
19599bbf6ecSNeilBrown 		return 2;
19682ead7feSJ. Bruce Fields 	return 0;
19782ead7feSJ. Bruce Fields }
19882ead7feSJ. Bruce Fields 
1996264d69dSAl Viro __be32
2006c0a654dSJ. Bruce Fields nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
2015a022fc8SChuck Lever 		   const char *name, unsigned int len,
2026c0a654dSJ. Bruce Fields 		   struct svc_export **exp_ret, struct dentry **dentry_ret)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	struct svc_export	*exp;
2051da177e4SLinus Torvalds 	struct dentry		*dparent;
2061da177e4SLinus Torvalds 	struct dentry		*dentry;
2076264d69dSAl Viro 	int			host_err;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	dparent = fhp->fh_dentry;
212bf18f163SKinglong Mee 	exp = exp_get(fhp->fh_export);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	/* Lookup the name, but don't follow links */
2151da177e4SLinus Torvalds 	if (isdotent(name, len)) {
2161da177e4SLinus Torvalds 		if (len==1)
2171da177e4SLinus Torvalds 			dentry = dget(dparent);
21854775491SJan Blunck 		else if (dparent != exp->ex_path.dentry)
2191da177e4SLinus Torvalds 			dentry = dget_parent(dparent);
220fed83811SJ. Bruce Fields 		else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
2211da177e4SLinus Torvalds 			dentry = dget(dparent); /* .. == . just like at / */
2221da177e4SLinus Torvalds 		else {
2231da177e4SLinus Torvalds 			/* checking mountpoint crossing is very different when stepping up */
224289ede45SJ. Bruce Fields 			host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
225289ede45SJ. Bruce Fields 			if (host_err)
2261da177e4SLinus Torvalds 				goto out_nfserr;
2271da177e4SLinus Torvalds 		}
2281da177e4SLinus Torvalds 	} else {
2294335723eSJ. Bruce Fields 		/*
2304335723eSJ. Bruce Fields 		 * In the nfsd4_open() case, this may be held across
2314335723eSJ. Bruce Fields 		 * subsequent open and delegation acquisition which may
2324335723eSJ. Bruce Fields 		 * need to take the child's i_mutex:
2334335723eSJ. Bruce Fields 		 */
2344335723eSJ. Bruce Fields 		fh_lock_nested(fhp, I_MUTEX_PARENT);
2351da177e4SLinus Torvalds 		dentry = lookup_one_len(name, dparent, len);
2366264d69dSAl Viro 		host_err = PTR_ERR(dentry);
2371da177e4SLinus Torvalds 		if (IS_ERR(dentry))
2381da177e4SLinus Torvalds 			goto out_nfserr;
23982ead7feSJ. Bruce Fields 		if (nfsd_mountpoint(dentry, exp)) {
240bbddca8eSNeilBrown 			/*
241bbddca8eSNeilBrown 			 * We don't need the i_mutex after all.  It's
242bbddca8eSNeilBrown 			 * still possible we could open this (regular
243bbddca8eSNeilBrown 			 * files can be mountpoints too), but the
244bbddca8eSNeilBrown 			 * i_mutex is just there to prevent renames of
245bbddca8eSNeilBrown 			 * something that we might be about to delegate,
246bbddca8eSNeilBrown 			 * and a mountpoint won't be renamed:
247bbddca8eSNeilBrown 			 */
248bbddca8eSNeilBrown 			fh_unlock(fhp);
2496264d69dSAl Viro 			if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
2501da177e4SLinus Torvalds 				dput(dentry);
2511da177e4SLinus Torvalds 				goto out_nfserr;
2521da177e4SLinus Torvalds 			}
2531da177e4SLinus Torvalds 		}
2541da177e4SLinus Torvalds 	}
2556c0a654dSJ. Bruce Fields 	*dentry_ret = dentry;
2566c0a654dSJ. Bruce Fields 	*exp_ret = exp;
2576c0a654dSJ. Bruce Fields 	return 0;
2586c0a654dSJ. Bruce Fields 
2596c0a654dSJ. Bruce Fields out_nfserr:
2606c0a654dSJ. Bruce Fields 	exp_put(exp);
2616c0a654dSJ. Bruce Fields 	return nfserrno(host_err);
2626c0a654dSJ. Bruce Fields }
2636c0a654dSJ. Bruce Fields 
2646c0a654dSJ. Bruce Fields /*
2656c0a654dSJ. Bruce Fields  * Look up one component of a pathname.
2666c0a654dSJ. Bruce Fields  * N.B. After this call _both_ fhp and resfh need an fh_put
2676c0a654dSJ. Bruce Fields  *
2686c0a654dSJ. Bruce Fields  * If the lookup would cross a mountpoint, and the mounted filesystem
2696c0a654dSJ. Bruce Fields  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
2706c0a654dSJ. Bruce Fields  * accepted as it stands and the mounted directory is
2716c0a654dSJ. Bruce Fields  * returned. Otherwise the covered directory is returned.
2726c0a654dSJ. Bruce Fields  * NOTE: this mountpoint crossing is not supported properly by all
2736c0a654dSJ. Bruce Fields  *   clients and is explicitly disallowed for NFSv3
2746c0a654dSJ. Bruce Fields  *      NeilBrown <neilb@cse.unsw.edu.au>
2756c0a654dSJ. Bruce Fields  */
2766c0a654dSJ. Bruce Fields __be32
2776c0a654dSJ. Bruce Fields nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
2785a022fc8SChuck Lever 				unsigned int len, struct svc_fh *resfh)
2796c0a654dSJ. Bruce Fields {
2806c0a654dSJ. Bruce Fields 	struct svc_export	*exp;
2816c0a654dSJ. Bruce Fields 	struct dentry		*dentry;
2826c0a654dSJ. Bruce Fields 	__be32 err;
2836c0a654dSJ. Bruce Fields 
28429a78a3eSJ. Bruce Fields 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
28529a78a3eSJ. Bruce Fields 	if (err)
28629a78a3eSJ. Bruce Fields 		return err;
2876c0a654dSJ. Bruce Fields 	err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
2886c0a654dSJ. Bruce Fields 	if (err)
2896c0a654dSJ. Bruce Fields 		return err;
29032c1eb0cSAndy Adamson 	err = check_nfsd_access(exp, rqstp);
29132c1eb0cSAndy Adamson 	if (err)
29232c1eb0cSAndy Adamson 		goto out;
2931da177e4SLinus Torvalds 	/*
2941da177e4SLinus Torvalds 	 * Note: we compose the file handle now, but as the
2951da177e4SLinus Torvalds 	 * dentry may be negative, it may need to be updated.
2961da177e4SLinus Torvalds 	 */
2971da177e4SLinus Torvalds 	err = fh_compose(resfh, exp, dentry, fhp);
2982b0143b5SDavid Howells 	if (!err && d_really_is_negative(dentry))
2991da177e4SLinus Torvalds 		err = nfserr_noent;
30032c1eb0cSAndy Adamson out:
3011da177e4SLinus Torvalds 	dput(dentry);
3021da177e4SLinus Torvalds 	exp_put(exp);
3031da177e4SLinus Torvalds 	return err;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306f501912aSBen Myers /*
307f501912aSBen Myers  * Commit metadata changes to stable storage.
308f501912aSBen Myers  */
309f501912aSBen Myers static int
310f501912aSBen Myers commit_metadata(struct svc_fh *fhp)
311f501912aSBen Myers {
3122b0143b5SDavid Howells 	struct inode *inode = d_inode(fhp->fh_dentry);
313f501912aSBen Myers 	const struct export_operations *export_ops = inode->i_sb->s_export_op;
314f501912aSBen Myers 
315f501912aSBen Myers 	if (!EX_ISSYNC(fhp->fh_export))
316f501912aSBen Myers 		return 0;
317f501912aSBen Myers 
318c3765016SChristoph Hellwig 	if (export_ops->commit_metadata)
319c3765016SChristoph Hellwig 		return export_ops->commit_metadata(inode);
320c3765016SChristoph Hellwig 	return sync_inode_metadata(inode, 1);
321f501912aSBen Myers }
3226c0a654dSJ. Bruce Fields 
3231da177e4SLinus Torvalds /*
324818e5a22SChristoph Hellwig  * Go over the attributes and take care of the small differences between
325818e5a22SChristoph Hellwig  * NFS semantics and what Linux expects.
326818e5a22SChristoph Hellwig  */
327818e5a22SChristoph Hellwig static void
328818e5a22SChristoph Hellwig nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
329818e5a22SChristoph Hellwig {
330818e5a22SChristoph Hellwig 	/* sanitize the mode change */
331818e5a22SChristoph Hellwig 	if (iap->ia_valid & ATTR_MODE) {
332818e5a22SChristoph Hellwig 		iap->ia_mode &= S_IALLUGO;
333818e5a22SChristoph Hellwig 		iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
334818e5a22SChristoph Hellwig 	}
335818e5a22SChristoph Hellwig 
336818e5a22SChristoph Hellwig 	/* Revoke setuid/setgid on chown */
337818e5a22SChristoph Hellwig 	if (!S_ISDIR(inode->i_mode) &&
338c4fa6d7cSStanislav Kholmanskikh 	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
339818e5a22SChristoph Hellwig 		iap->ia_valid |= ATTR_KILL_PRIV;
340818e5a22SChristoph Hellwig 		if (iap->ia_valid & ATTR_MODE) {
341818e5a22SChristoph Hellwig 			/* we're setting mode too, just clear the s*id bits */
342818e5a22SChristoph Hellwig 			iap->ia_mode &= ~S_ISUID;
343818e5a22SChristoph Hellwig 			if (iap->ia_mode & S_IXGRP)
344818e5a22SChristoph Hellwig 				iap->ia_mode &= ~S_ISGID;
345818e5a22SChristoph Hellwig 		} else {
346818e5a22SChristoph Hellwig 			/* set ATTR_KILL_* bits and let VFS handle it */
347818e5a22SChristoph Hellwig 			iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
348818e5a22SChristoph Hellwig 		}
349818e5a22SChristoph Hellwig 	}
350818e5a22SChristoph Hellwig }
351818e5a22SChristoph Hellwig 
3520839ffb8SJ. Bruce Fields static __be32
3530839ffb8SJ. Bruce Fields nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
3540839ffb8SJ. Bruce Fields 		struct iattr *iap)
3550839ffb8SJ. Bruce Fields {
3560839ffb8SJ. Bruce Fields 	struct inode *inode = d_inode(fhp->fh_dentry);
3570839ffb8SJ. Bruce Fields 	int host_err;
3580839ffb8SJ. Bruce Fields 
3590839ffb8SJ. Bruce Fields 	if (iap->ia_size < inode->i_size) {
3600839ffb8SJ. Bruce Fields 		__be32 err;
3610839ffb8SJ. Bruce Fields 
3620839ffb8SJ. Bruce Fields 		err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
3630839ffb8SJ. Bruce Fields 				NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
3640839ffb8SJ. Bruce Fields 		if (err)
3650839ffb8SJ. Bruce Fields 			return err;
3660839ffb8SJ. Bruce Fields 	}
3670839ffb8SJ. Bruce Fields 
3680839ffb8SJ. Bruce Fields 	host_err = get_write_access(inode);
3690839ffb8SJ. Bruce Fields 	if (host_err)
3700839ffb8SJ. Bruce Fields 		goto out_nfserrno;
3710839ffb8SJ. Bruce Fields 
3720839ffb8SJ. Bruce Fields 	host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
3730839ffb8SJ. Bruce Fields 	if (host_err)
3740839ffb8SJ. Bruce Fields 		goto out_put_write_access;
3750839ffb8SJ. Bruce Fields 	return 0;
3760839ffb8SJ. Bruce Fields 
3770839ffb8SJ. Bruce Fields out_put_write_access:
3780839ffb8SJ. Bruce Fields 	put_write_access(inode);
3790839ffb8SJ. Bruce Fields out_nfserrno:
3800839ffb8SJ. Bruce Fields 	return nfserrno(host_err);
3810839ffb8SJ. Bruce Fields }
3820839ffb8SJ. Bruce Fields 
383818e5a22SChristoph Hellwig /*
384818e5a22SChristoph Hellwig  * Set various file attributes.  After this call fhp needs an fh_put.
3851da177e4SLinus Torvalds  */
3866264d69dSAl Viro __be32
3871da177e4SLinus Torvalds nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
3881da177e4SLinus Torvalds 	     int check_guard, time_t guardtime)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	struct dentry	*dentry;
3911da177e4SLinus Torvalds 	struct inode	*inode;
3928837abcaSMiklos Szeredi 	int		accmode = NFSD_MAY_SATTR;
393175a4eb7SAl Viro 	umode_t		ftype = 0;
3946264d69dSAl Viro 	__be32		err;
3956264d69dSAl Viro 	int		host_err;
3969f67f189SJ. Bruce Fields 	bool		get_write_count;
397758e99feSChristoph Hellwig 	bool		size_change = (iap->ia_valid & ATTR_SIZE);
3981da177e4SLinus Torvalds 
399*255fbca6Szhengbin 	if (iap->ia_valid & ATTR_SIZE) {
4008837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
4011da177e4SLinus Torvalds 		ftype = S_IFREG;
402*255fbca6Szhengbin 	}
403*255fbca6Szhengbin 
404*255fbca6Szhengbin 	/*
405*255fbca6Szhengbin 	 * If utimes(2) and friends are called with times not NULL, we should
406*255fbca6Szhengbin 	 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
407*255fbca6Szhengbin 	 * will return EACCESS, when the caller's effective UID does not match
408*255fbca6Szhengbin 	 * the owner of the file, and the caller is not privileged. In this
409*255fbca6Szhengbin 	 * situation, we should return EPERM(notify_change will return this).
410*255fbca6Szhengbin 	 */
411*255fbca6Szhengbin 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
412*255fbca6Szhengbin 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
413*255fbca6Szhengbin 		if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
414*255fbca6Szhengbin 			accmode |= NFSD_MAY_WRITE;
415*255fbca6Szhengbin 	}
4161da177e4SLinus Torvalds 
4179f67f189SJ. Bruce Fields 	/* Callers that do fh_verify should do the fh_want_write: */
4189f67f189SJ. Bruce Fields 	get_write_count = !fhp->fh_dentry;
4199f67f189SJ. Bruce Fields 
4201da177e4SLinus Torvalds 	/* Get inode */
4211da177e4SLinus Torvalds 	err = fh_verify(rqstp, fhp, ftype, accmode);
42215b7a1b8SNeilBrown 	if (err)
423758e99feSChristoph Hellwig 		return err;
4249f67f189SJ. Bruce Fields 	if (get_write_count) {
4259f67f189SJ. Bruce Fields 		host_err = fh_want_write(fhp);
4269f67f189SJ. Bruce Fields 		if (host_err)
427758e99feSChristoph Hellwig 			goto out;
4289f67f189SJ. Bruce Fields 	}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
4312b0143b5SDavid Howells 	inode = d_inode(dentry);
4321da177e4SLinus Torvalds 
43315b7a1b8SNeilBrown 	/* Ignore any mode updates on symlinks */
43415b7a1b8SNeilBrown 	if (S_ISLNK(inode->i_mode))
43515b7a1b8SNeilBrown 		iap->ia_valid &= ~ATTR_MODE;
43615b7a1b8SNeilBrown 
43715b7a1b8SNeilBrown 	if (!iap->ia_valid)
438758e99feSChristoph Hellwig 		return 0;
43915b7a1b8SNeilBrown 
440818e5a22SChristoph Hellwig 	nfsd_sanitize_attrs(inode, iap);
4411da177e4SLinus Torvalds 
442758e99feSChristoph Hellwig 	if (check_guard && guardtime != inode->i_ctime.tv_sec)
443758e99feSChristoph Hellwig 		return nfserr_notsync;
444758e99feSChristoph Hellwig 
445f0c63124SChristoph Hellwig 	/*
44641f53350SChristoph Hellwig 	 * The size case is special, it changes the file in addition to the
447783112f7SChristoph Hellwig 	 * attributes, and file systems don't expect it to be mixed with
448783112f7SChristoph Hellwig 	 * "random" attribute changes.  We thus split out the size change
449783112f7SChristoph Hellwig 	 * into a separate call to ->setattr, and do the rest as a separate
450783112f7SChristoph Hellwig 	 * setattr call.
451f0c63124SChristoph Hellwig 	 */
452758e99feSChristoph Hellwig 	if (size_change) {
4530839ffb8SJ. Bruce Fields 		err = nfsd_get_write_access(rqstp, fhp, iap);
4540839ffb8SJ. Bruce Fields 		if (err)
455758e99feSChristoph Hellwig 			return err;
456783112f7SChristoph Hellwig 	}
45741f53350SChristoph Hellwig 
458783112f7SChristoph Hellwig 	fh_lock(fhp);
459783112f7SChristoph Hellwig 	if (size_change) {
46041f53350SChristoph Hellwig 		/*
4610839ffb8SJ. Bruce Fields 		 * RFC5661, Section 18.30.4:
4620839ffb8SJ. Bruce Fields 		 *   Changing the size of a file with SETATTR indirectly
4630839ffb8SJ. Bruce Fields 		 *   changes the time_modify and change attributes.
4640839ffb8SJ. Bruce Fields 		 *
4650839ffb8SJ. Bruce Fields 		 * (and similar for the older RFCs)
46641f53350SChristoph Hellwig 		 */
467783112f7SChristoph Hellwig 		struct iattr size_attr = {
468783112f7SChristoph Hellwig 			.ia_valid	= ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
469783112f7SChristoph Hellwig 			.ia_size	= iap->ia_size,
470783112f7SChristoph Hellwig 		};
471783112f7SChristoph Hellwig 
472783112f7SChristoph Hellwig 		host_err = notify_change(dentry, &size_attr, NULL);
473783112f7SChristoph Hellwig 		if (host_err)
474783112f7SChristoph Hellwig 			goto out_unlock;
475783112f7SChristoph Hellwig 		iap->ia_valid &= ~ATTR_SIZE;
476783112f7SChristoph Hellwig 
477783112f7SChristoph Hellwig 		/*
478783112f7SChristoph Hellwig 		 * Avoid the additional setattr call below if the only other
479783112f7SChristoph Hellwig 		 * attribute that the client sends is the mtime, as we update
480783112f7SChristoph Hellwig 		 * it as part of the size change above.
481783112f7SChristoph Hellwig 		 */
482783112f7SChristoph Hellwig 		if ((iap->ia_valid & ~ATTR_MTIME) == 0)
483783112f7SChristoph Hellwig 			goto out_unlock;
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	iap->ia_valid |= ATTR_CTIME;
48727ac0ffeSJ. Bruce Fields 	host_err = notify_change(dentry, iap, NULL);
488987da479SChristoph Hellwig 
489783112f7SChristoph Hellwig out_unlock:
490783112f7SChristoph Hellwig 	fh_unlock(fhp);
4910839ffb8SJ. Bruce Fields 	if (size_change)
4920839ffb8SJ. Bruce Fields 		put_write_access(inode);
4930839ffb8SJ. Bruce Fields out:
494758e99feSChristoph Hellwig 	if (!host_err)
495758e99feSChristoph Hellwig 		host_err = commit_metadata(fhp);
496758e99feSChristoph Hellwig 	return nfserrno(host_err);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
4995be196e5SChristoph Hellwig #if defined(CONFIG_NFSD_V4)
5009b4146e8SChuck Lever /*
5019b4146e8SChuck Lever  * NFS junction information is stored in an extended attribute.
5029b4146e8SChuck Lever  */
5039b4146e8SChuck Lever #define NFSD_JUNCTION_XATTR_NAME	XATTR_TRUSTED_PREFIX "junction.nfs"
5049b4146e8SChuck Lever 
5059b4146e8SChuck Lever /**
5069b4146e8SChuck Lever  * nfsd4_is_junction - Test if an object could be an NFS junction
5079b4146e8SChuck Lever  *
5089b4146e8SChuck Lever  * @dentry: object to test
5099b4146e8SChuck Lever  *
5109b4146e8SChuck Lever  * Returns 1 if "dentry" appears to contain NFS junction information.
5119b4146e8SChuck Lever  * Otherwise 0 is returned.
5129b4146e8SChuck Lever  */
51311fcee02STrond Myklebust int nfsd4_is_junction(struct dentry *dentry)
51411fcee02STrond Myklebust {
5152b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
51611fcee02STrond Myklebust 
51711fcee02STrond Myklebust 	if (inode == NULL)
51811fcee02STrond Myklebust 		return 0;
51911fcee02STrond Myklebust 	if (inode->i_mode & S_IXUGO)
52011fcee02STrond Myklebust 		return 0;
52111fcee02STrond Myklebust 	if (!(inode->i_mode & S_ISVTX))
52211fcee02STrond Myklebust 		return 0;
5239b4146e8SChuck Lever 	if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
52411fcee02STrond Myklebust 		return 0;
52511fcee02STrond Myklebust 	return 1;
52611fcee02STrond Myklebust }
52718032ca0SDavid Quigley #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
52818032ca0SDavid Quigley __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
52918032ca0SDavid Quigley 		struct xdr_netobj *label)
53018032ca0SDavid Quigley {
53118032ca0SDavid Quigley 	__be32 error;
53218032ca0SDavid Quigley 	int host_error;
53318032ca0SDavid Quigley 	struct dentry *dentry;
53418032ca0SDavid Quigley 
53518032ca0SDavid Quigley 	error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
53618032ca0SDavid Quigley 	if (error)
53718032ca0SDavid Quigley 		return error;
53818032ca0SDavid Quigley 
53918032ca0SDavid Quigley 	dentry = fhp->fh_dentry;
54018032ca0SDavid Quigley 
5415955102cSAl Viro 	inode_lock(d_inode(dentry));
54218032ca0SDavid Quigley 	host_error = security_inode_setsecctx(dentry, label->data, label->len);
5435955102cSAl Viro 	inode_unlock(d_inode(dentry));
54418032ca0SDavid Quigley 	return nfserrno(host_error);
54518032ca0SDavid Quigley }
54618032ca0SDavid Quigley #else
54718032ca0SDavid Quigley __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
54818032ca0SDavid Quigley 		struct xdr_netobj *label)
54918032ca0SDavid Quigley {
55018032ca0SDavid Quigley 	return nfserr_notsupp;
55118032ca0SDavid Quigley }
55218032ca0SDavid Quigley #endif
55318032ca0SDavid Quigley 
554ffa0160aSChristoph Hellwig __be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
555ffa0160aSChristoph Hellwig 		u64 dst_pos, u64 count)
556ffa0160aSChristoph Hellwig {
55742ec3d4cSDarrick J. Wong 	loff_t cloned;
55842ec3d4cSDarrick J. Wong 
559452ce659SDarrick J. Wong 	cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
56042ec3d4cSDarrick J. Wong 	if (count && cloned != count)
56142ec3d4cSDarrick J. Wong 		cloned = -EINVAL;
56242ec3d4cSDarrick J. Wong 	return nfserrno(cloned < 0 ? cloned : 0);
563ffa0160aSChristoph Hellwig }
564ffa0160aSChristoph Hellwig 
56529ae7f9dSAnna Schumaker ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
56629ae7f9dSAnna Schumaker 			     u64 dst_pos, u64 count)
56729ae7f9dSAnna Schumaker {
56829ae7f9dSAnna Schumaker 
56929ae7f9dSAnna Schumaker 	/*
57029ae7f9dSAnna Schumaker 	 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
57129ae7f9dSAnna Schumaker 	 * thread and client rpc slot.  The choice of 4MB is somewhat
57229ae7f9dSAnna Schumaker 	 * arbitrary.  We might instead base this on r/wsize, or make it
57329ae7f9dSAnna Schumaker 	 * tunable, or use a time instead of a byte limit, or implement
57429ae7f9dSAnna Schumaker 	 * asynchronous copy.  In theory a client could also recognize a
57529ae7f9dSAnna Schumaker 	 * limit like this and pipeline multiple COPY requests.
57629ae7f9dSAnna Schumaker 	 */
57729ae7f9dSAnna Schumaker 	count = min_t(u64, count, 1 << 22);
57829ae7f9dSAnna Schumaker 	return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
57929ae7f9dSAnna Schumaker }
58029ae7f9dSAnna Schumaker 
58195d871f0SAnna Schumaker __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
58295d871f0SAnna Schumaker 			   struct file *file, loff_t offset, loff_t len,
58395d871f0SAnna Schumaker 			   int flags)
58495d871f0SAnna Schumaker {
58595d871f0SAnna Schumaker 	int error;
58695d871f0SAnna Schumaker 
58795d871f0SAnna Schumaker 	if (!S_ISREG(file_inode(file)->i_mode))
58895d871f0SAnna Schumaker 		return nfserr_inval;
58995d871f0SAnna Schumaker 
59095d871f0SAnna Schumaker 	error = vfs_fallocate(file, flags, offset, len);
59195d871f0SAnna Schumaker 	if (!error)
59295d871f0SAnna Schumaker 		error = commit_metadata(fhp);
59395d871f0SAnna Schumaker 
59495d871f0SAnna Schumaker 	return nfserrno(error);
59595d871f0SAnna Schumaker }
5966a85d6c7SJ. Bruce Fields #endif /* defined(CONFIG_NFSD_V4) */
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
5991da177e4SLinus Torvalds /*
6001da177e4SLinus Torvalds  * Check server access rights to a file system object
6011da177e4SLinus Torvalds  */
6021da177e4SLinus Torvalds struct accessmap {
6031da177e4SLinus Torvalds 	u32		access;
6041da177e4SLinus Torvalds 	int		how;
6051da177e4SLinus Torvalds };
6061da177e4SLinus Torvalds static struct accessmap	nfs3_regaccess[] = {
6078837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
6088837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
6098837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_TRUNC	},
6108837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE			},
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds     {	0,			0				}
6131da177e4SLinus Torvalds };
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds static struct accessmap	nfs3_diraccess[] = {
6168837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
6178837abcaSMiklos Szeredi     {	NFS3_ACCESS_LOOKUP,	NFSD_MAY_EXEC			},
6188837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
6198837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_EXEC|NFSD_MAY_WRITE	},
6208837abcaSMiklos Szeredi     {	NFS3_ACCESS_DELETE,	NFSD_MAY_REMOVE			},
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds     {	0,			0				}
6231da177e4SLinus Torvalds };
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds static struct accessmap	nfs3_anyaccess[] = {
6261da177e4SLinus Torvalds 	/* Some clients - Solaris 2.6 at least, make an access call
6271da177e4SLinus Torvalds 	 * to the server to check for access for things like /dev/null
6281da177e4SLinus Torvalds 	 * (which really, the server doesn't care about).  So
6291da177e4SLinus Torvalds 	 * We provide simple access checking for them, looking
6301da177e4SLinus Torvalds 	 * mainly at mode bits, and we make sure to ignore read-only
6311da177e4SLinus Torvalds 	 * filesystem checks
6321da177e4SLinus Torvalds 	 */
6338837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
6348837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
6358837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
6368837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds     {	0,			0				}
6391da177e4SLinus Torvalds };
6401da177e4SLinus Torvalds 
6416264d69dSAl Viro __be32
6421da177e4SLinus Torvalds nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
6431da177e4SLinus Torvalds {
6441da177e4SLinus Torvalds 	struct accessmap	*map;
6451da177e4SLinus Torvalds 	struct svc_export	*export;
6461da177e4SLinus Torvalds 	struct dentry		*dentry;
6471da177e4SLinus Torvalds 	u32			query, result = 0, sresult = 0;
6486264d69dSAl Viro 	__be32			error;
6491da177e4SLinus Torvalds 
6508837abcaSMiklos Szeredi 	error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
6511da177e4SLinus Torvalds 	if (error)
6521da177e4SLinus Torvalds 		goto out;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	export = fhp->fh_export;
6551da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
6561da177e4SLinus Torvalds 
657e36cb0b8SDavid Howells 	if (d_is_reg(dentry))
6581da177e4SLinus Torvalds 		map = nfs3_regaccess;
659e36cb0b8SDavid Howells 	else if (d_is_dir(dentry))
6601da177e4SLinus Torvalds 		map = nfs3_diraccess;
6611da177e4SLinus Torvalds 	else
6621da177e4SLinus Torvalds 		map = nfs3_anyaccess;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	query = *access;
6661da177e4SLinus Torvalds 	for  (; map->access; map++) {
6671da177e4SLinus Torvalds 		if (map->access & query) {
6686264d69dSAl Viro 			__be32 err2;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 			sresult |= map->access;
6711da177e4SLinus Torvalds 
6720ec757dfSJ. Bruce Fields 			err2 = nfsd_permission(rqstp, export, dentry, map->how);
6731da177e4SLinus Torvalds 			switch (err2) {
6741da177e4SLinus Torvalds 			case nfs_ok:
6751da177e4SLinus Torvalds 				result |= map->access;
6761da177e4SLinus Torvalds 				break;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 			/* the following error codes just mean the access was not allowed,
6791da177e4SLinus Torvalds 			 * rather than an error occurred */
6801da177e4SLinus Torvalds 			case nfserr_rofs:
6811da177e4SLinus Torvalds 			case nfserr_acces:
6821da177e4SLinus Torvalds 			case nfserr_perm:
6831da177e4SLinus Torvalds 				/* simply don't "or" in the access bit. */
6841da177e4SLinus Torvalds 				break;
6851da177e4SLinus Torvalds 			default:
6861da177e4SLinus Torvalds 				error = err2;
6871da177e4SLinus Torvalds 				goto out;
6881da177e4SLinus Torvalds 			}
6891da177e4SLinus Torvalds 		}
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 	*access = result;
6921da177e4SLinus Torvalds 	if (supported)
6931da177e4SLinus Torvalds 		*supported = sresult;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds  out:
6961da177e4SLinus Torvalds 	return error;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
6991da177e4SLinus Torvalds 
700105f4622SJ. Bruce Fields static int nfsd_open_break_lease(struct inode *inode, int access)
701105f4622SJ. Bruce Fields {
702105f4622SJ. Bruce Fields 	unsigned int mode;
7031da177e4SLinus Torvalds 
704105f4622SJ. Bruce Fields 	if (access & NFSD_MAY_NOT_BREAK_LEASE)
705105f4622SJ. Bruce Fields 		return 0;
706105f4622SJ. Bruce Fields 	mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
707105f4622SJ. Bruce Fields 	return break_lease(inode, mode | O_NONBLOCK);
708105f4622SJ. Bruce Fields }
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds /*
7111da177e4SLinus Torvalds  * Open an existing file or directory.
712999448a8SBernd Schubert  * The may_flags argument indicates the type of open (read/write/lock)
713999448a8SBernd Schubert  * and additional flags.
7141da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
7151da177e4SLinus Torvalds  */
7166264d69dSAl Viro __be32
717175a4eb7SAl Viro nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
718999448a8SBernd Schubert 			int may_flags, struct file **filp)
7191da177e4SLinus Torvalds {
720765927b2SAl Viro 	struct path	path;
7211da177e4SLinus Torvalds 	struct inode	*inode;
7228519f994SKinglong Mee 	struct file	*file;
7236264d69dSAl Viro 	int		flags = O_RDONLY|O_LARGEFILE;
7246264d69dSAl Viro 	__be32		err;
72591885258SJeff Layton 	int		host_err = 0;
7261da177e4SLinus Torvalds 
727e0e81739SDavid Howells 	validate_process_creds();
728e0e81739SDavid Howells 
7291da177e4SLinus Torvalds 	/*
7301da177e4SLinus Torvalds 	 * If we get here, then the client has already done an "open",
7311da177e4SLinus Torvalds 	 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
7321da177e4SLinus Torvalds 	 * in case a chmod has now revoked permission.
733d91d0b56SJ. Bruce Fields 	 *
734d91d0b56SJ. Bruce Fields 	 * Arguably we should also allow the owner override for
735d91d0b56SJ. Bruce Fields 	 * directories, but we never have and it doesn't seem to have
736d91d0b56SJ. Bruce Fields 	 * caused anyone a problem.  If we were to change this, note
737d91d0b56SJ. Bruce Fields 	 * also that our filldir callbacks would need a variant of
738d91d0b56SJ. Bruce Fields 	 * lookup_one_len that doesn't check permissions.
7391da177e4SLinus Torvalds 	 */
740d91d0b56SJ. Bruce Fields 	if (type == S_IFREG)
741d91d0b56SJ. Bruce Fields 		may_flags |= NFSD_MAY_OWNER_OVERRIDE;
742d91d0b56SJ. Bruce Fields 	err = fh_verify(rqstp, fhp, type, may_flags);
7431da177e4SLinus Torvalds 	if (err)
7441da177e4SLinus Torvalds 		goto out;
7451da177e4SLinus Torvalds 
746765927b2SAl Viro 	path.mnt = fhp->fh_export->ex_path.mnt;
747765927b2SAl Viro 	path.dentry = fhp->fh_dentry;
7482b0143b5SDavid Howells 	inode = d_inode(path.dentry);
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	/* Disallow write access to files with the append-only bit set
7511da177e4SLinus Torvalds 	 * or any access when mandatory locking enabled
7521da177e4SLinus Torvalds 	 */
7531da177e4SLinus Torvalds 	err = nfserr_perm;
754999448a8SBernd Schubert 	if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
7551da177e4SLinus Torvalds 		goto out;
7565e7fc436SJ. Bruce Fields 	/*
7575e7fc436SJ. Bruce Fields 	 * We must ignore files (but only files) which might have mandatory
7585e7fc436SJ. Bruce Fields 	 * locks on them because there is no way to know if the accesser has
7595e7fc436SJ. Bruce Fields 	 * the lock.
7605e7fc436SJ. Bruce Fields 	 */
7615e7fc436SJ. Bruce Fields 	if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
7621da177e4SLinus Torvalds 		goto out;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	if (!inode->i_fop)
7651da177e4SLinus Torvalds 		goto out;
7661da177e4SLinus Torvalds 
767999448a8SBernd Schubert 	host_err = nfsd_open_break_lease(inode, may_flags);
7686264d69dSAl Viro 	if (host_err) /* NOMEM or WOULDBLOCK */
7691da177e4SLinus Torvalds 		goto out_nfserr;
7701da177e4SLinus Torvalds 
771999448a8SBernd Schubert 	if (may_flags & NFSD_MAY_WRITE) {
772999448a8SBernd Schubert 		if (may_flags & NFSD_MAY_READ)
7739ecb6a08SJ. Bruce Fields 			flags = O_RDWR|O_LARGEFILE;
7749ecb6a08SJ. Bruce Fields 		else
7751da177e4SLinus Torvalds 			flags = O_WRONLY|O_LARGEFILE;
7761da177e4SLinus Torvalds 	}
777999448a8SBernd Schubert 
7788519f994SKinglong Mee 	file = dentry_open(&path, flags, current_cred());
7798519f994SKinglong Mee 	if (IS_ERR(file)) {
7808519f994SKinglong Mee 		host_err = PTR_ERR(file);
7818519f994SKinglong Mee 		goto out_nfserr;
78206effdbbSBernd Schubert 	}
78306effdbbSBernd Schubert 
7846035a27bSAl Viro 	host_err = ima_file_check(file, may_flags);
7858519f994SKinglong Mee 	if (host_err) {
786fd891454SChristoph Hellwig 		fput(file);
7878519f994SKinglong Mee 		goto out_nfserr;
7888519f994SKinglong Mee 	}
7898519f994SKinglong Mee 
7908519f994SKinglong Mee 	if (may_flags & NFSD_MAY_64BIT_COOKIE)
7918519f994SKinglong Mee 		file->f_mode |= FMODE_64BITHASH;
7928519f994SKinglong Mee 	else
7938519f994SKinglong Mee 		file->f_mode |= FMODE_32BITHASH;
7948519f994SKinglong Mee 
7958519f994SKinglong Mee 	*filp = file;
7961da177e4SLinus Torvalds out_nfserr:
7976264d69dSAl Viro 	err = nfserrno(host_err);
7981da177e4SLinus Torvalds out:
799e0e81739SDavid Howells 	validate_process_creds();
8001da177e4SLinus Torvalds 	return err;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
803e749a462SChristoph Hellwig struct raparms *
804e749a462SChristoph Hellwig nfsd_init_raparms(struct file *file)
8051da177e4SLinus Torvalds {
806e749a462SChristoph Hellwig 	struct inode *inode = file_inode(file);
807e749a462SChristoph Hellwig 	dev_t dev = inode->i_sb->s_dev;
808e749a462SChristoph Hellwig 	ino_t ino = inode->i_ino;
8091da177e4SLinus Torvalds 	struct raparms	*ra, **rap, **frap = NULL;
8101da177e4SLinus Torvalds 	int depth = 0;
811fce1456aSGreg Banks 	unsigned int hash;
812fce1456aSGreg Banks 	struct raparm_hbucket *rab;
8131da177e4SLinus Torvalds 
814fce1456aSGreg Banks 	hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
815fce1456aSGreg Banks 	rab = &raparm_hash[hash];
816fce1456aSGreg Banks 
817fce1456aSGreg Banks 	spin_lock(&rab->pb_lock);
818fce1456aSGreg Banks 	for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
8191da177e4SLinus Torvalds 		if (ra->p_ino == ino && ra->p_dev == dev)
8201da177e4SLinus Torvalds 			goto found;
8211da177e4SLinus Torvalds 		depth++;
8221da177e4SLinus Torvalds 		if (ra->p_count == 0)
8231da177e4SLinus Torvalds 			frap = rap;
8241da177e4SLinus Torvalds 	}
8253aa6e0aaSKonstantin Khorenko 	depth = nfsdstats.ra_size;
8261da177e4SLinus Torvalds 	if (!frap) {
827fce1456aSGreg Banks 		spin_unlock(&rab->pb_lock);
8281da177e4SLinus Torvalds 		return NULL;
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 	rap = frap;
8311da177e4SLinus Torvalds 	ra = *frap;
8321da177e4SLinus Torvalds 	ra->p_dev = dev;
8331da177e4SLinus Torvalds 	ra->p_ino = ino;
8341da177e4SLinus Torvalds 	ra->p_set = 0;
835fce1456aSGreg Banks 	ra->p_hindex = hash;
8361da177e4SLinus Torvalds found:
837fce1456aSGreg Banks 	if (rap != &rab->pb_head) {
8381da177e4SLinus Torvalds 		*rap = ra->p_next;
839fce1456aSGreg Banks 		ra->p_next   = rab->pb_head;
840fce1456aSGreg Banks 		rab->pb_head = ra;
8411da177e4SLinus Torvalds 	}
8421da177e4SLinus Torvalds 	ra->p_count++;
8431da177e4SLinus Torvalds 	nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
844fce1456aSGreg Banks 	spin_unlock(&rab->pb_lock);
845e749a462SChristoph Hellwig 
846e749a462SChristoph Hellwig 	if (ra->p_set)
847e749a462SChristoph Hellwig 		file->f_ra = ra->p_ra;
8481da177e4SLinus Torvalds 	return ra;
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds 
851e749a462SChristoph Hellwig void nfsd_put_raparams(struct file *file, struct raparms *ra)
852e749a462SChristoph Hellwig {
853e749a462SChristoph Hellwig 	struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
854e749a462SChristoph Hellwig 
855e749a462SChristoph Hellwig 	spin_lock(&rab->pb_lock);
856e749a462SChristoph Hellwig 	ra->p_ra = file->f_ra;
857e749a462SChristoph Hellwig 	ra->p_set = 1;
858e749a462SChristoph Hellwig 	ra->p_count--;
859e749a462SChristoph Hellwig 	spin_unlock(&rab->pb_lock);
860e749a462SChristoph Hellwig }
861e749a462SChristoph Hellwig 
8621da177e4SLinus Torvalds /*
863cf8208d0SJens Axboe  * Grab and keep cached pages associated with a file in the svc_rqst
864cf8208d0SJens Axboe  * so that they can be passed to the network sendmsg/sendpage routines
865cf8208d0SJens Axboe  * directly. They will be released after the sending has completed.
8661da177e4SLinus Torvalds  */
8671da177e4SLinus Torvalds static int
868cf8208d0SJens Axboe nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
869cf8208d0SJens Axboe 		  struct splice_desc *sd)
8701da177e4SLinus Torvalds {
871cf8208d0SJens Axboe 	struct svc_rqst *rqstp = sd->u.data;
872afc59400SJ. Bruce Fields 	struct page **pp = rqstp->rq_next_page;
873cf8208d0SJens Axboe 	struct page *page = buf->page;
874cf8208d0SJens Axboe 	size_t size;
875cf8208d0SJens Axboe 
876cf8208d0SJens Axboe 	size = sd->len;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	if (rqstp->rq_res.page_len == 0) {
8791da177e4SLinus Torvalds 		get_page(page);
880afc59400SJ. Bruce Fields 		put_page(*rqstp->rq_next_page);
881afc59400SJ. Bruce Fields 		*(rqstp->rq_next_page++) = page;
882cf8208d0SJens Axboe 		rqstp->rq_res.page_base = buf->offset;
8831da177e4SLinus Torvalds 		rqstp->rq_res.page_len = size;
88444524359SNeilBrown 	} else if (page != pp[-1]) {
8851da177e4SLinus Torvalds 		get_page(page);
886afc59400SJ. Bruce Fields 		if (*rqstp->rq_next_page)
887afc59400SJ. Bruce Fields 			put_page(*rqstp->rq_next_page);
888afc59400SJ. Bruce Fields 		*(rqstp->rq_next_page++) = page;
8891da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
89044524359SNeilBrown 	} else
8911da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	return size;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
896cf8208d0SJens Axboe static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
897cf8208d0SJens Axboe 				    struct splice_desc *sd)
898cf8208d0SJens Axboe {
899cf8208d0SJens Axboe 	return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
900cf8208d0SJens Axboe }
901cf8208d0SJens Axboe 
90287c5942eSChuck Lever static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
90387c5942eSChuck Lever 			       struct file *file, loff_t offset,
90487c5942eSChuck Lever 			       unsigned long *count, int host_err)
9051da177e4SLinus Torvalds {
906dc97618dSJ. Bruce Fields 	if (host_err >= 0) {
907dc97618dSJ. Bruce Fields 		nfsdstats.io_read += host_err;
908dc97618dSJ. Bruce Fields 		*count = host_err;
909dc97618dSJ. Bruce Fields 		fsnotify_access(file);
91087c5942eSChuck Lever 		trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
911dc97618dSJ. Bruce Fields 		return 0;
91287c5942eSChuck Lever 	} else {
91387c5942eSChuck Lever 		trace_nfsd_read_err(rqstp, fhp, offset, host_err);
914dc97618dSJ. Bruce Fields 		return nfserrno(host_err);
915dc97618dSJ. Bruce Fields 	}
91687c5942eSChuck Lever }
9171da177e4SLinus Torvalds 
91887c5942eSChuck Lever __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
919dc97618dSJ. Bruce Fields 			struct file *file, loff_t offset, unsigned long *count)
920dc97618dSJ. Bruce Fields {
921cf8208d0SJens Axboe 	struct splice_desc sd = {
922cf8208d0SJens Axboe 		.len		= 0,
923cf8208d0SJens Axboe 		.total_len	= *count,
924cf8208d0SJens Axboe 		.pos		= offset,
925cf8208d0SJens Axboe 		.u.data		= rqstp,
926cf8208d0SJens Axboe 	};
927dc97618dSJ. Bruce Fields 	int host_err;
928cf8208d0SJens Axboe 
92987c5942eSChuck Lever 	trace_nfsd_read_splice(rqstp, fhp, offset, *count);
930afc59400SJ. Bruce Fields 	rqstp->rq_next_page = rqstp->rq_respages + 1;
931cf8208d0SJens Axboe 	host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
93287c5942eSChuck Lever 	return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
933dc97618dSJ. Bruce Fields }
934dc97618dSJ. Bruce Fields 
93587c5942eSChuck Lever __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
93687c5942eSChuck Lever 		  struct file *file, loff_t offset,
93787c5942eSChuck Lever 		  struct kvec *vec, int vlen, unsigned long *count)
938dc97618dSJ. Bruce Fields {
93973da852eSChristoph Hellwig 	struct iov_iter iter;
940dc97618dSJ. Bruce Fields 	int host_err;
941dc97618dSJ. Bruce Fields 
94287c5942eSChuck Lever 	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
943aa563d7bSDavid Howells 	iov_iter_kvec(&iter, READ, vec, vlen, *count);
94473da852eSChristoph Hellwig 	host_err = vfs_iter_read(file, &iter, &offset, 0);
94587c5942eSChuck Lever 	return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
948d911df7bSJ. Bruce Fields /*
949d911df7bSJ. Bruce Fields  * Gathered writes: If another process is currently writing to the file,
950d911df7bSJ. Bruce Fields  * there's a high chance this is another nfsd (triggered by a bulk write
951d911df7bSJ. Bruce Fields  * from a client's biod). Rather than syncing the file with each write
952d911df7bSJ. Bruce Fields  * request, we sleep for 10 msec.
953d911df7bSJ. Bruce Fields  *
954d911df7bSJ. Bruce Fields  * I don't know if this roughly approximates C. Juszak's idea of
955d911df7bSJ. Bruce Fields  * gathered writes, but it's a nice and simple solution (IMHO), and it
956d911df7bSJ. Bruce Fields  * seems to work:-)
957d911df7bSJ. Bruce Fields  *
958d911df7bSJ. Bruce Fields  * Note: we do this only in the NFSv2 case, since v3 and higher have a
959d911df7bSJ. Bruce Fields  * better tool (separate unstable writes and commits) for solving this
960d911df7bSJ. Bruce Fields  * problem.
961d911df7bSJ. Bruce Fields  */
962d911df7bSJ. Bruce Fields static int wait_for_concurrent_writes(struct file *file)
963d911df7bSJ. Bruce Fields {
964496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
965d911df7bSJ. Bruce Fields 	static ino_t last_ino;
966d911df7bSJ. Bruce Fields 	static dev_t last_dev;
967d911df7bSJ. Bruce Fields 	int err = 0;
968d911df7bSJ. Bruce Fields 
969d911df7bSJ. Bruce Fields 	if (atomic_read(&inode->i_writecount) > 1
970d911df7bSJ. Bruce Fields 	    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
971d911df7bSJ. Bruce Fields 		dprintk("nfsd: write defer %d\n", task_pid_nr(current));
972d911df7bSJ. Bruce Fields 		msleep(10);
973d911df7bSJ. Bruce Fields 		dprintk("nfsd: write resume %d\n", task_pid_nr(current));
974d911df7bSJ. Bruce Fields 	}
975d911df7bSJ. Bruce Fields 
976d911df7bSJ. Bruce Fields 	if (inode->i_state & I_DIRTY) {
977d911df7bSJ. Bruce Fields 		dprintk("nfsd: write sync %d\n", task_pid_nr(current));
9788018ab05SChristoph Hellwig 		err = vfs_fsync(file, 0);
979d911df7bSJ. Bruce Fields 	}
980d911df7bSJ. Bruce Fields 	last_ino = inode->i_ino;
981d911df7bSJ. Bruce Fields 	last_dev = inode->i_sb->s_dev;
982d911df7bSJ. Bruce Fields 	return err;
983d911df7bSJ. Bruce Fields }
984d911df7bSJ. Bruce Fields 
985af90f707SChristoph Hellwig __be32
9861da177e4SLinus Torvalds nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
9871da177e4SLinus Torvalds 				loff_t offset, struct kvec *vec, int vlen,
98854bbb7d2SKinglong Mee 				unsigned long *cnt, int stable)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	struct svc_export	*exp;
99173da852eSChristoph Hellwig 	struct iov_iter		iter;
992d890be15SChuck Lever 	__be32			nfserr;
9936264d69dSAl Viro 	int			host_err;
99448e03bc5STrond Myklebust 	int			use_wgather;
995e49dbbf3SKent Overstreet 	loff_t			pos = offset;
9968658452eSNeilBrown 	unsigned int		pflags = current->flags;
997ddef7ed2SChristoph Hellwig 	rwf_t			flags = 0;
9988658452eSNeilBrown 
999d890be15SChuck Lever 	trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
1000d890be15SChuck Lever 
10017501cc2bSJeff Layton 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
10028658452eSNeilBrown 		/*
10038658452eSNeilBrown 		 * We want less throttling in balance_dirty_pages()
10048658452eSNeilBrown 		 * and shrink_inactive_list() so that nfs to
10058658452eSNeilBrown 		 * localhost doesn't cause nfsd to lock up due to all
10068658452eSNeilBrown 		 * the client's dirty pages or its congested queue.
10078658452eSNeilBrown 		 */
10088658452eSNeilBrown 		current->flags |= PF_LESS_THROTTLE;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	exp = fhp->fh_export;
101148e03bc5STrond Myklebust 	use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	if (!EX_ISSYNC(exp))
101454bbb7d2SKinglong Mee 		stable = NFS_UNSTABLE;
10151da177e4SLinus Torvalds 
101624368aadSChristoph Hellwig 	if (stable && !use_wgather)
101724368aadSChristoph Hellwig 		flags |= RWF_SYNC;
101824368aadSChristoph Hellwig 
1019aa563d7bSDavid Howells 	iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
102073da852eSChristoph Hellwig 	host_err = vfs_iter_write(file, &iter, &pos, flags);
1021e4636d53SJ. Bruce Fields 	if (host_err < 0)
1022e4636d53SJ. Bruce Fields 		goto out_nfserr;
1023d890be15SChuck Lever 	nfsdstats.io_write += *cnt;
10242a12a9d7SEric Paris 	fsnotify_modify(file);
10251da177e4SLinus Torvalds 
102624368aadSChristoph Hellwig 	if (stable && use_wgather)
1027d911df7bSJ. Bruce Fields 		host_err = wait_for_concurrent_writes(file);
10281da177e4SLinus Torvalds 
1029e4636d53SJ. Bruce Fields out_nfserr:
1030d890be15SChuck Lever 	if (host_err >= 0) {
1031d890be15SChuck Lever 		trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1032d890be15SChuck Lever 		nfserr = nfs_ok;
1033d890be15SChuck Lever 	} else {
1034d890be15SChuck Lever 		trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1035d890be15SChuck Lever 		nfserr = nfserrno(host_err);
1036d890be15SChuck Lever 	}
10377501cc2bSJeff Layton 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
1038717a94b5SNeilBrown 		current_restore_flags(pflags, PF_LESS_THROTTLE);
1039d890be15SChuck Lever 	return nfserr;
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
1042dc97618dSJ. Bruce Fields /*
1043dc97618dSJ. Bruce Fields  * Read data from a file. count must contain the requested read count
1044dc97618dSJ. Bruce Fields  * on entry. On return, *count contains the number of bytes actually read.
1045dc97618dSJ. Bruce Fields  * N.B. After this call fhp needs an fh_put
1046dc97618dSJ. Bruce Fields  */
1047dc97618dSJ. Bruce Fields __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1048dc97618dSJ. Bruce Fields 	loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1049dc97618dSJ. Bruce Fields {
1050dc97618dSJ. Bruce Fields 	struct file *file;
1051dc97618dSJ. Bruce Fields 	struct raparms	*ra;
1052dc97618dSJ. Bruce Fields 	__be32 err;
1053dc97618dSJ. Bruce Fields 
1054f394b62bSChuck Lever 	trace_nfsd_read_start(rqstp, fhp, offset, *count);
1055e749a462SChristoph Hellwig 	err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1056dc97618dSJ. Bruce Fields 	if (err)
1057dc97618dSJ. Bruce Fields 		return err;
1058dc97618dSJ. Bruce Fields 
1059e749a462SChristoph Hellwig 	ra = nfsd_init_raparms(file);
10606e8b50d1SJeff Layton 
1061a4058c5bSChristoph Hellwig 	if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
106287c5942eSChuck Lever 		err = nfsd_splice_read(rqstp, fhp, file, offset, count);
1063a4058c5bSChristoph Hellwig 	else
106487c5942eSChuck Lever 		err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count);
10656e8b50d1SJeff Layton 
1066e749a462SChristoph Hellwig 	if (ra)
1067e749a462SChristoph Hellwig 		nfsd_put_raparams(file, ra);
1068e749a462SChristoph Hellwig 	fput(file);
1069dc97618dSJ. Bruce Fields 
1070f394b62bSChuck Lever 	trace_nfsd_read_done(rqstp, fhp, offset, *count);
10716e8b50d1SJeff Layton 
1072fa0a2126SJ. Bruce Fields 	return err;
1073fa0a2126SJ. Bruce Fields }
1074fa0a2126SJ. Bruce Fields 
10751da177e4SLinus Torvalds /*
10761da177e4SLinus Torvalds  * Write data to a file.
10771da177e4SLinus Torvalds  * The stable flag requests synchronous writes.
10781da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
10791da177e4SLinus Torvalds  */
10806264d69dSAl Viro __be32
108152e380e0SKinglong Mee nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
108252e380e0SKinglong Mee 	   struct kvec *vec, int vlen, unsigned long *cnt, int stable)
10831da177e4SLinus Torvalds {
108452e380e0SKinglong Mee 	struct file *file = NULL;
10856264d69dSAl Viro 	__be32 err = 0;
10861da177e4SLinus Torvalds 
1087f394b62bSChuck Lever 	trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
10886e8b50d1SJeff Layton 
10898837abcaSMiklos Szeredi 	err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
10901da177e4SLinus Torvalds 	if (err)
10911da177e4SLinus Torvalds 		goto out;
10921da177e4SLinus Torvalds 
109352e380e0SKinglong Mee 	err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt, stable);
1094fd891454SChristoph Hellwig 	fput(file);
10951da177e4SLinus Torvalds out:
1096f394b62bSChuck Lever 	trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
10971da177e4SLinus Torvalds 	return err;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
11011da177e4SLinus Torvalds /*
11021da177e4SLinus Torvalds  * Commit all pending writes to stable storage.
1103aa696a6fSTrond Myklebust  *
1104aa696a6fSTrond Myklebust  * Note: we only guarantee that data that lies within the range specified
1105aa696a6fSTrond Myklebust  * by the 'offset' and 'count' parameters will be synced.
11061da177e4SLinus Torvalds  *
11071da177e4SLinus Torvalds  * Unfortunately we cannot lock the file to make sure we return full WCC
11081da177e4SLinus Torvalds  * data to the client, as locking happens lower down in the filesystem.
11091da177e4SLinus Torvalds  */
11106264d69dSAl Viro __be32
11111da177e4SLinus Torvalds nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
11121da177e4SLinus Torvalds                loff_t offset, unsigned long count)
11131da177e4SLinus Torvalds {
11141da177e4SLinus Torvalds 	struct file	*file;
1115aa696a6fSTrond Myklebust 	loff_t		end = LLONG_MAX;
1116aa696a6fSTrond Myklebust 	__be32		err = nfserr_inval;
11171da177e4SLinus Torvalds 
1118aa696a6fSTrond Myklebust 	if (offset < 0)
1119aa696a6fSTrond Myklebust 		goto out;
1120aa696a6fSTrond Myklebust 	if (count != 0) {
1121aa696a6fSTrond Myklebust 		end = offset + (loff_t)count - 1;
1122aa696a6fSTrond Myklebust 		if (end < offset)
1123aa696a6fSTrond Myklebust 			goto out;
1124aa696a6fSTrond Myklebust 	}
11251da177e4SLinus Torvalds 
112691885258SJeff Layton 	err = nfsd_open(rqstp, fhp, S_IFREG,
112791885258SJeff Layton 			NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
11288837abcaSMiklos Szeredi 	if (err)
1129aa696a6fSTrond Myklebust 		goto out;
11301da177e4SLinus Torvalds 	if (EX_ISSYNC(fhp->fh_export)) {
11318018ab05SChristoph Hellwig 		int err2 = vfs_fsync_range(file, offset, end, 0);
1132aa696a6fSTrond Myklebust 
1133aa696a6fSTrond Myklebust 		if (err2 != -EINVAL)
1134aa696a6fSTrond Myklebust 			err = nfserrno(err2);
1135aa696a6fSTrond Myklebust 		else
11361da177e4SLinus Torvalds 			err = nfserr_notsupp;
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds 
1139fd891454SChristoph Hellwig 	fput(file);
1140aa696a6fSTrond Myklebust out:
11411da177e4SLinus Torvalds 	return err;
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
11441da177e4SLinus Torvalds 
1145f2b0dee2SAdrian Bunk static __be32
11465c002b3bSJ. Bruce Fields nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
11475c002b3bSJ. Bruce Fields 			struct iattr *iap)
11485c002b3bSJ. Bruce Fields {
11495c002b3bSJ. Bruce Fields 	/*
11505c002b3bSJ. Bruce Fields 	 * Mode has already been set earlier in create:
11515c002b3bSJ. Bruce Fields 	 */
11525c002b3bSJ. Bruce Fields 	iap->ia_valid &= ~ATTR_MODE;
11535c002b3bSJ. Bruce Fields 	/*
11545c002b3bSJ. Bruce Fields 	 * Setting uid/gid works only for root.  Irix appears to
11555c002b3bSJ. Bruce Fields 	 * send along the gid on create when it tries to implement
11565c002b3bSJ. Bruce Fields 	 * setgid directories via NFS:
11575c002b3bSJ. Bruce Fields 	 */
11586fab8779SEric W. Biederman 	if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
11595c002b3bSJ. Bruce Fields 		iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
11605c002b3bSJ. Bruce Fields 	if (iap->ia_valid)
11615c002b3bSJ. Bruce Fields 		return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
11620f3a24b4STrond Myklebust 	/* Callers expect file metadata to be committed here */
1163722b620dSJeff Layton 	return nfserrno(commit_metadata(resfhp));
11645c002b3bSJ. Bruce Fields }
11655c002b3bSJ. Bruce Fields 
11664ac35c2fSwengang wang /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
11674ac35c2fSwengang wang  * setting size to 0 may fail for some specific file systems by the permission
11684ac35c2fSwengang wang  * checking which requires WRITE permission but the mode is 000.
11694ac35c2fSwengang wang  * we ignore the resizing(to 0) on the just new created file, since the size is
11704ac35c2fSwengang wang  * 0 after file created.
11714ac35c2fSwengang wang  *
11724ac35c2fSwengang wang  * call this only after vfs_create() is called.
11734ac35c2fSwengang wang  * */
11744ac35c2fSwengang wang static void
11754ac35c2fSwengang wang nfsd_check_ignore_resizing(struct iattr *iap)
11764ac35c2fSwengang wang {
11774ac35c2fSwengang wang 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
11784ac35c2fSwengang wang 		iap->ia_valid &= ~ATTR_SIZE;
11794ac35c2fSwengang wang }
11804ac35c2fSwengang wang 
1181b44061d0SJ. Bruce Fields /* The parent directory should already be locked: */
11826264d69dSAl Viro __be32
1183b44061d0SJ. Bruce Fields nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
11841da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
11851da177e4SLinus Torvalds 		int type, dev_t rdev, struct svc_fh *resfhp)
11861da177e4SLinus Torvalds {
11872b118859SDan Carpenter 	struct dentry	*dentry, *dchild;
11881da177e4SLinus Torvalds 	struct inode	*dirp;
11896264d69dSAl Viro 	__be32		err;
11905c002b3bSJ. Bruce Fields 	__be32		err2;
11916264d69dSAl Viro 	int		host_err;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
11942b0143b5SDavid Howells 	dirp = d_inode(dentry);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	dchild = dget(resfhp->fh_dentry);
11971da177e4SLinus Torvalds 	if (!fhp->fh_locked) {
1198b44061d0SJ. Bruce Fields 		WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1199a6a9f18fSAl Viro 				dentry);
1200d75f2b9fSAl Viro 		err = nfserr_io;
12011da177e4SLinus Torvalds 		goto out;
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 
12047eed34f1SOleg Drokin 	err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
12057eed34f1SOleg Drokin 	if (err)
12067eed34f1SOleg Drokin 		goto out;
12077eed34f1SOleg Drokin 
12081da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
12091da177e4SLinus Torvalds 		iap->ia_mode = 0;
12101da177e4SLinus Torvalds 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
12111da177e4SLinus Torvalds 
1212088406bcSJ. Bruce Fields 	err = 0;
12134a55c101SJan Kara 	host_err = 0;
12141da177e4SLinus Torvalds 	switch (type) {
12151da177e4SLinus Torvalds 	case S_IFREG:
1216312b63fbSAl Viro 		host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
12174ac35c2fSwengang wang 		if (!host_err)
12184ac35c2fSwengang wang 			nfsd_check_ignore_resizing(iap);
12191da177e4SLinus Torvalds 		break;
12201da177e4SLinus Torvalds 	case S_IFDIR:
12216264d69dSAl Viro 		host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
12223819bb0dSAl Viro 		if (!host_err && unlikely(d_unhashed(dchild))) {
12233819bb0dSAl Viro 			struct dentry *d;
12243819bb0dSAl Viro 			d = lookup_one_len(dchild->d_name.name,
12253819bb0dSAl Viro 					   dchild->d_parent,
12263819bb0dSAl Viro 					   dchild->d_name.len);
12273819bb0dSAl Viro 			if (IS_ERR(d)) {
12283819bb0dSAl Viro 				host_err = PTR_ERR(d);
12293819bb0dSAl Viro 				break;
12303819bb0dSAl Viro 			}
12313819bb0dSAl Viro 			if (unlikely(d_is_negative(d))) {
12323819bb0dSAl Viro 				dput(d);
12333819bb0dSAl Viro 				err = nfserr_serverfault;
12343819bb0dSAl Viro 				goto out;
12353819bb0dSAl Viro 			}
12363819bb0dSAl Viro 			dput(resfhp->fh_dentry);
12373819bb0dSAl Viro 			resfhp->fh_dentry = dget(d);
12383819bb0dSAl Viro 			err = fh_update(resfhp);
12393819bb0dSAl Viro 			dput(dchild);
12403819bb0dSAl Viro 			dchild = d;
12413819bb0dSAl Viro 			if (err)
12423819bb0dSAl Viro 				goto out;
12433819bb0dSAl Viro 		}
12441da177e4SLinus Torvalds 		break;
12451da177e4SLinus Torvalds 	case S_IFCHR:
12461da177e4SLinus Torvalds 	case S_IFBLK:
12471da177e4SLinus Torvalds 	case S_IFIFO:
12481da177e4SLinus Torvalds 	case S_IFSOCK:
12496264d69dSAl Viro 		host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
12501da177e4SLinus Torvalds 		break;
125171423274SJ. Bruce Fields 	default:
125271423274SJ. Bruce Fields 		printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
125371423274SJ. Bruce Fields 		       type);
125471423274SJ. Bruce Fields 		host_err = -EINVAL;
1255463c3197SDave Hansen 	}
12564a55c101SJan Kara 	if (host_err < 0)
1257463c3197SDave Hansen 		goto out_nfserr;
12581da177e4SLinus Torvalds 
1259f501912aSBen Myers 	err = nfsd_create_setattr(rqstp, resfhp, iap);
12601da177e4SLinus Torvalds 
1261f501912aSBen Myers 	/*
12620f3a24b4STrond Myklebust 	 * nfsd_create_setattr already committed the child.  Transactional
12630f3a24b4STrond Myklebust 	 * filesystems had a chance to commit changes for both parent and
1264b44061d0SJ. Bruce Fields 	 * child simultaneously making the following commit_metadata a
12650f3a24b4STrond Myklebust 	 * noop.
1266f501912aSBen Myers 	 */
1267f501912aSBen Myers 	err2 = nfserrno(commit_metadata(fhp));
1268f193fbabSYAMAMOTO Takashi 	if (err2)
1269f193fbabSYAMAMOTO Takashi 		err = err2;
12701da177e4SLinus Torvalds 	/*
12711da177e4SLinus Torvalds 	 * Update the file handle to get the new inode info.
12721da177e4SLinus Torvalds 	 */
12731da177e4SLinus Torvalds 	if (!err)
12741da177e4SLinus Torvalds 		err = fh_update(resfhp);
12751da177e4SLinus Torvalds out:
12761da177e4SLinus Torvalds 	dput(dchild);
12771da177e4SLinus Torvalds 	return err;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds out_nfserr:
12806264d69dSAl Viro 	err = nfserrno(host_err);
12811da177e4SLinus Torvalds 	goto out;
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
1284b44061d0SJ. Bruce Fields /*
1285b44061d0SJ. Bruce Fields  * Create a filesystem object (regular, directory, special).
1286b44061d0SJ. Bruce Fields  * Note that the parent directory is left locked.
1287b44061d0SJ. Bruce Fields  *
1288b44061d0SJ. Bruce Fields  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1289b44061d0SJ. Bruce Fields  */
1290b44061d0SJ. Bruce Fields __be32
1291b44061d0SJ. Bruce Fields nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1292b44061d0SJ. Bruce Fields 		char *fname, int flen, struct iattr *iap,
1293b44061d0SJ. Bruce Fields 		int type, dev_t rdev, struct svc_fh *resfhp)
1294b44061d0SJ. Bruce Fields {
1295b44061d0SJ. Bruce Fields 	struct dentry	*dentry, *dchild = NULL;
1296b44061d0SJ. Bruce Fields 	__be32		err;
1297b44061d0SJ. Bruce Fields 	int		host_err;
1298b44061d0SJ. Bruce Fields 
1299b44061d0SJ. Bruce Fields 	if (isdotent(fname, flen))
1300b44061d0SJ. Bruce Fields 		return nfserr_exist;
1301b44061d0SJ. Bruce Fields 
1302fa08139dSJ. Bruce Fields 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1303b44061d0SJ. Bruce Fields 	if (err)
1304b44061d0SJ. Bruce Fields 		return err;
1305b44061d0SJ. Bruce Fields 
1306b44061d0SJ. Bruce Fields 	dentry = fhp->fh_dentry;
1307b44061d0SJ. Bruce Fields 
1308b44061d0SJ. Bruce Fields 	host_err = fh_want_write(fhp);
1309b44061d0SJ. Bruce Fields 	if (host_err)
1310b44061d0SJ. Bruce Fields 		return nfserrno(host_err);
1311b44061d0SJ. Bruce Fields 
1312b44061d0SJ. Bruce Fields 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1313b44061d0SJ. Bruce Fields 	dchild = lookup_one_len(fname, dentry, flen);
1314b44061d0SJ. Bruce Fields 	host_err = PTR_ERR(dchild);
1315b44061d0SJ. Bruce Fields 	if (IS_ERR(dchild))
1316b44061d0SJ. Bruce Fields 		return nfserrno(host_err);
1317b44061d0SJ. Bruce Fields 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1318502aa0a5SJosef Bacik 	/*
1319502aa0a5SJosef Bacik 	 * We unconditionally drop our ref to dchild as fh_compose will have
1320502aa0a5SJosef Bacik 	 * already grabbed its own ref for it.
1321502aa0a5SJosef Bacik 	 */
1322b44061d0SJ. Bruce Fields 	dput(dchild);
1323502aa0a5SJosef Bacik 	if (err)
1324b44061d0SJ. Bruce Fields 		return err;
1325b44061d0SJ. Bruce Fields 	return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1326b44061d0SJ. Bruce Fields 					rdev, resfhp);
1327b44061d0SJ. Bruce Fields }
1328b44061d0SJ. Bruce Fields 
13291da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
1330ac6721a1SMi Jinlong 
13311da177e4SLinus Torvalds /*
1332ac6721a1SMi Jinlong  * NFSv3 and NFSv4 version of nfsd_create
13331da177e4SLinus Torvalds  */
13346264d69dSAl Viro __be32
1335ac6721a1SMi Jinlong do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
13361da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
13371da177e4SLinus Torvalds 		struct svc_fh *resfhp, int createmode, u32 *verifier,
1338856121b2SJ. Bruce Fields 	        bool *truncp, bool *created)
13391da177e4SLinus Torvalds {
13401da177e4SLinus Torvalds 	struct dentry	*dentry, *dchild = NULL;
13411da177e4SLinus Torvalds 	struct inode	*dirp;
13426264d69dSAl Viro 	__be32		err;
13436264d69dSAl Viro 	int		host_err;
13441da177e4SLinus Torvalds 	__u32		v_mtime=0, v_atime=0;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	err = nfserr_perm;
13471da177e4SLinus Torvalds 	if (!flen)
13481da177e4SLinus Torvalds 		goto out;
13491da177e4SLinus Torvalds 	err = nfserr_exist;
13501da177e4SLinus Torvalds 	if (isdotent(fname, flen))
13511da177e4SLinus Torvalds 		goto out;
13521da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
13531da177e4SLinus Torvalds 		iap->ia_mode = 0;
13541574dff8SSachin Prabhu 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
13551da177e4SLinus Torvalds 	if (err)
13561da177e4SLinus Torvalds 		goto out;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
13592b0143b5SDavid Howells 	dirp = d_inode(dentry);
13601da177e4SLinus Torvalds 
13614a55c101SJan Kara 	host_err = fh_want_write(fhp);
13624a55c101SJan Kara 	if (host_err)
13634a55c101SJan Kara 		goto out_nfserr;
13644a55c101SJan Kara 
136512fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	/*
13681da177e4SLinus Torvalds 	 * Compose the response file handle.
13691da177e4SLinus Torvalds 	 */
13701da177e4SLinus Torvalds 	dchild = lookup_one_len(fname, dentry, flen);
13716264d69dSAl Viro 	host_err = PTR_ERR(dchild);
13721da177e4SLinus Torvalds 	if (IS_ERR(dchild))
13731da177e4SLinus Torvalds 		goto out_nfserr;
13741da177e4SLinus Torvalds 
13751574dff8SSachin Prabhu 	/* If file doesn't exist, check for permissions to create one */
13762b0143b5SDavid Howells 	if (d_really_is_negative(dchild)) {
13771574dff8SSachin Prabhu 		err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
13781574dff8SSachin Prabhu 		if (err)
13791574dff8SSachin Prabhu 			goto out;
13801574dff8SSachin Prabhu 	}
13811574dff8SSachin Prabhu 
13821da177e4SLinus Torvalds 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
13831da177e4SLinus Torvalds 	if (err)
13841da177e4SLinus Torvalds 		goto out;
13851da177e4SLinus Torvalds 
1386ac6721a1SMi Jinlong 	if (nfsd_create_is_exclusive(createmode)) {
1387c397852cSPeter Staubach 		/* solaris7 gets confused (bugid 4218508) if these have
1388749997e5SJeff Layton 		 * the high bit set, so just clear the high bits. If this is
1389749997e5SJeff Layton 		 * ever changed to use different attrs for storing the
1390749997e5SJeff Layton 		 * verifier, then do_open_lookup() will also need to be fixed
1391749997e5SJeff Layton 		 * accordingly.
13921da177e4SLinus Torvalds 		 */
13931da177e4SLinus Torvalds 		v_mtime = verifier[0]&0x7fffffff;
13941da177e4SLinus Torvalds 		v_atime = verifier[1]&0x7fffffff;
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds 
13972b0143b5SDavid Howells 	if (d_really_is_positive(dchild)) {
13981da177e4SLinus Torvalds 		err = 0;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 		switch (createmode) {
14011da177e4SLinus Torvalds 		case NFS3_CREATE_UNCHECKED:
1402e36cb0b8SDavid Howells 			if (! d_is_reg(dchild))
14039dc4e6c4SJ. Bruce Fields 				goto out;
14041da177e4SLinus Torvalds 			else if (truncp) {
14051da177e4SLinus Torvalds 				/* in nfsv4, we need to treat this case a little
14061da177e4SLinus Torvalds 				 * differently.  we don't want to truncate the
14071da177e4SLinus Torvalds 				 * file now; this would be wrong if the OPEN
14081da177e4SLinus Torvalds 				 * fails for some other reason.  furthermore,
14091da177e4SLinus Torvalds 				 * if the size is nonzero, we should ignore it
14101da177e4SLinus Torvalds 				 * according to spec!
14111da177e4SLinus Torvalds 				 */
14121da177e4SLinus Torvalds 				*truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
14131da177e4SLinus Torvalds 			}
14141da177e4SLinus Torvalds 			else {
14151da177e4SLinus Torvalds 				iap->ia_valid &= ATTR_SIZE;
14161da177e4SLinus Torvalds 				goto set_attr;
14171da177e4SLinus Torvalds 			}
14181da177e4SLinus Torvalds 			break;
14191da177e4SLinus Torvalds 		case NFS3_CREATE_EXCLUSIVE:
14202b0143b5SDavid Howells 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
14212b0143b5SDavid Howells 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
14222b0143b5SDavid Howells 			    && d_inode(dchild)->i_size  == 0 ) {
14237007c90fSNeil Brown 				if (created)
14247007c90fSNeil Brown 					*created = 1;
14251da177e4SLinus Torvalds 				break;
14267007c90fSNeil Brown 			}
14270ac203cbSGustavo A. R. Silva 			/* fall through */
1428ac6721a1SMi Jinlong 		case NFS4_CREATE_EXCLUSIVE4_1:
14292b0143b5SDavid Howells 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
14302b0143b5SDavid Howells 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
14312b0143b5SDavid Howells 			    && d_inode(dchild)->i_size  == 0 ) {
14327007c90fSNeil Brown 				if (created)
14337007c90fSNeil Brown 					*created = 1;
1434ac6721a1SMi Jinlong 				goto set_attr;
14357007c90fSNeil Brown 			}
14360ac203cbSGustavo A. R. Silva 			/* fall through */
14371da177e4SLinus Torvalds 		case NFS3_CREATE_GUARDED:
14381da177e4SLinus Torvalds 			err = nfserr_exist;
14391da177e4SLinus Torvalds 		}
1440bad0dcffSAl Viro 		fh_drop_write(fhp);
14411da177e4SLinus Torvalds 		goto out;
14421da177e4SLinus Torvalds 	}
14431da177e4SLinus Torvalds 
1444312b63fbSAl Viro 	host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1445463c3197SDave Hansen 	if (host_err < 0) {
1446bad0dcffSAl Viro 		fh_drop_write(fhp);
14471da177e4SLinus Torvalds 		goto out_nfserr;
1448463c3197SDave Hansen 	}
144981ac95c5SJ. Bruce Fields 	if (created)
145081ac95c5SJ. Bruce Fields 		*created = 1;
14511da177e4SLinus Torvalds 
14524ac35c2fSwengang wang 	nfsd_check_ignore_resizing(iap);
14534ac35c2fSwengang wang 
1454ac6721a1SMi Jinlong 	if (nfsd_create_is_exclusive(createmode)) {
1455c397852cSPeter Staubach 		/* Cram the verifier into atime/mtime */
14561da177e4SLinus Torvalds 		iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1457c397852cSPeter Staubach 			| ATTR_MTIME_SET|ATTR_ATIME_SET;
14581da177e4SLinus Torvalds 		/* XXX someone who knows this better please fix it for nsec */
14591da177e4SLinus Torvalds 		iap->ia_mtime.tv_sec = v_mtime;
14601da177e4SLinus Torvalds 		iap->ia_atime.tv_sec = v_atime;
14611da177e4SLinus Torvalds 		iap->ia_mtime.tv_nsec = 0;
14621da177e4SLinus Torvalds 		iap->ia_atime.tv_nsec = 0;
14631da177e4SLinus Torvalds 	}
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds  set_attr:
1466f501912aSBen Myers 	err = nfsd_create_setattr(rqstp, resfhp, iap);
1467f501912aSBen Myers 
1468f501912aSBen Myers 	/*
14690f3a24b4STrond Myklebust 	 * nfsd_create_setattr already committed the child
14700f3a24b4STrond Myklebust 	 * (and possibly also the parent).
1471f501912aSBen Myers 	 */
1472f501912aSBen Myers 	if (!err)
1473f501912aSBen Myers 		err = nfserrno(commit_metadata(fhp));
1474f193fbabSYAMAMOTO Takashi 
1475f193fbabSYAMAMOTO Takashi 	/*
1476f193fbabSYAMAMOTO Takashi 	 * Update the filehandle to get the new inode info.
1477f193fbabSYAMAMOTO Takashi 	 */
1478f193fbabSYAMAMOTO Takashi 	if (!err)
1479f193fbabSYAMAMOTO Takashi 		err = fh_update(resfhp);
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds  out:
14821da177e4SLinus Torvalds 	fh_unlock(fhp);
14831da177e4SLinus Torvalds 	if (dchild && !IS_ERR(dchild))
14841da177e4SLinus Torvalds 		dput(dchild);
14854a55c101SJan Kara 	fh_drop_write(fhp);
14861da177e4SLinus Torvalds  	return err;
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds  out_nfserr:
14896264d69dSAl Viro 	err = nfserrno(host_err);
14901da177e4SLinus Torvalds 	goto out;
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds /*
14951da177e4SLinus Torvalds  * Read a symlink. On entry, *lenp must contain the maximum path length that
14961da177e4SLinus Torvalds  * fits into the buffer. On return, it contains the true length.
14971da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
14981da177e4SLinus Torvalds  */
14996264d69dSAl Viro __be32
15001da177e4SLinus Torvalds nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
15011da177e4SLinus Torvalds {
15026264d69dSAl Viro 	__be32		err;
15034d7edbc3SAl Viro 	const char *link;
150468ac1234SAl Viro 	struct path path;
15054d7edbc3SAl Viro 	DEFINE_DELAYED_CALL(done);
15064d7edbc3SAl Viro 	int len;
15071da177e4SLinus Torvalds 
15088837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
15094d7edbc3SAl Viro 	if (unlikely(err))
15104d7edbc3SAl Viro 		return err;
15111da177e4SLinus Torvalds 
151268ac1234SAl Viro 	path.mnt = fhp->fh_export->ex_path.mnt;
151368ac1234SAl Viro 	path.dentry = fhp->fh_dentry;
15141da177e4SLinus Torvalds 
15154d7edbc3SAl Viro 	if (unlikely(!d_is_symlink(path.dentry)))
15164d7edbc3SAl Viro 		return nfserr_inval;
15171da177e4SLinus Torvalds 
151868ac1234SAl Viro 	touch_atime(&path);
15191da177e4SLinus Torvalds 
15204d7edbc3SAl Viro 	link = vfs_get_link(path.dentry, &done);
15214d7edbc3SAl Viro 	if (IS_ERR(link))
15224d7edbc3SAl Viro 		return nfserrno(PTR_ERR(link));
15231da177e4SLinus Torvalds 
15244d7edbc3SAl Viro 	len = strlen(link);
15254d7edbc3SAl Viro 	if (len < *lenp)
15264d7edbc3SAl Viro 		*lenp = len;
15274d7edbc3SAl Viro 	memcpy(buf, link, *lenp);
15284d7edbc3SAl Viro 	do_delayed_call(&done);
15294d7edbc3SAl Viro 	return 0;
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds /*
15331da177e4SLinus Torvalds  * Create a symlink and look up its inode
15341da177e4SLinus Torvalds  * N.B. After this call _both_ fhp and resfhp need an fh_put
15351da177e4SLinus Torvalds  */
15366264d69dSAl Viro __be32
15371da177e4SLinus Torvalds nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
15381da177e4SLinus Torvalds 				char *fname, int flen,
153952ee0433SJ. Bruce Fields 				char *path,
15401e444f5bSKinglong Mee 				struct svc_fh *resfhp)
15411da177e4SLinus Torvalds {
15421da177e4SLinus Torvalds 	struct dentry	*dentry, *dnew;
15436264d69dSAl Viro 	__be32		err, cerr;
15446264d69dSAl Viro 	int		host_err;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	err = nfserr_noent;
154752ee0433SJ. Bruce Fields 	if (!flen || path[0] == '\0')
15481da177e4SLinus Torvalds 		goto out;
15491da177e4SLinus Torvalds 	err = nfserr_exist;
15501da177e4SLinus Torvalds 	if (isdotent(fname, flen))
15511da177e4SLinus Torvalds 		goto out;
15521da177e4SLinus Torvalds 
15538837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
15541da177e4SLinus Torvalds 	if (err)
15551da177e4SLinus Torvalds 		goto out;
15564a55c101SJan Kara 
15574a55c101SJan Kara 	host_err = fh_want_write(fhp);
15584a55c101SJan Kara 	if (host_err)
15594a55c101SJan Kara 		goto out_nfserr;
15604a55c101SJan Kara 
15611da177e4SLinus Torvalds 	fh_lock(fhp);
15621da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
15631da177e4SLinus Torvalds 	dnew = lookup_one_len(fname, dentry, flen);
15646264d69dSAl Viro 	host_err = PTR_ERR(dnew);
15651da177e4SLinus Torvalds 	if (IS_ERR(dnew))
15661da177e4SLinus Torvalds 		goto out_nfserr;
15671da177e4SLinus Torvalds 
15682b0143b5SDavid Howells 	host_err = vfs_symlink(d_inode(dentry), dnew, path);
15696264d69dSAl Viro 	err = nfserrno(host_err);
1570f501912aSBen Myers 	if (!err)
1571f501912aSBen Myers 		err = nfserrno(commit_metadata(fhp));
15721da177e4SLinus Torvalds 	fh_unlock(fhp);
15731da177e4SLinus Torvalds 
1574bad0dcffSAl Viro 	fh_drop_write(fhp);
157575c3f29dSDave Hansen 
15761da177e4SLinus Torvalds 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
15771da177e4SLinus Torvalds 	dput(dnew);
15781da177e4SLinus Torvalds 	if (err==0) err = cerr;
15791da177e4SLinus Torvalds out:
15801da177e4SLinus Torvalds 	return err;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds out_nfserr:
15836264d69dSAl Viro 	err = nfserrno(host_err);
15841da177e4SLinus Torvalds 	goto out;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds /*
15881da177e4SLinus Torvalds  * Create a hardlink
15891da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
15901da177e4SLinus Torvalds  */
15916264d69dSAl Viro __be32
15921da177e4SLinus Torvalds nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
15931da177e4SLinus Torvalds 				char *name, int len, struct svc_fh *tfhp)
15941da177e4SLinus Torvalds {
15951da177e4SLinus Torvalds 	struct dentry	*ddir, *dnew, *dold;
159655b13354SJ. Bruce Fields 	struct inode	*dirp;
15976264d69dSAl Viro 	__be32		err;
15986264d69dSAl Viro 	int		host_err;
15991da177e4SLinus Torvalds 
16008837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
16011da177e4SLinus Torvalds 	if (err)
16021da177e4SLinus Torvalds 		goto out;
16037d818a7bSJ. Bruce Fields 	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
16041da177e4SLinus Torvalds 	if (err)
16051da177e4SLinus Torvalds 		goto out;
16067d818a7bSJ. Bruce Fields 	err = nfserr_isdir;
1607e36cb0b8SDavid Howells 	if (d_is_dir(tfhp->fh_dentry))
16087d818a7bSJ. Bruce Fields 		goto out;
16091da177e4SLinus Torvalds 	err = nfserr_perm;
16101da177e4SLinus Torvalds 	if (!len)
16111da177e4SLinus Torvalds 		goto out;
16121da177e4SLinus Torvalds 	err = nfserr_exist;
16131da177e4SLinus Torvalds 	if (isdotent(name, len))
16141da177e4SLinus Torvalds 		goto out;
16151da177e4SLinus Torvalds 
16164a55c101SJan Kara 	host_err = fh_want_write(tfhp);
16174a55c101SJan Kara 	if (host_err) {
16184a55c101SJan Kara 		err = nfserrno(host_err);
16194a55c101SJan Kara 		goto out;
16204a55c101SJan Kara 	}
16214a55c101SJan Kara 
162212fd3520SPeter Zijlstra 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
16231da177e4SLinus Torvalds 	ddir = ffhp->fh_dentry;
16242b0143b5SDavid Howells 	dirp = d_inode(ddir);
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	dnew = lookup_one_len(name, ddir, len);
16276264d69dSAl Viro 	host_err = PTR_ERR(dnew);
16281da177e4SLinus Torvalds 	if (IS_ERR(dnew))
16291da177e4SLinus Torvalds 		goto out_nfserr;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	dold = tfhp->fh_dentry;
16321da177e4SLinus Torvalds 
16334795bb37SJ. Bruce Fields 	err = nfserr_noent;
16342b0143b5SDavid Howells 	if (d_really_is_negative(dold))
16354a55c101SJan Kara 		goto out_dput;
1636146a8595SJ. Bruce Fields 	host_err = vfs_link(dold, dirp, dnew, NULL);
16376264d69dSAl Viro 	if (!host_err) {
1638f501912aSBen Myers 		err = nfserrno(commit_metadata(ffhp));
1639f501912aSBen Myers 		if (!err)
1640f501912aSBen Myers 			err = nfserrno(commit_metadata(tfhp));
16411da177e4SLinus Torvalds 	} else {
16426264d69dSAl Viro 		if (host_err == -EXDEV && rqstp->rq_vers == 2)
16431da177e4SLinus Torvalds 			err = nfserr_acces;
16441da177e4SLinus Torvalds 		else
16456264d69dSAl Viro 			err = nfserrno(host_err);
16461da177e4SLinus Torvalds 	}
164775c3f29dSDave Hansen out_dput:
16481da177e4SLinus Torvalds 	dput(dnew);
1649270d56e5SDavid M. Richter out_unlock:
1650270d56e5SDavid M. Richter 	fh_unlock(ffhp);
16514a55c101SJan Kara 	fh_drop_write(tfhp);
16521da177e4SLinus Torvalds out:
16531da177e4SLinus Torvalds 	return err;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds out_nfserr:
16566264d69dSAl Viro 	err = nfserrno(host_err);
1657270d56e5SDavid M. Richter 	goto out_unlock;
16581da177e4SLinus Torvalds }
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds /*
16611da177e4SLinus Torvalds  * Rename a file
16621da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
16631da177e4SLinus Torvalds  */
16646264d69dSAl Viro __be32
16651da177e4SLinus Torvalds nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
16661da177e4SLinus Torvalds 			    struct svc_fh *tfhp, char *tname, int tlen)
16671da177e4SLinus Torvalds {
16681da177e4SLinus Torvalds 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
16691da177e4SLinus Torvalds 	struct inode	*fdir, *tdir;
16706264d69dSAl Viro 	__be32		err;
16716264d69dSAl Viro 	int		host_err;
16721da177e4SLinus Torvalds 
16738837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
16741da177e4SLinus Torvalds 	if (err)
16751da177e4SLinus Torvalds 		goto out;
16768837abcaSMiklos Szeredi 	err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
16771da177e4SLinus Torvalds 	if (err)
16781da177e4SLinus Torvalds 		goto out;
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	fdentry = ffhp->fh_dentry;
16812b0143b5SDavid Howells 	fdir = d_inode(fdentry);
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	tdentry = tfhp->fh_dentry;
16842b0143b5SDavid Howells 	tdir = d_inode(tdentry);
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	err = nfserr_perm;
16871da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
16881da177e4SLinus Torvalds 		goto out;
16891da177e4SLinus Torvalds 
16904a55c101SJan Kara 	host_err = fh_want_write(ffhp);
16914a55c101SJan Kara 	if (host_err) {
16924a55c101SJan Kara 		err = nfserrno(host_err);
16934a55c101SJan Kara 		goto out;
16944a55c101SJan Kara 	}
16954a55c101SJan Kara 
16961da177e4SLinus Torvalds 	/* cannot use fh_lock as we need deadlock protective ordering
16971da177e4SLinus Torvalds 	 * so do it by hand */
16981da177e4SLinus Torvalds 	trap = lock_rename(tdentry, fdentry);
1699aaf91ec1SJeff Layton 	ffhp->fh_locked = tfhp->fh_locked = true;
17001da177e4SLinus Torvalds 	fill_pre_wcc(ffhp);
17011da177e4SLinus Torvalds 	fill_pre_wcc(tfhp);
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	odentry = lookup_one_len(fname, fdentry, flen);
17046264d69dSAl Viro 	host_err = PTR_ERR(odentry);
17051da177e4SLinus Torvalds 	if (IS_ERR(odentry))
17061da177e4SLinus Torvalds 		goto out_nfserr;
17071da177e4SLinus Torvalds 
17086264d69dSAl Viro 	host_err = -ENOENT;
17092b0143b5SDavid Howells 	if (d_really_is_negative(odentry))
17101da177e4SLinus Torvalds 		goto out_dput_old;
17116264d69dSAl Viro 	host_err = -EINVAL;
17121da177e4SLinus Torvalds 	if (odentry == trap)
17131da177e4SLinus Torvalds 		goto out_dput_old;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	ndentry = lookup_one_len(tname, tdentry, tlen);
17166264d69dSAl Viro 	host_err = PTR_ERR(ndentry);
17171da177e4SLinus Torvalds 	if (IS_ERR(ndentry))
17181da177e4SLinus Torvalds 		goto out_dput_old;
17196264d69dSAl Viro 	host_err = -ENOTEMPTY;
17201da177e4SLinus Torvalds 	if (ndentry == trap)
17211da177e4SLinus Torvalds 		goto out_dput_new;
17221da177e4SLinus Torvalds 
17239079b1ebSDave Hansen 	host_err = -EXDEV;
17249079b1ebSDave Hansen 	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
17259079b1ebSDave Hansen 		goto out_dput_new;
1726aa387d6cSJ. Bruce Fields 	if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1727aa387d6cSJ. Bruce Fields 		goto out_dput_new;
17289079b1ebSDave Hansen 
1729520c8b16SMiklos Szeredi 	host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
1730f501912aSBen Myers 	if (!host_err) {
1731f501912aSBen Myers 		host_err = commit_metadata(tfhp);
17326264d69dSAl Viro 		if (!host_err)
1733f501912aSBen Myers 			host_err = commit_metadata(ffhp);
17341da177e4SLinus Torvalds 	}
17351da177e4SLinus Torvalds  out_dput_new:
17361da177e4SLinus Torvalds 	dput(ndentry);
17371da177e4SLinus Torvalds  out_dput_old:
17381da177e4SLinus Torvalds 	dput(odentry);
17391da177e4SLinus Torvalds  out_nfserr:
17406264d69dSAl Viro 	err = nfserrno(host_err);
1741fbb74a34SJ. Bruce Fields 	/*
1742fbb74a34SJ. Bruce Fields 	 * We cannot rely on fh_unlock on the two filehandles,
17431da177e4SLinus Torvalds 	 * as that would do the wrong thing if the two directories
1744fbb74a34SJ. Bruce Fields 	 * were the same, so again we do it by hand.
17451da177e4SLinus Torvalds 	 */
17461da177e4SLinus Torvalds 	fill_post_wcc(ffhp);
17471da177e4SLinus Torvalds 	fill_post_wcc(tfhp);
17481da177e4SLinus Torvalds 	unlock_rename(tdentry, fdentry);
1749aaf91ec1SJeff Layton 	ffhp->fh_locked = tfhp->fh_locked = false;
17504a55c101SJan Kara 	fh_drop_write(ffhp);
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds out:
17531da177e4SLinus Torvalds 	return err;
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds /*
17571da177e4SLinus Torvalds  * Unlink a file or directory
17581da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
17591da177e4SLinus Torvalds  */
17606264d69dSAl Viro __be32
17611da177e4SLinus Torvalds nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
17621da177e4SLinus Torvalds 				char *fname, int flen)
17631da177e4SLinus Torvalds {
17641da177e4SLinus Torvalds 	struct dentry	*dentry, *rdentry;
17651da177e4SLinus Torvalds 	struct inode	*dirp;
17666264d69dSAl Viro 	__be32		err;
17676264d69dSAl Viro 	int		host_err;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	err = nfserr_acces;
17701da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen))
17711da177e4SLinus Torvalds 		goto out;
17728837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
17731da177e4SLinus Torvalds 	if (err)
17741da177e4SLinus Torvalds 		goto out;
17751da177e4SLinus Torvalds 
17764a55c101SJan Kara 	host_err = fh_want_write(fhp);
17774a55c101SJan Kara 	if (host_err)
17784a55c101SJan Kara 		goto out_nfserr;
17794a55c101SJan Kara 
178012fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
17811da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
17822b0143b5SDavid Howells 	dirp = d_inode(dentry);
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	rdentry = lookup_one_len(fname, dentry, flen);
17856264d69dSAl Viro 	host_err = PTR_ERR(rdentry);
17861da177e4SLinus Torvalds 	if (IS_ERR(rdentry))
17871da177e4SLinus Torvalds 		goto out_nfserr;
17881da177e4SLinus Torvalds 
17892b0143b5SDavid Howells 	if (d_really_is_negative(rdentry)) {
17901da177e4SLinus Torvalds 		dput(rdentry);
17911da177e4SLinus Torvalds 		err = nfserr_noent;
17921da177e4SLinus Torvalds 		goto out;
17931da177e4SLinus Torvalds 	}
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	if (!type)
17962b0143b5SDavid Howells 		type = d_inode(rdentry)->i_mode & S_IFMT;
17971da177e4SLinus Torvalds 
17989ce137eeSJ. Bruce Fields 	if (type != S_IFDIR)
1799b21996e3SJ. Bruce Fields 		host_err = vfs_unlink(dirp, rdentry, NULL);
18009ce137eeSJ. Bruce Fields 	else
18016264d69dSAl Viro 		host_err = vfs_rmdir(dirp, rdentry);
1802541ce98cSJ. Bruce Fields 	if (!host_err)
1803541ce98cSJ. Bruce Fields 		host_err = commit_metadata(fhp);
18041da177e4SLinus Torvalds 	dput(rdentry);
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds out_nfserr:
18076264d69dSAl Viro 	err = nfserrno(host_err);
1808f193fbabSYAMAMOTO Takashi out:
1809f193fbabSYAMAMOTO Takashi 	return err;
18101da177e4SLinus Torvalds }
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds /*
181314f7dd63SDavid Woodhouse  * We do this buffering because we must not call back into the file
181414f7dd63SDavid Woodhouse  * system's ->lookup() method from the filldir callback. That may well
181514f7dd63SDavid Woodhouse  * deadlock a number of file systems.
181614f7dd63SDavid Woodhouse  *
181714f7dd63SDavid Woodhouse  * This is based heavily on the implementation of same in XFS.
181814f7dd63SDavid Woodhouse  */
181914f7dd63SDavid Woodhouse struct buffered_dirent {
182014f7dd63SDavid Woodhouse 	u64		ino;
182114f7dd63SDavid Woodhouse 	loff_t		offset;
182214f7dd63SDavid Woodhouse 	int		namlen;
182314f7dd63SDavid Woodhouse 	unsigned int	d_type;
182414f7dd63SDavid Woodhouse 	char		name[];
182514f7dd63SDavid Woodhouse };
182614f7dd63SDavid Woodhouse 
182714f7dd63SDavid Woodhouse struct readdir_data {
18285c0ba4e0SAl Viro 	struct dir_context ctx;
182914f7dd63SDavid Woodhouse 	char		*dirent;
183014f7dd63SDavid Woodhouse 	size_t		used;
183153c9c5c0SAl Viro 	int		full;
183214f7dd63SDavid Woodhouse };
183314f7dd63SDavid Woodhouse 
1834ac7576f4SMiklos Szeredi static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1835ac7576f4SMiklos Szeredi 				 int namlen, loff_t offset, u64 ino,
1836ac7576f4SMiklos Szeredi 				 unsigned int d_type)
183714f7dd63SDavid Woodhouse {
1838ac7576f4SMiklos Szeredi 	struct readdir_data *buf =
1839ac7576f4SMiklos Szeredi 		container_of(ctx, struct readdir_data, ctx);
184014f7dd63SDavid Woodhouse 	struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
184114f7dd63SDavid Woodhouse 	unsigned int reclen;
184214f7dd63SDavid Woodhouse 
184314f7dd63SDavid Woodhouse 	reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
184453c9c5c0SAl Viro 	if (buf->used + reclen > PAGE_SIZE) {
184553c9c5c0SAl Viro 		buf->full = 1;
184614f7dd63SDavid Woodhouse 		return -EINVAL;
184753c9c5c0SAl Viro 	}
184814f7dd63SDavid Woodhouse 
184914f7dd63SDavid Woodhouse 	de->namlen = namlen;
185014f7dd63SDavid Woodhouse 	de->offset = offset;
185114f7dd63SDavid Woodhouse 	de->ino = ino;
185214f7dd63SDavid Woodhouse 	de->d_type = d_type;
185314f7dd63SDavid Woodhouse 	memcpy(de->name, name, namlen);
185414f7dd63SDavid Woodhouse 	buf->used += reclen;
185514f7dd63SDavid Woodhouse 
185614f7dd63SDavid Woodhouse 	return 0;
185714f7dd63SDavid Woodhouse }
185814f7dd63SDavid Woodhouse 
1859ac7576f4SMiklos Szeredi static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
18602628b766SDavid Woodhouse 				    struct readdir_cd *cdp, loff_t *offsetp)
18612628b766SDavid Woodhouse {
186214f7dd63SDavid Woodhouse 	struct buffered_dirent *de;
18632628b766SDavid Woodhouse 	int host_err;
186414f7dd63SDavid Woodhouse 	int size;
186514f7dd63SDavid Woodhouse 	loff_t offset;
1866ac6614b7SAl Viro 	struct readdir_data buf = {
1867ac6614b7SAl Viro 		.ctx.actor = nfsd_buffered_filldir,
1868ac6614b7SAl Viro 		.dirent = (void *)__get_free_page(GFP_KERNEL)
1869ac6614b7SAl Viro 	};
18702628b766SDavid Woodhouse 
187114f7dd63SDavid Woodhouse 	if (!buf.dirent)
18722f9092e1SDavid Woodhouse 		return nfserrno(-ENOMEM);
187314f7dd63SDavid Woodhouse 
187414f7dd63SDavid Woodhouse 	offset = *offsetp;
18752628b766SDavid Woodhouse 
187614f7dd63SDavid Woodhouse 	while (1) {
187714f7dd63SDavid Woodhouse 		unsigned int reclen;
187814f7dd63SDavid Woodhouse 
1879b726e923SDoug Nazar 		cdp->err = nfserr_eof; /* will be cleared on successful read */
188014f7dd63SDavid Woodhouse 		buf.used = 0;
188153c9c5c0SAl Viro 		buf.full = 0;
188214f7dd63SDavid Woodhouse 
18835c0ba4e0SAl Viro 		host_err = iterate_dir(file, &buf.ctx);
188453c9c5c0SAl Viro 		if (buf.full)
188553c9c5c0SAl Viro 			host_err = 0;
188653c9c5c0SAl Viro 
188753c9c5c0SAl Viro 		if (host_err < 0)
188814f7dd63SDavid Woodhouse 			break;
188914f7dd63SDavid Woodhouse 
189014f7dd63SDavid Woodhouse 		size = buf.used;
189114f7dd63SDavid Woodhouse 
189214f7dd63SDavid Woodhouse 		if (!size)
189314f7dd63SDavid Woodhouse 			break;
189414f7dd63SDavid Woodhouse 
189514f7dd63SDavid Woodhouse 		de = (struct buffered_dirent *)buf.dirent;
189614f7dd63SDavid Woodhouse 		while (size > 0) {
189714f7dd63SDavid Woodhouse 			offset = de->offset;
189814f7dd63SDavid Woodhouse 
189914f7dd63SDavid Woodhouse 			if (func(cdp, de->name, de->namlen, de->offset,
190014f7dd63SDavid Woodhouse 				 de->ino, de->d_type))
19012f9092e1SDavid Woodhouse 				break;
190214f7dd63SDavid Woodhouse 
190314f7dd63SDavid Woodhouse 			if (cdp->err != nfs_ok)
19042f9092e1SDavid Woodhouse 				break;
190514f7dd63SDavid Woodhouse 
190614f7dd63SDavid Woodhouse 			reclen = ALIGN(sizeof(*de) + de->namlen,
190714f7dd63SDavid Woodhouse 				       sizeof(u64));
190814f7dd63SDavid Woodhouse 			size -= reclen;
190914f7dd63SDavid Woodhouse 			de = (struct buffered_dirent *)((char *)de + reclen);
191014f7dd63SDavid Woodhouse 		}
19112f9092e1SDavid Woodhouse 		if (size > 0) /* We bailed out early */
19122f9092e1SDavid Woodhouse 			break;
19132f9092e1SDavid Woodhouse 
1914c002a6c7SDavid Woodhouse 		offset = vfs_llseek(file, 0, SEEK_CUR);
191514f7dd63SDavid Woodhouse 	}
191614f7dd63SDavid Woodhouse 
191714f7dd63SDavid Woodhouse 	free_page((unsigned long)(buf.dirent));
19182628b766SDavid Woodhouse 
19192628b766SDavid Woodhouse 	if (host_err)
19202628b766SDavid Woodhouse 		return nfserrno(host_err);
192114f7dd63SDavid Woodhouse 
192214f7dd63SDavid Woodhouse 	*offsetp = offset;
19232628b766SDavid Woodhouse 	return cdp->err;
19242628b766SDavid Woodhouse }
19252628b766SDavid Woodhouse 
19261da177e4SLinus Torvalds /*
19271da177e4SLinus Torvalds  * Read entries from a directory.
19281da177e4SLinus Torvalds  * The  NFSv3/4 verifier we ignore for now.
19291da177e4SLinus Torvalds  */
19306264d69dSAl Viro __be32
19311da177e4SLinus Torvalds nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1932ac7576f4SMiklos Szeredi 	     struct readdir_cd *cdp, nfsd_filldir_t func)
19331da177e4SLinus Torvalds {
19346264d69dSAl Viro 	__be32		err;
19351da177e4SLinus Torvalds 	struct file	*file;
19361da177e4SLinus Torvalds 	loff_t		offset = *offsetp;
193706effdbbSBernd Schubert 	int             may_flags = NFSD_MAY_READ;
19381da177e4SLinus Torvalds 
193906effdbbSBernd Schubert 	/* NFSv2 only supports 32 bit cookies */
194006effdbbSBernd Schubert 	if (rqstp->rq_vers > 2)
194106effdbbSBernd Schubert 		may_flags |= NFSD_MAY_64BIT_COOKIE;
194206effdbbSBernd Schubert 
194306effdbbSBernd Schubert 	err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
19441da177e4SLinus Torvalds 	if (err)
19451da177e4SLinus Torvalds 		goto out;
19461da177e4SLinus Torvalds 
1947b108fe6bSJeff Layton 	offset = vfs_llseek(file, offset, SEEK_SET);
19481da177e4SLinus Torvalds 	if (offset < 0) {
19491da177e4SLinus Torvalds 		err = nfserrno((int)offset);
19501da177e4SLinus Torvalds 		goto out_close;
19511da177e4SLinus Torvalds 	}
19521da177e4SLinus Torvalds 
195314f7dd63SDavid Woodhouse 	err = nfsd_buffered_readdir(file, func, cdp, offsetp);
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 	if (err == nfserr_eof || err == nfserr_toosmall)
19561da177e4SLinus Torvalds 		err = nfs_ok; /* can still be found in ->err */
19571da177e4SLinus Torvalds out_close:
1958fd891454SChristoph Hellwig 	fput(file);
19591da177e4SLinus Torvalds out:
19601da177e4SLinus Torvalds 	return err;
19611da177e4SLinus Torvalds }
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds /*
19641da177e4SLinus Torvalds  * Get file system stats
19651da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
19661da177e4SLinus Torvalds  */
19676264d69dSAl Viro __be32
196804716e66SJ. Bruce Fields nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
19691da177e4SLinus Torvalds {
1970f6360efbSTakashi Iwai 	__be32 err;
1971f6360efbSTakashi Iwai 
1972f6360efbSTakashi Iwai 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
1973f6360efbSTakashi Iwai 	if (!err) {
1974ebabe9a9SChristoph Hellwig 		struct path path = {
1975ebabe9a9SChristoph Hellwig 			.mnt	= fhp->fh_export->ex_path.mnt,
1976ebabe9a9SChristoph Hellwig 			.dentry	= fhp->fh_dentry,
1977ebabe9a9SChristoph Hellwig 		};
1978f6360efbSTakashi Iwai 		if (vfs_statfs(&path, stat))
19791da177e4SLinus Torvalds 			err = nfserr_io;
1980f6360efbSTakashi Iwai 	}
19811da177e4SLinus Torvalds 	return err;
19821da177e4SLinus Torvalds }
19831da177e4SLinus Torvalds 
1984c7d51402SJ. Bruce Fields static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
1985e22841c6SJ. Bruce Fields {
1986c7d51402SJ. Bruce Fields 	return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
1987e22841c6SJ. Bruce Fields }
1988e22841c6SJ. Bruce Fields 
19891da177e4SLinus Torvalds /*
19901da177e4SLinus Torvalds  * Check for a user's access permissions to this inode.
19911da177e4SLinus Torvalds  */
19926264d69dSAl Viro __be32
19930ec757dfSJ. Bruce Fields nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
19940ec757dfSJ. Bruce Fields 					struct dentry *dentry, int acc)
19951da177e4SLinus Torvalds {
19962b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
19971da177e4SLinus Torvalds 	int		err;
19981da177e4SLinus Torvalds 
1999aea93397SJ. Bruce Fields 	if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
20001da177e4SLinus Torvalds 		return 0;
20011da177e4SLinus Torvalds #if 0
20021da177e4SLinus Torvalds 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
20031da177e4SLinus Torvalds 		acc,
20048837abcaSMiklos Szeredi 		(acc & NFSD_MAY_READ)?	" read"  : "",
20058837abcaSMiklos Szeredi 		(acc & NFSD_MAY_WRITE)?	" write" : "",
20068837abcaSMiklos Szeredi 		(acc & NFSD_MAY_EXEC)?	" exec"  : "",
20078837abcaSMiklos Szeredi 		(acc & NFSD_MAY_SATTR)?	" sattr" : "",
20088837abcaSMiklos Szeredi 		(acc & NFSD_MAY_TRUNC)?	" trunc" : "",
20098837abcaSMiklos Szeredi 		(acc & NFSD_MAY_LOCK)?	" lock"  : "",
20108837abcaSMiklos Szeredi 		(acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
20111da177e4SLinus Torvalds 		inode->i_mode,
20121da177e4SLinus Torvalds 		IS_IMMUTABLE(inode)?	" immut" : "",
20131da177e4SLinus Torvalds 		IS_APPEND(inode)?	" append" : "",
20142c463e95SDave Hansen 		__mnt_is_readonly(exp->ex_path.mnt)?	" ro" : "");
20151da177e4SLinus Torvalds 	dprintk("      owner %d/%d user %d/%d\n",
20165cc0a840SDavid Howells 		inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
20171da177e4SLinus Torvalds #endif
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	/* Normally we reject any write/sattr etc access on a read-only file
20201da177e4SLinus Torvalds 	 * system.  But if it is IRIX doing check on write-access for a
20211da177e4SLinus Torvalds 	 * device special file, we ignore rofs.
20221da177e4SLinus Torvalds 	 */
20238837abcaSMiklos Szeredi 	if (!(acc & NFSD_MAY_LOCAL_ACCESS))
20248837abcaSMiklos Szeredi 		if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
20252c463e95SDave Hansen 			if (exp_rdonly(rqstp, exp) ||
20262c463e95SDave Hansen 			    __mnt_is_readonly(exp->ex_path.mnt))
20271da177e4SLinus Torvalds 				return nfserr_rofs;
20288837abcaSMiklos Szeredi 			if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
20291da177e4SLinus Torvalds 				return nfserr_perm;
20301da177e4SLinus Torvalds 		}
20318837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
20321da177e4SLinus Torvalds 		return nfserr_perm;
20331da177e4SLinus Torvalds 
20348837abcaSMiklos Szeredi 	if (acc & NFSD_MAY_LOCK) {
20351da177e4SLinus Torvalds 		/* If we cannot rely on authentication in NLM requests,
20361da177e4SLinus Torvalds 		 * just allow locks, otherwise require read permission, or
20371da177e4SLinus Torvalds 		 * ownership
20381da177e4SLinus Torvalds 		 */
20391da177e4SLinus Torvalds 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
20401da177e4SLinus Torvalds 			return 0;
20411da177e4SLinus Torvalds 		else
20428837abcaSMiklos Szeredi 			acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
20431da177e4SLinus Torvalds 	}
20441da177e4SLinus Torvalds 	/*
20451da177e4SLinus Torvalds 	 * The file owner always gets access permission for accesses that
20461da177e4SLinus Torvalds 	 * would normally be checked at open time. This is to make
20471da177e4SLinus Torvalds 	 * file access work even when the client has done a fchmod(fd, 0).
20481da177e4SLinus Torvalds 	 *
20491da177e4SLinus Torvalds 	 * However, `cp foo bar' should fail nevertheless when bar is
20501da177e4SLinus Torvalds 	 * readonly. A sensible way to do this might be to reject all
20511da177e4SLinus Torvalds 	 * attempts to truncate a read-only file, because a creat() call
20521da177e4SLinus Torvalds 	 * always implies file truncation.
20531da177e4SLinus Torvalds 	 * ... but this isn't really fair.  A process may reasonably call
20541da177e4SLinus Torvalds 	 * ftruncate on an open file descriptor on a file with perm 000.
20551da177e4SLinus Torvalds 	 * We must trust the client to do permission checking - using "ACCESS"
20561da177e4SLinus Torvalds 	 * with NFSv3.
20571da177e4SLinus Torvalds 	 */
20588837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
20596fab8779SEric W. Biederman 	    uid_eq(inode->i_uid, current_fsuid()))
20601da177e4SLinus Torvalds 		return 0;
20611da177e4SLinus Torvalds 
20628837abcaSMiklos Szeredi 	/* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2063f419a2e3SAl Viro 	err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds 	/* Allow read access to binaries even when mode 111 */
20661da177e4SLinus Torvalds 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
2067a043226bSJ. Bruce Fields 	     (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2068a043226bSJ. Bruce Fields 	      acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2069f419a2e3SAl Viro 		err = inode_permission(inode, MAY_EXEC);
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	return err? nfserrno(err) : 0;
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds void
20751da177e4SLinus Torvalds nfsd_racache_shutdown(void)
20761da177e4SLinus Torvalds {
207754a66e54SJeff Layton 	struct raparms *raparm, *last_raparm;
207854a66e54SJeff Layton 	unsigned int i;
207954a66e54SJeff Layton 
20801da177e4SLinus Torvalds 	dprintk("nfsd: freeing readahead buffers.\n");
208154a66e54SJeff Layton 
208254a66e54SJeff Layton 	for (i = 0; i < RAPARM_HASH_SIZE; i++) {
208354a66e54SJeff Layton 		raparm = raparm_hash[i].pb_head;
208454a66e54SJeff Layton 		while(raparm) {
208554a66e54SJeff Layton 			last_raparm = raparm;
208654a66e54SJeff Layton 			raparm = raparm->p_next;
208754a66e54SJeff Layton 			kfree(last_raparm);
208854a66e54SJeff Layton 		}
208954a66e54SJeff Layton 		raparm_hash[i].pb_head = NULL;
209054a66e54SJeff Layton 	}
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds /*
20931da177e4SLinus Torvalds  * Initialize readahead param cache
20941da177e4SLinus Torvalds  */
20951da177e4SLinus Torvalds int
20961da177e4SLinus Torvalds nfsd_racache_init(int cache_size)
20971da177e4SLinus Torvalds {
20981da177e4SLinus Torvalds 	int	i;
2099fce1456aSGreg Banks 	int	j = 0;
2100fce1456aSGreg Banks 	int	nperbucket;
210154a66e54SJeff Layton 	struct raparms **raparm = NULL;
21021da177e4SLinus Torvalds 
2103fce1456aSGreg Banks 
210454a66e54SJeff Layton 	if (raparm_hash[0].pb_head)
21051da177e4SLinus Torvalds 		return 0;
210654a66e54SJeff Layton 	nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
21073c7aa15dSKinglong Mee 	nperbucket = max(2, nperbucket);
210854a66e54SJeff Layton 	cache_size = nperbucket * RAPARM_HASH_SIZE;
21094b3bb06bSYan Burman 
21104b3bb06bSYan Burman 	dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
211154a66e54SJeff Layton 
2112fce1456aSGreg Banks 	for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2113fce1456aSGreg Banks 		spin_lock_init(&raparm_hash[i].pb_lock);
211454a66e54SJeff Layton 
211554a66e54SJeff Layton 		raparm = &raparm_hash[i].pb_head;
211654a66e54SJeff Layton 		for (j = 0; j < nperbucket; j++) {
211754a66e54SJeff Layton 			*raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
211854a66e54SJeff Layton 			if (!*raparm)
211954a66e54SJeff Layton 				goto out_nomem;
212054a66e54SJeff Layton 			raparm = &(*raparm)->p_next;
2121fce1456aSGreg Banks 		}
212254a66e54SJeff Layton 		*raparm = NULL;
21231da177e4SLinus Torvalds 	}
21244b3bb06bSYan Burman 
21251da177e4SLinus Torvalds 	nfsdstats.ra_size = cache_size;
21261da177e4SLinus Torvalds 	return 0;
212754a66e54SJeff Layton 
212854a66e54SJeff Layton out_nomem:
212954a66e54SJeff Layton 	dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
213054a66e54SJeff Layton 	nfsd_racache_shutdown();
213154a66e54SJeff Layton 	return -ENOMEM;
21321da177e4SLinus Torvalds }
2133