blob: 45c04979e7b3c1fed3d14c387c50de9ce95a729a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * File operations used by nfsd. Some of these have been ripped from
3 * other parts of the kernel because they weren't exported, others
4 * are partial duplicates with added or changed functionality.
5 *
6 * Note that several functions dget() the dentry upon which they want
7 * to act, most notably those that create directory entries. Response
8 * dentry's are dput()'d if necessary in the release callback.
9 * So if you notice code paths that apparently fail to dput() the
10 * dentry, don't worry--they have been taken care of.
11 *
12 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
13 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
17#include <linux/file.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020018#include <linux/splice.h>
Anna Schumaker95d871f2014-11-07 14:44:26 -050019#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/xattr.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020026#include <linux/jhash.h>
27#include <linux/ima.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020029#include <asm/uaccess.h>
Ben Myersf5019122010-02-17 14:05:11 -060030#include <linux/exportfs.h>
31#include <linux/writeback.h>
David Quigley18032ca2013-05-02 13:19:10 -040032#include <linux/security.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020033
34#ifdef CONFIG_NFSD_V3
35#include "xdr3.h"
36#endif /* CONFIG_NFSD_V3 */
37
Christoph Hellwig5be196e2006-01-09 20:51:55 -080038#ifdef CONFIG_NFSD_V4
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050039#include "acl.h"
40#include "idmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#endif /* CONFIG_NFSD_V4 */
42
Boaz Harrosh9a74af22009-12-03 20:30:56 +020043#include "nfsd.h"
44#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define NFSDDBG_FACILITY NFSDDBG_FILEOP
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * This is a cache of readahead params that help us choose the proper
51 * readahead strategy. Initially, we set all readahead parameters to 0
52 * and let the VFS handle things.
53 * If you increase the number of cached files very much, you'll need to
54 * add a hash table here.
55 */
56struct raparms {
57 struct raparms *p_next;
58 unsigned int p_count;
59 ino_t p_ino;
60 dev_t p_dev;
61 int p_set;
62 struct file_ra_state p_ra;
Greg Banksfce14562006-10-04 02:15:49 -070063 unsigned int p_hindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Greg Banksfce14562006-10-04 02:15:49 -070066struct raparm_hbucket {
67 struct raparms *pb_head;
68 spinlock_t pb_lock;
69} ____cacheline_aligned_in_smp;
70
Greg Banksfce14562006-10-04 02:15:49 -070071#define RAPARM_HASH_BITS 4
72#define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
73#define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
74static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/*
77 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
78 * a mount point.
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080079 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * or nfs_ok having possibly changed *dpp and *expp
81 */
82int
83nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
84 struct svc_export **expp)
85{
86 struct svc_export *exp = *expp, *exp2 = NULL;
87 struct dentry *dentry = *dpp;
Al Viro91c9fa82009-04-18 02:42:05 -040088 struct path path = {.mnt = mntget(exp->ex_path.mnt),
89 .dentry = dget(dentry)};
Al Viro6264d692006-10-19 23:28:58 -070090 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Al Viro7cc90cc2011-03-18 09:04:20 -040092 err = follow_down(&path);
David Howellscc53ce52011-01-14 18:45:26 +000093 if (err < 0)
94 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Al Viro91c9fa82009-04-18 02:42:05 -040096 exp2 = rqst_exp_get_by_name(rqstp, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (IS_ERR(exp2)) {
J. Bruce Fields3b6cee7bc2009-10-25 21:18:19 -040098 err = PTR_ERR(exp2);
99 /*
100 * We normally allow NFS clients to continue
101 * "underneath" a mountpoint that is not exported.
102 * The exception is V4ROOT, where no traversal is ever
103 * allowed without an explicit export of the new
104 * directory.
105 */
106 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
107 err = 0;
Al Viro91c9fa82009-04-18 02:42:05 -0400108 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 goto out;
110 }
Steve Dickson3c394dd2009-09-09 15:02:40 -0400111 if (nfsd_v4client(rqstp) ||
112 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* successfully crossed mount point */
Al Viro1644ccc2009-04-18 02:32:31 -0400114 /*
Al Viro91c9fa82009-04-18 02:42:05 -0400115 * This is subtle: path.dentry is *not* on path.mnt
116 * at this point. The only reason we are safe is that
117 * original mnt is pinned down by exp, so we should
118 * put path *before* putting exp
Al Viro1644ccc2009-04-18 02:32:31 -0400119 */
Al Viro91c9fa82009-04-18 02:42:05 -0400120 *dpp = path.dentry;
121 path.dentry = dentry;
Al Viro1644ccc2009-04-18 02:32:31 -0400122 *expp = exp2;
Al Viro91c9fa82009-04-18 02:42:05 -0400123 exp2 = exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Al Viro91c9fa82009-04-18 02:42:05 -0400125 path_put(&path);
126 exp_put(exp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127out:
128 return err;
129}
130
J. Bruce Fields289ede42009-09-26 20:32:24 -0400131static void follow_to_parent(struct path *path)
132{
133 struct dentry *dp;
134
135 while (path->dentry == path->mnt->mnt_root && follow_up(path))
136 ;
137 dp = dget_parent(path->dentry);
138 dput(path->dentry);
139 path->dentry = dp;
140}
141
142static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
143{
144 struct svc_export *exp2;
145 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
146 .dentry = dget(dparent)};
147
148 follow_to_parent(&path);
149
150 exp2 = rqst_exp_parent(rqstp, &path);
151 if (PTR_ERR(exp2) == -ENOENT) {
152 *dentryp = dget(dparent);
153 } else if (IS_ERR(exp2)) {
154 path_put(&path);
155 return PTR_ERR(exp2);
156 } else {
157 *dentryp = dget(path.dentry);
158 exp_put(*exp);
159 *exp = exp2;
160 }
161 path_put(&path);
162 return 0;
163}
164
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400165/*
166 * For nfsd purposes, we treat V4ROOT exports as though there was an
167 * export at *every* directory.
168 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -0400169int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400170{
171 if (d_mountpoint(dentry))
172 return 1;
Trond Myklebust11fcee022011-09-12 19:37:26 -0400173 if (nfsd4_is_junction(dentry))
174 return 1;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400175 if (!(exp->ex_flags & NFSEXP_V4ROOT))
176 return 0;
David Howells2b0143b2015-03-17 22:25:59 +0000177 return d_inode(dentry) != NULL;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400178}
179
Al Viro6264d692006-10-19 23:28:58 -0700180__be32
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700181nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400182 const char *name, unsigned int len,
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700183 struct svc_export **exp_ret, struct dentry **dentry_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct svc_export *exp;
186 struct dentry *dparent;
187 struct dentry *dentry;
Al Viro6264d692006-10-19 23:28:58 -0700188 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 dparent = fhp->fh_dentry;
Kinglong Meebf18f162014-06-10 22:06:44 +0800193 exp = exp_get(fhp->fh_export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* Lookup the name, but don't follow links */
196 if (isdotent(name, len)) {
197 if (len==1)
198 dentry = dget(dparent);
Jan Blunck54775492008-02-14 19:38:39 -0800199 else if (dparent != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dentry = dget_parent(dparent);
J. Bruce Fieldsfed83812009-09-26 16:53:01 -0400201 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dentry = dget(dparent); /* .. == . just like at / */
203 else {
204 /* checking mountpoint crossing is very different when stepping up */
J. Bruce Fields289ede42009-09-26 20:32:24 -0400205 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
206 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 } else {
J. Bruce Fields43357232014-01-24 18:04:40 -0500210 /*
211 * In the nfsd4_open() case, this may be held across
212 * subsequent open and delegation acquisition which may
213 * need to take the child's i_mutex:
214 */
215 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 dentry = lookup_one_len(name, dparent, len);
Al Viro6264d692006-10-19 23:28:58 -0700217 host_err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (IS_ERR(dentry))
219 goto out_nfserr;
220 /*
221 * check if we have crossed a mount point ...
222 */
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400223 if (nfsd_mountpoint(dentry, exp)) {
Al Viro6264d692006-10-19 23:28:58 -0700224 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dput(dentry);
226 goto out_nfserr;
227 }
228 }
229 }
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700230 *dentry_ret = dentry;
231 *exp_ret = exp;
232 return 0;
233
234out_nfserr:
235 exp_put(exp);
236 return nfserrno(host_err);
237}
238
239/*
240 * Look up one component of a pathname.
241 * N.B. After this call _both_ fhp and resfh need an fh_put
242 *
243 * If the lookup would cross a mountpoint, and the mounted filesystem
244 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
245 * accepted as it stands and the mounted directory is
246 * returned. Otherwise the covered directory is returned.
247 * NOTE: this mountpoint crossing is not supported properly by all
248 * clients and is explicitly disallowed for NFSv3
249 * NeilBrown <neilb@cse.unsw.edu.au>
250 */
251__be32
252nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400253 unsigned int len, struct svc_fh *resfh)
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700254{
255 struct svc_export *exp;
256 struct dentry *dentry;
257 __be32 err;
258
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400259 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
260 if (err)
261 return err;
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700262 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
263 if (err)
264 return err;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700265 err = check_nfsd_access(exp, rqstp);
266 if (err)
267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /*
269 * Note: we compose the file handle now, but as the
270 * dentry may be negative, it may need to be updated.
271 */
272 err = fh_compose(resfh, exp, dentry, fhp);
David Howells2b0143b2015-03-17 22:25:59 +0000273 if (!err && d_really_is_negative(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 err = nfserr_noent;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700275out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 exp_put(exp);
278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Ben Myersf5019122010-02-17 14:05:11 -0600281/*
282 * Commit metadata changes to stable storage.
283 */
284static int
285commit_metadata(struct svc_fh *fhp)
286{
David Howells2b0143b2015-03-17 22:25:59 +0000287 struct inode *inode = d_inode(fhp->fh_dentry);
Ben Myersf5019122010-02-17 14:05:11 -0600288 const struct export_operations *export_ops = inode->i_sb->s_export_op;
Ben Myersf5019122010-02-17 14:05:11 -0600289
290 if (!EX_ISSYNC(fhp->fh_export))
291 return 0;
292
Christoph Hellwigc37650162010-10-06 10:48:20 +0200293 if (export_ops->commit_metadata)
294 return export_ops->commit_metadata(inode);
295 return sync_inode_metadata(inode, 1);
Ben Myersf5019122010-02-17 14:05:11 -0600296}
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800299 * Go over the attributes and take care of the small differences between
300 * NFS semantics and what Linux expects.
301 */
302static void
303nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
304{
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800305 /* sanitize the mode change */
306 if (iap->ia_valid & ATTR_MODE) {
307 iap->ia_mode &= S_IALLUGO;
308 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
309 }
310
311 /* Revoke setuid/setgid on chown */
312 if (!S_ISDIR(inode->i_mode) &&
Stanislav Kholmanskikhc4fa6d72013-12-11 14:16:36 +0400313 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800314 iap->ia_valid |= ATTR_KILL_PRIV;
315 if (iap->ia_valid & ATTR_MODE) {
316 /* we're setting mode too, just clear the s*id bits */
317 iap->ia_mode &= ~S_ISUID;
318 if (iap->ia_mode & S_IXGRP)
319 iap->ia_mode &= ~S_ISGID;
320 } else {
321 /* set ATTR_KILL_* bits and let VFS handle it */
322 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
323 }
324 }
325}
326
327static __be32
328nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
329 struct iattr *iap)
330{
David Howells2b0143b2015-03-17 22:25:59 +0000331 struct inode *inode = d_inode(fhp->fh_dentry);
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800332 int host_err;
333
334 if (iap->ia_size < inode->i_size) {
335 __be32 err;
336
337 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
338 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
339 if (err)
340 return err;
341 }
342
343 host_err = get_write_access(inode);
344 if (host_err)
345 goto out_nfserrno;
346
347 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
348 if (host_err)
349 goto out_put_write_access;
350 return 0;
351
352out_put_write_access:
353 put_write_access(inode);
354out_nfserrno:
355 return nfserrno(host_err);
356}
357
358/*
359 * Set various file attributes. After this call fhp needs an fh_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Al Viro6264d692006-10-19 23:28:58 -0700361__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
363 int check_guard, time_t guardtime)
364{
365 struct dentry *dentry;
366 struct inode *inode;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200367 int accmode = NFSD_MAY_SATTR;
Al Viro175a4eb2011-07-26 03:30:54 -0400368 umode_t ftype = 0;
Al Viro6264d692006-10-19 23:28:58 -0700369 __be32 err;
370 int host_err;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500371 bool get_write_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int size_change = 0;
373
374 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200375 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (iap->ia_valid & ATTR_SIZE)
377 ftype = S_IFREG;
378
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500379 /* Callers that do fh_verify should do the fh_want_write: */
380 get_write_count = !fhp->fh_dentry;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* Get inode */
383 err = fh_verify(rqstp, fhp, ftype, accmode);
NeilBrown15b7a1b2005-11-07 01:00:23 -0800384 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto out;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500386 if (get_write_count) {
387 host_err = fh_want_write(fhp);
388 if (host_err)
389 return nfserrno(host_err);
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000393 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
NeilBrown15b7a1b2005-11-07 01:00:23 -0800395 /* Ignore any mode updates on symlinks */
396 if (S_ISLNK(inode->i_mode))
397 iap->ia_valid &= ~ATTR_MODE;
398
399 if (!iap->ia_valid)
400 goto out;
401
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800402 nfsd_sanitize_attrs(inode, iap);
403
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000404 /*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800405 * The size case is special, it changes the file in addition to the
406 * attributes.
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (iap->ia_valid & ATTR_SIZE) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800409 err = nfsd_get_write_access(rqstp, fhp, iap);
410 if (err)
411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 size_change = 1;
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700413
414 /*
415 * RFC5661, Section 18.30.4:
416 * Changing the size of a file with SETATTR indirectly
417 * changes the time_modify and change attributes.
418 *
419 * (and similar for the older RFCs)
420 */
421 if (iap->ia_size != i_size_read(inode))
422 iap->ia_valid |= ATTR_MTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 iap->ia_valid |= ATTR_CTIME;
426
Christoph Hellwig987da472013-11-18 05:07:47 -0800427 if (check_guard && guardtime != inode->i_ctime.tv_sec) {
428 err = nfserr_notsync;
429 goto out_put_write_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Christoph Hellwig987da472013-11-18 05:07:47 -0800431
Christoph Hellwig987da472013-11-18 05:07:47 -0800432 fh_lock(fhp);
433 host_err = notify_change(dentry, iap, NULL);
434 fh_unlock(fhp);
J. R. Okajima1406b912014-02-19 00:27:53 +0900435 err = nfserrno(host_err);
Christoph Hellwig987da472013-11-18 05:07:47 -0800436
Christoph Hellwig987da472013-11-18 05:07:47 -0800437out_put_write_access:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (size_change)
439 put_write_access(inode);
440 if (!err)
Jeff Layton722b6202014-07-03 07:54:19 -0400441 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442out:
443 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800446#if defined(CONFIG_NFSD_V4)
Chuck Lever9b4146e2012-01-04 16:26:43 -0500447/*
448 * NFS junction information is stored in an extended attribute.
449 */
450#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
451
452/**
453 * nfsd4_is_junction - Test if an object could be an NFS junction
454 *
455 * @dentry: object to test
456 *
457 * Returns 1 if "dentry" appears to contain NFS junction information.
458 * Otherwise 0 is returned.
459 */
Trond Myklebust11fcee022011-09-12 19:37:26 -0400460int nfsd4_is_junction(struct dentry *dentry)
461{
David Howells2b0143b2015-03-17 22:25:59 +0000462 struct inode *inode = d_inode(dentry);
Trond Myklebust11fcee022011-09-12 19:37:26 -0400463
464 if (inode == NULL)
465 return 0;
466 if (inode->i_mode & S_IXUGO)
467 return 0;
468 if (!(inode->i_mode & S_ISVTX))
469 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500470 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee022011-09-12 19:37:26 -0400471 return 0;
472 return 1;
473}
David Quigley18032ca2013-05-02 13:19:10 -0400474#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
475__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
476 struct xdr_netobj *label)
477{
478 __be32 error;
479 int host_error;
480 struct dentry *dentry;
481
482 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
483 if (error)
484 return error;
485
486 dentry = fhp->fh_dentry;
487
David Howells2b0143b2015-03-17 22:25:59 +0000488 mutex_lock(&d_inode(dentry)->i_mutex);
David Quigley18032ca2013-05-02 13:19:10 -0400489 host_error = security_inode_setsecctx(dentry, label->data, label->len);
David Howells2b0143b2015-03-17 22:25:59 +0000490 mutex_unlock(&d_inode(dentry)->i_mutex);
David Quigley18032ca2013-05-02 13:19:10 -0400491 return nfserrno(host_error);
492}
493#else
494__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
495 struct xdr_netobj *label)
496{
497 return nfserr_notsupp;
498}
499#endif
500
Anna Schumaker95d871f2014-11-07 14:44:26 -0500501__be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
502 struct file *file, loff_t offset, loff_t len,
503 int flags)
504{
Anna Schumaker95d871f2014-11-07 14:44:26 -0500505 int error;
506
507 if (!S_ISREG(file_inode(file)->i_mode))
508 return nfserr_inval;
509
Anna Schumaker95d871f2014-11-07 14:44:26 -0500510 error = vfs_fallocate(file, flags, offset, len);
511 if (!error)
512 error = commit_metadata(fhp);
513
514 return nfserrno(error);
515}
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400516#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518#ifdef CONFIG_NFSD_V3
519/*
520 * Check server access rights to a file system object
521 */
522struct accessmap {
523 u32 access;
524 int how;
525};
526static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200527 { NFS3_ACCESS_READ, NFSD_MAY_READ },
528 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
529 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
530 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 { 0, 0 }
533};
534
535static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200536 { NFS3_ACCESS_READ, NFSD_MAY_READ },
537 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
538 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
539 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
540 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 { 0, 0 }
543};
544
545static struct accessmap nfs3_anyaccess[] = {
546 /* Some clients - Solaris 2.6 at least, make an access call
547 * to the server to check for access for things like /dev/null
548 * (which really, the server doesn't care about). So
549 * We provide simple access checking for them, looking
550 * mainly at mode bits, and we make sure to ignore read-only
551 * filesystem checks
552 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200553 { NFS3_ACCESS_READ, NFSD_MAY_READ },
554 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
555 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
556 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 { 0, 0 }
559};
560
Al Viro6264d692006-10-19 23:28:58 -0700561__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
563{
564 struct accessmap *map;
565 struct svc_export *export;
566 struct dentry *dentry;
567 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700568 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200570 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (error)
572 goto out;
573
574 export = fhp->fh_export;
575 dentry = fhp->fh_dentry;
576
David Howellse36cb0b2015-01-29 12:02:35 +0000577 if (d_is_reg(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 map = nfs3_regaccess;
David Howellse36cb0b2015-01-29 12:02:35 +0000579 else if (d_is_dir(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 map = nfs3_diraccess;
581 else
582 map = nfs3_anyaccess;
583
584
585 query = *access;
586 for (; map->access; map++) {
587 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700588 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 sresult |= map->access;
591
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700592 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 switch (err2) {
594 case nfs_ok:
595 result |= map->access;
596 break;
597
598 /* the following error codes just mean the access was not allowed,
599 * rather than an error occurred */
600 case nfserr_rofs:
601 case nfserr_acces:
602 case nfserr_perm:
603 /* simply don't "or" in the access bit. */
604 break;
605 default:
606 error = err2;
607 goto out;
608 }
609 }
610 }
611 *access = result;
612 if (supported)
613 *supported = sresult;
614
615 out:
616 return error;
617}
618#endif /* CONFIG_NFSD_V3 */
619
J. Bruce Fields105f4622011-06-07 11:50:23 -0400620static int nfsd_open_break_lease(struct inode *inode, int access)
621{
622 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
J. Bruce Fields105f4622011-06-07 11:50:23 -0400624 if (access & NFSD_MAY_NOT_BREAK_LEASE)
625 return 0;
626 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
627 return break_lease(inode, mode | O_NONBLOCK);
628}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630/*
631 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400632 * The may_flags argument indicates the type of open (read/write/lock)
633 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * N.B. After this call fhp needs an fh_put
635 */
Al Viro6264d692006-10-19 23:28:58 -0700636__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400637nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400638 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Al Viro765927b2012-06-26 21:58:53 +0400640 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct inode *inode;
Kinglong Mee8519f992014-09-03 08:14:06 +0800642 struct file *file;
Al Viro6264d692006-10-19 23:28:58 -0700643 int flags = O_RDONLY|O_LARGEFILE;
644 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400645 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
David Howellse0e81732009-09-02 09:13:40 +0100647 validate_process_creds();
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
650 * If we get here, then the client has already done an "open",
651 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
652 * in case a chmod has now revoked permission.
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400653 *
654 * Arguably we should also allow the owner override for
655 * directories, but we never have and it doesn't seem to have
656 * caused anyone a problem. If we were to change this, note
657 * also that our filldir callbacks would need a variant of
658 * lookup_one_len that doesn't check permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400660 if (type == S_IFREG)
661 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
662 err = fh_verify(rqstp, fhp, type, may_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (err)
664 goto out;
665
Al Viro765927b2012-06-26 21:58:53 +0400666 path.mnt = fhp->fh_export->ex_path.mnt;
667 path.dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000668 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* Disallow write access to files with the append-only bit set
671 * or any access when mandatory locking enabled
672 */
673 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400674 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400676 /*
677 * We must ignore files (but only files) which might have mandatory
678 * locks on them because there is no way to know if the accesser has
679 * the lock.
680 */
681 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto out;
683
684 if (!inode->i_fop)
685 goto out;
686
Bernd Schubert999448a2012-03-18 22:44:49 -0400687 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700688 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto out_nfserr;
690
Bernd Schubert999448a2012-03-18 22:44:49 -0400691 if (may_flags & NFSD_MAY_WRITE) {
692 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700693 flags = O_RDWR|O_LARGEFILE;
694 else
695 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Bernd Schubert999448a2012-03-18 22:44:49 -0400697
Kinglong Mee8519f992014-09-03 08:14:06 +0800698 file = dentry_open(&path, flags, current_cred());
699 if (IS_ERR(file)) {
700 host_err = PTR_ERR(file);
701 goto out_nfserr;
Bernd Schubert06effdb2012-03-18 22:44:50 -0400702 }
703
Linus Torvalds5e40d332014-10-12 10:13:55 -0400704 host_err = ima_file_check(file, may_flags, 0);
Kinglong Mee8519f992014-09-03 08:14:06 +0800705 if (host_err) {
Christoph Hellwigfd891452015-04-28 15:41:16 +0200706 fput(file);
Kinglong Mee8519f992014-09-03 08:14:06 +0800707 goto out_nfserr;
708 }
709
710 if (may_flags & NFSD_MAY_64BIT_COOKIE)
711 file->f_mode |= FMODE_64BITHASH;
712 else
713 file->f_mode |= FMODE_32BITHASH;
714
715 *filp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700717 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718out:
David Howellse0e81732009-09-02 09:13:40 +0100719 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return err;
721}
722
Christoph Hellwige749a462015-06-18 16:44:58 +0200723struct raparms *
724nfsd_init_raparms(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Christoph Hellwige749a462015-06-18 16:44:58 +0200726 struct inode *inode = file_inode(file);
727 dev_t dev = inode->i_sb->s_dev;
728 ino_t ino = inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct raparms *ra, **rap, **frap = NULL;
730 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700731 unsigned int hash;
732 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Greg Banksfce14562006-10-04 02:15:49 -0700734 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
735 rab = &raparm_hash[hash];
736
737 spin_lock(&rab->pb_lock);
738 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (ra->p_ino == ino && ra->p_dev == dev)
740 goto found;
741 depth++;
742 if (ra->p_count == 0)
743 frap = rap;
744 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300745 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700747 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return NULL;
749 }
750 rap = frap;
751 ra = *frap;
752 ra->p_dev = dev;
753 ra->p_ino = ino;
754 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700755 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756found:
Greg Banksfce14562006-10-04 02:15:49 -0700757 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700759 ra->p_next = rab->pb_head;
760 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762 ra->p_count++;
763 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700764 spin_unlock(&rab->pb_lock);
Christoph Hellwige749a462015-06-18 16:44:58 +0200765
766 if (ra->p_set)
767 file->f_ra = ra->p_ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return ra;
769}
770
Christoph Hellwige749a462015-06-18 16:44:58 +0200771void nfsd_put_raparams(struct file *file, struct raparms *ra)
772{
773 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
774
775 spin_lock(&rab->pb_lock);
776 ra->p_ra = file->f_ra;
777 ra->p_set = 1;
778 ra->p_count--;
779 spin_unlock(&rab->pb_lock);
780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200783 * Grab and keep cached pages associated with a file in the svc_rqst
784 * so that they can be passed to the network sendmsg/sendpage routines
785 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
787static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200788nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
789 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Jens Axboecf8208d2007-06-12 21:22:14 +0200791 struct svc_rqst *rqstp = sd->u.data;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500792 struct page **pp = rqstp->rq_next_page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200793 struct page *page = buf->page;
794 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200795
796 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (rqstp->rq_res.page_len == 0) {
799 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500800 put_page(*rqstp->rq_next_page);
801 *(rqstp->rq_next_page++) = page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200802 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700804 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500806 if (*rqstp->rq_next_page)
807 put_page(*rqstp->rq_next_page);
808 *(rqstp->rq_next_page++) = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700810 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return size;
814}
815
Jens Axboecf8208d2007-06-12 21:22:14 +0200816static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
817 struct splice_desc *sd)
818{
819 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
820}
821
Jeff Laytone2afc812014-06-17 07:44:13 -0400822static __be32
823nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Al Viro6264d692006-10-19 23:28:58 -0700825 if (host_err >= 0) {
826 nfsdstats.io_read += host_err;
827 *count = host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500828 fsnotify_access(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 } else
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400831 return nfserrno(host_err);
832}
833
Jeff Laytone2afc812014-06-17 07:44:13 -0400834__be32 nfsd_splice_read(struct svc_rqst *rqstp,
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400835 struct file *file, loff_t offset, unsigned long *count)
836{
837 struct splice_desc sd = {
838 .len = 0,
839 .total_len = *count,
840 .pos = offset,
841 .u.data = rqstp,
842 };
843 int host_err;
844
845 rqstp->rq_next_page = rqstp->rq_respages + 1;
846 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
847 return nfsd_finish_read(file, count, host_err);
848}
849
Jeff Laytone2afc812014-06-17 07:44:13 -0400850__be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400851 unsigned long *count)
852{
853 mm_segment_t oldfs;
854 int host_err;
855
856 oldfs = get_fs();
857 set_fs(KERNEL_DS);
858 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
859 set_fs(oldfs);
860 return nfsd_finish_read(file, count, host_err);
861}
862
863static __be32
864nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
865 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
866{
Jeff Layton779fb0f2014-11-19 07:51:18 -0500867 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400868 return nfsd_splice_read(rqstp, file, offset, count);
869 else
870 return nfsd_readv(file, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700873/*
874 * Gathered writes: If another process is currently writing to the file,
875 * there's a high chance this is another nfsd (triggered by a bulk write
876 * from a client's biod). Rather than syncing the file with each write
877 * request, we sleep for 10 msec.
878 *
879 * I don't know if this roughly approximates C. Juszak's idea of
880 * gathered writes, but it's a nice and simple solution (IMHO), and it
881 * seems to work:-)
882 *
883 * Note: we do this only in the NFSv2 case, since v3 and higher have a
884 * better tool (separate unstable writes and commits) for solving this
885 * problem.
886 */
887static int wait_for_concurrent_writes(struct file *file)
888{
Al Viro496ad9a2013-01-23 17:07:38 -0500889 struct inode *inode = file_inode(file);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700890 static ino_t last_ino;
891 static dev_t last_dev;
892 int err = 0;
893
894 if (atomic_read(&inode->i_writecount) > 1
895 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
896 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
897 msleep(10);
898 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
899 }
900
901 if (inode->i_state & I_DIRTY) {
902 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100903 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700904 }
905 last_ino = inode->i_ino;
906 last_dev = inode->i_sb->s_dev;
907 return err;
908}
909
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200910__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
912 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -0500913 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 struct svc_export *exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct inode *inode;
917 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700918 __be32 err = 0;
919 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400921 int use_wgather;
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700922 loff_t pos = offset;
Zach Browne77a7b42014-10-06 16:42:52 -0700923 loff_t end = LLONG_MAX;
NeilBrown86584522014-05-12 11:22:47 +1000924 unsigned int pflags = current->flags;
925
Jeff Layton7501cc22014-11-19 07:51:15 -0500926 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
NeilBrown86584522014-05-12 11:22:47 +1000927 /*
928 * We want less throttling in balance_dirty_pages()
929 * and shrink_inactive_list() so that nfs to
930 * localhost doesn't cause nfsd to lock up due to all
931 * the client's dirty pages or its congested queue.
932 */
933 current->flags |= PF_LESS_THROTTLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Al Viro6f4e0d52014-10-31 02:42:53 -0400935 inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 exp = fhp->fh_export;
937
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400938 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!EX_ISSYNC(exp))
941 stable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* Write the data. */
944 oldfs = get_fs(); set_fs(KERNEL_DS);
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700945 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700947 if (host_err < 0)
948 goto out_nfserr;
949 *cnt = host_err;
950 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500951 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400953 if (stable) {
Zach Browne77a7b42014-10-06 16:42:52 -0700954 if (use_wgather) {
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400955 host_err = wait_for_concurrent_writes(file);
Zach Browne77a7b42014-10-06 16:42:52 -0700956 } else {
957 if (*cnt)
958 end = offset + *cnt - 1;
959 host_err = vfs_fsync_range(file, offset, end, 0);
960 }
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700963out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700964 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800965 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800967 else
Al Viro6264d692006-10-19 23:28:58 -0700968 err = nfserrno(host_err);
Jeff Layton7501cc22014-11-19 07:51:15 -0500969 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
NeilBrown86584522014-05-12 11:22:47 +1000970 tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return err;
972}
973
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400974/*
975 * Read data from a file. count must contain the requested read count
976 * on entry. On return, *count contains the number of bytes actually read.
977 * N.B. After this call fhp needs an fh_put
978 */
979__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
980 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
981{
982 struct file *file;
983 struct raparms *ra;
984 __be32 err;
985
Christoph Hellwige749a462015-06-18 16:44:58 +0200986 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400987 if (err)
988 return err;
989
Christoph Hellwige749a462015-06-18 16:44:58 +0200990 ra = nfsd_init_raparms(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400991 err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
Christoph Hellwige749a462015-06-18 16:44:58 +0200992 if (ra)
993 nfsd_put_raparams(file, ra);
994 fput(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400995
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -0400996 return err;
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/*
1000 * Write data to a file.
1001 * The stable flag requests synchronous writes.
1002 * N.B. After this call fhp needs an fh_put
1003 */
Al Viro6264d692006-10-19 23:28:58 -07001004__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001006 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int *stablep)
1008{
Al Viro6264d692006-10-19 23:28:58 -07001009 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001012 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001013 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (err)
1015 goto out;
1016 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1017 stablep);
1018 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001019 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (err)
1021 goto out;
1022
1023 if (cnt)
1024 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1025 cnt, stablep);
Christoph Hellwigfd891452015-04-28 15:41:16 +02001026 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028out:
1029 return err;
1030}
1031
1032#ifdef CONFIG_NFSD_V3
1033/*
1034 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001035 *
1036 * Note: we only guarantee that data that lies within the range specified
1037 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 *
1039 * Unfortunately we cannot lock the file to make sure we return full WCC
1040 * data to the client, as locking happens lower down in the filesystem.
1041 */
Al Viro6264d692006-10-19 23:28:58 -07001042__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1044 loff_t offset, unsigned long count)
1045{
1046 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001047 loff_t end = LLONG_MAX;
1048 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Trond Myklebustaa696a62010-01-29 16:44:25 -05001050 if (offset < 0)
1051 goto out;
1052 if (count != 0) {
1053 end = offset + (loff_t)count - 1;
1054 if (end < offset)
1055 goto out;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Jeff Layton91885252010-03-19 08:06:28 -04001058 err = nfsd_open(rqstp, fhp, S_IFREG,
1059 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001060 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001063 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001064
1065 if (err2 != -EINVAL)
1066 err = nfserrno(err2);
1067 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
Christoph Hellwigfd891452015-04-28 15:41:16 +02001071 fput(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001072out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return err;
1074}
1075#endif /* CONFIG_NFSD_V3 */
1076
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001077static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001078nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1079 struct iattr *iap)
1080{
1081 /*
1082 * Mode has already been set earlier in create:
1083 */
1084 iap->ia_valid &= ~ATTR_MODE;
1085 /*
1086 * Setting uid/gid works only for root. Irix appears to
1087 * send along the gid on create when it tries to implement
1088 * setgid directories via NFS:
1089 */
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001090 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001091 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1092 if (iap->ia_valid)
1093 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001094 /* Callers expect file metadata to be committed here */
Jeff Layton722b6202014-07-03 07:54:19 -04001095 return nfserrno(commit_metadata(resfhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001096}
1097
wengang wang4ac35c22009-02-10 11:27:51 +08001098/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1099 * setting size to 0 may fail for some specific file systems by the permission
1100 * checking which requires WRITE permission but the mode is 000.
1101 * we ignore the resizing(to 0) on the just new created file, since the size is
1102 * 0 after file created.
1103 *
1104 * call this only after vfs_create() is called.
1105 * */
1106static void
1107nfsd_check_ignore_resizing(struct iattr *iap)
1108{
1109 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1110 iap->ia_valid &= ~ATTR_SIZE;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/*
1114 * Create a file (regular, directory, device, fifo); UNIX sockets
1115 * not yet implemented.
1116 * If the response fh has been verified, the parent directory should
1117 * already be locked. Note that the parent directory is left locked.
1118 *
1119 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1120 */
Al Viro6264d692006-10-19 23:28:58 -07001121__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1123 char *fname, int flen, struct iattr *iap,
1124 int type, dev_t rdev, struct svc_fh *resfhp)
1125{
1126 struct dentry *dentry, *dchild = NULL;
1127 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001128 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001129 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001130 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 err = nfserr_perm;
1133 if (!flen)
1134 goto out;
1135 err = nfserr_exist;
1136 if (isdotent(fname, flen))
1137 goto out;
1138
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001139 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (err)
1141 goto out;
1142
1143 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001144 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001147 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 goto out;
1149 /*
1150 * Check whether the response file handle has been verified yet.
1151 * If it has, the parent directory should already be locked.
1152 */
1153 if (!resfhp->fh_dentry) {
Jan Kara4a55c102012-06-12 16:20:33 +02001154 host_err = fh_want_write(fhp);
1155 if (host_err)
1156 goto out_nfserr;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001159 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001161 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (IS_ERR(dchild))
1163 goto out_nfserr;
1164 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1165 if (err)
1166 goto out;
1167 } else {
1168 /* called from nfsd_proc_create */
1169 dchild = dget(resfhp->fh_dentry);
1170 if (!fhp->fh_locked) {
1171 /* not actually possible */
1172 printk(KERN_ERR
Al Viroa6a9f182013-09-16 10:57:01 -04001173 "nfsd_create: parent %pd2 not locked!\n",
1174 dentry);
Al Virod75f2b92006-01-18 17:43:44 -08001175 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto out;
1177 }
1178 }
1179 /*
1180 * Make sure the child dentry is still negative ...
1181 */
1182 err = nfserr_exist;
David Howells2b0143b2015-03-17 22:25:59 +00001183 if (d_really_is_positive(dchild)) {
Al Viroa6a9f182013-09-16 10:57:01 -04001184 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1185 dentry, dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto out;
1187 }
1188
1189 if (!(iap->ia_valid & ATTR_MODE))
1190 iap->ia_mode = 0;
1191 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1192
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001193 err = nfserr_inval;
1194 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1195 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1196 type);
1197 goto out;
1198 }
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /*
1201 * Get the dir op function pointer.
1202 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001203 err = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001204 host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 switch (type) {
1206 case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04001207 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001208 if (!host_err)
1209 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 break;
1211 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001212 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
1214 case S_IFCHR:
1215 case S_IFBLK:
1216 case S_IFIFO:
1217 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001218 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001220 }
Jan Kara4a55c102012-06-12 16:20:33 +02001221 if (host_err < 0)
Dave Hansen463c3192008-02-15 14:37:57 -08001222 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Ben Myersf5019122010-02-17 14:05:11 -06001224 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Ben Myersf5019122010-02-17 14:05:11 -06001226 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001227 * nfsd_create_setattr already committed the child. Transactional
1228 * filesystems had a chance to commit changes for both parent and
1229 * child * simultaneously making the following commit_metadata a
1230 * noop.
Ben Myersf5019122010-02-17 14:05:11 -06001231 */
1232 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001233 if (err2)
1234 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /*
1236 * Update the file handle to get the new inode info.
1237 */
1238 if (!err)
1239 err = fh_update(resfhp);
1240out:
1241 if (dchild && !IS_ERR(dchild))
1242 dput(dchild);
1243 return err;
1244
1245out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001246 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 goto out;
1248}
1249
1250#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001253 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
Al Viro6264d692006-10-19 23:28:58 -07001255__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001256do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 char *fname, int flen, struct iattr *iap,
1258 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001259 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct dentry *dentry, *dchild = NULL;
1262 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001263 __be32 err;
1264 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 err = nfserr_perm;
1268 if (!flen)
1269 goto out;
1270 err = nfserr_exist;
1271 if (isdotent(fname, flen))
1272 goto out;
1273 if (!(iap->ia_valid & ATTR_MODE))
1274 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001275 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (err)
1277 goto out;
1278
1279 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001280 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /* Get all the sanity checks out of the way before
1283 * we lock the parent. */
1284 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001285 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001287
1288 host_err = fh_want_write(fhp);
1289 if (host_err)
1290 goto out_nfserr;
1291
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001292 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /*
1295 * Compose the response file handle.
1296 */
1297 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001298 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (IS_ERR(dchild))
1300 goto out_nfserr;
1301
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001302 /* If file doesn't exist, check for permissions to create one */
David Howells2b0143b2015-03-17 22:25:59 +00001303 if (d_really_is_negative(dchild)) {
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001304 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1305 if (err)
1306 goto out;
1307 }
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1310 if (err)
1311 goto out;
1312
Mi Jinlongac6721a2011-04-20 17:06:25 +08001313 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001314 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001315 * the high bit set, so just clear the high bits. If this is
1316 * ever changed to use different attrs for storing the
1317 * verifier, then do_open_lookup() will also need to be fixed
1318 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
1320 v_mtime = verifier[0]&0x7fffffff;
1321 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
David Howells2b0143b2015-03-17 22:25:59 +00001324 if (d_really_is_positive(dchild)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 err = 0;
1326
1327 switch (createmode) {
1328 case NFS3_CREATE_UNCHECKED:
David Howellse36cb0b2015-01-29 12:02:35 +00001329 if (! d_is_reg(dchild))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 else if (truncp) {
1332 /* in nfsv4, we need to treat this case a little
1333 * differently. we don't want to truncate the
1334 * file now; this would be wrong if the OPEN
1335 * fails for some other reason. furthermore,
1336 * if the size is nonzero, we should ignore it
1337 * according to spec!
1338 */
1339 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1340 }
1341 else {
1342 iap->ia_valid &= ATTR_SIZE;
1343 goto set_attr;
1344 }
1345 break;
1346 case NFS3_CREATE_EXCLUSIVE:
David Howells2b0143b2015-03-17 22:25:59 +00001347 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1348 && d_inode(dchild)->i_atime.tv_sec == v_atime
1349 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001350 if (created)
1351 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 break;
Neil Brown7007c902012-12-07 15:40:55 -05001353 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001354 case NFS4_CREATE_EXCLUSIVE4_1:
David Howells2b0143b2015-03-17 22:25:59 +00001355 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1356 && d_inode(dchild)->i_atime.tv_sec == v_atime
1357 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001358 if (created)
1359 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001360 goto set_attr;
Neil Brown7007c902012-12-07 15:40:55 -05001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* fallthru */
1363 case NFS3_CREATE_GUARDED:
1364 err = nfserr_exist;
1365 }
Al Virobad0dcf2011-11-23 12:03:18 -05001366 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 goto out;
1368 }
1369
Al Viro312b63f2012-06-10 18:09:36 -04001370 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001371 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001372 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001374 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001375 if (created)
1376 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
wengang wang4ac35c22009-02-10 11:27:51 +08001378 nfsd_check_ignore_resizing(iap);
1379
Mi Jinlongac6721a2011-04-20 17:06:25 +08001380 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001381 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001383 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* XXX someone who knows this better please fix it for nsec */
1385 iap->ia_mtime.tv_sec = v_mtime;
1386 iap->ia_atime.tv_sec = v_atime;
1387 iap->ia_mtime.tv_nsec = 0;
1388 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001392 err = nfsd_create_setattr(rqstp, resfhp, iap);
1393
1394 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001395 * nfsd_create_setattr already committed the child
1396 * (and possibly also the parent).
Ben Myersf5019122010-02-17 14:05:11 -06001397 */
1398 if (!err)
1399 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001400
1401 /*
1402 * Update the filehandle to get the new inode info.
1403 */
1404 if (!err)
1405 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 out:
1408 fh_unlock(fhp);
1409 if (dchild && !IS_ERR(dchild))
1410 dput(dchild);
Jan Kara4a55c102012-06-12 16:20:33 +02001411 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return err;
1413
1414 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001415 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 goto out;
1417}
1418#endif /* CONFIG_NFSD_V3 */
1419
1420/*
1421 * Read a symlink. On entry, *lenp must contain the maximum path length that
1422 * fits into the buffer. On return, it contains the true length.
1423 * N.B. After this call fhp needs an fh_put
1424 */
Al Viro6264d692006-10-19 23:28:58 -07001425__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 struct inode *inode;
1429 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001430 __be32 err;
1431 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001432 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001434 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (err)
1436 goto out;
1437
Al Viro68ac1232012-03-15 08:21:57 -04001438 path.mnt = fhp->fh_export->ex_path.mnt;
1439 path.dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001440 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001443 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 goto out;
1445
Al Viro68ac1232012-03-15 08:21:57 -04001446 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* N.B. Why does this call need a get_fs()??
1448 * Remove the set_fs and watch the fireworks:-) --okir
1449 */
1450
1451 oldfs = get_fs(); set_fs(KERNEL_DS);
J. Bruce Fieldsfac7a172012-07-27 14:16:55 -04001452 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 set_fs(oldfs);
1454
Al Viro6264d692006-10-19 23:28:58 -07001455 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001457 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 err = 0;
1459out:
1460 return err;
1461
1462out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001463 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto out;
1465}
1466
1467/*
1468 * Create a symlink and look up its inode
1469 * N.B. After this call _both_ fhp and resfhp need an fh_put
1470 */
Al Viro6264d692006-10-19 23:28:58 -07001471__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1473 char *fname, int flen,
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001474 char *path,
Kinglong Mee1e444f52014-07-01 17:48:02 +08001475 struct svc_fh *resfhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001478 __be32 err, cerr;
1479 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 err = nfserr_noent;
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001482 if (!flen || path[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 goto out;
1484 err = nfserr_exist;
1485 if (isdotent(fname, flen))
1486 goto out;
1487
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001488 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (err)
1490 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001491
1492 host_err = fh_want_write(fhp);
1493 if (host_err)
1494 goto out_nfserr;
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 fh_lock(fhp);
1497 dentry = fhp->fh_dentry;
1498 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001499 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (IS_ERR(dnew))
1501 goto out_nfserr;
1502
David Howells2b0143b2015-03-17 22:25:59 +00001503 host_err = vfs_symlink(d_inode(dentry), dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001504 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001505 if (!err)
1506 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 fh_unlock(fhp);
1508
Al Virobad0dcf2011-11-23 12:03:18 -05001509 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1512 dput(dnew);
1513 if (err==0) err = cerr;
1514out:
1515 return err;
1516
1517out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001518 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 goto out;
1520}
1521
1522/*
1523 * Create a hardlink
1524 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1525 */
Al Viro6264d692006-10-19 23:28:58 -07001526__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1528 char *name, int len, struct svc_fh *tfhp)
1529{
1530 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001531 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001532 __be32 err;
1533 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001535 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (err)
1537 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001538 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (err)
1540 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001541 err = nfserr_isdir;
David Howellse36cb0b2015-01-29 12:02:35 +00001542 if (d_is_dir(tfhp->fh_dentry))
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001543 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 err = nfserr_perm;
1545 if (!len)
1546 goto out;
1547 err = nfserr_exist;
1548 if (isdotent(name, len))
1549 goto out;
1550
Jan Kara4a55c102012-06-12 16:20:33 +02001551 host_err = fh_want_write(tfhp);
1552 if (host_err) {
1553 err = nfserrno(host_err);
1554 goto out;
1555 }
1556
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001557 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ddir = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001559 dirp = d_inode(ddir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001562 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (IS_ERR(dnew))
1564 goto out_nfserr;
1565
1566 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001568 err = nfserr_noent;
David Howells2b0143b2015-03-17 22:25:59 +00001569 if (d_really_is_negative(dold))
Jan Kara4a55c102012-06-12 16:20:33 +02001570 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04001571 host_err = vfs_link(dold, dirp, dnew, NULL);
Al Viro6264d692006-10-19 23:28:58 -07001572 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001573 err = nfserrno(commit_metadata(ffhp));
1574 if (!err)
1575 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 } else {
Al Viro6264d692006-10-19 23:28:58 -07001577 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 err = nfserr_acces;
1579 else
Al Viro6264d692006-10-19 23:28:58 -07001580 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001582out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001584out_unlock:
1585 fh_unlock(ffhp);
Jan Kara4a55c102012-06-12 16:20:33 +02001586 fh_drop_write(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587out:
1588 return err;
1589
1590out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001591 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001592 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595/*
1596 * Rename a file
1597 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1598 */
Al Viro6264d692006-10-19 23:28:58 -07001599__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1601 struct svc_fh *tfhp, char *tname, int tlen)
1602{
1603 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1604 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001605 __be32 err;
1606 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001608 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (err)
1610 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001611 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (err)
1613 goto out;
1614
1615 fdentry = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001616 fdir = d_inode(fdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 tdentry = tfhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001619 tdir = d_inode(tdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 err = nfserr_perm;
1622 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1623 goto out;
1624
Jan Kara4a55c102012-06-12 16:20:33 +02001625 host_err = fh_want_write(ffhp);
1626 if (host_err) {
1627 err = nfserrno(host_err);
1628 goto out;
1629 }
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /* cannot use fh_lock as we need deadlock protective ordering
1632 * so do it by hand */
1633 trap = lock_rename(tdentry, fdentry);
1634 ffhp->fh_locked = tfhp->fh_locked = 1;
1635 fill_pre_wcc(ffhp);
1636 fill_pre_wcc(tfhp);
1637
1638 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001639 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (IS_ERR(odentry))
1641 goto out_nfserr;
1642
Al Viro6264d692006-10-19 23:28:58 -07001643 host_err = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001644 if (d_really_is_negative(odentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001646 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (odentry == trap)
1648 goto out_dput_old;
1649
1650 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001651 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (IS_ERR(ndentry))
1653 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001654 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (ndentry == trap)
1656 goto out_dput_new;
1657
Dave Hansen9079b1e2008-02-15 14:37:49 -08001658 host_err = -EXDEV;
1659 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1660 goto out_dput_new;
J. Bruce Fieldsaa387d6c2013-04-15 16:03:46 -04001661 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1662 goto out_dput_new;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001663
Miklos Szeredi520c8b12014-04-01 17:08:42 +02001664 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
Ben Myersf5019122010-02-17 14:05:11 -06001665 if (!host_err) {
1666 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001667 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001668 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 out_dput_new:
1671 dput(ndentry);
1672 out_dput_old:
1673 dput(odentry);
1674 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001675 err = nfserrno(host_err);
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001676 /*
1677 * We cannot rely on fh_unlock on the two filehandles,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 * as that would do the wrong thing if the two directories
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001679 * were the same, so again we do it by hand.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 */
1681 fill_post_wcc(ffhp);
1682 fill_post_wcc(tfhp);
1683 unlock_rename(tdentry, fdentry);
1684 ffhp->fh_locked = tfhp->fh_locked = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001685 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687out:
1688 return err;
1689}
1690
1691/*
1692 * Unlink a file or directory
1693 * N.B. After this call fhp needs an fh_put
1694 */
Al Viro6264d692006-10-19 23:28:58 -07001695__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1697 char *fname, int flen)
1698{
1699 struct dentry *dentry, *rdentry;
1700 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001701 __be32 err;
1702 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 err = nfserr_acces;
1705 if (!flen || isdotent(fname, flen))
1706 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001707 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (err)
1709 goto out;
1710
Jan Kara4a55c102012-06-12 16:20:33 +02001711 host_err = fh_want_write(fhp);
1712 if (host_err)
1713 goto out_nfserr;
1714
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001715 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001717 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001720 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (IS_ERR(rdentry))
1722 goto out_nfserr;
1723
David Howells2b0143b2015-03-17 22:25:59 +00001724 if (d_really_is_negative(rdentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 dput(rdentry);
1726 err = nfserr_noent;
1727 goto out;
1728 }
1729
1730 if (!type)
David Howells2b0143b2015-03-17 22:25:59 +00001731 type = d_inode(rdentry)->i_mode & S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001733 if (type != S_IFDIR)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04001734 host_err = vfs_unlink(dirp, rdentry, NULL);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001735 else
Al Viro6264d692006-10-19 23:28:58 -07001736 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001737 if (!host_err)
1738 host_err = commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 dput(rdentry);
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001742 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001743out:
1744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001748 * We do this buffering because we must not call back into the file
1749 * system's ->lookup() method from the filldir callback. That may well
1750 * deadlock a number of file systems.
1751 *
1752 * This is based heavily on the implementation of same in XFS.
1753 */
1754struct buffered_dirent {
1755 u64 ino;
1756 loff_t offset;
1757 int namlen;
1758 unsigned int d_type;
1759 char name[];
1760};
1761
1762struct readdir_data {
Al Viro5c0ba4e2013-05-15 13:52:59 -04001763 struct dir_context ctx;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001764 char *dirent;
1765 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001766 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001767};
1768
Miklos Szerediac7576f2014-10-30 17:37:34 +01001769static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1770 int namlen, loff_t offset, u64 ino,
1771 unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001772{
Miklos Szerediac7576f2014-10-30 17:37:34 +01001773 struct readdir_data *buf =
1774 container_of(ctx, struct readdir_data, ctx);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001775 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1776 unsigned int reclen;
1777
1778 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001779 if (buf->used + reclen > PAGE_SIZE) {
1780 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001781 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001782 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001783
1784 de->namlen = namlen;
1785 de->offset = offset;
1786 de->ino = ino;
1787 de->d_type = d_type;
1788 memcpy(de->name, name, namlen);
1789 buf->used += reclen;
1790
1791 return 0;
1792}
1793
Miklos Szerediac7576f2014-10-30 17:37:34 +01001794static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
David Woodhouse2f9092e2009-04-20 23:18:37 +01001795 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001796{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001797 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001798 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001799 int size;
1800 loff_t offset;
Al Viroac6614b2013-05-22 22:22:04 -04001801 struct readdir_data buf = {
1802 .ctx.actor = nfsd_buffered_filldir,
1803 .dirent = (void *)__get_free_page(GFP_KERNEL)
1804 };
David Woodhouse2628b762008-07-31 17:16:51 +01001805
David Woodhouse14f7dd62008-07-31 20:29:12 +01001806 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001807 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001808
David Woodhouse14f7dd62008-07-31 20:29:12 +01001809 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001810
1811 while (1) {
Al Viro496ad9a2013-01-23 17:07:38 -05001812 struct inode *dir_inode = file_inode(file);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001813 unsigned int reclen;
1814
Doug Nazarb726e922008-11-05 06:16:28 -05001815 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001816 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001817 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001818
Al Viro5c0ba4e2013-05-15 13:52:59 -04001819 host_err = iterate_dir(file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -04001820 if (buf.full)
1821 host_err = 0;
1822
1823 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001824 break;
1825
1826 size = buf.used;
1827
1828 if (!size)
1829 break;
1830
David Woodhouse2f9092e2009-04-20 23:18:37 +01001831 /*
1832 * Various filldir functions may end up calling back into
1833 * lookup_one_len() and the file system's ->lookup() method.
1834 * These expect i_mutex to be held, as it would within readdir.
1835 */
1836 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1837 if (host_err)
1838 break;
1839
David Woodhouse14f7dd62008-07-31 20:29:12 +01001840 de = (struct buffered_dirent *)buf.dirent;
1841 while (size > 0) {
1842 offset = de->offset;
1843
1844 if (func(cdp, de->name, de->namlen, de->offset,
1845 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01001846 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001847
1848 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001849 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001850
1851 reclen = ALIGN(sizeof(*de) + de->namlen,
1852 sizeof(u64));
1853 size -= reclen;
1854 de = (struct buffered_dirent *)((char *)de + reclen);
1855 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01001856 mutex_unlock(&dir_inode->i_mutex);
1857 if (size > 0) /* We bailed out early */
1858 break;
1859
David Woodhousec002a6c2008-08-17 17:21:18 +01001860 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001861 }
1862
David Woodhouse14f7dd62008-07-31 20:29:12 +01001863 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01001864
1865 if (host_err)
1866 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001867
1868 *offsetp = offset;
1869 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01001870}
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/*
1873 * Read entries from a directory.
1874 * The NFSv3/4 verifier we ignore for now.
1875 */
Al Viro6264d692006-10-19 23:28:58 -07001876__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
Miklos Szerediac7576f2014-10-30 17:37:34 +01001878 struct readdir_cd *cdp, nfsd_filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Al Viro6264d692006-10-19 23:28:58 -07001880 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 struct file *file;
1882 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04001883 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Bernd Schubert06effdb2012-03-18 22:44:50 -04001885 /* NFSv2 only supports 32 bit cookies */
1886 if (rqstp->rq_vers > 2)
1887 may_flags |= NFSD_MAY_64BIT_COOKIE;
1888
1889 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (err)
1891 goto out;
1892
Jeff Laytonb108fe62012-04-25 15:30:00 -04001893 offset = vfs_llseek(file, offset, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (offset < 0) {
1895 err = nfserrno((int)offset);
1896 goto out_close;
1897 }
1898
David Woodhouse14f7dd62008-07-31 20:29:12 +01001899 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 if (err == nfserr_eof || err == nfserr_toosmall)
1902 err = nfs_ok; /* can still be found in ->err */
1903out_close:
Christoph Hellwigfd891452015-04-28 15:41:16 +02001904 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905out:
1906 return err;
1907}
1908
1909/*
1910 * Get file system stats
1911 * N.B. After this call fhp needs an fh_put
1912 */
Al Viro6264d692006-10-19 23:28:58 -07001913__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04001914nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02001916 __be32 err;
1917
1918 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02001919 if (!err) {
1920 struct path path = {
1921 .mnt = fhp->fh_export->ex_path.mnt,
1922 .dentry = fhp->fh_dentry,
1923 };
1924 if (vfs_statfs(&path, stat))
1925 err = nfserr_io;
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return err;
1928}
1929
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001930static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001931{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001932 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001933}
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935/*
1936 * Check for a user's access permissions to this inode.
1937 */
Al Viro6264d692006-10-19 23:28:58 -07001938__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001939nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
1940 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
David Howells2b0143b2015-03-17 22:25:59 +00001942 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int err;
1944
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04001945 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return 0;
1947#if 0
1948 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1949 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001950 (acc & NFSD_MAY_READ)? " read" : "",
1951 (acc & NFSD_MAY_WRITE)? " write" : "",
1952 (acc & NFSD_MAY_EXEC)? " exec" : "",
1953 (acc & NFSD_MAY_SATTR)? " sattr" : "",
1954 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
1955 (acc & NFSD_MAY_LOCK)? " lock" : "",
1956 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 inode->i_mode,
1958 IS_IMMUTABLE(inode)? " immut" : "",
1959 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08001960 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11001962 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963#endif
1964
1965 /* Normally we reject any write/sattr etc access on a read-only file
1966 * system. But if it is IRIX doing check on write-access for a
1967 * device special file, we ignore rofs.
1968 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001969 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
1970 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08001971 if (exp_rdonly(rqstp, exp) ||
1972 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001974 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return nfserr_perm;
1976 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001977 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return nfserr_perm;
1979
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001980 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /* If we cannot rely on authentication in NLM requests,
1982 * just allow locks, otherwise require read permission, or
1983 * ownership
1984 */
1985 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1986 return 0;
1987 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001988 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
1990 /*
1991 * The file owner always gets access permission for accesses that
1992 * would normally be checked at open time. This is to make
1993 * file access work even when the client has done a fchmod(fd, 0).
1994 *
1995 * However, `cp foo bar' should fail nevertheless when bar is
1996 * readonly. A sensible way to do this might be to reject all
1997 * attempts to truncate a read-only file, because a creat() call
1998 * always implies file truncation.
1999 * ... but this isn't really fair. A process may reasonably call
2000 * ftruncate on an open file descriptor on a file with perm 000.
2001 * We must trust the client to do permission checking - using "ACCESS"
2002 * with NFSv3.
2003 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002004 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002005 uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return 0;
2007
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002008 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002009 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 /* Allow read access to binaries even when mode 111 */
2012 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002013 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2014 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002015 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 return err? nfserrno(err) : 0;
2018}
2019
2020void
2021nfsd_racache_shutdown(void)
2022{
Jeff Layton54a66e52008-08-13 22:03:27 -04002023 struct raparms *raparm, *last_raparm;
2024 unsigned int i;
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002027
2028 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2029 raparm = raparm_hash[i].pb_head;
2030 while(raparm) {
2031 last_raparm = raparm;
2032 raparm = raparm->p_next;
2033 kfree(last_raparm);
2034 }
2035 raparm_hash[i].pb_head = NULL;
2036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038/*
2039 * Initialize readahead param cache
2040 */
2041int
2042nfsd_racache_init(int cache_size)
2043{
2044 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002045 int j = 0;
2046 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002047 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Greg Banksfce14562006-10-04 02:15:49 -07002049
Jeff Layton54a66e52008-08-13 22:03:27 -04002050 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002052 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002053 nperbucket = max(2, nperbucket);
Jeff Layton54a66e52008-08-13 22:03:27 -04002054 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002055
2056 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002057
2058 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002059 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002060
2061 raparm = &raparm_hash[i].pb_head;
2062 for (j = 0; j < nperbucket; j++) {
2063 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2064 if (!*raparm)
2065 goto out_nomem;
2066 raparm = &(*raparm)->p_next;
2067 }
2068 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002069 }
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 nfsdstats.ra_size = cache_size;
2072 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002073
2074out_nomem:
2075 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2076 nfsd_racache_shutdown();
2077 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}