blob: 5411bf09b810b25e6ae7aae0f14406a0da6a7758 [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>
Anna Schumaker95d871f2014-11-07 14:44:26 -050019#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/xattr.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020026#include <linux/jhash.h>
27#include <linux/ima.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020029#include <asm/uaccess.h>
Ben Myersf5019122010-02-17 14:05:11 -060030#include <linux/exportfs.h>
31#include <linux/writeback.h>
David Quigley18032ca2013-05-02 13:19:10 -040032#include <linux/security.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020033
34#ifdef CONFIG_NFSD_V3
35#include "xdr3.h"
36#endif /* CONFIG_NFSD_V3 */
37
Christoph Hellwig5be196e2006-01-09 20:51:55 -080038#ifdef CONFIG_NFSD_V4
Christoph Hellwigffa01602015-12-03 12:59:52 +010039#include "../internal.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050040#include "acl.h"
41#include "idmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#endif /* CONFIG_NFSD_V4 */
43
Boaz Harrosh9a74af22009-12-03 20:30:56 +020044#include "nfsd.h"
45#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define NFSDDBG_FACILITY NFSDDBG_FILEOP
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * This is a cache of readahead params that help us choose the proper
52 * readahead strategy. Initially, we set all readahead parameters to 0
53 * and let the VFS handle things.
54 * If you increase the number of cached files very much, you'll need to
55 * add a hash table here.
56 */
57struct raparms {
58 struct raparms *p_next;
59 unsigned int p_count;
60 ino_t p_ino;
61 dev_t p_dev;
62 int p_set;
63 struct file_ra_state p_ra;
Greg Banksfce14562006-10-04 02:15:49 -070064 unsigned int p_hindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
Greg Banksfce14562006-10-04 02:15:49 -070067struct raparm_hbucket {
68 struct raparms *pb_head;
69 spinlock_t pb_lock;
70} ____cacheline_aligned_in_smp;
71
Greg Banksfce14562006-10-04 02:15:49 -070072#define RAPARM_HASH_BITS 4
73#define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
74#define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
75static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
79 * a mount point.
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080080 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * or nfs_ok having possibly changed *dpp and *expp
82 */
83int
84nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
85 struct svc_export **expp)
86{
87 struct svc_export *exp = *expp, *exp2 = NULL;
88 struct dentry *dentry = *dpp;
Al Viro91c9fa82009-04-18 02:42:05 -040089 struct path path = {.mnt = mntget(exp->ex_path.mnt),
90 .dentry = dget(dentry)};
Al Viro6264d692006-10-19 23:28:58 -070091 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Al Viro7cc90cc2011-03-18 09:04:20 -040093 err = follow_down(&path);
David Howellscc53ce52011-01-14 18:45:26 +000094 if (err < 0)
95 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Al Viro91c9fa82009-04-18 02:42:05 -040097 exp2 = rqst_exp_get_by_name(rqstp, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (IS_ERR(exp2)) {
J. Bruce Fields3b6cee7bc2009-10-25 21:18:19 -040099 err = PTR_ERR(exp2);
100 /*
101 * We normally allow NFS clients to continue
102 * "underneath" a mountpoint that is not exported.
103 * The exception is V4ROOT, where no traversal is ever
104 * allowed without an explicit export of the new
105 * directory.
106 */
107 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
108 err = 0;
Al Viro91c9fa82009-04-18 02:42:05 -0400109 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 goto out;
111 }
Steve Dickson3c394dd2009-09-09 15:02:40 -0400112 if (nfsd_v4client(rqstp) ||
113 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /* successfully crossed mount point */
Al Viro1644ccc2009-04-18 02:32:31 -0400115 /*
Al Viro91c9fa82009-04-18 02:42:05 -0400116 * This is subtle: path.dentry is *not* on path.mnt
117 * at this point. The only reason we are safe is that
118 * original mnt is pinned down by exp, so we should
119 * put path *before* putting exp
Al Viro1644ccc2009-04-18 02:32:31 -0400120 */
Al Viro91c9fa82009-04-18 02:42:05 -0400121 *dpp = path.dentry;
122 path.dentry = dentry;
Al Viro1644ccc2009-04-18 02:32:31 -0400123 *expp = exp2;
Al Viro91c9fa82009-04-18 02:42:05 -0400124 exp2 = exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Al Viro91c9fa82009-04-18 02:42:05 -0400126 path_put(&path);
127 exp_put(exp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128out:
129 return err;
130}
131
J. Bruce Fields289ede42009-09-26 20:32:24 -0400132static void follow_to_parent(struct path *path)
133{
134 struct dentry *dp;
135
136 while (path->dentry == path->mnt->mnt_root && follow_up(path))
137 ;
138 dp = dget_parent(path->dentry);
139 dput(path->dentry);
140 path->dentry = dp;
141}
142
143static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
144{
145 struct svc_export *exp2;
146 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
147 .dentry = dget(dparent)};
148
149 follow_to_parent(&path);
150
151 exp2 = rqst_exp_parent(rqstp, &path);
152 if (PTR_ERR(exp2) == -ENOENT) {
153 *dentryp = dget(dparent);
154 } else if (IS_ERR(exp2)) {
155 path_put(&path);
156 return PTR_ERR(exp2);
157 } else {
158 *dentryp = dget(path.dentry);
159 exp_put(*exp);
160 *exp = exp2;
161 }
162 path_put(&path);
163 return 0;
164}
165
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400166/*
167 * For nfsd purposes, we treat V4ROOT exports as though there was an
168 * export at *every* directory.
169 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -0400170int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400171{
172 if (d_mountpoint(dentry))
173 return 1;
Trond Myklebust11fcee022011-09-12 19:37:26 -0400174 if (nfsd4_is_junction(dentry))
175 return 1;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400176 if (!(exp->ex_flags & NFSEXP_V4ROOT))
177 return 0;
David Howells2b0143b2015-03-17 22:25:59 +0000178 return d_inode(dentry) != NULL;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400179}
180
Al Viro6264d692006-10-19 23:28:58 -0700181__be32
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700182nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400183 const char *name, unsigned int len,
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700184 struct svc_export **exp_ret, struct dentry **dentry_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct svc_export *exp;
187 struct dentry *dparent;
188 struct dentry *dentry;
Al Viro6264d692006-10-19 23:28:58 -0700189 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 dparent = fhp->fh_dentry;
Kinglong Meebf18f162014-06-10 22:06:44 +0800194 exp = exp_get(fhp->fh_export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Lookup the name, but don't follow links */
197 if (isdotent(name, len)) {
198 if (len==1)
199 dentry = dget(dparent);
Jan Blunck54775492008-02-14 19:38:39 -0800200 else if (dparent != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 dentry = dget_parent(dparent);
J. Bruce Fieldsfed83812009-09-26 16:53:01 -0400202 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 dentry = dget(dparent); /* .. == . just like at / */
204 else {
205 /* checking mountpoint crossing is very different when stepping up */
J. Bruce Fields289ede42009-09-26 20:32:24 -0400206 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
207 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 } else {
J. Bruce Fields43357232014-01-24 18:04:40 -0500211 /*
212 * In the nfsd4_open() case, this may be held across
213 * subsequent open and delegation acquisition which may
214 * need to take the child's i_mutex:
215 */
216 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 dentry = lookup_one_len(name, dparent, len);
Al Viro6264d692006-10-19 23:28:58 -0700218 host_err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (IS_ERR(dentry))
220 goto out_nfserr;
221 /*
222 * check if we have crossed a mount point ...
223 */
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400224 if (nfsd_mountpoint(dentry, exp)) {
Al Viro6264d692006-10-19 23:28:58 -0700225 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 dput(dentry);
227 goto out_nfserr;
228 }
229 }
230 }
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700231 *dentry_ret = dentry;
232 *exp_ret = exp;
233 return 0;
234
235out_nfserr:
236 exp_put(exp);
237 return nfserrno(host_err);
238}
239
240/*
241 * Look up one component of a pathname.
242 * N.B. After this call _both_ fhp and resfh need an fh_put
243 *
244 * If the lookup would cross a mountpoint, and the mounted filesystem
245 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
246 * accepted as it stands and the mounted directory is
247 * returned. Otherwise the covered directory is returned.
248 * NOTE: this mountpoint crossing is not supported properly by all
249 * clients and is explicitly disallowed for NFSv3
250 * NeilBrown <neilb@cse.unsw.edu.au>
251 */
252__be32
253nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400254 unsigned int len, struct svc_fh *resfh)
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700255{
256 struct svc_export *exp;
257 struct dentry *dentry;
258 __be32 err;
259
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400260 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
261 if (err)
262 return err;
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700263 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
264 if (err)
265 return err;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700266 err = check_nfsd_access(exp, rqstp);
267 if (err)
268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /*
270 * Note: we compose the file handle now, but as the
271 * dentry may be negative, it may need to be updated.
272 */
273 err = fh_compose(resfh, exp, dentry, fhp);
David Howells2b0143b2015-03-17 22:25:59 +0000274 if (!err && d_really_is_negative(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 err = nfserr_noent;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700276out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 exp_put(exp);
279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
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{
David Howells2b0143b2015-03-17 22:25:59 +0000288 struct inode *inode = d_inode(fhp->fh_dentry);
Ben Myersf5019122010-02-17 14:05:11 -0600289 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 Hellwigc37650162010-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 Hellwig818e5a22013-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{
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800306 /* sanitize the mode change */
307 if (iap->ia_valid & ATTR_MODE) {
308 iap->ia_mode &= S_IALLUGO;
309 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
310 }
311
312 /* Revoke setuid/setgid on chown */
313 if (!S_ISDIR(inode->i_mode) &&
Stanislav Kholmanskikhc4fa6d72013-12-11 14:16:36 +0400314 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800315 iap->ia_valid |= ATTR_KILL_PRIV;
316 if (iap->ia_valid & ATTR_MODE) {
317 /* we're setting mode too, just clear the s*id bits */
318 iap->ia_mode &= ~S_ISUID;
319 if (iap->ia_mode & S_IXGRP)
320 iap->ia_mode &= ~S_ISGID;
321 } else {
322 /* set ATTR_KILL_* bits and let VFS handle it */
323 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
324 }
325 }
326}
327
328static __be32
329nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
330 struct iattr *iap)
331{
David Howells2b0143b2015-03-17 22:25:59 +0000332 struct inode *inode = d_inode(fhp->fh_dentry);
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800333 int host_err;
334
335 if (iap->ia_size < inode->i_size) {
336 __be32 err;
337
338 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
339 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
340 if (err)
341 return err;
342 }
343
344 host_err = get_write_access(inode);
345 if (host_err)
346 goto out_nfserrno;
347
348 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
349 if (host_err)
350 goto out_put_write_access;
351 return 0;
352
353out_put_write_access:
354 put_write_access(inode);
355out_nfserrno:
356 return nfserrno(host_err);
357}
358
359/*
360 * Set various file attributes. After this call fhp needs an fh_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Al Viro6264d692006-10-19 23:28:58 -0700362__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
364 int check_guard, time_t guardtime)
365{
366 struct dentry *dentry;
367 struct inode *inode;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200368 int accmode = NFSD_MAY_SATTR;
Al Viro175a4eb2011-07-26 03:30:54 -0400369 umode_t ftype = 0;
Al Viro6264d692006-10-19 23:28:58 -0700370 __be32 err;
371 int host_err;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500372 bool get_write_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int size_change = 0;
374
375 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200376 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (iap->ia_valid & ATTR_SIZE)
378 ftype = S_IFREG;
379
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500380 /* Callers that do fh_verify should do the fh_want_write: */
381 get_write_count = !fhp->fh_dentry;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Get inode */
384 err = fh_verify(rqstp, fhp, ftype, accmode);
NeilBrown15b7a1b2005-11-07 01:00:23 -0800385 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto out;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500387 if (get_write_count) {
388 host_err = fh_want_write(fhp);
389 if (host_err)
390 return nfserrno(host_err);
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000394 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
NeilBrown15b7a1b2005-11-07 01:00:23 -0800396 /* Ignore any mode updates on symlinks */
397 if (S_ISLNK(inode->i_mode))
398 iap->ia_valid &= ~ATTR_MODE;
399
400 if (!iap->ia_valid)
401 goto out;
402
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800403 nfsd_sanitize_attrs(inode, iap);
404
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000405 /*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800406 * The size case is special, it changes the file in addition to the
407 * attributes.
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (iap->ia_valid & ATTR_SIZE) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800410 err = nfsd_get_write_access(rqstp, fhp, iap);
411 if (err)
412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 size_change = 1;
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700414
415 /*
416 * RFC5661, Section 18.30.4:
417 * Changing the size of a file with SETATTR indirectly
418 * changes the time_modify and change attributes.
419 *
420 * (and similar for the older RFCs)
421 */
422 if (iap->ia_size != i_size_read(inode))
423 iap->ia_valid |= ATTR_MTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 iap->ia_valid |= ATTR_CTIME;
427
Christoph Hellwig987da472013-11-18 05:07:47 -0800428 if (check_guard && guardtime != inode->i_ctime.tv_sec) {
429 err = nfserr_notsync;
430 goto out_put_write_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Christoph Hellwig987da472013-11-18 05:07:47 -0800432
Christoph Hellwig987da472013-11-18 05:07:47 -0800433 fh_lock(fhp);
434 host_err = notify_change(dentry, iap, NULL);
435 fh_unlock(fhp);
J. R. Okajima1406b912014-02-19 00:27:53 +0900436 err = nfserrno(host_err);
Christoph Hellwig987da472013-11-18 05:07:47 -0800437
Christoph Hellwig987da472013-11-18 05:07:47 -0800438out_put_write_access:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (size_change)
440 put_write_access(inode);
441 if (!err)
Jeff Layton722b6202014-07-03 07:54:19 -0400442 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443out:
444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800447#if defined(CONFIG_NFSD_V4)
Chuck Lever9b4146e2012-01-04 16:26:43 -0500448/*
449 * NFS junction information is stored in an extended attribute.
450 */
451#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
452
453/**
454 * nfsd4_is_junction - Test if an object could be an NFS junction
455 *
456 * @dentry: object to test
457 *
458 * Returns 1 if "dentry" appears to contain NFS junction information.
459 * Otherwise 0 is returned.
460 */
Trond Myklebust11fcee022011-09-12 19:37:26 -0400461int nfsd4_is_junction(struct dentry *dentry)
462{
David Howells2b0143b2015-03-17 22:25:59 +0000463 struct inode *inode = d_inode(dentry);
Trond Myklebust11fcee022011-09-12 19:37:26 -0400464
465 if (inode == NULL)
466 return 0;
467 if (inode->i_mode & S_IXUGO)
468 return 0;
469 if (!(inode->i_mode & S_ISVTX))
470 return 0;
Chuck Lever9b4146e2012-01-04 16:26:43 -0500471 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
Trond Myklebust11fcee022011-09-12 19:37:26 -0400472 return 0;
473 return 1;
474}
David Quigley18032ca2013-05-02 13:19:10 -0400475#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
476__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
477 struct xdr_netobj *label)
478{
479 __be32 error;
480 int host_error;
481 struct dentry *dentry;
482
483 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
484 if (error)
485 return error;
486
487 dentry = fhp->fh_dentry;
488
David Howells2b0143b2015-03-17 22:25:59 +0000489 mutex_lock(&d_inode(dentry)->i_mutex);
David Quigley18032ca2013-05-02 13:19:10 -0400490 host_error = security_inode_setsecctx(dentry, label->data, label->len);
David Howells2b0143b2015-03-17 22:25:59 +0000491 mutex_unlock(&d_inode(dentry)->i_mutex);
David Quigley18032ca2013-05-02 13:19:10 -0400492 return nfserrno(host_error);
493}
494#else
495__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
496 struct xdr_netobj *label)
497{
498 return nfserr_notsupp;
499}
500#endif
501
Christoph Hellwigffa01602015-12-03 12:59:52 +0100502__be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
503 u64 dst_pos, u64 count)
504{
505 return nfserrno(vfs_clone_file_range(src, src_pos, dst, dst_pos,
506 count));
507}
508
Anna Schumaker95d871f2014-11-07 14:44:26 -0500509__be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
510 struct file *file, loff_t offset, loff_t len,
511 int flags)
512{
Anna Schumaker95d871f2014-11-07 14:44:26 -0500513 int error;
514
515 if (!S_ISREG(file_inode(file)->i_mode))
516 return nfserr_inval;
517
Anna Schumaker95d871f2014-11-07 14:44:26 -0500518 error = vfs_fallocate(file, flags, offset, len);
519 if (!error)
520 error = commit_metadata(fhp);
521
522 return nfserrno(error);
523}
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400524#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526#ifdef CONFIG_NFSD_V3
527/*
528 * Check server access rights to a file system object
529 */
530struct accessmap {
531 u32 access;
532 int how;
533};
534static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200535 { NFS3_ACCESS_READ, NFSD_MAY_READ },
536 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
537 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
538 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 { 0, 0 }
541};
542
543static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200544 { NFS3_ACCESS_READ, NFSD_MAY_READ },
545 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
546 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
547 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
548 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 { 0, 0 }
551};
552
553static struct accessmap nfs3_anyaccess[] = {
554 /* Some clients - Solaris 2.6 at least, make an access call
555 * to the server to check for access for things like /dev/null
556 * (which really, the server doesn't care about). So
557 * We provide simple access checking for them, looking
558 * mainly at mode bits, and we make sure to ignore read-only
559 * filesystem checks
560 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200561 { NFS3_ACCESS_READ, NFSD_MAY_READ },
562 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
563 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
564 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 { 0, 0 }
567};
568
Al Viro6264d692006-10-19 23:28:58 -0700569__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
571{
572 struct accessmap *map;
573 struct svc_export *export;
574 struct dentry *dentry;
575 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700576 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200578 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (error)
580 goto out;
581
582 export = fhp->fh_export;
583 dentry = fhp->fh_dentry;
584
David Howellse36cb0b2015-01-29 12:02:35 +0000585 if (d_is_reg(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 map = nfs3_regaccess;
David Howellse36cb0b2015-01-29 12:02:35 +0000587 else if (d_is_dir(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 map = nfs3_diraccess;
589 else
590 map = nfs3_anyaccess;
591
592
593 query = *access;
594 for (; map->access; map++) {
595 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700596 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 sresult |= map->access;
599
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700600 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 switch (err2) {
602 case nfs_ok:
603 result |= map->access;
604 break;
605
606 /* the following error codes just mean the access was not allowed,
607 * rather than an error occurred */
608 case nfserr_rofs:
609 case nfserr_acces:
610 case nfserr_perm:
611 /* simply don't "or" in the access bit. */
612 break;
613 default:
614 error = err2;
615 goto out;
616 }
617 }
618 }
619 *access = result;
620 if (supported)
621 *supported = sresult;
622
623 out:
624 return error;
625}
626#endif /* CONFIG_NFSD_V3 */
627
J. Bruce Fields105f4622011-06-07 11:50:23 -0400628static int nfsd_open_break_lease(struct inode *inode, int access)
629{
630 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
J. Bruce Fields105f4622011-06-07 11:50:23 -0400632 if (access & NFSD_MAY_NOT_BREAK_LEASE)
633 return 0;
634 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
635 return break_lease(inode, mode | O_NONBLOCK);
636}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
639 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400640 * The may_flags argument indicates the type of open (read/write/lock)
641 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * N.B. After this call fhp needs an fh_put
643 */
Al Viro6264d692006-10-19 23:28:58 -0700644__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400645nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400646 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Al Viro765927b2012-06-26 21:58:53 +0400648 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct inode *inode;
Kinglong Mee8519f992014-09-03 08:14:06 +0800650 struct file *file;
Al Viro6264d692006-10-19 23:28:58 -0700651 int flags = O_RDONLY|O_LARGEFILE;
652 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400653 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
David Howellse0e81732009-09-02 09:13:40 +0100655 validate_process_creds();
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * If we get here, then the client has already done an "open",
659 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
660 * in case a chmod has now revoked permission.
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400661 *
662 * Arguably we should also allow the owner override for
663 * directories, but we never have and it doesn't seem to have
664 * caused anyone a problem. If we were to change this, note
665 * also that our filldir callbacks would need a variant of
666 * lookup_one_len that doesn't check permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400668 if (type == S_IFREG)
669 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
670 err = fh_verify(rqstp, fhp, type, may_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (err)
672 goto out;
673
Al Viro765927b2012-06-26 21:58:53 +0400674 path.mnt = fhp->fh_export->ex_path.mnt;
675 path.dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000676 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /* Disallow write access to files with the append-only bit set
679 * or any access when mandatory locking enabled
680 */
681 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400682 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400684 /*
685 * We must ignore files (but only files) which might have mandatory
686 * locks on them because there is no way to know if the accesser has
687 * the lock.
688 */
689 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 goto out;
691
692 if (!inode->i_fop)
693 goto out;
694
Bernd Schubert999448a2012-03-18 22:44:49 -0400695 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700696 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto out_nfserr;
698
Bernd Schubert999448a2012-03-18 22:44:49 -0400699 if (may_flags & NFSD_MAY_WRITE) {
700 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700701 flags = O_RDWR|O_LARGEFILE;
702 else
703 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Bernd Schubert999448a2012-03-18 22:44:49 -0400705
Kinglong Mee8519f992014-09-03 08:14:06 +0800706 file = dentry_open(&path, flags, current_cred());
707 if (IS_ERR(file)) {
708 host_err = PTR_ERR(file);
709 goto out_nfserr;
Bernd Schubert06effdb2012-03-18 22:44:50 -0400710 }
711
Linus Torvalds5e40d332014-10-12 10:13:55 -0400712 host_err = ima_file_check(file, may_flags, 0);
Kinglong Mee8519f992014-09-03 08:14:06 +0800713 if (host_err) {
Christoph Hellwigfd891452015-04-28 15:41:16 +0200714 fput(file);
Kinglong Mee8519f992014-09-03 08:14:06 +0800715 goto out_nfserr;
716 }
717
718 if (may_flags & NFSD_MAY_64BIT_COOKIE)
719 file->f_mode |= FMODE_64BITHASH;
720 else
721 file->f_mode |= FMODE_32BITHASH;
722
723 *filp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700725 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726out:
David Howellse0e81732009-09-02 09:13:40 +0100727 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return err;
729}
730
Christoph Hellwige749a462015-06-18 16:44:58 +0200731struct raparms *
732nfsd_init_raparms(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Christoph Hellwige749a462015-06-18 16:44:58 +0200734 struct inode *inode = file_inode(file);
735 dev_t dev = inode->i_sb->s_dev;
736 ino_t ino = inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct raparms *ra, **rap, **frap = NULL;
738 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700739 unsigned int hash;
740 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Greg Banksfce14562006-10-04 02:15:49 -0700742 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
743 rab = &raparm_hash[hash];
744
745 spin_lock(&rab->pb_lock);
746 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (ra->p_ino == ino && ra->p_dev == dev)
748 goto found;
749 depth++;
750 if (ra->p_count == 0)
751 frap = rap;
752 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300753 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700755 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return NULL;
757 }
758 rap = frap;
759 ra = *frap;
760 ra->p_dev = dev;
761 ra->p_ino = ino;
762 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700763 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764found:
Greg Banksfce14562006-10-04 02:15:49 -0700765 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700767 ra->p_next = rab->pb_head;
768 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 ra->p_count++;
771 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700772 spin_unlock(&rab->pb_lock);
Christoph Hellwige749a462015-06-18 16:44:58 +0200773
774 if (ra->p_set)
775 file->f_ra = ra->p_ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return ra;
777}
778
Christoph Hellwige749a462015-06-18 16:44:58 +0200779void nfsd_put_raparams(struct file *file, struct raparms *ra)
780{
781 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
782
783 spin_lock(&rab->pb_lock);
784 ra->p_ra = file->f_ra;
785 ra->p_set = 1;
786 ra->p_count--;
787 spin_unlock(&rab->pb_lock);
788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200791 * Grab and keep cached pages associated with a file in the svc_rqst
792 * so that they can be passed to the network sendmsg/sendpage routines
793 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
795static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200796nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
797 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Jens Axboecf8208d2007-06-12 21:22:14 +0200799 struct svc_rqst *rqstp = sd->u.data;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500800 struct page **pp = rqstp->rq_next_page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200801 struct page *page = buf->page;
802 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200803
804 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (rqstp->rq_res.page_len == 0) {
807 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500808 put_page(*rqstp->rq_next_page);
809 *(rqstp->rq_next_page++) = page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200810 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700812 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500814 if (*rqstp->rq_next_page)
815 put_page(*rqstp->rq_next_page);
816 *(rqstp->rq_next_page++) = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700818 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return size;
822}
823
Jens Axboecf8208d2007-06-12 21:22:14 +0200824static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
825 struct splice_desc *sd)
826{
827 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
828}
829
Jeff Laytone2afc812014-06-17 07:44:13 -0400830static __be32
831nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Al Viro6264d692006-10-19 23:28:58 -0700833 if (host_err >= 0) {
834 nfsdstats.io_read += host_err;
835 *count = host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500836 fsnotify_access(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 } else
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400839 return nfserrno(host_err);
840}
841
Jeff Laytone2afc812014-06-17 07:44:13 -0400842__be32 nfsd_splice_read(struct svc_rqst *rqstp,
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400843 struct file *file, loff_t offset, unsigned long *count)
844{
845 struct splice_desc sd = {
846 .len = 0,
847 .total_len = *count,
848 .pos = offset,
849 .u.data = rqstp,
850 };
851 int host_err;
852
853 rqstp->rq_next_page = rqstp->rq_respages + 1;
854 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
855 return nfsd_finish_read(file, count, host_err);
856}
857
Jeff Laytone2afc812014-06-17 07:44:13 -0400858__be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400859 unsigned long *count)
860{
861 mm_segment_t oldfs;
862 int host_err;
863
864 oldfs = get_fs();
865 set_fs(KERNEL_DS);
866 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
867 set_fs(oldfs);
868 return nfsd_finish_read(file, count, host_err);
869}
870
871static __be32
872nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
873 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
874{
Jeff Layton779fb0f2014-11-19 07:51:18 -0500875 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400876 return nfsd_splice_read(rqstp, file, offset, count);
877 else
878 return nfsd_readv(file, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700881/*
882 * Gathered writes: If another process is currently writing to the file,
883 * there's a high chance this is another nfsd (triggered by a bulk write
884 * from a client's biod). Rather than syncing the file with each write
885 * request, we sleep for 10 msec.
886 *
887 * I don't know if this roughly approximates C. Juszak's idea of
888 * gathered writes, but it's a nice and simple solution (IMHO), and it
889 * seems to work:-)
890 *
891 * Note: we do this only in the NFSv2 case, since v3 and higher have a
892 * better tool (separate unstable writes and commits) for solving this
893 * problem.
894 */
895static int wait_for_concurrent_writes(struct file *file)
896{
Al Viro496ad9a2013-01-23 17:07:38 -0500897 struct inode *inode = file_inode(file);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700898 static ino_t last_ino;
899 static dev_t last_dev;
900 int err = 0;
901
902 if (atomic_read(&inode->i_writecount) > 1
903 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
904 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
905 msleep(10);
906 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
907 }
908
909 if (inode->i_state & I_DIRTY) {
910 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100911 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700912 }
913 last_ino = inode->i_ino;
914 last_dev = inode->i_sb->s_dev;
915 return err;
916}
917
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200918__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
920 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -0500921 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct svc_export *exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct inode *inode;
925 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700926 __be32 err = 0;
927 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400929 int use_wgather;
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700930 loff_t pos = offset;
Zach Browne77a7b42014-10-06 16:42:52 -0700931 loff_t end = LLONG_MAX;
NeilBrown86584522014-05-12 11:22:47 +1000932 unsigned int pflags = current->flags;
933
Jeff Layton7501cc22014-11-19 07:51:15 -0500934 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
NeilBrown86584522014-05-12 11:22:47 +1000935 /*
936 * We want less throttling in balance_dirty_pages()
937 * and shrink_inactive_list() so that nfs to
938 * localhost doesn't cause nfsd to lock up due to all
939 * the client's dirty pages or its congested queue.
940 */
941 current->flags |= PF_LESS_THROTTLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Al Viro6f4e0d52014-10-31 02:42:53 -0400943 inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 exp = fhp->fh_export;
945
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400946 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (!EX_ISSYNC(exp))
949 stable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* Write the data. */
952 oldfs = get_fs(); set_fs(KERNEL_DS);
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700953 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700955 if (host_err < 0)
956 goto out_nfserr;
957 *cnt = host_err;
958 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500959 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400961 if (stable) {
Zach Browne77a7b42014-10-06 16:42:52 -0700962 if (use_wgather) {
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400963 host_err = wait_for_concurrent_writes(file);
Zach Browne77a7b42014-10-06 16:42:52 -0700964 } else {
965 if (*cnt)
966 end = offset + *cnt - 1;
967 host_err = vfs_fsync_range(file, offset, end, 0);
968 }
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700971out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700972 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800973 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800975 else
Al Viro6264d692006-10-19 23:28:58 -0700976 err = nfserrno(host_err);
Jeff Layton7501cc22014-11-19 07:51:15 -0500977 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
NeilBrown86584522014-05-12 11:22:47 +1000978 tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return err;
980}
981
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400982/*
983 * Read data from a file. count must contain the requested read count
984 * on entry. On return, *count contains the number of bytes actually read.
985 * N.B. After this call fhp needs an fh_put
986 */
987__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
988 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
989{
990 struct file *file;
991 struct raparms *ra;
992 __be32 err;
993
Christoph Hellwige749a462015-06-18 16:44:58 +0200994 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400995 if (err)
996 return err;
997
Christoph Hellwige749a462015-06-18 16:44:58 +0200998 ra = nfsd_init_raparms(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400999 err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
Christoph Hellwige749a462015-06-18 16:44:58 +02001000 if (ra)
1001 nfsd_put_raparams(file, ra);
1002 fput(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001003
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001004 return err;
1005}
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007/*
1008 * Write data to a file.
1009 * The stable flag requests synchronous writes.
1010 * N.B. After this call fhp needs an fh_put
1011 */
Al Viro6264d692006-10-19 23:28:58 -07001012__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001014 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int *stablep)
1016{
Al Viro6264d692006-10-19 23:28:58 -07001017 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001020 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001021 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (err)
1023 goto out;
1024 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1025 stablep);
1026 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001027 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (err)
1029 goto out;
1030
1031 if (cnt)
1032 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1033 cnt, stablep);
Christoph Hellwigfd891452015-04-28 15:41:16 +02001034 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036out:
1037 return err;
1038}
1039
1040#ifdef CONFIG_NFSD_V3
1041/*
1042 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001043 *
1044 * Note: we only guarantee that data that lies within the range specified
1045 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 *
1047 * Unfortunately we cannot lock the file to make sure we return full WCC
1048 * data to the client, as locking happens lower down in the filesystem.
1049 */
Al Viro6264d692006-10-19 23:28:58 -07001050__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1052 loff_t offset, unsigned long count)
1053{
1054 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001055 loff_t end = LLONG_MAX;
1056 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Trond Myklebustaa696a62010-01-29 16:44:25 -05001058 if (offset < 0)
1059 goto out;
1060 if (count != 0) {
1061 end = offset + (loff_t)count - 1;
1062 if (end < offset)
1063 goto out;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Jeff Layton91885252010-03-19 08:06:28 -04001066 err = nfsd_open(rqstp, fhp, S_IFREG,
1067 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001068 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001071 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001072
1073 if (err2 != -EINVAL)
1074 err = nfserrno(err2);
1075 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Christoph Hellwigfd891452015-04-28 15:41:16 +02001079 fput(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001080out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return err;
1082}
1083#endif /* CONFIG_NFSD_V3 */
1084
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001085static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001086nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1087 struct iattr *iap)
1088{
1089 /*
1090 * Mode has already been set earlier in create:
1091 */
1092 iap->ia_valid &= ~ATTR_MODE;
1093 /*
1094 * Setting uid/gid works only for root. Irix appears to
1095 * send along the gid on create when it tries to implement
1096 * setgid directories via NFS:
1097 */
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001098 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001099 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1100 if (iap->ia_valid)
1101 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001102 /* Callers expect file metadata to be committed here */
Jeff Layton722b6202014-07-03 07:54:19 -04001103 return nfserrno(commit_metadata(resfhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001104}
1105
wengang wang4ac35c22009-02-10 11:27:51 +08001106/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1107 * setting size to 0 may fail for some specific file systems by the permission
1108 * checking which requires WRITE permission but the mode is 000.
1109 * we ignore the resizing(to 0) on the just new created file, since the size is
1110 * 0 after file created.
1111 *
1112 * call this only after vfs_create() is called.
1113 * */
1114static void
1115nfsd_check_ignore_resizing(struct iattr *iap)
1116{
1117 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1118 iap->ia_valid &= ~ATTR_SIZE;
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/*
1122 * Create a file (regular, directory, device, fifo); UNIX sockets
1123 * not yet implemented.
1124 * If the response fh has been verified, the parent directory should
1125 * already be locked. Note that the parent directory is left locked.
1126 *
1127 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1128 */
Al Viro6264d692006-10-19 23:28:58 -07001129__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1131 char *fname, int flen, struct iattr *iap,
1132 int type, dev_t rdev, struct svc_fh *resfhp)
1133{
1134 struct dentry *dentry, *dchild = NULL;
1135 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001136 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001137 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001138 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 err = nfserr_perm;
1141 if (!flen)
1142 goto out;
1143 err = nfserr_exist;
1144 if (isdotent(fname, flen))
1145 goto out;
1146
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001147 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (err)
1149 goto out;
1150
1151 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001152 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001155 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 goto out;
1157 /*
1158 * Check whether the response file handle has been verified yet.
1159 * If it has, the parent directory should already be locked.
1160 */
1161 if (!resfhp->fh_dentry) {
Jan Kara4a55c102012-06-12 16:20:33 +02001162 host_err = fh_want_write(fhp);
1163 if (host_err)
1164 goto out_nfserr;
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001167 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001169 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (IS_ERR(dchild))
1171 goto out_nfserr;
1172 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1173 if (err)
1174 goto out;
1175 } else {
1176 /* called from nfsd_proc_create */
1177 dchild = dget(resfhp->fh_dentry);
1178 if (!fhp->fh_locked) {
1179 /* not actually possible */
1180 printk(KERN_ERR
Al Viroa6a9f182013-09-16 10:57:01 -04001181 "nfsd_create: parent %pd2 not locked!\n",
1182 dentry);
Al Virod75f2b92006-01-18 17:43:44 -08001183 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto out;
1185 }
1186 }
1187 /*
1188 * Make sure the child dentry is still negative ...
1189 */
1190 err = nfserr_exist;
David Howells2b0143b2015-03-17 22:25:59 +00001191 if (d_really_is_positive(dchild)) {
Al Viroa6a9f182013-09-16 10:57:01 -04001192 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1193 dentry, dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto out;
1195 }
1196
1197 if (!(iap->ia_valid & ATTR_MODE))
1198 iap->ia_mode = 0;
1199 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1200
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001201 err = nfserr_inval;
1202 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1203 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1204 type);
1205 goto out;
1206 }
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /*
1209 * Get the dir op function pointer.
1210 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001211 err = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001212 host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 switch (type) {
1214 case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04001215 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001216 if (!host_err)
1217 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 break;
1219 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001220 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 break;
1222 case S_IFCHR:
1223 case S_IFBLK:
1224 case S_IFIFO:
1225 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001226 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001228 }
Jan Kara4a55c102012-06-12 16:20:33 +02001229 if (host_err < 0)
Dave Hansen463c3192008-02-15 14:37:57 -08001230 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Ben Myersf5019122010-02-17 14:05:11 -06001232 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Ben Myersf5019122010-02-17 14:05:11 -06001234 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001235 * nfsd_create_setattr already committed the child. Transactional
1236 * filesystems had a chance to commit changes for both parent and
1237 * child * simultaneously making the following commit_metadata a
1238 * noop.
Ben Myersf5019122010-02-17 14:05:11 -06001239 */
1240 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001241 if (err2)
1242 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * Update the file handle to get the new inode info.
1245 */
1246 if (!err)
1247 err = fh_update(resfhp);
1248out:
1249 if (dchild && !IS_ERR(dchild))
1250 dput(dchild);
1251 return err;
1252
1253out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001254 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 goto out;
1256}
1257
1258#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001261 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
Al Viro6264d692006-10-19 23:28:58 -07001263__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001264do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 char *fname, int flen, struct iattr *iap,
1266 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001267 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 struct dentry *dentry, *dchild = NULL;
1270 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001271 __be32 err;
1272 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 err = nfserr_perm;
1276 if (!flen)
1277 goto out;
1278 err = nfserr_exist;
1279 if (isdotent(fname, flen))
1280 goto out;
1281 if (!(iap->ia_valid & ATTR_MODE))
1282 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001283 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (err)
1285 goto out;
1286
1287 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001288 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /* Get all the sanity checks out of the way before
1291 * we lock the parent. */
1292 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001293 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001295
1296 host_err = fh_want_write(fhp);
1297 if (host_err)
1298 goto out_nfserr;
1299
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001300 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /*
1303 * Compose the response file handle.
1304 */
1305 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001306 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (IS_ERR(dchild))
1308 goto out_nfserr;
1309
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001310 /* If file doesn't exist, check for permissions to create one */
David Howells2b0143b2015-03-17 22:25:59 +00001311 if (d_really_is_negative(dchild)) {
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001312 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1313 if (err)
1314 goto out;
1315 }
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1318 if (err)
1319 goto out;
1320
Mi Jinlongac6721a2011-04-20 17:06:25 +08001321 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001322 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001323 * the high bit set, so just clear the high bits. If this is
1324 * ever changed to use different attrs for storing the
1325 * verifier, then do_open_lookup() will also need to be fixed
1326 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
1328 v_mtime = verifier[0]&0x7fffffff;
1329 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
David Howells2b0143b2015-03-17 22:25:59 +00001332 if (d_really_is_positive(dchild)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 err = 0;
1334
1335 switch (createmode) {
1336 case NFS3_CREATE_UNCHECKED:
David Howellse36cb0b2015-01-29 12:02:35 +00001337 if (! d_is_reg(dchild))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 else if (truncp) {
1340 /* in nfsv4, we need to treat this case a little
1341 * differently. we don't want to truncate the
1342 * file now; this would be wrong if the OPEN
1343 * fails for some other reason. furthermore,
1344 * if the size is nonzero, we should ignore it
1345 * according to spec!
1346 */
1347 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1348 }
1349 else {
1350 iap->ia_valid &= ATTR_SIZE;
1351 goto set_attr;
1352 }
1353 break;
1354 case NFS3_CREATE_EXCLUSIVE:
David Howells2b0143b2015-03-17 22:25:59 +00001355 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1356 && d_inode(dchild)->i_atime.tv_sec == v_atime
1357 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001358 if (created)
1359 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
Neil Brown7007c902012-12-07 15:40:55 -05001361 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001362 case NFS4_CREATE_EXCLUSIVE4_1:
David Howells2b0143b2015-03-17 22:25:59 +00001363 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1364 && d_inode(dchild)->i_atime.tv_sec == v_atime
1365 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001366 if (created)
1367 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001368 goto set_attr;
Neil Brown7007c902012-12-07 15:40:55 -05001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /* fallthru */
1371 case NFS3_CREATE_GUARDED:
1372 err = nfserr_exist;
1373 }
Al Virobad0dcf2011-11-23 12:03:18 -05001374 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 goto out;
1376 }
1377
Al Viro312b63f2012-06-10 18:09:36 -04001378 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001379 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001380 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001382 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001383 if (created)
1384 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
wengang wang4ac35c22009-02-10 11:27:51 +08001386 nfsd_check_ignore_resizing(iap);
1387
Mi Jinlongac6721a2011-04-20 17:06:25 +08001388 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001389 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001391 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* XXX someone who knows this better please fix it for nsec */
1393 iap->ia_mtime.tv_sec = v_mtime;
1394 iap->ia_atime.tv_sec = v_atime;
1395 iap->ia_mtime.tv_nsec = 0;
1396 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001400 err = nfsd_create_setattr(rqstp, resfhp, iap);
1401
1402 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001403 * nfsd_create_setattr already committed the child
1404 * (and possibly also the parent).
Ben Myersf5019122010-02-17 14:05:11 -06001405 */
1406 if (!err)
1407 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001408
1409 /*
1410 * Update the filehandle to get the new inode info.
1411 */
1412 if (!err)
1413 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 out:
1416 fh_unlock(fhp);
1417 if (dchild && !IS_ERR(dchild))
1418 dput(dchild);
Jan Kara4a55c102012-06-12 16:20:33 +02001419 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return err;
1421
1422 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001423 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 goto out;
1425}
1426#endif /* CONFIG_NFSD_V3 */
1427
1428/*
1429 * Read a symlink. On entry, *lenp must contain the maximum path length that
1430 * fits into the buffer. On return, it contains the true length.
1431 * N.B. After this call fhp needs an fh_put
1432 */
Al Viro6264d692006-10-19 23:28:58 -07001433__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1435{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 struct inode *inode;
1437 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001438 __be32 err;
1439 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001440 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001442 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (err)
1444 goto out;
1445
Al Viro68ac1232012-03-15 08:21:57 -04001446 path.mnt = fhp->fh_export->ex_path.mnt;
1447 path.dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001448 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001451 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 goto out;
1453
Al Viro68ac1232012-03-15 08:21:57 -04001454 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /* N.B. Why does this call need a get_fs()??
1456 * Remove the set_fs and watch the fireworks:-) --okir
1457 */
1458
1459 oldfs = get_fs(); set_fs(KERNEL_DS);
J. Bruce Fieldsfac7a172012-07-27 14:16:55 -04001460 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 set_fs(oldfs);
1462
Al Viro6264d692006-10-19 23:28:58 -07001463 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001465 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 err = 0;
1467out:
1468 return err;
1469
1470out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001471 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto out;
1473}
1474
1475/*
1476 * Create a symlink and look up its inode
1477 * N.B. After this call _both_ fhp and resfhp need an fh_put
1478 */
Al Viro6264d692006-10-19 23:28:58 -07001479__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1481 char *fname, int flen,
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001482 char *path,
Kinglong Mee1e444f52014-07-01 17:48:02 +08001483 struct svc_fh *resfhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001486 __be32 err, cerr;
1487 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 err = nfserr_noent;
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001490 if (!flen || path[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 goto out;
1492 err = nfserr_exist;
1493 if (isdotent(fname, flen))
1494 goto out;
1495
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001496 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (err)
1498 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001499
1500 host_err = fh_want_write(fhp);
1501 if (host_err)
1502 goto out_nfserr;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 fh_lock(fhp);
1505 dentry = fhp->fh_dentry;
1506 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001507 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (IS_ERR(dnew))
1509 goto out_nfserr;
1510
David Howells2b0143b2015-03-17 22:25:59 +00001511 host_err = vfs_symlink(d_inode(dentry), dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001512 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001513 if (!err)
1514 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 fh_unlock(fhp);
1516
Al Virobad0dcf2011-11-23 12:03:18 -05001517 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1520 dput(dnew);
1521 if (err==0) err = cerr;
1522out:
1523 return err;
1524
1525out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001526 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 goto out;
1528}
1529
1530/*
1531 * Create a hardlink
1532 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1533 */
Al Viro6264d692006-10-19 23:28:58 -07001534__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1536 char *name, int len, struct svc_fh *tfhp)
1537{
1538 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001539 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001540 __be32 err;
1541 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001543 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (err)
1545 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001546 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (err)
1548 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001549 err = nfserr_isdir;
David Howellse36cb0b2015-01-29 12:02:35 +00001550 if (d_is_dir(tfhp->fh_dentry))
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 err = nfserr_perm;
1553 if (!len)
1554 goto out;
1555 err = nfserr_exist;
1556 if (isdotent(name, len))
1557 goto out;
1558
Jan Kara4a55c102012-06-12 16:20:33 +02001559 host_err = fh_want_write(tfhp);
1560 if (host_err) {
1561 err = nfserrno(host_err);
1562 goto out;
1563 }
1564
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001565 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 ddir = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001567 dirp = d_inode(ddir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001570 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (IS_ERR(dnew))
1572 goto out_nfserr;
1573
1574 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001576 err = nfserr_noent;
David Howells2b0143b2015-03-17 22:25:59 +00001577 if (d_really_is_negative(dold))
Jan Kara4a55c102012-06-12 16:20:33 +02001578 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04001579 host_err = vfs_link(dold, dirp, dnew, NULL);
Al Viro6264d692006-10-19 23:28:58 -07001580 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001581 err = nfserrno(commit_metadata(ffhp));
1582 if (!err)
1583 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 } else {
Al Viro6264d692006-10-19 23:28:58 -07001585 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 err = nfserr_acces;
1587 else
Al Viro6264d692006-10-19 23:28:58 -07001588 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001590out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001592out_unlock:
1593 fh_unlock(ffhp);
Jan Kara4a55c102012-06-12 16:20:33 +02001594 fh_drop_write(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595out:
1596 return err;
1597
1598out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001599 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001600 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603/*
1604 * Rename a file
1605 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1606 */
Al Viro6264d692006-10-19 23:28:58 -07001607__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1609 struct svc_fh *tfhp, char *tname, int tlen)
1610{
1611 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1612 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001613 __be32 err;
1614 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001616 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (err)
1618 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001619 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (err)
1621 goto out;
1622
1623 fdentry = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001624 fdir = d_inode(fdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 tdentry = tfhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001627 tdir = d_inode(tdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 err = nfserr_perm;
1630 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1631 goto out;
1632
Jan Kara4a55c102012-06-12 16:20:33 +02001633 host_err = fh_want_write(ffhp);
1634 if (host_err) {
1635 err = nfserrno(host_err);
1636 goto out;
1637 }
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /* cannot use fh_lock as we need deadlock protective ordering
1640 * so do it by hand */
1641 trap = lock_rename(tdentry, fdentry);
Jeff Laytonaaf91ec2015-09-17 08:28:39 -04001642 ffhp->fh_locked = tfhp->fh_locked = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 fill_pre_wcc(ffhp);
1644 fill_pre_wcc(tfhp);
1645
1646 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001647 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (IS_ERR(odentry))
1649 goto out_nfserr;
1650
Al Viro6264d692006-10-19 23:28:58 -07001651 host_err = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001652 if (d_really_is_negative(odentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001654 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (odentry == trap)
1656 goto out_dput_old;
1657
1658 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001659 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (IS_ERR(ndentry))
1661 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001662 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (ndentry == trap)
1664 goto out_dput_new;
1665
Dave Hansen9079b1e2008-02-15 14:37:49 -08001666 host_err = -EXDEV;
1667 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1668 goto out_dput_new;
J. Bruce Fieldsaa387d6c2013-04-15 16:03:46 -04001669 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1670 goto out_dput_new;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001671
Miklos Szeredi520c8b12014-04-01 17:08:42 +02001672 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
Ben Myersf5019122010-02-17 14:05:11 -06001673 if (!host_err) {
1674 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001675 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001676 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 out_dput_new:
1679 dput(ndentry);
1680 out_dput_old:
1681 dput(odentry);
1682 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001683 err = nfserrno(host_err);
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001684 /*
1685 * We cannot rely on fh_unlock on the two filehandles,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * as that would do the wrong thing if the two directories
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001687 * were the same, so again we do it by hand.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
1689 fill_post_wcc(ffhp);
1690 fill_post_wcc(tfhp);
1691 unlock_rename(tdentry, fdentry);
Jeff Laytonaaf91ec2015-09-17 08:28:39 -04001692 ffhp->fh_locked = tfhp->fh_locked = false;
Jan Kara4a55c102012-06-12 16:20:33 +02001693 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695out:
1696 return err;
1697}
1698
1699/*
1700 * Unlink a file or directory
1701 * N.B. After this call fhp needs an fh_put
1702 */
Al Viro6264d692006-10-19 23:28:58 -07001703__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1705 char *fname, int flen)
1706{
1707 struct dentry *dentry, *rdentry;
1708 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001709 __be32 err;
1710 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 err = nfserr_acces;
1713 if (!flen || isdotent(fname, flen))
1714 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001715 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (err)
1717 goto out;
1718
Jan Kara4a55c102012-06-12 16:20:33 +02001719 host_err = fh_want_write(fhp);
1720 if (host_err)
1721 goto out_nfserr;
1722
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001723 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001725 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001728 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (IS_ERR(rdentry))
1730 goto out_nfserr;
1731
David Howells2b0143b2015-03-17 22:25:59 +00001732 if (d_really_is_negative(rdentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 dput(rdentry);
1734 err = nfserr_noent;
1735 goto out;
1736 }
1737
1738 if (!type)
David Howells2b0143b2015-03-17 22:25:59 +00001739 type = d_inode(rdentry)->i_mode & S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001741 if (type != S_IFDIR)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04001742 host_err = vfs_unlink(dirp, rdentry, NULL);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001743 else
Al Viro6264d692006-10-19 23:28:58 -07001744 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001745 if (!host_err)
1746 host_err = commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 dput(rdentry);
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001750 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001751out:
1752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
1755/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001756 * We do this buffering because we must not call back into the file
1757 * system's ->lookup() method from the filldir callback. That may well
1758 * deadlock a number of file systems.
1759 *
1760 * This is based heavily on the implementation of same in XFS.
1761 */
1762struct buffered_dirent {
1763 u64 ino;
1764 loff_t offset;
1765 int namlen;
1766 unsigned int d_type;
1767 char name[];
1768};
1769
1770struct readdir_data {
Al Viro5c0ba4e2013-05-15 13:52:59 -04001771 struct dir_context ctx;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001772 char *dirent;
1773 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001774 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001775};
1776
Miklos Szerediac7576f2014-10-30 17:37:34 +01001777static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1778 int namlen, loff_t offset, u64 ino,
1779 unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001780{
Miklos Szerediac7576f2014-10-30 17:37:34 +01001781 struct readdir_data *buf =
1782 container_of(ctx, struct readdir_data, ctx);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001783 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1784 unsigned int reclen;
1785
1786 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001787 if (buf->used + reclen > PAGE_SIZE) {
1788 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001789 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001790 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001791
1792 de->namlen = namlen;
1793 de->offset = offset;
1794 de->ino = ino;
1795 de->d_type = d_type;
1796 memcpy(de->name, name, namlen);
1797 buf->used += reclen;
1798
1799 return 0;
1800}
1801
Miklos Szerediac7576f2014-10-30 17:37:34 +01001802static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
David Woodhouse2f9092e2009-04-20 23:18:37 +01001803 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001804{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001805 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001806 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001807 int size;
1808 loff_t offset;
Al Viroac6614b2013-05-22 22:22:04 -04001809 struct readdir_data buf = {
1810 .ctx.actor = nfsd_buffered_filldir,
1811 .dirent = (void *)__get_free_page(GFP_KERNEL)
1812 };
David Woodhouse2628b762008-07-31 17:16:51 +01001813
David Woodhouse14f7dd62008-07-31 20:29:12 +01001814 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001815 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001816
David Woodhouse14f7dd62008-07-31 20:29:12 +01001817 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001818
1819 while (1) {
Al Viro496ad9a2013-01-23 17:07:38 -05001820 struct inode *dir_inode = file_inode(file);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001821 unsigned int reclen;
1822
Doug Nazarb726e922008-11-05 06:16:28 -05001823 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001824 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001825 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001826
Al Viro5c0ba4e2013-05-15 13:52:59 -04001827 host_err = iterate_dir(file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -04001828 if (buf.full)
1829 host_err = 0;
1830
1831 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001832 break;
1833
1834 size = buf.used;
1835
1836 if (!size)
1837 break;
1838
David Woodhouse2f9092e2009-04-20 23:18:37 +01001839 /*
1840 * Various filldir functions may end up calling back into
1841 * lookup_one_len() and the file system's ->lookup() method.
1842 * These expect i_mutex to be held, as it would within readdir.
1843 */
1844 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1845 if (host_err)
1846 break;
1847
David Woodhouse14f7dd62008-07-31 20:29:12 +01001848 de = (struct buffered_dirent *)buf.dirent;
1849 while (size > 0) {
1850 offset = de->offset;
1851
1852 if (func(cdp, de->name, de->namlen, de->offset,
1853 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01001854 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001855
1856 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001857 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001858
1859 reclen = ALIGN(sizeof(*de) + de->namlen,
1860 sizeof(u64));
1861 size -= reclen;
1862 de = (struct buffered_dirent *)((char *)de + reclen);
1863 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01001864 mutex_unlock(&dir_inode->i_mutex);
1865 if (size > 0) /* We bailed out early */
1866 break;
1867
David Woodhousec002a6c2008-08-17 17:21:18 +01001868 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001869 }
1870
David Woodhouse14f7dd62008-07-31 20:29:12 +01001871 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01001872
1873 if (host_err)
1874 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001875
1876 *offsetp = offset;
1877 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01001878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880/*
1881 * Read entries from a directory.
1882 * The NFSv3/4 verifier we ignore for now.
1883 */
Al Viro6264d692006-10-19 23:28:58 -07001884__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
Miklos Szerediac7576f2014-10-30 17:37:34 +01001886 struct readdir_cd *cdp, nfsd_filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Al Viro6264d692006-10-19 23:28:58 -07001888 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 struct file *file;
1890 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04001891 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Bernd Schubert06effdb2012-03-18 22:44:50 -04001893 /* NFSv2 only supports 32 bit cookies */
1894 if (rqstp->rq_vers > 2)
1895 may_flags |= NFSD_MAY_64BIT_COOKIE;
1896
1897 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (err)
1899 goto out;
1900
Jeff Laytonb108fe62012-04-25 15:30:00 -04001901 offset = vfs_llseek(file, offset, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (offset < 0) {
1903 err = nfserrno((int)offset);
1904 goto out_close;
1905 }
1906
David Woodhouse14f7dd62008-07-31 20:29:12 +01001907 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 if (err == nfserr_eof || err == nfserr_toosmall)
1910 err = nfs_ok; /* can still be found in ->err */
1911out_close:
Christoph Hellwigfd891452015-04-28 15:41:16 +02001912 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913out:
1914 return err;
1915}
1916
1917/*
1918 * Get file system stats
1919 * N.B. After this call fhp needs an fh_put
1920 */
Al Viro6264d692006-10-19 23:28:58 -07001921__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04001922nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02001924 __be32 err;
1925
1926 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02001927 if (!err) {
1928 struct path path = {
1929 .mnt = fhp->fh_export->ex_path.mnt,
1930 .dentry = fhp->fh_dentry,
1931 };
1932 if (vfs_statfs(&path, stat))
1933 err = nfserr_io;
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return err;
1936}
1937
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001938static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001939{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001940 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001941}
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943/*
1944 * Check for a user's access permissions to this inode.
1945 */
Al Viro6264d692006-10-19 23:28:58 -07001946__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001947nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
1948 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
David Howells2b0143b2015-03-17 22:25:59 +00001950 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 int err;
1952
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04001953 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return 0;
1955#if 0
1956 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1957 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001958 (acc & NFSD_MAY_READ)? " read" : "",
1959 (acc & NFSD_MAY_WRITE)? " write" : "",
1960 (acc & NFSD_MAY_EXEC)? " exec" : "",
1961 (acc & NFSD_MAY_SATTR)? " sattr" : "",
1962 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
1963 (acc & NFSD_MAY_LOCK)? " lock" : "",
1964 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 inode->i_mode,
1966 IS_IMMUTABLE(inode)? " immut" : "",
1967 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08001968 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11001970 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971#endif
1972
1973 /* Normally we reject any write/sattr etc access on a read-only file
1974 * system. But if it is IRIX doing check on write-access for a
1975 * device special file, we ignore rofs.
1976 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001977 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
1978 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08001979 if (exp_rdonly(rqstp, exp) ||
1980 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001982 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return nfserr_perm;
1984 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001985 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return nfserr_perm;
1987
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001988 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /* If we cannot rely on authentication in NLM requests,
1990 * just allow locks, otherwise require read permission, or
1991 * ownership
1992 */
1993 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1994 return 0;
1995 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001996 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 /*
1999 * The file owner always gets access permission for accesses that
2000 * would normally be checked at open time. This is to make
2001 * file access work even when the client has done a fchmod(fd, 0).
2002 *
2003 * However, `cp foo bar' should fail nevertheless when bar is
2004 * readonly. A sensible way to do this might be to reject all
2005 * attempts to truncate a read-only file, because a creat() call
2006 * always implies file truncation.
2007 * ... but this isn't really fair. A process may reasonably call
2008 * ftruncate on an open file descriptor on a file with perm 000.
2009 * We must trust the client to do permission checking - using "ACCESS"
2010 * with NFSv3.
2011 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002012 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002013 uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002016 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002017 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 /* Allow read access to binaries even when mode 111 */
2020 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002021 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2022 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002023 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 return err? nfserrno(err) : 0;
2026}
2027
2028void
2029nfsd_racache_shutdown(void)
2030{
Jeff Layton54a66e52008-08-13 22:03:27 -04002031 struct raparms *raparm, *last_raparm;
2032 unsigned int i;
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002035
2036 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2037 raparm = raparm_hash[i].pb_head;
2038 while(raparm) {
2039 last_raparm = raparm;
2040 raparm = raparm->p_next;
2041 kfree(last_raparm);
2042 }
2043 raparm_hash[i].pb_head = NULL;
2044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046/*
2047 * Initialize readahead param cache
2048 */
2049int
2050nfsd_racache_init(int cache_size)
2051{
2052 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002053 int j = 0;
2054 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002055 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Greg Banksfce14562006-10-04 02:15:49 -07002057
Jeff Layton54a66e52008-08-13 22:03:27 -04002058 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002060 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002061 nperbucket = max(2, nperbucket);
Jeff Layton54a66e52008-08-13 22:03:27 -04002062 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002063
2064 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002065
2066 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002067 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002068
2069 raparm = &raparm_hash[i].pb_head;
2070 for (j = 0; j < nperbucket; j++) {
2071 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2072 if (!*raparm)
2073 goto out_nomem;
2074 raparm = &(*raparm)->p_next;
2075 }
2076 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002077 }
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 nfsdstats.ra_size = cache_size;
2080 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002081
2082out_nomem:
2083 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2084 nfsd_racache_shutdown();
2085 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}