Linux 3.12.28
[linux/fpc-iii.git] / fs / nfsd / vfs.c
blobe9a80e4553a3b824829ca1597810f762b115ddc6
1 /*
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.
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.
12 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
13 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
16 #include <linux/fs.h>
17 #include <linux/file.h>
18 #include <linux/splice.h>
19 #include <linux/fcntl.h>
20 #include <linux/namei.h>
21 #include <linux/delay.h>
22 #include <linux/fsnotify.h>
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/xattr.h>
25 #include <linux/jhash.h>
26 #include <linux/ima.h>
27 #include <linux/slab.h>
28 #include <asm/uaccess.h>
29 #include <linux/exportfs.h>
30 #include <linux/writeback.h>
31 #include <linux/security.h>
33 #ifdef CONFIG_NFSD_V3
34 #include "xdr3.h"
35 #endif /* CONFIG_NFSD_V3 */
37 #ifdef CONFIG_NFSD_V4
38 #include "acl.h"
39 #include "idmap.h"
40 #endif /* CONFIG_NFSD_V4 */
42 #include "nfsd.h"
43 #include "vfs.h"
45 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
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.
55 struct 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;
62 unsigned int p_hindex;
65 struct raparm_hbucket {
66 struct raparms *pb_head;
67 spinlock_t pb_lock;
68 } ____cacheline_aligned_in_smp;
70 #define RAPARM_HASH_BITS 4
71 #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
72 #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
73 static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
75 /*
76 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
77 * a mount point.
78 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
79 * or nfs_ok having possibly changed *dpp and *expp
81 int
82 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
83 struct svc_export **expp)
85 struct svc_export *exp = *expp, *exp2 = NULL;
86 struct dentry *dentry = *dpp;
87 struct path path = {.mnt = mntget(exp->ex_path.mnt),
88 .dentry = dget(dentry)};
89 int err = 0;
91 err = follow_down(&path);
92 if (err < 0)
93 goto out;
95 exp2 = rqst_exp_get_by_name(rqstp, &path);
96 if (IS_ERR(exp2)) {
97 err = PTR_ERR(exp2);
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.
105 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
106 err = 0;
107 path_put(&path);
108 goto out;
110 if (nfsd_v4client(rqstp) ||
111 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
112 /* successfully crossed mount point */
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
119 *dpp = path.dentry;
120 path.dentry = dentry;
121 *expp = exp2;
122 exp2 = exp;
124 path_put(&path);
125 exp_put(exp2);
126 out:
127 return err;
130 static void follow_to_parent(struct path *path)
132 struct dentry *dp;
134 while (path->dentry == path->mnt->mnt_root && follow_up(path))
136 dp = dget_parent(path->dentry);
137 dput(path->dentry);
138 path->dentry = dp;
141 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
143 struct svc_export *exp2;
144 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
145 .dentry = dget(dparent)};
147 follow_to_parent(&path);
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;
160 path_put(&path);
161 return 0;
165 * For nfsd purposes, we treat V4ROOT exports as though there was an
166 * export at *every* directory.
168 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
170 if (d_mountpoint(dentry))
171 return 1;
172 if (nfsd4_is_junction(dentry))
173 return 1;
174 if (!(exp->ex_flags & NFSEXP_V4ROOT))
175 return 0;
176 return dentry->d_inode != NULL;
179 __be32
180 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
181 const char *name, unsigned int len,
182 struct svc_export **exp_ret, struct dentry **dentry_ret)
184 struct svc_export *exp;
185 struct dentry *dparent;
186 struct dentry *dentry;
187 int host_err;
189 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
191 dparent = fhp->fh_dentry;
192 exp = fhp->fh_export;
193 exp_get(exp);
195 /* Lookup the name, but don't follow links */
196 if (isdotent(name, len)) {
197 if (len==1)
198 dentry = dget(dparent);
199 else if (dparent != exp->ex_path.dentry)
200 dentry = dget_parent(dparent);
201 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
202 dentry = dget(dparent); /* .. == . just like at / */
203 else {
204 /* checking mountpoint crossing is very different when stepping up */
205 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
206 if (host_err)
207 goto out_nfserr;
209 } else {
210 fh_lock(fhp);
211 dentry = lookup_one_len(name, dparent, len);
212 host_err = PTR_ERR(dentry);
213 if (IS_ERR(dentry))
214 goto out_nfserr;
216 * check if we have crossed a mount point ...
218 if (nfsd_mountpoint(dentry, exp)) {
219 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
220 dput(dentry);
221 goto out_nfserr;
225 *dentry_ret = dentry;
226 *exp_ret = exp;
227 return 0;
229 out_nfserr:
230 exp_put(exp);
231 return nfserrno(host_err);
235 * Look up one component of a pathname.
236 * N.B. After this call _both_ fhp and resfh need an fh_put
238 * If the lookup would cross a mountpoint, and the mounted filesystem
239 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
240 * accepted as it stands and the mounted directory is
241 * returned. Otherwise the covered directory is returned.
242 * NOTE: this mountpoint crossing is not supported properly by all
243 * clients and is explicitly disallowed for NFSv3
244 * NeilBrown <neilb@cse.unsw.edu.au>
246 __be32
247 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
248 unsigned int len, struct svc_fh *resfh)
250 struct svc_export *exp;
251 struct dentry *dentry;
252 __be32 err;
254 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
255 if (err)
256 return err;
257 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
258 if (err)
259 return err;
260 err = check_nfsd_access(exp, rqstp);
261 if (err)
262 goto out;
264 * Note: we compose the file handle now, but as the
265 * dentry may be negative, it may need to be updated.
267 err = fh_compose(resfh, exp, dentry, fhp);
268 if (!err && !dentry->d_inode)
269 err = nfserr_noent;
270 out:
271 dput(dentry);
272 exp_put(exp);
273 return err;
276 static int nfsd_break_lease(struct inode *inode)
278 if (!S_ISREG(inode->i_mode))
279 return 0;
280 return break_lease(inode, O_WRONLY | O_NONBLOCK);
284 * Commit metadata changes to stable storage.
286 static int
287 commit_metadata(struct svc_fh *fhp)
289 struct inode *inode = fhp->fh_dentry->d_inode;
290 const struct export_operations *export_ops = inode->i_sb->s_export_op;
292 if (!EX_ISSYNC(fhp->fh_export))
293 return 0;
295 if (export_ops->commit_metadata)
296 return export_ops->commit_metadata(inode);
297 return sync_inode_metadata(inode, 1);
301 * Go over the attributes and take care of the small differences between
302 * NFS semantics and what Linux expects.
304 static void
305 nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
308 * NFSv2 does not differentiate between "set-[ac]time-to-now"
309 * which only requires access, and "set-[ac]time-to-X" which
310 * requires ownership.
311 * So if it looks like it might be "set both to the same time which
312 * is close to now", and if inode_change_ok fails, then we
313 * convert to "set to now" instead of "set to explicit time"
315 * We only call inode_change_ok as the last test as technically
316 * it is not an interface that we should be using.
318 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
319 #define MAX_TOUCH_TIME_ERROR (30*60)
320 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
321 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
323 * Looks probable.
325 * Now just make sure time is in the right ballpark.
326 * Solaris, at least, doesn't seem to care what the time
327 * request is. We require it be within 30 minutes of now.
329 time_t delta = iap->ia_atime.tv_sec - get_seconds();
330 if (delta < 0)
331 delta = -delta;
332 if (delta < MAX_TOUCH_TIME_ERROR &&
333 inode_change_ok(inode, iap) != 0) {
335 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
336 * This will cause notify_change to set these times
337 * to "now"
339 iap->ia_valid &= ~BOTH_TIME_SET;
343 /* sanitize the mode change */
344 if (iap->ia_valid & ATTR_MODE) {
345 iap->ia_mode &= S_IALLUGO;
346 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
349 /* Revoke setuid/setgid on chown */
350 if (!S_ISDIR(inode->i_mode) &&
351 (((iap->ia_valid & ATTR_UID) && !uid_eq(iap->ia_uid, inode->i_uid)) ||
352 ((iap->ia_valid & ATTR_GID) && !gid_eq(iap->ia_gid, inode->i_gid)))) {
353 iap->ia_valid |= ATTR_KILL_PRIV;
354 if (iap->ia_valid & ATTR_MODE) {
355 /* we're setting mode too, just clear the s*id bits */
356 iap->ia_mode &= ~S_ISUID;
357 if (iap->ia_mode & S_IXGRP)
358 iap->ia_mode &= ~S_ISGID;
359 } else {
360 /* set ATTR_KILL_* bits and let VFS handle it */
361 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
366 static __be32
367 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
368 struct iattr *iap)
370 struct inode *inode = fhp->fh_dentry->d_inode;
371 int host_err;
373 if (iap->ia_size < inode->i_size) {
374 __be32 err;
376 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
377 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
378 if (err)
379 return err;
382 host_err = get_write_access(inode);
383 if (host_err)
384 goto out_nfserrno;
386 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
387 if (host_err)
388 goto out_put_write_access;
389 return 0;
391 out_put_write_access:
392 put_write_access(inode);
393 out_nfserrno:
394 return nfserrno(host_err);
398 * Set various file attributes. After this call fhp needs an fh_put.
400 __be32
401 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
402 int check_guard, time_t guardtime)
404 struct dentry *dentry;
405 struct inode *inode;
406 int accmode = NFSD_MAY_SATTR;
407 umode_t ftype = 0;
408 __be32 err;
409 int host_err;
410 bool get_write_count;
411 int size_change = 0;
413 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
414 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
415 if (iap->ia_valid & ATTR_SIZE)
416 ftype = S_IFREG;
418 /* Callers that do fh_verify should do the fh_want_write: */
419 get_write_count = !fhp->fh_dentry;
421 /* Get inode */
422 err = fh_verify(rqstp, fhp, ftype, accmode);
423 if (err)
424 goto out;
425 if (get_write_count) {
426 host_err = fh_want_write(fhp);
427 if (host_err)
428 return nfserrno(host_err);
431 dentry = fhp->fh_dentry;
432 inode = dentry->d_inode;
434 /* Ignore any mode updates on symlinks */
435 if (S_ISLNK(inode->i_mode))
436 iap->ia_valid &= ~ATTR_MODE;
438 if (!iap->ia_valid)
439 goto out;
441 nfsd_sanitize_attrs(inode, iap);
444 * The size case is special, it changes the file in addition to the
445 * attributes.
447 if (iap->ia_valid & ATTR_SIZE) {
448 err = nfsd_get_write_access(rqstp, fhp, iap);
449 if (err)
450 goto out;
451 size_change = 1;
454 iap->ia_valid |= ATTR_CTIME;
456 if (check_guard && guardtime != inode->i_ctime.tv_sec) {
457 err = nfserr_notsync;
458 goto out_put_write_access;
461 host_err = nfsd_break_lease(inode);
462 if (host_err)
463 goto out_put_write_access_nfserror;
465 fh_lock(fhp);
466 host_err = notify_change(dentry, iap);
467 fh_unlock(fhp);
469 out_put_write_access_nfserror:
470 err = nfserrno(host_err);
471 out_put_write_access:
472 if (size_change)
473 put_write_access(inode);
474 if (!err)
475 commit_metadata(fhp);
476 out:
477 return err;
480 #if defined(CONFIG_NFSD_V2_ACL) || \
481 defined(CONFIG_NFSD_V3_ACL) || \
482 defined(CONFIG_NFSD_V4)
483 static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
485 ssize_t buflen;
486 ssize_t ret;
488 buflen = vfs_getxattr(dentry, key, NULL, 0);
489 if (buflen <= 0)
490 return buflen;
492 *buf = kmalloc(buflen, GFP_KERNEL);
493 if (!*buf)
494 return -ENOMEM;
496 ret = vfs_getxattr(dentry, key, *buf, buflen);
497 if (ret < 0)
498 kfree(*buf);
499 return ret;
501 #endif
503 #if defined(CONFIG_NFSD_V4)
504 static int
505 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
507 int len;
508 size_t buflen;
509 char *buf = NULL;
510 int error = 0;
512 buflen = posix_acl_xattr_size(pacl->a_count);
513 buf = kmalloc(buflen, GFP_KERNEL);
514 error = -ENOMEM;
515 if (buf == NULL)
516 goto out;
518 len = posix_acl_to_xattr(&init_user_ns, pacl, buf, buflen);
519 if (len < 0) {
520 error = len;
521 goto out;
524 error = vfs_setxattr(dentry, key, buf, len, 0);
525 out:
526 kfree(buf);
527 return error;
530 __be32
531 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
532 struct nfs4_acl *acl)
534 __be32 error;
535 int host_error;
536 struct dentry *dentry;
537 struct inode *inode;
538 struct posix_acl *pacl = NULL, *dpacl = NULL;
539 unsigned int flags = 0;
541 /* Get inode */
542 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
543 if (error)
544 return error;
546 dentry = fhp->fh_dentry;
547 inode = dentry->d_inode;
548 if (S_ISDIR(inode->i_mode))
549 flags = NFS4_ACL_DIR;
551 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
552 if (host_error == -EINVAL) {
553 return nfserr_attrnotsupp;
554 } else if (host_error < 0)
555 goto out_nfserr;
557 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
558 if (host_error < 0)
559 goto out_release;
561 if (S_ISDIR(inode->i_mode))
562 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
564 out_release:
565 posix_acl_release(pacl);
566 posix_acl_release(dpacl);
567 out_nfserr:
568 if (host_error == -EOPNOTSUPP)
569 return nfserr_attrnotsupp;
570 else
571 return nfserrno(host_error);
574 static struct posix_acl *
575 _get_posix_acl(struct dentry *dentry, char *key)
577 void *buf = NULL;
578 struct posix_acl *pacl = NULL;
579 int buflen;
581 buflen = nfsd_getxattr(dentry, key, &buf);
582 if (!buflen)
583 buflen = -ENODATA;
584 if (buflen <= 0)
585 return ERR_PTR(buflen);
587 pacl = posix_acl_from_xattr(&init_user_ns, buf, buflen);
588 kfree(buf);
589 return pacl;
593 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
595 struct inode *inode = dentry->d_inode;
596 int error = 0;
597 struct posix_acl *pacl = NULL, *dpacl = NULL;
598 unsigned int flags = 0;
600 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
601 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
602 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
603 if (IS_ERR(pacl)) {
604 error = PTR_ERR(pacl);
605 pacl = NULL;
606 goto out;
609 if (S_ISDIR(inode->i_mode)) {
610 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
611 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
612 dpacl = NULL;
613 else if (IS_ERR(dpacl)) {
614 error = PTR_ERR(dpacl);
615 dpacl = NULL;
616 goto out;
618 flags = NFS4_ACL_DIR;
621 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
622 if (IS_ERR(*acl)) {
623 error = PTR_ERR(*acl);
624 *acl = NULL;
626 out:
627 posix_acl_release(pacl);
628 posix_acl_release(dpacl);
629 return error;
633 * NFS junction information is stored in an extended attribute.
635 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
638 * nfsd4_is_junction - Test if an object could be an NFS junction
640 * @dentry: object to test
642 * Returns 1 if "dentry" appears to contain NFS junction information.
643 * Otherwise 0 is returned.
645 int nfsd4_is_junction(struct dentry *dentry)
647 struct inode *inode = dentry->d_inode;
649 if (inode == NULL)
650 return 0;
651 if (inode->i_mode & S_IXUGO)
652 return 0;
653 if (!(inode->i_mode & S_ISVTX))
654 return 0;
655 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
656 return 0;
657 return 1;
659 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
660 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
661 struct xdr_netobj *label)
663 __be32 error;
664 int host_error;
665 struct dentry *dentry;
667 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
668 if (error)
669 return error;
671 dentry = fhp->fh_dentry;
673 mutex_lock(&dentry->d_inode->i_mutex);
674 host_error = security_inode_setsecctx(dentry, label->data, label->len);
675 mutex_unlock(&dentry->d_inode->i_mutex);
676 return nfserrno(host_error);
678 #else
679 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
680 struct xdr_netobj *label)
682 return nfserr_notsupp;
684 #endif
686 #endif /* defined(CONFIG_NFSD_V4) */
688 #ifdef CONFIG_NFSD_V3
690 * Check server access rights to a file system object
692 struct accessmap {
693 u32 access;
694 int how;
696 static struct accessmap nfs3_regaccess[] = {
697 { NFS3_ACCESS_READ, NFSD_MAY_READ },
698 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
699 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
700 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
702 { 0, 0 }
705 static struct accessmap nfs3_diraccess[] = {
706 { NFS3_ACCESS_READ, NFSD_MAY_READ },
707 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
708 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
709 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
710 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
712 { 0, 0 }
715 static struct accessmap nfs3_anyaccess[] = {
716 /* Some clients - Solaris 2.6 at least, make an access call
717 * to the server to check for access for things like /dev/null
718 * (which really, the server doesn't care about). So
719 * We provide simple access checking for them, looking
720 * mainly at mode bits, and we make sure to ignore read-only
721 * filesystem checks
723 { NFS3_ACCESS_READ, NFSD_MAY_READ },
724 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
725 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
726 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
728 { 0, 0 }
731 __be32
732 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
734 struct accessmap *map;
735 struct svc_export *export;
736 struct dentry *dentry;
737 u32 query, result = 0, sresult = 0;
738 __be32 error;
740 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
741 if (error)
742 goto out;
744 export = fhp->fh_export;
745 dentry = fhp->fh_dentry;
747 if (S_ISREG(dentry->d_inode->i_mode))
748 map = nfs3_regaccess;
749 else if (S_ISDIR(dentry->d_inode->i_mode))
750 map = nfs3_diraccess;
751 else
752 map = nfs3_anyaccess;
755 query = *access;
756 for (; map->access; map++) {
757 if (map->access & query) {
758 __be32 err2;
760 sresult |= map->access;
762 err2 = nfsd_permission(rqstp, export, dentry, map->how);
763 switch (err2) {
764 case nfs_ok:
765 result |= map->access;
766 break;
768 /* the following error codes just mean the access was not allowed,
769 * rather than an error occurred */
770 case nfserr_rofs:
771 case nfserr_acces:
772 case nfserr_perm:
773 /* simply don't "or" in the access bit. */
774 break;
775 default:
776 error = err2;
777 goto out;
781 *access = result;
782 if (supported)
783 *supported = sresult;
785 out:
786 return error;
788 #endif /* CONFIG_NFSD_V3 */
790 static int nfsd_open_break_lease(struct inode *inode, int access)
792 unsigned int mode;
794 if (access & NFSD_MAY_NOT_BREAK_LEASE)
795 return 0;
796 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
797 return break_lease(inode, mode | O_NONBLOCK);
801 * Open an existing file or directory.
802 * The may_flags argument indicates the type of open (read/write/lock)
803 * and additional flags.
804 * N.B. After this call fhp needs an fh_put
806 __be32
807 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
808 int may_flags, struct file **filp)
810 struct path path;
811 struct inode *inode;
812 int flags = O_RDONLY|O_LARGEFILE;
813 __be32 err;
814 int host_err = 0;
816 validate_process_creds();
819 * If we get here, then the client has already done an "open",
820 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
821 * in case a chmod has now revoked permission.
823 * Arguably we should also allow the owner override for
824 * directories, but we never have and it doesn't seem to have
825 * caused anyone a problem. If we were to change this, note
826 * also that our filldir callbacks would need a variant of
827 * lookup_one_len that doesn't check permissions.
829 if (type == S_IFREG)
830 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
831 err = fh_verify(rqstp, fhp, type, may_flags);
832 if (err)
833 goto out;
835 path.mnt = fhp->fh_export->ex_path.mnt;
836 path.dentry = fhp->fh_dentry;
837 inode = path.dentry->d_inode;
839 /* Disallow write access to files with the append-only bit set
840 * or any access when mandatory locking enabled
842 err = nfserr_perm;
843 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
844 goto out;
846 * We must ignore files (but only files) which might have mandatory
847 * locks on them because there is no way to know if the accesser has
848 * the lock.
850 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
851 goto out;
853 if (!inode->i_fop)
854 goto out;
856 host_err = nfsd_open_break_lease(inode, may_flags);
857 if (host_err) /* NOMEM or WOULDBLOCK */
858 goto out_nfserr;
860 if (may_flags & NFSD_MAY_WRITE) {
861 if (may_flags & NFSD_MAY_READ)
862 flags = O_RDWR|O_LARGEFILE;
863 else
864 flags = O_WRONLY|O_LARGEFILE;
866 *filp = dentry_open(&path, flags, current_cred());
867 if (IS_ERR(*filp)) {
868 host_err = PTR_ERR(*filp);
869 *filp = NULL;
870 } else {
871 host_err = ima_file_check(*filp, may_flags);
873 if (may_flags & NFSD_MAY_64BIT_COOKIE)
874 (*filp)->f_mode |= FMODE_64BITHASH;
875 else
876 (*filp)->f_mode |= FMODE_32BITHASH;
879 out_nfserr:
880 err = nfserrno(host_err);
881 out:
882 validate_process_creds();
883 return err;
887 * Close a file.
889 void
890 nfsd_close(struct file *filp)
892 fput(filp);
896 * Obtain the readahead parameters for the file
897 * specified by (dev, ino).
900 static inline struct raparms *
901 nfsd_get_raparms(dev_t dev, ino_t ino)
903 struct raparms *ra, **rap, **frap = NULL;
904 int depth = 0;
905 unsigned int hash;
906 struct raparm_hbucket *rab;
908 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
909 rab = &raparm_hash[hash];
911 spin_lock(&rab->pb_lock);
912 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
913 if (ra->p_ino == ino && ra->p_dev == dev)
914 goto found;
915 depth++;
916 if (ra->p_count == 0)
917 frap = rap;
919 depth = nfsdstats.ra_size;
920 if (!frap) {
921 spin_unlock(&rab->pb_lock);
922 return NULL;
924 rap = frap;
925 ra = *frap;
926 ra->p_dev = dev;
927 ra->p_ino = ino;
928 ra->p_set = 0;
929 ra->p_hindex = hash;
930 found:
931 if (rap != &rab->pb_head) {
932 *rap = ra->p_next;
933 ra->p_next = rab->pb_head;
934 rab->pb_head = ra;
936 ra->p_count++;
937 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
938 spin_unlock(&rab->pb_lock);
939 return ra;
943 * Grab and keep cached pages associated with a file in the svc_rqst
944 * so that they can be passed to the network sendmsg/sendpage routines
945 * directly. They will be released after the sending has completed.
947 static int
948 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
949 struct splice_desc *sd)
951 struct svc_rqst *rqstp = sd->u.data;
952 struct page **pp = rqstp->rq_next_page;
953 struct page *page = buf->page;
954 size_t size;
956 size = sd->len;
958 if (rqstp->rq_res.page_len == 0) {
959 get_page(page);
960 put_page(*rqstp->rq_next_page);
961 *(rqstp->rq_next_page++) = page;
962 rqstp->rq_res.page_base = buf->offset;
963 rqstp->rq_res.page_len = size;
964 } else if (page != pp[-1]) {
965 get_page(page);
966 if (*rqstp->rq_next_page)
967 put_page(*rqstp->rq_next_page);
968 *(rqstp->rq_next_page++) = page;
969 rqstp->rq_res.page_len += size;
970 } else
971 rqstp->rq_res.page_len += size;
973 return size;
976 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
977 struct splice_desc *sd)
979 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
982 static __be32
983 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
984 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
986 mm_segment_t oldfs;
987 __be32 err;
988 int host_err;
990 err = nfserr_perm;
992 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
993 struct splice_desc sd = {
994 .len = 0,
995 .total_len = *count,
996 .pos = offset,
997 .u.data = rqstp,
1000 rqstp->rq_next_page = rqstp->rq_respages + 1;
1001 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
1002 } else {
1003 oldfs = get_fs();
1004 set_fs(KERNEL_DS);
1005 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
1006 set_fs(oldfs);
1009 if (host_err >= 0) {
1010 nfsdstats.io_read += host_err;
1011 *count = host_err;
1012 err = 0;
1013 fsnotify_access(file);
1014 } else
1015 err = nfserrno(host_err);
1016 return err;
1019 static void kill_suid(struct dentry *dentry)
1021 struct iattr ia;
1022 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
1024 mutex_lock(&dentry->d_inode->i_mutex);
1025 notify_change(dentry, &ia);
1026 mutex_unlock(&dentry->d_inode->i_mutex);
1030 * Gathered writes: If another process is currently writing to the file,
1031 * there's a high chance this is another nfsd (triggered by a bulk write
1032 * from a client's biod). Rather than syncing the file with each write
1033 * request, we sleep for 10 msec.
1035 * I don't know if this roughly approximates C. Juszak's idea of
1036 * gathered writes, but it's a nice and simple solution (IMHO), and it
1037 * seems to work:-)
1039 * Note: we do this only in the NFSv2 case, since v3 and higher have a
1040 * better tool (separate unstable writes and commits) for solving this
1041 * problem.
1043 static int wait_for_concurrent_writes(struct file *file)
1045 struct inode *inode = file_inode(file);
1046 static ino_t last_ino;
1047 static dev_t last_dev;
1048 int err = 0;
1050 if (atomic_read(&inode->i_writecount) > 1
1051 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1052 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1053 msleep(10);
1054 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1057 if (inode->i_state & I_DIRTY) {
1058 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1059 err = vfs_fsync(file, 0);
1061 last_ino = inode->i_ino;
1062 last_dev = inode->i_sb->s_dev;
1063 return err;
1066 static __be32
1067 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1068 loff_t offset, struct kvec *vec, int vlen,
1069 unsigned long *cnt, int *stablep)
1071 struct svc_export *exp;
1072 struct dentry *dentry;
1073 struct inode *inode;
1074 mm_segment_t oldfs;
1075 __be32 err = 0;
1076 int host_err;
1077 int stable = *stablep;
1078 int use_wgather;
1079 loff_t pos = offset;
1081 dentry = file->f_path.dentry;
1082 inode = dentry->d_inode;
1083 exp = fhp->fh_export;
1085 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1087 if (!EX_ISSYNC(exp))
1088 stable = 0;
1090 /* Write the data. */
1091 oldfs = get_fs(); set_fs(KERNEL_DS);
1092 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
1093 set_fs(oldfs);
1094 if (host_err < 0)
1095 goto out_nfserr;
1096 *cnt = host_err;
1097 nfsdstats.io_write += host_err;
1098 fsnotify_modify(file);
1100 /* clear setuid/setgid flag after write */
1101 if (inode->i_mode & (S_ISUID | S_ISGID))
1102 kill_suid(dentry);
1104 if (stable) {
1105 if (use_wgather)
1106 host_err = wait_for_concurrent_writes(file);
1107 else
1108 host_err = vfs_fsync_range(file, offset, offset+*cnt, 0);
1111 out_nfserr:
1112 dprintk("nfsd: write complete host_err=%d\n", host_err);
1113 if (host_err >= 0)
1114 err = 0;
1115 else
1116 err = nfserrno(host_err);
1117 return err;
1121 * Read data from a file. count must contain the requested read count
1122 * on entry. On return, *count contains the number of bytes actually read.
1123 * N.B. After this call fhp needs an fh_put
1125 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1126 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1128 struct file *file;
1129 struct inode *inode;
1130 struct raparms *ra;
1131 __be32 err;
1133 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1134 if (err)
1135 return err;
1137 inode = file_inode(file);
1139 /* Get readahead parameters */
1140 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1142 if (ra && ra->p_set)
1143 file->f_ra = ra->p_ra;
1145 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1147 /* Write back readahead params */
1148 if (ra) {
1149 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1150 spin_lock(&rab->pb_lock);
1151 ra->p_ra = file->f_ra;
1152 ra->p_set = 1;
1153 ra->p_count--;
1154 spin_unlock(&rab->pb_lock);
1157 nfsd_close(file);
1158 return err;
1161 /* As above, but use the provided file descriptor. */
1162 __be32
1163 nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1164 loff_t offset, struct kvec *vec, int vlen,
1165 unsigned long *count)
1167 __be32 err;
1169 if (file) {
1170 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1171 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
1172 if (err)
1173 goto out;
1174 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1175 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1176 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
1177 out:
1178 return err;
1182 * Write data to a file.
1183 * The stable flag requests synchronous writes.
1184 * N.B. After this call fhp needs an fh_put
1186 __be32
1187 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1188 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1189 int *stablep)
1191 __be32 err = 0;
1193 if (file) {
1194 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1195 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1196 if (err)
1197 goto out;
1198 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1199 stablep);
1200 } else {
1201 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1202 if (err)
1203 goto out;
1205 if (cnt)
1206 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1207 cnt, stablep);
1208 nfsd_close(file);
1210 out:
1211 return err;
1214 #ifdef CONFIG_NFSD_V3
1216 * Commit all pending writes to stable storage.
1218 * Note: we only guarantee that data that lies within the range specified
1219 * by the 'offset' and 'count' parameters will be synced.
1221 * Unfortunately we cannot lock the file to make sure we return full WCC
1222 * data to the client, as locking happens lower down in the filesystem.
1224 __be32
1225 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1226 loff_t offset, unsigned long count)
1228 struct file *file;
1229 loff_t end = LLONG_MAX;
1230 __be32 err = nfserr_inval;
1232 if (offset < 0)
1233 goto out;
1234 if (count != 0) {
1235 end = offset + (loff_t)count - 1;
1236 if (end < offset)
1237 goto out;
1240 err = nfsd_open(rqstp, fhp, S_IFREG,
1241 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
1242 if (err)
1243 goto out;
1244 if (EX_ISSYNC(fhp->fh_export)) {
1245 int err2 = vfs_fsync_range(file, offset, end, 0);
1247 if (err2 != -EINVAL)
1248 err = nfserrno(err2);
1249 else
1250 err = nfserr_notsupp;
1253 nfsd_close(file);
1254 out:
1255 return err;
1257 #endif /* CONFIG_NFSD_V3 */
1259 static __be32
1260 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1261 struct iattr *iap)
1264 * Mode has already been set earlier in create:
1266 iap->ia_valid &= ~ATTR_MODE;
1268 * Setting uid/gid works only for root. Irix appears to
1269 * send along the gid on create when it tries to implement
1270 * setgid directories via NFS:
1272 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1273 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1274 if (iap->ia_valid)
1275 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1276 return 0;
1279 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1280 * setting size to 0 may fail for some specific file systems by the permission
1281 * checking which requires WRITE permission but the mode is 000.
1282 * we ignore the resizing(to 0) on the just new created file, since the size is
1283 * 0 after file created.
1285 * call this only after vfs_create() is called.
1286 * */
1287 static void
1288 nfsd_check_ignore_resizing(struct iattr *iap)
1290 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1291 iap->ia_valid &= ~ATTR_SIZE;
1295 * Create a file (regular, directory, device, fifo); UNIX sockets
1296 * not yet implemented.
1297 * If the response fh has been verified, the parent directory should
1298 * already be locked. Note that the parent directory is left locked.
1300 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1302 __be32
1303 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1304 char *fname, int flen, struct iattr *iap,
1305 int type, dev_t rdev, struct svc_fh *resfhp)
1307 struct dentry *dentry, *dchild = NULL;
1308 struct inode *dirp;
1309 __be32 err;
1310 __be32 err2;
1311 int host_err;
1313 err = nfserr_perm;
1314 if (!flen)
1315 goto out;
1316 err = nfserr_exist;
1317 if (isdotent(fname, flen))
1318 goto out;
1320 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1321 if (err)
1322 goto out;
1324 dentry = fhp->fh_dentry;
1325 dirp = dentry->d_inode;
1327 err = nfserr_notdir;
1328 if (!dirp->i_op->lookup)
1329 goto out;
1331 * Check whether the response file handle has been verified yet.
1332 * If it has, the parent directory should already be locked.
1334 if (!resfhp->fh_dentry) {
1335 host_err = fh_want_write(fhp);
1336 if (host_err)
1337 goto out_nfserr;
1339 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1340 fh_lock_nested(fhp, I_MUTEX_PARENT);
1341 dchild = lookup_one_len(fname, dentry, flen);
1342 host_err = PTR_ERR(dchild);
1343 if (IS_ERR(dchild))
1344 goto out_nfserr;
1345 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1346 if (err)
1347 goto out;
1348 } else {
1349 /* called from nfsd_proc_create */
1350 dchild = dget(resfhp->fh_dentry);
1351 if (!fhp->fh_locked) {
1352 /* not actually possible */
1353 printk(KERN_ERR
1354 "nfsd_create: parent %s/%s not locked!\n",
1355 dentry->d_parent->d_name.name,
1356 dentry->d_name.name);
1357 err = nfserr_io;
1358 goto out;
1362 * Make sure the child dentry is still negative ...
1364 err = nfserr_exist;
1365 if (dchild->d_inode) {
1366 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1367 dentry->d_name.name, dchild->d_name.name);
1368 goto out;
1371 if (!(iap->ia_valid & ATTR_MODE))
1372 iap->ia_mode = 0;
1373 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1375 err = nfserr_inval;
1376 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1377 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1378 type);
1379 goto out;
1383 * Get the dir op function pointer.
1385 err = 0;
1386 host_err = 0;
1387 switch (type) {
1388 case S_IFREG:
1389 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1390 if (!host_err)
1391 nfsd_check_ignore_resizing(iap);
1392 break;
1393 case S_IFDIR:
1394 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1395 break;
1396 case S_IFCHR:
1397 case S_IFBLK:
1398 case S_IFIFO:
1399 case S_IFSOCK:
1400 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1401 break;
1403 if (host_err < 0)
1404 goto out_nfserr;
1406 err = nfsd_create_setattr(rqstp, resfhp, iap);
1409 * nfsd_setattr already committed the child. Transactional filesystems
1410 * had a chance to commit changes for both parent and child
1411 * simultaneously making the following commit_metadata a noop.
1413 err2 = nfserrno(commit_metadata(fhp));
1414 if (err2)
1415 err = err2;
1417 * Update the file handle to get the new inode info.
1419 if (!err)
1420 err = fh_update(resfhp);
1421 out:
1422 if (dchild && !IS_ERR(dchild))
1423 dput(dchild);
1424 return err;
1426 out_nfserr:
1427 err = nfserrno(host_err);
1428 goto out;
1431 #ifdef CONFIG_NFSD_V3
1433 static inline int nfsd_create_is_exclusive(int createmode)
1435 return createmode == NFS3_CREATE_EXCLUSIVE
1436 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1440 * NFSv3 and NFSv4 version of nfsd_create
1442 __be32
1443 do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1444 char *fname, int flen, struct iattr *iap,
1445 struct svc_fh *resfhp, int createmode, u32 *verifier,
1446 bool *truncp, bool *created)
1448 struct dentry *dentry, *dchild = NULL;
1449 struct inode *dirp;
1450 __be32 err;
1451 int host_err;
1452 __u32 v_mtime=0, v_atime=0;
1454 err = nfserr_perm;
1455 if (!flen)
1456 goto out;
1457 err = nfserr_exist;
1458 if (isdotent(fname, flen))
1459 goto out;
1460 if (!(iap->ia_valid & ATTR_MODE))
1461 iap->ia_mode = 0;
1462 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1463 if (err)
1464 goto out;
1466 dentry = fhp->fh_dentry;
1467 dirp = dentry->d_inode;
1469 /* Get all the sanity checks out of the way before
1470 * we lock the parent. */
1471 err = nfserr_notdir;
1472 if (!dirp->i_op->lookup)
1473 goto out;
1475 host_err = fh_want_write(fhp);
1476 if (host_err)
1477 goto out_nfserr;
1479 fh_lock_nested(fhp, I_MUTEX_PARENT);
1482 * Compose the response file handle.
1484 dchild = lookup_one_len(fname, dentry, flen);
1485 host_err = PTR_ERR(dchild);
1486 if (IS_ERR(dchild))
1487 goto out_nfserr;
1489 /* If file doesn't exist, check for permissions to create one */
1490 if (!dchild->d_inode) {
1491 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1492 if (err)
1493 goto out;
1496 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1497 if (err)
1498 goto out;
1500 if (nfsd_create_is_exclusive(createmode)) {
1501 /* solaris7 gets confused (bugid 4218508) if these have
1502 * the high bit set, so just clear the high bits. If this is
1503 * ever changed to use different attrs for storing the
1504 * verifier, then do_open_lookup() will also need to be fixed
1505 * accordingly.
1507 v_mtime = verifier[0]&0x7fffffff;
1508 v_atime = verifier[1]&0x7fffffff;
1511 if (dchild->d_inode) {
1512 err = 0;
1514 switch (createmode) {
1515 case NFS3_CREATE_UNCHECKED:
1516 if (! S_ISREG(dchild->d_inode->i_mode))
1517 goto out;
1518 else if (truncp) {
1519 /* in nfsv4, we need to treat this case a little
1520 * differently. we don't want to truncate the
1521 * file now; this would be wrong if the OPEN
1522 * fails for some other reason. furthermore,
1523 * if the size is nonzero, we should ignore it
1524 * according to spec!
1526 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1528 else {
1529 iap->ia_valid &= ATTR_SIZE;
1530 goto set_attr;
1532 break;
1533 case NFS3_CREATE_EXCLUSIVE:
1534 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1535 && dchild->d_inode->i_atime.tv_sec == v_atime
1536 && dchild->d_inode->i_size == 0 ) {
1537 if (created)
1538 *created = 1;
1539 break;
1541 case NFS4_CREATE_EXCLUSIVE4_1:
1542 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1543 && dchild->d_inode->i_atime.tv_sec == v_atime
1544 && dchild->d_inode->i_size == 0 ) {
1545 if (created)
1546 *created = 1;
1547 goto set_attr;
1549 /* fallthru */
1550 case NFS3_CREATE_GUARDED:
1551 err = nfserr_exist;
1553 fh_drop_write(fhp);
1554 goto out;
1557 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1558 if (host_err < 0) {
1559 fh_drop_write(fhp);
1560 goto out_nfserr;
1562 if (created)
1563 *created = 1;
1565 nfsd_check_ignore_resizing(iap);
1567 if (nfsd_create_is_exclusive(createmode)) {
1568 /* Cram the verifier into atime/mtime */
1569 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1570 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1571 /* XXX someone who knows this better please fix it for nsec */
1572 iap->ia_mtime.tv_sec = v_mtime;
1573 iap->ia_atime.tv_sec = v_atime;
1574 iap->ia_mtime.tv_nsec = 0;
1575 iap->ia_atime.tv_nsec = 0;
1578 set_attr:
1579 err = nfsd_create_setattr(rqstp, resfhp, iap);
1582 * nfsd_setattr already committed the child (and possibly also the parent).
1584 if (!err)
1585 err = nfserrno(commit_metadata(fhp));
1588 * Update the filehandle to get the new inode info.
1590 if (!err)
1591 err = fh_update(resfhp);
1593 out:
1594 fh_unlock(fhp);
1595 if (dchild && !IS_ERR(dchild))
1596 dput(dchild);
1597 fh_drop_write(fhp);
1598 return err;
1600 out_nfserr:
1601 err = nfserrno(host_err);
1602 goto out;
1604 #endif /* CONFIG_NFSD_V3 */
1607 * Read a symlink. On entry, *lenp must contain the maximum path length that
1608 * fits into the buffer. On return, it contains the true length.
1609 * N.B. After this call fhp needs an fh_put
1611 __be32
1612 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1614 struct inode *inode;
1615 mm_segment_t oldfs;
1616 __be32 err;
1617 int host_err;
1618 struct path path;
1620 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1621 if (err)
1622 goto out;
1624 path.mnt = fhp->fh_export->ex_path.mnt;
1625 path.dentry = fhp->fh_dentry;
1626 inode = path.dentry->d_inode;
1628 err = nfserr_inval;
1629 if (!inode->i_op->readlink)
1630 goto out;
1632 touch_atime(&path);
1633 /* N.B. Why does this call need a get_fs()??
1634 * Remove the set_fs and watch the fireworks:-) --okir
1637 oldfs = get_fs(); set_fs(KERNEL_DS);
1638 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
1639 set_fs(oldfs);
1641 if (host_err < 0)
1642 goto out_nfserr;
1643 *lenp = host_err;
1644 err = 0;
1645 out:
1646 return err;
1648 out_nfserr:
1649 err = nfserrno(host_err);
1650 goto out;
1654 * Create a symlink and look up its inode
1655 * N.B. After this call _both_ fhp and resfhp need an fh_put
1657 __be32
1658 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1659 char *fname, int flen,
1660 char *path, int plen,
1661 struct svc_fh *resfhp,
1662 struct iattr *iap)
1664 struct dentry *dentry, *dnew;
1665 __be32 err, cerr;
1666 int host_err;
1668 err = nfserr_noent;
1669 if (!flen || !plen)
1670 goto out;
1671 err = nfserr_exist;
1672 if (isdotent(fname, flen))
1673 goto out;
1675 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1676 if (err)
1677 goto out;
1679 host_err = fh_want_write(fhp);
1680 if (host_err)
1681 goto out_nfserr;
1683 fh_lock(fhp);
1684 dentry = fhp->fh_dentry;
1685 dnew = lookup_one_len(fname, dentry, flen);
1686 host_err = PTR_ERR(dnew);
1687 if (IS_ERR(dnew))
1688 goto out_nfserr;
1690 if (unlikely(path[plen] != 0)) {
1691 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1692 if (path_alloced == NULL)
1693 host_err = -ENOMEM;
1694 else {
1695 strncpy(path_alloced, path, plen);
1696 path_alloced[plen] = 0;
1697 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1698 kfree(path_alloced);
1700 } else
1701 host_err = vfs_symlink(dentry->d_inode, dnew, path);
1702 err = nfserrno(host_err);
1703 if (!err)
1704 err = nfserrno(commit_metadata(fhp));
1705 fh_unlock(fhp);
1707 fh_drop_write(fhp);
1709 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1710 dput(dnew);
1711 if (err==0) err = cerr;
1712 out:
1713 return err;
1715 out_nfserr:
1716 err = nfserrno(host_err);
1717 goto out;
1721 * Create a hardlink
1722 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1724 __be32
1725 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1726 char *name, int len, struct svc_fh *tfhp)
1728 struct dentry *ddir, *dnew, *dold;
1729 struct inode *dirp;
1730 __be32 err;
1731 int host_err;
1733 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1734 if (err)
1735 goto out;
1736 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1737 if (err)
1738 goto out;
1739 err = nfserr_isdir;
1740 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1741 goto out;
1742 err = nfserr_perm;
1743 if (!len)
1744 goto out;
1745 err = nfserr_exist;
1746 if (isdotent(name, len))
1747 goto out;
1749 host_err = fh_want_write(tfhp);
1750 if (host_err) {
1751 err = nfserrno(host_err);
1752 goto out;
1755 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1756 ddir = ffhp->fh_dentry;
1757 dirp = ddir->d_inode;
1759 dnew = lookup_one_len(name, ddir, len);
1760 host_err = PTR_ERR(dnew);
1761 if (IS_ERR(dnew))
1762 goto out_nfserr;
1764 dold = tfhp->fh_dentry;
1766 err = nfserr_noent;
1767 if (!dold->d_inode)
1768 goto out_dput;
1769 host_err = nfsd_break_lease(dold->d_inode);
1770 if (host_err) {
1771 err = nfserrno(host_err);
1772 goto out_dput;
1774 host_err = vfs_link(dold, dirp, dnew);
1775 if (!host_err) {
1776 err = nfserrno(commit_metadata(ffhp));
1777 if (!err)
1778 err = nfserrno(commit_metadata(tfhp));
1779 } else {
1780 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1781 err = nfserr_acces;
1782 else
1783 err = nfserrno(host_err);
1785 out_dput:
1786 dput(dnew);
1787 out_unlock:
1788 fh_unlock(ffhp);
1789 fh_drop_write(tfhp);
1790 out:
1791 return err;
1793 out_nfserr:
1794 err = nfserrno(host_err);
1795 goto out_unlock;
1799 * Rename a file
1800 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1802 __be32
1803 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1804 struct svc_fh *tfhp, char *tname, int tlen)
1806 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1807 struct inode *fdir, *tdir;
1808 __be32 err;
1809 int host_err;
1811 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1812 if (err)
1813 goto out;
1814 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1815 if (err)
1816 goto out;
1818 fdentry = ffhp->fh_dentry;
1819 fdir = fdentry->d_inode;
1821 tdentry = tfhp->fh_dentry;
1822 tdir = tdentry->d_inode;
1824 err = nfserr_perm;
1825 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1826 goto out;
1828 host_err = fh_want_write(ffhp);
1829 if (host_err) {
1830 err = nfserrno(host_err);
1831 goto out;
1834 /* cannot use fh_lock as we need deadlock protective ordering
1835 * so do it by hand */
1836 trap = lock_rename(tdentry, fdentry);
1837 ffhp->fh_locked = tfhp->fh_locked = 1;
1838 fill_pre_wcc(ffhp);
1839 fill_pre_wcc(tfhp);
1841 odentry = lookup_one_len(fname, fdentry, flen);
1842 host_err = PTR_ERR(odentry);
1843 if (IS_ERR(odentry))
1844 goto out_nfserr;
1846 host_err = -ENOENT;
1847 if (!odentry->d_inode)
1848 goto out_dput_old;
1849 host_err = -EINVAL;
1850 if (odentry == trap)
1851 goto out_dput_old;
1853 ndentry = lookup_one_len(tname, tdentry, tlen);
1854 host_err = PTR_ERR(ndentry);
1855 if (IS_ERR(ndentry))
1856 goto out_dput_old;
1857 host_err = -ENOTEMPTY;
1858 if (ndentry == trap)
1859 goto out_dput_new;
1861 host_err = -EXDEV;
1862 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1863 goto out_dput_new;
1864 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1865 goto out_dput_new;
1867 host_err = nfsd_break_lease(odentry->d_inode);
1868 if (host_err)
1869 goto out_dput_new;
1870 if (ndentry->d_inode) {
1871 host_err = nfsd_break_lease(ndentry->d_inode);
1872 if (host_err)
1873 goto out_dput_new;
1875 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
1876 if (!host_err) {
1877 host_err = commit_metadata(tfhp);
1878 if (!host_err)
1879 host_err = commit_metadata(ffhp);
1881 out_dput_new:
1882 dput(ndentry);
1883 out_dput_old:
1884 dput(odentry);
1885 out_nfserr:
1886 err = nfserrno(host_err);
1888 /* we cannot reply on fh_unlock on the two filehandles,
1889 * as that would do the wrong thing if the two directories
1890 * were the same, so again we do it by hand
1892 fill_post_wcc(ffhp);
1893 fill_post_wcc(tfhp);
1894 unlock_rename(tdentry, fdentry);
1895 ffhp->fh_locked = tfhp->fh_locked = 0;
1896 fh_drop_write(ffhp);
1898 out:
1899 return err;
1903 * Unlink a file or directory
1904 * N.B. After this call fhp needs an fh_put
1906 __be32
1907 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1908 char *fname, int flen)
1910 struct dentry *dentry, *rdentry;
1911 struct inode *dirp;
1912 __be32 err;
1913 int host_err;
1915 err = nfserr_acces;
1916 if (!flen || isdotent(fname, flen))
1917 goto out;
1918 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1919 if (err)
1920 goto out;
1922 host_err = fh_want_write(fhp);
1923 if (host_err)
1924 goto out_nfserr;
1926 fh_lock_nested(fhp, I_MUTEX_PARENT);
1927 dentry = fhp->fh_dentry;
1928 dirp = dentry->d_inode;
1930 rdentry = lookup_one_len(fname, dentry, flen);
1931 host_err = PTR_ERR(rdentry);
1932 if (IS_ERR(rdentry))
1933 goto out_nfserr;
1935 if (!rdentry->d_inode) {
1936 dput(rdentry);
1937 err = nfserr_noent;
1938 goto out;
1941 if (!type)
1942 type = rdentry->d_inode->i_mode & S_IFMT;
1944 host_err = nfsd_break_lease(rdentry->d_inode);
1945 if (host_err)
1946 goto out_put;
1947 if (type != S_IFDIR)
1948 host_err = vfs_unlink(dirp, rdentry);
1949 else
1950 host_err = vfs_rmdir(dirp, rdentry);
1951 if (!host_err)
1952 host_err = commit_metadata(fhp);
1953 out_put:
1954 dput(rdentry);
1956 out_nfserr:
1957 err = nfserrno(host_err);
1958 out:
1959 return err;
1963 * We do this buffering because we must not call back into the file
1964 * system's ->lookup() method from the filldir callback. That may well
1965 * deadlock a number of file systems.
1967 * This is based heavily on the implementation of same in XFS.
1969 struct buffered_dirent {
1970 u64 ino;
1971 loff_t offset;
1972 int namlen;
1973 unsigned int d_type;
1974 char name[];
1977 struct readdir_data {
1978 struct dir_context ctx;
1979 char *dirent;
1980 size_t used;
1981 int full;
1984 static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1985 loff_t offset, u64 ino, unsigned int d_type)
1987 struct readdir_data *buf = __buf;
1988 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1989 unsigned int reclen;
1991 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1992 if (buf->used + reclen > PAGE_SIZE) {
1993 buf->full = 1;
1994 return -EINVAL;
1997 de->namlen = namlen;
1998 de->offset = offset;
1999 de->ino = ino;
2000 de->d_type = d_type;
2001 memcpy(de->name, name, namlen);
2002 buf->used += reclen;
2004 return 0;
2007 static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
2008 struct readdir_cd *cdp, loff_t *offsetp)
2010 struct buffered_dirent *de;
2011 int host_err;
2012 int size;
2013 loff_t offset;
2014 struct readdir_data buf = {
2015 .ctx.actor = nfsd_buffered_filldir,
2016 .dirent = (void *)__get_free_page(GFP_KERNEL)
2019 if (!buf.dirent)
2020 return nfserrno(-ENOMEM);
2022 offset = *offsetp;
2024 while (1) {
2025 struct inode *dir_inode = file_inode(file);
2026 unsigned int reclen;
2028 cdp->err = nfserr_eof; /* will be cleared on successful read */
2029 buf.used = 0;
2030 buf.full = 0;
2032 host_err = iterate_dir(file, &buf.ctx);
2033 if (buf.full)
2034 host_err = 0;
2036 if (host_err < 0)
2037 break;
2039 size = buf.used;
2041 if (!size)
2042 break;
2045 * Various filldir functions may end up calling back into
2046 * lookup_one_len() and the file system's ->lookup() method.
2047 * These expect i_mutex to be held, as it would within readdir.
2049 host_err = mutex_lock_killable(&dir_inode->i_mutex);
2050 if (host_err)
2051 break;
2053 de = (struct buffered_dirent *)buf.dirent;
2054 while (size > 0) {
2055 offset = de->offset;
2057 if (func(cdp, de->name, de->namlen, de->offset,
2058 de->ino, de->d_type))
2059 break;
2061 if (cdp->err != nfs_ok)
2062 break;
2064 reclen = ALIGN(sizeof(*de) + de->namlen,
2065 sizeof(u64));
2066 size -= reclen;
2067 de = (struct buffered_dirent *)((char *)de + reclen);
2069 mutex_unlock(&dir_inode->i_mutex);
2070 if (size > 0) /* We bailed out early */
2071 break;
2073 offset = vfs_llseek(file, 0, SEEK_CUR);
2076 free_page((unsigned long)(buf.dirent));
2078 if (host_err)
2079 return nfserrno(host_err);
2081 *offsetp = offset;
2082 return cdp->err;
2086 * Read entries from a directory.
2087 * The NFSv3/4 verifier we ignore for now.
2089 __be32
2090 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2091 struct readdir_cd *cdp, filldir_t func)
2093 __be32 err;
2094 struct file *file;
2095 loff_t offset = *offsetp;
2096 int may_flags = NFSD_MAY_READ;
2098 /* NFSv2 only supports 32 bit cookies */
2099 if (rqstp->rq_vers > 2)
2100 may_flags |= NFSD_MAY_64BIT_COOKIE;
2102 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
2103 if (err)
2104 goto out;
2106 offset = vfs_llseek(file, offset, SEEK_SET);
2107 if (offset < 0) {
2108 err = nfserrno((int)offset);
2109 goto out_close;
2112 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
2114 if (err == nfserr_eof || err == nfserr_toosmall)
2115 err = nfs_ok; /* can still be found in ->err */
2116 out_close:
2117 nfsd_close(file);
2118 out:
2119 return err;
2123 * Get file system stats
2124 * N.B. After this call fhp needs an fh_put
2126 __be32
2127 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2129 __be32 err;
2131 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2132 if (!err) {
2133 struct path path = {
2134 .mnt = fhp->fh_export->ex_path.mnt,
2135 .dentry = fhp->fh_dentry,
2137 if (vfs_statfs(&path, stat))
2138 err = nfserr_io;
2140 return err;
2143 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2145 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2149 * Check for a user's access permissions to this inode.
2151 __be32
2152 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2153 struct dentry *dentry, int acc)
2155 struct inode *inode = dentry->d_inode;
2156 int err;
2158 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2159 return 0;
2160 #if 0
2161 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2162 acc,
2163 (acc & NFSD_MAY_READ)? " read" : "",
2164 (acc & NFSD_MAY_WRITE)? " write" : "",
2165 (acc & NFSD_MAY_EXEC)? " exec" : "",
2166 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2167 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2168 (acc & NFSD_MAY_LOCK)? " lock" : "",
2169 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2170 inode->i_mode,
2171 IS_IMMUTABLE(inode)? " immut" : "",
2172 IS_APPEND(inode)? " append" : "",
2173 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2174 dprintk(" owner %d/%d user %d/%d\n",
2175 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2176 #endif
2178 /* Normally we reject any write/sattr etc access on a read-only file
2179 * system. But if it is IRIX doing check on write-access for a
2180 * device special file, we ignore rofs.
2182 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2183 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2184 if (exp_rdonly(rqstp, exp) ||
2185 __mnt_is_readonly(exp->ex_path.mnt))
2186 return nfserr_rofs;
2187 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2188 return nfserr_perm;
2190 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2191 return nfserr_perm;
2193 if (acc & NFSD_MAY_LOCK) {
2194 /* If we cannot rely on authentication in NLM requests,
2195 * just allow locks, otherwise require read permission, or
2196 * ownership
2198 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2199 return 0;
2200 else
2201 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2204 * The file owner always gets access permission for accesses that
2205 * would normally be checked at open time. This is to make
2206 * file access work even when the client has done a fchmod(fd, 0).
2208 * However, `cp foo bar' should fail nevertheless when bar is
2209 * readonly. A sensible way to do this might be to reject all
2210 * attempts to truncate a read-only file, because a creat() call
2211 * always implies file truncation.
2212 * ... but this isn't really fair. A process may reasonably call
2213 * ftruncate on an open file descriptor on a file with perm 000.
2214 * We must trust the client to do permission checking - using "ACCESS"
2215 * with NFSv3.
2217 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2218 uid_eq(inode->i_uid, current_fsuid()))
2219 return 0;
2221 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2222 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2224 /* Allow read access to binaries even when mode 111 */
2225 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2226 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2227 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2228 err = inode_permission(inode, MAY_EXEC);
2230 return err? nfserrno(err) : 0;
2233 void
2234 nfsd_racache_shutdown(void)
2236 struct raparms *raparm, *last_raparm;
2237 unsigned int i;
2239 dprintk("nfsd: freeing readahead buffers.\n");
2241 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2242 raparm = raparm_hash[i].pb_head;
2243 while(raparm) {
2244 last_raparm = raparm;
2245 raparm = raparm->p_next;
2246 kfree(last_raparm);
2248 raparm_hash[i].pb_head = NULL;
2252 * Initialize readahead param cache
2255 nfsd_racache_init(int cache_size)
2257 int i;
2258 int j = 0;
2259 int nperbucket;
2260 struct raparms **raparm = NULL;
2263 if (raparm_hash[0].pb_head)
2264 return 0;
2265 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2266 if (nperbucket < 2)
2267 nperbucket = 2;
2268 cache_size = nperbucket * RAPARM_HASH_SIZE;
2270 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2272 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2273 spin_lock_init(&raparm_hash[i].pb_lock);
2275 raparm = &raparm_hash[i].pb_head;
2276 for (j = 0; j < nperbucket; j++) {
2277 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2278 if (!*raparm)
2279 goto out_nomem;
2280 raparm = &(*raparm)->p_next;
2282 *raparm = NULL;
2285 nfsdstats.ra_size = cache_size;
2286 return 0;
2288 out_nomem:
2289 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2290 nfsd_racache_shutdown();
2291 return -ENOMEM;
2294 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2295 struct posix_acl *
2296 nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2298 struct inode *inode = fhp->fh_dentry->d_inode;
2299 char *name;
2300 void *value = NULL;
2301 ssize_t size;
2302 struct posix_acl *acl;
2304 if (!IS_POSIXACL(inode))
2305 return ERR_PTR(-EOPNOTSUPP);
2307 switch (type) {
2308 case ACL_TYPE_ACCESS:
2309 name = POSIX_ACL_XATTR_ACCESS;
2310 break;
2311 case ACL_TYPE_DEFAULT:
2312 name = POSIX_ACL_XATTR_DEFAULT;
2313 break;
2314 default:
2315 return ERR_PTR(-EOPNOTSUPP);
2318 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2319 if (size < 0)
2320 return ERR_PTR(size);
2322 acl = posix_acl_from_xattr(&init_user_ns, value, size);
2323 kfree(value);
2324 return acl;
2328 nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2330 struct inode *inode = fhp->fh_dentry->d_inode;
2331 char *name;
2332 void *value = NULL;
2333 size_t size;
2334 int error;
2336 if (!IS_POSIXACL(inode) ||
2337 !inode->i_op->setxattr || !inode->i_op->removexattr)
2338 return -EOPNOTSUPP;
2339 switch(type) {
2340 case ACL_TYPE_ACCESS:
2341 name = POSIX_ACL_XATTR_ACCESS;
2342 break;
2343 case ACL_TYPE_DEFAULT:
2344 name = POSIX_ACL_XATTR_DEFAULT;
2345 break;
2346 default:
2347 return -EOPNOTSUPP;
2350 if (acl && acl->a_count) {
2351 size = posix_acl_xattr_size(acl->a_count);
2352 value = kmalloc(size, GFP_KERNEL);
2353 if (!value)
2354 return -ENOMEM;
2355 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
2356 if (error < 0)
2357 goto getout;
2358 size = error;
2359 } else
2360 size = 0;
2362 error = fh_want_write(fhp);
2363 if (error)
2364 goto getout;
2365 if (size)
2366 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
2367 else {
2368 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2369 error = 0;
2370 else {
2371 error = vfs_removexattr(fhp->fh_dentry, name);
2372 if (error == -ENODATA)
2373 error = 0;
2376 fh_drop_write(fhp);
2378 getout:
2379 kfree(value);
2380 return error;
2382 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */