blob: 1426eb66c8c699cf4104bde226986dda77ccc4c8 [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;
192 exp = fhp->fh_export;
193 exp_get(exp);
194
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 {
210 fh_lock(fhp);
211 dentry = lookup_one_len(name, dparent, len);
Al Viro6264d692006-10-19 23:28:58 -0700212 host_err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (IS_ERR(dentry))
214 goto out_nfserr;
215 /*
216 * check if we have crossed a mount point ...
217 */
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400218 if (nfsd_mountpoint(dentry, exp)) {
Al Viro6264d692006-10-19 23:28:58 -0700219 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dput(dentry);
221 goto out_nfserr;
222 }
223 }
224 }
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700225 *dentry_ret = dentry;
226 *exp_ret = exp;
227 return 0;
228
229out_nfserr:
230 exp_put(exp);
231 return nfserrno(host_err);
232}
233
234/*
235 * Look up one component of a pathname.
236 * N.B. After this call _both_ fhp and resfh need an fh_put
237 *
238 * If the lookup would cross a mountpoint, and the mounted filesystem
239 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
240 * accepted as it stands and the mounted directory is
241 * returned. Otherwise the covered directory is returned.
242 * NOTE: this mountpoint crossing is not supported properly by all
243 * clients and is explicitly disallowed for NFSv3
244 * NeilBrown <neilb@cse.unsw.edu.au>
245 */
246__be32
247nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400248 unsigned int len, struct svc_fh *resfh)
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700249{
250 struct svc_export *exp;
251 struct dentry *dentry;
252 __be32 err;
253
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400254 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
255 if (err)
256 return err;
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700257 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
258 if (err)
259 return err;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700260 err = check_nfsd_access(exp, rqstp);
261 if (err)
262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /*
264 * Note: we compose the file handle now, but as the
265 * dentry may be negative, it may need to be updated.
266 */
267 err = fh_compose(resfh, exp, dentry, fhp);
268 if (!err && !dentry->d_inode)
269 err = nfserr_noent;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700270out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 exp_put(exp);
273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
J. Bruce Fields4795bb32011-01-11 13:55:46 -0500276static int nfsd_break_lease(struct inode *inode)
277{
278 if (!S_ISREG(inode->i_mode))
279 return 0;
280 return break_lease(inode, O_WRONLY | O_NONBLOCK);
281}
282
Ben Myersf5019122010-02-17 14:05:11 -0600283/*
284 * Commit metadata changes to stable storage.
285 */
286static int
287commit_metadata(struct svc_fh *fhp)
288{
289 struct inode *inode = fhp->fh_dentry->d_inode;
290 const struct export_operations *export_ops = inode->i_sb->s_export_op;
Ben Myersf5019122010-02-17 14:05:11 -0600291
292 if (!EX_ISSYNC(fhp->fh_export))
293 return 0;
294
Christoph Hellwigc37650162010-10-06 10:48:20 +0200295 if (export_ops->commit_metadata)
296 return export_ops->commit_metadata(inode);
297 return sync_inode_metadata(inode, 1);
Ben Myersf5019122010-02-17 14:05:11 -0600298}
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800301 * Go over the attributes and take care of the small differences between
302 * NFS semantics and what Linux expects.
303 */
304static void
305nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
306{
307 /*
308 * NFSv2 does not differentiate between "set-[ac]time-to-now"
309 * which only requires access, and "set-[ac]time-to-X" which
310 * requires ownership.
311 * So if it looks like it might be "set both to the same time which
312 * is close to now", and if inode_change_ok fails, then we
313 * convert to "set to now" instead of "set to explicit time"
314 *
315 * We only call inode_change_ok as the last test as technically
316 * it is not an interface that we should be using.
317 */
318#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
319#define MAX_TOUCH_TIME_ERROR (30*60)
320 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
321 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
322 /*
323 * Looks probable.
324 *
325 * Now just make sure time is in the right ballpark.
326 * Solaris, at least, doesn't seem to care what the time
327 * request is. We require it be within 30 minutes of now.
328 */
329 time_t delta = iap->ia_atime.tv_sec - get_seconds();
330 if (delta < 0)
331 delta = -delta;
332 if (delta < MAX_TOUCH_TIME_ERROR &&
333 inode_change_ok(inode, iap) != 0) {
334 /*
335 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
336 * This will cause notify_change to set these times
337 * to "now"
338 */
339 iap->ia_valid &= ~BOTH_TIME_SET;
340 }
341 }
342
343 /* sanitize the mode change */
344 if (iap->ia_valid & ATTR_MODE) {
345 iap->ia_mode &= S_IALLUGO;
346 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
347 }
348
349 /* Revoke setuid/setgid on chown */
350 if (!S_ISDIR(inode->i_mode) &&
351 (((iap->ia_valid & ATTR_UID) && !uid_eq(iap->ia_uid, inode->i_uid)) ||
352 ((iap->ia_valid & ATTR_GID) && !gid_eq(iap->ia_gid, inode->i_gid)))) {
353 iap->ia_valid |= ATTR_KILL_PRIV;
354 if (iap->ia_valid & ATTR_MODE) {
355 /* we're setting mode too, just clear the s*id bits */
356 iap->ia_mode &= ~S_ISUID;
357 if (iap->ia_mode & S_IXGRP)
358 iap->ia_mode &= ~S_ISGID;
359 } else {
360 /* set ATTR_KILL_* bits and let VFS handle it */
361 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
362 }
363 }
364}
365
366static __be32
367nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
368 struct iattr *iap)
369{
370 struct inode *inode = fhp->fh_dentry->d_inode;
371 int host_err;
372
373 if (iap->ia_size < inode->i_size) {
374 __be32 err;
375
376 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
377 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
378 if (err)
379 return err;
380 }
381
382 host_err = get_write_access(inode);
383 if (host_err)
384 goto out_nfserrno;
385
386 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
387 if (host_err)
388 goto out_put_write_access;
389 return 0;
390
391out_put_write_access:
392 put_write_access(inode);
393out_nfserrno:
394 return nfserrno(host_err);
395}
396
397/*
398 * Set various file attributes. After this call fhp needs an fh_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Al Viro6264d692006-10-19 23:28:58 -0700400__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
402 int check_guard, time_t guardtime)
403{
404 struct dentry *dentry;
405 struct inode *inode;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200406 int accmode = NFSD_MAY_SATTR;
Al Viro175a4eb2011-07-26 03:30:54 -0400407 umode_t ftype = 0;
Al Viro6264d692006-10-19 23:28:58 -0700408 __be32 err;
409 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int size_change = 0;
411
412 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200413 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (iap->ia_valid & ATTR_SIZE)
415 ftype = S_IFREG;
416
417 /* 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;
421
422 dentry = fhp->fh_dentry;
423 inode = dentry->d_inode;
424
NeilBrown15b7a1b2005-11-07 01:00:23 -0800425 /* Ignore any mode updates on symlinks */
426 if (S_ISLNK(inode->i_mode))
427 iap->ia_valid &= ~ATTR_MODE;
428
429 if (!iap->ia_valid)
430 goto out;
431
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800432 nfsd_sanitize_attrs(inode, iap);
433
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000434 /*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800435 * The size case is special, it changes the file in addition to the
436 * attributes.
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (iap->ia_valid & ATTR_SIZE) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800439 err = nfsd_get_write_access(rqstp, fhp, iap);
440 if (err)
441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 size_change = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 iap->ia_valid |= ATTR_CTIME;
446
Christoph Hellwig987da472013-11-18 05:07:47 -0800447 if (check_guard && guardtime != inode->i_ctime.tv_sec) {
448 err = nfserr_notsync;
449 goto out_put_write_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Christoph Hellwig987da472013-11-18 05:07:47 -0800451
452 host_err = nfsd_break_lease(inode);
453 if (host_err)
454 goto out_put_write_access_nfserror;
455
456 fh_lock(fhp);
457 host_err = notify_change(dentry, iap, NULL);
458 fh_unlock(fhp);
459
460out_put_write_access_nfserror:
461 err = nfserrno(host_err);
462out_put_write_access:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (size_change)
464 put_write_access(inode);
465 if (!err)
Christoph Hellwigb160fda2010-06-01 21:59:18 +0200466 commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467out:
468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800471#if defined(CONFIG_NFSD_V4)
Chuck Lever9b4146e2012-01-04 16:26:43 -0500472/*
473 * NFS junction information is stored in an extended attribute.
474 */
475#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
476
477/**
478 * nfsd4_is_junction - Test if an object could be an NFS junction
479 *
480 * @dentry: object to test
481 *
482 * Returns 1 if "dentry" appears to contain NFS junction information.
483 * Otherwise 0 is returned.
484 */
Trond Myklebust11fcee022011-09-12 19:37:26 -0400485int nfsd4_is_junction(struct dentry *dentry)
486{
487 struct inode *inode = dentry->d_inode;
488
489 if (inode == NULL)
490 return 0;
491 if (inode->i_mode & S_IXUGO)
492 return 0;
493 if (!(inode->i_mode & S_ISVTX))
494 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500495 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee022011-09-12 19:37:26 -0400496 return 0;
497 return 1;
498}
David Quigley18032ca2013-05-02 13:19:10 -0400499#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
500__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
501 struct xdr_netobj *label)
502{
503 __be32 error;
504 int host_error;
505 struct dentry *dentry;
506
507 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
508 if (error)
509 return error;
510
511 dentry = fhp->fh_dentry;
512
513 mutex_lock(&dentry->d_inode->i_mutex);
514 host_error = security_inode_setsecctx(dentry, label->data, label->len);
515 mutex_unlock(&dentry->d_inode->i_mutex);
516 return nfserrno(host_error);
517}
518#else
519__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
520 struct xdr_netobj *label)
521{
522 return nfserr_notsupp;
523}
524#endif
525
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400526#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528#ifdef CONFIG_NFSD_V3
529/*
530 * Check server access rights to a file system object
531 */
532struct accessmap {
533 u32 access;
534 int how;
535};
536static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200537 { NFS3_ACCESS_READ, NFSD_MAY_READ },
538 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
539 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
540 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 { 0, 0 }
543};
544
545static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200546 { NFS3_ACCESS_READ, NFSD_MAY_READ },
547 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
548 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
549 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
550 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 { 0, 0 }
553};
554
555static struct accessmap nfs3_anyaccess[] = {
556 /* Some clients - Solaris 2.6 at least, make an access call
557 * to the server to check for access for things like /dev/null
558 * (which really, the server doesn't care about). So
559 * We provide simple access checking for them, looking
560 * mainly at mode bits, and we make sure to ignore read-only
561 * filesystem checks
562 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200563 { NFS3_ACCESS_READ, NFSD_MAY_READ },
564 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
565 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
566 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 { 0, 0 }
569};
570
Al Viro6264d692006-10-19 23:28:58 -0700571__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
573{
574 struct accessmap *map;
575 struct svc_export *export;
576 struct dentry *dentry;
577 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700578 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200580 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (error)
582 goto out;
583
584 export = fhp->fh_export;
585 dentry = fhp->fh_dentry;
586
587 if (S_ISREG(dentry->d_inode->i_mode))
588 map = nfs3_regaccess;
589 else if (S_ISDIR(dentry->d_inode->i_mode))
590 map = nfs3_diraccess;
591 else
592 map = nfs3_anyaccess;
593
594
595 query = *access;
596 for (; map->access; map++) {
597 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700598 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 sresult |= map->access;
601
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700602 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 switch (err2) {
604 case nfs_ok:
605 result |= map->access;
606 break;
607
608 /* the following error codes just mean the access was not allowed,
609 * rather than an error occurred */
610 case nfserr_rofs:
611 case nfserr_acces:
612 case nfserr_perm:
613 /* simply don't "or" in the access bit. */
614 break;
615 default:
616 error = err2;
617 goto out;
618 }
619 }
620 }
621 *access = result;
622 if (supported)
623 *supported = sresult;
624
625 out:
626 return error;
627}
628#endif /* CONFIG_NFSD_V3 */
629
J. Bruce Fields105f4622011-06-07 11:50:23 -0400630static int nfsd_open_break_lease(struct inode *inode, int access)
631{
632 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
J. Bruce Fields105f4622011-06-07 11:50:23 -0400634 if (access & NFSD_MAY_NOT_BREAK_LEASE)
635 return 0;
636 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
637 return break_lease(inode, mode | O_NONBLOCK);
638}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640/*
641 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400642 * The may_flags argument indicates the type of open (read/write/lock)
643 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * N.B. After this call fhp needs an fh_put
645 */
Al Viro6264d692006-10-19 23:28:58 -0700646__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400647nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400648 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Al Viro765927b2012-06-26 21:58:53 +0400650 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct inode *inode;
Al Viro6264d692006-10-19 23:28:58 -0700652 int flags = O_RDONLY|O_LARGEFILE;
653 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400654 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
David Howellse0e81732009-09-02 09:13:40 +0100656 validate_process_creds();
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /*
659 * If we get here, then the client has already done an "open",
660 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
661 * in case a chmod has now revoked permission.
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400662 *
663 * Arguably we should also allow the owner override for
664 * directories, but we never have and it doesn't seem to have
665 * caused anyone a problem. If we were to change this, note
666 * also that our filldir callbacks would need a variant of
667 * lookup_one_len that doesn't check permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400669 if (type == S_IFREG)
670 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
671 err = fh_verify(rqstp, fhp, type, may_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (err)
673 goto out;
674
Al Viro765927b2012-06-26 21:58:53 +0400675 path.mnt = fhp->fh_export->ex_path.mnt;
676 path.dentry = fhp->fh_dentry;
677 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /* Disallow write access to files with the append-only bit set
680 * or any access when mandatory locking enabled
681 */
682 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400683 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400685 /*
686 * We must ignore files (but only files) which might have mandatory
687 * locks on them because there is no way to know if the accesser has
688 * the lock.
689 */
690 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 goto out;
692
693 if (!inode->i_fop)
694 goto out;
695
Bernd Schubert999448a2012-03-18 22:44:49 -0400696 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700697 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 goto out_nfserr;
699
Bernd Schubert999448a2012-03-18 22:44:49 -0400700 if (may_flags & NFSD_MAY_WRITE) {
701 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700702 flags = O_RDWR|O_LARGEFILE;
703 else
704 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Al Viro765927b2012-06-26 21:58:53 +0400706 *filp = dentry_open(&path, flags, current_cred());
Harshula Jayasuriyae4daf1f2013-07-23 14:21:35 +1000707 if (IS_ERR(*filp)) {
Al Viro6264d692006-10-19 23:28:58 -0700708 host_err = PTR_ERR(*filp);
Harshula Jayasuriyae4daf1f2013-07-23 14:21:35 +1000709 *filp = NULL;
710 } else {
Bernd Schubert999448a2012-03-18 22:44:49 -0400711 host_err = ima_file_check(*filp, may_flags);
712
Bernd Schubert06effdb2012-03-18 22:44:50 -0400713 if (may_flags & NFSD_MAY_64BIT_COOKIE)
714 (*filp)->f_mode |= FMODE_64BITHASH;
715 else
716 (*filp)->f_mode |= FMODE_32BITHASH;
717 }
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700720 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721out:
David Howellse0e81732009-09-02 09:13:40 +0100722 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return err;
724}
725
726/*
727 * Close a file.
728 */
729void
730nfsd_close(struct file *filp)
731{
732 fput(filp);
733}
734
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500735/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * Obtain the readahead parameters for the file
737 * specified by (dev, ino).
738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740static inline struct raparms *
741nfsd_get_raparms(dev_t dev, ino_t ino)
742{
743 struct raparms *ra, **rap, **frap = NULL;
744 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700745 unsigned int hash;
746 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Greg Banksfce14562006-10-04 02:15:49 -0700748 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
749 rab = &raparm_hash[hash];
750
751 spin_lock(&rab->pb_lock);
752 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (ra->p_ino == ino && ra->p_dev == dev)
754 goto found;
755 depth++;
756 if (ra->p_count == 0)
757 frap = rap;
758 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300759 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700761 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return NULL;
763 }
764 rap = frap;
765 ra = *frap;
766 ra->p_dev = dev;
767 ra->p_ino = ino;
768 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700769 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770found:
Greg Banksfce14562006-10-04 02:15:49 -0700771 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700773 ra->p_next = rab->pb_head;
774 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 ra->p_count++;
777 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700778 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return ra;
780}
781
782/*
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
Al Viro6264d692006-10-19 23:28:58 -0700822static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
824 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
825{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700827 __be32 err;
828 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 err = nfserr_perm;
Dave Hansena8754be2007-10-16 23:31:15 -0700831
Jens Axboecf8208d2007-06-12 21:22:14 +0200832 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
833 struct splice_desc sd = {
834 .len = 0,
835 .total_len = *count,
836 .pos = offset,
837 .u.data = rqstp,
838 };
839
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500840 rqstp->rq_next_page = rqstp->rq_respages + 1;
Jens Axboecf8208d2007-06-12 21:22:14 +0200841 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 } else {
843 oldfs = get_fs();
844 set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -0700845 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 set_fs(oldfs);
847 }
848
Al Viro6264d692006-10-19 23:28:58 -0700849 if (host_err >= 0) {
850 nfsdstats.io_read += host_err;
851 *count = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 err = 0;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500853 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 } else
Al Viro6264d692006-10-19 23:28:58 -0700855 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return err;
857}
858
Neil Brown9f708e42006-01-06 00:19:59 -0800859static void kill_suid(struct dentry *dentry)
860{
861 struct iattr ia;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700862 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Neil Brown9f708e42006-01-06 00:19:59 -0800863
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800864 mutex_lock(&dentry->d_inode->i_mutex);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400865 /*
866 * Note we call this on write, so notify_change will not
867 * encounter any conflicting delegations:
868 */
869 notify_change(dentry, &ia, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800870 mutex_unlock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800871}
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
Al Viro6264d692006-10-19 23:28:58 -0700910static __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;
916 struct dentry *dentry;
917 struct inode *inode;
918 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700919 __be32 err = 0;
920 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400922 int use_wgather;
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700923 loff_t pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -0800925 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 inode = dentry->d_inode;
927 exp = fhp->fh_export;
928
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400929 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (!EX_ISSYNC(exp))
932 stable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /* Write the data. */
935 oldfs = get_fs(); set_fs(KERNEL_DS);
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700936 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700938 if (host_err < 0)
939 goto out_nfserr;
940 *cnt = host_err;
941 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500942 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* clear setuid/setgid flag after write */
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700945 if (inode->i_mode & (S_ISUID | S_ISGID))
Neil Brown9f708e42006-01-06 00:19:59 -0800946 kill_suid(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400948 if (stable) {
949 if (use_wgather)
950 host_err = wait_for_concurrent_writes(file);
951 else
952 host_err = vfs_fsync_range(file, offset, offset+*cnt, 0);
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700955out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700956 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800957 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800959 else
Al Viro6264d692006-10-19 23:28:58 -0700960 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return err;
962}
963
964/*
965 * Read data from a file. count must contain the requested read count
966 * on entry. On return, *count contains the number of bytes actually read.
967 * N.B. After this call fhp needs an fh_put
968 */
J. Bruce Fields039a87c2010-07-30 11:33:32 -0400969__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -0400970 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
971{
972 struct file *file;
973 struct inode *inode;
974 struct raparms *ra;
975 __be32 err;
976
977 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
978 if (err)
979 return err;
980
Al Viro496ad9a2013-01-23 17:07:38 -0500981 inode = file_inode(file);
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -0400982
983 /* Get readahead parameters */
984 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
985
986 if (ra && ra->p_set)
987 file->f_ra = ra->p_ra;
988
989 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
990
991 /* Write back readahead params */
992 if (ra) {
993 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
994 spin_lock(&rab->pb_lock);
995 ra->p_ra = file->f_ra;
996 ra->p_set = 1;
997 ra->p_count--;
998 spin_unlock(&rab->pb_lock);
999 }
1000
1001 nfsd_close(file);
1002 return err;
1003}
1004
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001005/* As above, but use the provided file descriptor. */
Al Viro6264d692006-10-19 23:28:58 -07001006__be32
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001007nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 loff_t offset, struct kvec *vec, int vlen,
1009 unsigned long *count)
1010{
Al Viro6264d692006-10-19 23:28:58 -07001011 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001014 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001015 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (err)
1017 goto out;
1018 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001019 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1020 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021out:
1022 return err;
1023}
1024
1025/*
1026 * Write data to a file.
1027 * The stable flag requests synchronous writes.
1028 * N.B. After this call fhp needs an fh_put
1029 */
Al Viro6264d692006-10-19 23:28:58 -07001030__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001032 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 int *stablep)
1034{
Al Viro6264d692006-10-19 23:28:58 -07001035 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001038 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001039 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (err)
1041 goto out;
1042 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1043 stablep);
1044 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001045 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (err)
1047 goto out;
1048
1049 if (cnt)
1050 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1051 cnt, stablep);
1052 nfsd_close(file);
1053 }
1054out:
1055 return err;
1056}
1057
1058#ifdef CONFIG_NFSD_V3
1059/*
1060 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001061 *
1062 * Note: we only guarantee that data that lies within the range specified
1063 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 *
1065 * Unfortunately we cannot lock the file to make sure we return full WCC
1066 * data to the client, as locking happens lower down in the filesystem.
1067 */
Al Viro6264d692006-10-19 23:28:58 -07001068__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1070 loff_t offset, unsigned long count)
1071{
1072 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001073 loff_t end = LLONG_MAX;
1074 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Trond Myklebustaa696a62010-01-29 16:44:25 -05001076 if (offset < 0)
1077 goto out;
1078 if (count != 0) {
1079 end = offset + (loff_t)count - 1;
1080 if (end < offset)
1081 goto out;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Jeff Layton91885252010-03-19 08:06:28 -04001084 err = nfsd_open(rqstp, fhp, S_IFREG,
1085 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001086 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001089 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001090
1091 if (err2 != -EINVAL)
1092 err = nfserrno(err2);
1093 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
1097 nfsd_close(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001098out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return err;
1100}
1101#endif /* CONFIG_NFSD_V3 */
1102
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001103static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001104nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1105 struct iattr *iap)
1106{
1107 /*
1108 * Mode has already been set earlier in create:
1109 */
1110 iap->ia_valid &= ~ATTR_MODE;
1111 /*
1112 * Setting uid/gid works only for root. Irix appears to
1113 * send along the gid on create when it tries to implement
1114 * setgid directories via NFS:
1115 */
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001116 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001117 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1118 if (iap->ia_valid)
1119 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1120 return 0;
1121}
1122
wengang wang4ac35c22009-02-10 11:27:51 +08001123/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1124 * setting size to 0 may fail for some specific file systems by the permission
1125 * checking which requires WRITE permission but the mode is 000.
1126 * we ignore the resizing(to 0) on the just new created file, since the size is
1127 * 0 after file created.
1128 *
1129 * call this only after vfs_create() is called.
1130 * */
1131static void
1132nfsd_check_ignore_resizing(struct iattr *iap)
1133{
1134 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1135 iap->ia_valid &= ~ATTR_SIZE;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/*
1139 * Create a file (regular, directory, device, fifo); UNIX sockets
1140 * not yet implemented.
1141 * If the response fh has been verified, the parent directory should
1142 * already be locked. Note that the parent directory is left locked.
1143 *
1144 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1145 */
Al Viro6264d692006-10-19 23:28:58 -07001146__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1148 char *fname, int flen, struct iattr *iap,
1149 int type, dev_t rdev, struct svc_fh *resfhp)
1150{
1151 struct dentry *dentry, *dchild = NULL;
1152 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001153 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001154 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001155 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 err = nfserr_perm;
1158 if (!flen)
1159 goto out;
1160 err = nfserr_exist;
1161 if (isdotent(fname, flen))
1162 goto out;
1163
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001164 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (err)
1166 goto out;
1167
1168 dentry = fhp->fh_dentry;
1169 dirp = dentry->d_inode;
1170
1171 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001172 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 goto out;
1174 /*
1175 * Check whether the response file handle has been verified yet.
1176 * If it has, the parent directory should already be locked.
1177 */
1178 if (!resfhp->fh_dentry) {
Jan Kara4a55c102012-06-12 16:20:33 +02001179 host_err = fh_want_write(fhp);
1180 if (host_err)
1181 goto out_nfserr;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001184 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001186 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (IS_ERR(dchild))
1188 goto out_nfserr;
1189 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1190 if (err)
1191 goto out;
1192 } else {
1193 /* called from nfsd_proc_create */
1194 dchild = dget(resfhp->fh_dentry);
1195 if (!fhp->fh_locked) {
1196 /* not actually possible */
1197 printk(KERN_ERR
Al Viroa6a9f182013-09-16 10:57:01 -04001198 "nfsd_create: parent %pd2 not locked!\n",
1199 dentry);
Al Virod75f2b92006-01-18 17:43:44 -08001200 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 goto out;
1202 }
1203 }
1204 /*
1205 * Make sure the child dentry is still negative ...
1206 */
1207 err = nfserr_exist;
1208 if (dchild->d_inode) {
Al Viroa6a9f182013-09-16 10:57:01 -04001209 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1210 dentry, dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 goto out;
1212 }
1213
1214 if (!(iap->ia_valid & ATTR_MODE))
1215 iap->ia_mode = 0;
1216 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1217
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001218 err = nfserr_inval;
1219 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1220 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1221 type);
1222 goto out;
1223 }
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Get the dir op function pointer.
1227 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001228 err = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001229 host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 switch (type) {
1231 case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04001232 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001233 if (!host_err)
1234 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001237 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 break;
1239 case S_IFCHR:
1240 case S_IFBLK:
1241 case S_IFIFO:
1242 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001243 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001245 }
Jan Kara4a55c102012-06-12 16:20:33 +02001246 if (host_err < 0)
Dave Hansen463c3192008-02-15 14:37:57 -08001247 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Ben Myersf5019122010-02-17 14:05:11 -06001249 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Ben Myersf5019122010-02-17 14:05:11 -06001251 /*
1252 * nfsd_setattr already committed the child. Transactional filesystems
1253 * had a chance to commit changes for both parent and child
1254 * simultaneously making the following commit_metadata a noop.
1255 */
1256 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001257 if (err2)
1258 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Update the file handle to get the new inode info.
1261 */
1262 if (!err)
1263 err = fh_update(resfhp);
1264out:
1265 if (dchild && !IS_ERR(dchild))
1266 dput(dchild);
1267 return err;
1268
1269out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001270 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 goto out;
1272}
1273
1274#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001275
1276static inline int nfsd_create_is_exclusive(int createmode)
1277{
1278 return createmode == NFS3_CREATE_EXCLUSIVE
1279 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001283 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
Al Viro6264d692006-10-19 23:28:58 -07001285__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001286do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 char *fname, int flen, struct iattr *iap,
1288 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001289 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct dentry *dentry, *dchild = NULL;
1292 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001293 __be32 err;
1294 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 err = nfserr_perm;
1298 if (!flen)
1299 goto out;
1300 err = nfserr_exist;
1301 if (isdotent(fname, flen))
1302 goto out;
1303 if (!(iap->ia_valid & ATTR_MODE))
1304 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001305 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (err)
1307 goto out;
1308
1309 dentry = fhp->fh_dentry;
1310 dirp = dentry->d_inode;
1311
1312 /* Get all the sanity checks out of the way before
1313 * we lock the parent. */
1314 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001315 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001317
1318 host_err = fh_want_write(fhp);
1319 if (host_err)
1320 goto out_nfserr;
1321
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001322 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /*
1325 * Compose the response file handle.
1326 */
1327 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001328 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (IS_ERR(dchild))
1330 goto out_nfserr;
1331
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001332 /* If file doesn't exist, check for permissions to create one */
1333 if (!dchild->d_inode) {
1334 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1335 if (err)
1336 goto out;
1337 }
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1340 if (err)
1341 goto out;
1342
Mi Jinlongac6721a2011-04-20 17:06:25 +08001343 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001344 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001345 * the high bit set, so just clear the high bits. If this is
1346 * ever changed to use different attrs for storing the
1347 * verifier, then do_open_lookup() will also need to be fixed
1348 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
1350 v_mtime = verifier[0]&0x7fffffff;
1351 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353
1354 if (dchild->d_inode) {
1355 err = 0;
1356
1357 switch (createmode) {
1358 case NFS3_CREATE_UNCHECKED:
1359 if (! S_ISREG(dchild->d_inode->i_mode))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 else if (truncp) {
1362 /* in nfsv4, we need to treat this case a little
1363 * differently. we don't want to truncate the
1364 * file now; this would be wrong if the OPEN
1365 * fails for some other reason. furthermore,
1366 * if the size is nonzero, we should ignore it
1367 * according to spec!
1368 */
1369 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1370 }
1371 else {
1372 iap->ia_valid &= ATTR_SIZE;
1373 goto set_attr;
1374 }
1375 break;
1376 case NFS3_CREATE_EXCLUSIVE:
1377 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1378 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown7007c902012-12-07 15:40:55 -05001379 && dchild->d_inode->i_size == 0 ) {
1380 if (created)
1381 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
Neil Brown7007c902012-12-07 15:40:55 -05001383 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001384 case NFS4_CREATE_EXCLUSIVE4_1:
1385 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1386 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown7007c902012-12-07 15:40:55 -05001387 && dchild->d_inode->i_size == 0 ) {
1388 if (created)
1389 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001390 goto set_attr;
Neil Brown7007c902012-12-07 15:40:55 -05001391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* fallthru */
1393 case NFS3_CREATE_GUARDED:
1394 err = nfserr_exist;
1395 }
Al Virobad0dcf2011-11-23 12:03:18 -05001396 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 goto out;
1398 }
1399
Al Viro312b63f2012-06-10 18:09:36 -04001400 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001401 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001402 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001404 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001405 if (created)
1406 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
wengang wang4ac35c22009-02-10 11:27:51 +08001408 nfsd_check_ignore_resizing(iap);
1409
Mi Jinlongac6721a2011-04-20 17:06:25 +08001410 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001411 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001413 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* XXX someone who knows this better please fix it for nsec */
1415 iap->ia_mtime.tv_sec = v_mtime;
1416 iap->ia_atime.tv_sec = v_atime;
1417 iap->ia_mtime.tv_nsec = 0;
1418 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001422 err = nfsd_create_setattr(rqstp, resfhp, iap);
1423
1424 /*
1425 * nfsd_setattr already committed the child (and possibly also the parent).
1426 */
1427 if (!err)
1428 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001429
1430 /*
1431 * Update the filehandle to get the new inode info.
1432 */
1433 if (!err)
1434 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 out:
1437 fh_unlock(fhp);
1438 if (dchild && !IS_ERR(dchild))
1439 dput(dchild);
Jan Kara4a55c102012-06-12 16:20:33 +02001440 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return err;
1442
1443 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001444 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 goto out;
1446}
1447#endif /* CONFIG_NFSD_V3 */
1448
1449/*
1450 * Read a symlink. On entry, *lenp must contain the maximum path length that
1451 * fits into the buffer. On return, it contains the true length.
1452 * N.B. After this call fhp needs an fh_put
1453 */
Al Viro6264d692006-10-19 23:28:58 -07001454__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct inode *inode;
1458 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001459 __be32 err;
1460 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001461 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001463 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (err)
1465 goto out;
1466
Al Viro68ac1232012-03-15 08:21:57 -04001467 path.mnt = fhp->fh_export->ex_path.mnt;
1468 path.dentry = fhp->fh_dentry;
1469 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001472 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 goto out;
1474
Al Viro68ac1232012-03-15 08:21:57 -04001475 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* N.B. Why does this call need a get_fs()??
1477 * Remove the set_fs and watch the fireworks:-) --okir
1478 */
1479
1480 oldfs = get_fs(); set_fs(KERNEL_DS);
J. Bruce Fieldsfac7a172012-07-27 14:16:55 -04001481 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 set_fs(oldfs);
1483
Al Viro6264d692006-10-19 23:28:58 -07001484 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001486 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 err = 0;
1488out:
1489 return err;
1490
1491out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001492 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 goto out;
1494}
1495
1496/*
1497 * Create a symlink and look up its inode
1498 * N.B. After this call _both_ fhp and resfhp need an fh_put
1499 */
Al Viro6264d692006-10-19 23:28:58 -07001500__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1502 char *fname, int flen,
1503 char *path, int plen,
1504 struct svc_fh *resfhp,
1505 struct iattr *iap)
1506{
1507 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001508 __be32 err, cerr;
1509 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 err = nfserr_noent;
1512 if (!flen || !plen)
1513 goto out;
1514 err = nfserr_exist;
1515 if (isdotent(fname, flen))
1516 goto out;
1517
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001518 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (err)
1520 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001521
1522 host_err = fh_want_write(fhp);
1523 if (host_err)
1524 goto out_nfserr;
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 fh_lock(fhp);
1527 dentry = fhp->fh_dentry;
1528 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001529 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (IS_ERR(dnew))
1531 goto out_nfserr;
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (unlikely(path[plen] != 0)) {
1534 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1535 if (path_alloced == NULL)
Al Viro6264d692006-10-19 23:28:58 -07001536 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 else {
1538 strncpy(path_alloced, path, plen);
1539 path_alloced[plen] = 0;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001540 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 kfree(path_alloced);
1542 }
1543 } else
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001544 host_err = vfs_symlink(dentry->d_inode, dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001545 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001546 if (!err)
1547 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 fh_unlock(fhp);
1549
Al Virobad0dcf2011-11-23 12:03:18 -05001550 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1553 dput(dnew);
1554 if (err==0) err = cerr;
1555out:
1556 return err;
1557
1558out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001559 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto out;
1561}
1562
1563/*
1564 * Create a hardlink
1565 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1566 */
Al Viro6264d692006-10-19 23:28:58 -07001567__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1569 char *name, int len, struct svc_fh *tfhp)
1570{
1571 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001572 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001573 __be32 err;
1574 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001576 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (err)
1578 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001579 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (err)
1581 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001582 err = nfserr_isdir;
1583 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1584 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 err = nfserr_perm;
1586 if (!len)
1587 goto out;
1588 err = nfserr_exist;
1589 if (isdotent(name, len))
1590 goto out;
1591
Jan Kara4a55c102012-06-12 16:20:33 +02001592 host_err = fh_want_write(tfhp);
1593 if (host_err) {
1594 err = nfserrno(host_err);
1595 goto out;
1596 }
1597
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001598 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ddir = ffhp->fh_dentry;
1600 dirp = ddir->d_inode;
1601
1602 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001603 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (IS_ERR(dnew))
1605 goto out_nfserr;
1606
1607 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001609 err = nfserr_noent;
1610 if (!dold->d_inode)
Jan Kara4a55c102012-06-12 16:20:33 +02001611 goto out_dput;
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001612 host_err = nfsd_break_lease(dold->d_inode);
Casey Bodley7d751f62011-06-03 12:21:23 -04001613 if (host_err) {
1614 err = nfserrno(host_err);
Jan Kara4a55c102012-06-12 16:20:33 +02001615 goto out_dput;
Casey Bodley7d751f62011-06-03 12:21:23 -04001616 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04001617 host_err = vfs_link(dold, dirp, dnew, NULL);
Al Viro6264d692006-10-19 23:28:58 -07001618 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001619 err = nfserrno(commit_metadata(ffhp));
1620 if (!err)
1621 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 } else {
Al Viro6264d692006-10-19 23:28:58 -07001623 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 err = nfserr_acces;
1625 else
Al Viro6264d692006-10-19 23:28:58 -07001626 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001628out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001630out_unlock:
1631 fh_unlock(ffhp);
Jan Kara4a55c102012-06-12 16:20:33 +02001632 fh_drop_write(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633out:
1634 return err;
1635
1636out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001637 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001638 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641/*
1642 * Rename a file
1643 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1644 */
Al Viro6264d692006-10-19 23:28:58 -07001645__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1647 struct svc_fh *tfhp, char *tname, int tlen)
1648{
1649 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1650 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001651 __be32 err;
1652 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001654 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (err)
1656 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001657 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (err)
1659 goto out;
1660
1661 fdentry = ffhp->fh_dentry;
1662 fdir = fdentry->d_inode;
1663
1664 tdentry = tfhp->fh_dentry;
1665 tdir = tdentry->d_inode;
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 err = nfserr_perm;
1668 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1669 goto out;
1670
Jan Kara4a55c102012-06-12 16:20:33 +02001671 host_err = fh_want_write(ffhp);
1672 if (host_err) {
1673 err = nfserrno(host_err);
1674 goto out;
1675 }
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* cannot use fh_lock as we need deadlock protective ordering
1678 * so do it by hand */
1679 trap = lock_rename(tdentry, fdentry);
1680 ffhp->fh_locked = tfhp->fh_locked = 1;
1681 fill_pre_wcc(ffhp);
1682 fill_pre_wcc(tfhp);
1683
1684 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001685 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (IS_ERR(odentry))
1687 goto out_nfserr;
1688
Al Viro6264d692006-10-19 23:28:58 -07001689 host_err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (!odentry->d_inode)
1691 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001692 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (odentry == trap)
1694 goto out_dput_old;
1695
1696 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001697 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (IS_ERR(ndentry))
1699 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001700 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (ndentry == trap)
1702 goto out_dput_new;
1703
Dave Hansen9079b1e2008-02-15 14:37:49 -08001704 host_err = -EXDEV;
1705 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1706 goto out_dput_new;
J. Bruce Fieldsaa387d6c2013-04-15 16:03:46 -04001707 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1708 goto out_dput_new;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001709
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001710 host_err = nfsd_break_lease(odentry->d_inode);
1711 if (host_err)
Jan Kara4a55c102012-06-12 16:20:33 +02001712 goto out_dput_new;
J. Bruce Fields83f6b0c2011-02-06 16:46:30 -05001713 if (ndentry->d_inode) {
1714 host_err = nfsd_break_lease(ndentry->d_inode);
1715 if (host_err)
Jan Kara4a55c102012-06-12 16:20:33 +02001716 goto out_dput_new;
J. Bruce Fields83f6b0c2011-02-06 16:46:30 -05001717 }
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04001718 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL);
Ben Myersf5019122010-02-17 14:05:11 -06001719 if (!host_err) {
1720 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001721 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001722 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 out_dput_new:
1725 dput(ndentry);
1726 out_dput_old:
1727 dput(odentry);
1728 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001729 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /* we cannot reply on fh_unlock on the two filehandles,
1732 * as that would do the wrong thing if the two directories
1733 * were the same, so again we do it by hand
1734 */
1735 fill_post_wcc(ffhp);
1736 fill_post_wcc(tfhp);
1737 unlock_rename(tdentry, fdentry);
1738 ffhp->fh_locked = tfhp->fh_locked = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001739 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741out:
1742 return err;
1743}
1744
1745/*
1746 * Unlink a file or directory
1747 * N.B. After this call fhp needs an fh_put
1748 */
Al Viro6264d692006-10-19 23:28:58 -07001749__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1751 char *fname, int flen)
1752{
1753 struct dentry *dentry, *rdentry;
1754 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001755 __be32 err;
1756 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 err = nfserr_acces;
1759 if (!flen || isdotent(fname, flen))
1760 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001761 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (err)
1763 goto out;
1764
Jan Kara4a55c102012-06-12 16:20:33 +02001765 host_err = fh_want_write(fhp);
1766 if (host_err)
1767 goto out_nfserr;
1768
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001769 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 dentry = fhp->fh_dentry;
1771 dirp = dentry->d_inode;
1772
1773 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001774 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (IS_ERR(rdentry))
1776 goto out_nfserr;
1777
1778 if (!rdentry->d_inode) {
1779 dput(rdentry);
1780 err = nfserr_noent;
1781 goto out;
1782 }
1783
1784 if (!type)
1785 type = rdentry->d_inode->i_mode & S_IFMT;
1786
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001787 host_err = nfsd_break_lease(rdentry->d_inode);
1788 if (host_err)
Jan Kara4a55c102012-06-12 16:20:33 +02001789 goto out_put;
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001790 if (type != S_IFDIR)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04001791 host_err = vfs_unlink(dirp, rdentry, NULL);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001792 else
Al Viro6264d692006-10-19 23:28:58 -07001793 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001794 if (!host_err)
1795 host_err = commit_metadata(fhp);
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001796out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 dput(rdentry);
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001800 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001801out:
1802 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001806 * We do this buffering because we must not call back into the file
1807 * system's ->lookup() method from the filldir callback. That may well
1808 * deadlock a number of file systems.
1809 *
1810 * This is based heavily on the implementation of same in XFS.
1811 */
1812struct buffered_dirent {
1813 u64 ino;
1814 loff_t offset;
1815 int namlen;
1816 unsigned int d_type;
1817 char name[];
1818};
1819
1820struct readdir_data {
Al Viro5c0ba4e2013-05-15 13:52:59 -04001821 struct dir_context ctx;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001822 char *dirent;
1823 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001824 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001825};
1826
1827static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1828 loff_t offset, u64 ino, unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001829{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001830 struct readdir_data *buf = __buf;
1831 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1832 unsigned int reclen;
1833
1834 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001835 if (buf->used + reclen > PAGE_SIZE) {
1836 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001837 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001838 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001839
1840 de->namlen = namlen;
1841 de->offset = offset;
1842 de->ino = ino;
1843 de->d_type = d_type;
1844 memcpy(de->name, name, namlen);
1845 buf->used += reclen;
1846
1847 return 0;
1848}
1849
David Woodhouse2f9092e2009-04-20 23:18:37 +01001850static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1851 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001852{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001853 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001854 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001855 int size;
1856 loff_t offset;
Al Viroac6614b2013-05-22 22:22:04 -04001857 struct readdir_data buf = {
1858 .ctx.actor = nfsd_buffered_filldir,
1859 .dirent = (void *)__get_free_page(GFP_KERNEL)
1860 };
David Woodhouse2628b762008-07-31 17:16:51 +01001861
David Woodhouse14f7dd62008-07-31 20:29:12 +01001862 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001863 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001864
David Woodhouse14f7dd62008-07-31 20:29:12 +01001865 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001866
1867 while (1) {
Al Viro496ad9a2013-01-23 17:07:38 -05001868 struct inode *dir_inode = file_inode(file);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001869 unsigned int reclen;
1870
Doug Nazarb726e922008-11-05 06:16:28 -05001871 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001872 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001873 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001874
Al Viro5c0ba4e2013-05-15 13:52:59 -04001875 host_err = iterate_dir(file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -04001876 if (buf.full)
1877 host_err = 0;
1878
1879 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001880 break;
1881
1882 size = buf.used;
1883
1884 if (!size)
1885 break;
1886
David Woodhouse2f9092e2009-04-20 23:18:37 +01001887 /*
1888 * Various filldir functions may end up calling back into
1889 * lookup_one_len() and the file system's ->lookup() method.
1890 * These expect i_mutex to be held, as it would within readdir.
1891 */
1892 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1893 if (host_err)
1894 break;
1895
David Woodhouse14f7dd62008-07-31 20:29:12 +01001896 de = (struct buffered_dirent *)buf.dirent;
1897 while (size > 0) {
1898 offset = de->offset;
1899
1900 if (func(cdp, de->name, de->namlen, de->offset,
1901 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01001902 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001903
1904 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001905 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001906
1907 reclen = ALIGN(sizeof(*de) + de->namlen,
1908 sizeof(u64));
1909 size -= reclen;
1910 de = (struct buffered_dirent *)((char *)de + reclen);
1911 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01001912 mutex_unlock(&dir_inode->i_mutex);
1913 if (size > 0) /* We bailed out early */
1914 break;
1915
David Woodhousec002a6c2008-08-17 17:21:18 +01001916 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001917 }
1918
David Woodhouse14f7dd62008-07-31 20:29:12 +01001919 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01001920
1921 if (host_err)
1922 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001923
1924 *offsetp = offset;
1925 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01001926}
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928/*
1929 * Read entries from a directory.
1930 * The NFSv3/4 verifier we ignore for now.
1931 */
Al Viro6264d692006-10-19 23:28:58 -07001932__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
NeilBrowna0ad13e2007-01-26 00:57:10 -08001934 struct readdir_cd *cdp, filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Al Viro6264d692006-10-19 23:28:58 -07001936 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 struct file *file;
1938 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04001939 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Bernd Schubert06effdb2012-03-18 22:44:50 -04001941 /* NFSv2 only supports 32 bit cookies */
1942 if (rqstp->rq_vers > 2)
1943 may_flags |= NFSD_MAY_64BIT_COOKIE;
1944
1945 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (err)
1947 goto out;
1948
Jeff Laytonb108fe62012-04-25 15:30:00 -04001949 offset = vfs_llseek(file, offset, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (offset < 0) {
1951 err = nfserrno((int)offset);
1952 goto out_close;
1953 }
1954
David Woodhouse14f7dd62008-07-31 20:29:12 +01001955 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 if (err == nfserr_eof || err == nfserr_toosmall)
1958 err = nfs_ok; /* can still be found in ->err */
1959out_close:
1960 nfsd_close(file);
1961out:
1962 return err;
1963}
1964
1965/*
1966 * Get file system stats
1967 * N.B. After this call fhp needs an fh_put
1968 */
Al Viro6264d692006-10-19 23:28:58 -07001969__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04001970nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02001972 __be32 err;
1973
1974 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02001975 if (!err) {
1976 struct path path = {
1977 .mnt = fhp->fh_export->ex_path.mnt,
1978 .dentry = fhp->fh_dentry,
1979 };
1980 if (vfs_statfs(&path, stat))
1981 err = nfserr_io;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return err;
1984}
1985
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001986static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001987{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001988 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001989}
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991/*
1992 * Check for a user's access permissions to this inode.
1993 */
Al Viro6264d692006-10-19 23:28:58 -07001994__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001995nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
1996 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct inode *inode = dentry->d_inode;
1999 int err;
2000
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04002001 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return 0;
2003#if 0
2004 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2005 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002006 (acc & NFSD_MAY_READ)? " read" : "",
2007 (acc & NFSD_MAY_WRITE)? " write" : "",
2008 (acc & NFSD_MAY_EXEC)? " exec" : "",
2009 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2010 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2011 (acc & NFSD_MAY_LOCK)? " lock" : "",
2012 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 inode->i_mode,
2014 IS_IMMUTABLE(inode)? " immut" : "",
2015 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002016 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002018 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019#endif
2020
2021 /* Normally we reject any write/sattr etc access on a read-only file
2022 * system. But if it is IRIX doing check on write-access for a
2023 * device special file, we ignore rofs.
2024 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002025 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2026 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002027 if (exp_rdonly(rqstp, exp) ||
2028 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002030 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(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_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return nfserr_perm;
2035
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002036 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 /* If we cannot rely on authentication in NLM requests,
2038 * just allow locks, otherwise require read permission, or
2039 * ownership
2040 */
2041 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2042 return 0;
2043 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002044 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
2046 /*
2047 * The file owner always gets access permission for accesses that
2048 * would normally be checked at open time. This is to make
2049 * file access work even when the client has done a fchmod(fd, 0).
2050 *
2051 * However, `cp foo bar' should fail nevertheless when bar is
2052 * readonly. A sensible way to do this might be to reject all
2053 * attempts to truncate a read-only file, because a creat() call
2054 * always implies file truncation.
2055 * ... but this isn't really fair. A process may reasonably call
2056 * ftruncate on an open file descriptor on a file with perm 000.
2057 * We must trust the client to do permission checking - using "ACCESS"
2058 * with NFSv3.
2059 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002060 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002061 uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return 0;
2063
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002064 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002065 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /* Allow read access to binaries even when mode 111 */
2068 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002069 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2070 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002071 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 return err? nfserrno(err) : 0;
2074}
2075
2076void
2077nfsd_racache_shutdown(void)
2078{
Jeff Layton54a66e52008-08-13 22:03:27 -04002079 struct raparms *raparm, *last_raparm;
2080 unsigned int i;
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002083
2084 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2085 raparm = raparm_hash[i].pb_head;
2086 while(raparm) {
2087 last_raparm = raparm;
2088 raparm = raparm->p_next;
2089 kfree(last_raparm);
2090 }
2091 raparm_hash[i].pb_head = NULL;
2092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094/*
2095 * Initialize readahead param cache
2096 */
2097int
2098nfsd_racache_init(int cache_size)
2099{
2100 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002101 int j = 0;
2102 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002103 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Greg Banksfce14562006-10-04 02:15:49 -07002105
Jeff Layton54a66e52008-08-13 22:03:27 -04002106 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002108 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2109 if (nperbucket < 2)
2110 nperbucket = 2;
2111 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002112
2113 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002114
2115 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002116 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002117
2118 raparm = &raparm_hash[i].pb_head;
2119 for (j = 0; j < nperbucket; j++) {
2120 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2121 if (!*raparm)
2122 goto out_nomem;
2123 raparm = &(*raparm)->p_next;
2124 }
2125 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002126 }
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 nfsdstats.ra_size = cache_size;
2129 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002130
2131out_nomem:
2132 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2133 nfsd_racache_shutdown();
2134 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}