blob: 8593c642333691ba54cec9cc837bd8e0917af94a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * File operations used by nfsd. Some of these have been ripped from
4 * other parts of the kernel because they weren't exported, others
5 * are partial duplicates with added or changed functionality.
6 *
7 * Note that several functions dget() the dentry upon which they want
8 * to act, most notably those that create directory entries. Response
9 * dentry's are dput()'d if necessary in the release callback.
10 * So if you notice code paths that apparently fail to dput() the
11 * dentry, don't worry--they have been taken care of.
12 *
13 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/fs.h>
18#include <linux/file.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020019#include <linux/splice.h>
Anna Schumaker95d871f2014-11-07 14:44:26 -050020#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/xattr.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020027#include <linux/jhash.h>
28#include <linux/ima.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080030#include <linux/uaccess.h>
Ben Myersf5019122010-02-17 14:05:11 -060031#include <linux/exportfs.h>
32#include <linux/writeback.h>
David Quigley18032ca2013-05-02 13:19:10 -040033#include <linux/security.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020034
35#ifdef CONFIG_NFSD_V3
36#include "xdr3.h"
37#endif /* CONFIG_NFSD_V3 */
38
Christoph Hellwig5be196e2006-01-09 20:51:55 -080039#ifdef CONFIG_NFSD_V4
Christoph Hellwigffa01602015-12-03 12:59:52 +010040#include "../internal.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050041#include "acl.h"
42#include "idmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#endif /* CONFIG_NFSD_V4 */
44
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "nfsd.h"
46#include "vfs.h"
Jeff Laytonb4935232019-08-18 14:18:49 -040047#include "filecache.h"
Jeff Layton6e8b50d2015-11-17 06:52:23 -050048#include "trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define NFSDDBG_FACILITY NFSDDBG_FILEOP
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * This is a cache of readahead params that help us choose the proper
55 * readahead strategy. Initially, we set all readahead parameters to 0
56 * and let the VFS handle things.
57 * If you increase the number of cached files very much, you'll need to
58 * add a hash table here.
59 */
60struct raparms {
61 struct raparms *p_next;
62 unsigned int p_count;
63 ino_t p_ino;
64 dev_t p_dev;
65 int p_set;
66 struct file_ra_state p_ra;
Greg Banksfce14562006-10-04 02:15:49 -070067 unsigned int p_hindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Greg Banksfce14562006-10-04 02:15:49 -070070struct raparm_hbucket {
71 struct raparms *pb_head;
72 spinlock_t pb_lock;
73} ____cacheline_aligned_in_smp;
74
Greg Banksfce14562006-10-04 02:15:49 -070075#define RAPARM_HASH_BITS 4
76#define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
77#define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
78static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
82 * a mount point.
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080083 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * or nfs_ok having possibly changed *dpp and *expp
85 */
86int
87nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
88 struct svc_export **expp)
89{
90 struct svc_export *exp = *expp, *exp2 = NULL;
91 struct dentry *dentry = *dpp;
Al Viro91c9fa82009-04-18 02:42:05 -040092 struct path path = {.mnt = mntget(exp->ex_path.mnt),
93 .dentry = dget(dentry)};
Al Viro6264d692006-10-19 23:28:58 -070094 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Al Viro7cc90cc2011-03-18 09:04:20 -040096 err = follow_down(&path);
David Howellscc53ce52011-01-14 18:45:26 +000097 if (err < 0)
98 goto out;
NeilBrown99bbf6e2017-03-15 12:40:44 +110099 if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
100 nfsd_mountpoint(dentry, exp) == 2) {
101 /* This is only a mountpoint in some other namespace */
102 path_put(&path);
103 goto out;
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Al Viro91c9fa82009-04-18 02:42:05 -0400106 exp2 = rqst_exp_get_by_name(rqstp, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (IS_ERR(exp2)) {
J. Bruce Fields3b6cee7bc2009-10-25 21:18:19 -0400108 err = PTR_ERR(exp2);
109 /*
110 * We normally allow NFS clients to continue
111 * "underneath" a mountpoint that is not exported.
112 * The exception is V4ROOT, where no traversal is ever
113 * allowed without an explicit export of the new
114 * directory.
115 */
116 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
117 err = 0;
Al Viro91c9fa82009-04-18 02:42:05 -0400118 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 goto out;
120 }
Steve Dickson3c394dd2009-09-09 15:02:40 -0400121 if (nfsd_v4client(rqstp) ||
122 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* successfully crossed mount point */
Al Viro1644ccc2009-04-18 02:32:31 -0400124 /*
Al Viro91c9fa82009-04-18 02:42:05 -0400125 * This is subtle: path.dentry is *not* on path.mnt
126 * at this point. The only reason we are safe is that
127 * original mnt is pinned down by exp, so we should
128 * put path *before* putting exp
Al Viro1644ccc2009-04-18 02:32:31 -0400129 */
Al Viro91c9fa82009-04-18 02:42:05 -0400130 *dpp = path.dentry;
131 path.dentry = dentry;
Al Viro1644ccc2009-04-18 02:32:31 -0400132 *expp = exp2;
Al Viro91c9fa82009-04-18 02:42:05 -0400133 exp2 = exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Al Viro91c9fa82009-04-18 02:42:05 -0400135 path_put(&path);
136 exp_put(exp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137out:
138 return err;
139}
140
J. Bruce Fields289ede42009-09-26 20:32:24 -0400141static void follow_to_parent(struct path *path)
142{
143 struct dentry *dp;
144
145 while (path->dentry == path->mnt->mnt_root && follow_up(path))
146 ;
147 dp = dget_parent(path->dentry);
148 dput(path->dentry);
149 path->dentry = dp;
150}
151
152static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
153{
154 struct svc_export *exp2;
155 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
156 .dentry = dget(dparent)};
157
158 follow_to_parent(&path);
159
160 exp2 = rqst_exp_parent(rqstp, &path);
161 if (PTR_ERR(exp2) == -ENOENT) {
162 *dentryp = dget(dparent);
163 } else if (IS_ERR(exp2)) {
164 path_put(&path);
165 return PTR_ERR(exp2);
166 } else {
167 *dentryp = dget(path.dentry);
168 exp_put(*exp);
169 *exp = exp2;
170 }
171 path_put(&path);
172 return 0;
173}
174
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400175/*
176 * For nfsd purposes, we treat V4ROOT exports as though there was an
177 * export at *every* directory.
NeilBrown99bbf6e2017-03-15 12:40:44 +1100178 * We return:
179 * '1' if this dentry *must* be an export point,
180 * '2' if it might be, if there is really a mount here, and
181 * '0' if there is no chance of an export point here.
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400182 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -0400183int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400184{
NeilBrown99bbf6e2017-03-15 12:40:44 +1100185 if (!d_inode(dentry))
186 return 0;
187 if (exp->ex_flags & NFSEXP_V4ROOT)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400188 return 1;
Trond Myklebust11fcee022011-09-12 19:37:26 -0400189 if (nfsd4_is_junction(dentry))
190 return 1;
NeilBrown99bbf6e2017-03-15 12:40:44 +1100191 if (d_mountpoint(dentry))
192 /*
193 * Might only be a mountpoint in a different namespace,
194 * but we need to check.
195 */
196 return 2;
197 return 0;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400198}
199
Al Viro6264d692006-10-19 23:28:58 -0700200__be32
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700201nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400202 const char *name, unsigned int len,
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700203 struct svc_export **exp_ret, struct dentry **dentry_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct svc_export *exp;
206 struct dentry *dparent;
207 struct dentry *dentry;
Al Viro6264d692006-10-19 23:28:58 -0700208 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 dparent = fhp->fh_dentry;
Kinglong Meebf18f162014-06-10 22:06:44 +0800213 exp = exp_get(fhp->fh_export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Lookup the name, but don't follow links */
216 if (isdotent(name, len)) {
217 if (len==1)
218 dentry = dget(dparent);
Jan Blunck54775492008-02-14 19:38:39 -0800219 else if (dparent != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dentry = dget_parent(dparent);
J. Bruce Fieldsfed83812009-09-26 16:53:01 -0400221 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 dentry = dget(dparent); /* .. == . just like at / */
223 else {
224 /* checking mountpoint crossing is very different when stepping up */
J. Bruce Fields289ede42009-09-26 20:32:24 -0400225 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
226 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 } else {
J. Bruce Fields43357232014-01-24 18:04:40 -0500230 /*
231 * In the nfsd4_open() case, this may be held across
232 * subsequent open and delegation acquisition which may
233 * need to take the child's i_mutex:
234 */
235 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 dentry = lookup_one_len(name, dparent, len);
Al Viro6264d692006-10-19 23:28:58 -0700237 host_err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (IS_ERR(dentry))
239 goto out_nfserr;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400240 if (nfsd_mountpoint(dentry, exp)) {
NeilBrownbbddca82016-01-07 16:08:20 -0500241 /*
242 * We don't need the i_mutex after all. It's
243 * still possible we could open this (regular
244 * files can be mountpoints too), but the
245 * i_mutex is just there to prevent renames of
246 * something that we might be about to delegate,
247 * and a mountpoint won't be renamed:
248 */
249 fh_unlock(fhp);
Al Viro6264d692006-10-19 23:28:58 -0700250 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 dput(dentry);
252 goto out_nfserr;
253 }
254 }
255 }
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700256 *dentry_ret = dentry;
257 *exp_ret = exp;
258 return 0;
259
260out_nfserr:
261 exp_put(exp);
262 return nfserrno(host_err);
263}
264
265/*
266 * Look up one component of a pathname.
267 * N.B. After this call _both_ fhp and resfh need an fh_put
268 *
269 * If the lookup would cross a mountpoint, and the mounted filesystem
270 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
271 * accepted as it stands and the mounted directory is
272 * returned. Otherwise the covered directory is returned.
273 * NOTE: this mountpoint crossing is not supported properly by all
274 * clients and is explicitly disallowed for NFSv3
275 * NeilBrown <neilb@cse.unsw.edu.au>
276 */
277__be32
278nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400279 unsigned int len, struct svc_fh *resfh)
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700280{
281 struct svc_export *exp;
282 struct dentry *dentry;
283 __be32 err;
284
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400285 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
286 if (err)
287 return err;
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700288 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
289 if (err)
290 return err;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700291 err = check_nfsd_access(exp, rqstp);
292 if (err)
293 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
295 * Note: we compose the file handle now, but as the
296 * dentry may be negative, it may need to be updated.
297 */
298 err = fh_compose(resfh, exp, dentry, fhp);
David Howells2b0143b2015-03-17 22:25:59 +0000299 if (!err && d_really_is_negative(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 err = nfserr_noent;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700301out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 exp_put(exp);
304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Ben Myersf5019122010-02-17 14:05:11 -0600307/*
308 * Commit metadata changes to stable storage.
309 */
310static int
311commit_metadata(struct svc_fh *fhp)
312{
David Howells2b0143b2015-03-17 22:25:59 +0000313 struct inode *inode = d_inode(fhp->fh_dentry);
Ben Myersf5019122010-02-17 14:05:11 -0600314 const struct export_operations *export_ops = inode->i_sb->s_export_op;
Ben Myersf5019122010-02-17 14:05:11 -0600315
316 if (!EX_ISSYNC(fhp->fh_export))
317 return 0;
318
Christoph Hellwigc37650162010-10-06 10:48:20 +0200319 if (export_ops->commit_metadata)
320 return export_ops->commit_metadata(inode);
321 return sync_inode_metadata(inode, 1);
Ben Myersf5019122010-02-17 14:05:11 -0600322}
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800325 * Go over the attributes and take care of the small differences between
326 * NFS semantics and what Linux expects.
327 */
328static void
329nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
330{
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800331 /* sanitize the mode change */
332 if (iap->ia_valid & ATTR_MODE) {
333 iap->ia_mode &= S_IALLUGO;
334 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
335 }
336
337 /* Revoke setuid/setgid on chown */
338 if (!S_ISDIR(inode->i_mode) &&
Stanislav Kholmanskikhc4fa6d72013-12-11 14:16:36 +0400339 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800340 iap->ia_valid |= ATTR_KILL_PRIV;
341 if (iap->ia_valid & ATTR_MODE) {
342 /* we're setting mode too, just clear the s*id bits */
343 iap->ia_mode &= ~S_ISUID;
344 if (iap->ia_mode & S_IXGRP)
345 iap->ia_mode &= ~S_ISGID;
346 } else {
347 /* set ATTR_KILL_* bits and let VFS handle it */
348 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
349 }
350 }
351}
352
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500353static __be32
354nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
355 struct iattr *iap)
356{
357 struct inode *inode = d_inode(fhp->fh_dentry);
358 int host_err;
359
360 if (iap->ia_size < inode->i_size) {
361 __be32 err;
362
363 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
364 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
365 if (err)
366 return err;
367 }
368
369 host_err = get_write_access(inode);
370 if (host_err)
371 goto out_nfserrno;
372
373 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
374 if (host_err)
375 goto out_put_write_access;
376 return 0;
377
378out_put_write_access:
379 put_write_access(inode);
380out_nfserrno:
381 return nfserrno(host_err);
382}
383
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800384/*
385 * Set various file attributes. After this call fhp needs an fh_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
Al Viro6264d692006-10-19 23:28:58 -0700387__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
389 int check_guard, time_t guardtime)
390{
391 struct dentry *dentry;
392 struct inode *inode;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200393 int accmode = NFSD_MAY_SATTR;
Al Viro175a4eb2011-07-26 03:30:54 -0400394 umode_t ftype = 0;
Al Viro6264d692006-10-19 23:28:58 -0700395 __be32 err;
396 int host_err;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500397 bool get_write_count;
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500398 bool size_change = (iap->ia_valid & ATTR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
zhengbin255fbca2018-11-30 16:04:25 +0800400 if (iap->ia_valid & ATTR_SIZE) {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200401 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 ftype = S_IFREG;
zhengbin255fbca2018-11-30 16:04:25 +0800403 }
404
405 /*
406 * If utimes(2) and friends are called with times not NULL, we should
407 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
Geert Uytterhoevene977cc82019-05-27 14:21:32 +0200408 * will return EACCES, when the caller's effective UID does not match
zhengbin255fbca2018-11-30 16:04:25 +0800409 * the owner of the file, and the caller is not privileged. In this
410 * situation, we should return EPERM(notify_change will return this).
411 */
412 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
413 accmode |= NFSD_MAY_OWNER_OVERRIDE;
414 if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
415 accmode |= NFSD_MAY_WRITE;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500418 /* Callers that do fh_verify should do the fh_want_write: */
419 get_write_count = !fhp->fh_dentry;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* Get inode */
422 err = fh_verify(rqstp, fhp, ftype, accmode);
NeilBrown15b7a1b2005-11-07 01:00:23 -0800423 if (err)
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500424 return err;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500425 if (get_write_count) {
426 host_err = fh_want_write(fhp);
427 if (host_err)
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500428 goto out;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000432 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
NeilBrown15b7a1b2005-11-07 01:00:23 -0800434 /* Ignore any mode updates on symlinks */
435 if (S_ISLNK(inode->i_mode))
436 iap->ia_valid &= ~ATTR_MODE;
437
438 if (!iap->ia_valid)
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500439 return 0;
NeilBrown15b7a1b2005-11-07 01:00:23 -0800440
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800441 nfsd_sanitize_attrs(inode, iap);
442
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500443 if (check_guard && guardtime != inode->i_ctime.tv_sec)
444 return nfserr_notsync;
445
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000446 /*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800447 * The size case is special, it changes the file in addition to the
Christoph Hellwig783112f2017-02-20 07:21:33 +0100448 * attributes, and file systems don't expect it to be mixed with
449 * "random" attribute changes. We thus split out the size change
450 * into a separate call to ->setattr, and do the rest as a separate
451 * setattr call.
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000452 */
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500453 if (size_change) {
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500454 err = nfsd_get_write_access(rqstp, fhp, iap);
455 if (err)
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500456 return err;
Christoph Hellwig783112f2017-02-20 07:21:33 +0100457 }
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700458
Christoph Hellwig783112f2017-02-20 07:21:33 +0100459 fh_lock(fhp);
460 if (size_change) {
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700461 /*
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500462 * RFC5661, Section 18.30.4:
463 * Changing the size of a file with SETATTR indirectly
464 * changes the time_modify and change attributes.
465 *
466 * (and similar for the older RFCs)
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700467 */
Christoph Hellwig783112f2017-02-20 07:21:33 +0100468 struct iattr size_attr = {
469 .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
470 .ia_size = iap->ia_size,
471 };
472
473 host_err = notify_change(dentry, &size_attr, NULL);
474 if (host_err)
475 goto out_unlock;
476 iap->ia_valid &= ~ATTR_SIZE;
477
478 /*
479 * Avoid the additional setattr call below if the only other
480 * attribute that the client sends is the mtime, as we update
481 * it as part of the size change above.
482 */
483 if ((iap->ia_valid & ~ATTR_MTIME) == 0)
484 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 iap->ia_valid |= ATTR_CTIME;
Christoph Hellwig987da472013-11-18 05:07:47 -0800488 host_err = notify_change(dentry, iap, NULL);
Christoph Hellwig987da472013-11-18 05:07:47 -0800489
Christoph Hellwig783112f2017-02-20 07:21:33 +0100490out_unlock:
491 fh_unlock(fhp);
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500492 if (size_change)
493 put_write_access(inode);
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500494out:
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500495 if (!host_err)
496 host_err = commit_metadata(fhp);
497 return nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800500#if defined(CONFIG_NFSD_V4)
Chuck Lever9b4146e2012-01-04 16:26:43 -0500501/*
502 * NFS junction information is stored in an extended attribute.
503 */
504#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
505
506/**
507 * nfsd4_is_junction - Test if an object could be an NFS junction
508 *
509 * @dentry: object to test
510 *
511 * Returns 1 if "dentry" appears to contain NFS junction information.
512 * Otherwise 0 is returned.
513 */
Trond Myklebust11fcee022011-09-12 19:37:26 -0400514int nfsd4_is_junction(struct dentry *dentry)
515{
David Howells2b0143b2015-03-17 22:25:59 +0000516 struct inode *inode = d_inode(dentry);
Trond Myklebust11fcee022011-09-12 19:37:26 -0400517
518 if (inode == NULL)
519 return 0;
520 if (inode->i_mode & S_IXUGO)
521 return 0;
522 if (!(inode->i_mode & S_ISVTX))
523 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500524 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee022011-09-12 19:37:26 -0400525 return 0;
526 return 1;
527}
David Quigley18032ca2013-05-02 13:19:10 -0400528#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
529__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
530 struct xdr_netobj *label)
531{
532 __be32 error;
533 int host_error;
534 struct dentry *dentry;
535
536 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
537 if (error)
538 return error;
539
540 dentry = fhp->fh_dentry;
541
Al Viro59551022016-01-22 15:40:57 -0500542 inode_lock(d_inode(dentry));
David Quigley18032ca2013-05-02 13:19:10 -0400543 host_error = security_inode_setsecctx(dentry, label->data, label->len);
Al Viro59551022016-01-22 15:40:57 -0500544 inode_unlock(d_inode(dentry));
David Quigley18032ca2013-05-02 13:19:10 -0400545 return nfserrno(host_error);
546}
547#else
548__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
549 struct xdr_netobj *label)
550{
551 return nfserr_notsupp;
552}
553#endif
554
Christoph Hellwigffa01602015-12-03 12:59:52 +0100555__be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
556 u64 dst_pos, u64 count)
557{
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100558 loff_t cloned;
559
Darrick J. Wong452ce652018-10-30 10:41:56 +1100560 cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
Trond Myklebuste3fdc892019-01-21 15:58:38 -0500561 if (cloned < 0)
562 return nfserrno(cloned);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100563 if (count && cloned != count)
Trond Myklebuste3fdc892019-01-21 15:58:38 -0500564 return nfserrno(-EINVAL);
565 return 0;
Christoph Hellwigffa01602015-12-03 12:59:52 +0100566}
567
Anna Schumaker29ae7f92016-09-07 15:57:30 -0400568ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
569 u64 dst_pos, u64 count)
570{
571
572 /*
573 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
574 * thread and client rpc slot. The choice of 4MB is somewhat
575 * arbitrary. We might instead base this on r/wsize, or make it
576 * tunable, or use a time instead of a byte limit, or implement
577 * asynchronous copy. In theory a client could also recognize a
578 * limit like this and pipeline multiple COPY requests.
579 */
580 count = min_t(u64, count, 1 << 22);
581 return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
582}
583
Anna Schumaker95d871f2014-11-07 14:44:26 -0500584__be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
585 struct file *file, loff_t offset, loff_t len,
586 int flags)
587{
Anna Schumaker95d871f2014-11-07 14:44:26 -0500588 int error;
589
590 if (!S_ISREG(file_inode(file)->i_mode))
591 return nfserr_inval;
592
Anna Schumaker95d871f2014-11-07 14:44:26 -0500593 error = vfs_fallocate(file, flags, offset, len);
594 if (!error)
595 error = commit_metadata(fhp);
596
597 return nfserrno(error);
598}
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400599#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601#ifdef CONFIG_NFSD_V3
602/*
603 * Check server access rights to a file system object
604 */
605struct accessmap {
606 u32 access;
607 int how;
608};
609static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200610 { NFS3_ACCESS_READ, NFSD_MAY_READ },
611 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
612 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
613 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 { 0, 0 }
616};
617
618static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200619 { NFS3_ACCESS_READ, NFSD_MAY_READ },
620 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
621 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
622 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
623 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 { 0, 0 }
626};
627
628static struct accessmap nfs3_anyaccess[] = {
629 /* Some clients - Solaris 2.6 at least, make an access call
630 * to the server to check for access for things like /dev/null
631 * (which really, the server doesn't care about). So
632 * We provide simple access checking for them, looking
633 * mainly at mode bits, and we make sure to ignore read-only
634 * filesystem checks
635 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200636 { NFS3_ACCESS_READ, NFSD_MAY_READ },
637 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
638 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
639 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 { 0, 0 }
642};
643
Al Viro6264d692006-10-19 23:28:58 -0700644__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
646{
647 struct accessmap *map;
648 struct svc_export *export;
649 struct dentry *dentry;
650 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700651 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200653 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (error)
655 goto out;
656
657 export = fhp->fh_export;
658 dentry = fhp->fh_dentry;
659
David Howellse36cb0b2015-01-29 12:02:35 +0000660 if (d_is_reg(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 map = nfs3_regaccess;
David Howellse36cb0b2015-01-29 12:02:35 +0000662 else if (d_is_dir(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 map = nfs3_diraccess;
664 else
665 map = nfs3_anyaccess;
666
667
668 query = *access;
669 for (; map->access; map++) {
670 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700671 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 sresult |= map->access;
674
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700675 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 switch (err2) {
677 case nfs_ok:
678 result |= map->access;
679 break;
680
681 /* the following error codes just mean the access was not allowed,
682 * rather than an error occurred */
683 case nfserr_rofs:
684 case nfserr_acces:
685 case nfserr_perm:
686 /* simply don't "or" in the access bit. */
687 break;
688 default:
689 error = err2;
690 goto out;
691 }
692 }
693 }
694 *access = result;
695 if (supported)
696 *supported = sresult;
697
698 out:
699 return error;
700}
701#endif /* CONFIG_NFSD_V3 */
702
Jeff Layton65294c12019-08-18 14:18:48 -0400703int nfsd_open_break_lease(struct inode *inode, int access)
J. Bruce Fields105f4622011-06-07 11:50:23 -0400704{
705 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
J. Bruce Fields105f4622011-06-07 11:50:23 -0400707 if (access & NFSD_MAY_NOT_BREAK_LEASE)
708 return 0;
709 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
710 return break_lease(inode, mode | O_NONBLOCK);
711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713/*
714 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400715 * The may_flags argument indicates the type of open (read/write/lock)
716 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * N.B. After this call fhp needs an fh_put
718 */
Jeff Layton65294c12019-08-18 14:18:48 -0400719static __be32
720__nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400721 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Al Viro765927b2012-06-26 21:58:53 +0400723 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct inode *inode;
Kinglong Mee8519f992014-09-03 08:14:06 +0800725 struct file *file;
Al Viro6264d692006-10-19 23:28:58 -0700726 int flags = O_RDONLY|O_LARGEFILE;
727 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400728 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Al Viro765927b2012-06-26 21:58:53 +0400730 path.mnt = fhp->fh_export->ex_path.mnt;
731 path.dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000732 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* Disallow write access to files with the append-only bit set
735 * or any access when mandatory locking enabled
736 */
737 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400738 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400740 /*
741 * We must ignore files (but only files) which might have mandatory
742 * locks on them because there is no way to know if the accesser has
743 * the lock.
744 */
745 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto out;
747
748 if (!inode->i_fop)
749 goto out;
750
Bernd Schubert999448a2012-03-18 22:44:49 -0400751 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700752 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 goto out_nfserr;
754
Bernd Schubert999448a2012-03-18 22:44:49 -0400755 if (may_flags & NFSD_MAY_WRITE) {
756 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700757 flags = O_RDWR|O_LARGEFILE;
758 else
759 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Bernd Schubert999448a2012-03-18 22:44:49 -0400761
Kinglong Mee8519f992014-09-03 08:14:06 +0800762 file = dentry_open(&path, flags, current_cred());
763 if (IS_ERR(file)) {
764 host_err = PTR_ERR(file);
765 goto out_nfserr;
Bernd Schubert06effdb2012-03-18 22:44:50 -0400766 }
767
Al Viro6035a272018-06-08 13:40:10 -0400768 host_err = ima_file_check(file, may_flags);
Kinglong Mee8519f992014-09-03 08:14:06 +0800769 if (host_err) {
Christoph Hellwigfd891452015-04-28 15:41:16 +0200770 fput(file);
Kinglong Mee8519f992014-09-03 08:14:06 +0800771 goto out_nfserr;
772 }
773
774 if (may_flags & NFSD_MAY_64BIT_COOKIE)
775 file->f_mode |= FMODE_64BITHASH;
776 else
777 file->f_mode |= FMODE_32BITHASH;
778
779 *filp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700781 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782out:
Jeff Layton65294c12019-08-18 14:18:48 -0400783 return err;
784}
785
786__be32
787nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
788 int may_flags, struct file **filp)
789{
790 __be32 err;
791
792 validate_process_creds();
793 /*
794 * If we get here, then the client has already done an "open",
795 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
796 * in case a chmod has now revoked permission.
797 *
798 * Arguably we should also allow the owner override for
799 * directories, but we never have and it doesn't seem to have
800 * caused anyone a problem. If we were to change this, note
801 * also that our filldir callbacks would need a variant of
802 * lookup_one_len that doesn't check permissions.
803 */
804 if (type == S_IFREG)
805 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
806 err = fh_verify(rqstp, fhp, type, may_flags);
807 if (!err)
808 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
David Howellse0e81732009-09-02 09:13:40 +0100809 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return err;
811}
812
Jeff Layton65294c12019-08-18 14:18:48 -0400813__be32
814nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
815 int may_flags, struct file **filp)
816{
817 __be32 err;
818
819 validate_process_creds();
820 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
821 validate_process_creds();
822 return err;
823}
824
825
826
Christoph Hellwige749a462015-06-18 16:44:58 +0200827struct raparms *
828nfsd_init_raparms(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Christoph Hellwige749a462015-06-18 16:44:58 +0200830 struct inode *inode = file_inode(file);
831 dev_t dev = inode->i_sb->s_dev;
832 ino_t ino = inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct raparms *ra, **rap, **frap = NULL;
834 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700835 unsigned int hash;
836 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Greg Banksfce14562006-10-04 02:15:49 -0700838 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
839 rab = &raparm_hash[hash];
840
841 spin_lock(&rab->pb_lock);
842 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (ra->p_ino == ino && ra->p_dev == dev)
844 goto found;
845 depth++;
846 if (ra->p_count == 0)
847 frap = rap;
848 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300849 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700851 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return NULL;
853 }
854 rap = frap;
855 ra = *frap;
856 ra->p_dev = dev;
857 ra->p_ino = ino;
858 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700859 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860found:
Greg Banksfce14562006-10-04 02:15:49 -0700861 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700863 ra->p_next = rab->pb_head;
864 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 ra->p_count++;
867 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700868 spin_unlock(&rab->pb_lock);
Christoph Hellwige749a462015-06-18 16:44:58 +0200869
870 if (ra->p_set)
871 file->f_ra = ra->p_ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return ra;
873}
874
Christoph Hellwige749a462015-06-18 16:44:58 +0200875void nfsd_put_raparams(struct file *file, struct raparms *ra)
876{
877 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
878
879 spin_lock(&rab->pb_lock);
880 ra->p_ra = file->f_ra;
881 ra->p_set = 1;
882 ra->p_count--;
883 spin_unlock(&rab->pb_lock);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200887 * Grab and keep cached pages associated with a file in the svc_rqst
888 * so that they can be passed to the network sendmsg/sendpage routines
889 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
891static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200892nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
893 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Jens Axboecf8208d2007-06-12 21:22:14 +0200895 struct svc_rqst *rqstp = sd->u.data;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500896 struct page **pp = rqstp->rq_next_page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200897 struct page *page = buf->page;
898 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200899
900 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (rqstp->rq_res.page_len == 0) {
903 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500904 put_page(*rqstp->rq_next_page);
905 *(rqstp->rq_next_page++) = page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200906 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700908 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500910 if (*rqstp->rq_next_page)
911 put_page(*rqstp->rq_next_page);
912 *(rqstp->rq_next_page++) = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700914 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return size;
918}
919
Jens Axboecf8208d2007-06-12 21:22:14 +0200920static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
921 struct splice_desc *sd)
922{
923 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
924}
925
Chuck Lever87c59422018-03-28 13:29:11 -0400926static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
927 struct file *file, loff_t offset,
928 unsigned long *count, int host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Al Viro6264d692006-10-19 23:28:58 -0700930 if (host_err >= 0) {
931 nfsdstats.io_read += host_err;
932 *count = host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500933 fsnotify_access(file);
Chuck Lever87c59422018-03-28 13:29:11 -0400934 trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400935 return 0;
Chuck Lever87c59422018-03-28 13:29:11 -0400936 } else {
937 trace_nfsd_read_err(rqstp, fhp, offset, host_err);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400938 return nfserrno(host_err);
Chuck Lever87c59422018-03-28 13:29:11 -0400939 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400940}
941
Chuck Lever87c59422018-03-28 13:29:11 -0400942__be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
943 struct file *file, loff_t offset, unsigned long *count)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400944{
945 struct splice_desc sd = {
946 .len = 0,
947 .total_len = *count,
948 .pos = offset,
949 .u.data = rqstp,
950 };
951 int host_err;
952
Chuck Lever87c59422018-03-28 13:29:11 -0400953 trace_nfsd_read_splice(rqstp, fhp, offset, *count);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400954 rqstp->rq_next_page = rqstp->rq_respages + 1;
955 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
Chuck Lever87c59422018-03-28 13:29:11 -0400956 return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400957}
958
Chuck Lever87c59422018-03-28 13:29:11 -0400959__be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
960 struct file *file, loff_t offset,
961 struct kvec *vec, int vlen, unsigned long *count)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400962{
Christoph Hellwig73da8522017-05-27 11:16:53 +0300963 struct iov_iter iter;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400964 int host_err;
965
Chuck Lever87c59422018-03-28 13:29:11 -0400966 trace_nfsd_read_vector(rqstp, fhp, offset, *count);
David Howellsaa563d72018-10-20 00:57:56 +0100967 iov_iter_kvec(&iter, READ, vec, vlen, *count);
Christoph Hellwig73da8522017-05-27 11:16:53 +0300968 host_err = vfs_iter_read(file, &iter, &offset, 0);
Chuck Lever87c59422018-03-28 13:29:11 -0400969 return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700972/*
973 * Gathered writes: If another process is currently writing to the file,
974 * there's a high chance this is another nfsd (triggered by a bulk write
975 * from a client's biod). Rather than syncing the file with each write
976 * request, we sleep for 10 msec.
977 *
978 * I don't know if this roughly approximates C. Juszak's idea of
979 * gathered writes, but it's a nice and simple solution (IMHO), and it
980 * seems to work:-)
981 *
982 * Note: we do this only in the NFSv2 case, since v3 and higher have a
983 * better tool (separate unstable writes and commits) for solving this
984 * problem.
985 */
986static int wait_for_concurrent_writes(struct file *file)
987{
Al Viro496ad9a2013-01-23 17:07:38 -0500988 struct inode *inode = file_inode(file);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700989 static ino_t last_ino;
990 static dev_t last_dev;
991 int err = 0;
992
993 if (atomic_read(&inode->i_writecount) > 1
994 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
995 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
996 msleep(10);
997 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
998 }
999
1000 if (inode->i_state & I_DIRTY) {
1001 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001002 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001003 }
1004 last_ino = inode->i_ino;
1005 last_dev = inode->i_sb->s_dev;
1006 return err;
1007}
1008
Christoph Hellwigaf90f702015-06-18 16:45:00 +02001009__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1011 loff_t offset, struct kvec *vec, int vlen,
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001012 unsigned long *cnt, int stable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct svc_export *exp;
Christoph Hellwig73da8522017-05-27 11:16:53 +03001015 struct iov_iter iter;
Chuck Leverd890be12018-03-27 10:53:27 -04001016 __be32 nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001017 int host_err;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001018 int use_wgather;
Kent Overstreete49dbbf2013-03-22 11:18:24 -07001019 loff_t pos = offset;
NeilBrown86584522014-05-12 11:22:47 +10001020 unsigned int pflags = current->flags;
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001021 rwf_t flags = 0;
NeilBrown86584522014-05-12 11:22:47 +10001022
Chuck Leverd890be12018-03-27 10:53:27 -04001023 trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
1024
Jeff Layton7501cc22014-11-19 07:51:15 -05001025 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
NeilBrown86584522014-05-12 11:22:47 +10001026 /*
1027 * We want less throttling in balance_dirty_pages()
1028 * and shrink_inactive_list() so that nfs to
1029 * localhost doesn't cause nfsd to lock up due to all
1030 * the client's dirty pages or its congested queue.
1031 */
1032 current->flags |= PF_LESS_THROTTLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Kinglong Mee865d50b2016-12-31 21:00:21 +08001034 exp = fhp->fh_export;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001035 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!EX_ISSYNC(exp))
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001038 stable = NFS_UNSTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Christoph Hellwig24368aa2016-04-07 08:52:04 -07001040 if (stable && !use_wgather)
1041 flags |= RWF_SYNC;
1042
David Howellsaa563d72018-10-20 00:57:56 +01001043 iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
Christoph Hellwig73da8522017-05-27 11:16:53 +03001044 host_err = vfs_iter_write(file, &iter, &pos, flags);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001045 if (host_err < 0)
1046 goto out_nfserr;
Chuck Leverd890be12018-03-27 10:53:27 -04001047 nfsdstats.io_write += *cnt;
Eric Paris2a12a9d2009-12-17 21:24:21 -05001048 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Christoph Hellwig24368aa2016-04-07 08:52:04 -07001050 if (stable && use_wgather)
1051 host_err = wait_for_concurrent_writes(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001053out_nfserr:
Chuck Leverd890be12018-03-27 10:53:27 -04001054 if (host_err >= 0) {
1055 trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1056 nfserr = nfs_ok;
1057 } else {
1058 trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1059 nfserr = nfserrno(host_err);
1060 }
Jeff Layton7501cc22014-11-19 07:51:15 -05001061 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
NeilBrown717a94b2017-04-07 10:03:26 +10001062 current_restore_flags(pflags, PF_LESS_THROTTLE);
Chuck Leverd890be12018-03-27 10:53:27 -04001063 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001066/*
1067 * Read data from a file. count must contain the requested read count
1068 * on entry. On return, *count contains the number of bytes actually read.
1069 * N.B. After this call fhp needs an fh_put
1070 */
1071__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1072 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1073{
Jeff Layton48cd7b52019-08-18 14:18:50 -04001074 struct nfsd_file *nf;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001075 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001076 __be32 err;
1077
Chuck Leverf394b622018-03-27 10:53:11 -04001078 trace_nfsd_read_start(rqstp, fhp, offset, *count);
Jeff Layton48cd7b52019-08-18 14:18:50 -04001079 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001080 if (err)
1081 return err;
1082
Jeff Layton48cd7b52019-08-18 14:18:50 -04001083 file = nf->nf_file;
Christoph Hellwiga4058c52017-05-27 11:16:54 +03001084 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
Chuck Lever87c59422018-03-28 13:29:11 -04001085 err = nfsd_splice_read(rqstp, fhp, file, offset, count);
Christoph Hellwiga4058c52017-05-27 11:16:54 +03001086 else
Chuck Lever87c59422018-03-28 13:29:11 -04001087 err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count);
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001088
Jeff Layton48cd7b52019-08-18 14:18:50 -04001089 nfsd_file_put(nf);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001090
Chuck Leverf394b622018-03-27 10:53:11 -04001091 trace_nfsd_read_done(rqstp, fhp, offset, *count);
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001092
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001093 return err;
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/*
1097 * Write data to a file.
1098 * The stable flag requests synchronous writes.
1099 * N.B. After this call fhp needs an fh_put
1100 */
Al Viro6264d692006-10-19 23:28:58 -07001101__be32
Kinglong Mee52e380e2016-12-31 21:00:13 +08001102nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1103 struct kvec *vec, int vlen, unsigned long *cnt, int stable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Jeff Laytonb4935232019-08-18 14:18:49 -04001105 struct nfsd_file *nf;
1106 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Chuck Leverf394b622018-03-27 10:53:11 -04001108 trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001109
Jeff Laytonb4935232019-08-18 14:18:49 -04001110 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
Kinglong Mee52e380e2016-12-31 21:00:13 +08001111 if (err)
1112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Jeff Laytonb4935232019-08-18 14:18:49 -04001114 err = nfsd_vfs_write(rqstp, fhp, nf->nf_file, offset, vec,
1115 vlen, cnt, stable);
1116 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117out:
Chuck Leverf394b622018-03-27 10:53:11 -04001118 trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return err;
1120}
1121
1122#ifdef CONFIG_NFSD_V3
1123/*
1124 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001125 *
1126 * Note: we only guarantee that data that lies within the range specified
1127 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *
1129 * Unfortunately we cannot lock the file to make sure we return full WCC
1130 * data to the client, as locking happens lower down in the filesystem.
1131 */
Al Viro6264d692006-10-19 23:28:58 -07001132__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1134 loff_t offset, unsigned long count)
1135{
1136 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001137 loff_t end = LLONG_MAX;
1138 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Trond Myklebustaa696a62010-01-29 16:44:25 -05001140 if (offset < 0)
1141 goto out;
1142 if (count != 0) {
1143 end = offset + (loff_t)count - 1;
1144 if (end < offset)
1145 goto out;
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jeff Layton91885252010-03-19 08:06:28 -04001148 err = nfsd_open(rqstp, fhp, S_IFREG,
1149 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001150 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001153 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001154
1155 if (err2 != -EINVAL)
1156 err = nfserrno(err2);
1157 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
Christoph Hellwigfd891452015-04-28 15:41:16 +02001161 fput(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001162out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return err;
1164}
1165#endif /* CONFIG_NFSD_V3 */
1166
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001167static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001168nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1169 struct iattr *iap)
1170{
1171 /*
1172 * Mode has already been set earlier in create:
1173 */
1174 iap->ia_valid &= ~ATTR_MODE;
1175 /*
1176 * Setting uid/gid works only for root. Irix appears to
1177 * send along the gid on create when it tries to implement
1178 * setgid directories via NFS:
1179 */
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001180 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001181 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1182 if (iap->ia_valid)
1183 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001184 /* Callers expect file metadata to be committed here */
Jeff Layton722b6202014-07-03 07:54:19 -04001185 return nfserrno(commit_metadata(resfhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001186}
1187
wengang wang4ac35c22009-02-10 11:27:51 +08001188/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1189 * setting size to 0 may fail for some specific file systems by the permission
1190 * checking which requires WRITE permission but the mode is 000.
1191 * we ignore the resizing(to 0) on the just new created file, since the size is
1192 * 0 after file created.
1193 *
1194 * call this only after vfs_create() is called.
1195 * */
1196static void
1197nfsd_check_ignore_resizing(struct iattr *iap)
1198{
1199 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1200 iap->ia_valid &= ~ATTR_SIZE;
1201}
1202
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001203/* The parent directory should already be locked: */
Al Viro6264d692006-10-19 23:28:58 -07001204__be32
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001205nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 char *fname, int flen, struct iattr *iap,
1207 int type, dev_t rdev, struct svc_fh *resfhp)
1208{
Dan Carpenter2b118852016-08-03 22:05:00 +03001209 struct dentry *dentry, *dchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001211 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001212 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001213 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001216 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001218 dchild = dget(resfhp->fh_dentry);
1219 if (!fhp->fh_locked) {
1220 WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
Al Viroa6a9f182013-09-16 10:57:01 -04001221 dentry);
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001222 err = nfserr_io;
1223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Oleg Drokin7eed34f2016-07-14 23:20:22 -04001226 err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1227 if (err)
1228 goto out;
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!(iap->ia_valid & ATTR_MODE))
1231 iap->ia_mode = 0;
1232 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1233
J. Bruce Fields088406b2006-11-08 17:44:59 -08001234 err = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001235 host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 switch (type) {
1237 case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04001238 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001239 if (!host_err)
1240 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 break;
1242 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001243 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Al Viro3819bb02018-05-11 17:03:19 -04001244 if (!host_err && unlikely(d_unhashed(dchild))) {
1245 struct dentry *d;
1246 d = lookup_one_len(dchild->d_name.name,
1247 dchild->d_parent,
1248 dchild->d_name.len);
1249 if (IS_ERR(d)) {
1250 host_err = PTR_ERR(d);
1251 break;
1252 }
1253 if (unlikely(d_is_negative(d))) {
1254 dput(d);
1255 err = nfserr_serverfault;
1256 goto out;
1257 }
1258 dput(resfhp->fh_dentry);
1259 resfhp->fh_dentry = dget(d);
1260 err = fh_update(resfhp);
1261 dput(dchild);
1262 dchild = d;
1263 if (err)
1264 goto out;
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 break;
1267 case S_IFCHR:
1268 case S_IFBLK:
1269 case S_IFIFO:
1270 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001271 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
J. Bruce Fields714232742016-07-22 12:03:46 -04001273 default:
1274 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1275 type);
1276 host_err = -EINVAL;
Dave Hansen463c3192008-02-15 14:37:57 -08001277 }
Jan Kara4a55c102012-06-12 16:20:33 +02001278 if (host_err < 0)
Dave Hansen463c3192008-02-15 14:37:57 -08001279 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Ben Myersf5019122010-02-17 14:05:11 -06001281 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Ben Myersf5019122010-02-17 14:05:11 -06001283 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001284 * nfsd_create_setattr already committed the child. Transactional
1285 * filesystems had a chance to commit changes for both parent and
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001286 * child simultaneously making the following commit_metadata a
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001287 * noop.
Ben Myersf5019122010-02-17 14:05:11 -06001288 */
1289 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001290 if (err2)
1291 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /*
1293 * Update the file handle to get the new inode info.
1294 */
1295 if (!err)
1296 err = fh_update(resfhp);
1297out:
Dan Carpenter2b118852016-08-03 22:05:00 +03001298 dput(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return err;
1300
1301out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001302 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto out;
1304}
1305
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001306/*
1307 * Create a filesystem object (regular, directory, special).
1308 * Note that the parent directory is left locked.
1309 *
1310 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1311 */
1312__be32
1313nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1314 char *fname, int flen, struct iattr *iap,
1315 int type, dev_t rdev, struct svc_fh *resfhp)
1316{
1317 struct dentry *dentry, *dchild = NULL;
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001318 __be32 err;
1319 int host_err;
1320
1321 if (isdotent(fname, flen))
1322 return nfserr_exist;
1323
J. Bruce Fieldsfa081392016-07-21 16:00:12 -04001324 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001325 if (err)
1326 return err;
1327
1328 dentry = fhp->fh_dentry;
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001329
1330 host_err = fh_want_write(fhp);
1331 if (host_err)
1332 return nfserrno(host_err);
1333
1334 fh_lock_nested(fhp, I_MUTEX_PARENT);
1335 dchild = lookup_one_len(fname, dentry, flen);
1336 host_err = PTR_ERR(dchild);
1337 if (IS_ERR(dchild))
1338 return nfserrno(host_err);
1339 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
Josef Bacik502aa0a2016-08-10 14:46:27 -04001340 /*
1341 * We unconditionally drop our ref to dchild as fh_compose will have
1342 * already grabbed its own ref for it.
1343 */
1344 dput(dchild);
1345 if (err)
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001346 return err;
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001347 return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1348 rdev, resfhp);
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001354 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
Al Viro6264d692006-10-19 23:28:58 -07001356__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001357do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 char *fname, int flen, struct iattr *iap,
1359 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001360 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct dentry *dentry, *dchild = NULL;
1363 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001364 __be32 err;
1365 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 err = nfserr_perm;
1369 if (!flen)
1370 goto out;
1371 err = nfserr_exist;
1372 if (isdotent(fname, flen))
1373 goto out;
1374 if (!(iap->ia_valid & ATTR_MODE))
1375 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001376 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (err)
1378 goto out;
1379
1380 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001381 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Jan Kara4a55c102012-06-12 16:20:33 +02001383 host_err = fh_want_write(fhp);
1384 if (host_err)
1385 goto out_nfserr;
1386
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001387 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /*
1390 * Compose the response file handle.
1391 */
1392 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001393 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (IS_ERR(dchild))
1395 goto out_nfserr;
1396
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001397 /* If file doesn't exist, check for permissions to create one */
David Howells2b0143b2015-03-17 22:25:59 +00001398 if (d_really_is_negative(dchild)) {
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001399 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1400 if (err)
1401 goto out;
1402 }
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1405 if (err)
1406 goto out;
1407
Mi Jinlongac6721a2011-04-20 17:06:25 +08001408 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001409 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001410 * the high bit set, so just clear the high bits. If this is
1411 * ever changed to use different attrs for storing the
1412 * verifier, then do_open_lookup() will also need to be fixed
1413 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
1415 v_mtime = verifier[0]&0x7fffffff;
1416 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418
David Howells2b0143b2015-03-17 22:25:59 +00001419 if (d_really_is_positive(dchild)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 err = 0;
1421
1422 switch (createmode) {
1423 case NFS3_CREATE_UNCHECKED:
David Howellse36cb0b2015-01-29 12:02:35 +00001424 if (! d_is_reg(dchild))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001425 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 else if (truncp) {
1427 /* in nfsv4, we need to treat this case a little
1428 * differently. we don't want to truncate the
1429 * file now; this would be wrong if the OPEN
1430 * fails for some other reason. furthermore,
1431 * if the size is nonzero, we should ignore it
1432 * according to spec!
1433 */
1434 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1435 }
1436 else {
1437 iap->ia_valid &= ATTR_SIZE;
1438 goto set_attr;
1439 }
1440 break;
1441 case NFS3_CREATE_EXCLUSIVE:
David Howells2b0143b2015-03-17 22:25:59 +00001442 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1443 && d_inode(dchild)->i_atime.tv_sec == v_atime
1444 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001445 if (created)
1446 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
Neil Brown7007c902012-12-07 15:40:55 -05001448 }
Gustavo A. R. Silva0ac203c2018-10-02 12:08:48 +02001449 /* fall through */
Mi Jinlongac6721a2011-04-20 17:06:25 +08001450 case NFS4_CREATE_EXCLUSIVE4_1:
David Howells2b0143b2015-03-17 22:25:59 +00001451 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1452 && d_inode(dchild)->i_atime.tv_sec == v_atime
1453 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001454 if (created)
1455 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001456 goto set_attr;
Neil Brown7007c902012-12-07 15:40:55 -05001457 }
Gustavo A. R. Silva0ac203c2018-10-02 12:08:48 +02001458 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 case NFS3_CREATE_GUARDED:
1460 err = nfserr_exist;
1461 }
Al Virobad0dcf2011-11-23 12:03:18 -05001462 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 goto out;
1464 }
1465
Al Viro312b63f2012-06-10 18:09:36 -04001466 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001467 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001468 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001470 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001471 if (created)
1472 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
wengang wang4ac35c22009-02-10 11:27:51 +08001474 nfsd_check_ignore_resizing(iap);
1475
Mi Jinlongac6721a2011-04-20 17:06:25 +08001476 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001477 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001479 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* XXX someone who knows this better please fix it for nsec */
1481 iap->ia_mtime.tv_sec = v_mtime;
1482 iap->ia_atime.tv_sec = v_atime;
1483 iap->ia_mtime.tv_nsec = 0;
1484 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001488 err = nfsd_create_setattr(rqstp, resfhp, iap);
1489
1490 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001491 * nfsd_create_setattr already committed the child
1492 * (and possibly also the parent).
Ben Myersf5019122010-02-17 14:05:11 -06001493 */
1494 if (!err)
1495 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001496
1497 /*
1498 * Update the filehandle to get the new inode info.
1499 */
1500 if (!err)
1501 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 out:
1504 fh_unlock(fhp);
1505 if (dchild && !IS_ERR(dchild))
1506 dput(dchild);
Jan Kara4a55c102012-06-12 16:20:33 +02001507 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return err;
1509
1510 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001511 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 goto out;
1513}
1514#endif /* CONFIG_NFSD_V3 */
1515
1516/*
1517 * Read a symlink. On entry, *lenp must contain the maximum path length that
1518 * fits into the buffer. On return, it contains the true length.
1519 * N.B. After this call fhp needs an fh_put
1520 */
Al Viro6264d692006-10-19 23:28:58 -07001521__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1523{
Al Viro6264d692006-10-19 23:28:58 -07001524 __be32 err;
Al Viro4d7edbc2017-05-27 16:11:23 -04001525 const char *link;
Al Viro68ac1232012-03-15 08:21:57 -04001526 struct path path;
Al Viro4d7edbc2017-05-27 16:11:23 -04001527 DEFINE_DELAYED_CALL(done);
1528 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001530 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Al Viro4d7edbc2017-05-27 16:11:23 -04001531 if (unlikely(err))
1532 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Al Viro68ac1232012-03-15 08:21:57 -04001534 path.mnt = fhp->fh_export->ex_path.mnt;
1535 path.dentry = fhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Al Viro4d7edbc2017-05-27 16:11:23 -04001537 if (unlikely(!d_is_symlink(path.dentry)))
1538 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Al Viro68ac1232012-03-15 08:21:57 -04001540 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Al Viro4d7edbc2017-05-27 16:11:23 -04001542 link = vfs_get_link(path.dentry, &done);
1543 if (IS_ERR(link))
1544 return nfserrno(PTR_ERR(link));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Al Viro4d7edbc2017-05-27 16:11:23 -04001546 len = strlen(link);
1547 if (len < *lenp)
1548 *lenp = len;
1549 memcpy(buf, link, *lenp);
1550 do_delayed_call(&done);
1551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
1554/*
1555 * Create a symlink and look up its inode
1556 * N.B. After this call _both_ fhp and resfhp need an fh_put
1557 */
Al Viro6264d692006-10-19 23:28:58 -07001558__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1560 char *fname, int flen,
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001561 char *path,
Kinglong Mee1e444f52014-07-01 17:48:02 +08001562 struct svc_fh *resfhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001565 __be32 err, cerr;
1566 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 err = nfserr_noent;
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001569 if (!flen || path[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto out;
1571 err = nfserr_exist;
1572 if (isdotent(fname, flen))
1573 goto out;
1574
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001575 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (err)
1577 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001578
1579 host_err = fh_want_write(fhp);
1580 if (host_err)
1581 goto out_nfserr;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 fh_lock(fhp);
1584 dentry = fhp->fh_dentry;
1585 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001586 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (IS_ERR(dnew))
1588 goto out_nfserr;
1589
David Howells2b0143b2015-03-17 22:25:59 +00001590 host_err = vfs_symlink(d_inode(dentry), dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001591 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001592 if (!err)
1593 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 fh_unlock(fhp);
1595
Al Virobad0dcf2011-11-23 12:03:18 -05001596 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1599 dput(dnew);
1600 if (err==0) err = cerr;
1601out:
1602 return err;
1603
1604out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001605 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto out;
1607}
1608
1609/*
1610 * Create a hardlink
1611 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1612 */
Al Viro6264d692006-10-19 23:28:58 -07001613__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1615 char *name, int len, struct svc_fh *tfhp)
1616{
1617 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001618 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001619 __be32 err;
1620 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001622 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (err)
1624 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001625 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (err)
1627 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001628 err = nfserr_isdir;
David Howellse36cb0b2015-01-29 12:02:35 +00001629 if (d_is_dir(tfhp->fh_dentry))
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001630 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 err = nfserr_perm;
1632 if (!len)
1633 goto out;
1634 err = nfserr_exist;
1635 if (isdotent(name, len))
1636 goto out;
1637
Jan Kara4a55c102012-06-12 16:20:33 +02001638 host_err = fh_want_write(tfhp);
1639 if (host_err) {
1640 err = nfserrno(host_err);
1641 goto out;
1642 }
1643
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001644 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 ddir = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001646 dirp = d_inode(ddir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001649 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (IS_ERR(dnew))
1651 goto out_nfserr;
1652
1653 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001655 err = nfserr_noent;
David Howells2b0143b2015-03-17 22:25:59 +00001656 if (d_really_is_negative(dold))
Jan Kara4a55c102012-06-12 16:20:33 +02001657 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04001658 host_err = vfs_link(dold, dirp, dnew, NULL);
Al Viro6264d692006-10-19 23:28:58 -07001659 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001660 err = nfserrno(commit_metadata(ffhp));
1661 if (!err)
1662 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 } else {
Al Viro6264d692006-10-19 23:28:58 -07001664 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 err = nfserr_acces;
1666 else
Al Viro6264d692006-10-19 23:28:58 -07001667 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001669out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001671out_unlock:
1672 fh_unlock(ffhp);
Jan Kara4a55c102012-06-12 16:20:33 +02001673 fh_drop_write(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674out:
1675 return err;
1676
1677out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001678 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001679 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682/*
1683 * Rename a file
1684 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1685 */
Al Viro6264d692006-10-19 23:28:58 -07001686__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1688 struct svc_fh *tfhp, char *tname, int tlen)
1689{
1690 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1691 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001692 __be32 err;
1693 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001695 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (err)
1697 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001698 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (err)
1700 goto out;
1701
1702 fdentry = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001703 fdir = d_inode(fdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 tdentry = tfhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001706 tdir = d_inode(tdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 err = nfserr_perm;
1709 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1710 goto out;
1711
Jan Kara4a55c102012-06-12 16:20:33 +02001712 host_err = fh_want_write(ffhp);
1713 if (host_err) {
1714 err = nfserrno(host_err);
1715 goto out;
1716 }
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /* cannot use fh_lock as we need deadlock protective ordering
1719 * so do it by hand */
1720 trap = lock_rename(tdentry, fdentry);
Jeff Laytonaaf91ec2015-09-17 08:28:39 -04001721 ffhp->fh_locked = tfhp->fh_locked = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 fill_pre_wcc(ffhp);
1723 fill_pre_wcc(tfhp);
1724
1725 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001726 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (IS_ERR(odentry))
1728 goto out_nfserr;
1729
Al Viro6264d692006-10-19 23:28:58 -07001730 host_err = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001731 if (d_really_is_negative(odentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001733 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (odentry == trap)
1735 goto out_dput_old;
1736
1737 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001738 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (IS_ERR(ndentry))
1740 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001741 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (ndentry == trap)
1743 goto out_dput_new;
1744
Dave Hansen9079b1e2008-02-15 14:37:49 -08001745 host_err = -EXDEV;
1746 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1747 goto out_dput_new;
J. Bruce Fieldsaa387d6c2013-04-15 16:03:46 -04001748 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1749 goto out_dput_new;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001750
Miklos Szeredi520c8b12014-04-01 17:08:42 +02001751 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
Ben Myersf5019122010-02-17 14:05:11 -06001752 if (!host_err) {
1753 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001754 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001755 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 out_dput_new:
1758 dput(ndentry);
1759 out_dput_old:
1760 dput(odentry);
1761 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001762 err = nfserrno(host_err);
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001763 /*
1764 * We cannot rely on fh_unlock on the two filehandles,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 * as that would do the wrong thing if the two directories
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001766 * were the same, so again we do it by hand.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
1768 fill_post_wcc(ffhp);
1769 fill_post_wcc(tfhp);
1770 unlock_rename(tdentry, fdentry);
Jeff Laytonaaf91ec2015-09-17 08:28:39 -04001771 ffhp->fh_locked = tfhp->fh_locked = false;
Jan Kara4a55c102012-06-12 16:20:33 +02001772 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774out:
1775 return err;
1776}
1777
1778/*
1779 * Unlink a file or directory
1780 * N.B. After this call fhp needs an fh_put
1781 */
Al Viro6264d692006-10-19 23:28:58 -07001782__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1784 char *fname, int flen)
1785{
1786 struct dentry *dentry, *rdentry;
1787 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001788 __be32 err;
1789 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 err = nfserr_acces;
1792 if (!flen || isdotent(fname, flen))
1793 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001794 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (err)
1796 goto out;
1797
Jan Kara4a55c102012-06-12 16:20:33 +02001798 host_err = fh_want_write(fhp);
1799 if (host_err)
1800 goto out_nfserr;
1801
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001802 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001804 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001807 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (IS_ERR(rdentry))
J. Bruce Fields0ca0c9d2019-04-12 16:26:30 -04001809 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
David Howells2b0143b2015-03-17 22:25:59 +00001811 if (d_really_is_negative(rdentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 dput(rdentry);
J. Bruce Fields0ca0c9d2019-04-12 16:26:30 -04001813 host_err = -ENOENT;
1814 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816
1817 if (!type)
David Howells2b0143b2015-03-17 22:25:59 +00001818 type = d_inode(rdentry)->i_mode & S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001820 if (type != S_IFDIR)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04001821 host_err = vfs_unlink(dirp, rdentry, NULL);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001822 else
Al Viro6264d692006-10-19 23:28:58 -07001823 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001824 if (!host_err)
1825 host_err = commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 dput(rdentry);
1827
J. Bruce Fields0ca0c9d2019-04-12 16:26:30 -04001828out_drop_write:
1829 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001831 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001832out:
1833 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
1836/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001837 * We do this buffering because we must not call back into the file
1838 * system's ->lookup() method from the filldir callback. That may well
1839 * deadlock a number of file systems.
1840 *
1841 * This is based heavily on the implementation of same in XFS.
1842 */
1843struct buffered_dirent {
1844 u64 ino;
1845 loff_t offset;
1846 int namlen;
1847 unsigned int d_type;
1848 char name[];
1849};
1850
1851struct readdir_data {
Al Viro5c0ba4e2013-05-15 13:52:59 -04001852 struct dir_context ctx;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001853 char *dirent;
1854 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001855 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001856};
1857
Miklos Szerediac7576f2014-10-30 17:37:34 +01001858static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1859 int namlen, loff_t offset, u64 ino,
1860 unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001861{
Miklos Szerediac7576f2014-10-30 17:37:34 +01001862 struct readdir_data *buf =
1863 container_of(ctx, struct readdir_data, ctx);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001864 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1865 unsigned int reclen;
1866
1867 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001868 if (buf->used + reclen > PAGE_SIZE) {
1869 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001870 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001871 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001872
1873 de->namlen = namlen;
1874 de->offset = offset;
1875 de->ino = ino;
1876 de->d_type = d_type;
1877 memcpy(de->name, name, namlen);
1878 buf->used += reclen;
1879
1880 return 0;
1881}
1882
Miklos Szerediac7576f2014-10-30 17:37:34 +01001883static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
David Woodhouse2f9092e2009-04-20 23:18:37 +01001884 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001885{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001886 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001887 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001888 int size;
1889 loff_t offset;
Al Viroac6614b2013-05-22 22:22:04 -04001890 struct readdir_data buf = {
1891 .ctx.actor = nfsd_buffered_filldir,
1892 .dirent = (void *)__get_free_page(GFP_KERNEL)
1893 };
David Woodhouse2628b762008-07-31 17:16:51 +01001894
David Woodhouse14f7dd62008-07-31 20:29:12 +01001895 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001896 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001897
David Woodhouse14f7dd62008-07-31 20:29:12 +01001898 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001899
1900 while (1) {
1901 unsigned int reclen;
1902
Doug Nazarb726e922008-11-05 06:16:28 -05001903 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001904 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001905 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001906
Al Viro5c0ba4e2013-05-15 13:52:59 -04001907 host_err = iterate_dir(file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -04001908 if (buf.full)
1909 host_err = 0;
1910
1911 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001912 break;
1913
1914 size = buf.used;
1915
1916 if (!size)
1917 break;
1918
David Woodhouse14f7dd62008-07-31 20:29:12 +01001919 de = (struct buffered_dirent *)buf.dirent;
1920 while (size > 0) {
1921 offset = de->offset;
1922
1923 if (func(cdp, de->name, de->namlen, de->offset,
1924 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01001925 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001926
1927 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001928 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001929
1930 reclen = ALIGN(sizeof(*de) + de->namlen,
1931 sizeof(u64));
1932 size -= reclen;
1933 de = (struct buffered_dirent *)((char *)de + reclen);
1934 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01001935 if (size > 0) /* We bailed out early */
1936 break;
1937
David Woodhousec002a6c2008-08-17 17:21:18 +01001938 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001939 }
1940
David Woodhouse14f7dd62008-07-31 20:29:12 +01001941 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01001942
1943 if (host_err)
1944 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001945
1946 *offsetp = offset;
1947 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01001948}
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950/*
1951 * Read entries from a directory.
1952 * The NFSv3/4 verifier we ignore for now.
1953 */
Al Viro6264d692006-10-19 23:28:58 -07001954__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
Miklos Szerediac7576f2014-10-30 17:37:34 +01001956 struct readdir_cd *cdp, nfsd_filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Al Viro6264d692006-10-19 23:28:58 -07001958 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 struct file *file;
1960 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04001961 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Bernd Schubert06effdb2012-03-18 22:44:50 -04001963 /* NFSv2 only supports 32 bit cookies */
1964 if (rqstp->rq_vers > 2)
1965 may_flags |= NFSD_MAY_64BIT_COOKIE;
1966
1967 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (err)
1969 goto out;
1970
Jeff Laytonb108fe62012-04-25 15:30:00 -04001971 offset = vfs_llseek(file, offset, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (offset < 0) {
1973 err = nfserrno((int)offset);
1974 goto out_close;
1975 }
1976
David Woodhouse14f7dd62008-07-31 20:29:12 +01001977 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (err == nfserr_eof || err == nfserr_toosmall)
1980 err = nfs_ok; /* can still be found in ->err */
1981out_close:
Christoph Hellwigfd891452015-04-28 15:41:16 +02001982 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983out:
1984 return err;
1985}
1986
1987/*
1988 * Get file system stats
1989 * N.B. After this call fhp needs an fh_put
1990 */
Al Viro6264d692006-10-19 23:28:58 -07001991__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04001992nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02001994 __be32 err;
1995
1996 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02001997 if (!err) {
1998 struct path path = {
1999 .mnt = fhp->fh_export->ex_path.mnt,
2000 .dentry = fhp->fh_dentry,
2001 };
2002 if (vfs_statfs(&path, stat))
2003 err = nfserr_io;
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return err;
2006}
2007
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002008static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002009{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002010 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013/*
2014 * Check for a user's access permissions to this inode.
2015 */
Al Viro6264d692006-10-19 23:28:58 -07002016__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07002017nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2018 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
David Howells2b0143b2015-03-17 22:25:59 +00002020 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 int err;
2022
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04002023 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return 0;
2025#if 0
2026 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2027 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002028 (acc & NFSD_MAY_READ)? " read" : "",
2029 (acc & NFSD_MAY_WRITE)? " write" : "",
2030 (acc & NFSD_MAY_EXEC)? " exec" : "",
2031 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2032 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2033 (acc & NFSD_MAY_LOCK)? " lock" : "",
2034 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 inode->i_mode,
2036 IS_IMMUTABLE(inode)? " immut" : "",
2037 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002038 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002040 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041#endif
2042
2043 /* Normally we reject any write/sattr etc access on a read-only file
2044 * system. But if it is IRIX doing check on write-access for a
2045 * device special file, we ignore rofs.
2046 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002047 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2048 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002049 if (exp_rdonly(rqstp, exp) ||
2050 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002052 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return nfserr_perm;
2054 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002055 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return nfserr_perm;
2057
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002058 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 /* If we cannot rely on authentication in NLM requests,
2060 * just allow locks, otherwise require read permission, or
2061 * ownership
2062 */
2063 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2064 return 0;
2065 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002066 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068 /*
2069 * The file owner always gets access permission for accesses that
2070 * would normally be checked at open time. This is to make
2071 * file access work even when the client has done a fchmod(fd, 0).
2072 *
2073 * However, `cp foo bar' should fail nevertheless when bar is
2074 * readonly. A sensible way to do this might be to reject all
2075 * attempts to truncate a read-only file, because a creat() call
2076 * always implies file truncation.
2077 * ... but this isn't really fair. A process may reasonably call
2078 * ftruncate on an open file descriptor on a file with perm 000.
2079 * We must trust the client to do permission checking - using "ACCESS"
2080 * with NFSv3.
2081 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002082 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002083 uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 0;
2085
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002086 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002087 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 /* Allow read access to binaries even when mode 111 */
2090 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002091 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2092 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002093 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 return err? nfserrno(err) : 0;
2096}
2097
2098void
2099nfsd_racache_shutdown(void)
2100{
Jeff Layton54a66e52008-08-13 22:03:27 -04002101 struct raparms *raparm, *last_raparm;
2102 unsigned int i;
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002105
2106 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2107 raparm = raparm_hash[i].pb_head;
2108 while(raparm) {
2109 last_raparm = raparm;
2110 raparm = raparm->p_next;
2111 kfree(last_raparm);
2112 }
2113 raparm_hash[i].pb_head = NULL;
2114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116/*
2117 * Initialize readahead param cache
2118 */
2119int
2120nfsd_racache_init(int cache_size)
2121{
2122 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002123 int j = 0;
2124 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002125 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Greg Banksfce14562006-10-04 02:15:49 -07002127
Jeff Layton54a66e52008-08-13 22:03:27 -04002128 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002130 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002131 nperbucket = max(2, nperbucket);
Jeff Layton54a66e52008-08-13 22:03:27 -04002132 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002133
2134 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002135
2136 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002137 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002138
2139 raparm = &raparm_hash[i].pb_head;
2140 for (j = 0; j < nperbucket; j++) {
2141 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2142 if (!*raparm)
2143 goto out_nomem;
2144 raparm = &(*raparm)->p_next;
2145 }
2146 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002147 }
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 nfsdstats.ra_size = cache_size;
2150 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002151
2152out_nomem:
2153 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2154 nfsd_racache_shutdown();
2155 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}