Add a dentry op to allow processes to be held during pathwalk transit
[deliverable/linux.git] / fs / nfsd / vfs.c
CommitLineData
1da177e4
LT
1#define MSNFS /* HACK HACK */
2/*
1da177e4
LT
3 * 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
1da177e4
LT
17#include <linux/fs.h>
18#include <linux/file.h>
d6b29d7c 19#include <linux/splice.h>
1da177e4 20#include <linux/fcntl.h>
1da177e4 21#include <linux/namei.h>
1da177e4 22#include <linux/delay.h>
0eeca283 23#include <linux/fsnotify.h>
1da177e4 24#include <linux/posix_acl_xattr.h>
1da177e4 25#include <linux/xattr.h>
9a74af21
BH
26#include <linux/jhash.h>
27#include <linux/ima.h>
5a0e3ad6 28#include <linux/slab.h>
9a74af21 29#include <asm/uaccess.h>
f501912a
BM
30#include <linux/exportfs.h>
31#include <linux/writeback.h>
9a74af21
BH
32
33#ifdef CONFIG_NFSD_V3
34#include "xdr3.h"
35#endif /* CONFIG_NFSD_V3 */
36
5be196e5 37#ifdef CONFIG_NFSD_V4
1da177e4
LT
38#include <linux/nfs4_acl.h>
39#include <linux/nfsd_idmap.h>
1da177e4
LT
40#endif /* CONFIG_NFSD_V4 */
41
9a74af21
BH
42#include "nfsd.h"
43#include "vfs.h"
1da177e4
LT
44
45#define NFSDDBG_FACILITY NFSDDBG_FILEOP
1da177e4
LT
46
47
1da177e4
LT
48/*
49 * This is a cache of readahead params that help us choose the proper
50 * readahead strategy. Initially, we set all readahead parameters to 0
51 * and let the VFS handle things.
52 * If you increase the number of cached files very much, you'll need to
53 * add a hash table here.
54 */
55struct raparms {
56 struct raparms *p_next;
57 unsigned int p_count;
58 ino_t p_ino;
59 dev_t p_dev;
60 int p_set;
61 struct file_ra_state p_ra;
fce1456a 62 unsigned int p_hindex;
1da177e4
LT
63};
64
fce1456a
GB
65struct raparm_hbucket {
66 struct raparms *pb_head;
67 spinlock_t pb_lock;
68} ____cacheline_aligned_in_smp;
69
fce1456a
GB
70#define RAPARM_HASH_BITS 4
71#define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
72#define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
73static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
1da177e4
LT
74
75/*
76 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
77 * a mount point.
e0bb89ef 78 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
1da177e4
LT
79 * or nfs_ok having possibly changed *dpp and *expp
80 */
81int
82nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
83 struct svc_export **expp)
84{
85 struct svc_export *exp = *expp, *exp2 = NULL;
86 struct dentry *dentry = *dpp;
91c9fa8f
AV
87 struct path path = {.mnt = mntget(exp->ex_path.mnt),
88 .dentry = dget(dentry)};
6264d69d 89 int err = 0;
1da177e4 90
cc53ce53
DH
91 err = follow_down(&path, false);
92 if (err < 0)
93 goto out;
1da177e4 94
91c9fa8f 95 exp2 = rqst_exp_get_by_name(rqstp, &path);
1da177e4 96 if (IS_ERR(exp2)) {
3b6cee7b
BF
97 err = PTR_ERR(exp2);
98 /*
99 * We normally allow NFS clients to continue
100 * "underneath" a mountpoint that is not exported.
101 * The exception is V4ROOT, where no traversal is ever
102 * allowed without an explicit export of the new
103 * directory.
104 */
105 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
106 err = 0;
91c9fa8f 107 path_put(&path);
1da177e4
LT
108 goto out;
109 }
3c394dda
SD
110 if (nfsd_v4client(rqstp) ||
111 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
1da177e4 112 /* successfully crossed mount point */
1644ccc8 113 /*
91c9fa8f
AV
114 * This is subtle: path.dentry is *not* on path.mnt
115 * at this point. The only reason we are safe is that
116 * original mnt is pinned down by exp, so we should
117 * put path *before* putting exp
1644ccc8 118 */
91c9fa8f
AV
119 *dpp = path.dentry;
120 path.dentry = dentry;
1644ccc8 121 *expp = exp2;
91c9fa8f 122 exp2 = exp;
1da177e4 123 }
91c9fa8f
AV
124 path_put(&path);
125 exp_put(exp2);
1da177e4
LT
126out:
127 return err;
128}
129
289ede45
BF
130static void follow_to_parent(struct path *path)
131{
132 struct dentry *dp;
133
134 while (path->dentry == path->mnt->mnt_root && follow_up(path))
135 ;
136 dp = dget_parent(path->dentry);
137 dput(path->dentry);
138 path->dentry = dp;
139}
140
141static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
142{
143 struct svc_export *exp2;
144 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
145 .dentry = dget(dparent)};
146
147 follow_to_parent(&path);
148
149 exp2 = rqst_exp_parent(rqstp, &path);
150 if (PTR_ERR(exp2) == -ENOENT) {
151 *dentryp = dget(dparent);
152 } else if (IS_ERR(exp2)) {
153 path_put(&path);
154 return PTR_ERR(exp2);
155 } else {
156 *dentryp = dget(path.dentry);
157 exp_put(*exp);
158 *exp = exp2;
159 }
160 path_put(&path);
161 return 0;
162}
163
82ead7fe
BF
164/*
165 * For nfsd purposes, we treat V4ROOT exports as though there was an
166 * export at *every* directory.
167 */
3227fa41 168int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
82ead7fe
BF
169{
170 if (d_mountpoint(dentry))
171 return 1;
172 if (!(exp->ex_flags & NFSEXP_V4ROOT))
173 return 0;
174 return dentry->d_inode != NULL;
175}
176
6264d69d 177__be32
6c0a654d 178nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
5a022fc8 179 const char *name, unsigned int len,
6c0a654d 180 struct svc_export **exp_ret, struct dentry **dentry_ret)
1da177e4
LT
181{
182 struct svc_export *exp;
183 struct dentry *dparent;
184 struct dentry *dentry;
6264d69d
AV
185 __be32 err;
186 int host_err;
1da177e4
LT
187
188 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
189
190 /* Obtain dentry and export. */
8837abca 191 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1da177e4
LT
192 if (err)
193 return err;
194
195 dparent = fhp->fh_dentry;
196 exp = fhp->fh_export;
197 exp_get(exp);
198
1da177e4
LT
199 /* Lookup the name, but don't follow links */
200 if (isdotent(name, len)) {
201 if (len==1)
202 dentry = dget(dparent);
54775491 203 else if (dparent != exp->ex_path.dentry)
1da177e4 204 dentry = dget_parent(dparent);
fed83811 205 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
1da177e4
LT
206 dentry = dget(dparent); /* .. == . just like at / */
207 else {
208 /* checking mountpoint crossing is very different when stepping up */
289ede45
BF
209 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
210 if (host_err)
1da177e4 211 goto out_nfserr;
1da177e4
LT
212 }
213 } else {
214 fh_lock(fhp);
215 dentry = lookup_one_len(name, dparent, len);
6264d69d 216 host_err = PTR_ERR(dentry);
1da177e4
LT
217 if (IS_ERR(dentry))
218 goto out_nfserr;
219 /*
220 * check if we have crossed a mount point ...
221 */
82ead7fe 222 if (nfsd_mountpoint(dentry, exp)) {
6264d69d 223 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
1da177e4
LT
224 dput(dentry);
225 goto out_nfserr;
226 }
227 }
228 }
6c0a654d
BF
229 *dentry_ret = dentry;
230 *exp_ret = exp;
231 return 0;
232
233out_nfserr:
234 exp_put(exp);
235 return nfserrno(host_err);
236}
237
238/*
239 * Look up one component of a pathname.
240 * N.B. After this call _both_ fhp and resfh need an fh_put
241 *
242 * If the lookup would cross a mountpoint, and the mounted filesystem
243 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
244 * accepted as it stands and the mounted directory is
245 * returned. Otherwise the covered directory is returned.
246 * NOTE: this mountpoint crossing is not supported properly by all
247 * clients and is explicitly disallowed for NFSv3
248 * NeilBrown <neilb@cse.unsw.edu.au>
249 */
250__be32
251nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
5a022fc8 252 unsigned int len, struct svc_fh *resfh)
6c0a654d
BF
253{
254 struct svc_export *exp;
255 struct dentry *dentry;
256 __be32 err;
257
258 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
259 if (err)
260 return err;
32c1eb0c
AA
261 err = check_nfsd_access(exp, rqstp);
262 if (err)
263 goto out;
1da177e4
LT
264 /*
265 * Note: we compose the file handle now, but as the
266 * dentry may be negative, it may need to be updated.
267 */
268 err = fh_compose(resfh, exp, dentry, fhp);
269 if (!err && !dentry->d_inode)
270 err = nfserr_noent;
32c1eb0c 271out:
1da177e4 272 dput(dentry);
1da177e4
LT
273 exp_put(exp);
274 return err;
1da177e4
LT
275}
276
f501912a
BM
277/*
278 * Commit metadata changes to stable storage.
279 */
280static int
281commit_metadata(struct svc_fh *fhp)
282{
283 struct inode *inode = fhp->fh_dentry->d_inode;
284 const struct export_operations *export_ops = inode->i_sb->s_export_op;
f501912a
BM
285
286 if (!EX_ISSYNC(fhp->fh_export))
287 return 0;
288
c3765016
CH
289 if (export_ops->commit_metadata)
290 return export_ops->commit_metadata(inode);
291 return sync_inode_metadata(inode, 1);
f501912a 292}
6c0a654d 293
1da177e4
LT
294/*
295 * Set various file attributes.
296 * N.B. After this call fhp needs an fh_put
297 */
6264d69d 298__be32
1da177e4
LT
299nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
300 int check_guard, time_t guardtime)
301{
302 struct dentry *dentry;
303 struct inode *inode;
8837abca 304 int accmode = NFSD_MAY_SATTR;
1da177e4 305 int ftype = 0;
6264d69d
AV
306 __be32 err;
307 int host_err;
1da177e4
LT
308 int size_change = 0;
309
310 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
8837abca 311 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
1da177e4
LT
312 if (iap->ia_valid & ATTR_SIZE)
313 ftype = S_IFREG;
314
315 /* Get inode */
316 err = fh_verify(rqstp, fhp, ftype, accmode);
15b7a1b8 317 if (err)
1da177e4
LT
318 goto out;
319
320 dentry = fhp->fh_dentry;
321 inode = dentry->d_inode;
322
15b7a1b8
N
323 /* Ignore any mode updates on symlinks */
324 if (S_ISLNK(inode->i_mode))
325 iap->ia_valid &= ~ATTR_MODE;
326
327 if (!iap->ia_valid)
328 goto out;
329
9c85fca5
CH
330 /*
331 * NFSv2 does not differentiate between "set-[ac]time-to-now"
1da177e4
LT
332 * which only requires access, and "set-[ac]time-to-X" which
333 * requires ownership.
334 * So if it looks like it might be "set both to the same time which
335 * is close to now", and if inode_change_ok fails, then we
336 * convert to "set to now" instead of "set to explicit time"
337 *
338 * We only call inode_change_ok as the last test as technically
339 * it is not an interface that we should be using. It is only
340 * valid if the filesystem does not define it's own i_op->setattr.
341 */
342#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
343#define MAX_TOUCH_TIME_ERROR (30*60)
9c85fca5
CH
344 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
345 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
346 /*
347 * Looks probable.
348 *
349 * Now just make sure time is in the right ballpark.
350 * Solaris, at least, doesn't seem to care what the time
351 * request is. We require it be within 30 minutes of now.
1da177e4 352 */
9c85fca5
CH
353 time_t delta = iap->ia_atime.tv_sec - get_seconds();
354 if (delta < 0)
355 delta = -delta;
356 if (delta < MAX_TOUCH_TIME_ERROR &&
357 inode_change_ok(inode, iap) != 0) {
358 /*
359 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
360 * This will cause notify_change to set these times
361 * to "now"
362 */
363 iap->ia_valid &= ~BOTH_TIME_SET;
364 }
1da177e4
LT
365 }
366
9c85fca5
CH
367 /*
368 * The size case is special.
369 * It changes the file as well as the attributes.
370 */
1da177e4
LT
371 if (iap->ia_valid & ATTR_SIZE) {
372 if (iap->ia_size < inode->i_size) {
8837abca
MS
373 err = nfsd_permission(rqstp, fhp->fh_export, dentry,
374 NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
1da177e4
LT
375 if (err)
376 goto out;
377 }
378
379 /*
380 * If we are changing the size of the file, then
381 * we need to break all leases.
382 */
8737c930 383 host_err = break_lease(inode, O_WRONLY | O_NONBLOCK);
6264d69d
AV
384 if (host_err == -EWOULDBLOCK)
385 host_err = -ETIMEDOUT;
386 if (host_err) /* ENOMEM or EWOULDBLOCK */
1da177e4
LT
387 goto out_nfserr;
388
6264d69d
AV
389 host_err = get_write_access(inode);
390 if (host_err)
1da177e4
LT
391 goto out_nfserr;
392
393 size_change = 1;
6264d69d
AV
394 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
395 if (host_err) {
1da177e4
LT
396 put_write_access(inode);
397 goto out_nfserr;
398 }
1da177e4
LT
399 }
400
ca456252 401 /* sanitize the mode change */
1da177e4
LT
402 if (iap->ia_valid & ATTR_MODE) {
403 iap->ia_mode &= S_IALLUGO;
dee3209d 404 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
ca456252
JL
405 }
406
407 /* Revoke setuid/setgid on chown */
0953e620
SP
408 if (!S_ISDIR(inode->i_mode) &&
409 (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
410 ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid))) {
ca456252
JL
411 iap->ia_valid |= ATTR_KILL_PRIV;
412 if (iap->ia_valid & ATTR_MODE) {
413 /* we're setting mode too, just clear the s*id bits */
8a0ce7d9 414 iap->ia_mode &= ~S_ISUID;
ca456252
JL
415 if (iap->ia_mode & S_IXGRP)
416 iap->ia_mode &= ~S_ISGID;
417 } else {
418 /* set ATTR_KILL_* bits and let VFS handle it */
419 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
8a0ce7d9 420 }
1da177e4
LT
421 }
422
1da177e4
LT
423 /* Change the attributes. */
424
425 iap->ia_valid |= ATTR_CTIME;
426
427 err = nfserr_notsync;
428 if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
429 fh_lock(fhp);
6264d69d
AV
430 host_err = notify_change(dentry, iap);
431 err = nfserrno(host_err);
1da177e4
LT
432 fh_unlock(fhp);
433 }
434 if (size_change)
435 put_write_access(inode);
436 if (!err)
b160fdab 437 commit_metadata(fhp);
1da177e4
LT
438out:
439 return err;
440
441out_nfserr:
6264d69d 442 err = nfserrno(host_err);
1da177e4
LT
443 goto out;
444}
445
5be196e5
CH
446#if defined(CONFIG_NFSD_V2_ACL) || \
447 defined(CONFIG_NFSD_V3_ACL) || \
448 defined(CONFIG_NFSD_V4)
449static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
450{
451 ssize_t buflen;
6c6a426f 452 ssize_t ret;
5be196e5
CH
453
454 buflen = vfs_getxattr(dentry, key, NULL, 0);
455 if (buflen <= 0)
456 return buflen;
1da177e4 457
5be196e5
CH
458 *buf = kmalloc(buflen, GFP_KERNEL);
459 if (!*buf)
460 return -ENOMEM;
461
6c6a426f
KK
462 ret = vfs_getxattr(dentry, key, *buf, buflen);
463 if (ret < 0)
464 kfree(*buf);
465 return ret;
5be196e5
CH
466}
467#endif
468
469#if defined(CONFIG_NFSD_V4)
1da177e4
LT
470static int
471set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
472{
473 int len;
474 size_t buflen;
475 char *buf = NULL;
476 int error = 0;
1da177e4
LT
477
478 buflen = posix_acl_xattr_size(pacl->a_count);
479 buf = kmalloc(buflen, GFP_KERNEL);
480 error = -ENOMEM;
481 if (buf == NULL)
482 goto out;
483
484 len = posix_acl_to_xattr(pacl, buf, buflen);
485 if (len < 0) {
486 error = len;
487 goto out;
488 }
489
5be196e5 490 error = vfs_setxattr(dentry, key, buf, len, 0);
1da177e4
LT
491out:
492 kfree(buf);
493 return error;
494}
495
6264d69d 496__be32
1da177e4
LT
497nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
498 struct nfs4_acl *acl)
499{
6264d69d
AV
500 __be32 error;
501 int host_error;
1da177e4
LT
502 struct dentry *dentry;
503 struct inode *inode;
504 struct posix_acl *pacl = NULL, *dpacl = NULL;
505 unsigned int flags = 0;
506
507 /* Get inode */
8837abca 508 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
1da177e4 509 if (error)
4b2ca38a 510 return error;
1da177e4
LT
511
512 dentry = fhp->fh_dentry;
513 inode = dentry->d_inode;
514 if (S_ISDIR(inode->i_mode))
515 flags = NFS4_ACL_DIR;
516
6264d69d
AV
517 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
518 if (host_error == -EINVAL) {
4b2ca38a 519 return nfserr_attrnotsupp;
6264d69d 520 } else if (host_error < 0)
1da177e4
LT
521 goto out_nfserr;
522
6264d69d
AV
523 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
524 if (host_error < 0)
4b2ca38a 525 goto out_release;
1da177e4 526
4b2ca38a 527 if (S_ISDIR(inode->i_mode))
6264d69d 528 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
1da177e4 529
4b2ca38a 530out_release:
1da177e4
LT
531 posix_acl_release(pacl);
532 posix_acl_release(dpacl);
1da177e4 533out_nfserr:
f34f9242 534 if (host_error == -EOPNOTSUPP)
4b2ca38a 535 return nfserr_attrnotsupp;
f34f9242 536 else
4b2ca38a 537 return nfserrno(host_error);
1da177e4
LT
538}
539
540static struct posix_acl *
541_get_posix_acl(struct dentry *dentry, char *key)
542{
5be196e5 543 void *buf = NULL;
1da177e4 544 struct posix_acl *pacl = NULL;
5be196e5 545 int buflen;
1da177e4 546
5be196e5
CH
547 buflen = nfsd_getxattr(dentry, key, &buf);
548 if (!buflen)
549 buflen = -ENODATA;
550 if (buflen <= 0)
551 return ERR_PTR(buflen);
1da177e4
LT
552
553 pacl = posix_acl_from_xattr(buf, buflen);
1da177e4
LT
554 kfree(buf);
555 return pacl;
1da177e4
LT
556}
557
558int
559nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
560{
561 struct inode *inode = dentry->d_inode;
562 int error = 0;
563 struct posix_acl *pacl = NULL, *dpacl = NULL;
564 unsigned int flags = 0;
565
9a59f452 566 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
1da177e4
LT
567 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
568 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
569 if (IS_ERR(pacl)) {
570 error = PTR_ERR(pacl);
571 pacl = NULL;
572 goto out;
573 }
574
575 if (S_ISDIR(inode->i_mode)) {
9a59f452 576 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
1da177e4
LT
577 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
578 dpacl = NULL;
579 else if (IS_ERR(dpacl)) {
580 error = PTR_ERR(dpacl);
581 dpacl = NULL;
582 goto out;
583 }
584 flags = NFS4_ACL_DIR;
585 }
586
587 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
588 if (IS_ERR(*acl)) {
589 error = PTR_ERR(*acl);
590 *acl = NULL;
591 }
592 out:
593 posix_acl_release(pacl);
594 posix_acl_release(dpacl);
595 return error;
596}
597
6a85d6c7 598#endif /* defined(CONFIG_NFSD_V4) */
1da177e4
LT
599
600#ifdef CONFIG_NFSD_V3
601/*
602 * Check server access rights to a file system object
603 */
604struct accessmap {
605 u32 access;
606 int how;
607};
608static struct accessmap nfs3_regaccess[] = {
8837abca
MS
609 { NFS3_ACCESS_READ, NFSD_MAY_READ },
610 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
611 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
612 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
1da177e4
LT
613
614 { 0, 0 }
615};
616
617static struct accessmap nfs3_diraccess[] = {
8837abca
MS
618 { NFS3_ACCESS_READ, NFSD_MAY_READ },
619 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
620 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
621 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
622 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
1da177e4
LT
623
624 { 0, 0 }
625};
626
627static struct accessmap nfs3_anyaccess[] = {
628 /* Some clients - Solaris 2.6 at least, make an access call
629 * to the server to check for access for things like /dev/null
630 * (which really, the server doesn't care about). So
631 * We provide simple access checking for them, looking
632 * mainly at mode bits, and we make sure to ignore read-only
633 * filesystem checks
634 */
8837abca
MS
635 { NFS3_ACCESS_READ, NFSD_MAY_READ },
636 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
637 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
638 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
1da177e4
LT
639
640 { 0, 0 }
641};
642
6264d69d 643__be32
1da177e4
LT
644nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
645{
646 struct accessmap *map;
647 struct svc_export *export;
648 struct dentry *dentry;
649 u32 query, result = 0, sresult = 0;
6264d69d 650 __be32 error;
1da177e4 651
8837abca 652 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
1da177e4
LT
653 if (error)
654 goto out;
655
656 export = fhp->fh_export;
657 dentry = fhp->fh_dentry;
658
659 if (S_ISREG(dentry->d_inode->i_mode))
660 map = nfs3_regaccess;
661 else if (S_ISDIR(dentry->d_inode->i_mode))
662 map = nfs3_diraccess;
663 else
664 map = nfs3_anyaccess;
665
666
667 query = *access;
668 for (; map->access; map++) {
669 if (map->access & query) {
6264d69d 670 __be32 err2;
1da177e4
LT
671
672 sresult |= map->access;
673
0ec757df 674 err2 = nfsd_permission(rqstp, export, dentry, map->how);
1da177e4
LT
675 switch (err2) {
676 case nfs_ok:
677 result |= map->access;
678 break;
679
680 /* the following error codes just mean the access was not allowed,
681 * rather than an error occurred */
682 case nfserr_rofs:
683 case nfserr_acces:
684 case nfserr_perm:
685 /* simply don't "or" in the access bit. */
686 break;
687 default:
688 error = err2;
689 goto out;
690 }
691 }
692 }
693 *access = result;
694 if (supported)
695 *supported = sresult;
696
697 out:
698 return error;
699}
700#endif /* CONFIG_NFSD_V3 */
701
702
703
704/*
705 * Open an existing file or directory.
706 * The access argument indicates the type of open (read/write/lock)
707 * N.B. After this call fhp needs an fh_put
708 */
6264d69d 709__be32
1da177e4
LT
710nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
711 int access, struct file **filp)
712{
713 struct dentry *dentry;
714 struct inode *inode;
6264d69d
AV
715 int flags = O_RDONLY|O_LARGEFILE;
716 __be32 err;
91885258 717 int host_err = 0;
1da177e4 718
e0e81739
DH
719 validate_process_creds();
720
1da177e4
LT
721 /*
722 * If we get here, then the client has already done an "open",
723 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
724 * in case a chmod has now revoked permission.
725 */
8837abca 726 err = fh_verify(rqstp, fhp, type, access | NFSD_MAY_OWNER_OVERRIDE);
1da177e4
LT
727 if (err)
728 goto out;
729
730 dentry = fhp->fh_dentry;
731 inode = dentry->d_inode;
732
733 /* Disallow write access to files with the append-only bit set
734 * or any access when mandatory locking enabled
735 */
736 err = nfserr_perm;
8837abca 737 if (IS_APPEND(inode) && (access & NFSD_MAY_WRITE))
1da177e4 738 goto out;
5e7fc436
BF
739 /*
740 * We must ignore files (but only files) which might have mandatory
741 * locks on them because there is no way to know if the accesser has
742 * the lock.
743 */
744 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
1da177e4
LT
745 goto out;
746
747 if (!inode->i_fop)
748 goto out;
749
750 /*
751 * Check to see if there are any leases on this file.
752 * This may block while leases are broken.
753 */
91885258
JL
754 if (!(access & NFSD_MAY_NOT_BREAK_LEASE))
755 host_err = break_lease(inode, O_NONBLOCK | ((access & NFSD_MAY_WRITE) ? O_WRONLY : 0));
6264d69d
AV
756 if (host_err == -EWOULDBLOCK)
757 host_err = -ETIMEDOUT;
758 if (host_err) /* NOMEM or WOULDBLOCK */
1da177e4
LT
759 goto out_nfserr;
760
8837abca
MS
761 if (access & NFSD_MAY_WRITE) {
762 if (access & NFSD_MAY_READ)
9ecb6a08
BF
763 flags = O_RDWR|O_LARGEFILE;
764 else
765 flags = O_WRONLY|O_LARGEFILE;
1da177e4 766 }
54775491 767 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
033a666c 768 flags, current_cred());
1da177e4 769 if (IS_ERR(*filp))
6264d69d 770 host_err = PTR_ERR(*filp);
aeaa5ccd
CE
771 else
772 host_err = ima_file_check(*filp, access);
1da177e4 773out_nfserr:
6264d69d 774 err = nfserrno(host_err);
1da177e4 775out:
e0e81739 776 validate_process_creds();
1da177e4
LT
777 return err;
778}
779
780/*
781 * Close a file.
782 */
783void
784nfsd_close(struct file *filp)
785{
786 fput(filp);
787}
788
1da177e4
LT
789/*
790 * Obtain the readahead parameters for the file
791 * specified by (dev, ino).
792 */
1da177e4
LT
793
794static inline struct raparms *
795nfsd_get_raparms(dev_t dev, ino_t ino)
796{
797 struct raparms *ra, **rap, **frap = NULL;
798 int depth = 0;
fce1456a
GB
799 unsigned int hash;
800 struct raparm_hbucket *rab;
801
802 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
803 rab = &raparm_hash[hash];
1da177e4 804
fce1456a
GB
805 spin_lock(&rab->pb_lock);
806 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
1da177e4
LT
807 if (ra->p_ino == ino && ra->p_dev == dev)
808 goto found;
809 depth++;
810 if (ra->p_count == 0)
811 frap = rap;
812 }
813 depth = nfsdstats.ra_size*11/10;
814 if (!frap) {
fce1456a 815 spin_unlock(&rab->pb_lock);
1da177e4
LT
816 return NULL;
817 }
818 rap = frap;
819 ra = *frap;
820 ra->p_dev = dev;
821 ra->p_ino = ino;
822 ra->p_set = 0;
fce1456a 823 ra->p_hindex = hash;
1da177e4 824found:
fce1456a 825 if (rap != &rab->pb_head) {
1da177e4 826 *rap = ra->p_next;
fce1456a
GB
827 ra->p_next = rab->pb_head;
828 rab->pb_head = ra;
1da177e4
LT
829 }
830 ra->p_count++;
831 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
fce1456a 832 spin_unlock(&rab->pb_lock);
1da177e4
LT
833 return ra;
834}
835
836/*
cf8208d0
JA
837 * Grab and keep cached pages associated with a file in the svc_rqst
838 * so that they can be passed to the network sendmsg/sendpage routines
839 * directly. They will be released after the sending has completed.
1da177e4
LT
840 */
841static int
cf8208d0
JA
842nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
843 struct splice_desc *sd)
1da177e4 844{
cf8208d0 845 struct svc_rqst *rqstp = sd->u.data;
44524359 846 struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
cf8208d0
JA
847 struct page *page = buf->page;
848 size_t size;
cf8208d0
JA
849
850 size = sd->len;
1da177e4
LT
851
852 if (rqstp->rq_res.page_len == 0) {
853 get_page(page);
44524359
N
854 put_page(*pp);
855 *pp = page;
856 rqstp->rq_resused++;
cf8208d0 857 rqstp->rq_res.page_base = buf->offset;
1da177e4 858 rqstp->rq_res.page_len = size;
44524359 859 } else if (page != pp[-1]) {
1da177e4 860 get_page(page);
250f3915
N
861 if (*pp)
862 put_page(*pp);
44524359
N
863 *pp = page;
864 rqstp->rq_resused++;
1da177e4 865 rqstp->rq_res.page_len += size;
44524359 866 } else
1da177e4 867 rqstp->rq_res.page_len += size;
1da177e4 868
1da177e4
LT
869 return size;
870}
871
cf8208d0
JA
872static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
873 struct splice_desc *sd)
874{
875 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
876}
877
a8754bee
DH
878static inline int svc_msnfs(struct svc_fh *ffhp)
879{
880#ifdef MSNFS
881 return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
882#else
883 return 0;
884#endif
885}
886
6264d69d 887static __be32
1da177e4
LT
888nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
889 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
890{
891 struct inode *inode;
1da177e4 892 mm_segment_t oldfs;
6264d69d
AV
893 __be32 err;
894 int host_err;
1da177e4
LT
895
896 err = nfserr_perm;
7eaa36e2 897 inode = file->f_path.dentry->d_inode;
a8754bee
DH
898
899 if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
1da177e4 900 goto out;
1da177e4 901
cf8208d0
JA
902 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
903 struct splice_desc sd = {
904 .len = 0,
905 .total_len = *count,
906 .pos = offset,
907 .u.data = rqstp,
908 };
909
4fbef206 910 rqstp->rq_resused = 1;
cf8208d0 911 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
1da177e4
LT
912 } else {
913 oldfs = get_fs();
914 set_fs(KERNEL_DS);
6264d69d 915 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
1da177e4
LT
916 set_fs(oldfs);
917 }
918
6264d69d
AV
919 if (host_err >= 0) {
920 nfsdstats.io_read += host_err;
921 *count = host_err;
1da177e4 922 err = 0;
2a12a9d7 923 fsnotify_access(file);
1da177e4 924 } else
6264d69d 925 err = nfserrno(host_err);
1da177e4
LT
926out:
927 return err;
928}
929
9f708e40
NB
930static void kill_suid(struct dentry *dentry)
931{
932 struct iattr ia;
b5376771 933 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
9f708e40 934
1b1dcc1b 935 mutex_lock(&dentry->d_inode->i_mutex);
9f708e40 936 notify_change(dentry, &ia);
1b1dcc1b 937 mutex_unlock(&dentry->d_inode->i_mutex);
9f708e40
NB
938}
939
d911df7b
BF
940/*
941 * Gathered writes: If another process is currently writing to the file,
942 * there's a high chance this is another nfsd (triggered by a bulk write
943 * from a client's biod). Rather than syncing the file with each write
944 * request, we sleep for 10 msec.
945 *
946 * I don't know if this roughly approximates C. Juszak's idea of
947 * gathered writes, but it's a nice and simple solution (IMHO), and it
948 * seems to work:-)
949 *
950 * Note: we do this only in the NFSv2 case, since v3 and higher have a
951 * better tool (separate unstable writes and commits) for solving this
952 * problem.
953 */
954static int wait_for_concurrent_writes(struct file *file)
955{
956 struct inode *inode = file->f_path.dentry->d_inode;
957 static ino_t last_ino;
958 static dev_t last_dev;
959 int err = 0;
960
961 if (atomic_read(&inode->i_writecount) > 1
962 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
963 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
964 msleep(10);
965 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
966 }
967
968 if (inode->i_state & I_DIRTY) {
969 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
8018ab05 970 err = vfs_fsync(file, 0);
d911df7b
BF
971 }
972 last_ino = inode->i_ino;
973 last_dev = inode->i_sb->s_dev;
974 return err;
975}
976
6264d69d 977static __be32
1da177e4
LT
978nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
979 loff_t offset, struct kvec *vec, int vlen,
31dec253 980 unsigned long *cnt, int *stablep)
1da177e4
LT
981{
982 struct svc_export *exp;
983 struct dentry *dentry;
984 struct inode *inode;
985 mm_segment_t oldfs;
6264d69d
AV
986 __be32 err = 0;
987 int host_err;
1da177e4 988 int stable = *stablep;
48e03bc5 989 int use_wgather;
1da177e4 990
45bd3b3d 991#ifdef MSNFS
1da177e4
LT
992 err = nfserr_perm;
993
1da177e4 994 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
31dec253 995 (!lock_may_write(file->f_path.dentry->d_inode, offset, *cnt)))
1da177e4
LT
996 goto out;
997#endif
998
7eaa36e2 999 dentry = file->f_path.dentry;
1da177e4
LT
1000 inode = dentry->d_inode;
1001 exp = fhp->fh_export;
1002
1003 /*
1004 * Request sync writes if
1005 * - the sync export option has been set, or
1006 * - the client requested O_SYNC behavior (NFSv3 feature).
1007 * - The file system doesn't support fsync().
48e03bc5 1008 * When NFSv2 gathered writes have been configured for this volume,
1da177e4
LT
1009 * flushing the data to disk is handled separately below.
1010 */
48e03bc5 1011 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1da177e4 1012
3ba15148 1013 if (!file->f_op->fsync) {/* COMMIT3 cannot work */
1da177e4
LT
1014 stable = 2;
1015 *stablep = 2; /* FILE_SYNC */
1016 }
1017
1018 if (!EX_ISSYNC(exp))
1019 stable = 0;
48e03bc5 1020 if (stable && !use_wgather) {
db1dd4d3 1021 spin_lock(&file->f_lock);
1da177e4 1022 file->f_flags |= O_SYNC;
db1dd4d3
JC
1023 spin_unlock(&file->f_lock);
1024 }
1da177e4
LT
1025
1026 /* Write the data. */
1027 oldfs = get_fs(); set_fs(KERNEL_DS);
6264d69d 1028 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
1da177e4 1029 set_fs(oldfs);
e4636d53
BF
1030 if (host_err < 0)
1031 goto out_nfserr;
1032 *cnt = host_err;
1033 nfsdstats.io_write += host_err;
2a12a9d7 1034 fsnotify_modify(file);
1da177e4
LT
1035
1036 /* clear setuid/setgid flag after write */
e4636d53 1037 if (inode->i_mode & (S_ISUID | S_ISGID))
9f708e40 1038 kill_suid(dentry);
1da177e4 1039
e4636d53 1040 if (stable && use_wgather)
d911df7b 1041 host_err = wait_for_concurrent_writes(file);
1da177e4 1042
e4636d53 1043out_nfserr:
6264d69d 1044 dprintk("nfsd: write complete host_err=%d\n", host_err);
a0d24b29 1045 if (host_err >= 0)
1da177e4 1046 err = 0;
a0d24b29 1047 else
6264d69d 1048 err = nfserrno(host_err);
1da177e4
LT
1049out:
1050 return err;
1051}
1052
1053/*
1054 * Read data from a file. count must contain the requested read count
1055 * on entry. On return, *count contains the number of bytes actually read.
1056 * N.B. After this call fhp needs an fh_put
1057 */
039a87ca 1058__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
fa0a2126
BF
1059 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1060{
1061 struct file *file;
1062 struct inode *inode;
1063 struct raparms *ra;
1064 __be32 err;
1065
1066 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1067 if (err)
1068 return err;
1069
1070 inode = file->f_path.dentry->d_inode;
1071
1072 /* Get readahead parameters */
1073 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1074
1075 if (ra && ra->p_set)
1076 file->f_ra = ra->p_ra;
1077
1078 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1079
1080 /* Write back readahead params */
1081 if (ra) {
1082 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1083 spin_lock(&rab->pb_lock);
1084 ra->p_ra = file->f_ra;
1085 ra->p_set = 1;
1086 ra->p_count--;
1087 spin_unlock(&rab->pb_lock);
1088 }
1089
1090 nfsd_close(file);
1091 return err;
1092}
1093
039a87ca 1094/* As above, but use the provided file descriptor. */
6264d69d 1095__be32
039a87ca 1096nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1da177e4
LT
1097 loff_t offset, struct kvec *vec, int vlen,
1098 unsigned long *count)
1099{
6264d69d 1100 __be32 err;
1da177e4
LT
1101
1102 if (file) {
0ec757df 1103 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
8837abca 1104 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
1da177e4
LT
1105 if (err)
1106 goto out;
1107 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
039a87ca
BF
1108 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1109 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
1da177e4
LT
1110out:
1111 return err;
1112}
1113
1114/*
1115 * Write data to a file.
1116 * The stable flag requests synchronous writes.
1117 * N.B. After this call fhp needs an fh_put
1118 */
6264d69d 1119__be32
1da177e4 1120nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
31dec253 1121 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1da177e4
LT
1122 int *stablep)
1123{
6264d69d 1124 __be32 err = 0;
1da177e4
LT
1125
1126 if (file) {
0ec757df 1127 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
8837abca 1128 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1da177e4
LT
1129 if (err)
1130 goto out;
1131 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1132 stablep);
1133 } else {
8837abca 1134 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1da177e4
LT
1135 if (err)
1136 goto out;
1137
1138 if (cnt)
1139 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1140 cnt, stablep);
1141 nfsd_close(file);
1142 }
1143out:
1144 return err;
1145}
1146
1147#ifdef CONFIG_NFSD_V3
1148/*
1149 * Commit all pending writes to stable storage.
aa696a6f
TM
1150 *
1151 * Note: we only guarantee that data that lies within the range specified
1152 * by the 'offset' and 'count' parameters will be synced.
1da177e4
LT
1153 *
1154 * Unfortunately we cannot lock the file to make sure we return full WCC
1155 * data to the client, as locking happens lower down in the filesystem.
1156 */
6264d69d 1157__be32
1da177e4
LT
1158nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1159 loff_t offset, unsigned long count)
1160{
1161 struct file *file;
aa696a6f
TM
1162 loff_t end = LLONG_MAX;
1163 __be32 err = nfserr_inval;
1da177e4 1164
aa696a6f
TM
1165 if (offset < 0)
1166 goto out;
1167 if (count != 0) {
1168 end = offset + (loff_t)count - 1;
1169 if (end < offset)
1170 goto out;
1171 }
1da177e4 1172
91885258
JL
1173 err = nfsd_open(rqstp, fhp, S_IFREG,
1174 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
8837abca 1175 if (err)
aa696a6f 1176 goto out;
1da177e4 1177 if (EX_ISSYNC(fhp->fh_export)) {
8018ab05 1178 int err2 = vfs_fsync_range(file, offset, end, 0);
aa696a6f
TM
1179
1180 if (err2 != -EINVAL)
1181 err = nfserrno(err2);
1182 else
1da177e4 1183 err = nfserr_notsupp;
1da177e4
LT
1184 }
1185
1186 nfsd_close(file);
aa696a6f 1187out:
1da177e4
LT
1188 return err;
1189}
1190#endif /* CONFIG_NFSD_V3 */
1191
f2b0dee2 1192static __be32
5c002b3b
BF
1193nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1194 struct iattr *iap)
1195{
1196 /*
1197 * Mode has already been set earlier in create:
1198 */
1199 iap->ia_valid &= ~ATTR_MODE;
1200 /*
1201 * Setting uid/gid works only for root. Irix appears to
1202 * send along the gid on create when it tries to implement
1203 * setgid directories via NFS:
1204 */
5cc0a840 1205 if (current_fsuid() != 0)
5c002b3b
BF
1206 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1207 if (iap->ia_valid)
1208 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1209 return 0;
1210}
1211
4ac35c2f 1212/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1213 * setting size to 0 may fail for some specific file systems by the permission
1214 * checking which requires WRITE permission but the mode is 000.
1215 * we ignore the resizing(to 0) on the just new created file, since the size is
1216 * 0 after file created.
1217 *
1218 * call this only after vfs_create() is called.
1219 * */
1220static void
1221nfsd_check_ignore_resizing(struct iattr *iap)
1222{
1223 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1224 iap->ia_valid &= ~ATTR_SIZE;
1225}
1226
1da177e4
LT
1227/*
1228 * Create a file (regular, directory, device, fifo); UNIX sockets
1229 * not yet implemented.
1230 * If the response fh has been verified, the parent directory should
1231 * already be locked. Note that the parent directory is left locked.
1232 *
1233 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1234 */
6264d69d 1235__be32
1da177e4
LT
1236nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1237 char *fname, int flen, struct iattr *iap,
1238 int type, dev_t rdev, struct svc_fh *resfhp)
1239{
1240 struct dentry *dentry, *dchild = NULL;
1241 struct inode *dirp;
6264d69d 1242 __be32 err;
5c002b3b 1243 __be32 err2;
6264d69d 1244 int host_err;
1da177e4
LT
1245
1246 err = nfserr_perm;
1247 if (!flen)
1248 goto out;
1249 err = nfserr_exist;
1250 if (isdotent(fname, flen))
1251 goto out;
1252
8837abca 1253 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1da177e4
LT
1254 if (err)
1255 goto out;
1256
1257 dentry = fhp->fh_dentry;
1258 dirp = dentry->d_inode;
1259
1260 err = nfserr_notdir;
acfa4380 1261 if (!dirp->i_op->lookup)
1da177e4
LT
1262 goto out;
1263 /*
1264 * Check whether the response file handle has been verified yet.
1265 * If it has, the parent directory should already be locked.
1266 */
1267 if (!resfhp->fh_dentry) {
1268 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
12fd3520 1269 fh_lock_nested(fhp, I_MUTEX_PARENT);
1da177e4 1270 dchild = lookup_one_len(fname, dentry, flen);
6264d69d 1271 host_err = PTR_ERR(dchild);
1da177e4
LT
1272 if (IS_ERR(dchild))
1273 goto out_nfserr;
1274 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1275 if (err)
1276 goto out;
1277 } else {
1278 /* called from nfsd_proc_create */
1279 dchild = dget(resfhp->fh_dentry);
1280 if (!fhp->fh_locked) {
1281 /* not actually possible */
1282 printk(KERN_ERR
1283 "nfsd_create: parent %s/%s not locked!\n",
1284 dentry->d_parent->d_name.name,
1285 dentry->d_name.name);
d75f2b9f 1286 err = nfserr_io;
1da177e4
LT
1287 goto out;
1288 }
1289 }
1290 /*
1291 * Make sure the child dentry is still negative ...
1292 */
1293 err = nfserr_exist;
1294 if (dchild->d_inode) {
1295 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1296 dentry->d_name.name, dchild->d_name.name);
1297 goto out;
1298 }
1299
1300 if (!(iap->ia_valid & ATTR_MODE))
1301 iap->ia_mode = 0;
1302 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1303
07cad1d2
MS
1304 err = nfserr_inval;
1305 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1306 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1307 type);
1308 goto out;
1309 }
1310
1311 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1312 if (host_err)
1313 goto out_nfserr;
1314
1da177e4
LT
1315 /*
1316 * Get the dir op function pointer.
1317 */
088406bc 1318 err = 0;
1da177e4
LT
1319 switch (type) {
1320 case S_IFREG:
6264d69d 1321 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
4ac35c2f 1322 if (!host_err)
1323 nfsd_check_ignore_resizing(iap);
1da177e4
LT
1324 break;
1325 case S_IFDIR:
6264d69d 1326 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1da177e4
LT
1327 break;
1328 case S_IFCHR:
1329 case S_IFBLK:
1330 case S_IFIFO:
1331 case S_IFSOCK:
6264d69d 1332 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1da177e4 1333 break;
1da177e4 1334 }
463c3197
DH
1335 if (host_err < 0) {
1336 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1da177e4 1337 goto out_nfserr;
463c3197 1338 }
1da177e4 1339
f501912a 1340 err = nfsd_create_setattr(rqstp, resfhp, iap);
1da177e4 1341
f501912a
BM
1342 /*
1343 * nfsd_setattr already committed the child. Transactional filesystems
1344 * had a chance to commit changes for both parent and child
1345 * simultaneously making the following commit_metadata a noop.
1346 */
1347 err2 = nfserrno(commit_metadata(fhp));
5c002b3b
BF
1348 if (err2)
1349 err = err2;
463c3197 1350 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1da177e4
LT
1351 /*
1352 * Update the file handle to get the new inode info.
1353 */
1354 if (!err)
1355 err = fh_update(resfhp);
1356out:
1357 if (dchild && !IS_ERR(dchild))
1358 dput(dchild);
1359 return err;
1360
1361out_nfserr:
6264d69d 1362 err = nfserrno(host_err);
1da177e4
LT
1363 goto out;
1364}
1365
1366#ifdef CONFIG_NFSD_V3
1367/*
1368 * NFSv3 version of nfsd_create
1369 */
6264d69d 1370__be32
1da177e4
LT
1371nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
1372 char *fname, int flen, struct iattr *iap,
1373 struct svc_fh *resfhp, int createmode, u32 *verifier,
81ac95c5 1374 int *truncp, int *created)
1da177e4
LT
1375{
1376 struct dentry *dentry, *dchild = NULL;
1377 struct inode *dirp;
6264d69d
AV
1378 __be32 err;
1379 int host_err;
1da177e4 1380 __u32 v_mtime=0, v_atime=0;
1da177e4
LT
1381
1382 err = nfserr_perm;
1383 if (!flen)
1384 goto out;
1385 err = nfserr_exist;
1386 if (isdotent(fname, flen))
1387 goto out;
1388 if (!(iap->ia_valid & ATTR_MODE))
1389 iap->ia_mode = 0;
8837abca 1390 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1da177e4
LT
1391 if (err)
1392 goto out;
1393
1394 dentry = fhp->fh_dentry;
1395 dirp = dentry->d_inode;
1396
1397 /* Get all the sanity checks out of the way before
1398 * we lock the parent. */
1399 err = nfserr_notdir;
acfa4380 1400 if (!dirp->i_op->lookup)
1da177e4 1401 goto out;
12fd3520 1402 fh_lock_nested(fhp, I_MUTEX_PARENT);
1da177e4
LT
1403
1404 /*
1405 * Compose the response file handle.
1406 */
1407 dchild = lookup_one_len(fname, dentry, flen);
6264d69d 1408 host_err = PTR_ERR(dchild);
1da177e4
LT
1409 if (IS_ERR(dchild))
1410 goto out_nfserr;
1411
1412 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1413 if (err)
1414 goto out;
1415
1416 if (createmode == NFS3_CREATE_EXCLUSIVE) {
c397852c 1417 /* solaris7 gets confused (bugid 4218508) if these have
749997e5
JL
1418 * the high bit set, so just clear the high bits. If this is
1419 * ever changed to use different attrs for storing the
1420 * verifier, then do_open_lookup() will also need to be fixed
1421 * accordingly.
1da177e4
LT
1422 */
1423 v_mtime = verifier[0]&0x7fffffff;
1424 v_atime = verifier[1]&0x7fffffff;
1da177e4
LT
1425 }
1426
463c3197
DH
1427 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1428 if (host_err)
1429 goto out_nfserr;
1da177e4
LT
1430 if (dchild->d_inode) {
1431 err = 0;
1432
1433 switch (createmode) {
1434 case NFS3_CREATE_UNCHECKED:
1435 if (! S_ISREG(dchild->d_inode->i_mode))
1436 err = nfserr_exist;
1437 else if (truncp) {
1438 /* in nfsv4, we need to treat this case a little
1439 * differently. we don't want to truncate the
1440 * file now; this would be wrong if the OPEN
1441 * fails for some other reason. furthermore,
1442 * if the size is nonzero, we should ignore it
1443 * according to spec!
1444 */
1445 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1446 }
1447 else {
1448 iap->ia_valid &= ATTR_SIZE;
1449 goto set_attr;
1450 }
1451 break;
1452 case NFS3_CREATE_EXCLUSIVE:
1453 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1454 && dchild->d_inode->i_atime.tv_sec == v_atime
1da177e4
LT
1455 && dchild->d_inode->i_size == 0 )
1456 break;
1457 /* fallthru */
1458 case NFS3_CREATE_GUARDED:
1459 err = nfserr_exist;
1460 }
463c3197 1461 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1da177e4
LT
1462 goto out;
1463 }
1464
6264d69d 1465 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
463c3197
DH
1466 if (host_err < 0) {
1467 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1da177e4 1468 goto out_nfserr;
463c3197 1469 }
81ac95c5
BF
1470 if (created)
1471 *created = 1;
1da177e4 1472
4ac35c2f 1473 nfsd_check_ignore_resizing(iap);
1474
1da177e4 1475 if (createmode == NFS3_CREATE_EXCLUSIVE) {
c397852c 1476 /* Cram the verifier into atime/mtime */
1da177e4 1477 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
c397852c 1478 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1da177e4
LT
1479 /* XXX someone who knows this better please fix it for nsec */
1480 iap->ia_mtime.tv_sec = v_mtime;
1481 iap->ia_atime.tv_sec = v_atime;
1482 iap->ia_mtime.tv_nsec = 0;
1483 iap->ia_atime.tv_nsec = 0;
1da177e4
LT
1484 }
1485
1da177e4 1486 set_attr:
f501912a
BM
1487 err = nfsd_create_setattr(rqstp, resfhp, iap);
1488
1489 /*
1490 * nfsd_setattr already committed the child (and possibly also the parent).
1491 */
1492 if (!err)
1493 err = nfserrno(commit_metadata(fhp));
f193fbab 1494
463c3197 1495 mnt_drop_write(fhp->fh_export->ex_path.mnt);
f193fbab
YT
1496 /*
1497 * Update the filehandle to get the new inode info.
1498 */
1499 if (!err)
1500 err = fh_update(resfhp);
1da177e4
LT
1501
1502 out:
1503 fh_unlock(fhp);
1504 if (dchild && !IS_ERR(dchild))
1505 dput(dchild);
1506 return err;
1507
1508 out_nfserr:
6264d69d 1509 err = nfserrno(host_err);
1da177e4
LT
1510 goto out;
1511}
1512#endif /* CONFIG_NFSD_V3 */
1513
1514/*
1515 * Read a symlink. On entry, *lenp must contain the maximum path length that
1516 * fits into the buffer. On return, it contains the true length.
1517 * N.B. After this call fhp needs an fh_put
1518 */
6264d69d 1519__be32
1da177e4
LT
1520nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1521{
1522 struct dentry *dentry;
1523 struct inode *inode;
1524 mm_segment_t oldfs;
6264d69d
AV
1525 __be32 err;
1526 int host_err;
1da177e4 1527
8837abca 1528 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1da177e4
LT
1529 if (err)
1530 goto out;
1531
1532 dentry = fhp->fh_dentry;
1533 inode = dentry->d_inode;
1534
1535 err = nfserr_inval;
acfa4380 1536 if (!inode->i_op->readlink)
1da177e4
LT
1537 goto out;
1538
54775491 1539 touch_atime(fhp->fh_export->ex_path.mnt, dentry);
1da177e4
LT
1540 /* N.B. Why does this call need a get_fs()??
1541 * Remove the set_fs and watch the fireworks:-) --okir
1542 */
1543
1544 oldfs = get_fs(); set_fs(KERNEL_DS);
6264d69d 1545 host_err = inode->i_op->readlink(dentry, buf, *lenp);
1da177e4
LT
1546 set_fs(oldfs);
1547
6264d69d 1548 if (host_err < 0)
1da177e4 1549 goto out_nfserr;
6264d69d 1550 *lenp = host_err;
1da177e4
LT
1551 err = 0;
1552out:
1553 return err;
1554
1555out_nfserr:
6264d69d 1556 err = nfserrno(host_err);
1da177e4
LT
1557 goto out;
1558}
1559
1560/*
1561 * Create a symlink and look up its inode
1562 * N.B. After this call _both_ fhp and resfhp need an fh_put
1563 */
6264d69d 1564__be32
1da177e4
LT
1565nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1566 char *fname, int flen,
1567 char *path, int plen,
1568 struct svc_fh *resfhp,
1569 struct iattr *iap)
1570{
1571 struct dentry *dentry, *dnew;
6264d69d
AV
1572 __be32 err, cerr;
1573 int host_err;
1da177e4
LT
1574
1575 err = nfserr_noent;
1576 if (!flen || !plen)
1577 goto out;
1578 err = nfserr_exist;
1579 if (isdotent(fname, flen))
1580 goto out;
1581
8837abca 1582 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1da177e4
LT
1583 if (err)
1584 goto out;
1585 fh_lock(fhp);
1586 dentry = fhp->fh_dentry;
1587 dnew = lookup_one_len(fname, dentry, flen);
6264d69d 1588 host_err = PTR_ERR(dnew);
1da177e4
LT
1589 if (IS_ERR(dnew))
1590 goto out_nfserr;
1591
75c3f29d
DH
1592 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1593 if (host_err)
1594 goto out_nfserr;
1595
1da177e4
LT
1596 if (unlikely(path[plen] != 0)) {
1597 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1598 if (path_alloced == NULL)
6264d69d 1599 host_err = -ENOMEM;
1da177e4
LT
1600 else {
1601 strncpy(path_alloced, path, plen);
1602 path_alloced[plen] = 0;
db2e747b 1603 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1da177e4
LT
1604 kfree(path_alloced);
1605 }
1606 } else
db2e747b 1607 host_err = vfs_symlink(dentry->d_inode, dnew, path);
6264d69d 1608 err = nfserrno(host_err);
f501912a
BM
1609 if (!err)
1610 err = nfserrno(commit_metadata(fhp));
1da177e4
LT
1611 fh_unlock(fhp);
1612
75c3f29d
DH
1613 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1614
1da177e4
LT
1615 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:
6264d69d 1622 err = nfserrno(host_err);
1da177e4
LT
1623 goto out;
1624}
1625
1626/*
1627 * Create a hardlink
1628 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1629 */
6264d69d 1630__be32
1da177e4
LT
1631nfsd_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;
55b13354 1635 struct inode *dirp;
6264d69d
AV
1636 __be32 err;
1637 int host_err;
1da177e4 1638
8837abca 1639 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1da177e4
LT
1640 if (err)
1641 goto out;
8837abca 1642 err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
1da177e4
LT
1643 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
12fd3520 1653 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1da177e4
LT
1654 ddir = ffhp->fh_dentry;
1655 dirp = ddir->d_inode;
1656
1657 dnew = lookup_one_len(name, ddir, len);
6264d69d 1658 host_err = PTR_ERR(dnew);
1da177e4
LT
1659 if (IS_ERR(dnew))
1660 goto out_nfserr;
1661
1662 dold = tfhp->fh_dentry;
1da177e4 1663
75c3f29d
DH
1664 host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
1665 if (host_err) {
1666 err = nfserrno(host_err);
1667 goto out_dput;
1668 }
6264d69d
AV
1669 host_err = vfs_link(dold, dirp, dnew);
1670 if (!host_err) {
f501912a
BM
1671 err = nfserrno(commit_metadata(ffhp));
1672 if (!err)
1673 err = nfserrno(commit_metadata(tfhp));
1da177e4 1674 } else {
6264d69d 1675 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1da177e4
LT
1676 err = nfserr_acces;
1677 else
6264d69d 1678 err = nfserrno(host_err);
1da177e4 1679 }
75c3f29d
DH
1680 mnt_drop_write(tfhp->fh_export->ex_path.mnt);
1681out_dput:
1da177e4 1682 dput(dnew);
270d56e5
DR
1683out_unlock:
1684 fh_unlock(ffhp);
1da177e4
LT
1685out:
1686 return err;
1687
1688out_nfserr:
6264d69d 1689 err = nfserrno(host_err);
270d56e5 1690 goto out_unlock;
1da177e4
LT
1691}
1692
1693/*
1694 * Rename a file
1695 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1696 */
6264d69d 1697__be32
1da177e4
LT
1698nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1699 struct svc_fh *tfhp, char *tname, int tlen)
1700{
1701 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1702 struct inode *fdir, *tdir;
6264d69d
AV
1703 __be32 err;
1704 int host_err;
1da177e4 1705
8837abca 1706 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1da177e4
LT
1707 if (err)
1708 goto out;
8837abca 1709 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1da177e4
LT
1710 if (err)
1711 goto out;
1712
1713 fdentry = ffhp->fh_dentry;
1714 fdir = fdentry->d_inode;
1715
1716 tdentry = tfhp->fh_dentry;
1717 tdir = tdentry->d_inode;
1718
1719 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
a56f3937 1720 if (ffhp->fh_export != tfhp->fh_export)
1da177e4
LT
1721 goto out;
1722
1723 err = nfserr_perm;
1724 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1725 goto out;
1726
1727 /* cannot use fh_lock as we need deadlock protective ordering
1728 * so do it by hand */
1729 trap = lock_rename(tdentry, fdentry);
1730 ffhp->fh_locked = tfhp->fh_locked = 1;
1731 fill_pre_wcc(ffhp);
1732 fill_pre_wcc(tfhp);
1733
1734 odentry = lookup_one_len(fname, fdentry, flen);
6264d69d 1735 host_err = PTR_ERR(odentry);
1da177e4
LT
1736 if (IS_ERR(odentry))
1737 goto out_nfserr;
1738
6264d69d 1739 host_err = -ENOENT;
1da177e4
LT
1740 if (!odentry->d_inode)
1741 goto out_dput_old;
6264d69d 1742 host_err = -EINVAL;
1da177e4
LT
1743 if (odentry == trap)
1744 goto out_dput_old;
1745
1746 ndentry = lookup_one_len(tname, tdentry, tlen);
6264d69d 1747 host_err = PTR_ERR(ndentry);
1da177e4
LT
1748 if (IS_ERR(ndentry))
1749 goto out_dput_old;
6264d69d 1750 host_err = -ENOTEMPTY;
1da177e4
LT
1751 if (ndentry == trap)
1752 goto out_dput_new;
1753
9079b1eb 1754 if (svc_msnfs(ffhp) &&
b7ab39f6 1755 ((odentry->d_count > 1) || (ndentry->d_count > 1))) {
6264d69d 1756 host_err = -EPERM;
9079b1eb
DH
1757 goto out_dput_new;
1758 }
1759
1760 host_err = -EXDEV;
1761 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1762 goto out_dput_new;
1763 host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
1764 if (host_err)
1765 goto out_dput_new;
1766
6264d69d 1767 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
f501912a
BM
1768 if (!host_err) {
1769 host_err = commit_metadata(tfhp);
6264d69d 1770 if (!host_err)
f501912a 1771 host_err = commit_metadata(ffhp);
1da177e4
LT
1772 }
1773
9079b1eb
DH
1774 mnt_drop_write(ffhp->fh_export->ex_path.mnt);
1775
1da177e4
LT
1776 out_dput_new:
1777 dput(ndentry);
1778 out_dput_old:
1779 dput(odentry);
1780 out_nfserr:
6264d69d 1781 err = nfserrno(host_err);
1da177e4
LT
1782
1783 /* we cannot reply on fh_unlock on the two filehandles,
1784 * as that would do the wrong thing if the two directories
1785 * were the same, so again we do it by hand
1786 */
1787 fill_post_wcc(ffhp);
1788 fill_post_wcc(tfhp);
1789 unlock_rename(tdentry, fdentry);
1790 ffhp->fh_locked = tfhp->fh_locked = 0;
1791
1792out:
1793 return err;
1794}
1795
1796/*
1797 * Unlink a file or directory
1798 * N.B. After this call fhp needs an fh_put
1799 */
6264d69d 1800__be32
1da177e4
LT
1801nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1802 char *fname, int flen)
1803{
1804 struct dentry *dentry, *rdentry;
1805 struct inode *dirp;
6264d69d
AV
1806 __be32 err;
1807 int host_err;
1da177e4
LT
1808
1809 err = nfserr_acces;
1810 if (!flen || isdotent(fname, flen))
1811 goto out;
8837abca 1812 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1da177e4
LT
1813 if (err)
1814 goto out;
1815
12fd3520 1816 fh_lock_nested(fhp, I_MUTEX_PARENT);
1da177e4
LT
1817 dentry = fhp->fh_dentry;
1818 dirp = dentry->d_inode;
1819
1820 rdentry = lookup_one_len(fname, dentry, flen);
6264d69d 1821 host_err = PTR_ERR(rdentry);
1da177e4
LT
1822 if (IS_ERR(rdentry))
1823 goto out_nfserr;
1824
1825 if (!rdentry->d_inode) {
1826 dput(rdentry);
1827 err = nfserr_noent;
1828 goto out;
1829 }
1830
1831 if (!type)
1832 type = rdentry->d_inode->i_mode & S_IFMT;
1833
0622753b
DH
1834 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1835 if (host_err)
1836 goto out_nfserr;
1837
1da177e4
LT
1838 if (type != S_IFDIR) { /* It's UNLINK */
1839#ifdef MSNFS
1840 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
b7ab39f6 1841 (rdentry->d_count > 1)) {
6264d69d 1842 host_err = -EPERM;
1da177e4
LT
1843 } else
1844#endif
6264d69d 1845 host_err = vfs_unlink(dirp, rdentry);
1da177e4 1846 } else { /* It's RMDIR */
6264d69d 1847 host_err = vfs_rmdir(dirp, rdentry);
1da177e4
LT
1848 }
1849
1850 dput(rdentry);
1851
f501912a
BM
1852 if (!host_err)
1853 host_err = commit_metadata(fhp);
1da177e4 1854
0622753b 1855 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1da177e4 1856out_nfserr:
6264d69d 1857 err = nfserrno(host_err);
f193fbab
YT
1858out:
1859 return err;
1da177e4
LT
1860}
1861
14f7dd63
DW
1862/*
1863 * We do this buffering because we must not call back into the file
1864 * system's ->lookup() method from the filldir callback. That may well
1865 * deadlock a number of file systems.
1866 *
1867 * This is based heavily on the implementation of same in XFS.
1868 */
1869struct buffered_dirent {
1870 u64 ino;
1871 loff_t offset;
1872 int namlen;
1873 unsigned int d_type;
1874 char name[];
1875};
1876
1877struct readdir_data {
1878 char *dirent;
1879 size_t used;
53c9c5c0 1880 int full;
14f7dd63
DW
1881};
1882
1883static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1884 loff_t offset, u64 ino, unsigned int d_type)
1885{
1886 struct readdir_data *buf = __buf;
1887 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1888 unsigned int reclen;
1889
1890 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
53c9c5c0
AV
1891 if (buf->used + reclen > PAGE_SIZE) {
1892 buf->full = 1;
14f7dd63 1893 return -EINVAL;
53c9c5c0 1894 }
14f7dd63
DW
1895
1896 de->namlen = namlen;
1897 de->offset = offset;
1898 de->ino = ino;
1899 de->d_type = d_type;
1900 memcpy(de->name, name, namlen);
1901 buf->used += reclen;
1902
1903 return 0;
1904}
1905
2f9092e1
DW
1906static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1907 struct readdir_cd *cdp, loff_t *offsetp)
2628b766 1908{
14f7dd63
DW
1909 struct readdir_data buf;
1910 struct buffered_dirent *de;
2628b766 1911 int host_err;
14f7dd63
DW
1912 int size;
1913 loff_t offset;
2628b766 1914
14f7dd63
DW
1915 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1916 if (!buf.dirent)
2f9092e1 1917 return nfserrno(-ENOMEM);
14f7dd63
DW
1918
1919 offset = *offsetp;
2628b766 1920
14f7dd63 1921 while (1) {
2f9092e1 1922 struct inode *dir_inode = file->f_path.dentry->d_inode;
14f7dd63
DW
1923 unsigned int reclen;
1924
b726e923 1925 cdp->err = nfserr_eof; /* will be cleared on successful read */
14f7dd63 1926 buf.used = 0;
53c9c5c0 1927 buf.full = 0;
14f7dd63
DW
1928
1929 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
53c9c5c0
AV
1930 if (buf.full)
1931 host_err = 0;
1932
1933 if (host_err < 0)
14f7dd63
DW
1934 break;
1935
1936 size = buf.used;
1937
1938 if (!size)
1939 break;
1940
2f9092e1
DW
1941 /*
1942 * Various filldir functions may end up calling back into
1943 * lookup_one_len() and the file system's ->lookup() method.
1944 * These expect i_mutex to be held, as it would within readdir.
1945 */
1946 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1947 if (host_err)
1948 break;
1949
14f7dd63
DW
1950 de = (struct buffered_dirent *)buf.dirent;
1951 while (size > 0) {
1952 offset = de->offset;
1953
1954 if (func(cdp, de->name, de->namlen, de->offset,
1955 de->ino, de->d_type))
2f9092e1 1956 break;
14f7dd63
DW
1957
1958 if (cdp->err != nfs_ok)
2f9092e1 1959 break;
14f7dd63
DW
1960
1961 reclen = ALIGN(sizeof(*de) + de->namlen,
1962 sizeof(u64));
1963 size -= reclen;
1964 de = (struct buffered_dirent *)((char *)de + reclen);
1965 }
2f9092e1
DW
1966 mutex_unlock(&dir_inode->i_mutex);
1967 if (size > 0) /* We bailed out early */
1968 break;
1969
c002a6c7 1970 offset = vfs_llseek(file, 0, SEEK_CUR);
14f7dd63
DW
1971 }
1972
14f7dd63 1973 free_page((unsigned long)(buf.dirent));
2628b766
DW
1974
1975 if (host_err)
1976 return nfserrno(host_err);
14f7dd63
DW
1977
1978 *offsetp = offset;
1979 return cdp->err;
2628b766
DW
1980}
1981
1da177e4
LT
1982/*
1983 * Read entries from a directory.
1984 * The NFSv3/4 verifier we ignore for now.
1985 */
6264d69d 1986__be32
1da177e4 1987nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
a0ad13ef 1988 struct readdir_cd *cdp, filldir_t func)
1da177e4 1989{
6264d69d 1990 __be32 err;
1da177e4
LT
1991 struct file *file;
1992 loff_t offset = *offsetp;
1993
8837abca 1994 err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
1da177e4
LT
1995 if (err)
1996 goto out;
1997
1998 offset = vfs_llseek(file, offset, 0);
1999 if (offset < 0) {
2000 err = nfserrno((int)offset);
2001 goto out_close;
2002 }
2003
14f7dd63 2004 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
1da177e4
LT
2005
2006 if (err == nfserr_eof || err == nfserr_toosmall)
2007 err = nfs_ok; /* can still be found in ->err */
2008out_close:
2009 nfsd_close(file);
2010out:
2011 return err;
2012}
2013
2014/*
2015 * Get file system stats
2016 * N.B. After this call fhp needs an fh_put
2017 */
6264d69d 2018__be32
04716e66 2019nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
1da177e4 2020{
ebabe9a9
CH
2021 __be32 err;
2022
2023 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
f6360efb
TI
2024 if (!err) {
2025 struct path path = {
2026 .mnt = fhp->fh_export->ex_path.mnt,
2027 .dentry = fhp->fh_dentry,
2028 };
2029 if (vfs_statfs(&path, stat))
2030 err = nfserr_io;
2031 }
1da177e4
LT
2032 return err;
2033}
2034
c7d51402 2035static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
e22841c6 2036{
c7d51402 2037 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
e22841c6
BF
2038}
2039
1da177e4
LT
2040/*
2041 * Check for a user's access permissions to this inode.
2042 */
6264d69d 2043__be32
0ec757df
BF
2044nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2045 struct dentry *dentry, int acc)
1da177e4
LT
2046{
2047 struct inode *inode = dentry->d_inode;
2048 int err;
2049
8837abca 2050 if (acc == NFSD_MAY_NOP)
1da177e4
LT
2051 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,
8837abca
MS
2055 (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" : "",
1da177e4
LT
2062 inode->i_mode,
2063 IS_IMMUTABLE(inode)? " immut" : "",
2064 IS_APPEND(inode)? " append" : "",
2c463e95 2065 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
1da177e4 2066 dprintk(" owner %d/%d user %d/%d\n",
5cc0a840 2067 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
1da177e4
LT
2068#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 */
8837abca
MS
2074 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2075 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2c463e95
DH
2076 if (exp_rdonly(rqstp, exp) ||
2077 __mnt_is_readonly(exp->ex_path.mnt))
1da177e4 2078 return nfserr_rofs;
8837abca 2079 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
1da177e4
LT
2080 return nfserr_perm;
2081 }
8837abca 2082 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
1da177e4
LT
2083 return nfserr_perm;
2084
8837abca 2085 if (acc & NFSD_MAY_LOCK) {
1da177e4
LT
2086 /* 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
8837abca 2093 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
1da177e4
LT
2094 }
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 */
8837abca 2109 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
5cc0a840 2110 inode->i_uid == current_fsuid())
1da177e4
LT
2111 return 0;
2112
8837abca 2113 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
f419a2e3 2114 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
1da177e4
LT
2115
2116 /* Allow read access to binaries even when mode 111 */
2117 if (err == -EACCES && S_ISREG(inode->i_mode) &&
8837abca 2118 acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
f419a2e3 2119 err = inode_permission(inode, MAY_EXEC);
1da177e4
LT
2120
2121 return err? nfserrno(err) : 0;
2122}
2123
2124void
2125nfsd_racache_shutdown(void)
2126{
54a66e54
JL
2127 struct raparms *raparm, *last_raparm;
2128 unsigned int i;
2129
1da177e4 2130 dprintk("nfsd: freeing readahead buffers.\n");
54a66e54
JL
2131
2132 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2133 raparm = raparm_hash[i].pb_head;
2134 while(raparm) {
2135 last_raparm = raparm;
2136 raparm = raparm->p_next;
2137 kfree(last_raparm);
2138 }
2139 raparm_hash[i].pb_head = NULL;
2140 }
1da177e4
LT
2141}
2142/*
2143 * Initialize readahead param cache
2144 */
2145int
2146nfsd_racache_init(int cache_size)
2147{
2148 int i;
fce1456a
GB
2149 int j = 0;
2150 int nperbucket;
54a66e54 2151 struct raparms **raparm = NULL;
1da177e4 2152
fce1456a 2153
54a66e54 2154 if (raparm_hash[0].pb_head)
1da177e4 2155 return 0;
54a66e54
JL
2156 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2157 if (nperbucket < 2)
2158 nperbucket = 2;
2159 cache_size = nperbucket * RAPARM_HASH_SIZE;
4b3bb06b
YB
2160
2161 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
54a66e54
JL
2162
2163 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
4b3bb06b 2164 spin_lock_init(&raparm_hash[i].pb_lock);
54a66e54
JL
2165
2166 raparm = &raparm_hash[i].pb_head;
2167 for (j = 0; j < nperbucket; j++) {
2168 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2169 if (!*raparm)
2170 goto out_nomem;
2171 raparm = &(*raparm)->p_next;
2172 }
2173 *raparm = NULL;
4b3bb06b
YB
2174 }
2175
1da177e4
LT
2176 nfsdstats.ra_size = cache_size;
2177 return 0;
54a66e54
JL
2178
2179out_nomem:
2180 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2181 nfsd_racache_shutdown();
2182 return -ENOMEM;
1da177e4 2183}
a257cdd0
AG
2184
2185#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2186struct posix_acl *
2187nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2188{
2189 struct inode *inode = fhp->fh_dentry->d_inode;
2190 char *name;
2191 void *value = NULL;
2192 ssize_t size;
2193 struct posix_acl *acl;
2194
5be196e5
CH
2195 if (!IS_POSIXACL(inode))
2196 return ERR_PTR(-EOPNOTSUPP);
2197
2198 switch (type) {
2199 case ACL_TYPE_ACCESS:
2200 name = POSIX_ACL_XATTR_ACCESS;
2201 break;
2202 case ACL_TYPE_DEFAULT:
2203 name = POSIX_ACL_XATTR_DEFAULT;
2204 break;
2205 default:
a257cdd0 2206 return ERR_PTR(-EOPNOTSUPP);
a257cdd0
AG
2207 }
2208
5be196e5
CH
2209 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2210 if (size < 0)
2211 return ERR_PTR(size);
a257cdd0 2212
a257cdd0 2213 acl = posix_acl_from_xattr(value, size);
a257cdd0
AG
2214 kfree(value);
2215 return acl;
2216}
2217
2218int
2219nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2220{
2221 struct inode *inode = fhp->fh_dentry->d_inode;
2222 char *name;
2223 void *value = NULL;
2224 size_t size;
2225 int error;
2226
acfa4380 2227 if (!IS_POSIXACL(inode) ||
a257cdd0
AG
2228 !inode->i_op->setxattr || !inode->i_op->removexattr)
2229 return -EOPNOTSUPP;
2230 switch(type) {
2231 case ACL_TYPE_ACCESS:
334a13ec 2232 name = POSIX_ACL_XATTR_ACCESS;
a257cdd0
AG
2233 break;
2234 case ACL_TYPE_DEFAULT:
334a13ec 2235 name = POSIX_ACL_XATTR_DEFAULT;
a257cdd0
AG
2236 break;
2237 default:
2238 return -EOPNOTSUPP;
2239 }
2240
2241 if (acl && acl->a_count) {
334a13ec 2242 size = posix_acl_xattr_size(acl->a_count);
a257cdd0
AG
2243 value = kmalloc(size, GFP_KERNEL);
2244 if (!value)
2245 return -ENOMEM;
9ccfc29c
FM
2246 error = posix_acl_to_xattr(acl, value, size);
2247 if (error < 0)
a257cdd0 2248 goto getout;
9ccfc29c 2249 size = error;
a257cdd0
AG
2250 } else
2251 size = 0;
2252
18f335af
DH
2253 error = mnt_want_write(fhp->fh_export->ex_path.mnt);
2254 if (error)
2255 goto getout;
a257cdd0 2256 if (size)
5be196e5 2257 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
a257cdd0
AG
2258 else {
2259 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2260 error = 0;
2261 else {
5be196e5 2262 error = vfs_removexattr(fhp->fh_dentry, name);
a257cdd0
AG
2263 if (error == -ENODATA)
2264 error = 0;
2265 }
2266 }
18f335af 2267 mnt_drop_write(fhp->fh_export->ex_path.mnt);
a257cdd0
AG
2268
2269getout:
2270 kfree(value);
2271 return error;
2272}
2273#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
This page took 0.634297 seconds and 5 git commands to generate.