blob: 8eca17df4f6351f0d91b1bb294e7b24bcb6c59d3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#define MSNFS /* HACK HACK */
2/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * File operations used by nfsd. Some of these have been ripped from
4 * other parts of the kernel because they weren't exported, others
5 * are partial duplicates with added or changed functionality.
6 *
7 * Note that several functions dget() the dentry upon which they want
8 * to act, most notably those that create directory entries. Response
9 * dentry's are dput()'d if necessary in the release callback.
10 * So if you notice code paths that apparently fail to dput() the
11 * dentry, don't worry--they have been taken care of.
12 *
13 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/fs.h>
18#include <linux/file.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020019#include <linux/splice.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>
28#include <asm/uaccess.h>
29
30#ifdef CONFIG_NFSD_V3
31#include "xdr3.h"
32#endif /* CONFIG_NFSD_V3 */
33
Christoph Hellwig5be196e2006-01-09 20:51:55 -080034#ifdef CONFIG_NFSD_V4
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/nfs4_acl.h>
36#include <linux/nfsd_idmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#endif /* CONFIG_NFSD_V4 */
38
Boaz Harrosh9a74af22009-12-03 20:30:56 +020039#include "nfsd.h"
40#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define NFSDDBG_FACILITY NFSDDBG_FILEOP
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * This is a cache of readahead params that help us choose the proper
47 * readahead strategy. Initially, we set all readahead parameters to 0
48 * and let the VFS handle things.
49 * If you increase the number of cached files very much, you'll need to
50 * add a hash table here.
51 */
52struct raparms {
53 struct raparms *p_next;
54 unsigned int p_count;
55 ino_t p_ino;
56 dev_t p_dev;
57 int p_set;
58 struct file_ra_state p_ra;
Greg Banksfce14562006-10-04 02:15:49 -070059 unsigned int p_hindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Greg Banksfce14562006-10-04 02:15:49 -070062struct raparm_hbucket {
63 struct raparms *pb_head;
64 spinlock_t pb_lock;
65} ____cacheline_aligned_in_smp;
66
Greg Banksfce14562006-10-04 02:15:49 -070067#define RAPARM_HASH_BITS 4
68#define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
69#define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
70static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
73 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
74 * a mount point.
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080075 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * or nfs_ok having possibly changed *dpp and *expp
77 */
78int
79nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
80 struct svc_export **expp)
81{
82 struct svc_export *exp = *expp, *exp2 = NULL;
83 struct dentry *dentry = *dpp;
Al Viro91c9fa82009-04-18 02:42:05 -040084 struct path path = {.mnt = mntget(exp->ex_path.mnt),
85 .dentry = dget(dentry)};
Al Viro6264d692006-10-19 23:28:58 -070086 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Al Viro9393bd02009-04-18 13:58:15 -040088 while (d_mountpoint(path.dentry) && follow_down(&path))
Al Viro91c9fa82009-04-18 02:42:05 -040089 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Al Viro91c9fa82009-04-18 02:42:05 -040091 exp2 = rqst_exp_get_by_name(rqstp, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (IS_ERR(exp2)) {
J. Bruce Fields3b6cee7bc2009-10-25 21:18:19 -040093 err = PTR_ERR(exp2);
94 /*
95 * We normally allow NFS clients to continue
96 * "underneath" a mountpoint that is not exported.
97 * The exception is V4ROOT, where no traversal is ever
98 * allowed without an explicit export of the new
99 * directory.
100 */
101 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
102 err = 0;
Al Viro91c9fa82009-04-18 02:42:05 -0400103 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 goto out;
105 }
Steve Dickson3c394dd2009-09-09 15:02:40 -0400106 if (nfsd_v4client(rqstp) ||
107 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* successfully crossed mount point */
Al Viro1644ccc2009-04-18 02:32:31 -0400109 /*
Al Viro91c9fa82009-04-18 02:42:05 -0400110 * This is subtle: path.dentry is *not* on path.mnt
111 * at this point. The only reason we are safe is that
112 * original mnt is pinned down by exp, so we should
113 * put path *before* putting exp
Al Viro1644ccc2009-04-18 02:32:31 -0400114 */
Al Viro91c9fa82009-04-18 02:42:05 -0400115 *dpp = path.dentry;
116 path.dentry = dentry;
Al Viro1644ccc2009-04-18 02:32:31 -0400117 *expp = exp2;
Al Viro91c9fa82009-04-18 02:42:05 -0400118 exp2 = exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Al Viro91c9fa82009-04-18 02:42:05 -0400120 path_put(&path);
121 exp_put(exp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122out:
123 return err;
124}
125
J. Bruce Fields289ede42009-09-26 20:32:24 -0400126static void follow_to_parent(struct path *path)
127{
128 struct dentry *dp;
129
130 while (path->dentry == path->mnt->mnt_root && follow_up(path))
131 ;
132 dp = dget_parent(path->dentry);
133 dput(path->dentry);
134 path->dentry = dp;
135}
136
137static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
138{
139 struct svc_export *exp2;
140 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
141 .dentry = dget(dparent)};
142
143 follow_to_parent(&path);
144
145 exp2 = rqst_exp_parent(rqstp, &path);
146 if (PTR_ERR(exp2) == -ENOENT) {
147 *dentryp = dget(dparent);
148 } else if (IS_ERR(exp2)) {
149 path_put(&path);
150 return PTR_ERR(exp2);
151 } else {
152 *dentryp = dget(path.dentry);
153 exp_put(*exp);
154 *exp = exp2;
155 }
156 path_put(&path);
157 return 0;
158}
159
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400160/*
161 * For nfsd purposes, we treat V4ROOT exports as though there was an
162 * export at *every* directory.
163 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -0400164int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400165{
166 if (d_mountpoint(dentry))
167 return 1;
168 if (!(exp->ex_flags & NFSEXP_V4ROOT))
169 return 0;
170 return dentry->d_inode != NULL;
171}
172
Al Viro6264d692006-10-19 23:28:58 -0700173__be32
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700174nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400175 const char *name, unsigned int len,
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700176 struct svc_export **exp_ret, struct dentry **dentry_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct svc_export *exp;
179 struct dentry *dparent;
180 struct dentry *dentry;
Al Viro6264d692006-10-19 23:28:58 -0700181 __be32 err;
182 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
185
186 /* Obtain dentry and export. */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200187 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (err)
189 return err;
190
191 dparent = fhp->fh_dentry;
192 exp = fhp->fh_export;
193 exp_get(exp);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* Lookup the name, but don't follow links */
196 if (isdotent(name, len)) {
197 if (len==1)
198 dentry = dget(dparent);
Jan Blunck54775492008-02-14 19:38:39 -0800199 else if (dparent != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dentry = dget_parent(dparent);
J. Bruce Fieldsfed83812009-09-26 16:53:01 -0400201 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dentry = dget(dparent); /* .. == . just like at / */
203 else {
204 /* checking mountpoint crossing is very different when stepping up */
J. Bruce Fields289ede42009-09-26 20:32:24 -0400205 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
206 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 } else {
210 fh_lock(fhp);
211 dentry = lookup_one_len(name, dparent, len);
Al Viro6264d692006-10-19 23:28:58 -0700212 host_err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (IS_ERR(dentry))
214 goto out_nfserr;
215 /*
216 * check if we have crossed a mount point ...
217 */
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400218 if (nfsd_mountpoint(dentry, exp)) {
Al Viro6264d692006-10-19 23:28:58 -0700219 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dput(dentry);
221 goto out_nfserr;
222 }
223 }
224 }
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700225 *dentry_ret = dentry;
226 *exp_ret = exp;
227 return 0;
228
229out_nfserr:
230 exp_put(exp);
231 return nfserrno(host_err);
232}
233
234/*
235 * Look up one component of a pathname.
236 * N.B. After this call _both_ fhp and resfh need an fh_put
237 *
238 * If the lookup would cross a mountpoint, and the mounted filesystem
239 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
240 * accepted as it stands and the mounted directory is
241 * returned. Otherwise the covered directory is returned.
242 * NOTE: this mountpoint crossing is not supported properly by all
243 * clients and is explicitly disallowed for NFSv3
244 * NeilBrown <neilb@cse.unsw.edu.au>
245 */
246__be32
247nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400248 unsigned int len, struct svc_fh *resfh)
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700249{
250 struct svc_export *exp;
251 struct dentry *dentry;
252 __be32 err;
253
254 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
255 if (err)
256 return err;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700257 err = check_nfsd_access(exp, rqstp);
258 if (err)
259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /*
261 * Note: we compose the file handle now, but as the
262 * dentry may be negative, it may need to be updated.
263 */
264 err = fh_compose(resfh, exp, dentry, fhp);
265 if (!err && !dentry->d_inode)
266 err = nfserr_noent;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700267out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 exp_put(exp);
270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*
275 * Set various file attributes.
276 * N.B. After this call fhp needs an fh_put
277 */
Al Viro6264d692006-10-19 23:28:58 -0700278__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
280 int check_guard, time_t guardtime)
281{
282 struct dentry *dentry;
283 struct inode *inode;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200284 int accmode = NFSD_MAY_SATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int ftype = 0;
Al Viro6264d692006-10-19 23:28:58 -0700286 __be32 err;
287 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int size_change = 0;
289
290 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200291 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (iap->ia_valid & ATTR_SIZE)
293 ftype = S_IFREG;
294
295 /* Get inode */
296 err = fh_verify(rqstp, fhp, ftype, accmode);
NeilBrown15b7a1b2005-11-07 01:00:23 -0800297 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto out;
299
300 dentry = fhp->fh_dentry;
301 inode = dentry->d_inode;
302
NeilBrown15b7a1b2005-11-07 01:00:23 -0800303 /* Ignore any mode updates on symlinks */
304 if (S_ISLNK(inode->i_mode))
305 iap->ia_valid &= ~ATTR_MODE;
306
307 if (!iap->ia_valid)
308 goto out;
309
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000310 /*
311 * NFSv2 does not differentiate between "set-[ac]time-to-now"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * which only requires access, and "set-[ac]time-to-X" which
313 * requires ownership.
314 * So if it looks like it might be "set both to the same time which
315 * is close to now", and if inode_change_ok fails, then we
316 * convert to "set to now" instead of "set to explicit time"
317 *
318 * We only call inode_change_ok as the last test as technically
319 * it is not an interface that we should be using. It is only
320 * valid if the filesystem does not define it's own i_op->setattr.
321 */
322#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
323#define MAX_TOUCH_TIME_ERROR (30*60)
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000324 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
325 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
326 /*
327 * Looks probable.
328 *
329 * Now just make sure time is in the right ballpark.
330 * Solaris, at least, doesn't seem to care what the time
331 * request is. We require it be within 30 minutes of now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000333 time_t delta = iap->ia_atime.tv_sec - get_seconds();
334 if (delta < 0)
335 delta = -delta;
336 if (delta < MAX_TOUCH_TIME_ERROR &&
337 inode_change_ok(inode, iap) != 0) {
338 /*
339 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
340 * This will cause notify_change to set these times
341 * to "now"
342 */
343 iap->ia_valid &= ~BOTH_TIME_SET;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000347 /*
348 * The size case is special.
349 * It changes the file as well as the attributes.
350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (iap->ia_valid & ATTR_SIZE) {
352 if (iap->ia_size < inode->i_size) {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200353 err = nfsd_permission(rqstp, fhp->fh_export, dentry,
354 NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (err)
356 goto out;
357 }
358
359 /*
360 * If we are changing the size of the file, then
361 * we need to break all leases.
362 */
Al Viro8737c932009-12-24 06:47:55 -0500363 host_err = break_lease(inode, O_WRONLY | O_NONBLOCK);
Al Viro6264d692006-10-19 23:28:58 -0700364 if (host_err == -EWOULDBLOCK)
365 host_err = -ETIMEDOUT;
366 if (host_err) /* ENOMEM or EWOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto out_nfserr;
368
Al Viro6264d692006-10-19 23:28:58 -0700369 host_err = get_write_access(inode);
370 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto out_nfserr;
372
373 size_change = 1;
Al Viro6264d692006-10-19 23:28:58 -0700374 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
375 if (host_err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 put_write_access(inode);
377 goto out_nfserr;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Jeff Laytonca456252008-04-16 16:28:47 -0400381 /* sanitize the mode change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (iap->ia_valid & ATTR_MODE) {
383 iap->ia_mode &= S_IALLUGO;
Jeff Laytondee32092008-04-16 16:28:46 -0400384 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
Jeff Laytonca456252008-04-16 16:28:47 -0400385 }
386
387 /* Revoke setuid/setgid on chown */
Sachin S. Prabhu0953e622009-02-23 16:22:03 +0000388 if (!S_ISDIR(inode->i_mode) &&
389 (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
390 ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid))) {
Jeff Laytonca456252008-04-16 16:28:47 -0400391 iap->ia_valid |= ATTR_KILL_PRIV;
392 if (iap->ia_valid & ATTR_MODE) {
393 /* we're setting mode too, just clear the s*id bits */
Jeff Layton8a0ce7d2007-10-18 03:05:19 -0700394 iap->ia_mode &= ~S_ISUID;
Jeff Laytonca456252008-04-16 16:28:47 -0400395 if (iap->ia_mode & S_IXGRP)
396 iap->ia_mode &= ~S_ISGID;
397 } else {
398 /* set ATTR_KILL_* bits and let VFS handle it */
399 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
Jeff Layton8a0ce7d2007-10-18 03:05:19 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* Change the attributes. */
404
405 iap->ia_valid |= ATTR_CTIME;
406
407 err = nfserr_notsync;
408 if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
409 fh_lock(fhp);
Al Viro6264d692006-10-19 23:28:58 -0700410 host_err = notify_change(dentry, iap);
411 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 fh_unlock(fhp);
413 }
414 if (size_change)
415 put_write_access(inode);
416 if (!err)
417 if (EX_ISSYNC(fhp->fh_export))
418 write_inode_now(inode, 1);
419out:
420 return err;
421
422out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700423 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto out;
425}
426
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800427#if defined(CONFIG_NFSD_V2_ACL) || \
428 defined(CONFIG_NFSD_V3_ACL) || \
429 defined(CONFIG_NFSD_V4)
430static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
431{
432 ssize_t buflen;
Krishna Kumar6c6a4262008-10-22 14:48:36 +0530433 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800435 buflen = vfs_getxattr(dentry, key, NULL, 0);
436 if (buflen <= 0)
437 return buflen;
438
439 *buf = kmalloc(buflen, GFP_KERNEL);
440 if (!*buf)
441 return -ENOMEM;
442
Krishna Kumar6c6a4262008-10-22 14:48:36 +0530443 ret = vfs_getxattr(dentry, key, *buf, buflen);
444 if (ret < 0)
445 kfree(*buf);
446 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800447}
448#endif
449
450#if defined(CONFIG_NFSD_V4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451static int
452set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
453{
454 int len;
455 size_t buflen;
456 char *buf = NULL;
457 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 buflen = posix_acl_xattr_size(pacl->a_count);
460 buf = kmalloc(buflen, GFP_KERNEL);
461 error = -ENOMEM;
462 if (buf == NULL)
463 goto out;
464
465 len = posix_acl_to_xattr(pacl, buf, buflen);
466 if (len < 0) {
467 error = len;
468 goto out;
469 }
470
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800471 error = vfs_setxattr(dentry, key, buf, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472out:
473 kfree(buf);
474 return error;
475}
476
Al Viro6264d692006-10-19 23:28:58 -0700477__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
479 struct nfs4_acl *acl)
480{
Al Viro6264d692006-10-19 23:28:58 -0700481 __be32 error;
482 int host_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct dentry *dentry;
484 struct inode *inode;
485 struct posix_acl *pacl = NULL, *dpacl = NULL;
486 unsigned int flags = 0;
487
488 /* Get inode */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200489 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (error)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700491 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 dentry = fhp->fh_dentry;
494 inode = dentry->d_inode;
495 if (S_ISDIR(inode->i_mode))
496 flags = NFS4_ACL_DIR;
497
Al Viro6264d692006-10-19 23:28:58 -0700498 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
499 if (host_error == -EINVAL) {
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700500 return nfserr_attrnotsupp;
Al Viro6264d692006-10-19 23:28:58 -0700501 } else if (host_error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto out_nfserr;
503
Al Viro6264d692006-10-19 23:28:58 -0700504 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
505 if (host_error < 0)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700506 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700508 if (S_ISDIR(inode->i_mode))
Al Viro6264d692006-10-19 23:28:58 -0700509 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700511out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 posix_acl_release(pacl);
513 posix_acl_release(dpacl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514out_nfserr:
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800515 if (host_error == -EOPNOTSUPP)
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700516 return nfserr_attrnotsupp;
J. Bruce Fieldsf34f9242007-02-16 01:28:34 -0800517 else
J. Bruce Fields4b2ca382007-07-17 04:04:37 -0700518 return nfserrno(host_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static struct posix_acl *
522_get_posix_acl(struct dentry *dentry, char *key)
523{
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800524 void *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct posix_acl *pacl = NULL;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800526 int buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800528 buflen = nfsd_getxattr(dentry, key, &buf);
529 if (!buflen)
530 buflen = -ENODATA;
531 if (buflen <= 0)
532 return ERR_PTR(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 pacl = posix_acl_from_xattr(buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 kfree(buf);
536 return pacl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539int
540nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
541{
542 struct inode *inode = dentry->d_inode;
543 int error = 0;
544 struct posix_acl *pacl = NULL, *dpacl = NULL;
545 unsigned int flags = 0;
546
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700547 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
549 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
550 if (IS_ERR(pacl)) {
551 error = PTR_ERR(pacl);
552 pacl = NULL;
553 goto out;
554 }
555
556 if (S_ISDIR(inode->i_mode)) {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700557 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
559 dpacl = NULL;
560 else if (IS_ERR(dpacl)) {
561 error = PTR_ERR(dpacl);
562 dpacl = NULL;
563 goto out;
564 }
565 flags = NFS4_ACL_DIR;
566 }
567
568 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
569 if (IS_ERR(*acl)) {
570 error = PTR_ERR(*acl);
571 *acl = NULL;
572 }
573 out:
574 posix_acl_release(pacl);
575 posix_acl_release(dpacl);
576 return error;
577}
578
579#endif /* defined(CONFIG_NFS_V4) */
580
581#ifdef CONFIG_NFSD_V3
582/*
583 * Check server access rights to a file system object
584 */
585struct accessmap {
586 u32 access;
587 int how;
588};
589static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200590 { NFS3_ACCESS_READ, NFSD_MAY_READ },
591 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
592 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
593 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 { 0, 0 }
596};
597
598static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200599 { NFS3_ACCESS_READ, NFSD_MAY_READ },
600 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
601 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
602 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
603 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 { 0, 0 }
606};
607
608static struct accessmap nfs3_anyaccess[] = {
609 /* Some clients - Solaris 2.6 at least, make an access call
610 * to the server to check for access for things like /dev/null
611 * (which really, the server doesn't care about). So
612 * We provide simple access checking for them, looking
613 * mainly at mode bits, and we make sure to ignore read-only
614 * filesystem checks
615 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200616 { NFS3_ACCESS_READ, NFSD_MAY_READ },
617 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
618 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
619 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 { 0, 0 }
622};
623
Al Viro6264d692006-10-19 23:28:58 -0700624__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
626{
627 struct accessmap *map;
628 struct svc_export *export;
629 struct dentry *dentry;
630 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700631 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200633 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (error)
635 goto out;
636
637 export = fhp->fh_export;
638 dentry = fhp->fh_dentry;
639
640 if (S_ISREG(dentry->d_inode->i_mode))
641 map = nfs3_regaccess;
642 else if (S_ISDIR(dentry->d_inode->i_mode))
643 map = nfs3_diraccess;
644 else
645 map = nfs3_anyaccess;
646
647
648 query = *access;
649 for (; map->access; map++) {
650 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700651 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 sresult |= map->access;
654
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700655 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 switch (err2) {
657 case nfs_ok:
658 result |= map->access;
659 break;
660
661 /* the following error codes just mean the access was not allowed,
662 * rather than an error occurred */
663 case nfserr_rofs:
664 case nfserr_acces:
665 case nfserr_perm:
666 /* simply don't "or" in the access bit. */
667 break;
668 default:
669 error = err2;
670 goto out;
671 }
672 }
673 }
674 *access = result;
675 if (supported)
676 *supported = sresult;
677
678 out:
679 return error;
680}
681#endif /* CONFIG_NFSD_V3 */
682
683
684
685/*
686 * Open an existing file or directory.
687 * The access argument indicates the type of open (read/write/lock)
688 * N.B. After this call fhp needs an fh_put
689 */
Al Viro6264d692006-10-19 23:28:58 -0700690__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
692 int access, struct file **filp)
693{
694 struct dentry *dentry;
695 struct inode *inode;
Al Viro6264d692006-10-19 23:28:58 -0700696 int flags = O_RDONLY|O_LARGEFILE;
697 __be32 err;
698 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David Howellse0e81732009-09-02 09:13:40 +0100700 validate_process_creds();
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /*
703 * If we get here, then the client has already done an "open",
704 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
705 * in case a chmod has now revoked permission.
706 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200707 err = fh_verify(rqstp, fhp, type, access | NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (err)
709 goto out;
710
711 dentry = fhp->fh_dentry;
712 inode = dentry->d_inode;
713
714 /* Disallow write access to files with the append-only bit set
715 * or any access when mandatory locking enabled
716 */
717 err = nfserr_perm;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200718 if (IS_APPEND(inode) && (access & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto out;
J. Bruce Fields5e7fc432007-10-02 14:18:12 -0400720 /*
721 * We must ignore files (but only files) which might have mandatory
722 * locks on them because there is no way to know if the accesser has
723 * the lock.
724 */
725 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out;
727
728 if (!inode->i_fop)
729 goto out;
730
731 /*
732 * Check to see if there are any leases on this file.
733 * This may block while leases are broken.
734 */
Al Viro8737c932009-12-24 06:47:55 -0500735 host_err = break_lease(inode, O_NONBLOCK | ((access & NFSD_MAY_WRITE) ? O_WRONLY : 0));
Al Viro6264d692006-10-19 23:28:58 -0700736 if (host_err == -EWOULDBLOCK)
737 host_err = -ETIMEDOUT;
738 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto out_nfserr;
740
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200741 if (access & NFSD_MAY_WRITE) {
742 if (access & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700743 flags = O_RDWR|O_LARGEFILE;
744 else
745 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Jan Blunck54775492008-02-14 19:38:39 -0800747 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
David Howells033a6662009-07-02 14:35:32 +0100748 flags, current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (IS_ERR(*filp))
Al Viro6264d692006-10-19 23:28:58 -0700750 host_err = PTR_ERR(*filp);
Chuck Ebbertaeaa5cc2010-02-15 18:07:39 -0500751 else
752 host_err = ima_file_check(*filp, access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700754 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755out:
David Howellse0e81732009-09-02 09:13:40 +0100756 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return err;
758}
759
760/*
761 * Close a file.
762 */
763void
764nfsd_close(struct file *filp)
765{
766 fput(filp);
767}
768
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500769/*
770 * Sync a file
771 * As this calls fsync (not fdatasync) there is no need for a write_inode
772 * after it.
773 */
774static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
775 const struct file_operations *fop)
776{
777 struct inode *inode = dp->d_inode;
778 int (*fsync) (struct file *, struct dentry *, int);
779 int err;
780
Christoph Hellwig7211a4e2009-12-25 17:44:45 +0100781 err = filemap_write_and_wait(inode->i_mapping);
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500782 if (err == 0 && fop && (fsync = fop->fsync))
783 err = fsync(filp, dp, 0);
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500784 return err;
785}
786
David Shawa334de22006-01-06 00:19:58 -0800787static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788nfsd_sync(struct file *filp)
789{
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500790 int err;
791 struct inode *inode = filp->f_path.dentry->d_inode;
792 dprintk("nfsd: sync file %s\n", filp->f_path.dentry->d_name.name);
793 mutex_lock(&inode->i_mutex);
794 err=nfsd_dosync(filp, filp->f_path.dentry, filp->f_op);
795 mutex_unlock(&inode->i_mutex);
796
797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -0800800int
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500801nfsd_sync_dir(struct dentry *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
J. Bruce Fields9a8d2482009-01-06 13:37:03 -0500803 return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806/*
807 * Obtain the readahead parameters for the file
808 * specified by (dev, ino).
809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811static inline struct raparms *
812nfsd_get_raparms(dev_t dev, ino_t ino)
813{
814 struct raparms *ra, **rap, **frap = NULL;
815 int depth = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700816 unsigned int hash;
817 struct raparm_hbucket *rab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Greg Banksfce14562006-10-04 02:15:49 -0700819 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
820 rab = &raparm_hash[hash];
821
822 spin_lock(&rab->pb_lock);
823 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (ra->p_ino == ino && ra->p_dev == dev)
825 goto found;
826 depth++;
827 if (ra->p_count == 0)
828 frap = rap;
829 }
830 depth = nfsdstats.ra_size*11/10;
831 if (!frap) {
Greg Banksfce14562006-10-04 02:15:49 -0700832 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return NULL;
834 }
835 rap = frap;
836 ra = *frap;
837 ra->p_dev = dev;
838 ra->p_ino = ino;
839 ra->p_set = 0;
Greg Banksfce14562006-10-04 02:15:49 -0700840 ra->p_hindex = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841found:
Greg Banksfce14562006-10-04 02:15:49 -0700842 if (rap != &rab->pb_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 *rap = ra->p_next;
Greg Banksfce14562006-10-04 02:15:49 -0700844 ra->p_next = rab->pb_head;
845 rab->pb_head = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 ra->p_count++;
848 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
Greg Banksfce14562006-10-04 02:15:49 -0700849 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return ra;
851}
852
853/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200854 * Grab and keep cached pages associated with a file in the svc_rqst
855 * so that they can be passed to the network sendmsg/sendpage routines
856 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 */
858static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200859nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
860 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Jens Axboecf8208d2007-06-12 21:22:14 +0200862 struct svc_rqst *rqstp = sd->u.data;
NeilBrown44524352006-10-04 02:15:46 -0700863 struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
Jens Axboecf8208d2007-06-12 21:22:14 +0200864 struct page *page = buf->page;
865 size_t size;
866 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Jens Axboecac36bb02007-06-14 13:10:48 +0200868 ret = buf->ops->confirm(pipe, buf);
Jens Axboecf8208d2007-06-12 21:22:14 +0200869 if (unlikely(ret))
870 return ret;
871
872 size = sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (rqstp->rq_res.page_len == 0) {
875 get_page(page);
NeilBrown44524352006-10-04 02:15:46 -0700876 put_page(*pp);
877 *pp = page;
878 rqstp->rq_resused++;
Jens Axboecf8208d2007-06-12 21:22:14 +0200879 rqstp->rq_res.page_base = buf->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 rqstp->rq_res.page_len = size;
NeilBrown44524352006-10-04 02:15:46 -0700881 } else if (page != pp[-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 get_page(page);
NeilBrown250f3912007-01-26 00:56:59 -0800883 if (*pp)
884 put_page(*pp);
NeilBrown44524352006-10-04 02:15:46 -0700885 *pp = page;
886 rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 rqstp->rq_res.page_len += size;
NeilBrown44524352006-10-04 02:15:46 -0700888 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 rqstp->rq_res.page_len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return size;
892}
893
Jens Axboecf8208d2007-06-12 21:22:14 +0200894static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
895 struct splice_desc *sd)
896{
897 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
898}
899
Dave Hansena8754be2007-10-16 23:31:15 -0700900static inline int svc_msnfs(struct svc_fh *ffhp)
901{
902#ifdef MSNFS
903 return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
904#else
905 return 0;
906#endif
907}
908
Al Viro6264d692006-10-19 23:28:58 -0700909static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
911 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
912{
913 struct inode *inode;
914 struct raparms *ra;
915 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -0700916 __be32 err;
917 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 err = nfserr_perm;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -0800920 inode = file->f_path.dentry->d_inode;
Dave Hansena8754be2007-10-16 23:31:15 -0700921
922 if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 /* Get readahead parameters */
926 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
927
928 if (ra && ra->p_set)
929 file->f_ra = ra->p_ra;
930
Jens Axboecf8208d2007-06-12 21:22:14 +0200931 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
932 struct splice_desc sd = {
933 .len = 0,
934 .total_len = *count,
935 .pos = offset,
936 .u.data = rqstp,
937 };
938
Jens Axboe4fbef202007-07-13 22:42:20 +0200939 rqstp->rq_resused = 1;
Jens Axboecf8208d2007-06-12 21:22:14 +0200940 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 } else {
942 oldfs = get_fs();
943 set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -0700944 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 set_fs(oldfs);
946 }
947
948 /* Write back readahead params */
949 if (ra) {
Greg Banksfce14562006-10-04 02:15:49 -0700950 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
951 spin_lock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ra->p_ra = file->f_ra;
953 ra->p_set = 1;
954 ra->p_count--;
Greg Banksfce14562006-10-04 02:15:49 -0700955 spin_unlock(&rab->pb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
Al Viro6264d692006-10-19 23:28:58 -0700958 if (host_err >= 0) {
959 nfsdstats.io_read += host_err;
960 *count = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 err = 0;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -0800962 fsnotify_access(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 } else
Al Viro6264d692006-10-19 23:28:58 -0700964 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965out:
966 return err;
967}
968
Neil Brown9f708e42006-01-06 00:19:59 -0800969static void kill_suid(struct dentry *dentry)
970{
971 struct iattr ia;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700972 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Neil Brown9f708e42006-01-06 00:19:59 -0800973
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800974 mutex_lock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800975 notify_change(dentry, &ia);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800976 mutex_unlock(&dentry->d_inode->i_mutex);
Neil Brown9f708e42006-01-06 00:19:59 -0800977}
978
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700979/*
980 * Gathered writes: If another process is currently writing to the file,
981 * there's a high chance this is another nfsd (triggered by a bulk write
982 * from a client's biod). Rather than syncing the file with each write
983 * request, we sleep for 10 msec.
984 *
985 * I don't know if this roughly approximates C. Juszak's idea of
986 * gathered writes, but it's a nice and simple solution (IMHO), and it
987 * seems to work:-)
988 *
989 * Note: we do this only in the NFSv2 case, since v3 and higher have a
990 * better tool (separate unstable writes and commits) for solving this
991 * problem.
992 */
993static int wait_for_concurrent_writes(struct file *file)
994{
995 struct inode *inode = file->f_path.dentry->d_inode;
996 static ino_t last_ino;
997 static dev_t last_dev;
998 int err = 0;
999
1000 if (atomic_read(&inode->i_writecount) > 1
1001 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1002 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1003 msleep(10);
1004 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1005 }
1006
1007 if (inode->i_state & I_DIRTY) {
1008 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1009 err = nfsd_sync(file);
1010 }
1011 last_ino = inode->i_ino;
1012 last_dev = inode->i_sb->s_dev;
1013 return err;
1014}
1015
Al Viro6264d692006-10-19 23:28:58 -07001016static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1018 loff_t offset, struct kvec *vec, int vlen,
David Shaw31dec252009-03-05 20:16:14 -05001019 unsigned long *cnt, int *stablep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct svc_export *exp;
1022 struct dentry *dentry;
1023 struct inode *inode;
1024 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001025 __be32 err = 0;
1026 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int stable = *stablep;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001028 int use_wgather;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
NeilBrown45bd3b32006-01-18 17:43:50 -08001030#ifdef MSNFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 err = nfserr_perm;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
David Shaw31dec252009-03-05 20:16:14 -05001034 (!lock_may_write(file->f_path.dentry->d_inode, offset, *cnt)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 goto out;
1036#endif
1037
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08001038 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 inode = dentry->d_inode;
1040 exp = fhp->fh_export;
1041
1042 /*
1043 * Request sync writes if
1044 * - the sync export option has been set, or
1045 * - the client requested O_SYNC behavior (NFSv3 feature).
1046 * - The file system doesn't support fsync().
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001047 * When NFSv2 gathered writes have been configured for this volume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * flushing the data to disk is handled separately below.
1049 */
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001050 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Harvey Harrison3ba15142008-02-20 12:49:02 -08001052 if (!file->f_op->fsync) {/* COMMIT3 cannot work */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 stable = 2;
1054 *stablep = 2; /* FILE_SYNC */
1055 }
1056
1057 if (!EX_ISSYNC(exp))
1058 stable = 0;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001059 if (stable && !use_wgather) {
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001060 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 file->f_flags |= O_SYNC;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001062 spin_unlock(&file->f_lock);
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /* Write the data. */
1066 oldfs = get_fs(); set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -07001067 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 set_fs(oldfs);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001069 if (host_err < 0)
1070 goto out_nfserr;
1071 *cnt = host_err;
1072 nfsdstats.io_write += host_err;
1073 fsnotify_modify(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 /* clear setuid/setgid flag after write */
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001076 if (inode->i_mode & (S_ISUID | S_ISGID))
Neil Brown9f708e42006-01-06 00:19:59 -08001077 kill_suid(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001079 if (stable && use_wgather)
J. Bruce Fieldsd911df72009-06-15 16:03:53 -07001080 host_err = wait_for_concurrent_writes(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001082out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001083 dprintk("nfsd: write complete host_err=%d\n", host_err);
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001084 if (host_err >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 err = 0;
Wei Yongjuna0d24b22009-05-19 12:03:15 +08001086 else
Al Viro6264d692006-10-19 23:28:58 -07001087 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088out:
1089 return err;
1090}
1091
1092/*
1093 * Read data from a file. count must contain the requested read count
1094 * on entry. On return, *count contains the number of bytes actually read.
1095 * N.B. After this call fhp needs an fh_put
1096 */
Al Viro6264d692006-10-19 23:28:58 -07001097__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1099 loff_t offset, struct kvec *vec, int vlen,
1100 unsigned long *count)
1101{
Al Viro6264d692006-10-19 23:28:58 -07001102 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001105 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001106 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (err)
1108 goto out;
1109 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1110 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001111 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (err)
1113 goto out;
1114 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1115 nfsd_close(file);
1116 }
1117out:
1118 return err;
1119}
1120
1121/*
1122 * Write data to a file.
1123 * The stable flag requests synchronous writes.
1124 * N.B. After this call fhp needs an fh_put
1125 */
Al Viro6264d692006-10-19 23:28:58 -07001126__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
David Shaw31dec252009-03-05 20:16:14 -05001128 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int *stablep)
1130{
Al Viro6264d692006-10-19 23:28:58 -07001131 __be32 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 if (file) {
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07001134 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001135 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (err)
1137 goto out;
1138 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1139 stablep);
1140 } else {
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001141 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (err)
1143 goto out;
1144
1145 if (cnt)
1146 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1147 cnt, stablep);
1148 nfsd_close(file);
1149 }
1150out:
1151 return err;
1152}
1153
1154#ifdef CONFIG_NFSD_V3
1155/*
1156 * Commit all pending writes to stable storage.
1157 * Strictly speaking, we could sync just the indicated file region here,
1158 * but there's currently no way we can ask the VFS to do so.
1159 *
1160 * Unfortunately we cannot lock the file to make sure we return full WCC
1161 * data to the client, as locking happens lower down in the filesystem.
1162 */
Al Viro6264d692006-10-19 23:28:58 -07001163__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1165 loff_t offset, unsigned long count)
1166{
1167 struct file *file;
Al Viro6264d692006-10-19 23:28:58 -07001168 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 if ((u64)count > ~(u64)offset)
1171 return nfserr_inval;
1172
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001173 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1174 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return err;
1176 if (EX_ISSYNC(fhp->fh_export)) {
1177 if (file->f_op && file->f_op->fsync) {
NeilBrown45bd3b32006-01-18 17:43:50 -08001178 err = nfserrno(nfsd_sync(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 } else {
1180 err = nfserr_notsupp;
1181 }
1182 }
1183
1184 nfsd_close(file);
1185 return err;
1186}
1187#endif /* CONFIG_NFSD_V3 */
1188
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001189static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001190nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1191 struct iattr *iap)
1192{
1193 /*
1194 * Mode has already been set earlier in create:
1195 */
1196 iap->ia_valid &= ~ATTR_MODE;
1197 /*
1198 * Setting uid/gid works only for root. Irix appears to
1199 * send along the gid on create when it tries to implement
1200 * setgid directories via NFS:
1201 */
David Howells5cc0a842008-11-14 10:38:58 +11001202 if (current_fsuid() != 0)
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001203 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1204 if (iap->ia_valid)
1205 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1206 return 0;
1207}
1208
wengang wang4ac35c22009-02-10 11:27:51 +08001209/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1210 * setting size to 0 may fail for some specific file systems by the permission
1211 * checking which requires WRITE permission but the mode is 000.
1212 * we ignore the resizing(to 0) on the just new created file, since the size is
1213 * 0 after file created.
1214 *
1215 * call this only after vfs_create() is called.
1216 * */
1217static void
1218nfsd_check_ignore_resizing(struct iattr *iap)
1219{
1220 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1221 iap->ia_valid &= ~ATTR_SIZE;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/*
1225 * Create a file (regular, directory, device, fifo); UNIX sockets
1226 * not yet implemented.
1227 * If the response fh has been verified, the parent directory should
1228 * already be locked. Note that the parent directory is left locked.
1229 *
1230 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1231 */
Al Viro6264d692006-10-19 23:28:58 -07001232__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1234 char *fname, int flen, struct iattr *iap,
1235 int type, dev_t rdev, struct svc_fh *resfhp)
1236{
1237 struct dentry *dentry, *dchild = NULL;
1238 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001239 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001240 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001241 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 err = nfserr_perm;
1244 if (!flen)
1245 goto out;
1246 err = nfserr_exist;
1247 if (isdotent(fname, flen))
1248 goto out;
1249
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001250 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (err)
1252 goto out;
1253
1254 dentry = fhp->fh_dentry;
1255 dirp = dentry->d_inode;
1256
1257 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001258 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto out;
1260 /*
1261 * Check whether the response file handle has been verified yet.
1262 * If it has, the parent directory should already be locked.
1263 */
1264 if (!resfhp->fh_dentry) {
1265 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001266 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001268 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (IS_ERR(dchild))
1270 goto out_nfserr;
1271 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1272 if (err)
1273 goto out;
1274 } else {
1275 /* called from nfsd_proc_create */
1276 dchild = dget(resfhp->fh_dentry);
1277 if (!fhp->fh_locked) {
1278 /* not actually possible */
1279 printk(KERN_ERR
1280 "nfsd_create: parent %s/%s not locked!\n",
1281 dentry->d_parent->d_name.name,
1282 dentry->d_name.name);
Al Virod75f2b92006-01-18 17:43:44 -08001283 err = nfserr_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto out;
1285 }
1286 }
1287 /*
1288 * Make sure the child dentry is still negative ...
1289 */
1290 err = nfserr_exist;
1291 if (dchild->d_inode) {
1292 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1293 dentry->d_name.name, dchild->d_name.name);
1294 goto out;
1295 }
1296
1297 if (!(iap->ia_valid & ATTR_MODE))
1298 iap->ia_mode = 0;
1299 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1300
Miklos Szeredi07cad1d2008-07-01 15:38:35 +02001301 err = nfserr_inval;
1302 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1303 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1304 type);
1305 goto out;
1306 }
1307
1308 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1309 if (host_err)
1310 goto out_nfserr;
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /*
1313 * Get the dir op function pointer.
1314 */
J. Bruce Fields088406b2006-11-08 17:44:59 -08001315 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 switch (type) {
1317 case S_IFREG:
Al Viro6264d692006-10-19 23:28:58 -07001318 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
wengang wang4ac35c22009-02-10 11:27:51 +08001319 if (!host_err)
1320 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 case S_IFDIR:
Al Viro6264d692006-10-19 23:28:58 -07001323 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325 case S_IFCHR:
1326 case S_IFBLK:
1327 case S_IFIFO:
1328 case S_IFSOCK:
Al Viro6264d692006-10-19 23:28:58 -07001329 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 break;
Dave Hansen463c3192008-02-15 14:37:57 -08001331 }
1332 if (host_err < 0) {
1333 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1334 goto out_nfserr;
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (EX_ISSYNC(fhp->fh_export)) {
NeilBrown45bd3b32006-01-18 17:43:50 -08001338 err = nfserrno(nfsd_sync_dir(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 write_inode_now(dchild->d_inode, 1);
1340 }
1341
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001342 err2 = nfsd_create_setattr(rqstp, resfhp, iap);
1343 if (err2)
1344 err = err2;
Dave Hansen463c3192008-02-15 14:37:57 -08001345 mnt_drop_write(fhp->fh_export->ex_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 /*
1347 * Update the file handle to get the new inode info.
1348 */
1349 if (!err)
1350 err = fh_update(resfhp);
1351out:
1352 if (dchild && !IS_ERR(dchild))
1353 dput(dchild);
1354 return err;
1355
1356out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001357 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 goto out;
1359}
1360
1361#ifdef CONFIG_NFSD_V3
1362/*
1363 * NFSv3 version of nfsd_create
1364 */
Al Viro6264d692006-10-19 23:28:58 -07001365__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
1367 char *fname, int flen, struct iattr *iap,
1368 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001369 int *truncp, int *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 struct dentry *dentry, *dchild = NULL;
1372 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001373 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001374 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001375 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 err = nfserr_perm;
1379 if (!flen)
1380 goto out;
1381 err = nfserr_exist;
1382 if (isdotent(fname, flen))
1383 goto out;
1384 if (!(iap->ia_valid & ATTR_MODE))
1385 iap->ia_mode = 0;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001386 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (err)
1388 goto out;
1389
1390 dentry = fhp->fh_dentry;
1391 dirp = dentry->d_inode;
1392
1393 /* Get all the sanity checks out of the way before
1394 * we lock the parent. */
1395 err = nfserr_notdir;
Al Viroacfa4382008-12-04 10:06:33 -05001396 if (!dirp->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 goto out;
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001398 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
1401 * Compose the response file handle.
1402 */
1403 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001404 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (IS_ERR(dchild))
1406 goto out_nfserr;
1407
1408 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1409 if (err)
1410 goto out;
1411
1412 if (createmode == NFS3_CREATE_EXCLUSIVE) {
Peter Staubachc3978522007-01-26 00:57:00 -08001413 /* solaris7 gets confused (bugid 4218508) if these have
Jeff Layton749997e2007-07-31 00:37:51 -07001414 * the high bit set, so just clear the high bits. If this is
1415 * ever changed to use different attrs for storing the
1416 * verifier, then do_open_lookup() will also need to be fixed
1417 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
1419 v_mtime = verifier[0]&0x7fffffff;
1420 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422
Dave Hansen463c3192008-02-15 14:37:57 -08001423 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1424 if (host_err)
1425 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (dchild->d_inode) {
1427 err = 0;
1428
1429 switch (createmode) {
1430 case NFS3_CREATE_UNCHECKED:
1431 if (! S_ISREG(dchild->d_inode->i_mode))
1432 err = nfserr_exist;
1433 else if (truncp) {
1434 /* in nfsv4, we need to treat this case a little
1435 * differently. we don't want to truncate the
1436 * file now; this would be wrong if the OPEN
1437 * fails for some other reason. furthermore,
1438 * if the size is nonzero, we should ignore it
1439 * according to spec!
1440 */
1441 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1442 }
1443 else {
1444 iap->ia_valid &= ATTR_SIZE;
1445 goto set_attr;
1446 }
1447 break;
1448 case NFS3_CREATE_EXCLUSIVE:
1449 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1450 && dchild->d_inode->i_atime.tv_sec == v_atime
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 && dchild->d_inode->i_size == 0 )
1452 break;
1453 /* fallthru */
1454 case NFS3_CREATE_GUARDED:
1455 err = nfserr_exist;
1456 }
Dave Hansen463c3192008-02-15 14:37:57 -08001457 mnt_drop_write(fhp->fh_export->ex_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto out;
1459 }
1460
Al Viro6264d692006-10-19 23:28:58 -07001461 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
Dave Hansen463c3192008-02-15 14:37:57 -08001462 if (host_err < 0) {
1463 mnt_drop_write(fhp->fh_export->ex_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001465 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001466 if (created)
1467 *created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (EX_ISSYNC(fhp->fh_export)) {
NeilBrown45bd3b32006-01-18 17:43:50 -08001470 err = nfserrno(nfsd_sync_dir(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* setattr will sync the child (or not) */
1472 }
1473
wengang wang4ac35c22009-02-10 11:27:51 +08001474 nfsd_check_ignore_resizing(iap);
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (createmode == NFS3_CREATE_EXCLUSIVE) {
Peter Staubachc3978522007-01-26 00:57:00 -08001477 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001479 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* XXX someone who knows this better please fix it for nsec */
1481 iap->ia_mtime.tv_sec = v_mtime;
1482 iap->ia_atime.tv_sec = v_atime;
1483 iap->ia_mtime.tv_nsec = 0;
1484 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 set_attr:
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001488 err2 = nfsd_create_setattr(rqstp, resfhp, iap);
1489 if (err2)
1490 err = err2;
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001491
Dave Hansen463c3192008-02-15 14:37:57 -08001492 mnt_drop_write(fhp->fh_export->ex_path.mnt);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001493 /*
1494 * Update the filehandle to get the new inode info.
1495 */
1496 if (!err)
1497 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 out:
1500 fh_unlock(fhp);
1501 if (dchild && !IS_ERR(dchild))
1502 dput(dchild);
1503 return err;
1504
1505 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001506 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto out;
1508}
1509#endif /* CONFIG_NFSD_V3 */
1510
1511/*
1512 * Read a symlink. On entry, *lenp must contain the maximum path length that
1513 * fits into the buffer. On return, it contains the true length.
1514 * N.B. After this call fhp needs an fh_put
1515 */
Al Viro6264d692006-10-19 23:28:58 -07001516__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1518{
1519 struct dentry *dentry;
1520 struct inode *inode;
1521 mm_segment_t oldfs;
Al Viro6264d692006-10-19 23:28:58 -07001522 __be32 err;
1523 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001525 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (err)
1527 goto out;
1528
1529 dentry = fhp->fh_dentry;
1530 inode = dentry->d_inode;
1531
1532 err = nfserr_inval;
Al Viroacfa4382008-12-04 10:06:33 -05001533 if (!inode->i_op->readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 goto out;
1535
Jan Blunck54775492008-02-14 19:38:39 -08001536 touch_atime(fhp->fh_export->ex_path.mnt, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /* N.B. Why does this call need a get_fs()??
1538 * Remove the set_fs and watch the fireworks:-) --okir
1539 */
1540
1541 oldfs = get_fs(); set_fs(KERNEL_DS);
Al Viro6264d692006-10-19 23:28:58 -07001542 host_err = inode->i_op->readlink(dentry, buf, *lenp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 set_fs(oldfs);
1544
Al Viro6264d692006-10-19 23:28:58 -07001545 if (host_err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 goto out_nfserr;
Al Viro6264d692006-10-19 23:28:58 -07001547 *lenp = host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 err = 0;
1549out:
1550 return err;
1551
1552out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001553 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 goto out;
1555}
1556
1557/*
1558 * Create a symlink and look up its inode
1559 * N.B. After this call _both_ fhp and resfhp need an fh_put
1560 */
Al Viro6264d692006-10-19 23:28:58 -07001561__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1563 char *fname, int flen,
1564 char *path, int plen,
1565 struct svc_fh *resfhp,
1566 struct iattr *iap)
1567{
1568 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001569 __be32 err, cerr;
1570 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 err = nfserr_noent;
1573 if (!flen || !plen)
1574 goto out;
1575 err = nfserr_exist;
1576 if (isdotent(fname, flen))
1577 goto out;
1578
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001579 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (err)
1581 goto out;
1582 fh_lock(fhp);
1583 dentry = fhp->fh_dentry;
1584 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001585 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (IS_ERR(dnew))
1587 goto out_nfserr;
1588
Dave Hansen75c3f292008-02-15 14:37:45 -08001589 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1590 if (host_err)
1591 goto out_nfserr;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (unlikely(path[plen] != 0)) {
1594 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1595 if (path_alloced == NULL)
Al Viro6264d692006-10-19 23:28:58 -07001596 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 else {
1598 strncpy(path_alloced, path, plen);
1599 path_alloced[plen] = 0;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001600 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 kfree(path_alloced);
1602 }
1603 } else
Miklos Szeredidb2e7472008-06-24 16:50:16 +02001604 host_err = vfs_symlink(dentry->d_inode, dnew, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Al Viro6264d692006-10-19 23:28:58 -07001606 if (!host_err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (EX_ISSYNC(fhp->fh_export))
Al Viro6264d692006-10-19 23:28:58 -07001608 host_err = nfsd_sync_dir(dentry);
1609 }
1610 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 fh_unlock(fhp);
1612
Dave Hansen75c3f292008-02-15 14:37:45 -08001613 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1616 dput(dnew);
1617 if (err==0) err = cerr;
1618out:
1619 return err;
1620
1621out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001622 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto out;
1624}
1625
1626/*
1627 * Create a hardlink
1628 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1629 */
Al Viro6264d692006-10-19 23:28:58 -07001630__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1632 char *name, int len, struct svc_fh *tfhp)
1633{
1634 struct dentry *ddir, *dnew, *dold;
1635 struct inode *dirp, *dest;
Al Viro6264d692006-10-19 23:28:58 -07001636 __be32 err;
1637 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001639 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (err)
1641 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001642 err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (err)
1644 goto out;
1645
1646 err = nfserr_perm;
1647 if (!len)
1648 goto out;
1649 err = nfserr_exist;
1650 if (isdotent(name, len))
1651 goto out;
1652
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001653 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 ddir = ffhp->fh_dentry;
1655 dirp = ddir->d_inode;
1656
1657 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001658 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (IS_ERR(dnew))
1660 goto out_nfserr;
1661
1662 dold = tfhp->fh_dentry;
1663 dest = dold->d_inode;
1664
Dave Hansen75c3f292008-02-15 14:37:45 -08001665 host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
1666 if (host_err) {
1667 err = nfserrno(host_err);
1668 goto out_dput;
1669 }
Al Viro6264d692006-10-19 23:28:58 -07001670 host_err = vfs_link(dold, dirp, dnew);
1671 if (!host_err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (EX_ISSYNC(ffhp->fh_export)) {
NeilBrown45bd3b32006-01-18 17:43:50 -08001673 err = nfserrno(nfsd_sync_dir(ddir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 write_inode_now(dest, 1);
1675 }
Al Viro6264d692006-10-19 23:28:58 -07001676 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 } else {
Al Viro6264d692006-10-19 23:28:58 -07001678 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 err = nfserr_acces;
1680 else
Al Viro6264d692006-10-19 23:28:58 -07001681 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001683 mnt_drop_write(tfhp->fh_export->ex_path.mnt);
1684out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001686out_unlock:
1687 fh_unlock(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688out:
1689 return err;
1690
1691out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001692 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001693 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696/*
1697 * Rename a file
1698 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1699 */
Al Viro6264d692006-10-19 23:28:58 -07001700__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1702 struct svc_fh *tfhp, char *tname, int tlen)
1703{
1704 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1705 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001706 __be32 err;
1707 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001709 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (err)
1711 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001712 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (err)
1714 goto out;
1715
1716 fdentry = ffhp->fh_dentry;
1717 fdir = fdentry->d_inode;
1718
1719 tdentry = tfhp->fh_dentry;
1720 tdir = tdentry->d_inode;
1721
1722 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
NeilBrowna56f3932006-06-30 01:56:10 -07001723 if (ffhp->fh_export != tfhp->fh_export)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 goto out;
1725
1726 err = nfserr_perm;
1727 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1728 goto out;
1729
1730 /* cannot use fh_lock as we need deadlock protective ordering
1731 * so do it by hand */
1732 trap = lock_rename(tdentry, fdentry);
1733 ffhp->fh_locked = tfhp->fh_locked = 1;
1734 fill_pre_wcc(ffhp);
1735 fill_pre_wcc(tfhp);
1736
1737 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001738 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (IS_ERR(odentry))
1740 goto out_nfserr;
1741
Al Viro6264d692006-10-19 23:28:58 -07001742 host_err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (!odentry->d_inode)
1744 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001745 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (odentry == trap)
1747 goto out_dput_old;
1748
1749 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001750 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (IS_ERR(ndentry))
1752 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001753 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (ndentry == trap)
1755 goto out_dput_new;
1756
Dave Hansen9079b1e2008-02-15 14:37:49 -08001757 if (svc_msnfs(ffhp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 ((atomic_read(&odentry->d_count) > 1)
1759 || (atomic_read(&ndentry->d_count) > 1))) {
Al Viro6264d692006-10-19 23:28:58 -07001760 host_err = -EPERM;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001761 goto out_dput_new;
1762 }
1763
1764 host_err = -EXDEV;
1765 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1766 goto out_dput_new;
1767 host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
1768 if (host_err)
1769 goto out_dput_new;
1770
Al Viro6264d692006-10-19 23:28:58 -07001771 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
1772 if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
1773 host_err = nfsd_sync_dir(tdentry);
1774 if (!host_err)
1775 host_err = nfsd_sync_dir(fdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
Dave Hansen9079b1e2008-02-15 14:37:49 -08001778 mnt_drop_write(ffhp->fh_export->ex_path.mnt);
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 out_dput_new:
1781 dput(ndentry);
1782 out_dput_old:
1783 dput(odentry);
1784 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001785 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 /* we cannot reply on fh_unlock on the two filehandles,
1788 * as that would do the wrong thing if the two directories
1789 * were the same, so again we do it by hand
1790 */
1791 fill_post_wcc(ffhp);
1792 fill_post_wcc(tfhp);
1793 unlock_rename(tdentry, fdentry);
1794 ffhp->fh_locked = tfhp->fh_locked = 0;
1795
1796out:
1797 return err;
1798}
1799
1800/*
1801 * Unlink a file or directory
1802 * N.B. After this call fhp needs an fh_put
1803 */
Al Viro6264d692006-10-19 23:28:58 -07001804__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1806 char *fname, int flen)
1807{
1808 struct dentry *dentry, *rdentry;
1809 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001810 __be32 err;
1811 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 err = nfserr_acces;
1814 if (!flen || isdotent(fname, flen))
1815 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001816 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (err)
1818 goto out;
1819
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001820 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 dentry = fhp->fh_dentry;
1822 dirp = dentry->d_inode;
1823
1824 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001825 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (IS_ERR(rdentry))
1827 goto out_nfserr;
1828
1829 if (!rdentry->d_inode) {
1830 dput(rdentry);
1831 err = nfserr_noent;
1832 goto out;
1833 }
1834
1835 if (!type)
1836 type = rdentry->d_inode->i_mode & S_IFMT;
1837
Dave Hansen06227532008-02-15 14:37:34 -08001838 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1839 if (host_err)
1840 goto out_nfserr;
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (type != S_IFDIR) { /* It's UNLINK */
1843#ifdef MSNFS
1844 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1845 (atomic_read(&rdentry->d_count) > 1)) {
Al Viro6264d692006-10-19 23:28:58 -07001846 host_err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 } else
1848#endif
Al Viro6264d692006-10-19 23:28:58 -07001849 host_err = vfs_unlink(dirp, rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 } else { /* It's RMDIR */
Al Viro6264d692006-10-19 23:28:58 -07001851 host_err = vfs_rmdir(dirp, rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
1853
1854 dput(rdentry);
1855
Al Viro6264d692006-10-19 23:28:58 -07001856 if (host_err)
Dave Hansen06227532008-02-15 14:37:34 -08001857 goto out_drop;
Al Viro6264d692006-10-19 23:28:58 -07001858 if (EX_ISSYNC(fhp->fh_export))
1859 host_err = nfsd_sync_dir(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Dave Hansen06227532008-02-15 14:37:34 -08001861out_drop:
1862 mnt_drop_write(fhp->fh_export->ex_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001864 err = nfserrno(host_err);
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001865out:
1866 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
1869/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001870 * We do this buffering because we must not call back into the file
1871 * system's ->lookup() method from the filldir callback. That may well
1872 * deadlock a number of file systems.
1873 *
1874 * This is based heavily on the implementation of same in XFS.
1875 */
1876struct buffered_dirent {
1877 u64 ino;
1878 loff_t offset;
1879 int namlen;
1880 unsigned int d_type;
1881 char name[];
1882};
1883
1884struct readdir_data {
1885 char *dirent;
1886 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001887 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001888};
1889
1890static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1891 loff_t offset, u64 ino, unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001892{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001893 struct readdir_data *buf = __buf;
1894 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1895 unsigned int reclen;
1896
1897 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001898 if (buf->used + reclen > PAGE_SIZE) {
1899 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001900 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001901 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001902
1903 de->namlen = namlen;
1904 de->offset = offset;
1905 de->ino = ino;
1906 de->d_type = d_type;
1907 memcpy(de->name, name, namlen);
1908 buf->used += reclen;
1909
1910 return 0;
1911}
1912
David Woodhouse2f9092e2009-04-20 23:18:37 +01001913static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1914 struct readdir_cd *cdp, loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001915{
1916 struct readdir_data buf;
1917 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001918 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001919 int size;
1920 loff_t offset;
David Woodhouse2628b762008-07-31 17:16:51 +01001921
David Woodhouse14f7dd62008-07-31 20:29:12 +01001922 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1923 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001924 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001925
David Woodhouse14f7dd62008-07-31 20:29:12 +01001926 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001927
1928 while (1) {
David Woodhouse2f9092e2009-04-20 23:18:37 +01001929 struct inode *dir_inode = file->f_path.dentry->d_inode;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001930 unsigned int reclen;
1931
Doug Nazarb726e922008-11-05 06:16:28 -05001932 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001933 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001934 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001935
1936 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
Al Viro53c9c5c2008-08-24 07:29:52 -04001937 if (buf.full)
1938 host_err = 0;
1939
1940 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001941 break;
1942
1943 size = buf.used;
1944
1945 if (!size)
1946 break;
1947
David Woodhouse2f9092e2009-04-20 23:18:37 +01001948 /*
1949 * Various filldir functions may end up calling back into
1950 * lookup_one_len() and the file system's ->lookup() method.
1951 * These expect i_mutex to be held, as it would within readdir.
1952 */
1953 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1954 if (host_err)
1955 break;
1956
David Woodhouse14f7dd62008-07-31 20:29:12 +01001957 de = (struct buffered_dirent *)buf.dirent;
1958 while (size > 0) {
1959 offset = de->offset;
1960
1961 if (func(cdp, de->name, de->namlen, de->offset,
1962 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01001963 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001964
1965 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001966 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001967
1968 reclen = ALIGN(sizeof(*de) + de->namlen,
1969 sizeof(u64));
1970 size -= reclen;
1971 de = (struct buffered_dirent *)((char *)de + reclen);
1972 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01001973 mutex_unlock(&dir_inode->i_mutex);
1974 if (size > 0) /* We bailed out early */
1975 break;
1976
David Woodhousec002a6c2008-08-17 17:21:18 +01001977 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001978 }
1979
David Woodhouse14f7dd62008-07-31 20:29:12 +01001980 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01001981
1982 if (host_err)
1983 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001984
1985 *offsetp = offset;
1986 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01001987}
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989/*
1990 * Read entries from a directory.
1991 * The NFSv3/4 verifier we ignore for now.
1992 */
Al Viro6264d692006-10-19 23:28:58 -07001993__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
NeilBrowna0ad13e2007-01-26 00:57:10 -08001995 struct readdir_cd *cdp, filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Al Viro6264d692006-10-19 23:28:58 -07001997 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct file *file;
1999 loff_t offset = *offsetp;
2000
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002001 err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (err)
2003 goto out;
2004
2005 offset = vfs_llseek(file, offset, 0);
2006 if (offset < 0) {
2007 err = nfserrno((int)offset);
2008 goto out_close;
2009 }
2010
David Woodhouse14f7dd62008-07-31 20:29:12 +01002011 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 if (err == nfserr_eof || err == nfserr_toosmall)
2014 err = nfs_ok; /* can still be found in ->err */
2015out_close:
2016 nfsd_close(file);
2017out:
2018 return err;
2019}
2020
2021/*
2022 * Get file system stats
2023 * N.B. After this call fhp needs an fh_put
2024 */
Al Viro6264d692006-10-19 23:28:58 -07002025__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04002026nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
J. Bruce Fields04716e62008-08-07 13:00:20 -04002028 __be32 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
David Howells726c3342006-06-23 02:02:58 -07002029 if (!err && vfs_statfs(fhp->fh_dentry,stat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 err = nfserr_io;
2031 return err;
2032}
2033
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002034static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002035{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002036 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002037}
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/*
2040 * Check for a user's access permissions to this inode.
2041 */
Al Viro6264d692006-10-19 23:28:58 -07002042__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07002043nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2044 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 struct inode *inode = dentry->d_inode;
Mimi Zohar14dba532009-05-27 09:31:52 -04002047 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int err;
2049
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002050 if (acc == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
2052#if 0
2053 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2054 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002055 (acc & NFSD_MAY_READ)? " read" : "",
2056 (acc & NFSD_MAY_WRITE)? " write" : "",
2057 (acc & NFSD_MAY_EXEC)? " exec" : "",
2058 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2059 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2060 (acc & NFSD_MAY_LOCK)? " lock" : "",
2061 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 inode->i_mode,
2063 IS_IMMUTABLE(inode)? " immut" : "",
2064 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002065 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002067 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068#endif
2069
2070 /* Normally we reject any write/sattr etc access on a read-only file
2071 * system. But if it is IRIX doing check on write-access for a
2072 * device special file, we ignore rofs.
2073 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002074 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2075 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002076 if (exp_rdonly(rqstp, exp) ||
2077 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002079 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return nfserr_perm;
2081 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002082 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return nfserr_perm;
2084
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002085 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /* If we cannot rely on authentication in NLM requests,
2087 * just allow locks, otherwise require read permission, or
2088 * ownership
2089 */
2090 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2091 return 0;
2092 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002093 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 /*
2096 * The file owner always gets access permission for accesses that
2097 * would normally be checked at open time. This is to make
2098 * file access work even when the client has done a fchmod(fd, 0).
2099 *
2100 * However, `cp foo bar' should fail nevertheless when bar is
2101 * readonly. A sensible way to do this might be to reject all
2102 * attempts to truncate a read-only file, because a creat() call
2103 * always implies file truncation.
2104 * ... but this isn't really fair. A process may reasonably call
2105 * ftruncate on an open file descriptor on a file with perm 000.
2106 * We must trust the client to do permission checking - using "ACCESS"
2107 * with NFSv3.
2108 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002109 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
David Howells5cc0a842008-11-14 10:38:58 +11002110 inode->i_uid == current_fsuid())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return 0;
2112
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002113 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Al Virof419a2e2008-07-22 00:07:17 -04002114 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /* Allow read access to binaries even when mode 111 */
2117 if (err == -EACCES && S_ISREG(inode->i_mode) &&
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002118 acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
Al Virof419a2e2008-07-22 00:07:17 -04002119 err = inode_permission(inode, MAY_EXEC);
Mimi Zohar14dba532009-05-27 09:31:52 -04002120 if (err)
2121 goto nfsd_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Mimi Zohar14dba532009-05-27 09:31:52 -04002123 /* Do integrity (permission) checking now, but defer incrementing
2124 * IMA counts to the actual file open.
2125 */
2126 path.mnt = exp->ex_path.mnt;
2127 path.dentry = dentry;
Mimi Zohar14dba532009-05-27 09:31:52 -04002128nfsd_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return err? nfserrno(err) : 0;
2130}
2131
2132void
2133nfsd_racache_shutdown(void)
2134{
Jeff Layton54a66e52008-08-13 22:03:27 -04002135 struct raparms *raparm, *last_raparm;
2136 unsigned int i;
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 dprintk("nfsd: freeing readahead buffers.\n");
Jeff Layton54a66e52008-08-13 22:03:27 -04002139
2140 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2141 raparm = raparm_hash[i].pb_head;
2142 while(raparm) {
2143 last_raparm = raparm;
2144 raparm = raparm->p_next;
2145 kfree(last_raparm);
2146 }
2147 raparm_hash[i].pb_head = NULL;
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150/*
2151 * Initialize readahead param cache
2152 */
2153int
2154nfsd_racache_init(int cache_size)
2155{
2156 int i;
Greg Banksfce14562006-10-04 02:15:49 -07002157 int j = 0;
2158 int nperbucket;
Jeff Layton54a66e52008-08-13 22:03:27 -04002159 struct raparms **raparm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Greg Banksfce14562006-10-04 02:15:49 -07002161
Jeff Layton54a66e52008-08-13 22:03:27 -04002162 if (raparm_hash[0].pb_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002164 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2165 if (nperbucket < 2)
2166 nperbucket = 2;
2167 cache_size = nperbucket * RAPARM_HASH_SIZE;
Yan Burman4b3bb062006-12-08 02:39:41 -08002168
2169 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
Jeff Layton54a66e52008-08-13 22:03:27 -04002170
2171 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
Yan Burman4b3bb062006-12-08 02:39:41 -08002172 spin_lock_init(&raparm_hash[i].pb_lock);
Jeff Layton54a66e52008-08-13 22:03:27 -04002173
2174 raparm = &raparm_hash[i].pb_head;
2175 for (j = 0; j < nperbucket; j++) {
2176 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2177 if (!*raparm)
2178 goto out_nomem;
2179 raparm = &(*raparm)->p_next;
2180 }
2181 *raparm = NULL;
Yan Burman4b3bb062006-12-08 02:39:41 -08002182 }
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 nfsdstats.ra_size = cache_size;
2185 return 0;
Jeff Layton54a66e52008-08-13 22:03:27 -04002186
2187out_nomem:
2188 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2189 nfsd_racache_shutdown();
2190 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002192
2193#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2194struct posix_acl *
2195nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2196{
2197 struct inode *inode = fhp->fh_dentry->d_inode;
2198 char *name;
2199 void *value = NULL;
2200 ssize_t size;
2201 struct posix_acl *acl;
2202
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002203 if (!IS_POSIXACL(inode))
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002204 return ERR_PTR(-EOPNOTSUPP);
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002205
2206 switch (type) {
2207 case ACL_TYPE_ACCESS:
2208 name = POSIX_ACL_XATTR_ACCESS;
2209 break;
2210 case ACL_TYPE_DEFAULT:
2211 name = POSIX_ACL_XATTR_DEFAULT;
2212 break;
2213 default:
2214 return ERR_PTR(-EOPNOTSUPP);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002215 }
2216
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002217 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2218 if (size < 0)
2219 return ERR_PTR(size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002220
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002221 acl = posix_acl_from_xattr(value, size);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002222 kfree(value);
2223 return acl;
2224}
2225
2226int
2227nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2228{
2229 struct inode *inode = fhp->fh_dentry->d_inode;
2230 char *name;
2231 void *value = NULL;
2232 size_t size;
2233 int error;
2234
Al Viroacfa4382008-12-04 10:06:33 -05002235 if (!IS_POSIXACL(inode) ||
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002236 !inode->i_op->setxattr || !inode->i_op->removexattr)
2237 return -EOPNOTSUPP;
2238 switch(type) {
2239 case ACL_TYPE_ACCESS:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002240 name = POSIX_ACL_XATTR_ACCESS;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002241 break;
2242 case ACL_TYPE_DEFAULT:
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002243 name = POSIX_ACL_XATTR_DEFAULT;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002244 break;
2245 default:
2246 return -EOPNOTSUPP;
2247 }
2248
2249 if (acl && acl->a_count) {
Christoph Hellwig334a13e2005-06-28 20:44:58 -07002250 size = posix_acl_xattr_size(acl->a_count);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002251 value = kmalloc(size, GFP_KERNEL);
2252 if (!value)
2253 return -ENOMEM;
Florin Malita9ccfc292006-05-20 14:59:58 -07002254 error = posix_acl_to_xattr(acl, value, size);
2255 if (error < 0)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002256 goto getout;
Florin Malita9ccfc292006-05-20 14:59:58 -07002257 size = error;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002258 } else
2259 size = 0;
2260
Dave Hansen18f335a2008-02-15 14:37:38 -08002261 error = mnt_want_write(fhp->fh_export->ex_path.mnt);
2262 if (error)
2263 goto getout;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002264 if (size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002265 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002266 else {
2267 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2268 error = 0;
2269 else {
Christoph Hellwig5be196e2006-01-09 20:51:55 -08002270 error = vfs_removexattr(fhp->fh_dentry, name);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002271 if (error == -ENODATA)
2272 error = 0;
2273 }
2274 }
Dave Hansen18f335a2008-02-15 14:37:38 -08002275 mnt_drop_write(fhp->fh_export->ex_path.mnt);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002276
2277getout:
2278 kfree(value);
2279 return error;
2280}
2281#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */