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/pagemap.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <linux/exportfs.h>
33 #include <linux/writeback.h>
34 #include <linux/security.h>
39 #include "../internal.h"
43 #endif /* CONFIG_NFSD_V4 */
47 #include "filecache.h"
50 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
53 * nfserrno - Map Linux errnos to NFS errnos
54 * @errno: POSIX(-ish) error code to be mapped
56 * Returns the appropriate (net-endian) nfserr_* (or nfs_ok if errno is 0). If
57 * it's an error we don't expect, log it once and return nfserr_io.
67 { nfserr_perm, -EPERM },
68 { nfserr_noent, -ENOENT },
70 { nfserr_nxio, -ENXIO },
71 { nfserr_fbig, -E2BIG },
72 { nfserr_stale, -EBADF },
73 { nfserr_acces, -EACCES },
74 { nfserr_exist, -EEXIST },
75 { nfserr_xdev, -EXDEV },
76 { nfserr_mlink, -EMLINK },
77 { nfserr_nodev, -ENODEV },
78 { nfserr_notdir, -ENOTDIR },
79 { nfserr_isdir, -EISDIR },
80 { nfserr_inval, -EINVAL },
81 { nfserr_fbig, -EFBIG },
82 { nfserr_nospc, -ENOSPC },
83 { nfserr_rofs, -EROFS },
84 { nfserr_mlink, -EMLINK },
85 { nfserr_nametoolong, -ENAMETOOLONG },
86 { nfserr_notempty, -ENOTEMPTY },
87 { nfserr_dquot, -EDQUOT },
88 { nfserr_stale, -ESTALE },
89 { nfserr_jukebox, -ETIMEDOUT },
90 { nfserr_jukebox, -ERESTARTSYS },
91 { nfserr_jukebox, -EAGAIN },
92 { nfserr_jukebox, -EWOULDBLOCK },
93 { nfserr_jukebox, -ENOMEM },
94 { nfserr_io, -ETXTBSY },
95 { nfserr_notsupp, -EOPNOTSUPP },
96 { nfserr_toosmall, -ETOOSMALL },
97 { nfserr_serverfault, -ESERVERFAULT },
98 { nfserr_serverfault, -ENFILE },
99 { nfserr_io, -EREMOTEIO },
100 { nfserr_stale, -EOPENSTALE },
101 { nfserr_io, -EUCLEAN },
102 { nfserr_perm, -ENOKEY },
103 { nfserr_no_grace, -ENOGRACE},
107 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
108 if (nfs_errtbl[i].syserr == errno)
109 return nfs_errtbl[i].nfserr;
111 WARN_ONCE(1, "nfsd: non-standard errno: %d\n", errno);
116 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
118 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
119 * or nfs_ok having possibly changed *dpp and *expp
122 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
123 struct svc_export **expp)
125 struct svc_export *exp = *expp, *exp2 = NULL;
126 struct dentry *dentry = *dpp;
127 struct path path = {.mnt = mntget(exp->ex_path.mnt),
128 .dentry = dget(dentry)};
131 err = follow_down(&path);
134 if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
135 nfsd_mountpoint(dentry, exp) == 2) {
136 /* This is only a mountpoint in some other namespace */
141 exp2 = rqst_exp_get_by_name(rqstp, &path);
145 * We normally allow NFS clients to continue
146 * "underneath" a mountpoint that is not exported.
147 * The exception is V4ROOT, where no traversal is ever
148 * allowed without an explicit export of the new
151 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
156 if (nfsd_v4client(rqstp) ||
157 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
158 /* successfully crossed mount point */
160 * This is subtle: path.dentry is *not* on path.mnt
161 * at this point. The only reason we are safe is that
162 * original mnt is pinned down by exp, so we should
163 * put path *before* putting exp
166 path.dentry = dentry;
176 static void follow_to_parent(struct path *path)
180 while (path->dentry == path->mnt->mnt_root && follow_up(path))
182 dp = dget_parent(path->dentry);
187 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
189 struct svc_export *exp2;
190 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
191 .dentry = dget(dparent)};
193 follow_to_parent(&path);
195 exp2 = rqst_exp_parent(rqstp, &path);
196 if (PTR_ERR(exp2) == -ENOENT) {
197 *dentryp = dget(dparent);
198 } else if (IS_ERR(exp2)) {
200 return PTR_ERR(exp2);
202 *dentryp = dget(path.dentry);
211 * For nfsd purposes, we treat V4ROOT exports as though there was an
212 * export at *every* directory.
214 * '1' if this dentry *must* be an export point,
215 * '2' if it might be, if there is really a mount here, and
216 * '0' if there is no chance of an export point here.
218 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
220 if (!d_inode(dentry))
222 if (exp->ex_flags & NFSEXP_V4ROOT)
224 if (nfsd4_is_junction(dentry))
226 if (d_mountpoint(dentry))
228 * Might only be a mountpoint in a different namespace,
229 * but we need to check.
236 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
237 const char *name, unsigned int len,
238 struct svc_export **exp_ret, struct dentry **dentry_ret)
240 struct svc_export *exp;
241 struct dentry *dparent;
242 struct dentry *dentry;
245 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
247 dparent = fhp->fh_dentry;
248 exp = exp_get(fhp->fh_export);
250 /* Lookup the name, but don't follow links */
251 if (isdotent(name, len)) {
253 dentry = dget(dparent);
254 else if (dparent != exp->ex_path.dentry)
255 dentry = dget_parent(dparent);
256 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
257 dentry = dget(dparent); /* .. == . just like at / */
259 /* checking mountpoint crossing is very different when stepping up */
260 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
265 dentry = lookup_one_len_unlocked(name, dparent, len);
266 host_err = PTR_ERR(dentry);
269 if (nfsd_mountpoint(dentry, exp)) {
270 host_err = nfsd_cross_mnt(rqstp, &dentry, &exp);
277 *dentry_ret = dentry;
283 return nfserrno(host_err);
287 * nfsd_lookup - look up a single path component for nfsd
289 * @rqstp: the request context
290 * @fhp: the file handle of the directory
291 * @name: the component name, or %NULL to look up parent
292 * @len: length of name to examine
293 * @resfh: pointer to pre-initialised filehandle to hold result.
295 * Look up one component of a pathname.
296 * N.B. After this call _both_ fhp and resfh need an fh_put
298 * If the lookup would cross a mountpoint, and the mounted filesystem
299 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
300 * accepted as it stands and the mounted directory is
301 * returned. Otherwise the covered directory is returned.
302 * NOTE: this mountpoint crossing is not supported properly by all
303 * clients and is explicitly disallowed for NFSv3
307 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
308 unsigned int len, struct svc_fh *resfh)
310 struct svc_export *exp;
311 struct dentry *dentry;
314 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
317 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
320 err = check_nfsd_access(exp, rqstp);
324 * Note: we compose the file handle now, but as the
325 * dentry may be negative, it may need to be updated.
327 err = fh_compose(resfh, exp, dentry, fhp);
328 if (!err && d_really_is_negative(dentry))
337 * Commit metadata changes to stable storage.
340 commit_inode_metadata(struct inode *inode)
342 const struct export_operations *export_ops = inode->i_sb->s_export_op;
344 if (export_ops->commit_metadata)
345 return export_ops->commit_metadata(inode);
346 return sync_inode_metadata(inode, 1);
350 commit_metadata(struct svc_fh *fhp)
352 struct inode *inode = d_inode(fhp->fh_dentry);
354 if (!EX_ISSYNC(fhp->fh_export))
356 return commit_inode_metadata(inode);
360 * Go over the attributes and take care of the small differences between
361 * NFS semantics and what Linux expects.
364 nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
366 /* Ignore mode updates on symlinks */
367 if (S_ISLNK(inode->i_mode))
368 iap->ia_valid &= ~ATTR_MODE;
370 /* sanitize the mode change */
371 if (iap->ia_valid & ATTR_MODE) {
372 iap->ia_mode &= S_IALLUGO;
373 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
376 /* Revoke setuid/setgid on chown */
377 if (!S_ISDIR(inode->i_mode) &&
378 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
379 iap->ia_valid |= ATTR_KILL_PRIV;
380 if (iap->ia_valid & ATTR_MODE) {
381 /* we're setting mode too, just clear the s*id bits */
382 iap->ia_mode &= ~S_ISUID;
383 if (iap->ia_mode & S_IXGRP)
384 iap->ia_mode &= ~S_ISGID;
386 /* set ATTR_KILL_* bits and let VFS handle it */
387 iap->ia_valid |= ATTR_KILL_SUID;
389 setattr_should_drop_sgid(&init_user_ns, inode);
395 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
398 struct inode *inode = d_inode(fhp->fh_dentry);
400 if (iap->ia_size < inode->i_size) {
403 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
404 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
408 return nfserrno(get_write_access(inode));
411 static int __nfsd_setattr(struct dentry *dentry, struct iattr *iap)
415 if (iap->ia_valid & ATTR_SIZE) {
417 * RFC5661, Section 18.30.4:
418 * Changing the size of a file with SETATTR indirectly
419 * changes the time_modify and change attributes.
421 * (and similar for the older RFCs)
423 struct iattr size_attr = {
424 .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
425 .ia_size = iap->ia_size,
428 if (iap->ia_size < 0)
431 host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL);
434 iap->ia_valid &= ~ATTR_SIZE;
437 * Avoid the additional setattr call below if the only other
438 * attribute that the client sends is the mtime, as we update
439 * it as part of the size change above.
441 if ((iap->ia_valid & ~ATTR_MTIME) == 0)
448 iap->ia_valid |= ATTR_CTIME;
449 return notify_change(&init_user_ns, dentry, iap, NULL);
453 * nfsd_setattr - Set various file attributes.
454 * @rqstp: controlling RPC transaction
455 * @fhp: filehandle of target
456 * @attr: attributes to set
457 * @check_guard: set to 1 if guardtime is a valid timestamp
458 * @guardtime: do not act if ctime.tv_sec does not match this timestamp
460 * This call may adjust the contents of @attr (in particular, this
461 * call may change the bits in the na_iattr.ia_valid field).
463 * Returns nfs_ok on success, otherwise an NFS status code is
464 * returned. Caller must release @fhp by calling fh_put in either
468 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
469 struct nfsd_attrs *attr,
470 int check_guard, time64_t guardtime)
472 struct dentry *dentry;
474 struct iattr *iap = attr->na_iattr;
475 int accmode = NFSD_MAY_SATTR;
479 bool get_write_count;
480 bool size_change = (iap->ia_valid & ATTR_SIZE);
483 if (iap->ia_valid & ATTR_SIZE) {
484 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
489 * If utimes(2) and friends are called with times not NULL, we should
490 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
491 * will return EACCES, when the caller's effective UID does not match
492 * the owner of the file, and the caller is not privileged. In this
493 * situation, we should return EPERM(notify_change will return this).
495 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
496 accmode |= NFSD_MAY_OWNER_OVERRIDE;
497 if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
498 accmode |= NFSD_MAY_WRITE;
501 /* Callers that do fh_verify should do the fh_want_write: */
502 get_write_count = !fhp->fh_dentry;
505 err = fh_verify(rqstp, fhp, ftype, accmode);
508 if (get_write_count) {
509 host_err = fh_want_write(fhp);
514 dentry = fhp->fh_dentry;
515 inode = d_inode(dentry);
517 nfsd_sanitize_attrs(inode, iap);
519 if (check_guard && guardtime != inode->i_ctime.tv_sec)
520 return nfserr_notsync;
523 * The size case is special, it changes the file in addition to the
524 * attributes, and file systems don't expect it to be mixed with
525 * "random" attribute changes. We thus split out the size change
526 * into a separate call to ->setattr, and do the rest as a separate
530 err = nfsd_get_write_access(rqstp, fhp, iap);
536 for (retries = 1;;) {
540 * notify_change() can alter its iattr argument, making
541 * @iap unsuitable for submission multiple times. Make a
542 * copy for every loop iteration.
545 host_err = __nfsd_setattr(dentry, &attrs);
546 if (host_err != -EAGAIN || !retries--)
548 if (!nfsd_wait_for_delegreturn(rqstp, inode))
551 if (attr->na_seclabel && attr->na_seclabel->len)
552 attr->na_labelerr = security_inode_setsecctx(dentry,
553 attr->na_seclabel->data, attr->na_seclabel->len);
554 if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && attr->na_pacl)
555 attr->na_aclerr = set_posix_acl(&init_user_ns,
556 inode, ACL_TYPE_ACCESS,
558 if (IS_ENABLED(CONFIG_FS_POSIX_ACL) &&
559 !attr->na_aclerr && attr->na_dpacl && S_ISDIR(inode->i_mode))
560 attr->na_aclerr = set_posix_acl(&init_user_ns,
561 inode, ACL_TYPE_DEFAULT,
565 put_write_access(inode);
568 host_err = commit_metadata(fhp);
569 return nfserrno(host_err);
572 #if defined(CONFIG_NFSD_V4)
574 * NFS junction information is stored in an extended attribute.
576 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
579 * nfsd4_is_junction - Test if an object could be an NFS junction
581 * @dentry: object to test
583 * Returns 1 if "dentry" appears to contain NFS junction information.
584 * Otherwise 0 is returned.
586 int nfsd4_is_junction(struct dentry *dentry)
588 struct inode *inode = d_inode(dentry);
592 if (inode->i_mode & S_IXUGO)
594 if (!(inode->i_mode & S_ISVTX))
596 if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME,
602 static struct nfsd4_compound_state *nfsd4_get_cstate(struct svc_rqst *rqstp)
604 return &((struct nfsd4_compoundres *)rqstp->rq_resp)->cstate;
607 __be32 nfsd4_clone_file_range(struct svc_rqst *rqstp,
608 struct nfsd_file *nf_src, u64 src_pos,
609 struct nfsd_file *nf_dst, u64 dst_pos,
610 u64 count, bool sync)
612 struct file *src = nf_src->nf_file;
613 struct file *dst = nf_dst->nf_file;
618 since = READ_ONCE(dst->f_wb_err);
619 cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
621 ret = nfserrno(cloned);
624 if (count && cloned != count) {
625 ret = nfserrno(-EINVAL);
629 loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
630 int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
633 status = filemap_check_wb_err(dst->f_mapping, since);
635 status = commit_inode_metadata(file_inode(src));
637 struct nfsd_net *nn = net_generic(nf_dst->nf_net,
640 trace_nfsd_clone_file_range_err(rqstp,
641 &nfsd4_get_cstate(rqstp)->save_fh,
643 &nfsd4_get_cstate(rqstp)->current_fh,
646 nfsd_reset_write_verifier(nn);
647 trace_nfsd_writeverf_reset(nn, rqstp, status);
648 ret = nfserrno(status);
655 ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
656 u64 dst_pos, u64 count)
661 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
662 * thread and client rpc slot. The choice of 4MB is somewhat
663 * arbitrary. We might instead base this on r/wsize, or make it
664 * tunable, or use a time instead of a byte limit, or implement
665 * asynchronous copy. In theory a client could also recognize a
666 * limit like this and pipeline multiple COPY requests.
668 count = min_t(u64, count, 1 << 22);
669 ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
671 if (ret == -EOPNOTSUPP || ret == -EXDEV)
672 ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count,
677 __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
678 struct file *file, loff_t offset, loff_t len,
683 if (!S_ISREG(file_inode(file)->i_mode))
686 error = vfs_fallocate(file, flags, offset, len);
688 error = commit_metadata(fhp);
690 return nfserrno(error);
692 #endif /* defined(CONFIG_NFSD_V4) */
695 * Check server access rights to a file system object
701 static struct accessmap nfs3_regaccess[] = {
702 { NFS3_ACCESS_READ, NFSD_MAY_READ },
703 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
704 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
705 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
707 #ifdef CONFIG_NFSD_V4
708 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
709 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
710 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
716 static struct accessmap nfs3_diraccess[] = {
717 { NFS3_ACCESS_READ, NFSD_MAY_READ },
718 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
719 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
720 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
721 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
723 #ifdef CONFIG_NFSD_V4
724 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
725 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
726 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
732 static struct accessmap nfs3_anyaccess[] = {
733 /* Some clients - Solaris 2.6 at least, make an access call
734 * to the server to check for access for things like /dev/null
735 * (which really, the server doesn't care about). So
736 * We provide simple access checking for them, looking
737 * mainly at mode bits, and we make sure to ignore read-only
740 { NFS3_ACCESS_READ, NFSD_MAY_READ },
741 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
742 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
743 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
749 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
751 struct accessmap *map;
752 struct svc_export *export;
753 struct dentry *dentry;
754 u32 query, result = 0, sresult = 0;
757 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
761 export = fhp->fh_export;
762 dentry = fhp->fh_dentry;
764 if (d_is_reg(dentry))
765 map = nfs3_regaccess;
766 else if (d_is_dir(dentry))
767 map = nfs3_diraccess;
769 map = nfs3_anyaccess;
773 for (; map->access; map++) {
774 if (map->access & query) {
777 sresult |= map->access;
779 err2 = nfsd_permission(rqstp, export, dentry, map->how);
782 result |= map->access;
785 /* the following error codes just mean the access was not allowed,
786 * rather than an error occurred */
790 /* simply don't "or" in the access bit. */
800 *supported = sresult;
806 int nfsd_open_break_lease(struct inode *inode, int access)
810 if (access & NFSD_MAY_NOT_BREAK_LEASE)
812 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
813 return break_lease(inode, mode | O_NONBLOCK);
817 * Open an existing file or directory.
818 * The may_flags argument indicates the type of open (read/write/lock)
819 * and additional flags.
820 * N.B. After this call fhp needs an fh_put
823 __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
824 int may_flags, struct file **filp)
829 int flags = O_RDONLY|O_LARGEFILE;
833 path.mnt = fhp->fh_export->ex_path.mnt;
834 path.dentry = fhp->fh_dentry;
835 inode = d_inode(path.dentry);
838 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
844 host_err = nfsd_open_break_lease(inode, may_flags);
845 if (host_err) /* NOMEM or WOULDBLOCK */
848 if (may_flags & NFSD_MAY_WRITE) {
849 if (may_flags & NFSD_MAY_READ)
850 flags = O_RDWR|O_LARGEFILE;
852 flags = O_WRONLY|O_LARGEFILE;
855 file = dentry_open(&path, flags, current_cred());
857 host_err = PTR_ERR(file);
861 host_err = ima_file_check(file, may_flags);
867 if (may_flags & NFSD_MAY_64BIT_COOKIE)
868 file->f_mode |= FMODE_64BITHASH;
870 file->f_mode |= FMODE_32BITHASH;
874 err = nfserrno(host_err);
880 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
881 int may_flags, struct file **filp)
884 bool retried = false;
886 validate_process_creds();
888 * If we get here, then the client has already done an "open",
889 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
890 * in case a chmod has now revoked permission.
892 * Arguably we should also allow the owner override for
893 * directories, but we never have and it doesn't seem to have
894 * caused anyone a problem. If we were to change this, note
895 * also that our filldir callbacks would need a variant of
896 * lookup_one_len that doesn't check permissions.
899 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
901 err = fh_verify(rqstp, fhp, type, may_flags);
903 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
904 if (err == nfserr_stale && !retried) {
910 validate_process_creds();
915 * nfsd_open_verified - Open a regular file for the filecache
916 * @rqstp: RPC request
917 * @fhp: NFS filehandle of the file to open
918 * @may_flags: internal permission flags
919 * @filp: OUT: open "struct file *"
921 * Returns an nfsstat value in network byte order.
924 nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, int may_flags,
929 validate_process_creds();
930 err = __nfsd_open(rqstp, fhp, S_IFREG, may_flags, filp);
931 validate_process_creds();
936 * Grab and keep cached pages associated with a file in the svc_rqst
937 * so that they can be passed to the network sendmsg/sendpage routines
938 * directly. They will be released after the sending has completed.
941 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
942 struct splice_desc *sd)
944 struct svc_rqst *rqstp = sd->u.data;
945 struct page *page = buf->page; // may be a compound one
946 unsigned offset = buf->offset;
947 struct page *last_page;
949 last_page = page + (offset + sd->len - 1) / PAGE_SIZE;
950 for (page += offset / PAGE_SIZE; page <= last_page; page++) {
952 * Skip page replacement when extending the contents
953 * of the current page.
955 if (page == *(rqstp->rq_next_page - 1))
957 svc_rqst_replace_page(rqstp, page);
959 if (rqstp->rq_res.page_len == 0) // first call
960 rqstp->rq_res.page_base = offset % PAGE_SIZE;
961 rqstp->rq_res.page_len += sd->len;
965 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
966 struct splice_desc *sd)
968 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
971 static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
974 if (expected != 0 && len == 0)
976 if (offset+len >= i_size_read(file_inode(file)))
981 static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
982 struct file *file, loff_t offset,
983 unsigned long *count, u32 *eof, ssize_t host_err)
986 nfsd_stats_io_read_add(fhp->fh_export, host_err);
987 *eof = nfsd_eof_on_read(file, offset, host_err, *count);
989 fsnotify_access(file);
990 trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
993 trace_nfsd_read_err(rqstp, fhp, offset, host_err);
994 return nfserrno(host_err);
998 __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
999 struct file *file, loff_t offset, unsigned long *count,
1002 struct splice_desc sd = {
1004 .total_len = *count,
1010 trace_nfsd_read_splice(rqstp, fhp, offset, *count);
1011 rqstp->rq_next_page = rqstp->rq_respages + 1;
1012 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
1013 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
1016 __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
1017 struct file *file, loff_t offset,
1018 struct kvec *vec, int vlen, unsigned long *count,
1021 struct iov_iter iter;
1022 loff_t ppos = offset;
1025 trace_nfsd_read_vector(rqstp, fhp, offset, *count);
1026 iov_iter_kvec(&iter, ITER_DEST, vec, vlen, *count);
1027 host_err = vfs_iter_read(file, &iter, &ppos, 0);
1028 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
1032 * Gathered writes: If another process is currently writing to the file,
1033 * there's a high chance this is another nfsd (triggered by a bulk write
1034 * from a client's biod). Rather than syncing the file with each write
1035 * request, we sleep for 10 msec.
1037 * I don't know if this roughly approximates C. Juszak's idea of
1038 * gathered writes, but it's a nice and simple solution (IMHO), and it
1041 * Note: we do this only in the NFSv2 case, since v3 and higher have a
1042 * better tool (separate unstable writes and commits) for solving this
1045 static int wait_for_concurrent_writes(struct file *file)
1047 struct inode *inode = file_inode(file);
1048 static ino_t last_ino;
1049 static dev_t last_dev;
1052 if (atomic_read(&inode->i_writecount) > 1
1053 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
1054 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
1056 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1059 if (inode->i_state & I_DIRTY) {
1060 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1061 err = vfs_fsync(file, 0);
1063 last_ino = inode->i_ino;
1064 last_dev = inode->i_sb->s_dev;
1069 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
1070 loff_t offset, struct kvec *vec, int vlen,
1071 unsigned long *cnt, int stable,
1074 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1075 struct file *file = nf->nf_file;
1076 struct super_block *sb = file_inode(file)->i_sb;
1077 struct svc_export *exp;
1078 struct iov_iter iter;
1083 loff_t pos = offset;
1084 unsigned long exp_op_flags = 0;
1085 unsigned int pflags = current->flags;
1087 bool restore_flags = false;
1089 trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
1091 if (sb->s_export_op)
1092 exp_op_flags = sb->s_export_op->flags;
1094 if (test_bit(RQ_LOCAL, &rqstp->rq_flags) &&
1095 !(exp_op_flags & EXPORT_OP_REMOTE_FS)) {
1097 * We want throttling in balance_dirty_pages()
1098 * and shrink_inactive_list() to only consider
1099 * the backingdev we are writing to, so that nfs to
1100 * localhost doesn't cause nfsd to lock up due to all
1101 * the client's dirty pages or its congested queue.
1103 current->flags |= PF_LOCAL_THROTTLE;
1104 restore_flags = true;
1107 exp = fhp->fh_export;
1108 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1110 if (!EX_ISSYNC(exp))
1111 stable = NFS_UNSTABLE;
1113 if (stable && !use_wgather)
1116 iov_iter_kvec(&iter, ITER_SOURCE, vec, vlen, *cnt);
1117 since = READ_ONCE(file->f_wb_err);
1119 nfsd_copy_write_verifier(verf, nn);
1120 file_start_write(file);
1121 host_err = vfs_iter_write(file, &iter, &pos, flags);
1122 file_end_write(file);
1124 nfsd_reset_write_verifier(nn);
1125 trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1129 nfsd_stats_io_write_add(exp, *cnt);
1130 fsnotify_modify(file);
1131 host_err = filemap_check_wb_err(file->f_mapping, since);
1135 if (stable && use_wgather) {
1136 host_err = wait_for_concurrent_writes(file);
1138 nfsd_reset_write_verifier(nn);
1139 trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1144 if (host_err >= 0) {
1145 trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1148 trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1149 nfserr = nfserrno(host_err);
1152 current_restore_flags(pflags, PF_LOCAL_THROTTLE);
1157 * Read data from a file. count must contain the requested read count
1158 * on entry. On return, *count contains the number of bytes actually read.
1159 * N.B. After this call fhp needs an fh_put
1161 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1162 loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
1165 struct nfsd_file *nf;
1169 trace_nfsd_read_start(rqstp, fhp, offset, *count);
1170 err = nfsd_file_acquire_gc(rqstp, fhp, NFSD_MAY_READ, &nf);
1175 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
1176 err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
1178 err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
1182 trace_nfsd_read_done(rqstp, fhp, offset, *count);
1188 * Write data to a file.
1189 * The stable flag requests synchronous writes.
1190 * N.B. After this call fhp needs an fh_put
1193 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1194 struct kvec *vec, int vlen, unsigned long *cnt, int stable,
1197 struct nfsd_file *nf;
1200 trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
1202 err = nfsd_file_acquire_gc(rqstp, fhp, NFSD_MAY_WRITE, &nf);
1206 err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
1207 vlen, cnt, stable, verf);
1210 trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
1215 * nfsd_commit - Commit pending writes to stable storage
1216 * @rqstp: RPC request being processed
1217 * @fhp: NFS filehandle
1219 * @offset: raw offset from beginning of file
1220 * @count: raw count of bytes to sync
1221 * @verf: filled in with the server's current write verifier
1223 * Note: we guarantee that data that lies within the range specified
1224 * by the 'offset' and 'count' parameters will be synced. The server
1225 * is permitted to sync data that lies outside this range at the
1228 * Unfortunately we cannot lock the file to make sure we return full WCC
1229 * data to the client, as locking happens lower down in the filesystem.
1232 * An nfsstat value in network byte order.
1235 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
1236 u64 offset, u32 count, __be32 *verf)
1238 __be32 err = nfs_ok;
1241 struct nfsd_net *nn;
1244 * Convert the client-provided (offset, count) range to a
1245 * (start, end) range. If the client-provided range falls
1246 * outside the maximum file size of the underlying FS,
1247 * clamp the sync range appropriately.
1251 maxbytes = (u64)fhp->fh_dentry->d_sb->s_maxbytes;
1252 if (offset < maxbytes) {
1254 if (count && (offset + count - 1 < maxbytes))
1255 end = offset + count - 1;
1258 nn = net_generic(nf->nf_net, nfsd_net_id);
1259 if (EX_ISSYNC(fhp->fh_export)) {
1260 errseq_t since = READ_ONCE(nf->nf_file->f_wb_err);
1263 err2 = vfs_fsync_range(nf->nf_file, start, end, 0);
1266 nfsd_copy_write_verifier(verf, nn);
1267 err2 = filemap_check_wb_err(nf->nf_file->f_mapping,
1269 err = nfserrno(err2);
1272 err = nfserr_notsupp;
1275 nfsd_reset_write_verifier(nn);
1276 trace_nfsd_writeverf_reset(nn, rqstp, err2);
1277 err = nfserrno(err2);
1280 nfsd_copy_write_verifier(verf, nn);
1286 * nfsd_create_setattr - Set a created file's attributes
1287 * @rqstp: RPC transaction being executed
1288 * @fhp: NFS filehandle of parent directory
1289 * @resfhp: NFS filehandle of new object
1290 * @attrs: requested attributes of new object
1292 * Returns nfs_ok on success, or an nfsstat in network byte order.
1295 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
1296 struct svc_fh *resfhp, struct nfsd_attrs *attrs)
1298 struct iattr *iap = attrs->na_iattr;
1302 * Mode has already been set by file creation.
1304 iap->ia_valid &= ~ATTR_MODE;
1307 * Setting uid/gid works only for root. Irix appears to
1308 * send along the gid on create when it tries to implement
1309 * setgid directories via NFS:
1311 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1312 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1315 * Callers expect new file metadata to be committed even
1316 * if the attributes have not changed.
1319 status = nfsd_setattr(rqstp, resfhp, attrs, 0, (time64_t)0);
1321 status = nfserrno(commit_metadata(resfhp));
1324 * Transactional filesystems had a chance to commit changes
1325 * for both parent and child simultaneously making the
1326 * following commit_metadata a noop in many cases.
1329 status = nfserrno(commit_metadata(fhp));
1332 * Update the new filehandle to pick up the new attributes.
1335 status = fh_update(resfhp);
1340 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1341 * setting size to 0 may fail for some specific file systems by the permission
1342 * checking which requires WRITE permission but the mode is 000.
1343 * we ignore the resizing(to 0) on the just new created file, since the size is
1344 * 0 after file created.
1346 * call this only after vfs_create() is called.
1349 nfsd_check_ignore_resizing(struct iattr *iap)
1351 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1352 iap->ia_valid &= ~ATTR_SIZE;
1355 /* The parent directory should already be locked: */
1357 nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
1358 struct nfsd_attrs *attrs,
1359 int type, dev_t rdev, struct svc_fh *resfhp)
1361 struct dentry *dentry, *dchild;
1363 struct iattr *iap = attrs->na_iattr;
1367 dentry = fhp->fh_dentry;
1368 dirp = d_inode(dentry);
1370 dchild = dget(resfhp->fh_dentry);
1371 err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1375 if (!(iap->ia_valid & ATTR_MODE))
1377 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1379 if (!IS_POSIXACL(dirp))
1380 iap->ia_mode &= ~current_umask();
1385 host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
1387 nfsd_check_ignore_resizing(iap);
1390 host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode);
1391 if (!host_err && unlikely(d_unhashed(dchild))) {
1393 d = lookup_one_len(dchild->d_name.name,
1395 dchild->d_name.len);
1397 host_err = PTR_ERR(d);
1400 if (unlikely(d_is_negative(d))) {
1402 err = nfserr_serverfault;
1405 dput(resfhp->fh_dentry);
1406 resfhp->fh_dentry = dget(d);
1407 err = fh_update(resfhp);
1418 host_err = vfs_mknod(&init_user_ns, dirp, dchild,
1419 iap->ia_mode, rdev);
1422 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1429 err = nfsd_create_setattr(rqstp, fhp, resfhp, attrs);
1436 err = nfserrno(host_err);
1441 * Create a filesystem object (regular, directory, special).
1442 * Note that the parent directory is left locked.
1444 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1447 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1448 char *fname, int flen, struct nfsd_attrs *attrs,
1449 int type, dev_t rdev, struct svc_fh *resfhp)
1451 struct dentry *dentry, *dchild = NULL;
1455 if (isdotent(fname, flen))
1456 return nfserr_exist;
1458 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1462 dentry = fhp->fh_dentry;
1464 host_err = fh_want_write(fhp);
1466 return nfserrno(host_err);
1468 inode_lock_nested(dentry->d_inode, I_MUTEX_PARENT);
1469 dchild = lookup_one_len(fname, dentry, flen);
1470 host_err = PTR_ERR(dchild);
1471 if (IS_ERR(dchild)) {
1472 err = nfserrno(host_err);
1475 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1477 * We unconditionally drop our ref to dchild as fh_compose will have
1478 * already grabbed its own ref for it.
1483 fh_fill_pre_attrs(fhp);
1484 err = nfsd_create_locked(rqstp, fhp, attrs, type, rdev, resfhp);
1485 fh_fill_post_attrs(fhp);
1487 inode_unlock(dentry->d_inode);
1492 * Read a symlink. On entry, *lenp must contain the maximum path length that
1493 * fits into the buffer. On return, it contains the true length.
1494 * N.B. After this call fhp needs an fh_put
1497 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1502 DEFINE_DELAYED_CALL(done);
1505 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1509 path.mnt = fhp->fh_export->ex_path.mnt;
1510 path.dentry = fhp->fh_dentry;
1512 if (unlikely(!d_is_symlink(path.dentry)))
1513 return nfserr_inval;
1517 link = vfs_get_link(path.dentry, &done);
1519 return nfserrno(PTR_ERR(link));
1524 memcpy(buf, link, *lenp);
1525 do_delayed_call(&done);
1530 * nfsd_symlink - Create a symlink and look up its inode
1531 * @rqstp: RPC transaction being executed
1532 * @fhp: NFS filehandle of parent directory
1533 * @fname: filename of the new symlink
1534 * @flen: length of @fname
1535 * @path: content of the new symlink (NUL-terminated)
1536 * @attrs: requested attributes of new object
1537 * @resfhp: NFS filehandle of new object
1539 * N.B. After this call _both_ fhp and resfhp need an fh_put
1541 * Returns nfs_ok on success, or an nfsstat in network byte order.
1544 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1545 char *fname, int flen,
1546 char *path, struct nfsd_attrs *attrs,
1547 struct svc_fh *resfhp)
1549 struct dentry *dentry, *dnew;
1554 if (!flen || path[0] == '\0')
1557 if (isdotent(fname, flen))
1560 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1564 host_err = fh_want_write(fhp);
1566 err = nfserrno(host_err);
1570 dentry = fhp->fh_dentry;
1571 inode_lock_nested(dentry->d_inode, I_MUTEX_PARENT);
1572 dnew = lookup_one_len(fname, dentry, flen);
1574 err = nfserrno(PTR_ERR(dnew));
1575 inode_unlock(dentry->d_inode);
1576 goto out_drop_write;
1578 fh_fill_pre_attrs(fhp);
1579 host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
1580 err = nfserrno(host_err);
1581 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1583 nfsd_create_setattr(rqstp, fhp, resfhp, attrs);
1584 fh_fill_post_attrs(fhp);
1585 inode_unlock(dentry->d_inode);
1587 err = nfserrno(commit_metadata(fhp));
1589 if (err==0) err = cerr;
1598 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1601 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1602 char *name, int len, struct svc_fh *tfhp)
1604 struct dentry *ddir, *dnew, *dold;
1609 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1612 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1616 if (d_is_dir(tfhp->fh_dentry))
1622 if (isdotent(name, len))
1625 host_err = fh_want_write(tfhp);
1627 err = nfserrno(host_err);
1631 ddir = ffhp->fh_dentry;
1632 dirp = d_inode(ddir);
1633 inode_lock_nested(dirp, I_MUTEX_PARENT);
1635 dnew = lookup_one_len(name, ddir, len);
1637 err = nfserrno(PTR_ERR(dnew));
1641 dold = tfhp->fh_dentry;
1644 if (d_really_is_negative(dold))
1646 fh_fill_pre_attrs(ffhp);
1647 host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
1648 fh_fill_post_attrs(ffhp);
1651 err = nfserrno(commit_metadata(ffhp));
1653 err = nfserrno(commit_metadata(tfhp));
1655 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1658 err = nfserrno(host_err);
1662 fh_drop_write(tfhp);
1670 goto out_drop_write;
1674 nfsd_close_cached_files(struct dentry *dentry)
1676 struct inode *inode = d_inode(dentry);
1678 if (inode && S_ISREG(inode->i_mode))
1679 nfsd_file_close_inode_sync(inode);
1683 nfsd_has_cached_files(struct dentry *dentry)
1686 struct inode *inode = d_inode(dentry);
1688 if (inode && S_ISREG(inode->i_mode))
1689 ret = nfsd_file_is_cached(inode);
1695 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1698 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1699 struct svc_fh *tfhp, char *tname, int tlen)
1701 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1702 struct inode *fdir, *tdir;
1705 bool close_cached = false;
1707 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1710 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1714 fdentry = ffhp->fh_dentry;
1715 fdir = d_inode(fdentry);
1717 tdentry = tfhp->fh_dentry;
1718 tdir = d_inode(tdentry);
1721 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1724 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1725 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1727 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1731 host_err = fh_want_write(ffhp);
1733 err = nfserrno(host_err);
1737 trap = lock_rename(tdentry, fdentry);
1738 fh_fill_pre_attrs(ffhp);
1739 fh_fill_pre_attrs(tfhp);
1741 odentry = lookup_one_len(fname, fdentry, flen);
1742 host_err = PTR_ERR(odentry);
1743 if (IS_ERR(odentry))
1747 if (d_really_is_negative(odentry))
1750 if (odentry == trap)
1753 ndentry = lookup_one_len(tname, tdentry, tlen);
1754 host_err = PTR_ERR(ndentry);
1755 if (IS_ERR(ndentry))
1757 host_err = -ENOTEMPTY;
1758 if (ndentry == trap)
1761 if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) &&
1762 nfsd_has_cached_files(ndentry)) {
1763 close_cached = true;
1766 struct renamedata rd = {
1767 .old_mnt_userns = &init_user_ns,
1769 .old_dentry = odentry,
1770 .new_mnt_userns = &init_user_ns,
1772 .new_dentry = ndentry,
1776 for (retries = 1;;) {
1777 host_err = vfs_rename(&rd);
1778 if (host_err != -EAGAIN || !retries--)
1780 if (!nfsd_wait_for_delegreturn(rqstp, d_inode(odentry)))
1784 host_err = commit_metadata(tfhp);
1786 host_err = commit_metadata(ffhp);
1794 err = nfserrno(host_err);
1796 if (!close_cached) {
1797 fh_fill_post_attrs(ffhp);
1798 fh_fill_post_attrs(tfhp);
1800 unlock_rename(tdentry, fdentry);
1801 fh_drop_write(ffhp);
1804 * If the target dentry has cached open files, then we need to try to
1805 * close them prior to doing the rename. Flushing delayed fput
1806 * shouldn't be done with locks held however, so we delay it until this
1807 * point and then reattempt the whole shebang.
1810 close_cached = false;
1811 nfsd_close_cached_files(ndentry);
1820 * Unlink a file or directory
1821 * N.B. After this call fhp needs an fh_put
1824 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1825 char *fname, int flen)
1827 struct dentry *dentry, *rdentry;
1829 struct inode *rinode;
1834 if (!flen || isdotent(fname, flen))
1836 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1840 host_err = fh_want_write(fhp);
1844 dentry = fhp->fh_dentry;
1845 dirp = d_inode(dentry);
1846 inode_lock_nested(dirp, I_MUTEX_PARENT);
1848 rdentry = lookup_one_len(fname, dentry, flen);
1849 host_err = PTR_ERR(rdentry);
1850 if (IS_ERR(rdentry))
1853 if (d_really_is_negative(rdentry)) {
1858 rinode = d_inode(rdentry);
1862 type = d_inode(rdentry)->i_mode & S_IFMT;
1864 fh_fill_pre_attrs(fhp);
1865 if (type != S_IFDIR) {
1868 if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK)
1869 nfsd_close_cached_files(rdentry);
1871 for (retries = 1;;) {
1872 host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL);
1873 if (host_err != -EAGAIN || !retries--)
1875 if (!nfsd_wait_for_delegreturn(rqstp, rinode))
1879 host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
1881 fh_fill_post_attrs(fhp);
1885 host_err = commit_metadata(fhp);
1887 iput(rinode); /* truncate the inode here */
1892 if (host_err == -EBUSY) {
1893 /* name is mounted-on. There is no perfect
1896 if (nfsd_v4client(rqstp))
1897 err = nfserr_file_open;
1901 err = nfserrno(host_err);
1907 goto out_drop_write;
1911 * We do this buffering because we must not call back into the file
1912 * system's ->lookup() method from the filldir callback. That may well
1913 * deadlock a number of file systems.
1915 * This is based heavily on the implementation of same in XFS.
1917 struct buffered_dirent {
1921 unsigned int d_type;
1925 struct readdir_data {
1926 struct dir_context ctx;
1932 static bool nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1933 int namlen, loff_t offset, u64 ino,
1934 unsigned int d_type)
1936 struct readdir_data *buf =
1937 container_of(ctx, struct readdir_data, ctx);
1938 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1939 unsigned int reclen;
1941 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1942 if (buf->used + reclen > PAGE_SIZE) {
1947 de->namlen = namlen;
1948 de->offset = offset;
1950 de->d_type = d_type;
1951 memcpy(de->name, name, namlen);
1952 buf->used += reclen;
1957 static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp,
1958 nfsd_filldir_t func, struct readdir_cd *cdp,
1961 struct buffered_dirent *de;
1965 struct readdir_data buf = {
1966 .ctx.actor = nfsd_buffered_filldir,
1967 .dirent = (void *)__get_free_page(GFP_KERNEL)
1971 return nfserrno(-ENOMEM);
1976 unsigned int reclen;
1978 cdp->err = nfserr_eof; /* will be cleared on successful read */
1982 host_err = iterate_dir(file, &buf.ctx);
1994 de = (struct buffered_dirent *)buf.dirent;
1996 offset = de->offset;
1998 if (func(cdp, de->name, de->namlen, de->offset,
1999 de->ino, de->d_type))
2002 if (cdp->err != nfs_ok)
2005 trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen);
2007 reclen = ALIGN(sizeof(*de) + de->namlen,
2010 de = (struct buffered_dirent *)((char *)de + reclen);
2012 if (size > 0) /* We bailed out early */
2015 offset = vfs_llseek(file, 0, SEEK_CUR);
2018 free_page((unsigned long)(buf.dirent));
2021 return nfserrno(host_err);
2028 * Read entries from a directory.
2029 * The NFSv3/4 verifier we ignore for now.
2032 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2033 struct readdir_cd *cdp, nfsd_filldir_t func)
2037 loff_t offset = *offsetp;
2038 int may_flags = NFSD_MAY_READ;
2040 /* NFSv2 only supports 32 bit cookies */
2041 if (rqstp->rq_vers > 2)
2042 may_flags |= NFSD_MAY_64BIT_COOKIE;
2044 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
2048 offset = vfs_llseek(file, offset, SEEK_SET);
2050 err = nfserrno((int)offset);
2054 err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp);
2056 if (err == nfserr_eof || err == nfserr_toosmall)
2057 err = nfs_ok; /* can still be found in ->err */
2065 * Get file system stats
2066 * N.B. After this call fhp needs an fh_put
2069 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2073 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2075 struct path path = {
2076 .mnt = fhp->fh_export->ex_path.mnt,
2077 .dentry = fhp->fh_dentry,
2079 if (vfs_statfs(&path, stat))
2085 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2087 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2090 #ifdef CONFIG_NFSD_V4
2092 * Helper function to translate error numbers. In the case of xattr operations,
2093 * some error codes need to be translated outside of the standard translations.
2095 * ENODATA needs to be translated to nfserr_noxattr.
2096 * E2BIG to nfserr_xattr2big.
2098 * Additionally, vfs_listxattr can return -ERANGE. This means that the
2099 * file has too many extended attributes to retrieve inside an
2100 * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
2101 * filesystems will allow the adding of extended attributes until they hit
2102 * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
2103 * So, at that point, the attributes are present and valid, but can't
2104 * be retrieved using listxattr, since the upper level xattr code enforces
2105 * the XATTR_LIST_MAX limit.
2107 * This bug means that we need to deal with listxattr returning -ERANGE. The
2108 * best mapping is to return TOOSMALL.
2111 nfsd_xattr_errno(int err)
2115 return nfserr_noxattr;
2117 return nfserr_xattr2big;
2119 return nfserr_toosmall;
2121 return nfserrno(err);
2125 * Retrieve the specified user extended attribute. To avoid always
2126 * having to allocate the maximum size (since we are not getting
2127 * a maximum size from the RPC), do a probe + alloc. Hold a reader
2128 * lock on i_rwsem to prevent the extended attribute from changing
2129 * size while we're doing this.
2132 nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2133 void **bufp, int *lenp)
2138 struct inode *inode;
2139 struct dentry *dentry;
2141 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2146 dentry = fhp->fh_dentry;
2147 inode = d_inode(dentry);
2149 inode_lock_shared(inode);
2151 len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
2154 * Zero-length attribute, just return.
2163 err = nfsd_xattr_errno(len);
2168 err = nfserr_toosmall;
2172 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2174 err = nfserr_jukebox;
2178 len = vfs_getxattr(&init_user_ns, dentry, name, buf, len);
2182 err = nfsd_xattr_errno(len);
2189 inode_unlock_shared(inode);
2195 * Retrieve the xattr names. Since we can't know how many are
2196 * user extended attributes, we must get all attributes here,
2197 * and have the XDR encode filter out the "user." ones.
2199 * While this could always just allocate an XATTR_LIST_MAX
2200 * buffer, that's a waste, so do a probe + allocate. To
2201 * avoid any changes between the probe and allocate, wrap
2202 * this in inode_lock.
2205 nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
2211 struct inode *inode;
2212 struct dentry *dentry;
2214 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2218 dentry = fhp->fh_dentry;
2219 inode = d_inode(dentry);
2222 inode_lock_shared(inode);
2224 len = vfs_listxattr(dentry, NULL, 0);
2226 err = nfsd_xattr_errno(len);
2230 if (len > XATTR_LIST_MAX) {
2231 err = nfserr_xattr2big;
2236 * We're holding i_rwsem - use GFP_NOFS.
2238 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2240 err = nfserr_jukebox;
2244 len = vfs_listxattr(dentry, buf, len);
2247 err = nfsd_xattr_errno(len);
2256 inode_unlock_shared(inode);
2262 * nfsd_removexattr - Remove an extended attribute
2263 * @rqstp: RPC transaction being executed
2264 * @fhp: NFS filehandle of object with xattr to remove
2265 * @name: name of xattr to remove (NUL-terminate)
2267 * Pass in a NULL pointer for delegated_inode, and let the client deal
2268 * with NFS4ERR_DELAY (same as with e.g. setattr and remove).
2270 * Returns nfs_ok on success, or an nfsstat in network byte order.
2273 nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
2278 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2282 ret = fh_want_write(fhp);
2284 return nfserrno(ret);
2286 inode_lock(fhp->fh_dentry->d_inode);
2287 fh_fill_pre_attrs(fhp);
2289 ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
2292 fh_fill_post_attrs(fhp);
2293 inode_unlock(fhp->fh_dentry->d_inode);
2296 return nfsd_xattr_errno(ret);
2300 nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2301 void *buf, u32 len, u32 flags)
2306 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2310 ret = fh_want_write(fhp);
2312 return nfserrno(ret);
2313 inode_lock(fhp->fh_dentry->d_inode);
2314 fh_fill_pre_attrs(fhp);
2316 ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
2318 fh_fill_post_attrs(fhp);
2319 inode_unlock(fhp->fh_dentry->d_inode);
2322 return nfsd_xattr_errno(ret);
2327 * Check for a user's access permissions to this inode.
2330 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2331 struct dentry *dentry, int acc)
2333 struct inode *inode = d_inode(dentry);
2336 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2339 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2341 (acc & NFSD_MAY_READ)? " read" : "",
2342 (acc & NFSD_MAY_WRITE)? " write" : "",
2343 (acc & NFSD_MAY_EXEC)? " exec" : "",
2344 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2345 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2346 (acc & NFSD_MAY_LOCK)? " lock" : "",
2347 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2349 IS_IMMUTABLE(inode)? " immut" : "",
2350 IS_APPEND(inode)? " append" : "",
2351 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2352 dprintk(" owner %d/%d user %d/%d\n",
2353 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2356 /* Normally we reject any write/sattr etc access on a read-only file
2357 * system. But if it is IRIX doing check on write-access for a
2358 * device special file, we ignore rofs.
2360 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2361 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2362 if (exp_rdonly(rqstp, exp) ||
2363 __mnt_is_readonly(exp->ex_path.mnt))
2365 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2368 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2371 if (acc & NFSD_MAY_LOCK) {
2372 /* If we cannot rely on authentication in NLM requests,
2373 * just allow locks, otherwise require read permission, or
2376 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2379 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2382 * The file owner always gets access permission for accesses that
2383 * would normally be checked at open time. This is to make
2384 * file access work even when the client has done a fchmod(fd, 0).
2386 * However, `cp foo bar' should fail nevertheless when bar is
2387 * readonly. A sensible way to do this might be to reject all
2388 * attempts to truncate a read-only file, because a creat() call
2389 * always implies file truncation.
2390 * ... but this isn't really fair. A process may reasonably call
2391 * ftruncate on an open file descriptor on a file with perm 000.
2392 * We must trust the client to do permission checking - using "ACCESS"
2395 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2396 uid_eq(inode->i_uid, current_fsuid()))
2399 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2400 err = inode_permission(&init_user_ns, inode,
2401 acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
2403 /* Allow read access to binaries even when mode 111 */
2404 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2405 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2406 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2407 err = inode_permission(&init_user_ns, inode, MAY_EXEC);
2409 return err? nfserrno(err) : 0;