blob: 3257c59dc860169749eb577574bf5a3b650b6377 [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
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050039#include "acl.h"
40#include "idmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#endif /* CONFIG_NFSD_V4 */
42
Boaz Harrosh9a74af22009-12-03 20:30:56 +020043#include "nfsd.h"
44#include "vfs.h"
Jeff Layton6e8b50d2015-11-17 06:52:23 -050045#include "trace.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
Anna Schumaker95d871f2014-11-07 14:44:26 -0500502__be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
503 struct file *file, loff_t offset, loff_t len,
504 int flags)
505{
Anna Schumaker95d871f2014-11-07 14:44:26 -0500506 int error;
507
508 if (!S_ISREG(file_inode(file)->i_mode))
509 return nfserr_inval;
510
Anna Schumaker95d871f2014-11-07 14:44:26 -0500511 error = vfs_fallocate(file, flags, offset, len);
512 if (!error)
513 error = commit_metadata(fhp);
514
515 return nfserrno(error);
516}
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400517#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519#ifdef CONFIG_NFSD_V3
520/*
521 * Check server access rights to a file system object
522 */
523struct accessmap {
524 u32 access;
525 int how;
526};
527static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200528 { NFS3_ACCESS_READ, NFSD_MAY_READ },
529 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
530 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
531 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 { 0, 0 }
534};
535
536static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200537 { NFS3_ACCESS_READ, NFSD_MAY_READ },
538 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
539 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
540 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
541 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 { 0, 0 }
544};
545
546static struct accessmap nfs3_anyaccess[] = {
547 /* Some clients - Solaris 2.6 at least, make an access call
548 * to the server to check for access for things like /dev/null
549 * (which really, the server doesn't care about). So
550 * We provide simple access checking for them, looking
551 * mainly at mode bits, and we make sure to ignore read-only
552 * filesystem checks
553 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200554 { NFS3_ACCESS_READ, NFSD_MAY_READ },
555 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
556 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
557 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 { 0, 0 }
560};
561
Al Viro6264d692006-10-19 23:28:58 -0700562__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
564{
565 struct accessmap *map;
566 struct svc_export *export;
567 struct dentry *dentry;
568 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700569 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200571 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (error)
573 goto out;
574
575 export = fhp->fh_export;
576 dentry = fhp->fh_dentry;
577
David Howellse36cb0b2015-01-29 12:02:35 +0000578 if (d_is_reg(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 map = nfs3_regaccess;
David Howellse36cb0b2015-01-29 12:02:35 +0000580 else if (d_is_dir(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 map = nfs3_diraccess;
582 else
583 map = nfs3_anyaccess;
584
585
586 query = *access;
587 for (; map->access; map++) {
588 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700589 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 sresult |= map->access;
592
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700593 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 switch (err2) {
595 case nfs_ok:
596 result |= map->access;
597 break;
598
599 /* the following error codes just mean the access was not allowed,
600 * rather than an error occurred */
601 case nfserr_rofs:
602 case nfserr_acces:
603 case nfserr_perm:
604 /* simply don't "or" in the access bit. */
605 break;
606 default:
607 error = err2;
608 goto out;
609 }
610 }
611 }
612 *access = result;
613 if (supported)
614 *supported = sresult;
615
616 out:
617 return error;
618}
619#endif /* CONFIG_NFSD_V3 */
620
J. Bruce Fields105f4622011-06-07 11:50:23 -0400621static int nfsd_open_break_lease(struct inode *inode, int access)
622{
623 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
J. Bruce Fields105f4622011-06-07 11:50:23 -0400625 if (access & NFSD_MAY_NOT_BREAK_LEASE)
626 return 0;
627 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
628 return break_lease(inode, mode | O_NONBLOCK);
629}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631/*
632 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400633 * The may_flags argument indicates the type of open (read/write/lock)
634 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * N.B. After this call fhp needs an fh_put
636 */
Al Viro6264d692006-10-19 23:28:58 -0700637__be32
Al Viro175a4eb2011-07-26 03:30:54 -0400638nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400639 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Al Viro765927b2012-06-26 21:58:53 +0400641 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct inode *inode;
Kinglong Mee8519f992014-09-03 08:14:06 +0800643 struct file *file;
Al Viro6264d692006-10-19 23:28:58 -0700644 int flags = O_RDONLY|O_LARGEFILE;
645 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400646 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
David Howellse0e81732009-09-02 09:13:40 +0100648 validate_process_creds();
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
651 * If we get here, then the client has already done an "open",
652 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
653 * in case a chmod has now revoked permission.
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400654 *
655 * Arguably we should also allow the owner override for
656 * directories, but we never have and it doesn't seem to have
657 * caused anyone a problem. If we were to change this, note
658 * also that our filldir callbacks would need a variant of
659 * lookup_one_len that doesn't check permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
J. Bruce Fieldsd91d0b52012-06-06 12:12:57 -0400661 if (type == S_IFREG)
662 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
663 err = fh_verify(rqstp, fhp, type, may_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (err)
665 goto out;
666
Al Viro765927b2012-06-26 21:58:53 +0400667 path.mnt = fhp->fh_export->ex_path.mnt;
668 path.dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000669 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* Disallow write access to files with the append-only bit set
672 * or any access when mandatory locking enabled
673 */
674 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400675 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400677 /*
678 * We must ignore files (but only files) which might have mandatory
679 * locks on them because there is no way to know if the accesser has
680 * the lock.
681 */
682 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto out;
684
685 if (!inode->i_fop)
686 goto out;
687
Bernd Schubert999448a2012-03-18 22:44:49 -0400688 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700689 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 goto out_nfserr;
691
Bernd Schubert999448a2012-03-18 22:44:49 -0400692 if (may_flags & NFSD_MAY_WRITE) {
693 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700694 flags = O_RDWR|O_LARGEFILE;
695 else
696 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Bernd Schubert999448a2012-03-18 22:44:49 -0400698
Kinglong Mee8519f992014-09-03 08:14:06 +0800699 file = dentry_open(&path, flags, current_cred());
700 if (IS_ERR(file)) {
701 host_err = PTR_ERR(file);
702 goto out_nfserr;
Bernd Schubert06effdb2012-03-18 22:44:50 -0400703 }
704
Linus Torvalds5e40d332014-10-12 10:13:55 -0400705 host_err = ima_file_check(file, may_flags, 0);
Kinglong Mee8519f992014-09-03 08:14:06 +0800706 if (host_err) {
Christoph Hellwigfd891452015-04-28 15:41:16 +0200707 fput(file);
Kinglong Mee8519f992014-09-03 08:14:06 +0800708 goto out_nfserr;
709 }
710
711 if (may_flags & NFSD_MAY_64BIT_COOKIE)
712 file->f_mode |= FMODE_64BITHASH;
713 else
714 file->f_mode |= FMODE_32BITHASH;
715
716 *filp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700718 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719out:
David Howellse0e81732009-09-02 09:13:40 +0100720 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return err;
722}
723
Christoph Hellwige749a462015-06-18 16:44:58 +0200724struct raparms *
725nfsd_init_raparms(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Christoph Hellwige749a462015-06-18 16:44:58 +0200727 struct inode *inode = file_inode(file);
728 dev_t dev = inode->i_sb->s_dev;
729 ino_t ino = inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct raparms *ra, **rap, **frap = NULL;
731 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700732 unsigned int hash;
733 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Greg Banksfce14562006-10-04 02:15:49 -0700735 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
736 rab = &raparm_hash[hash];
737
738 spin_lock(&rab->pb_lock);
739 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (ra->p_ino == ino && ra->p_dev == dev)
741 goto found;
742 depth++;
743 if (ra->p_count == 0)
744 frap = rap;
745 }
Konstantin Khorenko3aa6e0a2011-02-01 17:16:29 +0300746 depth = nfsdstats.ra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700748 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return NULL;
750 }
751 rap = frap;
752 ra = *frap;
753 ra->p_dev = dev;
754 ra->p_ino = ino;
755 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700756 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757found:
Greg Banksfce14562006-10-04 02:15:49 -0700758 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700760 ra->p_next = rab->pb_head;
761 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 ra->p_count++;
764 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700765 spin_unlock(&rab->pb_lock);
Christoph Hellwige749a462015-06-18 16:44:58 +0200766
767 if (ra->p_set)
768 file->f_ra = ra->p_ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return ra;
770}
771
Christoph Hellwige749a462015-06-18 16:44:58 +0200772void nfsd_put_raparams(struct file *file, struct raparms *ra)
773{
774 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
775
776 spin_lock(&rab->pb_lock);
777 ra->p_ra = file->f_ra;
778 ra->p_set = 1;
779 ra->p_count--;
780 spin_unlock(&rab->pb_lock);
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200784 * Grab and keep cached pages associated with a file in the svc_rqst
785 * so that they can be passed to the network sendmsg/sendpage routines
786 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
788static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200789nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
790 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jens Axboecf8208d2007-06-12 21:22:14 +0200792 struct svc_rqst *rqstp = sd->u.data;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500793 struct page **pp = rqstp->rq_next_page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200794 struct page *page = buf->page;
795 size_t size;
Jens Axboecf8208d2007-06-12 21:22:14 +0200796
797 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if (rqstp->rq_res.page_len == 0) {
800 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500801 put_page(*rqstp->rq_next_page);
802 *(rqstp->rq_next_page++) = page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200803 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700805 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 get_page(page);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500807 if (*rqstp->rq_next_page)
808 put_page(*rqstp->rq_next_page);
809 *(rqstp->rq_next_page++) = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700811 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return size;
815}
816
Jens Axboecf8208d2007-06-12 21:22:14 +0200817static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
818 struct splice_desc *sd)
819{
820 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
821}
822
Jeff Laytone2afc812014-06-17 07:44:13 -0400823static __be32
824nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Al Viro6264d692006-10-19 23:28:58 -0700826 if (host_err >= 0) {
827 nfsdstats.io_read += host_err;
828 *count = host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500829 fsnotify_access(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 } else
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400832 return nfserrno(host_err);
833}
834
Jeff Laytone2afc812014-06-17 07:44:13 -0400835__be32 nfsd_splice_read(struct svc_rqst *rqstp,
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400836 struct file *file, loff_t offset, unsigned long *count)
837{
838 struct splice_desc sd = {
839 .len = 0,
840 .total_len = *count,
841 .pos = offset,
842 .u.data = rqstp,
843 };
844 int host_err;
845
846 rqstp->rq_next_page = rqstp->rq_respages + 1;
847 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
848 return nfsd_finish_read(file, count, host_err);
849}
850
Jeff Laytone2afc812014-06-17 07:44:13 -0400851__be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400852 unsigned long *count)
853{
854 mm_segment_t oldfs;
855 int host_err;
856
857 oldfs = get_fs();
858 set_fs(KERNEL_DS);
859 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
860 set_fs(oldfs);
861 return nfsd_finish_read(file, count, host_err);
862}
863
864static __be32
865nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
866 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
867{
Jeff Layton779fb0f2014-11-19 07:51:18 -0500868 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400869 return nfsd_splice_read(rqstp, file, offset, count);
870 else
871 return nfsd_readv(file, offset, vec, vlen, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700874/*
875 * Gathered writes: If another process is currently writing to the file,
876 * there's a high chance this is another nfsd (triggered by a bulk write
877 * from a client's biod). Rather than syncing the file with each write
878 * request, we sleep for 10 msec.
879 *
880 * I don't know if this roughly approximates C. Juszak's idea of
881 * gathered writes, but it's a nice and simple solution (IMHO), and it
882 * seems to work:-)
883 *
884 * Note: we do this only in the NFSv2 case, since v3 and higher have a
885 * better tool (separate unstable writes and commits) for solving this
886 * problem.
887 */
888static int wait_for_concurrent_writes(struct file *file)
889{
Al Viro496ad9a2013-01-23 17:07:38 -0500890 struct inode *inode = file_inode(file);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700891 static ino_t last_ino;
892 static dev_t last_dev;
893 int err = 0;
894
895 if (atomic_read(&inode->i_writecount) > 1
896 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
897 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
898 msleep(10);
899 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
900 }
901
902 if (inode->i_state & I_DIRTY) {
903 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100904 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700905 }
906 last_ino = inode->i_ino;
907 last_dev = inode->i_sb->s_dev;
908 return err;
909}
910
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200911__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
913 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -0500914 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 struct svc_export *exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct inode *inode;
918 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700919 __be32 err = 0;
920 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400922 int use_wgather;
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700923 loff_t pos = offset;
Zach Browne77a7b42014-10-06 16:42:52 -0700924 loff_t end = LLONG_MAX;
NeilBrown86584522014-05-12 11:22:47 +1000925 unsigned int pflags = current->flags;
926
Jeff Layton7501cc22014-11-19 07:51:15 -0500927 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
NeilBrown86584522014-05-12 11:22:47 +1000928 /*
929 * We want less throttling in balance_dirty_pages()
930 * and shrink_inactive_list() so that nfs to
931 * localhost doesn't cause nfsd to lock up due to all
932 * the client's dirty pages or its congested queue.
933 */
934 current->flags |= PF_LESS_THROTTLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Al Viro6f4e0d52014-10-31 02:42:53 -0400936 inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 exp = fhp->fh_export;
938
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400939 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!EX_ISSYNC(exp))
942 stable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* Write the data. */
945 oldfs = get_fs(); set_fs(KERNEL_DS);
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700946 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700948 if (host_err < 0)
949 goto out_nfserr;
950 *cnt = host_err;
951 nfsdstats.io_write += host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500952 fsnotify_modify(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400954 if (stable) {
Zach Browne77a7b42014-10-06 16:42:52 -0700955 if (use_wgather) {
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400956 host_err = wait_for_concurrent_writes(file);
Zach Browne77a7b42014-10-06 16:42:52 -0700957 } else {
958 if (*cnt)
959 end = offset + *cnt - 1;
960 host_err = vfs_fsync_range(file, offset, end, 0);
961 }
J. Bruce Fieldsface1502012-10-26 16:12:31 -0400962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
J. Bruce Fieldse4636d52009-06-15 19:07:13 -0700964out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700965 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800966 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +0800968 else
Al Viro6264d692006-10-19 23:28:58 -0700969 err = nfserrno(host_err);
Jeff Layton7501cc22014-11-19 07:51:15 -0500970 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
NeilBrown86584522014-05-12 11:22:47 +1000971 tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return err;
973}
974
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400975/*
976 * Read data from a file. count must contain the requested read count
977 * on entry. On return, *count contains the number of bytes actually read.
978 * N.B. After this call fhp needs an fh_put
979 */
980__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
981 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
982{
983 struct file *file;
984 struct raparms *ra;
985 __be32 err;
986
Jeff Layton6e8b50d2015-11-17 06:52:23 -0500987 trace_read_start(rqstp, fhp, offset, vlen);
Christoph Hellwige749a462015-06-18 16:44:58 +0200988 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400989 if (err)
990 return err;
991
Christoph Hellwige749a462015-06-18 16:44:58 +0200992 ra = nfsd_init_raparms(file);
Jeff Layton6e8b50d2015-11-17 06:52:23 -0500993
994 trace_read_opened(rqstp, fhp, offset, vlen);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400995 err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
Jeff Layton6e8b50d2015-11-17 06:52:23 -0500996 trace_read_io_done(rqstp, fhp, offset, vlen);
997
Christoph Hellwige749a462015-06-18 16:44:58 +0200998 if (ra)
999 nfsd_put_raparams(file, ra);
1000 fput(file);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001001
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001002 trace_read_done(rqstp, fhp, offset, vlen);
1003
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
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001019 trace_write_start(rqstp, fhp, offset, vlen);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001022 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001023 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (err)
1025 goto out;
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001026 trace_write_opened(rqstp, fhp, offset, vlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1028 stablep);
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001029 trace_write_io_done(rqstp, fhp, offset, vlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001031 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (err)
1033 goto out;
1034
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001035 trace_write_opened(rqstp, fhp, offset, vlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (cnt)
1037 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1038 cnt, stablep);
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001039 trace_write_io_done(rqstp, fhp, offset, vlen);
Christoph Hellwigfd891452015-04-28 15:41:16 +02001040 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042out:
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001043 trace_write_done(rqstp, fhp, offset, vlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return err;
1045}
1046
1047#ifdef CONFIG_NFSD_V3
1048/*
1049 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001050 *
1051 * Note: we only guarantee that data that lies within the range specified
1052 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 *
1054 * Unfortunately we cannot lock the file to make sure we return full WCC
1055 * data to the client, as locking happens lower down in the filesystem.
1056 */
Al Viro6264d692006-10-19 23:28:58 -07001057__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1059 loff_t offset, unsigned long count)
1060{
1061 struct file *file;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001062 loff_t end = LLONG_MAX;
1063 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Trond Myklebustaa696a62010-01-29 16:44:25 -05001065 if (offset < 0)
1066 goto out;
1067 if (count != 0) {
1068 end = offset + (loff_t)count - 1;
1069 if (end < offset)
1070 goto out;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Jeff Layton91885252010-03-19 08:06:28 -04001073 err = nfsd_open(rqstp, fhp, S_IFREG,
1074 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001075 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (EX_ISSYNC(fhp->fh_export)) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001078 int err2 = vfs_fsync_range(file, offset, end, 0);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001079
1080 if (err2 != -EINVAL)
1081 err = nfserrno(err2);
1082 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 err = nfserr_notsupp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
Christoph Hellwigfd891452015-04-28 15:41:16 +02001086 fput(file);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001087out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return err;
1089}
1090#endif /* CONFIG_NFSD_V3 */
1091
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001092static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001093nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1094 struct iattr *iap)
1095{
1096 /*
1097 * Mode has already been set earlier in create:
1098 */
1099 iap->ia_valid &= ~ATTR_MODE;
1100 /*
1101 * Setting uid/gid works only for root. Irix appears to
1102 * send along the gid on create when it tries to implement
1103 * setgid directories via NFS:
1104 */
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001105 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001106 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1107 if (iap->ia_valid)
1108 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001109 /* Callers expect file metadata to be committed here */
Jeff Layton722b6202014-07-03 07:54:19 -04001110 return nfserrno(commit_metadata(resfhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001111}
1112
wengang wang4ac35c22009-02-10 11:27:51 +08001113/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1114 * setting size to 0 may fail for some specific file systems by the permission
1115 * checking which requires WRITE permission but the mode is 000.
1116 * we ignore the resizing(to 0) on the just new created file, since the size is
1117 * 0 after file created.
1118 *
1119 * call this only after vfs_create() is called.
1120 * */
1121static void
1122nfsd_check_ignore_resizing(struct iattr *iap)
1123{
1124 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1125 iap->ia_valid &= ~ATTR_SIZE;
1126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/*
1129 * Create a file (regular, directory, device, fifo); UNIX sockets
1130 * not yet implemented.
1131 * If the response fh has been verified, the parent directory should
1132 * already be locked. Note that the parent directory is left locked.
1133 *
1134 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1135 */
Al Viro6264d692006-10-19 23:28:58 -07001136__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1138 char *fname, int flen, struct iattr *iap,
1139 int type, dev_t rdev, struct svc_fh *resfhp)
1140{
1141 struct dentry *dentry, *dchild = NULL;
1142 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001143 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001144 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001145 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 err = nfserr_perm;
1148 if (!flen)
1149 goto out;
1150 err = nfserr_exist;
1151 if (isdotent(fname, flen))
1152 goto out;
1153
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001154 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (err)
1156 goto out;
1157
1158 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001159 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001162 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto out;
1164 /*
1165 * Check whether the response file handle has been verified yet.
1166 * If it has, the parent directory should already be locked.
1167 */
1168 if (!resfhp->fh_dentry) {
Jan Kara4a55c102012-06-12 16:20:33 +02001169 host_err = fh_want_write(fhp);
1170 if (host_err)
1171 goto out_nfserr;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001174 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001176 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (IS_ERR(dchild))
1178 goto out_nfserr;
1179 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1180 if (err)
1181 goto out;
1182 } else {
1183 /* called from nfsd_proc_create */
1184 dchild = dget(resfhp->fh_dentry);
1185 if (!fhp->fh_locked) {
1186 /* not actually possible */
1187 printk(KERN_ERR
Al Viroa6a9f182013-09-16 10:57:01 -04001188 "nfsd_create: parent %pd2 not locked!\n",
1189 dentry);
Al Virod75f2b92006-01-18 17:43:44 -08001190 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto out;
1192 }
1193 }
1194 /*
1195 * Make sure the child dentry is still negative ...
1196 */
1197 err = nfserr_exist;
David Howells2b0143b2015-03-17 22:25:59 +00001198 if (d_really_is_positive(dchild)) {
Al Viroa6a9f182013-09-16 10:57:01 -04001199 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1200 dentry, dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 goto out;
1202 }
1203
1204 if (!(iap->ia_valid & ATTR_MODE))
1205 iap->ia_mode = 0;
1206 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1207
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001208 err = nfserr_inval;
1209 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1210 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1211 type);
1212 goto out;
1213 }
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /*
1216 * Get the dir op function pointer.
1217 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001218 err = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001219 host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 switch (type) {
1221 case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04001222 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001223 if (!host_err)
1224 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 break;
1226 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001227 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 break;
1229 case S_IFCHR:
1230 case S_IFBLK:
1231 case S_IFIFO:
1232 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001233 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001235 }
Jan Kara4a55c102012-06-12 16:20:33 +02001236 if (host_err < 0)
Dave Hansen463c3192008-02-15 14:37:57 -08001237 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Ben Myersf5019122010-02-17 14:05:11 -06001239 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Ben Myersf5019122010-02-17 14:05:11 -06001241 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001242 * nfsd_create_setattr already committed the child. Transactional
1243 * filesystems had a chance to commit changes for both parent and
1244 * child * simultaneously making the following commit_metadata a
1245 * noop.
Ben Myersf5019122010-02-17 14:05:11 -06001246 */
1247 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001248 if (err2)
1249 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * Update the file handle to get the new inode info.
1252 */
1253 if (!err)
1254 err = fh_update(resfhp);
1255out:
1256 if (dchild && !IS_ERR(dchild))
1257 dput(dchild);
1258 return err;
1259
1260out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001261 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto out;
1263}
1264
1265#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001268 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
Al Viro6264d692006-10-19 23:28:58 -07001270__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001271do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 char *fname, int flen, struct iattr *iap,
1273 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001274 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 struct dentry *dentry, *dchild = NULL;
1277 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001278 __be32 err;
1279 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 err = nfserr_perm;
1283 if (!flen)
1284 goto out;
1285 err = nfserr_exist;
1286 if (isdotent(fname, flen))
1287 goto out;
1288 if (!(iap->ia_valid & ATTR_MODE))
1289 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001290 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (err)
1292 goto out;
1293
1294 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001295 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* Get all the sanity checks out of the way before
1298 * we lock the parent. */
1299 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001300 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001302
1303 host_err = fh_want_write(fhp);
1304 if (host_err)
1305 goto out_nfserr;
1306
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001307 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /*
1310 * Compose the response file handle.
1311 */
1312 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001313 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (IS_ERR(dchild))
1315 goto out_nfserr;
1316
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001317 /* If file doesn't exist, check for permissions to create one */
David Howells2b0143b2015-03-17 22:25:59 +00001318 if (d_really_is_negative(dchild)) {
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001319 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1320 if (err)
1321 goto out;
1322 }
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1325 if (err)
1326 goto out;
1327
Mi Jinlongac6721a2011-04-20 17:06:25 +08001328 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001329 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001330 * the high bit set, so just clear the high bits. If this is
1331 * ever changed to use different attrs for storing the
1332 * verifier, then do_open_lookup() will also need to be fixed
1333 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
1335 v_mtime = verifier[0]&0x7fffffff;
1336 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
David Howells2b0143b2015-03-17 22:25:59 +00001339 if (d_really_is_positive(dchild)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 err = 0;
1341
1342 switch (createmode) {
1343 case NFS3_CREATE_UNCHECKED:
David Howellse36cb0b2015-01-29 12:02:35 +00001344 if (! d_is_reg(dchild))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 else if (truncp) {
1347 /* in nfsv4, we need to treat this case a little
1348 * differently. we don't want to truncate the
1349 * file now; this would be wrong if the OPEN
1350 * fails for some other reason. furthermore,
1351 * if the size is nonzero, we should ignore it
1352 * according to spec!
1353 */
1354 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1355 }
1356 else {
1357 iap->ia_valid &= ATTR_SIZE;
1358 goto set_attr;
1359 }
1360 break;
1361 case NFS3_CREATE_EXCLUSIVE:
David Howells2b0143b2015-03-17 22:25:59 +00001362 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1363 && d_inode(dchild)->i_atime.tv_sec == v_atime
1364 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001365 if (created)
1366 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 break;
Neil Brown7007c902012-12-07 15:40:55 -05001368 }
Mi Jinlongac6721a2011-04-20 17:06:25 +08001369 case NFS4_CREATE_EXCLUSIVE4_1:
David Howells2b0143b2015-03-17 22:25:59 +00001370 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1371 && d_inode(dchild)->i_atime.tv_sec == v_atime
1372 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001373 if (created)
1374 *created = 1;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001375 goto set_attr;
Neil Brown7007c902012-12-07 15:40:55 -05001376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* fallthru */
1378 case NFS3_CREATE_GUARDED:
1379 err = nfserr_exist;
1380 }
Al Virobad0dcf2011-11-23 12:03:18 -05001381 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 goto out;
1383 }
1384
Al Viro312b63f2012-06-10 18:09:36 -04001385 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001386 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001387 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001389 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001390 if (created)
1391 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
wengang wang4ac35c22009-02-10 11:27:51 +08001393 nfsd_check_ignore_resizing(iap);
1394
Mi Jinlongac6721a2011-04-20 17:06:25 +08001395 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001396 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001398 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* XXX someone who knows this better please fix it for nsec */
1400 iap->ia_mtime.tv_sec = v_mtime;
1401 iap->ia_atime.tv_sec = v_atime;
1402 iap->ia_mtime.tv_nsec = 0;
1403 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001407 err = nfsd_create_setattr(rqstp, resfhp, iap);
1408
1409 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001410 * nfsd_create_setattr already committed the child
1411 * (and possibly also the parent).
Ben Myersf5019122010-02-17 14:05:11 -06001412 */
1413 if (!err)
1414 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001415
1416 /*
1417 * Update the filehandle to get the new inode info.
1418 */
1419 if (!err)
1420 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 out:
1423 fh_unlock(fhp);
1424 if (dchild && !IS_ERR(dchild))
1425 dput(dchild);
Jan Kara4a55c102012-06-12 16:20:33 +02001426 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return err;
1428
1429 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001430 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 goto out;
1432}
1433#endif /* CONFIG_NFSD_V3 */
1434
1435/*
1436 * Read a symlink. On entry, *lenp must contain the maximum path length that
1437 * fits into the buffer. On return, it contains the true length.
1438 * N.B. After this call fhp needs an fh_put
1439 */
Al Viro6264d692006-10-19 23:28:58 -07001440__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1442{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct inode *inode;
1444 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001445 __be32 err;
1446 int host_err;
Al Viro68ac1232012-03-15 08:21:57 -04001447 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001449 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (err)
1451 goto out;
1452
Al Viro68ac1232012-03-15 08:21:57 -04001453 path.mnt = fhp->fh_export->ex_path.mnt;
1454 path.dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001455 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001458 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 goto out;
1460
Al Viro68ac1232012-03-15 08:21:57 -04001461 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* N.B. Why does this call need a get_fs()??
1463 * Remove the set_fs and watch the fireworks:-) --okir
1464 */
1465
1466 oldfs = get_fs(); set_fs(KERNEL_DS);
J. Bruce Fieldsfac7a172012-07-27 14:16:55 -04001467 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 set_fs(oldfs);
1469
Al Viro6264d692006-10-19 23:28:58 -07001470 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001472 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 err = 0;
1474out:
1475 return err;
1476
1477out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001478 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 goto out;
1480}
1481
1482/*
1483 * Create a symlink and look up its inode
1484 * N.B. After this call _both_ fhp and resfhp need an fh_put
1485 */
Al Viro6264d692006-10-19 23:28:58 -07001486__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1488 char *fname, int flen,
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001489 char *path,
Kinglong Mee1e444f52014-07-01 17:48:02 +08001490 struct svc_fh *resfhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001493 __be32 err, cerr;
1494 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 err = nfserr_noent;
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001497 if (!flen || path[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 goto out;
1499 err = nfserr_exist;
1500 if (isdotent(fname, flen))
1501 goto out;
1502
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001503 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (err)
1505 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001506
1507 host_err = fh_want_write(fhp);
1508 if (host_err)
1509 goto out_nfserr;
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 fh_lock(fhp);
1512 dentry = fhp->fh_dentry;
1513 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001514 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (IS_ERR(dnew))
1516 goto out_nfserr;
1517
David Howells2b0143b2015-03-17 22:25:59 +00001518 host_err = vfs_symlink(d_inode(dentry), dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001519 err = nfserrno(host_err);
Ben Myersf5019122010-02-17 14:05:11 -06001520 if (!err)
1521 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 fh_unlock(fhp);
1523
Al Virobad0dcf2011-11-23 12:03:18 -05001524 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1527 dput(dnew);
1528 if (err==0) err = cerr;
1529out:
1530 return err;
1531
1532out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001533 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 goto out;
1535}
1536
1537/*
1538 * Create a hardlink
1539 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1540 */
Al Viro6264d692006-10-19 23:28:58 -07001541__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1543 char *name, int len, struct svc_fh *tfhp)
1544{
1545 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001546 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001547 __be32 err;
1548 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001550 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (err)
1552 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001553 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (err)
1555 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001556 err = nfserr_isdir;
David Howellse36cb0b2015-01-29 12:02:35 +00001557 if (d_is_dir(tfhp->fh_dentry))
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 err = nfserr_perm;
1560 if (!len)
1561 goto out;
1562 err = nfserr_exist;
1563 if (isdotent(name, len))
1564 goto out;
1565
Jan Kara4a55c102012-06-12 16:20:33 +02001566 host_err = fh_want_write(tfhp);
1567 if (host_err) {
1568 err = nfserrno(host_err);
1569 goto out;
1570 }
1571
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001572 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 ddir = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001574 dirp = d_inode(ddir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001577 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (IS_ERR(dnew))
1579 goto out_nfserr;
1580
1581 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001583 err = nfserr_noent;
David Howells2b0143b2015-03-17 22:25:59 +00001584 if (d_really_is_negative(dold))
Jan Kara4a55c102012-06-12 16:20:33 +02001585 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04001586 host_err = vfs_link(dold, dirp, dnew, NULL);
Al Viro6264d692006-10-19 23:28:58 -07001587 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001588 err = nfserrno(commit_metadata(ffhp));
1589 if (!err)
1590 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 } else {
Al Viro6264d692006-10-19 23:28:58 -07001592 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 err = nfserr_acces;
1594 else
Al Viro6264d692006-10-19 23:28:58 -07001595 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001597out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001599out_unlock:
1600 fh_unlock(ffhp);
Jan Kara4a55c102012-06-12 16:20:33 +02001601 fh_drop_write(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602out:
1603 return err;
1604
1605out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001606 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001607 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610/*
1611 * Rename a file
1612 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1613 */
Al Viro6264d692006-10-19 23:28:58 -07001614__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1616 struct svc_fh *tfhp, char *tname, int tlen)
1617{
1618 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1619 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001620 __be32 err;
1621 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001623 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (err)
1625 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001626 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (err)
1628 goto out;
1629
1630 fdentry = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001631 fdir = d_inode(fdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 tdentry = tfhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001634 tdir = d_inode(tdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 err = nfserr_perm;
1637 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1638 goto out;
1639
Jan Kara4a55c102012-06-12 16:20:33 +02001640 host_err = fh_want_write(ffhp);
1641 if (host_err) {
1642 err = nfserrno(host_err);
1643 goto out;
1644 }
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /* cannot use fh_lock as we need deadlock protective ordering
1647 * so do it by hand */
1648 trap = lock_rename(tdentry, fdentry);
Jeff Laytonaaf91ec2015-09-17 08:28:39 -04001649 ffhp->fh_locked = tfhp->fh_locked = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 fill_pre_wcc(ffhp);
1651 fill_pre_wcc(tfhp);
1652
1653 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001654 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (IS_ERR(odentry))
1656 goto out_nfserr;
1657
Al Viro6264d692006-10-19 23:28:58 -07001658 host_err = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001659 if (d_really_is_negative(odentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001661 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (odentry == trap)
1663 goto out_dput_old;
1664
1665 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001666 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (IS_ERR(ndentry))
1668 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001669 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (ndentry == trap)
1671 goto out_dput_new;
1672
Dave Hansen9079b1e2008-02-15 14:37:49 -08001673 host_err = -EXDEV;
1674 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1675 goto out_dput_new;
J. Bruce Fieldsaa387d6c2013-04-15 16:03:46 -04001676 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1677 goto out_dput_new;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001678
Miklos Szeredi520c8b12014-04-01 17:08:42 +02001679 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
Ben Myersf5019122010-02-17 14:05:11 -06001680 if (!host_err) {
1681 host_err = commit_metadata(tfhp);
Al Viro6264d692006-10-19 23:28:58 -07001682 if (!host_err)
Ben Myersf5019122010-02-17 14:05:11 -06001683 host_err = commit_metadata(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 out_dput_new:
1686 dput(ndentry);
1687 out_dput_old:
1688 dput(odentry);
1689 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001690 err = nfserrno(host_err);
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001691 /*
1692 * We cannot rely on fh_unlock on the two filehandles,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 * as that would do the wrong thing if the two directories
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001694 * were the same, so again we do it by hand.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
1696 fill_post_wcc(ffhp);
1697 fill_post_wcc(tfhp);
1698 unlock_rename(tdentry, fdentry);
Jeff Laytonaaf91ec2015-09-17 08:28:39 -04001699 ffhp->fh_locked = tfhp->fh_locked = false;
Jan Kara4a55c102012-06-12 16:20:33 +02001700 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702out:
1703 return err;
1704}
1705
1706/*
1707 * Unlink a file or directory
1708 * N.B. After this call fhp needs an fh_put
1709 */
Al Viro6264d692006-10-19 23:28:58 -07001710__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1712 char *fname, int flen)
1713{
1714 struct dentry *dentry, *rdentry;
1715 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001716 __be32 err;
1717 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 err = nfserr_acces;
1720 if (!flen || isdotent(fname, flen))
1721 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001722 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (err)
1724 goto out;
1725
Jan Kara4a55c102012-06-12 16:20:33 +02001726 host_err = fh_want_write(fhp);
1727 if (host_err)
1728 goto out_nfserr;
1729
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001730 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001732 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001735 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (IS_ERR(rdentry))
1737 goto out_nfserr;
1738
David Howells2b0143b2015-03-17 22:25:59 +00001739 if (d_really_is_negative(rdentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 dput(rdentry);
1741 err = nfserr_noent;
1742 goto out;
1743 }
1744
1745 if (!type)
David Howells2b0143b2015-03-17 22:25:59 +00001746 type = d_inode(rdentry)->i_mode & S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001748 if (type != S_IFDIR)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04001749 host_err = vfs_unlink(dirp, rdentry, NULL);
J. Bruce Fields9ce137e2011-01-11 14:07:12 -05001750 else
Al Viro6264d692006-10-19 23:28:58 -07001751 host_err = vfs_rmdir(dirp, rdentry);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001752 if (!host_err)
1753 host_err = commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 dput(rdentry);
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001757 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001758out:
1759 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
1762/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001763 * We do this buffering because we must not call back into the file
1764 * system's ->lookup() method from the filldir callback. That may well
1765 * deadlock a number of file systems.
1766 *
1767 * This is based heavily on the implementation of same in XFS.
1768 */
1769struct buffered_dirent {
1770 u64 ino;
1771 loff_t offset;
1772 int namlen;
1773 unsigned int d_type;
1774 char name[];
1775};
1776
1777struct readdir_data {
Al Viro5c0ba4e2013-05-15 13:52:59 -04001778 struct dir_context ctx;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001779 char *dirent;
1780 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001781 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001782};
1783
Miklos Szerediac7576f2014-10-30 17:37:34 +01001784static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1785 int namlen, loff_t offset, u64 ino,
1786 unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001787{
Miklos Szerediac7576f2014-10-30 17:37:34 +01001788 struct readdir_data *buf =
1789 container_of(ctx, struct readdir_data, ctx);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001790 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1791 unsigned int reclen;
1792
1793 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001794 if (buf->used + reclen > PAGE_SIZE) {
1795 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001796 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001797 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001798
1799 de->namlen = namlen;
1800 de->offset = offset;
1801 de->ino = ino;
1802 de->d_type = d_type;
1803 memcpy(de->name, name, namlen);
1804 buf->used += reclen;
1805
1806 return 0;
1807}
1808
Miklos Szerediac7576f2014-10-30 17:37:34 +01001809static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
David Woodhouse2f9092e2009-04-20 23:18:37 +01001810 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001811{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001812 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001813 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001814 int size;
1815 loff_t offset;
Al Viroac6614b2013-05-22 22:22:04 -04001816 struct readdir_data buf = {
1817 .ctx.actor = nfsd_buffered_filldir,
1818 .dirent = (void *)__get_free_page(GFP_KERNEL)
1819 };
David Woodhouse2628b762008-07-31 17:16:51 +01001820
David Woodhouse14f7dd62008-07-31 20:29:12 +01001821 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001822 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001823
David Woodhouse14f7dd62008-07-31 20:29:12 +01001824 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001825
1826 while (1) {
Al Viro496ad9a2013-01-23 17:07:38 -05001827 struct inode *dir_inode = file_inode(file);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001828 unsigned int reclen;
1829
Doug Nazarb726e922008-11-05 06:16:28 -05001830 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001831 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001832 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001833
Al Viro5c0ba4e2013-05-15 13:52:59 -04001834 host_err = iterate_dir(file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -04001835 if (buf.full)
1836 host_err = 0;
1837
1838 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001839 break;
1840
1841 size = buf.used;
1842
1843 if (!size)
1844 break;
1845
David Woodhouse2f9092e2009-04-20 23:18:37 +01001846 /*
1847 * Various filldir functions may end up calling back into
1848 * lookup_one_len() and the file system's ->lookup() method.
1849 * These expect i_mutex to be held, as it would within readdir.
1850 */
1851 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1852 if (host_err)
1853 break;
1854
David Woodhouse14f7dd62008-07-31 20:29:12 +01001855 de = (struct buffered_dirent *)buf.dirent;
1856 while (size > 0) {
1857 offset = de->offset;
1858
1859 if (func(cdp, de->name, de->namlen, de->offset,
1860 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01001861 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001862
1863 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001864 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001865
1866 reclen = ALIGN(sizeof(*de) + de->namlen,
1867 sizeof(u64));
1868 size -= reclen;
1869 de = (struct buffered_dirent *)((char *)de + reclen);
1870 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01001871 mutex_unlock(&dir_inode->i_mutex);
1872 if (size > 0) /* We bailed out early */
1873 break;
1874
David Woodhousec002a6c2008-08-17 17:21:18 +01001875 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001876 }
1877
David Woodhouse14f7dd62008-07-31 20:29:12 +01001878 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01001879
1880 if (host_err)
1881 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001882
1883 *offsetp = offset;
1884 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01001885}
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887/*
1888 * Read entries from a directory.
1889 * The NFSv3/4 verifier we ignore for now.
1890 */
Al Viro6264d692006-10-19 23:28:58 -07001891__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
Miklos Szerediac7576f2014-10-30 17:37:34 +01001893 struct readdir_cd *cdp, nfsd_filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Al Viro6264d692006-10-19 23:28:58 -07001895 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 struct file *file;
1897 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04001898 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Bernd Schubert06effdb2012-03-18 22:44:50 -04001900 /* NFSv2 only supports 32 bit cookies */
1901 if (rqstp->rq_vers > 2)
1902 may_flags |= NFSD_MAY_64BIT_COOKIE;
1903
1904 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (err)
1906 goto out;
1907
Jeff Laytonb108fe62012-04-25 15:30:00 -04001908 offset = vfs_llseek(file, offset, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (offset < 0) {
1910 err = nfserrno((int)offset);
1911 goto out_close;
1912 }
1913
David Woodhouse14f7dd62008-07-31 20:29:12 +01001914 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 if (err == nfserr_eof || err == nfserr_toosmall)
1917 err = nfs_ok; /* can still be found in ->err */
1918out_close:
Christoph Hellwigfd891452015-04-28 15:41:16 +02001919 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920out:
1921 return err;
1922}
1923
1924/*
1925 * Get file system stats
1926 * N.B. After this call fhp needs an fh_put
1927 */
Al Viro6264d692006-10-19 23:28:58 -07001928__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04001929nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02001931 __be32 err;
1932
1933 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02001934 if (!err) {
1935 struct path path = {
1936 .mnt = fhp->fh_export->ex_path.mnt,
1937 .dentry = fhp->fh_dentry,
1938 };
1939 if (vfs_statfs(&path, stat))
1940 err = nfserr_io;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return err;
1943}
1944
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001945static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001946{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07001947 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07001948}
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950/*
1951 * Check for a user's access permissions to this inode.
1952 */
Al Viro6264d692006-10-19 23:28:58 -07001953__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001954nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
1955 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
David Howells2b0143b2015-03-17 22:25:59 +00001957 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 int err;
1959
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04001960 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return 0;
1962#if 0
1963 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1964 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001965 (acc & NFSD_MAY_READ)? " read" : "",
1966 (acc & NFSD_MAY_WRITE)? " write" : "",
1967 (acc & NFSD_MAY_EXEC)? " exec" : "",
1968 (acc & NFSD_MAY_SATTR)? " sattr" : "",
1969 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
1970 (acc & NFSD_MAY_LOCK)? " lock" : "",
1971 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 inode->i_mode,
1973 IS_IMMUTABLE(inode)? " immut" : "",
1974 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08001975 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11001977 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978#endif
1979
1980 /* Normally we reject any write/sattr etc access on a read-only file
1981 * system. But if it is IRIX doing check on write-access for a
1982 * device special file, we ignore rofs.
1983 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001984 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
1985 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08001986 if (exp_rdonly(rqstp, exp) ||
1987 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001989 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return nfserr_perm;
1991 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001992 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return nfserr_perm;
1994
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001995 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /* If we cannot rely on authentication in NLM requests,
1997 * just allow locks, otherwise require read permission, or
1998 * ownership
1999 */
2000 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2001 return 0;
2002 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002003 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005 /*
2006 * The file owner always gets access permission for accesses that
2007 * would normally be checked at open time. This is to make
2008 * file access work even when the client has done a fchmod(fd, 0).
2009 *
2010 * However, `cp foo bar' should fail nevertheless when bar is
2011 * readonly. A sensible way to do this might be to reject all
2012 * attempts to truncate a read-only file, because a creat() call
2013 * always implies file truncation.
2014 * ... but this isn't really fair. A process may reasonably call
2015 * ftruncate on an open file descriptor on a file with perm 000.
2016 * We must trust the client to do permission checking - using "ACCESS"
2017 * with NFSv3.
2018 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002019 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002020 uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return 0;
2022
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002023 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002024 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 /* Allow read access to binaries even when mode 111 */
2027 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002028 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2029 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Al Virof419a2e2008-07-22 00:07:17 -04002030 err = inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 return err? nfserrno(err) : 0;
2033}
2034
2035void
2036nfsd_racache_shutdown(void)
2037{
Jeff Layton54a66e52008-08-13 22:03:27 -04002038 struct raparms *raparm, *last_raparm;
2039 unsigned int i;
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002042
2043 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2044 raparm = raparm_hash[i].pb_head;
2045 while(raparm) {
2046 last_raparm = raparm;
2047 raparm = raparm->p_next;
2048 kfree(last_raparm);
2049 }
2050 raparm_hash[i].pb_head = NULL;
2051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053/*
2054 * Initialize readahead param cache
2055 */
2056int
2057nfsd_racache_init(int cache_size)
2058{
2059 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002060 int j = 0;
2061 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002062 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Greg Banksfce14562006-10-04 02:15:49 -07002064
Jeff Layton54a66e52008-08-13 22:03:27 -04002065 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002067 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002068 nperbucket = max(2, nperbucket);
Jeff Layton54a66e52008-08-13 22:03:27 -04002069 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002070
2071 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002072
2073 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002074 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002075
2076 raparm = &raparm_hash[i].pb_head;
2077 for (j = 0; j < nperbucket; j++) {
2078 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2079 if (!*raparm)
2080 goto out_nomem;
2081 raparm = &(*raparm)->p_next;
2082 }
2083 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002084 }
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 nfsdstats.ra_size = cache_size;
2087 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002088
2089out_nomem:
2090 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2091 nfsd_racache_shutdown();
2092 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}