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.
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"
44 #endif /* CONFIG_NFSD_V4 */
48 #include "filecache.h"
51 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
54 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
56 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
57 * or nfs_ok having possibly changed *dpp and *expp
60 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
61 struct svc_export **expp)
63 struct svc_export *exp = *expp, *exp2 = NULL;
64 struct dentry *dentry = *dpp;
65 struct path path = {.mnt = mntget(exp->ex_path.mnt),
66 .dentry = dget(dentry)};
69 err = follow_down(&path);
72 if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
73 nfsd_mountpoint(dentry, exp) == 2) {
74 /* This is only a mountpoint in some other namespace */
79 exp2 = rqst_exp_get_by_name(rqstp, &path);
83 * We normally allow NFS clients to continue
84 * "underneath" a mountpoint that is not exported.
85 * The exception is V4ROOT, where no traversal is ever
86 * allowed without an explicit export of the new
89 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
94 if (nfsd_v4client(rqstp) ||
95 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
96 /* successfully crossed mount point */
98 * This is subtle: path.dentry is *not* on path.mnt
99 * at this point. The only reason we are safe is that
100 * original mnt is pinned down by exp, so we should
101 * put path *before* putting exp
104 path.dentry = dentry;
114 static void follow_to_parent(struct path *path)
118 while (path->dentry == path->mnt->mnt_root && follow_up(path))
120 dp = dget_parent(path->dentry);
125 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
127 struct svc_export *exp2;
128 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
129 .dentry = dget(dparent)};
131 follow_to_parent(&path);
133 exp2 = rqst_exp_parent(rqstp, &path);
134 if (PTR_ERR(exp2) == -ENOENT) {
135 *dentryp = dget(dparent);
136 } else if (IS_ERR(exp2)) {
138 return PTR_ERR(exp2);
140 *dentryp = dget(path.dentry);
149 * For nfsd purposes, we treat V4ROOT exports as though there was an
150 * export at *every* directory.
152 * '1' if this dentry *must* be an export point,
153 * '2' if it might be, if there is really a mount here, and
154 * '0' if there is no chance of an export point here.
156 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
158 if (!d_inode(dentry))
160 if (exp->ex_flags & NFSEXP_V4ROOT)
162 if (nfsd4_is_junction(dentry))
164 if (d_mountpoint(dentry))
166 * Might only be a mountpoint in a different namespace,
167 * but we need to check.
174 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
175 const char *name, unsigned int len,
176 struct svc_export **exp_ret, struct dentry **dentry_ret)
178 struct svc_export *exp;
179 struct dentry *dparent;
180 struct dentry *dentry;
183 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
185 dparent = fhp->fh_dentry;
186 exp = exp_get(fhp->fh_export);
188 /* Lookup the name, but don't follow links */
189 if (isdotent(name, len)) {
191 dentry = dget(dparent);
192 else if (dparent != exp->ex_path.dentry)
193 dentry = dget_parent(dparent);
194 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
195 dentry = dget(dparent); /* .. == . just like at / */
197 /* checking mountpoint crossing is very different when stepping up */
198 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
204 * In the nfsd4_open() case, this may be held across
205 * subsequent open and delegation acquisition which may
206 * need to take the child's i_mutex:
208 fh_lock_nested(fhp, I_MUTEX_PARENT);
209 dentry = lookup_one_len(name, dparent, len);
210 host_err = PTR_ERR(dentry);
213 if (nfsd_mountpoint(dentry, exp)) {
215 * We don't need the i_mutex after all. It's
216 * still possible we could open this (regular
217 * files can be mountpoints too), but the
218 * i_mutex is just there to prevent renames of
219 * something that we might be about to delegate,
220 * and a mountpoint won't be renamed:
223 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
229 *dentry_ret = dentry;
235 return nfserrno(host_err);
239 * Look up one component of a pathname.
240 * N.B. After this call _both_ fhp and resfh need an fh_put
242 * If the lookup would cross a mountpoint, and the mounted filesystem
243 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
244 * accepted as it stands and the mounted directory is
245 * returned. Otherwise the covered directory is returned.
246 * NOTE: this mountpoint crossing is not supported properly by all
247 * clients and is explicitly disallowed for NFSv3
251 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
252 unsigned int len, struct svc_fh *resfh)
254 struct svc_export *exp;
255 struct dentry *dentry;
258 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
261 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
264 err = check_nfsd_access(exp, rqstp);
268 * Note: we compose the file handle now, but as the
269 * dentry may be negative, it may need to be updated.
271 err = fh_compose(resfh, exp, dentry, fhp);
272 if (!err && d_really_is_negative(dentry))
281 * Commit metadata changes to stable storage.
284 commit_inode_metadata(struct inode *inode)
286 const struct export_operations *export_ops = inode->i_sb->s_export_op;
288 if (export_ops->commit_metadata)
289 return export_ops->commit_metadata(inode);
290 return sync_inode_metadata(inode, 1);
294 commit_metadata(struct svc_fh *fhp)
296 struct inode *inode = d_inode(fhp->fh_dentry);
298 if (!EX_ISSYNC(fhp->fh_export))
300 return commit_inode_metadata(inode);
304 * Go over the attributes and take care of the small differences between
305 * NFS semantics and what Linux expects.
308 nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
310 /* sanitize the mode change */
311 if (iap->ia_valid & ATTR_MODE) {
312 iap->ia_mode &= S_IALLUGO;
313 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
316 /* Revoke setuid/setgid on chown */
317 if (!S_ISDIR(inode->i_mode) &&
318 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
319 iap->ia_valid |= ATTR_KILL_PRIV;
320 if (iap->ia_valid & ATTR_MODE) {
321 /* we're setting mode too, just clear the s*id bits */
322 iap->ia_mode &= ~S_ISUID;
323 if (iap->ia_mode & S_IXGRP)
324 iap->ia_mode &= ~S_ISGID;
326 /* set ATTR_KILL_* bits and let VFS handle it */
327 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
333 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
336 struct inode *inode = d_inode(fhp->fh_dentry);
338 if (iap->ia_size < inode->i_size) {
341 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
342 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
346 return nfserrno(get_write_access(inode));
350 * Set various file attributes. After this call fhp needs an fh_put.
353 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
354 int check_guard, time64_t guardtime)
356 struct dentry *dentry;
358 int accmode = NFSD_MAY_SATTR;
362 bool get_write_count;
363 bool size_change = (iap->ia_valid & ATTR_SIZE);
365 if (iap->ia_valid & ATTR_SIZE) {
366 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
371 * If utimes(2) and friends are called with times not NULL, we should
372 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
373 * will return EACCES, when the caller's effective UID does not match
374 * the owner of the file, and the caller is not privileged. In this
375 * situation, we should return EPERM(notify_change will return this).
377 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
378 accmode |= NFSD_MAY_OWNER_OVERRIDE;
379 if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
380 accmode |= NFSD_MAY_WRITE;
383 /* Callers that do fh_verify should do the fh_want_write: */
384 get_write_count = !fhp->fh_dentry;
387 err = fh_verify(rqstp, fhp, ftype, accmode);
390 if (get_write_count) {
391 host_err = fh_want_write(fhp);
396 dentry = fhp->fh_dentry;
397 inode = d_inode(dentry);
399 /* Ignore any mode updates on symlinks */
400 if (S_ISLNK(inode->i_mode))
401 iap->ia_valid &= ~ATTR_MODE;
406 nfsd_sanitize_attrs(inode, iap);
408 if (check_guard && guardtime != inode->i_ctime.tv_sec)
409 return nfserr_notsync;
412 * The size case is special, it changes the file in addition to the
413 * attributes, and file systems don't expect it to be mixed with
414 * "random" attribute changes. We thus split out the size change
415 * into a separate call to ->setattr, and do the rest as a separate
419 err = nfsd_get_write_access(rqstp, fhp, iap);
427 * RFC5661, Section 18.30.4:
428 * Changing the size of a file with SETATTR indirectly
429 * changes the time_modify and change attributes.
431 * (and similar for the older RFCs)
433 struct iattr size_attr = {
434 .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
435 .ia_size = iap->ia_size,
439 if (iap->ia_size < 0)
442 host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL);
445 iap->ia_valid &= ~ATTR_SIZE;
448 * Avoid the additional setattr call below if the only other
449 * attribute that the client sends is the mtime, as we update
450 * it as part of the size change above.
452 if ((iap->ia_valid & ~ATTR_MTIME) == 0)
456 iap->ia_valid |= ATTR_CTIME;
457 host_err = notify_change(&init_user_ns, dentry, iap, NULL);
462 put_write_access(inode);
465 host_err = commit_metadata(fhp);
466 return nfserrno(host_err);
469 #if defined(CONFIG_NFSD_V4)
471 * NFS junction information is stored in an extended attribute.
473 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
476 * nfsd4_is_junction - Test if an object could be an NFS junction
478 * @dentry: object to test
480 * Returns 1 if "dentry" appears to contain NFS junction information.
481 * Otherwise 0 is returned.
483 int nfsd4_is_junction(struct dentry *dentry)
485 struct inode *inode = d_inode(dentry);
489 if (inode->i_mode & S_IXUGO)
491 if (!(inode->i_mode & S_ISVTX))
493 if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME,
498 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
499 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
500 struct xdr_netobj *label)
504 struct dentry *dentry;
506 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
510 dentry = fhp->fh_dentry;
512 inode_lock(d_inode(dentry));
513 host_error = security_inode_setsecctx(dentry, label->data, label->len);
514 inode_unlock(d_inode(dentry));
515 return nfserrno(host_error);
518 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
519 struct xdr_netobj *label)
521 return nfserr_notsupp;
525 static struct nfsd4_compound_state *nfsd4_get_cstate(struct svc_rqst *rqstp)
527 return &((struct nfsd4_compoundres *)rqstp->rq_resp)->cstate;
530 __be32 nfsd4_clone_file_range(struct svc_rqst *rqstp,
531 struct nfsd_file *nf_src, u64 src_pos,
532 struct nfsd_file *nf_dst, u64 dst_pos,
533 u64 count, bool sync)
535 struct file *src = nf_src->nf_file;
536 struct file *dst = nf_dst->nf_file;
541 since = READ_ONCE(dst->f_wb_err);
542 cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
544 ret = nfserrno(cloned);
547 if (count && cloned != count) {
548 ret = nfserrno(-EINVAL);
552 loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
553 int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
556 status = filemap_check_wb_err(dst->f_mapping, since);
558 status = commit_inode_metadata(file_inode(src));
560 struct nfsd_net *nn = net_generic(nf_dst->nf_net,
563 trace_nfsd_clone_file_range_err(rqstp,
564 &nfsd4_get_cstate(rqstp)->save_fh,
566 &nfsd4_get_cstate(rqstp)->current_fh,
569 nfsd_reset_write_verifier(nn);
570 trace_nfsd_writeverf_reset(nn, rqstp, status);
571 ret = nfserrno(status);
578 ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
579 u64 dst_pos, u64 count)
583 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
584 * thread and client rpc slot. The choice of 4MB is somewhat
585 * arbitrary. We might instead base this on r/wsize, or make it
586 * tunable, or use a time instead of a byte limit, or implement
587 * asynchronous copy. In theory a client could also recognize a
588 * limit like this and pipeline multiple COPY requests.
590 count = min_t(u64, count, 1 << 22);
591 return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
594 __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
595 struct file *file, loff_t offset, loff_t len,
600 if (!S_ISREG(file_inode(file)->i_mode))
603 error = vfs_fallocate(file, flags, offset, len);
605 error = commit_metadata(fhp);
607 return nfserrno(error);
609 #endif /* defined(CONFIG_NFSD_V4) */
611 #ifdef CONFIG_NFSD_V3
613 * Check server access rights to a file system object
619 static struct accessmap nfs3_regaccess[] = {
620 { NFS3_ACCESS_READ, NFSD_MAY_READ },
621 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
622 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
623 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
625 #ifdef CONFIG_NFSD_V4
626 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
627 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
628 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
634 static struct accessmap nfs3_diraccess[] = {
635 { NFS3_ACCESS_READ, NFSD_MAY_READ },
636 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
637 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
638 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
639 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
641 #ifdef CONFIG_NFSD_V4
642 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
643 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
644 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
650 static struct accessmap nfs3_anyaccess[] = {
651 /* Some clients - Solaris 2.6 at least, make an access call
652 * to the server to check for access for things like /dev/null
653 * (which really, the server doesn't care about). So
654 * We provide simple access checking for them, looking
655 * mainly at mode bits, and we make sure to ignore read-only
658 { NFS3_ACCESS_READ, NFSD_MAY_READ },
659 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
660 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
661 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
667 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
669 struct accessmap *map;
670 struct svc_export *export;
671 struct dentry *dentry;
672 u32 query, result = 0, sresult = 0;
675 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
679 export = fhp->fh_export;
680 dentry = fhp->fh_dentry;
682 if (d_is_reg(dentry))
683 map = nfs3_regaccess;
684 else if (d_is_dir(dentry))
685 map = nfs3_diraccess;
687 map = nfs3_anyaccess;
691 for (; map->access; map++) {
692 if (map->access & query) {
695 sresult |= map->access;
697 err2 = nfsd_permission(rqstp, export, dentry, map->how);
700 result |= map->access;
703 /* the following error codes just mean the access was not allowed,
704 * rather than an error occurred */
708 /* simply don't "or" in the access bit. */
718 *supported = sresult;
723 #endif /* CONFIG_NFSD_V3 */
725 int nfsd_open_break_lease(struct inode *inode, int access)
729 if (access & NFSD_MAY_NOT_BREAK_LEASE)
731 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
732 return break_lease(inode, mode | O_NONBLOCK);
736 * Open an existing file or directory.
737 * The may_flags argument indicates the type of open (read/write/lock)
738 * and additional flags.
739 * N.B. After this call fhp needs an fh_put
742 __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
743 int may_flags, struct file **filp)
748 int flags = O_RDONLY|O_LARGEFILE;
752 path.mnt = fhp->fh_export->ex_path.mnt;
753 path.dentry = fhp->fh_dentry;
754 inode = d_inode(path.dentry);
757 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
763 host_err = nfsd_open_break_lease(inode, may_flags);
764 if (host_err) /* NOMEM or WOULDBLOCK */
767 if (may_flags & NFSD_MAY_WRITE) {
768 if (may_flags & NFSD_MAY_READ)
769 flags = O_RDWR|O_LARGEFILE;
771 flags = O_WRONLY|O_LARGEFILE;
774 file = dentry_open(&path, flags, current_cred());
776 host_err = PTR_ERR(file);
780 host_err = ima_file_check(file, may_flags);
786 if (may_flags & NFSD_MAY_64BIT_COOKIE)
787 file->f_mode |= FMODE_64BITHASH;
789 file->f_mode |= FMODE_32BITHASH;
793 err = nfserrno(host_err);
799 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
800 int may_flags, struct file **filp)
803 bool retried = false;
805 validate_process_creds();
807 * If we get here, then the client has already done an "open",
808 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
809 * in case a chmod has now revoked permission.
811 * Arguably we should also allow the owner override for
812 * directories, but we never have and it doesn't seem to have
813 * caused anyone a problem. If we were to change this, note
814 * also that our filldir callbacks would need a variant of
815 * lookup_one_len that doesn't check permissions.
818 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
820 err = fh_verify(rqstp, fhp, type, may_flags);
822 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
823 if (err == nfserr_stale && !retried) {
829 validate_process_creds();
834 nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
835 int may_flags, struct file **filp)
839 validate_process_creds();
840 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
841 validate_process_creds();
846 * Grab and keep cached pages associated with a file in the svc_rqst
847 * so that they can be passed to the network sendmsg/sendpage routines
848 * directly. They will be released after the sending has completed.
851 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
852 struct splice_desc *sd)
854 struct svc_rqst *rqstp = sd->u.data;
855 struct page **pp = rqstp->rq_next_page;
856 struct page *page = buf->page;
858 if (rqstp->rq_res.page_len == 0) {
859 svc_rqst_replace_page(rqstp, page);
860 rqstp->rq_res.page_base = buf->offset;
861 } else if (page != pp[-1]) {
862 svc_rqst_replace_page(rqstp, page);
864 rqstp->rq_res.page_len += sd->len;
869 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
870 struct splice_desc *sd)
872 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
875 static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
878 if (expected != 0 && len == 0)
880 if (offset+len >= i_size_read(file_inode(file)))
885 static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
886 struct file *file, loff_t offset,
887 unsigned long *count, u32 *eof, ssize_t host_err)
890 nfsd_stats_io_read_add(fhp->fh_export, host_err);
891 *eof = nfsd_eof_on_read(file, offset, host_err, *count);
893 fsnotify_access(file);
894 trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
897 trace_nfsd_read_err(rqstp, fhp, offset, host_err);
898 return nfserrno(host_err);
902 __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
903 struct file *file, loff_t offset, unsigned long *count,
906 struct splice_desc sd = {
914 trace_nfsd_read_splice(rqstp, fhp, offset, *count);
915 rqstp->rq_next_page = rqstp->rq_respages + 1;
916 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
917 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
920 __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
921 struct file *file, loff_t offset,
922 struct kvec *vec, int vlen, unsigned long *count,
925 struct iov_iter iter;
926 loff_t ppos = offset;
929 trace_nfsd_read_vector(rqstp, fhp, offset, *count);
930 iov_iter_kvec(&iter, READ, vec, vlen, *count);
931 host_err = vfs_iter_read(file, &iter, &ppos, 0);
932 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
936 * Gathered writes: If another process is currently writing to the file,
937 * there's a high chance this is another nfsd (triggered by a bulk write
938 * from a client's biod). Rather than syncing the file with each write
939 * request, we sleep for 10 msec.
941 * I don't know if this roughly approximates C. Juszak's idea of
942 * gathered writes, but it's a nice and simple solution (IMHO), and it
945 * Note: we do this only in the NFSv2 case, since v3 and higher have a
946 * better tool (separate unstable writes and commits) for solving this
949 static int wait_for_concurrent_writes(struct file *file)
951 struct inode *inode = file_inode(file);
952 static ino_t last_ino;
953 static dev_t last_dev;
956 if (atomic_read(&inode->i_writecount) > 1
957 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
958 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
960 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
963 if (inode->i_state & I_DIRTY) {
964 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
965 err = vfs_fsync(file, 0);
967 last_ino = inode->i_ino;
968 last_dev = inode->i_sb->s_dev;
973 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
974 loff_t offset, struct kvec *vec, int vlen,
975 unsigned long *cnt, int stable,
978 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
979 struct file *file = nf->nf_file;
980 struct super_block *sb = file_inode(file)->i_sb;
981 struct svc_export *exp;
982 struct iov_iter iter;
988 unsigned long exp_op_flags = 0;
989 unsigned int pflags = current->flags;
991 bool restore_flags = false;
993 trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
996 exp_op_flags = sb->s_export_op->flags;
998 if (test_bit(RQ_LOCAL, &rqstp->rq_flags) &&
999 !(exp_op_flags & EXPORT_OP_REMOTE_FS)) {
1001 * We want throttling in balance_dirty_pages()
1002 * and shrink_inactive_list() to only consider
1003 * the backingdev we are writing to, so that nfs to
1004 * localhost doesn't cause nfsd to lock up due to all
1005 * the client's dirty pages or its congested queue.
1007 current->flags |= PF_LOCAL_THROTTLE;
1008 restore_flags = true;
1011 exp = fhp->fh_export;
1012 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1014 if (!EX_ISSYNC(exp))
1015 stable = NFS_UNSTABLE;
1017 if (stable && !use_wgather)
1020 iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
1021 since = READ_ONCE(file->f_wb_err);
1023 nfsd_copy_write_verifier(verf, nn);
1024 host_err = vfs_iter_write(file, &iter, &pos, flags);
1026 nfsd_reset_write_verifier(nn);
1027 trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1031 nfsd_stats_io_write_add(exp, *cnt);
1032 fsnotify_modify(file);
1033 host_err = filemap_check_wb_err(file->f_mapping, since);
1037 if (stable && use_wgather) {
1038 host_err = wait_for_concurrent_writes(file);
1040 nfsd_reset_write_verifier(nn);
1041 trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1046 if (host_err >= 0) {
1047 trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1050 trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1051 nfserr = nfserrno(host_err);
1054 current_restore_flags(pflags, PF_LOCAL_THROTTLE);
1059 * Read data from a file. count must contain the requested read count
1060 * on entry. On return, *count contains the number of bytes actually read.
1061 * N.B. After this call fhp needs an fh_put
1063 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1064 loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
1067 struct nfsd_file *nf;
1071 trace_nfsd_read_start(rqstp, fhp, offset, *count);
1072 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
1077 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
1078 err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
1080 err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
1084 trace_nfsd_read_done(rqstp, fhp, offset, *count);
1090 * Write data to a file.
1091 * The stable flag requests synchronous writes.
1092 * N.B. After this call fhp needs an fh_put
1095 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1096 struct kvec *vec, int vlen, unsigned long *cnt, int stable,
1099 struct nfsd_file *nf;
1102 trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
1104 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
1108 err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
1109 vlen, cnt, stable, verf);
1112 trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
1116 #ifdef CONFIG_NFSD_V3
1118 * nfsd_commit - Commit pending writes to stable storage
1119 * @rqstp: RPC request being processed
1120 * @fhp: NFS filehandle
1121 * @offset: raw offset from beginning of file
1122 * @count: raw count of bytes to sync
1123 * @verf: filled in with the server's current write verifier
1125 * Note: we guarantee that data that lies within the range specified
1126 * by the 'offset' and 'count' parameters will be synced. The server
1127 * is permitted to sync data that lies outside this range at the
1130 * Unfortunately we cannot lock the file to make sure we return full WCC
1131 * data to the client, as locking happens lower down in the filesystem.
1134 * An nfsstat value in network byte order.
1137 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, u64 offset,
1138 u32 count, __be32 *verf)
1142 struct nfsd_net *nn;
1143 struct nfsd_file *nf;
1146 err = nfsd_file_acquire(rqstp, fhp,
1147 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
1152 * Convert the client-provided (offset, count) range to a
1153 * (start, end) range. If the client-provided range falls
1154 * outside the maximum file size of the underlying FS,
1155 * clamp the sync range appropriately.
1159 maxbytes = (u64)fhp->fh_dentry->d_sb->s_maxbytes;
1160 if (offset < maxbytes) {
1162 if (count && (offset + count - 1 < maxbytes))
1163 end = offset + count - 1;
1166 nn = net_generic(nf->nf_net, nfsd_net_id);
1167 if (EX_ISSYNC(fhp->fh_export)) {
1168 errseq_t since = READ_ONCE(nf->nf_file->f_wb_err);
1171 err2 = vfs_fsync_range(nf->nf_file, start, end, 0);
1174 nfsd_copy_write_verifier(verf, nn);
1175 err2 = filemap_check_wb_err(nf->nf_file->f_mapping,
1179 err = nfserr_notsupp;
1182 nfsd_reset_write_verifier(nn);
1183 trace_nfsd_writeverf_reset(nn, rqstp, err2);
1185 err = nfserrno(err2);
1187 nfsd_copy_write_verifier(verf, nn);
1193 #endif /* CONFIG_NFSD_V3 */
1196 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1200 * Mode has already been set earlier in create:
1202 iap->ia_valid &= ~ATTR_MODE;
1204 * Setting uid/gid works only for root. Irix appears to
1205 * send along the gid on create when it tries to implement
1206 * setgid directories via NFS:
1208 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1209 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1211 return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0);
1212 /* Callers expect file metadata to be committed here */
1213 return nfserrno(commit_metadata(resfhp));
1216 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1217 * setting size to 0 may fail for some specific file systems by the permission
1218 * checking which requires WRITE permission but the mode is 000.
1219 * we ignore the resizing(to 0) on the just new created file, since the size is
1220 * 0 after file created.
1222 * call this only after vfs_create() is called.
1225 nfsd_check_ignore_resizing(struct iattr *iap)
1227 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1228 iap->ia_valid &= ~ATTR_SIZE;
1231 /* The parent directory should already be locked: */
1233 nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
1234 char *fname, int flen, struct iattr *iap,
1235 int type, dev_t rdev, struct svc_fh *resfhp)
1237 struct dentry *dentry, *dchild;
1243 dentry = fhp->fh_dentry;
1244 dirp = d_inode(dentry);
1246 dchild = dget(resfhp->fh_dentry);
1247 if (!fhp->fh_locked) {
1248 WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1254 err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1258 if (!(iap->ia_valid & ATTR_MODE))
1260 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1262 if (!IS_POSIXACL(dirp))
1263 iap->ia_mode &= ~current_umask();
1269 host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
1271 nfsd_check_ignore_resizing(iap);
1274 host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode);
1275 if (!host_err && unlikely(d_unhashed(dchild))) {
1277 d = lookup_one_len(dchild->d_name.name,
1279 dchild->d_name.len);
1281 host_err = PTR_ERR(d);
1284 if (unlikely(d_is_negative(d))) {
1286 err = nfserr_serverfault;
1289 dput(resfhp->fh_dentry);
1290 resfhp->fh_dentry = dget(d);
1291 err = fh_update(resfhp);
1302 host_err = vfs_mknod(&init_user_ns, dirp, dchild,
1303 iap->ia_mode, rdev);
1306 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1313 err = nfsd_create_setattr(rqstp, resfhp, iap);
1316 * nfsd_create_setattr already committed the child. Transactional
1317 * filesystems had a chance to commit changes for both parent and
1318 * child simultaneously making the following commit_metadata a
1321 err2 = nfserrno(commit_metadata(fhp));
1325 * Update the file handle to get the new inode info.
1328 err = fh_update(resfhp);
1334 err = nfserrno(host_err);
1339 * Create a filesystem object (regular, directory, special).
1340 * Note that the parent directory is left locked.
1342 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1345 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1346 char *fname, int flen, struct iattr *iap,
1347 int type, dev_t rdev, struct svc_fh *resfhp)
1349 struct dentry *dentry, *dchild = NULL;
1353 if (isdotent(fname, flen))
1354 return nfserr_exist;
1356 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1360 dentry = fhp->fh_dentry;
1362 host_err = fh_want_write(fhp);
1364 return nfserrno(host_err);
1366 fh_lock_nested(fhp, I_MUTEX_PARENT);
1367 dchild = lookup_one_len(fname, dentry, flen);
1368 host_err = PTR_ERR(dchild);
1370 return nfserrno(host_err);
1371 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1373 * We unconditionally drop our ref to dchild as fh_compose will have
1374 * already grabbed its own ref for it.
1379 return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1383 #ifdef CONFIG_NFSD_V3
1386 * NFSv3 and NFSv4 version of nfsd_create
1389 do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1390 char *fname, int flen, struct iattr *iap,
1391 struct svc_fh *resfhp, int createmode, u32 *verifier,
1392 bool *truncp, bool *created)
1394 struct dentry *dentry, *dchild = NULL;
1398 __u32 v_mtime=0, v_atime=0;
1404 if (isdotent(fname, flen))
1406 if (!(iap->ia_valid & ATTR_MODE))
1408 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1412 dentry = fhp->fh_dentry;
1413 dirp = d_inode(dentry);
1415 host_err = fh_want_write(fhp);
1419 fh_lock_nested(fhp, I_MUTEX_PARENT);
1422 * Compose the response file handle.
1424 dchild = lookup_one_len(fname, dentry, flen);
1425 host_err = PTR_ERR(dchild);
1429 /* If file doesn't exist, check for permissions to create one */
1430 if (d_really_is_negative(dchild)) {
1431 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1436 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1440 if (nfsd_create_is_exclusive(createmode)) {
1441 /* solaris7 gets confused (bugid 4218508) if these have
1442 * the high bit set, as do xfs filesystems without the
1443 * "bigtime" feature. So just clear the high bits. If this is
1444 * ever changed to use different attrs for storing the
1445 * verifier, then do_open_lookup() will also need to be fixed
1448 v_mtime = verifier[0]&0x7fffffff;
1449 v_atime = verifier[1]&0x7fffffff;
1452 if (d_really_is_positive(dchild)) {
1455 switch (createmode) {
1456 case NFS3_CREATE_UNCHECKED:
1457 if (! d_is_reg(dchild))
1460 /* in nfsv4, we need to treat this case a little
1461 * differently. we don't want to truncate the
1462 * file now; this would be wrong if the OPEN
1463 * fails for some other reason. furthermore,
1464 * if the size is nonzero, we should ignore it
1465 * according to spec!
1467 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1470 iap->ia_valid &= ATTR_SIZE;
1474 case NFS3_CREATE_EXCLUSIVE:
1475 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1476 && d_inode(dchild)->i_atime.tv_sec == v_atime
1477 && d_inode(dchild)->i_size == 0 ) {
1483 case NFS4_CREATE_EXCLUSIVE4_1:
1484 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1485 && d_inode(dchild)->i_atime.tv_sec == v_atime
1486 && d_inode(dchild)->i_size == 0 ) {
1492 case NFS3_CREATE_GUARDED:
1499 if (!IS_POSIXACL(dirp))
1500 iap->ia_mode &= ~current_umask();
1502 host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
1510 nfsd_check_ignore_resizing(iap);
1512 if (nfsd_create_is_exclusive(createmode)) {
1513 /* Cram the verifier into atime/mtime */
1514 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1515 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1516 /* XXX someone who knows this better please fix it for nsec */
1517 iap->ia_mtime.tv_sec = v_mtime;
1518 iap->ia_atime.tv_sec = v_atime;
1519 iap->ia_mtime.tv_nsec = 0;
1520 iap->ia_atime.tv_nsec = 0;
1524 err = nfsd_create_setattr(rqstp, resfhp, iap);
1527 * nfsd_create_setattr already committed the child
1528 * (and possibly also the parent).
1531 err = nfserrno(commit_metadata(fhp));
1534 * Update the filehandle to get the new inode info.
1537 err = fh_update(resfhp);
1541 if (dchild && !IS_ERR(dchild))
1547 err = nfserrno(host_err);
1550 #endif /* CONFIG_NFSD_V3 */
1553 * Read a symlink. On entry, *lenp must contain the maximum path length that
1554 * fits into the buffer. On return, it contains the true length.
1555 * N.B. After this call fhp needs an fh_put
1558 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1563 DEFINE_DELAYED_CALL(done);
1566 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1570 path.mnt = fhp->fh_export->ex_path.mnt;
1571 path.dentry = fhp->fh_dentry;
1573 if (unlikely(!d_is_symlink(path.dentry)))
1574 return nfserr_inval;
1578 link = vfs_get_link(path.dentry, &done);
1580 return nfserrno(PTR_ERR(link));
1585 memcpy(buf, link, *lenp);
1586 do_delayed_call(&done);
1591 * Create a symlink and look up its inode
1592 * N.B. After this call _both_ fhp and resfhp need an fh_put
1595 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1596 char *fname, int flen,
1598 struct svc_fh *resfhp)
1600 struct dentry *dentry, *dnew;
1605 if (!flen || path[0] == '\0')
1608 if (isdotent(fname, flen))
1611 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1615 host_err = fh_want_write(fhp);
1620 dentry = fhp->fh_dentry;
1621 dnew = lookup_one_len(fname, dentry, flen);
1622 host_err = PTR_ERR(dnew);
1626 host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
1627 err = nfserrno(host_err);
1630 err = nfserrno(commit_metadata(fhp));
1634 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1636 if (err==0) err = cerr;
1641 err = nfserrno(host_err);
1647 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1650 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1651 char *name, int len, struct svc_fh *tfhp)
1653 struct dentry *ddir, *dnew, *dold;
1658 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1661 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1665 if (d_is_dir(tfhp->fh_dentry))
1671 if (isdotent(name, len))
1674 host_err = fh_want_write(tfhp);
1676 err = nfserrno(host_err);
1680 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1681 ddir = ffhp->fh_dentry;
1682 dirp = d_inode(ddir);
1684 dnew = lookup_one_len(name, ddir, len);
1685 host_err = PTR_ERR(dnew);
1689 dold = tfhp->fh_dentry;
1692 if (d_really_is_negative(dold))
1694 host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
1697 err = nfserrno(commit_metadata(ffhp));
1699 err = nfserrno(commit_metadata(tfhp));
1701 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1704 err = nfserrno(host_err);
1710 fh_drop_write(tfhp);
1715 err = nfserrno(host_err);
1720 nfsd_close_cached_files(struct dentry *dentry)
1722 struct inode *inode = d_inode(dentry);
1724 if (inode && S_ISREG(inode->i_mode))
1725 nfsd_file_close_inode_sync(inode);
1729 nfsd_has_cached_files(struct dentry *dentry)
1732 struct inode *inode = d_inode(dentry);
1734 if (inode && S_ISREG(inode->i_mode))
1735 ret = nfsd_file_is_cached(inode);
1741 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1744 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1745 struct svc_fh *tfhp, char *tname, int tlen)
1747 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1748 struct inode *fdir, *tdir;
1751 bool close_cached = false;
1753 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1756 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1760 fdentry = ffhp->fh_dentry;
1761 fdir = d_inode(fdentry);
1763 tdentry = tfhp->fh_dentry;
1764 tdir = d_inode(tdentry);
1767 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1771 host_err = fh_want_write(ffhp);
1773 err = nfserrno(host_err);
1777 /* cannot use fh_lock as we need deadlock protective ordering
1778 * so do it by hand */
1779 trap = lock_rename(tdentry, fdentry);
1780 ffhp->fh_locked = tfhp->fh_locked = true;
1781 fh_fill_pre_attrs(ffhp);
1782 fh_fill_pre_attrs(tfhp);
1784 odentry = lookup_one_len(fname, fdentry, flen);
1785 host_err = PTR_ERR(odentry);
1786 if (IS_ERR(odentry))
1790 if (d_really_is_negative(odentry))
1793 if (odentry == trap)
1796 ndentry = lookup_one_len(tname, tdentry, tlen);
1797 host_err = PTR_ERR(ndentry);
1798 if (IS_ERR(ndentry))
1800 host_err = -ENOTEMPTY;
1801 if (ndentry == trap)
1805 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1807 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1810 if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) &&
1811 nfsd_has_cached_files(ndentry)) {
1812 close_cached = true;
1815 struct renamedata rd = {
1816 .old_mnt_userns = &init_user_ns,
1818 .old_dentry = odentry,
1819 .new_mnt_userns = &init_user_ns,
1821 .new_dentry = ndentry,
1823 host_err = vfs_rename(&rd);
1825 host_err = commit_metadata(tfhp);
1827 host_err = commit_metadata(ffhp);
1835 err = nfserrno(host_err);
1837 * We cannot rely on fh_unlock on the two filehandles,
1838 * as that would do the wrong thing if the two directories
1839 * were the same, so again we do it by hand.
1841 if (!close_cached) {
1842 fh_fill_post_attrs(ffhp);
1843 fh_fill_post_attrs(tfhp);
1845 unlock_rename(tdentry, fdentry);
1846 ffhp->fh_locked = tfhp->fh_locked = false;
1847 fh_drop_write(ffhp);
1850 * If the target dentry has cached open files, then we need to try to
1851 * close them prior to doing the rename. Flushing delayed fput
1852 * shouldn't be done with locks held however, so we delay it until this
1853 * point and then reattempt the whole shebang.
1856 close_cached = false;
1857 nfsd_close_cached_files(ndentry);
1866 * Unlink a file or directory
1867 * N.B. After this call fhp needs an fh_put
1870 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1871 char *fname, int flen)
1873 struct dentry *dentry, *rdentry;
1875 struct inode *rinode;
1880 if (!flen || isdotent(fname, flen))
1882 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1886 host_err = fh_want_write(fhp);
1890 fh_lock_nested(fhp, I_MUTEX_PARENT);
1891 dentry = fhp->fh_dentry;
1892 dirp = d_inode(dentry);
1894 rdentry = lookup_one_len(fname, dentry, flen);
1895 host_err = PTR_ERR(rdentry);
1896 if (IS_ERR(rdentry))
1897 goto out_drop_write;
1899 if (d_really_is_negative(rdentry)) {
1902 goto out_drop_write;
1904 rinode = d_inode(rdentry);
1908 type = d_inode(rdentry)->i_mode & S_IFMT;
1910 if (type != S_IFDIR) {
1911 if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK)
1912 nfsd_close_cached_files(rdentry);
1913 host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL);
1915 host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
1920 host_err = commit_metadata(fhp);
1922 iput(rinode); /* truncate the inode here */
1927 if (host_err == -EBUSY) {
1928 /* name is mounted-on. There is no perfect
1931 if (nfsd_v4client(rqstp))
1932 err = nfserr_file_open;
1936 err = nfserrno(host_err);
1943 * We do this buffering because we must not call back into the file
1944 * system's ->lookup() method from the filldir callback. That may well
1945 * deadlock a number of file systems.
1947 * This is based heavily on the implementation of same in XFS.
1949 struct buffered_dirent {
1953 unsigned int d_type;
1957 struct readdir_data {
1958 struct dir_context ctx;
1964 static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1965 int namlen, loff_t offset, u64 ino,
1966 unsigned int d_type)
1968 struct readdir_data *buf =
1969 container_of(ctx, struct readdir_data, ctx);
1970 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1971 unsigned int reclen;
1973 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1974 if (buf->used + reclen > PAGE_SIZE) {
1979 de->namlen = namlen;
1980 de->offset = offset;
1982 de->d_type = d_type;
1983 memcpy(de->name, name, namlen);
1984 buf->used += reclen;
1989 static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp,
1990 nfsd_filldir_t func, struct readdir_cd *cdp,
1993 struct buffered_dirent *de;
1997 struct readdir_data buf = {
1998 .ctx.actor = nfsd_buffered_filldir,
1999 .dirent = (void *)__get_free_page(GFP_KERNEL)
2003 return nfserrno(-ENOMEM);
2008 unsigned int reclen;
2010 cdp->err = nfserr_eof; /* will be cleared on successful read */
2014 host_err = iterate_dir(file, &buf.ctx);
2026 de = (struct buffered_dirent *)buf.dirent;
2028 offset = de->offset;
2030 if (func(cdp, de->name, de->namlen, de->offset,
2031 de->ino, de->d_type))
2034 if (cdp->err != nfs_ok)
2037 trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen);
2039 reclen = ALIGN(sizeof(*de) + de->namlen,
2042 de = (struct buffered_dirent *)((char *)de + reclen);
2044 if (size > 0) /* We bailed out early */
2047 offset = vfs_llseek(file, 0, SEEK_CUR);
2050 free_page((unsigned long)(buf.dirent));
2053 return nfserrno(host_err);
2060 * Read entries from a directory.
2061 * The NFSv3/4 verifier we ignore for now.
2064 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2065 struct readdir_cd *cdp, nfsd_filldir_t func)
2069 loff_t offset = *offsetp;
2070 int may_flags = NFSD_MAY_READ;
2072 /* NFSv2 only supports 32 bit cookies */
2073 if (rqstp->rq_vers > 2)
2074 may_flags |= NFSD_MAY_64BIT_COOKIE;
2076 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
2080 offset = vfs_llseek(file, offset, SEEK_SET);
2082 err = nfserrno((int)offset);
2086 err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp);
2088 if (err == nfserr_eof || err == nfserr_toosmall)
2089 err = nfs_ok; /* can still be found in ->err */
2097 * Get file system stats
2098 * N.B. After this call fhp needs an fh_put
2101 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2105 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2107 struct path path = {
2108 .mnt = fhp->fh_export->ex_path.mnt,
2109 .dentry = fhp->fh_dentry,
2111 if (vfs_statfs(&path, stat))
2117 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2119 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2122 #ifdef CONFIG_NFSD_V4
2124 * Helper function to translate error numbers. In the case of xattr operations,
2125 * some error codes need to be translated outside of the standard translations.
2127 * ENODATA needs to be translated to nfserr_noxattr.
2128 * E2BIG to nfserr_xattr2big.
2130 * Additionally, vfs_listxattr can return -ERANGE. This means that the
2131 * file has too many extended attributes to retrieve inside an
2132 * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
2133 * filesystems will allow the adding of extended attributes until they hit
2134 * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
2135 * So, at that point, the attributes are present and valid, but can't
2136 * be retrieved using listxattr, since the upper level xattr code enforces
2137 * the XATTR_LIST_MAX limit.
2139 * This bug means that we need to deal with listxattr returning -ERANGE. The
2140 * best mapping is to return TOOSMALL.
2143 nfsd_xattr_errno(int err)
2147 return nfserr_noxattr;
2149 return nfserr_xattr2big;
2151 return nfserr_toosmall;
2153 return nfserrno(err);
2157 * Retrieve the specified user extended attribute. To avoid always
2158 * having to allocate the maximum size (since we are not getting
2159 * a maximum size from the RPC), do a probe + alloc. Hold a reader
2160 * lock on i_rwsem to prevent the extended attribute from changing
2161 * size while we're doing this.
2164 nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2165 void **bufp, int *lenp)
2170 struct inode *inode;
2171 struct dentry *dentry;
2173 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2178 dentry = fhp->fh_dentry;
2179 inode = d_inode(dentry);
2181 inode_lock_shared(inode);
2183 len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
2186 * Zero-length attribute, just return.
2195 err = nfsd_xattr_errno(len);
2200 err = nfserr_toosmall;
2204 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2206 err = nfserr_jukebox;
2210 len = vfs_getxattr(&init_user_ns, dentry, name, buf, len);
2214 err = nfsd_xattr_errno(len);
2221 inode_unlock_shared(inode);
2227 * Retrieve the xattr names. Since we can't know how many are
2228 * user extended attributes, we must get all attributes here,
2229 * and have the XDR encode filter out the "user." ones.
2231 * While this could always just allocate an XATTR_LIST_MAX
2232 * buffer, that's a waste, so do a probe + allocate. To
2233 * avoid any changes between the probe and allocate, wrap
2234 * this in inode_lock.
2237 nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
2243 struct inode *inode;
2244 struct dentry *dentry;
2246 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2250 dentry = fhp->fh_dentry;
2251 inode = d_inode(dentry);
2254 inode_lock_shared(inode);
2256 len = vfs_listxattr(dentry, NULL, 0);
2258 err = nfsd_xattr_errno(len);
2262 if (len > XATTR_LIST_MAX) {
2263 err = nfserr_xattr2big;
2268 * We're holding i_rwsem - use GFP_NOFS.
2270 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2272 err = nfserr_jukebox;
2276 len = vfs_listxattr(dentry, buf, len);
2279 err = nfsd_xattr_errno(len);
2288 inode_unlock_shared(inode);
2294 * Removexattr and setxattr need to call fh_lock to both lock the inode
2295 * and set the change attribute. Since the top-level vfs_removexattr
2296 * and vfs_setxattr calls already do their own inode_lock calls, call
2297 * the _locked variant. Pass in a NULL pointer for delegated_inode,
2298 * and let the client deal with NFS4ERR_DELAY (same as with e.g.
2299 * setattr and remove).
2302 nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
2307 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2311 ret = fh_want_write(fhp);
2313 return nfserrno(ret);
2317 ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
2323 return nfsd_xattr_errno(ret);
2327 nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2328 void *buf, u32 len, u32 flags)
2333 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2337 ret = fh_want_write(fhp);
2339 return nfserrno(ret);
2342 ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
2348 return nfsd_xattr_errno(ret);
2353 * Check for a user's access permissions to this inode.
2356 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2357 struct dentry *dentry, int acc)
2359 struct inode *inode = d_inode(dentry);
2362 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2365 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2367 (acc & NFSD_MAY_READ)? " read" : "",
2368 (acc & NFSD_MAY_WRITE)? " write" : "",
2369 (acc & NFSD_MAY_EXEC)? " exec" : "",
2370 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2371 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2372 (acc & NFSD_MAY_LOCK)? " lock" : "",
2373 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2375 IS_IMMUTABLE(inode)? " immut" : "",
2376 IS_APPEND(inode)? " append" : "",
2377 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2378 dprintk(" owner %d/%d user %d/%d\n",
2379 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2382 /* Normally we reject any write/sattr etc access on a read-only file
2383 * system. But if it is IRIX doing check on write-access for a
2384 * device special file, we ignore rofs.
2386 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2387 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2388 if (exp_rdonly(rqstp, exp) ||
2389 __mnt_is_readonly(exp->ex_path.mnt))
2391 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2394 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2397 if (acc & NFSD_MAY_LOCK) {
2398 /* If we cannot rely on authentication in NLM requests,
2399 * just allow locks, otherwise require read permission, or
2402 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2405 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2408 * The file owner always gets access permission for accesses that
2409 * would normally be checked at open time. This is to make
2410 * file access work even when the client has done a fchmod(fd, 0).
2412 * However, `cp foo bar' should fail nevertheless when bar is
2413 * readonly. A sensible way to do this might be to reject all
2414 * attempts to truncate a read-only file, because a creat() call
2415 * always implies file truncation.
2416 * ... but this isn't really fair. A process may reasonably call
2417 * ftruncate on an open file descriptor on a file with perm 000.
2418 * We must trust the client to do permission checking - using "ACCESS"
2421 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2422 uid_eq(inode->i_uid, current_fsuid()))
2425 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2426 err = inode_permission(&init_user_ns, inode,
2427 acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
2429 /* Allow read access to binaries even when mode 111 */
2430 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2431 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2432 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2433 err = inode_permission(&init_user_ns, inode, MAY_EXEC);
2435 return err? nfserrno(err) : 0;