blob: 477a58d6cc86dde76639e91f7155b99f747db40e [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>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020031
32#ifdef CONFIG_NFSD_V3
33#include "xdr3.h"
34#endif /* CONFIG_NFSD_V3 */
35
Christoph Hellwig5be196e2006-01-09 20:51:55 -080036#ifdef CONFIG_NFSD_V4
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050037#include "acl.h"
38#include "idmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#endif /* CONFIG_NFSD_V4 */
40
Boaz Harrosh9a74af22009-12-03 20:30:56 +020041#include "nfsd.h"
42#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define NFSDDBG_FACILITY NFSDDBG_FILEOP
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * This is a cache of readahead params that help us choose the proper
49 * readahead strategy. Initially, we set all readahead parameters to 0
50 * and let the VFS handle things.
51 * If you increase the number of cached files very much, you'll need to
52 * add a hash table here.
53 */
54struct raparms {
55 struct raparms *p_next;
56 unsigned int p_count;
57 ino_t p_ino;
58 dev_t p_dev;
59 int p_set;
60 struct file_ra_state p_ra;
Greg Banksfce14562006-10-04 02:15:49 -070061 unsigned int p_hindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Greg Banksfce14562006-10-04 02:15:49 -070064struct raparm_hbucket {
65 struct raparms *pb_head;
66 spinlock_t pb_lock;
67} ____cacheline_aligned_in_smp;
68
Greg Banksfce14562006-10-04 02:15:49 -070069#define RAPARM_HASH_BITS 4
70#define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
71#define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
72static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
76 * a mount point.
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080077 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * or nfs_ok having possibly changed *dpp and *expp
79 */
80int
81nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
82 struct svc_export **expp)
83{
84 struct svc_export *exp = *expp, *exp2 = NULL;
85 struct dentry *dentry = *dpp;
Al Viro91c9fa82009-04-18 02:42:05 -040086 struct path path = {.mnt = mntget(exp->ex_path.mnt),
87 .dentry = dget(dentry)};
Al Viro6264d692006-10-19 23:28:58 -070088 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Al Viro7cc90cc2011-03-18 09:04:20 -040090 err = follow_down(&path);
David Howellscc53ce52011-01-14 18:45:26 +000091 if (err < 0)
92 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Al Viro91c9fa82009-04-18 02:42:05 -040094 exp2 = rqst_exp_get_by_name(rqstp, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (IS_ERR(exp2)) {
J. Bruce Fields3b6cee7b2009-10-25 21:18:19 -040096 err = PTR_ERR(exp2);
97 /*
98 * We normally allow NFS clients to continue
99 * "underneath" a mountpoint that is not exported.
100 * The exception is V4ROOT, where no traversal is ever
101 * allowed without an explicit export of the new
102 * directory.
103 */
104 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
105 err = 0;
Al Viro91c9fa82009-04-18 02:42:05 -0400106 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 goto out;
108 }
Steve Dickson3c394dd2009-09-09 15:02:40 -0400109 if (nfsd_v4client(rqstp) ||
110 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* successfully crossed mount point */
Al Viro1644ccc2009-04-18 02:32:31 -0400112 /*
Al Viro91c9fa82009-04-18 02:42:05 -0400113 * This is subtle: path.dentry is *not* on path.mnt
114 * at this point. The only reason we are safe is that
115 * original mnt is pinned down by exp, so we should
116 * put path *before* putting exp
Al Viro1644ccc2009-04-18 02:32:31 -0400117 */
Al Viro91c9fa82009-04-18 02:42:05 -0400118 *dpp = path.dentry;
119 path.dentry = dentry;
Al Viro1644ccc2009-04-18 02:32:31 -0400120 *expp = exp2;
Al Viro91c9fa82009-04-18 02:42:05 -0400121 exp2 = exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Al Viro91c9fa82009-04-18 02:42:05 -0400123 path_put(&path);
124 exp_put(exp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125out:
126 return err;
127}
128
J. Bruce Fields289ede42009-09-26 20:32:24 -0400129static void follow_to_parent(struct path *path)
130{
131 struct dentry *dp;
132
133 while (path->dentry == path->mnt->mnt_root && follow_up(path))
134 ;
135 dp = dget_parent(path->dentry);
136 dput(path->dentry);
137 path->dentry = dp;
138}
139
140static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
141{
142 struct svc_export *exp2;
143 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
144 .dentry = dget(dparent)};
145
146 follow_to_parent(&path);
147
148 exp2 = rqst_exp_parent(rqstp, &path);
149 if (PTR_ERR(exp2) == -ENOENT) {
150 *dentryp = dget(dparent);
151 } else if (IS_ERR(exp2)) {
152 path_put(&path);
153 return PTR_ERR(exp2);
154 } else {
155 *dentryp = dget(path.dentry);
156 exp_put(*exp);
157 *exp = exp2;
158 }
159 path_put(&path);
160 return 0;
161}
162
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400163/*
164 * For nfsd purposes, we treat V4ROOT exports as though there was an
165 * export at *every* directory.
166 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -0400167int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400168{
169 if (d_mountpoint(dentry))
170 return 1;
Trond Myklebust11fcee02011-09-12 19:37:26 -0400171 if (nfsd4_is_junction(dentry))
172 return 1;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400173 if (!(exp->ex_flags & NFSEXP_V4ROOT))
174 return 0;
175 return dentry->d_inode != NULL;
176}
177
Al Viro6264d692006-10-19 23:28:58 -0700178__be32
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700179nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400180 const char *name, unsigned int len,
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700181 struct svc_export **exp_ret, struct dentry **dentry_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct svc_export *exp;
184 struct dentry *dparent;
185 struct dentry *dentry;
Al Viro6264d692006-10-19 23:28:58 -0700186 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 dparent = fhp->fh_dentry;
191 exp = fhp->fh_export;
192 exp_get(exp);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Lookup the name, but don't follow links */
195 if (isdotent(name, len)) {
196 if (len==1)
197 dentry = dget(dparent);
Jan Blunck54775492008-02-14 19:38:39 -0800198 else if (dparent != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 dentry = dget_parent(dparent);
J. Bruce Fieldsfed83812009-09-26 16:53:01 -0400200 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 dentry = dget(dparent); /* .. == . just like at / */
202 else {
203 /* checking mountpoint crossing is very different when stepping up */
J. Bruce Fields289ede42009-09-26 20:32:24 -0400204 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
205 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 } else {
209 fh_lock(fhp);
210 dentry = lookup_one_len(name, dparent, len);
Al Viro6264d692006-10-19 23:28:58 -0700211 host_err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (IS_ERR(dentry))
213 goto out_nfserr;
214 /*
215 * check if we have crossed a mount point ...
216 */
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400217 if (nfsd_mountpoint(dentry, exp)) {
Al Viro6264d692006-10-19 23:28:58 -0700218 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 dput(dentry);
220 goto out_nfserr;
221 }
222 }
223 }
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700224 *dentry_ret = dentry;
225 *exp_ret = exp;
226 return 0;
227
228out_nfserr:
229 exp_put(exp);
230 return nfserrno(host_err);
231}
232
233/*
234 * Look up one component of a pathname.
235 * N.B. After this call _both_ fhp and resfh need an fh_put
236 *
237 * If the lookup would cross a mountpoint, and the mounted filesystem
238 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
239 * accepted as it stands and the mounted directory is
240 * returned. Otherwise the covered directory is returned.
241 * NOTE: this mountpoint crossing is not supported properly by all
242 * clients and is explicitly disallowed for NFSv3
243 * NeilBrown <neilb@cse.unsw.edu.au>
244 */
245__be32
246nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400247 unsigned int len, struct svc_fh *resfh)
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700248{
249 struct svc_export *exp;
250 struct dentry *dentry;
251 __be32 err;
252
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400253 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
254 if (err)
255 return err;
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700256 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
257 if (err)
258 return err;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700259 err = check_nfsd_access(exp, rqstp);
260 if (err)
261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * Note: we compose the file handle now, but as the
264 * dentry may be negative, it may need to be updated.
265 */
266 err = fh_compose(resfh, exp, dentry, fhp);
267 if (!err && !dentry->d_inode)
268 err = nfserr_noent;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700269out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 exp_put(exp);
272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
J. Bruce Fields4795bb32011-01-11 13:55:46 -0500275static int nfsd_break_lease(struct inode *inode)
276{
277 if (!S_ISREG(inode->i_mode))
278 return 0;
279 return break_lease(inode, O_WRONLY | O_NONBLOCK);
280}
281
Ben Myersf5019122010-02-17 14:05:11 -0600282/*
283 * Commit metadata changes to stable storage.
284 */
285static int
286commit_metadata(struct svc_fh *fhp)
287{
288 struct inode *inode = fhp->fh_dentry->d_inode;
289 const struct export_operations *export_ops = inode->i_sb->s_export_op;
Ben Myersf5019122010-02-17 14:05:11 -0600290
291 if (!EX_ISSYNC(fhp->fh_export))
292 return 0;
293
Christoph Hellwigc3765012010-10-06 10:48:20 +0200294 if (export_ops->commit_metadata)
295 return export_ops->commit_metadata(inode);
296 return sync_inode_metadata(inode, 1);
Ben Myersf5019122010-02-17 14:05:11 -0600297}
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
Christoph Hellwig6ee4fd22013-11-18 05:07:30 -0800300 * Go over the attributes and take care of the small differences between
301 * NFS semantics and what Linux expects.
302 */
303static void
304nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
305{
306 /*
307 * NFSv2 does not differentiate between "set-[ac]time-to-now"
308 * which only requires access, and "set-[ac]time-to-X" which
309 * requires ownership.
310 * So if it looks like it might be "set both to the same time which
311 * is close to now", and if inode_change_ok fails, then we
312 * convert to "set to now" instead of "set to explicit time"
313 *
314 * We only call inode_change_ok as the last test as technically
315 * it is not an interface that we should be using.
316 */
317#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
318#define MAX_TOUCH_TIME_ERROR (30*60)
319 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
320 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
321 /*
322 * Looks probable.
323 *
324 * Now just make sure time is in the right ballpark.
325 * Solaris, at least, doesn't seem to care what the time
326 * request is. We require it be within 30 minutes of now.
327 */
328 time_t delta = iap->ia_atime.tv_sec - get_seconds();
329 if (delta < 0)
330 delta = -delta;
331 if (delta < MAX_TOUCH_TIME_ERROR &&
332 inode_change_ok(inode, iap) != 0) {
333 /*
334 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
335 * This will cause notify_change to set these times
336 * to "now"
337 */
338 iap->ia_valid &= ~BOTH_TIME_SET;
339 }
340 }
341
342 /* sanitize the mode change */
343 if (iap->ia_valid & ATTR_MODE) {
344 iap->ia_mode &= S_IALLUGO;
345 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
346 }
347
348 /* Revoke setuid/setgid on chown */
349 if (!S_ISDIR(inode->i_mode) &&
350 (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
351 ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid))) {
352 iap->ia_valid |= ATTR_KILL_PRIV;
353 if (iap->ia_valid & ATTR_MODE) {
354 /* we're setting mode too, just clear the s*id bits */
355 iap->ia_mode &= ~S_ISUID;
356 if (iap->ia_mode & S_IXGRP)
357 iap->ia_mode &= ~S_ISGID;
358 } else {
359 /* set ATTR_KILL_* bits and let VFS handle it */
360 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
361 }
362 }
363}
364
365static __be32
366nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
367 struct iattr *iap)
368{
369 struct inode *inode = fhp->fh_dentry->d_inode;
370 int host_err;
371
372 if (iap->ia_size < inode->i_size) {
373 __be32 err;
374
375 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
376 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
377 if (err)
378 return err;
379 }
380
381 host_err = get_write_access(inode);
382 if (host_err)
383 goto out_nfserrno;
384
385 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
386 if (host_err)
387 goto out_put_write_access;
388 return 0;
389
390out_put_write_access:
391 put_write_access(inode);
392out_nfserrno:
393 return nfserrno(host_err);
394}
395
396/*
397 * Set various file attributes. After this call fhp needs an fh_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Al Viro6264d692006-10-19 23:28:58 -0700399__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
401 int check_guard, time_t guardtime)
402{
403 struct dentry *dentry;
404 struct inode *inode;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200405 int accmode = NFSD_MAY_SATTR;
Al Viro175a4eb2011-07-26 03:30:54 -0400406 umode_t ftype = 0;
Al Viro6264d692006-10-19 23:28:58 -0700407 __be32 err;
408 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int size_change = 0;
410
411 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200412 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (iap->ia_valid & ATTR_SIZE)
414 ftype = S_IFREG;
415
416 /* Get inode */
417 err = fh_verify(rqstp, fhp, ftype, accmode);
NeilBrown15b7a1b2005-11-07 01:00:23 -0800418 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 goto out;
420
421 dentry = fhp->fh_dentry;
422 inode = dentry->d_inode;
423
NeilBrown15b7a1b2005-11-07 01:00:23 -0800424 /* Ignore any mode updates on symlinks */
425 if (S_ISLNK(inode->i_mode))
426 iap->ia_valid &= ~ATTR_MODE;
427
428 if (!iap->ia_valid)
429 goto out;
430
Christoph Hellwig6ee4fd22013-11-18 05:07:30 -0800431 nfsd_sanitize_attrs(inode, iap);
432
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000433 /*
Christoph Hellwig6ee4fd22013-11-18 05:07:30 -0800434 * The size case is special, it changes the file in addition to the
435 * attributes.
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (iap->ia_valid & ATTR_SIZE) {
Christoph Hellwig6ee4fd22013-11-18 05:07:30 -0800438 err = nfsd_get_write_access(rqstp, fhp, iap);
439 if (err)
440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 size_change = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 iap->ia_valid |= ATTR_CTIME;
445
446 err = nfserr_notsync;
447 if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
J. Bruce Fields4795bb32011-01-11 13:55:46 -0500448 host_err = nfsd_break_lease(inode);
J. Bruce Fields6a76beb2011-01-11 12:54:39 -0500449 if (host_err)
450 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 fh_lock(fhp);
J. Bruce Fields6a76beb2011-01-11 12:54:39 -0500452
Al Viro6264d692006-10-19 23:28:58 -0700453 host_err = notify_change(dentry, iap);
454 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 fh_unlock(fhp);
456 }
457 if (size_change)
458 put_write_access(inode);
459 if (!err)
Christoph Hellwigb160fda2010-06-01 21:59:18 +0200460 commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461out:
462 return err;
463
464out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700465 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 goto out;
467}
468
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800469#if defined(CONFIG_NFSD_V2_ACL) || \
470 defined(CONFIG_NFSD_V3_ACL) || \
471 defined(CONFIG_NFSD_V4)
472static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
473{
474 ssize_t buflen;
Krishna Kumar6c6a4262008-10-22 14:48:36 +0530475 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800477 buflen = vfs_getxattr(dentry, key, NULL, 0);
478 if (buflen <= 0)
479 return buflen;
480
481 *buf = kmalloc(buflen, GFP_KERNEL);
482 if (!*buf)
483 return -ENOMEM;
484
Krishna Kumar6c6a4262008-10-22 14:48:36 +0530485 ret = vfs_getxattr(dentry, key, *buf, buflen);
486 if (ret < 0)
487 kfree(*buf);
488 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800489}
490#endif
491
492#if defined(CONFIG_NFSD_V4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int
494set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
495{
496 int len;
497 size_t buflen;
498 char *buf = NULL;
499 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 buflen = posix_acl_xattr_size(pacl->a_count);
502 buf = kmalloc(buflen, GFP_KERNEL);
503 error = -ENOMEM;
504 if (buf == NULL)
505 goto out;
506
507 len = posix_acl_to_xattr(pacl, buf, buflen);
508 if (len < 0) {
509 error = len;
510 goto out;
511 }
512
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800513 error = vfs_setxattr(dentry, key, buf, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514out:
515 kfree(buf);
516 return error;
517}
518
Al Viro6264d692006-10-19 23:28:58 -0700519__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
521 struct nfs4_acl *acl)
522{
Al Viro6264d692006-10-19 23:28:58 -0700523 __be32 error;
524 int host_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct dentry *dentry;
526 struct inode *inode;
527 struct posix_acl *pacl = NULL, *dpacl = NULL;
528 unsigned int flags = 0;
529
530 /* Get inode */
J. Bruce Fieldse281d812011-08-15 16:57:07 -0400531 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (error)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700533 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 dentry = fhp->fh_dentry;
536 inode = dentry->d_inode;
537 if (S_ISDIR(inode->i_mode))
538 flags = NFS4_ACL_DIR;
539
Al Viro6264d692006-10-19 23:28:58 -0700540 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
541 if (host_error == -EINVAL) {
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700542 return nfserr_attrnotsupp;
Al Viro6264d692006-10-19 23:28:58 -0700543 } else if (host_error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto out_nfserr;
545
Al Viro6264d692006-10-19 23:28:58 -0700546 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
547 if (host_error < 0)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700548 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700550 if (S_ISDIR(inode->i_mode))
Al Viro6264d692006-10-19 23:28:58 -0700551 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700553out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 posix_acl_release(pacl);
555 posix_acl_release(dpacl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556out_nfserr:
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800557 if (host_error == -EOPNOTSUPP)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700558 return nfserr_attrnotsupp;
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800559 else
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700560 return nfserrno(host_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563static struct posix_acl *
564_get_posix_acl(struct dentry *dentry, char *key)
565{
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800566 void *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct posix_acl *pacl = NULL;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800568 int buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800570 buflen = nfsd_getxattr(dentry, key, &buf);
571 if (!buflen)
572 buflen = -ENODATA;
573 if (buflen <= 0)
574 return ERR_PTR(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 pacl = posix_acl_from_xattr(buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 kfree(buf);
578 return pacl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581int
582nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
583{
584 struct inode *inode = dentry->d_inode;
585 int error = 0;
586 struct posix_acl *pacl = NULL, *dpacl = NULL;
587 unsigned int flags = 0;
588
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700589 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
591 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
592 if (IS_ERR(pacl)) {
593 error = PTR_ERR(pacl);
594 pacl = NULL;
595 goto out;
596 }
597
598 if (S_ISDIR(inode->i_mode)) {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700599 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
601 dpacl = NULL;
602 else if (IS_ERR(dpacl)) {
603 error = PTR_ERR(dpacl);
604 dpacl = NULL;
605 goto out;
606 }
607 flags = NFS4_ACL_DIR;
608 }
609
610 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
611 if (IS_ERR(*acl)) {
612 error = PTR_ERR(*acl);
613 *acl = NULL;
614 }
615 out:
616 posix_acl_release(pacl);
617 posix_acl_release(dpacl);
618 return error;
619}
620
Chuck Lever9b4146e2012-01-04 16:26:43 -0500621/*
622 * NFS junction information is stored in an extended attribute.
623 */
624#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
625
626/**
627 * nfsd4_is_junction - Test if an object could be an NFS junction
628 *
629 * @dentry: object to test
630 *
631 * Returns 1 if "dentry" appears to contain NFS junction information.
632 * Otherwise 0 is returned.
633 */
Trond Myklebust11fcee02011-09-12 19:37:26 -0400634int nfsd4_is_junction(struct dentry *dentry)
635{
636 struct inode *inode = dentry->d_inode;
637
638 if (inode == NULL)
639 return 0;
640 if (inode->i_mode & S_IXUGO)
641 return 0;
642 if (!(inode->i_mode & S_ISVTX))
643 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500644 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee02011-09-12 19:37:26 -0400645 return 0;
646 return 1;
647}
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400648#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650#ifdef CONFIG_NFSD_V3
651/*
652 * Check server access rights to a file system object
653 */
654struct accessmap {
655 u32 access;
656 int how;
657};
658static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200659 { NFS3_ACCESS_READ, NFSD_MAY_READ },
660 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
661 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
662 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 { 0, 0 }
665};
666
667static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200668 { NFS3_ACCESS_READ, NFSD_MAY_READ },
669 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
670 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
671 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
672 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 { 0, 0 }
675};
676
677static struct accessmap nfs3_anyaccess[] = {
678 /* Some clients - Solaris 2.6 at least, make an access call
679 * to the server to check for access for things like /dev/null
680 * (which really, the server doesn't care about). So
681 * We provide simple access checking for them, looking
682 * mainly at mode bits, and we make sure to ignore read-only
683 * filesystem checks
684 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200685 { NFS3_ACCESS_READ, NFSD_MAY_READ },
686 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
687 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
688 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 { 0, 0 }
691};
692
Al Viro6264d692006-10-19 23:28:58 -0700693__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
695{
696 struct accessmap *map;
697 struct svc_export *export;
698 struct dentry *dentry;
699 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700700 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200702 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (error)
704 goto out;
705
706 export = fhp->fh_export;
707 dentry = fhp->fh_dentry;
708
709 if (S_ISREG(dentry->d_inode->i_mode))
710 map = nfs3_regaccess;
711 else if (S_ISDIR(dentry->d_inode->i_mode))
712 map = nfs3_diraccess;
713 else
714 map = nfs3_anyaccess;
715
716
717 query = *access;
718 for (; map->access; map++) {
719 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700720 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 sresult |= map->access;
723
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700724 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 switch (err2) {
726 case nfs_ok:
727 result |= map->access;
728 break;
729
730 /* the following error codes just mean the access was not allowed,
731 * rather than an error occurred */
732 case nfserr_rofs:
733 case nfserr_acces:
734 case nfserr_perm:
735 /* simply don't "or" in the access bit. */
736 break;
737 default:
738 error = err2;
739 goto out;
740 }
741 }
742 }
743 *access = result;
744 if (supported)
745 *supported = sresult;
746
747 out:
748 return error;
749}
750#endif /* CONFIG_NFSD_V3 */
751
J. Bruce Fields105f4622011-06-07 11:50:23 -0400752static int nfsd_open_break_lease(struct inode *inode, int access)
753{
754 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
J. Bruce Fields105f4622011-06-07 11:50:23 -0400756 if (access & NFSD_MAY_NOT_BREAK_LEASE)
757 return 0;
758 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
759 return break_lease(inode, mode | O_NONBLOCK);
760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762/*
763 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400764 * The may_flags argument indicates the type of open (read/write/lock)
765 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * N.B. After this call fhp needs an fh_put
767 */
Al Viro6264d692006-10-19 23:28:58 -0700768__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400769nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400770 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct dentry *dentry;
773 struct inode *inode;
Al Viro6264d692006-10-19 23:28:58 -0700774 int flags = O_RDONLY|O_LARGEFILE;
775 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400776 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David Howellse0e81732009-09-02 09:13:40 +0100778 validate_process_creds();
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * If we get here, then the client has already done an "open",
782 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
783 * in case a chmod has now revoked permission.
784 */
Bernd Schubert999448a2012-03-18 22:44:49 -0400785 err = fh_verify(rqstp, fhp, type, may_flags | NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (err)
787 goto out;
788
789 dentry = fhp->fh_dentry;
790 inode = dentry->d_inode;
791
792 /* Disallow write access to files with the append-only bit set
793 * or any access when mandatory locking enabled
794 */
795 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400796 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400798 /*
799 * We must ignore files (but only files) which might have mandatory
800 * locks on them because there is no way to know if the accesser has
801 * the lock.
802 */
803 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto out;
805
806 if (!inode->i_fop)
807 goto out;
808
Bernd Schubert999448a2012-03-18 22:44:49 -0400809 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700810 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto out_nfserr;
812
Bernd Schubert999448a2012-03-18 22:44:49 -0400813 if (may_flags & NFSD_MAY_WRITE) {
814 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700815 flags = O_RDWR|O_LARGEFILE;
816 else
817 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Jan Blunck54775492008-02-14 19:38:39 -0800819 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
David Howells033a6662009-07-02 14:35:32 +0100820 flags, current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (IS_ERR(*filp))
Al Viro6264d692006-10-19 23:28:58 -0700822 host_err = PTR_ERR(*filp);
Bernd Schubert06effdb2012-03-18 22:44:50 -0400823 else {
Bernd Schubert999448a2012-03-18 22:44:49 -0400824 host_err = ima_file_check(*filp, may_flags);
825
Bernd Schubert06effdb2012-03-18 22:44:50 -0400826 if (may_flags & NFSD_MAY_64BIT_COOKIE)
827 (*filp)->f_mode |= FMODE_64BITHASH;
828 else
829 (*filp)->f_mode |= FMODE_32BITHASH;
830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700833 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834out:
David Howellse0e81732009-09-02 09:13:40 +0100835 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return err;
837}
838
839/*
840 * Close a file.
841 */
842void
843nfsd_close(struct file *filp)
844{
845 fput(filp);
846}
847
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500848/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * Obtain the readahead parameters for the file
850 * specified by (dev, ino).
851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853static inline struct raparms *
854nfsd_get_raparms(dev_t dev, ino_t ino)
855{
856 struct raparms *ra, **rap, **frap = NULL;
857 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700858 unsigned int hash;
859 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Greg Banksfce14562006-10-04 02:15:49 -0700861 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
862 rab = &raparm_hash[hash];
863
864 spin_lock(&rab->pb_lock);
865 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (ra->p_ino == ino && ra->p_dev == dev)
867 goto found;
868 depth++;
869 if (ra->p_count == 0)
870 frap = rap;
871 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300872 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700874 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return NULL;
876 }
877 rap = frap;
878 ra = *frap;
879 ra->p_dev = dev;
880 ra->p_ino = ino;
881 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700882 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883found:
Greg Banksfce14562006-10-04 02:15:49 -0700884 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700886 ra->p_next = rab->pb_head;
887 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 ra->p_count++;
890 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700891 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return ra;
893}
894
895/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200896 * Grab and keep cached pages associated with a file in the svc_rqst
897 * so that they can be passed to the network sendmsg/sendpage routines
898 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
900static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200901nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
902 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Jens Axboecf8208d2007-06-12 21:22:14 +0200904 struct svc_rqst *rqstp = sd->u.data;
NeilBrown44524352006-10-04 02:15:46 -0700905 struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
Jens Axboecf8208d2007-06-12 21:22:14 +0200906 struct page *page = buf->page;
907 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200908
909 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (rqstp->rq_res.page_len == 0) {
912 get_page(page);
NeilBrown44524352006-10-04 02:15:46 -0700913 put_page(*pp);
914 *pp = page;
915 rqstp->rq_resused++;
Jens Axboecf8208d2007-06-12 21:22:14 +0200916 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700918 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 get_page(page);
NeilBrown250f3912007-01-26 00:56:59 -0800920 if (*pp)
921 put_page(*pp);
NeilBrown44524352006-10-04 02:15:46 -0700922 *pp = page;
923 rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700925 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return size;
929}
930
Jens Axboecf8208d2007-06-12 21:22:14 +0200931static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
932 struct splice_desc *sd)
933{
934 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
935}
936
Al Viro6264d692006-10-19 23:28:58 -0700937static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
939 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
940{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700942 __be32 err;
943 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 err = nfserr_perm;
Dave Hansena8754be2007-10-16 23:31:15 -0700946
Jens Axboecf8208d2007-06-12 21:22:14 +0200947 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
948 struct splice_desc sd = {
949 .len = 0,
950 .total_len = *count,
951 .pos = offset,
952 .u.data = rqstp,
953 };
954
Jens Axboe4fbef202007-07-13 22:42:20 +0200955 rqstp->rq_resused = 1;
Jens Axboecf8208d2007-06-12 21:22:14 +0200956 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 } else {
958 oldfs = get_fs();
959 set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -0700960 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 set_fs(oldfs);
962 }
963
Al Viro6264d692006-10-19 23:28:58 -0700964 if (host_err >= 0) {
965 nfsdstats.io_read += host_err;
966 *count = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 err = 0;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500968 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 } else
Al Viro6264d692006-10-19 23:28:58 -0700970 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return err;
972}
973
Neil Brown9f708e42006-01-06 00:19:59 -0800974static void kill_suid(struct dentry *dentry)
975{
976 struct iattr ia;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700977 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Neil Brown9f708e42006-01-06 00:19:59 -0800978
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800979 mutex_lock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800980 notify_change(dentry, &ia);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800981 mutex_unlock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800982}
983
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700984/*
985 * Gathered writes: If another process is currently writing to the file,
986 * there's a high chance this is another nfsd (triggered by a bulk write
987 * from a client's biod). Rather than syncing the file with each write
988 * request, we sleep for 10 msec.
989 *
990 * I don't know if this roughly approximates C. Juszak's idea of
991 * gathered writes, but it's a nice and simple solution (IMHO), and it
992 * seems to work:-)
993 *
994 * Note: we do this only in the NFSv2 case, since v3 and higher have a
995 * better tool (separate unstable writes and commits) for solving this
996 * problem.
997 */
998static int wait_for_concurrent_writes(struct file *file)
999{
1000 struct inode *inode = file->f_path.dentry->d_inode;
1001 static ino_t last_ino;
1002 static dev_t last_dev;
1003 int err = 0;
1004
1005 if (atomic_read(&inode->i_writecount) > 1
1006 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1007 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1008 msleep(10);
1009 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1010 }
1011
1012 if (inode->i_state & I_DIRTY) {
1013 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001014 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001015 }
1016 last_ino = inode->i_ino;
1017 last_dev = inode->i_sb->s_dev;
1018 return err;
1019}
1020
Al Viro6264d692006-10-19 23:28:58 -07001021static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1023 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -05001024 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct svc_export *exp;
1027 struct dentry *dentry;
1028 struct inode *inode;
1029 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001030 __be32 err = 0;
1031 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001033 int use_wgather;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001035 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 inode = dentry->d_inode;
1037 exp = fhp->fh_export;
1038
1039 /*
1040 * Request sync writes if
1041 * - the sync export option has been set, or
1042 * - the client requested O_SYNC behavior (NFSv3 feature).
1043 * - The file system doesn't support fsync().
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001044 * When NFSv2 gathered writes have been configured for this volume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * flushing the data to disk is handled separately below.
1046 */
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001047 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Harvey Harrison3ba15142008-02-20 12:49:02 -08001049 if (!file->f_op->fsync) {/* COMMIT3 cannot work */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 stable = 2;
1051 *stablep = 2; /* FILE_SYNC */
1052 }
1053
1054 if (!EX_ISSYNC(exp))
1055 stable = 0;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001056 if (stable && !use_wgather) {
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001057 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 file->f_flags |= O_SYNC;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001059 spin_unlock(&file->f_lock);
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /* Write the data. */
1063 oldfs = get_fs(); set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -07001064 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001066 if (host_err < 0)
1067 goto out_nfserr;
1068 *cnt = host_err;
1069 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -05001070 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* clear setuid/setgid flag after write */
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001073 if (inode->i_mode & (S_ISUID | S_ISGID))
Neil Brown9f708e42006-01-06 00:19:59 -08001074 kill_suid(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001076 if (stable && use_wgather)
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001077 host_err = wait_for_concurrent_writes(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001079out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001080 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001081 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001083 else
Al Viro6264d692006-10-19 23:28:58 -07001084 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return err;
1086}
1087
1088/*
1089 * Read data from a file. count must contain the requested read count
1090 * on entry. On return, *count contains the number of bytes actually read.
1091 * N.B. After this call fhp needs an fh_put
1092 */
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001093__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001094 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1095{
1096 struct file *file;
1097 struct inode *inode;
1098 struct raparms *ra;
1099 __be32 err;
1100
1101 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1102 if (err)
1103 return err;
1104
1105 inode = file->f_path.dentry->d_inode;
1106
1107 /* Get readahead parameters */
1108 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1109
1110 if (ra && ra->p_set)
1111 file->f_ra = ra->p_ra;
1112
1113 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1114
1115 /* Write back readahead params */
1116 if (ra) {
1117 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1118 spin_lock(&rab->pb_lock);
1119 ra->p_ra = file->f_ra;
1120 ra->p_set = 1;
1121 ra->p_count--;
1122 spin_unlock(&rab->pb_lock);
1123 }
1124
1125 nfsd_close(file);
1126 return err;
1127}
1128
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001129/* As above, but use the provided file descriptor. */
Al Viro6264d692006-10-19 23:28:58 -07001130__be32
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001131nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 loff_t offset, struct kvec *vec, int vlen,
1133 unsigned long *count)
1134{
Al Viro6264d692006-10-19 23:28:58 -07001135 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001138 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001139 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (err)
1141 goto out;
1142 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001143 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1144 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145out:
1146 return err;
1147}
1148
1149/*
1150 * Write data to a file.
1151 * The stable flag requests synchronous writes.
1152 * N.B. After this call fhp needs an fh_put
1153 */
Al Viro6264d692006-10-19 23:28:58 -07001154__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001156 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int *stablep)
1158{
Al Viro6264d692006-10-19 23:28:58 -07001159 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001162 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001163 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (err)
1165 goto out;
1166 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1167 stablep);
1168 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001169 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (err)
1171 goto out;
1172
1173 if (cnt)
1174 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1175 cnt, stablep);
1176 nfsd_close(file);
1177 }
1178out:
1179 return err;
1180}
1181
1182#ifdef CONFIG_NFSD_V3
1183/*
1184 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001185 *
1186 * Note: we only guarantee that data that lies within the range specified
1187 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 *
1189 * Unfortunately we cannot lock the file to make sure we return full WCC
1190 * data to the client, as locking happens lower down in the filesystem.
1191 */
Al Viro6264d692006-10-19 23:28:58 -07001192__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1194 loff_t offset, unsigned long count)
1195{
1196 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001197 loff_t end = LLONG_MAX;
1198 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Trond Myklebustaa696a62010-01-29 16:44:25 -05001200 if (offset < 0)
1201 goto out;
1202 if (count != 0) {
1203 end = offset + (loff_t)count - 1;
1204 if (end < offset)
1205 goto out;
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jeff Layton91885252010-03-19 08:06:28 -04001208 err = nfsd_open(rqstp, fhp, S_IFREG,
1209 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001210 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001211 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001213 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001214
1215 if (err2 != -EINVAL)
1216 err = nfserrno(err2);
1217 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 nfsd_close(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001222out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return err;
1224}
1225#endif /* CONFIG_NFSD_V3 */
1226
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001227static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001228nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1229 struct iattr *iap)
1230{
1231 /*
1232 * Mode has already been set earlier in create:
1233 */
1234 iap->ia_valid &= ~ATTR_MODE;
1235 /*
1236 * Setting uid/gid works only for root. Irix appears to
1237 * send along the gid on create when it tries to implement
1238 * setgid directories via NFS:
1239 */
David Howells5cc0a842008-11-14 10:38:58 +11001240 if (current_fsuid() != 0)
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001241 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1242 if (iap->ia_valid)
1243 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1244 return 0;
1245}
1246
wengang wang4ac35c22009-02-10 11:27:51 +08001247/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1248 * setting size to 0 may fail for some specific file systems by the permission
1249 * checking which requires WRITE permission but the mode is 000.
1250 * we ignore the resizing(to 0) on the just new created file, since the size is
1251 * 0 after file created.
1252 *
1253 * call this only after vfs_create() is called.
1254 * */
1255static void
1256nfsd_check_ignore_resizing(struct iattr *iap)
1257{
1258 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1259 iap->ia_valid &= ~ATTR_SIZE;
1260}
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262/*
1263 * Create a file (regular, directory, device, fifo); UNIX sockets
1264 * not yet implemented.
1265 * If the response fh has been verified, the parent directory should
1266 * already be locked. Note that the parent directory is left locked.
1267 *
1268 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1269 */
Al Viro6264d692006-10-19 23:28:58 -07001270__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1272 char *fname, int flen, struct iattr *iap,
1273 int type, dev_t rdev, struct svc_fh *resfhp)
1274{
1275 struct dentry *dentry, *dchild = NULL;
1276 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001277 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001278 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001279 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 err = nfserr_perm;
1282 if (!flen)
1283 goto out;
1284 err = nfserr_exist;
1285 if (isdotent(fname, flen))
1286 goto out;
1287
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001288 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (err)
1290 goto out;
1291
1292 dentry = fhp->fh_dentry;
1293 dirp = dentry->d_inode;
1294
1295 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001296 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 goto out;
1298 /*
1299 * Check whether the response file handle has been verified yet.
1300 * If it has, the parent directory should already be locked.
1301 */
1302 if (!resfhp->fh_dentry) {
1303 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001304 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001306 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (IS_ERR(dchild))
1308 goto out_nfserr;
1309 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1310 if (err)
1311 goto out;
1312 } else {
1313 /* called from nfsd_proc_create */
1314 dchild = dget(resfhp->fh_dentry);
1315 if (!fhp->fh_locked) {
1316 /* not actually possible */
1317 printk(KERN_ERR
1318 "nfsd_create: parent %s/%s not locked!\n",
1319 dentry->d_parent->d_name.name,
1320 dentry->d_name.name);
Al Virod75f2b92006-01-18 17:43:44 -08001321 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto out;
1323 }
1324 }
1325 /*
1326 * Make sure the child dentry is still negative ...
1327 */
1328 err = nfserr_exist;
1329 if (dchild->d_inode) {
1330 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1331 dentry->d_name.name, dchild->d_name.name);
1332 goto out;
1333 }
1334
1335 if (!(iap->ia_valid & ATTR_MODE))
1336 iap->ia_mode = 0;
1337 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1338
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001339 err = nfserr_inval;
1340 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1341 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1342 type);
1343 goto out;
1344 }
1345
Al Virobad0dcf2011-11-23 12:03:18 -05001346 host_err = fh_want_write(fhp);
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001347 if (host_err)
1348 goto out_nfserr;
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /*
1351 * Get the dir op function pointer.
1352 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001353 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 switch (type) {
1355 case S_IFREG:
Al Viro6264d692006-10-19 23:28:58 -07001356 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
wengang wang4ac35c22009-02-10 11:27:51 +08001357 if (!host_err)
1358 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 break;
1360 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001361 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 break;
1363 case S_IFCHR:
1364 case S_IFBLK:
1365 case S_IFIFO:
1366 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001367 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001369 }
1370 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001371 fh_drop_write(fhp);
Dave Hansen463c3192008-02-15 14:37:57 -08001372 goto out_nfserr;
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Ben Myersf5019122010-02-17 14:05:11 -06001375 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Ben Myersf5019122010-02-17 14:05:11 -06001377 /*
1378 * nfsd_setattr already committed the child. Transactional filesystems
1379 * had a chance to commit changes for both parent and child
1380 * simultaneously making the following commit_metadata a noop.
1381 */
1382 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001383 if (err2)
1384 err = err2;
Al Virobad0dcf2011-11-23 12:03:18 -05001385 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * Update the file handle to get the new inode info.
1388 */
1389 if (!err)
1390 err = fh_update(resfhp);
1391out:
1392 if (dchild && !IS_ERR(dchild))
1393 dput(dchild);
1394 return err;
1395
1396out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001397 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 goto out;
1399}
1400
1401#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001402
1403static inline int nfsd_create_is_exclusive(int createmode)
1404{
1405 return createmode == NFS3_CREATE_EXCLUSIVE
1406 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001410 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
Al Viro6264d692006-10-19 23:28:58 -07001412__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001413do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 char *fname, int flen, struct iattr *iap,
1415 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001416 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct dentry *dentry, *dchild = NULL;
1419 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001420 __be32 err;
1421 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 err = nfserr_perm;
1425 if (!flen)
1426 goto out;
1427 err = nfserr_exist;
1428 if (isdotent(fname, flen))
1429 goto out;
1430 if (!(iap->ia_valid & ATTR_MODE))
1431 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001432 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (err)
1434 goto out;
1435
1436 dentry = fhp->fh_dentry;
1437 dirp = dentry->d_inode;
1438
1439 /* Get all the sanity checks out of the way before
1440 * we lock the parent. */
1441 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001442 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto out;
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001444 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /*
1447 * Compose the response file handle.
1448 */
1449 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001450 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (IS_ERR(dchild))
1452 goto out_nfserr;
1453
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001454 /* If file doesn't exist, check for permissions to create one */
1455 if (!dchild->d_inode) {
1456 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1457 if (err)
1458 goto out;
1459 }
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1462 if (err)
1463 goto out;
1464
Mi Jinlongac6721a2011-04-20 17:06:25 +08001465 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001466 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001467 * the high bit set, so just clear the high bits. If this is
1468 * ever changed to use different attrs for storing the
1469 * verifier, then do_open_lookup() will also need to be fixed
1470 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 */
1472 v_mtime = verifier[0]&0x7fffffff;
1473 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
Al Virobad0dcf2011-11-23 12:03:18 -05001476 host_err = fh_want_write(fhp);
Dave Hansen463c3192008-02-15 14:37:57 -08001477 if (host_err)
1478 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (dchild->d_inode) {
1480 err = 0;
1481
1482 switch (createmode) {
1483 case NFS3_CREATE_UNCHECKED:
1484 if (! S_ISREG(dchild->d_inode->i_mode))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 else if (truncp) {
1487 /* in nfsv4, we need to treat this case a little
1488 * differently. we don't want to truncate the
1489 * file now; this would be wrong if the OPEN
1490 * fails for some other reason. furthermore,
1491 * if the size is nonzero, we should ignore it
1492 * according to spec!
1493 */
1494 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1495 }
1496 else {
1497 iap->ia_valid &= ATTR_SIZE;
1498 goto set_attr;
1499 }
1500 break;
1501 case NFS3_CREATE_EXCLUSIVE:
1502 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1503 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown59d6ce42012-12-07 15:40:55 -05001504 && dchild->d_inode->i_size == 0 ) {
1505 if (created)
1506 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 break;
Neil Brown59d6ce42012-12-07 15:40:55 -05001508 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001509 case NFS4_CREATE_EXCLUSIVE4_1:
1510 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1511 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown59d6ce42012-12-07 15:40:55 -05001512 && dchild->d_inode->i_size == 0 ) {
1513 if (created)
1514 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001515 goto set_attr;
Neil Brown59d6ce42012-12-07 15:40:55 -05001516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /* fallthru */
1518 case NFS3_CREATE_GUARDED:
1519 err = nfserr_exist;
1520 }
Al Virobad0dcf2011-11-23 12:03:18 -05001521 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 goto out;
1523 }
1524
Al Viro6264d692006-10-19 23:28:58 -07001525 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
Dave Hansen463c3192008-02-15 14:37:57 -08001526 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001527 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001529 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001530 if (created)
1531 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
wengang wang4ac35c22009-02-10 11:27:51 +08001533 nfsd_check_ignore_resizing(iap);
1534
Mi Jinlongac6721a2011-04-20 17:06:25 +08001535 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001536 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001538 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* XXX someone who knows this better please fix it for nsec */
1540 iap->ia_mtime.tv_sec = v_mtime;
1541 iap->ia_atime.tv_sec = v_atime;
1542 iap->ia_mtime.tv_nsec = 0;
1543 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001547 err = nfsd_create_setattr(rqstp, resfhp, iap);
1548
1549 /*
1550 * nfsd_setattr already committed the child (and possibly also the parent).
1551 */
1552 if (!err)
1553 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001554
Al Virobad0dcf2011-11-23 12:03:18 -05001555 fh_drop_write(fhp);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001556 /*
1557 * Update the filehandle to get the new inode info.
1558 */
1559 if (!err)
1560 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 out:
1563 fh_unlock(fhp);
1564 if (dchild && !IS_ERR(dchild))
1565 dput(dchild);
1566 return err;
1567
1568 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001569 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto out;
1571}
1572#endif /* CONFIG_NFSD_V3 */
1573
1574/*
1575 * Read a symlink. On entry, *lenp must contain the maximum path length that
1576 * fits into the buffer. On return, it contains the true length.
1577 * N.B. After this call fhp needs an fh_put
1578 */
Al Viro6264d692006-10-19 23:28:58 -07001579__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 struct inode *inode;
1583 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001584 __be32 err;
1585 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001586 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001588 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (err)
1590 goto out;
1591
Al Viro68ac1232012-03-15 08:21:57 -04001592 path.mnt = fhp->fh_export->ex_path.mnt;
1593 path.dentry = fhp->fh_dentry;
1594 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001597 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 goto out;
1599
Al Viro68ac1232012-03-15 08:21:57 -04001600 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /* N.B. Why does this call need a get_fs()??
1602 * Remove the set_fs and watch the fireworks:-) --okir
1603 */
1604
1605 oldfs = get_fs(); set_fs(KERNEL_DS);
Al Viro68ac1232012-03-15 08:21:57 -04001606 host_err = inode->i_op->readlink(path.dentry, buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 set_fs(oldfs);
1608
Al Viro6264d692006-10-19 23:28:58 -07001609 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001611 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 err = 0;
1613out:
1614 return err;
1615
1616out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001617 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 goto out;
1619}
1620
1621/*
1622 * Create a symlink and look up its inode
1623 * N.B. After this call _both_ fhp and resfhp need an fh_put
1624 */
Al Viro6264d692006-10-19 23:28:58 -07001625__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1627 char *fname, int flen,
1628 char *path, int plen,
1629 struct svc_fh *resfhp,
1630 struct iattr *iap)
1631{
1632 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001633 __be32 err, cerr;
1634 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 err = nfserr_noent;
1637 if (!flen || !plen)
1638 goto out;
1639 err = nfserr_exist;
1640 if (isdotent(fname, flen))
1641 goto out;
1642
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001643 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (err)
1645 goto out;
1646 fh_lock(fhp);
1647 dentry = fhp->fh_dentry;
1648 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001649 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (IS_ERR(dnew))
1651 goto out_nfserr;
1652
Al Virobad0dcf2011-11-23 12:03:18 -05001653 host_err = fh_want_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001654 if (host_err)
1655 goto out_nfserr;
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (unlikely(path[plen] != 0)) {
1658 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1659 if (path_alloced == NULL)
Al Viro6264d692006-10-19 23:28:58 -07001660 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 else {
1662 strncpy(path_alloced, path, plen);
1663 path_alloced[plen] = 0;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001664 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 kfree(path_alloced);
1666 }
1667 } else
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001668 host_err = vfs_symlink(dentry->d_inode, dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001669 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001670 if (!err)
1671 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 fh_unlock(fhp);
1673
Al Virobad0dcf2011-11-23 12:03:18 -05001674 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1677 dput(dnew);
1678 if (err==0) err = cerr;
1679out:
1680 return err;
1681
1682out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001683 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 goto out;
1685}
1686
1687/*
1688 * Create a hardlink
1689 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1690 */
Al Viro6264d692006-10-19 23:28:58 -07001691__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1693 char *name, int len, struct svc_fh *tfhp)
1694{
1695 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001696 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001697 __be32 err;
1698 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001700 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (err)
1702 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001703 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (err)
1705 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001706 err = nfserr_isdir;
1707 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 err = nfserr_perm;
1710 if (!len)
1711 goto out;
1712 err = nfserr_exist;
1713 if (isdotent(name, len))
1714 goto out;
1715
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001716 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 ddir = ffhp->fh_dentry;
1718 dirp = ddir->d_inode;
1719
1720 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001721 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (IS_ERR(dnew))
1723 goto out_nfserr;
1724
1725 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Al Virobad0dcf2011-11-23 12:03:18 -05001727 host_err = fh_want_write(tfhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001728 if (host_err) {
1729 err = nfserrno(host_err);
1730 goto out_dput;
1731 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001732 err = nfserr_noent;
1733 if (!dold->d_inode)
1734 goto out_drop_write;
1735 host_err = nfsd_break_lease(dold->d_inode);
Casey Bodley7d751f62011-06-03 12:21:23 -04001736 if (host_err) {
1737 err = nfserrno(host_err);
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001738 goto out_drop_write;
Casey Bodley7d751f62011-06-03 12:21:23 -04001739 }
Al Viro6264d692006-10-19 23:28:58 -07001740 host_err = vfs_link(dold, dirp, dnew);
1741 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001742 err = nfserrno(commit_metadata(ffhp));
1743 if (!err)
1744 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 } else {
Al Viro6264d692006-10-19 23:28:58 -07001746 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 err = nfserr_acces;
1748 else
Al Viro6264d692006-10-19 23:28:58 -07001749 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001751out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001752 fh_drop_write(tfhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001753out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001755out_unlock:
1756 fh_unlock(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757out:
1758 return err;
1759
1760out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001761 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001762 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765/*
1766 * Rename a file
1767 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1768 */
Al Viro6264d692006-10-19 23:28:58 -07001769__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1771 struct svc_fh *tfhp, char *tname, int tlen)
1772{
1773 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1774 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001775 __be32 err;
1776 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001778 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (err)
1780 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001781 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (err)
1783 goto out;
1784
1785 fdentry = ffhp->fh_dentry;
1786 fdir = fdentry->d_inode;
1787
1788 tdentry = tfhp->fh_dentry;
1789 tdir = tdentry->d_inode;
1790
1791 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
NeilBrowna56f3932006-06-30 01:56:10 -07001792 if (ffhp->fh_export != tfhp->fh_export)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 goto out;
1794
1795 err = nfserr_perm;
1796 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1797 goto out;
1798
1799 /* cannot use fh_lock as we need deadlock protective ordering
1800 * so do it by hand */
1801 trap = lock_rename(tdentry, fdentry);
1802 ffhp->fh_locked = tfhp->fh_locked = 1;
1803 fill_pre_wcc(ffhp);
1804 fill_pre_wcc(tfhp);
1805
1806 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001807 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (IS_ERR(odentry))
1809 goto out_nfserr;
1810
Al Viro6264d692006-10-19 23:28:58 -07001811 host_err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (!odentry->d_inode)
1813 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001814 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (odentry == trap)
1816 goto out_dput_old;
1817
1818 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001819 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (IS_ERR(ndentry))
1821 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001822 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (ndentry == trap)
1824 goto out_dput_new;
1825
Dave Hansen9079b1e2008-02-15 14:37:49 -08001826 host_err = -EXDEV;
1827 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1828 goto out_dput_new;
Al Virobad0dcf2011-11-23 12:03:18 -05001829 host_err = fh_want_write(ffhp);
Dave Hansen9079b1e2008-02-15 14:37:49 -08001830 if (host_err)
1831 goto out_dput_new;
1832
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001833 host_err = nfsd_break_lease(odentry->d_inode);
1834 if (host_err)
1835 goto out_drop_write;
J. Bruce Fields83f6b0c2011-02-06 16:46:30 -05001836 if (ndentry->d_inode) {
1837 host_err = nfsd_break_lease(ndentry->d_inode);
1838 if (host_err)
1839 goto out_drop_write;
1840 }
Al Viro6264d692006-10-19 23:28:58 -07001841 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
Ben Myersf5019122010-02-17 14:05:11 -06001842 if (!host_err) {
1843 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001844 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001845 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001847out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001848 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 out_dput_new:
1850 dput(ndentry);
1851 out_dput_old:
1852 dput(odentry);
1853 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001854 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* we cannot reply on fh_unlock on the two filehandles,
1857 * as that would do the wrong thing if the two directories
1858 * were the same, so again we do it by hand
1859 */
1860 fill_post_wcc(ffhp);
1861 fill_post_wcc(tfhp);
1862 unlock_rename(tdentry, fdentry);
1863 ffhp->fh_locked = tfhp->fh_locked = 0;
1864
1865out:
1866 return err;
1867}
1868
1869/*
1870 * Unlink a file or directory
1871 * N.B. After this call fhp needs an fh_put
1872 */
Al Viro6264d692006-10-19 23:28:58 -07001873__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1875 char *fname, int flen)
1876{
1877 struct dentry *dentry, *rdentry;
1878 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001879 __be32 err;
1880 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 err = nfserr_acces;
1883 if (!flen || isdotent(fname, flen))
1884 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001885 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (err)
1887 goto out;
1888
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001889 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 dentry = fhp->fh_dentry;
1891 dirp = dentry->d_inode;
1892
1893 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001894 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (IS_ERR(rdentry))
1896 goto out_nfserr;
1897
1898 if (!rdentry->d_inode) {
1899 dput(rdentry);
1900 err = nfserr_noent;
1901 goto out;
1902 }
1903
1904 if (!type)
1905 type = rdentry->d_inode->i_mode & S_IFMT;
1906
Al Virobad0dcf2011-11-23 12:03:18 -05001907 host_err = fh_want_write(fhp);
Dave Hansen06227532008-02-15 14:37:34 -08001908 if (host_err)
J. Bruce Fields541ce982011-01-14 20:00:02 -05001909 goto out_put;
Dave Hansen06227532008-02-15 14:37:34 -08001910
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001911 host_err = nfsd_break_lease(rdentry->d_inode);
1912 if (host_err)
J. Bruce Fields541ce982011-01-14 20:00:02 -05001913 goto out_drop_write;
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001914 if (type != S_IFDIR)
Al Viro6264d692006-10-19 23:28:58 -07001915 host_err = vfs_unlink(dirp, rdentry);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001916 else
Al Viro6264d692006-10-19 23:28:58 -07001917 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001918 if (!host_err)
1919 host_err = commit_metadata(fhp);
1920out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001921 fh_drop_write(fhp);
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001922out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 dput(rdentry);
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001926 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001927out:
1928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001932 * We do this buffering because we must not call back into the file
1933 * system's ->lookup() method from the filldir callback. That may well
1934 * deadlock a number of file systems.
1935 *
1936 * This is based heavily on the implementation of same in XFS.
1937 */
1938struct buffered_dirent {
1939 u64 ino;
1940 loff_t offset;
1941 int namlen;
1942 unsigned int d_type;
1943 char name[];
1944};
1945
1946struct readdir_data {
1947 char *dirent;
1948 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001949 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001950};
1951
1952static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1953 loff_t offset, u64 ino, unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001954{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001955 struct readdir_data *buf = __buf;
1956 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1957 unsigned int reclen;
1958
1959 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001960 if (buf->used + reclen > PAGE_SIZE) {
1961 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001962 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001963 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001964
1965 de->namlen = namlen;
1966 de->offset = offset;
1967 de->ino = ino;
1968 de->d_type = d_type;
1969 memcpy(de->name, name, namlen);
1970 buf->used += reclen;
1971
1972 return 0;
1973}
1974
David Woodhouse2f9092e2009-04-20 23:18:37 +01001975static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1976 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001977{
1978 struct readdir_data buf;
1979 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001980 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001981 int size;
1982 loff_t offset;
David Woodhouse2628b762008-07-31 17:16:51 +01001983
David Woodhouse14f7dd62008-07-31 20:29:12 +01001984 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1985 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001986 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001987
David Woodhouse14f7dd62008-07-31 20:29:12 +01001988 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001989
1990 while (1) {
David Woodhouse2f9092e2009-04-20 23:18:37 +01001991 struct inode *dir_inode = file->f_path.dentry->d_inode;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001992 unsigned int reclen;
1993
Doug Nazarb726e922008-11-05 06:16:28 -05001994 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001995 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001996 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001997
1998 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
Al Viro53c9c5c2008-08-24 07:29:52 -04001999 if (buf.full)
2000 host_err = 0;
2001
2002 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01002003 break;
2004
2005 size = buf.used;
2006
2007 if (!size)
2008 break;
2009
David Woodhouse2f9092e2009-04-20 23:18:37 +01002010 /*
2011 * Various filldir functions may end up calling back into
2012 * lookup_one_len() and the file system's ->lookup() method.
2013 * These expect i_mutex to be held, as it would within readdir.
2014 */
2015 host_err = mutex_lock_killable(&dir_inode->i_mutex);
2016 if (host_err)
2017 break;
2018
David Woodhouse14f7dd62008-07-31 20:29:12 +01002019 de = (struct buffered_dirent *)buf.dirent;
2020 while (size > 0) {
2021 offset = de->offset;
2022
2023 if (func(cdp, de->name, de->namlen, de->offset,
2024 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01002025 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002026
2027 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01002028 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002029
2030 reclen = ALIGN(sizeof(*de) + de->namlen,
2031 sizeof(u64));
2032 size -= reclen;
2033 de = (struct buffered_dirent *)((char *)de + reclen);
2034 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01002035 mutex_unlock(&dir_inode->i_mutex);
2036 if (size > 0) /* We bailed out early */
2037 break;
2038
David Woodhousec002a6c2008-08-17 17:21:18 +01002039 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002040 }
2041
David Woodhouse14f7dd62008-07-31 20:29:12 +01002042 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01002043
2044 if (host_err)
2045 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002046
2047 *offsetp = offset;
2048 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01002049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051/*
2052 * Read entries from a directory.
2053 * The NFSv3/4 verifier we ignore for now.
2054 */
Al Viro6264d692006-10-19 23:28:58 -07002055__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
NeilBrowna0ad13e2007-01-26 00:57:10 -08002057 struct readdir_cd *cdp, filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Al Viro6264d692006-10-19 23:28:58 -07002059 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct file *file;
2061 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04002062 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Bernd Schubert06effdb2012-03-18 22:44:50 -04002064 /* NFSv2 only supports 32 bit cookies */
2065 if (rqstp->rq_vers > 2)
2066 may_flags |= NFSD_MAY_64BIT_COOKIE;
2067
2068 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (err)
2070 goto out;
2071
2072 offset = vfs_llseek(file, offset, 0);
2073 if (offset < 0) {
2074 err = nfserrno((int)offset);
2075 goto out_close;
2076 }
2077
David Woodhouse14f7dd62008-07-31 20:29:12 +01002078 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if (err == nfserr_eof || err == nfserr_toosmall)
2081 err = nfs_ok; /* can still be found in ->err */
2082out_close:
2083 nfsd_close(file);
2084out:
2085 return err;
2086}
2087
2088/*
2089 * Get file system stats
2090 * N.B. After this call fhp needs an fh_put
2091 */
Al Viro6264d692006-10-19 23:28:58 -07002092__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04002093nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002095 __be32 err;
2096
2097 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02002098 if (!err) {
2099 struct path path = {
2100 .mnt = fhp->fh_export->ex_path.mnt,
2101 .dentry = fhp->fh_dentry,
2102 };
2103 if (vfs_statfs(&path, stat))
2104 err = nfserr_io;
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return err;
2107}
2108
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002109static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002110{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002111 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114/*
2115 * Check for a user's access permissions to this inode.
2116 */
Al Viro6264d692006-10-19 23:28:58 -07002117__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07002118nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2119 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct inode *inode = dentry->d_inode;
2122 int err;
2123
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04002124 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return 0;
2126#if 0
2127 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2128 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002129 (acc & NFSD_MAY_READ)? " read" : "",
2130 (acc & NFSD_MAY_WRITE)? " write" : "",
2131 (acc & NFSD_MAY_EXEC)? " exec" : "",
2132 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2133 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2134 (acc & NFSD_MAY_LOCK)? " lock" : "",
2135 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 inode->i_mode,
2137 IS_IMMUTABLE(inode)? " immut" : "",
2138 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002139 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002141 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142#endif
2143
2144 /* Normally we reject any write/sattr etc access on a read-only file
2145 * system. But if it is IRIX doing check on write-access for a
2146 * device special file, we ignore rofs.
2147 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002148 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2149 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002150 if (exp_rdonly(rqstp, exp) ||
2151 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002153 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return nfserr_perm;
2155 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002156 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return nfserr_perm;
2158
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002159 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /* If we cannot rely on authentication in NLM requests,
2161 * just allow locks, otherwise require read permission, or
2162 * ownership
2163 */
2164 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2165 return 0;
2166 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002167 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169 /*
2170 * The file owner always gets access permission for accesses that
2171 * would normally be checked at open time. This is to make
2172 * file access work even when the client has done a fchmod(fd, 0).
2173 *
2174 * However, `cp foo bar' should fail nevertheless when bar is
2175 * readonly. A sensible way to do this might be to reject all
2176 * attempts to truncate a read-only file, because a creat() call
2177 * always implies file truncation.
2178 * ... but this isn't really fair. A process may reasonably call
2179 * ftruncate on an open file descriptor on a file with perm 000.
2180 * We must trust the client to do permission checking - using "ACCESS"
2181 * with NFSv3.
2182 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002183 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
David Howells5cc0a842008-11-14 10:38:58 +11002184 inode->i_uid == current_fsuid())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return 0;
2186
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002187 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002188 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 /* Allow read access to binaries even when mode 111 */
2191 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002192 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2193 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002194 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 return err? nfserrno(err) : 0;
2197}
2198
2199void
2200nfsd_racache_shutdown(void)
2201{
Jeff Layton54a66e52008-08-13 22:03:27 -04002202 struct raparms *raparm, *last_raparm;
2203 unsigned int i;
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002206
2207 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2208 raparm = raparm_hash[i].pb_head;
2209 while(raparm) {
2210 last_raparm = raparm;
2211 raparm = raparm->p_next;
2212 kfree(last_raparm);
2213 }
2214 raparm_hash[i].pb_head = NULL;
2215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217/*
2218 * Initialize readahead param cache
2219 */
2220int
2221nfsd_racache_init(int cache_size)
2222{
2223 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002224 int j = 0;
2225 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002226 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Greg Banksfce14562006-10-04 02:15:49 -07002228
Jeff Layton54a66e52008-08-13 22:03:27 -04002229 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002231 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2232 if (nperbucket < 2)
2233 nperbucket = 2;
2234 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002235
2236 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002237
2238 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002239 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002240
2241 raparm = &raparm_hash[i].pb_head;
2242 for (j = 0; j < nperbucket; j++) {
2243 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2244 if (!*raparm)
2245 goto out_nomem;
2246 raparm = &(*raparm)->p_next;
2247 }
2248 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002249 }
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 nfsdstats.ra_size = cache_size;
2252 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002253
2254out_nomem:
2255 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2256 nfsd_racache_shutdown();
2257 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002259
2260#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2261struct posix_acl *
2262nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2263{
2264 struct inode *inode = fhp->fh_dentry->d_inode;
2265 char *name;
2266 void *value = NULL;
2267 ssize_t size;
2268 struct posix_acl *acl;
2269
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002270 if (!IS_POSIXACL(inode))
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002271 return ERR_PTR(-EOPNOTSUPP);
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002272
2273 switch (type) {
2274 case ACL_TYPE_ACCESS:
2275 name = POSIX_ACL_XATTR_ACCESS;
2276 break;
2277 case ACL_TYPE_DEFAULT:
2278 name = POSIX_ACL_XATTR_DEFAULT;
2279 break;
2280 default:
2281 return ERR_PTR(-EOPNOTSUPP);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002282 }
2283
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002284 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2285 if (size < 0)
2286 return ERR_PTR(size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002287
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002288 acl = posix_acl_from_xattr(value, size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002289 kfree(value);
2290 return acl;
2291}
2292
2293int
2294nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2295{
2296 struct inode *inode = fhp->fh_dentry->d_inode;
2297 char *name;
2298 void *value = NULL;
2299 size_t size;
2300 int error;
2301
Al Viroacfa4382008-12-04 10:06:33 -05002302 if (!IS_POSIXACL(inode) ||
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002303 !inode->i_op->setxattr || !inode->i_op->removexattr)
2304 return -EOPNOTSUPP;
2305 switch(type) {
2306 case ACL_TYPE_ACCESS:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002307 name = POSIX_ACL_XATTR_ACCESS;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002308 break;
2309 case ACL_TYPE_DEFAULT:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002310 name = POSIX_ACL_XATTR_DEFAULT;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002311 break;
2312 default:
2313 return -EOPNOTSUPP;
2314 }
2315
2316 if (acl && acl->a_count) {
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002317 size = posix_acl_xattr_size(acl->a_count);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002318 value = kmalloc(size, GFP_KERNEL);
2319 if (!value)
2320 return -ENOMEM;
Florin Malita9ccfc292006-05-20 14:59:58 -07002321 error = posix_acl_to_xattr(acl, value, size);
2322 if (error < 0)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002323 goto getout;
Florin Malita9ccfc292006-05-20 14:59:58 -07002324 size = error;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002325 } else
2326 size = 0;
2327
Al Virobad0dcf2011-11-23 12:03:18 -05002328 error = fh_want_write(fhp);
Dave Hansen18f335a2008-02-15 14:37:38 -08002329 if (error)
2330 goto getout;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002331 if (size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002332 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002333 else {
2334 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2335 error = 0;
2336 else {
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002337 error = vfs_removexattr(fhp->fh_dentry, name);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002338 if (error == -ENODATA)
2339 error = 0;
2340 }
2341 }
Al Virobad0dcf2011-11-23 12:03:18 -05002342 fh_drop_write(fhp);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002343
2344getout:
2345 kfree(value);
2346 return error;
2347}
2348#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */