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