xref: /openbmc/linux/fs/nfsd/vfs.c (revision 745ca247)
11da177e4SLinus Torvalds #define MSNFS	/* HACK HACK */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/nfsd/vfs.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * File operations used by nfsd. Some of these have been ripped from
61da177e4SLinus Torvalds  * other parts of the kernel because they weren't exported, others
71da177e4SLinus Torvalds  * are partial duplicates with added or changed functionality.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Note that several functions dget() the dentry upon which they want
101da177e4SLinus Torvalds  * to act, most notably those that create directory entries. Response
111da177e4SLinus Torvalds  * dentry's are dput()'d if necessary in the release callback.
121da177e4SLinus Torvalds  * So if you notice code paths that apparently fail to dput() the
131da177e4SLinus Torvalds  * dentry, don't worry--they have been taken care of.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
161da177e4SLinus Torvalds  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/fs.h>
231da177e4SLinus Torvalds #include <linux/file.h>
241da177e4SLinus Torvalds #include <linux/mount.h>
251da177e4SLinus Torvalds #include <linux/major.h>
26d6b29d7cSJens Axboe #include <linux/splice.h>
271da177e4SLinus Torvalds #include <linux/proc_fs.h>
281da177e4SLinus Torvalds #include <linux/stat.h>
291da177e4SLinus Torvalds #include <linux/fcntl.h>
301da177e4SLinus Torvalds #include <linux/net.h>
311da177e4SLinus Torvalds #include <linux/unistd.h>
321da177e4SLinus Torvalds #include <linux/slab.h>
331da177e4SLinus Torvalds #include <linux/pagemap.h>
341da177e4SLinus Torvalds #include <linux/in.h>
351da177e4SLinus Torvalds #include <linux/module.h>
361da177e4SLinus Torvalds #include <linux/namei.h>
371da177e4SLinus Torvalds #include <linux/vfs.h>
381da177e4SLinus Torvalds #include <linux/delay.h>
391da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
401da177e4SLinus Torvalds #include <linux/nfsd/nfsd.h>
411da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
421da177e4SLinus Torvalds #include <linux/nfs3.h>
431da177e4SLinus Torvalds #include <linux/nfsd/xdr3.h>
441da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
451da177e4SLinus Torvalds #include <linux/nfsd/nfsfh.h>
461da177e4SLinus Torvalds #include <linux/quotaops.h>
470eeca283SRobert Love #include <linux/fsnotify.h>
481da177e4SLinus Torvalds #include <linux/posix_acl.h>
491da177e4SLinus Torvalds #include <linux/posix_acl_xattr.h>
501da177e4SLinus Torvalds #include <linux/xattr.h>
515be196e5SChristoph Hellwig #ifdef CONFIG_NFSD_V4
521da177e4SLinus Torvalds #include <linux/nfs4.h>
531da177e4SLinus Torvalds #include <linux/nfs4_acl.h>
541da177e4SLinus Torvalds #include <linux/nfsd_idmap.h>
551da177e4SLinus Torvalds #include <linux/security.h>
561da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V4 */
57fce1456aSGreg Banks #include <linux/jhash.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <asm/uaccess.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_FILEOP
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * This is a cache of readahead params that help us choose the proper
661da177e4SLinus Torvalds  * readahead strategy. Initially, we set all readahead parameters to 0
671da177e4SLinus Torvalds  * and let the VFS handle things.
681da177e4SLinus Torvalds  * If you increase the number of cached files very much, you'll need to
691da177e4SLinus Torvalds  * add a hash table here.
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds struct raparms {
721da177e4SLinus Torvalds 	struct raparms		*p_next;
731da177e4SLinus Torvalds 	unsigned int		p_count;
741da177e4SLinus Torvalds 	ino_t			p_ino;
751da177e4SLinus Torvalds 	dev_t			p_dev;
761da177e4SLinus Torvalds 	int			p_set;
771da177e4SLinus Torvalds 	struct file_ra_state	p_ra;
78fce1456aSGreg Banks 	unsigned int		p_hindex;
791da177e4SLinus Torvalds };
801da177e4SLinus Torvalds 
81fce1456aSGreg Banks struct raparm_hbucket {
82fce1456aSGreg Banks 	struct raparms		*pb_head;
83fce1456aSGreg Banks 	spinlock_t		pb_lock;
84fce1456aSGreg Banks } ____cacheline_aligned_in_smp;
85fce1456aSGreg Banks 
86fce1456aSGreg Banks #define RAPARM_HASH_BITS	4
87fce1456aSGreg Banks #define RAPARM_HASH_SIZE	(1<<RAPARM_HASH_BITS)
88fce1456aSGreg Banks #define RAPARM_HASH_MASK	(RAPARM_HASH_SIZE-1)
89fce1456aSGreg Banks static struct raparm_hbucket	raparm_hash[RAPARM_HASH_SIZE];
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /*
921da177e4SLinus Torvalds  * Called from nfsd_lookup and encode_dirent. Check if we have crossed
931da177e4SLinus Torvalds  * a mount point.
94e0bb89efSJ.Bruce Fields  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
951da177e4SLinus Torvalds  *  or nfs_ok having possibly changed *dpp and *expp
961da177e4SLinus Torvalds  */
971da177e4SLinus Torvalds int
981da177e4SLinus Torvalds nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
991da177e4SLinus Torvalds 		        struct svc_export **expp)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	struct svc_export *exp = *expp, *exp2 = NULL;
1021da177e4SLinus Torvalds 	struct dentry *dentry = *dpp;
10354775491SJan Blunck 	struct vfsmount *mnt = mntget(exp->ex_path.mnt);
1041da177e4SLinus Torvalds 	struct dentry *mounts = dget(dentry);
1056264d69dSAl Viro 	int err = 0;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts));
1081da177e4SLinus Torvalds 
1090989a788SJ. Bruce Fields 	exp2 = rqst_exp_get_by_name(rqstp, mnt, mounts);
1101da177e4SLinus Torvalds 	if (IS_ERR(exp2)) {
111a1033be7SNeil Brown 		if (PTR_ERR(exp2) != -ENOENT)
1121da177e4SLinus Torvalds 			err = PTR_ERR(exp2);
1131da177e4SLinus Torvalds 		dput(mounts);
1141da177e4SLinus Torvalds 		mntput(mnt);
1151da177e4SLinus Torvalds 		goto out;
1161da177e4SLinus Torvalds 	}
1175d3dbbeaSJ. Bruce Fields 	if ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
1181da177e4SLinus Torvalds 		/* successfully crossed mount point */
1191da177e4SLinus Torvalds 		exp_put(exp);
1201da177e4SLinus Torvalds 		*expp = exp2;
1211da177e4SLinus Torvalds 		dput(dentry);
1221da177e4SLinus Torvalds 		*dpp = mounts;
1231da177e4SLinus Torvalds 	} else {
1245d3dbbeaSJ. Bruce Fields 		exp_put(exp2);
1251da177e4SLinus Torvalds 		dput(mounts);
1261da177e4SLinus Torvalds 	}
1271da177e4SLinus Torvalds 	mntput(mnt);
1281da177e4SLinus Torvalds out:
1291da177e4SLinus Torvalds 	return err;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1326264d69dSAl Viro __be32
1336c0a654dSJ. Bruce Fields nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
1345a022fc8SChuck Lever 		   const char *name, unsigned int len,
1356c0a654dSJ. Bruce Fields 		   struct svc_export **exp_ret, struct dentry **dentry_ret)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	struct svc_export	*exp;
1381da177e4SLinus Torvalds 	struct dentry		*dparent;
1391da177e4SLinus Torvalds 	struct dentry		*dentry;
1406264d69dSAl Viro 	__be32			err;
1416264d69dSAl Viro 	int			host_err;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	/* Obtain dentry and export. */
1468837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1471da177e4SLinus Torvalds 	if (err)
1481da177e4SLinus Torvalds 		return err;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	dparent = fhp->fh_dentry;
1511da177e4SLinus Torvalds 	exp  = fhp->fh_export;
1521da177e4SLinus Torvalds 	exp_get(exp);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	/* Lookup the name, but don't follow links */
1551da177e4SLinus Torvalds 	if (isdotent(name, len)) {
1561da177e4SLinus Torvalds 		if (len==1)
1571da177e4SLinus Torvalds 			dentry = dget(dparent);
15854775491SJan Blunck 		else if (dparent != exp->ex_path.dentry)
1591da177e4SLinus Torvalds 			dentry = dget_parent(dparent);
16054775491SJan Blunck 		else if (!EX_NOHIDE(exp))
1611da177e4SLinus Torvalds 			dentry = dget(dparent); /* .. == . just like at / */
1621da177e4SLinus Torvalds 		else {
1631da177e4SLinus Torvalds 			/* checking mountpoint crossing is very different when stepping up */
1641da177e4SLinus Torvalds 			struct svc_export *exp2 = NULL;
1651da177e4SLinus Torvalds 			struct dentry *dp;
16654775491SJan Blunck 			struct vfsmount *mnt = mntget(exp->ex_path.mnt);
1671da177e4SLinus Torvalds 			dentry = dget(dparent);
1681da177e4SLinus Torvalds 			while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
1691da177e4SLinus Torvalds 				;
1701da177e4SLinus Torvalds 			dp = dget_parent(dentry);
1711da177e4SLinus Torvalds 			dput(dentry);
1721da177e4SLinus Torvalds 			dentry = dp;
1731da177e4SLinus Torvalds 
1740989a788SJ. Bruce Fields 			exp2 = rqst_exp_parent(rqstp, mnt, dentry);
1752d3bb252SJ. Bruce Fields 			if (PTR_ERR(exp2) == -ENOENT) {
1762d3bb252SJ. Bruce Fields 				dput(dentry);
1772d3bb252SJ. Bruce Fields 				dentry = dget(dparent);
1782d3bb252SJ. Bruce Fields 			} else if (IS_ERR(exp2)) {
1796264d69dSAl Viro 				host_err = PTR_ERR(exp2);
1801da177e4SLinus Torvalds 				dput(dentry);
1811da177e4SLinus Torvalds 				mntput(mnt);
1821da177e4SLinus Torvalds 				goto out_nfserr;
1831da177e4SLinus Torvalds 			} else {
1841da177e4SLinus Torvalds 				exp_put(exp);
1851da177e4SLinus Torvalds 				exp = exp2;
1861da177e4SLinus Torvalds 			}
1871da177e4SLinus Torvalds 			mntput(mnt);
1881da177e4SLinus Torvalds 		}
1891da177e4SLinus Torvalds 	} else {
1901da177e4SLinus Torvalds 		fh_lock(fhp);
1911da177e4SLinus Torvalds 		dentry = lookup_one_len(name, dparent, len);
1926264d69dSAl Viro 		host_err = PTR_ERR(dentry);
1931da177e4SLinus Torvalds 		if (IS_ERR(dentry))
1941da177e4SLinus Torvalds 			goto out_nfserr;
1951da177e4SLinus Torvalds 		/*
1961da177e4SLinus Torvalds 		 * check if we have crossed a mount point ...
1971da177e4SLinus Torvalds 		 */
1981da177e4SLinus Torvalds 		if (d_mountpoint(dentry)) {
1996264d69dSAl Viro 			if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
2001da177e4SLinus Torvalds 				dput(dentry);
2011da177e4SLinus Torvalds 				goto out_nfserr;
2021da177e4SLinus Torvalds 			}
2031da177e4SLinus Torvalds 		}
2041da177e4SLinus Torvalds 	}
2056c0a654dSJ. Bruce Fields 	*dentry_ret = dentry;
2066c0a654dSJ. Bruce Fields 	*exp_ret = exp;
2076c0a654dSJ. Bruce Fields 	return 0;
2086c0a654dSJ. Bruce Fields 
2096c0a654dSJ. Bruce Fields out_nfserr:
2106c0a654dSJ. Bruce Fields 	exp_put(exp);
2116c0a654dSJ. Bruce Fields 	return nfserrno(host_err);
2126c0a654dSJ. Bruce Fields }
2136c0a654dSJ. Bruce Fields 
2146c0a654dSJ. Bruce Fields /*
2156c0a654dSJ. Bruce Fields  * Look up one component of a pathname.
2166c0a654dSJ. Bruce Fields  * N.B. After this call _both_ fhp and resfh need an fh_put
2176c0a654dSJ. Bruce Fields  *
2186c0a654dSJ. Bruce Fields  * If the lookup would cross a mountpoint, and the mounted filesystem
2196c0a654dSJ. Bruce Fields  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
2206c0a654dSJ. Bruce Fields  * accepted as it stands and the mounted directory is
2216c0a654dSJ. Bruce Fields  * returned. Otherwise the covered directory is returned.
2226c0a654dSJ. Bruce Fields  * NOTE: this mountpoint crossing is not supported properly by all
2236c0a654dSJ. Bruce Fields  *   clients and is explicitly disallowed for NFSv3
2246c0a654dSJ. Bruce Fields  *      NeilBrown <neilb@cse.unsw.edu.au>
2256c0a654dSJ. Bruce Fields  */
2266c0a654dSJ. Bruce Fields __be32
2276c0a654dSJ. Bruce Fields nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
2285a022fc8SChuck Lever 				unsigned int len, struct svc_fh *resfh)
2296c0a654dSJ. Bruce Fields {
2306c0a654dSJ. Bruce Fields 	struct svc_export	*exp;
2316c0a654dSJ. Bruce Fields 	struct dentry		*dentry;
2326c0a654dSJ. Bruce Fields 	__be32 err;
2336c0a654dSJ. Bruce Fields 
2346c0a654dSJ. Bruce Fields 	err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
2356c0a654dSJ. Bruce Fields 	if (err)
2366c0a654dSJ. Bruce Fields 		return err;
23732c1eb0cSAndy Adamson 	err = check_nfsd_access(exp, rqstp);
23832c1eb0cSAndy Adamson 	if (err)
23932c1eb0cSAndy Adamson 		goto out;
2401da177e4SLinus Torvalds 	/*
2411da177e4SLinus Torvalds 	 * Note: we compose the file handle now, but as the
2421da177e4SLinus Torvalds 	 * dentry may be negative, it may need to be updated.
2431da177e4SLinus Torvalds 	 */
2441da177e4SLinus Torvalds 	err = fh_compose(resfh, exp, dentry, fhp);
2451da177e4SLinus Torvalds 	if (!err && !dentry->d_inode)
2461da177e4SLinus Torvalds 		err = nfserr_noent;
24732c1eb0cSAndy Adamson out:
2481da177e4SLinus Torvalds 	dput(dentry);
2491da177e4SLinus Torvalds 	exp_put(exp);
2501da177e4SLinus Torvalds 	return err;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2536c0a654dSJ. Bruce Fields 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  * Set various file attributes.
2561da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
2571da177e4SLinus Torvalds  */
2586264d69dSAl Viro __be32
2591da177e4SLinus Torvalds nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
2601da177e4SLinus Torvalds 	     int check_guard, time_t guardtime)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	struct dentry	*dentry;
2631da177e4SLinus Torvalds 	struct inode	*inode;
2648837abcaSMiklos Szeredi 	int		accmode = NFSD_MAY_SATTR;
2651da177e4SLinus Torvalds 	int		ftype = 0;
2666264d69dSAl Viro 	__be32		err;
2676264d69dSAl Viro 	int		host_err;
2681da177e4SLinus Torvalds 	int		size_change = 0;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
2718837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
2721da177e4SLinus Torvalds 	if (iap->ia_valid & ATTR_SIZE)
2731da177e4SLinus Torvalds 		ftype = S_IFREG;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* Get inode */
2761da177e4SLinus Torvalds 	err = fh_verify(rqstp, fhp, ftype, accmode);
27715b7a1b8SNeilBrown 	if (err)
2781da177e4SLinus Torvalds 		goto out;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
2811da177e4SLinus Torvalds 	inode = dentry->d_inode;
2821da177e4SLinus Torvalds 
28315b7a1b8SNeilBrown 	/* Ignore any mode updates on symlinks */
28415b7a1b8SNeilBrown 	if (S_ISLNK(inode->i_mode))
28515b7a1b8SNeilBrown 		iap->ia_valid &= ~ATTR_MODE;
28615b7a1b8SNeilBrown 
28715b7a1b8SNeilBrown 	if (!iap->ia_valid)
28815b7a1b8SNeilBrown 		goto out;
28915b7a1b8SNeilBrown 
2909c85fca5SChristoph Hellwig 	/*
2919c85fca5SChristoph Hellwig 	 * NFSv2 does not differentiate between "set-[ac]time-to-now"
2921da177e4SLinus Torvalds 	 * which only requires access, and "set-[ac]time-to-X" which
2931da177e4SLinus Torvalds 	 * requires ownership.
2941da177e4SLinus Torvalds 	 * So if it looks like it might be "set both to the same time which
2951da177e4SLinus Torvalds 	 * is close to now", and if inode_change_ok fails, then we
2961da177e4SLinus Torvalds 	 * convert to "set to now" instead of "set to explicit time"
2971da177e4SLinus Torvalds 	 *
2981da177e4SLinus Torvalds 	 * We only call inode_change_ok as the last test as technically
2991da177e4SLinus Torvalds 	 * it is not an interface that we should be using.  It is only
3001da177e4SLinus Torvalds 	 * valid if the filesystem does not define it's own i_op->setattr.
3011da177e4SLinus Torvalds 	 */
3021da177e4SLinus Torvalds #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
3031da177e4SLinus Torvalds #define	MAX_TOUCH_TIME_ERROR (30*60)
3049c85fca5SChristoph Hellwig 	if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
3059c85fca5SChristoph Hellwig 	    iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
3069c85fca5SChristoph Hellwig 		/*
3079c85fca5SChristoph Hellwig 		 * Looks probable.
3089c85fca5SChristoph Hellwig 		 *
3099c85fca5SChristoph Hellwig 		 * Now just make sure time is in the right ballpark.
3109c85fca5SChristoph Hellwig 		 * Solaris, at least, doesn't seem to care what the time
3119c85fca5SChristoph Hellwig 		 * request is.  We require it be within 30 minutes of now.
3121da177e4SLinus Torvalds 		 */
3131da177e4SLinus Torvalds 		time_t delta = iap->ia_atime.tv_sec - get_seconds();
3149c85fca5SChristoph Hellwig 		if (delta < 0)
3159c85fca5SChristoph Hellwig 			delta = -delta;
3161da177e4SLinus Torvalds 		if (delta < MAX_TOUCH_TIME_ERROR &&
3171da177e4SLinus Torvalds 		    inode_change_ok(inode, iap) != 0) {
3189c85fca5SChristoph Hellwig 			/*
3199c85fca5SChristoph Hellwig 			 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
3209c85fca5SChristoph Hellwig 			 * This will cause notify_change to set these times
3219c85fca5SChristoph Hellwig 			 * to "now"
3221da177e4SLinus Torvalds 			 */
3231da177e4SLinus Torvalds 			iap->ia_valid &= ~BOTH_TIME_SET;
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
3279c85fca5SChristoph Hellwig 	/*
3289c85fca5SChristoph Hellwig 	 * The size case is special.
3299c85fca5SChristoph Hellwig 	 * It changes the file as well as the attributes.
3309c85fca5SChristoph Hellwig 	 */
3311da177e4SLinus Torvalds 	if (iap->ia_valid & ATTR_SIZE) {
3321da177e4SLinus Torvalds 		if (iap->ia_size < inode->i_size) {
3338837abcaSMiklos Szeredi 			err = nfsd_permission(rqstp, fhp->fh_export, dentry,
3348837abcaSMiklos Szeredi 					NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
3351da177e4SLinus Torvalds 			if (err)
3361da177e4SLinus Torvalds 				goto out;
3371da177e4SLinus Torvalds 		}
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 		/*
3401da177e4SLinus Torvalds 		 * If we are changing the size of the file, then
3411da177e4SLinus Torvalds 		 * we need to break all leases.
3421da177e4SLinus Torvalds 		 */
3436264d69dSAl Viro 		host_err = break_lease(inode, FMODE_WRITE | O_NONBLOCK);
3446264d69dSAl Viro 		if (host_err == -EWOULDBLOCK)
3456264d69dSAl Viro 			host_err = -ETIMEDOUT;
3466264d69dSAl Viro 		if (host_err) /* ENOMEM or EWOULDBLOCK */
3471da177e4SLinus Torvalds 			goto out_nfserr;
3481da177e4SLinus Torvalds 
3496264d69dSAl Viro 		host_err = get_write_access(inode);
3506264d69dSAl Viro 		if (host_err)
3511da177e4SLinus Torvalds 			goto out_nfserr;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 		size_change = 1;
3546264d69dSAl Viro 		host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
3556264d69dSAl Viro 		if (host_err) {
3561da177e4SLinus Torvalds 			put_write_access(inode);
3571da177e4SLinus Torvalds 			goto out_nfserr;
3581da177e4SLinus Torvalds 		}
3591da177e4SLinus Torvalds 		DQUOT_INIT(inode);
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
362ca456252SJeff Layton 	/* sanitize the mode change */
3631da177e4SLinus Torvalds 	if (iap->ia_valid & ATTR_MODE) {
3641da177e4SLinus Torvalds 		iap->ia_mode &= S_IALLUGO;
365dee3209dSJeff Layton 		iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
3661da177e4SLinus Torvalds 	}
367ca456252SJeff Layton 
368ca456252SJeff Layton 	/* Revoke setuid/setgid on chown */
369ca456252SJeff Layton 	if (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
370ca456252SJeff Layton 	    ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)) {
371ca456252SJeff Layton 		iap->ia_valid |= ATTR_KILL_PRIV;
372ca456252SJeff Layton 		if (iap->ia_valid & ATTR_MODE) {
373ca456252SJeff Layton 			/* we're setting mode too, just clear the s*id bits */
374ca456252SJeff Layton 			iap->ia_mode &= ~S_ISUID;
375ca456252SJeff Layton 			if (iap->ia_mode & S_IXGRP)
3768a0ce7d9SJeff Layton 				iap->ia_mode &= ~S_ISGID;
3778a0ce7d9SJeff Layton 		} else {
378ca456252SJeff Layton 			/* set ATTR_KILL_* bits and let VFS handle it */
379ca456252SJeff Layton 			iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
380ca456252SJeff Layton 		}
3818a0ce7d9SJeff Layton 	}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	/* Change the attributes. */
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	iap->ia_valid |= ATTR_CTIME;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	err = nfserr_notsync;
3881da177e4SLinus Torvalds 	if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
3891da177e4SLinus Torvalds 		fh_lock(fhp);
3906264d69dSAl Viro 		host_err = notify_change(dentry, iap);
3916264d69dSAl Viro 		err = nfserrno(host_err);
3921da177e4SLinus Torvalds 		fh_unlock(fhp);
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds 	if (size_change)
3951da177e4SLinus Torvalds 		put_write_access(inode);
3961da177e4SLinus Torvalds 	if (!err)
3971da177e4SLinus Torvalds 		if (EX_ISSYNC(fhp->fh_export))
3981da177e4SLinus Torvalds 			write_inode_now(inode, 1);
3991da177e4SLinus Torvalds out:
4001da177e4SLinus Torvalds 	return err;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds out_nfserr:
4036264d69dSAl Viro 	err = nfserrno(host_err);
4041da177e4SLinus Torvalds 	goto out;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4075be196e5SChristoph Hellwig #if defined(CONFIG_NFSD_V2_ACL) || \
4085be196e5SChristoph Hellwig     defined(CONFIG_NFSD_V3_ACL) || \
4095be196e5SChristoph Hellwig     defined(CONFIG_NFSD_V4)
4105be196e5SChristoph Hellwig static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
4115be196e5SChristoph Hellwig {
4125be196e5SChristoph Hellwig 	ssize_t buflen;
4136c6a426fSKrishna Kumar 	ssize_t ret;
4141da177e4SLinus Torvalds 
4155be196e5SChristoph Hellwig 	buflen = vfs_getxattr(dentry, key, NULL, 0);
4165be196e5SChristoph Hellwig 	if (buflen <= 0)
4175be196e5SChristoph Hellwig 		return buflen;
4185be196e5SChristoph Hellwig 
4195be196e5SChristoph Hellwig 	*buf = kmalloc(buflen, GFP_KERNEL);
4205be196e5SChristoph Hellwig 	if (!*buf)
4215be196e5SChristoph Hellwig 		return -ENOMEM;
4225be196e5SChristoph Hellwig 
4236c6a426fSKrishna Kumar 	ret = vfs_getxattr(dentry, key, *buf, buflen);
4246c6a426fSKrishna Kumar 	if (ret < 0)
4256c6a426fSKrishna Kumar 		kfree(*buf);
4266c6a426fSKrishna Kumar 	return ret;
4275be196e5SChristoph Hellwig }
4285be196e5SChristoph Hellwig #endif
4295be196e5SChristoph Hellwig 
4305be196e5SChristoph Hellwig #if defined(CONFIG_NFSD_V4)
4311da177e4SLinus Torvalds static int
4321da177e4SLinus Torvalds set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	int len;
4351da177e4SLinus Torvalds 	size_t buflen;
4361da177e4SLinus Torvalds 	char *buf = NULL;
4371da177e4SLinus Torvalds 	int error = 0;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	buflen = posix_acl_xattr_size(pacl->a_count);
4401da177e4SLinus Torvalds 	buf = kmalloc(buflen, GFP_KERNEL);
4411da177e4SLinus Torvalds 	error = -ENOMEM;
4421da177e4SLinus Torvalds 	if (buf == NULL)
4431da177e4SLinus Torvalds 		goto out;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	len = posix_acl_to_xattr(pacl, buf, buflen);
4461da177e4SLinus Torvalds 	if (len < 0) {
4471da177e4SLinus Torvalds 		error = len;
4481da177e4SLinus Torvalds 		goto out;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 
4515be196e5SChristoph Hellwig 	error = vfs_setxattr(dentry, key, buf, len, 0);
4521da177e4SLinus Torvalds out:
4531da177e4SLinus Torvalds 	kfree(buf);
4541da177e4SLinus Torvalds 	return error;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4576264d69dSAl Viro __be32
4581da177e4SLinus Torvalds nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
4591da177e4SLinus Torvalds     struct nfs4_acl *acl)
4601da177e4SLinus Torvalds {
4616264d69dSAl Viro 	__be32 error;
4626264d69dSAl Viro 	int host_error;
4631da177e4SLinus Torvalds 	struct dentry *dentry;
4641da177e4SLinus Torvalds 	struct inode *inode;
4651da177e4SLinus Torvalds 	struct posix_acl *pacl = NULL, *dpacl = NULL;
4661da177e4SLinus Torvalds 	unsigned int flags = 0;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	/* Get inode */
4698837abcaSMiklos Szeredi 	error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
4701da177e4SLinus Torvalds 	if (error)
4714b2ca38aSJ. Bruce Fields 		return error;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
4741da177e4SLinus Torvalds 	inode = dentry->d_inode;
4751da177e4SLinus Torvalds 	if (S_ISDIR(inode->i_mode))
4761da177e4SLinus Torvalds 		flags = NFS4_ACL_DIR;
4771da177e4SLinus Torvalds 
4786264d69dSAl Viro 	host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
4796264d69dSAl Viro 	if (host_error == -EINVAL) {
4804b2ca38aSJ. Bruce Fields 		return nfserr_attrnotsupp;
4816264d69dSAl Viro 	} else if (host_error < 0)
4821da177e4SLinus Torvalds 		goto out_nfserr;
4831da177e4SLinus Torvalds 
4846264d69dSAl Viro 	host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
4856264d69dSAl Viro 	if (host_error < 0)
4864b2ca38aSJ. Bruce Fields 		goto out_release;
4871da177e4SLinus Torvalds 
4884b2ca38aSJ. Bruce Fields 	if (S_ISDIR(inode->i_mode))
4896264d69dSAl Viro 		host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
4901da177e4SLinus Torvalds 
4914b2ca38aSJ. Bruce Fields out_release:
4921da177e4SLinus Torvalds 	posix_acl_release(pacl);
4931da177e4SLinus Torvalds 	posix_acl_release(dpacl);
4941da177e4SLinus Torvalds out_nfserr:
495f34f9242SJ. Bruce Fields 	if (host_error == -EOPNOTSUPP)
4964b2ca38aSJ. Bruce Fields 		return nfserr_attrnotsupp;
497f34f9242SJ. Bruce Fields 	else
4984b2ca38aSJ. Bruce Fields 		return nfserrno(host_error);
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds static struct posix_acl *
5021da177e4SLinus Torvalds _get_posix_acl(struct dentry *dentry, char *key)
5031da177e4SLinus Torvalds {
5045be196e5SChristoph Hellwig 	void *buf = NULL;
5051da177e4SLinus Torvalds 	struct posix_acl *pacl = NULL;
5065be196e5SChristoph Hellwig 	int buflen;
5071da177e4SLinus Torvalds 
5085be196e5SChristoph Hellwig 	buflen = nfsd_getxattr(dentry, key, &buf);
5095be196e5SChristoph Hellwig 	if (!buflen)
5105be196e5SChristoph Hellwig 		buflen = -ENODATA;
5115be196e5SChristoph Hellwig 	if (buflen <= 0)
5125be196e5SChristoph Hellwig 		return ERR_PTR(buflen);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	pacl = posix_acl_from_xattr(buf, buflen);
5151da177e4SLinus Torvalds 	kfree(buf);
5161da177e4SLinus Torvalds 	return pacl;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds int
5201da177e4SLinus Torvalds nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5231da177e4SLinus Torvalds 	int error = 0;
5241da177e4SLinus Torvalds 	struct posix_acl *pacl = NULL, *dpacl = NULL;
5251da177e4SLinus Torvalds 	unsigned int flags = 0;
5261da177e4SLinus Torvalds 
5279a59f452SChristoph Hellwig 	pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
5281da177e4SLinus Torvalds 	if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
5291da177e4SLinus Torvalds 		pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
5301da177e4SLinus Torvalds 	if (IS_ERR(pacl)) {
5311da177e4SLinus Torvalds 		error = PTR_ERR(pacl);
5321da177e4SLinus Torvalds 		pacl = NULL;
5331da177e4SLinus Torvalds 		goto out;
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (S_ISDIR(inode->i_mode)) {
5379a59f452SChristoph Hellwig 		dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
5381da177e4SLinus Torvalds 		if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
5391da177e4SLinus Torvalds 			dpacl = NULL;
5401da177e4SLinus Torvalds 		else if (IS_ERR(dpacl)) {
5411da177e4SLinus Torvalds 			error = PTR_ERR(dpacl);
5421da177e4SLinus Torvalds 			dpacl = NULL;
5431da177e4SLinus Torvalds 			goto out;
5441da177e4SLinus Torvalds 		}
5451da177e4SLinus Torvalds 		flags = NFS4_ACL_DIR;
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	*acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
5491da177e4SLinus Torvalds 	if (IS_ERR(*acl)) {
5501da177e4SLinus Torvalds 		error = PTR_ERR(*acl);
5511da177e4SLinus Torvalds 		*acl = NULL;
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds  out:
5541da177e4SLinus Torvalds 	posix_acl_release(pacl);
5551da177e4SLinus Torvalds 	posix_acl_release(dpacl);
5561da177e4SLinus Torvalds 	return error;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds #endif /* defined(CONFIG_NFS_V4) */
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
5621da177e4SLinus Torvalds /*
5631da177e4SLinus Torvalds  * Check server access rights to a file system object
5641da177e4SLinus Torvalds  */
5651da177e4SLinus Torvalds struct accessmap {
5661da177e4SLinus Torvalds 	u32		access;
5671da177e4SLinus Torvalds 	int		how;
5681da177e4SLinus Torvalds };
5691da177e4SLinus Torvalds static struct accessmap	nfs3_regaccess[] = {
5708837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
5718837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
5728837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_TRUNC	},
5738837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE			},
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds     {	0,			0				}
5761da177e4SLinus Torvalds };
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds static struct accessmap	nfs3_diraccess[] = {
5798837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
5808837abcaSMiklos Szeredi     {	NFS3_ACCESS_LOOKUP,	NFSD_MAY_EXEC			},
5818837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
5828837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_EXEC|NFSD_MAY_WRITE	},
5838837abcaSMiklos Szeredi     {	NFS3_ACCESS_DELETE,	NFSD_MAY_REMOVE			},
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds     {	0,			0				}
5861da177e4SLinus Torvalds };
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds static struct accessmap	nfs3_anyaccess[] = {
5891da177e4SLinus Torvalds 	/* Some clients - Solaris 2.6 at least, make an access call
5901da177e4SLinus Torvalds 	 * to the server to check for access for things like /dev/null
5911da177e4SLinus Torvalds 	 * (which really, the server doesn't care about).  So
5921da177e4SLinus Torvalds 	 * We provide simple access checking for them, looking
5931da177e4SLinus Torvalds 	 * mainly at mode bits, and we make sure to ignore read-only
5941da177e4SLinus Torvalds 	 * filesystem checks
5951da177e4SLinus Torvalds 	 */
5968837abcaSMiklos Szeredi     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
5978837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
5988837abcaSMiklos Szeredi     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
5998837abcaSMiklos Szeredi     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds     {	0,			0				}
6021da177e4SLinus Torvalds };
6031da177e4SLinus Torvalds 
6046264d69dSAl Viro __be32
6051da177e4SLinus Torvalds nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	struct accessmap	*map;
6081da177e4SLinus Torvalds 	struct svc_export	*export;
6091da177e4SLinus Torvalds 	struct dentry		*dentry;
6101da177e4SLinus Torvalds 	u32			query, result = 0, sresult = 0;
6116264d69dSAl Viro 	__be32			error;
6121da177e4SLinus Torvalds 
6138837abcaSMiklos Szeredi 	error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
6141da177e4SLinus Torvalds 	if (error)
6151da177e4SLinus Torvalds 		goto out;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	export = fhp->fh_export;
6181da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	if (S_ISREG(dentry->d_inode->i_mode))
6211da177e4SLinus Torvalds 		map = nfs3_regaccess;
6221da177e4SLinus Torvalds 	else if (S_ISDIR(dentry->d_inode->i_mode))
6231da177e4SLinus Torvalds 		map = nfs3_diraccess;
6241da177e4SLinus Torvalds 	else
6251da177e4SLinus Torvalds 		map = nfs3_anyaccess;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	query = *access;
6291da177e4SLinus Torvalds 	for  (; map->access; map++) {
6301da177e4SLinus Torvalds 		if (map->access & query) {
6316264d69dSAl Viro 			__be32 err2;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 			sresult |= map->access;
6341da177e4SLinus Torvalds 
6350ec757dfSJ. Bruce Fields 			err2 = nfsd_permission(rqstp, export, dentry, map->how);
6361da177e4SLinus Torvalds 			switch (err2) {
6371da177e4SLinus Torvalds 			case nfs_ok:
6381da177e4SLinus Torvalds 				result |= map->access;
6391da177e4SLinus Torvalds 				break;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 			/* the following error codes just mean the access was not allowed,
6421da177e4SLinus Torvalds 			 * rather than an error occurred */
6431da177e4SLinus Torvalds 			case nfserr_rofs:
6441da177e4SLinus Torvalds 			case nfserr_acces:
6451da177e4SLinus Torvalds 			case nfserr_perm:
6461da177e4SLinus Torvalds 				/* simply don't "or" in the access bit. */
6471da177e4SLinus Torvalds 				break;
6481da177e4SLinus Torvalds 			default:
6491da177e4SLinus Torvalds 				error = err2;
6501da177e4SLinus Torvalds 				goto out;
6511da177e4SLinus Torvalds 			}
6521da177e4SLinus Torvalds 		}
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 	*access = result;
6551da177e4SLinus Torvalds 	if (supported)
6561da177e4SLinus Torvalds 		*supported = sresult;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds  out:
6591da177e4SLinus Torvalds 	return error;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds /*
6661da177e4SLinus Torvalds  * Open an existing file or directory.
6671da177e4SLinus Torvalds  * The access argument indicates the type of open (read/write/lock)
6681da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
6691da177e4SLinus Torvalds  */
6706264d69dSAl Viro __be32
6711da177e4SLinus Torvalds nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
6721da177e4SLinus Torvalds 			int access, struct file **filp)
6731da177e4SLinus Torvalds {
674*745ca247SDavid Howells 	const struct cred *cred = current_cred();
6751da177e4SLinus Torvalds 	struct dentry	*dentry;
6761da177e4SLinus Torvalds 	struct inode	*inode;
6776264d69dSAl Viro 	int		flags = O_RDONLY|O_LARGEFILE;
6786264d69dSAl Viro 	__be32		err;
6796264d69dSAl Viro 	int		host_err;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	/*
6821da177e4SLinus Torvalds 	 * If we get here, then the client has already done an "open",
6831da177e4SLinus Torvalds 	 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
6841da177e4SLinus Torvalds 	 * in case a chmod has now revoked permission.
6851da177e4SLinus Torvalds 	 */
6868837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, type, access | NFSD_MAY_OWNER_OVERRIDE);
6871da177e4SLinus Torvalds 	if (err)
6881da177e4SLinus Torvalds 		goto out;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
6911da177e4SLinus Torvalds 	inode = dentry->d_inode;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	/* Disallow write access to files with the append-only bit set
6941da177e4SLinus Torvalds 	 * or any access when mandatory locking enabled
6951da177e4SLinus Torvalds 	 */
6961da177e4SLinus Torvalds 	err = nfserr_perm;
6978837abcaSMiklos Szeredi 	if (IS_APPEND(inode) && (access & NFSD_MAY_WRITE))
6981da177e4SLinus Torvalds 		goto out;
6995e7fc436SJ. Bruce Fields 	/*
7005e7fc436SJ. Bruce Fields 	 * We must ignore files (but only files) which might have mandatory
7015e7fc436SJ. Bruce Fields 	 * locks on them because there is no way to know if the accesser has
7025e7fc436SJ. Bruce Fields 	 * the lock.
7035e7fc436SJ. Bruce Fields 	 */
7045e7fc436SJ. Bruce Fields 	if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
7051da177e4SLinus Torvalds 		goto out;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	if (!inode->i_fop)
7081da177e4SLinus Torvalds 		goto out;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	/*
7111da177e4SLinus Torvalds 	 * Check to see if there are any leases on this file.
7121da177e4SLinus Torvalds 	 * This may block while leases are broken.
7131da177e4SLinus Torvalds 	 */
7148837abcaSMiklos Szeredi 	host_err = break_lease(inode, O_NONBLOCK | ((access & NFSD_MAY_WRITE) ? FMODE_WRITE : 0));
7156264d69dSAl Viro 	if (host_err == -EWOULDBLOCK)
7166264d69dSAl Viro 		host_err = -ETIMEDOUT;
7176264d69dSAl Viro 	if (host_err) /* NOMEM or WOULDBLOCK */
7181da177e4SLinus Torvalds 		goto out_nfserr;
7191da177e4SLinus Torvalds 
7208837abcaSMiklos Szeredi 	if (access & NFSD_MAY_WRITE) {
7218837abcaSMiklos Szeredi 		if (access & NFSD_MAY_READ)
7229ecb6a08SJ. Bruce Fields 			flags = O_RDWR|O_LARGEFILE;
7239ecb6a08SJ. Bruce Fields 		else
7241da177e4SLinus Torvalds 			flags = O_WRONLY|O_LARGEFILE;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		DQUOT_INIT(inode);
7271da177e4SLinus Torvalds 	}
72854775491SJan Blunck 	*filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
729*745ca247SDavid Howells 			    flags, cred);
7301da177e4SLinus Torvalds 	if (IS_ERR(*filp))
7316264d69dSAl Viro 		host_err = PTR_ERR(*filp);
7321da177e4SLinus Torvalds out_nfserr:
7336264d69dSAl Viro 	err = nfserrno(host_err);
7341da177e4SLinus Torvalds out:
7351da177e4SLinus Torvalds 	return err;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds /*
7391da177e4SLinus Torvalds  * Close a file.
7401da177e4SLinus Torvalds  */
7411da177e4SLinus Torvalds void
7421da177e4SLinus Torvalds nfsd_close(struct file *filp)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	fput(filp);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds /*
7481da177e4SLinus Torvalds  * Sync a file
7491da177e4SLinus Torvalds  * As this calls fsync (not fdatasync) there is no need for a write_inode
7501da177e4SLinus Torvalds  * after it.
7511da177e4SLinus Torvalds  */
752a334de28SDavid Shaw static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
75399ac48f5SArjan van de Ven 			      const struct file_operations *fop)
7541da177e4SLinus Torvalds {
7551da177e4SLinus Torvalds 	struct inode *inode = dp->d_inode;
7561da177e4SLinus Torvalds 	int (*fsync) (struct file *, struct dentry *, int);
757f193fbabSYAMAMOTO Takashi 	int err;
7581da177e4SLinus Torvalds 
759f193fbabSYAMAMOTO Takashi 	err = filemap_fdatawrite(inode->i_mapping);
760f193fbabSYAMAMOTO Takashi 	if (err == 0 && fop && (fsync = fop->fsync))
761a334de28SDavid Shaw 		err = fsync(filp, dp, 0);
762f193fbabSYAMAMOTO Takashi 	if (err == 0)
763f193fbabSYAMAMOTO Takashi 		err = filemap_fdatawait(inode->i_mapping);
764a334de28SDavid Shaw 
765f193fbabSYAMAMOTO Takashi 	return err;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 
769a334de28SDavid Shaw static int
7701da177e4SLinus Torvalds nfsd_sync(struct file *filp)
7711da177e4SLinus Torvalds {
772a334de28SDavid Shaw         int err;
7737eaa36e2SJosef "Jeff" Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
7747eaa36e2SJosef "Jeff" Sipek 	dprintk("nfsd: sync file %s\n", filp->f_path.dentry->d_name.name);
7751b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
7767eaa36e2SJosef "Jeff" Sipek 	err=nfsd_dosync(filp, filp->f_path.dentry, filp->f_op);
7771b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
778a334de28SDavid Shaw 
779a334de28SDavid Shaw 	return err;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
782f193fbabSYAMAMOTO Takashi int
7831da177e4SLinus Torvalds nfsd_sync_dir(struct dentry *dp)
7841da177e4SLinus Torvalds {
785f193fbabSYAMAMOTO Takashi 	return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds /*
7891da177e4SLinus Torvalds  * Obtain the readahead parameters for the file
7901da177e4SLinus Torvalds  * specified by (dev, ino).
7911da177e4SLinus Torvalds  */
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds static inline struct raparms *
7941da177e4SLinus Torvalds nfsd_get_raparms(dev_t dev, ino_t ino)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	struct raparms	*ra, **rap, **frap = NULL;
7971da177e4SLinus Torvalds 	int depth = 0;
798fce1456aSGreg Banks 	unsigned int hash;
799fce1456aSGreg Banks 	struct raparm_hbucket *rab;
8001da177e4SLinus Torvalds 
801fce1456aSGreg Banks 	hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
802fce1456aSGreg Banks 	rab = &raparm_hash[hash];
803fce1456aSGreg Banks 
804fce1456aSGreg Banks 	spin_lock(&rab->pb_lock);
805fce1456aSGreg Banks 	for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
8061da177e4SLinus Torvalds 		if (ra->p_ino == ino && ra->p_dev == dev)
8071da177e4SLinus Torvalds 			goto found;
8081da177e4SLinus Torvalds 		depth++;
8091da177e4SLinus Torvalds 		if (ra->p_count == 0)
8101da177e4SLinus Torvalds 			frap = rap;
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 	depth = nfsdstats.ra_size*11/10;
8131da177e4SLinus Torvalds 	if (!frap) {
814fce1456aSGreg Banks 		spin_unlock(&rab->pb_lock);
8151da177e4SLinus Torvalds 		return NULL;
8161da177e4SLinus Torvalds 	}
8171da177e4SLinus Torvalds 	rap = frap;
8181da177e4SLinus Torvalds 	ra = *frap;
8191da177e4SLinus Torvalds 	ra->p_dev = dev;
8201da177e4SLinus Torvalds 	ra->p_ino = ino;
8211da177e4SLinus Torvalds 	ra->p_set = 0;
822fce1456aSGreg Banks 	ra->p_hindex = hash;
8231da177e4SLinus Torvalds found:
824fce1456aSGreg Banks 	if (rap != &rab->pb_head) {
8251da177e4SLinus Torvalds 		*rap = ra->p_next;
826fce1456aSGreg Banks 		ra->p_next   = rab->pb_head;
827fce1456aSGreg Banks 		rab->pb_head = ra;
8281da177e4SLinus Torvalds 	}
8291da177e4SLinus Torvalds 	ra->p_count++;
8301da177e4SLinus Torvalds 	nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
831fce1456aSGreg Banks 	spin_unlock(&rab->pb_lock);
8321da177e4SLinus Torvalds 	return ra;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds /*
836cf8208d0SJens Axboe  * Grab and keep cached pages associated with a file in the svc_rqst
837cf8208d0SJens Axboe  * so that they can be passed to the network sendmsg/sendpage routines
838cf8208d0SJens Axboe  * directly. They will be released after the sending has completed.
8391da177e4SLinus Torvalds  */
8401da177e4SLinus Torvalds static int
841cf8208d0SJens Axboe nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
842cf8208d0SJens Axboe 		  struct splice_desc *sd)
8431da177e4SLinus Torvalds {
844cf8208d0SJens Axboe 	struct svc_rqst *rqstp = sd->u.data;
84544524359SNeilBrown 	struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
846cf8208d0SJens Axboe 	struct page *page = buf->page;
847cf8208d0SJens Axboe 	size_t size;
848cf8208d0SJens Axboe 	int ret;
8491da177e4SLinus Torvalds 
850cac36bb0SJens Axboe 	ret = buf->ops->confirm(pipe, buf);
851cf8208d0SJens Axboe 	if (unlikely(ret))
852cf8208d0SJens Axboe 		return ret;
853cf8208d0SJens Axboe 
854cf8208d0SJens Axboe 	size = sd->len;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	if (rqstp->rq_res.page_len == 0) {
8571da177e4SLinus Torvalds 		get_page(page);
85844524359SNeilBrown 		put_page(*pp);
85944524359SNeilBrown 		*pp = page;
86044524359SNeilBrown 		rqstp->rq_resused++;
861cf8208d0SJens Axboe 		rqstp->rq_res.page_base = buf->offset;
8621da177e4SLinus Torvalds 		rqstp->rq_res.page_len = size;
86344524359SNeilBrown 	} else if (page != pp[-1]) {
8641da177e4SLinus Torvalds 		get_page(page);
865250f3915SNeilBrown 		if (*pp)
86644524359SNeilBrown 			put_page(*pp);
86744524359SNeilBrown 		*pp = page;
86844524359SNeilBrown 		rqstp->rq_resused++;
8691da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
87044524359SNeilBrown 	} else
8711da177e4SLinus Torvalds 		rqstp->rq_res.page_len += size;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	return size;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
876cf8208d0SJens Axboe static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
877cf8208d0SJens Axboe 				    struct splice_desc *sd)
878cf8208d0SJens Axboe {
879cf8208d0SJens Axboe 	return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
880cf8208d0SJens Axboe }
881cf8208d0SJens Axboe 
882a8754beeSDave Hansen static inline int svc_msnfs(struct svc_fh *ffhp)
883a8754beeSDave Hansen {
884a8754beeSDave Hansen #ifdef MSNFS
885a8754beeSDave Hansen 	return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
886a8754beeSDave Hansen #else
887a8754beeSDave Hansen 	return 0;
888a8754beeSDave Hansen #endif
889a8754beeSDave Hansen }
890a8754beeSDave Hansen 
8916264d69dSAl Viro static __be32
8921da177e4SLinus Torvalds nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
8931da177e4SLinus Torvalds               loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
8941da177e4SLinus Torvalds {
8951da177e4SLinus Torvalds 	struct inode *inode;
8961da177e4SLinus Torvalds 	struct raparms	*ra;
8971da177e4SLinus Torvalds 	mm_segment_t	oldfs;
8986264d69dSAl Viro 	__be32		err;
8996264d69dSAl Viro 	int		host_err;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	err = nfserr_perm;
9027eaa36e2SJosef "Jeff" Sipek 	inode = file->f_path.dentry->d_inode;
903a8754beeSDave Hansen 
904a8754beeSDave Hansen 	if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
9051da177e4SLinus Torvalds 		goto out;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* Get readahead parameters */
9081da177e4SLinus Torvalds 	ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	if (ra && ra->p_set)
9111da177e4SLinus Torvalds 		file->f_ra = ra->p_ra;
9121da177e4SLinus Torvalds 
913cf8208d0SJens Axboe 	if (file->f_op->splice_read && rqstp->rq_splice_ok) {
914cf8208d0SJens Axboe 		struct splice_desc sd = {
915cf8208d0SJens Axboe 			.len		= 0,
916cf8208d0SJens Axboe 			.total_len	= *count,
917cf8208d0SJens Axboe 			.pos		= offset,
918cf8208d0SJens Axboe 			.u.data		= rqstp,
919cf8208d0SJens Axboe 		};
920cf8208d0SJens Axboe 
9214fbef206SJens Axboe 		rqstp->rq_resused = 1;
922cf8208d0SJens Axboe 		host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
9231da177e4SLinus Torvalds 	} else {
9241da177e4SLinus Torvalds 		oldfs = get_fs();
9251da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
9266264d69dSAl Viro 		host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
9271da177e4SLinus Torvalds 		set_fs(oldfs);
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	/* Write back readahead params */
9311da177e4SLinus Torvalds 	if (ra) {
932fce1456aSGreg Banks 		struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
933fce1456aSGreg Banks 		spin_lock(&rab->pb_lock);
9341da177e4SLinus Torvalds 		ra->p_ra = file->f_ra;
9351da177e4SLinus Torvalds 		ra->p_set = 1;
9361da177e4SLinus Torvalds 		ra->p_count--;
937fce1456aSGreg Banks 		spin_unlock(&rab->pb_lock);
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9406264d69dSAl Viro 	if (host_err >= 0) {
9416264d69dSAl Viro 		nfsdstats.io_read += host_err;
9426264d69dSAl Viro 		*count = host_err;
9431da177e4SLinus Torvalds 		err = 0;
9447eaa36e2SJosef "Jeff" Sipek 		fsnotify_access(file->f_path.dentry);
9451da177e4SLinus Torvalds 	} else
9466264d69dSAl Viro 		err = nfserrno(host_err);
9471da177e4SLinus Torvalds out:
9481da177e4SLinus Torvalds 	return err;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9519f708e40SNeil Brown static void kill_suid(struct dentry *dentry)
9529f708e40SNeil Brown {
9539f708e40SNeil Brown 	struct iattr	ia;
954b5376771SSerge E. Hallyn 	ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
9559f708e40SNeil Brown 
9561b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
9579f708e40SNeil Brown 	notify_change(dentry, &ia);
9581b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
9599f708e40SNeil Brown }
9609f708e40SNeil Brown 
9616264d69dSAl Viro static __be32
9621da177e4SLinus Torvalds nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
9631da177e4SLinus Torvalds 				loff_t offset, struct kvec *vec, int vlen,
9641da177e4SLinus Torvalds 	   			unsigned long cnt, int *stablep)
9651da177e4SLinus Torvalds {
9661da177e4SLinus Torvalds 	struct svc_export	*exp;
9671da177e4SLinus Torvalds 	struct dentry		*dentry;
9681da177e4SLinus Torvalds 	struct inode		*inode;
9691da177e4SLinus Torvalds 	mm_segment_t		oldfs;
9706264d69dSAl Viro 	__be32			err = 0;
9716264d69dSAl Viro 	int			host_err;
9721da177e4SLinus Torvalds 	int			stable = *stablep;
9731da177e4SLinus Torvalds 
97445bd3b3dSNeilBrown #ifdef MSNFS
9751da177e4SLinus Torvalds 	err = nfserr_perm;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
9787eaa36e2SJosef "Jeff" Sipek 		(!lock_may_write(file->f_path.dentry->d_inode, offset, cnt)))
9791da177e4SLinus Torvalds 		goto out;
9801da177e4SLinus Torvalds #endif
9811da177e4SLinus Torvalds 
9827eaa36e2SJosef "Jeff" Sipek 	dentry = file->f_path.dentry;
9831da177e4SLinus Torvalds 	inode = dentry->d_inode;
9841da177e4SLinus Torvalds 	exp   = fhp->fh_export;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	/*
9871da177e4SLinus Torvalds 	 * Request sync writes if
9881da177e4SLinus Torvalds 	 *  -	the sync export option has been set, or
9891da177e4SLinus Torvalds 	 *  -	the client requested O_SYNC behavior (NFSv3 feature).
9901da177e4SLinus Torvalds 	 *  -   The file system doesn't support fsync().
9911da177e4SLinus Torvalds 	 * When gathered writes have been configured for this volume,
9921da177e4SLinus Torvalds 	 * flushing the data to disk is handled separately below.
9931da177e4SLinus Torvalds 	 */
9941da177e4SLinus Torvalds 
9953ba15148SHarvey Harrison 	if (!file->f_op->fsync) {/* COMMIT3 cannot work */
9961da177e4SLinus Torvalds 	       stable = 2;
9971da177e4SLinus Torvalds 	       *stablep = 2; /* FILE_SYNC */
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	if (!EX_ISSYNC(exp))
10011da177e4SLinus Torvalds 		stable = 0;
10021da177e4SLinus Torvalds 	if (stable && !EX_WGATHER(exp))
10031da177e4SLinus Torvalds 		file->f_flags |= O_SYNC;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	/* Write the data. */
10061da177e4SLinus Torvalds 	oldfs = get_fs(); set_fs(KERNEL_DS);
10076264d69dSAl Viro 	host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
10081da177e4SLinus Torvalds 	set_fs(oldfs);
10096264d69dSAl Viro 	if (host_err >= 0) {
10101da177e4SLinus Torvalds 		nfsdstats.io_write += cnt;
10117eaa36e2SJosef "Jeff" Sipek 		fsnotify_modify(file->f_path.dentry);
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	/* clear setuid/setgid flag after write */
10156264d69dSAl Viro 	if (host_err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
10169f708e40SNeil Brown 		kill_suid(dentry);
10171da177e4SLinus Torvalds 
10186264d69dSAl Viro 	if (host_err >= 0 && stable) {
10191da177e4SLinus Torvalds 		static ino_t	last_ino;
10201da177e4SLinus Torvalds 		static dev_t	last_dev;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		/*
10231da177e4SLinus Torvalds 		 * Gathered writes: If another process is currently
10241da177e4SLinus Torvalds 		 * writing to the file, there's a high chance
10251da177e4SLinus Torvalds 		 * this is another nfsd (triggered by a bulk write
10261da177e4SLinus Torvalds 		 * from a client's biod). Rather than syncing the
10271da177e4SLinus Torvalds 		 * file with each write request, we sleep for 10 msec.
10281da177e4SLinus Torvalds 		 *
10291da177e4SLinus Torvalds 		 * I don't know if this roughly approximates
10301da177e4SLinus Torvalds 		 * C. Juszak's idea of gathered writes, but it's a
10311da177e4SLinus Torvalds 		 * nice and simple solution (IMHO), and it seems to
10321da177e4SLinus Torvalds 		 * work:-)
10331da177e4SLinus Torvalds 		 */
10341da177e4SLinus Torvalds 		if (EX_WGATHER(exp)) {
10351da177e4SLinus Torvalds 			if (atomic_read(&inode->i_writecount) > 1
10361da177e4SLinus Torvalds 			    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1037ba25f9dcSPavel Emelyanov 				dprintk("nfsd: write defer %d\n", task_pid_nr(current));
10381da177e4SLinus Torvalds 				msleep(10);
1039ba25f9dcSPavel Emelyanov 				dprintk("nfsd: write resume %d\n", task_pid_nr(current));
10401da177e4SLinus Torvalds 			}
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 			if (inode->i_state & I_DIRTY) {
1043ba25f9dcSPavel Emelyanov 				dprintk("nfsd: write sync %d\n", task_pid_nr(current));
10446264d69dSAl Viro 				host_err=nfsd_sync(file);
10451da177e4SLinus Torvalds 			}
10461da177e4SLinus Torvalds #if 0
10471da177e4SLinus Torvalds 			wake_up(&inode->i_wait);
10481da177e4SLinus Torvalds #endif
10491da177e4SLinus Torvalds 		}
10501da177e4SLinus Torvalds 		last_ino = inode->i_ino;
10511da177e4SLinus Torvalds 		last_dev = inode->i_sb->s_dev;
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds 
10546264d69dSAl Viro 	dprintk("nfsd: write complete host_err=%d\n", host_err);
10556264d69dSAl Viro 	if (host_err >= 0)
10561da177e4SLinus Torvalds 		err = 0;
10571da177e4SLinus Torvalds 	else
10586264d69dSAl Viro 		err = nfserrno(host_err);
10591da177e4SLinus Torvalds out:
10601da177e4SLinus Torvalds 	return err;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds /*
10641da177e4SLinus Torvalds  * Read data from a file. count must contain the requested read count
10651da177e4SLinus Torvalds  * on entry. On return, *count contains the number of bytes actually read.
10661da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
10671da177e4SLinus Torvalds  */
10686264d69dSAl Viro __be32
10691da177e4SLinus Torvalds nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
10701da177e4SLinus Torvalds 		loff_t offset, struct kvec *vec, int vlen,
10711da177e4SLinus Torvalds 		unsigned long *count)
10721da177e4SLinus Torvalds {
10736264d69dSAl Viro 	__be32		err;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	if (file) {
10760ec757dfSJ. Bruce Fields 		err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
10778837abcaSMiklos Szeredi 				NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
10781da177e4SLinus Torvalds 		if (err)
10791da177e4SLinus Torvalds 			goto out;
10801da177e4SLinus Torvalds 		err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
10811da177e4SLinus Torvalds 	} else {
10828837abcaSMiklos Szeredi 		err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
10831da177e4SLinus Torvalds 		if (err)
10841da177e4SLinus Torvalds 			goto out;
10851da177e4SLinus Torvalds 		err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
10861da177e4SLinus Torvalds 		nfsd_close(file);
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds out:
10891da177e4SLinus Torvalds 	return err;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds /*
10931da177e4SLinus Torvalds  * Write data to a file.
10941da177e4SLinus Torvalds  * The stable flag requests synchronous writes.
10951da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
10961da177e4SLinus Torvalds  */
10976264d69dSAl Viro __be32
10981da177e4SLinus Torvalds nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
10991da177e4SLinus Torvalds 		loff_t offset, struct kvec *vec, int vlen, unsigned long cnt,
11001da177e4SLinus Torvalds 		int *stablep)
11011da177e4SLinus Torvalds {
11026264d69dSAl Viro 	__be32			err = 0;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	if (file) {
11050ec757dfSJ. Bruce Fields 		err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
11068837abcaSMiklos Szeredi 				NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
11071da177e4SLinus Torvalds 		if (err)
11081da177e4SLinus Torvalds 			goto out;
11091da177e4SLinus Torvalds 		err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
11101da177e4SLinus Torvalds 				stablep);
11111da177e4SLinus Torvalds 	} else {
11128837abcaSMiklos Szeredi 		err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
11131da177e4SLinus Torvalds 		if (err)
11141da177e4SLinus Torvalds 			goto out;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 		if (cnt)
11171da177e4SLinus Torvalds 			err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
11181da177e4SLinus Torvalds 					     cnt, stablep);
11191da177e4SLinus Torvalds 		nfsd_close(file);
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds out:
11221da177e4SLinus Torvalds 	return err;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
11261da177e4SLinus Torvalds /*
11271da177e4SLinus Torvalds  * Commit all pending writes to stable storage.
11281da177e4SLinus Torvalds  * Strictly speaking, we could sync just the indicated file region here,
11291da177e4SLinus Torvalds  * but there's currently no way we can ask the VFS to do so.
11301da177e4SLinus Torvalds  *
11311da177e4SLinus Torvalds  * Unfortunately we cannot lock the file to make sure we return full WCC
11321da177e4SLinus Torvalds  * data to the client, as locking happens lower down in the filesystem.
11331da177e4SLinus Torvalds  */
11346264d69dSAl Viro __be32
11351da177e4SLinus Torvalds nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
11361da177e4SLinus Torvalds                loff_t offset, unsigned long count)
11371da177e4SLinus Torvalds {
11381da177e4SLinus Torvalds 	struct file	*file;
11396264d69dSAl Viro 	__be32		err;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	if ((u64)count > ~(u64)offset)
11421da177e4SLinus Torvalds 		return nfserr_inval;
11431da177e4SLinus Torvalds 
11448837abcaSMiklos Szeredi 	err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
11458837abcaSMiklos Szeredi 	if (err)
11461da177e4SLinus Torvalds 		return err;
11471da177e4SLinus Torvalds 	if (EX_ISSYNC(fhp->fh_export)) {
11481da177e4SLinus Torvalds 		if (file->f_op && file->f_op->fsync) {
114945bd3b3dSNeilBrown 			err = nfserrno(nfsd_sync(file));
11501da177e4SLinus Torvalds 		} else {
11511da177e4SLinus Torvalds 			err = nfserr_notsupp;
11521da177e4SLinus Torvalds 		}
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	nfsd_close(file);
11561da177e4SLinus Torvalds 	return err;
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
11591da177e4SLinus Torvalds 
1160f2b0dee2SAdrian Bunk static __be32
11615c002b3bSJ. Bruce Fields nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
11625c002b3bSJ. Bruce Fields 			struct iattr *iap)
11635c002b3bSJ. Bruce Fields {
11645c002b3bSJ. Bruce Fields 	/*
11655c002b3bSJ. Bruce Fields 	 * Mode has already been set earlier in create:
11665c002b3bSJ. Bruce Fields 	 */
11675c002b3bSJ. Bruce Fields 	iap->ia_valid &= ~ATTR_MODE;
11685c002b3bSJ. Bruce Fields 	/*
11695c002b3bSJ. Bruce Fields 	 * Setting uid/gid works only for root.  Irix appears to
11705c002b3bSJ. Bruce Fields 	 * send along the gid on create when it tries to implement
11715c002b3bSJ. Bruce Fields 	 * setgid directories via NFS:
11725c002b3bSJ. Bruce Fields 	 */
11735cc0a840SDavid Howells 	if (current_fsuid() != 0)
11745c002b3bSJ. Bruce Fields 		iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
11755c002b3bSJ. Bruce Fields 	if (iap->ia_valid)
11765c002b3bSJ. Bruce Fields 		return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
11775c002b3bSJ. Bruce Fields 	return 0;
11785c002b3bSJ. Bruce Fields }
11795c002b3bSJ. Bruce Fields 
11801da177e4SLinus Torvalds /*
11811da177e4SLinus Torvalds  * Create a file (regular, directory, device, fifo); UNIX sockets
11821da177e4SLinus Torvalds  * not yet implemented.
11831da177e4SLinus Torvalds  * If the response fh has been verified, the parent directory should
11841da177e4SLinus Torvalds  * already be locked. Note that the parent directory is left locked.
11851da177e4SLinus Torvalds  *
11861da177e4SLinus Torvalds  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
11871da177e4SLinus Torvalds  */
11886264d69dSAl Viro __be32
11891da177e4SLinus Torvalds nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
11901da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
11911da177e4SLinus Torvalds 		int type, dev_t rdev, struct svc_fh *resfhp)
11921da177e4SLinus Torvalds {
11931da177e4SLinus Torvalds 	struct dentry	*dentry, *dchild = NULL;
11941da177e4SLinus Torvalds 	struct inode	*dirp;
11956264d69dSAl Viro 	__be32		err;
11965c002b3bSJ. Bruce Fields 	__be32		err2;
11976264d69dSAl Viro 	int		host_err;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	err = nfserr_perm;
12001da177e4SLinus Torvalds 	if (!flen)
12011da177e4SLinus Torvalds 		goto out;
12021da177e4SLinus Torvalds 	err = nfserr_exist;
12031da177e4SLinus Torvalds 	if (isdotent(fname, flen))
12041da177e4SLinus Torvalds 		goto out;
12051da177e4SLinus Torvalds 
12068837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
12071da177e4SLinus Torvalds 	if (err)
12081da177e4SLinus Torvalds 		goto out;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
12111da177e4SLinus Torvalds 	dirp = dentry->d_inode;
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	err = nfserr_notdir;
12141da177e4SLinus Torvalds 	if(!dirp->i_op || !dirp->i_op->lookup)
12151da177e4SLinus Torvalds 		goto out;
12161da177e4SLinus Torvalds 	/*
12171da177e4SLinus Torvalds 	 * Check whether the response file handle has been verified yet.
12181da177e4SLinus Torvalds 	 * If it has, the parent directory should already be locked.
12191da177e4SLinus Torvalds 	 */
12201da177e4SLinus Torvalds 	if (!resfhp->fh_dentry) {
12211da177e4SLinus Torvalds 		/* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
122212fd3520SPeter Zijlstra 		fh_lock_nested(fhp, I_MUTEX_PARENT);
12231da177e4SLinus Torvalds 		dchild = lookup_one_len(fname, dentry, flen);
12246264d69dSAl Viro 		host_err = PTR_ERR(dchild);
12251da177e4SLinus Torvalds 		if (IS_ERR(dchild))
12261da177e4SLinus Torvalds 			goto out_nfserr;
12271da177e4SLinus Torvalds 		err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
12281da177e4SLinus Torvalds 		if (err)
12291da177e4SLinus Torvalds 			goto out;
12301da177e4SLinus Torvalds 	} else {
12311da177e4SLinus Torvalds 		/* called from nfsd_proc_create */
12321da177e4SLinus Torvalds 		dchild = dget(resfhp->fh_dentry);
12331da177e4SLinus Torvalds 		if (!fhp->fh_locked) {
12341da177e4SLinus Torvalds 			/* not actually possible */
12351da177e4SLinus Torvalds 			printk(KERN_ERR
12361da177e4SLinus Torvalds 				"nfsd_create: parent %s/%s not locked!\n",
12371da177e4SLinus Torvalds 				dentry->d_parent->d_name.name,
12381da177e4SLinus Torvalds 				dentry->d_name.name);
1239d75f2b9fSAl Viro 			err = nfserr_io;
12401da177e4SLinus Torvalds 			goto out;
12411da177e4SLinus Torvalds 		}
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 	/*
12441da177e4SLinus Torvalds 	 * Make sure the child dentry is still negative ...
12451da177e4SLinus Torvalds 	 */
12461da177e4SLinus Torvalds 	err = nfserr_exist;
12471da177e4SLinus Torvalds 	if (dchild->d_inode) {
12481da177e4SLinus Torvalds 		dprintk("nfsd_create: dentry %s/%s not negative!\n",
12491da177e4SLinus Torvalds 			dentry->d_name.name, dchild->d_name.name);
12501da177e4SLinus Torvalds 		goto out;
12511da177e4SLinus Torvalds 	}
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
12541da177e4SLinus Torvalds 		iap->ia_mode = 0;
12551da177e4SLinus Torvalds 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
12561da177e4SLinus Torvalds 
125707cad1d2SMiklos Szeredi 	err = nfserr_inval;
125807cad1d2SMiklos Szeredi 	if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
125907cad1d2SMiklos Szeredi 		printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
126007cad1d2SMiklos Szeredi 		       type);
126107cad1d2SMiklos Szeredi 		goto out;
126207cad1d2SMiklos Szeredi 	}
126307cad1d2SMiklos Szeredi 
126407cad1d2SMiklos Szeredi 	host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
126507cad1d2SMiklos Szeredi 	if (host_err)
126607cad1d2SMiklos Szeredi 		goto out_nfserr;
126707cad1d2SMiklos Szeredi 
12681da177e4SLinus Torvalds 	/*
12691da177e4SLinus Torvalds 	 * Get the dir op function pointer.
12701da177e4SLinus Torvalds 	 */
1271088406bcSJ. Bruce Fields 	err = 0;
12721da177e4SLinus Torvalds 	switch (type) {
12731da177e4SLinus Torvalds 	case S_IFREG:
12746264d69dSAl Viro 		host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
12751da177e4SLinus Torvalds 		break;
12761da177e4SLinus Torvalds 	case S_IFDIR:
12776264d69dSAl Viro 		host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
12781da177e4SLinus Torvalds 		break;
12791da177e4SLinus Torvalds 	case S_IFCHR:
12801da177e4SLinus Torvalds 	case S_IFBLK:
12811da177e4SLinus Torvalds 	case S_IFIFO:
12821da177e4SLinus Torvalds 	case S_IFSOCK:
12836264d69dSAl Viro 		host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
12841da177e4SLinus Torvalds 		break;
1285463c3197SDave Hansen 	}
1286463c3197SDave Hansen 	if (host_err < 0) {
1287463c3197SDave Hansen 		mnt_drop_write(fhp->fh_export->ex_path.mnt);
1288463c3197SDave Hansen 		goto out_nfserr;
1289463c3197SDave Hansen 	}
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	if (EX_ISSYNC(fhp->fh_export)) {
129245bd3b3dSNeilBrown 		err = nfserrno(nfsd_sync_dir(dentry));
12931da177e4SLinus Torvalds 		write_inode_now(dchild->d_inode, 1);
12941da177e4SLinus Torvalds 	}
12951da177e4SLinus Torvalds 
12965c002b3bSJ. Bruce Fields 	err2 = nfsd_create_setattr(rqstp, resfhp, iap);
1297f193fbabSYAMAMOTO Takashi 	if (err2)
1298f193fbabSYAMAMOTO Takashi 		err = err2;
1299463c3197SDave Hansen 	mnt_drop_write(fhp->fh_export->ex_path.mnt);
13001da177e4SLinus Torvalds 	/*
13011da177e4SLinus Torvalds 	 * Update the file handle to get the new inode info.
13021da177e4SLinus Torvalds 	 */
13031da177e4SLinus Torvalds 	if (!err)
13041da177e4SLinus Torvalds 		err = fh_update(resfhp);
13051da177e4SLinus Torvalds out:
13061da177e4SLinus Torvalds 	if (dchild && !IS_ERR(dchild))
13071da177e4SLinus Torvalds 		dput(dchild);
13081da177e4SLinus Torvalds 	return err;
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds out_nfserr:
13116264d69dSAl Viro 	err = nfserrno(host_err);
13121da177e4SLinus Torvalds 	goto out;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V3
13161da177e4SLinus Torvalds /*
13171da177e4SLinus Torvalds  * NFSv3 version of nfsd_create
13181da177e4SLinus Torvalds  */
13196264d69dSAl Viro __be32
13201da177e4SLinus Torvalds nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
13211da177e4SLinus Torvalds 		char *fname, int flen, struct iattr *iap,
13221da177e4SLinus Torvalds 		struct svc_fh *resfhp, int createmode, u32 *verifier,
132381ac95c5SJ. Bruce Fields 	        int *truncp, int *created)
13241da177e4SLinus Torvalds {
13251da177e4SLinus Torvalds 	struct dentry	*dentry, *dchild = NULL;
13261da177e4SLinus Torvalds 	struct inode	*dirp;
13276264d69dSAl Viro 	__be32		err;
13285c002b3bSJ. Bruce Fields 	__be32		err2;
13296264d69dSAl Viro 	int		host_err;
13301da177e4SLinus Torvalds 	__u32		v_mtime=0, v_atime=0;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	err = nfserr_perm;
13331da177e4SLinus Torvalds 	if (!flen)
13341da177e4SLinus Torvalds 		goto out;
13351da177e4SLinus Torvalds 	err = nfserr_exist;
13361da177e4SLinus Torvalds 	if (isdotent(fname, flen))
13371da177e4SLinus Torvalds 		goto out;
13381da177e4SLinus Torvalds 	if (!(iap->ia_valid & ATTR_MODE))
13391da177e4SLinus Torvalds 		iap->ia_mode = 0;
13408837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
13411da177e4SLinus Torvalds 	if (err)
13421da177e4SLinus Torvalds 		goto out;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
13451da177e4SLinus Torvalds 	dirp = dentry->d_inode;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	/* Get all the sanity checks out of the way before
13481da177e4SLinus Torvalds 	 * we lock the parent. */
13491da177e4SLinus Torvalds 	err = nfserr_notdir;
13501da177e4SLinus Torvalds 	if(!dirp->i_op || !dirp->i_op->lookup)
13511da177e4SLinus Torvalds 		goto out;
135212fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	/*
13551da177e4SLinus Torvalds 	 * Compose the response file handle.
13561da177e4SLinus Torvalds 	 */
13571da177e4SLinus Torvalds 	dchild = lookup_one_len(fname, dentry, flen);
13586264d69dSAl Viro 	host_err = PTR_ERR(dchild);
13591da177e4SLinus Torvalds 	if (IS_ERR(dchild))
13601da177e4SLinus Torvalds 		goto out_nfserr;
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
13631da177e4SLinus Torvalds 	if (err)
13641da177e4SLinus Torvalds 		goto out;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	if (createmode == NFS3_CREATE_EXCLUSIVE) {
1367c397852cSPeter Staubach 		/* solaris7 gets confused (bugid 4218508) if these have
1368749997e5SJeff Layton 		 * the high bit set, so just clear the high bits. If this is
1369749997e5SJeff Layton 		 * ever changed to use different attrs for storing the
1370749997e5SJeff Layton 		 * verifier, then do_open_lookup() will also need to be fixed
1371749997e5SJeff Layton 		 * accordingly.
13721da177e4SLinus Torvalds 		 */
13731da177e4SLinus Torvalds 		v_mtime = verifier[0]&0x7fffffff;
13741da177e4SLinus Torvalds 		v_atime = verifier[1]&0x7fffffff;
13751da177e4SLinus Torvalds 	}
13761da177e4SLinus Torvalds 
1377463c3197SDave Hansen 	host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1378463c3197SDave Hansen 	if (host_err)
1379463c3197SDave Hansen 		goto out_nfserr;
13801da177e4SLinus Torvalds 	if (dchild->d_inode) {
13811da177e4SLinus Torvalds 		err = 0;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 		switch (createmode) {
13841da177e4SLinus Torvalds 		case NFS3_CREATE_UNCHECKED:
13851da177e4SLinus Torvalds 			if (! S_ISREG(dchild->d_inode->i_mode))
13861da177e4SLinus Torvalds 				err = nfserr_exist;
13871da177e4SLinus Torvalds 			else if (truncp) {
13881da177e4SLinus Torvalds 				/* in nfsv4, we need to treat this case a little
13891da177e4SLinus Torvalds 				 * differently.  we don't want to truncate the
13901da177e4SLinus Torvalds 				 * file now; this would be wrong if the OPEN
13911da177e4SLinus Torvalds 				 * fails for some other reason.  furthermore,
13921da177e4SLinus Torvalds 				 * if the size is nonzero, we should ignore it
13931da177e4SLinus Torvalds 				 * according to spec!
13941da177e4SLinus Torvalds 				 */
13951da177e4SLinus Torvalds 				*truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
13961da177e4SLinus Torvalds 			}
13971da177e4SLinus Torvalds 			else {
13981da177e4SLinus Torvalds 				iap->ia_valid &= ATTR_SIZE;
13991da177e4SLinus Torvalds 				goto set_attr;
14001da177e4SLinus Torvalds 			}
14011da177e4SLinus Torvalds 			break;
14021da177e4SLinus Torvalds 		case NFS3_CREATE_EXCLUSIVE:
14031da177e4SLinus Torvalds 			if (   dchild->d_inode->i_mtime.tv_sec == v_mtime
14041da177e4SLinus Torvalds 			    && dchild->d_inode->i_atime.tv_sec == v_atime
14051da177e4SLinus Torvalds 			    && dchild->d_inode->i_size  == 0 )
14061da177e4SLinus Torvalds 				break;
14071da177e4SLinus Torvalds 			 /* fallthru */
14081da177e4SLinus Torvalds 		case NFS3_CREATE_GUARDED:
14091da177e4SLinus Torvalds 			err = nfserr_exist;
14101da177e4SLinus Torvalds 		}
1411463c3197SDave Hansen 		mnt_drop_write(fhp->fh_export->ex_path.mnt);
14121da177e4SLinus Torvalds 		goto out;
14131da177e4SLinus Torvalds 	}
14141da177e4SLinus Torvalds 
14156264d69dSAl Viro 	host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1416463c3197SDave Hansen 	if (host_err < 0) {
1417463c3197SDave Hansen 		mnt_drop_write(fhp->fh_export->ex_path.mnt);
14181da177e4SLinus Torvalds 		goto out_nfserr;
1419463c3197SDave Hansen 	}
142081ac95c5SJ. Bruce Fields 	if (created)
142181ac95c5SJ. Bruce Fields 		*created = 1;
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	if (EX_ISSYNC(fhp->fh_export)) {
142445bd3b3dSNeilBrown 		err = nfserrno(nfsd_sync_dir(dentry));
14251da177e4SLinus Torvalds 		/* setattr will sync the child (or not) */
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	if (createmode == NFS3_CREATE_EXCLUSIVE) {
1429c397852cSPeter Staubach 		/* Cram the verifier into atime/mtime */
14301da177e4SLinus Torvalds 		iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1431c397852cSPeter Staubach 			| ATTR_MTIME_SET|ATTR_ATIME_SET;
14321da177e4SLinus Torvalds 		/* XXX someone who knows this better please fix it for nsec */
14331da177e4SLinus Torvalds 		iap->ia_mtime.tv_sec = v_mtime;
14341da177e4SLinus Torvalds 		iap->ia_atime.tv_sec = v_atime;
14351da177e4SLinus Torvalds 		iap->ia_mtime.tv_nsec = 0;
14361da177e4SLinus Torvalds 		iap->ia_atime.tv_nsec = 0;
14371da177e4SLinus Torvalds 	}
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds  set_attr:
14405c002b3bSJ. Bruce Fields 	err2 = nfsd_create_setattr(rqstp, resfhp, iap);
1441f193fbabSYAMAMOTO Takashi 	if (err2)
144245bd3b3dSNeilBrown 		err = err2;
1443f193fbabSYAMAMOTO Takashi 
1444463c3197SDave Hansen 	mnt_drop_write(fhp->fh_export->ex_path.mnt);
1445f193fbabSYAMAMOTO Takashi 	/*
1446f193fbabSYAMAMOTO Takashi 	 * Update the filehandle to get the new inode info.
1447f193fbabSYAMAMOTO Takashi 	 */
1448f193fbabSYAMAMOTO Takashi 	if (!err)
1449f193fbabSYAMAMOTO Takashi 		err = fh_update(resfhp);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds  out:
14521da177e4SLinus Torvalds 	fh_unlock(fhp);
14531da177e4SLinus Torvalds 	if (dchild && !IS_ERR(dchild))
14541da177e4SLinus Torvalds 		dput(dchild);
14551da177e4SLinus Torvalds  	return err;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds  out_nfserr:
14586264d69dSAl Viro 	err = nfserrno(host_err);
14591da177e4SLinus Torvalds 	goto out;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds #endif /* CONFIG_NFSD_V3 */
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds /*
14641da177e4SLinus Torvalds  * Read a symlink. On entry, *lenp must contain the maximum path length that
14651da177e4SLinus Torvalds  * fits into the buffer. On return, it contains the true length.
14661da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
14671da177e4SLinus Torvalds  */
14686264d69dSAl Viro __be32
14691da177e4SLinus Torvalds nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
14701da177e4SLinus Torvalds {
14711da177e4SLinus Torvalds 	struct dentry	*dentry;
14721da177e4SLinus Torvalds 	struct inode	*inode;
14731da177e4SLinus Torvalds 	mm_segment_t	oldfs;
14746264d69dSAl Viro 	__be32		err;
14756264d69dSAl Viro 	int		host_err;
14761da177e4SLinus Torvalds 
14778837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
14781da177e4SLinus Torvalds 	if (err)
14791da177e4SLinus Torvalds 		goto out;
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
14821da177e4SLinus Torvalds 	inode = dentry->d_inode;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	err = nfserr_inval;
14851da177e4SLinus Torvalds 	if (!inode->i_op || !inode->i_op->readlink)
14861da177e4SLinus Torvalds 		goto out;
14871da177e4SLinus Torvalds 
148854775491SJan Blunck 	touch_atime(fhp->fh_export->ex_path.mnt, dentry);
14891da177e4SLinus Torvalds 	/* N.B. Why does this call need a get_fs()??
14901da177e4SLinus Torvalds 	 * Remove the set_fs and watch the fireworks:-) --okir
14911da177e4SLinus Torvalds 	 */
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	oldfs = get_fs(); set_fs(KERNEL_DS);
14946264d69dSAl Viro 	host_err = inode->i_op->readlink(dentry, buf, *lenp);
14951da177e4SLinus Torvalds 	set_fs(oldfs);
14961da177e4SLinus Torvalds 
14976264d69dSAl Viro 	if (host_err < 0)
14981da177e4SLinus Torvalds 		goto out_nfserr;
14996264d69dSAl Viro 	*lenp = host_err;
15001da177e4SLinus Torvalds 	err = 0;
15011da177e4SLinus Torvalds out:
15021da177e4SLinus Torvalds 	return err;
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds out_nfserr:
15056264d69dSAl Viro 	err = nfserrno(host_err);
15061da177e4SLinus Torvalds 	goto out;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds /*
15101da177e4SLinus Torvalds  * Create a symlink and look up its inode
15111da177e4SLinus Torvalds  * N.B. After this call _both_ fhp and resfhp need an fh_put
15121da177e4SLinus Torvalds  */
15136264d69dSAl Viro __be32
15141da177e4SLinus Torvalds nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
15151da177e4SLinus Torvalds 				char *fname, int flen,
15161da177e4SLinus Torvalds 				char *path,  int plen,
15171da177e4SLinus Torvalds 				struct svc_fh *resfhp,
15181da177e4SLinus Torvalds 				struct iattr *iap)
15191da177e4SLinus Torvalds {
15201da177e4SLinus Torvalds 	struct dentry	*dentry, *dnew;
15216264d69dSAl Viro 	__be32		err, cerr;
15226264d69dSAl Viro 	int		host_err;
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	err = nfserr_noent;
15251da177e4SLinus Torvalds 	if (!flen || !plen)
15261da177e4SLinus Torvalds 		goto out;
15271da177e4SLinus Torvalds 	err = nfserr_exist;
15281da177e4SLinus Torvalds 	if (isdotent(fname, flen))
15291da177e4SLinus Torvalds 		goto out;
15301da177e4SLinus Torvalds 
15318837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
15321da177e4SLinus Torvalds 	if (err)
15331da177e4SLinus Torvalds 		goto out;
15341da177e4SLinus Torvalds 	fh_lock(fhp);
15351da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
15361da177e4SLinus Torvalds 	dnew = lookup_one_len(fname, dentry, flen);
15376264d69dSAl Viro 	host_err = PTR_ERR(dnew);
15381da177e4SLinus Torvalds 	if (IS_ERR(dnew))
15391da177e4SLinus Torvalds 		goto out_nfserr;
15401da177e4SLinus Torvalds 
154175c3f29dSDave Hansen 	host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
154275c3f29dSDave Hansen 	if (host_err)
154375c3f29dSDave Hansen 		goto out_nfserr;
154475c3f29dSDave Hansen 
15451da177e4SLinus Torvalds 	if (unlikely(path[plen] != 0)) {
15461da177e4SLinus Torvalds 		char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
15471da177e4SLinus Torvalds 		if (path_alloced == NULL)
15486264d69dSAl Viro 			host_err = -ENOMEM;
15491da177e4SLinus Torvalds 		else {
15501da177e4SLinus Torvalds 			strncpy(path_alloced, path, plen);
15511da177e4SLinus Torvalds 			path_alloced[plen] = 0;
1552db2e747bSMiklos Szeredi 			host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
15531da177e4SLinus Torvalds 			kfree(path_alloced);
15541da177e4SLinus Torvalds 		}
15551da177e4SLinus Torvalds 	} else
1556db2e747bSMiklos Szeredi 		host_err = vfs_symlink(dentry->d_inode, dnew, path);
15571da177e4SLinus Torvalds 
15586264d69dSAl Viro 	if (!host_err) {
15591da177e4SLinus Torvalds 		if (EX_ISSYNC(fhp->fh_export))
15606264d69dSAl Viro 			host_err = nfsd_sync_dir(dentry);
15616264d69dSAl Viro 	}
15626264d69dSAl Viro 	err = nfserrno(host_err);
15631da177e4SLinus Torvalds 	fh_unlock(fhp);
15641da177e4SLinus Torvalds 
156575c3f29dSDave Hansen 	mnt_drop_write(fhp->fh_export->ex_path.mnt);
156675c3f29dSDave Hansen 
15671da177e4SLinus Torvalds 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
15681da177e4SLinus Torvalds 	dput(dnew);
15691da177e4SLinus Torvalds 	if (err==0) err = cerr;
15701da177e4SLinus Torvalds out:
15711da177e4SLinus Torvalds 	return err;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds out_nfserr:
15746264d69dSAl Viro 	err = nfserrno(host_err);
15751da177e4SLinus Torvalds 	goto out;
15761da177e4SLinus Torvalds }
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds /*
15791da177e4SLinus Torvalds  * Create a hardlink
15801da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
15811da177e4SLinus Torvalds  */
15826264d69dSAl Viro __be32
15831da177e4SLinus Torvalds nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
15841da177e4SLinus Torvalds 				char *name, int len, struct svc_fh *tfhp)
15851da177e4SLinus Torvalds {
15861da177e4SLinus Torvalds 	struct dentry	*ddir, *dnew, *dold;
15871da177e4SLinus Torvalds 	struct inode	*dirp, *dest;
15886264d69dSAl Viro 	__be32		err;
15896264d69dSAl Viro 	int		host_err;
15901da177e4SLinus Torvalds 
15918837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
15921da177e4SLinus Torvalds 	if (err)
15931da177e4SLinus Torvalds 		goto out;
15948837abcaSMiklos Szeredi 	err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
15951da177e4SLinus Torvalds 	if (err)
15961da177e4SLinus Torvalds 		goto out;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	err = nfserr_perm;
15991da177e4SLinus Torvalds 	if (!len)
16001da177e4SLinus Torvalds 		goto out;
16011da177e4SLinus Torvalds 	err = nfserr_exist;
16021da177e4SLinus Torvalds 	if (isdotent(name, len))
16031da177e4SLinus Torvalds 		goto out;
16041da177e4SLinus Torvalds 
160512fd3520SPeter Zijlstra 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
16061da177e4SLinus Torvalds 	ddir = ffhp->fh_dentry;
16071da177e4SLinus Torvalds 	dirp = ddir->d_inode;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	dnew = lookup_one_len(name, ddir, len);
16106264d69dSAl Viro 	host_err = PTR_ERR(dnew);
16111da177e4SLinus Torvalds 	if (IS_ERR(dnew))
16121da177e4SLinus Torvalds 		goto out_nfserr;
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	dold = tfhp->fh_dentry;
16151da177e4SLinus Torvalds 	dest = dold->d_inode;
16161da177e4SLinus Torvalds 
161775c3f29dSDave Hansen 	host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
161875c3f29dSDave Hansen 	if (host_err) {
161975c3f29dSDave Hansen 		err = nfserrno(host_err);
162075c3f29dSDave Hansen 		goto out_dput;
162175c3f29dSDave Hansen 	}
16226264d69dSAl Viro 	host_err = vfs_link(dold, dirp, dnew);
16236264d69dSAl Viro 	if (!host_err) {
16241da177e4SLinus Torvalds 		if (EX_ISSYNC(ffhp->fh_export)) {
162545bd3b3dSNeilBrown 			err = nfserrno(nfsd_sync_dir(ddir));
16261da177e4SLinus Torvalds 			write_inode_now(dest, 1);
16271da177e4SLinus Torvalds 		}
16286264d69dSAl Viro 		err = 0;
16291da177e4SLinus Torvalds 	} else {
16306264d69dSAl Viro 		if (host_err == -EXDEV && rqstp->rq_vers == 2)
16311da177e4SLinus Torvalds 			err = nfserr_acces;
16321da177e4SLinus Torvalds 		else
16336264d69dSAl Viro 			err = nfserrno(host_err);
16341da177e4SLinus Torvalds 	}
163575c3f29dSDave Hansen 	mnt_drop_write(tfhp->fh_export->ex_path.mnt);
163675c3f29dSDave Hansen out_dput:
16371da177e4SLinus Torvalds 	dput(dnew);
1638270d56e5SDavid M. Richter out_unlock:
1639270d56e5SDavid M. Richter 	fh_unlock(ffhp);
16401da177e4SLinus Torvalds out:
16411da177e4SLinus Torvalds 	return err;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds out_nfserr:
16446264d69dSAl Viro 	err = nfserrno(host_err);
1645270d56e5SDavid M. Richter 	goto out_unlock;
16461da177e4SLinus Torvalds }
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds /*
16491da177e4SLinus Torvalds  * Rename a file
16501da177e4SLinus Torvalds  * N.B. After this call _both_ ffhp and tfhp need an fh_put
16511da177e4SLinus Torvalds  */
16526264d69dSAl Viro __be32
16531da177e4SLinus Torvalds nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
16541da177e4SLinus Torvalds 			    struct svc_fh *tfhp, char *tname, int tlen)
16551da177e4SLinus Torvalds {
16561da177e4SLinus Torvalds 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
16571da177e4SLinus Torvalds 	struct inode	*fdir, *tdir;
16586264d69dSAl Viro 	__be32		err;
16596264d69dSAl Viro 	int		host_err;
16601da177e4SLinus Torvalds 
16618837abcaSMiklos Szeredi 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
16621da177e4SLinus Torvalds 	if (err)
16631da177e4SLinus Torvalds 		goto out;
16648837abcaSMiklos Szeredi 	err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
16651da177e4SLinus Torvalds 	if (err)
16661da177e4SLinus Torvalds 		goto out;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	fdentry = ffhp->fh_dentry;
16691da177e4SLinus Torvalds 	fdir = fdentry->d_inode;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	tdentry = tfhp->fh_dentry;
16721da177e4SLinus Torvalds 	tdir = tdentry->d_inode;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1675a56f3937SNeilBrown 	if (ffhp->fh_export != tfhp->fh_export)
16761da177e4SLinus Torvalds 		goto out;
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	err = nfserr_perm;
16791da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
16801da177e4SLinus Torvalds 		goto out;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 	/* cannot use fh_lock as we need deadlock protective ordering
16831da177e4SLinus Torvalds 	 * so do it by hand */
16841da177e4SLinus Torvalds 	trap = lock_rename(tdentry, fdentry);
16851da177e4SLinus Torvalds 	ffhp->fh_locked = tfhp->fh_locked = 1;
16861da177e4SLinus Torvalds 	fill_pre_wcc(ffhp);
16871da177e4SLinus Torvalds 	fill_pre_wcc(tfhp);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	odentry = lookup_one_len(fname, fdentry, flen);
16906264d69dSAl Viro 	host_err = PTR_ERR(odentry);
16911da177e4SLinus Torvalds 	if (IS_ERR(odentry))
16921da177e4SLinus Torvalds 		goto out_nfserr;
16931da177e4SLinus Torvalds 
16946264d69dSAl Viro 	host_err = -ENOENT;
16951da177e4SLinus Torvalds 	if (!odentry->d_inode)
16961da177e4SLinus Torvalds 		goto out_dput_old;
16976264d69dSAl Viro 	host_err = -EINVAL;
16981da177e4SLinus Torvalds 	if (odentry == trap)
16991da177e4SLinus Torvalds 		goto out_dput_old;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	ndentry = lookup_one_len(tname, tdentry, tlen);
17026264d69dSAl Viro 	host_err = PTR_ERR(ndentry);
17031da177e4SLinus Torvalds 	if (IS_ERR(ndentry))
17041da177e4SLinus Torvalds 		goto out_dput_old;
17056264d69dSAl Viro 	host_err = -ENOTEMPTY;
17061da177e4SLinus Torvalds 	if (ndentry == trap)
17071da177e4SLinus Torvalds 		goto out_dput_new;
17081da177e4SLinus Torvalds 
17099079b1ebSDave Hansen 	if (svc_msnfs(ffhp) &&
17101da177e4SLinus Torvalds 		((atomic_read(&odentry->d_count) > 1)
17111da177e4SLinus Torvalds 		 || (atomic_read(&ndentry->d_count) > 1))) {
17126264d69dSAl Viro 			host_err = -EPERM;
17139079b1ebSDave Hansen 			goto out_dput_new;
17149079b1ebSDave Hansen 	}
17159079b1ebSDave Hansen 
17169079b1ebSDave Hansen 	host_err = -EXDEV;
17179079b1ebSDave Hansen 	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
17189079b1ebSDave Hansen 		goto out_dput_new;
17199079b1ebSDave Hansen 	host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
17209079b1ebSDave Hansen 	if (host_err)
17219079b1ebSDave Hansen 		goto out_dput_new;
17229079b1ebSDave Hansen 
17236264d69dSAl Viro 	host_err = vfs_rename(fdir, odentry, tdir, ndentry);
17246264d69dSAl Viro 	if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
17256264d69dSAl Viro 		host_err = nfsd_sync_dir(tdentry);
17266264d69dSAl Viro 		if (!host_err)
17276264d69dSAl Viro 			host_err = nfsd_sync_dir(fdentry);
17281da177e4SLinus Torvalds 	}
17291da177e4SLinus Torvalds 
17309079b1ebSDave Hansen 	mnt_drop_write(ffhp->fh_export->ex_path.mnt);
17319079b1ebSDave Hansen 
17321da177e4SLinus Torvalds  out_dput_new:
17331da177e4SLinus Torvalds 	dput(ndentry);
17341da177e4SLinus Torvalds  out_dput_old:
17351da177e4SLinus Torvalds 	dput(odentry);
17361da177e4SLinus Torvalds  out_nfserr:
17376264d69dSAl Viro 	err = nfserrno(host_err);
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 	/* we cannot reply on fh_unlock on the two filehandles,
17401da177e4SLinus Torvalds 	 * as that would do the wrong thing if the two directories
17411da177e4SLinus Torvalds 	 * were the same, so again we do it by hand
17421da177e4SLinus Torvalds 	 */
17431da177e4SLinus Torvalds 	fill_post_wcc(ffhp);
17441da177e4SLinus Torvalds 	fill_post_wcc(tfhp);
17451da177e4SLinus Torvalds 	unlock_rename(tdentry, fdentry);
17461da177e4SLinus Torvalds 	ffhp->fh_locked = tfhp->fh_locked = 0;
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds out:
17491da177e4SLinus Torvalds 	return err;
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds /*
17531da177e4SLinus Torvalds  * Unlink a file or directory
17541da177e4SLinus Torvalds  * N.B. After this call fhp needs an fh_put
17551da177e4SLinus Torvalds  */
17566264d69dSAl Viro __be32
17571da177e4SLinus Torvalds nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
17581da177e4SLinus Torvalds 				char *fname, int flen)
17591da177e4SLinus Torvalds {
17601da177e4SLinus Torvalds 	struct dentry	*dentry, *rdentry;
17611da177e4SLinus Torvalds 	struct inode	*dirp;
17626264d69dSAl Viro 	__be32		err;
17636264d69dSAl Viro 	int		host_err;
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 	err = nfserr_acces;
17661da177e4SLinus Torvalds 	if (!flen || isdotent(fname, flen))
17671da177e4SLinus Torvalds 		goto out;
17688837abcaSMiklos Szeredi 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
17691da177e4SLinus Torvalds 	if (err)
17701da177e4SLinus Torvalds 		goto out;
17711da177e4SLinus Torvalds 
177212fd3520SPeter Zijlstra 	fh_lock_nested(fhp, I_MUTEX_PARENT);
17731da177e4SLinus Torvalds 	dentry = fhp->fh_dentry;
17741da177e4SLinus Torvalds 	dirp = dentry->d_inode;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	rdentry = lookup_one_len(fname, dentry, flen);
17776264d69dSAl Viro 	host_err = PTR_ERR(rdentry);
17781da177e4SLinus Torvalds 	if (IS_ERR(rdentry))
17791da177e4SLinus Torvalds 		goto out_nfserr;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	if (!rdentry->d_inode) {
17821da177e4SLinus Torvalds 		dput(rdentry);
17831da177e4SLinus Torvalds 		err = nfserr_noent;
17841da177e4SLinus Torvalds 		goto out;
17851da177e4SLinus Torvalds 	}
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	if (!type)
17881da177e4SLinus Torvalds 		type = rdentry->d_inode->i_mode & S_IFMT;
17891da177e4SLinus Torvalds 
17900622753bSDave Hansen 	host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
17910622753bSDave Hansen 	if (host_err)
17920622753bSDave Hansen 		goto out_nfserr;
17930622753bSDave Hansen 
17941da177e4SLinus Torvalds 	if (type != S_IFDIR) { /* It's UNLINK */
17951da177e4SLinus Torvalds #ifdef MSNFS
17961da177e4SLinus Torvalds 		if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
17971da177e4SLinus Torvalds 			(atomic_read(&rdentry->d_count) > 1)) {
17986264d69dSAl Viro 			host_err = -EPERM;
17991da177e4SLinus Torvalds 		} else
18001da177e4SLinus Torvalds #endif
18016264d69dSAl Viro 		host_err = vfs_unlink(dirp, rdentry);
18021da177e4SLinus Torvalds 	} else { /* It's RMDIR */
18036264d69dSAl Viro 		host_err = vfs_rmdir(dirp, rdentry);
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	dput(rdentry);
18071da177e4SLinus Torvalds 
18086264d69dSAl Viro 	if (host_err)
18090622753bSDave Hansen 		goto out_drop;
18106264d69dSAl Viro 	if (EX_ISSYNC(fhp->fh_export))
18116264d69dSAl Viro 		host_err = nfsd_sync_dir(dentry);
18121da177e4SLinus Torvalds 
18130622753bSDave Hansen out_drop:
18140622753bSDave Hansen 	mnt_drop_write(fhp->fh_export->ex_path.mnt);
18151da177e4SLinus Torvalds out_nfserr:
18166264d69dSAl Viro 	err = nfserrno(host_err);
1817f193fbabSYAMAMOTO Takashi out:
1818f193fbabSYAMAMOTO Takashi 	return err;
18191da177e4SLinus Torvalds }
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds /*
182214f7dd63SDavid Woodhouse  * We do this buffering because we must not call back into the file
182314f7dd63SDavid Woodhouse  * system's ->lookup() method from the filldir callback. That may well
182414f7dd63SDavid Woodhouse  * deadlock a number of file systems.
182514f7dd63SDavid Woodhouse  *
182614f7dd63SDavid Woodhouse  * This is based heavily on the implementation of same in XFS.
182714f7dd63SDavid Woodhouse  */
182814f7dd63SDavid Woodhouse struct buffered_dirent {
182914f7dd63SDavid Woodhouse 	u64		ino;
183014f7dd63SDavid Woodhouse 	loff_t		offset;
183114f7dd63SDavid Woodhouse 	int		namlen;
183214f7dd63SDavid Woodhouse 	unsigned int	d_type;
183314f7dd63SDavid Woodhouse 	char		name[];
183414f7dd63SDavid Woodhouse };
183514f7dd63SDavid Woodhouse 
183614f7dd63SDavid Woodhouse struct readdir_data {
183714f7dd63SDavid Woodhouse 	char		*dirent;
183814f7dd63SDavid Woodhouse 	size_t		used;
183953c9c5c0SAl Viro 	int		full;
184014f7dd63SDavid Woodhouse };
184114f7dd63SDavid Woodhouse 
184214f7dd63SDavid Woodhouse static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
184314f7dd63SDavid Woodhouse 				 loff_t offset, u64 ino, unsigned int d_type)
184414f7dd63SDavid Woodhouse {
184514f7dd63SDavid Woodhouse 	struct readdir_data *buf = __buf;
184614f7dd63SDavid Woodhouse 	struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
184714f7dd63SDavid Woodhouse 	unsigned int reclen;
184814f7dd63SDavid Woodhouse 
184914f7dd63SDavid Woodhouse 	reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
185053c9c5c0SAl Viro 	if (buf->used + reclen > PAGE_SIZE) {
185153c9c5c0SAl Viro 		buf->full = 1;
185214f7dd63SDavid Woodhouse 		return -EINVAL;
185353c9c5c0SAl Viro 	}
185414f7dd63SDavid Woodhouse 
185514f7dd63SDavid Woodhouse 	de->namlen = namlen;
185614f7dd63SDavid Woodhouse 	de->offset = offset;
185714f7dd63SDavid Woodhouse 	de->ino = ino;
185814f7dd63SDavid Woodhouse 	de->d_type = d_type;
185914f7dd63SDavid Woodhouse 	memcpy(de->name, name, namlen);
186014f7dd63SDavid Woodhouse 	buf->used += reclen;
186114f7dd63SDavid Woodhouse 
186214f7dd63SDavid Woodhouse 	return 0;
186314f7dd63SDavid Woodhouse }
186414f7dd63SDavid Woodhouse 
186514f7dd63SDavid Woodhouse static int nfsd_buffered_readdir(struct file *file, filldir_t func,
18662628b766SDavid Woodhouse 				 struct readdir_cd *cdp, loff_t *offsetp)
18672628b766SDavid Woodhouse {
186814f7dd63SDavid Woodhouse 	struct readdir_data buf;
186914f7dd63SDavid Woodhouse 	struct buffered_dirent *de;
18702628b766SDavid Woodhouse 	int host_err;
187114f7dd63SDavid Woodhouse 	int size;
187214f7dd63SDavid Woodhouse 	loff_t offset;
18732628b766SDavid Woodhouse 
187414f7dd63SDavid Woodhouse 	buf.dirent = (void *)__get_free_page(GFP_KERNEL);
187514f7dd63SDavid Woodhouse 	if (!buf.dirent)
187614f7dd63SDavid Woodhouse 		return -ENOMEM;
187714f7dd63SDavid Woodhouse 
187814f7dd63SDavid Woodhouse 	offset = *offsetp;
18792628b766SDavid Woodhouse 	cdp->err = nfserr_eof; /* will be cleared on successful read */
18802628b766SDavid Woodhouse 
188114f7dd63SDavid Woodhouse 	while (1) {
188214f7dd63SDavid Woodhouse 		unsigned int reclen;
188314f7dd63SDavid Woodhouse 
188414f7dd63SDavid Woodhouse 		buf.used = 0;
188553c9c5c0SAl Viro 		buf.full = 0;
188614f7dd63SDavid Woodhouse 
188714f7dd63SDavid Woodhouse 		host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
188853c9c5c0SAl Viro 		if (buf.full)
188953c9c5c0SAl Viro 			host_err = 0;
189053c9c5c0SAl Viro 
189153c9c5c0SAl Viro 		if (host_err < 0)
189214f7dd63SDavid Woodhouse 			break;
189314f7dd63SDavid Woodhouse 
189414f7dd63SDavid Woodhouse 		size = buf.used;
189514f7dd63SDavid Woodhouse 
189614f7dd63SDavid Woodhouse 		if (!size)
189714f7dd63SDavid Woodhouse 			break;
189814f7dd63SDavid Woodhouse 
189914f7dd63SDavid Woodhouse 		de = (struct buffered_dirent *)buf.dirent;
190014f7dd63SDavid Woodhouse 		while (size > 0) {
190114f7dd63SDavid Woodhouse 			offset = de->offset;
190214f7dd63SDavid Woodhouse 
190314f7dd63SDavid Woodhouse 			if (func(cdp, de->name, de->namlen, de->offset,
190414f7dd63SDavid Woodhouse 				 de->ino, de->d_type))
190514f7dd63SDavid Woodhouse 				goto done;
190614f7dd63SDavid Woodhouse 
190714f7dd63SDavid Woodhouse 			if (cdp->err != nfs_ok)
190814f7dd63SDavid Woodhouse 				goto done;
190914f7dd63SDavid Woodhouse 
191014f7dd63SDavid Woodhouse 			reclen = ALIGN(sizeof(*de) + de->namlen,
191114f7dd63SDavid Woodhouse 				       sizeof(u64));
191214f7dd63SDavid Woodhouse 			size -= reclen;
191314f7dd63SDavid Woodhouse 			de = (struct buffered_dirent *)((char *)de + reclen);
191414f7dd63SDavid Woodhouse 		}
1915c002a6c7SDavid Woodhouse 		offset = vfs_llseek(file, 0, SEEK_CUR);
19168d7c4203SJ. Bruce Fields 		cdp->err = nfserr_eof;
1917c002a6c7SDavid Woodhouse 		if (!buf.full)
1918c002a6c7SDavid Woodhouse 			break;
191914f7dd63SDavid Woodhouse 	}
192014f7dd63SDavid Woodhouse 
192114f7dd63SDavid Woodhouse  done:
192214f7dd63SDavid Woodhouse 	free_page((unsigned long)(buf.dirent));
19232628b766SDavid Woodhouse 
19242628b766SDavid Woodhouse 	if (host_err)
19252628b766SDavid Woodhouse 		return nfserrno(host_err);
192614f7dd63SDavid Woodhouse 
192714f7dd63SDavid Woodhouse 	*offsetp = offset;
19282628b766SDavid Woodhouse 	return cdp->err;
19292628b766SDavid Woodhouse }
19302628b766SDavid Woodhouse 
19311da177e4SLinus Torvalds /*
19321da177e4SLinus Torvalds  * Read entries from a directory.
19331da177e4SLinus Torvalds  * The  NFSv3/4 verifier we ignore for now.
19341da177e4SLinus Torvalds  */
19356264d69dSAl Viro __be32
19361da177e4SLinus Torvalds nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1937a0ad13efSNeilBrown 	     struct readdir_cd *cdp, filldir_t func)
19381da177e4SLinus Torvalds {
19396264d69dSAl Viro 	__be32		err;
19401da177e4SLinus Torvalds 	struct file	*file;
19411da177e4SLinus Torvalds 	loff_t		offset = *offsetp;
19421da177e4SLinus Torvalds 
19438837abcaSMiklos Szeredi 	err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
19441da177e4SLinus Torvalds 	if (err)
19451da177e4SLinus Torvalds 		goto out;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	offset = vfs_llseek(file, offset, 0);
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:
19581da177e4SLinus Torvalds 	nfsd_close(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 {
197004716e66SJ. Bruce Fields 	__be32 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
1971726c3342SDavid Howells 	if (!err && vfs_statfs(fhp->fh_dentry,stat))
19721da177e4SLinus Torvalds 		err = nfserr_io;
19731da177e4SLinus Torvalds 	return err;
19741da177e4SLinus Torvalds }
19751da177e4SLinus Torvalds 
1976c7d51402SJ. Bruce Fields static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
1977e22841c6SJ. Bruce Fields {
1978c7d51402SJ. Bruce Fields 	return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
1979e22841c6SJ. Bruce Fields }
1980e22841c6SJ. Bruce Fields 
19811da177e4SLinus Torvalds /*
19821da177e4SLinus Torvalds  * Check for a user's access permissions to this inode.
19831da177e4SLinus Torvalds  */
19846264d69dSAl Viro __be32
19850ec757dfSJ. Bruce Fields nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
19860ec757dfSJ. Bruce Fields 					struct dentry *dentry, int acc)
19871da177e4SLinus Torvalds {
19881da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
19891da177e4SLinus Torvalds 	int		err;
19901da177e4SLinus Torvalds 
19918837abcaSMiklos Szeredi 	if (acc == NFSD_MAY_NOP)
19921da177e4SLinus Torvalds 		return 0;
19931da177e4SLinus Torvalds #if 0
19941da177e4SLinus Torvalds 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
19951da177e4SLinus Torvalds 		acc,
19968837abcaSMiklos Szeredi 		(acc & NFSD_MAY_READ)?	" read"  : "",
19978837abcaSMiklos Szeredi 		(acc & NFSD_MAY_WRITE)?	" write" : "",
19988837abcaSMiklos Szeredi 		(acc & NFSD_MAY_EXEC)?	" exec"  : "",
19998837abcaSMiklos Szeredi 		(acc & NFSD_MAY_SATTR)?	" sattr" : "",
20008837abcaSMiklos Szeredi 		(acc & NFSD_MAY_TRUNC)?	" trunc" : "",
20018837abcaSMiklos Szeredi 		(acc & NFSD_MAY_LOCK)?	" lock"  : "",
20028837abcaSMiklos Szeredi 		(acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
20031da177e4SLinus Torvalds 		inode->i_mode,
20041da177e4SLinus Torvalds 		IS_IMMUTABLE(inode)?	" immut" : "",
20051da177e4SLinus Torvalds 		IS_APPEND(inode)?	" append" : "",
20062c463e95SDave Hansen 		__mnt_is_readonly(exp->ex_path.mnt)?	" ro" : "");
20071da177e4SLinus Torvalds 	dprintk("      owner %d/%d user %d/%d\n",
20085cc0a840SDavid Howells 		inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
20091da177e4SLinus Torvalds #endif
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	/* Normally we reject any write/sattr etc access on a read-only file
20121da177e4SLinus Torvalds 	 * system.  But if it is IRIX doing check on write-access for a
20131da177e4SLinus Torvalds 	 * device special file, we ignore rofs.
20141da177e4SLinus Torvalds 	 */
20158837abcaSMiklos Szeredi 	if (!(acc & NFSD_MAY_LOCAL_ACCESS))
20168837abcaSMiklos Szeredi 		if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
20172c463e95SDave Hansen 			if (exp_rdonly(rqstp, exp) ||
20182c463e95SDave Hansen 			    __mnt_is_readonly(exp->ex_path.mnt))
20191da177e4SLinus Torvalds 				return nfserr_rofs;
20208837abcaSMiklos Szeredi 			if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
20211da177e4SLinus Torvalds 				return nfserr_perm;
20221da177e4SLinus Torvalds 		}
20238837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
20241da177e4SLinus Torvalds 		return nfserr_perm;
20251da177e4SLinus Torvalds 
20268837abcaSMiklos Szeredi 	if (acc & NFSD_MAY_LOCK) {
20271da177e4SLinus Torvalds 		/* If we cannot rely on authentication in NLM requests,
20281da177e4SLinus Torvalds 		 * just allow locks, otherwise require read permission, or
20291da177e4SLinus Torvalds 		 * ownership
20301da177e4SLinus Torvalds 		 */
20311da177e4SLinus Torvalds 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
20321da177e4SLinus Torvalds 			return 0;
20331da177e4SLinus Torvalds 		else
20348837abcaSMiklos Szeredi 			acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 	/*
20371da177e4SLinus Torvalds 	 * The file owner always gets access permission for accesses that
20381da177e4SLinus Torvalds 	 * would normally be checked at open time. This is to make
20391da177e4SLinus Torvalds 	 * file access work even when the client has done a fchmod(fd, 0).
20401da177e4SLinus Torvalds 	 *
20411da177e4SLinus Torvalds 	 * However, `cp foo bar' should fail nevertheless when bar is
20421da177e4SLinus Torvalds 	 * readonly. A sensible way to do this might be to reject all
20431da177e4SLinus Torvalds 	 * attempts to truncate a read-only file, because a creat() call
20441da177e4SLinus Torvalds 	 * always implies file truncation.
20451da177e4SLinus Torvalds 	 * ... but this isn't really fair.  A process may reasonably call
20461da177e4SLinus Torvalds 	 * ftruncate on an open file descriptor on a file with perm 000.
20471da177e4SLinus Torvalds 	 * We must trust the client to do permission checking - using "ACCESS"
20481da177e4SLinus Torvalds 	 * with NFSv3.
20491da177e4SLinus Torvalds 	 */
20508837abcaSMiklos Szeredi 	if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
20515cc0a840SDavid Howells 	    inode->i_uid == current_fsuid())
20521da177e4SLinus Torvalds 		return 0;
20531da177e4SLinus Torvalds 
20548837abcaSMiklos Szeredi 	/* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2055f419a2e3SAl Viro 	err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	/* Allow read access to binaries even when mode 111 */
20581da177e4SLinus Torvalds 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
20598837abcaSMiklos Szeredi 	    acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
2060f419a2e3SAl Viro 		err = inode_permission(inode, MAY_EXEC);
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	return err? nfserrno(err) : 0;
20631da177e4SLinus Torvalds }
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds void
20661da177e4SLinus Torvalds nfsd_racache_shutdown(void)
20671da177e4SLinus Torvalds {
206854a66e54SJeff Layton 	struct raparms *raparm, *last_raparm;
206954a66e54SJeff Layton 	unsigned int i;
207054a66e54SJeff Layton 
20711da177e4SLinus Torvalds 	dprintk("nfsd: freeing readahead buffers.\n");
207254a66e54SJeff Layton 
207354a66e54SJeff Layton 	for (i = 0; i < RAPARM_HASH_SIZE; i++) {
207454a66e54SJeff Layton 		raparm = raparm_hash[i].pb_head;
207554a66e54SJeff Layton 		while(raparm) {
207654a66e54SJeff Layton 			last_raparm = raparm;
207754a66e54SJeff Layton 			raparm = raparm->p_next;
207854a66e54SJeff Layton 			kfree(last_raparm);
207954a66e54SJeff Layton 		}
208054a66e54SJeff Layton 		raparm_hash[i].pb_head = NULL;
208154a66e54SJeff Layton 	}
20821da177e4SLinus Torvalds }
20831da177e4SLinus Torvalds /*
20841da177e4SLinus Torvalds  * Initialize readahead param cache
20851da177e4SLinus Torvalds  */
20861da177e4SLinus Torvalds int
20871da177e4SLinus Torvalds nfsd_racache_init(int cache_size)
20881da177e4SLinus Torvalds {
20891da177e4SLinus Torvalds 	int	i;
2090fce1456aSGreg Banks 	int	j = 0;
2091fce1456aSGreg Banks 	int	nperbucket;
209254a66e54SJeff Layton 	struct raparms **raparm = NULL;
20931da177e4SLinus Torvalds 
2094fce1456aSGreg Banks 
209554a66e54SJeff Layton 	if (raparm_hash[0].pb_head)
20961da177e4SLinus Torvalds 		return 0;
209754a66e54SJeff Layton 	nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
209854a66e54SJeff Layton 	if (nperbucket < 2)
209954a66e54SJeff Layton 		nperbucket = 2;
210054a66e54SJeff Layton 	cache_size = nperbucket * RAPARM_HASH_SIZE;
21014b3bb06bSYan Burman 
21024b3bb06bSYan Burman 	dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
210354a66e54SJeff Layton 
2104fce1456aSGreg Banks 	for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2105fce1456aSGreg Banks 		spin_lock_init(&raparm_hash[i].pb_lock);
210654a66e54SJeff Layton 
210754a66e54SJeff Layton 		raparm = &raparm_hash[i].pb_head;
210854a66e54SJeff Layton 		for (j = 0; j < nperbucket; j++) {
210954a66e54SJeff Layton 			*raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
211054a66e54SJeff Layton 			if (!*raparm)
211154a66e54SJeff Layton 				goto out_nomem;
211254a66e54SJeff Layton 			raparm = &(*raparm)->p_next;
2113fce1456aSGreg Banks 		}
211454a66e54SJeff Layton 		*raparm = NULL;
21151da177e4SLinus Torvalds 	}
21164b3bb06bSYan Burman 
21171da177e4SLinus Torvalds 	nfsdstats.ra_size = cache_size;
21181da177e4SLinus Torvalds 	return 0;
211954a66e54SJeff Layton 
212054a66e54SJeff Layton out_nomem:
212154a66e54SJeff Layton 	dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
212254a66e54SJeff Layton 	nfsd_racache_shutdown();
212354a66e54SJeff Layton 	return -ENOMEM;
21241da177e4SLinus Torvalds }
2125a257cdd0SAndreas Gruenbacher 
2126a257cdd0SAndreas Gruenbacher #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2127a257cdd0SAndreas Gruenbacher struct posix_acl *
2128a257cdd0SAndreas Gruenbacher nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2129a257cdd0SAndreas Gruenbacher {
2130a257cdd0SAndreas Gruenbacher 	struct inode *inode = fhp->fh_dentry->d_inode;
2131a257cdd0SAndreas Gruenbacher 	char *name;
2132a257cdd0SAndreas Gruenbacher 	void *value = NULL;
2133a257cdd0SAndreas Gruenbacher 	ssize_t size;
2134a257cdd0SAndreas Gruenbacher 	struct posix_acl *acl;
2135a257cdd0SAndreas Gruenbacher 
21365be196e5SChristoph Hellwig 	if (!IS_POSIXACL(inode))
2137a257cdd0SAndreas Gruenbacher 		return ERR_PTR(-EOPNOTSUPP);
21385be196e5SChristoph Hellwig 
2139a257cdd0SAndreas Gruenbacher 	switch (type) {
2140a257cdd0SAndreas Gruenbacher 	case ACL_TYPE_ACCESS:
2141334a13ecSChristoph Hellwig 		name = POSIX_ACL_XATTR_ACCESS;
2142a257cdd0SAndreas Gruenbacher 		break;
2143a257cdd0SAndreas Gruenbacher 	case ACL_TYPE_DEFAULT:
2144334a13ecSChristoph Hellwig 		name = POSIX_ACL_XATTR_DEFAULT;
2145a257cdd0SAndreas Gruenbacher 		break;
2146a257cdd0SAndreas Gruenbacher 	default:
2147a257cdd0SAndreas Gruenbacher 		return ERR_PTR(-EOPNOTSUPP);
2148a257cdd0SAndreas Gruenbacher 	}
2149a257cdd0SAndreas Gruenbacher 
21505be196e5SChristoph Hellwig 	size = nfsd_getxattr(fhp->fh_dentry, name, &value);
21515be196e5SChristoph Hellwig 	if (size < 0)
21525be196e5SChristoph Hellwig 		return ERR_PTR(size);
2153a257cdd0SAndreas Gruenbacher 
2154a257cdd0SAndreas Gruenbacher 	acl = posix_acl_from_xattr(value, size);
2155a257cdd0SAndreas Gruenbacher 	kfree(value);
2156a257cdd0SAndreas Gruenbacher 	return acl;
2157a257cdd0SAndreas Gruenbacher }
2158a257cdd0SAndreas Gruenbacher 
2159a257cdd0SAndreas Gruenbacher int
2160a257cdd0SAndreas Gruenbacher nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2161a257cdd0SAndreas Gruenbacher {
2162a257cdd0SAndreas Gruenbacher 	struct inode *inode = fhp->fh_dentry->d_inode;
2163a257cdd0SAndreas Gruenbacher 	char *name;
2164a257cdd0SAndreas Gruenbacher 	void *value = NULL;
2165a257cdd0SAndreas Gruenbacher 	size_t size;
2166a257cdd0SAndreas Gruenbacher 	int error;
2167a257cdd0SAndreas Gruenbacher 
2168a257cdd0SAndreas Gruenbacher 	if (!IS_POSIXACL(inode) || !inode->i_op ||
2169a257cdd0SAndreas Gruenbacher 	    !inode->i_op->setxattr || !inode->i_op->removexattr)
2170a257cdd0SAndreas Gruenbacher 		return -EOPNOTSUPP;
2171a257cdd0SAndreas Gruenbacher 	switch(type) {
2172a257cdd0SAndreas Gruenbacher 		case ACL_TYPE_ACCESS:
2173334a13ecSChristoph Hellwig 			name = POSIX_ACL_XATTR_ACCESS;
2174a257cdd0SAndreas Gruenbacher 			break;
2175a257cdd0SAndreas Gruenbacher 		case ACL_TYPE_DEFAULT:
2176334a13ecSChristoph Hellwig 			name = POSIX_ACL_XATTR_DEFAULT;
2177a257cdd0SAndreas Gruenbacher 			break;
2178a257cdd0SAndreas Gruenbacher 		default:
2179a257cdd0SAndreas Gruenbacher 			return -EOPNOTSUPP;
2180a257cdd0SAndreas Gruenbacher 	}
2181a257cdd0SAndreas Gruenbacher 
2182a257cdd0SAndreas Gruenbacher 	if (acl && acl->a_count) {
2183334a13ecSChristoph Hellwig 		size = posix_acl_xattr_size(acl->a_count);
2184a257cdd0SAndreas Gruenbacher 		value = kmalloc(size, GFP_KERNEL);
2185a257cdd0SAndreas Gruenbacher 		if (!value)
2186a257cdd0SAndreas Gruenbacher 			return -ENOMEM;
21879ccfc29cSFlorin Malita 		error = posix_acl_to_xattr(acl, value, size);
21889ccfc29cSFlorin Malita 		if (error < 0)
2189a257cdd0SAndreas Gruenbacher 			goto getout;
21909ccfc29cSFlorin Malita 		size = error;
2191a257cdd0SAndreas Gruenbacher 	} else
2192a257cdd0SAndreas Gruenbacher 		size = 0;
2193a257cdd0SAndreas Gruenbacher 
219418f335afSDave Hansen 	error = mnt_want_write(fhp->fh_export->ex_path.mnt);
219518f335afSDave Hansen 	if (error)
219618f335afSDave Hansen 		goto getout;
2197a257cdd0SAndreas Gruenbacher 	if (size)
21985be196e5SChristoph Hellwig 		error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
2199a257cdd0SAndreas Gruenbacher 	else {
2200a257cdd0SAndreas Gruenbacher 		if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2201a257cdd0SAndreas Gruenbacher 			error = 0;
2202a257cdd0SAndreas Gruenbacher 		else {
22035be196e5SChristoph Hellwig 			error = vfs_removexattr(fhp->fh_dentry, name);
2204a257cdd0SAndreas Gruenbacher 			if (error == -ENODATA)
2205a257cdd0SAndreas Gruenbacher 				error = 0;
2206a257cdd0SAndreas Gruenbacher 		}
2207a257cdd0SAndreas Gruenbacher 	}
220818f335afSDave Hansen 	mnt_drop_write(fhp->fh_export->ex_path.mnt);
2209a257cdd0SAndreas Gruenbacher 
2210a257cdd0SAndreas Gruenbacher getout:
2211a257cdd0SAndreas Gruenbacher 	kfree(value);
2212a257cdd0SAndreas Gruenbacher 	return error;
2213a257cdd0SAndreas Gruenbacher }
2214a257cdd0SAndreas Gruenbacher #endif  /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
2215