blob: 161d2d51b6f2d83125e403fe23fac92a4336063b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/delay.h>
Robert Love0eeca282005-07-12 17:06:03 -040022#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/xattr.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020025#include <linux/jhash.h>
26#include <linux/ima.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020028#include <asm/uaccess.h>
Ben Myersf5019122010-02-17 14:05:11 -060029#include <linux/exportfs.h>
30#include <linux/writeback.h>
David Quigley18032ca2013-05-02 13:19:10 -040031#include <linux/security.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020032
33#ifdef CONFIG_NFSD_V3
34#include "xdr3.h"
35#endif /* CONFIG_NFSD_V3 */
36
Christoph Hellwig5be196e2006-01-09 20:51:55 -080037#ifdef CONFIG_NFSD_V4
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050038#include "acl.h"
39#include "idmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#endif /* CONFIG_NFSD_V4 */
41
Boaz Harrosh9a74af22009-12-03 20:30:56 +020042#include "nfsd.h"
43#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define NFSDDBG_FACILITY NFSDDBG_FILEOP
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * This is a cache of readahead params that help us choose the proper
50 * readahead strategy. Initially, we set all readahead parameters to 0
51 * and let the VFS handle things.
52 * If you increase the number of cached files very much, you'll need to
53 * add a hash table here.
54 */
55struct raparms {
56 struct raparms *p_next;
57 unsigned int p_count;
58 ino_t p_ino;
59 dev_t p_dev;
60 int p_set;
61 struct file_ra_state p_ra;
Greg Banksfce14562006-10-04 02:15:49 -070062 unsigned int p_hindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Greg Banksfce14562006-10-04 02:15:49 -070065struct raparm_hbucket {
66 struct raparms *pb_head;
67 spinlock_t pb_lock;
68} ____cacheline_aligned_in_smp;
69
Greg Banksfce14562006-10-04 02:15:49 -070070#define RAPARM_HASH_BITS 4
71#define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
72#define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
73static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
77 * a mount point.
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080078 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * or nfs_ok having possibly changed *dpp and *expp
80 */
81int
82nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
83 struct svc_export **expp)
84{
85 struct svc_export *exp = *expp, *exp2 = NULL;
86 struct dentry *dentry = *dpp;
Al Viro91c9fa82009-04-18 02:42:05 -040087 struct path path = {.mnt = mntget(exp->ex_path.mnt),
88 .dentry = dget(dentry)};
Al Viro6264d692006-10-19 23:28:58 -070089 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Al Viro7cc90cc2011-03-18 09:04:20 -040091 err = follow_down(&path);
David Howellscc53ce52011-01-14 18:45:26 +000092 if (err < 0)
93 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Al Viro91c9fa82009-04-18 02:42:05 -040095 exp2 = rqst_exp_get_by_name(rqstp, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (IS_ERR(exp2)) {
J. Bruce Fields3b6cee7bc2009-10-25 21:18:19 -040097 err = PTR_ERR(exp2);
98 /*
99 * We normally allow NFS clients to continue
100 * "underneath" a mountpoint that is not exported.
101 * The exception is V4ROOT, where no traversal is ever
102 * allowed without an explicit export of the new
103 * directory.
104 */
105 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
106 err = 0;
Al Viro91c9fa82009-04-18 02:42:05 -0400107 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 goto out;
109 }
Steve Dickson3c394dd2009-09-09 15:02:40 -0400110 if (nfsd_v4client(rqstp) ||
111 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* successfully crossed mount point */
Al Viro1644ccc2009-04-18 02:32:31 -0400113 /*
Al Viro91c9fa82009-04-18 02:42:05 -0400114 * This is subtle: path.dentry is *not* on path.mnt
115 * at this point. The only reason we are safe is that
116 * original mnt is pinned down by exp, so we should
117 * put path *before* putting exp
Al Viro1644ccc2009-04-18 02:32:31 -0400118 */
Al Viro91c9fa82009-04-18 02:42:05 -0400119 *dpp = path.dentry;
120 path.dentry = dentry;
Al Viro1644ccc2009-04-18 02:32:31 -0400121 *expp = exp2;
Al Viro91c9fa82009-04-18 02:42:05 -0400122 exp2 = exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Al Viro91c9fa82009-04-18 02:42:05 -0400124 path_put(&path);
125 exp_put(exp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126out:
127 return err;
128}
129
J. Bruce Fields289ede42009-09-26 20:32:24 -0400130static void follow_to_parent(struct path *path)
131{
132 struct dentry *dp;
133
134 while (path->dentry == path->mnt->mnt_root && follow_up(path))
135 ;
136 dp = dget_parent(path->dentry);
137 dput(path->dentry);
138 path->dentry = dp;
139}
140
141static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
142{
143 struct svc_export *exp2;
144 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
145 .dentry = dget(dparent)};
146
147 follow_to_parent(&path);
148
149 exp2 = rqst_exp_parent(rqstp, &path);
150 if (PTR_ERR(exp2) == -ENOENT) {
151 *dentryp = dget(dparent);
152 } else if (IS_ERR(exp2)) {
153 path_put(&path);
154 return PTR_ERR(exp2);
155 } else {
156 *dentryp = dget(path.dentry);
157 exp_put(*exp);
158 *exp = exp2;
159 }
160 path_put(&path);
161 return 0;
162}
163
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400164/*
165 * For nfsd purposes, we treat V4ROOT exports as though there was an
166 * export at *every* directory.
167 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -0400168int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400169{
170 if (d_mountpoint(dentry))
171 return 1;
Trond Myklebust11fcee022011-09-12 19:37:26 -0400172 if (nfsd4_is_junction(dentry))
173 return 1;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400174 if (!(exp->ex_flags & NFSEXP_V4ROOT))
175 return 0;
176 return dentry->d_inode != NULL;
177}
178
Al Viro6264d692006-10-19 23:28:58 -0700179__be32
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700180nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400181 const char *name, unsigned int len,
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700182 struct svc_export **exp_ret, struct dentry **dentry_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct svc_export *exp;
185 struct dentry *dparent;
186 struct dentry *dentry;
Al Viro6264d692006-10-19 23:28:58 -0700187 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 dparent = fhp->fh_dentry;
Kinglong Meebf18f162014-06-10 22:06:44 +0800192 exp = exp_get(fhp->fh_export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Lookup the name, but don't follow links */
195 if (isdotent(name, len)) {
196 if (len==1)
197 dentry = dget(dparent);
Jan Blunck54775492008-02-14 19:38:39 -0800198 else if (dparent != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 dentry = dget_parent(dparent);
J. Bruce Fieldsfed83812009-09-26 16:53:01 -0400200 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 dentry = dget(dparent); /* .. == . just like at / */
202 else {
203 /* checking mountpoint crossing is very different when stepping up */
J. Bruce Fields289ede42009-09-26 20:32:24 -0400204 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
205 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 } else {
J. Bruce Fields43357232014-01-24 18:04:40 -0500209 /*
210 * In the nfsd4_open() case, this may be held across
211 * subsequent open and delegation acquisition which may
212 * need to take the child's i_mutex:
213 */
214 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dentry = lookup_one_len(name, dparent, len);
Al Viro6264d692006-10-19 23:28:58 -0700216 host_err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (IS_ERR(dentry))
218 goto out_nfserr;
219 /*
220 * check if we have crossed a mount point ...
221 */
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400222 if (nfsd_mountpoint(dentry, exp)) {
Al Viro6264d692006-10-19 23:28:58 -0700223 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dput(dentry);
225 goto out_nfserr;
226 }
227 }
228 }
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700229 *dentry_ret = dentry;
230 *exp_ret = exp;
231 return 0;
232
233out_nfserr:
234 exp_put(exp);
235 return nfserrno(host_err);
236}
237
238/*
239 * Look up one component of a pathname.
240 * N.B. After this call _both_ fhp and resfh need an fh_put
241 *
242 * If the lookup would cross a mountpoint, and the mounted filesystem
243 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
244 * accepted as it stands and the mounted directory is
245 * returned. Otherwise the covered directory is returned.
246 * NOTE: this mountpoint crossing is not supported properly by all
247 * clients and is explicitly disallowed for NFSv3
248 * NeilBrown <neilb@cse.unsw.edu.au>
249 */
250__be32
251nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400252 unsigned int len, struct svc_fh *resfh)
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700253{
254 struct svc_export *exp;
255 struct dentry *dentry;
256 __be32 err;
257
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400258 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
259 if (err)
260 return err;
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700261 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
262 if (err)
263 return err;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700264 err = check_nfsd_access(exp, rqstp);
265 if (err)
266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /*
268 * Note: we compose the file handle now, but as the
269 * dentry may be negative, it may need to be updated.
270 */
271 err = fh_compose(resfh, exp, dentry, fhp);
272 if (!err && !dentry->d_inode)
273 err = nfserr_noent;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700274out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 exp_put(exp);
277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Ben Myersf5019122010-02-17 14:05:11 -0600280/*
281 * Commit metadata changes to stable storage.
282 */
283static int
284commit_metadata(struct svc_fh *fhp)
285{
286 struct inode *inode = fhp->fh_dentry->d_inode;
287 const struct export_operations *export_ops = inode->i_sb->s_export_op;
Ben Myersf5019122010-02-17 14:05:11 -0600288
289 if (!EX_ISSYNC(fhp->fh_export))
290 return 0;
291
Christoph Hellwigc37650162010-10-06 10:48:20 +0200292 if (export_ops->commit_metadata)
293 return export_ops->commit_metadata(inode);
294 return sync_inode_metadata(inode, 1);
Ben Myersf5019122010-02-17 14:05:11 -0600295}
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800298 * Go over the attributes and take care of the small differences between
299 * NFS semantics and what Linux expects.
300 */
301static void
302nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
303{
304 /*
305 * NFSv2 does not differentiate between "set-[ac]time-to-now"
306 * which only requires access, and "set-[ac]time-to-X" which
307 * requires ownership.
308 * So if it looks like it might be "set both to the same time which
309 * is close to now", and if inode_change_ok fails, then we
310 * convert to "set to now" instead of "set to explicit time"
311 *
312 * We only call inode_change_ok as the last test as technically
313 * it is not an interface that we should be using.
314 */
315#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
316#define MAX_TOUCH_TIME_ERROR (30*60)
317 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
318 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
319 /*
320 * Looks probable.
321 *
322 * Now just make sure time is in the right ballpark.
323 * Solaris, at least, doesn't seem to care what the time
324 * request is. We require it be within 30 minutes of now.
325 */
326 time_t delta = iap->ia_atime.tv_sec - get_seconds();
327 if (delta < 0)
328 delta = -delta;
329 if (delta < MAX_TOUCH_TIME_ERROR &&
330 inode_change_ok(inode, iap) != 0) {
331 /*
332 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
333 * This will cause notify_change to set these times
334 * to "now"
335 */
336 iap->ia_valid &= ~BOTH_TIME_SET;
337 }
338 }
339
340 /* sanitize the mode change */
341 if (iap->ia_valid & ATTR_MODE) {
342 iap->ia_mode &= S_IALLUGO;
343 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
344 }
345
346 /* Revoke setuid/setgid on chown */
347 if (!S_ISDIR(inode->i_mode) &&
Stanislav Kholmanskikhc4fa6d72013-12-11 14:16:36 +0400348 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800349 iap->ia_valid |= ATTR_KILL_PRIV;
350 if (iap->ia_valid & ATTR_MODE) {
351 /* we're setting mode too, just clear the s*id bits */
352 iap->ia_mode &= ~S_ISUID;
353 if (iap->ia_mode & S_IXGRP)
354 iap->ia_mode &= ~S_ISGID;
355 } else {
356 /* set ATTR_KILL_* bits and let VFS handle it */
357 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
358 }
359 }
360}
361
362static __be32
363nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
364 struct iattr *iap)
365{
366 struct inode *inode = fhp->fh_dentry->d_inode;
367 int host_err;
368
369 if (iap->ia_size < inode->i_size) {
370 __be32 err;
371
372 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
373 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
374 if (err)
375 return err;
376 }
377
378 host_err = get_write_access(inode);
379 if (host_err)
380 goto out_nfserrno;
381
382 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
383 if (host_err)
384 goto out_put_write_access;
385 return 0;
386
387out_put_write_access:
388 put_write_access(inode);
389out_nfserrno:
390 return nfserrno(host_err);
391}
392
393/*
394 * Set various file attributes. After this call fhp needs an fh_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Al Viro6264d692006-10-19 23:28:58 -0700396__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
398 int check_guard, time_t guardtime)
399{
400 struct dentry *dentry;
401 struct inode *inode;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200402 int accmode = NFSD_MAY_SATTR;
Al Viro175a4eb2011-07-26 03:30:54 -0400403 umode_t ftype = 0;
Al Viro6264d692006-10-19 23:28:58 -0700404 __be32 err;
405 int host_err;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500406 bool get_write_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int size_change = 0;
408
409 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200410 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (iap->ia_valid & ATTR_SIZE)
412 ftype = S_IFREG;
413
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500414 /* Callers that do fh_verify should do the fh_want_write: */
415 get_write_count = !fhp->fh_dentry;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* Get inode */
418 err = fh_verify(rqstp, fhp, ftype, accmode);
NeilBrown15b7a1b2005-11-07 01:00:23 -0800419 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto out;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500421 if (get_write_count) {
422 host_err = fh_want_write(fhp);
423 if (host_err)
424 return nfserrno(host_err);
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 dentry = fhp->fh_dentry;
428 inode = dentry->d_inode;
429
NeilBrown15b7a1b2005-11-07 01:00:23 -0800430 /* Ignore any mode updates on symlinks */
431 if (S_ISLNK(inode->i_mode))
432 iap->ia_valid &= ~ATTR_MODE;
433
434 if (!iap->ia_valid)
435 goto out;
436
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800437 nfsd_sanitize_attrs(inode, iap);
438
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000439 /*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800440 * The size case is special, it changes the file in addition to the
441 * attributes.
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (iap->ia_valid & ATTR_SIZE) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800444 err = nfsd_get_write_access(rqstp, fhp, iap);
445 if (err)
446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 size_change = 1;
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700448
449 /*
450 * RFC5661, Section 18.30.4:
451 * Changing the size of a file with SETATTR indirectly
452 * changes the time_modify and change attributes.
453 *
454 * (and similar for the older RFCs)
455 */
456 if (iap->ia_size != i_size_read(inode))
457 iap->ia_valid |= ATTR_MTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 iap->ia_valid |= ATTR_CTIME;
461
Christoph Hellwig987da472013-11-18 05:07:47 -0800462 if (check_guard && guardtime != inode->i_ctime.tv_sec) {
463 err = nfserr_notsync;
464 goto out_put_write_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Christoph Hellwig987da472013-11-18 05:07:47 -0800466
Christoph Hellwig987da472013-11-18 05:07:47 -0800467 fh_lock(fhp);
468 host_err = notify_change(dentry, iap, NULL);
469 fh_unlock(fhp);
J. R. Okajima1406b912014-02-19 00:27:53 +0900470 err = nfserrno(host_err);
Christoph Hellwig987da472013-11-18 05:07:47 -0800471
Christoph Hellwig987da472013-11-18 05:07:47 -0800472out_put_write_access:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (size_change)
474 put_write_access(inode);
475 if (!err)
Jeff Layton722b6202014-07-03 07:54:19 -0400476 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477out:
478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800481#if defined(CONFIG_NFSD_V4)
Chuck Lever9b4146e2012-01-04 16:26:43 -0500482/*
483 * NFS junction information is stored in an extended attribute.
484 */
485#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
486
487/**
488 * nfsd4_is_junction - Test if an object could be an NFS junction
489 *
490 * @dentry: object to test
491 *
492 * Returns 1 if "dentry" appears to contain NFS junction information.
493 * Otherwise 0 is returned.
494 */
Trond Myklebust11fcee022011-09-12 19:37:26 -0400495int nfsd4_is_junction(struct dentry *dentry)
496{
497 struct inode *inode = dentry->d_inode;
498
499 if (inode == NULL)
500 return 0;
501 if (inode->i_mode & S_IXUGO)
502 return 0;
503 if (!(inode->i_mode & S_ISVTX))
504 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500505 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee022011-09-12 19:37:26 -0400506 return 0;
507 return 1;
508}
David Quigley18032ca2013-05-02 13:19:10 -0400509#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
510__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
511 struct xdr_netobj *label)
512{
513 __be32 error;
514 int host_error;
515 struct dentry *dentry;
516
517 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
518 if (error)
519 return error;
520
521 dentry = fhp->fh_dentry;
522
523 mutex_lock(&dentry->d_inode->i_mutex);
524 host_error = security_inode_setsecctx(dentry, label->data, label->len);
525 mutex_unlock(&dentry->d_inode->i_mutex);
526 return nfserrno(host_error);
527}
528#else
529__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
530 struct xdr_netobj *label)
531{
532 return nfserr_notsupp;
533}
534#endif
535
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400536#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538#ifdef CONFIG_NFSD_V3
539/*
540 * Check server access rights to a file system object
541 */
542struct accessmap {
543 u32 access;
544 int how;
545};
546static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200547 { NFS3_ACCESS_READ, NFSD_MAY_READ },
548 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
549 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
550 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 { 0, 0 }
553};
554
555static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200556 { NFS3_ACCESS_READ, NFSD_MAY_READ },
557 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
558 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
559 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
560 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 { 0, 0 }
563};
564
565static struct accessmap nfs3_anyaccess[] = {
566 /* Some clients - Solaris 2.6 at least, make an access call
567 * to the server to check for access for things like /dev/null
568 * (which really, the server doesn't care about). So
569 * We provide simple access checking for them, looking
570 * mainly at mode bits, and we make sure to ignore read-only
571 * filesystem checks
572 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200573 { NFS3_ACCESS_READ, NFSD_MAY_READ },
574 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
575 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
576 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 { 0, 0 }
579};
580
Al Viro6264d692006-10-19 23:28:58 -0700581__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
583{
584 struct accessmap *map;
585 struct svc_export *export;
586 struct dentry *dentry;
587 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700588 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200590 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (error)
592 goto out;
593
594 export = fhp->fh_export;
595 dentry = fhp->fh_dentry;
596
597 if (S_ISREG(dentry->d_inode->i_mode))
598 map = nfs3_regaccess;
599 else if (S_ISDIR(dentry->d_inode->i_mode))
600 map = nfs3_diraccess;
601 else
602 map = nfs3_anyaccess;
603
604
605 query = *access;
606 for (; map->access; map++) {
607 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700608 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 sresult |= map->access;
611
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700612 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 switch (err2) {
614 case nfs_ok:
615 result |= map->access;
616 break;
617
618 /* the following error codes just mean the access was not allowed,
619 * rather than an error occurred */
620 case nfserr_rofs:
621 case nfserr_acces:
622 case nfserr_perm:
623 /* simply don't "or" in the access bit. */
624 break;
625 default:
626 error = err2;
627 goto out;
628 }
629 }
630 }
631 *access = result;
632 if (supported)
633 *supported = sresult;
634
635 out:
636 return error;
637}
638#endif /* CONFIG_NFSD_V3 */
639
J. Bruce Fields105f4622011-06-07 11:50:23 -0400640static int nfsd_open_break_lease(struct inode *inode, int access)
641{
642 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
J. Bruce Fields105f4622011-06-07 11:50:23 -0400644 if (access & NFSD_MAY_NOT_BREAK_LEASE)
645 return 0;
646 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
647 return break_lease(inode, mode | O_NONBLOCK);
648}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650/*
651 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400652 * The may_flags argument indicates the type of open (read/write/lock)
653 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * N.B. After this call fhp needs an fh_put
655 */
Al Viro6264d692006-10-19 23:28:58 -0700656__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400657nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400658 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Al Viro765927b2012-06-26 21:58:53 +0400660 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct inode *inode;
Kinglong Mee8519f992014-09-03 08:14:06 +0800662 struct file *file;
Al Viro6264d692006-10-19 23:28:58 -0700663 int flags = O_RDONLY|O_LARGEFILE;
664 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400665 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
David Howellse0e81732009-09-02 09:13:40 +0100667 validate_process_creds();
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * If we get here, then the client has already done an "open",
671 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
672 * in case a chmod has now revoked permission.
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400673 *
674 * Arguably we should also allow the owner override for
675 * directories, but we never have and it doesn't seem to have
676 * caused anyone a problem. If we were to change this, note
677 * also that our filldir callbacks would need a variant of
678 * lookup_one_len that doesn't check permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400680 if (type == S_IFREG)
681 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
682 err = fh_verify(rqstp, fhp, type, may_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (err)
684 goto out;
685
Al Viro765927b2012-06-26 21:58:53 +0400686 path.mnt = fhp->fh_export->ex_path.mnt;
687 path.dentry = fhp->fh_dentry;
688 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* Disallow write access to files with the append-only bit set
691 * or any access when mandatory locking enabled
692 */
693 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400694 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400696 /*
697 * We must ignore files (but only files) which might have mandatory
698 * locks on them because there is no way to know if the accesser has
699 * the lock.
700 */
701 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto out;
703
704 if (!inode->i_fop)
705 goto out;
706
Bernd Schubert999448a2012-03-18 22:44:49 -0400707 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700708 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto out_nfserr;
710
Bernd Schubert999448a2012-03-18 22:44:49 -0400711 if (may_flags & NFSD_MAY_WRITE) {
712 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700713 flags = O_RDWR|O_LARGEFILE;
714 else
715 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Bernd Schubert999448a2012-03-18 22:44:49 -0400717
Kinglong Mee8519f992014-09-03 08:14:06 +0800718 file = dentry_open(&path, flags, current_cred());
719 if (IS_ERR(file)) {
720 host_err = PTR_ERR(file);
721 goto out_nfserr;
Bernd Schubert06effdb2012-03-18 22:44:50 -0400722 }
723
Linus Torvalds5e40d332014-10-12 10:13:55 -0400724 host_err = ima_file_check(file, may_flags, 0);
Kinglong Mee8519f992014-09-03 08:14:06 +0800725 if (host_err) {
726 nfsd_close(file);
727 goto out_nfserr;
728 }
729
730 if (may_flags & NFSD_MAY_64BIT_COOKIE)
731 file->f_mode |= FMODE_64BITHASH;
732 else
733 file->f_mode |= FMODE_32BITHASH;
734
735 *filp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700737 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738out:
David Howellse0e81732009-09-02 09:13:40 +0100739 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return err;
741}
742
743/*
744 * Close a file.
745 */
746void
747nfsd_close(struct file *filp)
748{
749 fput(filp);
750}
751
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500752/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * Obtain the readahead parameters for the file
754 * specified by (dev, ino).
755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757static inline struct raparms *
758nfsd_get_raparms(dev_t dev, ino_t ino)
759{
760 struct raparms *ra, **rap, **frap = NULL;
761 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700762 unsigned int hash;
763 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Greg Banksfce14562006-10-04 02:15:49 -0700765 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
766 rab = &raparm_hash[hash];
767
768 spin_lock(&rab->pb_lock);
769 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (ra->p_ino == ino && ra->p_dev == dev)
771 goto found;
772 depth++;
773 if (ra->p_count == 0)
774 frap = rap;
775 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300776 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700778 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return NULL;
780 }
781 rap = frap;
782 ra = *frap;
783 ra->p_dev = dev;
784 ra->p_ino = ino;
785 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700786 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787found:
Greg Banksfce14562006-10-04 02:15:49 -0700788 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700790 ra->p_next = rab->pb_head;
791 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 ra->p_count++;
794 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700795 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return ra;
797}
798
799/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200800 * Grab and keep cached pages associated with a file in the svc_rqst
801 * so that they can be passed to the network sendmsg/sendpage routines
802 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
804static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200805nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
806 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Jens Axboecf8208d2007-06-12 21:22:14 +0200808 struct svc_rqst *rqstp = sd->u.data;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500809 struct page **pp = rqstp->rq_next_page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200810 struct page *page = buf->page;
811 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200812
813 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (rqstp->rq_res.page_len == 0) {
816 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500817 put_page(*rqstp->rq_next_page);
818 *(rqstp->rq_next_page++) = page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200819 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700821 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500823 if (*rqstp->rq_next_page)
824 put_page(*rqstp->rq_next_page);
825 *(rqstp->rq_next_page++) = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700827 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return size;
831}
832
Jens Axboecf8208d2007-06-12 21:22:14 +0200833static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
834 struct splice_desc *sd)
835{
836 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
837}
838
Jeff Laytone2afc812014-06-17 07:44:13 -0400839static __be32
840nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Al Viro6264d692006-10-19 23:28:58 -0700842 if (host_err >= 0) {
843 nfsdstats.io_read += host_err;
844 *count = host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500845 fsnotify_access(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 } else
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400848 return nfserrno(host_err);
849}
850
Jeff Laytone2afc812014-06-17 07:44:13 -0400851__be32 nfsd_splice_read(struct svc_rqst *rqstp,
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400852 struct file *file, loff_t offset, unsigned long *count)
853{
854 struct splice_desc sd = {
855 .len = 0,
856 .total_len = *count,
857 .pos = offset,
858 .u.data = rqstp,
859 };
860 int host_err;
861
862 rqstp->rq_next_page = rqstp->rq_respages + 1;
863 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
864 return nfsd_finish_read(file, count, host_err);
865}
866
Jeff Laytone2afc812014-06-17 07:44:13 -0400867__be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400868 unsigned long *count)
869{
870 mm_segment_t oldfs;
871 int host_err;
872
873 oldfs = get_fs();
874 set_fs(KERNEL_DS);
875 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
876 set_fs(oldfs);
877 return nfsd_finish_read(file, count, host_err);
878}
879
880static __be32
881nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
882 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
883{
884 if (file->f_op->splice_read && rqstp->rq_splice_ok)
885 return nfsd_splice_read(rqstp, file, offset, count);
886 else
887 return nfsd_readv(file, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700890/*
891 * Gathered writes: If another process is currently writing to the file,
892 * there's a high chance this is another nfsd (triggered by a bulk write
893 * from a client's biod). Rather than syncing the file with each write
894 * request, we sleep for 10 msec.
895 *
896 * I don't know if this roughly approximates C. Juszak's idea of
897 * gathered writes, but it's a nice and simple solution (IMHO), and it
898 * seems to work:-)
899 *
900 * Note: we do this only in the NFSv2 case, since v3 and higher have a
901 * better tool (separate unstable writes and commits) for solving this
902 * problem.
903 */
904static int wait_for_concurrent_writes(struct file *file)
905{
Al Viro496ad9a2013-01-23 17:07:38 -0500906 struct inode *inode = file_inode(file);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700907 static ino_t last_ino;
908 static dev_t last_dev;
909 int err = 0;
910
911 if (atomic_read(&inode->i_writecount) > 1
912 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
913 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
914 msleep(10);
915 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
916 }
917
918 if (inode->i_state & I_DIRTY) {
919 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100920 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700921 }
922 last_ino = inode->i_ino;
923 last_dev = inode->i_sb->s_dev;
924 return err;
925}
926
Al Viro6264d692006-10-19 23:28:58 -0700927static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
929 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -0500930 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 struct svc_export *exp;
933 struct dentry *dentry;
934 struct inode *inode;
935 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700936 __be32 err = 0;
937 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400939 int use_wgather;
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700940 loff_t pos = offset;
NeilBrown86584522014-05-12 11:22:47 +1000941 unsigned int pflags = current->flags;
942
943 if (rqstp->rq_local)
944 /*
945 * We want less throttling in balance_dirty_pages()
946 * and shrink_inactive_list() so that nfs to
947 * localhost doesn't cause nfsd to lock up due to all
948 * the client's dirty pages or its congested queue.
949 */
950 current->flags |= PF_LESS_THROTTLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -0800952 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 inode = dentry->d_inode;
954 exp = fhp->fh_export;
955
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400956 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!EX_ISSYNC(exp))
959 stable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* Write the data. */
962 oldfs = get_fs(); set_fs(KERNEL_DS);
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700963 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700965 if (host_err < 0)
966 goto out_nfserr;
967 *cnt = host_err;
968 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500969 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400971 if (stable) {
972 if (use_wgather)
973 host_err = wait_for_concurrent_writes(file);
974 else
975 host_err = vfs_fsync_range(file, offset, offset+*cnt, 0);
976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700978out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700979 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800980 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800982 else
Al Viro6264d692006-10-19 23:28:58 -0700983 err = nfserrno(host_err);
NeilBrown86584522014-05-12 11:22:47 +1000984 if (rqstp->rq_local)
985 tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return err;
987}
988
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400989__be32 nfsd_get_tmp_read_open(struct svc_rqst *rqstp, struct svc_fh *fhp,
990 struct file **file, struct raparms **ra)
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -0400991{
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -0400992 struct inode *inode;
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -0400993 __be32 err;
994
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400995 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, file);
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -0400996 if (err)
997 return err;
998
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400999 inode = file_inode(*file);
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001000
1001 /* Get readahead parameters */
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001002 *ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001003
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001004 if (*ra && (*ra)->p_set)
1005 (*file)->f_ra = (*ra)->p_ra;
1006 return nfs_ok;
1007}
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001008
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001009void nfsd_put_tmp_read_open(struct file *file, struct raparms *ra)
1010{
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001011 /* Write back readahead params */
1012 if (ra) {
1013 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1014 spin_lock(&rab->pb_lock);
1015 ra->p_ra = file->f_ra;
1016 ra->p_set = 1;
1017 ra->p_count--;
1018 spin_unlock(&rab->pb_lock);
1019 }
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001020 nfsd_close(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001021}
1022
1023/*
1024 * Read data from a file. count must contain the requested read count
1025 * on entry. On return, *count contains the number of bytes actually read.
1026 * N.B. After this call fhp needs an fh_put
1027 */
1028__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1029 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1030{
1031 struct file *file;
1032 struct raparms *ra;
1033 __be32 err;
1034
1035 err = nfsd_get_tmp_read_open(rqstp, fhp, &file, &ra);
1036 if (err)
1037 return err;
1038
1039 err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
1040
1041 nfsd_put_tmp_read_open(file, ra);
1042
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001043 return err;
1044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
1047 * Write data to a file.
1048 * The stable flag requests synchronous writes.
1049 * N.B. After this call fhp needs an fh_put
1050 */
Al Viro6264d692006-10-19 23:28:58 -07001051__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001053 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 int *stablep)
1055{
Al Viro6264d692006-10-19 23:28:58 -07001056 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001059 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001060 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (err)
1062 goto out;
1063 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1064 stablep);
1065 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001066 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (err)
1068 goto out;
1069
1070 if (cnt)
1071 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1072 cnt, stablep);
1073 nfsd_close(file);
1074 }
1075out:
1076 return err;
1077}
1078
1079#ifdef CONFIG_NFSD_V3
1080/*
1081 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001082 *
1083 * Note: we only guarantee that data that lies within the range specified
1084 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *
1086 * Unfortunately we cannot lock the file to make sure we return full WCC
1087 * data to the client, as locking happens lower down in the filesystem.
1088 */
Al Viro6264d692006-10-19 23:28:58 -07001089__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1091 loff_t offset, unsigned long count)
1092{
1093 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001094 loff_t end = LLONG_MAX;
1095 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Trond Myklebustaa696a62010-01-29 16:44:25 -05001097 if (offset < 0)
1098 goto out;
1099 if (count != 0) {
1100 end = offset + (loff_t)count - 1;
1101 if (end < offset)
1102 goto out;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Jeff Layton91885252010-03-19 08:06:28 -04001105 err = nfsd_open(rqstp, fhp, S_IFREG,
1106 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001107 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001110 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001111
1112 if (err2 != -EINVAL)
1113 err = nfserrno(err2);
1114 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
1118 nfsd_close(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001119out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return err;
1121}
1122#endif /* CONFIG_NFSD_V3 */
1123
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001124static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001125nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1126 struct iattr *iap)
1127{
1128 /*
1129 * Mode has already been set earlier in create:
1130 */
1131 iap->ia_valid &= ~ATTR_MODE;
1132 /*
1133 * Setting uid/gid works only for root. Irix appears to
1134 * send along the gid on create when it tries to implement
1135 * setgid directories via NFS:
1136 */
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001137 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001138 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1139 if (iap->ia_valid)
1140 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001141 /* Callers expect file metadata to be committed here */
Jeff Layton722b6202014-07-03 07:54:19 -04001142 return nfserrno(commit_metadata(resfhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001143}
1144
wengang wang4ac35c22009-02-10 11:27:51 +08001145/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1146 * setting size to 0 may fail for some specific file systems by the permission
1147 * checking which requires WRITE permission but the mode is 000.
1148 * we ignore the resizing(to 0) on the just new created file, since the size is
1149 * 0 after file created.
1150 *
1151 * call this only after vfs_create() is called.
1152 * */
1153static void
1154nfsd_check_ignore_resizing(struct iattr *iap)
1155{
1156 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1157 iap->ia_valid &= ~ATTR_SIZE;
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/*
1161 * Create a file (regular, directory, device, fifo); UNIX sockets
1162 * not yet implemented.
1163 * If the response fh has been verified, the parent directory should
1164 * already be locked. Note that the parent directory is left locked.
1165 *
1166 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1167 */
Al Viro6264d692006-10-19 23:28:58 -07001168__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1170 char *fname, int flen, struct iattr *iap,
1171 int type, dev_t rdev, struct svc_fh *resfhp)
1172{
1173 struct dentry *dentry, *dchild = NULL;
1174 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001175 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001176 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001177 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 err = nfserr_perm;
1180 if (!flen)
1181 goto out;
1182 err = nfserr_exist;
1183 if (isdotent(fname, flen))
1184 goto out;
1185
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001186 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (err)
1188 goto out;
1189
1190 dentry = fhp->fh_dentry;
1191 dirp = dentry->d_inode;
1192
1193 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001194 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto out;
1196 /*
1197 * Check whether the response file handle has been verified yet.
1198 * If it has, the parent directory should already be locked.
1199 */
1200 if (!resfhp->fh_dentry) {
Jan Kara4a55c102012-06-12 16:20:33 +02001201 host_err = fh_want_write(fhp);
1202 if (host_err)
1203 goto out_nfserr;
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001206 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001208 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (IS_ERR(dchild))
1210 goto out_nfserr;
1211 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1212 if (err)
1213 goto out;
1214 } else {
1215 /* called from nfsd_proc_create */
1216 dchild = dget(resfhp->fh_dentry);
1217 if (!fhp->fh_locked) {
1218 /* not actually possible */
1219 printk(KERN_ERR
Al Viroa6a9f182013-09-16 10:57:01 -04001220 "nfsd_create: parent %pd2 not locked!\n",
1221 dentry);
Al Virod75f2b92006-01-18 17:43:44 -08001222 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto out;
1224 }
1225 }
1226 /*
1227 * Make sure the child dentry is still negative ...
1228 */
1229 err = nfserr_exist;
1230 if (dchild->d_inode) {
Al Viroa6a9f182013-09-16 10:57:01 -04001231 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1232 dentry, dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 goto out;
1234 }
1235
1236 if (!(iap->ia_valid & ATTR_MODE))
1237 iap->ia_mode = 0;
1238 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1239
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001240 err = nfserr_inval;
1241 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1242 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1243 type);
1244 goto out;
1245 }
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /*
1248 * Get the dir op function pointer.
1249 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001250 err = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001251 host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 switch (type) {
1253 case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04001254 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001255 if (!host_err)
1256 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
1258 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001259 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261 case S_IFCHR:
1262 case S_IFBLK:
1263 case S_IFIFO:
1264 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001265 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001267 }
Jan Kara4a55c102012-06-12 16:20:33 +02001268 if (host_err < 0)
Dave Hansen463c3192008-02-15 14:37:57 -08001269 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Ben Myersf5019122010-02-17 14:05:11 -06001271 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Ben Myersf5019122010-02-17 14:05:11 -06001273 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001274 * nfsd_create_setattr already committed the child. Transactional
1275 * filesystems had a chance to commit changes for both parent and
1276 * child * simultaneously making the following commit_metadata a
1277 * noop.
Ben Myersf5019122010-02-17 14:05:11 -06001278 */
1279 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001280 if (err2)
1281 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * Update the file handle to get the new inode info.
1284 */
1285 if (!err)
1286 err = fh_update(resfhp);
1287out:
1288 if (dchild && !IS_ERR(dchild))
1289 dput(dchild);
1290 return err;
1291
1292out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001293 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out;
1295}
1296
1297#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001298
1299static inline int nfsd_create_is_exclusive(int createmode)
1300{
1301 return createmode == NFS3_CREATE_EXCLUSIVE
1302 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001306 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
Al Viro6264d692006-10-19 23:28:58 -07001308__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001309do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 char *fname, int flen, struct iattr *iap,
1311 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001312 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct dentry *dentry, *dchild = NULL;
1315 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001316 __be32 err;
1317 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 err = nfserr_perm;
1321 if (!flen)
1322 goto out;
1323 err = nfserr_exist;
1324 if (isdotent(fname, flen))
1325 goto out;
1326 if (!(iap->ia_valid & ATTR_MODE))
1327 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001328 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (err)
1330 goto out;
1331
1332 dentry = fhp->fh_dentry;
1333 dirp = dentry->d_inode;
1334
1335 /* Get all the sanity checks out of the way before
1336 * we lock the parent. */
1337 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001338 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001340
1341 host_err = fh_want_write(fhp);
1342 if (host_err)
1343 goto out_nfserr;
1344
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001345 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /*
1348 * Compose the response file handle.
1349 */
1350 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001351 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (IS_ERR(dchild))
1353 goto out_nfserr;
1354
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001355 /* If file doesn't exist, check for permissions to create one */
1356 if (!dchild->d_inode) {
1357 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1358 if (err)
1359 goto out;
1360 }
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1363 if (err)
1364 goto out;
1365
Mi Jinlongac6721a2011-04-20 17:06:25 +08001366 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001367 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001368 * the high bit set, so just clear the high bits. If this is
1369 * ever changed to use different attrs for storing the
1370 * verifier, then do_open_lookup() will also need to be fixed
1371 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
1373 v_mtime = verifier[0]&0x7fffffff;
1374 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
1377 if (dchild->d_inode) {
1378 err = 0;
1379
1380 switch (createmode) {
1381 case NFS3_CREATE_UNCHECKED:
1382 if (! S_ISREG(dchild->d_inode->i_mode))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 else if (truncp) {
1385 /* in nfsv4, we need to treat this case a little
1386 * differently. we don't want to truncate the
1387 * file now; this would be wrong if the OPEN
1388 * fails for some other reason. furthermore,
1389 * if the size is nonzero, we should ignore it
1390 * according to spec!
1391 */
1392 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1393 }
1394 else {
1395 iap->ia_valid &= ATTR_SIZE;
1396 goto set_attr;
1397 }
1398 break;
1399 case NFS3_CREATE_EXCLUSIVE:
1400 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1401 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown7007c902012-12-07 15:40:55 -05001402 && dchild->d_inode->i_size == 0 ) {
1403 if (created)
1404 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 break;
Neil Brown7007c902012-12-07 15:40:55 -05001406 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001407 case NFS4_CREATE_EXCLUSIVE4_1:
1408 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1409 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown7007c902012-12-07 15:40:55 -05001410 && dchild->d_inode->i_size == 0 ) {
1411 if (created)
1412 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001413 goto set_attr;
Neil Brown7007c902012-12-07 15:40:55 -05001414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* fallthru */
1416 case NFS3_CREATE_GUARDED:
1417 err = nfserr_exist;
1418 }
Al Virobad0dcf2011-11-23 12:03:18 -05001419 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto out;
1421 }
1422
Al Viro312b63f2012-06-10 18:09:36 -04001423 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001424 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001425 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001427 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001428 if (created)
1429 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
wengang wang4ac35c22009-02-10 11:27:51 +08001431 nfsd_check_ignore_resizing(iap);
1432
Mi Jinlongac6721a2011-04-20 17:06:25 +08001433 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001434 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001436 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* XXX someone who knows this better please fix it for nsec */
1438 iap->ia_mtime.tv_sec = v_mtime;
1439 iap->ia_atime.tv_sec = v_atime;
1440 iap->ia_mtime.tv_nsec = 0;
1441 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001445 err = nfsd_create_setattr(rqstp, resfhp, iap);
1446
1447 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001448 * nfsd_create_setattr already committed the child
1449 * (and possibly also the parent).
Ben Myersf5019122010-02-17 14:05:11 -06001450 */
1451 if (!err)
1452 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001453
1454 /*
1455 * Update the filehandle to get the new inode info.
1456 */
1457 if (!err)
1458 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 out:
1461 fh_unlock(fhp);
1462 if (dchild && !IS_ERR(dchild))
1463 dput(dchild);
Jan Kara4a55c102012-06-12 16:20:33 +02001464 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return err;
1466
1467 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001468 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 goto out;
1470}
1471#endif /* CONFIG_NFSD_V3 */
1472
1473/*
1474 * Read a symlink. On entry, *lenp must contain the maximum path length that
1475 * fits into the buffer. On return, it contains the true length.
1476 * N.B. After this call fhp needs an fh_put
1477 */
Al Viro6264d692006-10-19 23:28:58 -07001478__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct inode *inode;
1482 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001483 __be32 err;
1484 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001485 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001487 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (err)
1489 goto out;
1490
Al Viro68ac1232012-03-15 08:21:57 -04001491 path.mnt = fhp->fh_export->ex_path.mnt;
1492 path.dentry = fhp->fh_dentry;
1493 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001496 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto out;
1498
Al Viro68ac1232012-03-15 08:21:57 -04001499 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* N.B. Why does this call need a get_fs()??
1501 * Remove the set_fs and watch the fireworks:-) --okir
1502 */
1503
1504 oldfs = get_fs(); set_fs(KERNEL_DS);
J. Bruce Fieldsfac7a172012-07-27 14:16:55 -04001505 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 set_fs(oldfs);
1507
Al Viro6264d692006-10-19 23:28:58 -07001508 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001510 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 err = 0;
1512out:
1513 return err;
1514
1515out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001516 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 goto out;
1518}
1519
1520/*
1521 * Create a symlink and look up its inode
1522 * N.B. After this call _both_ fhp and resfhp need an fh_put
1523 */
Al Viro6264d692006-10-19 23:28:58 -07001524__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1526 char *fname, int flen,
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001527 char *path,
Kinglong Mee1e444f52014-07-01 17:48:02 +08001528 struct svc_fh *resfhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001531 __be32 err, cerr;
1532 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 err = nfserr_noent;
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001535 if (!flen || path[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 goto out;
1537 err = nfserr_exist;
1538 if (isdotent(fname, flen))
1539 goto out;
1540
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001541 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (err)
1543 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001544
1545 host_err = fh_want_write(fhp);
1546 if (host_err)
1547 goto out_nfserr;
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 fh_lock(fhp);
1550 dentry = fhp->fh_dentry;
1551 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001552 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (IS_ERR(dnew))
1554 goto out_nfserr;
1555
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001556 host_err = vfs_symlink(dentry->d_inode, dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001557 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001558 if (!err)
1559 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 fh_unlock(fhp);
1561
Al Virobad0dcf2011-11-23 12:03:18 -05001562 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1565 dput(dnew);
1566 if (err==0) err = cerr;
1567out:
1568 return err;
1569
1570out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001571 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 goto out;
1573}
1574
1575/*
1576 * Create a hardlink
1577 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1578 */
Al Viro6264d692006-10-19 23:28:58 -07001579__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1581 char *name, int len, struct svc_fh *tfhp)
1582{
1583 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001584 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001585 __be32 err;
1586 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001588 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (err)
1590 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001591 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (err)
1593 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001594 err = nfserr_isdir;
1595 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 err = nfserr_perm;
1598 if (!len)
1599 goto out;
1600 err = nfserr_exist;
1601 if (isdotent(name, len))
1602 goto out;
1603
Jan Kara4a55c102012-06-12 16:20:33 +02001604 host_err = fh_want_write(tfhp);
1605 if (host_err) {
1606 err = nfserrno(host_err);
1607 goto out;
1608 }
1609
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001610 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 ddir = ffhp->fh_dentry;
1612 dirp = ddir->d_inode;
1613
1614 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001615 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (IS_ERR(dnew))
1617 goto out_nfserr;
1618
1619 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001621 err = nfserr_noent;
1622 if (!dold->d_inode)
Jan Kara4a55c102012-06-12 16:20:33 +02001623 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04001624 host_err = vfs_link(dold, dirp, dnew, NULL);
Al Viro6264d692006-10-19 23:28:58 -07001625 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001626 err = nfserrno(commit_metadata(ffhp));
1627 if (!err)
1628 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 } else {
Al Viro6264d692006-10-19 23:28:58 -07001630 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 err = nfserr_acces;
1632 else
Al Viro6264d692006-10-19 23:28:58 -07001633 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001635out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001637out_unlock:
1638 fh_unlock(ffhp);
Jan Kara4a55c102012-06-12 16:20:33 +02001639 fh_drop_write(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640out:
1641 return err;
1642
1643out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001644 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001645 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648/*
1649 * Rename a file
1650 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1651 */
Al Viro6264d692006-10-19 23:28:58 -07001652__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1654 struct svc_fh *tfhp, char *tname, int tlen)
1655{
1656 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1657 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001658 __be32 err;
1659 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001661 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (err)
1663 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001664 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (err)
1666 goto out;
1667
1668 fdentry = ffhp->fh_dentry;
1669 fdir = fdentry->d_inode;
1670
1671 tdentry = tfhp->fh_dentry;
1672 tdir = tdentry->d_inode;
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 err = nfserr_perm;
1675 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1676 goto out;
1677
Jan Kara4a55c102012-06-12 16:20:33 +02001678 host_err = fh_want_write(ffhp);
1679 if (host_err) {
1680 err = nfserrno(host_err);
1681 goto out;
1682 }
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /* cannot use fh_lock as we need deadlock protective ordering
1685 * so do it by hand */
1686 trap = lock_rename(tdentry, fdentry);
1687 ffhp->fh_locked = tfhp->fh_locked = 1;
1688 fill_pre_wcc(ffhp);
1689 fill_pre_wcc(tfhp);
1690
1691 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001692 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (IS_ERR(odentry))
1694 goto out_nfserr;
1695
Al Viro6264d692006-10-19 23:28:58 -07001696 host_err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (!odentry->d_inode)
1698 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001699 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (odentry == trap)
1701 goto out_dput_old;
1702
1703 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001704 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (IS_ERR(ndentry))
1706 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001707 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (ndentry == trap)
1709 goto out_dput_new;
1710
Dave Hansen9079b1e2008-02-15 14:37:49 -08001711 host_err = -EXDEV;
1712 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1713 goto out_dput_new;
J. Bruce Fieldsaa387d6c2013-04-15 16:03:46 -04001714 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1715 goto out_dput_new;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001716
Miklos Szeredi520c8b12014-04-01 17:08:42 +02001717 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
Ben Myersf5019122010-02-17 14:05:11 -06001718 if (!host_err) {
1719 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001720 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001721 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 out_dput_new:
1724 dput(ndentry);
1725 out_dput_old:
1726 dput(odentry);
1727 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001728 err = nfserrno(host_err);
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001729 /*
1730 * We cannot rely on fh_unlock on the two filehandles,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * as that would do the wrong thing if the two directories
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001732 * were the same, so again we do it by hand.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
1734 fill_post_wcc(ffhp);
1735 fill_post_wcc(tfhp);
1736 unlock_rename(tdentry, fdentry);
1737 ffhp->fh_locked = tfhp->fh_locked = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001738 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740out:
1741 return err;
1742}
1743
1744/*
1745 * Unlink a file or directory
1746 * N.B. After this call fhp needs an fh_put
1747 */
Al Viro6264d692006-10-19 23:28:58 -07001748__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1750 char *fname, int flen)
1751{
1752 struct dentry *dentry, *rdentry;
1753 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001754 __be32 err;
1755 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 err = nfserr_acces;
1758 if (!flen || isdotent(fname, flen))
1759 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001760 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (err)
1762 goto out;
1763
Jan Kara4a55c102012-06-12 16:20:33 +02001764 host_err = fh_want_write(fhp);
1765 if (host_err)
1766 goto out_nfserr;
1767
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001768 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 dentry = fhp->fh_dentry;
1770 dirp = dentry->d_inode;
1771
1772 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001773 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (IS_ERR(rdentry))
1775 goto out_nfserr;
1776
1777 if (!rdentry->d_inode) {
1778 dput(rdentry);
1779 err = nfserr_noent;
1780 goto out;
1781 }
1782
1783 if (!type)
1784 type = rdentry->d_inode->i_mode & S_IFMT;
1785
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001786 if (type != S_IFDIR)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04001787 host_err = vfs_unlink(dirp, rdentry, NULL);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001788 else
Al Viro6264d692006-10-19 23:28:58 -07001789 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001790 if (!host_err)
1791 host_err = commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 dput(rdentry);
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001795 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001796out:
1797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001801 * We do this buffering because we must not call back into the file
1802 * system's ->lookup() method from the filldir callback. That may well
1803 * deadlock a number of file systems.
1804 *
1805 * This is based heavily on the implementation of same in XFS.
1806 */
1807struct buffered_dirent {
1808 u64 ino;
1809 loff_t offset;
1810 int namlen;
1811 unsigned int d_type;
1812 char name[];
1813};
1814
1815struct readdir_data {
Al Viro5c0ba4e2013-05-15 13:52:59 -04001816 struct dir_context ctx;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001817 char *dirent;
1818 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001819 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001820};
1821
Miklos Szerediac7576f2014-10-30 17:37:34 +01001822static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1823 int namlen, loff_t offset, u64 ino,
1824 unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001825{
Miklos Szerediac7576f2014-10-30 17:37:34 +01001826 struct readdir_data *buf =
1827 container_of(ctx, struct readdir_data, ctx);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001828 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1829 unsigned int reclen;
1830
1831 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001832 if (buf->used + reclen > PAGE_SIZE) {
1833 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001834 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001835 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001836
1837 de->namlen = namlen;
1838 de->offset = offset;
1839 de->ino = ino;
1840 de->d_type = d_type;
1841 memcpy(de->name, name, namlen);
1842 buf->used += reclen;
1843
1844 return 0;
1845}
1846
Miklos Szerediac7576f2014-10-30 17:37:34 +01001847static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
David Woodhouse2f9092e2009-04-20 23:18:37 +01001848 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001849{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001850 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001851 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001852 int size;
1853 loff_t offset;
Al Viroac6614b2013-05-22 22:22:04 -04001854 struct readdir_data buf = {
1855 .ctx.actor = nfsd_buffered_filldir,
1856 .dirent = (void *)__get_free_page(GFP_KERNEL)
1857 };
David Woodhouse2628b762008-07-31 17:16:51 +01001858
David Woodhouse14f7dd62008-07-31 20:29:12 +01001859 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001860 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001861
David Woodhouse14f7dd62008-07-31 20:29:12 +01001862 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001863
1864 while (1) {
Al Viro496ad9a2013-01-23 17:07:38 -05001865 struct inode *dir_inode = file_inode(file);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001866 unsigned int reclen;
1867
Doug Nazarb726e922008-11-05 06:16:28 -05001868 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001869 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001870 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001871
Al Viro5c0ba4e2013-05-15 13:52:59 -04001872 host_err = iterate_dir(file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -04001873 if (buf.full)
1874 host_err = 0;
1875
1876 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001877 break;
1878
1879 size = buf.used;
1880
1881 if (!size)
1882 break;
1883
David Woodhouse2f9092e2009-04-20 23:18:37 +01001884 /*
1885 * Various filldir functions may end up calling back into
1886 * lookup_one_len() and the file system's ->lookup() method.
1887 * These expect i_mutex to be held, as it would within readdir.
1888 */
1889 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1890 if (host_err)
1891 break;
1892
David Woodhouse14f7dd62008-07-31 20:29:12 +01001893 de = (struct buffered_dirent *)buf.dirent;
1894 while (size > 0) {
1895 offset = de->offset;
1896
1897 if (func(cdp, de->name, de->namlen, de->offset,
1898 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01001899 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001900
1901 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001902 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001903
1904 reclen = ALIGN(sizeof(*de) + de->namlen,
1905 sizeof(u64));
1906 size -= reclen;
1907 de = (struct buffered_dirent *)((char *)de + reclen);
1908 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01001909 mutex_unlock(&dir_inode->i_mutex);
1910 if (size > 0) /* We bailed out early */
1911 break;
1912
David Woodhousec002a6c2008-08-17 17:21:18 +01001913 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001914 }
1915
David Woodhouse14f7dd62008-07-31 20:29:12 +01001916 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01001917
1918 if (host_err)
1919 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001920
1921 *offsetp = offset;
1922 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01001923}
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925/*
1926 * Read entries from a directory.
1927 * The NFSv3/4 verifier we ignore for now.
1928 */
Al Viro6264d692006-10-19 23:28:58 -07001929__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
Miklos Szerediac7576f2014-10-30 17:37:34 +01001931 struct readdir_cd *cdp, nfsd_filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Al Viro6264d692006-10-19 23:28:58 -07001933 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct file *file;
1935 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04001936 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Bernd Schubert06effdb2012-03-18 22:44:50 -04001938 /* NFSv2 only supports 32 bit cookies */
1939 if (rqstp->rq_vers > 2)
1940 may_flags |= NFSD_MAY_64BIT_COOKIE;
1941
1942 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (err)
1944 goto out;
1945
Jeff Laytonb108fe62012-04-25 15:30:00 -04001946 offset = vfs_llseek(file, offset, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (offset < 0) {
1948 err = nfserrno((int)offset);
1949 goto out_close;
1950 }
1951
David Woodhouse14f7dd62008-07-31 20:29:12 +01001952 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 if (err == nfserr_eof || err == nfserr_toosmall)
1955 err = nfs_ok; /* can still be found in ->err */
1956out_close:
1957 nfsd_close(file);
1958out:
1959 return err;
1960}
1961
1962/*
1963 * Get file system stats
1964 * N.B. After this call fhp needs an fh_put
1965 */
Al Viro6264d692006-10-19 23:28:58 -07001966__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04001967nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02001969 __be32 err;
1970
1971 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02001972 if (!err) {
1973 struct path path = {
1974 .mnt = fhp->fh_export->ex_path.mnt,
1975 .dentry = fhp->fh_dentry,
1976 };
1977 if (vfs_statfs(&path, stat))
1978 err = nfserr_io;
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return err;
1981}
1982
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001983static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001984{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001985 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001986}
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988/*
1989 * Check for a user's access permissions to this inode.
1990 */
Al Viro6264d692006-10-19 23:28:58 -07001991__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001992nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
1993 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct inode *inode = dentry->d_inode;
1996 int err;
1997
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04001998 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return 0;
2000#if 0
2001 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2002 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002003 (acc & NFSD_MAY_READ)? " read" : "",
2004 (acc & NFSD_MAY_WRITE)? " write" : "",
2005 (acc & NFSD_MAY_EXEC)? " exec" : "",
2006 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2007 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2008 (acc & NFSD_MAY_LOCK)? " lock" : "",
2009 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 inode->i_mode,
2011 IS_IMMUTABLE(inode)? " immut" : "",
2012 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002013 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002015 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016#endif
2017
2018 /* Normally we reject any write/sattr etc access on a read-only file
2019 * system. But if it is IRIX doing check on write-access for a
2020 * device special file, we ignore rofs.
2021 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002022 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2023 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002024 if (exp_rdonly(rqstp, exp) ||
2025 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002027 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return nfserr_perm;
2029 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002030 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return nfserr_perm;
2032
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002033 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /* If we cannot rely on authentication in NLM requests,
2035 * just allow locks, otherwise require read permission, or
2036 * ownership
2037 */
2038 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2039 return 0;
2040 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002041 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 /*
2044 * The file owner always gets access permission for accesses that
2045 * would normally be checked at open time. This is to make
2046 * file access work even when the client has done a fchmod(fd, 0).
2047 *
2048 * However, `cp foo bar' should fail nevertheless when bar is
2049 * readonly. A sensible way to do this might be to reject all
2050 * attempts to truncate a read-only file, because a creat() call
2051 * always implies file truncation.
2052 * ... but this isn't really fair. A process may reasonably call
2053 * ftruncate on an open file descriptor on a file with perm 000.
2054 * We must trust the client to do permission checking - using "ACCESS"
2055 * with NFSv3.
2056 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002057 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002058 uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return 0;
2060
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002061 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002062 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /* Allow read access to binaries even when mode 111 */
2065 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002066 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2067 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002068 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 return err? nfserrno(err) : 0;
2071}
2072
2073void
2074nfsd_racache_shutdown(void)
2075{
Jeff Layton54a66e52008-08-13 22:03:27 -04002076 struct raparms *raparm, *last_raparm;
2077 unsigned int i;
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002080
2081 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2082 raparm = raparm_hash[i].pb_head;
2083 while(raparm) {
2084 last_raparm = raparm;
2085 raparm = raparm->p_next;
2086 kfree(last_raparm);
2087 }
2088 raparm_hash[i].pb_head = NULL;
2089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091/*
2092 * Initialize readahead param cache
2093 */
2094int
2095nfsd_racache_init(int cache_size)
2096{
2097 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002098 int j = 0;
2099 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002100 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Greg Banksfce14562006-10-04 02:15:49 -07002102
Jeff Layton54a66e52008-08-13 22:03:27 -04002103 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002105 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002106 nperbucket = max(2, nperbucket);
Jeff Layton54a66e52008-08-13 22:03:27 -04002107 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002108
2109 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002110
2111 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002112 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002113
2114 raparm = &raparm_hash[i].pb_head;
2115 for (j = 0; j < nperbucket; j++) {
2116 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2117 if (!*raparm)
2118 goto out_nomem;
2119 raparm = &(*raparm)->p_next;
2120 }
2121 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002122 }
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 nfsdstats.ra_size = cache_size;
2125 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002126
2127out_nomem:
2128 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2129 nfsd_racache_shutdown();
2130 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}