blob: 7eea63cada1d4a3ea5f9dd95401f83714689fcb0 [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_V2_ACL) || \
472 defined(CONFIG_NFSD_V3_ACL) || \
473 defined(CONFIG_NFSD_V4)
474static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
475{
476 ssize_t buflen;
Krishna Kumar6c6a4262008-10-22 14:48:36 +0530477 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800479 buflen = vfs_getxattr(dentry, key, NULL, 0);
480 if (buflen <= 0)
481 return buflen;
482
483 *buf = kmalloc(buflen, GFP_KERNEL);
484 if (!*buf)
485 return -ENOMEM;
486
Krishna Kumar6c6a4262008-10-22 14:48:36 +0530487 ret = vfs_getxattr(dentry, key, *buf, buflen);
488 if (ret < 0)
489 kfree(*buf);
490 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800491}
492#endif
493
494#if defined(CONFIG_NFSD_V4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static int
496set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
497{
498 int len;
499 size_t buflen;
500 char *buf = NULL;
501 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 buflen = posix_acl_xattr_size(pacl->a_count);
504 buf = kmalloc(buflen, GFP_KERNEL);
505 error = -ENOMEM;
506 if (buf == NULL)
507 goto out;
508
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700509 len = posix_acl_to_xattr(&init_user_ns, pacl, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (len < 0) {
511 error = len;
512 goto out;
513 }
514
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800515 error = vfs_setxattr(dentry, key, buf, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516out:
517 kfree(buf);
518 return error;
519}
520
Al Viro6264d692006-10-19 23:28:58 -0700521__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
523 struct nfs4_acl *acl)
524{
Al Viro6264d692006-10-19 23:28:58 -0700525 __be32 error;
526 int host_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct dentry *dentry;
528 struct inode *inode;
529 struct posix_acl *pacl = NULL, *dpacl = NULL;
530 unsigned int flags = 0;
531
532 /* Get inode */
J. Bruce Fieldse281d812011-08-15 16:57:07 -0400533 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (error)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700535 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 dentry = fhp->fh_dentry;
538 inode = dentry->d_inode;
539 if (S_ISDIR(inode->i_mode))
540 flags = NFS4_ACL_DIR;
541
Al Viro6264d692006-10-19 23:28:58 -0700542 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
543 if (host_error == -EINVAL) {
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700544 return nfserr_attrnotsupp;
Al Viro6264d692006-10-19 23:28:58 -0700545 } else if (host_error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto out_nfserr;
547
Al Viro6264d692006-10-19 23:28:58 -0700548 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
549 if (host_error < 0)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700550 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700552 if (S_ISDIR(inode->i_mode))
Al Viro6264d692006-10-19 23:28:58 -0700553 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700555out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 posix_acl_release(pacl);
557 posix_acl_release(dpacl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558out_nfserr:
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800559 if (host_error == -EOPNOTSUPP)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700560 return nfserr_attrnotsupp;
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800561 else
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700562 return nfserrno(host_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565static struct posix_acl *
566_get_posix_acl(struct dentry *dentry, char *key)
567{
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800568 void *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct posix_acl *pacl = NULL;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800570 int buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800572 buflen = nfsd_getxattr(dentry, key, &buf);
573 if (!buflen)
574 buflen = -ENODATA;
575 if (buflen <= 0)
576 return ERR_PTR(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700578 pacl = posix_acl_from_xattr(&init_user_ns, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 kfree(buf);
580 return pacl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583int
584nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
585{
586 struct inode *inode = dentry->d_inode;
587 int error = 0;
588 struct posix_acl *pacl = NULL, *dpacl = NULL;
589 unsigned int flags = 0;
590
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700591 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
593 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
594 if (IS_ERR(pacl)) {
595 error = PTR_ERR(pacl);
596 pacl = NULL;
597 goto out;
598 }
599
600 if (S_ISDIR(inode->i_mode)) {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700601 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
603 dpacl = NULL;
604 else if (IS_ERR(dpacl)) {
605 error = PTR_ERR(dpacl);
606 dpacl = NULL;
607 goto out;
608 }
609 flags = NFS4_ACL_DIR;
610 }
611
612 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
613 if (IS_ERR(*acl)) {
614 error = PTR_ERR(*acl);
615 *acl = NULL;
616 }
617 out:
618 posix_acl_release(pacl);
619 posix_acl_release(dpacl);
620 return error;
621}
622
Chuck Lever9b4146e2012-01-04 16:26:43 -0500623/*
624 * NFS junction information is stored in an extended attribute.
625 */
626#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
627
628/**
629 * nfsd4_is_junction - Test if an object could be an NFS junction
630 *
631 * @dentry: object to test
632 *
633 * Returns 1 if "dentry" appears to contain NFS junction information.
634 * Otherwise 0 is returned.
635 */
Trond Myklebust11fcee022011-09-12 19:37:26 -0400636int nfsd4_is_junction(struct dentry *dentry)
637{
638 struct inode *inode = dentry->d_inode;
639
640 if (inode == NULL)
641 return 0;
642 if (inode->i_mode & S_IXUGO)
643 return 0;
644 if (!(inode->i_mode & S_ISVTX))
645 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500646 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee022011-09-12 19:37:26 -0400647 return 0;
648 return 1;
649}
David Quigley18032ca2013-05-02 13:19:10 -0400650#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
651__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
652 struct xdr_netobj *label)
653{
654 __be32 error;
655 int host_error;
656 struct dentry *dentry;
657
658 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
659 if (error)
660 return error;
661
662 dentry = fhp->fh_dentry;
663
664 mutex_lock(&dentry->d_inode->i_mutex);
665 host_error = security_inode_setsecctx(dentry, label->data, label->len);
666 mutex_unlock(&dentry->d_inode->i_mutex);
667 return nfserrno(host_error);
668}
669#else
670__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
671 struct xdr_netobj *label)
672{
673 return nfserr_notsupp;
674}
675#endif
676
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400677#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679#ifdef CONFIG_NFSD_V3
680/*
681 * Check server access rights to a file system object
682 */
683struct accessmap {
684 u32 access;
685 int how;
686};
687static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200688 { NFS3_ACCESS_READ, NFSD_MAY_READ },
689 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
690 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
691 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 { 0, 0 }
694};
695
696static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200697 { NFS3_ACCESS_READ, NFSD_MAY_READ },
698 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
699 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
700 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
701 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 { 0, 0 }
704};
705
706static struct accessmap nfs3_anyaccess[] = {
707 /* Some clients - Solaris 2.6 at least, make an access call
708 * to the server to check for access for things like /dev/null
709 * (which really, the server doesn't care about). So
710 * We provide simple access checking for them, looking
711 * mainly at mode bits, and we make sure to ignore read-only
712 * filesystem checks
713 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200714 { NFS3_ACCESS_READ, NFSD_MAY_READ },
715 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
716 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
717 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 { 0, 0 }
720};
721
Al Viro6264d692006-10-19 23:28:58 -0700722__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
724{
725 struct accessmap *map;
726 struct svc_export *export;
727 struct dentry *dentry;
728 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700729 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200731 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (error)
733 goto out;
734
735 export = fhp->fh_export;
736 dentry = fhp->fh_dentry;
737
738 if (S_ISREG(dentry->d_inode->i_mode))
739 map = nfs3_regaccess;
740 else if (S_ISDIR(dentry->d_inode->i_mode))
741 map = nfs3_diraccess;
742 else
743 map = nfs3_anyaccess;
744
745
746 query = *access;
747 for (; map->access; map++) {
748 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700749 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 sresult |= map->access;
752
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700753 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 switch (err2) {
755 case nfs_ok:
756 result |= map->access;
757 break;
758
759 /* the following error codes just mean the access was not allowed,
760 * rather than an error occurred */
761 case nfserr_rofs:
762 case nfserr_acces:
763 case nfserr_perm:
764 /* simply don't "or" in the access bit. */
765 break;
766 default:
767 error = err2;
768 goto out;
769 }
770 }
771 }
772 *access = result;
773 if (supported)
774 *supported = sresult;
775
776 out:
777 return error;
778}
779#endif /* CONFIG_NFSD_V3 */
780
J. Bruce Fields105f4622011-06-07 11:50:23 -0400781static int nfsd_open_break_lease(struct inode *inode, int access)
782{
783 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
J. Bruce Fields105f4622011-06-07 11:50:23 -0400785 if (access & NFSD_MAY_NOT_BREAK_LEASE)
786 return 0;
787 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
788 return break_lease(inode, mode | O_NONBLOCK);
789}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791/*
792 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400793 * The may_flags argument indicates the type of open (read/write/lock)
794 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 * N.B. After this call fhp needs an fh_put
796 */
Al Viro6264d692006-10-19 23:28:58 -0700797__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400798nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400799 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Al Viro765927b2012-06-26 21:58:53 +0400801 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct inode *inode;
Al Viro6264d692006-10-19 23:28:58 -0700803 int flags = O_RDONLY|O_LARGEFILE;
804 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400805 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
David Howellse0e81732009-09-02 09:13:40 +0100807 validate_process_creds();
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /*
810 * If we get here, then the client has already done an "open",
811 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
812 * in case a chmod has now revoked permission.
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400813 *
814 * Arguably we should also allow the owner override for
815 * directories, but we never have and it doesn't seem to have
816 * caused anyone a problem. If we were to change this, note
817 * also that our filldir callbacks would need a variant of
818 * lookup_one_len that doesn't check permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400820 if (type == S_IFREG)
821 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
822 err = fh_verify(rqstp, fhp, type, may_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (err)
824 goto out;
825
Al Viro765927b2012-06-26 21:58:53 +0400826 path.mnt = fhp->fh_export->ex_path.mnt;
827 path.dentry = fhp->fh_dentry;
828 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* Disallow write access to files with the append-only bit set
831 * or any access when mandatory locking enabled
832 */
833 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400834 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400836 /*
837 * We must ignore files (but only files) which might have mandatory
838 * locks on them because there is no way to know if the accesser has
839 * the lock.
840 */
841 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto out;
843
844 if (!inode->i_fop)
845 goto out;
846
Bernd Schubert999448a2012-03-18 22:44:49 -0400847 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700848 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 goto out_nfserr;
850
Bernd Schubert999448a2012-03-18 22:44:49 -0400851 if (may_flags & NFSD_MAY_WRITE) {
852 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700853 flags = O_RDWR|O_LARGEFILE;
854 else
855 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Al Viro765927b2012-06-26 21:58:53 +0400857 *filp = dentry_open(&path, flags, current_cred());
Harshula Jayasuriyae4daf1f2013-07-23 14:21:35 +1000858 if (IS_ERR(*filp)) {
Al Viro6264d692006-10-19 23:28:58 -0700859 host_err = PTR_ERR(*filp);
Harshula Jayasuriyae4daf1f2013-07-23 14:21:35 +1000860 *filp = NULL;
861 } else {
Bernd Schubert999448a2012-03-18 22:44:49 -0400862 host_err = ima_file_check(*filp, may_flags);
863
Bernd Schubert06effdb2012-03-18 22:44:50 -0400864 if (may_flags & NFSD_MAY_64BIT_COOKIE)
865 (*filp)->f_mode |= FMODE_64BITHASH;
866 else
867 (*filp)->f_mode |= FMODE_32BITHASH;
868 }
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700871 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872out:
David Howellse0e81732009-09-02 09:13:40 +0100873 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return err;
875}
876
877/*
878 * Close a file.
879 */
880void
881nfsd_close(struct file *filp)
882{
883 fput(filp);
884}
885
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500886/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 * Obtain the readahead parameters for the file
888 * specified by (dev, ino).
889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891static inline struct raparms *
892nfsd_get_raparms(dev_t dev, ino_t ino)
893{
894 struct raparms *ra, **rap, **frap = NULL;
895 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700896 unsigned int hash;
897 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Greg Banksfce14562006-10-04 02:15:49 -0700899 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
900 rab = &raparm_hash[hash];
901
902 spin_lock(&rab->pb_lock);
903 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (ra->p_ino == ino && ra->p_dev == dev)
905 goto found;
906 depth++;
907 if (ra->p_count == 0)
908 frap = rap;
909 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300910 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700912 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return NULL;
914 }
915 rap = frap;
916 ra = *frap;
917 ra->p_dev = dev;
918 ra->p_ino = ino;
919 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700920 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921found:
Greg Banksfce14562006-10-04 02:15:49 -0700922 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700924 ra->p_next = rab->pb_head;
925 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 ra->p_count++;
928 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700929 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return ra;
931}
932
933/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200934 * Grab and keep cached pages associated with a file in the svc_rqst
935 * so that they can be passed to the network sendmsg/sendpage routines
936 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
938static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200939nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
940 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jens Axboecf8208d2007-06-12 21:22:14 +0200942 struct svc_rqst *rqstp = sd->u.data;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500943 struct page **pp = rqstp->rq_next_page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200944 struct page *page = buf->page;
945 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200946
947 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (rqstp->rq_res.page_len == 0) {
950 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500951 put_page(*rqstp->rq_next_page);
952 *(rqstp->rq_next_page++) = page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200953 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700955 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500957 if (*rqstp->rq_next_page)
958 put_page(*rqstp->rq_next_page);
959 *(rqstp->rq_next_page++) = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700961 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return size;
965}
966
Jens Axboecf8208d2007-06-12 21:22:14 +0200967static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
968 struct splice_desc *sd)
969{
970 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
971}
972
Al Viro6264d692006-10-19 23:28:58 -0700973static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
975 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
976{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700978 __be32 err;
979 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 err = nfserr_perm;
Dave Hansena8754be2007-10-16 23:31:15 -0700982
Jens Axboecf8208d2007-06-12 21:22:14 +0200983 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
984 struct splice_desc sd = {
985 .len = 0,
986 .total_len = *count,
987 .pos = offset,
988 .u.data = rqstp,
989 };
990
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500991 rqstp->rq_next_page = rqstp->rq_respages + 1;
Jens Axboecf8208d2007-06-12 21:22:14 +0200992 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 } else {
994 oldfs = get_fs();
995 set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -0700996 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 set_fs(oldfs);
998 }
999
Al Viro6264d692006-10-19 23:28:58 -07001000 if (host_err >= 0) {
1001 nfsdstats.io_read += host_err;
1002 *count = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 err = 0;
Eric Paris2a12a9d2009-12-17 21:24:21 -05001004 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 } else
Al Viro6264d692006-10-19 23:28:58 -07001006 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return err;
1008}
1009
Neil Brown9f708e42006-01-06 00:19:59 -08001010static void kill_suid(struct dentry *dentry)
1011{
1012 struct iattr ia;
Serge E. Hallynb5376772007-10-16 23:31:36 -07001013 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Neil Brown9f708e42006-01-06 00:19:59 -08001014
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001015 mutex_lock(&dentry->d_inode->i_mutex);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -04001016 /*
1017 * Note we call this on write, so notify_change will not
1018 * encounter any conflicting delegations:
1019 */
1020 notify_change(dentry, &ia, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001021 mutex_unlock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -08001022}
1023
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001024/*
1025 * Gathered writes: If another process is currently writing to the file,
1026 * there's a high chance this is another nfsd (triggered by a bulk write
1027 * from a client's biod). Rather than syncing the file with each write
1028 * request, we sleep for 10 msec.
1029 *
1030 * I don't know if this roughly approximates C. Juszak's idea of
1031 * gathered writes, but it's a nice and simple solution (IMHO), and it
1032 * seems to work:-)
1033 *
1034 * Note: we do this only in the NFSv2 case, since v3 and higher have a
1035 * better tool (separate unstable writes and commits) for solving this
1036 * problem.
1037 */
1038static int wait_for_concurrent_writes(struct file *file)
1039{
Al Viro496ad9a2013-01-23 17:07:38 -05001040 struct inode *inode = file_inode(file);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001041 static ino_t last_ino;
1042 static dev_t last_dev;
1043 int err = 0;
1044
1045 if (atomic_read(&inode->i_writecount) > 1
1046 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1047 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1048 msleep(10);
1049 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1050 }
1051
1052 if (inode->i_state & I_DIRTY) {
1053 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001054 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001055 }
1056 last_ino = inode->i_ino;
1057 last_dev = inode->i_sb->s_dev;
1058 return err;
1059}
1060
Al Viro6264d692006-10-19 23:28:58 -07001061static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1063 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -05001064 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct svc_export *exp;
1067 struct dentry *dentry;
1068 struct inode *inode;
1069 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001070 __be32 err = 0;
1071 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001073 int use_wgather;
Kent Overstreete49dbbf2013-03-22 11:18:24 -07001074 loff_t pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001076 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 inode = dentry->d_inode;
1078 exp = fhp->fh_export;
1079
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001080 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!EX_ISSYNC(exp))
1083 stable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* Write the data. */
1086 oldfs = get_fs(); set_fs(KERNEL_DS);
Kent Overstreete49dbbf2013-03-22 11:18:24 -07001087 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001089 if (host_err < 0)
1090 goto out_nfserr;
1091 *cnt = host_err;
1092 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -05001093 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /* clear setuid/setgid flag after write */
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001096 if (inode->i_mode & (S_ISUID | S_ISGID))
Neil Brown9f708e42006-01-06 00:19:59 -08001097 kill_suid(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
J. Bruce Fieldsface1502012-10-26 16:12:31 -04001099 if (stable) {
1100 if (use_wgather)
1101 host_err = wait_for_concurrent_writes(file);
1102 else
1103 host_err = vfs_fsync_range(file, offset, offset+*cnt, 0);
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001106out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001107 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001108 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001110 else
Al Viro6264d692006-10-19 23:28:58 -07001111 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return err;
1113}
1114
1115/*
1116 * Read data from a file. count must contain the requested read count
1117 * on entry. On return, *count contains the number of bytes actually read.
1118 * N.B. After this call fhp needs an fh_put
1119 */
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001120__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001121 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1122{
1123 struct file *file;
1124 struct inode *inode;
1125 struct raparms *ra;
1126 __be32 err;
1127
1128 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1129 if (err)
1130 return err;
1131
Al Viro496ad9a2013-01-23 17:07:38 -05001132 inode = file_inode(file);
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001133
1134 /* Get readahead parameters */
1135 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1136
1137 if (ra && ra->p_set)
1138 file->f_ra = ra->p_ra;
1139
1140 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1141
1142 /* Write back readahead params */
1143 if (ra) {
1144 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1145 spin_lock(&rab->pb_lock);
1146 ra->p_ra = file->f_ra;
1147 ra->p_set = 1;
1148 ra->p_count--;
1149 spin_unlock(&rab->pb_lock);
1150 }
1151
1152 nfsd_close(file);
1153 return err;
1154}
1155
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001156/* As above, but use the provided file descriptor. */
Al Viro6264d692006-10-19 23:28:58 -07001157__be32
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001158nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 loff_t offset, struct kvec *vec, int vlen,
1160 unsigned long *count)
1161{
Al Viro6264d692006-10-19 23:28:58 -07001162 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001165 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001166 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (err)
1168 goto out;
1169 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001170 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1171 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172out:
1173 return err;
1174}
1175
1176/*
1177 * Write data to a file.
1178 * The stable flag requests synchronous writes.
1179 * N.B. After this call fhp needs an fh_put
1180 */
Al Viro6264d692006-10-19 23:28:58 -07001181__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001183 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int *stablep)
1185{
Al Viro6264d692006-10-19 23:28:58 -07001186 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001189 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001190 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (err)
1192 goto out;
1193 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1194 stablep);
1195 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001196 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (err)
1198 goto out;
1199
1200 if (cnt)
1201 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1202 cnt, stablep);
1203 nfsd_close(file);
1204 }
1205out:
1206 return err;
1207}
1208
1209#ifdef CONFIG_NFSD_V3
1210/*
1211 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001212 *
1213 * Note: we only guarantee that data that lies within the range specified
1214 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 *
1216 * Unfortunately we cannot lock the file to make sure we return full WCC
1217 * data to the client, as locking happens lower down in the filesystem.
1218 */
Al Viro6264d692006-10-19 23:28:58 -07001219__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1221 loff_t offset, unsigned long count)
1222{
1223 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001224 loff_t end = LLONG_MAX;
1225 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Trond Myklebustaa696a62010-01-29 16:44:25 -05001227 if (offset < 0)
1228 goto out;
1229 if (count != 0) {
1230 end = offset + (loff_t)count - 1;
1231 if (end < offset)
1232 goto out;
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Jeff Layton91885252010-03-19 08:06:28 -04001235 err = nfsd_open(rqstp, fhp, S_IFREG,
1236 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001237 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001240 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001241
1242 if (err2 != -EINVAL)
1243 err = nfserrno(err2);
1244 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 nfsd_close(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001249out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return err;
1251}
1252#endif /* CONFIG_NFSD_V3 */
1253
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001254static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001255nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1256 struct iattr *iap)
1257{
1258 /*
1259 * Mode has already been set earlier in create:
1260 */
1261 iap->ia_valid &= ~ATTR_MODE;
1262 /*
1263 * Setting uid/gid works only for root. Irix appears to
1264 * send along the gid on create when it tries to implement
1265 * setgid directories via NFS:
1266 */
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001267 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001268 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1269 if (iap->ia_valid)
1270 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1271 return 0;
1272}
1273
wengang wang4ac35c22009-02-10 11:27:51 +08001274/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1275 * setting size to 0 may fail for some specific file systems by the permission
1276 * checking which requires WRITE permission but the mode is 000.
1277 * we ignore the resizing(to 0) on the just new created file, since the size is
1278 * 0 after file created.
1279 *
1280 * call this only after vfs_create() is called.
1281 * */
1282static void
1283nfsd_check_ignore_resizing(struct iattr *iap)
1284{
1285 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1286 iap->ia_valid &= ~ATTR_SIZE;
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289/*
1290 * Create a file (regular, directory, device, fifo); UNIX sockets
1291 * not yet implemented.
1292 * If the response fh has been verified, the parent directory should
1293 * already be locked. Note that the parent directory is left locked.
1294 *
1295 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1296 */
Al Viro6264d692006-10-19 23:28:58 -07001297__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1299 char *fname, int flen, struct iattr *iap,
1300 int type, dev_t rdev, struct svc_fh *resfhp)
1301{
1302 struct dentry *dentry, *dchild = NULL;
1303 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001304 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001305 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001306 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 err = nfserr_perm;
1309 if (!flen)
1310 goto out;
1311 err = nfserr_exist;
1312 if (isdotent(fname, flen))
1313 goto out;
1314
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001315 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (err)
1317 goto out;
1318
1319 dentry = fhp->fh_dentry;
1320 dirp = dentry->d_inode;
1321
1322 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001323 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto out;
1325 /*
1326 * Check whether the response file handle has been verified yet.
1327 * If it has, the parent directory should already be locked.
1328 */
1329 if (!resfhp->fh_dentry) {
Jan Kara4a55c102012-06-12 16:20:33 +02001330 host_err = fh_want_write(fhp);
1331 if (host_err)
1332 goto out_nfserr;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001335 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001337 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (IS_ERR(dchild))
1339 goto out_nfserr;
1340 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1341 if (err)
1342 goto out;
1343 } else {
1344 /* called from nfsd_proc_create */
1345 dchild = dget(resfhp->fh_dentry);
1346 if (!fhp->fh_locked) {
1347 /* not actually possible */
1348 printk(KERN_ERR
Al Viroa6a9f182013-09-16 10:57:01 -04001349 "nfsd_create: parent %pd2 not locked!\n",
1350 dentry);
Al Virod75f2b92006-01-18 17:43:44 -08001351 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 goto out;
1353 }
1354 }
1355 /*
1356 * Make sure the child dentry is still negative ...
1357 */
1358 err = nfserr_exist;
1359 if (dchild->d_inode) {
Al Viroa6a9f182013-09-16 10:57:01 -04001360 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1361 dentry, dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto out;
1363 }
1364
1365 if (!(iap->ia_valid & ATTR_MODE))
1366 iap->ia_mode = 0;
1367 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1368
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001369 err = nfserr_inval;
1370 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1371 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1372 type);
1373 goto out;
1374 }
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /*
1377 * Get the dir op function pointer.
1378 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001379 err = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001380 host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 switch (type) {
1382 case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04001383 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001384 if (!host_err)
1385 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 break;
1387 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001388 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 break;
1390 case S_IFCHR:
1391 case S_IFBLK:
1392 case S_IFIFO:
1393 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001394 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001396 }
Jan Kara4a55c102012-06-12 16:20:33 +02001397 if (host_err < 0)
Dave Hansen463c3192008-02-15 14:37:57 -08001398 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Ben Myersf5019122010-02-17 14:05:11 -06001400 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Ben Myersf5019122010-02-17 14:05:11 -06001402 /*
1403 * nfsd_setattr already committed the child. Transactional filesystems
1404 * had a chance to commit changes for both parent and child
1405 * simultaneously making the following commit_metadata a noop.
1406 */
1407 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001408 if (err2)
1409 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /*
1411 * Update the file handle to get the new inode info.
1412 */
1413 if (!err)
1414 err = fh_update(resfhp);
1415out:
1416 if (dchild && !IS_ERR(dchild))
1417 dput(dchild);
1418 return err;
1419
1420out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001421 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 goto out;
1423}
1424
1425#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001426
1427static inline int nfsd_create_is_exclusive(int createmode)
1428{
1429 return createmode == NFS3_CREATE_EXCLUSIVE
1430 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001434 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 */
Al Viro6264d692006-10-19 23:28:58 -07001436__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001437do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 char *fname, int flen, struct iattr *iap,
1439 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001440 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct dentry *dentry, *dchild = NULL;
1443 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001444 __be32 err;
1445 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 err = nfserr_perm;
1449 if (!flen)
1450 goto out;
1451 err = nfserr_exist;
1452 if (isdotent(fname, flen))
1453 goto out;
1454 if (!(iap->ia_valid & ATTR_MODE))
1455 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001456 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (err)
1458 goto out;
1459
1460 dentry = fhp->fh_dentry;
1461 dirp = dentry->d_inode;
1462
1463 /* Get all the sanity checks out of the way before
1464 * we lock the parent. */
1465 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001466 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001468
1469 host_err = fh_want_write(fhp);
1470 if (host_err)
1471 goto out_nfserr;
1472
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001473 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /*
1476 * Compose the response file handle.
1477 */
1478 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001479 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (IS_ERR(dchild))
1481 goto out_nfserr;
1482
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001483 /* If file doesn't exist, check for permissions to create one */
1484 if (!dchild->d_inode) {
1485 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1486 if (err)
1487 goto out;
1488 }
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1491 if (err)
1492 goto out;
1493
Mi Jinlongac6721a2011-04-20 17:06:25 +08001494 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001495 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001496 * the high bit set, so just clear the high bits. If this is
1497 * ever changed to use different attrs for storing the
1498 * verifier, then do_open_lookup() will also need to be fixed
1499 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
1501 v_mtime = verifier[0]&0x7fffffff;
1502 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504
1505 if (dchild->d_inode) {
1506 err = 0;
1507
1508 switch (createmode) {
1509 case NFS3_CREATE_UNCHECKED:
1510 if (! S_ISREG(dchild->d_inode->i_mode))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001511 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 else if (truncp) {
1513 /* in nfsv4, we need to treat this case a little
1514 * differently. we don't want to truncate the
1515 * file now; this would be wrong if the OPEN
1516 * fails for some other reason. furthermore,
1517 * if the size is nonzero, we should ignore it
1518 * according to spec!
1519 */
1520 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1521 }
1522 else {
1523 iap->ia_valid &= ATTR_SIZE;
1524 goto set_attr;
1525 }
1526 break;
1527 case NFS3_CREATE_EXCLUSIVE:
1528 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1529 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown7007c902012-12-07 15:40:55 -05001530 && dchild->d_inode->i_size == 0 ) {
1531 if (created)
1532 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 break;
Neil Brown7007c902012-12-07 15:40:55 -05001534 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001535 case NFS4_CREATE_EXCLUSIVE4_1:
1536 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1537 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown7007c902012-12-07 15:40:55 -05001538 && dchild->d_inode->i_size == 0 ) {
1539 if (created)
1540 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001541 goto set_attr;
Neil Brown7007c902012-12-07 15:40:55 -05001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /* fallthru */
1544 case NFS3_CREATE_GUARDED:
1545 err = nfserr_exist;
1546 }
Al Virobad0dcf2011-11-23 12:03:18 -05001547 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 goto out;
1549 }
1550
Al Viro312b63f2012-06-10 18:09:36 -04001551 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001552 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001553 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001555 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001556 if (created)
1557 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
wengang wang4ac35c22009-02-10 11:27:51 +08001559 nfsd_check_ignore_resizing(iap);
1560
Mi Jinlongac6721a2011-04-20 17:06:25 +08001561 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001562 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001564 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* XXX someone who knows this better please fix it for nsec */
1566 iap->ia_mtime.tv_sec = v_mtime;
1567 iap->ia_atime.tv_sec = v_atime;
1568 iap->ia_mtime.tv_nsec = 0;
1569 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001573 err = nfsd_create_setattr(rqstp, resfhp, iap);
1574
1575 /*
1576 * nfsd_setattr already committed the child (and possibly also the parent).
1577 */
1578 if (!err)
1579 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001580
1581 /*
1582 * Update the filehandle to get the new inode info.
1583 */
1584 if (!err)
1585 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 out:
1588 fh_unlock(fhp);
1589 if (dchild && !IS_ERR(dchild))
1590 dput(dchild);
Jan Kara4a55c102012-06-12 16:20:33 +02001591 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return err;
1593
1594 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001595 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 goto out;
1597}
1598#endif /* CONFIG_NFSD_V3 */
1599
1600/*
1601 * Read a symlink. On entry, *lenp must contain the maximum path length that
1602 * fits into the buffer. On return, it contains the true length.
1603 * N.B. After this call fhp needs an fh_put
1604 */
Al Viro6264d692006-10-19 23:28:58 -07001605__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1607{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 struct inode *inode;
1609 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001610 __be32 err;
1611 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001612 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001614 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (err)
1616 goto out;
1617
Al Viro68ac1232012-03-15 08:21:57 -04001618 path.mnt = fhp->fh_export->ex_path.mnt;
1619 path.dentry = fhp->fh_dentry;
1620 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001623 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto out;
1625
Al Viro68ac1232012-03-15 08:21:57 -04001626 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /* N.B. Why does this call need a get_fs()??
1628 * Remove the set_fs and watch the fireworks:-) --okir
1629 */
1630
1631 oldfs = get_fs(); set_fs(KERNEL_DS);
J. Bruce Fieldsfac7a172012-07-27 14:16:55 -04001632 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 set_fs(oldfs);
1634
Al Viro6264d692006-10-19 23:28:58 -07001635 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001637 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 err = 0;
1639out:
1640 return err;
1641
1642out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001643 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto out;
1645}
1646
1647/*
1648 * Create a symlink and look up its inode
1649 * N.B. After this call _both_ fhp and resfhp need an fh_put
1650 */
Al Viro6264d692006-10-19 23:28:58 -07001651__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1653 char *fname, int flen,
1654 char *path, int plen,
1655 struct svc_fh *resfhp,
1656 struct iattr *iap)
1657{
1658 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001659 __be32 err, cerr;
1660 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 err = nfserr_noent;
1663 if (!flen || !plen)
1664 goto out;
1665 err = nfserr_exist;
1666 if (isdotent(fname, flen))
1667 goto out;
1668
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001669 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (err)
1671 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001672
1673 host_err = fh_want_write(fhp);
1674 if (host_err)
1675 goto out_nfserr;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 fh_lock(fhp);
1678 dentry = fhp->fh_dentry;
1679 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001680 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (IS_ERR(dnew))
1682 goto out_nfserr;
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (unlikely(path[plen] != 0)) {
1685 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1686 if (path_alloced == NULL)
Al Viro6264d692006-10-19 23:28:58 -07001687 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 else {
1689 strncpy(path_alloced, path, plen);
1690 path_alloced[plen] = 0;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001691 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 kfree(path_alloced);
1693 }
1694 } else
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001695 host_err = vfs_symlink(dentry->d_inode, dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001696 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001697 if (!err)
1698 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 fh_unlock(fhp);
1700
Al Virobad0dcf2011-11-23 12:03:18 -05001701 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1704 dput(dnew);
1705 if (err==0) err = cerr;
1706out:
1707 return err;
1708
1709out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001710 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto out;
1712}
1713
1714/*
1715 * Create a hardlink
1716 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1717 */
Al Viro6264d692006-10-19 23:28:58 -07001718__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1720 char *name, int len, struct svc_fh *tfhp)
1721{
1722 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001723 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001724 __be32 err;
1725 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001727 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (err)
1729 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001730 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (err)
1732 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001733 err = nfserr_isdir;
1734 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 err = nfserr_perm;
1737 if (!len)
1738 goto out;
1739 err = nfserr_exist;
1740 if (isdotent(name, len))
1741 goto out;
1742
Jan Kara4a55c102012-06-12 16:20:33 +02001743 host_err = fh_want_write(tfhp);
1744 if (host_err) {
1745 err = nfserrno(host_err);
1746 goto out;
1747 }
1748
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001749 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 ddir = ffhp->fh_dentry;
1751 dirp = ddir->d_inode;
1752
1753 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001754 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (IS_ERR(dnew))
1756 goto out_nfserr;
1757
1758 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001760 err = nfserr_noent;
1761 if (!dold->d_inode)
Jan Kara4a55c102012-06-12 16:20:33 +02001762 goto out_dput;
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001763 host_err = nfsd_break_lease(dold->d_inode);
Casey Bodley7d751f62011-06-03 12:21:23 -04001764 if (host_err) {
1765 err = nfserrno(host_err);
Jan Kara4a55c102012-06-12 16:20:33 +02001766 goto out_dput;
Casey Bodley7d751f62011-06-03 12:21:23 -04001767 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04001768 host_err = vfs_link(dold, dirp, dnew, NULL);
Al Viro6264d692006-10-19 23:28:58 -07001769 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001770 err = nfserrno(commit_metadata(ffhp));
1771 if (!err)
1772 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 } else {
Al Viro6264d692006-10-19 23:28:58 -07001774 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 err = nfserr_acces;
1776 else
Al Viro6264d692006-10-19 23:28:58 -07001777 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001779out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001781out_unlock:
1782 fh_unlock(ffhp);
Jan Kara4a55c102012-06-12 16:20:33 +02001783 fh_drop_write(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784out:
1785 return err;
1786
1787out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001788 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001789 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/*
1793 * Rename a file
1794 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1795 */
Al Viro6264d692006-10-19 23:28:58 -07001796__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1798 struct svc_fh *tfhp, char *tname, int tlen)
1799{
1800 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1801 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001802 __be32 err;
1803 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001805 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (err)
1807 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001808 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (err)
1810 goto out;
1811
1812 fdentry = ffhp->fh_dentry;
1813 fdir = fdentry->d_inode;
1814
1815 tdentry = tfhp->fh_dentry;
1816 tdir = tdentry->d_inode;
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 err = nfserr_perm;
1819 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1820 goto out;
1821
Jan Kara4a55c102012-06-12 16:20:33 +02001822 host_err = fh_want_write(ffhp);
1823 if (host_err) {
1824 err = nfserrno(host_err);
1825 goto out;
1826 }
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* cannot use fh_lock as we need deadlock protective ordering
1829 * so do it by hand */
1830 trap = lock_rename(tdentry, fdentry);
1831 ffhp->fh_locked = tfhp->fh_locked = 1;
1832 fill_pre_wcc(ffhp);
1833 fill_pre_wcc(tfhp);
1834
1835 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001836 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (IS_ERR(odentry))
1838 goto out_nfserr;
1839
Al Viro6264d692006-10-19 23:28:58 -07001840 host_err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (!odentry->d_inode)
1842 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001843 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (odentry == trap)
1845 goto out_dput_old;
1846
1847 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001848 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (IS_ERR(ndentry))
1850 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001851 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (ndentry == trap)
1853 goto out_dput_new;
1854
Dave Hansen9079b1e2008-02-15 14:37:49 -08001855 host_err = -EXDEV;
1856 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1857 goto out_dput_new;
J. Bruce Fieldsaa387d6c2013-04-15 16:03:46 -04001858 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1859 goto out_dput_new;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001860
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001861 host_err = nfsd_break_lease(odentry->d_inode);
1862 if (host_err)
Jan Kara4a55c102012-06-12 16:20:33 +02001863 goto out_dput_new;
J. Bruce Fields83f6b0c2011-02-06 16:46:30 -05001864 if (ndentry->d_inode) {
1865 host_err = nfsd_break_lease(ndentry->d_inode);
1866 if (host_err)
Jan Kara4a55c102012-06-12 16:20:33 +02001867 goto out_dput_new;
J. Bruce Fields83f6b0c2011-02-06 16:46:30 -05001868 }
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04001869 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL);
Ben Myersf5019122010-02-17 14:05:11 -06001870 if (!host_err) {
1871 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001872 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001873 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 out_dput_new:
1876 dput(ndentry);
1877 out_dput_old:
1878 dput(odentry);
1879 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001880 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 /* we cannot reply on fh_unlock on the two filehandles,
1883 * as that would do the wrong thing if the two directories
1884 * were the same, so again we do it by hand
1885 */
1886 fill_post_wcc(ffhp);
1887 fill_post_wcc(tfhp);
1888 unlock_rename(tdentry, fdentry);
1889 ffhp->fh_locked = tfhp->fh_locked = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001890 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892out:
1893 return err;
1894}
1895
1896/*
1897 * Unlink a file or directory
1898 * N.B. After this call fhp needs an fh_put
1899 */
Al Viro6264d692006-10-19 23:28:58 -07001900__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1902 char *fname, int flen)
1903{
1904 struct dentry *dentry, *rdentry;
1905 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001906 __be32 err;
1907 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 err = nfserr_acces;
1910 if (!flen || isdotent(fname, flen))
1911 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001912 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (err)
1914 goto out;
1915
Jan Kara4a55c102012-06-12 16:20:33 +02001916 host_err = fh_want_write(fhp);
1917 if (host_err)
1918 goto out_nfserr;
1919
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001920 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 dentry = fhp->fh_dentry;
1922 dirp = dentry->d_inode;
1923
1924 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001925 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (IS_ERR(rdentry))
1927 goto out_nfserr;
1928
1929 if (!rdentry->d_inode) {
1930 dput(rdentry);
1931 err = nfserr_noent;
1932 goto out;
1933 }
1934
1935 if (!type)
1936 type = rdentry->d_inode->i_mode & S_IFMT;
1937
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001938 host_err = nfsd_break_lease(rdentry->d_inode);
1939 if (host_err)
Jan Kara4a55c102012-06-12 16:20:33 +02001940 goto out_put;
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001941 if (type != S_IFDIR)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04001942 host_err = vfs_unlink(dirp, rdentry, NULL);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001943 else
Al Viro6264d692006-10-19 23:28:58 -07001944 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001945 if (!host_err)
1946 host_err = commit_metadata(fhp);
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001947out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 dput(rdentry);
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001951 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001952out:
1953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001957 * We do this buffering because we must not call back into the file
1958 * system's ->lookup() method from the filldir callback. That may well
1959 * deadlock a number of file systems.
1960 *
1961 * This is based heavily on the implementation of same in XFS.
1962 */
1963struct buffered_dirent {
1964 u64 ino;
1965 loff_t offset;
1966 int namlen;
1967 unsigned int d_type;
1968 char name[];
1969};
1970
1971struct readdir_data {
Al Viro5c0ba4e2013-05-15 13:52:59 -04001972 struct dir_context ctx;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001973 char *dirent;
1974 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001975 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001976};
1977
1978static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1979 loff_t offset, u64 ino, unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001980{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001981 struct readdir_data *buf = __buf;
1982 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1983 unsigned int reclen;
1984
1985 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001986 if (buf->used + reclen > PAGE_SIZE) {
1987 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001988 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001989 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001990
1991 de->namlen = namlen;
1992 de->offset = offset;
1993 de->ino = ino;
1994 de->d_type = d_type;
1995 memcpy(de->name, name, namlen);
1996 buf->used += reclen;
1997
1998 return 0;
1999}
2000
David Woodhouse2f9092e2009-04-20 23:18:37 +01002001static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
2002 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01002003{
David Woodhouse14f7dd62008-07-31 20:29:12 +01002004 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01002005 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002006 int size;
2007 loff_t offset;
Al Viroac6614b2013-05-22 22:22:04 -04002008 struct readdir_data buf = {
2009 .ctx.actor = nfsd_buffered_filldir,
2010 .dirent = (void *)__get_free_page(GFP_KERNEL)
2011 };
David Woodhouse2628b762008-07-31 17:16:51 +01002012
David Woodhouse14f7dd62008-07-31 20:29:12 +01002013 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01002014 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01002015
David Woodhouse14f7dd62008-07-31 20:29:12 +01002016 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002017
2018 while (1) {
Al Viro496ad9a2013-01-23 17:07:38 -05002019 struct inode *dir_inode = file_inode(file);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002020 unsigned int reclen;
2021
Doug Nazarb726e922008-11-05 06:16:28 -05002022 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01002023 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04002024 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002025
Al Viro5c0ba4e2013-05-15 13:52:59 -04002026 host_err = iterate_dir(file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -04002027 if (buf.full)
2028 host_err = 0;
2029
2030 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01002031 break;
2032
2033 size = buf.used;
2034
2035 if (!size)
2036 break;
2037
David Woodhouse2f9092e2009-04-20 23:18:37 +01002038 /*
2039 * Various filldir functions may end up calling back into
2040 * lookup_one_len() and the file system's ->lookup() method.
2041 * These expect i_mutex to be held, as it would within readdir.
2042 */
2043 host_err = mutex_lock_killable(&dir_inode->i_mutex);
2044 if (host_err)
2045 break;
2046
David Woodhouse14f7dd62008-07-31 20:29:12 +01002047 de = (struct buffered_dirent *)buf.dirent;
2048 while (size > 0) {
2049 offset = de->offset;
2050
2051 if (func(cdp, de->name, de->namlen, de->offset,
2052 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01002053 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002054
2055 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01002056 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002057
2058 reclen = ALIGN(sizeof(*de) + de->namlen,
2059 sizeof(u64));
2060 size -= reclen;
2061 de = (struct buffered_dirent *)((char *)de + reclen);
2062 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01002063 mutex_unlock(&dir_inode->i_mutex);
2064 if (size > 0) /* We bailed out early */
2065 break;
2066
David Woodhousec002a6c2008-08-17 17:21:18 +01002067 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002068 }
2069
David Woodhouse14f7dd62008-07-31 20:29:12 +01002070 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01002071
2072 if (host_err)
2073 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002074
2075 *offsetp = offset;
2076 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01002077}
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079/*
2080 * Read entries from a directory.
2081 * The NFSv3/4 verifier we ignore for now.
2082 */
Al Viro6264d692006-10-19 23:28:58 -07002083__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
NeilBrowna0ad13e2007-01-26 00:57:10 -08002085 struct readdir_cd *cdp, filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Al Viro6264d692006-10-19 23:28:58 -07002087 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 struct file *file;
2089 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04002090 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Bernd Schubert06effdb2012-03-18 22:44:50 -04002092 /* NFSv2 only supports 32 bit cookies */
2093 if (rqstp->rq_vers > 2)
2094 may_flags |= NFSD_MAY_64BIT_COOKIE;
2095
2096 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (err)
2098 goto out;
2099
Jeff Laytonb108fe62012-04-25 15:30:00 -04002100 offset = vfs_llseek(file, offset, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (offset < 0) {
2102 err = nfserrno((int)offset);
2103 goto out_close;
2104 }
2105
David Woodhouse14f7dd62008-07-31 20:29:12 +01002106 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 if (err == nfserr_eof || err == nfserr_toosmall)
2109 err = nfs_ok; /* can still be found in ->err */
2110out_close:
2111 nfsd_close(file);
2112out:
2113 return err;
2114}
2115
2116/*
2117 * Get file system stats
2118 * N.B. After this call fhp needs an fh_put
2119 */
Al Viro6264d692006-10-19 23:28:58 -07002120__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04002121nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002123 __be32 err;
2124
2125 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02002126 if (!err) {
2127 struct path path = {
2128 .mnt = fhp->fh_export->ex_path.mnt,
2129 .dentry = fhp->fh_dentry,
2130 };
2131 if (vfs_statfs(&path, stat))
2132 err = nfserr_io;
2133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return err;
2135}
2136
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002137static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002138{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002139 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002140}
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142/*
2143 * Check for a user's access permissions to this inode.
2144 */
Al Viro6264d692006-10-19 23:28:58 -07002145__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07002146nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2147 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 struct inode *inode = dentry->d_inode;
2150 int err;
2151
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04002152 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return 0;
2154#if 0
2155 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2156 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002157 (acc & NFSD_MAY_READ)? " read" : "",
2158 (acc & NFSD_MAY_WRITE)? " write" : "",
2159 (acc & NFSD_MAY_EXEC)? " exec" : "",
2160 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2161 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2162 (acc & NFSD_MAY_LOCK)? " lock" : "",
2163 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 inode->i_mode,
2165 IS_IMMUTABLE(inode)? " immut" : "",
2166 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002167 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002169 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170#endif
2171
2172 /* Normally we reject any write/sattr etc access on a read-only file
2173 * system. But if it is IRIX doing check on write-access for a
2174 * device special file, we ignore rofs.
2175 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002176 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2177 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002178 if (exp_rdonly(rqstp, exp) ||
2179 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002181 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return nfserr_perm;
2183 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002184 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return nfserr_perm;
2186
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002187 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /* If we cannot rely on authentication in NLM requests,
2189 * just allow locks, otherwise require read permission, or
2190 * ownership
2191 */
2192 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2193 return 0;
2194 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002195 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197 /*
2198 * The file owner always gets access permission for accesses that
2199 * would normally be checked at open time. This is to make
2200 * file access work even when the client has done a fchmod(fd, 0).
2201 *
2202 * However, `cp foo bar' should fail nevertheless when bar is
2203 * readonly. A sensible way to do this might be to reject all
2204 * attempts to truncate a read-only file, because a creat() call
2205 * always implies file truncation.
2206 * ... but this isn't really fair. A process may reasonably call
2207 * ftruncate on an open file descriptor on a file with perm 000.
2208 * We must trust the client to do permission checking - using "ACCESS"
2209 * with NFSv3.
2210 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002211 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002212 uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return 0;
2214
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002215 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002216 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* Allow read access to binaries even when mode 111 */
2219 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002220 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2221 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002222 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 return err? nfserrno(err) : 0;
2225}
2226
2227void
2228nfsd_racache_shutdown(void)
2229{
Jeff Layton54a66e52008-08-13 22:03:27 -04002230 struct raparms *raparm, *last_raparm;
2231 unsigned int i;
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002234
2235 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2236 raparm = raparm_hash[i].pb_head;
2237 while(raparm) {
2238 last_raparm = raparm;
2239 raparm = raparm->p_next;
2240 kfree(last_raparm);
2241 }
2242 raparm_hash[i].pb_head = NULL;
2243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245/*
2246 * Initialize readahead param cache
2247 */
2248int
2249nfsd_racache_init(int cache_size)
2250{
2251 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002252 int j = 0;
2253 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002254 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Greg Banksfce14562006-10-04 02:15:49 -07002256
Jeff Layton54a66e52008-08-13 22:03:27 -04002257 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002259 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2260 if (nperbucket < 2)
2261 nperbucket = 2;
2262 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002263
2264 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002265
2266 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002267 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002268
2269 raparm = &raparm_hash[i].pb_head;
2270 for (j = 0; j < nperbucket; j++) {
2271 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2272 if (!*raparm)
2273 goto out_nomem;
2274 raparm = &(*raparm)->p_next;
2275 }
2276 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002277 }
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 nfsdstats.ra_size = cache_size;
2280 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002281
2282out_nomem:
2283 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2284 nfsd_racache_shutdown();
2285 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002287
2288#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2289struct posix_acl *
2290nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2291{
2292 struct inode *inode = fhp->fh_dentry->d_inode;
2293 char *name;
2294 void *value = NULL;
2295 ssize_t size;
2296 struct posix_acl *acl;
2297
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002298 if (!IS_POSIXACL(inode))
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002299 return ERR_PTR(-EOPNOTSUPP);
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002300
2301 switch (type) {
2302 case ACL_TYPE_ACCESS:
2303 name = POSIX_ACL_XATTR_ACCESS;
2304 break;
2305 case ACL_TYPE_DEFAULT:
2306 name = POSIX_ACL_XATTR_DEFAULT;
2307 break;
2308 default:
2309 return ERR_PTR(-EOPNOTSUPP);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002310 }
2311
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002312 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2313 if (size < 0)
2314 return ERR_PTR(size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002315
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -07002316 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002317 kfree(value);
2318 return acl;
2319}
2320
2321int
2322nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2323{
2324 struct inode *inode = fhp->fh_dentry->d_inode;
2325 char *name;
2326 void *value = NULL;
2327 size_t size;
2328 int error;
2329
Al Viroacfa4382008-12-04 10:06:33 -05002330 if (!IS_POSIXACL(inode) ||
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002331 !inode->i_op->setxattr || !inode->i_op->removexattr)
2332 return -EOPNOTSUPP;
2333 switch(type) {
2334 case ACL_TYPE_ACCESS:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002335 name = POSIX_ACL_XATTR_ACCESS;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002336 break;
2337 case ACL_TYPE_DEFAULT:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002338 name = POSIX_ACL_XATTR_DEFAULT;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002339 break;
2340 default:
2341 return -EOPNOTSUPP;
2342 }
2343
2344 if (acl && acl->a_count) {
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002345 size = posix_acl_xattr_size(acl->a_count);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002346 value = kmalloc(size, GFP_KERNEL);
2347 if (!value)
2348 return -ENOMEM;
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -07002349 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Florin Malita9ccfc292006-05-20 14:59:58 -07002350 if (error < 0)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002351 goto getout;
Florin Malita9ccfc292006-05-20 14:59:58 -07002352 size = error;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002353 } else
2354 size = 0;
2355
Al Virobad0dcf2011-11-23 12:03:18 -05002356 error = fh_want_write(fhp);
Dave Hansen18f335a2008-02-15 14:37:38 -08002357 if (error)
2358 goto getout;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002359 if (size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002360 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002361 else {
2362 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2363 error = 0;
2364 else {
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002365 error = vfs_removexattr(fhp->fh_dentry, name);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002366 if (error == -ENODATA)
2367 error = 0;
2368 }
2369 }
Al Virobad0dcf2011-11-23 12:03:18 -05002370 fh_drop_write(fhp);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002371
2372getout:
2373 kfree(value);
2374 return error;
2375}
2376#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */