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/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 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
55 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
56 * or nfs_ok having possibly changed *dpp and *expp
59 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
60 struct svc_export **expp)
62 struct svc_export *exp = *expp, *exp2 = NULL;
63 struct dentry *dentry = *dpp;
64 struct path path = {.mnt = mntget(exp->ex_path.mnt),
65 .dentry = dget(dentry)};
68 err = follow_down(&path);
71 if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
72 nfsd_mountpoint(dentry, exp) == 2) {
73 /* This is only a mountpoint in some other namespace */
78 exp2 = rqst_exp_get_by_name(rqstp, &path);
82 * We normally allow NFS clients to continue
83 * "underneath" a mountpoint that is not exported.
84 * The exception is V4ROOT, where no traversal is ever
85 * allowed without an explicit export of the new
88 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
93 if (nfsd_v4client(rqstp) ||
94 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
95 /* successfully crossed mount point */
97 * This is subtle: path.dentry is *not* on path.mnt
98 * at this point. The only reason we are safe is that
99 * original mnt is pinned down by exp, so we should
100 * put path *before* putting exp
103 path.dentry = dentry;
113 static void follow_to_parent(struct path *path)
117 while (path->dentry == path->mnt->mnt_root && follow_up(path))
119 dp = dget_parent(path->dentry);
124 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
126 struct svc_export *exp2;
127 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
128 .dentry = dget(dparent)};
130 follow_to_parent(&path);
132 exp2 = rqst_exp_parent(rqstp, &path);
133 if (PTR_ERR(exp2) == -ENOENT) {
134 *dentryp = dget(dparent);
135 } else if (IS_ERR(exp2)) {
137 return PTR_ERR(exp2);
139 *dentryp = dget(path.dentry);
148 * For nfsd purposes, we treat V4ROOT exports as though there was an
149 * export at *every* directory.
151 * '1' if this dentry *must* be an export point,
152 * '2' if it might be, if there is really a mount here, and
153 * '0' if there is no chance of an export point here.
155 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
157 if (!d_inode(dentry))
159 if (exp->ex_flags & NFSEXP_V4ROOT)
161 if (nfsd4_is_junction(dentry))
163 if (d_mountpoint(dentry))
165 * Might only be a mountpoint in a different namespace,
166 * but we need to check.
173 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
174 const char *name, unsigned int len,
175 struct svc_export **exp_ret, struct dentry **dentry_ret)
177 struct svc_export *exp;
178 struct dentry *dparent;
179 struct dentry *dentry;
182 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
184 dparent = fhp->fh_dentry;
185 exp = exp_get(fhp->fh_export);
187 /* Lookup the name, but don't follow links */
188 if (isdotent(name, len)) {
190 dentry = dget(dparent);
191 else if (dparent != exp->ex_path.dentry)
192 dentry = dget_parent(dparent);
193 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
194 dentry = dget(dparent); /* .. == . just like at / */
196 /* checking mountpoint crossing is very different when stepping up */
197 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
202 dentry = lookup_one_len_unlocked(name, dparent, len);
203 host_err = PTR_ERR(dentry);
206 if (nfsd_mountpoint(dentry, exp)) {
207 host_err = nfsd_cross_mnt(rqstp, &dentry, &exp);
214 *dentry_ret = dentry;
220 return nfserrno(host_err);
224 * nfsd_lookup - look up a single path component for nfsd
226 * @rqstp: the request context
227 * @fhp: the file handle of the directory
228 * @name: the component name, or %NULL to look up parent
229 * @len: length of name to examine
230 * @resfh: pointer to pre-initialised filehandle to hold result.
232 * Look up one component of a pathname.
233 * N.B. After this call _both_ fhp and resfh need an fh_put
235 * If the lookup would cross a mountpoint, and the mounted filesystem
236 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
237 * accepted as it stands and the mounted directory is
238 * returned. Otherwise the covered directory is returned.
239 * NOTE: this mountpoint crossing is not supported properly by all
240 * clients and is explicitly disallowed for NFSv3
244 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
245 unsigned int len, struct svc_fh *resfh)
247 struct svc_export *exp;
248 struct dentry *dentry;
251 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
254 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
257 err = check_nfsd_access(exp, rqstp);
261 * Note: we compose the file handle now, but as the
262 * dentry may be negative, it may need to be updated.
264 err = fh_compose(resfh, exp, dentry, fhp);
265 if (!err && d_really_is_negative(dentry))
274 * Commit metadata changes to stable storage.
277 commit_inode_metadata(struct inode *inode)
279 const struct export_operations *export_ops = inode->i_sb->s_export_op;
281 if (export_ops->commit_metadata)
282 return export_ops->commit_metadata(inode);
283 return sync_inode_metadata(inode, 1);
287 commit_metadata(struct svc_fh *fhp)
289 struct inode *inode = d_inode(fhp->fh_dentry);
291 if (!EX_ISSYNC(fhp->fh_export))
293 return commit_inode_metadata(inode);
297 * Go over the attributes and take care of the small differences between
298 * NFS semantics and what Linux expects.
301 nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
303 /* sanitize the mode change */
304 if (iap->ia_valid & ATTR_MODE) {
305 iap->ia_mode &= S_IALLUGO;
306 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
309 /* Revoke setuid/setgid on chown */
310 if (!S_ISDIR(inode->i_mode) &&
311 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
312 iap->ia_valid |= ATTR_KILL_PRIV;
313 if (iap->ia_valid & ATTR_MODE) {
314 /* we're setting mode too, just clear the s*id bits */
315 iap->ia_mode &= ~S_ISUID;
316 if (iap->ia_mode & S_IXGRP)
317 iap->ia_mode &= ~S_ISGID;
319 /* set ATTR_KILL_* bits and let VFS handle it */
320 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
326 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
329 struct inode *inode = d_inode(fhp->fh_dentry);
331 if (iap->ia_size < inode->i_size) {
334 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
335 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
339 return nfserrno(get_write_access(inode));
343 * Set various file attributes. After this call fhp needs an fh_put.
346 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
347 struct nfsd_attrs *attr,
348 int check_guard, time64_t guardtime)
350 struct dentry *dentry;
352 struct iattr *iap = attr->na_iattr;
353 int accmode = NFSD_MAY_SATTR;
357 bool get_write_count;
358 bool size_change = (iap->ia_valid & ATTR_SIZE);
360 if (iap->ia_valid & ATTR_SIZE) {
361 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
366 * If utimes(2) and friends are called with times not NULL, we should
367 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
368 * will return EACCES, when the caller's effective UID does not match
369 * the owner of the file, and the caller is not privileged. In this
370 * situation, we should return EPERM(notify_change will return this).
372 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
373 accmode |= NFSD_MAY_OWNER_OVERRIDE;
374 if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
375 accmode |= NFSD_MAY_WRITE;
378 /* Callers that do fh_verify should do the fh_want_write: */
379 get_write_count = !fhp->fh_dentry;
382 err = fh_verify(rqstp, fhp, ftype, accmode);
385 if (get_write_count) {
386 host_err = fh_want_write(fhp);
391 dentry = fhp->fh_dentry;
392 inode = d_inode(dentry);
394 /* Ignore any mode updates on symlinks */
395 if (S_ISLNK(inode->i_mode))
396 iap->ia_valid &= ~ATTR_MODE;
401 nfsd_sanitize_attrs(inode, iap);
403 if (check_guard && guardtime != inode->i_ctime.tv_sec)
404 return nfserr_notsync;
407 * The size case is special, it changes the file in addition to the
408 * attributes, and file systems don't expect it to be mixed with
409 * "random" attribute changes. We thus split out the size change
410 * into a separate call to ->setattr, and do the rest as a separate
414 err = nfsd_get_write_access(rqstp, fhp, iap);
422 * RFC5661, Section 18.30.4:
423 * Changing the size of a file with SETATTR indirectly
424 * changes the time_modify and change attributes.
426 * (and similar for the older RFCs)
428 struct iattr size_attr = {
429 .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
430 .ia_size = iap->ia_size,
434 if (iap->ia_size < 0)
437 host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL);
440 iap->ia_valid &= ~ATTR_SIZE;
443 * Avoid the additional setattr call below if the only other
444 * attribute that the client sends is the mtime, as we update
445 * it as part of the size change above.
447 if ((iap->ia_valid & ~ATTR_MTIME) == 0)
451 iap->ia_valid |= ATTR_CTIME;
452 host_err = notify_change(&init_user_ns, dentry, iap, NULL);
455 if (attr->na_seclabel && attr->na_seclabel->len)
456 attr->na_labelerr = security_inode_setsecctx(dentry,
457 attr->na_seclabel->data, attr->na_seclabel->len);
458 if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && attr->na_pacl)
459 attr->na_aclerr = set_posix_acl(&init_user_ns,
460 inode, ACL_TYPE_ACCESS,
462 if (IS_ENABLED(CONFIG_FS_POSIX_ACL) &&
463 !attr->na_aclerr && attr->na_dpacl && S_ISDIR(inode->i_mode))
464 attr->na_aclerr = set_posix_acl(&init_user_ns,
465 inode, ACL_TYPE_DEFAULT,
469 put_write_access(inode);
472 host_err = commit_metadata(fhp);
473 return nfserrno(host_err);
476 #if defined(CONFIG_NFSD_V4)
478 * NFS junction information is stored in an extended attribute.
480 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
483 * nfsd4_is_junction - Test if an object could be an NFS junction
485 * @dentry: object to test
487 * Returns 1 if "dentry" appears to contain NFS junction information.
488 * Otherwise 0 is returned.
490 int nfsd4_is_junction(struct dentry *dentry)
492 struct inode *inode = d_inode(dentry);
496 if (inode->i_mode & S_IXUGO)
498 if (!(inode->i_mode & S_ISVTX))
500 if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME,
506 static struct nfsd4_compound_state *nfsd4_get_cstate(struct svc_rqst *rqstp)
508 return &((struct nfsd4_compoundres *)rqstp->rq_resp)->cstate;
511 __be32 nfsd4_clone_file_range(struct svc_rqst *rqstp,
512 struct nfsd_file *nf_src, u64 src_pos,
513 struct nfsd_file *nf_dst, u64 dst_pos,
514 u64 count, bool sync)
516 struct file *src = nf_src->nf_file;
517 struct file *dst = nf_dst->nf_file;
522 since = READ_ONCE(dst->f_wb_err);
523 cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
525 ret = nfserrno(cloned);
528 if (count && cloned != count) {
529 ret = nfserrno(-EINVAL);
533 loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
534 int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
537 status = filemap_check_wb_err(dst->f_mapping, since);
539 status = commit_inode_metadata(file_inode(src));
541 struct nfsd_net *nn = net_generic(nf_dst->nf_net,
544 trace_nfsd_clone_file_range_err(rqstp,
545 &nfsd4_get_cstate(rqstp)->save_fh,
547 &nfsd4_get_cstate(rqstp)->current_fh,
550 nfsd_reset_write_verifier(nn);
551 trace_nfsd_writeverf_reset(nn, rqstp, status);
552 ret = nfserrno(status);
559 ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
560 u64 dst_pos, u64 count)
565 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
566 * thread and client rpc slot. The choice of 4MB is somewhat
567 * arbitrary. We might instead base this on r/wsize, or make it
568 * tunable, or use a time instead of a byte limit, or implement
569 * asynchronous copy. In theory a client could also recognize a
570 * limit like this and pipeline multiple COPY requests.
572 count = min_t(u64, count, 1 << 22);
573 ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
575 if (ret == -EOPNOTSUPP || ret == -EXDEV)
576 ret = generic_copy_file_range(src, src_pos, dst, dst_pos,
581 __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
582 struct file *file, loff_t offset, loff_t len,
587 if (!S_ISREG(file_inode(file)->i_mode))
590 error = vfs_fallocate(file, flags, offset, len);
592 error = commit_metadata(fhp);
594 return nfserrno(error);
596 #endif /* defined(CONFIG_NFSD_V4) */
599 * Check server access rights to a file system object
605 static struct accessmap nfs3_regaccess[] = {
606 { NFS3_ACCESS_READ, NFSD_MAY_READ },
607 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
608 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
609 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
611 #ifdef CONFIG_NFSD_V4
612 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
613 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
614 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
620 static struct accessmap nfs3_diraccess[] = {
621 { NFS3_ACCESS_READ, NFSD_MAY_READ },
622 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
623 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
624 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
625 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
627 #ifdef CONFIG_NFSD_V4
628 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
629 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
630 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
636 static struct accessmap nfs3_anyaccess[] = {
637 /* Some clients - Solaris 2.6 at least, make an access call
638 * to the server to check for access for things like /dev/null
639 * (which really, the server doesn't care about). So
640 * We provide simple access checking for them, looking
641 * mainly at mode bits, and we make sure to ignore read-only
644 { NFS3_ACCESS_READ, NFSD_MAY_READ },
645 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
646 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
647 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
653 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
655 struct accessmap *map;
656 struct svc_export *export;
657 struct dentry *dentry;
658 u32 query, result = 0, sresult = 0;
661 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
665 export = fhp->fh_export;
666 dentry = fhp->fh_dentry;
668 if (d_is_reg(dentry))
669 map = nfs3_regaccess;
670 else if (d_is_dir(dentry))
671 map = nfs3_diraccess;
673 map = nfs3_anyaccess;
677 for (; map->access; map++) {
678 if (map->access & query) {
681 sresult |= map->access;
683 err2 = nfsd_permission(rqstp, export, dentry, map->how);
686 result |= map->access;
689 /* the following error codes just mean the access was not allowed,
690 * rather than an error occurred */
694 /* simply don't "or" in the access bit. */
704 *supported = sresult;
710 int nfsd_open_break_lease(struct inode *inode, int access)
714 if (access & NFSD_MAY_NOT_BREAK_LEASE)
716 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
717 return break_lease(inode, mode | O_NONBLOCK);
721 * Open an existing file or directory.
722 * The may_flags argument indicates the type of open (read/write/lock)
723 * and additional flags.
724 * N.B. After this call fhp needs an fh_put
727 __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
728 int may_flags, struct file **filp)
733 int flags = O_RDONLY|O_LARGEFILE;
737 path.mnt = fhp->fh_export->ex_path.mnt;
738 path.dentry = fhp->fh_dentry;
739 inode = d_inode(path.dentry);
742 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
748 host_err = nfsd_open_break_lease(inode, may_flags);
749 if (host_err) /* NOMEM or WOULDBLOCK */
752 if (may_flags & NFSD_MAY_WRITE) {
753 if (may_flags & NFSD_MAY_READ)
754 flags = O_RDWR|O_LARGEFILE;
756 flags = O_WRONLY|O_LARGEFILE;
759 file = dentry_open(&path, flags, current_cred());
761 host_err = PTR_ERR(file);
765 host_err = ima_file_check(file, may_flags);
771 if (may_flags & NFSD_MAY_64BIT_COOKIE)
772 file->f_mode |= FMODE_64BITHASH;
774 file->f_mode |= FMODE_32BITHASH;
778 err = nfserrno(host_err);
784 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
785 int may_flags, struct file **filp)
788 bool retried = false;
790 validate_process_creds();
792 * If we get here, then the client has already done an "open",
793 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
794 * in case a chmod has now revoked permission.
796 * Arguably we should also allow the owner override for
797 * directories, but we never have and it doesn't seem to have
798 * caused anyone a problem. If we were to change this, note
799 * also that our filldir callbacks would need a variant of
800 * lookup_one_len that doesn't check permissions.
803 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
805 err = fh_verify(rqstp, fhp, type, may_flags);
807 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
808 if (err == nfserr_stale && !retried) {
814 validate_process_creds();
819 * nfsd_open_verified - Open a regular file for the filecache
820 * @rqstp: RPC request
821 * @fhp: NFS filehandle of the file to open
822 * @may_flags: internal permission flags
823 * @filp: OUT: open "struct file *"
825 * Returns an nfsstat value in network byte order.
828 nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, int may_flags,
833 validate_process_creds();
834 err = __nfsd_open(rqstp, fhp, S_IFREG, may_flags, filp);
835 validate_process_creds();
840 * Grab and keep cached pages associated with a file in the svc_rqst
841 * so that they can be passed to the network sendmsg/sendpage routines
842 * directly. They will be released after the sending has completed.
845 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
846 struct splice_desc *sd)
848 struct svc_rqst *rqstp = sd->u.data;
850 svc_rqst_replace_page(rqstp, buf->page);
851 if (rqstp->rq_res.page_len == 0)
852 rqstp->rq_res.page_base = buf->offset;
853 rqstp->rq_res.page_len += sd->len;
857 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
858 struct splice_desc *sd)
860 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
863 static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
866 if (expected != 0 && len == 0)
868 if (offset+len >= i_size_read(file_inode(file)))
873 static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
874 struct file *file, loff_t offset,
875 unsigned long *count, u32 *eof, ssize_t host_err)
878 nfsd_stats_io_read_add(fhp->fh_export, host_err);
879 *eof = nfsd_eof_on_read(file, offset, host_err, *count);
881 fsnotify_access(file);
882 trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
885 trace_nfsd_read_err(rqstp, fhp, offset, host_err);
886 return nfserrno(host_err);
890 __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
891 struct file *file, loff_t offset, unsigned long *count,
894 struct splice_desc sd = {
902 trace_nfsd_read_splice(rqstp, fhp, offset, *count);
903 rqstp->rq_next_page = rqstp->rq_respages + 1;
904 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
905 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
908 __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
909 struct file *file, loff_t offset,
910 struct kvec *vec, int vlen, unsigned long *count,
913 struct iov_iter iter;
914 loff_t ppos = offset;
917 trace_nfsd_read_vector(rqstp, fhp, offset, *count);
918 iov_iter_kvec(&iter, READ, vec, vlen, *count);
919 host_err = vfs_iter_read(file, &iter, &ppos, 0);
920 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
924 * Gathered writes: If another process is currently writing to the file,
925 * there's a high chance this is another nfsd (triggered by a bulk write
926 * from a client's biod). Rather than syncing the file with each write
927 * request, we sleep for 10 msec.
929 * I don't know if this roughly approximates C. Juszak's idea of
930 * gathered writes, but it's a nice and simple solution (IMHO), and it
933 * Note: we do this only in the NFSv2 case, since v3 and higher have a
934 * better tool (separate unstable writes and commits) for solving this
937 static int wait_for_concurrent_writes(struct file *file)
939 struct inode *inode = file_inode(file);
940 static ino_t last_ino;
941 static dev_t last_dev;
944 if (atomic_read(&inode->i_writecount) > 1
945 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
946 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
948 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
951 if (inode->i_state & I_DIRTY) {
952 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
953 err = vfs_fsync(file, 0);
955 last_ino = inode->i_ino;
956 last_dev = inode->i_sb->s_dev;
961 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
962 loff_t offset, struct kvec *vec, int vlen,
963 unsigned long *cnt, int stable,
966 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
967 struct file *file = nf->nf_file;
968 struct super_block *sb = file_inode(file)->i_sb;
969 struct svc_export *exp;
970 struct iov_iter iter;
976 unsigned long exp_op_flags = 0;
977 unsigned int pflags = current->flags;
979 bool restore_flags = false;
981 trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
984 exp_op_flags = sb->s_export_op->flags;
986 if (test_bit(RQ_LOCAL, &rqstp->rq_flags) &&
987 !(exp_op_flags & EXPORT_OP_REMOTE_FS)) {
989 * We want throttling in balance_dirty_pages()
990 * and shrink_inactive_list() to only consider
991 * the backingdev we are writing to, so that nfs to
992 * localhost doesn't cause nfsd to lock up due to all
993 * the client's dirty pages or its congested queue.
995 current->flags |= PF_LOCAL_THROTTLE;
996 restore_flags = true;
999 exp = fhp->fh_export;
1000 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1002 if (!EX_ISSYNC(exp))
1003 stable = NFS_UNSTABLE;
1005 if (stable && !use_wgather)
1008 iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
1009 since = READ_ONCE(file->f_wb_err);
1011 nfsd_copy_write_verifier(verf, nn);
1012 host_err = vfs_iter_write(file, &iter, &pos, flags);
1014 nfsd_reset_write_verifier(nn);
1015 trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1019 nfsd_stats_io_write_add(exp, *cnt);
1020 fsnotify_modify(file);
1021 host_err = filemap_check_wb_err(file->f_mapping, since);
1025 if (stable && use_wgather) {
1026 host_err = wait_for_concurrent_writes(file);
1028 nfsd_reset_write_verifier(nn);
1029 trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1034 if (host_err >= 0) {
1035 trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1038 trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1039 nfserr = nfserrno(host_err);
1042 current_restore_flags(pflags, PF_LOCAL_THROTTLE);
1047 * Read data from a file. count must contain the requested read count
1048 * on entry. On return, *count contains the number of bytes actually read.
1049 * N.B. After this call fhp needs an fh_put
1051 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1052 loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
1055 struct nfsd_file *nf;
1059 trace_nfsd_read_start(rqstp, fhp, offset, *count);
1060 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
1065 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
1066 err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
1068 err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
1072 trace_nfsd_read_done(rqstp, fhp, offset, *count);
1078 * Write data to a file.
1079 * The stable flag requests synchronous writes.
1080 * N.B. After this call fhp needs an fh_put
1083 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1084 struct kvec *vec, int vlen, unsigned long *cnt, int stable,
1087 struct nfsd_file *nf;
1090 trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
1092 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
1096 err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
1097 vlen, cnt, stable, verf);
1100 trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
1105 * nfsd_commit - Commit pending writes to stable storage
1106 * @rqstp: RPC request being processed
1107 * @fhp: NFS filehandle
1108 * @offset: raw offset from beginning of file
1109 * @count: raw count of bytes to sync
1110 * @verf: filled in with the server's current write verifier
1112 * Note: we guarantee that data that lies within the range specified
1113 * by the 'offset' and 'count' parameters will be synced. The server
1114 * is permitted to sync data that lies outside this range at the
1117 * Unfortunately we cannot lock the file to make sure we return full WCC
1118 * data to the client, as locking happens lower down in the filesystem.
1121 * An nfsstat value in network byte order.
1124 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, u64 offset,
1125 u32 count, __be32 *verf)
1129 struct nfsd_net *nn;
1130 struct nfsd_file *nf;
1133 err = nfsd_file_acquire(rqstp, fhp,
1134 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
1139 * Convert the client-provided (offset, count) range to a
1140 * (start, end) range. If the client-provided range falls
1141 * outside the maximum file size of the underlying FS,
1142 * clamp the sync range appropriately.
1146 maxbytes = (u64)fhp->fh_dentry->d_sb->s_maxbytes;
1147 if (offset < maxbytes) {
1149 if (count && (offset + count - 1 < maxbytes))
1150 end = offset + count - 1;
1153 nn = net_generic(nf->nf_net, nfsd_net_id);
1154 if (EX_ISSYNC(fhp->fh_export)) {
1155 errseq_t since = READ_ONCE(nf->nf_file->f_wb_err);
1158 err2 = vfs_fsync_range(nf->nf_file, start, end, 0);
1161 nfsd_copy_write_verifier(verf, nn);
1162 err2 = filemap_check_wb_err(nf->nf_file->f_mapping,
1164 err = nfserrno(err2);
1167 err = nfserr_notsupp;
1170 nfsd_reset_write_verifier(nn);
1171 trace_nfsd_writeverf_reset(nn, rqstp, err2);
1172 err = nfserrno(err2);
1175 nfsd_copy_write_verifier(verf, nn);
1183 * nfsd_create_setattr - Set a created file's attributes
1184 * @rqstp: RPC transaction being executed
1185 * @fhp: NFS filehandle of parent directory
1186 * @resfhp: NFS filehandle of new object
1187 * @attrs: requested attributes of new object
1189 * Returns nfs_ok on success, or an nfsstat in network byte order.
1192 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
1193 struct svc_fh *resfhp, struct nfsd_attrs *attrs)
1195 struct iattr *iap = attrs->na_iattr;
1199 * Mode has already been set by file creation.
1201 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);
1212 * Callers expect new file metadata to be committed even
1213 * if the attributes have not changed.
1216 status = nfsd_setattr(rqstp, resfhp, attrs, 0, (time64_t)0);
1218 status = nfserrno(commit_metadata(resfhp));
1221 * Transactional filesystems had a chance to commit changes
1222 * for both parent and child simultaneously making the
1223 * following commit_metadata a noop in many cases.
1226 status = nfserrno(commit_metadata(fhp));
1229 * Update the new filehandle to pick up the new attributes.
1232 status = fh_update(resfhp);
1237 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1238 * setting size to 0 may fail for some specific file systems by the permission
1239 * checking which requires WRITE permission but the mode is 000.
1240 * we ignore the resizing(to 0) on the just new created file, since the size is
1241 * 0 after file created.
1243 * call this only after vfs_create() is called.
1246 nfsd_check_ignore_resizing(struct iattr *iap)
1248 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1249 iap->ia_valid &= ~ATTR_SIZE;
1252 /* The parent directory should already be locked: */
1254 nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
1255 char *fname, int flen, struct nfsd_attrs *attrs,
1256 int type, dev_t rdev, struct svc_fh *resfhp)
1258 struct dentry *dentry, *dchild;
1260 struct iattr *iap = attrs->na_iattr;
1264 dentry = fhp->fh_dentry;
1265 dirp = d_inode(dentry);
1267 dchild = dget(resfhp->fh_dentry);
1268 err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1272 if (!(iap->ia_valid & ATTR_MODE))
1274 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1276 if (!IS_POSIXACL(dirp))
1277 iap->ia_mode &= ~current_umask();
1283 host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
1285 nfsd_check_ignore_resizing(iap);
1288 host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode);
1289 if (!host_err && unlikely(d_unhashed(dchild))) {
1291 d = lookup_one_len(dchild->d_name.name,
1293 dchild->d_name.len);
1295 host_err = PTR_ERR(d);
1298 if (unlikely(d_is_negative(d))) {
1300 err = nfserr_serverfault;
1303 dput(resfhp->fh_dentry);
1304 resfhp->fh_dentry = dget(d);
1305 err = fh_update(resfhp);
1316 host_err = vfs_mknod(&init_user_ns, dirp, dchild,
1317 iap->ia_mode, rdev);
1320 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1327 err = nfsd_create_setattr(rqstp, fhp, resfhp, attrs);
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 nfsd_attrs *attrs,
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 inode_lock_nested(dentry->d_inode, I_MUTEX_PARENT);
1367 dchild = lookup_one_len(fname, dentry, flen);
1368 host_err = PTR_ERR(dchild);
1369 if (IS_ERR(dchild)) {
1370 err = nfserrno(host_err);
1373 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1375 * We unconditionally drop our ref to dchild as fh_compose will have
1376 * already grabbed its own ref for it.
1381 fh_fill_pre_attrs(fhp);
1382 err = nfsd_create_locked(rqstp, fhp, fname, flen, attrs, type,
1384 fh_fill_post_attrs(fhp);
1386 inode_unlock(dentry->d_inode);
1391 * Read a symlink. On entry, *lenp must contain the maximum path length that
1392 * fits into the buffer. On return, it contains the true length.
1393 * N.B. After this call fhp needs an fh_put
1396 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1401 DEFINE_DELAYED_CALL(done);
1404 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1408 path.mnt = fhp->fh_export->ex_path.mnt;
1409 path.dentry = fhp->fh_dentry;
1411 if (unlikely(!d_is_symlink(path.dentry)))
1412 return nfserr_inval;
1416 link = vfs_get_link(path.dentry, &done);
1418 return nfserrno(PTR_ERR(link));
1423 memcpy(buf, link, *lenp);
1424 do_delayed_call(&done);
1429 * nfsd_symlink - Create a symlink and look up its inode
1430 * @rqstp: RPC transaction being executed
1431 * @fhp: NFS filehandle of parent directory
1432 * @fname: filename of the new symlink
1433 * @flen: length of @fname
1434 * @path: content of the new symlink (NUL-terminated)
1435 * @attrs: requested attributes of new object
1436 * @resfhp: NFS filehandle of new object
1438 * N.B. After this call _both_ fhp and resfhp need an fh_put
1440 * Returns nfs_ok on success, or an nfsstat in network byte order.
1443 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1444 char *fname, int flen,
1445 char *path, struct nfsd_attrs *attrs,
1446 struct svc_fh *resfhp)
1448 struct dentry *dentry, *dnew;
1453 if (!flen || path[0] == '\0')
1456 if (isdotent(fname, flen))
1459 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1463 host_err = fh_want_write(fhp);
1465 err = nfserrno(host_err);
1469 dentry = fhp->fh_dentry;
1470 inode_lock_nested(dentry->d_inode, I_MUTEX_PARENT);
1471 dnew = lookup_one_len(fname, dentry, flen);
1473 err = nfserrno(PTR_ERR(dnew));
1474 inode_unlock(dentry->d_inode);
1475 goto out_drop_write;
1477 fh_fill_pre_attrs(fhp);
1478 host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
1479 err = nfserrno(host_err);
1480 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1482 nfsd_create_setattr(rqstp, fhp, resfhp, attrs);
1483 fh_fill_post_attrs(fhp);
1484 inode_unlock(dentry->d_inode);
1486 err = nfserrno(commit_metadata(fhp));
1488 if (err==0) err = cerr;
1497 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1500 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1501 char *name, int len, struct svc_fh *tfhp)
1503 struct dentry *ddir, *dnew, *dold;
1508 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1511 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1515 if (d_is_dir(tfhp->fh_dentry))
1521 if (isdotent(name, len))
1524 host_err = fh_want_write(tfhp);
1526 err = nfserrno(host_err);
1530 ddir = ffhp->fh_dentry;
1531 dirp = d_inode(ddir);
1532 inode_lock_nested(dirp, I_MUTEX_PARENT);
1534 dnew = lookup_one_len(name, ddir, len);
1536 err = nfserrno(PTR_ERR(dnew));
1540 dold = tfhp->fh_dentry;
1543 if (d_really_is_negative(dold))
1545 fh_fill_pre_attrs(ffhp);
1546 host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
1547 fh_fill_post_attrs(ffhp);
1550 err = nfserrno(commit_metadata(ffhp));
1552 err = nfserrno(commit_metadata(tfhp));
1554 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1557 err = nfserrno(host_err);
1561 fh_drop_write(tfhp);
1569 goto out_drop_write;
1573 nfsd_close_cached_files(struct dentry *dentry)
1575 struct inode *inode = d_inode(dentry);
1577 if (inode && S_ISREG(inode->i_mode))
1578 nfsd_file_close_inode_sync(inode);
1582 nfsd_has_cached_files(struct dentry *dentry)
1585 struct inode *inode = d_inode(dentry);
1587 if (inode && S_ISREG(inode->i_mode))
1588 ret = nfsd_file_is_cached(inode);
1594 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1597 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1598 struct svc_fh *tfhp, char *tname, int tlen)
1600 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1601 struct inode *fdir, *tdir;
1604 bool close_cached = false;
1606 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1609 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1613 fdentry = ffhp->fh_dentry;
1614 fdir = d_inode(fdentry);
1616 tdentry = tfhp->fh_dentry;
1617 tdir = d_inode(tdentry);
1620 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1624 host_err = fh_want_write(ffhp);
1626 err = nfserrno(host_err);
1630 trap = lock_rename(tdentry, fdentry);
1631 fh_fill_pre_attrs(ffhp);
1632 fh_fill_pre_attrs(tfhp);
1634 odentry = lookup_one_len(fname, fdentry, flen);
1635 host_err = PTR_ERR(odentry);
1636 if (IS_ERR(odentry))
1640 if (d_really_is_negative(odentry))
1643 if (odentry == trap)
1646 ndentry = lookup_one_len(tname, tdentry, tlen);
1647 host_err = PTR_ERR(ndentry);
1648 if (IS_ERR(ndentry))
1650 host_err = -ENOTEMPTY;
1651 if (ndentry == trap)
1655 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1657 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1660 if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) &&
1661 nfsd_has_cached_files(ndentry)) {
1662 close_cached = true;
1665 struct renamedata rd = {
1666 .old_mnt_userns = &init_user_ns,
1668 .old_dentry = odentry,
1669 .new_mnt_userns = &init_user_ns,
1671 .new_dentry = ndentry,
1673 host_err = vfs_rename(&rd);
1675 host_err = commit_metadata(tfhp);
1677 host_err = commit_metadata(ffhp);
1685 err = nfserrno(host_err);
1687 if (!close_cached) {
1688 fh_fill_post_attrs(ffhp);
1689 fh_fill_post_attrs(tfhp);
1691 unlock_rename(tdentry, fdentry);
1692 fh_drop_write(ffhp);
1695 * If the target dentry has cached open files, then we need to try to
1696 * close them prior to doing the rename. Flushing delayed fput
1697 * shouldn't be done with locks held however, so we delay it until this
1698 * point and then reattempt the whole shebang.
1701 close_cached = false;
1702 nfsd_close_cached_files(ndentry);
1711 * Unlink a file or directory
1712 * N.B. After this call fhp needs an fh_put
1715 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1716 char *fname, int flen)
1718 struct dentry *dentry, *rdentry;
1720 struct inode *rinode;
1725 if (!flen || isdotent(fname, flen))
1727 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1731 host_err = fh_want_write(fhp);
1735 dentry = fhp->fh_dentry;
1736 dirp = d_inode(dentry);
1737 inode_lock_nested(dirp, I_MUTEX_PARENT);
1739 rdentry = lookup_one_len(fname, dentry, flen);
1740 host_err = PTR_ERR(rdentry);
1741 if (IS_ERR(rdentry))
1744 if (d_really_is_negative(rdentry)) {
1749 rinode = d_inode(rdentry);
1753 type = d_inode(rdentry)->i_mode & S_IFMT;
1755 fh_fill_pre_attrs(fhp);
1756 if (type != S_IFDIR) {
1757 if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK)
1758 nfsd_close_cached_files(rdentry);
1759 host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL);
1761 host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
1763 fh_fill_post_attrs(fhp);
1767 host_err = commit_metadata(fhp);
1769 iput(rinode); /* truncate the inode here */
1774 if (host_err == -EBUSY) {
1775 /* name is mounted-on. There is no perfect
1778 if (nfsd_v4client(rqstp))
1779 err = nfserr_file_open;
1783 err = nfserrno(host_err);
1789 goto out_drop_write;
1793 * We do this buffering because we must not call back into the file
1794 * system's ->lookup() method from the filldir callback. That may well
1795 * deadlock a number of file systems.
1797 * This is based heavily on the implementation of same in XFS.
1799 struct buffered_dirent {
1803 unsigned int d_type;
1807 struct readdir_data {
1808 struct dir_context ctx;
1814 static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1815 int namlen, loff_t offset, u64 ino,
1816 unsigned int d_type)
1818 struct readdir_data *buf =
1819 container_of(ctx, struct readdir_data, ctx);
1820 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1821 unsigned int reclen;
1823 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1824 if (buf->used + reclen > PAGE_SIZE) {
1829 de->namlen = namlen;
1830 de->offset = offset;
1832 de->d_type = d_type;
1833 memcpy(de->name, name, namlen);
1834 buf->used += reclen;
1839 static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp,
1840 nfsd_filldir_t func, struct readdir_cd *cdp,
1843 struct buffered_dirent *de;
1847 struct readdir_data buf = {
1848 .ctx.actor = nfsd_buffered_filldir,
1849 .dirent = (void *)__get_free_page(GFP_KERNEL)
1853 return nfserrno(-ENOMEM);
1858 unsigned int reclen;
1860 cdp->err = nfserr_eof; /* will be cleared on successful read */
1864 host_err = iterate_dir(file, &buf.ctx);
1876 de = (struct buffered_dirent *)buf.dirent;
1878 offset = de->offset;
1880 if (func(cdp, de->name, de->namlen, de->offset,
1881 de->ino, de->d_type))
1884 if (cdp->err != nfs_ok)
1887 trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen);
1889 reclen = ALIGN(sizeof(*de) + de->namlen,
1892 de = (struct buffered_dirent *)((char *)de + reclen);
1894 if (size > 0) /* We bailed out early */
1897 offset = vfs_llseek(file, 0, SEEK_CUR);
1900 free_page((unsigned long)(buf.dirent));
1903 return nfserrno(host_err);
1910 * Read entries from a directory.
1911 * The NFSv3/4 verifier we ignore for now.
1914 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
1915 struct readdir_cd *cdp, nfsd_filldir_t func)
1919 loff_t offset = *offsetp;
1920 int may_flags = NFSD_MAY_READ;
1922 /* NFSv2 only supports 32 bit cookies */
1923 if (rqstp->rq_vers > 2)
1924 may_flags |= NFSD_MAY_64BIT_COOKIE;
1926 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
1930 offset = vfs_llseek(file, offset, SEEK_SET);
1932 err = nfserrno((int)offset);
1936 err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp);
1938 if (err == nfserr_eof || err == nfserr_toosmall)
1939 err = nfs_ok; /* can still be found in ->err */
1947 * Get file system stats
1948 * N.B. After this call fhp needs an fh_put
1951 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
1955 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
1957 struct path path = {
1958 .mnt = fhp->fh_export->ex_path.mnt,
1959 .dentry = fhp->fh_dentry,
1961 if (vfs_statfs(&path, stat))
1967 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
1969 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
1972 #ifdef CONFIG_NFSD_V4
1974 * Helper function to translate error numbers. In the case of xattr operations,
1975 * some error codes need to be translated outside of the standard translations.
1977 * ENODATA needs to be translated to nfserr_noxattr.
1978 * E2BIG to nfserr_xattr2big.
1980 * Additionally, vfs_listxattr can return -ERANGE. This means that the
1981 * file has too many extended attributes to retrieve inside an
1982 * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
1983 * filesystems will allow the adding of extended attributes until they hit
1984 * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
1985 * So, at that point, the attributes are present and valid, but can't
1986 * be retrieved using listxattr, since the upper level xattr code enforces
1987 * the XATTR_LIST_MAX limit.
1989 * This bug means that we need to deal with listxattr returning -ERANGE. The
1990 * best mapping is to return TOOSMALL.
1993 nfsd_xattr_errno(int err)
1997 return nfserr_noxattr;
1999 return nfserr_xattr2big;
2001 return nfserr_toosmall;
2003 return nfserrno(err);
2007 * Retrieve the specified user extended attribute. To avoid always
2008 * having to allocate the maximum size (since we are not getting
2009 * a maximum size from the RPC), do a probe + alloc. Hold a reader
2010 * lock on i_rwsem to prevent the extended attribute from changing
2011 * size while we're doing this.
2014 nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2015 void **bufp, int *lenp)
2020 struct inode *inode;
2021 struct dentry *dentry;
2023 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2028 dentry = fhp->fh_dentry;
2029 inode = d_inode(dentry);
2031 inode_lock_shared(inode);
2033 len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
2036 * Zero-length attribute, just return.
2045 err = nfsd_xattr_errno(len);
2050 err = nfserr_toosmall;
2054 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2056 err = nfserr_jukebox;
2060 len = vfs_getxattr(&init_user_ns, dentry, name, buf, len);
2064 err = nfsd_xattr_errno(len);
2071 inode_unlock_shared(inode);
2077 * Retrieve the xattr names. Since we can't know how many are
2078 * user extended attributes, we must get all attributes here,
2079 * and have the XDR encode filter out the "user." ones.
2081 * While this could always just allocate an XATTR_LIST_MAX
2082 * buffer, that's a waste, so do a probe + allocate. To
2083 * avoid any changes between the probe and allocate, wrap
2084 * this in inode_lock.
2087 nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
2093 struct inode *inode;
2094 struct dentry *dentry;
2096 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2100 dentry = fhp->fh_dentry;
2101 inode = d_inode(dentry);
2104 inode_lock_shared(inode);
2106 len = vfs_listxattr(dentry, NULL, 0);
2108 err = nfsd_xattr_errno(len);
2112 if (len > XATTR_LIST_MAX) {
2113 err = nfserr_xattr2big;
2118 * We're holding i_rwsem - use GFP_NOFS.
2120 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2122 err = nfserr_jukebox;
2126 len = vfs_listxattr(dentry, buf, len);
2129 err = nfsd_xattr_errno(len);
2138 inode_unlock_shared(inode);
2144 * nfsd_removexattr - Remove an extended attribute
2145 * @rqstp: RPC transaction being executed
2146 * @fhp: NFS filehandle of object with xattr to remove
2147 * @name: name of xattr to remove (NUL-terminate)
2149 * Pass in a NULL pointer for delegated_inode, and let the client deal
2150 * with NFS4ERR_DELAY (same as with e.g. setattr and remove).
2152 * Returns nfs_ok on success, or an nfsstat in network byte order.
2155 nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
2160 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2164 ret = fh_want_write(fhp);
2166 return nfserrno(ret);
2168 inode_lock(fhp->fh_dentry->d_inode);
2169 fh_fill_pre_attrs(fhp);
2171 ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
2174 fh_fill_post_attrs(fhp);
2175 inode_unlock(fhp->fh_dentry->d_inode);
2178 return nfsd_xattr_errno(ret);
2182 nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2183 void *buf, u32 len, u32 flags)
2188 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2192 ret = fh_want_write(fhp);
2194 return nfserrno(ret);
2195 inode_lock(fhp->fh_dentry->d_inode);
2196 fh_fill_pre_attrs(fhp);
2198 ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
2200 fh_fill_post_attrs(fhp);
2201 inode_unlock(fhp->fh_dentry->d_inode);
2204 return nfsd_xattr_errno(ret);
2209 * Check for a user's access permissions to this inode.
2212 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2213 struct dentry *dentry, int acc)
2215 struct inode *inode = d_inode(dentry);
2218 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2221 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2223 (acc & NFSD_MAY_READ)? " read" : "",
2224 (acc & NFSD_MAY_WRITE)? " write" : "",
2225 (acc & NFSD_MAY_EXEC)? " exec" : "",
2226 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2227 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2228 (acc & NFSD_MAY_LOCK)? " lock" : "",
2229 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2231 IS_IMMUTABLE(inode)? " immut" : "",
2232 IS_APPEND(inode)? " append" : "",
2233 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2234 dprintk(" owner %d/%d user %d/%d\n",
2235 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2238 /* Normally we reject any write/sattr etc access on a read-only file
2239 * system. But if it is IRIX doing check on write-access for a
2240 * device special file, we ignore rofs.
2242 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2243 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2244 if (exp_rdonly(rqstp, exp) ||
2245 __mnt_is_readonly(exp->ex_path.mnt))
2247 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2250 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2253 if (acc & NFSD_MAY_LOCK) {
2254 /* If we cannot rely on authentication in NLM requests,
2255 * just allow locks, otherwise require read permission, or
2258 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2261 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2264 * The file owner always gets access permission for accesses that
2265 * would normally be checked at open time. This is to make
2266 * file access work even when the client has done a fchmod(fd, 0).
2268 * However, `cp foo bar' should fail nevertheless when bar is
2269 * readonly. A sensible way to do this might be to reject all
2270 * attempts to truncate a read-only file, because a creat() call
2271 * always implies file truncation.
2272 * ... but this isn't really fair. A process may reasonably call
2273 * ftruncate on an open file descriptor on a file with perm 000.
2274 * We must trust the client to do permission checking - using "ACCESS"
2277 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2278 uid_eq(inode->i_uid, current_fsuid()))
2281 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2282 err = inode_permission(&init_user_ns, inode,
2283 acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
2285 /* Allow read access to binaries even when mode 111 */
2286 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2287 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2288 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2289 err = inode_permission(&init_user_ns, inode, MAY_EXEC);
2291 return err? nfserrno(err) : 0;