1 // SPDX-License-Identifier: GPL-2.0
3 * File operations used by nfsd. Some of these have been ripped from
4 * other parts of the kernel because they weren't exported, others
5 * are partial duplicates with added or changed functionality.
7 * Note that several functions dget() the dentry upon which they want
8 * to act, most notably those that create directory entries. Response
9 * dentry's are dput()'d if necessary in the release callback.
10 * So if you notice code paths that apparently fail to dput() the
11 * dentry, don't worry--they have been taken care of.
13 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
18 #include <linux/file.h>
19 #include <linux/splice.h>
20 #include <linux/falloc.h>
21 #include <linux/fcntl.h>
22 #include <linux/namei.h>
23 #include <linux/delay.h>
24 #include <linux/fsnotify.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/xattr.h>
27 #include <linux/jhash.h>
28 #include <linux/ima.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 #include <linux/exportfs.h>
32 #include <linux/writeback.h>
33 #include <linux/security.h>
37 #endif /* CONFIG_NFSD_V3 */
40 #include "../internal.h"
43 #endif /* CONFIG_NFSD_V4 */
47 #include "filecache.h"
50 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
54 * This is a cache of readahead params that help us choose the proper
55 * readahead strategy. Initially, we set all readahead parameters to 0
56 * and let the VFS handle things.
57 * If you increase the number of cached files very much, you'll need to
58 * add a hash table here.
61 struct raparms *p_next;
66 struct file_ra_state p_ra;
67 unsigned int p_hindex;
70 struct raparm_hbucket {
71 struct raparms *pb_head;
73 } ____cacheline_aligned_in_smp;
75 #define RAPARM_HASH_BITS 4
76 #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
77 #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
78 static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
81 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
83 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
84 * or nfs_ok having possibly changed *dpp and *expp
87 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
88 struct svc_export **expp)
90 struct svc_export *exp = *expp, *exp2 = NULL;
91 struct dentry *dentry = *dpp;
92 struct path path = {.mnt = mntget(exp->ex_path.mnt),
93 .dentry = dget(dentry)};
96 err = follow_down(&path);
99 if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
100 nfsd_mountpoint(dentry, exp) == 2) {
101 /* This is only a mountpoint in some other namespace */
106 exp2 = rqst_exp_get_by_name(rqstp, &path);
110 * We normally allow NFS clients to continue
111 * "underneath" a mountpoint that is not exported.
112 * The exception is V4ROOT, where no traversal is ever
113 * allowed without an explicit export of the new
116 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
121 if (nfsd_v4client(rqstp) ||
122 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
123 /* successfully crossed mount point */
125 * This is subtle: path.dentry is *not* on path.mnt
126 * at this point. The only reason we are safe is that
127 * original mnt is pinned down by exp, so we should
128 * put path *before* putting exp
131 path.dentry = dentry;
141 static void follow_to_parent(struct path *path)
145 while (path->dentry == path->mnt->mnt_root && follow_up(path))
147 dp = dget_parent(path->dentry);
152 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
154 struct svc_export *exp2;
155 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
156 .dentry = dget(dparent)};
158 follow_to_parent(&path);
160 exp2 = rqst_exp_parent(rqstp, &path);
161 if (PTR_ERR(exp2) == -ENOENT) {
162 *dentryp = dget(dparent);
163 } else if (IS_ERR(exp2)) {
165 return PTR_ERR(exp2);
167 *dentryp = dget(path.dentry);
176 * For nfsd purposes, we treat V4ROOT exports as though there was an
177 * export at *every* directory.
179 * '1' if this dentry *must* be an export point,
180 * '2' if it might be, if there is really a mount here, and
181 * '0' if there is no chance of an export point here.
183 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
185 if (!d_inode(dentry))
187 if (exp->ex_flags & NFSEXP_V4ROOT)
189 if (nfsd4_is_junction(dentry))
191 if (d_mountpoint(dentry))
193 * Might only be a mountpoint in a different namespace,
194 * but we need to check.
201 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
202 const char *name, unsigned int len,
203 struct svc_export **exp_ret, struct dentry **dentry_ret)
205 struct svc_export *exp;
206 struct dentry *dparent;
207 struct dentry *dentry;
210 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
212 dparent = fhp->fh_dentry;
213 exp = exp_get(fhp->fh_export);
215 /* Lookup the name, but don't follow links */
216 if (isdotent(name, len)) {
218 dentry = dget(dparent);
219 else if (dparent != exp->ex_path.dentry)
220 dentry = dget_parent(dparent);
221 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
222 dentry = dget(dparent); /* .. == . just like at / */
224 /* checking mountpoint crossing is very different when stepping up */
225 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
231 * In the nfsd4_open() case, this may be held across
232 * subsequent open and delegation acquisition which may
233 * need to take the child's i_mutex:
235 fh_lock_nested(fhp, I_MUTEX_PARENT);
236 dentry = lookup_one_len(name, dparent, len);
237 host_err = PTR_ERR(dentry);
240 if (nfsd_mountpoint(dentry, exp)) {
242 * We don't need the i_mutex after all. It's
243 * still possible we could open this (regular
244 * files can be mountpoints too), but the
245 * i_mutex is just there to prevent renames of
246 * something that we might be about to delegate,
247 * and a mountpoint won't be renamed:
250 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
256 *dentry_ret = dentry;
262 return nfserrno(host_err);
266 * Look up one component of a pathname.
267 * N.B. After this call _both_ fhp and resfh need an fh_put
269 * If the lookup would cross a mountpoint, and the mounted filesystem
270 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
271 * accepted as it stands and the mounted directory is
272 * returned. Otherwise the covered directory is returned.
273 * NOTE: this mountpoint crossing is not supported properly by all
274 * clients and is explicitly disallowed for NFSv3
275 * NeilBrown <neilb@cse.unsw.edu.au>
278 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
279 unsigned int len, struct svc_fh *resfh)
281 struct svc_export *exp;
282 struct dentry *dentry;
285 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
288 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
291 err = check_nfsd_access(exp, rqstp);
295 * Note: we compose the file handle now, but as the
296 * dentry may be negative, it may need to be updated.
298 err = fh_compose(resfh, exp, dentry, fhp);
299 if (!err && d_really_is_negative(dentry))
308 * Commit metadata changes to stable storage.
311 commit_metadata(struct svc_fh *fhp)
313 struct inode *inode = d_inode(fhp->fh_dentry);
314 const struct export_operations *export_ops = inode->i_sb->s_export_op;
316 if (!EX_ISSYNC(fhp->fh_export))
319 if (export_ops->commit_metadata)
320 return export_ops->commit_metadata(inode);
321 return sync_inode_metadata(inode, 1);
325 * Go over the attributes and take care of the small differences between
326 * NFS semantics and what Linux expects.
329 nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
331 /* sanitize the mode change */
332 if (iap->ia_valid & ATTR_MODE) {
333 iap->ia_mode &= S_IALLUGO;
334 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
337 /* Revoke setuid/setgid on chown */
338 if (!S_ISDIR(inode->i_mode) &&
339 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
340 iap->ia_valid |= ATTR_KILL_PRIV;
341 if (iap->ia_valid & ATTR_MODE) {
342 /* we're setting mode too, just clear the s*id bits */
343 iap->ia_mode &= ~S_ISUID;
344 if (iap->ia_mode & S_IXGRP)
345 iap->ia_mode &= ~S_ISGID;
347 /* set ATTR_KILL_* bits and let VFS handle it */
348 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
354 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
357 struct inode *inode = d_inode(fhp->fh_dentry);
360 if (iap->ia_size < inode->i_size) {
363 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
364 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
369 host_err = get_write_access(inode);
373 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
375 goto out_put_write_access;
378 out_put_write_access:
379 put_write_access(inode);
381 return nfserrno(host_err);
385 * Set various file attributes. After this call fhp needs an fh_put.
388 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
389 int check_guard, time_t guardtime)
391 struct dentry *dentry;
393 int accmode = NFSD_MAY_SATTR;
397 bool get_write_count;
398 bool size_change = (iap->ia_valid & ATTR_SIZE);
400 if (iap->ia_valid & ATTR_SIZE) {
401 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
406 * If utimes(2) and friends are called with times not NULL, we should
407 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
408 * will return EACCES, when the caller's effective UID does not match
409 * the owner of the file, and the caller is not privileged. In this
410 * situation, we should return EPERM(notify_change will return this).
412 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
413 accmode |= NFSD_MAY_OWNER_OVERRIDE;
414 if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
415 accmode |= NFSD_MAY_WRITE;
418 /* Callers that do fh_verify should do the fh_want_write: */
419 get_write_count = !fhp->fh_dentry;
422 err = fh_verify(rqstp, fhp, ftype, accmode);
425 if (get_write_count) {
426 host_err = fh_want_write(fhp);
431 dentry = fhp->fh_dentry;
432 inode = d_inode(dentry);
434 /* Ignore any mode updates on symlinks */
435 if (S_ISLNK(inode->i_mode))
436 iap->ia_valid &= ~ATTR_MODE;
441 nfsd_sanitize_attrs(inode, iap);
443 if (check_guard && guardtime != inode->i_ctime.tv_sec)
444 return nfserr_notsync;
447 * The size case is special, it changes the file in addition to the
448 * attributes, and file systems don't expect it to be mixed with
449 * "random" attribute changes. We thus split out the size change
450 * into a separate call to ->setattr, and do the rest as a separate
454 err = nfsd_get_write_access(rqstp, fhp, iap);
462 * RFC5661, Section 18.30.4:
463 * Changing the size of a file with SETATTR indirectly
464 * changes the time_modify and change attributes.
466 * (and similar for the older RFCs)
468 struct iattr size_attr = {
469 .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
470 .ia_size = iap->ia_size,
473 host_err = notify_change(dentry, &size_attr, NULL);
476 iap->ia_valid &= ~ATTR_SIZE;
479 * Avoid the additional setattr call below if the only other
480 * attribute that the client sends is the mtime, as we update
481 * it as part of the size change above.
483 if ((iap->ia_valid & ~ATTR_MTIME) == 0)
487 iap->ia_valid |= ATTR_CTIME;
488 host_err = notify_change(dentry, iap, NULL);
493 put_write_access(inode);
496 host_err = commit_metadata(fhp);
497 return nfserrno(host_err);
500 #if defined(CONFIG_NFSD_V4)
502 * NFS junction information is stored in an extended attribute.
504 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
507 * nfsd4_is_junction - Test if an object could be an NFS junction
509 * @dentry: object to test
511 * Returns 1 if "dentry" appears to contain NFS junction information.
512 * Otherwise 0 is returned.
514 int nfsd4_is_junction(struct dentry *dentry)
516 struct inode *inode = d_inode(dentry);
520 if (inode->i_mode & S_IXUGO)
522 if (!(inode->i_mode & S_ISVTX))
524 if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
528 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
529 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
530 struct xdr_netobj *label)
534 struct dentry *dentry;
536 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
540 dentry = fhp->fh_dentry;
542 inode_lock(d_inode(dentry));
543 host_error = security_inode_setsecctx(dentry, label->data, label->len);
544 inode_unlock(d_inode(dentry));
545 return nfserrno(host_error);
548 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
549 struct xdr_netobj *label)
551 return nfserr_notsupp;
555 __be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
556 u64 dst_pos, u64 count)
560 cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
562 return nfserrno(cloned);
563 if (count && cloned != count)
564 return nfserrno(-EINVAL);
568 ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
569 u64 dst_pos, u64 count)
573 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
574 * thread and client rpc slot. The choice of 4MB is somewhat
575 * arbitrary. We might instead base this on r/wsize, or make it
576 * tunable, or use a time instead of a byte limit, or implement
577 * asynchronous copy. In theory a client could also recognize a
578 * limit like this and pipeline multiple COPY requests.
580 count = min_t(u64, count, 1 << 22);
581 return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
584 __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
585 struct file *file, loff_t offset, loff_t len,
590 if (!S_ISREG(file_inode(file)->i_mode))
593 error = vfs_fallocate(file, flags, offset, len);
595 error = commit_metadata(fhp);
597 return nfserrno(error);
599 #endif /* defined(CONFIG_NFSD_V4) */
601 #ifdef CONFIG_NFSD_V3
603 * Check server access rights to a file system object
609 static struct accessmap nfs3_regaccess[] = {
610 { NFS3_ACCESS_READ, NFSD_MAY_READ },
611 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
612 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
613 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
618 static struct accessmap nfs3_diraccess[] = {
619 { NFS3_ACCESS_READ, NFSD_MAY_READ },
620 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
621 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
622 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
623 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
628 static struct accessmap nfs3_anyaccess[] = {
629 /* Some clients - Solaris 2.6 at least, make an access call
630 * to the server to check for access for things like /dev/null
631 * (which really, the server doesn't care about). So
632 * We provide simple access checking for them, looking
633 * mainly at mode bits, and we make sure to ignore read-only
636 { NFS3_ACCESS_READ, NFSD_MAY_READ },
637 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
638 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
639 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
645 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
647 struct accessmap *map;
648 struct svc_export *export;
649 struct dentry *dentry;
650 u32 query, result = 0, sresult = 0;
653 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
657 export = fhp->fh_export;
658 dentry = fhp->fh_dentry;
660 if (d_is_reg(dentry))
661 map = nfs3_regaccess;
662 else if (d_is_dir(dentry))
663 map = nfs3_diraccess;
665 map = nfs3_anyaccess;
669 for (; map->access; map++) {
670 if (map->access & query) {
673 sresult |= map->access;
675 err2 = nfsd_permission(rqstp, export, dentry, map->how);
678 result |= map->access;
681 /* the following error codes just mean the access was not allowed,
682 * rather than an error occurred */
686 /* simply don't "or" in the access bit. */
696 *supported = sresult;
701 #endif /* CONFIG_NFSD_V3 */
703 int nfsd_open_break_lease(struct inode *inode, int access)
707 if (access & NFSD_MAY_NOT_BREAK_LEASE)
709 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
710 return break_lease(inode, mode | O_NONBLOCK);
714 * Open an existing file or directory.
715 * The may_flags argument indicates the type of open (read/write/lock)
716 * and additional flags.
717 * N.B. After this call fhp needs an fh_put
720 __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
721 int may_flags, struct file **filp)
726 int flags = O_RDONLY|O_LARGEFILE;
730 path.mnt = fhp->fh_export->ex_path.mnt;
731 path.dentry = fhp->fh_dentry;
732 inode = d_inode(path.dentry);
734 /* Disallow write access to files with the append-only bit set
735 * or any access when mandatory locking enabled
738 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
741 * We must ignore files (but only files) which might have mandatory
742 * locks on them because there is no way to know if the accesser has
745 if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
751 host_err = nfsd_open_break_lease(inode, may_flags);
752 if (host_err) /* NOMEM or WOULDBLOCK */
755 if (may_flags & NFSD_MAY_WRITE) {
756 if (may_flags & NFSD_MAY_READ)
757 flags = O_RDWR|O_LARGEFILE;
759 flags = O_WRONLY|O_LARGEFILE;
762 file = dentry_open(&path, flags, current_cred());
764 host_err = PTR_ERR(file);
768 host_err = ima_file_check(file, may_flags);
774 if (may_flags & NFSD_MAY_64BIT_COOKIE)
775 file->f_mode |= FMODE_64BITHASH;
777 file->f_mode |= FMODE_32BITHASH;
781 err = nfserrno(host_err);
787 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
788 int may_flags, struct file **filp)
792 validate_process_creds();
794 * If we get here, then the client has already done an "open",
795 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
796 * in case a chmod has now revoked permission.
798 * Arguably we should also allow the owner override for
799 * directories, but we never have and it doesn't seem to have
800 * caused anyone a problem. If we were to change this, note
801 * also that our filldir callbacks would need a variant of
802 * lookup_one_len that doesn't check permissions.
805 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
806 err = fh_verify(rqstp, fhp, type, may_flags);
808 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
809 validate_process_creds();
814 nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
815 int may_flags, struct file **filp)
819 validate_process_creds();
820 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
821 validate_process_creds();
828 nfsd_init_raparms(struct file *file)
830 struct inode *inode = file_inode(file);
831 dev_t dev = inode->i_sb->s_dev;
832 ino_t ino = inode->i_ino;
833 struct raparms *ra, **rap, **frap = NULL;
836 struct raparm_hbucket *rab;
838 hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
839 rab = &raparm_hash[hash];
841 spin_lock(&rab->pb_lock);
842 for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
843 if (ra->p_ino == ino && ra->p_dev == dev)
846 if (ra->p_count == 0)
849 depth = nfsdstats.ra_size;
851 spin_unlock(&rab->pb_lock);
861 if (rap != &rab->pb_head) {
863 ra->p_next = rab->pb_head;
867 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
868 spin_unlock(&rab->pb_lock);
871 file->f_ra = ra->p_ra;
875 void nfsd_put_raparams(struct file *file, struct raparms *ra)
877 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
879 spin_lock(&rab->pb_lock);
880 ra->p_ra = file->f_ra;
883 spin_unlock(&rab->pb_lock);
887 * Grab and keep cached pages associated with a file in the svc_rqst
888 * so that they can be passed to the network sendmsg/sendpage routines
889 * directly. They will be released after the sending has completed.
892 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
893 struct splice_desc *sd)
895 struct svc_rqst *rqstp = sd->u.data;
896 struct page **pp = rqstp->rq_next_page;
897 struct page *page = buf->page;
902 if (rqstp->rq_res.page_len == 0) {
904 put_page(*rqstp->rq_next_page);
905 *(rqstp->rq_next_page++) = page;
906 rqstp->rq_res.page_base = buf->offset;
907 rqstp->rq_res.page_len = size;
908 } else if (page != pp[-1]) {
910 if (*rqstp->rq_next_page)
911 put_page(*rqstp->rq_next_page);
912 *(rqstp->rq_next_page++) = page;
913 rqstp->rq_res.page_len += size;
915 rqstp->rq_res.page_len += size;
920 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
921 struct splice_desc *sd)
923 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
926 static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
927 struct file *file, loff_t offset,
928 unsigned long *count, int host_err)
931 nfsdstats.io_read += host_err;
933 fsnotify_access(file);
934 trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
937 trace_nfsd_read_err(rqstp, fhp, offset, host_err);
938 return nfserrno(host_err);
942 __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
943 struct file *file, loff_t offset, unsigned long *count)
945 struct splice_desc sd = {
953 trace_nfsd_read_splice(rqstp, fhp, offset, *count);
954 rqstp->rq_next_page = rqstp->rq_respages + 1;
955 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
956 return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
959 __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
960 struct file *file, loff_t offset,
961 struct kvec *vec, int vlen, unsigned long *count)
963 struct iov_iter iter;
966 trace_nfsd_read_vector(rqstp, fhp, offset, *count);
967 iov_iter_kvec(&iter, READ, vec, vlen, *count);
968 host_err = vfs_iter_read(file, &iter, &offset, 0);
969 return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
973 * Gathered writes: If another process is currently writing to the file,
974 * there's a high chance this is another nfsd (triggered by a bulk write
975 * from a client's biod). Rather than syncing the file with each write
976 * request, we sleep for 10 msec.
978 * I don't know if this roughly approximates C. Juszak's idea of
979 * gathered writes, but it's a nice and simple solution (IMHO), and it
982 * Note: we do this only in the NFSv2 case, since v3 and higher have a
983 * better tool (separate unstable writes and commits) for solving this
986 static int wait_for_concurrent_writes(struct file *file)
988 struct inode *inode = file_inode(file);
989 static ino_t last_ino;
990 static dev_t last_dev;
993 if (atomic_read(&inode->i_writecount) > 1
994 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
995 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
997 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1000 if (inode->i_state & I_DIRTY) {
1001 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1002 err = vfs_fsync(file, 0);
1004 last_ino = inode->i_ino;
1005 last_dev = inode->i_sb->s_dev;
1010 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1011 loff_t offset, struct kvec *vec, int vlen,
1012 unsigned long *cnt, int stable)
1014 struct svc_export *exp;
1015 struct iov_iter iter;
1019 loff_t pos = offset;
1020 unsigned int pflags = current->flags;
1023 trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
1025 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
1027 * We want less throttling in balance_dirty_pages()
1028 * and shrink_inactive_list() so that nfs to
1029 * localhost doesn't cause nfsd to lock up due to all
1030 * the client's dirty pages or its congested queue.
1032 current->flags |= PF_LESS_THROTTLE;
1034 exp = fhp->fh_export;
1035 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1037 if (!EX_ISSYNC(exp))
1038 stable = NFS_UNSTABLE;
1040 if (stable && !use_wgather)
1043 iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
1044 host_err = vfs_iter_write(file, &iter, &pos, flags);
1047 nfsdstats.io_write += *cnt;
1048 fsnotify_modify(file);
1050 if (stable && use_wgather)
1051 host_err = wait_for_concurrent_writes(file);
1054 if (host_err >= 0) {
1055 trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1058 trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1059 nfserr = nfserrno(host_err);
1061 if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
1062 current_restore_flags(pflags, PF_LESS_THROTTLE);
1067 * Read data from a file. count must contain the requested read count
1068 * on entry. On return, *count contains the number of bytes actually read.
1069 * N.B. After this call fhp needs an fh_put
1071 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1072 loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
1074 struct nfsd_file *nf;
1078 trace_nfsd_read_start(rqstp, fhp, offset, *count);
1079 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
1084 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
1085 err = nfsd_splice_read(rqstp, fhp, file, offset, count);
1087 err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count);
1091 trace_nfsd_read_done(rqstp, fhp, offset, *count);
1097 * Write data to a file.
1098 * The stable flag requests synchronous writes.
1099 * N.B. After this call fhp needs an fh_put
1102 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1103 struct kvec *vec, int vlen, unsigned long *cnt, int stable)
1105 struct nfsd_file *nf;
1108 trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
1110 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
1114 err = nfsd_vfs_write(rqstp, fhp, nf->nf_file, offset, vec,
1118 trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
1122 #ifdef CONFIG_NFSD_V3
1124 * Commit all pending writes to stable storage.
1126 * Note: we only guarantee that data that lies within the range specified
1127 * by the 'offset' and 'count' parameters will be synced.
1129 * Unfortunately we cannot lock the file to make sure we return full WCC
1130 * data to the client, as locking happens lower down in the filesystem.
1133 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1134 loff_t offset, unsigned long count)
1137 loff_t end = LLONG_MAX;
1138 __be32 err = nfserr_inval;
1143 end = offset + (loff_t)count - 1;
1148 err = nfsd_open(rqstp, fhp, S_IFREG,
1149 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
1152 if (EX_ISSYNC(fhp->fh_export)) {
1153 int err2 = vfs_fsync_range(file, offset, end, 0);
1155 if (err2 != -EINVAL)
1156 err = nfserrno(err2);
1158 err = nfserr_notsupp;
1165 #endif /* CONFIG_NFSD_V3 */
1168 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1172 * Mode has already been set earlier in create:
1174 iap->ia_valid &= ~ATTR_MODE;
1176 * Setting uid/gid works only for root. Irix appears to
1177 * send along the gid on create when it tries to implement
1178 * setgid directories via NFS:
1180 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1181 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1183 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1184 /* Callers expect file metadata to be committed here */
1185 return nfserrno(commit_metadata(resfhp));
1188 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1189 * setting size to 0 may fail for some specific file systems by the permission
1190 * checking which requires WRITE permission but the mode is 000.
1191 * we ignore the resizing(to 0) on the just new created file, since the size is
1192 * 0 after file created.
1194 * call this only after vfs_create() is called.
1197 nfsd_check_ignore_resizing(struct iattr *iap)
1199 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1200 iap->ia_valid &= ~ATTR_SIZE;
1203 /* The parent directory should already be locked: */
1205 nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
1206 char *fname, int flen, struct iattr *iap,
1207 int type, dev_t rdev, struct svc_fh *resfhp)
1209 struct dentry *dentry, *dchild;
1215 dentry = fhp->fh_dentry;
1216 dirp = d_inode(dentry);
1218 dchild = dget(resfhp->fh_dentry);
1219 if (!fhp->fh_locked) {
1220 WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1226 err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1230 if (!(iap->ia_valid & ATTR_MODE))
1232 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1238 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1240 nfsd_check_ignore_resizing(iap);
1243 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1244 if (!host_err && unlikely(d_unhashed(dchild))) {
1246 d = lookup_one_len(dchild->d_name.name,
1248 dchild->d_name.len);
1250 host_err = PTR_ERR(d);
1253 if (unlikely(d_is_negative(d))) {
1255 err = nfserr_serverfault;
1258 dput(resfhp->fh_dentry);
1259 resfhp->fh_dentry = dget(d);
1260 err = fh_update(resfhp);
1271 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1274 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1281 err = nfsd_create_setattr(rqstp, resfhp, iap);
1284 * nfsd_create_setattr already committed the child. Transactional
1285 * filesystems had a chance to commit changes for both parent and
1286 * child simultaneously making the following commit_metadata a
1289 err2 = nfserrno(commit_metadata(fhp));
1293 * Update the file handle to get the new inode info.
1296 err = fh_update(resfhp);
1302 err = nfserrno(host_err);
1307 * Create a filesystem object (regular, directory, special).
1308 * Note that the parent directory is left locked.
1310 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1313 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1314 char *fname, int flen, struct iattr *iap,
1315 int type, dev_t rdev, struct svc_fh *resfhp)
1317 struct dentry *dentry, *dchild = NULL;
1321 if (isdotent(fname, flen))
1322 return nfserr_exist;
1324 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1328 dentry = fhp->fh_dentry;
1330 host_err = fh_want_write(fhp);
1332 return nfserrno(host_err);
1334 fh_lock_nested(fhp, I_MUTEX_PARENT);
1335 dchild = lookup_one_len(fname, dentry, flen);
1336 host_err = PTR_ERR(dchild);
1338 return nfserrno(host_err);
1339 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1341 * We unconditionally drop our ref to dchild as fh_compose will have
1342 * already grabbed its own ref for it.
1347 return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1351 #ifdef CONFIG_NFSD_V3
1354 * NFSv3 and NFSv4 version of nfsd_create
1357 do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1358 char *fname, int flen, struct iattr *iap,
1359 struct svc_fh *resfhp, int createmode, u32 *verifier,
1360 bool *truncp, bool *created)
1362 struct dentry *dentry, *dchild = NULL;
1366 __u32 v_mtime=0, v_atime=0;
1372 if (isdotent(fname, flen))
1374 if (!(iap->ia_valid & ATTR_MODE))
1376 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1380 dentry = fhp->fh_dentry;
1381 dirp = d_inode(dentry);
1383 host_err = fh_want_write(fhp);
1387 fh_lock_nested(fhp, I_MUTEX_PARENT);
1390 * Compose the response file handle.
1392 dchild = lookup_one_len(fname, dentry, flen);
1393 host_err = PTR_ERR(dchild);
1397 /* If file doesn't exist, check for permissions to create one */
1398 if (d_really_is_negative(dchild)) {
1399 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1404 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1408 if (nfsd_create_is_exclusive(createmode)) {
1409 /* solaris7 gets confused (bugid 4218508) if these have
1410 * the high bit set, so just clear the high bits. If this is
1411 * ever changed to use different attrs for storing the
1412 * verifier, then do_open_lookup() will also need to be fixed
1415 v_mtime = verifier[0]&0x7fffffff;
1416 v_atime = verifier[1]&0x7fffffff;
1419 if (d_really_is_positive(dchild)) {
1422 switch (createmode) {
1423 case NFS3_CREATE_UNCHECKED:
1424 if (! d_is_reg(dchild))
1427 /* in nfsv4, we need to treat this case a little
1428 * differently. we don't want to truncate the
1429 * file now; this would be wrong if the OPEN
1430 * fails for some other reason. furthermore,
1431 * if the size is nonzero, we should ignore it
1432 * according to spec!
1434 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1437 iap->ia_valid &= ATTR_SIZE;
1441 case NFS3_CREATE_EXCLUSIVE:
1442 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1443 && d_inode(dchild)->i_atime.tv_sec == v_atime
1444 && d_inode(dchild)->i_size == 0 ) {
1450 case NFS4_CREATE_EXCLUSIVE4_1:
1451 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1452 && d_inode(dchild)->i_atime.tv_sec == v_atime
1453 && d_inode(dchild)->i_size == 0 ) {
1459 case NFS3_CREATE_GUARDED:
1466 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1474 nfsd_check_ignore_resizing(iap);
1476 if (nfsd_create_is_exclusive(createmode)) {
1477 /* Cram the verifier into atime/mtime */
1478 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1479 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1480 /* XXX someone who knows this better please fix it for nsec */
1481 iap->ia_mtime.tv_sec = v_mtime;
1482 iap->ia_atime.tv_sec = v_atime;
1483 iap->ia_mtime.tv_nsec = 0;
1484 iap->ia_atime.tv_nsec = 0;
1488 err = nfsd_create_setattr(rqstp, resfhp, iap);
1491 * nfsd_create_setattr already committed the child
1492 * (and possibly also the parent).
1495 err = nfserrno(commit_metadata(fhp));
1498 * Update the filehandle to get the new inode info.
1501 err = fh_update(resfhp);
1505 if (dchild && !IS_ERR(dchild))
1511 err = nfserrno(host_err);
1514 #endif /* CONFIG_NFSD_V3 */
1517 * Read a symlink. On entry, *lenp must contain the maximum path length that
1518 * fits into the buffer. On return, it contains the true length.
1519 * N.B. After this call fhp needs an fh_put
1522 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1527 DEFINE_DELAYED_CALL(done);
1530 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1534 path.mnt = fhp->fh_export->ex_path.mnt;
1535 path.dentry = fhp->fh_dentry;
1537 if (unlikely(!d_is_symlink(path.dentry)))
1538 return nfserr_inval;
1542 link = vfs_get_link(path.dentry, &done);
1544 return nfserrno(PTR_ERR(link));
1549 memcpy(buf, link, *lenp);
1550 do_delayed_call(&done);
1555 * Create a symlink and look up its inode
1556 * N.B. After this call _both_ fhp and resfhp need an fh_put
1559 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1560 char *fname, int flen,
1562 struct svc_fh *resfhp)
1564 struct dentry *dentry, *dnew;
1569 if (!flen || path[0] == '\0')
1572 if (isdotent(fname, flen))
1575 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1579 host_err = fh_want_write(fhp);
1584 dentry = fhp->fh_dentry;
1585 dnew = lookup_one_len(fname, dentry, flen);
1586 host_err = PTR_ERR(dnew);
1590 host_err = vfs_symlink(d_inode(dentry), dnew, path);
1591 err = nfserrno(host_err);
1593 err = nfserrno(commit_metadata(fhp));
1598 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1600 if (err==0) err = cerr;
1605 err = nfserrno(host_err);
1611 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1614 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1615 char *name, int len, struct svc_fh *tfhp)
1617 struct dentry *ddir, *dnew, *dold;
1622 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1625 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1629 if (d_is_dir(tfhp->fh_dentry))
1635 if (isdotent(name, len))
1638 host_err = fh_want_write(tfhp);
1640 err = nfserrno(host_err);
1644 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1645 ddir = ffhp->fh_dentry;
1646 dirp = d_inode(ddir);
1648 dnew = lookup_one_len(name, ddir, len);
1649 host_err = PTR_ERR(dnew);
1653 dold = tfhp->fh_dentry;
1656 if (d_really_is_negative(dold))
1658 host_err = vfs_link(dold, dirp, dnew, NULL);
1660 err = nfserrno(commit_metadata(ffhp));
1662 err = nfserrno(commit_metadata(tfhp));
1664 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1667 err = nfserrno(host_err);
1673 fh_drop_write(tfhp);
1678 err = nfserrno(host_err);
1684 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1687 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1688 struct svc_fh *tfhp, char *tname, int tlen)
1690 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1691 struct inode *fdir, *tdir;
1695 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1698 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1702 fdentry = ffhp->fh_dentry;
1703 fdir = d_inode(fdentry);
1705 tdentry = tfhp->fh_dentry;
1706 tdir = d_inode(tdentry);
1709 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1712 host_err = fh_want_write(ffhp);
1714 err = nfserrno(host_err);
1718 /* cannot use fh_lock as we need deadlock protective ordering
1719 * so do it by hand */
1720 trap = lock_rename(tdentry, fdentry);
1721 ffhp->fh_locked = tfhp->fh_locked = true;
1725 odentry = lookup_one_len(fname, fdentry, flen);
1726 host_err = PTR_ERR(odentry);
1727 if (IS_ERR(odentry))
1731 if (d_really_is_negative(odentry))
1734 if (odentry == trap)
1737 ndentry = lookup_one_len(tname, tdentry, tlen);
1738 host_err = PTR_ERR(ndentry);
1739 if (IS_ERR(ndentry))
1741 host_err = -ENOTEMPTY;
1742 if (ndentry == trap)
1746 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1748 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1751 host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
1753 host_err = commit_metadata(tfhp);
1755 host_err = commit_metadata(ffhp);
1762 err = nfserrno(host_err);
1764 * We cannot rely on fh_unlock on the two filehandles,
1765 * as that would do the wrong thing if the two directories
1766 * were the same, so again we do it by hand.
1768 fill_post_wcc(ffhp);
1769 fill_post_wcc(tfhp);
1770 unlock_rename(tdentry, fdentry);
1771 ffhp->fh_locked = tfhp->fh_locked = false;
1772 fh_drop_write(ffhp);
1779 * Unlink a file or directory
1780 * N.B. After this call fhp needs an fh_put
1783 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1784 char *fname, int flen)
1786 struct dentry *dentry, *rdentry;
1792 if (!flen || isdotent(fname, flen))
1794 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1798 host_err = fh_want_write(fhp);
1802 fh_lock_nested(fhp, I_MUTEX_PARENT);
1803 dentry = fhp->fh_dentry;
1804 dirp = d_inode(dentry);
1806 rdentry = lookup_one_len(fname, dentry, flen);
1807 host_err = PTR_ERR(rdentry);
1808 if (IS_ERR(rdentry))
1809 goto out_drop_write;
1811 if (d_really_is_negative(rdentry)) {
1814 goto out_drop_write;
1818 type = d_inode(rdentry)->i_mode & S_IFMT;
1820 if (type != S_IFDIR)
1821 host_err = vfs_unlink(dirp, rdentry, NULL);
1823 host_err = vfs_rmdir(dirp, rdentry);
1825 host_err = commit_metadata(fhp);
1831 err = nfserrno(host_err);
1837 * We do this buffering because we must not call back into the file
1838 * system's ->lookup() method from the filldir callback. That may well
1839 * deadlock a number of file systems.
1841 * This is based heavily on the implementation of same in XFS.
1843 struct buffered_dirent {
1847 unsigned int d_type;
1851 struct readdir_data {
1852 struct dir_context ctx;
1858 static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1859 int namlen, loff_t offset, u64 ino,
1860 unsigned int d_type)
1862 struct readdir_data *buf =
1863 container_of(ctx, struct readdir_data, ctx);
1864 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1865 unsigned int reclen;
1867 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1868 if (buf->used + reclen > PAGE_SIZE) {
1873 de->namlen = namlen;
1874 de->offset = offset;
1876 de->d_type = d_type;
1877 memcpy(de->name, name, namlen);
1878 buf->used += reclen;
1883 static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
1884 struct readdir_cd *cdp, loff_t *offsetp)
1886 struct buffered_dirent *de;
1890 struct readdir_data buf = {
1891 .ctx.actor = nfsd_buffered_filldir,
1892 .dirent = (void *)__get_free_page(GFP_KERNEL)
1896 return nfserrno(-ENOMEM);
1901 unsigned int reclen;
1903 cdp->err = nfserr_eof; /* will be cleared on successful read */
1907 host_err = iterate_dir(file, &buf.ctx);
1919 de = (struct buffered_dirent *)buf.dirent;
1921 offset = de->offset;
1923 if (func(cdp, de->name, de->namlen, de->offset,
1924 de->ino, de->d_type))
1927 if (cdp->err != nfs_ok)
1930 reclen = ALIGN(sizeof(*de) + de->namlen,
1933 de = (struct buffered_dirent *)((char *)de + reclen);
1935 if (size > 0) /* We bailed out early */
1938 offset = vfs_llseek(file, 0, SEEK_CUR);
1941 free_page((unsigned long)(buf.dirent));
1944 return nfserrno(host_err);
1951 * Read entries from a directory.
1952 * The NFSv3/4 verifier we ignore for now.
1955 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1956 struct readdir_cd *cdp, nfsd_filldir_t func)
1960 loff_t offset = *offsetp;
1961 int may_flags = NFSD_MAY_READ;
1963 /* NFSv2 only supports 32 bit cookies */
1964 if (rqstp->rq_vers > 2)
1965 may_flags |= NFSD_MAY_64BIT_COOKIE;
1967 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
1971 offset = vfs_llseek(file, offset, SEEK_SET);
1973 err = nfserrno((int)offset);
1977 err = nfsd_buffered_readdir(file, func, cdp, offsetp);
1979 if (err == nfserr_eof || err == nfserr_toosmall)
1980 err = nfs_ok; /* can still be found in ->err */
1988 * Get file system stats
1989 * N.B. After this call fhp needs an fh_put
1992 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
1996 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
1998 struct path path = {
1999 .mnt = fhp->fh_export->ex_path.mnt,
2000 .dentry = fhp->fh_dentry,
2002 if (vfs_statfs(&path, stat))
2008 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2010 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2014 * Check for a user's access permissions to this inode.
2017 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2018 struct dentry *dentry, int acc)
2020 struct inode *inode = d_inode(dentry);
2023 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2026 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2028 (acc & NFSD_MAY_READ)? " read" : "",
2029 (acc & NFSD_MAY_WRITE)? " write" : "",
2030 (acc & NFSD_MAY_EXEC)? " exec" : "",
2031 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2032 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2033 (acc & NFSD_MAY_LOCK)? " lock" : "",
2034 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2036 IS_IMMUTABLE(inode)? " immut" : "",
2037 IS_APPEND(inode)? " append" : "",
2038 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2039 dprintk(" owner %d/%d user %d/%d\n",
2040 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2043 /* Normally we reject any write/sattr etc access on a read-only file
2044 * system. But if it is IRIX doing check on write-access for a
2045 * device special file, we ignore rofs.
2047 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2048 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2049 if (exp_rdonly(rqstp, exp) ||
2050 __mnt_is_readonly(exp->ex_path.mnt))
2052 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2055 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2058 if (acc & NFSD_MAY_LOCK) {
2059 /* If we cannot rely on authentication in NLM requests,
2060 * just allow locks, otherwise require read permission, or
2063 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2066 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2069 * The file owner always gets access permission for accesses that
2070 * would normally be checked at open time. This is to make
2071 * file access work even when the client has done a fchmod(fd, 0).
2073 * However, `cp foo bar' should fail nevertheless when bar is
2074 * readonly. A sensible way to do this might be to reject all
2075 * attempts to truncate a read-only file, because a creat() call
2076 * always implies file truncation.
2077 * ... but this isn't really fair. A process may reasonably call
2078 * ftruncate on an open file descriptor on a file with perm 000.
2079 * We must trust the client to do permission checking - using "ACCESS"
2082 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2083 uid_eq(inode->i_uid, current_fsuid()))
2086 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2087 err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2089 /* Allow read access to binaries even when mode 111 */
2090 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2091 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2092 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2093 err = inode_permission(inode, MAY_EXEC);
2095 return err? nfserrno(err) : 0;
2099 nfsd_racache_shutdown(void)
2101 struct raparms *raparm, *last_raparm;
2104 dprintk("nfsd: freeing readahead buffers.\n");
2106 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2107 raparm = raparm_hash[i].pb_head;
2109 last_raparm = raparm;
2110 raparm = raparm->p_next;
2113 raparm_hash[i].pb_head = NULL;
2117 * Initialize readahead param cache
2120 nfsd_racache_init(int cache_size)
2125 struct raparms **raparm = NULL;
2128 if (raparm_hash[0].pb_head)
2130 nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2131 nperbucket = max(2, nperbucket);
2132 cache_size = nperbucket * RAPARM_HASH_SIZE;
2134 dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2136 for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2137 spin_lock_init(&raparm_hash[i].pb_lock);
2139 raparm = &raparm_hash[i].pb_head;
2140 for (j = 0; j < nperbucket; j++) {
2141 *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2144 raparm = &(*raparm)->p_next;
2149 nfsdstats.ra_size = cache_size;
2153 dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2154 nfsd_racache_shutdown();