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>
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>
34 #endif /* CONFIG_NFSD_V3 */
39 #endif /* CONFIG_NFSD_V4 */
44 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
48 * This is a cache of readahead params that help us choose the proper
49 * readahead strategy. Initially, we set all readahead parameters to 0
50 * and let the VFS handle things.
51 * If you increase the number of cached files very much, you'll need to
52 * add a hash table here.
55 struct raparms *p_next;
60 struct file_ra_state p_ra;
61 unsigned int p_hindex;
64 struct raparm_hbucket {
65 struct raparms *pb_head;
67 } ____cacheline_aligned_in_smp;
69 #define RAPARM_HASH_BITS 4
70 #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
71 #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
72 static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
75 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
77 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
78 * or nfs_ok having possibly changed *dpp and *expp
81 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
82 struct svc_export **expp)
84 struct svc_export *exp = *expp, *exp2 = NULL;
85 struct dentry *dentry = *dpp;
86 struct path path = {.mnt = mntget(exp->ex_path.mnt),
87 .dentry = dget(dentry)};
90 err = follow_down(&path);
94 exp2 = rqst_exp_get_by_name(rqstp, &path);
98 * We normally allow NFS clients to continue
99 * "underneath" a mountpoint that is not exported.
100 * The exception is V4ROOT, where no traversal is ever
101 * allowed without an explicit export of the new
104 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
109 if (nfsd_v4client(rqstp) ||
110 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
111 /* successfully crossed mount point */
113 * This is subtle: path.dentry is *not* on path.mnt
114 * at this point. The only reason we are safe is that
115 * original mnt is pinned down by exp, so we should
116 * put path *before* putting exp
119 path.dentry = dentry;
129 static void follow_to_parent(struct path *path)
133 while (path->dentry == path->mnt->mnt_root && follow_up(path))
135 dp = dget_parent(path->dentry);
140 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
142 struct svc_export *exp2;
143 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
144 .dentry = dget(dparent)};
146 follow_to_parent(&path);
148 exp2 = rqst_exp_parent(rqstp, &path);
149 if (PTR_ERR(exp2) == -ENOENT) {
150 *dentryp = dget(dparent);
151 } else if (IS_ERR(exp2)) {
153 return PTR_ERR(exp2);
155 *dentryp = dget(path.dentry);
164 * For nfsd purposes, we treat V4ROOT exports as though there was an
165 * export at *every* directory.
167 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
169 if (d_mountpoint(dentry))
171 if (nfsd4_is_junction(dentry))
173 if (!(exp->ex_flags & NFSEXP_V4ROOT))
175 return dentry->d_inode != NULL;
179 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
180 const char *name, unsigned int len,
181 struct svc_export **exp_ret, struct dentry **dentry_ret)
183 struct svc_export *exp;
184 struct dentry *dparent;
185 struct dentry *dentry;
188 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
190 dparent = fhp->fh_dentry;
191 exp = fhp->fh_export;
194 /* Lookup the name, but don't follow links */
195 if (isdotent(name, len)) {
197 dentry = dget(dparent);
198 else if (dparent != exp->ex_path.dentry)
199 dentry = dget_parent(dparent);
200 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
201 dentry = dget(dparent); /* .. == . just like at / */
203 /* checking mountpoint crossing is very different when stepping up */
204 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
210 dentry = lookup_one_len(name, dparent, len);
211 host_err = PTR_ERR(dentry);
215 * check if we have crossed a mount point ...
217 if (nfsd_mountpoint(dentry, exp)) {
218 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
224 *dentry_ret = dentry;
230 return nfserrno(host_err);
234 * Look up one component of a pathname.
235 * N.B. After this call _both_ fhp and resfh need an fh_put
237 * If the lookup would cross a mountpoint, and the mounted filesystem
238 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
239 * accepted as it stands and the mounted directory is
240 * returned. Otherwise the covered directory is returned.
241 * NOTE: this mountpoint crossing is not supported properly by all
242 * clients and is explicitly disallowed for NFSv3
243 * NeilBrown <neilb@cse.unsw.edu.au>
246 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
247 unsigned int len, struct svc_fh *resfh)
249 struct svc_export *exp;
250 struct dentry *dentry;
253 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
256 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
259 err = check_nfsd_access(exp, rqstp);
263 * Note: we compose the file handle now, but as the
264 * dentry may be negative, it may need to be updated.
266 err = fh_compose(resfh, exp, dentry, fhp);
267 if (!err && !dentry->d_inode)
275 static int nfsd_break_lease(struct inode *inode)
277 if (!S_ISREG(inode->i_mode))
279 return break_lease(inode, O_WRONLY | O_NONBLOCK);
283 * Commit metadata changes to stable storage.
286 commit_metadata(struct svc_fh *fhp)
288 struct inode *inode = fhp->fh_dentry->d_inode;
289 const struct export_operations *export_ops = inode->i_sb->s_export_op;
291 if (!EX_ISSYNC(fhp->fh_export))
294 if (export_ops->commit_metadata)
295 return export_ops->commit_metadata(inode);
296 return sync_inode_metadata(inode, 1);
300 * Go over the attributes and take care of the small differences between
301 * NFS semantics and what Linux expects.
304 nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
307 * NFSv2 does not differentiate between "set-[ac]time-to-now"
308 * which only requires access, and "set-[ac]time-to-X" which
309 * requires ownership.
310 * So if it looks like it might be "set both to the same time which
311 * is close to now", and if inode_change_ok fails, then we
312 * convert to "set to now" instead of "set to explicit time"
314 * We only call inode_change_ok as the last test as technically
315 * it is not an interface that we should be using.
317 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
318 #define MAX_TOUCH_TIME_ERROR (30*60)
319 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
320 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
324 * Now just make sure time is in the right ballpark.
325 * Solaris, at least, doesn't seem to care what the time
326 * request is. We require it be within 30 minutes of now.
328 time_t delta = iap->ia_atime.tv_sec - get_seconds();
331 if (delta < MAX_TOUCH_TIME_ERROR &&
332 inode_change_ok(inode, iap) != 0) {
334 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
335 * This will cause notify_change to set these times
338 iap->ia_valid &= ~BOTH_TIME_SET;
342 /* sanitize the mode change */
343 if (iap->ia_valid & ATTR_MODE) {
344 iap->ia_mode &= S_IALLUGO;
345 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
348 /* Revoke setuid/setgid on chown */
349 if (!S_ISDIR(inode->i_mode) &&
350 (((iap->ia_valid & ATTR_UID) && !uid_eq(iap->ia_uid, inode->i_uid)) ||
351 ((iap->ia_valid & ATTR_GID) && !gid_eq(iap->ia_gid, inode->i_gid)))) {
352 iap->ia_valid |= ATTR_KILL_PRIV;
353 if (iap->ia_valid & ATTR_MODE) {
354 /* we're setting mode too, just clear the s*id bits */
355 iap->ia_mode &= ~S_ISUID;
356 if (iap->ia_mode & S_IXGRP)
357 iap->ia_mode &= ~S_ISGID;
359 /* set ATTR_KILL_* bits and let VFS handle it */
360 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
366 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
369 struct inode *inode = fhp->fh_dentry->d_inode;
372 if (iap->ia_size < inode->i_size) {
375 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
376 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
381 host_err = get_write_access(inode);
385 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
387 goto out_put_write_access;
390 out_put_write_access:
391 put_write_access(inode);
393 return nfserrno(host_err);
397 * Set various file attributes. After this call fhp needs an fh_put.
400 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
401 int check_guard, time_t guardtime)
403 struct dentry *dentry;
405 int accmode = NFSD_MAY_SATTR;
411 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
412 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
413 if (iap->ia_valid & ATTR_SIZE)
417 err = fh_verify(rqstp, fhp, ftype, accmode);
421 dentry = fhp->fh_dentry;
422 inode = dentry->d_inode;
424 /* Ignore any mode updates on symlinks */
425 if (S_ISLNK(inode->i_mode))
426 iap->ia_valid &= ~ATTR_MODE;
431 nfsd_sanitize_attrs(inode, iap);
434 * The size case is special, it changes the file in addition to the
437 if (iap->ia_valid & ATTR_SIZE) {
438 err = nfsd_get_write_access(rqstp, fhp, iap);
444 iap->ia_valid |= ATTR_CTIME;
446 if (check_guard && guardtime != inode->i_ctime.tv_sec) {
447 err = nfserr_notsync;
448 goto out_put_write_access;
451 host_err = nfsd_break_lease(inode);
453 goto out_put_write_access_nfserror;
456 host_err = notify_change(dentry, iap);
459 out_put_write_access_nfserror:
460 err = nfserrno(host_err);
461 out_put_write_access:
463 put_write_access(inode);
465 commit_metadata(fhp);
470 #if defined(CONFIG_NFSD_V2_ACL) || \
471 defined(CONFIG_NFSD_V3_ACL) || \
472 defined(CONFIG_NFSD_V4)
473 static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
478 buflen = vfs_getxattr(dentry, key, NULL, 0);
482 *buf = kmalloc(buflen, GFP_KERNEL);
486 ret = vfs_getxattr(dentry, key, *buf, buflen);
493 #if defined(CONFIG_NFSD_V4)
495 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
502 buflen = posix_acl_xattr_size(pacl->a_count);
503 buf = kmalloc(buflen, GFP_KERNEL);
508 len = posix_acl_to_xattr(&init_user_ns, pacl, buf, buflen);
514 error = vfs_setxattr(dentry, key, buf, len, 0);
521 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
522 struct nfs4_acl *acl)
526 struct dentry *dentry;
528 struct posix_acl *pacl = NULL, *dpacl = NULL;
529 unsigned int flags = 0;
532 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
536 dentry = fhp->fh_dentry;
537 inode = dentry->d_inode;
538 if (S_ISDIR(inode->i_mode))
539 flags = NFS4_ACL_DIR;
541 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
542 if (host_error == -EINVAL) {
543 return nfserr_attrnotsupp;
544 } else if (host_error < 0)
547 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
551 if (S_ISDIR(inode->i_mode))
552 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
555 posix_acl_release(pacl);
556 posix_acl_release(dpacl);
558 if (host_error == -EOPNOTSUPP)
559 return nfserr_attrnotsupp;
561 return nfserrno(host_error);
564 static struct posix_acl *
565 _get_posix_acl(struct dentry *dentry, char *key)
568 struct posix_acl *pacl = NULL;
571 buflen = nfsd_getxattr(dentry, key, &buf);
575 return ERR_PTR(buflen);
577 pacl = posix_acl_from_xattr(&init_user_ns, buf, buflen);
583 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
585 struct inode *inode = dentry->d_inode;
587 struct posix_acl *pacl = NULL, *dpacl = NULL;
588 unsigned int flags = 0;
590 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
591 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
592 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
594 error = PTR_ERR(pacl);
599 if (S_ISDIR(inode->i_mode)) {
600 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
601 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
603 else if (IS_ERR(dpacl)) {
604 error = PTR_ERR(dpacl);
608 flags = NFS4_ACL_DIR;
611 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
613 error = PTR_ERR(*acl);
617 posix_acl_release(pacl);
618 posix_acl_release(dpacl);
623 * NFS junction information is stored in an extended attribute.
625 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
628 * nfsd4_is_junction - Test if an object could be an NFS junction
630 * @dentry: object to test
632 * Returns 1 if "dentry" appears to contain NFS junction information.
633 * Otherwise 0 is returned.
635 int nfsd4_is_junction(struct dentry *dentry)
637 struct inode *inode = dentry->d_inode;
641 if (inode->i_mode & S_IXUGO)
643 if (!(inode->i_mode & S_ISVTX))
645 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
649 #endif /* defined(CONFIG_NFSD_V4) */
651 #ifdef CONFIG_NFSD_V3
653 * Check server access rights to a file system object
659 static struct accessmap nfs3_regaccess[] = {
660 { NFS3_ACCESS_READ, NFSD_MAY_READ },
661 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
662 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
663 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
668 static struct accessmap nfs3_diraccess[] = {
669 { NFS3_ACCESS_READ, NFSD_MAY_READ },
670 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
671 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
672 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
673 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
678 static struct accessmap nfs3_anyaccess[] = {
679 /* Some clients - Solaris 2.6 at least, make an access call
680 * to the server to check for access for things like /dev/null
681 * (which really, the server doesn't care about). So
682 * We provide simple access checking for them, looking
683 * mainly at mode bits, and we make sure to ignore read-only
686 { NFS3_ACCESS_READ, NFSD_MAY_READ },
687 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
688 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
689 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
695 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
697 struct accessmap *map;
698 struct svc_export *export;
699 struct dentry *dentry;
700 u32 query, result = 0, sresult = 0;
703 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
707 export = fhp->fh_export;
708 dentry = fhp->fh_dentry;
710 if (S_ISREG(dentry->d_inode->i_mode))
711 map = nfs3_regaccess;
712 else if (S_ISDIR(dentry->d_inode->i_mode))
713 map = nfs3_diraccess;
715 map = nfs3_anyaccess;
719 for (; map->access; map++) {
720 if (map->access & query) {
723 sresult |= map->access;
725 err2 = nfsd_permission(rqstp, export, dentry, map->how);
728 result |= map->access;
731 /* the following error codes just mean the access was not allowed,
732 * rather than an error occurred */
736 /* simply don't "or" in the access bit. */
746 *supported = sresult;
751 #endif /* CONFIG_NFSD_V3 */
753 static int nfsd_open_break_lease(struct inode *inode, int access)
757 if (access & NFSD_MAY_NOT_BREAK_LEASE)
759 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
760 return break_lease(inode, mode | O_NONBLOCK);
764 * Open an existing file or directory.
765 * The may_flags argument indicates the type of open (read/write/lock)
766 * and additional flags.
767 * N.B. After this call fhp needs an fh_put
770 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
771 int may_flags, struct file **filp)
775 int flags = O_RDONLY|O_LARGEFILE;
779 validate_process_creds();
782 * If we get here, then the client has already done an "open",
783 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
784 * in case a chmod has now revoked permission.
786 * Arguably we should also allow the owner override for
787 * directories, but we never have and it doesn't seem to have
788 * caused anyone a problem. If we were to change this, note
789 * also that our filldir callbacks would need a variant of
790 * lookup_one_len that doesn't check permissions.
793 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
794 err = fh_verify(rqstp, fhp, type, may_flags);
798 path.mnt = fhp->fh_export->ex_path.mnt;
799 path.dentry = fhp->fh_dentry;
800 inode = path.dentry->d_inode;
802 /* Disallow write access to files with the append-only bit set
803 * or any access when mandatory locking enabled
806 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
809 * We must ignore files (but only files) which might have mandatory
810 * locks on them because there is no way to know if the accesser has
813 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
819 host_err = nfsd_open_break_lease(inode, may_flags);
820 if (host_err) /* NOMEM or WOULDBLOCK */
823 if (may_flags & NFSD_MAY_WRITE) {
824 if (may_flags & NFSD_MAY_READ)
825 flags = O_RDWR|O_LARGEFILE;
827 flags = O_WRONLY|O_LARGEFILE;
829 *filp = dentry_open(&path, flags, current_cred());
831 host_err = PTR_ERR(*filp);
834 host_err = ima_file_check(*filp, may_flags);
836 if (may_flags & NFSD_MAY_64BIT_COOKIE)
837 (*filp)->f_mode |= FMODE_64BITHASH;
839 (*filp)->f_mode |= FMODE_32BITHASH;
843 err = nfserrno(host_err);
845 validate_process_creds();
853 nfsd_close(struct file *filp)
859 * Obtain the readahead parameters for the file
860 * specified by (dev, ino).
863 static inline struct raparms *
864 nfsd_get_raparms(dev_t dev, ino_t ino)
866 struct raparms *ra, **rap, **frap = NULL;
869 struct raparm_hbucket *rab;
871 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
872 rab = &raparm_hash[hash];
874 spin_lock(&rab->pb_lock);
875 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
876 if (ra->p_ino == ino && ra->p_dev == dev)
879 if (ra->p_count == 0)
882 depth = nfsdstats.ra_size;
884 spin_unlock(&rab->pb_lock);
894 if (rap != &rab->pb_head) {
896 ra->p_next = rab->pb_head;
900 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
901 spin_unlock(&rab->pb_lock);
906 * Grab and keep cached pages associated with a file in the svc_rqst
907 * so that they can be passed to the network sendmsg/sendpage routines
908 * directly. They will be released after the sending has completed.
911 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
912 struct splice_desc *sd)
914 struct svc_rqst *rqstp = sd->u.data;
915 struct page **pp = rqstp->rq_next_page;
916 struct page *page = buf->page;
921 if (rqstp->rq_res.page_len == 0) {
923 put_page(*rqstp->rq_next_page);
924 *(rqstp->rq_next_page++) = page;
925 rqstp->rq_res.page_base = buf->offset;
926 rqstp->rq_res.page_len = size;
927 } else if (page != pp[-1]) {
929 if (*rqstp->rq_next_page)
930 put_page(*rqstp->rq_next_page);
931 *(rqstp->rq_next_page++) = page;
932 rqstp->rq_res.page_len += size;
934 rqstp->rq_res.page_len += size;
939 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
940 struct splice_desc *sd)
942 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
946 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
947 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
955 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
956 struct splice_desc sd = {
963 rqstp->rq_next_page = rqstp->rq_respages + 1;
964 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
968 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
973 nfsdstats.io_read += host_err;
976 fsnotify_access(file);
978 err = nfserrno(host_err);
982 static void kill_suid(struct dentry *dentry)
985 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
987 mutex_lock(&dentry->d_inode->i_mutex);
988 notify_change(dentry, &ia);
989 mutex_unlock(&dentry->d_inode->i_mutex);
993 * Gathered writes: If another process is currently writing to the file,
994 * there's a high chance this is another nfsd (triggered by a bulk write
995 * from a client's biod). Rather than syncing the file with each write
996 * request, we sleep for 10 msec.
998 * I don't know if this roughly approximates C. Juszak's idea of
999 * gathered writes, but it's a nice and simple solution (IMHO), and it
1002 * Note: we do this only in the NFSv2 case, since v3 and higher have a
1003 * better tool (separate unstable writes and commits) for solving this
1006 static int wait_for_concurrent_writes(struct file *file)
1008 struct inode *inode = file_inode(file);
1009 static ino_t last_ino;
1010 static dev_t last_dev;
1013 if (atomic_read(&inode->i_writecount) > 1
1014 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1015 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1017 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1020 if (inode->i_state & I_DIRTY) {
1021 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1022 err = vfs_fsync(file, 0);
1024 last_ino = inode->i_ino;
1025 last_dev = inode->i_sb->s_dev;
1030 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1031 loff_t offset, struct kvec *vec, int vlen,
1032 unsigned long *cnt, int *stablep)
1034 struct svc_export *exp;
1035 struct dentry *dentry;
1036 struct inode *inode;
1040 int stable = *stablep;
1042 loff_t pos = offset;
1044 dentry = file->f_path.dentry;
1045 inode = dentry->d_inode;
1046 exp = fhp->fh_export;
1048 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1050 if (!EX_ISSYNC(exp))
1053 /* Write the data. */
1054 oldfs = get_fs(); set_fs(KERNEL_DS);
1055 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
1060 nfsdstats.io_write += host_err;
1061 fsnotify_modify(file);
1063 /* clear setuid/setgid flag after write */
1064 if (inode->i_mode & (S_ISUID | S_ISGID))
1069 host_err = wait_for_concurrent_writes(file);
1071 host_err = vfs_fsync_range(file, offset, offset+*cnt, 0);
1075 dprintk("nfsd: write complete host_err=%d\n", host_err);
1079 err = nfserrno(host_err);
1084 * Read data from a file. count must contain the requested read count
1085 * on entry. On return, *count contains the number of bytes actually read.
1086 * N.B. After this call fhp needs an fh_put
1088 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1089 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1092 struct inode *inode;
1096 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1100 inode = file_inode(file);
1102 /* Get readahead parameters */
1103 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1105 if (ra && ra->p_set)
1106 file->f_ra = ra->p_ra;
1108 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1110 /* Write back readahead params */
1112 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1113 spin_lock(&rab->pb_lock);
1114 ra->p_ra = file->f_ra;
1117 spin_unlock(&rab->pb_lock);
1124 /* As above, but use the provided file descriptor. */
1126 nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1127 loff_t offset, struct kvec *vec, int vlen,
1128 unsigned long *count)
1133 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1134 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
1137 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1138 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1139 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
1145 * Write data to a file.
1146 * The stable flag requests synchronous writes.
1147 * N.B. After this call fhp needs an fh_put
1150 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1151 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1157 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1158 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1161 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1164 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1169 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1177 #ifdef CONFIG_NFSD_V3
1179 * Commit all pending writes to stable storage.
1181 * Note: we only guarantee that data that lies within the range specified
1182 * by the 'offset' and 'count' parameters will be synced.
1184 * Unfortunately we cannot lock the file to make sure we return full WCC
1185 * data to the client, as locking happens lower down in the filesystem.
1188 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1189 loff_t offset, unsigned long count)
1192 loff_t end = LLONG_MAX;
1193 __be32 err = nfserr_inval;
1198 end = offset + (loff_t)count - 1;
1203 err = nfsd_open(rqstp, fhp, S_IFREG,
1204 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
1207 if (EX_ISSYNC(fhp->fh_export)) {
1208 int err2 = vfs_fsync_range(file, offset, end, 0);
1210 if (err2 != -EINVAL)
1211 err = nfserrno(err2);
1213 err = nfserr_notsupp;
1220 #endif /* CONFIG_NFSD_V3 */
1223 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1227 * Mode has already been set earlier in create:
1229 iap->ia_valid &= ~ATTR_MODE;
1231 * Setting uid/gid works only for root. Irix appears to
1232 * send along the gid on create when it tries to implement
1233 * setgid directories via NFS:
1235 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1236 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1238 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1242 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1243 * setting size to 0 may fail for some specific file systems by the permission
1244 * checking which requires WRITE permission but the mode is 000.
1245 * we ignore the resizing(to 0) on the just new created file, since the size is
1246 * 0 after file created.
1248 * call this only after vfs_create() is called.
1251 nfsd_check_ignore_resizing(struct iattr *iap)
1253 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1254 iap->ia_valid &= ~ATTR_SIZE;
1258 * Create a file (regular, directory, device, fifo); UNIX sockets
1259 * not yet implemented.
1260 * If the response fh has been verified, the parent directory should
1261 * already be locked. Note that the parent directory is left locked.
1263 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1266 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1267 char *fname, int flen, struct iattr *iap,
1268 int type, dev_t rdev, struct svc_fh *resfhp)
1270 struct dentry *dentry, *dchild = NULL;
1280 if (isdotent(fname, flen))
1283 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1287 dentry = fhp->fh_dentry;
1288 dirp = dentry->d_inode;
1290 err = nfserr_notdir;
1291 if (!dirp->i_op->lookup)
1294 * Check whether the response file handle has been verified yet.
1295 * If it has, the parent directory should already be locked.
1297 if (!resfhp->fh_dentry) {
1298 host_err = fh_want_write(fhp);
1302 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1303 fh_lock_nested(fhp, I_MUTEX_PARENT);
1304 dchild = lookup_one_len(fname, dentry, flen);
1305 host_err = PTR_ERR(dchild);
1308 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1312 /* called from nfsd_proc_create */
1313 dchild = dget(resfhp->fh_dentry);
1314 if (!fhp->fh_locked) {
1315 /* not actually possible */
1317 "nfsd_create: parent %s/%s not locked!\n",
1318 dentry->d_parent->d_name.name,
1319 dentry->d_name.name);
1325 * Make sure the child dentry is still negative ...
1328 if (dchild->d_inode) {
1329 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1330 dentry->d_name.name, dchild->d_name.name);
1334 if (!(iap->ia_valid & ATTR_MODE))
1336 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1339 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1340 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1346 * Get the dir op function pointer.
1352 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1354 nfsd_check_ignore_resizing(iap);
1357 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1363 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1369 err = nfsd_create_setattr(rqstp, resfhp, iap);
1372 * nfsd_setattr already committed the child. Transactional filesystems
1373 * had a chance to commit changes for both parent and child
1374 * simultaneously making the following commit_metadata a noop.
1376 err2 = nfserrno(commit_metadata(fhp));
1380 * Update the file handle to get the new inode info.
1383 err = fh_update(resfhp);
1385 if (dchild && !IS_ERR(dchild))
1390 err = nfserrno(host_err);
1394 #ifdef CONFIG_NFSD_V3
1396 static inline int nfsd_create_is_exclusive(int createmode)
1398 return createmode == NFS3_CREATE_EXCLUSIVE
1399 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1403 * NFSv3 and NFSv4 version of nfsd_create
1406 do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1407 char *fname, int flen, struct iattr *iap,
1408 struct svc_fh *resfhp, int createmode, u32 *verifier,
1409 bool *truncp, bool *created)
1411 struct dentry *dentry, *dchild = NULL;
1415 __u32 v_mtime=0, v_atime=0;
1421 if (isdotent(fname, flen))
1423 if (!(iap->ia_valid & ATTR_MODE))
1425 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1429 dentry = fhp->fh_dentry;
1430 dirp = dentry->d_inode;
1432 /* Get all the sanity checks out of the way before
1433 * we lock the parent. */
1434 err = nfserr_notdir;
1435 if (!dirp->i_op->lookup)
1438 host_err = fh_want_write(fhp);
1442 fh_lock_nested(fhp, I_MUTEX_PARENT);
1445 * Compose the response file handle.
1447 dchild = lookup_one_len(fname, dentry, flen);
1448 host_err = PTR_ERR(dchild);
1452 /* If file doesn't exist, check for permissions to create one */
1453 if (!dchild->d_inode) {
1454 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1459 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1463 if (nfsd_create_is_exclusive(createmode)) {
1464 /* solaris7 gets confused (bugid 4218508) if these have
1465 * the high bit set, so just clear the high bits. If this is
1466 * ever changed to use different attrs for storing the
1467 * verifier, then do_open_lookup() will also need to be fixed
1470 v_mtime = verifier[0]&0x7fffffff;
1471 v_atime = verifier[1]&0x7fffffff;
1474 if (dchild->d_inode) {
1477 switch (createmode) {
1478 case NFS3_CREATE_UNCHECKED:
1479 if (! S_ISREG(dchild->d_inode->i_mode))
1482 /* in nfsv4, we need to treat this case a little
1483 * differently. we don't want to truncate the
1484 * file now; this would be wrong if the OPEN
1485 * fails for some other reason. furthermore,
1486 * if the size is nonzero, we should ignore it
1487 * according to spec!
1489 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1492 iap->ia_valid &= ATTR_SIZE;
1496 case NFS3_CREATE_EXCLUSIVE:
1497 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1498 && dchild->d_inode->i_atime.tv_sec == v_atime
1499 && dchild->d_inode->i_size == 0 ) {
1504 case NFS4_CREATE_EXCLUSIVE4_1:
1505 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1506 && dchild->d_inode->i_atime.tv_sec == v_atime
1507 && dchild->d_inode->i_size == 0 ) {
1513 case NFS3_CREATE_GUARDED:
1520 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1528 nfsd_check_ignore_resizing(iap);
1530 if (nfsd_create_is_exclusive(createmode)) {
1531 /* Cram the verifier into atime/mtime */
1532 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1533 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1534 /* XXX someone who knows this better please fix it for nsec */
1535 iap->ia_mtime.tv_sec = v_mtime;
1536 iap->ia_atime.tv_sec = v_atime;
1537 iap->ia_mtime.tv_nsec = 0;
1538 iap->ia_atime.tv_nsec = 0;
1542 err = nfsd_create_setattr(rqstp, resfhp, iap);
1545 * nfsd_setattr already committed the child (and possibly also the parent).
1548 err = nfserrno(commit_metadata(fhp));
1551 * Update the filehandle to get the new inode info.
1554 err = fh_update(resfhp);
1558 if (dchild && !IS_ERR(dchild))
1564 err = nfserrno(host_err);
1567 #endif /* CONFIG_NFSD_V3 */
1570 * Read a symlink. On entry, *lenp must contain the maximum path length that
1571 * fits into the buffer. On return, it contains the true length.
1572 * N.B. After this call fhp needs an fh_put
1575 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1577 struct inode *inode;
1583 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1587 path.mnt = fhp->fh_export->ex_path.mnt;
1588 path.dentry = fhp->fh_dentry;
1589 inode = path.dentry->d_inode;
1592 if (!inode->i_op->readlink)
1596 /* N.B. Why does this call need a get_fs()??
1597 * Remove the set_fs and watch the fireworks:-) --okir
1600 oldfs = get_fs(); set_fs(KERNEL_DS);
1601 host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
1612 err = nfserrno(host_err);
1617 * Create a symlink and look up its inode
1618 * N.B. After this call _both_ fhp and resfhp need an fh_put
1621 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1622 char *fname, int flen,
1623 char *path, int plen,
1624 struct svc_fh *resfhp,
1627 struct dentry *dentry, *dnew;
1635 if (isdotent(fname, flen))
1638 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1642 host_err = fh_want_write(fhp);
1647 dentry = fhp->fh_dentry;
1648 dnew = lookup_one_len(fname, dentry, flen);
1649 host_err = PTR_ERR(dnew);
1653 if (unlikely(path[plen] != 0)) {
1654 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1655 if (path_alloced == NULL)
1658 strncpy(path_alloced, path, plen);
1659 path_alloced[plen] = 0;
1660 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1661 kfree(path_alloced);
1664 host_err = vfs_symlink(dentry->d_inode, dnew, path);
1665 err = nfserrno(host_err);
1667 err = nfserrno(commit_metadata(fhp));
1672 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1674 if (err==0) err = cerr;
1679 err = nfserrno(host_err);
1685 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1688 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1689 char *name, int len, struct svc_fh *tfhp)
1691 struct dentry *ddir, *dnew, *dold;
1696 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1699 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1703 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1709 if (isdotent(name, len))
1712 host_err = fh_want_write(tfhp);
1714 err = nfserrno(host_err);
1718 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1719 ddir = ffhp->fh_dentry;
1720 dirp = ddir->d_inode;
1722 dnew = lookup_one_len(name, ddir, len);
1723 host_err = PTR_ERR(dnew);
1727 dold = tfhp->fh_dentry;
1732 host_err = nfsd_break_lease(dold->d_inode);
1734 err = nfserrno(host_err);
1737 host_err = vfs_link(dold, dirp, dnew);
1739 err = nfserrno(commit_metadata(ffhp));
1741 err = nfserrno(commit_metadata(tfhp));
1743 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1746 err = nfserrno(host_err);
1752 fh_drop_write(tfhp);
1757 err = nfserrno(host_err);
1763 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1766 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1767 struct svc_fh *tfhp, char *tname, int tlen)
1769 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1770 struct inode *fdir, *tdir;
1774 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1777 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1781 fdentry = ffhp->fh_dentry;
1782 fdir = fdentry->d_inode;
1784 tdentry = tfhp->fh_dentry;
1785 tdir = tdentry->d_inode;
1788 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1791 host_err = fh_want_write(ffhp);
1793 err = nfserrno(host_err);
1797 /* cannot use fh_lock as we need deadlock protective ordering
1798 * so do it by hand */
1799 trap = lock_rename(tdentry, fdentry);
1800 ffhp->fh_locked = tfhp->fh_locked = 1;
1804 odentry = lookup_one_len(fname, fdentry, flen);
1805 host_err = PTR_ERR(odentry);
1806 if (IS_ERR(odentry))
1810 if (!odentry->d_inode)
1813 if (odentry == trap)
1816 ndentry = lookup_one_len(tname, tdentry, tlen);
1817 host_err = PTR_ERR(ndentry);
1818 if (IS_ERR(ndentry))
1820 host_err = -ENOTEMPTY;
1821 if (ndentry == trap)
1825 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1827 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1830 host_err = nfsd_break_lease(odentry->d_inode);
1833 if (ndentry->d_inode) {
1834 host_err = nfsd_break_lease(ndentry->d_inode);
1838 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
1840 host_err = commit_metadata(tfhp);
1842 host_err = commit_metadata(ffhp);
1849 err = nfserrno(host_err);
1851 /* we cannot reply on fh_unlock on the two filehandles,
1852 * as that would do the wrong thing if the two directories
1853 * were the same, so again we do it by hand
1855 fill_post_wcc(ffhp);
1856 fill_post_wcc(tfhp);
1857 unlock_rename(tdentry, fdentry);
1858 ffhp->fh_locked = tfhp->fh_locked = 0;
1859 fh_drop_write(ffhp);
1866 * Unlink a file or directory
1867 * N.B. After this call fhp needs an fh_put
1870 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1871 char *fname, int flen)
1873 struct dentry *dentry, *rdentry;
1879 if (!flen || isdotent(fname, flen))
1881 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1885 host_err = fh_want_write(fhp);
1889 fh_lock_nested(fhp, I_MUTEX_PARENT);
1890 dentry = fhp->fh_dentry;
1891 dirp = dentry->d_inode;
1893 rdentry = lookup_one_len(fname, dentry, flen);
1894 host_err = PTR_ERR(rdentry);
1895 if (IS_ERR(rdentry))
1898 if (!rdentry->d_inode) {
1905 type = rdentry->d_inode->i_mode & S_IFMT;
1907 host_err = nfsd_break_lease(rdentry->d_inode);
1910 if (type != S_IFDIR)
1911 host_err = vfs_unlink(dirp, rdentry);
1913 host_err = vfs_rmdir(dirp, rdentry);
1915 host_err = commit_metadata(fhp);
1920 err = nfserrno(host_err);
1926 * We do this buffering because we must not call back into the file
1927 * system's ->lookup() method from the filldir callback. That may well
1928 * deadlock a number of file systems.
1930 * This is based heavily on the implementation of same in XFS.
1932 struct buffered_dirent {
1936 unsigned int d_type;
1940 struct readdir_data {
1946 static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1947 loff_t offset, u64 ino, unsigned int d_type)
1949 struct readdir_data *buf = __buf;
1950 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1951 unsigned int reclen;
1953 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1954 if (buf->used + reclen > PAGE_SIZE) {
1959 de->namlen = namlen;
1960 de->offset = offset;
1962 de->d_type = d_type;
1963 memcpy(de->name, name, namlen);
1964 buf->used += reclen;
1969 static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1970 struct readdir_cd *cdp, loff_t *offsetp)
1972 struct readdir_data buf;
1973 struct buffered_dirent *de;
1978 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1980 return nfserrno(-ENOMEM);
1985 struct inode *dir_inode = file_inode(file);
1986 unsigned int reclen;
1988 cdp->err = nfserr_eof; /* will be cleared on successful read */
1992 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
2005 * Various filldir functions may end up calling back into
2006 * lookup_one_len() and the file system's ->lookup() method.
2007 * These expect i_mutex to be held, as it would within readdir.
2009 host_err = mutex_lock_killable(&dir_inode->i_mutex);
2013 de = (struct buffered_dirent *)buf.dirent;
2015 offset = de->offset;
2017 if (func(cdp, de->name, de->namlen, de->offset,
2018 de->ino, de->d_type))
2021 if (cdp->err != nfs_ok)
2024 reclen = ALIGN(sizeof(*de) + de->namlen,
2027 de = (struct buffered_dirent *)((char *)de + reclen);
2029 mutex_unlock(&dir_inode->i_mutex);
2030 if (size > 0) /* We bailed out early */
2033 offset = vfs_llseek(file, 0, SEEK_CUR);
2036 free_page((unsigned long)(buf.dirent));
2039 return nfserrno(host_err);
2046 * Read entries from a directory.
2047 * The NFSv3/4 verifier we ignore for now.
2050 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2051 struct readdir_cd *cdp, filldir_t func)
2055 loff_t offset = *offsetp;
2056 int may_flags = NFSD_MAY_READ;
2058 /* NFSv2 only supports 32 bit cookies */
2059 if (rqstp->rq_vers > 2)
2060 may_flags |= NFSD_MAY_64BIT_COOKIE;
2062 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
2066 offset = vfs_llseek(file, offset, SEEK_SET);
2068 err = nfserrno((int)offset);
2072 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
2074 if (err == nfserr_eof || err == nfserr_toosmall)
2075 err = nfs_ok; /* can still be found in ->err */
2083 * Get file system stats
2084 * N.B. After this call fhp needs an fh_put
2087 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2091 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2093 struct path path = {
2094 .mnt = fhp->fh_export->ex_path.mnt,
2095 .dentry = fhp->fh_dentry,
2097 if (vfs_statfs(&path, stat))
2103 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2105 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2109 * Check for a user's access permissions to this inode.
2112 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2113 struct dentry *dentry, int acc)
2115 struct inode *inode = dentry->d_inode;
2118 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2121 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2123 (acc & NFSD_MAY_READ)? " read" : "",
2124 (acc & NFSD_MAY_WRITE)? " write" : "",
2125 (acc & NFSD_MAY_EXEC)? " exec" : "",
2126 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2127 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2128 (acc & NFSD_MAY_LOCK)? " lock" : "",
2129 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2131 IS_IMMUTABLE(inode)? " immut" : "",
2132 IS_APPEND(inode)? " append" : "",
2133 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2134 dprintk(" owner %d/%d user %d/%d\n",
2135 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2138 /* Normally we reject any write/sattr etc access on a read-only file
2139 * system. But if it is IRIX doing check on write-access for a
2140 * device special file, we ignore rofs.
2142 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2143 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2144 if (exp_rdonly(rqstp, exp) ||
2145 __mnt_is_readonly(exp->ex_path.mnt))
2147 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2150 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2153 if (acc & NFSD_MAY_LOCK) {
2154 /* If we cannot rely on authentication in NLM requests,
2155 * just allow locks, otherwise require read permission, or
2158 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2161 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2164 * The file owner always gets access permission for accesses that
2165 * would normally be checked at open time. This is to make
2166 * file access work even when the client has done a fchmod(fd, 0).
2168 * However, `cp foo bar' should fail nevertheless when bar is
2169 * readonly. A sensible way to do this might be to reject all
2170 * attempts to truncate a read-only file, because a creat() call
2171 * always implies file truncation.
2172 * ... but this isn't really fair. A process may reasonably call
2173 * ftruncate on an open file descriptor on a file with perm 000.
2174 * We must trust the client to do permission checking - using "ACCESS"
2177 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2178 uid_eq(inode->i_uid, current_fsuid()))
2181 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2182 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2184 /* Allow read access to binaries even when mode 111 */
2185 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2186 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2187 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2188 err = inode_permission(inode, MAY_EXEC);
2190 return err? nfserrno(err) : 0;
2194 nfsd_racache_shutdown(void)
2196 struct raparms *raparm, *last_raparm;
2199 dprintk("nfsd: freeing readahead buffers.\n");
2201 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2202 raparm = raparm_hash[i].pb_head;
2204 last_raparm = raparm;
2205 raparm = raparm->p_next;
2208 raparm_hash[i].pb_head = NULL;
2212 * Initialize readahead param cache
2215 nfsd_racache_init(int cache_size)
2220 struct raparms **raparm = NULL;
2223 if (raparm_hash[0].pb_head)
2225 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2228 cache_size = nperbucket * RAPARM_HASH_SIZE;
2230 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2232 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2233 spin_lock_init(&raparm_hash[i].pb_lock);
2235 raparm = &raparm_hash[i].pb_head;
2236 for (j = 0; j < nperbucket; j++) {
2237 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2240 raparm = &(*raparm)->p_next;
2245 nfsdstats.ra_size = cache_size;
2249 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2250 nfsd_racache_shutdown();
2254 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2256 nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2258 struct inode *inode = fhp->fh_dentry->d_inode;
2262 struct posix_acl *acl;
2264 if (!IS_POSIXACL(inode))
2265 return ERR_PTR(-EOPNOTSUPP);
2268 case ACL_TYPE_ACCESS:
2269 name = POSIX_ACL_XATTR_ACCESS;
2271 case ACL_TYPE_DEFAULT:
2272 name = POSIX_ACL_XATTR_DEFAULT;
2275 return ERR_PTR(-EOPNOTSUPP);
2278 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2280 return ERR_PTR(size);
2282 acl = posix_acl_from_xattr(&init_user_ns, value, size);
2288 nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2290 struct inode *inode = fhp->fh_dentry->d_inode;
2296 if (!IS_POSIXACL(inode) ||
2297 !inode->i_op->setxattr || !inode->i_op->removexattr)
2300 case ACL_TYPE_ACCESS:
2301 name = POSIX_ACL_XATTR_ACCESS;
2303 case ACL_TYPE_DEFAULT:
2304 name = POSIX_ACL_XATTR_DEFAULT;
2310 if (acl && acl->a_count) {
2311 size = posix_acl_xattr_size(acl->a_count);
2312 value = kmalloc(size, GFP_KERNEL);
2315 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
2322 error = fh_want_write(fhp);
2326 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
2328 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2331 error = vfs_removexattr(fhp->fh_dentry, name);
2332 if (error == -ENODATA)
2342 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */