Linux 3.12.5
[linux/fpc-iii.git] / fs / nfsd / vfs.c
blob72cb28e73ca0a4e515e791a7a33fefccb86ab356
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 int size_change = 0;
412 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
413 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
414 if (iap->ia_valid & ATTR_SIZE)
415 ftype = S_IFREG;
417 /* Get inode */
418 err = fh_verify(rqstp, fhp, ftype, accmode);
419 if (err)
420 goto out;
422 dentry = fhp->fh_dentry;
423 inode = dentry->d_inode;
425 /* Ignore any mode updates on symlinks */
426 if (S_ISLNK(inode->i_mode))
427 iap->ia_valid &= ~ATTR_MODE;
429 if (!iap->ia_valid)
430 goto out;
432 nfsd_sanitize_attrs(inode, iap);
435 * The size case is special, it changes the file in addition to the
436 * attributes.
438 if (iap->ia_valid & ATTR_SIZE) {
439 err = nfsd_get_write_access(rqstp, fhp, iap);
440 if (err)
441 goto out;
442 size_change = 1;
445 iap->ia_valid |= ATTR_CTIME;
447 if (check_guard && guardtime != inode->i_ctime.tv_sec) {
448 err = nfserr_notsync;
449 goto out_put_write_access;
452 host_err = nfsd_break_lease(inode);
453 if (host_err)
454 goto out_put_write_access_nfserror;
456 fh_lock(fhp);
457 host_err = notify_change(dentry, iap);
458 fh_unlock(fhp);
460 out_put_write_access_nfserror:
461 err = nfserrno(host_err);
462 out_put_write_access:
463 if (size_change)
464 put_write_access(inode);
465 if (!err)
466 commit_metadata(fhp);
467 out:
468 return err;
471 #if defined(CONFIG_NFSD_V2_ACL) || \
472 defined(CONFIG_NFSD_V3_ACL) || \
473 defined(CONFIG_NFSD_V4)
474 static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
476 ssize_t buflen;
477 ssize_t ret;
479 buflen = vfs_getxattr(dentry, key, NULL, 0);
480 if (buflen <= 0)
481 return buflen;
483 *buf = kmalloc(buflen, GFP_KERNEL);
484 if (!*buf)
485 return -ENOMEM;
487 ret = vfs_getxattr(dentry, key, *buf, buflen);
488 if (ret < 0)
489 kfree(*buf);
490 return ret;
492 #endif
494 #if defined(CONFIG_NFSD_V4)
495 static int
496 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
498 int len;
499 size_t buflen;
500 char *buf = NULL;
501 int error = 0;
503 buflen = posix_acl_xattr_size(pacl->a_count);
504 buf = kmalloc(buflen, GFP_KERNEL);
505 error = -ENOMEM;
506 if (buf == NULL)
507 goto out;
509 len = posix_acl_to_xattr(&init_user_ns, pacl, buf, buflen);
510 if (len < 0) {
511 error = len;
512 goto out;
515 error = vfs_setxattr(dentry, key, buf, len, 0);
516 out:
517 kfree(buf);
518 return error;
521 __be32
522 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
523 struct nfs4_acl *acl)
525 __be32 error;
526 int host_error;
527 struct dentry *dentry;
528 struct inode *inode;
529 struct posix_acl *pacl = NULL, *dpacl = NULL;
530 unsigned int flags = 0;
532 /* Get inode */
533 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
534 if (error)
535 return error;
537 dentry = fhp->fh_dentry;
538 inode = dentry->d_inode;
539 if (S_ISDIR(inode->i_mode))
540 flags = NFS4_ACL_DIR;
542 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
543 if (host_error == -EINVAL) {
544 return nfserr_attrnotsupp;
545 } else if (host_error < 0)
546 goto out_nfserr;
548 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
549 if (host_error < 0)
550 goto out_release;
552 if (S_ISDIR(inode->i_mode))
553 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
555 out_release:
556 posix_acl_release(pacl);
557 posix_acl_release(dpacl);
558 out_nfserr:
559 if (host_error == -EOPNOTSUPP)
560 return nfserr_attrnotsupp;
561 else
562 return nfserrno(host_error);
565 static struct posix_acl *
566 _get_posix_acl(struct dentry *dentry, char *key)
568 void *buf = NULL;
569 struct posix_acl *pacl = NULL;
570 int buflen;
572 buflen = nfsd_getxattr(dentry, key, &buf);
573 if (!buflen)
574 buflen = -ENODATA;
575 if (buflen <= 0)
576 return ERR_PTR(buflen);
578 pacl = posix_acl_from_xattr(&init_user_ns, buf, buflen);
579 kfree(buf);
580 return pacl;
584 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
586 struct inode *inode = dentry->d_inode;
587 int error = 0;
588 struct posix_acl *pacl = NULL, *dpacl = NULL;
589 unsigned int flags = 0;
591 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
592 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
593 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
594 if (IS_ERR(pacl)) {
595 error = PTR_ERR(pacl);
596 pacl = NULL;
597 goto out;
600 if (S_ISDIR(inode->i_mode)) {
601 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
602 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
603 dpacl = NULL;
604 else if (IS_ERR(dpacl)) {
605 error = PTR_ERR(dpacl);
606 dpacl = NULL;
607 goto out;
609 flags = NFS4_ACL_DIR;
612 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
613 if (IS_ERR(*acl)) {
614 error = PTR_ERR(*acl);
615 *acl = NULL;
617 out:
618 posix_acl_release(pacl);
619 posix_acl_release(dpacl);
620 return error;
624 * NFS junction information is stored in an extended attribute.
626 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
629 * nfsd4_is_junction - Test if an object could be an NFS junction
631 * @dentry: object to test
633 * Returns 1 if "dentry" appears to contain NFS junction information.
634 * Otherwise 0 is returned.
636 int nfsd4_is_junction(struct dentry *dentry)
638 struct inode *inode = dentry->d_inode;
640 if (inode == NULL)
641 return 0;
642 if (inode->i_mode & S_IXUGO)
643 return 0;
644 if (!(inode->i_mode & S_ISVTX))
645 return 0;
646 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
647 return 0;
648 return 1;
650 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
651 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
652 struct xdr_netobj *label)
654 __be32 error;
655 int host_error;
656 struct dentry *dentry;
658 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
659 if (error)
660 return error;
662 dentry = fhp->fh_dentry;
664 mutex_lock(&dentry->d_inode->i_mutex);
665 host_error = security_inode_setsecctx(dentry, label->data, label->len);
666 mutex_unlock(&dentry->d_inode->i_mutex);
667 return nfserrno(host_error);
669 #else
670 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
671 struct xdr_netobj *label)
673 return nfserr_notsupp;
675 #endif
677 #endif /* defined(CONFIG_NFSD_V4) */
679 #ifdef CONFIG_NFSD_V3
681 * Check server access rights to a file system object
683 struct accessmap {
684 u32 access;
685 int how;
687 static struct accessmap nfs3_regaccess[] = {
688 { NFS3_ACCESS_READ, NFSD_MAY_READ },
689 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
690 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
691 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
693 { 0, 0 }
696 static struct accessmap nfs3_diraccess[] = {
697 { NFS3_ACCESS_READ, NFSD_MAY_READ },
698 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
699 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
700 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
701 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
703 { 0, 0 }
706 static struct accessmap nfs3_anyaccess[] = {
707 /* Some clients - Solaris 2.6 at least, make an access call
708 * to the server to check for access for things like /dev/null
709 * (which really, the server doesn't care about). So
710 * We provide simple access checking for them, looking
711 * mainly at mode bits, and we make sure to ignore read-only
712 * filesystem checks
714 { NFS3_ACCESS_READ, NFSD_MAY_READ },
715 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
716 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
717 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
719 { 0, 0 }
722 __be32
723 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
725 struct accessmap *map;
726 struct svc_export *export;
727 struct dentry *dentry;
728 u32 query, result = 0, sresult = 0;
729 __be32 error;
731 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
732 if (error)
733 goto out;
735 export = fhp->fh_export;
736 dentry = fhp->fh_dentry;
738 if (S_ISREG(dentry->d_inode->i_mode))
739 map = nfs3_regaccess;
740 else if (S_ISDIR(dentry->d_inode->i_mode))
741 map = nfs3_diraccess;
742 else
743 map = nfs3_anyaccess;
746 query = *access;
747 for (; map->access; map++) {
748 if (map->access & query) {
749 __be32 err2;
751 sresult |= map->access;
753 err2 = nfsd_permission(rqstp, export, dentry, map->how);
754 switch (err2) {
755 case nfs_ok:
756 result |= map->access;
757 break;
759 /* the following error codes just mean the access was not allowed,
760 * rather than an error occurred */
761 case nfserr_rofs:
762 case nfserr_acces:
763 case nfserr_perm:
764 /* simply don't "or" in the access bit. */
765 break;
766 default:
767 error = err2;
768 goto out;
772 *access = result;
773 if (supported)
774 *supported = sresult;
776 out:
777 return error;
779 #endif /* CONFIG_NFSD_V3 */
781 static int nfsd_open_break_lease(struct inode *inode, int access)
783 unsigned int mode;
785 if (access & NFSD_MAY_NOT_BREAK_LEASE)
786 return 0;
787 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
788 return break_lease(inode, mode | O_NONBLOCK);
792 * Open an existing file or directory.
793 * The may_flags argument indicates the type of open (read/write/lock)
794 * and additional flags.
795 * N.B. After this call fhp needs an fh_put
797 __be32
798 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
799 int may_flags, struct file **filp)
801 struct path path;
802 struct inode *inode;
803 int flags = O_RDONLY|O_LARGEFILE;
804 __be32 err;
805 int host_err = 0;
807 validate_process_creds();
810 * If we get here, then the client has already done an "open",
811 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
812 * in case a chmod has now revoked permission.
814 * Arguably we should also allow the owner override for
815 * directories, but we never have and it doesn't seem to have
816 * caused anyone a problem. If we were to change this, note
817 * also that our filldir callbacks would need a variant of
818 * lookup_one_len that doesn't check permissions.
820 if (type == S_IFREG)
821 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
822 err = fh_verify(rqstp, fhp, type, may_flags);
823 if (err)
824 goto out;
826 path.mnt = fhp->fh_export->ex_path.mnt;
827 path.dentry = fhp->fh_dentry;
828 inode = path.dentry->d_inode;
830 /* Disallow write access to files with the append-only bit set
831 * or any access when mandatory locking enabled
833 err = nfserr_perm;
834 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
835 goto out;
837 * We must ignore files (but only files) which might have mandatory
838 * locks on them because there is no way to know if the accesser has
839 * the lock.
841 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
842 goto out;
844 if (!inode->i_fop)
845 goto out;
847 host_err = nfsd_open_break_lease(inode, may_flags);
848 if (host_err) /* NOMEM or WOULDBLOCK */
849 goto out_nfserr;
851 if (may_flags & NFSD_MAY_WRITE) {
852 if (may_flags & NFSD_MAY_READ)
853 flags = O_RDWR|O_LARGEFILE;
854 else
855 flags = O_WRONLY|O_LARGEFILE;
857 *filp = dentry_open(&path, flags, current_cred());
858 if (IS_ERR(*filp)) {
859 host_err = PTR_ERR(*filp);
860 *filp = NULL;
861 } else {
862 host_err = ima_file_check(*filp, may_flags);
864 if (may_flags & NFSD_MAY_64BIT_COOKIE)
865 (*filp)->f_mode |= FMODE_64BITHASH;
866 else
867 (*filp)->f_mode |= FMODE_32BITHASH;
870 out_nfserr:
871 err = nfserrno(host_err);
872 out:
873 validate_process_creds();
874 return err;
878 * Close a file.
880 void
881 nfsd_close(struct file *filp)
883 fput(filp);
887 * Obtain the readahead parameters for the file
888 * specified by (dev, ino).
891 static inline struct raparms *
892 nfsd_get_raparms(dev_t dev, ino_t ino)
894 struct raparms *ra, **rap, **frap = NULL;
895 int depth = 0;
896 unsigned int hash;
897 struct raparm_hbucket *rab;
899 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
900 rab = &raparm_hash[hash];
902 spin_lock(&rab->pb_lock);
903 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
904 if (ra->p_ino == ino && ra->p_dev == dev)
905 goto found;
906 depth++;
907 if (ra->p_count == 0)
908 frap = rap;
910 depth = nfsdstats.ra_size;
911 if (!frap) {
912 spin_unlock(&rab->pb_lock);
913 return NULL;
915 rap = frap;
916 ra = *frap;
917 ra->p_dev = dev;
918 ra->p_ino = ino;
919 ra->p_set = 0;
920 ra->p_hindex = hash;
921 found:
922 if (rap != &rab->pb_head) {
923 *rap = ra->p_next;
924 ra->p_next = rab->pb_head;
925 rab->pb_head = ra;
927 ra->p_count++;
928 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
929 spin_unlock(&rab->pb_lock);
930 return ra;
934 * Grab and keep cached pages associated with a file in the svc_rqst
935 * so that they can be passed to the network sendmsg/sendpage routines
936 * directly. They will be released after the sending has completed.
938 static int
939 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
940 struct splice_desc *sd)
942 struct svc_rqst *rqstp = sd->u.data;
943 struct page **pp = rqstp->rq_next_page;
944 struct page *page = buf->page;
945 size_t size;
947 size = sd->len;
949 if (rqstp->rq_res.page_len == 0) {
950 get_page(page);
951 put_page(*rqstp->rq_next_page);
952 *(rqstp->rq_next_page++) = page;
953 rqstp->rq_res.page_base = buf->offset;
954 rqstp->rq_res.page_len = size;
955 } else if (page != pp[-1]) {
956 get_page(page);
957 if (*rqstp->rq_next_page)
958 put_page(*rqstp->rq_next_page);
959 *(rqstp->rq_next_page++) = page;
960 rqstp->rq_res.page_len += size;
961 } else
962 rqstp->rq_res.page_len += size;
964 return size;
967 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
968 struct splice_desc *sd)
970 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
973 static __be32
974 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
975 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
977 mm_segment_t oldfs;
978 __be32 err;
979 int host_err;
981 err = nfserr_perm;
983 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
984 struct splice_desc sd = {
985 .len = 0,
986 .total_len = *count,
987 .pos = offset,
988 .u.data = rqstp,
991 rqstp->rq_next_page = rqstp->rq_respages + 1;
992 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
993 } else {
994 oldfs = get_fs();
995 set_fs(KERNEL_DS);
996 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
997 set_fs(oldfs);
1000 if (host_err >= 0) {
1001 nfsdstats.io_read += host_err;
1002 *count = host_err;
1003 err = 0;
1004 fsnotify_access(file);
1005 } else
1006 err = nfserrno(host_err);
1007 return err;
1010 static void kill_suid(struct dentry *dentry)
1012 struct iattr ia;
1013 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
1015 mutex_lock(&dentry->d_inode->i_mutex);
1016 notify_change(dentry, &ia);
1017 mutex_unlock(&dentry->d_inode->i_mutex);
1021 * Gathered writes: If another process is currently writing to the file,
1022 * there's a high chance this is another nfsd (triggered by a bulk write
1023 * from a client's biod). Rather than syncing the file with each write
1024 * request, we sleep for 10 msec.
1026 * I don't know if this roughly approximates C. Juszak's idea of
1027 * gathered writes, but it's a nice and simple solution (IMHO), and it
1028 * seems to work:-)
1030 * Note: we do this only in the NFSv2 case, since v3 and higher have a
1031 * better tool (separate unstable writes and commits) for solving this
1032 * problem.
1034 static int wait_for_concurrent_writes(struct file *file)
1036 struct inode *inode = file_inode(file);
1037 static ino_t last_ino;
1038 static dev_t last_dev;
1039 int err = 0;
1041 if (atomic_read(&inode->i_writecount) > 1
1042 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1043 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1044 msleep(10);
1045 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1048 if (inode->i_state & I_DIRTY) {
1049 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1050 err = vfs_fsync(file, 0);
1052 last_ino = inode->i_ino;
1053 last_dev = inode->i_sb->s_dev;
1054 return err;
1057 static __be32
1058 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1059 loff_t offset, struct kvec *vec, int vlen,
1060 unsigned long *cnt, int *stablep)
1062 struct svc_export *exp;
1063 struct dentry *dentry;
1064 struct inode *inode;
1065 mm_segment_t oldfs;
1066 __be32 err = 0;
1067 int host_err;
1068 int stable = *stablep;
1069 int use_wgather;
1070 loff_t pos = offset;
1072 dentry = file->f_path.dentry;
1073 inode = dentry->d_inode;
1074 exp = fhp->fh_export;
1076 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1078 if (!EX_ISSYNC(exp))
1079 stable = 0;
1081 /* Write the data. */
1082 oldfs = get_fs(); set_fs(KERNEL_DS);
1083 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
1084 set_fs(oldfs);
1085 if (host_err < 0)
1086 goto out_nfserr;
1087 *cnt = host_err;
1088 nfsdstats.io_write += host_err;
1089 fsnotify_modify(file);
1091 /* clear setuid/setgid flag after write */
1092 if (inode->i_mode & (S_ISUID | S_ISGID))
1093 kill_suid(dentry);
1095 if (stable) {
1096 if (use_wgather)
1097 host_err = wait_for_concurrent_writes(file);
1098 else
1099 host_err = vfs_fsync_range(file, offset, offset+*cnt, 0);
1102 out_nfserr:
1103 dprintk("nfsd: write complete host_err=%d\n", host_err);
1104 if (host_err >= 0)
1105 err = 0;
1106 else
1107 err = nfserrno(host_err);
1108 return err;
1112 * Read data from a file. count must contain the requested read count
1113 * on entry. On return, *count contains the number of bytes actually read.
1114 * N.B. After this call fhp needs an fh_put
1116 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1117 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1119 struct file *file;
1120 struct inode *inode;
1121 struct raparms *ra;
1122 __be32 err;
1124 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1125 if (err)
1126 return err;
1128 inode = file_inode(file);
1130 /* Get readahead parameters */
1131 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1133 if (ra && ra->p_set)
1134 file->f_ra = ra->p_ra;
1136 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1138 /* Write back readahead params */
1139 if (ra) {
1140 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1141 spin_lock(&rab->pb_lock);
1142 ra->p_ra = file->f_ra;
1143 ra->p_set = 1;
1144 ra->p_count--;
1145 spin_unlock(&rab->pb_lock);
1148 nfsd_close(file);
1149 return err;
1152 /* As above, but use the provided file descriptor. */
1153 __be32
1154 nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1155 loff_t offset, struct kvec *vec, int vlen,
1156 unsigned long *count)
1158 __be32 err;
1160 if (file) {
1161 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1162 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
1163 if (err)
1164 goto out;
1165 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1166 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1167 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
1168 out:
1169 return err;
1173 * Write data to a file.
1174 * The stable flag requests synchronous writes.
1175 * N.B. After this call fhp needs an fh_put
1177 __be32
1178 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1179 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1180 int *stablep)
1182 __be32 err = 0;
1184 if (file) {
1185 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1186 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1187 if (err)
1188 goto out;
1189 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1190 stablep);
1191 } else {
1192 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1193 if (err)
1194 goto out;
1196 if (cnt)
1197 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1198 cnt, stablep);
1199 nfsd_close(file);
1201 out:
1202 return err;
1205 #ifdef CONFIG_NFSD_V3
1207 * Commit all pending writes to stable storage.
1209 * Note: we only guarantee that data that lies within the range specified
1210 * by the 'offset' and 'count' parameters will be synced.
1212 * Unfortunately we cannot lock the file to make sure we return full WCC
1213 * data to the client, as locking happens lower down in the filesystem.
1215 __be32
1216 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1217 loff_t offset, unsigned long count)
1219 struct file *file;
1220 loff_t end = LLONG_MAX;
1221 __be32 err = nfserr_inval;
1223 if (offset < 0)
1224 goto out;
1225 if (count != 0) {
1226 end = offset + (loff_t)count - 1;
1227 if (end < offset)
1228 goto out;
1231 err = nfsd_open(rqstp, fhp, S_IFREG,
1232 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
1233 if (err)
1234 goto out;
1235 if (EX_ISSYNC(fhp->fh_export)) {
1236 int err2 = vfs_fsync_range(file, offset, end, 0);
1238 if (err2 != -EINVAL)
1239 err = nfserrno(err2);
1240 else
1241 err = nfserr_notsupp;
1244 nfsd_close(file);
1245 out:
1246 return err;
1248 #endif /* CONFIG_NFSD_V3 */
1250 static __be32
1251 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1252 struct iattr *iap)
1255 * Mode has already been set earlier in create:
1257 iap->ia_valid &= ~ATTR_MODE;
1259 * Setting uid/gid works only for root. Irix appears to
1260 * send along the gid on create when it tries to implement
1261 * setgid directories via NFS:
1263 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1264 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1265 if (iap->ia_valid)
1266 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1267 return 0;
1270 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1271 * setting size to 0 may fail for some specific file systems by the permission
1272 * checking which requires WRITE permission but the mode is 000.
1273 * we ignore the resizing(to 0) on the just new created file, since the size is
1274 * 0 after file created.
1276 * call this only after vfs_create() is called.
1277 * */
1278 static void
1279 nfsd_check_ignore_resizing(struct iattr *iap)
1281 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1282 iap->ia_valid &= ~ATTR_SIZE;
1286 * Create a file (regular, directory, device, fifo); UNIX sockets
1287 * not yet implemented.
1288 * If the response fh has been verified, the parent directory should
1289 * already be locked. Note that the parent directory is left locked.
1291 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1293 __be32
1294 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1295 char *fname, int flen, struct iattr *iap,
1296 int type, dev_t rdev, struct svc_fh *resfhp)
1298 struct dentry *dentry, *dchild = NULL;
1299 struct inode *dirp;
1300 __be32 err;
1301 __be32 err2;
1302 int host_err;
1304 err = nfserr_perm;
1305 if (!flen)
1306 goto out;
1307 err = nfserr_exist;
1308 if (isdotent(fname, flen))
1309 goto out;
1311 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1312 if (err)
1313 goto out;
1315 dentry = fhp->fh_dentry;
1316 dirp = dentry->d_inode;
1318 err = nfserr_notdir;
1319 if (!dirp->i_op->lookup)
1320 goto out;
1322 * Check whether the response file handle has been verified yet.
1323 * If it has, the parent directory should already be locked.
1325 if (!resfhp->fh_dentry) {
1326 host_err = fh_want_write(fhp);
1327 if (host_err)
1328 goto out_nfserr;
1330 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1331 fh_lock_nested(fhp, I_MUTEX_PARENT);
1332 dchild = lookup_one_len(fname, dentry, flen);
1333 host_err = PTR_ERR(dchild);
1334 if (IS_ERR(dchild))
1335 goto out_nfserr;
1336 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1337 if (err)
1338 goto out;
1339 } else {
1340 /* called from nfsd_proc_create */
1341 dchild = dget(resfhp->fh_dentry);
1342 if (!fhp->fh_locked) {
1343 /* not actually possible */
1344 printk(KERN_ERR
1345 "nfsd_create: parent %s/%s not locked!\n",
1346 dentry->d_parent->d_name.name,
1347 dentry->d_name.name);
1348 err = nfserr_io;
1349 goto out;
1353 * Make sure the child dentry is still negative ...
1355 err = nfserr_exist;
1356 if (dchild->d_inode) {
1357 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1358 dentry->d_name.name, dchild->d_name.name);
1359 goto out;
1362 if (!(iap->ia_valid & ATTR_MODE))
1363 iap->ia_mode = 0;
1364 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1366 err = nfserr_inval;
1367 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1368 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1369 type);
1370 goto out;
1374 * Get the dir op function pointer.
1376 err = 0;
1377 host_err = 0;
1378 switch (type) {
1379 case S_IFREG:
1380 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1381 if (!host_err)
1382 nfsd_check_ignore_resizing(iap);
1383 break;
1384 case S_IFDIR:
1385 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1386 break;
1387 case S_IFCHR:
1388 case S_IFBLK:
1389 case S_IFIFO:
1390 case S_IFSOCK:
1391 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1392 break;
1394 if (host_err < 0)
1395 goto out_nfserr;
1397 err = nfsd_create_setattr(rqstp, resfhp, iap);
1400 * nfsd_setattr already committed the child. Transactional filesystems
1401 * had a chance to commit changes for both parent and child
1402 * simultaneously making the following commit_metadata a noop.
1404 err2 = nfserrno(commit_metadata(fhp));
1405 if (err2)
1406 err = err2;
1408 * Update the file handle to get the new inode info.
1410 if (!err)
1411 err = fh_update(resfhp);
1412 out:
1413 if (dchild && !IS_ERR(dchild))
1414 dput(dchild);
1415 return err;
1417 out_nfserr:
1418 err = nfserrno(host_err);
1419 goto out;
1422 #ifdef CONFIG_NFSD_V3
1424 static inline int nfsd_create_is_exclusive(int createmode)
1426 return createmode == NFS3_CREATE_EXCLUSIVE
1427 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1431 * NFSv3 and NFSv4 version of nfsd_create
1433 __be32
1434 do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1435 char *fname, int flen, struct iattr *iap,
1436 struct svc_fh *resfhp, int createmode, u32 *verifier,
1437 bool *truncp, bool *created)
1439 struct dentry *dentry, *dchild = NULL;
1440 struct inode *dirp;
1441 __be32 err;
1442 int host_err;
1443 __u32 v_mtime=0, v_atime=0;
1445 err = nfserr_perm;
1446 if (!flen)
1447 goto out;
1448 err = nfserr_exist;
1449 if (isdotent(fname, flen))
1450 goto out;
1451 if (!(iap->ia_valid & ATTR_MODE))
1452 iap->ia_mode = 0;
1453 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1454 if (err)
1455 goto out;
1457 dentry = fhp->fh_dentry;
1458 dirp = dentry->d_inode;
1460 /* Get all the sanity checks out of the way before
1461 * we lock the parent. */
1462 err = nfserr_notdir;
1463 if (!dirp->i_op->lookup)
1464 goto out;
1466 host_err = fh_want_write(fhp);
1467 if (host_err)
1468 goto out_nfserr;
1470 fh_lock_nested(fhp, I_MUTEX_PARENT);
1473 * Compose the response file handle.
1475 dchild = lookup_one_len(fname, dentry, flen);
1476 host_err = PTR_ERR(dchild);
1477 if (IS_ERR(dchild))
1478 goto out_nfserr;
1480 /* If file doesn't exist, check for permissions to create one */
1481 if (!dchild->d_inode) {
1482 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1483 if (err)
1484 goto out;
1487 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1488 if (err)
1489 goto out;
1491 if (nfsd_create_is_exclusive(createmode)) {
1492 /* solaris7 gets confused (bugid 4218508) if these have
1493 * the high bit set, so just clear the high bits. If this is
1494 * ever changed to use different attrs for storing the
1495 * verifier, then do_open_lookup() will also need to be fixed
1496 * accordingly.
1498 v_mtime = verifier[0]&0x7fffffff;
1499 v_atime = verifier[1]&0x7fffffff;
1502 if (dchild->d_inode) {
1503 err = 0;
1505 switch (createmode) {
1506 case NFS3_CREATE_UNCHECKED:
1507 if (! S_ISREG(dchild->d_inode->i_mode))
1508 goto out;
1509 else if (truncp) {
1510 /* in nfsv4, we need to treat this case a little
1511 * differently. we don't want to truncate the
1512 * file now; this would be wrong if the OPEN
1513 * fails for some other reason. furthermore,
1514 * if the size is nonzero, we should ignore it
1515 * according to spec!
1517 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1519 else {
1520 iap->ia_valid &= ATTR_SIZE;
1521 goto set_attr;
1523 break;
1524 case NFS3_CREATE_EXCLUSIVE:
1525 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1526 && dchild->d_inode->i_atime.tv_sec == v_atime
1527 && dchild->d_inode->i_size == 0 ) {
1528 if (created)
1529 *created = 1;
1530 break;
1532 case NFS4_CREATE_EXCLUSIVE4_1:
1533 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1534 && dchild->d_inode->i_atime.tv_sec == v_atime
1535 && dchild->d_inode->i_size == 0 ) {
1536 if (created)
1537 *created = 1;
1538 goto set_attr;
1540 /* fallthru */
1541 case NFS3_CREATE_GUARDED:
1542 err = nfserr_exist;
1544 fh_drop_write(fhp);
1545 goto out;
1548 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1549 if (host_err < 0) {
1550 fh_drop_write(fhp);
1551 goto out_nfserr;
1553 if (created)
1554 *created = 1;
1556 nfsd_check_ignore_resizing(iap);
1558 if (nfsd_create_is_exclusive(createmode)) {
1559 /* Cram the verifier into atime/mtime */
1560 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1561 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1562 /* XXX someone who knows this better please fix it for nsec */
1563 iap->ia_mtime.tv_sec = v_mtime;
1564 iap->ia_atime.tv_sec = v_atime;
1565 iap->ia_mtime.tv_nsec = 0;
1566 iap->ia_atime.tv_nsec = 0;
1569 set_attr:
1570 err = nfsd_create_setattr(rqstp, resfhp, iap);
1573 * nfsd_setattr already committed the child (and possibly also the parent).
1575 if (!err)
1576 err = nfserrno(commit_metadata(fhp));
1579 * Update the filehandle to get the new inode info.
1581 if (!err)
1582 err = fh_update(resfhp);
1584 out:
1585 fh_unlock(fhp);
1586 if (dchild && !IS_ERR(dchild))
1587 dput(dchild);
1588 fh_drop_write(fhp);
1589 return err;
1591 out_nfserr:
1592 err = nfserrno(host_err);
1593 goto out;
1595 #endif /* CONFIG_NFSD_V3 */
1598 * Read a symlink. On entry, *lenp must contain the maximum path length that
1599 * fits into the buffer. On return, it contains the true length.
1600 * N.B. After this call fhp needs an fh_put
1602 __be32
1603 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1605 struct inode *inode;
1606 mm_segment_t oldfs;
1607 __be32 err;
1608 int host_err;
1609 struct path path;
1611 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1612 if (err)
1613 goto out;
1615 path.mnt = fhp->fh_export->ex_path.mnt;
1616 path.dentry = fhp->fh_dentry;
1617 inode = path.dentry->d_inode;
1619 err = nfserr_inval;
1620 if (!inode->i_op->readlink)
1621 goto out;
1623 touch_atime(&path);
1624 /* N.B. Why does this call need a get_fs()??
1625 * Remove the set_fs and watch the fireworks:-) --okir
1628 oldfs = get_fs(); set_fs(KERNEL_DS);
1629 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
1630 set_fs(oldfs);
1632 if (host_err < 0)
1633 goto out_nfserr;
1634 *lenp = host_err;
1635 err = 0;
1636 out:
1637 return err;
1639 out_nfserr:
1640 err = nfserrno(host_err);
1641 goto out;
1645 * Create a symlink and look up its inode
1646 * N.B. After this call _both_ fhp and resfhp need an fh_put
1648 __be32
1649 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1650 char *fname, int flen,
1651 char *path, int plen,
1652 struct svc_fh *resfhp,
1653 struct iattr *iap)
1655 struct dentry *dentry, *dnew;
1656 __be32 err, cerr;
1657 int host_err;
1659 err = nfserr_noent;
1660 if (!flen || !plen)
1661 goto out;
1662 err = nfserr_exist;
1663 if (isdotent(fname, flen))
1664 goto out;
1666 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1667 if (err)
1668 goto out;
1670 host_err = fh_want_write(fhp);
1671 if (host_err)
1672 goto out_nfserr;
1674 fh_lock(fhp);
1675 dentry = fhp->fh_dentry;
1676 dnew = lookup_one_len(fname, dentry, flen);
1677 host_err = PTR_ERR(dnew);
1678 if (IS_ERR(dnew))
1679 goto out_nfserr;
1681 if (unlikely(path[plen] != 0)) {
1682 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1683 if (path_alloced == NULL)
1684 host_err = -ENOMEM;
1685 else {
1686 strncpy(path_alloced, path, plen);
1687 path_alloced[plen] = 0;
1688 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1689 kfree(path_alloced);
1691 } else
1692 host_err = vfs_symlink(dentry->d_inode, dnew, path);
1693 err = nfserrno(host_err);
1694 if (!err)
1695 err = nfserrno(commit_metadata(fhp));
1696 fh_unlock(fhp);
1698 fh_drop_write(fhp);
1700 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1701 dput(dnew);
1702 if (err==0) err = cerr;
1703 out:
1704 return err;
1706 out_nfserr:
1707 err = nfserrno(host_err);
1708 goto out;
1712 * Create a hardlink
1713 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1715 __be32
1716 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1717 char *name, int len, struct svc_fh *tfhp)
1719 struct dentry *ddir, *dnew, *dold;
1720 struct inode *dirp;
1721 __be32 err;
1722 int host_err;
1724 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1725 if (err)
1726 goto out;
1727 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1728 if (err)
1729 goto out;
1730 err = nfserr_isdir;
1731 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1732 goto out;
1733 err = nfserr_perm;
1734 if (!len)
1735 goto out;
1736 err = nfserr_exist;
1737 if (isdotent(name, len))
1738 goto out;
1740 host_err = fh_want_write(tfhp);
1741 if (host_err) {
1742 err = nfserrno(host_err);
1743 goto out;
1746 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1747 ddir = ffhp->fh_dentry;
1748 dirp = ddir->d_inode;
1750 dnew = lookup_one_len(name, ddir, len);
1751 host_err = PTR_ERR(dnew);
1752 if (IS_ERR(dnew))
1753 goto out_nfserr;
1755 dold = tfhp->fh_dentry;
1757 err = nfserr_noent;
1758 if (!dold->d_inode)
1759 goto out_dput;
1760 host_err = nfsd_break_lease(dold->d_inode);
1761 if (host_err) {
1762 err = nfserrno(host_err);
1763 goto out_dput;
1765 host_err = vfs_link(dold, dirp, dnew);
1766 if (!host_err) {
1767 err = nfserrno(commit_metadata(ffhp));
1768 if (!err)
1769 err = nfserrno(commit_metadata(tfhp));
1770 } else {
1771 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1772 err = nfserr_acces;
1773 else
1774 err = nfserrno(host_err);
1776 out_dput:
1777 dput(dnew);
1778 out_unlock:
1779 fh_unlock(ffhp);
1780 fh_drop_write(tfhp);
1781 out:
1782 return err;
1784 out_nfserr:
1785 err = nfserrno(host_err);
1786 goto out_unlock;
1790 * Rename a file
1791 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1793 __be32
1794 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1795 struct svc_fh *tfhp, char *tname, int tlen)
1797 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1798 struct inode *fdir, *tdir;
1799 __be32 err;
1800 int host_err;
1802 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1803 if (err)
1804 goto out;
1805 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1806 if (err)
1807 goto out;
1809 fdentry = ffhp->fh_dentry;
1810 fdir = fdentry->d_inode;
1812 tdentry = tfhp->fh_dentry;
1813 tdir = tdentry->d_inode;
1815 err = nfserr_perm;
1816 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1817 goto out;
1819 host_err = fh_want_write(ffhp);
1820 if (host_err) {
1821 err = nfserrno(host_err);
1822 goto out;
1825 /* cannot use fh_lock as we need deadlock protective ordering
1826 * so do it by hand */
1827 trap = lock_rename(tdentry, fdentry);
1828 ffhp->fh_locked = tfhp->fh_locked = 1;
1829 fill_pre_wcc(ffhp);
1830 fill_pre_wcc(tfhp);
1832 odentry = lookup_one_len(fname, fdentry, flen);
1833 host_err = PTR_ERR(odentry);
1834 if (IS_ERR(odentry))
1835 goto out_nfserr;
1837 host_err = -ENOENT;
1838 if (!odentry->d_inode)
1839 goto out_dput_old;
1840 host_err = -EINVAL;
1841 if (odentry == trap)
1842 goto out_dput_old;
1844 ndentry = lookup_one_len(tname, tdentry, tlen);
1845 host_err = PTR_ERR(ndentry);
1846 if (IS_ERR(ndentry))
1847 goto out_dput_old;
1848 host_err = -ENOTEMPTY;
1849 if (ndentry == trap)
1850 goto out_dput_new;
1852 host_err = -EXDEV;
1853 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1854 goto out_dput_new;
1855 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1856 goto out_dput_new;
1858 host_err = nfsd_break_lease(odentry->d_inode);
1859 if (host_err)
1860 goto out_dput_new;
1861 if (ndentry->d_inode) {
1862 host_err = nfsd_break_lease(ndentry->d_inode);
1863 if (host_err)
1864 goto out_dput_new;
1866 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
1867 if (!host_err) {
1868 host_err = commit_metadata(tfhp);
1869 if (!host_err)
1870 host_err = commit_metadata(ffhp);
1872 out_dput_new:
1873 dput(ndentry);
1874 out_dput_old:
1875 dput(odentry);
1876 out_nfserr:
1877 err = nfserrno(host_err);
1879 /* we cannot reply on fh_unlock on the two filehandles,
1880 * as that would do the wrong thing if the two directories
1881 * were the same, so again we do it by hand
1883 fill_post_wcc(ffhp);
1884 fill_post_wcc(tfhp);
1885 unlock_rename(tdentry, fdentry);
1886 ffhp->fh_locked = tfhp->fh_locked = 0;
1887 fh_drop_write(ffhp);
1889 out:
1890 return err;
1894 * Unlink a file or directory
1895 * N.B. After this call fhp needs an fh_put
1897 __be32
1898 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1899 char *fname, int flen)
1901 struct dentry *dentry, *rdentry;
1902 struct inode *dirp;
1903 __be32 err;
1904 int host_err;
1906 err = nfserr_acces;
1907 if (!flen || isdotent(fname, flen))
1908 goto out;
1909 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1910 if (err)
1911 goto out;
1913 host_err = fh_want_write(fhp);
1914 if (host_err)
1915 goto out_nfserr;
1917 fh_lock_nested(fhp, I_MUTEX_PARENT);
1918 dentry = fhp->fh_dentry;
1919 dirp = dentry->d_inode;
1921 rdentry = lookup_one_len(fname, dentry, flen);
1922 host_err = PTR_ERR(rdentry);
1923 if (IS_ERR(rdentry))
1924 goto out_nfserr;
1926 if (!rdentry->d_inode) {
1927 dput(rdentry);
1928 err = nfserr_noent;
1929 goto out;
1932 if (!type)
1933 type = rdentry->d_inode->i_mode & S_IFMT;
1935 host_err = nfsd_break_lease(rdentry->d_inode);
1936 if (host_err)
1937 goto out_put;
1938 if (type != S_IFDIR)
1939 host_err = vfs_unlink(dirp, rdentry);
1940 else
1941 host_err = vfs_rmdir(dirp, rdentry);
1942 if (!host_err)
1943 host_err = commit_metadata(fhp);
1944 out_put:
1945 dput(rdentry);
1947 out_nfserr:
1948 err = nfserrno(host_err);
1949 out:
1950 return err;
1954 * We do this buffering because we must not call back into the file
1955 * system's ->lookup() method from the filldir callback. That may well
1956 * deadlock a number of file systems.
1958 * This is based heavily on the implementation of same in XFS.
1960 struct buffered_dirent {
1961 u64 ino;
1962 loff_t offset;
1963 int namlen;
1964 unsigned int d_type;
1965 char name[];
1968 struct readdir_data {
1969 struct dir_context ctx;
1970 char *dirent;
1971 size_t used;
1972 int full;
1975 static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1976 loff_t offset, u64 ino, unsigned int d_type)
1978 struct readdir_data *buf = __buf;
1979 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1980 unsigned int reclen;
1982 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1983 if (buf->used + reclen > PAGE_SIZE) {
1984 buf->full = 1;
1985 return -EINVAL;
1988 de->namlen = namlen;
1989 de->offset = offset;
1990 de->ino = ino;
1991 de->d_type = d_type;
1992 memcpy(de->name, name, namlen);
1993 buf->used += reclen;
1995 return 0;
1998 static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1999 struct readdir_cd *cdp, loff_t *offsetp)
2001 struct buffered_dirent *de;
2002 int host_err;
2003 int size;
2004 loff_t offset;
2005 struct readdir_data buf = {
2006 .ctx.actor = nfsd_buffered_filldir,
2007 .dirent = (void *)__get_free_page(GFP_KERNEL)
2010 if (!buf.dirent)
2011 return nfserrno(-ENOMEM);
2013 offset = *offsetp;
2015 while (1) {
2016 struct inode *dir_inode = file_inode(file);
2017 unsigned int reclen;
2019 cdp->err = nfserr_eof; /* will be cleared on successful read */
2020 buf.used = 0;
2021 buf.full = 0;
2023 host_err = iterate_dir(file, &buf.ctx);
2024 if (buf.full)
2025 host_err = 0;
2027 if (host_err < 0)
2028 break;
2030 size = buf.used;
2032 if (!size)
2033 break;
2036 * Various filldir functions may end up calling back into
2037 * lookup_one_len() and the file system's ->lookup() method.
2038 * These expect i_mutex to be held, as it would within readdir.
2040 host_err = mutex_lock_killable(&dir_inode->i_mutex);
2041 if (host_err)
2042 break;
2044 de = (struct buffered_dirent *)buf.dirent;
2045 while (size > 0) {
2046 offset = de->offset;
2048 if (func(cdp, de->name, de->namlen, de->offset,
2049 de->ino, de->d_type))
2050 break;
2052 if (cdp->err != nfs_ok)
2053 break;
2055 reclen = ALIGN(sizeof(*de) + de->namlen,
2056 sizeof(u64));
2057 size -= reclen;
2058 de = (struct buffered_dirent *)((char *)de + reclen);
2060 mutex_unlock(&dir_inode->i_mutex);
2061 if (size > 0) /* We bailed out early */
2062 break;
2064 offset = vfs_llseek(file, 0, SEEK_CUR);
2067 free_page((unsigned long)(buf.dirent));
2069 if (host_err)
2070 return nfserrno(host_err);
2072 *offsetp = offset;
2073 return cdp->err;
2077 * Read entries from a directory.
2078 * The NFSv3/4 verifier we ignore for now.
2080 __be32
2081 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2082 struct readdir_cd *cdp, filldir_t func)
2084 __be32 err;
2085 struct file *file;
2086 loff_t offset = *offsetp;
2087 int may_flags = NFSD_MAY_READ;
2089 /* NFSv2 only supports 32 bit cookies */
2090 if (rqstp->rq_vers > 2)
2091 may_flags |= NFSD_MAY_64BIT_COOKIE;
2093 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
2094 if (err)
2095 goto out;
2097 offset = vfs_llseek(file, offset, SEEK_SET);
2098 if (offset < 0) {
2099 err = nfserrno((int)offset);
2100 goto out_close;
2103 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
2105 if (err == nfserr_eof || err == nfserr_toosmall)
2106 err = nfs_ok; /* can still be found in ->err */
2107 out_close:
2108 nfsd_close(file);
2109 out:
2110 return err;
2114 * Get file system stats
2115 * N.B. After this call fhp needs an fh_put
2117 __be32
2118 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2120 __be32 err;
2122 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2123 if (!err) {
2124 struct path path = {
2125 .mnt = fhp->fh_export->ex_path.mnt,
2126 .dentry = fhp->fh_dentry,
2128 if (vfs_statfs(&path, stat))
2129 err = nfserr_io;
2131 return err;
2134 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2136 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2140 * Check for a user's access permissions to this inode.
2142 __be32
2143 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2144 struct dentry *dentry, int acc)
2146 struct inode *inode = dentry->d_inode;
2147 int err;
2149 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2150 return 0;
2151 #if 0
2152 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2153 acc,
2154 (acc & NFSD_MAY_READ)? " read" : "",
2155 (acc & NFSD_MAY_WRITE)? " write" : "",
2156 (acc & NFSD_MAY_EXEC)? " exec" : "",
2157 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2158 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2159 (acc & NFSD_MAY_LOCK)? " lock" : "",
2160 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2161 inode->i_mode,
2162 IS_IMMUTABLE(inode)? " immut" : "",
2163 IS_APPEND(inode)? " append" : "",
2164 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2165 dprintk(" owner %d/%d user %d/%d\n",
2166 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2167 #endif
2169 /* Normally we reject any write/sattr etc access on a read-only file
2170 * system. But if it is IRIX doing check on write-access for a
2171 * device special file, we ignore rofs.
2173 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2174 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2175 if (exp_rdonly(rqstp, exp) ||
2176 __mnt_is_readonly(exp->ex_path.mnt))
2177 return nfserr_rofs;
2178 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2179 return nfserr_perm;
2181 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2182 return nfserr_perm;
2184 if (acc & NFSD_MAY_LOCK) {
2185 /* If we cannot rely on authentication in NLM requests,
2186 * just allow locks, otherwise require read permission, or
2187 * ownership
2189 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2190 return 0;
2191 else
2192 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2195 * The file owner always gets access permission for accesses that
2196 * would normally be checked at open time. This is to make
2197 * file access work even when the client has done a fchmod(fd, 0).
2199 * However, `cp foo bar' should fail nevertheless when bar is
2200 * readonly. A sensible way to do this might be to reject all
2201 * attempts to truncate a read-only file, because a creat() call
2202 * always implies file truncation.
2203 * ... but this isn't really fair. A process may reasonably call
2204 * ftruncate on an open file descriptor on a file with perm 000.
2205 * We must trust the client to do permission checking - using "ACCESS"
2206 * with NFSv3.
2208 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2209 uid_eq(inode->i_uid, current_fsuid()))
2210 return 0;
2212 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2213 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2215 /* Allow read access to binaries even when mode 111 */
2216 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2217 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2218 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2219 err = inode_permission(inode, MAY_EXEC);
2221 return err? nfserrno(err) : 0;
2224 void
2225 nfsd_racache_shutdown(void)
2227 struct raparms *raparm, *last_raparm;
2228 unsigned int i;
2230 dprintk("nfsd: freeing readahead buffers.\n");
2232 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2233 raparm = raparm_hash[i].pb_head;
2234 while(raparm) {
2235 last_raparm = raparm;
2236 raparm = raparm->p_next;
2237 kfree(last_raparm);
2239 raparm_hash[i].pb_head = NULL;
2243 * Initialize readahead param cache
2246 nfsd_racache_init(int cache_size)
2248 int i;
2249 int j = 0;
2250 int nperbucket;
2251 struct raparms **raparm = NULL;
2254 if (raparm_hash[0].pb_head)
2255 return 0;
2256 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2257 if (nperbucket < 2)
2258 nperbucket = 2;
2259 cache_size = nperbucket * RAPARM_HASH_SIZE;
2261 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2263 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2264 spin_lock_init(&raparm_hash[i].pb_lock);
2266 raparm = &raparm_hash[i].pb_head;
2267 for (j = 0; j < nperbucket; j++) {
2268 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2269 if (!*raparm)
2270 goto out_nomem;
2271 raparm = &(*raparm)->p_next;
2273 *raparm = NULL;
2276 nfsdstats.ra_size = cache_size;
2277 return 0;
2279 out_nomem:
2280 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2281 nfsd_racache_shutdown();
2282 return -ENOMEM;
2285 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2286 struct posix_acl *
2287 nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2289 struct inode *inode = fhp->fh_dentry->d_inode;
2290 char *name;
2291 void *value = NULL;
2292 ssize_t size;
2293 struct posix_acl *acl;
2295 if (!IS_POSIXACL(inode))
2296 return ERR_PTR(-EOPNOTSUPP);
2298 switch (type) {
2299 case ACL_TYPE_ACCESS:
2300 name = POSIX_ACL_XATTR_ACCESS;
2301 break;
2302 case ACL_TYPE_DEFAULT:
2303 name = POSIX_ACL_XATTR_DEFAULT;
2304 break;
2305 default:
2306 return ERR_PTR(-EOPNOTSUPP);
2309 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2310 if (size < 0)
2311 return ERR_PTR(size);
2313 acl = posix_acl_from_xattr(&init_user_ns, value, size);
2314 kfree(value);
2315 return acl;
2319 nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2321 struct inode *inode = fhp->fh_dentry->d_inode;
2322 char *name;
2323 void *value = NULL;
2324 size_t size;
2325 int error;
2327 if (!IS_POSIXACL(inode) ||
2328 !inode->i_op->setxattr || !inode->i_op->removexattr)
2329 return -EOPNOTSUPP;
2330 switch(type) {
2331 case ACL_TYPE_ACCESS:
2332 name = POSIX_ACL_XATTR_ACCESS;
2333 break;
2334 case ACL_TYPE_DEFAULT:
2335 name = POSIX_ACL_XATTR_DEFAULT;
2336 break;
2337 default:
2338 return -EOPNOTSUPP;
2341 if (acl && acl->a_count) {
2342 size = posix_acl_xattr_size(acl->a_count);
2343 value = kmalloc(size, GFP_KERNEL);
2344 if (!value)
2345 return -ENOMEM;
2346 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
2347 if (error < 0)
2348 goto getout;
2349 size = error;
2350 } else
2351 size = 0;
2353 error = fh_want_write(fhp);
2354 if (error)
2355 goto getout;
2356 if (size)
2357 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
2358 else {
2359 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2360 error = 0;
2361 else {
2362 error = vfs_removexattr(fhp->fh_dentry, name);
2363 if (error == -ENODATA)
2364 error = 0;
2367 fh_drop_write(fhp);
2369 getout:
2370 kfree(value);
2371 return error;
2373 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */