blob: 638306219ae80b2c64996c1be9974092ef9f34eb [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;
J. Bruce Fields367bce42014-02-24 14:59:47 -0500409 bool get_write_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int size_change = 0;
411
412 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200413 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (iap->ia_valid & ATTR_SIZE)
415 ftype = S_IFREG;
416
J. Bruce Fields367bce42014-02-24 14:59:47 -0500417 /* Callers that do fh_verify should do the fh_want_write: */
418 get_write_count = !fhp->fh_dentry;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Get inode */
421 err = fh_verify(rqstp, fhp, ftype, accmode);
NeilBrown15b7a1b2005-11-07 01:00:23 -0800422 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 goto out;
J. Bruce Fields367bce42014-02-24 14:59:47 -0500424 if (get_write_count) {
425 host_err = fh_want_write(fhp);
426 if (host_err)
427 return nfserrno(host_err);
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 dentry = fhp->fh_dentry;
431 inode = dentry->d_inode;
432
NeilBrown15b7a1b2005-11-07 01:00:23 -0800433 /* Ignore any mode updates on symlinks */
434 if (S_ISLNK(inode->i_mode))
435 iap->ia_valid &= ~ATTR_MODE;
436
437 if (!iap->ia_valid)
438 goto out;
439
Christoph Hellwig6ee4fd22013-11-18 05:07:30 -0800440 nfsd_sanitize_attrs(inode, iap);
441
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000442 /*
Christoph Hellwig6ee4fd22013-11-18 05:07:30 -0800443 * The size case is special, it changes the file in addition to the
444 * attributes.
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (iap->ia_valid & ATTR_SIZE) {
Christoph Hellwig6ee4fd22013-11-18 05:07:30 -0800447 err = nfsd_get_write_access(rqstp, fhp, iap);
448 if (err)
449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 size_change = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 iap->ia_valid |= ATTR_CTIME;
454
Christoph Hellwig107bb3c2013-11-18 05:07:47 -0800455 if (check_guard && guardtime != inode->i_ctime.tv_sec) {
456 err = nfserr_notsync;
457 goto out_put_write_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Christoph Hellwig107bb3c2013-11-18 05:07:47 -0800459
460 host_err = nfsd_break_lease(inode);
461 if (host_err)
462 goto out_put_write_access_nfserror;
463
464 fh_lock(fhp);
465 host_err = notify_change(dentry, iap);
466 fh_unlock(fhp);
467
468out_put_write_access_nfserror:
469 err = nfserrno(host_err);
470out_put_write_access:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (size_change)
472 put_write_access(inode);
473 if (!err)
Christoph Hellwigb160fda2010-06-01 21:59:18 +0200474 commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475out:
476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800479#if defined(CONFIG_NFSD_V2_ACL) || \
480 defined(CONFIG_NFSD_V3_ACL) || \
481 defined(CONFIG_NFSD_V4)
482static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
483{
484 ssize_t buflen;
Krishna Kumar6c6a4262008-10-22 14:48:36 +0530485 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800487 buflen = vfs_getxattr(dentry, key, NULL, 0);
488 if (buflen <= 0)
489 return buflen;
490
491 *buf = kmalloc(buflen, GFP_KERNEL);
492 if (!*buf)
493 return -ENOMEM;
494
Krishna Kumar6c6a4262008-10-22 14:48:36 +0530495 ret = vfs_getxattr(dentry, key, *buf, buflen);
496 if (ret < 0)
497 kfree(*buf);
498 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800499}
500#endif
501
502#if defined(CONFIG_NFSD_V4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static int
504set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
505{
506 int len;
507 size_t buflen;
508 char *buf = NULL;
509 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 buflen = posix_acl_xattr_size(pacl->a_count);
512 buf = kmalloc(buflen, GFP_KERNEL);
513 error = -ENOMEM;
514 if (buf == NULL)
515 goto out;
516
517 len = posix_acl_to_xattr(pacl, buf, buflen);
518 if (len < 0) {
519 error = len;
520 goto out;
521 }
522
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800523 error = vfs_setxattr(dentry, key, buf, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524out:
525 kfree(buf);
526 return error;
527}
528
Al Viro6264d692006-10-19 23:28:58 -0700529__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
531 struct nfs4_acl *acl)
532{
Al Viro6264d692006-10-19 23:28:58 -0700533 __be32 error;
534 int host_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct dentry *dentry;
536 struct inode *inode;
537 struct posix_acl *pacl = NULL, *dpacl = NULL;
538 unsigned int flags = 0;
539
540 /* Get inode */
J. Bruce Fieldse281d812011-08-15 16:57:07 -0400541 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (error)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700543 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 dentry = fhp->fh_dentry;
546 inode = dentry->d_inode;
547 if (S_ISDIR(inode->i_mode))
548 flags = NFS4_ACL_DIR;
549
Al Viro6264d692006-10-19 23:28:58 -0700550 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
551 if (host_error == -EINVAL) {
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700552 return nfserr_attrnotsupp;
Al Viro6264d692006-10-19 23:28:58 -0700553 } else if (host_error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto out_nfserr;
555
Al Viro6264d692006-10-19 23:28:58 -0700556 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
557 if (host_error < 0)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700558 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700560 if (S_ISDIR(inode->i_mode))
Al Viro6264d692006-10-19 23:28:58 -0700561 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700563out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 posix_acl_release(pacl);
565 posix_acl_release(dpacl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566out_nfserr:
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800567 if (host_error == -EOPNOTSUPP)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700568 return nfserr_attrnotsupp;
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800569 else
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700570 return nfserrno(host_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573static struct posix_acl *
574_get_posix_acl(struct dentry *dentry, char *key)
575{
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800576 void *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct posix_acl *pacl = NULL;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800578 int buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800580 buflen = nfsd_getxattr(dentry, key, &buf);
581 if (!buflen)
582 buflen = -ENODATA;
583 if (buflen <= 0)
584 return ERR_PTR(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 pacl = posix_acl_from_xattr(buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 kfree(buf);
588 return pacl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591int
592nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
593{
594 struct inode *inode = dentry->d_inode;
595 int error = 0;
596 struct posix_acl *pacl = NULL, *dpacl = NULL;
597 unsigned int flags = 0;
598
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700599 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
601 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
602 if (IS_ERR(pacl)) {
603 error = PTR_ERR(pacl);
604 pacl = NULL;
605 goto out;
606 }
607
608 if (S_ISDIR(inode->i_mode)) {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700609 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
611 dpacl = NULL;
612 else if (IS_ERR(dpacl)) {
613 error = PTR_ERR(dpacl);
614 dpacl = NULL;
615 goto out;
616 }
617 flags = NFS4_ACL_DIR;
618 }
619
620 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
621 if (IS_ERR(*acl)) {
622 error = PTR_ERR(*acl);
623 *acl = NULL;
624 }
625 out:
626 posix_acl_release(pacl);
627 posix_acl_release(dpacl);
628 return error;
629}
630
Chuck Lever9b4146e2012-01-04 16:26:43 -0500631/*
632 * NFS junction information is stored in an extended attribute.
633 */
634#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
635
636/**
637 * nfsd4_is_junction - Test if an object could be an NFS junction
638 *
639 * @dentry: object to test
640 *
641 * Returns 1 if "dentry" appears to contain NFS junction information.
642 * Otherwise 0 is returned.
643 */
Trond Myklebust11fcee02011-09-12 19:37:26 -0400644int nfsd4_is_junction(struct dentry *dentry)
645{
646 struct inode *inode = dentry->d_inode;
647
648 if (inode == NULL)
649 return 0;
650 if (inode->i_mode & S_IXUGO)
651 return 0;
652 if (!(inode->i_mode & S_ISVTX))
653 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500654 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee02011-09-12 19:37:26 -0400655 return 0;
656 return 1;
657}
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400658#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660#ifdef CONFIG_NFSD_V3
661/*
662 * Check server access rights to a file system object
663 */
664struct accessmap {
665 u32 access;
666 int how;
667};
668static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200669 { NFS3_ACCESS_READ, NFSD_MAY_READ },
670 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
671 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
672 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 { 0, 0 }
675};
676
677static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200678 { NFS3_ACCESS_READ, NFSD_MAY_READ },
679 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
680 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
681 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
682 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 { 0, 0 }
685};
686
687static struct accessmap nfs3_anyaccess[] = {
688 /* Some clients - Solaris 2.6 at least, make an access call
689 * to the server to check for access for things like /dev/null
690 * (which really, the server doesn't care about). So
691 * We provide simple access checking for them, looking
692 * mainly at mode bits, and we make sure to ignore read-only
693 * filesystem checks
694 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200695 { NFS3_ACCESS_READ, NFSD_MAY_READ },
696 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
697 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
698 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 { 0, 0 }
701};
702
Al Viro6264d692006-10-19 23:28:58 -0700703__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
705{
706 struct accessmap *map;
707 struct svc_export *export;
708 struct dentry *dentry;
709 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700710 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200712 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (error)
714 goto out;
715
716 export = fhp->fh_export;
717 dentry = fhp->fh_dentry;
718
719 if (S_ISREG(dentry->d_inode->i_mode))
720 map = nfs3_regaccess;
721 else if (S_ISDIR(dentry->d_inode->i_mode))
722 map = nfs3_diraccess;
723 else
724 map = nfs3_anyaccess;
725
726
727 query = *access;
728 for (; map->access; map++) {
729 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700730 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 sresult |= map->access;
733
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700734 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 switch (err2) {
736 case nfs_ok:
737 result |= map->access;
738 break;
739
740 /* the following error codes just mean the access was not allowed,
741 * rather than an error occurred */
742 case nfserr_rofs:
743 case nfserr_acces:
744 case nfserr_perm:
745 /* simply don't "or" in the access bit. */
746 break;
747 default:
748 error = err2;
749 goto out;
750 }
751 }
752 }
753 *access = result;
754 if (supported)
755 *supported = sresult;
756
757 out:
758 return error;
759}
760#endif /* CONFIG_NFSD_V3 */
761
J. Bruce Fields105f4622011-06-07 11:50:23 -0400762static int nfsd_open_break_lease(struct inode *inode, int access)
763{
764 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
J. Bruce Fields105f4622011-06-07 11:50:23 -0400766 if (access & NFSD_MAY_NOT_BREAK_LEASE)
767 return 0;
768 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
769 return break_lease(inode, mode | O_NONBLOCK);
770}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772/*
773 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400774 * The may_flags argument indicates the type of open (read/write/lock)
775 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * N.B. After this call fhp needs an fh_put
777 */
Al Viro6264d692006-10-19 23:28:58 -0700778__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400779nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400780 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct dentry *dentry;
783 struct inode *inode;
Al Viro6264d692006-10-19 23:28:58 -0700784 int flags = O_RDONLY|O_LARGEFILE;
785 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400786 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
David Howellse0e81732009-09-02 09:13:40 +0100788 validate_process_creds();
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * If we get here, then the client has already done an "open",
792 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
793 * in case a chmod has now revoked permission.
794 */
Bernd Schubert999448a2012-03-18 22:44:49 -0400795 err = fh_verify(rqstp, fhp, type, may_flags | NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (err)
797 goto out;
798
799 dentry = fhp->fh_dentry;
800 inode = dentry->d_inode;
801
802 /* Disallow write access to files with the append-only bit set
803 * or any access when mandatory locking enabled
804 */
805 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400806 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400808 /*
809 * We must ignore files (but only files) which might have mandatory
810 * locks on them because there is no way to know if the accesser has
811 * the lock.
812 */
813 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto out;
815
816 if (!inode->i_fop)
817 goto out;
818
Bernd Schubert999448a2012-03-18 22:44:49 -0400819 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700820 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto out_nfserr;
822
Bernd Schubert999448a2012-03-18 22:44:49 -0400823 if (may_flags & NFSD_MAY_WRITE) {
824 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700825 flags = O_RDWR|O_LARGEFILE;
826 else
827 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Jan Blunck54775492008-02-14 19:38:39 -0800829 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
David Howells033a6662009-07-02 14:35:32 +0100830 flags, current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (IS_ERR(*filp))
Al Viro6264d692006-10-19 23:28:58 -0700832 host_err = PTR_ERR(*filp);
Bernd Schubert06effdb2012-03-18 22:44:50 -0400833 else {
Bernd Schubert999448a2012-03-18 22:44:49 -0400834 host_err = ima_file_check(*filp, may_flags);
835
Bernd Schubert06effdb2012-03-18 22:44:50 -0400836 if (may_flags & NFSD_MAY_64BIT_COOKIE)
837 (*filp)->f_mode |= FMODE_64BITHASH;
838 else
839 (*filp)->f_mode |= FMODE_32BITHASH;
840 }
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700843 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844out:
David Howellse0e81732009-09-02 09:13:40 +0100845 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return err;
847}
848
849/*
850 * Close a file.
851 */
852void
853nfsd_close(struct file *filp)
854{
855 fput(filp);
856}
857
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500858/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * Obtain the readahead parameters for the file
860 * specified by (dev, ino).
861 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863static inline struct raparms *
864nfsd_get_raparms(dev_t dev, ino_t ino)
865{
866 struct raparms *ra, **rap, **frap = NULL;
867 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700868 unsigned int hash;
869 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Greg Banksfce14562006-10-04 02:15:49 -0700871 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
872 rab = &raparm_hash[hash];
873
874 spin_lock(&rab->pb_lock);
875 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (ra->p_ino == ino && ra->p_dev == dev)
877 goto found;
878 depth++;
879 if (ra->p_count == 0)
880 frap = rap;
881 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300882 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700884 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return NULL;
886 }
887 rap = frap;
888 ra = *frap;
889 ra->p_dev = dev;
890 ra->p_ino = ino;
891 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700892 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893found:
Greg Banksfce14562006-10-04 02:15:49 -0700894 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700896 ra->p_next = rab->pb_head;
897 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 ra->p_count++;
900 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700901 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return ra;
903}
904
905/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200906 * Grab and keep cached pages associated with a file in the svc_rqst
907 * so that they can be passed to the network sendmsg/sendpage routines
908 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
910static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200911nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
912 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Jens Axboecf8208d2007-06-12 21:22:14 +0200914 struct svc_rqst *rqstp = sd->u.data;
NeilBrown44524352006-10-04 02:15:46 -0700915 struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
Jens Axboecf8208d2007-06-12 21:22:14 +0200916 struct page *page = buf->page;
917 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200918
919 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (rqstp->rq_res.page_len == 0) {
922 get_page(page);
NeilBrown44524352006-10-04 02:15:46 -0700923 put_page(*pp);
924 *pp = page;
925 rqstp->rq_resused++;
Jens Axboecf8208d2007-06-12 21:22:14 +0200926 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700928 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 get_page(page);
NeilBrown250f3912007-01-26 00:56:59 -0800930 if (*pp)
931 put_page(*pp);
NeilBrown44524352006-10-04 02:15:46 -0700932 *pp = page;
933 rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700935 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return size;
939}
940
Jens Axboecf8208d2007-06-12 21:22:14 +0200941static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
942 struct splice_desc *sd)
943{
944 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
945}
946
Al Viro6264d692006-10-19 23:28:58 -0700947static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
949 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
950{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700952 __be32 err;
953 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 err = nfserr_perm;
Dave Hansena8754be2007-10-16 23:31:15 -0700956
Jens Axboecf8208d2007-06-12 21:22:14 +0200957 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
958 struct splice_desc sd = {
959 .len = 0,
960 .total_len = *count,
961 .pos = offset,
962 .u.data = rqstp,
963 };
964
Jens Axboe4fbef202007-07-13 22:42:20 +0200965 rqstp->rq_resused = 1;
Jens Axboecf8208d2007-06-12 21:22:14 +0200966 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 } else {
968 oldfs = get_fs();
969 set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -0700970 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 set_fs(oldfs);
972 }
973
Al Viro6264d692006-10-19 23:28:58 -0700974 if (host_err >= 0) {
975 nfsdstats.io_read += host_err;
976 *count = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 err = 0;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500978 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 } else
Al Viro6264d692006-10-19 23:28:58 -0700980 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return err;
982}
983
Neil Brown9f708e42006-01-06 00:19:59 -0800984static void kill_suid(struct dentry *dentry)
985{
986 struct iattr ia;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700987 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Neil Brown9f708e42006-01-06 00:19:59 -0800988
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800989 mutex_lock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800990 notify_change(dentry, &ia);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800991 mutex_unlock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800992}
993
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700994/*
995 * Gathered writes: If another process is currently writing to the file,
996 * there's a high chance this is another nfsd (triggered by a bulk write
997 * from a client's biod). Rather than syncing the file with each write
998 * request, we sleep for 10 msec.
999 *
1000 * I don't know if this roughly approximates C. Juszak's idea of
1001 * gathered writes, but it's a nice and simple solution (IMHO), and it
1002 * seems to work:-)
1003 *
1004 * Note: we do this only in the NFSv2 case, since v3 and higher have a
1005 * better tool (separate unstable writes and commits) for solving this
1006 * problem.
1007 */
1008static int wait_for_concurrent_writes(struct file *file)
1009{
1010 struct inode *inode = file->f_path.dentry->d_inode;
1011 static ino_t last_ino;
1012 static dev_t last_dev;
1013 int err = 0;
1014
1015 if (atomic_read(&inode->i_writecount) > 1
1016 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1017 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1018 msleep(10);
1019 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1020 }
1021
1022 if (inode->i_state & I_DIRTY) {
1023 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001024 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001025 }
1026 last_ino = inode->i_ino;
1027 last_dev = inode->i_sb->s_dev;
1028 return err;
1029}
1030
Al Viro6264d692006-10-19 23:28:58 -07001031static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1033 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -05001034 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct svc_export *exp;
1037 struct dentry *dentry;
1038 struct inode *inode;
1039 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001040 __be32 err = 0;
1041 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001043 int use_wgather;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001045 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 inode = dentry->d_inode;
1047 exp = fhp->fh_export;
1048
1049 /*
1050 * Request sync writes if
1051 * - the sync export option has been set, or
1052 * - the client requested O_SYNC behavior (NFSv3 feature).
1053 * - The file system doesn't support fsync().
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001054 * When NFSv2 gathered writes have been configured for this volume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * flushing the data to disk is handled separately below.
1056 */
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001057 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Harvey Harrison3ba15142008-02-20 12:49:02 -08001059 if (!file->f_op->fsync) {/* COMMIT3 cannot work */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 stable = 2;
1061 *stablep = 2; /* FILE_SYNC */
1062 }
1063
1064 if (!EX_ISSYNC(exp))
1065 stable = 0;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001066 if (stable && !use_wgather) {
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001067 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 file->f_flags |= O_SYNC;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001069 spin_unlock(&file->f_lock);
1070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* Write the data. */
1073 oldfs = get_fs(); set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -07001074 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001076 if (host_err < 0)
1077 goto out_nfserr;
1078 *cnt = host_err;
1079 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -05001080 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /* clear setuid/setgid flag after write */
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001083 if (inode->i_mode & (S_ISUID | S_ISGID))
Neil Brown9f708e42006-01-06 00:19:59 -08001084 kill_suid(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001086 if (stable && use_wgather)
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001087 host_err = wait_for_concurrent_writes(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001089out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001090 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001091 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001093 else
Al Viro6264d692006-10-19 23:28:58 -07001094 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return err;
1096}
1097
1098/*
1099 * Read data from a file. count must contain the requested read count
1100 * on entry. On return, *count contains the number of bytes actually read.
1101 * N.B. After this call fhp needs an fh_put
1102 */
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001103__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001104 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1105{
1106 struct file *file;
1107 struct inode *inode;
1108 struct raparms *ra;
1109 __be32 err;
1110
1111 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1112 if (err)
1113 return err;
1114
1115 inode = file->f_path.dentry->d_inode;
1116
1117 /* Get readahead parameters */
1118 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1119
1120 if (ra && ra->p_set)
1121 file->f_ra = ra->p_ra;
1122
1123 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1124
1125 /* Write back readahead params */
1126 if (ra) {
1127 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1128 spin_lock(&rab->pb_lock);
1129 ra->p_ra = file->f_ra;
1130 ra->p_set = 1;
1131 ra->p_count--;
1132 spin_unlock(&rab->pb_lock);
1133 }
1134
1135 nfsd_close(file);
1136 return err;
1137}
1138
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001139/* As above, but use the provided file descriptor. */
Al Viro6264d692006-10-19 23:28:58 -07001140__be32
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001141nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 loff_t offset, struct kvec *vec, int vlen,
1143 unsigned long *count)
1144{
Al Viro6264d692006-10-19 23:28:58 -07001145 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001148 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001149 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (err)
1151 goto out;
1152 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001153 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1154 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155out:
1156 return err;
1157}
1158
1159/*
1160 * Write data to a file.
1161 * The stable flag requests synchronous writes.
1162 * N.B. After this call fhp needs an fh_put
1163 */
Al Viro6264d692006-10-19 23:28:58 -07001164__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001166 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int *stablep)
1168{
Al Viro6264d692006-10-19 23:28:58 -07001169 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001172 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001173 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (err)
1175 goto out;
1176 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1177 stablep);
1178 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001179 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (err)
1181 goto out;
1182
1183 if (cnt)
1184 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1185 cnt, stablep);
1186 nfsd_close(file);
1187 }
1188out:
1189 return err;
1190}
1191
1192#ifdef CONFIG_NFSD_V3
1193/*
1194 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001195 *
1196 * Note: we only guarantee that data that lies within the range specified
1197 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 *
1199 * Unfortunately we cannot lock the file to make sure we return full WCC
1200 * data to the client, as locking happens lower down in the filesystem.
1201 */
Al Viro6264d692006-10-19 23:28:58 -07001202__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1204 loff_t offset, unsigned long count)
1205{
1206 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001207 loff_t end = LLONG_MAX;
1208 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Trond Myklebustaa696a62010-01-29 16:44:25 -05001210 if (offset < 0)
1211 goto out;
1212 if (count != 0) {
1213 end = offset + (loff_t)count - 1;
1214 if (end < offset)
1215 goto out;
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Jeff Layton91885252010-03-19 08:06:28 -04001218 err = nfsd_open(rqstp, fhp, S_IFREG,
1219 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001220 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001223 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001224
1225 if (err2 != -EINVAL)
1226 err = nfserrno(err2);
1227 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
1231 nfsd_close(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001232out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return err;
1234}
1235#endif /* CONFIG_NFSD_V3 */
1236
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001237static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001238nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1239 struct iattr *iap)
1240{
1241 /*
1242 * Mode has already been set earlier in create:
1243 */
1244 iap->ia_valid &= ~ATTR_MODE;
1245 /*
1246 * Setting uid/gid works only for root. Irix appears to
1247 * send along the gid on create when it tries to implement
1248 * setgid directories via NFS:
1249 */
David Howells5cc0a842008-11-14 10:38:58 +11001250 if (current_fsuid() != 0)
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001251 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1252 if (iap->ia_valid)
1253 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1254 return 0;
1255}
1256
wengang wang4ac35c22009-02-10 11:27:51 +08001257/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1258 * setting size to 0 may fail for some specific file systems by the permission
1259 * checking which requires WRITE permission but the mode is 000.
1260 * we ignore the resizing(to 0) on the just new created file, since the size is
1261 * 0 after file created.
1262 *
1263 * call this only after vfs_create() is called.
1264 * */
1265static void
1266nfsd_check_ignore_resizing(struct iattr *iap)
1267{
1268 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1269 iap->ia_valid &= ~ATTR_SIZE;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
1273 * Create a file (regular, directory, device, fifo); UNIX sockets
1274 * not yet implemented.
1275 * If the response fh has been verified, the parent directory should
1276 * already be locked. Note that the parent directory is left locked.
1277 *
1278 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1279 */
Al Viro6264d692006-10-19 23:28:58 -07001280__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1282 char *fname, int flen, struct iattr *iap,
1283 int type, dev_t rdev, struct svc_fh *resfhp)
1284{
1285 struct dentry *dentry, *dchild = NULL;
1286 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001287 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001288 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001289 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 err = nfserr_perm;
1292 if (!flen)
1293 goto out;
1294 err = nfserr_exist;
1295 if (isdotent(fname, flen))
1296 goto out;
1297
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001298 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (err)
1300 goto out;
1301
1302 dentry = fhp->fh_dentry;
1303 dirp = dentry->d_inode;
1304
1305 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001306 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 goto out;
1308 /*
1309 * Check whether the response file handle has been verified yet.
1310 * If it has, the parent directory should already be locked.
1311 */
1312 if (!resfhp->fh_dentry) {
1313 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001314 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001316 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (IS_ERR(dchild))
1318 goto out_nfserr;
1319 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1320 if (err)
1321 goto out;
1322 } else {
1323 /* called from nfsd_proc_create */
1324 dchild = dget(resfhp->fh_dentry);
1325 if (!fhp->fh_locked) {
1326 /* not actually possible */
1327 printk(KERN_ERR
1328 "nfsd_create: parent %s/%s not locked!\n",
1329 dentry->d_parent->d_name.name,
1330 dentry->d_name.name);
Al Virod75f2b92006-01-18 17:43:44 -08001331 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 goto out;
1333 }
1334 }
1335 /*
1336 * Make sure the child dentry is still negative ...
1337 */
1338 err = nfserr_exist;
1339 if (dchild->d_inode) {
1340 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1341 dentry->d_name.name, dchild->d_name.name);
1342 goto out;
1343 }
1344
1345 if (!(iap->ia_valid & ATTR_MODE))
1346 iap->ia_mode = 0;
1347 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1348
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001349 err = nfserr_inval;
1350 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1351 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1352 type);
1353 goto out;
1354 }
1355
Al Virobad0dcf2011-11-23 12:03:18 -05001356 host_err = fh_want_write(fhp);
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001357 if (host_err)
1358 goto out_nfserr;
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /*
1361 * Get the dir op function pointer.
1362 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001363 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 switch (type) {
1365 case S_IFREG:
Al Viro6264d692006-10-19 23:28:58 -07001366 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
wengang wang4ac35c22009-02-10 11:27:51 +08001367 if (!host_err)
1368 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 break;
1370 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001371 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 break;
1373 case S_IFCHR:
1374 case S_IFBLK:
1375 case S_IFIFO:
1376 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001377 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001379 }
1380 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001381 fh_drop_write(fhp);
Dave Hansen463c3192008-02-15 14:37:57 -08001382 goto out_nfserr;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Ben Myersf5019122010-02-17 14:05:11 -06001385 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Ben Myersf5019122010-02-17 14:05:11 -06001387 /*
1388 * nfsd_setattr already committed the child. Transactional filesystems
1389 * had a chance to commit changes for both parent and child
1390 * simultaneously making the following commit_metadata a noop.
1391 */
1392 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001393 if (err2)
1394 err = err2;
Al Virobad0dcf2011-11-23 12:03:18 -05001395 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /*
1397 * Update the file handle to get the new inode info.
1398 */
1399 if (!err)
1400 err = fh_update(resfhp);
1401out:
1402 if (dchild && !IS_ERR(dchild))
1403 dput(dchild);
1404 return err;
1405
1406out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001407 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 goto out;
1409}
1410
1411#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001412
1413static inline int nfsd_create_is_exclusive(int createmode)
1414{
1415 return createmode == NFS3_CREATE_EXCLUSIVE
1416 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001420 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 */
Al Viro6264d692006-10-19 23:28:58 -07001422__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001423do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 char *fname, int flen, struct iattr *iap,
1425 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001426 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct dentry *dentry, *dchild = NULL;
1429 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001430 __be32 err;
1431 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 err = nfserr_perm;
1435 if (!flen)
1436 goto out;
1437 err = nfserr_exist;
1438 if (isdotent(fname, flen))
1439 goto out;
1440 if (!(iap->ia_valid & ATTR_MODE))
1441 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001442 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (err)
1444 goto out;
1445
1446 dentry = fhp->fh_dentry;
1447 dirp = dentry->d_inode;
1448
1449 /* Get all the sanity checks out of the way before
1450 * we lock the parent. */
1451 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001452 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto out;
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001454 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /*
1457 * Compose the response file handle.
1458 */
1459 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001460 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (IS_ERR(dchild))
1462 goto out_nfserr;
1463
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001464 /* If file doesn't exist, check for permissions to create one */
1465 if (!dchild->d_inode) {
1466 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1467 if (err)
1468 goto out;
1469 }
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1472 if (err)
1473 goto out;
1474
Mi Jinlongac6721a2011-04-20 17:06:25 +08001475 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001476 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001477 * the high bit set, so just clear the high bits. If this is
1478 * ever changed to use different attrs for storing the
1479 * verifier, then do_open_lookup() will also need to be fixed
1480 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
1482 v_mtime = verifier[0]&0x7fffffff;
1483 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485
Al Virobad0dcf2011-11-23 12:03:18 -05001486 host_err = fh_want_write(fhp);
Dave Hansen463c3192008-02-15 14:37:57 -08001487 if (host_err)
1488 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (dchild->d_inode) {
1490 err = 0;
1491
1492 switch (createmode) {
1493 case NFS3_CREATE_UNCHECKED:
1494 if (! S_ISREG(dchild->d_inode->i_mode))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 else if (truncp) {
1497 /* in nfsv4, we need to treat this case a little
1498 * differently. we don't want to truncate the
1499 * file now; this would be wrong if the OPEN
1500 * fails for some other reason. furthermore,
1501 * if the size is nonzero, we should ignore it
1502 * according to spec!
1503 */
1504 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1505 }
1506 else {
1507 iap->ia_valid &= ATTR_SIZE;
1508 goto set_attr;
1509 }
1510 break;
1511 case NFS3_CREATE_EXCLUSIVE:
1512 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1513 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown59d6ce42012-12-07 15:40:55 -05001514 && dchild->d_inode->i_size == 0 ) {
1515 if (created)
1516 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 break;
Neil Brown59d6ce42012-12-07 15:40:55 -05001518 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001519 case NFS4_CREATE_EXCLUSIVE4_1:
1520 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1521 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown59d6ce42012-12-07 15:40:55 -05001522 && dchild->d_inode->i_size == 0 ) {
1523 if (created)
1524 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001525 goto set_attr;
Neil Brown59d6ce42012-12-07 15:40:55 -05001526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /* fallthru */
1528 case NFS3_CREATE_GUARDED:
1529 err = nfserr_exist;
1530 }
Al Virobad0dcf2011-11-23 12:03:18 -05001531 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 goto out;
1533 }
1534
Al Viro6264d692006-10-19 23:28:58 -07001535 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
Dave Hansen463c3192008-02-15 14:37:57 -08001536 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001537 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001539 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001540 if (created)
1541 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
wengang wang4ac35c22009-02-10 11:27:51 +08001543 nfsd_check_ignore_resizing(iap);
1544
Mi Jinlongac6721a2011-04-20 17:06:25 +08001545 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001546 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001548 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /* XXX someone who knows this better please fix it for nsec */
1550 iap->ia_mtime.tv_sec = v_mtime;
1551 iap->ia_atime.tv_sec = v_atime;
1552 iap->ia_mtime.tv_nsec = 0;
1553 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001557 err = nfsd_create_setattr(rqstp, resfhp, iap);
1558
1559 /*
1560 * nfsd_setattr already committed the child (and possibly also the parent).
1561 */
1562 if (!err)
1563 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001564
Al Virobad0dcf2011-11-23 12:03:18 -05001565 fh_drop_write(fhp);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001566 /*
1567 * Update the filehandle to get the new inode info.
1568 */
1569 if (!err)
1570 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 out:
1573 fh_unlock(fhp);
1574 if (dchild && !IS_ERR(dchild))
1575 dput(dchild);
1576 return err;
1577
1578 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001579 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 goto out;
1581}
1582#endif /* CONFIG_NFSD_V3 */
1583
1584/*
1585 * Read a symlink. On entry, *lenp must contain the maximum path length that
1586 * fits into the buffer. On return, it contains the true length.
1587 * N.B. After this call fhp needs an fh_put
1588 */
Al Viro6264d692006-10-19 23:28:58 -07001589__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1591{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 struct inode *inode;
1593 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001594 __be32 err;
1595 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001596 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001598 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (err)
1600 goto out;
1601
Al Viro68ac1232012-03-15 08:21:57 -04001602 path.mnt = fhp->fh_export->ex_path.mnt;
1603 path.dentry = fhp->fh_dentry;
1604 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001607 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 goto out;
1609
Al Viro68ac1232012-03-15 08:21:57 -04001610 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /* N.B. Why does this call need a get_fs()??
1612 * Remove the set_fs and watch the fireworks:-) --okir
1613 */
1614
1615 oldfs = get_fs(); set_fs(KERNEL_DS);
Al Viro68ac1232012-03-15 08:21:57 -04001616 host_err = inode->i_op->readlink(path.dentry, buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 set_fs(oldfs);
1618
Al Viro6264d692006-10-19 23:28:58 -07001619 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001621 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 err = 0;
1623out:
1624 return err;
1625
1626out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001627 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto out;
1629}
1630
1631/*
1632 * Create a symlink and look up its inode
1633 * N.B. After this call _both_ fhp and resfhp need an fh_put
1634 */
Al Viro6264d692006-10-19 23:28:58 -07001635__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1637 char *fname, int flen,
1638 char *path, int plen,
1639 struct svc_fh *resfhp,
1640 struct iattr *iap)
1641{
1642 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001643 __be32 err, cerr;
1644 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 err = nfserr_noent;
1647 if (!flen || !plen)
1648 goto out;
1649 err = nfserr_exist;
1650 if (isdotent(fname, flen))
1651 goto out;
1652
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001653 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (err)
1655 goto out;
1656 fh_lock(fhp);
1657 dentry = fhp->fh_dentry;
1658 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001659 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (IS_ERR(dnew))
1661 goto out_nfserr;
1662
Al Virobad0dcf2011-11-23 12:03:18 -05001663 host_err = fh_want_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001664 if (host_err)
1665 goto out_nfserr;
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (unlikely(path[plen] != 0)) {
1668 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1669 if (path_alloced == NULL)
Al Viro6264d692006-10-19 23:28:58 -07001670 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 else {
1672 strncpy(path_alloced, path, plen);
1673 path_alloced[plen] = 0;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001674 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 kfree(path_alloced);
1676 }
1677 } else
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001678 host_err = vfs_symlink(dentry->d_inode, dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001679 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001680 if (!err)
1681 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 fh_unlock(fhp);
1683
Al Virobad0dcf2011-11-23 12:03:18 -05001684 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1687 dput(dnew);
1688 if (err==0) err = cerr;
1689out:
1690 return err;
1691
1692out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001693 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 goto out;
1695}
1696
1697/*
1698 * Create a hardlink
1699 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1700 */
Al Viro6264d692006-10-19 23:28:58 -07001701__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1703 char *name, int len, struct svc_fh *tfhp)
1704{
1705 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001706 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001707 __be32 err;
1708 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001710 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (err)
1712 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001713 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (err)
1715 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001716 err = nfserr_isdir;
1717 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 err = nfserr_perm;
1720 if (!len)
1721 goto out;
1722 err = nfserr_exist;
1723 if (isdotent(name, len))
1724 goto out;
1725
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001726 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ddir = ffhp->fh_dentry;
1728 dirp = ddir->d_inode;
1729
1730 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001731 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (IS_ERR(dnew))
1733 goto out_nfserr;
1734
1735 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Al Virobad0dcf2011-11-23 12:03:18 -05001737 host_err = fh_want_write(tfhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001738 if (host_err) {
1739 err = nfserrno(host_err);
1740 goto out_dput;
1741 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001742 err = nfserr_noent;
1743 if (!dold->d_inode)
1744 goto out_drop_write;
1745 host_err = nfsd_break_lease(dold->d_inode);
Casey Bodley7d751f62011-06-03 12:21:23 -04001746 if (host_err) {
1747 err = nfserrno(host_err);
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001748 goto out_drop_write;
Casey Bodley7d751f62011-06-03 12:21:23 -04001749 }
Al Viro6264d692006-10-19 23:28:58 -07001750 host_err = vfs_link(dold, dirp, dnew);
1751 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001752 err = nfserrno(commit_metadata(ffhp));
1753 if (!err)
1754 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 } else {
Al Viro6264d692006-10-19 23:28:58 -07001756 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 err = nfserr_acces;
1758 else
Al Viro6264d692006-10-19 23:28:58 -07001759 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001761out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001762 fh_drop_write(tfhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001763out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001765out_unlock:
1766 fh_unlock(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767out:
1768 return err;
1769
1770out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001771 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001772 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
1775/*
1776 * Rename a file
1777 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1778 */
Al Viro6264d692006-10-19 23:28:58 -07001779__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1781 struct svc_fh *tfhp, char *tname, int tlen)
1782{
1783 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1784 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001785 __be32 err;
1786 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001788 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (err)
1790 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001791 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (err)
1793 goto out;
1794
1795 fdentry = ffhp->fh_dentry;
1796 fdir = fdentry->d_inode;
1797
1798 tdentry = tfhp->fh_dentry;
1799 tdir = tdentry->d_inode;
1800
1801 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
NeilBrowna56f3932006-06-30 01:56:10 -07001802 if (ffhp->fh_export != tfhp->fh_export)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto out;
1804
1805 err = nfserr_perm;
1806 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1807 goto out;
1808
1809 /* cannot use fh_lock as we need deadlock protective ordering
1810 * so do it by hand */
1811 trap = lock_rename(tdentry, fdentry);
1812 ffhp->fh_locked = tfhp->fh_locked = 1;
1813 fill_pre_wcc(ffhp);
1814 fill_pre_wcc(tfhp);
1815
1816 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001817 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (IS_ERR(odentry))
1819 goto out_nfserr;
1820
Al Viro6264d692006-10-19 23:28:58 -07001821 host_err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (!odentry->d_inode)
1823 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001824 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (odentry == trap)
1826 goto out_dput_old;
1827
1828 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001829 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (IS_ERR(ndentry))
1831 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001832 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (ndentry == trap)
1834 goto out_dput_new;
1835
Dave Hansen9079b1e2008-02-15 14:37:49 -08001836 host_err = -EXDEV;
1837 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1838 goto out_dput_new;
Al Virobad0dcf2011-11-23 12:03:18 -05001839 host_err = fh_want_write(ffhp);
Dave Hansen9079b1e2008-02-15 14:37:49 -08001840 if (host_err)
1841 goto out_dput_new;
1842
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001843 host_err = nfsd_break_lease(odentry->d_inode);
1844 if (host_err)
1845 goto out_drop_write;
J. Bruce Fields83f6b0c2011-02-06 16:46:30 -05001846 if (ndentry->d_inode) {
1847 host_err = nfsd_break_lease(ndentry->d_inode);
1848 if (host_err)
1849 goto out_drop_write;
1850 }
Al Viro6264d692006-10-19 23:28:58 -07001851 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
Ben Myersf5019122010-02-17 14:05:11 -06001852 if (!host_err) {
1853 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001854 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001855 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001857out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001858 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 out_dput_new:
1860 dput(ndentry);
1861 out_dput_old:
1862 dput(odentry);
1863 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001864 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* we cannot reply on fh_unlock on the two filehandles,
1867 * as that would do the wrong thing if the two directories
1868 * were the same, so again we do it by hand
1869 */
1870 fill_post_wcc(ffhp);
1871 fill_post_wcc(tfhp);
1872 unlock_rename(tdentry, fdentry);
1873 ffhp->fh_locked = tfhp->fh_locked = 0;
1874
1875out:
1876 return err;
1877}
1878
1879/*
1880 * Unlink a file or directory
1881 * N.B. After this call fhp needs an fh_put
1882 */
Al Viro6264d692006-10-19 23:28:58 -07001883__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1885 char *fname, int flen)
1886{
1887 struct dentry *dentry, *rdentry;
1888 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001889 __be32 err;
1890 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 err = nfserr_acces;
1893 if (!flen || isdotent(fname, flen))
1894 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001895 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (err)
1897 goto out;
1898
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001899 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 dentry = fhp->fh_dentry;
1901 dirp = dentry->d_inode;
1902
1903 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001904 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (IS_ERR(rdentry))
1906 goto out_nfserr;
1907
1908 if (!rdentry->d_inode) {
1909 dput(rdentry);
1910 err = nfserr_noent;
1911 goto out;
1912 }
1913
1914 if (!type)
1915 type = rdentry->d_inode->i_mode & S_IFMT;
1916
Al Virobad0dcf2011-11-23 12:03:18 -05001917 host_err = fh_want_write(fhp);
Dave Hansen06227532008-02-15 14:37:34 -08001918 if (host_err)
J. Bruce Fields541ce982011-01-14 20:00:02 -05001919 goto out_put;
Dave Hansen06227532008-02-15 14:37:34 -08001920
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001921 host_err = nfsd_break_lease(rdentry->d_inode);
1922 if (host_err)
J. Bruce Fields541ce982011-01-14 20:00:02 -05001923 goto out_drop_write;
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001924 if (type != S_IFDIR)
Al Viro6264d692006-10-19 23:28:58 -07001925 host_err = vfs_unlink(dirp, rdentry);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001926 else
Al Viro6264d692006-10-19 23:28:58 -07001927 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001928 if (!host_err)
1929 host_err = commit_metadata(fhp);
1930out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001931 fh_drop_write(fhp);
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001932out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 dput(rdentry);
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001936 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001937out:
1938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001942 * We do this buffering because we must not call back into the file
1943 * system's ->lookup() method from the filldir callback. That may well
1944 * deadlock a number of file systems.
1945 *
1946 * This is based heavily on the implementation of same in XFS.
1947 */
1948struct buffered_dirent {
1949 u64 ino;
1950 loff_t offset;
1951 int namlen;
1952 unsigned int d_type;
1953 char name[];
1954};
1955
1956struct readdir_data {
1957 char *dirent;
1958 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001959 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001960};
1961
1962static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1963 loff_t offset, u64 ino, unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001964{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001965 struct readdir_data *buf = __buf;
1966 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1967 unsigned int reclen;
1968
1969 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001970 if (buf->used + reclen > PAGE_SIZE) {
1971 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001972 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001973 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001974
1975 de->namlen = namlen;
1976 de->offset = offset;
1977 de->ino = ino;
1978 de->d_type = d_type;
1979 memcpy(de->name, name, namlen);
1980 buf->used += reclen;
1981
1982 return 0;
1983}
1984
David Woodhouse2f9092e2009-04-20 23:18:37 +01001985static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1986 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001987{
1988 struct readdir_data buf;
1989 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001990 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001991 int size;
1992 loff_t offset;
David Woodhouse2628b762008-07-31 17:16:51 +01001993
David Woodhouse14f7dd62008-07-31 20:29:12 +01001994 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1995 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001996 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001997
David Woodhouse14f7dd62008-07-31 20:29:12 +01001998 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001999
2000 while (1) {
David Woodhouse2f9092e2009-04-20 23:18:37 +01002001 struct inode *dir_inode = file->f_path.dentry->d_inode;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002002 unsigned int reclen;
2003
Doug Nazarb726e922008-11-05 06:16:28 -05002004 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01002005 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04002006 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002007
2008 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
Al Viro53c9c5c2008-08-24 07:29:52 -04002009 if (buf.full)
2010 host_err = 0;
2011
2012 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01002013 break;
2014
2015 size = buf.used;
2016
2017 if (!size)
2018 break;
2019
David Woodhouse2f9092e2009-04-20 23:18:37 +01002020 /*
2021 * Various filldir functions may end up calling back into
2022 * lookup_one_len() and the file system's ->lookup() method.
2023 * These expect i_mutex to be held, as it would within readdir.
2024 */
2025 host_err = mutex_lock_killable(&dir_inode->i_mutex);
2026 if (host_err)
2027 break;
2028
David Woodhouse14f7dd62008-07-31 20:29:12 +01002029 de = (struct buffered_dirent *)buf.dirent;
2030 while (size > 0) {
2031 offset = de->offset;
2032
2033 if (func(cdp, de->name, de->namlen, de->offset,
2034 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01002035 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002036
2037 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01002038 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002039
2040 reclen = ALIGN(sizeof(*de) + de->namlen,
2041 sizeof(u64));
2042 size -= reclen;
2043 de = (struct buffered_dirent *)((char *)de + reclen);
2044 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01002045 mutex_unlock(&dir_inode->i_mutex);
2046 if (size > 0) /* We bailed out early */
2047 break;
2048
David Woodhousec002a6c2008-08-17 17:21:18 +01002049 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002050 }
2051
David Woodhouse14f7dd62008-07-31 20:29:12 +01002052 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01002053
2054 if (host_err)
2055 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002056
2057 *offsetp = offset;
2058 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01002059}
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061/*
2062 * Read entries from a directory.
2063 * The NFSv3/4 verifier we ignore for now.
2064 */
Al Viro6264d692006-10-19 23:28:58 -07002065__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
NeilBrowna0ad13e2007-01-26 00:57:10 -08002067 struct readdir_cd *cdp, filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Al Viro6264d692006-10-19 23:28:58 -07002069 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 struct file *file;
2071 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04002072 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Bernd Schubert06effdb2012-03-18 22:44:50 -04002074 /* NFSv2 only supports 32 bit cookies */
2075 if (rqstp->rq_vers > 2)
2076 may_flags |= NFSD_MAY_64BIT_COOKIE;
2077
2078 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (err)
2080 goto out;
2081
2082 offset = vfs_llseek(file, offset, 0);
2083 if (offset < 0) {
2084 err = nfserrno((int)offset);
2085 goto out_close;
2086 }
2087
David Woodhouse14f7dd62008-07-31 20:29:12 +01002088 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 if (err == nfserr_eof || err == nfserr_toosmall)
2091 err = nfs_ok; /* can still be found in ->err */
2092out_close:
2093 nfsd_close(file);
2094out:
2095 return err;
2096}
2097
2098/*
2099 * Get file system stats
2100 * N.B. After this call fhp needs an fh_put
2101 */
Al Viro6264d692006-10-19 23:28:58 -07002102__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04002103nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002105 __be32 err;
2106
2107 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02002108 if (!err) {
2109 struct path path = {
2110 .mnt = fhp->fh_export->ex_path.mnt,
2111 .dentry = fhp->fh_dentry,
2112 };
2113 if (vfs_statfs(&path, stat))
2114 err = nfserr_io;
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return err;
2117}
2118
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002119static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002120{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002121 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002122}
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124/*
2125 * Check for a user's access permissions to this inode.
2126 */
Al Viro6264d692006-10-19 23:28:58 -07002127__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07002128nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2129 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 struct inode *inode = dentry->d_inode;
2132 int err;
2133
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04002134 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return 0;
2136#if 0
2137 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2138 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002139 (acc & NFSD_MAY_READ)? " read" : "",
2140 (acc & NFSD_MAY_WRITE)? " write" : "",
2141 (acc & NFSD_MAY_EXEC)? " exec" : "",
2142 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2143 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2144 (acc & NFSD_MAY_LOCK)? " lock" : "",
2145 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 inode->i_mode,
2147 IS_IMMUTABLE(inode)? " immut" : "",
2148 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002149 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002151 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152#endif
2153
2154 /* Normally we reject any write/sattr etc access on a read-only file
2155 * system. But if it is IRIX doing check on write-access for a
2156 * device special file, we ignore rofs.
2157 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002158 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2159 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002160 if (exp_rdonly(rqstp, exp) ||
2161 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002163 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return nfserr_perm;
2165 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002166 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return nfserr_perm;
2168
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002169 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /* If we cannot rely on authentication in NLM requests,
2171 * just allow locks, otherwise require read permission, or
2172 * ownership
2173 */
2174 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2175 return 0;
2176 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002177 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
2179 /*
2180 * The file owner always gets access permission for accesses that
2181 * would normally be checked at open time. This is to make
2182 * file access work even when the client has done a fchmod(fd, 0).
2183 *
2184 * However, `cp foo bar' should fail nevertheless when bar is
2185 * readonly. A sensible way to do this might be to reject all
2186 * attempts to truncate a read-only file, because a creat() call
2187 * always implies file truncation.
2188 * ... but this isn't really fair. A process may reasonably call
2189 * ftruncate on an open file descriptor on a file with perm 000.
2190 * We must trust the client to do permission checking - using "ACCESS"
2191 * with NFSv3.
2192 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002193 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
David Howells5cc0a842008-11-14 10:38:58 +11002194 inode->i_uid == current_fsuid())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return 0;
2196
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002197 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002198 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 /* Allow read access to binaries even when mode 111 */
2201 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002202 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2203 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002204 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 return err? nfserrno(err) : 0;
2207}
2208
2209void
2210nfsd_racache_shutdown(void)
2211{
Jeff Layton54a66e52008-08-13 22:03:27 -04002212 struct raparms *raparm, *last_raparm;
2213 unsigned int i;
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002216
2217 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2218 raparm = raparm_hash[i].pb_head;
2219 while(raparm) {
2220 last_raparm = raparm;
2221 raparm = raparm->p_next;
2222 kfree(last_raparm);
2223 }
2224 raparm_hash[i].pb_head = NULL;
2225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227/*
2228 * Initialize readahead param cache
2229 */
2230int
2231nfsd_racache_init(int cache_size)
2232{
2233 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002234 int j = 0;
2235 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002236 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Greg Banksfce14562006-10-04 02:15:49 -07002238
Jeff Layton54a66e52008-08-13 22:03:27 -04002239 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002241 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2242 if (nperbucket < 2)
2243 nperbucket = 2;
2244 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002245
2246 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002247
2248 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002249 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002250
2251 raparm = &raparm_hash[i].pb_head;
2252 for (j = 0; j < nperbucket; j++) {
2253 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2254 if (!*raparm)
2255 goto out_nomem;
2256 raparm = &(*raparm)->p_next;
2257 }
2258 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002259 }
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 nfsdstats.ra_size = cache_size;
2262 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002263
2264out_nomem:
2265 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2266 nfsd_racache_shutdown();
2267 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002269
2270#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2271struct posix_acl *
2272nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2273{
2274 struct inode *inode = fhp->fh_dentry->d_inode;
2275 char *name;
2276 void *value = NULL;
2277 ssize_t size;
2278 struct posix_acl *acl;
2279
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002280 if (!IS_POSIXACL(inode))
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002281 return ERR_PTR(-EOPNOTSUPP);
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002282
2283 switch (type) {
2284 case ACL_TYPE_ACCESS:
2285 name = POSIX_ACL_XATTR_ACCESS;
2286 break;
2287 case ACL_TYPE_DEFAULT:
2288 name = POSIX_ACL_XATTR_DEFAULT;
2289 break;
2290 default:
2291 return ERR_PTR(-EOPNOTSUPP);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002292 }
2293
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002294 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2295 if (size < 0)
2296 return ERR_PTR(size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002297
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002298 acl = posix_acl_from_xattr(value, size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002299 kfree(value);
2300 return acl;
2301}
2302
2303int
2304nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2305{
2306 struct inode *inode = fhp->fh_dentry->d_inode;
2307 char *name;
2308 void *value = NULL;
2309 size_t size;
2310 int error;
2311
Al Viroacfa4382008-12-04 10:06:33 -05002312 if (!IS_POSIXACL(inode) ||
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002313 !inode->i_op->setxattr || !inode->i_op->removexattr)
2314 return -EOPNOTSUPP;
2315 switch(type) {
2316 case ACL_TYPE_ACCESS:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002317 name = POSIX_ACL_XATTR_ACCESS;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002318 break;
2319 case ACL_TYPE_DEFAULT:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002320 name = POSIX_ACL_XATTR_DEFAULT;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002321 break;
2322 default:
2323 return -EOPNOTSUPP;
2324 }
2325
2326 if (acl && acl->a_count) {
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002327 size = posix_acl_xattr_size(acl->a_count);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002328 value = kmalloc(size, GFP_KERNEL);
2329 if (!value)
2330 return -ENOMEM;
Florin Malita9ccfc292006-05-20 14:59:58 -07002331 error = posix_acl_to_xattr(acl, value, size);
2332 if (error < 0)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002333 goto getout;
Florin Malita9ccfc292006-05-20 14:59:58 -07002334 size = error;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002335 } else
2336 size = 0;
2337
Al Virobad0dcf2011-11-23 12:03:18 -05002338 error = fh_want_write(fhp);
Dave Hansen18f335a2008-02-15 14:37:38 -08002339 if (error)
2340 goto getout;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002341 if (size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002342 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002343 else {
2344 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2345 error = 0;
2346 else {
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002347 error = vfs_removexattr(fhp->fh_dentry, name);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002348 if (error == -ENODATA)
2349 error = 0;
2350 }
2351 }
Al Virobad0dcf2011-11-23 12:03:18 -05002352 fh_drop_write(fhp);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002353
2354getout:
2355 kfree(value);
2356 return error;
2357}
2358#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */