blob: 8ace1bf90a99b90a1163203706245bd58f756340 [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 Hellwigd51f76b2013-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 Fields9c657e82014-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 Fields9c657e82014-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 Fields9c657e82014-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 Hellwigd51f76b2013-11-18 05:07:30 -0800440 nfsd_sanitize_attrs(inode, iap);
441
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000442 /*
Christoph Hellwigd51f76b2013-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 Hellwigd51f76b2013-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 Hellwigdb80ab72013-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 Hellwigdb80ab72013-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
Sergio Gelatofe8e5782014-10-10 22:46:36 +0800511 if (!pacl)
512 return vfs_setxattr(dentry, key, NULL, 0, 0);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 buflen = posix_acl_xattr_size(pacl->a_count);
515 buf = kmalloc(buflen, GFP_KERNEL);
516 error = -ENOMEM;
517 if (buf == NULL)
518 goto out;
519
520 len = posix_acl_to_xattr(pacl, buf, buflen);
521 if (len < 0) {
522 error = len;
523 goto out;
524 }
525
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800526 error = vfs_setxattr(dentry, key, buf, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527out:
528 kfree(buf);
529 return error;
530}
531
Al Viro6264d692006-10-19 23:28:58 -0700532__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
534 struct nfs4_acl *acl)
535{
Al Viro6264d692006-10-19 23:28:58 -0700536 __be32 error;
537 int host_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct dentry *dentry;
539 struct inode *inode;
540 struct posix_acl *pacl = NULL, *dpacl = NULL;
541 unsigned int flags = 0;
542
543 /* Get inode */
J. Bruce Fieldse281d812011-08-15 16:57:07 -0400544 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (error)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700546 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 dentry = fhp->fh_dentry;
549 inode = dentry->d_inode;
550 if (S_ISDIR(inode->i_mode))
551 flags = NFS4_ACL_DIR;
552
Al Viro6264d692006-10-19 23:28:58 -0700553 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
554 if (host_error == -EINVAL) {
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700555 return nfserr_attrnotsupp;
Al Viro6264d692006-10-19 23:28:58 -0700556 } else if (host_error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto out_nfserr;
558
Al Viro6264d692006-10-19 23:28:58 -0700559 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
560 if (host_error < 0)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700561 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700563 if (S_ISDIR(inode->i_mode))
Al Viro6264d692006-10-19 23:28:58 -0700564 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700566out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 posix_acl_release(pacl);
568 posix_acl_release(dpacl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569out_nfserr:
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800570 if (host_error == -EOPNOTSUPP)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700571 return nfserr_attrnotsupp;
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800572 else
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700573 return nfserrno(host_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static struct posix_acl *
577_get_posix_acl(struct dentry *dentry, char *key)
578{
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800579 void *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct posix_acl *pacl = NULL;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800581 int buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800583 buflen = nfsd_getxattr(dentry, key, &buf);
584 if (!buflen)
585 buflen = -ENODATA;
586 if (buflen <= 0)
587 return ERR_PTR(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 pacl = posix_acl_from_xattr(buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 kfree(buf);
591 return pacl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594int
595nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
596{
597 struct inode *inode = dentry->d_inode;
598 int error = 0;
599 struct posix_acl *pacl = NULL, *dpacl = NULL;
600 unsigned int flags = 0;
601
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700602 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
604 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
605 if (IS_ERR(pacl)) {
606 error = PTR_ERR(pacl);
607 pacl = NULL;
608 goto out;
609 }
610
611 if (S_ISDIR(inode->i_mode)) {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700612 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
614 dpacl = NULL;
615 else if (IS_ERR(dpacl)) {
616 error = PTR_ERR(dpacl);
617 dpacl = NULL;
618 goto out;
619 }
620 flags = NFS4_ACL_DIR;
621 }
622
623 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
624 if (IS_ERR(*acl)) {
625 error = PTR_ERR(*acl);
626 *acl = NULL;
627 }
628 out:
629 posix_acl_release(pacl);
630 posix_acl_release(dpacl);
631 return error;
632}
633
Chuck Lever9b4146e2012-01-04 16:26:43 -0500634/*
635 * NFS junction information is stored in an extended attribute.
636 */
637#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
638
639/**
640 * nfsd4_is_junction - Test if an object could be an NFS junction
641 *
642 * @dentry: object to test
643 *
644 * Returns 1 if "dentry" appears to contain NFS junction information.
645 * Otherwise 0 is returned.
646 */
Trond Myklebust11fcee02011-09-12 19:37:26 -0400647int nfsd4_is_junction(struct dentry *dentry)
648{
649 struct inode *inode = dentry->d_inode;
650
651 if (inode == NULL)
652 return 0;
653 if (inode->i_mode & S_IXUGO)
654 return 0;
655 if (!(inode->i_mode & S_ISVTX))
656 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500657 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee02011-09-12 19:37:26 -0400658 return 0;
659 return 1;
660}
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400661#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663#ifdef CONFIG_NFSD_V3
664/*
665 * Check server access rights to a file system object
666 */
667struct accessmap {
668 u32 access;
669 int how;
670};
671static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200672 { NFS3_ACCESS_READ, NFSD_MAY_READ },
673 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
674 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
675 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 { 0, 0 }
678};
679
680static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200681 { NFS3_ACCESS_READ, NFSD_MAY_READ },
682 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
683 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
684 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
685 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 { 0, 0 }
688};
689
690static struct accessmap nfs3_anyaccess[] = {
691 /* Some clients - Solaris 2.6 at least, make an access call
692 * to the server to check for access for things like /dev/null
693 * (which really, the server doesn't care about). So
694 * We provide simple access checking for them, looking
695 * mainly at mode bits, and we make sure to ignore read-only
696 * filesystem checks
697 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200698 { NFS3_ACCESS_READ, NFSD_MAY_READ },
699 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
700 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
701 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 { 0, 0 }
704};
705
Al Viro6264d692006-10-19 23:28:58 -0700706__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
708{
709 struct accessmap *map;
710 struct svc_export *export;
711 struct dentry *dentry;
712 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700713 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200715 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (error)
717 goto out;
718
719 export = fhp->fh_export;
720 dentry = fhp->fh_dentry;
721
722 if (S_ISREG(dentry->d_inode->i_mode))
723 map = nfs3_regaccess;
724 else if (S_ISDIR(dentry->d_inode->i_mode))
725 map = nfs3_diraccess;
726 else
727 map = nfs3_anyaccess;
728
729
730 query = *access;
731 for (; map->access; map++) {
732 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700733 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 sresult |= map->access;
736
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700737 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 switch (err2) {
739 case nfs_ok:
740 result |= map->access;
741 break;
742
743 /* the following error codes just mean the access was not allowed,
744 * rather than an error occurred */
745 case nfserr_rofs:
746 case nfserr_acces:
747 case nfserr_perm:
748 /* simply don't "or" in the access bit. */
749 break;
750 default:
751 error = err2;
752 goto out;
753 }
754 }
755 }
756 *access = result;
757 if (supported)
758 *supported = sresult;
759
760 out:
761 return error;
762}
763#endif /* CONFIG_NFSD_V3 */
764
J. Bruce Fields105f4622011-06-07 11:50:23 -0400765static int nfsd_open_break_lease(struct inode *inode, int access)
766{
767 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
J. Bruce Fields105f4622011-06-07 11:50:23 -0400769 if (access & NFSD_MAY_NOT_BREAK_LEASE)
770 return 0;
771 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
772 return break_lease(inode, mode | O_NONBLOCK);
773}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775/*
776 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400777 * The may_flags argument indicates the type of open (read/write/lock)
778 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * N.B. After this call fhp needs an fh_put
780 */
Al Viro6264d692006-10-19 23:28:58 -0700781__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400782nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400783 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct dentry *dentry;
786 struct inode *inode;
Al Viro6264d692006-10-19 23:28:58 -0700787 int flags = O_RDONLY|O_LARGEFILE;
788 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400789 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
David Howellse0e81732009-09-02 09:13:40 +0100791 validate_process_creds();
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * If we get here, then the client has already done an "open",
795 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
796 * in case a chmod has now revoked permission.
797 */
Bernd Schubert999448a2012-03-18 22:44:49 -0400798 err = fh_verify(rqstp, fhp, type, may_flags | NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (err)
800 goto out;
801
802 dentry = fhp->fh_dentry;
803 inode = dentry->d_inode;
804
805 /* Disallow write access to files with the append-only bit set
806 * or any access when mandatory locking enabled
807 */
808 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400809 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400811 /*
812 * We must ignore files (but only files) which might have mandatory
813 * locks on them because there is no way to know if the accesser has
814 * the lock.
815 */
816 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto out;
818
819 if (!inode->i_fop)
820 goto out;
821
Bernd Schubert999448a2012-03-18 22:44:49 -0400822 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700823 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 goto out_nfserr;
825
Bernd Schubert999448a2012-03-18 22:44:49 -0400826 if (may_flags & NFSD_MAY_WRITE) {
827 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700828 flags = O_RDWR|O_LARGEFILE;
829 else
830 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Jan Blunck54775492008-02-14 19:38:39 -0800832 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
David Howells033a6662009-07-02 14:35:32 +0100833 flags, current_cred());
Harshula Jayasuriyaf32094c2013-07-23 14:21:35 +1000834 if (IS_ERR(*filp)) {
Al Viro6264d692006-10-19 23:28:58 -0700835 host_err = PTR_ERR(*filp);
Harshula Jayasuriyaf32094c2013-07-23 14:21:35 +1000836 *filp = NULL;
837 } else {
Bernd Schubert999448a2012-03-18 22:44:49 -0400838 host_err = ima_file_check(*filp, may_flags);
839
Bernd Schubert06effdb2012-03-18 22:44:50 -0400840 if (may_flags & NFSD_MAY_64BIT_COOKIE)
841 (*filp)->f_mode |= FMODE_64BITHASH;
842 else
843 (*filp)->f_mode |= FMODE_32BITHASH;
844 }
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700847 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848out:
David Howellse0e81732009-09-02 09:13:40 +0100849 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return err;
851}
852
853/*
854 * Close a file.
855 */
856void
857nfsd_close(struct file *filp)
858{
859 fput(filp);
860}
861
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500862/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * Obtain the readahead parameters for the file
864 * specified by (dev, ino).
865 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867static inline struct raparms *
868nfsd_get_raparms(dev_t dev, ino_t ino)
869{
870 struct raparms *ra, **rap, **frap = NULL;
871 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700872 unsigned int hash;
873 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Greg Banksfce14562006-10-04 02:15:49 -0700875 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
876 rab = &raparm_hash[hash];
877
878 spin_lock(&rab->pb_lock);
879 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (ra->p_ino == ino && ra->p_dev == dev)
881 goto found;
882 depth++;
883 if (ra->p_count == 0)
884 frap = rap;
885 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300886 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700888 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return NULL;
890 }
891 rap = frap;
892 ra = *frap;
893 ra->p_dev = dev;
894 ra->p_ino = ino;
895 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700896 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897found:
Greg Banksfce14562006-10-04 02:15:49 -0700898 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700900 ra->p_next = rab->pb_head;
901 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 ra->p_count++;
904 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700905 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return ra;
907}
908
909/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200910 * Grab and keep cached pages associated with a file in the svc_rqst
911 * so that they can be passed to the network sendmsg/sendpage routines
912 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
914static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200915nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
916 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Jens Axboecf8208d2007-06-12 21:22:14 +0200918 struct svc_rqst *rqstp = sd->u.data;
NeilBrown44524352006-10-04 02:15:46 -0700919 struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
Jens Axboecf8208d2007-06-12 21:22:14 +0200920 struct page *page = buf->page;
921 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200922
923 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (rqstp->rq_res.page_len == 0) {
926 get_page(page);
NeilBrown44524352006-10-04 02:15:46 -0700927 put_page(*pp);
928 *pp = page;
929 rqstp->rq_resused++;
Jens Axboecf8208d2007-06-12 21:22:14 +0200930 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700932 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 get_page(page);
NeilBrown250f3912007-01-26 00:56:59 -0800934 if (*pp)
935 put_page(*pp);
NeilBrown44524352006-10-04 02:15:46 -0700936 *pp = page;
937 rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700939 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return size;
943}
944
Jens Axboecf8208d2007-06-12 21:22:14 +0200945static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
946 struct splice_desc *sd)
947{
948 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
949}
950
Al Viro6264d692006-10-19 23:28:58 -0700951static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
953 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
954{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700956 __be32 err;
957 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 err = nfserr_perm;
Dave Hansena8754be2007-10-16 23:31:15 -0700960
Jens Axboecf8208d2007-06-12 21:22:14 +0200961 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
962 struct splice_desc sd = {
963 .len = 0,
964 .total_len = *count,
965 .pos = offset,
966 .u.data = rqstp,
967 };
968
Jens Axboe4fbef202007-07-13 22:42:20 +0200969 rqstp->rq_resused = 1;
Jens Axboecf8208d2007-06-12 21:22:14 +0200970 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 } else {
972 oldfs = get_fs();
973 set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -0700974 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 set_fs(oldfs);
976 }
977
Al Viro6264d692006-10-19 23:28:58 -0700978 if (host_err >= 0) {
979 nfsdstats.io_read += host_err;
980 *count = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 err = 0;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500982 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 } else
Al Viro6264d692006-10-19 23:28:58 -0700984 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return err;
986}
987
Neil Brown9f708e42006-01-06 00:19:59 -0800988static void kill_suid(struct dentry *dentry)
989{
990 struct iattr ia;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700991 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Neil Brown9f708e42006-01-06 00:19:59 -0800992
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800993 mutex_lock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800994 notify_change(dentry, &ia);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800995 mutex_unlock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800996}
997
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700998/*
999 * Gathered writes: If another process is currently writing to the file,
1000 * there's a high chance this is another nfsd (triggered by a bulk write
1001 * from a client's biod). Rather than syncing the file with each write
1002 * request, we sleep for 10 msec.
1003 *
1004 * I don't know if this roughly approximates C. Juszak's idea of
1005 * gathered writes, but it's a nice and simple solution (IMHO), and it
1006 * seems to work:-)
1007 *
1008 * Note: we do this only in the NFSv2 case, since v3 and higher have a
1009 * better tool (separate unstable writes and commits) for solving this
1010 * problem.
1011 */
1012static int wait_for_concurrent_writes(struct file *file)
1013{
1014 struct inode *inode = file->f_path.dentry->d_inode;
1015 static ino_t last_ino;
1016 static dev_t last_dev;
1017 int err = 0;
1018
1019 if (atomic_read(&inode->i_writecount) > 1
1020 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1021 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1022 msleep(10);
1023 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1024 }
1025
1026 if (inode->i_state & I_DIRTY) {
1027 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001028 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001029 }
1030 last_ino = inode->i_ino;
1031 last_dev = inode->i_sb->s_dev;
1032 return err;
1033}
1034
Al Viro6264d692006-10-19 23:28:58 -07001035static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1037 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -05001038 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct svc_export *exp;
1041 struct dentry *dentry;
1042 struct inode *inode;
1043 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001044 __be32 err = 0;
1045 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001047 int use_wgather;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001049 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 inode = dentry->d_inode;
1051 exp = fhp->fh_export;
1052
1053 /*
1054 * Request sync writes if
1055 * - the sync export option has been set, or
1056 * - the client requested O_SYNC behavior (NFSv3 feature).
1057 * - The file system doesn't support fsync().
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001058 * When NFSv2 gathered writes have been configured for this volume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 * flushing the data to disk is handled separately below.
1060 */
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001061 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Harvey Harrison3ba15142008-02-20 12:49:02 -08001063 if (!file->f_op->fsync) {/* COMMIT3 cannot work */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 stable = 2;
1065 *stablep = 2; /* FILE_SYNC */
1066 }
1067
1068 if (!EX_ISSYNC(exp))
1069 stable = 0;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001070 if (stable && !use_wgather) {
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001071 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 file->f_flags |= O_SYNC;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001073 spin_unlock(&file->f_lock);
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /* Write the data. */
1077 oldfs = get_fs(); set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -07001078 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001080 if (host_err < 0)
1081 goto out_nfserr;
1082 *cnt = host_err;
1083 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -05001084 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 /* clear setuid/setgid flag after write */
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001087 if (inode->i_mode & (S_ISUID | S_ISGID))
Neil Brown9f708e42006-01-06 00:19:59 -08001088 kill_suid(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001090 if (stable && use_wgather)
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001091 host_err = wait_for_concurrent_writes(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001093out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001094 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001095 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001097 else
Al Viro6264d692006-10-19 23:28:58 -07001098 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return err;
1100}
1101
1102/*
1103 * Read data from a file. count must contain the requested read count
1104 * on entry. On return, *count contains the number of bytes actually read.
1105 * N.B. After this call fhp needs an fh_put
1106 */
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001107__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001108 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1109{
1110 struct file *file;
1111 struct inode *inode;
1112 struct raparms *ra;
1113 __be32 err;
1114
1115 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1116 if (err)
1117 return err;
1118
1119 inode = file->f_path.dentry->d_inode;
1120
1121 /* Get readahead parameters */
1122 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1123
1124 if (ra && ra->p_set)
1125 file->f_ra = ra->p_ra;
1126
1127 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1128
1129 /* Write back readahead params */
1130 if (ra) {
1131 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1132 spin_lock(&rab->pb_lock);
1133 ra->p_ra = file->f_ra;
1134 ra->p_set = 1;
1135 ra->p_count--;
1136 spin_unlock(&rab->pb_lock);
1137 }
1138
1139 nfsd_close(file);
1140 return err;
1141}
1142
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001143/* As above, but use the provided file descriptor. */
Al Viro6264d692006-10-19 23:28:58 -07001144__be32
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001145nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 loff_t offset, struct kvec *vec, int vlen,
1147 unsigned long *count)
1148{
Al Viro6264d692006-10-19 23:28:58 -07001149 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001152 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001153 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (err)
1155 goto out;
1156 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
J. Bruce Fields039a87c2010-07-30 11:33:32 -04001157 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1158 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159out:
1160 return err;
1161}
1162
1163/*
1164 * Write data to a file.
1165 * The stable flag requests synchronous writes.
1166 * N.B. After this call fhp needs an fh_put
1167 */
Al Viro6264d692006-10-19 23:28:58 -07001168__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001170 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int *stablep)
1172{
Al Viro6264d692006-10-19 23:28:58 -07001173 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001176 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001177 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (err)
1179 goto out;
1180 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1181 stablep);
1182 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001183 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (err)
1185 goto out;
1186
1187 if (cnt)
1188 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1189 cnt, stablep);
1190 nfsd_close(file);
1191 }
1192out:
1193 return err;
1194}
1195
1196#ifdef CONFIG_NFSD_V3
1197/*
1198 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001199 *
1200 * Note: we only guarantee that data that lies within the range specified
1201 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 *
1203 * Unfortunately we cannot lock the file to make sure we return full WCC
1204 * data to the client, as locking happens lower down in the filesystem.
1205 */
Al Viro6264d692006-10-19 23:28:58 -07001206__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1208 loff_t offset, unsigned long count)
1209{
1210 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001211 loff_t end = LLONG_MAX;
1212 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Trond Myklebustaa696a62010-01-29 16:44:25 -05001214 if (offset < 0)
1215 goto out;
1216 if (count != 0) {
1217 end = offset + (loff_t)count - 1;
1218 if (end < offset)
1219 goto out;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jeff Layton91885252010-03-19 08:06:28 -04001222 err = nfsd_open(rqstp, fhp, S_IFREG,
1223 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001224 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001227 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001228
1229 if (err2 != -EINVAL)
1230 err = nfserrno(err2);
1231 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 nfsd_close(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001236out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return err;
1238}
1239#endif /* CONFIG_NFSD_V3 */
1240
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001241static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001242nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1243 struct iattr *iap)
1244{
1245 /*
1246 * Mode has already been set earlier in create:
1247 */
1248 iap->ia_valid &= ~ATTR_MODE;
1249 /*
1250 * Setting uid/gid works only for root. Irix appears to
1251 * send along the gid on create when it tries to implement
1252 * setgid directories via NFS:
1253 */
David Howells5cc0a842008-11-14 10:38:58 +11001254 if (current_fsuid() != 0)
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001255 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1256 if (iap->ia_valid)
1257 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1258 return 0;
1259}
1260
wengang wang4ac35c22009-02-10 11:27:51 +08001261/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1262 * setting size to 0 may fail for some specific file systems by the permission
1263 * checking which requires WRITE permission but the mode is 000.
1264 * we ignore the resizing(to 0) on the just new created file, since the size is
1265 * 0 after file created.
1266 *
1267 * call this only after vfs_create() is called.
1268 * */
1269static void
1270nfsd_check_ignore_resizing(struct iattr *iap)
1271{
1272 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1273 iap->ia_valid &= ~ATTR_SIZE;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/*
1277 * Create a file (regular, directory, device, fifo); UNIX sockets
1278 * not yet implemented.
1279 * If the response fh has been verified, the parent directory should
1280 * already be locked. Note that the parent directory is left locked.
1281 *
1282 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1283 */
Al Viro6264d692006-10-19 23:28:58 -07001284__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1286 char *fname, int flen, struct iattr *iap,
1287 int type, dev_t rdev, struct svc_fh *resfhp)
1288{
1289 struct dentry *dentry, *dchild = NULL;
1290 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001291 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001292 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001293 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 err = nfserr_perm;
1296 if (!flen)
1297 goto out;
1298 err = nfserr_exist;
1299 if (isdotent(fname, flen))
1300 goto out;
1301
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001302 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (err)
1304 goto out;
1305
1306 dentry = fhp->fh_dentry;
1307 dirp = dentry->d_inode;
1308
1309 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001310 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 goto out;
1312 /*
1313 * Check whether the response file handle has been verified yet.
1314 * If it has, the parent directory should already be locked.
1315 */
1316 if (!resfhp->fh_dentry) {
1317 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001318 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001320 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (IS_ERR(dchild))
1322 goto out_nfserr;
1323 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1324 if (err)
1325 goto out;
1326 } else {
1327 /* called from nfsd_proc_create */
1328 dchild = dget(resfhp->fh_dentry);
1329 if (!fhp->fh_locked) {
1330 /* not actually possible */
1331 printk(KERN_ERR
1332 "nfsd_create: parent %s/%s not locked!\n",
1333 dentry->d_parent->d_name.name,
1334 dentry->d_name.name);
Al Virod75f2b92006-01-18 17:43:44 -08001335 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 goto out;
1337 }
1338 }
1339 /*
1340 * Make sure the child dentry is still negative ...
1341 */
1342 err = nfserr_exist;
1343 if (dchild->d_inode) {
1344 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1345 dentry->d_name.name, dchild->d_name.name);
1346 goto out;
1347 }
1348
1349 if (!(iap->ia_valid & ATTR_MODE))
1350 iap->ia_mode = 0;
1351 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1352
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001353 err = nfserr_inval;
1354 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1355 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1356 type);
1357 goto out;
1358 }
1359
Al Virobad0dcf2011-11-23 12:03:18 -05001360 host_err = fh_want_write(fhp);
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001361 if (host_err)
1362 goto out_nfserr;
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /*
1365 * Get the dir op function pointer.
1366 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001367 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 switch (type) {
1369 case S_IFREG:
Al Viro8da0eaf2012-06-10 18:09:36 -04001370 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001371 if (!host_err)
1372 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 break;
1374 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001375 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
1377 case S_IFCHR:
1378 case S_IFBLK:
1379 case S_IFIFO:
1380 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001381 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001383 }
1384 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001385 fh_drop_write(fhp);
Dave Hansen463c3192008-02-15 14:37:57 -08001386 goto out_nfserr;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Ben Myersf5019122010-02-17 14:05:11 -06001389 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Ben Myersf5019122010-02-17 14:05:11 -06001391 /*
1392 * nfsd_setattr already committed the child. Transactional filesystems
1393 * had a chance to commit changes for both parent and child
1394 * simultaneously making the following commit_metadata a noop.
1395 */
1396 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001397 if (err2)
1398 err = err2;
Al Virobad0dcf2011-11-23 12:03:18 -05001399 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /*
1401 * Update the file handle to get the new inode info.
1402 */
1403 if (!err)
1404 err = fh_update(resfhp);
1405out:
1406 if (dchild && !IS_ERR(dchild))
1407 dput(dchild);
1408 return err;
1409
1410out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001411 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 goto out;
1413}
1414
1415#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001416
1417static inline int nfsd_create_is_exclusive(int createmode)
1418{
1419 return createmode == NFS3_CREATE_EXCLUSIVE
1420 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001424 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
Al Viro6264d692006-10-19 23:28:58 -07001426__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001427do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 char *fname, int flen, struct iattr *iap,
1429 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001430 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct dentry *dentry, *dchild = NULL;
1433 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001434 __be32 err;
1435 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 err = nfserr_perm;
1439 if (!flen)
1440 goto out;
1441 err = nfserr_exist;
1442 if (isdotent(fname, flen))
1443 goto out;
1444 if (!(iap->ia_valid & ATTR_MODE))
1445 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001446 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (err)
1448 goto out;
1449
1450 dentry = fhp->fh_dentry;
1451 dirp = dentry->d_inode;
1452
1453 /* Get all the sanity checks out of the way before
1454 * we lock the parent. */
1455 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001456 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 goto out;
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001458 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /*
1461 * Compose the response file handle.
1462 */
1463 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001464 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (IS_ERR(dchild))
1466 goto out_nfserr;
1467
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001468 /* If file doesn't exist, check for permissions to create one */
1469 if (!dchild->d_inode) {
1470 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1471 if (err)
1472 goto out;
1473 }
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1476 if (err)
1477 goto out;
1478
Mi Jinlongac6721a2011-04-20 17:06:25 +08001479 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001480 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001481 * the high bit set, so just clear the high bits. If this is
1482 * ever changed to use different attrs for storing the
1483 * verifier, then do_open_lookup() will also need to be fixed
1484 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486 v_mtime = verifier[0]&0x7fffffff;
1487 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
Al Virobad0dcf2011-11-23 12:03:18 -05001490 host_err = fh_want_write(fhp);
Dave Hansen463c3192008-02-15 14:37:57 -08001491 if (host_err)
1492 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (dchild->d_inode) {
1494 err = 0;
1495
1496 switch (createmode) {
1497 case NFS3_CREATE_UNCHECKED:
1498 if (! S_ISREG(dchild->d_inode->i_mode))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 else if (truncp) {
1501 /* in nfsv4, we need to treat this case a little
1502 * differently. we don't want to truncate the
1503 * file now; this would be wrong if the OPEN
1504 * fails for some other reason. furthermore,
1505 * if the size is nonzero, we should ignore it
1506 * according to spec!
1507 */
1508 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1509 }
1510 else {
1511 iap->ia_valid &= ATTR_SIZE;
1512 goto set_attr;
1513 }
1514 break;
1515 case NFS3_CREATE_EXCLUSIVE:
1516 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1517 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown44c88ab2012-12-07 15:40:55 -05001518 && dchild->d_inode->i_size == 0 ) {
1519 if (created)
1520 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
Neil Brown44c88ab2012-12-07 15:40:55 -05001522 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001523 case NFS4_CREATE_EXCLUSIVE4_1:
1524 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1525 && dchild->d_inode->i_atime.tv_sec == v_atime
Neil Brown44c88ab2012-12-07 15:40:55 -05001526 && dchild->d_inode->i_size == 0 ) {
1527 if (created)
1528 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001529 goto set_attr;
Neil Brown44c88ab2012-12-07 15:40:55 -05001530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /* fallthru */
1532 case NFS3_CREATE_GUARDED:
1533 err = nfserr_exist;
1534 }
Al Virobad0dcf2011-11-23 12:03:18 -05001535 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 goto out;
1537 }
1538
Al Viro8da0eaf2012-06-10 18:09:36 -04001539 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001540 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001541 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001543 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001544 if (created)
1545 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
wengang wang4ac35c22009-02-10 11:27:51 +08001547 nfsd_check_ignore_resizing(iap);
1548
Mi Jinlongac6721a2011-04-20 17:06:25 +08001549 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001550 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001552 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /* XXX someone who knows this better please fix it for nsec */
1554 iap->ia_mtime.tv_sec = v_mtime;
1555 iap->ia_atime.tv_sec = v_atime;
1556 iap->ia_mtime.tv_nsec = 0;
1557 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001561 err = nfsd_create_setattr(rqstp, resfhp, iap);
1562
1563 /*
1564 * nfsd_setattr already committed the child (and possibly also the parent).
1565 */
1566 if (!err)
1567 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001568
Al Virobad0dcf2011-11-23 12:03:18 -05001569 fh_drop_write(fhp);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001570 /*
1571 * Update the filehandle to get the new inode info.
1572 */
1573 if (!err)
1574 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 out:
1577 fh_unlock(fhp);
1578 if (dchild && !IS_ERR(dchild))
1579 dput(dchild);
1580 return err;
1581
1582 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001583 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 goto out;
1585}
1586#endif /* CONFIG_NFSD_V3 */
1587
1588/*
1589 * Read a symlink. On entry, *lenp must contain the maximum path length that
1590 * fits into the buffer. On return, it contains the true length.
1591 * N.B. After this call fhp needs an fh_put
1592 */
Al Viro6264d692006-10-19 23:28:58 -07001593__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 struct inode *inode;
1597 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001598 __be32 err;
1599 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001600 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001602 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (err)
1604 goto out;
1605
Al Viro68ac1232012-03-15 08:21:57 -04001606 path.mnt = fhp->fh_export->ex_path.mnt;
1607 path.dentry = fhp->fh_dentry;
1608 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001611 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 goto out;
1613
Al Viro68ac1232012-03-15 08:21:57 -04001614 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /* N.B. Why does this call need a get_fs()??
1616 * Remove the set_fs and watch the fireworks:-) --okir
1617 */
1618
1619 oldfs = get_fs(); set_fs(KERNEL_DS);
Al Viro68ac1232012-03-15 08:21:57 -04001620 host_err = inode->i_op->readlink(path.dentry, buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 set_fs(oldfs);
1622
Al Viro6264d692006-10-19 23:28:58 -07001623 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001625 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 err = 0;
1627out:
1628 return err;
1629
1630out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001631 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 goto out;
1633}
1634
1635/*
1636 * Create a symlink and look up its inode
1637 * N.B. After this call _both_ fhp and resfhp need an fh_put
1638 */
Al Viro6264d692006-10-19 23:28:58 -07001639__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1641 char *fname, int flen,
1642 char *path, int plen,
1643 struct svc_fh *resfhp,
1644 struct iattr *iap)
1645{
1646 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001647 __be32 err, cerr;
1648 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 err = nfserr_noent;
1651 if (!flen || !plen)
1652 goto out;
1653 err = nfserr_exist;
1654 if (isdotent(fname, flen))
1655 goto out;
1656
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001657 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (err)
1659 goto out;
1660 fh_lock(fhp);
1661 dentry = fhp->fh_dentry;
1662 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001663 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (IS_ERR(dnew))
1665 goto out_nfserr;
1666
Al Virobad0dcf2011-11-23 12:03:18 -05001667 host_err = fh_want_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001668 if (host_err)
1669 goto out_nfserr;
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (unlikely(path[plen] != 0)) {
1672 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1673 if (path_alloced == NULL)
Al Viro6264d692006-10-19 23:28:58 -07001674 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 else {
1676 strncpy(path_alloced, path, plen);
1677 path_alloced[plen] = 0;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001678 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 kfree(path_alloced);
1680 }
1681 } else
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001682 host_err = vfs_symlink(dentry->d_inode, dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001683 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001684 if (!err)
1685 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 fh_unlock(fhp);
1687
Al Virobad0dcf2011-11-23 12:03:18 -05001688 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1691 dput(dnew);
1692 if (err==0) err = cerr;
1693out:
1694 return err;
1695
1696out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001697 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 goto out;
1699}
1700
1701/*
1702 * Create a hardlink
1703 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1704 */
Al Viro6264d692006-10-19 23:28:58 -07001705__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1707 char *name, int len, struct svc_fh *tfhp)
1708{
1709 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001710 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001711 __be32 err;
1712 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001714 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (err)
1716 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001717 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (err)
1719 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001720 err = nfserr_isdir;
1721 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1722 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 err = nfserr_perm;
1724 if (!len)
1725 goto out;
1726 err = nfserr_exist;
1727 if (isdotent(name, len))
1728 goto out;
1729
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001730 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 ddir = ffhp->fh_dentry;
1732 dirp = ddir->d_inode;
1733
1734 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001735 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (IS_ERR(dnew))
1737 goto out_nfserr;
1738
1739 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Al Virobad0dcf2011-11-23 12:03:18 -05001741 host_err = fh_want_write(tfhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001742 if (host_err) {
1743 err = nfserrno(host_err);
1744 goto out_dput;
1745 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001746 err = nfserr_noent;
1747 if (!dold->d_inode)
1748 goto out_drop_write;
1749 host_err = nfsd_break_lease(dold->d_inode);
Casey Bodley7d751f62011-06-03 12:21:23 -04001750 if (host_err) {
1751 err = nfserrno(host_err);
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001752 goto out_drop_write;
Casey Bodley7d751f62011-06-03 12:21:23 -04001753 }
Al Viro6264d692006-10-19 23:28:58 -07001754 host_err = vfs_link(dold, dirp, dnew);
1755 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001756 err = nfserrno(commit_metadata(ffhp));
1757 if (!err)
1758 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 } else {
Al Viro6264d692006-10-19 23:28:58 -07001760 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 err = nfserr_acces;
1762 else
Al Viro6264d692006-10-19 23:28:58 -07001763 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001765out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001766 fh_drop_write(tfhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001767out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001769out_unlock:
1770 fh_unlock(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771out:
1772 return err;
1773
1774out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001775 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001776 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779/*
1780 * Rename a file
1781 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1782 */
Al Viro6264d692006-10-19 23:28:58 -07001783__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1785 struct svc_fh *tfhp, char *tname, int tlen)
1786{
1787 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1788 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001789 __be32 err;
1790 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001792 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (err)
1794 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001795 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (err)
1797 goto out;
1798
1799 fdentry = ffhp->fh_dentry;
1800 fdir = fdentry->d_inode;
1801
1802 tdentry = tfhp->fh_dentry;
1803 tdir = tdentry->d_inode;
1804
1805 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
NeilBrowna56f3932006-06-30 01:56:10 -07001806 if (ffhp->fh_export != tfhp->fh_export)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto out;
1808
1809 err = nfserr_perm;
1810 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1811 goto out;
1812
1813 /* cannot use fh_lock as we need deadlock protective ordering
1814 * so do it by hand */
1815 trap = lock_rename(tdentry, fdentry);
1816 ffhp->fh_locked = tfhp->fh_locked = 1;
1817 fill_pre_wcc(ffhp);
1818 fill_pre_wcc(tfhp);
1819
1820 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001821 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (IS_ERR(odentry))
1823 goto out_nfserr;
1824
Al Viro6264d692006-10-19 23:28:58 -07001825 host_err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (!odentry->d_inode)
1827 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001828 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (odentry == trap)
1830 goto out_dput_old;
1831
1832 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001833 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (IS_ERR(ndentry))
1835 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001836 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (ndentry == trap)
1838 goto out_dput_new;
1839
Dave Hansen9079b1e2008-02-15 14:37:49 -08001840 host_err = -EXDEV;
1841 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1842 goto out_dput_new;
Al Virobad0dcf2011-11-23 12:03:18 -05001843 host_err = fh_want_write(ffhp);
Dave Hansen9079b1e2008-02-15 14:37:49 -08001844 if (host_err)
1845 goto out_dput_new;
1846
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001847 host_err = nfsd_break_lease(odentry->d_inode);
1848 if (host_err)
1849 goto out_drop_write;
J. Bruce Fields83f6b0c2011-02-06 16:46:30 -05001850 if (ndentry->d_inode) {
1851 host_err = nfsd_break_lease(ndentry->d_inode);
1852 if (host_err)
1853 goto out_drop_write;
1854 }
Al Viro6264d692006-10-19 23:28:58 -07001855 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
Ben Myersf5019122010-02-17 14:05:11 -06001856 if (!host_err) {
1857 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001858 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001859 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001861out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001862 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 out_dput_new:
1864 dput(ndentry);
1865 out_dput_old:
1866 dput(odentry);
1867 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001868 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* we cannot reply on fh_unlock on the two filehandles,
1871 * as that would do the wrong thing if the two directories
1872 * were the same, so again we do it by hand
1873 */
1874 fill_post_wcc(ffhp);
1875 fill_post_wcc(tfhp);
1876 unlock_rename(tdentry, fdentry);
1877 ffhp->fh_locked = tfhp->fh_locked = 0;
1878
1879out:
1880 return err;
1881}
1882
1883/*
1884 * Unlink a file or directory
1885 * N.B. After this call fhp needs an fh_put
1886 */
Al Viro6264d692006-10-19 23:28:58 -07001887__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1889 char *fname, int flen)
1890{
1891 struct dentry *dentry, *rdentry;
1892 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001893 __be32 err;
1894 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 err = nfserr_acces;
1897 if (!flen || isdotent(fname, flen))
1898 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001899 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (err)
1901 goto out;
1902
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001903 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 dentry = fhp->fh_dentry;
1905 dirp = dentry->d_inode;
1906
1907 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001908 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (IS_ERR(rdentry))
1910 goto out_nfserr;
1911
1912 if (!rdentry->d_inode) {
1913 dput(rdentry);
1914 err = nfserr_noent;
1915 goto out;
1916 }
1917
1918 if (!type)
1919 type = rdentry->d_inode->i_mode & S_IFMT;
1920
Al Virobad0dcf2011-11-23 12:03:18 -05001921 host_err = fh_want_write(fhp);
Dave Hansen06227532008-02-15 14:37:34 -08001922 if (host_err)
J. Bruce Fields541ce982011-01-14 20:00:02 -05001923 goto out_put;
Dave Hansen06227532008-02-15 14:37:34 -08001924
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001925 host_err = nfsd_break_lease(rdentry->d_inode);
1926 if (host_err)
J. Bruce Fields541ce982011-01-14 20:00:02 -05001927 goto out_drop_write;
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001928 if (type != S_IFDIR)
Al Viro6264d692006-10-19 23:28:58 -07001929 host_err = vfs_unlink(dirp, rdentry);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001930 else
Al Viro6264d692006-10-19 23:28:58 -07001931 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001932 if (!host_err)
1933 host_err = commit_metadata(fhp);
1934out_drop_write:
Al Virobad0dcf2011-11-23 12:03:18 -05001935 fh_drop_write(fhp);
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001936out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 dput(rdentry);
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001940 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001941out:
1942 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001946 * We do this buffering because we must not call back into the file
1947 * system's ->lookup() method from the filldir callback. That may well
1948 * deadlock a number of file systems.
1949 *
1950 * This is based heavily on the implementation of same in XFS.
1951 */
1952struct buffered_dirent {
1953 u64 ino;
1954 loff_t offset;
1955 int namlen;
1956 unsigned int d_type;
1957 char name[];
1958};
1959
1960struct readdir_data {
1961 char *dirent;
1962 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001963 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001964};
1965
1966static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1967 loff_t offset, u64 ino, unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001968{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001969 struct readdir_data *buf = __buf;
1970 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1971 unsigned int reclen;
1972
1973 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001974 if (buf->used + reclen > PAGE_SIZE) {
1975 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001976 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001977 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001978
1979 de->namlen = namlen;
1980 de->offset = offset;
1981 de->ino = ino;
1982 de->d_type = d_type;
1983 memcpy(de->name, name, namlen);
1984 buf->used += reclen;
1985
1986 return 0;
1987}
1988
David Woodhouse2f9092e2009-04-20 23:18:37 +01001989static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1990 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001991{
1992 struct readdir_data buf;
1993 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001994 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001995 int size;
1996 loff_t offset;
David Woodhouse2628b762008-07-31 17:16:51 +01001997
David Woodhouse14f7dd62008-07-31 20:29:12 +01001998 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1999 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01002000 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01002001
David Woodhouse14f7dd62008-07-31 20:29:12 +01002002 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002003
2004 while (1) {
David Woodhouse2f9092e2009-04-20 23:18:37 +01002005 struct inode *dir_inode = file->f_path.dentry->d_inode;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002006 unsigned int reclen;
2007
Doug Nazarb726e922008-11-05 06:16:28 -05002008 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01002009 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04002010 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002011
2012 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
Al Viro53c9c5c2008-08-24 07:29:52 -04002013 if (buf.full)
2014 host_err = 0;
2015
2016 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01002017 break;
2018
2019 size = buf.used;
2020
2021 if (!size)
2022 break;
2023
David Woodhouse2f9092e2009-04-20 23:18:37 +01002024 /*
2025 * Various filldir functions may end up calling back into
2026 * lookup_one_len() and the file system's ->lookup() method.
2027 * These expect i_mutex to be held, as it would within readdir.
2028 */
2029 host_err = mutex_lock_killable(&dir_inode->i_mutex);
2030 if (host_err)
2031 break;
2032
David Woodhouse14f7dd62008-07-31 20:29:12 +01002033 de = (struct buffered_dirent *)buf.dirent;
2034 while (size > 0) {
2035 offset = de->offset;
2036
2037 if (func(cdp, de->name, de->namlen, de->offset,
2038 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01002039 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002040
2041 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01002042 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002043
2044 reclen = ALIGN(sizeof(*de) + de->namlen,
2045 sizeof(u64));
2046 size -= reclen;
2047 de = (struct buffered_dirent *)((char *)de + reclen);
2048 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01002049 mutex_unlock(&dir_inode->i_mutex);
2050 if (size > 0) /* We bailed out early */
2051 break;
2052
David Woodhousec002a6c2008-08-17 17:21:18 +01002053 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002054 }
2055
David Woodhouse14f7dd62008-07-31 20:29:12 +01002056 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01002057
2058 if (host_err)
2059 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002060
2061 *offsetp = offset;
2062 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01002063}
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065/*
2066 * Read entries from a directory.
2067 * The NFSv3/4 verifier we ignore for now.
2068 */
Al Viro6264d692006-10-19 23:28:58 -07002069__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
NeilBrowna0ad13e2007-01-26 00:57:10 -08002071 struct readdir_cd *cdp, filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Al Viro6264d692006-10-19 23:28:58 -07002073 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct file *file;
2075 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04002076 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Bernd Schubert06effdb2012-03-18 22:44:50 -04002078 /* NFSv2 only supports 32 bit cookies */
2079 if (rqstp->rq_vers > 2)
2080 may_flags |= NFSD_MAY_64BIT_COOKIE;
2081
2082 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (err)
2084 goto out;
2085
2086 offset = vfs_llseek(file, offset, 0);
2087 if (offset < 0) {
2088 err = nfserrno((int)offset);
2089 goto out_close;
2090 }
2091
David Woodhouse14f7dd62008-07-31 20:29:12 +01002092 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 if (err == nfserr_eof || err == nfserr_toosmall)
2095 err = nfs_ok; /* can still be found in ->err */
2096out_close:
2097 nfsd_close(file);
2098out:
2099 return err;
2100}
2101
2102/*
2103 * Get file system stats
2104 * N.B. After this call fhp needs an fh_put
2105 */
Al Viro6264d692006-10-19 23:28:58 -07002106__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04002107nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002109 __be32 err;
2110
2111 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02002112 if (!err) {
2113 struct path path = {
2114 .mnt = fhp->fh_export->ex_path.mnt,
2115 .dentry = fhp->fh_dentry,
2116 };
2117 if (vfs_statfs(&path, stat))
2118 err = nfserr_io;
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return err;
2121}
2122
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002123static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002124{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002125 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128/*
2129 * Check for a user's access permissions to this inode.
2130 */
Al Viro6264d692006-10-19 23:28:58 -07002131__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07002132nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2133 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 struct inode *inode = dentry->d_inode;
2136 int err;
2137
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04002138 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return 0;
2140#if 0
2141 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2142 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002143 (acc & NFSD_MAY_READ)? " read" : "",
2144 (acc & NFSD_MAY_WRITE)? " write" : "",
2145 (acc & NFSD_MAY_EXEC)? " exec" : "",
2146 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2147 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2148 (acc & NFSD_MAY_LOCK)? " lock" : "",
2149 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 inode->i_mode,
2151 IS_IMMUTABLE(inode)? " immut" : "",
2152 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002153 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002155 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#endif
2157
2158 /* Normally we reject any write/sattr etc access on a read-only file
2159 * system. But if it is IRIX doing check on write-access for a
2160 * device special file, we ignore rofs.
2161 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002162 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2163 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002164 if (exp_rdonly(rqstp, exp) ||
2165 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002167 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return nfserr_perm;
2169 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002170 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return nfserr_perm;
2172
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002173 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /* If we cannot rely on authentication in NLM requests,
2175 * just allow locks, otherwise require read permission, or
2176 * ownership
2177 */
2178 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2179 return 0;
2180 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002181 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183 /*
2184 * The file owner always gets access permission for accesses that
2185 * would normally be checked at open time. This is to make
2186 * file access work even when the client has done a fchmod(fd, 0).
2187 *
2188 * However, `cp foo bar' should fail nevertheless when bar is
2189 * readonly. A sensible way to do this might be to reject all
2190 * attempts to truncate a read-only file, because a creat() call
2191 * always implies file truncation.
2192 * ... but this isn't really fair. A process may reasonably call
2193 * ftruncate on an open file descriptor on a file with perm 000.
2194 * We must trust the client to do permission checking - using "ACCESS"
2195 * with NFSv3.
2196 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002197 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
David Howells5cc0a842008-11-14 10:38:58 +11002198 inode->i_uid == current_fsuid())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 0;
2200
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002201 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002202 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 /* Allow read access to binaries even when mode 111 */
2205 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002206 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2207 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002208 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 return err? nfserrno(err) : 0;
2211}
2212
2213void
2214nfsd_racache_shutdown(void)
2215{
Jeff Layton54a66e52008-08-13 22:03:27 -04002216 struct raparms *raparm, *last_raparm;
2217 unsigned int i;
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002220
2221 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2222 raparm = raparm_hash[i].pb_head;
2223 while(raparm) {
2224 last_raparm = raparm;
2225 raparm = raparm->p_next;
2226 kfree(last_raparm);
2227 }
2228 raparm_hash[i].pb_head = NULL;
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231/*
2232 * Initialize readahead param cache
2233 */
2234int
2235nfsd_racache_init(int cache_size)
2236{
2237 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002238 int j = 0;
2239 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002240 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Greg Banksfce14562006-10-04 02:15:49 -07002242
Jeff Layton54a66e52008-08-13 22:03:27 -04002243 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002245 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2246 if (nperbucket < 2)
2247 nperbucket = 2;
2248 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002249
2250 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002251
2252 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002253 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002254
2255 raparm = &raparm_hash[i].pb_head;
2256 for (j = 0; j < nperbucket; j++) {
2257 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2258 if (!*raparm)
2259 goto out_nomem;
2260 raparm = &(*raparm)->p_next;
2261 }
2262 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002263 }
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 nfsdstats.ra_size = cache_size;
2266 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002267
2268out_nomem:
2269 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2270 nfsd_racache_shutdown();
2271 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002273
2274#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2275struct posix_acl *
2276nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2277{
2278 struct inode *inode = fhp->fh_dentry->d_inode;
2279 char *name;
2280 void *value = NULL;
2281 ssize_t size;
2282 struct posix_acl *acl;
2283
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002284 if (!IS_POSIXACL(inode))
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002285 return ERR_PTR(-EOPNOTSUPP);
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002286
2287 switch (type) {
2288 case ACL_TYPE_ACCESS:
2289 name = POSIX_ACL_XATTR_ACCESS;
2290 break;
2291 case ACL_TYPE_DEFAULT:
2292 name = POSIX_ACL_XATTR_DEFAULT;
2293 break;
2294 default:
2295 return ERR_PTR(-EOPNOTSUPP);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002296 }
2297
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002298 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2299 if (size < 0)
2300 return ERR_PTR(size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002301
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002302 acl = posix_acl_from_xattr(value, size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002303 kfree(value);
2304 return acl;
2305}
2306
2307int
2308nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2309{
2310 struct inode *inode = fhp->fh_dentry->d_inode;
2311 char *name;
2312 void *value = NULL;
2313 size_t size;
2314 int error;
2315
Al Viroacfa4382008-12-04 10:06:33 -05002316 if (!IS_POSIXACL(inode) ||
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002317 !inode->i_op->setxattr || !inode->i_op->removexattr)
2318 return -EOPNOTSUPP;
2319 switch(type) {
2320 case ACL_TYPE_ACCESS:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002321 name = POSIX_ACL_XATTR_ACCESS;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002322 break;
2323 case ACL_TYPE_DEFAULT:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002324 name = POSIX_ACL_XATTR_DEFAULT;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002325 break;
2326 default:
2327 return -EOPNOTSUPP;
2328 }
2329
2330 if (acl && acl->a_count) {
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002331 size = posix_acl_xattr_size(acl->a_count);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002332 value = kmalloc(size, GFP_KERNEL);
2333 if (!value)
2334 return -ENOMEM;
Florin Malita9ccfc292006-05-20 14:59:58 -07002335 error = posix_acl_to_xattr(acl, value, size);
2336 if (error < 0)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002337 goto getout;
Florin Malita9ccfc292006-05-20 14:59:58 -07002338 size = error;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002339 } else
2340 size = 0;
2341
Al Virobad0dcf2011-11-23 12:03:18 -05002342 error = fh_want_write(fhp);
Dave Hansen18f335a2008-02-15 14:37:38 -08002343 if (error)
2344 goto getout;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002345 if (size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002346 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002347 else {
2348 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2349 error = 0;
2350 else {
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002351 error = vfs_removexattr(fhp->fh_dentry, name);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002352 if (error == -ENODATA)
2353 error = 0;
2354 }
2355 }
Al Virobad0dcf2011-11-23 12:03:18 -05002356 fh_drop_write(fhp);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002357
2358getout:
2359 kfree(value);
2360 return error;
2361}
2362#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */