xref: /OK3568_Linux_fs/kernel/fs/nfsd/vfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * File operations used by nfsd. Some of these have been ripped from
4*4882a593Smuzhiyun  * other parts of the kernel because they weren't exported, others
5*4882a593Smuzhiyun  * are partial duplicates with added or changed functionality.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Note that several functions dget() the dentry upon which they want
8*4882a593Smuzhiyun  * to act, most notably those that create directory entries. Response
9*4882a593Smuzhiyun  * dentry's are dput()'d if necessary in the release callback.
10*4882a593Smuzhiyun  * So if you notice code paths that apparently fail to dput() the
11*4882a593Smuzhiyun  * dentry, don't worry--they have been taken care of.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14*4882a593Smuzhiyun  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/fs.h>
18*4882a593Smuzhiyun #include <linux/file.h>
19*4882a593Smuzhiyun #include <linux/splice.h>
20*4882a593Smuzhiyun #include <linux/falloc.h>
21*4882a593Smuzhiyun #include <linux/fcntl.h>
22*4882a593Smuzhiyun #include <linux/namei.h>
23*4882a593Smuzhiyun #include <linux/delay.h>
24*4882a593Smuzhiyun #include <linux/fsnotify.h>
25*4882a593Smuzhiyun #include <linux/posix_acl_xattr.h>
26*4882a593Smuzhiyun #include <linux/xattr.h>
27*4882a593Smuzhiyun #include <linux/jhash.h>
28*4882a593Smuzhiyun #include <linux/ima.h>
29*4882a593Smuzhiyun #include <linux/slab.h>
30*4882a593Smuzhiyun #include <linux/uaccess.h>
31*4882a593Smuzhiyun #include <linux/exportfs.h>
32*4882a593Smuzhiyun #include <linux/writeback.h>
33*4882a593Smuzhiyun #include <linux/security.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V3
36*4882a593Smuzhiyun #include "xdr3.h"
37*4882a593Smuzhiyun #endif /* CONFIG_NFSD_V3 */
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V4
40*4882a593Smuzhiyun #include "../internal.h"
41*4882a593Smuzhiyun #include "acl.h"
42*4882a593Smuzhiyun #include "idmap.h"
43*4882a593Smuzhiyun #endif /* CONFIG_NFSD_V4 */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #include "nfsd.h"
46*4882a593Smuzhiyun #include "vfs.h"
47*4882a593Smuzhiyun #include "filecache.h"
48*4882a593Smuzhiyun #include "trace.h"
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #define NFSDDBG_FACILITY		NFSDDBG_FILEOP
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun  * Called from nfsd_lookup and encode_dirent. Check if we have crossed
54*4882a593Smuzhiyun  * a mount point.
55*4882a593Smuzhiyun  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
56*4882a593Smuzhiyun  *  or nfs_ok having possibly changed *dpp and *expp
57*4882a593Smuzhiyun  */
58*4882a593Smuzhiyun int
nfsd_cross_mnt(struct svc_rqst * rqstp,struct dentry ** dpp,struct svc_export ** expp)59*4882a593Smuzhiyun nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
60*4882a593Smuzhiyun 		        struct svc_export **expp)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	struct svc_export *exp = *expp, *exp2 = NULL;
63*4882a593Smuzhiyun 	struct dentry *dentry = *dpp;
64*4882a593Smuzhiyun 	struct path path = {.mnt = mntget(exp->ex_path.mnt),
65*4882a593Smuzhiyun 			    .dentry = dget(dentry)};
66*4882a593Smuzhiyun 	int err = 0;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	err = follow_down(&path);
69*4882a593Smuzhiyun 	if (err < 0)
70*4882a593Smuzhiyun 		goto out;
71*4882a593Smuzhiyun 	if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
72*4882a593Smuzhiyun 	    nfsd_mountpoint(dentry, exp) == 2) {
73*4882a593Smuzhiyun 		/* This is only a mountpoint in some other namespace */
74*4882a593Smuzhiyun 		path_put(&path);
75*4882a593Smuzhiyun 		goto out;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	exp2 = rqst_exp_get_by_name(rqstp, &path);
79*4882a593Smuzhiyun 	if (IS_ERR(exp2)) {
80*4882a593Smuzhiyun 		err = PTR_ERR(exp2);
81*4882a593Smuzhiyun 		/*
82*4882a593Smuzhiyun 		 * We normally allow NFS clients to continue
83*4882a593Smuzhiyun 		 * "underneath" a mountpoint that is not exported.
84*4882a593Smuzhiyun 		 * The exception is V4ROOT, where no traversal is ever
85*4882a593Smuzhiyun 		 * allowed without an explicit export of the new
86*4882a593Smuzhiyun 		 * directory.
87*4882a593Smuzhiyun 		 */
88*4882a593Smuzhiyun 		if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
89*4882a593Smuzhiyun 			err = 0;
90*4882a593Smuzhiyun 		path_put(&path);
91*4882a593Smuzhiyun 		goto out;
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 	if (nfsd_v4client(rqstp) ||
94*4882a593Smuzhiyun 		(exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
95*4882a593Smuzhiyun 		/* successfully crossed mount point */
96*4882a593Smuzhiyun 		/*
97*4882a593Smuzhiyun 		 * This is subtle: path.dentry is *not* on path.mnt
98*4882a593Smuzhiyun 		 * at this point.  The only reason we are safe is that
99*4882a593Smuzhiyun 		 * original mnt is pinned down by exp, so we should
100*4882a593Smuzhiyun 		 * put path *before* putting exp
101*4882a593Smuzhiyun 		 */
102*4882a593Smuzhiyun 		*dpp = path.dentry;
103*4882a593Smuzhiyun 		path.dentry = dentry;
104*4882a593Smuzhiyun 		*expp = exp2;
105*4882a593Smuzhiyun 		exp2 = exp;
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 	path_put(&path);
108*4882a593Smuzhiyun 	exp_put(exp2);
109*4882a593Smuzhiyun out:
110*4882a593Smuzhiyun 	return err;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
follow_to_parent(struct path * path)113*4882a593Smuzhiyun static void follow_to_parent(struct path *path)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	struct dentry *dp;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	while (path->dentry == path->mnt->mnt_root && follow_up(path))
118*4882a593Smuzhiyun 		;
119*4882a593Smuzhiyun 	dp = dget_parent(path->dentry);
120*4882a593Smuzhiyun 	dput(path->dentry);
121*4882a593Smuzhiyun 	path->dentry = dp;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
nfsd_lookup_parent(struct svc_rqst * rqstp,struct dentry * dparent,struct svc_export ** exp,struct dentry ** dentryp)124*4882a593Smuzhiyun static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	struct svc_export *exp2;
127*4882a593Smuzhiyun 	struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
128*4882a593Smuzhiyun 			    .dentry = dget(dparent)};
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	follow_to_parent(&path);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	exp2 = rqst_exp_parent(rqstp, &path);
133*4882a593Smuzhiyun 	if (PTR_ERR(exp2) == -ENOENT) {
134*4882a593Smuzhiyun 		*dentryp = dget(dparent);
135*4882a593Smuzhiyun 	} else if (IS_ERR(exp2)) {
136*4882a593Smuzhiyun 		path_put(&path);
137*4882a593Smuzhiyun 		return PTR_ERR(exp2);
138*4882a593Smuzhiyun 	} else {
139*4882a593Smuzhiyun 		*dentryp = dget(path.dentry);
140*4882a593Smuzhiyun 		exp_put(*exp);
141*4882a593Smuzhiyun 		*exp = exp2;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 	path_put(&path);
144*4882a593Smuzhiyun 	return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun  * For nfsd purposes, we treat V4ROOT exports as though there was an
149*4882a593Smuzhiyun  * export at *every* directory.
150*4882a593Smuzhiyun  * We return:
151*4882a593Smuzhiyun  * '1' if this dentry *must* be an export point,
152*4882a593Smuzhiyun  * '2' if it might be, if there is really a mount here, and
153*4882a593Smuzhiyun  * '0' if there is no chance of an export point here.
154*4882a593Smuzhiyun  */
nfsd_mountpoint(struct dentry * dentry,struct svc_export * exp)155*4882a593Smuzhiyun int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	if (!d_inode(dentry))
158*4882a593Smuzhiyun 		return 0;
159*4882a593Smuzhiyun 	if (exp->ex_flags & NFSEXP_V4ROOT)
160*4882a593Smuzhiyun 		return 1;
161*4882a593Smuzhiyun 	if (nfsd4_is_junction(dentry))
162*4882a593Smuzhiyun 		return 1;
163*4882a593Smuzhiyun 	if (d_mountpoint(dentry))
164*4882a593Smuzhiyun 		/*
165*4882a593Smuzhiyun 		 * Might only be a mountpoint in a different namespace,
166*4882a593Smuzhiyun 		 * but we need to check.
167*4882a593Smuzhiyun 		 */
168*4882a593Smuzhiyun 		return 2;
169*4882a593Smuzhiyun 	return 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun __be32
nfsd_lookup_dentry(struct svc_rqst * rqstp,struct svc_fh * fhp,const char * name,unsigned int len,struct svc_export ** exp_ret,struct dentry ** dentry_ret)173*4882a593Smuzhiyun nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
174*4882a593Smuzhiyun 		   const char *name, unsigned int len,
175*4882a593Smuzhiyun 		   struct svc_export **exp_ret, struct dentry **dentry_ret)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct svc_export	*exp;
178*4882a593Smuzhiyun 	struct dentry		*dparent;
179*4882a593Smuzhiyun 	struct dentry		*dentry;
180*4882a593Smuzhiyun 	int			host_err;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	dparent = fhp->fh_dentry;
185*4882a593Smuzhiyun 	exp = exp_get(fhp->fh_export);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	/* Lookup the name, but don't follow links */
188*4882a593Smuzhiyun 	if (isdotent(name, len)) {
189*4882a593Smuzhiyun 		if (len==1)
190*4882a593Smuzhiyun 			dentry = dget(dparent);
191*4882a593Smuzhiyun 		else if (dparent != exp->ex_path.dentry)
192*4882a593Smuzhiyun 			dentry = dget_parent(dparent);
193*4882a593Smuzhiyun 		else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
194*4882a593Smuzhiyun 			dentry = dget(dparent); /* .. == . just like at / */
195*4882a593Smuzhiyun 		else {
196*4882a593Smuzhiyun 			/* checking mountpoint crossing is very different when stepping up */
197*4882a593Smuzhiyun 			host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
198*4882a593Smuzhiyun 			if (host_err)
199*4882a593Smuzhiyun 				goto out_nfserr;
200*4882a593Smuzhiyun 		}
201*4882a593Smuzhiyun 	} else {
202*4882a593Smuzhiyun 		/*
203*4882a593Smuzhiyun 		 * In the nfsd4_open() case, this may be held across
204*4882a593Smuzhiyun 		 * subsequent open and delegation acquisition which may
205*4882a593Smuzhiyun 		 * need to take the child's i_mutex:
206*4882a593Smuzhiyun 		 */
207*4882a593Smuzhiyun 		fh_lock_nested(fhp, I_MUTEX_PARENT);
208*4882a593Smuzhiyun 		dentry = lookup_one_len(name, dparent, len);
209*4882a593Smuzhiyun 		host_err = PTR_ERR(dentry);
210*4882a593Smuzhiyun 		if (IS_ERR(dentry))
211*4882a593Smuzhiyun 			goto out_nfserr;
212*4882a593Smuzhiyun 		if (nfsd_mountpoint(dentry, exp)) {
213*4882a593Smuzhiyun 			/*
214*4882a593Smuzhiyun 			 * We don't need the i_mutex after all.  It's
215*4882a593Smuzhiyun 			 * still possible we could open this (regular
216*4882a593Smuzhiyun 			 * files can be mountpoints too), but the
217*4882a593Smuzhiyun 			 * i_mutex is just there to prevent renames of
218*4882a593Smuzhiyun 			 * something that we might be about to delegate,
219*4882a593Smuzhiyun 			 * and a mountpoint won't be renamed:
220*4882a593Smuzhiyun 			 */
221*4882a593Smuzhiyun 			fh_unlock(fhp);
222*4882a593Smuzhiyun 			if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
223*4882a593Smuzhiyun 				dput(dentry);
224*4882a593Smuzhiyun 				goto out_nfserr;
225*4882a593Smuzhiyun 			}
226*4882a593Smuzhiyun 		}
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 	*dentry_ret = dentry;
229*4882a593Smuzhiyun 	*exp_ret = exp;
230*4882a593Smuzhiyun 	return 0;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun out_nfserr:
233*4882a593Smuzhiyun 	exp_put(exp);
234*4882a593Smuzhiyun 	return nfserrno(host_err);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun /*
238*4882a593Smuzhiyun  * Look up one component of a pathname.
239*4882a593Smuzhiyun  * N.B. After this call _both_ fhp and resfh need an fh_put
240*4882a593Smuzhiyun  *
241*4882a593Smuzhiyun  * If the lookup would cross a mountpoint, and the mounted filesystem
242*4882a593Smuzhiyun  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
243*4882a593Smuzhiyun  * accepted as it stands and the mounted directory is
244*4882a593Smuzhiyun  * returned. Otherwise the covered directory is returned.
245*4882a593Smuzhiyun  * NOTE: this mountpoint crossing is not supported properly by all
246*4882a593Smuzhiyun  *   clients and is explicitly disallowed for NFSv3
247*4882a593Smuzhiyun  *      NeilBrown <neilb@cse.unsw.edu.au>
248*4882a593Smuzhiyun  */
249*4882a593Smuzhiyun __be32
nfsd_lookup(struct svc_rqst * rqstp,struct svc_fh * fhp,const char * name,unsigned int len,struct svc_fh * resfh)250*4882a593Smuzhiyun nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
251*4882a593Smuzhiyun 				unsigned int len, struct svc_fh *resfh)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	struct svc_export	*exp;
254*4882a593Smuzhiyun 	struct dentry		*dentry;
255*4882a593Smuzhiyun 	__be32 err;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
258*4882a593Smuzhiyun 	if (err)
259*4882a593Smuzhiyun 		return err;
260*4882a593Smuzhiyun 	err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
261*4882a593Smuzhiyun 	if (err)
262*4882a593Smuzhiyun 		return err;
263*4882a593Smuzhiyun 	err = check_nfsd_access(exp, rqstp);
264*4882a593Smuzhiyun 	if (err)
265*4882a593Smuzhiyun 		goto out;
266*4882a593Smuzhiyun 	/*
267*4882a593Smuzhiyun 	 * Note: we compose the file handle now, but as the
268*4882a593Smuzhiyun 	 * dentry may be negative, it may need to be updated.
269*4882a593Smuzhiyun 	 */
270*4882a593Smuzhiyun 	err = fh_compose(resfh, exp, dentry, fhp);
271*4882a593Smuzhiyun 	if (!err && d_really_is_negative(dentry))
272*4882a593Smuzhiyun 		err = nfserr_noent;
273*4882a593Smuzhiyun out:
274*4882a593Smuzhiyun 	dput(dentry);
275*4882a593Smuzhiyun 	exp_put(exp);
276*4882a593Smuzhiyun 	return err;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun  * Commit metadata changes to stable storage.
281*4882a593Smuzhiyun  */
282*4882a593Smuzhiyun static int
commit_inode_metadata(struct inode * inode)283*4882a593Smuzhiyun commit_inode_metadata(struct inode *inode)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	const struct export_operations *export_ops = inode->i_sb->s_export_op;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	if (export_ops->commit_metadata)
288*4882a593Smuzhiyun 		return export_ops->commit_metadata(inode);
289*4882a593Smuzhiyun 	return sync_inode_metadata(inode, 1);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun static int
commit_metadata(struct svc_fh * fhp)293*4882a593Smuzhiyun commit_metadata(struct svc_fh *fhp)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	struct inode *inode = d_inode(fhp->fh_dentry);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if (!EX_ISSYNC(fhp->fh_export))
298*4882a593Smuzhiyun 		return 0;
299*4882a593Smuzhiyun 	return commit_inode_metadata(inode);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /*
303*4882a593Smuzhiyun  * Go over the attributes and take care of the small differences between
304*4882a593Smuzhiyun  * NFS semantics and what Linux expects.
305*4882a593Smuzhiyun  */
306*4882a593Smuzhiyun static void
nfsd_sanitize_attrs(struct inode * inode,struct iattr * iap)307*4882a593Smuzhiyun nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	/* sanitize the mode change */
310*4882a593Smuzhiyun 	if (iap->ia_valid & ATTR_MODE) {
311*4882a593Smuzhiyun 		iap->ia_mode &= S_IALLUGO;
312*4882a593Smuzhiyun 		iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	/* Revoke setuid/setgid on chown */
316*4882a593Smuzhiyun 	if (!S_ISDIR(inode->i_mode) &&
317*4882a593Smuzhiyun 	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
318*4882a593Smuzhiyun 		iap->ia_valid |= ATTR_KILL_PRIV;
319*4882a593Smuzhiyun 		if (iap->ia_valid & ATTR_MODE) {
320*4882a593Smuzhiyun 			/* we're setting mode too, just clear the s*id bits */
321*4882a593Smuzhiyun 			iap->ia_mode &= ~S_ISUID;
322*4882a593Smuzhiyun 			if (iap->ia_mode & S_IXGRP)
323*4882a593Smuzhiyun 				iap->ia_mode &= ~S_ISGID;
324*4882a593Smuzhiyun 		} else {
325*4882a593Smuzhiyun 			/* set ATTR_KILL_* bits and let VFS handle it */
326*4882a593Smuzhiyun 			iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
327*4882a593Smuzhiyun 		}
328*4882a593Smuzhiyun 	}
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun static __be32
nfsd_get_write_access(struct svc_rqst * rqstp,struct svc_fh * fhp,struct iattr * iap)332*4882a593Smuzhiyun nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
333*4882a593Smuzhiyun 		struct iattr *iap)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	struct inode *inode = d_inode(fhp->fh_dentry);
336*4882a593Smuzhiyun 	int host_err;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	if (iap->ia_size < inode->i_size) {
339*4882a593Smuzhiyun 		__be32 err;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 		err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
342*4882a593Smuzhiyun 				NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
343*4882a593Smuzhiyun 		if (err)
344*4882a593Smuzhiyun 			return err;
345*4882a593Smuzhiyun 	}
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	host_err = get_write_access(inode);
348*4882a593Smuzhiyun 	if (host_err)
349*4882a593Smuzhiyun 		goto out_nfserrno;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
352*4882a593Smuzhiyun 	if (host_err)
353*4882a593Smuzhiyun 		goto out_put_write_access;
354*4882a593Smuzhiyun 	return 0;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun out_put_write_access:
357*4882a593Smuzhiyun 	put_write_access(inode);
358*4882a593Smuzhiyun out_nfserrno:
359*4882a593Smuzhiyun 	return nfserrno(host_err);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun /*
363*4882a593Smuzhiyun  * Set various file attributes.  After this call fhp needs an fh_put.
364*4882a593Smuzhiyun  */
365*4882a593Smuzhiyun __be32
nfsd_setattr(struct svc_rqst * rqstp,struct svc_fh * fhp,struct iattr * iap,int check_guard,time64_t guardtime)366*4882a593Smuzhiyun nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
367*4882a593Smuzhiyun 	     int check_guard, time64_t guardtime)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun 	struct dentry	*dentry;
370*4882a593Smuzhiyun 	struct inode	*inode;
371*4882a593Smuzhiyun 	int		accmode = NFSD_MAY_SATTR;
372*4882a593Smuzhiyun 	umode_t		ftype = 0;
373*4882a593Smuzhiyun 	__be32		err;
374*4882a593Smuzhiyun 	int		host_err;
375*4882a593Smuzhiyun 	bool		get_write_count;
376*4882a593Smuzhiyun 	bool		size_change = (iap->ia_valid & ATTR_SIZE);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	if (iap->ia_valid & ATTR_SIZE) {
379*4882a593Smuzhiyun 		accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
380*4882a593Smuzhiyun 		ftype = S_IFREG;
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	/*
384*4882a593Smuzhiyun 	 * If utimes(2) and friends are called with times not NULL, we should
385*4882a593Smuzhiyun 	 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
386*4882a593Smuzhiyun 	 * will return EACCES, when the caller's effective UID does not match
387*4882a593Smuzhiyun 	 * the owner of the file, and the caller is not privileged. In this
388*4882a593Smuzhiyun 	 * situation, we should return EPERM(notify_change will return this).
389*4882a593Smuzhiyun 	 */
390*4882a593Smuzhiyun 	if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
391*4882a593Smuzhiyun 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
392*4882a593Smuzhiyun 		if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
393*4882a593Smuzhiyun 			accmode |= NFSD_MAY_WRITE;
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	/* Callers that do fh_verify should do the fh_want_write: */
397*4882a593Smuzhiyun 	get_write_count = !fhp->fh_dentry;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/* Get inode */
400*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, ftype, accmode);
401*4882a593Smuzhiyun 	if (err)
402*4882a593Smuzhiyun 		return err;
403*4882a593Smuzhiyun 	if (get_write_count) {
404*4882a593Smuzhiyun 		host_err = fh_want_write(fhp);
405*4882a593Smuzhiyun 		if (host_err)
406*4882a593Smuzhiyun 			goto out;
407*4882a593Smuzhiyun 	}
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
410*4882a593Smuzhiyun 	inode = d_inode(dentry);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* Ignore any mode updates on symlinks */
413*4882a593Smuzhiyun 	if (S_ISLNK(inode->i_mode))
414*4882a593Smuzhiyun 		iap->ia_valid &= ~ATTR_MODE;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	if (!iap->ia_valid)
417*4882a593Smuzhiyun 		return 0;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	nfsd_sanitize_attrs(inode, iap);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	if (check_guard && guardtime != inode->i_ctime.tv_sec)
422*4882a593Smuzhiyun 		return nfserr_notsync;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	/*
425*4882a593Smuzhiyun 	 * The size case is special, it changes the file in addition to the
426*4882a593Smuzhiyun 	 * attributes, and file systems don't expect it to be mixed with
427*4882a593Smuzhiyun 	 * "random" attribute changes.  We thus split out the size change
428*4882a593Smuzhiyun 	 * into a separate call to ->setattr, and do the rest as a separate
429*4882a593Smuzhiyun 	 * setattr call.
430*4882a593Smuzhiyun 	 */
431*4882a593Smuzhiyun 	if (size_change) {
432*4882a593Smuzhiyun 		err = nfsd_get_write_access(rqstp, fhp, iap);
433*4882a593Smuzhiyun 		if (err)
434*4882a593Smuzhiyun 			return err;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	fh_lock(fhp);
438*4882a593Smuzhiyun 	if (size_change) {
439*4882a593Smuzhiyun 		/*
440*4882a593Smuzhiyun 		 * RFC5661, Section 18.30.4:
441*4882a593Smuzhiyun 		 *   Changing the size of a file with SETATTR indirectly
442*4882a593Smuzhiyun 		 *   changes the time_modify and change attributes.
443*4882a593Smuzhiyun 		 *
444*4882a593Smuzhiyun 		 * (and similar for the older RFCs)
445*4882a593Smuzhiyun 		 */
446*4882a593Smuzhiyun 		struct iattr size_attr = {
447*4882a593Smuzhiyun 			.ia_valid	= ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
448*4882a593Smuzhiyun 			.ia_size	= iap->ia_size,
449*4882a593Smuzhiyun 		};
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 		host_err = notify_change(dentry, &size_attr, NULL);
452*4882a593Smuzhiyun 		if (host_err)
453*4882a593Smuzhiyun 			goto out_unlock;
454*4882a593Smuzhiyun 		iap->ia_valid &= ~ATTR_SIZE;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 		/*
457*4882a593Smuzhiyun 		 * Avoid the additional setattr call below if the only other
458*4882a593Smuzhiyun 		 * attribute that the client sends is the mtime, as we update
459*4882a593Smuzhiyun 		 * it as part of the size change above.
460*4882a593Smuzhiyun 		 */
461*4882a593Smuzhiyun 		if ((iap->ia_valid & ~ATTR_MTIME) == 0)
462*4882a593Smuzhiyun 			goto out_unlock;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	iap->ia_valid |= ATTR_CTIME;
466*4882a593Smuzhiyun 	host_err = notify_change(dentry, iap, NULL);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun out_unlock:
469*4882a593Smuzhiyun 	fh_unlock(fhp);
470*4882a593Smuzhiyun 	if (size_change)
471*4882a593Smuzhiyun 		put_write_access(inode);
472*4882a593Smuzhiyun out:
473*4882a593Smuzhiyun 	if (!host_err)
474*4882a593Smuzhiyun 		host_err = commit_metadata(fhp);
475*4882a593Smuzhiyun 	return nfserrno(host_err);
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun #if defined(CONFIG_NFSD_V4)
479*4882a593Smuzhiyun /*
480*4882a593Smuzhiyun  * NFS junction information is stored in an extended attribute.
481*4882a593Smuzhiyun  */
482*4882a593Smuzhiyun #define NFSD_JUNCTION_XATTR_NAME	XATTR_TRUSTED_PREFIX "junction.nfs"
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun /**
485*4882a593Smuzhiyun  * nfsd4_is_junction - Test if an object could be an NFS junction
486*4882a593Smuzhiyun  *
487*4882a593Smuzhiyun  * @dentry: object to test
488*4882a593Smuzhiyun  *
489*4882a593Smuzhiyun  * Returns 1 if "dentry" appears to contain NFS junction information.
490*4882a593Smuzhiyun  * Otherwise 0 is returned.
491*4882a593Smuzhiyun  */
nfsd4_is_junction(struct dentry * dentry)492*4882a593Smuzhiyun int nfsd4_is_junction(struct dentry *dentry)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	struct inode *inode = d_inode(dentry);
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	if (inode == NULL)
497*4882a593Smuzhiyun 		return 0;
498*4882a593Smuzhiyun 	if (inode->i_mode & S_IXUGO)
499*4882a593Smuzhiyun 		return 0;
500*4882a593Smuzhiyun 	if (!(inode->i_mode & S_ISVTX))
501*4882a593Smuzhiyun 		return 0;
502*4882a593Smuzhiyun 	if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
503*4882a593Smuzhiyun 		return 0;
504*4882a593Smuzhiyun 	return 1;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
nfsd4_set_nfs4_label(struct svc_rqst * rqstp,struct svc_fh * fhp,struct xdr_netobj * label)507*4882a593Smuzhiyun __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
508*4882a593Smuzhiyun 		struct xdr_netobj *label)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	__be32 error;
511*4882a593Smuzhiyun 	int host_error;
512*4882a593Smuzhiyun 	struct dentry *dentry;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
515*4882a593Smuzhiyun 	if (error)
516*4882a593Smuzhiyun 		return error;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	inode_lock(d_inode(dentry));
521*4882a593Smuzhiyun 	host_error = security_inode_setsecctx(dentry, label->data, label->len);
522*4882a593Smuzhiyun 	inode_unlock(d_inode(dentry));
523*4882a593Smuzhiyun 	return nfserrno(host_error);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun #else
nfsd4_set_nfs4_label(struct svc_rqst * rqstp,struct svc_fh * fhp,struct xdr_netobj * label)526*4882a593Smuzhiyun __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
527*4882a593Smuzhiyun 		struct xdr_netobj *label)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	return nfserr_notsupp;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun #endif
532*4882a593Smuzhiyun 
nfsd4_clone_file_range(struct nfsd_file * nf_src,u64 src_pos,struct nfsd_file * nf_dst,u64 dst_pos,u64 count,bool sync)533*4882a593Smuzhiyun __be32 nfsd4_clone_file_range(struct nfsd_file *nf_src, u64 src_pos,
534*4882a593Smuzhiyun 		struct nfsd_file *nf_dst, u64 dst_pos, u64 count, bool sync)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	struct file *src = nf_src->nf_file;
537*4882a593Smuzhiyun 	struct file *dst = nf_dst->nf_file;
538*4882a593Smuzhiyun 	errseq_t since;
539*4882a593Smuzhiyun 	loff_t cloned;
540*4882a593Smuzhiyun 	__be32 ret = 0;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	since = READ_ONCE(dst->f_wb_err);
543*4882a593Smuzhiyun 	cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
544*4882a593Smuzhiyun 	if (cloned < 0) {
545*4882a593Smuzhiyun 		ret = nfserrno(cloned);
546*4882a593Smuzhiyun 		goto out_err;
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 	if (count && cloned != count) {
549*4882a593Smuzhiyun 		ret = nfserrno(-EINVAL);
550*4882a593Smuzhiyun 		goto out_err;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 	if (sync) {
553*4882a593Smuzhiyun 		loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
554*4882a593Smuzhiyun 		int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 		if (!status)
557*4882a593Smuzhiyun 			status = filemap_check_wb_err(dst->f_mapping, since);
558*4882a593Smuzhiyun 		if (!status)
559*4882a593Smuzhiyun 			status = commit_inode_metadata(file_inode(src));
560*4882a593Smuzhiyun 		if (status < 0) {
561*4882a593Smuzhiyun 			nfsd_reset_boot_verifier(net_generic(nf_dst->nf_net,
562*4882a593Smuzhiyun 						 nfsd_net_id));
563*4882a593Smuzhiyun 			ret = nfserrno(status);
564*4882a593Smuzhiyun 		}
565*4882a593Smuzhiyun 	}
566*4882a593Smuzhiyun out_err:
567*4882a593Smuzhiyun 	return ret;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun 
nfsd_copy_file_range(struct file * src,u64 src_pos,struct file * dst,u64 dst_pos,u64 count)570*4882a593Smuzhiyun ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
571*4882a593Smuzhiyun 			     u64 dst_pos, u64 count)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun 	ssize_t ret;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	/*
576*4882a593Smuzhiyun 	 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
577*4882a593Smuzhiyun 	 * thread and client rpc slot.  The choice of 4MB is somewhat
578*4882a593Smuzhiyun 	 * arbitrary.  We might instead base this on r/wsize, or make it
579*4882a593Smuzhiyun 	 * tunable, or use a time instead of a byte limit, or implement
580*4882a593Smuzhiyun 	 * asynchronous copy.  In theory a client could also recognize a
581*4882a593Smuzhiyun 	 * limit like this and pipeline multiple COPY requests.
582*4882a593Smuzhiyun 	 */
583*4882a593Smuzhiyun 	count = min_t(u64, count, 1 << 22);
584*4882a593Smuzhiyun 	ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	if (ret == -EOPNOTSUPP || ret == -EXDEV)
587*4882a593Smuzhiyun 		ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count,
588*4882a593Smuzhiyun 					  COPY_FILE_SPLICE);
589*4882a593Smuzhiyun 	return ret;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun 
nfsd4_vfs_fallocate(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file * file,loff_t offset,loff_t len,int flags)592*4882a593Smuzhiyun __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
593*4882a593Smuzhiyun 			   struct file *file, loff_t offset, loff_t len,
594*4882a593Smuzhiyun 			   int flags)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun 	int error;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	if (!S_ISREG(file_inode(file)->i_mode))
599*4882a593Smuzhiyun 		return nfserr_inval;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	error = vfs_fallocate(file, flags, offset, len);
602*4882a593Smuzhiyun 	if (!error)
603*4882a593Smuzhiyun 		error = commit_metadata(fhp);
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	return nfserrno(error);
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun #endif /* defined(CONFIG_NFSD_V4) */
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V3
610*4882a593Smuzhiyun /*
611*4882a593Smuzhiyun  * Check server access rights to a file system object
612*4882a593Smuzhiyun  */
613*4882a593Smuzhiyun struct accessmap {
614*4882a593Smuzhiyun 	u32		access;
615*4882a593Smuzhiyun 	int		how;
616*4882a593Smuzhiyun };
617*4882a593Smuzhiyun static struct accessmap	nfs3_regaccess[] = {
618*4882a593Smuzhiyun     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
619*4882a593Smuzhiyun     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
620*4882a593Smuzhiyun     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_TRUNC	},
621*4882a593Smuzhiyun     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE			},
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V4
624*4882a593Smuzhiyun     {	NFS4_ACCESS_XAREAD,	NFSD_MAY_READ			},
625*4882a593Smuzhiyun     {	NFS4_ACCESS_XAWRITE,	NFSD_MAY_WRITE			},
626*4882a593Smuzhiyun     {	NFS4_ACCESS_XALIST,	NFSD_MAY_READ			},
627*4882a593Smuzhiyun #endif
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun     {	0,			0				}
630*4882a593Smuzhiyun };
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun static struct accessmap	nfs3_diraccess[] = {
633*4882a593Smuzhiyun     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
634*4882a593Smuzhiyun     {	NFS3_ACCESS_LOOKUP,	NFSD_MAY_EXEC			},
635*4882a593Smuzhiyun     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
636*4882a593Smuzhiyun     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_EXEC|NFSD_MAY_WRITE	},
637*4882a593Smuzhiyun     {	NFS3_ACCESS_DELETE,	NFSD_MAY_REMOVE			},
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V4
640*4882a593Smuzhiyun     {	NFS4_ACCESS_XAREAD,	NFSD_MAY_READ			},
641*4882a593Smuzhiyun     {	NFS4_ACCESS_XAWRITE,	NFSD_MAY_WRITE			},
642*4882a593Smuzhiyun     {	NFS4_ACCESS_XALIST,	NFSD_MAY_READ			},
643*4882a593Smuzhiyun #endif
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun     {	0,			0				}
646*4882a593Smuzhiyun };
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun static struct accessmap	nfs3_anyaccess[] = {
649*4882a593Smuzhiyun 	/* Some clients - Solaris 2.6 at least, make an access call
650*4882a593Smuzhiyun 	 * to the server to check for access for things like /dev/null
651*4882a593Smuzhiyun 	 * (which really, the server doesn't care about).  So
652*4882a593Smuzhiyun 	 * We provide simple access checking for them, looking
653*4882a593Smuzhiyun 	 * mainly at mode bits, and we make sure to ignore read-only
654*4882a593Smuzhiyun 	 * filesystem checks
655*4882a593Smuzhiyun 	 */
656*4882a593Smuzhiyun     {	NFS3_ACCESS_READ,	NFSD_MAY_READ			},
657*4882a593Smuzhiyun     {	NFS3_ACCESS_EXECUTE,	NFSD_MAY_EXEC			},
658*4882a593Smuzhiyun     {	NFS3_ACCESS_MODIFY,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
659*4882a593Smuzhiyun     {	NFS3_ACCESS_EXTEND,	NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS	},
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun     {	0,			0				}
662*4882a593Smuzhiyun };
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun __be32
nfsd_access(struct svc_rqst * rqstp,struct svc_fh * fhp,u32 * access,u32 * supported)665*4882a593Smuzhiyun nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun 	struct accessmap	*map;
668*4882a593Smuzhiyun 	struct svc_export	*export;
669*4882a593Smuzhiyun 	struct dentry		*dentry;
670*4882a593Smuzhiyun 	u32			query, result = 0, sresult = 0;
671*4882a593Smuzhiyun 	__be32			error;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
674*4882a593Smuzhiyun 	if (error)
675*4882a593Smuzhiyun 		goto out;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	export = fhp->fh_export;
678*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	if (d_is_reg(dentry))
681*4882a593Smuzhiyun 		map = nfs3_regaccess;
682*4882a593Smuzhiyun 	else if (d_is_dir(dentry))
683*4882a593Smuzhiyun 		map = nfs3_diraccess;
684*4882a593Smuzhiyun 	else
685*4882a593Smuzhiyun 		map = nfs3_anyaccess;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	query = *access;
689*4882a593Smuzhiyun 	for  (; map->access; map++) {
690*4882a593Smuzhiyun 		if (map->access & query) {
691*4882a593Smuzhiyun 			__be32 err2;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 			sresult |= map->access;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 			err2 = nfsd_permission(rqstp, export, dentry, map->how);
696*4882a593Smuzhiyun 			switch (err2) {
697*4882a593Smuzhiyun 			case nfs_ok:
698*4882a593Smuzhiyun 				result |= map->access;
699*4882a593Smuzhiyun 				break;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 			/* the following error codes just mean the access was not allowed,
702*4882a593Smuzhiyun 			 * rather than an error occurred */
703*4882a593Smuzhiyun 			case nfserr_rofs:
704*4882a593Smuzhiyun 			case nfserr_acces:
705*4882a593Smuzhiyun 			case nfserr_perm:
706*4882a593Smuzhiyun 				/* simply don't "or" in the access bit. */
707*4882a593Smuzhiyun 				break;
708*4882a593Smuzhiyun 			default:
709*4882a593Smuzhiyun 				error = err2;
710*4882a593Smuzhiyun 				goto out;
711*4882a593Smuzhiyun 			}
712*4882a593Smuzhiyun 		}
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 	*access = result;
715*4882a593Smuzhiyun 	if (supported)
716*4882a593Smuzhiyun 		*supported = sresult;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun  out:
719*4882a593Smuzhiyun 	return error;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun #endif /* CONFIG_NFSD_V3 */
722*4882a593Smuzhiyun 
nfsd_open_break_lease(struct inode * inode,int access)723*4882a593Smuzhiyun int nfsd_open_break_lease(struct inode *inode, int access)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	unsigned int mode;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	if (access & NFSD_MAY_NOT_BREAK_LEASE)
728*4882a593Smuzhiyun 		return 0;
729*4882a593Smuzhiyun 	mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
730*4882a593Smuzhiyun 	return break_lease(inode, mode | O_NONBLOCK);
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun /*
734*4882a593Smuzhiyun  * Open an existing file or directory.
735*4882a593Smuzhiyun  * The may_flags argument indicates the type of open (read/write/lock)
736*4882a593Smuzhiyun  * and additional flags.
737*4882a593Smuzhiyun  * N.B. After this call fhp needs an fh_put
738*4882a593Smuzhiyun  */
739*4882a593Smuzhiyun static __be32
__nfsd_open(struct svc_rqst * rqstp,struct svc_fh * fhp,umode_t type,int may_flags,struct file ** filp)740*4882a593Smuzhiyun __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
741*4882a593Smuzhiyun 			int may_flags, struct file **filp)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun 	struct path	path;
744*4882a593Smuzhiyun 	struct inode	*inode;
745*4882a593Smuzhiyun 	struct file	*file;
746*4882a593Smuzhiyun 	int		flags = O_RDONLY|O_LARGEFILE;
747*4882a593Smuzhiyun 	__be32		err;
748*4882a593Smuzhiyun 	int		host_err = 0;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	path.mnt = fhp->fh_export->ex_path.mnt;
751*4882a593Smuzhiyun 	path.dentry = fhp->fh_dentry;
752*4882a593Smuzhiyun 	inode = d_inode(path.dentry);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	/* Disallow write access to files with the append-only bit set
755*4882a593Smuzhiyun 	 * or any access when mandatory locking enabled
756*4882a593Smuzhiyun 	 */
757*4882a593Smuzhiyun 	err = nfserr_perm;
758*4882a593Smuzhiyun 	if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
759*4882a593Smuzhiyun 		goto out;
760*4882a593Smuzhiyun 	/*
761*4882a593Smuzhiyun 	 * We must ignore files (but only files) which might have mandatory
762*4882a593Smuzhiyun 	 * locks on them because there is no way to know if the accesser has
763*4882a593Smuzhiyun 	 * the lock.
764*4882a593Smuzhiyun 	 */
765*4882a593Smuzhiyun 	if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
766*4882a593Smuzhiyun 		goto out;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	if (!inode->i_fop)
769*4882a593Smuzhiyun 		goto out;
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	host_err = nfsd_open_break_lease(inode, may_flags);
772*4882a593Smuzhiyun 	if (host_err) /* NOMEM or WOULDBLOCK */
773*4882a593Smuzhiyun 		goto out_nfserr;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	if (may_flags & NFSD_MAY_WRITE) {
776*4882a593Smuzhiyun 		if (may_flags & NFSD_MAY_READ)
777*4882a593Smuzhiyun 			flags = O_RDWR|O_LARGEFILE;
778*4882a593Smuzhiyun 		else
779*4882a593Smuzhiyun 			flags = O_WRONLY|O_LARGEFILE;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	file = dentry_open(&path, flags, current_cred());
783*4882a593Smuzhiyun 	if (IS_ERR(file)) {
784*4882a593Smuzhiyun 		host_err = PTR_ERR(file);
785*4882a593Smuzhiyun 		goto out_nfserr;
786*4882a593Smuzhiyun 	}
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	host_err = ima_file_check(file, may_flags);
789*4882a593Smuzhiyun 	if (host_err) {
790*4882a593Smuzhiyun 		fput(file);
791*4882a593Smuzhiyun 		goto out_nfserr;
792*4882a593Smuzhiyun 	}
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	if (may_flags & NFSD_MAY_64BIT_COOKIE)
795*4882a593Smuzhiyun 		file->f_mode |= FMODE_64BITHASH;
796*4882a593Smuzhiyun 	else
797*4882a593Smuzhiyun 		file->f_mode |= FMODE_32BITHASH;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	*filp = file;
800*4882a593Smuzhiyun out_nfserr:
801*4882a593Smuzhiyun 	err = nfserrno(host_err);
802*4882a593Smuzhiyun out:
803*4882a593Smuzhiyun 	return err;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun __be32
nfsd_open(struct svc_rqst * rqstp,struct svc_fh * fhp,umode_t type,int may_flags,struct file ** filp)807*4882a593Smuzhiyun nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
808*4882a593Smuzhiyun 		int may_flags, struct file **filp)
809*4882a593Smuzhiyun {
810*4882a593Smuzhiyun 	__be32 err;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	validate_process_creds();
813*4882a593Smuzhiyun 	/*
814*4882a593Smuzhiyun 	 * If we get here, then the client has already done an "open",
815*4882a593Smuzhiyun 	 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
816*4882a593Smuzhiyun 	 * in case a chmod has now revoked permission.
817*4882a593Smuzhiyun 	 *
818*4882a593Smuzhiyun 	 * Arguably we should also allow the owner override for
819*4882a593Smuzhiyun 	 * directories, but we never have and it doesn't seem to have
820*4882a593Smuzhiyun 	 * caused anyone a problem.  If we were to change this, note
821*4882a593Smuzhiyun 	 * also that our filldir callbacks would need a variant of
822*4882a593Smuzhiyun 	 * lookup_one_len that doesn't check permissions.
823*4882a593Smuzhiyun 	 */
824*4882a593Smuzhiyun 	if (type == S_IFREG)
825*4882a593Smuzhiyun 		may_flags |= NFSD_MAY_OWNER_OVERRIDE;
826*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, type, may_flags);
827*4882a593Smuzhiyun 	if (!err)
828*4882a593Smuzhiyun 		err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
829*4882a593Smuzhiyun 	validate_process_creds();
830*4882a593Smuzhiyun 	return err;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun __be32
nfsd_open_verified(struct svc_rqst * rqstp,struct svc_fh * fhp,umode_t type,int may_flags,struct file ** filp)834*4882a593Smuzhiyun nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
835*4882a593Smuzhiyun 		int may_flags, struct file **filp)
836*4882a593Smuzhiyun {
837*4882a593Smuzhiyun 	__be32 err;
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	validate_process_creds();
840*4882a593Smuzhiyun 	err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
841*4882a593Smuzhiyun 	validate_process_creds();
842*4882a593Smuzhiyun 	return err;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun /*
846*4882a593Smuzhiyun  * Grab and keep cached pages associated with a file in the svc_rqst
847*4882a593Smuzhiyun  * so that they can be passed to the network sendmsg/sendpage routines
848*4882a593Smuzhiyun  * directly. They will be released after the sending has completed.
849*4882a593Smuzhiyun  */
850*4882a593Smuzhiyun static int
nfsd_splice_actor(struct pipe_inode_info * pipe,struct pipe_buffer * buf,struct splice_desc * sd)851*4882a593Smuzhiyun nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
852*4882a593Smuzhiyun 		  struct splice_desc *sd)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	struct svc_rqst *rqstp = sd->u.data;
855*4882a593Smuzhiyun 	struct page **pp = rqstp->rq_next_page;
856*4882a593Smuzhiyun 	struct page *page = buf->page;
857*4882a593Smuzhiyun 	size_t size;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	size = sd->len;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	if (rqstp->rq_res.page_len == 0) {
862*4882a593Smuzhiyun 		get_page(page);
863*4882a593Smuzhiyun 		put_page(*rqstp->rq_next_page);
864*4882a593Smuzhiyun 		*(rqstp->rq_next_page++) = page;
865*4882a593Smuzhiyun 		rqstp->rq_res.page_base = buf->offset;
866*4882a593Smuzhiyun 		rqstp->rq_res.page_len = size;
867*4882a593Smuzhiyun 	} else if (page != pp[-1]) {
868*4882a593Smuzhiyun 		get_page(page);
869*4882a593Smuzhiyun 		if (*rqstp->rq_next_page)
870*4882a593Smuzhiyun 			put_page(*rqstp->rq_next_page);
871*4882a593Smuzhiyun 		*(rqstp->rq_next_page++) = page;
872*4882a593Smuzhiyun 		rqstp->rq_res.page_len += size;
873*4882a593Smuzhiyun 	} else
874*4882a593Smuzhiyun 		rqstp->rq_res.page_len += size;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	return size;
877*4882a593Smuzhiyun }
878*4882a593Smuzhiyun 
nfsd_direct_splice_actor(struct pipe_inode_info * pipe,struct splice_desc * sd)879*4882a593Smuzhiyun static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
880*4882a593Smuzhiyun 				    struct splice_desc *sd)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun 	return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun 
nfsd_eof_on_read(struct file * file,loff_t offset,ssize_t len,size_t expected)885*4882a593Smuzhiyun static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
886*4882a593Smuzhiyun 		size_t expected)
887*4882a593Smuzhiyun {
888*4882a593Smuzhiyun 	if (expected != 0 && len == 0)
889*4882a593Smuzhiyun 		return 1;
890*4882a593Smuzhiyun 	if (offset+len >= i_size_read(file_inode(file)))
891*4882a593Smuzhiyun 		return 1;
892*4882a593Smuzhiyun 	return 0;
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun 
nfsd_finish_read(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file * file,loff_t offset,unsigned long * count,u32 * eof,ssize_t host_err)895*4882a593Smuzhiyun static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
896*4882a593Smuzhiyun 			       struct file *file, loff_t offset,
897*4882a593Smuzhiyun 			       unsigned long *count, u32 *eof, ssize_t host_err)
898*4882a593Smuzhiyun {
899*4882a593Smuzhiyun 	if (host_err >= 0) {
900*4882a593Smuzhiyun 		nfsdstats.io_read += host_err;
901*4882a593Smuzhiyun 		*eof = nfsd_eof_on_read(file, offset, host_err, *count);
902*4882a593Smuzhiyun 		*count = host_err;
903*4882a593Smuzhiyun 		fsnotify_access(file);
904*4882a593Smuzhiyun 		trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
905*4882a593Smuzhiyun 		return 0;
906*4882a593Smuzhiyun 	} else {
907*4882a593Smuzhiyun 		trace_nfsd_read_err(rqstp, fhp, offset, host_err);
908*4882a593Smuzhiyun 		return nfserrno(host_err);
909*4882a593Smuzhiyun 	}
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun 
nfsd_splice_read(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file * file,loff_t offset,unsigned long * count,u32 * eof)912*4882a593Smuzhiyun __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
913*4882a593Smuzhiyun 			struct file *file, loff_t offset, unsigned long *count,
914*4882a593Smuzhiyun 			u32 *eof)
915*4882a593Smuzhiyun {
916*4882a593Smuzhiyun 	struct splice_desc sd = {
917*4882a593Smuzhiyun 		.len		= 0,
918*4882a593Smuzhiyun 		.total_len	= *count,
919*4882a593Smuzhiyun 		.pos		= offset,
920*4882a593Smuzhiyun 		.u.data		= rqstp,
921*4882a593Smuzhiyun 	};
922*4882a593Smuzhiyun 	ssize_t host_err;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	trace_nfsd_read_splice(rqstp, fhp, offset, *count);
925*4882a593Smuzhiyun 	rqstp->rq_next_page = rqstp->rq_respages + 1;
926*4882a593Smuzhiyun 	host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
927*4882a593Smuzhiyun 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun 
nfsd_readv(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file * file,loff_t offset,struct kvec * vec,int vlen,unsigned long * count,u32 * eof)930*4882a593Smuzhiyun __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
931*4882a593Smuzhiyun 		  struct file *file, loff_t offset,
932*4882a593Smuzhiyun 		  struct kvec *vec, int vlen, unsigned long *count,
933*4882a593Smuzhiyun 		  u32 *eof)
934*4882a593Smuzhiyun {
935*4882a593Smuzhiyun 	struct iov_iter iter;
936*4882a593Smuzhiyun 	loff_t ppos = offset;
937*4882a593Smuzhiyun 	ssize_t host_err;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	trace_nfsd_read_vector(rqstp, fhp, offset, *count);
940*4882a593Smuzhiyun 	iov_iter_kvec(&iter, READ, vec, vlen, *count);
941*4882a593Smuzhiyun 	host_err = vfs_iter_read(file, &iter, &ppos, 0);
942*4882a593Smuzhiyun 	return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun /*
946*4882a593Smuzhiyun  * Gathered writes: If another process is currently writing to the file,
947*4882a593Smuzhiyun  * there's a high chance this is another nfsd (triggered by a bulk write
948*4882a593Smuzhiyun  * from a client's biod). Rather than syncing the file with each write
949*4882a593Smuzhiyun  * request, we sleep for 10 msec.
950*4882a593Smuzhiyun  *
951*4882a593Smuzhiyun  * I don't know if this roughly approximates C. Juszak's idea of
952*4882a593Smuzhiyun  * gathered writes, but it's a nice and simple solution (IMHO), and it
953*4882a593Smuzhiyun  * seems to work:-)
954*4882a593Smuzhiyun  *
955*4882a593Smuzhiyun  * Note: we do this only in the NFSv2 case, since v3 and higher have a
956*4882a593Smuzhiyun  * better tool (separate unstable writes and commits) for solving this
957*4882a593Smuzhiyun  * problem.
958*4882a593Smuzhiyun  */
wait_for_concurrent_writes(struct file * file)959*4882a593Smuzhiyun static int wait_for_concurrent_writes(struct file *file)
960*4882a593Smuzhiyun {
961*4882a593Smuzhiyun 	struct inode *inode = file_inode(file);
962*4882a593Smuzhiyun 	static ino_t last_ino;
963*4882a593Smuzhiyun 	static dev_t last_dev;
964*4882a593Smuzhiyun 	int err = 0;
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	if (atomic_read(&inode->i_writecount) > 1
967*4882a593Smuzhiyun 	    || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
968*4882a593Smuzhiyun 		dprintk("nfsd: write defer %d\n", task_pid_nr(current));
969*4882a593Smuzhiyun 		msleep(10);
970*4882a593Smuzhiyun 		dprintk("nfsd: write resume %d\n", task_pid_nr(current));
971*4882a593Smuzhiyun 	}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	if (inode->i_state & I_DIRTY) {
974*4882a593Smuzhiyun 		dprintk("nfsd: write sync %d\n", task_pid_nr(current));
975*4882a593Smuzhiyun 		err = vfs_fsync(file, 0);
976*4882a593Smuzhiyun 	}
977*4882a593Smuzhiyun 	last_ino = inode->i_ino;
978*4882a593Smuzhiyun 	last_dev = inode->i_sb->s_dev;
979*4882a593Smuzhiyun 	return err;
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun __be32
nfsd_vfs_write(struct svc_rqst * rqstp,struct svc_fh * fhp,struct nfsd_file * nf,loff_t offset,struct kvec * vec,int vlen,unsigned long * cnt,int stable,__be32 * verf)983*4882a593Smuzhiyun nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
984*4882a593Smuzhiyun 				loff_t offset, struct kvec *vec, int vlen,
985*4882a593Smuzhiyun 				unsigned long *cnt, int stable,
986*4882a593Smuzhiyun 				__be32 *verf)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun 	struct file		*file = nf->nf_file;
989*4882a593Smuzhiyun 	struct svc_export	*exp;
990*4882a593Smuzhiyun 	struct iov_iter		iter;
991*4882a593Smuzhiyun 	errseq_t		since;
992*4882a593Smuzhiyun 	__be32			nfserr;
993*4882a593Smuzhiyun 	int			host_err;
994*4882a593Smuzhiyun 	int			use_wgather;
995*4882a593Smuzhiyun 	loff_t			pos = offset;
996*4882a593Smuzhiyun 	unsigned int		pflags = current->flags;
997*4882a593Smuzhiyun 	rwf_t			flags = 0;
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
1002*4882a593Smuzhiyun 		/*
1003*4882a593Smuzhiyun 		 * We want throttling in balance_dirty_pages()
1004*4882a593Smuzhiyun 		 * and shrink_inactive_list() to only consider
1005*4882a593Smuzhiyun 		 * the backingdev we are writing to, so that nfs to
1006*4882a593Smuzhiyun 		 * localhost doesn't cause nfsd to lock up due to all
1007*4882a593Smuzhiyun 		 * the client's dirty pages or its congested queue.
1008*4882a593Smuzhiyun 		 */
1009*4882a593Smuzhiyun 		current->flags |= PF_LOCAL_THROTTLE;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	exp = fhp->fh_export;
1012*4882a593Smuzhiyun 	use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	if (!EX_ISSYNC(exp))
1015*4882a593Smuzhiyun 		stable = NFS_UNSTABLE;
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	if (stable && !use_wgather)
1018*4882a593Smuzhiyun 		flags |= RWF_SYNC;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
1021*4882a593Smuzhiyun 	since = READ_ONCE(file->f_wb_err);
1022*4882a593Smuzhiyun 	if (flags & RWF_SYNC) {
1023*4882a593Smuzhiyun 		if (verf)
1024*4882a593Smuzhiyun 			nfsd_copy_boot_verifier(verf,
1025*4882a593Smuzhiyun 					net_generic(SVC_NET(rqstp),
1026*4882a593Smuzhiyun 					nfsd_net_id));
1027*4882a593Smuzhiyun 		host_err = vfs_iter_write(file, &iter, &pos, flags);
1028*4882a593Smuzhiyun 		if (host_err < 0)
1029*4882a593Smuzhiyun 			nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
1030*4882a593Smuzhiyun 						 nfsd_net_id));
1031*4882a593Smuzhiyun 	} else {
1032*4882a593Smuzhiyun 		if (verf)
1033*4882a593Smuzhiyun 			nfsd_copy_boot_verifier(verf,
1034*4882a593Smuzhiyun 					net_generic(SVC_NET(rqstp),
1035*4882a593Smuzhiyun 					nfsd_net_id));
1036*4882a593Smuzhiyun 		host_err = vfs_iter_write(file, &iter, &pos, flags);
1037*4882a593Smuzhiyun 	}
1038*4882a593Smuzhiyun 	if (host_err < 0) {
1039*4882a593Smuzhiyun 		nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
1040*4882a593Smuzhiyun 					 nfsd_net_id));
1041*4882a593Smuzhiyun 		goto out_nfserr;
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 	*cnt = host_err;
1044*4882a593Smuzhiyun 	nfsdstats.io_write += *cnt;
1045*4882a593Smuzhiyun 	fsnotify_modify(file);
1046*4882a593Smuzhiyun 	host_err = filemap_check_wb_err(file->f_mapping, since);
1047*4882a593Smuzhiyun 	if (host_err < 0)
1048*4882a593Smuzhiyun 		goto out_nfserr;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	if (stable && use_wgather) {
1051*4882a593Smuzhiyun 		host_err = wait_for_concurrent_writes(file);
1052*4882a593Smuzhiyun 		if (host_err < 0)
1053*4882a593Smuzhiyun 			nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
1054*4882a593Smuzhiyun 						 nfsd_net_id));
1055*4882a593Smuzhiyun 	}
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun out_nfserr:
1058*4882a593Smuzhiyun 	if (host_err >= 0) {
1059*4882a593Smuzhiyun 		trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1060*4882a593Smuzhiyun 		nfserr = nfs_ok;
1061*4882a593Smuzhiyun 	} else {
1062*4882a593Smuzhiyun 		trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1063*4882a593Smuzhiyun 		nfserr = nfserrno(host_err);
1064*4882a593Smuzhiyun 	}
1065*4882a593Smuzhiyun 	if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
1066*4882a593Smuzhiyun 		current_restore_flags(pflags, PF_LOCAL_THROTTLE);
1067*4882a593Smuzhiyun 	return nfserr;
1068*4882a593Smuzhiyun }
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun /*
1071*4882a593Smuzhiyun  * Read data from a file. count must contain the requested read count
1072*4882a593Smuzhiyun  * on entry. On return, *count contains the number of bytes actually read.
1073*4882a593Smuzhiyun  * N.B. After this call fhp needs an fh_put
1074*4882a593Smuzhiyun  */
nfsd_read(struct svc_rqst * rqstp,struct svc_fh * fhp,loff_t offset,struct kvec * vec,int vlen,unsigned long * count,u32 * eof)1075*4882a593Smuzhiyun __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1076*4882a593Smuzhiyun 	loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
1077*4882a593Smuzhiyun 	u32 *eof)
1078*4882a593Smuzhiyun {
1079*4882a593Smuzhiyun 	struct nfsd_file	*nf;
1080*4882a593Smuzhiyun 	struct file *file;
1081*4882a593Smuzhiyun 	__be32 err;
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	trace_nfsd_read_start(rqstp, fhp, offset, *count);
1084*4882a593Smuzhiyun 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
1085*4882a593Smuzhiyun 	if (err)
1086*4882a593Smuzhiyun 		return err;
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	file = nf->nf_file;
1089*4882a593Smuzhiyun 	if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
1090*4882a593Smuzhiyun 		err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
1091*4882a593Smuzhiyun 	else
1092*4882a593Smuzhiyun 		err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	nfsd_file_put(nf);
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	trace_nfsd_read_done(rqstp, fhp, offset, *count);
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	return err;
1099*4882a593Smuzhiyun }
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun /*
1102*4882a593Smuzhiyun  * Write data to a file.
1103*4882a593Smuzhiyun  * The stable flag requests synchronous writes.
1104*4882a593Smuzhiyun  * N.B. After this call fhp needs an fh_put
1105*4882a593Smuzhiyun  */
1106*4882a593Smuzhiyun __be32
nfsd_write(struct svc_rqst * rqstp,struct svc_fh * fhp,loff_t offset,struct kvec * vec,int vlen,unsigned long * cnt,int stable,__be32 * verf)1107*4882a593Smuzhiyun nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1108*4882a593Smuzhiyun 	   struct kvec *vec, int vlen, unsigned long *cnt, int stable,
1109*4882a593Smuzhiyun 	   __be32 *verf)
1110*4882a593Smuzhiyun {
1111*4882a593Smuzhiyun 	struct nfsd_file *nf;
1112*4882a593Smuzhiyun 	__be32 err;
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun 	trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
1117*4882a593Smuzhiyun 	if (err)
1118*4882a593Smuzhiyun 		goto out;
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
1121*4882a593Smuzhiyun 			vlen, cnt, stable, verf);
1122*4882a593Smuzhiyun 	nfsd_file_put(nf);
1123*4882a593Smuzhiyun out:
1124*4882a593Smuzhiyun 	trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
1125*4882a593Smuzhiyun 	return err;
1126*4882a593Smuzhiyun }
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V3
1129*4882a593Smuzhiyun /*
1130*4882a593Smuzhiyun  * Commit all pending writes to stable storage.
1131*4882a593Smuzhiyun  *
1132*4882a593Smuzhiyun  * Note: we only guarantee that data that lies within the range specified
1133*4882a593Smuzhiyun  * by the 'offset' and 'count' parameters will be synced.
1134*4882a593Smuzhiyun  *
1135*4882a593Smuzhiyun  * Unfortunately we cannot lock the file to make sure we return full WCC
1136*4882a593Smuzhiyun  * data to the client, as locking happens lower down in the filesystem.
1137*4882a593Smuzhiyun  */
1138*4882a593Smuzhiyun __be32
nfsd_commit(struct svc_rqst * rqstp,struct svc_fh * fhp,loff_t offset,unsigned long count,__be32 * verf)1139*4882a593Smuzhiyun nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1140*4882a593Smuzhiyun                loff_t offset, unsigned long count, __be32 *verf)
1141*4882a593Smuzhiyun {
1142*4882a593Smuzhiyun 	struct nfsd_file	*nf;
1143*4882a593Smuzhiyun 	loff_t			end = LLONG_MAX;
1144*4882a593Smuzhiyun 	__be32			err = nfserr_inval;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	if (offset < 0)
1147*4882a593Smuzhiyun 		goto out;
1148*4882a593Smuzhiyun 	if (count != 0) {
1149*4882a593Smuzhiyun 		end = offset + (loff_t)count - 1;
1150*4882a593Smuzhiyun 		if (end < offset)
1151*4882a593Smuzhiyun 			goto out;
1152*4882a593Smuzhiyun 	}
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	err = nfsd_file_acquire(rqstp, fhp,
1155*4882a593Smuzhiyun 			NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
1156*4882a593Smuzhiyun 	if (err)
1157*4882a593Smuzhiyun 		goto out;
1158*4882a593Smuzhiyun 	if (EX_ISSYNC(fhp->fh_export)) {
1159*4882a593Smuzhiyun 		errseq_t since = READ_ONCE(nf->nf_file->f_wb_err);
1160*4882a593Smuzhiyun 		int err2;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 		err2 = vfs_fsync_range(nf->nf_file, offset, end, 0);
1163*4882a593Smuzhiyun 		switch (err2) {
1164*4882a593Smuzhiyun 		case 0:
1165*4882a593Smuzhiyun 			nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
1166*4882a593Smuzhiyun 						nfsd_net_id));
1167*4882a593Smuzhiyun 			err2 = filemap_check_wb_err(nf->nf_file->f_mapping,
1168*4882a593Smuzhiyun 						    since);
1169*4882a593Smuzhiyun 			err = nfserrno(err2);
1170*4882a593Smuzhiyun 			break;
1171*4882a593Smuzhiyun 		case -EINVAL:
1172*4882a593Smuzhiyun 			err = nfserr_notsupp;
1173*4882a593Smuzhiyun 			break;
1174*4882a593Smuzhiyun 		default:
1175*4882a593Smuzhiyun 			nfsd_reset_boot_verifier(net_generic(nf->nf_net,
1176*4882a593Smuzhiyun 						 nfsd_net_id));
1177*4882a593Smuzhiyun 			err = nfserrno(err2);
1178*4882a593Smuzhiyun 		}
1179*4882a593Smuzhiyun 	} else
1180*4882a593Smuzhiyun 		nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
1181*4882a593Smuzhiyun 					nfsd_net_id));
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	nfsd_file_put(nf);
1184*4882a593Smuzhiyun out:
1185*4882a593Smuzhiyun 	return err;
1186*4882a593Smuzhiyun }
1187*4882a593Smuzhiyun #endif /* CONFIG_NFSD_V3 */
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun static __be32
nfsd_create_setattr(struct svc_rqst * rqstp,struct svc_fh * resfhp,struct iattr * iap)1190*4882a593Smuzhiyun nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1191*4882a593Smuzhiyun 			struct iattr *iap)
1192*4882a593Smuzhiyun {
1193*4882a593Smuzhiyun 	/*
1194*4882a593Smuzhiyun 	 * Mode has already been set earlier in create:
1195*4882a593Smuzhiyun 	 */
1196*4882a593Smuzhiyun 	iap->ia_valid &= ~ATTR_MODE;
1197*4882a593Smuzhiyun 	/*
1198*4882a593Smuzhiyun 	 * Setting uid/gid works only for root.  Irix appears to
1199*4882a593Smuzhiyun 	 * send along the gid on create when it tries to implement
1200*4882a593Smuzhiyun 	 * setgid directories via NFS:
1201*4882a593Smuzhiyun 	 */
1202*4882a593Smuzhiyun 	if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1203*4882a593Smuzhiyun 		iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1204*4882a593Smuzhiyun 	if (iap->ia_valid)
1205*4882a593Smuzhiyun 		return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0);
1206*4882a593Smuzhiyun 	/* Callers expect file metadata to be committed here */
1207*4882a593Smuzhiyun 	return nfserrno(commit_metadata(resfhp));
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1211*4882a593Smuzhiyun  * setting size to 0 may fail for some specific file systems by the permission
1212*4882a593Smuzhiyun  * checking which requires WRITE permission but the mode is 000.
1213*4882a593Smuzhiyun  * we ignore the resizing(to 0) on the just new created file, since the size is
1214*4882a593Smuzhiyun  * 0 after file created.
1215*4882a593Smuzhiyun  *
1216*4882a593Smuzhiyun  * call this only after vfs_create() is called.
1217*4882a593Smuzhiyun  * */
1218*4882a593Smuzhiyun static void
nfsd_check_ignore_resizing(struct iattr * iap)1219*4882a593Smuzhiyun nfsd_check_ignore_resizing(struct iattr *iap)
1220*4882a593Smuzhiyun {
1221*4882a593Smuzhiyun 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1222*4882a593Smuzhiyun 		iap->ia_valid &= ~ATTR_SIZE;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun /* The parent directory should already be locked: */
1226*4882a593Smuzhiyun __be32
nfsd_create_locked(struct svc_rqst * rqstp,struct svc_fh * fhp,char * fname,int flen,struct iattr * iap,int type,dev_t rdev,struct svc_fh * resfhp)1227*4882a593Smuzhiyun nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
1228*4882a593Smuzhiyun 		char *fname, int flen, struct iattr *iap,
1229*4882a593Smuzhiyun 		int type, dev_t rdev, struct svc_fh *resfhp)
1230*4882a593Smuzhiyun {
1231*4882a593Smuzhiyun 	struct dentry	*dentry, *dchild;
1232*4882a593Smuzhiyun 	struct inode	*dirp;
1233*4882a593Smuzhiyun 	__be32		err;
1234*4882a593Smuzhiyun 	__be32		err2;
1235*4882a593Smuzhiyun 	int		host_err;
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
1238*4882a593Smuzhiyun 	dirp = d_inode(dentry);
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 	dchild = dget(resfhp->fh_dentry);
1241*4882a593Smuzhiyun 	if (!fhp->fh_locked) {
1242*4882a593Smuzhiyun 		WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1243*4882a593Smuzhiyun 				dentry);
1244*4882a593Smuzhiyun 		err = nfserr_io;
1245*4882a593Smuzhiyun 		goto out;
1246*4882a593Smuzhiyun 	}
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 	err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1249*4882a593Smuzhiyun 	if (err)
1250*4882a593Smuzhiyun 		goto out;
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	if (!(iap->ia_valid & ATTR_MODE))
1253*4882a593Smuzhiyun 		iap->ia_mode = 0;
1254*4882a593Smuzhiyun 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	if (!IS_POSIXACL(dirp))
1257*4882a593Smuzhiyun 		iap->ia_mode &= ~current_umask();
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	err = 0;
1260*4882a593Smuzhiyun 	host_err = 0;
1261*4882a593Smuzhiyun 	switch (type) {
1262*4882a593Smuzhiyun 	case S_IFREG:
1263*4882a593Smuzhiyun 		host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1264*4882a593Smuzhiyun 		if (!host_err)
1265*4882a593Smuzhiyun 			nfsd_check_ignore_resizing(iap);
1266*4882a593Smuzhiyun 		break;
1267*4882a593Smuzhiyun 	case S_IFDIR:
1268*4882a593Smuzhiyun 		host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1269*4882a593Smuzhiyun 		if (!host_err && unlikely(d_unhashed(dchild))) {
1270*4882a593Smuzhiyun 			struct dentry *d;
1271*4882a593Smuzhiyun 			d = lookup_one_len(dchild->d_name.name,
1272*4882a593Smuzhiyun 					   dchild->d_parent,
1273*4882a593Smuzhiyun 					   dchild->d_name.len);
1274*4882a593Smuzhiyun 			if (IS_ERR(d)) {
1275*4882a593Smuzhiyun 				host_err = PTR_ERR(d);
1276*4882a593Smuzhiyun 				break;
1277*4882a593Smuzhiyun 			}
1278*4882a593Smuzhiyun 			if (unlikely(d_is_negative(d))) {
1279*4882a593Smuzhiyun 				dput(d);
1280*4882a593Smuzhiyun 				err = nfserr_serverfault;
1281*4882a593Smuzhiyun 				goto out;
1282*4882a593Smuzhiyun 			}
1283*4882a593Smuzhiyun 			dput(resfhp->fh_dentry);
1284*4882a593Smuzhiyun 			resfhp->fh_dentry = dget(d);
1285*4882a593Smuzhiyun 			err = fh_update(resfhp);
1286*4882a593Smuzhiyun 			dput(dchild);
1287*4882a593Smuzhiyun 			dchild = d;
1288*4882a593Smuzhiyun 			if (err)
1289*4882a593Smuzhiyun 				goto out;
1290*4882a593Smuzhiyun 		}
1291*4882a593Smuzhiyun 		break;
1292*4882a593Smuzhiyun 	case S_IFCHR:
1293*4882a593Smuzhiyun 	case S_IFBLK:
1294*4882a593Smuzhiyun 	case S_IFIFO:
1295*4882a593Smuzhiyun 	case S_IFSOCK:
1296*4882a593Smuzhiyun 		host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1297*4882a593Smuzhiyun 		break;
1298*4882a593Smuzhiyun 	default:
1299*4882a593Smuzhiyun 		printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1300*4882a593Smuzhiyun 		       type);
1301*4882a593Smuzhiyun 		host_err = -EINVAL;
1302*4882a593Smuzhiyun 	}
1303*4882a593Smuzhiyun 	if (host_err < 0)
1304*4882a593Smuzhiyun 		goto out_nfserr;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	err = nfsd_create_setattr(rqstp, resfhp, iap);
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	/*
1309*4882a593Smuzhiyun 	 * nfsd_create_setattr already committed the child.  Transactional
1310*4882a593Smuzhiyun 	 * filesystems had a chance to commit changes for both parent and
1311*4882a593Smuzhiyun 	 * child simultaneously making the following commit_metadata a
1312*4882a593Smuzhiyun 	 * noop.
1313*4882a593Smuzhiyun 	 */
1314*4882a593Smuzhiyun 	err2 = nfserrno(commit_metadata(fhp));
1315*4882a593Smuzhiyun 	if (err2)
1316*4882a593Smuzhiyun 		err = err2;
1317*4882a593Smuzhiyun 	/*
1318*4882a593Smuzhiyun 	 * Update the file handle to get the new inode info.
1319*4882a593Smuzhiyun 	 */
1320*4882a593Smuzhiyun 	if (!err)
1321*4882a593Smuzhiyun 		err = fh_update(resfhp);
1322*4882a593Smuzhiyun out:
1323*4882a593Smuzhiyun 	dput(dchild);
1324*4882a593Smuzhiyun 	return err;
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun out_nfserr:
1327*4882a593Smuzhiyun 	err = nfserrno(host_err);
1328*4882a593Smuzhiyun 	goto out;
1329*4882a593Smuzhiyun }
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun /*
1332*4882a593Smuzhiyun  * Create a filesystem object (regular, directory, special).
1333*4882a593Smuzhiyun  * Note that the parent directory is left locked.
1334*4882a593Smuzhiyun  *
1335*4882a593Smuzhiyun  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1336*4882a593Smuzhiyun  */
1337*4882a593Smuzhiyun __be32
nfsd_create(struct svc_rqst * rqstp,struct svc_fh * fhp,char * fname,int flen,struct iattr * iap,int type,dev_t rdev,struct svc_fh * resfhp)1338*4882a593Smuzhiyun nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1339*4882a593Smuzhiyun 		char *fname, int flen, struct iattr *iap,
1340*4882a593Smuzhiyun 		int type, dev_t rdev, struct svc_fh *resfhp)
1341*4882a593Smuzhiyun {
1342*4882a593Smuzhiyun 	struct dentry	*dentry, *dchild = NULL;
1343*4882a593Smuzhiyun 	__be32		err;
1344*4882a593Smuzhiyun 	int		host_err;
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	if (isdotent(fname, flen))
1347*4882a593Smuzhiyun 		return nfserr_exist;
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1350*4882a593Smuzhiyun 	if (err)
1351*4882a593Smuzhiyun 		return err;
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	host_err = fh_want_write(fhp);
1356*4882a593Smuzhiyun 	if (host_err)
1357*4882a593Smuzhiyun 		return nfserrno(host_err);
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1360*4882a593Smuzhiyun 	dchild = lookup_one_len(fname, dentry, flen);
1361*4882a593Smuzhiyun 	host_err = PTR_ERR(dchild);
1362*4882a593Smuzhiyun 	if (IS_ERR(dchild))
1363*4882a593Smuzhiyun 		return nfserrno(host_err);
1364*4882a593Smuzhiyun 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1365*4882a593Smuzhiyun 	/*
1366*4882a593Smuzhiyun 	 * We unconditionally drop our ref to dchild as fh_compose will have
1367*4882a593Smuzhiyun 	 * already grabbed its own ref for it.
1368*4882a593Smuzhiyun 	 */
1369*4882a593Smuzhiyun 	dput(dchild);
1370*4882a593Smuzhiyun 	if (err)
1371*4882a593Smuzhiyun 		return err;
1372*4882a593Smuzhiyun 	return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1373*4882a593Smuzhiyun 					rdev, resfhp);
1374*4882a593Smuzhiyun }
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V3
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun /*
1379*4882a593Smuzhiyun  * NFSv3 and NFSv4 version of nfsd_create
1380*4882a593Smuzhiyun  */
1381*4882a593Smuzhiyun __be32
do_nfsd_create(struct svc_rqst * rqstp,struct svc_fh * fhp,char * fname,int flen,struct iattr * iap,struct svc_fh * resfhp,int createmode,u32 * verifier,bool * truncp,bool * created)1382*4882a593Smuzhiyun do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1383*4882a593Smuzhiyun 		char *fname, int flen, struct iattr *iap,
1384*4882a593Smuzhiyun 		struct svc_fh *resfhp, int createmode, u32 *verifier,
1385*4882a593Smuzhiyun 	        bool *truncp, bool *created)
1386*4882a593Smuzhiyun {
1387*4882a593Smuzhiyun 	struct dentry	*dentry, *dchild = NULL;
1388*4882a593Smuzhiyun 	struct inode	*dirp;
1389*4882a593Smuzhiyun 	__be32		err;
1390*4882a593Smuzhiyun 	int		host_err;
1391*4882a593Smuzhiyun 	__u32		v_mtime=0, v_atime=0;
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	err = nfserr_perm;
1394*4882a593Smuzhiyun 	if (!flen)
1395*4882a593Smuzhiyun 		goto out;
1396*4882a593Smuzhiyun 	err = nfserr_exist;
1397*4882a593Smuzhiyun 	if (isdotent(fname, flen))
1398*4882a593Smuzhiyun 		goto out;
1399*4882a593Smuzhiyun 	if (!(iap->ia_valid & ATTR_MODE))
1400*4882a593Smuzhiyun 		iap->ia_mode = 0;
1401*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1402*4882a593Smuzhiyun 	if (err)
1403*4882a593Smuzhiyun 		goto out;
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
1406*4882a593Smuzhiyun 	dirp = d_inode(dentry);
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	host_err = fh_want_write(fhp);
1409*4882a593Smuzhiyun 	if (host_err)
1410*4882a593Smuzhiyun 		goto out_nfserr;
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 	/*
1415*4882a593Smuzhiyun 	 * Compose the response file handle.
1416*4882a593Smuzhiyun 	 */
1417*4882a593Smuzhiyun 	dchild = lookup_one_len(fname, dentry, flen);
1418*4882a593Smuzhiyun 	host_err = PTR_ERR(dchild);
1419*4882a593Smuzhiyun 	if (IS_ERR(dchild))
1420*4882a593Smuzhiyun 		goto out_nfserr;
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	/* If file doesn't exist, check for permissions to create one */
1423*4882a593Smuzhiyun 	if (d_really_is_negative(dchild)) {
1424*4882a593Smuzhiyun 		err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1425*4882a593Smuzhiyun 		if (err)
1426*4882a593Smuzhiyun 			goto out;
1427*4882a593Smuzhiyun 	}
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 	err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1430*4882a593Smuzhiyun 	if (err)
1431*4882a593Smuzhiyun 		goto out;
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	if (nfsd_create_is_exclusive(createmode)) {
1434*4882a593Smuzhiyun 		/* solaris7 gets confused (bugid 4218508) if these have
1435*4882a593Smuzhiyun 		 * the high bit set, so just clear the high bits. If this is
1436*4882a593Smuzhiyun 		 * ever changed to use different attrs for storing the
1437*4882a593Smuzhiyun 		 * verifier, then do_open_lookup() will also need to be fixed
1438*4882a593Smuzhiyun 		 * accordingly.
1439*4882a593Smuzhiyun 		 */
1440*4882a593Smuzhiyun 		v_mtime = verifier[0]&0x7fffffff;
1441*4882a593Smuzhiyun 		v_atime = verifier[1]&0x7fffffff;
1442*4882a593Smuzhiyun 	}
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	if (d_really_is_positive(dchild)) {
1445*4882a593Smuzhiyun 		err = 0;
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 		switch (createmode) {
1448*4882a593Smuzhiyun 		case NFS3_CREATE_UNCHECKED:
1449*4882a593Smuzhiyun 			if (! d_is_reg(dchild))
1450*4882a593Smuzhiyun 				goto out;
1451*4882a593Smuzhiyun 			else if (truncp) {
1452*4882a593Smuzhiyun 				/* in nfsv4, we need to treat this case a little
1453*4882a593Smuzhiyun 				 * differently.  we don't want to truncate the
1454*4882a593Smuzhiyun 				 * file now; this would be wrong if the OPEN
1455*4882a593Smuzhiyun 				 * fails for some other reason.  furthermore,
1456*4882a593Smuzhiyun 				 * if the size is nonzero, we should ignore it
1457*4882a593Smuzhiyun 				 * according to spec!
1458*4882a593Smuzhiyun 				 */
1459*4882a593Smuzhiyun 				*truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1460*4882a593Smuzhiyun 			}
1461*4882a593Smuzhiyun 			else {
1462*4882a593Smuzhiyun 				iap->ia_valid &= ATTR_SIZE;
1463*4882a593Smuzhiyun 				goto set_attr;
1464*4882a593Smuzhiyun 			}
1465*4882a593Smuzhiyun 			break;
1466*4882a593Smuzhiyun 		case NFS3_CREATE_EXCLUSIVE:
1467*4882a593Smuzhiyun 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
1468*4882a593Smuzhiyun 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
1469*4882a593Smuzhiyun 			    && d_inode(dchild)->i_size  == 0 ) {
1470*4882a593Smuzhiyun 				if (created)
1471*4882a593Smuzhiyun 					*created = true;
1472*4882a593Smuzhiyun 				break;
1473*4882a593Smuzhiyun 			}
1474*4882a593Smuzhiyun 			fallthrough;
1475*4882a593Smuzhiyun 		case NFS4_CREATE_EXCLUSIVE4_1:
1476*4882a593Smuzhiyun 			if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
1477*4882a593Smuzhiyun 			    && d_inode(dchild)->i_atime.tv_sec == v_atime
1478*4882a593Smuzhiyun 			    && d_inode(dchild)->i_size  == 0 ) {
1479*4882a593Smuzhiyun 				if (created)
1480*4882a593Smuzhiyun 					*created = true;
1481*4882a593Smuzhiyun 				goto set_attr;
1482*4882a593Smuzhiyun 			}
1483*4882a593Smuzhiyun 			fallthrough;
1484*4882a593Smuzhiyun 		case NFS3_CREATE_GUARDED:
1485*4882a593Smuzhiyun 			err = nfserr_exist;
1486*4882a593Smuzhiyun 		}
1487*4882a593Smuzhiyun 		fh_drop_write(fhp);
1488*4882a593Smuzhiyun 		goto out;
1489*4882a593Smuzhiyun 	}
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun 	if (!IS_POSIXACL(dirp))
1492*4882a593Smuzhiyun 		iap->ia_mode &= ~current_umask();
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 	host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1495*4882a593Smuzhiyun 	if (host_err < 0) {
1496*4882a593Smuzhiyun 		fh_drop_write(fhp);
1497*4882a593Smuzhiyun 		goto out_nfserr;
1498*4882a593Smuzhiyun 	}
1499*4882a593Smuzhiyun 	if (created)
1500*4882a593Smuzhiyun 		*created = true;
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 	nfsd_check_ignore_resizing(iap);
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	if (nfsd_create_is_exclusive(createmode)) {
1505*4882a593Smuzhiyun 		/* Cram the verifier into atime/mtime */
1506*4882a593Smuzhiyun 		iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1507*4882a593Smuzhiyun 			| ATTR_MTIME_SET|ATTR_ATIME_SET;
1508*4882a593Smuzhiyun 		/* XXX someone who knows this better please fix it for nsec */
1509*4882a593Smuzhiyun 		iap->ia_mtime.tv_sec = v_mtime;
1510*4882a593Smuzhiyun 		iap->ia_atime.tv_sec = v_atime;
1511*4882a593Smuzhiyun 		iap->ia_mtime.tv_nsec = 0;
1512*4882a593Smuzhiyun 		iap->ia_atime.tv_nsec = 0;
1513*4882a593Smuzhiyun 	}
1514*4882a593Smuzhiyun 
1515*4882a593Smuzhiyun  set_attr:
1516*4882a593Smuzhiyun 	err = nfsd_create_setattr(rqstp, resfhp, iap);
1517*4882a593Smuzhiyun 
1518*4882a593Smuzhiyun 	/*
1519*4882a593Smuzhiyun 	 * nfsd_create_setattr already committed the child
1520*4882a593Smuzhiyun 	 * (and possibly also the parent).
1521*4882a593Smuzhiyun 	 */
1522*4882a593Smuzhiyun 	if (!err)
1523*4882a593Smuzhiyun 		err = nfserrno(commit_metadata(fhp));
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun 	/*
1526*4882a593Smuzhiyun 	 * Update the filehandle to get the new inode info.
1527*4882a593Smuzhiyun 	 */
1528*4882a593Smuzhiyun 	if (!err)
1529*4882a593Smuzhiyun 		err = fh_update(resfhp);
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun  out:
1532*4882a593Smuzhiyun 	fh_unlock(fhp);
1533*4882a593Smuzhiyun 	if (dchild && !IS_ERR(dchild))
1534*4882a593Smuzhiyun 		dput(dchild);
1535*4882a593Smuzhiyun 	fh_drop_write(fhp);
1536*4882a593Smuzhiyun  	return err;
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun  out_nfserr:
1539*4882a593Smuzhiyun 	err = nfserrno(host_err);
1540*4882a593Smuzhiyun 	goto out;
1541*4882a593Smuzhiyun }
1542*4882a593Smuzhiyun #endif /* CONFIG_NFSD_V3 */
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun /*
1545*4882a593Smuzhiyun  * Read a symlink. On entry, *lenp must contain the maximum path length that
1546*4882a593Smuzhiyun  * fits into the buffer. On return, it contains the true length.
1547*4882a593Smuzhiyun  * N.B. After this call fhp needs an fh_put
1548*4882a593Smuzhiyun  */
1549*4882a593Smuzhiyun __be32
nfsd_readlink(struct svc_rqst * rqstp,struct svc_fh * fhp,char * buf,int * lenp)1550*4882a593Smuzhiyun nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1551*4882a593Smuzhiyun {
1552*4882a593Smuzhiyun 	__be32		err;
1553*4882a593Smuzhiyun 	const char *link;
1554*4882a593Smuzhiyun 	struct path path;
1555*4882a593Smuzhiyun 	DEFINE_DELAYED_CALL(done);
1556*4882a593Smuzhiyun 	int len;
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1559*4882a593Smuzhiyun 	if (unlikely(err))
1560*4882a593Smuzhiyun 		return err;
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun 	path.mnt = fhp->fh_export->ex_path.mnt;
1563*4882a593Smuzhiyun 	path.dentry = fhp->fh_dentry;
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	if (unlikely(!d_is_symlink(path.dentry)))
1566*4882a593Smuzhiyun 		return nfserr_inval;
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun 	touch_atime(&path);
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	link = vfs_get_link(path.dentry, &done);
1571*4882a593Smuzhiyun 	if (IS_ERR(link))
1572*4882a593Smuzhiyun 		return nfserrno(PTR_ERR(link));
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	len = strlen(link);
1575*4882a593Smuzhiyun 	if (len < *lenp)
1576*4882a593Smuzhiyun 		*lenp = len;
1577*4882a593Smuzhiyun 	memcpy(buf, link, *lenp);
1578*4882a593Smuzhiyun 	do_delayed_call(&done);
1579*4882a593Smuzhiyun 	return 0;
1580*4882a593Smuzhiyun }
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun /*
1583*4882a593Smuzhiyun  * Create a symlink and look up its inode
1584*4882a593Smuzhiyun  * N.B. After this call _both_ fhp and resfhp need an fh_put
1585*4882a593Smuzhiyun  */
1586*4882a593Smuzhiyun __be32
nfsd_symlink(struct svc_rqst * rqstp,struct svc_fh * fhp,char * fname,int flen,char * path,struct svc_fh * resfhp)1587*4882a593Smuzhiyun nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1588*4882a593Smuzhiyun 				char *fname, int flen,
1589*4882a593Smuzhiyun 				char *path,
1590*4882a593Smuzhiyun 				struct svc_fh *resfhp)
1591*4882a593Smuzhiyun {
1592*4882a593Smuzhiyun 	struct dentry	*dentry, *dnew;
1593*4882a593Smuzhiyun 	__be32		err, cerr;
1594*4882a593Smuzhiyun 	int		host_err;
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	err = nfserr_noent;
1597*4882a593Smuzhiyun 	if (!flen || path[0] == '\0')
1598*4882a593Smuzhiyun 		goto out;
1599*4882a593Smuzhiyun 	err = nfserr_exist;
1600*4882a593Smuzhiyun 	if (isdotent(fname, flen))
1601*4882a593Smuzhiyun 		goto out;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1604*4882a593Smuzhiyun 	if (err)
1605*4882a593Smuzhiyun 		goto out;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	host_err = fh_want_write(fhp);
1608*4882a593Smuzhiyun 	if (host_err)
1609*4882a593Smuzhiyun 		goto out_nfserr;
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 	fh_lock(fhp);
1612*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
1613*4882a593Smuzhiyun 	dnew = lookup_one_len(fname, dentry, flen);
1614*4882a593Smuzhiyun 	host_err = PTR_ERR(dnew);
1615*4882a593Smuzhiyun 	if (IS_ERR(dnew))
1616*4882a593Smuzhiyun 		goto out_nfserr;
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 	host_err = vfs_symlink(d_inode(dentry), dnew, path);
1619*4882a593Smuzhiyun 	err = nfserrno(host_err);
1620*4882a593Smuzhiyun 	if (!err)
1621*4882a593Smuzhiyun 		err = nfserrno(commit_metadata(fhp));
1622*4882a593Smuzhiyun 	fh_unlock(fhp);
1623*4882a593Smuzhiyun 
1624*4882a593Smuzhiyun 	fh_drop_write(fhp);
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1627*4882a593Smuzhiyun 	dput(dnew);
1628*4882a593Smuzhiyun 	if (err==0) err = cerr;
1629*4882a593Smuzhiyun out:
1630*4882a593Smuzhiyun 	return err;
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun out_nfserr:
1633*4882a593Smuzhiyun 	err = nfserrno(host_err);
1634*4882a593Smuzhiyun 	goto out;
1635*4882a593Smuzhiyun }
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun /*
1638*4882a593Smuzhiyun  * Create a hardlink
1639*4882a593Smuzhiyun  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1640*4882a593Smuzhiyun  */
1641*4882a593Smuzhiyun __be32
nfsd_link(struct svc_rqst * rqstp,struct svc_fh * ffhp,char * name,int len,struct svc_fh * tfhp)1642*4882a593Smuzhiyun nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1643*4882a593Smuzhiyun 				char *name, int len, struct svc_fh *tfhp)
1644*4882a593Smuzhiyun {
1645*4882a593Smuzhiyun 	struct dentry	*ddir, *dnew, *dold;
1646*4882a593Smuzhiyun 	struct inode	*dirp;
1647*4882a593Smuzhiyun 	__be32		err;
1648*4882a593Smuzhiyun 	int		host_err;
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1651*4882a593Smuzhiyun 	if (err)
1652*4882a593Smuzhiyun 		goto out;
1653*4882a593Smuzhiyun 	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1654*4882a593Smuzhiyun 	if (err)
1655*4882a593Smuzhiyun 		goto out;
1656*4882a593Smuzhiyun 	err = nfserr_isdir;
1657*4882a593Smuzhiyun 	if (d_is_dir(tfhp->fh_dentry))
1658*4882a593Smuzhiyun 		goto out;
1659*4882a593Smuzhiyun 	err = nfserr_perm;
1660*4882a593Smuzhiyun 	if (!len)
1661*4882a593Smuzhiyun 		goto out;
1662*4882a593Smuzhiyun 	err = nfserr_exist;
1663*4882a593Smuzhiyun 	if (isdotent(name, len))
1664*4882a593Smuzhiyun 		goto out;
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun 	host_err = fh_want_write(tfhp);
1667*4882a593Smuzhiyun 	if (host_err) {
1668*4882a593Smuzhiyun 		err = nfserrno(host_err);
1669*4882a593Smuzhiyun 		goto out;
1670*4882a593Smuzhiyun 	}
1671*4882a593Smuzhiyun 
1672*4882a593Smuzhiyun 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
1673*4882a593Smuzhiyun 	ddir = ffhp->fh_dentry;
1674*4882a593Smuzhiyun 	dirp = d_inode(ddir);
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun 	dnew = lookup_one_len(name, ddir, len);
1677*4882a593Smuzhiyun 	host_err = PTR_ERR(dnew);
1678*4882a593Smuzhiyun 	if (IS_ERR(dnew))
1679*4882a593Smuzhiyun 		goto out_nfserr;
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun 	dold = tfhp->fh_dentry;
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun 	err = nfserr_noent;
1684*4882a593Smuzhiyun 	if (d_really_is_negative(dold))
1685*4882a593Smuzhiyun 		goto out_dput;
1686*4882a593Smuzhiyun 	host_err = vfs_link(dold, dirp, dnew, NULL);
1687*4882a593Smuzhiyun 	if (!host_err) {
1688*4882a593Smuzhiyun 		err = nfserrno(commit_metadata(ffhp));
1689*4882a593Smuzhiyun 		if (!err)
1690*4882a593Smuzhiyun 			err = nfserrno(commit_metadata(tfhp));
1691*4882a593Smuzhiyun 	} else {
1692*4882a593Smuzhiyun 		if (host_err == -EXDEV && rqstp->rq_vers == 2)
1693*4882a593Smuzhiyun 			err = nfserr_acces;
1694*4882a593Smuzhiyun 		else
1695*4882a593Smuzhiyun 			err = nfserrno(host_err);
1696*4882a593Smuzhiyun 	}
1697*4882a593Smuzhiyun out_dput:
1698*4882a593Smuzhiyun 	dput(dnew);
1699*4882a593Smuzhiyun out_unlock:
1700*4882a593Smuzhiyun 	fh_unlock(ffhp);
1701*4882a593Smuzhiyun 	fh_drop_write(tfhp);
1702*4882a593Smuzhiyun out:
1703*4882a593Smuzhiyun 	return err;
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun out_nfserr:
1706*4882a593Smuzhiyun 	err = nfserrno(host_err);
1707*4882a593Smuzhiyun 	goto out_unlock;
1708*4882a593Smuzhiyun }
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun static void
nfsd_close_cached_files(struct dentry * dentry)1711*4882a593Smuzhiyun nfsd_close_cached_files(struct dentry *dentry)
1712*4882a593Smuzhiyun {
1713*4882a593Smuzhiyun 	struct inode *inode = d_inode(dentry);
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 	if (inode && S_ISREG(inode->i_mode))
1716*4882a593Smuzhiyun 		nfsd_file_close_inode_sync(inode);
1717*4882a593Smuzhiyun }
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun static bool
nfsd_has_cached_files(struct dentry * dentry)1720*4882a593Smuzhiyun nfsd_has_cached_files(struct dentry *dentry)
1721*4882a593Smuzhiyun {
1722*4882a593Smuzhiyun 	bool		ret = false;
1723*4882a593Smuzhiyun 	struct inode *inode = d_inode(dentry);
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 	if (inode && S_ISREG(inode->i_mode))
1726*4882a593Smuzhiyun 		ret = nfsd_file_is_cached(inode);
1727*4882a593Smuzhiyun 	return ret;
1728*4882a593Smuzhiyun }
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun /*
1731*4882a593Smuzhiyun  * Rename a file
1732*4882a593Smuzhiyun  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1733*4882a593Smuzhiyun  */
1734*4882a593Smuzhiyun __be32
nfsd_rename(struct svc_rqst * rqstp,struct svc_fh * ffhp,char * fname,int flen,struct svc_fh * tfhp,char * tname,int tlen)1735*4882a593Smuzhiyun nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1736*4882a593Smuzhiyun 			    struct svc_fh *tfhp, char *tname, int tlen)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
1739*4882a593Smuzhiyun 	struct inode	*fdir, *tdir;
1740*4882a593Smuzhiyun 	__be32		err;
1741*4882a593Smuzhiyun 	int		host_err;
1742*4882a593Smuzhiyun 	bool		has_cached = false;
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun 	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1745*4882a593Smuzhiyun 	if (err)
1746*4882a593Smuzhiyun 		goto out;
1747*4882a593Smuzhiyun 	err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1748*4882a593Smuzhiyun 	if (err)
1749*4882a593Smuzhiyun 		goto out;
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun 	fdentry = ffhp->fh_dentry;
1752*4882a593Smuzhiyun 	fdir = d_inode(fdentry);
1753*4882a593Smuzhiyun 
1754*4882a593Smuzhiyun 	tdentry = tfhp->fh_dentry;
1755*4882a593Smuzhiyun 	tdir = d_inode(tdentry);
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun 	err = nfserr_perm;
1758*4882a593Smuzhiyun 	if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1759*4882a593Smuzhiyun 		goto out;
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun retry:
1762*4882a593Smuzhiyun 	host_err = fh_want_write(ffhp);
1763*4882a593Smuzhiyun 	if (host_err) {
1764*4882a593Smuzhiyun 		err = nfserrno(host_err);
1765*4882a593Smuzhiyun 		goto out;
1766*4882a593Smuzhiyun 	}
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun 	/* cannot use fh_lock as we need deadlock protective ordering
1769*4882a593Smuzhiyun 	 * so do it by hand */
1770*4882a593Smuzhiyun 	trap = lock_rename(tdentry, fdentry);
1771*4882a593Smuzhiyun 	ffhp->fh_locked = tfhp->fh_locked = true;
1772*4882a593Smuzhiyun 	fill_pre_wcc(ffhp);
1773*4882a593Smuzhiyun 	fill_pre_wcc(tfhp);
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun 	odentry = lookup_one_len(fname, fdentry, flen);
1776*4882a593Smuzhiyun 	host_err = PTR_ERR(odentry);
1777*4882a593Smuzhiyun 	if (IS_ERR(odentry))
1778*4882a593Smuzhiyun 		goto out_nfserr;
1779*4882a593Smuzhiyun 
1780*4882a593Smuzhiyun 	host_err = -ENOENT;
1781*4882a593Smuzhiyun 	if (d_really_is_negative(odentry))
1782*4882a593Smuzhiyun 		goto out_dput_old;
1783*4882a593Smuzhiyun 	host_err = -EINVAL;
1784*4882a593Smuzhiyun 	if (odentry == trap)
1785*4882a593Smuzhiyun 		goto out_dput_old;
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun 	ndentry = lookup_one_len(tname, tdentry, tlen);
1788*4882a593Smuzhiyun 	host_err = PTR_ERR(ndentry);
1789*4882a593Smuzhiyun 	if (IS_ERR(ndentry))
1790*4882a593Smuzhiyun 		goto out_dput_old;
1791*4882a593Smuzhiyun 	host_err = -ENOTEMPTY;
1792*4882a593Smuzhiyun 	if (ndentry == trap)
1793*4882a593Smuzhiyun 		goto out_dput_new;
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	host_err = -EXDEV;
1796*4882a593Smuzhiyun 	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1797*4882a593Smuzhiyun 		goto out_dput_new;
1798*4882a593Smuzhiyun 	if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1799*4882a593Smuzhiyun 		goto out_dput_new;
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 	if (nfsd_has_cached_files(ndentry)) {
1802*4882a593Smuzhiyun 		has_cached = true;
1803*4882a593Smuzhiyun 		goto out_dput_old;
1804*4882a593Smuzhiyun 	} else {
1805*4882a593Smuzhiyun 		host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
1806*4882a593Smuzhiyun 		if (!host_err) {
1807*4882a593Smuzhiyun 			host_err = commit_metadata(tfhp);
1808*4882a593Smuzhiyun 			if (!host_err)
1809*4882a593Smuzhiyun 				host_err = commit_metadata(ffhp);
1810*4882a593Smuzhiyun 		}
1811*4882a593Smuzhiyun 	}
1812*4882a593Smuzhiyun  out_dput_new:
1813*4882a593Smuzhiyun 	dput(ndentry);
1814*4882a593Smuzhiyun  out_dput_old:
1815*4882a593Smuzhiyun 	dput(odentry);
1816*4882a593Smuzhiyun  out_nfserr:
1817*4882a593Smuzhiyun 	err = nfserrno(host_err);
1818*4882a593Smuzhiyun 	/*
1819*4882a593Smuzhiyun 	 * We cannot rely on fh_unlock on the two filehandles,
1820*4882a593Smuzhiyun 	 * as that would do the wrong thing if the two directories
1821*4882a593Smuzhiyun 	 * were the same, so again we do it by hand.
1822*4882a593Smuzhiyun 	 */
1823*4882a593Smuzhiyun 	if (!has_cached) {
1824*4882a593Smuzhiyun 		fill_post_wcc(ffhp);
1825*4882a593Smuzhiyun 		fill_post_wcc(tfhp);
1826*4882a593Smuzhiyun 	}
1827*4882a593Smuzhiyun 	unlock_rename(tdentry, fdentry);
1828*4882a593Smuzhiyun 	ffhp->fh_locked = tfhp->fh_locked = false;
1829*4882a593Smuzhiyun 	fh_drop_write(ffhp);
1830*4882a593Smuzhiyun 
1831*4882a593Smuzhiyun 	/*
1832*4882a593Smuzhiyun 	 * If the target dentry has cached open files, then we need to try to
1833*4882a593Smuzhiyun 	 * close them prior to doing the rename. Flushing delayed fput
1834*4882a593Smuzhiyun 	 * shouldn't be done with locks held however, so we delay it until this
1835*4882a593Smuzhiyun 	 * point and then reattempt the whole shebang.
1836*4882a593Smuzhiyun 	 */
1837*4882a593Smuzhiyun 	if (has_cached) {
1838*4882a593Smuzhiyun 		has_cached = false;
1839*4882a593Smuzhiyun 		nfsd_close_cached_files(ndentry);
1840*4882a593Smuzhiyun 		dput(ndentry);
1841*4882a593Smuzhiyun 		goto retry;
1842*4882a593Smuzhiyun 	}
1843*4882a593Smuzhiyun out:
1844*4882a593Smuzhiyun 	return err;
1845*4882a593Smuzhiyun }
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun /*
1848*4882a593Smuzhiyun  * Unlink a file or directory
1849*4882a593Smuzhiyun  * N.B. After this call fhp needs an fh_put
1850*4882a593Smuzhiyun  */
1851*4882a593Smuzhiyun __be32
nfsd_unlink(struct svc_rqst * rqstp,struct svc_fh * fhp,int type,char * fname,int flen)1852*4882a593Smuzhiyun nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1853*4882a593Smuzhiyun 				char *fname, int flen)
1854*4882a593Smuzhiyun {
1855*4882a593Smuzhiyun 	struct dentry	*dentry, *rdentry;
1856*4882a593Smuzhiyun 	struct inode	*dirp;
1857*4882a593Smuzhiyun 	__be32		err;
1858*4882a593Smuzhiyun 	int		host_err;
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	err = nfserr_acces;
1861*4882a593Smuzhiyun 	if (!flen || isdotent(fname, flen))
1862*4882a593Smuzhiyun 		goto out;
1863*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1864*4882a593Smuzhiyun 	if (err)
1865*4882a593Smuzhiyun 		goto out;
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 	host_err = fh_want_write(fhp);
1868*4882a593Smuzhiyun 	if (host_err)
1869*4882a593Smuzhiyun 		goto out_nfserr;
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun 	fh_lock_nested(fhp, I_MUTEX_PARENT);
1872*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
1873*4882a593Smuzhiyun 	dirp = d_inode(dentry);
1874*4882a593Smuzhiyun 
1875*4882a593Smuzhiyun 	rdentry = lookup_one_len(fname, dentry, flen);
1876*4882a593Smuzhiyun 	host_err = PTR_ERR(rdentry);
1877*4882a593Smuzhiyun 	if (IS_ERR(rdentry))
1878*4882a593Smuzhiyun 		goto out_drop_write;
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	if (d_really_is_negative(rdentry)) {
1881*4882a593Smuzhiyun 		dput(rdentry);
1882*4882a593Smuzhiyun 		host_err = -ENOENT;
1883*4882a593Smuzhiyun 		goto out_drop_write;
1884*4882a593Smuzhiyun 	}
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun 	if (!type)
1887*4882a593Smuzhiyun 		type = d_inode(rdentry)->i_mode & S_IFMT;
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun 	if (type != S_IFDIR) {
1890*4882a593Smuzhiyun 		nfsd_close_cached_files(rdentry);
1891*4882a593Smuzhiyun 		host_err = vfs_unlink(dirp, rdentry, NULL);
1892*4882a593Smuzhiyun 	} else {
1893*4882a593Smuzhiyun 		host_err = vfs_rmdir(dirp, rdentry);
1894*4882a593Smuzhiyun 	}
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun 	if (!host_err)
1897*4882a593Smuzhiyun 		host_err = commit_metadata(fhp);
1898*4882a593Smuzhiyun 	dput(rdentry);
1899*4882a593Smuzhiyun 
1900*4882a593Smuzhiyun out_drop_write:
1901*4882a593Smuzhiyun 	fh_drop_write(fhp);
1902*4882a593Smuzhiyun out_nfserr:
1903*4882a593Smuzhiyun 	if (host_err == -EBUSY) {
1904*4882a593Smuzhiyun 		/* name is mounted-on. There is no perfect
1905*4882a593Smuzhiyun 		 * error status.
1906*4882a593Smuzhiyun 		 */
1907*4882a593Smuzhiyun 		if (nfsd_v4client(rqstp))
1908*4882a593Smuzhiyun 			err = nfserr_file_open;
1909*4882a593Smuzhiyun 		else
1910*4882a593Smuzhiyun 			err = nfserr_acces;
1911*4882a593Smuzhiyun 	} else {
1912*4882a593Smuzhiyun 		err = nfserrno(host_err);
1913*4882a593Smuzhiyun 	}
1914*4882a593Smuzhiyun out:
1915*4882a593Smuzhiyun 	return err;
1916*4882a593Smuzhiyun }
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun /*
1919*4882a593Smuzhiyun  * We do this buffering because we must not call back into the file
1920*4882a593Smuzhiyun  * system's ->lookup() method from the filldir callback. That may well
1921*4882a593Smuzhiyun  * deadlock a number of file systems.
1922*4882a593Smuzhiyun  *
1923*4882a593Smuzhiyun  * This is based heavily on the implementation of same in XFS.
1924*4882a593Smuzhiyun  */
1925*4882a593Smuzhiyun struct buffered_dirent {
1926*4882a593Smuzhiyun 	u64		ino;
1927*4882a593Smuzhiyun 	loff_t		offset;
1928*4882a593Smuzhiyun 	int		namlen;
1929*4882a593Smuzhiyun 	unsigned int	d_type;
1930*4882a593Smuzhiyun 	char		name[];
1931*4882a593Smuzhiyun };
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun struct readdir_data {
1934*4882a593Smuzhiyun 	struct dir_context ctx;
1935*4882a593Smuzhiyun 	char		*dirent;
1936*4882a593Smuzhiyun 	size_t		used;
1937*4882a593Smuzhiyun 	int		full;
1938*4882a593Smuzhiyun };
1939*4882a593Smuzhiyun 
nfsd_buffered_filldir(struct dir_context * ctx,const char * name,int namlen,loff_t offset,u64 ino,unsigned int d_type)1940*4882a593Smuzhiyun static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1941*4882a593Smuzhiyun 				 int namlen, loff_t offset, u64 ino,
1942*4882a593Smuzhiyun 				 unsigned int d_type)
1943*4882a593Smuzhiyun {
1944*4882a593Smuzhiyun 	struct readdir_data *buf =
1945*4882a593Smuzhiyun 		container_of(ctx, struct readdir_data, ctx);
1946*4882a593Smuzhiyun 	struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1947*4882a593Smuzhiyun 	unsigned int reclen;
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun 	reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1950*4882a593Smuzhiyun 	if (buf->used + reclen > PAGE_SIZE) {
1951*4882a593Smuzhiyun 		buf->full = 1;
1952*4882a593Smuzhiyun 		return -EINVAL;
1953*4882a593Smuzhiyun 	}
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun 	de->namlen = namlen;
1956*4882a593Smuzhiyun 	de->offset = offset;
1957*4882a593Smuzhiyun 	de->ino = ino;
1958*4882a593Smuzhiyun 	de->d_type = d_type;
1959*4882a593Smuzhiyun 	memcpy(de->name, name, namlen);
1960*4882a593Smuzhiyun 	buf->used += reclen;
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	return 0;
1963*4882a593Smuzhiyun }
1964*4882a593Smuzhiyun 
nfsd_buffered_readdir(struct file * file,nfsd_filldir_t func,struct readdir_cd * cdp,loff_t * offsetp)1965*4882a593Smuzhiyun static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
1966*4882a593Smuzhiyun 				    struct readdir_cd *cdp, loff_t *offsetp)
1967*4882a593Smuzhiyun {
1968*4882a593Smuzhiyun 	struct buffered_dirent *de;
1969*4882a593Smuzhiyun 	int host_err;
1970*4882a593Smuzhiyun 	int size;
1971*4882a593Smuzhiyun 	loff_t offset;
1972*4882a593Smuzhiyun 	struct readdir_data buf = {
1973*4882a593Smuzhiyun 		.ctx.actor = nfsd_buffered_filldir,
1974*4882a593Smuzhiyun 		.dirent = (void *)__get_free_page(GFP_KERNEL)
1975*4882a593Smuzhiyun 	};
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun 	if (!buf.dirent)
1978*4882a593Smuzhiyun 		return nfserrno(-ENOMEM);
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun 	offset = *offsetp;
1981*4882a593Smuzhiyun 
1982*4882a593Smuzhiyun 	while (1) {
1983*4882a593Smuzhiyun 		unsigned int reclen;
1984*4882a593Smuzhiyun 
1985*4882a593Smuzhiyun 		cdp->err = nfserr_eof; /* will be cleared on successful read */
1986*4882a593Smuzhiyun 		buf.used = 0;
1987*4882a593Smuzhiyun 		buf.full = 0;
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 		host_err = iterate_dir(file, &buf.ctx);
1990*4882a593Smuzhiyun 		if (buf.full)
1991*4882a593Smuzhiyun 			host_err = 0;
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 		if (host_err < 0)
1994*4882a593Smuzhiyun 			break;
1995*4882a593Smuzhiyun 
1996*4882a593Smuzhiyun 		size = buf.used;
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 		if (!size)
1999*4882a593Smuzhiyun 			break;
2000*4882a593Smuzhiyun 
2001*4882a593Smuzhiyun 		de = (struct buffered_dirent *)buf.dirent;
2002*4882a593Smuzhiyun 		while (size > 0) {
2003*4882a593Smuzhiyun 			offset = de->offset;
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 			if (func(cdp, de->name, de->namlen, de->offset,
2006*4882a593Smuzhiyun 				 de->ino, de->d_type))
2007*4882a593Smuzhiyun 				break;
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 			if (cdp->err != nfs_ok)
2010*4882a593Smuzhiyun 				break;
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun 			reclen = ALIGN(sizeof(*de) + de->namlen,
2013*4882a593Smuzhiyun 				       sizeof(u64));
2014*4882a593Smuzhiyun 			size -= reclen;
2015*4882a593Smuzhiyun 			de = (struct buffered_dirent *)((char *)de + reclen);
2016*4882a593Smuzhiyun 		}
2017*4882a593Smuzhiyun 		if (size > 0) /* We bailed out early */
2018*4882a593Smuzhiyun 			break;
2019*4882a593Smuzhiyun 
2020*4882a593Smuzhiyun 		offset = vfs_llseek(file, 0, SEEK_CUR);
2021*4882a593Smuzhiyun 	}
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun 	free_page((unsigned long)(buf.dirent));
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 	if (host_err)
2026*4882a593Smuzhiyun 		return nfserrno(host_err);
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun 	*offsetp = offset;
2029*4882a593Smuzhiyun 	return cdp->err;
2030*4882a593Smuzhiyun }
2031*4882a593Smuzhiyun 
2032*4882a593Smuzhiyun /*
2033*4882a593Smuzhiyun  * Read entries from a directory.
2034*4882a593Smuzhiyun  * The  NFSv3/4 verifier we ignore for now.
2035*4882a593Smuzhiyun  */
2036*4882a593Smuzhiyun __be32
nfsd_readdir(struct svc_rqst * rqstp,struct svc_fh * fhp,loff_t * offsetp,struct readdir_cd * cdp,nfsd_filldir_t func)2037*4882a593Smuzhiyun nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2038*4882a593Smuzhiyun 	     struct readdir_cd *cdp, nfsd_filldir_t func)
2039*4882a593Smuzhiyun {
2040*4882a593Smuzhiyun 	__be32		err;
2041*4882a593Smuzhiyun 	struct file	*file;
2042*4882a593Smuzhiyun 	loff_t		offset = *offsetp;
2043*4882a593Smuzhiyun 	int             may_flags = NFSD_MAY_READ;
2044*4882a593Smuzhiyun 
2045*4882a593Smuzhiyun 	/* NFSv2 only supports 32 bit cookies */
2046*4882a593Smuzhiyun 	if (rqstp->rq_vers > 2)
2047*4882a593Smuzhiyun 		may_flags |= NFSD_MAY_64BIT_COOKIE;
2048*4882a593Smuzhiyun 
2049*4882a593Smuzhiyun 	err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
2050*4882a593Smuzhiyun 	if (err)
2051*4882a593Smuzhiyun 		goto out;
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun 	offset = vfs_llseek(file, offset, SEEK_SET);
2054*4882a593Smuzhiyun 	if (offset < 0) {
2055*4882a593Smuzhiyun 		err = nfserrno((int)offset);
2056*4882a593Smuzhiyun 		goto out_close;
2057*4882a593Smuzhiyun 	}
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 	err = nfsd_buffered_readdir(file, func, cdp, offsetp);
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	if (err == nfserr_eof || err == nfserr_toosmall)
2062*4882a593Smuzhiyun 		err = nfs_ok; /* can still be found in ->err */
2063*4882a593Smuzhiyun out_close:
2064*4882a593Smuzhiyun 	fput(file);
2065*4882a593Smuzhiyun out:
2066*4882a593Smuzhiyun 	return err;
2067*4882a593Smuzhiyun }
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun /*
2070*4882a593Smuzhiyun  * Get file system stats
2071*4882a593Smuzhiyun  * N.B. After this call fhp needs an fh_put
2072*4882a593Smuzhiyun  */
2073*4882a593Smuzhiyun __be32
nfsd_statfs(struct svc_rqst * rqstp,struct svc_fh * fhp,struct kstatfs * stat,int access)2074*4882a593Smuzhiyun nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2075*4882a593Smuzhiyun {
2076*4882a593Smuzhiyun 	__be32 err;
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2079*4882a593Smuzhiyun 	if (!err) {
2080*4882a593Smuzhiyun 		struct path path = {
2081*4882a593Smuzhiyun 			.mnt	= fhp->fh_export->ex_path.mnt,
2082*4882a593Smuzhiyun 			.dentry	= fhp->fh_dentry,
2083*4882a593Smuzhiyun 		};
2084*4882a593Smuzhiyun 		if (vfs_statfs(&path, stat))
2085*4882a593Smuzhiyun 			err = nfserr_io;
2086*4882a593Smuzhiyun 	}
2087*4882a593Smuzhiyun 	return err;
2088*4882a593Smuzhiyun }
2089*4882a593Smuzhiyun 
exp_rdonly(struct svc_rqst * rqstp,struct svc_export * exp)2090*4882a593Smuzhiyun static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2091*4882a593Smuzhiyun {
2092*4882a593Smuzhiyun 	return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2093*4882a593Smuzhiyun }
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V4
2096*4882a593Smuzhiyun /*
2097*4882a593Smuzhiyun  * Helper function to translate error numbers. In the case of xattr operations,
2098*4882a593Smuzhiyun  * some error codes need to be translated outside of the standard translations.
2099*4882a593Smuzhiyun  *
2100*4882a593Smuzhiyun  * ENODATA needs to be translated to nfserr_noxattr.
2101*4882a593Smuzhiyun  * E2BIG to nfserr_xattr2big.
2102*4882a593Smuzhiyun  *
2103*4882a593Smuzhiyun  * Additionally, vfs_listxattr can return -ERANGE. This means that the
2104*4882a593Smuzhiyun  * file has too many extended attributes to retrieve inside an
2105*4882a593Smuzhiyun  * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
2106*4882a593Smuzhiyun  * filesystems will allow the adding of extended attributes until they hit
2107*4882a593Smuzhiyun  * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
2108*4882a593Smuzhiyun  * So, at that point, the attributes are present and valid, but can't
2109*4882a593Smuzhiyun  * be retrieved using listxattr, since the upper level xattr code enforces
2110*4882a593Smuzhiyun  * the XATTR_LIST_MAX limit.
2111*4882a593Smuzhiyun  *
2112*4882a593Smuzhiyun  * This bug means that we need to deal with listxattr returning -ERANGE. The
2113*4882a593Smuzhiyun  * best mapping is to return TOOSMALL.
2114*4882a593Smuzhiyun  */
2115*4882a593Smuzhiyun static __be32
nfsd_xattr_errno(int err)2116*4882a593Smuzhiyun nfsd_xattr_errno(int err)
2117*4882a593Smuzhiyun {
2118*4882a593Smuzhiyun 	switch (err) {
2119*4882a593Smuzhiyun 	case -ENODATA:
2120*4882a593Smuzhiyun 		return nfserr_noxattr;
2121*4882a593Smuzhiyun 	case -E2BIG:
2122*4882a593Smuzhiyun 		return nfserr_xattr2big;
2123*4882a593Smuzhiyun 	case -ERANGE:
2124*4882a593Smuzhiyun 		return nfserr_toosmall;
2125*4882a593Smuzhiyun 	}
2126*4882a593Smuzhiyun 	return nfserrno(err);
2127*4882a593Smuzhiyun }
2128*4882a593Smuzhiyun 
2129*4882a593Smuzhiyun /*
2130*4882a593Smuzhiyun  * Retrieve the specified user extended attribute. To avoid always
2131*4882a593Smuzhiyun  * having to allocate the maximum size (since we are not getting
2132*4882a593Smuzhiyun  * a maximum size from the RPC), do a probe + alloc. Hold a reader
2133*4882a593Smuzhiyun  * lock on i_rwsem to prevent the extended attribute from changing
2134*4882a593Smuzhiyun  * size while we're doing this.
2135*4882a593Smuzhiyun  */
2136*4882a593Smuzhiyun __be32
nfsd_getxattr(struct svc_rqst * rqstp,struct svc_fh * fhp,char * name,void ** bufp,int * lenp)2137*4882a593Smuzhiyun nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2138*4882a593Smuzhiyun 	      void **bufp, int *lenp)
2139*4882a593Smuzhiyun {
2140*4882a593Smuzhiyun 	ssize_t len;
2141*4882a593Smuzhiyun 	__be32 err;
2142*4882a593Smuzhiyun 	char *buf;
2143*4882a593Smuzhiyun 	struct inode *inode;
2144*4882a593Smuzhiyun 	struct dentry *dentry;
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2147*4882a593Smuzhiyun 	if (err)
2148*4882a593Smuzhiyun 		return err;
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	err = nfs_ok;
2151*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
2152*4882a593Smuzhiyun 	inode = d_inode(dentry);
2153*4882a593Smuzhiyun 
2154*4882a593Smuzhiyun 	inode_lock_shared(inode);
2155*4882a593Smuzhiyun 
2156*4882a593Smuzhiyun 	len = vfs_getxattr(dentry, name, NULL, 0);
2157*4882a593Smuzhiyun 
2158*4882a593Smuzhiyun 	/*
2159*4882a593Smuzhiyun 	 * Zero-length attribute, just return.
2160*4882a593Smuzhiyun 	 */
2161*4882a593Smuzhiyun 	if (len == 0) {
2162*4882a593Smuzhiyun 		*bufp = NULL;
2163*4882a593Smuzhiyun 		*lenp = 0;
2164*4882a593Smuzhiyun 		goto out;
2165*4882a593Smuzhiyun 	}
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun 	if (len < 0) {
2168*4882a593Smuzhiyun 		err = nfsd_xattr_errno(len);
2169*4882a593Smuzhiyun 		goto out;
2170*4882a593Smuzhiyun 	}
2171*4882a593Smuzhiyun 
2172*4882a593Smuzhiyun 	if (len > *lenp) {
2173*4882a593Smuzhiyun 		err = nfserr_toosmall;
2174*4882a593Smuzhiyun 		goto out;
2175*4882a593Smuzhiyun 	}
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun 	buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2178*4882a593Smuzhiyun 	if (buf == NULL) {
2179*4882a593Smuzhiyun 		err = nfserr_jukebox;
2180*4882a593Smuzhiyun 		goto out;
2181*4882a593Smuzhiyun 	}
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 	len = vfs_getxattr(dentry, name, buf, len);
2184*4882a593Smuzhiyun 	if (len <= 0) {
2185*4882a593Smuzhiyun 		kvfree(buf);
2186*4882a593Smuzhiyun 		buf = NULL;
2187*4882a593Smuzhiyun 		err = nfsd_xattr_errno(len);
2188*4882a593Smuzhiyun 	}
2189*4882a593Smuzhiyun 
2190*4882a593Smuzhiyun 	*lenp = len;
2191*4882a593Smuzhiyun 	*bufp = buf;
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun out:
2194*4882a593Smuzhiyun 	inode_unlock_shared(inode);
2195*4882a593Smuzhiyun 
2196*4882a593Smuzhiyun 	return err;
2197*4882a593Smuzhiyun }
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun /*
2200*4882a593Smuzhiyun  * Retrieve the xattr names. Since we can't know how many are
2201*4882a593Smuzhiyun  * user extended attributes, we must get all attributes here,
2202*4882a593Smuzhiyun  * and have the XDR encode filter out the "user." ones.
2203*4882a593Smuzhiyun  *
2204*4882a593Smuzhiyun  * While this could always just allocate an XATTR_LIST_MAX
2205*4882a593Smuzhiyun  * buffer, that's a waste, so do a probe + allocate. To
2206*4882a593Smuzhiyun  * avoid any changes between the probe and allocate, wrap
2207*4882a593Smuzhiyun  * this in inode_lock.
2208*4882a593Smuzhiyun  */
2209*4882a593Smuzhiyun __be32
nfsd_listxattr(struct svc_rqst * rqstp,struct svc_fh * fhp,char ** bufp,int * lenp)2210*4882a593Smuzhiyun nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
2211*4882a593Smuzhiyun 	       int *lenp)
2212*4882a593Smuzhiyun {
2213*4882a593Smuzhiyun 	ssize_t len;
2214*4882a593Smuzhiyun 	__be32 err;
2215*4882a593Smuzhiyun 	char *buf;
2216*4882a593Smuzhiyun 	struct inode *inode;
2217*4882a593Smuzhiyun 	struct dentry *dentry;
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2220*4882a593Smuzhiyun 	if (err)
2221*4882a593Smuzhiyun 		return err;
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun 	dentry = fhp->fh_dentry;
2224*4882a593Smuzhiyun 	inode = d_inode(dentry);
2225*4882a593Smuzhiyun 	*lenp = 0;
2226*4882a593Smuzhiyun 
2227*4882a593Smuzhiyun 	inode_lock_shared(inode);
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun 	len = vfs_listxattr(dentry, NULL, 0);
2230*4882a593Smuzhiyun 	if (len <= 0) {
2231*4882a593Smuzhiyun 		err = nfsd_xattr_errno(len);
2232*4882a593Smuzhiyun 		goto out;
2233*4882a593Smuzhiyun 	}
2234*4882a593Smuzhiyun 
2235*4882a593Smuzhiyun 	if (len > XATTR_LIST_MAX) {
2236*4882a593Smuzhiyun 		err = nfserr_xattr2big;
2237*4882a593Smuzhiyun 		goto out;
2238*4882a593Smuzhiyun 	}
2239*4882a593Smuzhiyun 
2240*4882a593Smuzhiyun 	/*
2241*4882a593Smuzhiyun 	 * We're holding i_rwsem - use GFP_NOFS.
2242*4882a593Smuzhiyun 	 */
2243*4882a593Smuzhiyun 	buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2244*4882a593Smuzhiyun 	if (buf == NULL) {
2245*4882a593Smuzhiyun 		err = nfserr_jukebox;
2246*4882a593Smuzhiyun 		goto out;
2247*4882a593Smuzhiyun 	}
2248*4882a593Smuzhiyun 
2249*4882a593Smuzhiyun 	len = vfs_listxattr(dentry, buf, len);
2250*4882a593Smuzhiyun 	if (len <= 0) {
2251*4882a593Smuzhiyun 		kvfree(buf);
2252*4882a593Smuzhiyun 		err = nfsd_xattr_errno(len);
2253*4882a593Smuzhiyun 		goto out;
2254*4882a593Smuzhiyun 	}
2255*4882a593Smuzhiyun 
2256*4882a593Smuzhiyun 	*lenp = len;
2257*4882a593Smuzhiyun 	*bufp = buf;
2258*4882a593Smuzhiyun 
2259*4882a593Smuzhiyun 	err = nfs_ok;
2260*4882a593Smuzhiyun out:
2261*4882a593Smuzhiyun 	inode_unlock_shared(inode);
2262*4882a593Smuzhiyun 
2263*4882a593Smuzhiyun 	return err;
2264*4882a593Smuzhiyun }
2265*4882a593Smuzhiyun 
2266*4882a593Smuzhiyun /*
2267*4882a593Smuzhiyun  * Removexattr and setxattr need to call fh_lock to both lock the inode
2268*4882a593Smuzhiyun  * and set the change attribute. Since the top-level vfs_removexattr
2269*4882a593Smuzhiyun  * and vfs_setxattr calls already do their own inode_lock calls, call
2270*4882a593Smuzhiyun  * the _locked variant. Pass in a NULL pointer for delegated_inode,
2271*4882a593Smuzhiyun  * and let the client deal with NFS4ERR_DELAY (same as with e.g.
2272*4882a593Smuzhiyun  * setattr and remove).
2273*4882a593Smuzhiyun  */
2274*4882a593Smuzhiyun __be32
nfsd_removexattr(struct svc_rqst * rqstp,struct svc_fh * fhp,char * name)2275*4882a593Smuzhiyun nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
2276*4882a593Smuzhiyun {
2277*4882a593Smuzhiyun 	__be32 err;
2278*4882a593Smuzhiyun 	int ret;
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2281*4882a593Smuzhiyun 	if (err)
2282*4882a593Smuzhiyun 		return err;
2283*4882a593Smuzhiyun 
2284*4882a593Smuzhiyun 	ret = fh_want_write(fhp);
2285*4882a593Smuzhiyun 	if (ret)
2286*4882a593Smuzhiyun 		return nfserrno(ret);
2287*4882a593Smuzhiyun 
2288*4882a593Smuzhiyun 	fh_lock(fhp);
2289*4882a593Smuzhiyun 
2290*4882a593Smuzhiyun 	ret = __vfs_removexattr_locked(fhp->fh_dentry, name, NULL);
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun 	fh_unlock(fhp);
2293*4882a593Smuzhiyun 	fh_drop_write(fhp);
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 	return nfsd_xattr_errno(ret);
2296*4882a593Smuzhiyun }
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun __be32
nfsd_setxattr(struct svc_rqst * rqstp,struct svc_fh * fhp,char * name,void * buf,u32 len,u32 flags)2299*4882a593Smuzhiyun nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2300*4882a593Smuzhiyun 	      void *buf, u32 len, u32 flags)
2301*4882a593Smuzhiyun {
2302*4882a593Smuzhiyun 	__be32 err;
2303*4882a593Smuzhiyun 	int ret;
2304*4882a593Smuzhiyun 
2305*4882a593Smuzhiyun 	err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2306*4882a593Smuzhiyun 	if (err)
2307*4882a593Smuzhiyun 		return err;
2308*4882a593Smuzhiyun 
2309*4882a593Smuzhiyun 	ret = fh_want_write(fhp);
2310*4882a593Smuzhiyun 	if (ret)
2311*4882a593Smuzhiyun 		return nfserrno(ret);
2312*4882a593Smuzhiyun 	fh_lock(fhp);
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun 	ret = __vfs_setxattr_locked(fhp->fh_dentry, name, buf, len, flags,
2315*4882a593Smuzhiyun 				    NULL);
2316*4882a593Smuzhiyun 
2317*4882a593Smuzhiyun 	fh_unlock(fhp);
2318*4882a593Smuzhiyun 	fh_drop_write(fhp);
2319*4882a593Smuzhiyun 
2320*4882a593Smuzhiyun 	return nfsd_xattr_errno(ret);
2321*4882a593Smuzhiyun }
2322*4882a593Smuzhiyun #endif
2323*4882a593Smuzhiyun 
2324*4882a593Smuzhiyun /*
2325*4882a593Smuzhiyun  * Check for a user's access permissions to this inode.
2326*4882a593Smuzhiyun  */
2327*4882a593Smuzhiyun __be32
nfsd_permission(struct svc_rqst * rqstp,struct svc_export * exp,struct dentry * dentry,int acc)2328*4882a593Smuzhiyun nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2329*4882a593Smuzhiyun 					struct dentry *dentry, int acc)
2330*4882a593Smuzhiyun {
2331*4882a593Smuzhiyun 	struct inode	*inode = d_inode(dentry);
2332*4882a593Smuzhiyun 	int		err;
2333*4882a593Smuzhiyun 
2334*4882a593Smuzhiyun 	if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2335*4882a593Smuzhiyun 		return 0;
2336*4882a593Smuzhiyun #if 0
2337*4882a593Smuzhiyun 	dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2338*4882a593Smuzhiyun 		acc,
2339*4882a593Smuzhiyun 		(acc & NFSD_MAY_READ)?	" read"  : "",
2340*4882a593Smuzhiyun 		(acc & NFSD_MAY_WRITE)?	" write" : "",
2341*4882a593Smuzhiyun 		(acc & NFSD_MAY_EXEC)?	" exec"  : "",
2342*4882a593Smuzhiyun 		(acc & NFSD_MAY_SATTR)?	" sattr" : "",
2343*4882a593Smuzhiyun 		(acc & NFSD_MAY_TRUNC)?	" trunc" : "",
2344*4882a593Smuzhiyun 		(acc & NFSD_MAY_LOCK)?	" lock"  : "",
2345*4882a593Smuzhiyun 		(acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2346*4882a593Smuzhiyun 		inode->i_mode,
2347*4882a593Smuzhiyun 		IS_IMMUTABLE(inode)?	" immut" : "",
2348*4882a593Smuzhiyun 		IS_APPEND(inode)?	" append" : "",
2349*4882a593Smuzhiyun 		__mnt_is_readonly(exp->ex_path.mnt)?	" ro" : "");
2350*4882a593Smuzhiyun 	dprintk("      owner %d/%d user %d/%d\n",
2351*4882a593Smuzhiyun 		inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2352*4882a593Smuzhiyun #endif
2353*4882a593Smuzhiyun 
2354*4882a593Smuzhiyun 	/* Normally we reject any write/sattr etc access on a read-only file
2355*4882a593Smuzhiyun 	 * system.  But if it is IRIX doing check on write-access for a
2356*4882a593Smuzhiyun 	 * device special file, we ignore rofs.
2357*4882a593Smuzhiyun 	 */
2358*4882a593Smuzhiyun 	if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2359*4882a593Smuzhiyun 		if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2360*4882a593Smuzhiyun 			if (exp_rdonly(rqstp, exp) ||
2361*4882a593Smuzhiyun 			    __mnt_is_readonly(exp->ex_path.mnt))
2362*4882a593Smuzhiyun 				return nfserr_rofs;
2363*4882a593Smuzhiyun 			if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2364*4882a593Smuzhiyun 				return nfserr_perm;
2365*4882a593Smuzhiyun 		}
2366*4882a593Smuzhiyun 	if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2367*4882a593Smuzhiyun 		return nfserr_perm;
2368*4882a593Smuzhiyun 
2369*4882a593Smuzhiyun 	if (acc & NFSD_MAY_LOCK) {
2370*4882a593Smuzhiyun 		/* If we cannot rely on authentication in NLM requests,
2371*4882a593Smuzhiyun 		 * just allow locks, otherwise require read permission, or
2372*4882a593Smuzhiyun 		 * ownership
2373*4882a593Smuzhiyun 		 */
2374*4882a593Smuzhiyun 		if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2375*4882a593Smuzhiyun 			return 0;
2376*4882a593Smuzhiyun 		else
2377*4882a593Smuzhiyun 			acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2378*4882a593Smuzhiyun 	}
2379*4882a593Smuzhiyun 	/*
2380*4882a593Smuzhiyun 	 * The file owner always gets access permission for accesses that
2381*4882a593Smuzhiyun 	 * would normally be checked at open time. This is to make
2382*4882a593Smuzhiyun 	 * file access work even when the client has done a fchmod(fd, 0).
2383*4882a593Smuzhiyun 	 *
2384*4882a593Smuzhiyun 	 * However, `cp foo bar' should fail nevertheless when bar is
2385*4882a593Smuzhiyun 	 * readonly. A sensible way to do this might be to reject all
2386*4882a593Smuzhiyun 	 * attempts to truncate a read-only file, because a creat() call
2387*4882a593Smuzhiyun 	 * always implies file truncation.
2388*4882a593Smuzhiyun 	 * ... but this isn't really fair.  A process may reasonably call
2389*4882a593Smuzhiyun 	 * ftruncate on an open file descriptor on a file with perm 000.
2390*4882a593Smuzhiyun 	 * We must trust the client to do permission checking - using "ACCESS"
2391*4882a593Smuzhiyun 	 * with NFSv3.
2392*4882a593Smuzhiyun 	 */
2393*4882a593Smuzhiyun 	if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2394*4882a593Smuzhiyun 	    uid_eq(inode->i_uid, current_fsuid()))
2395*4882a593Smuzhiyun 		return 0;
2396*4882a593Smuzhiyun 
2397*4882a593Smuzhiyun 	/* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2398*4882a593Smuzhiyun 	err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2399*4882a593Smuzhiyun 
2400*4882a593Smuzhiyun 	/* Allow read access to binaries even when mode 111 */
2401*4882a593Smuzhiyun 	if (err == -EACCES && S_ISREG(inode->i_mode) &&
2402*4882a593Smuzhiyun 	     (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2403*4882a593Smuzhiyun 	      acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2404*4882a593Smuzhiyun 		err = inode_permission(inode, MAY_EXEC);
2405*4882a593Smuzhiyun 
2406*4882a593Smuzhiyun 	return err? nfserrno(err) : 0;
2407*4882a593Smuzhiyun }
2408