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 dentry *dentry, struct iattr *iap)
306 struct inode *inode = dentry->d_inode;
309 * NFSv2 does not differentiate between "set-[ac]time-to-now"
310 * which only requires access, and "set-[ac]time-to-X" which
311 * requires ownership.
312 * So if it looks like it might be "set both to the same time which
313 * is close to now", and if setattr_prepare fails, then we
314 * convert to "set to now" instead of "set to explicit time"
316 * We only call setattr_prepare as the last test as technically
317 * it is not an interface that we should be using.
319 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
320 #define MAX_TOUCH_TIME_ERROR (30*60)
321 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
322 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
326 * Now just make sure time is in the right ballpark.
327 * Solaris, at least, doesn't seem to care what the time
328 * request is. We require it be within 30 minutes of now.
330 time_t delta = iap->ia_atime.tv_sec - get_seconds();
333 if (delta < MAX_TOUCH_TIME_ERROR &&
334 setattr_prepare(dentry, iap) != 0) {
336 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
337 * This will cause notify_change to set these times
340 iap->ia_valid &= ~BOTH_TIME_SET;
344 /* sanitize the mode change */
345 if (iap->ia_valid & ATTR_MODE) {
346 iap->ia_mode &= S_IALLUGO;
347 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
350 /* Revoke setuid/setgid on chown */
351 if (!S_ISDIR(inode->i_mode) &&
352 (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
353 ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid))) {
354 iap->ia_valid |= ATTR_KILL_PRIV;
355 if (iap->ia_valid & ATTR_MODE) {
356 /* we're setting mode too, just clear the s*id bits */
357 iap->ia_mode &= ~S_ISUID;
358 if (iap->ia_mode & S_IXGRP)
359 iap->ia_mode &= ~S_ISGID;
361 /* set ATTR_KILL_* bits and let VFS handle it */
362 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
368 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
371 struct inode *inode = fhp->fh_dentry->d_inode;
374 if (iap->ia_size < inode->i_size) {
377 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
378 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
383 host_err = get_write_access(inode);
387 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
389 goto out_put_write_access;
392 out_put_write_access:
393 put_write_access(inode);
395 return nfserrno(host_err);
399 * Set various file attributes. After this call fhp needs an fh_put.
402 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
403 int check_guard, time_t guardtime)
405 struct dentry *dentry;
407 int accmode = NFSD_MAY_SATTR;
411 bool get_write_count;
412 bool size_change = (iap->ia_valid & ATTR_SIZE);
414 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
415 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
416 if (iap->ia_valid & ATTR_SIZE)
419 /* Callers that do fh_verify should do the fh_want_write: */
420 get_write_count = !fhp->fh_dentry;
423 err = fh_verify(rqstp, fhp, ftype, accmode);
426 if (get_write_count) {
427 host_err = fh_want_write(fhp);
432 dentry = fhp->fh_dentry;
433 inode = dentry->d_inode;
435 /* Ignore any mode updates on symlinks */
436 if (S_ISLNK(inode->i_mode))
437 iap->ia_valid &= ~ATTR_MODE;
442 nfsd_sanitize_attrs(dentry, iap);
444 if (check_guard && guardtime != inode->i_ctime.tv_sec)
445 return nfserr_notsync;
448 * The size case is special, it changes the file in addition to the
449 * attributes, and file systems don't expect it to be mixed with
450 * "random" attribute changes. We thus split out the size change
451 * into a separate call to ->setattr, and do the rest as a separate
455 err = nfsd_get_write_access(rqstp, fhp, iap);
460 host_err = nfsd_break_lease(inode);
462 goto out_put_write_access_nfserror;
467 * RFC5661, Section 18.30.4:
468 * Changing the size of a file with SETATTR indirectly
469 * changes the time_modify and change attributes.
471 * (and similar for the older RFCs)
473 struct iattr size_attr = {
474 .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
475 .ia_size = iap->ia_size,
478 host_err = notify_change(dentry, &size_attr);
481 iap->ia_valid &= ~ATTR_SIZE;
484 * Avoid the additional setattr call below if the only other
485 * attribute that the client sends is the mtime, as we update
486 * it as part of the size change above.
488 if ((iap->ia_valid & ~ATTR_MTIME) == 0)
492 iap->ia_valid |= ATTR_CTIME;
493 host_err = notify_change(dentry, iap);
497 out_put_write_access_nfserror:
499 put_write_access(inode);
502 commit_metadata(fhp);
503 return nfserrno(host_err);
506 #if defined(CONFIG_NFSD_V2_ACL) || \
507 defined(CONFIG_NFSD_V3_ACL) || \
508 defined(CONFIG_NFSD_V4)
509 static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
514 buflen = vfs_getxattr(dentry, key, NULL, 0);
518 *buf = kmalloc(buflen, GFP_KERNEL);
522 ret = vfs_getxattr(dentry, key, *buf, buflen);
529 #if defined(CONFIG_NFSD_V4)
531 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
539 return vfs_setxattr(dentry, key, NULL, 0, 0);
541 buflen = posix_acl_xattr_size(pacl->a_count);
542 buf = kmalloc(buflen, GFP_KERNEL);
547 len = posix_acl_to_xattr(pacl, buf, buflen);
553 error = vfs_setxattr(dentry, key, buf, len, 0);
560 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
561 struct nfs4_acl *acl)
565 struct dentry *dentry;
567 struct posix_acl *pacl = NULL, *dpacl = NULL;
568 unsigned int flags = 0;
571 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
575 dentry = fhp->fh_dentry;
576 inode = dentry->d_inode;
577 if (S_ISDIR(inode->i_mode))
578 flags = NFS4_ACL_DIR;
580 host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
581 if (host_error == -EINVAL) {
582 return nfserr_attrnotsupp;
583 } else if (host_error < 0)
586 host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
590 if (S_ISDIR(inode->i_mode))
591 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
594 posix_acl_release(pacl);
595 posix_acl_release(dpacl);
597 if (host_error == -EOPNOTSUPP)
598 return nfserr_attrnotsupp;
600 return nfserrno(host_error);
603 static struct posix_acl *
604 _get_posix_acl(struct dentry *dentry, char *key)
607 struct posix_acl *pacl = NULL;
610 buflen = nfsd_getxattr(dentry, key, &buf);
614 return ERR_PTR(buflen);
616 pacl = posix_acl_from_xattr(buf, buflen);
622 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
624 struct inode *inode = dentry->d_inode;
626 struct posix_acl *pacl = NULL, *dpacl = NULL;
627 unsigned int flags = 0;
629 pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
630 if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
631 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
633 error = PTR_ERR(pacl);
638 if (S_ISDIR(inode->i_mode)) {
639 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
640 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
642 else if (IS_ERR(dpacl)) {
643 error = PTR_ERR(dpacl);
647 flags = NFS4_ACL_DIR;
650 *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
652 error = PTR_ERR(*acl);
656 posix_acl_release(pacl);
657 posix_acl_release(dpacl);
661 #define NFSD_XATTR_JUNCTION_PREFIX XATTR_TRUSTED_PREFIX "junction."
662 #define NFSD_XATTR_JUNCTION_TYPE NFSD_XATTR_JUNCTION_PREFIX "type"
663 int nfsd4_is_junction(struct dentry *dentry)
665 struct inode *inode = dentry->d_inode;
669 if (inode->i_mode & S_IXUGO)
671 if (!(inode->i_mode & S_ISVTX))
673 if (vfs_getxattr(dentry, NFSD_XATTR_JUNCTION_TYPE, NULL, 0) <= 0)
677 #endif /* defined(CONFIG_NFSD_V4) */
679 #ifdef CONFIG_NFSD_V3
681 * Check server access rights to a file system object
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 },
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 },
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
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 },
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;
731 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
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;
743 map = nfs3_anyaccess;
747 for (; map->access; map++) {
748 if (map->access & query) {
751 sresult |= map->access;
753 err2 = nfsd_permission(rqstp, export, dentry, map->how);
756 result |= map->access;
759 /* the following error codes just mean the access was not allowed,
760 * rather than an error occurred */
764 /* simply don't "or" in the access bit. */
774 *supported = sresult;
779 #endif /* CONFIG_NFSD_V3 */
781 static int nfsd_open_break_lease(struct inode *inode, int access)
785 if (access & NFSD_MAY_NOT_BREAK_LEASE)
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
798 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
799 int may_flags, struct file **filp)
801 struct dentry *dentry;
803 int flags = O_RDONLY|O_LARGEFILE;
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 err = fh_verify(rqstp, fhp, type, may_flags | NFSD_MAY_OWNER_OVERRIDE);
818 dentry = fhp->fh_dentry;
819 inode = dentry->d_inode;
821 /* Disallow write access to files with the append-only bit set
822 * or any access when mandatory locking enabled
825 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
828 * We must ignore files (but only files) which might have mandatory
829 * locks on them because there is no way to know if the accesser has
832 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
838 host_err = nfsd_open_break_lease(inode, may_flags);
839 if (host_err) /* NOMEM or WOULDBLOCK */
842 if (may_flags & NFSD_MAY_WRITE) {
843 if (may_flags & NFSD_MAY_READ)
844 flags = O_RDWR|O_LARGEFILE;
846 flags = O_WRONLY|O_LARGEFILE;
848 *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
849 flags, current_cred());
851 host_err = PTR_ERR(*filp);
854 host_err = ima_file_check(*filp, may_flags);
856 if (may_flags & NFSD_MAY_64BIT_COOKIE)
857 (*filp)->f_mode |= FMODE_64BITHASH;
859 (*filp)->f_mode |= FMODE_32BITHASH;
863 err = nfserrno(host_err);
865 validate_process_creds();
873 nfsd_close(struct file *filp)
879 * Obtain the readahead parameters for the file
880 * specified by (dev, ino).
883 static inline struct raparms *
884 nfsd_get_raparms(dev_t dev, ino_t ino)
886 struct raparms *ra, **rap, **frap = NULL;
889 struct raparm_hbucket *rab;
891 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
892 rab = &raparm_hash[hash];
894 spin_lock(&rab->pb_lock);
895 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
896 if (ra->p_ino == ino && ra->p_dev == dev)
899 if (ra->p_count == 0)
902 depth = nfsdstats.ra_size;
904 spin_unlock(&rab->pb_lock);
914 if (rap != &rab->pb_head) {
916 ra->p_next = rab->pb_head;
920 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
921 spin_unlock(&rab->pb_lock);
926 * Grab and keep cached pages associated with a file in the svc_rqst
927 * so that they can be passed to the network sendmsg/sendpage routines
928 * directly. They will be released after the sending has completed.
931 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
932 struct splice_desc *sd)
934 struct svc_rqst *rqstp = sd->u.data;
935 struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
936 struct page *page = buf->page;
941 if (rqstp->rq_res.page_len == 0) {
946 rqstp->rq_res.page_base = buf->offset;
947 rqstp->rq_res.page_len = size;
948 } else if (page != pp[-1]) {
954 rqstp->rq_res.page_len += size;
956 rqstp->rq_res.page_len += size;
961 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
962 struct splice_desc *sd)
964 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
968 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
969 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
977 if (file->f_op->splice_read && rqstp->rq_splice_ok) {
978 struct splice_desc sd = {
985 rqstp->rq_resused = 1;
986 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
990 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
995 nfsdstats.io_read += host_err;
998 fsnotify_access(file);
1000 err = nfserrno(host_err);
1004 static void kill_suid(struct dentry *dentry)
1007 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
1009 mutex_lock(&dentry->d_inode->i_mutex);
1010 notify_change(dentry, &ia);
1011 mutex_unlock(&dentry->d_inode->i_mutex);
1015 * Gathered writes: If another process is currently writing to the file,
1016 * there's a high chance this is another nfsd (triggered by a bulk write
1017 * from a client's biod). Rather than syncing the file with each write
1018 * request, we sleep for 10 msec.
1020 * I don't know if this roughly approximates C. Juszak's idea of
1021 * gathered writes, but it's a nice and simple solution (IMHO), and it
1024 * Note: we do this only in the NFSv2 case, since v3 and higher have a
1025 * better tool (separate unstable writes and commits) for solving this
1028 static int wait_for_concurrent_writes(struct file *file)
1030 struct inode *inode = file->f_path.dentry->d_inode;
1031 static ino_t last_ino;
1032 static dev_t last_dev;
1035 if (atomic_read(&inode->i_writecount) > 1
1036 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1037 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1039 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1042 if (inode->i_state & I_DIRTY) {
1043 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1044 err = vfs_fsync(file, 0);
1046 last_ino = inode->i_ino;
1047 last_dev = inode->i_sb->s_dev;
1052 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1053 loff_t offset, struct kvec *vec, int vlen,
1054 unsigned long *cnt, int *stablep)
1056 struct svc_export *exp;
1057 struct dentry *dentry;
1058 struct inode *inode;
1062 int stable = *stablep;
1065 dentry = file->f_path.dentry;
1066 inode = dentry->d_inode;
1067 exp = fhp->fh_export;
1070 * Request sync writes if
1071 * - the sync export option has been set, or
1072 * - the client requested O_SYNC behavior (NFSv3 feature).
1073 * - The file system doesn't support fsync().
1074 * When NFSv2 gathered writes have been configured for this volume,
1075 * flushing the data to disk is handled separately below.
1077 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1079 if (!file->f_op->fsync) {/* COMMIT3 cannot work */
1081 *stablep = 2; /* FILE_SYNC */
1084 if (!EX_ISSYNC(exp))
1086 if (stable && !use_wgather) {
1087 spin_lock(&file->f_lock);
1088 file->f_flags |= O_SYNC;
1089 spin_unlock(&file->f_lock);
1092 /* Write the data. */
1093 oldfs = get_fs(); set_fs(KERNEL_DS);
1094 host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
1099 nfsdstats.io_write += host_err;
1100 fsnotify_modify(file);
1102 /* clear setuid/setgid flag after write */
1103 if (inode->i_mode & (S_ISUID | S_ISGID))
1106 if (stable && use_wgather)
1107 host_err = wait_for_concurrent_writes(file);
1110 dprintk("nfsd: write complete host_err=%d\n", host_err);
1114 err = nfserrno(host_err);
1119 * Read data from a file. count must contain the requested read count
1120 * on entry. On return, *count contains the number of bytes actually read.
1121 * N.B. After this call fhp needs an fh_put
1123 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1124 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1127 struct inode *inode;
1131 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1135 inode = file->f_path.dentry->d_inode;
1137 /* Get readahead parameters */
1138 ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
1140 if (ra && ra->p_set)
1141 file->f_ra = ra->p_ra;
1143 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1145 /* Write back readahead params */
1147 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
1148 spin_lock(&rab->pb_lock);
1149 ra->p_ra = file->f_ra;
1152 spin_unlock(&rab->pb_lock);
1159 /* As above, but use the provided file descriptor. */
1161 nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1162 loff_t offset, struct kvec *vec, int vlen,
1163 unsigned long *count)
1168 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1169 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
1172 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1173 } else /* Note file may still be NULL in NFSv4 special stateid case: */
1174 err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
1180 * Write data to a file.
1181 * The stable flag requests synchronous writes.
1182 * N.B. After this call fhp needs an fh_put
1185 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1186 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1192 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1193 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1196 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1199 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1204 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1212 #ifdef CONFIG_NFSD_V3
1214 * Commit all pending writes to stable storage.
1216 * Note: we only guarantee that data that lies within the range specified
1217 * by the 'offset' and 'count' parameters will be synced.
1219 * Unfortunately we cannot lock the file to make sure we return full WCC
1220 * data to the client, as locking happens lower down in the filesystem.
1223 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1224 loff_t offset, unsigned long count)
1227 loff_t end = LLONG_MAX;
1228 __be32 err = nfserr_inval;
1233 end = offset + (loff_t)count - 1;
1238 err = nfsd_open(rqstp, fhp, S_IFREG,
1239 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
1242 if (EX_ISSYNC(fhp->fh_export)) {
1243 int err2 = vfs_fsync_range(file, offset, end, 0);
1245 if (err2 != -EINVAL)
1246 err = nfserrno(err2);
1248 err = nfserr_notsupp;
1255 #endif /* CONFIG_NFSD_V3 */
1258 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1262 * Mode has already been set earlier in create:
1264 iap->ia_valid &= ~ATTR_MODE;
1266 * Setting uid/gid works only for root. Irix appears to
1267 * send along the gid on create when it tries to implement
1268 * setgid directories via NFS:
1270 if (current_fsuid() != 0)
1271 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1273 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1277 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1278 * setting size to 0 may fail for some specific file systems by the permission
1279 * checking which requires WRITE permission but the mode is 000.
1280 * we ignore the resizing(to 0) on the just new created file, since the size is
1281 * 0 after file created.
1283 * call this only after vfs_create() is called.
1286 nfsd_check_ignore_resizing(struct iattr *iap)
1288 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1289 iap->ia_valid &= ~ATTR_SIZE;
1293 * Create a file (regular, directory, device, fifo); UNIX sockets
1294 * not yet implemented.
1295 * If the response fh has been verified, the parent directory should
1296 * already be locked. Note that the parent directory is left locked.
1298 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1301 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1302 char *fname, int flen, struct iattr *iap,
1303 int type, dev_t rdev, struct svc_fh *resfhp)
1305 struct dentry *dentry, *dchild = NULL;
1315 if (isdotent(fname, flen))
1318 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1322 dentry = fhp->fh_dentry;
1323 dirp = dentry->d_inode;
1325 err = nfserr_notdir;
1326 if (!dirp->i_op->lookup)
1329 * Check whether the response file handle has been verified yet.
1330 * If it has, the parent directory should already be locked.
1332 if (!resfhp->fh_dentry) {
1333 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1334 fh_lock_nested(fhp, I_MUTEX_PARENT);
1335 dchild = lookup_one_len(fname, dentry, flen);
1336 host_err = PTR_ERR(dchild);
1339 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1343 /* called from nfsd_proc_create */
1344 dchild = dget(resfhp->fh_dentry);
1345 if (!fhp->fh_locked) {
1346 /* not actually possible */
1348 "nfsd_create: parent %s/%s not locked!\n",
1349 dentry->d_parent->d_name.name,
1350 dentry->d_name.name);
1356 * Make sure the child dentry is still negative ...
1359 if (dchild->d_inode) {
1360 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1361 dentry->d_name.name, dchild->d_name.name);
1365 if (!(iap->ia_valid & ATTR_MODE))
1367 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1370 if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1371 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1376 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1381 * Get the dir op function pointer.
1386 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1388 nfsd_check_ignore_resizing(iap);
1391 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1397 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1401 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1405 err = nfsd_create_setattr(rqstp, resfhp, iap);
1408 * nfsd_setattr already committed the child. Transactional filesystems
1409 * had a chance to commit changes for both parent and child
1410 * simultaneously making the following commit_metadata a noop.
1412 err2 = nfserrno(commit_metadata(fhp));
1415 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1417 * Update the file handle to get the new inode info.
1420 err = fh_update(resfhp);
1422 if (dchild && !IS_ERR(dchild))
1427 err = nfserrno(host_err);
1431 #ifdef CONFIG_NFSD_V3
1433 static inline int nfsd_create_is_exclusive(int createmode)
1435 return createmode == NFS3_CREATE_EXCLUSIVE
1436 || createmode == NFS4_CREATE_EXCLUSIVE4_1;
1440 * NFSv3 and NFSv4 version of nfsd_create
1443 do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1444 char *fname, int flen, struct iattr *iap,
1445 struct svc_fh *resfhp, int createmode, u32 *verifier,
1446 bool *truncp, bool *created)
1448 struct dentry *dentry, *dchild = NULL;
1452 __u32 v_mtime=0, v_atime=0;
1458 if (isdotent(fname, flen))
1460 if (!(iap->ia_valid & ATTR_MODE))
1462 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1466 dentry = fhp->fh_dentry;
1467 dirp = dentry->d_inode;
1469 /* Get all the sanity checks out of the way before
1470 * we lock the parent. */
1471 err = nfserr_notdir;
1472 if (!dirp->i_op->lookup)
1474 fh_lock_nested(fhp, I_MUTEX_PARENT);
1477 * Compose the response file handle.
1479 dchild = lookup_one_len(fname, dentry, flen);
1480 host_err = PTR_ERR(dchild);
1484 /* If file doesn't exist, check for permissions to create one */
1485 if (!dchild->d_inode) {
1486 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1491 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1495 if (nfsd_create_is_exclusive(createmode)) {
1496 /* solaris7 gets confused (bugid 4218508) if these have
1497 * the high bit set, so just clear the high bits. If this is
1498 * ever changed to use different attrs for storing the
1499 * verifier, then do_open_lookup() will also need to be fixed
1502 v_mtime = verifier[0]&0x7fffffff;
1503 v_atime = verifier[1]&0x7fffffff;
1506 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1509 if (dchild->d_inode) {
1512 switch (createmode) {
1513 case NFS3_CREATE_UNCHECKED:
1514 if (! S_ISREG(dchild->d_inode->i_mode))
1517 /* in nfsv4, we need to treat this case a little
1518 * differently. we don't want to truncate the
1519 * file now; this would be wrong if the OPEN
1520 * fails for some other reason. furthermore,
1521 * if the size is nonzero, we should ignore it
1522 * according to spec!
1524 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1527 iap->ia_valid &= ATTR_SIZE;
1531 case NFS3_CREATE_EXCLUSIVE:
1532 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1533 && dchild->d_inode->i_atime.tv_sec == v_atime
1534 && dchild->d_inode->i_size == 0 ) {
1539 case NFS4_CREATE_EXCLUSIVE4_1:
1540 if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
1541 && dchild->d_inode->i_atime.tv_sec == v_atime
1542 && dchild->d_inode->i_size == 0 ) {
1548 case NFS3_CREATE_GUARDED:
1551 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1555 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1557 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1563 nfsd_check_ignore_resizing(iap);
1565 if (nfsd_create_is_exclusive(createmode)) {
1566 /* Cram the verifier into atime/mtime */
1567 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1568 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1569 /* XXX someone who knows this better please fix it for nsec */
1570 iap->ia_mtime.tv_sec = v_mtime;
1571 iap->ia_atime.tv_sec = v_atime;
1572 iap->ia_mtime.tv_nsec = 0;
1573 iap->ia_atime.tv_nsec = 0;
1577 err = nfsd_create_setattr(rqstp, resfhp, iap);
1580 * nfsd_setattr already committed the child (and possibly also the parent).
1583 err = nfserrno(commit_metadata(fhp));
1585 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1587 * Update the filehandle to get the new inode info.
1590 err = fh_update(resfhp);
1594 if (dchild && !IS_ERR(dchild))
1599 err = nfserrno(host_err);
1602 #endif /* CONFIG_NFSD_V3 */
1605 * Read a symlink. On entry, *lenp must contain the maximum path length that
1606 * fits into the buffer. On return, it contains the true length.
1607 * N.B. After this call fhp needs an fh_put
1610 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1612 struct dentry *dentry;
1613 struct inode *inode;
1618 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1622 dentry = fhp->fh_dentry;
1623 inode = dentry->d_inode;
1626 if (!inode->i_op->readlink)
1629 touch_atime(fhp->fh_export->ex_path.mnt, dentry);
1630 /* N.B. Why does this call need a get_fs()??
1631 * Remove the set_fs and watch the fireworks:-) --okir
1634 oldfs = get_fs(); set_fs(KERNEL_DS);
1635 host_err = inode->i_op->readlink(dentry, buf, *lenp);
1646 err = nfserrno(host_err);
1651 * Create a symlink and look up its inode
1652 * N.B. After this call _both_ fhp and resfhp need an fh_put
1655 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1656 char *fname, int flen,
1657 char *path, int plen,
1658 struct svc_fh *resfhp,
1661 struct dentry *dentry, *dnew;
1669 if (isdotent(fname, flen))
1672 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1676 dentry = fhp->fh_dentry;
1677 dnew = lookup_one_len(fname, dentry, flen);
1678 host_err = PTR_ERR(dnew);
1682 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1686 if (unlikely(path[plen] != 0)) {
1687 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1688 if (path_alloced == NULL)
1691 strncpy(path_alloced, path, plen);
1692 path_alloced[plen] = 0;
1693 host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1694 kfree(path_alloced);
1697 host_err = vfs_symlink(dentry->d_inode, dnew, path);
1698 err = nfserrno(host_err);
1700 err = nfserrno(commit_metadata(fhp));
1703 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1705 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1707 if (err==0) err = cerr;
1712 err = nfserrno(host_err);
1718 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1721 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1722 char *name, int len, struct svc_fh *tfhp)
1724 struct dentry *ddir, *dnew, *dold;
1729 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1732 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1736 if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
1742 if (isdotent(name, len))
1745 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1746 ddir = ffhp->fh_dentry;
1747 dirp = ddir->d_inode;
1749 dnew = lookup_one_len(name, ddir, len);
1750 host_err = PTR_ERR(dnew);
1754 dold = tfhp->fh_dentry;
1756 host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
1758 err = nfserrno(host_err);
1763 goto out_drop_write;
1764 host_err = nfsd_break_lease(dold->d_inode);
1766 err = nfserrno(host_err);
1767 goto out_drop_write;
1769 host_err = vfs_link(dold, dirp, dnew);
1771 err = nfserrno(commit_metadata(ffhp));
1773 err = nfserrno(commit_metadata(tfhp));
1775 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1778 err = nfserrno(host_err);
1781 mnt_drop_write(tfhp->fh_export->ex_path.mnt);
1790 err = nfserrno(host_err);
1796 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1799 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1800 struct svc_fh *tfhp, char *tname, int tlen)
1802 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1803 struct inode *fdir, *tdir;
1807 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1810 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1814 fdentry = ffhp->fh_dentry;
1815 fdir = fdentry->d_inode;
1817 tdentry = tfhp->fh_dentry;
1818 tdir = tdentry->d_inode;
1820 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1821 if (ffhp->fh_export != tfhp->fh_export)
1825 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1828 /* cannot use fh_lock as we need deadlock protective ordering
1829 * so do it by hand */
1830 trap = lock_rename(tdentry, fdentry);
1831 ffhp->fh_locked = tfhp->fh_locked = 1;
1835 odentry = lookup_one_len(fname, fdentry, flen);
1836 host_err = PTR_ERR(odentry);
1837 if (IS_ERR(odentry))
1841 if (!odentry->d_inode)
1844 if (odentry == trap)
1847 ndentry = lookup_one_len(tname, tdentry, tlen);
1848 host_err = PTR_ERR(ndentry);
1849 if (IS_ERR(ndentry))
1851 host_err = -ENOTEMPTY;
1852 if (ndentry == trap)
1856 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1858 host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
1862 host_err = nfsd_break_lease(odentry->d_inode);
1864 goto out_drop_write;
1865 if (ndentry->d_inode) {
1866 host_err = nfsd_break_lease(ndentry->d_inode);
1868 goto out_drop_write;
1870 host_err = vfs_rename(fdir, odentry, tdir, ndentry);
1872 host_err = commit_metadata(tfhp);
1874 host_err = commit_metadata(ffhp);
1877 mnt_drop_write(ffhp->fh_export->ex_path.mnt);
1883 err = nfserrno(host_err);
1885 /* we cannot reply on fh_unlock on the two filehandles,
1886 * as that would do the wrong thing if the two directories
1887 * were the same, so again we do it by hand
1889 fill_post_wcc(ffhp);
1890 fill_post_wcc(tfhp);
1891 unlock_rename(tdentry, fdentry);
1892 ffhp->fh_locked = tfhp->fh_locked = 0;
1899 * Unlink a file or directory
1900 * N.B. After this call fhp needs an fh_put
1903 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1904 char *fname, int flen)
1906 struct dentry *dentry, *rdentry;
1912 if (!flen || isdotent(fname, flen))
1914 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1918 fh_lock_nested(fhp, I_MUTEX_PARENT);
1919 dentry = fhp->fh_dentry;
1920 dirp = dentry->d_inode;
1922 rdentry = lookup_one_len(fname, dentry, flen);
1923 host_err = PTR_ERR(rdentry);
1924 if (IS_ERR(rdentry))
1927 if (!rdentry->d_inode) {
1934 type = rdentry->d_inode->i_mode & S_IFMT;
1936 host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1940 host_err = nfsd_break_lease(rdentry->d_inode);
1942 goto out_drop_write;
1943 if (type != S_IFDIR)
1944 host_err = vfs_unlink(dirp, rdentry);
1946 host_err = vfs_rmdir(dirp, rdentry);
1948 host_err = commit_metadata(fhp);
1950 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1955 err = nfserrno(host_err);
1961 * We do this buffering because we must not call back into the file
1962 * system's ->lookup() method from the filldir callback. That may well
1963 * deadlock a number of file systems.
1965 * This is based heavily on the implementation of same in XFS.
1967 struct buffered_dirent {
1971 unsigned int d_type;
1975 struct readdir_data {
1981 static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1982 loff_t offset, u64 ino, unsigned int d_type)
1984 struct readdir_data *buf = __buf;
1985 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1986 unsigned int reclen;
1988 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1989 if (buf->used + reclen > PAGE_SIZE) {
1994 de->namlen = namlen;
1995 de->offset = offset;
1997 de->d_type = d_type;
1998 memcpy(de->name, name, namlen);
1999 buf->used += reclen;
2004 static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
2005 struct readdir_cd *cdp, loff_t *offsetp)
2007 struct readdir_data buf;
2008 struct buffered_dirent *de;
2013 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
2015 return nfserrno(-ENOMEM);
2020 struct inode *dir_inode = file->f_path.dentry->d_inode;
2021 unsigned int reclen;
2023 cdp->err = nfserr_eof; /* will be cleared on successful read */
2027 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
2040 * Various filldir functions may end up calling back into
2041 * lookup_one_len() and the file system's ->lookup() method.
2042 * These expect i_mutex to be held, as it would within readdir.
2044 host_err = mutex_lock_killable(&dir_inode->i_mutex);
2048 de = (struct buffered_dirent *)buf.dirent;
2050 offset = de->offset;
2052 if (func(cdp, de->name, de->namlen, de->offset,
2053 de->ino, de->d_type))
2056 if (cdp->err != nfs_ok)
2059 reclen = ALIGN(sizeof(*de) + de->namlen,
2062 de = (struct buffered_dirent *)((char *)de + reclen);
2064 mutex_unlock(&dir_inode->i_mutex);
2065 if (size > 0) /* We bailed out early */
2068 offset = vfs_llseek(file, 0, SEEK_CUR);
2071 free_page((unsigned long)(buf.dirent));
2074 return nfserrno(host_err);
2081 * Read entries from a directory.
2082 * The NFSv3/4 verifier we ignore for now.
2085 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2086 struct readdir_cd *cdp, filldir_t func)
2090 loff_t offset = *offsetp;
2091 int may_flags = NFSD_MAY_READ;
2093 /* NFSv2 only supports 32 bit cookies */
2094 if (rqstp->rq_vers > 2)
2095 may_flags |= NFSD_MAY_64BIT_COOKIE;
2097 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
2101 offset = vfs_llseek(file, offset, 0);
2103 err = nfserrno((int)offset);
2107 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
2109 if (err == nfserr_eof || err == nfserr_toosmall)
2110 err = nfs_ok; /* can still be found in ->err */
2118 * Get file system stats
2119 * N.B. After this call fhp needs an fh_put
2122 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2126 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2128 struct path path = {
2129 .mnt = fhp->fh_export->ex_path.mnt,
2130 .dentry = fhp->fh_dentry,
2132 if (vfs_statfs(&path, stat))
2138 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2140 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2144 * Check for a user's access permissions to this inode.
2147 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2148 struct dentry *dentry, int acc)
2150 struct inode *inode = dentry->d_inode;
2153 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2156 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2158 (acc & NFSD_MAY_READ)? " read" : "",
2159 (acc & NFSD_MAY_WRITE)? " write" : "",
2160 (acc & NFSD_MAY_EXEC)? " exec" : "",
2161 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2162 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2163 (acc & NFSD_MAY_LOCK)? " lock" : "",
2164 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2166 IS_IMMUTABLE(inode)? " immut" : "",
2167 IS_APPEND(inode)? " append" : "",
2168 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2169 dprintk(" owner %d/%d user %d/%d\n",
2170 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2173 /* Normally we reject any write/sattr etc access on a read-only file
2174 * system. But if it is IRIX doing check on write-access for a
2175 * device special file, we ignore rofs.
2177 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2178 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2179 if (exp_rdonly(rqstp, exp) ||
2180 __mnt_is_readonly(exp->ex_path.mnt))
2182 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2185 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2188 if (acc & NFSD_MAY_LOCK) {
2189 /* If we cannot rely on authentication in NLM requests,
2190 * just allow locks, otherwise require read permission, or
2193 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2196 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2199 * The file owner always gets access permission for accesses that
2200 * would normally be checked at open time. This is to make
2201 * file access work even when the client has done a fchmod(fd, 0).
2203 * However, `cp foo bar' should fail nevertheless when bar is
2204 * readonly. A sensible way to do this might be to reject all
2205 * attempts to truncate a read-only file, because a creat() call
2206 * always implies file truncation.
2207 * ... but this isn't really fair. A process may reasonably call
2208 * ftruncate on an open file descriptor on a file with perm 000.
2209 * We must trust the client to do permission checking - using "ACCESS"
2212 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2213 inode->i_uid == current_fsuid())
2216 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2217 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2219 /* Allow read access to binaries even when mode 111 */
2220 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2221 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2222 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2223 err = inode_permission(inode, MAY_EXEC);
2225 return err? nfserrno(err) : 0;
2229 nfsd_racache_shutdown(void)
2231 struct raparms *raparm, *last_raparm;
2234 dprintk("nfsd: freeing readahead buffers.\n");
2236 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2237 raparm = raparm_hash[i].pb_head;
2239 last_raparm = raparm;
2240 raparm = raparm->p_next;
2243 raparm_hash[i].pb_head = NULL;
2247 * Initialize readahead param cache
2250 nfsd_racache_init(int cache_size)
2255 struct raparms **raparm = NULL;
2258 if (raparm_hash[0].pb_head)
2260 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2263 cache_size = nperbucket * RAPARM_HASH_SIZE;
2265 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2267 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2268 spin_lock_init(&raparm_hash[i].pb_lock);
2270 raparm = &raparm_hash[i].pb_head;
2271 for (j = 0; j < nperbucket; j++) {
2272 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2275 raparm = &(*raparm)->p_next;
2280 nfsdstats.ra_size = cache_size;
2284 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2285 nfsd_racache_shutdown();
2289 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2291 nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2293 struct inode *inode = fhp->fh_dentry->d_inode;
2297 struct posix_acl *acl;
2299 if (!IS_POSIXACL(inode))
2300 return ERR_PTR(-EOPNOTSUPP);
2303 case ACL_TYPE_ACCESS:
2304 name = POSIX_ACL_XATTR_ACCESS;
2306 case ACL_TYPE_DEFAULT:
2307 name = POSIX_ACL_XATTR_DEFAULT;
2310 return ERR_PTR(-EOPNOTSUPP);
2313 size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2315 return ERR_PTR(size);
2317 acl = posix_acl_from_xattr(value, size);
2323 nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2325 struct inode *inode = fhp->fh_dentry->d_inode;
2331 if (!IS_POSIXACL(inode) ||
2332 !inode->i_op->setxattr || !inode->i_op->removexattr)
2335 case ACL_TYPE_ACCESS:
2336 name = POSIX_ACL_XATTR_ACCESS;
2338 case ACL_TYPE_DEFAULT:
2339 name = POSIX_ACL_XATTR_DEFAULT;
2345 if (acl && acl->a_count) {
2346 size = posix_acl_xattr_size(acl->a_count);
2347 value = kmalloc(size, GFP_KERNEL);
2350 error = posix_acl_to_xattr(acl, value, size);
2357 error = mnt_want_write(fhp->fh_export->ex_path.mnt);
2361 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
2363 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2366 error = vfs_removexattr(fhp->fh_dentry, name);
2367 if (error == -ENODATA)
2371 mnt_drop_write(fhp->fh_export->ex_path.mnt);
2377 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */