1 // SPDX-License-Identifier: GPL-2.0
3 * File operations used by nfsd. Some of these have been ripped from
4 * other parts of the kernel because they weren't exported, others
5 * are partial duplicates with added or changed functionality.
7 * Note that several functions dget() the dentry upon which they want
8 * to act, most notably those that create directory entries. Response
9 * dentry's are dput()'d if necessary in the release callback.
10 * So if you notice code paths that apparently fail to dput() the
11 * dentry, don't worry--they have been taken care of.
13 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
18 #include <linux/file.h>
19 #include <linux/splice.h>
20 #include <linux/falloc.h>
21 #include <linux/fcntl.h>
22 #include <linux/namei.h>
23 #include <linux/delay.h>
24 #include <linux/fsnotify.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/xattr.h>
27 #include <linux/jhash.h>
28 #include <linux/ima.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 #include <linux/exportfs.h>
32 #include <linux/writeback.h>
33 #include <linux/security.h>
37 #endif /* CONFIG_NFSD_V3 */
40 #include "../internal.h"
43 #endif /* CONFIG_NFSD_V4 */
47 #include "filecache.h"
50 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
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
);
203 * In the nfsd4_open() case, this may be held across
204 * subsequent open and delegation acquisition which may
205 * need to take the child's i_mutex:
207 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
208 dentry
= lookup_one_len(name
, dparent
, len
);
209 host_err
= PTR_ERR(dentry
);
212 if (nfsd_mountpoint(dentry
, exp
)) {
214 * We don't need the i_mutex after all. It's
215 * still possible we could open this (regular
216 * files can be mountpoints too), but the
217 * i_mutex is just there to prevent renames of
218 * something that we might be about to delegate,
219 * and a mountpoint won't be renamed:
222 if ((host_err
= nfsd_cross_mnt(rqstp
, &dentry
, &exp
))) {
228 *dentry_ret
= dentry
;
234 return nfserrno(host_err
);
238 * Look up one component of a pathname.
239 * N.B. After this call _both_ fhp and resfh need an fh_put
241 * If the lookup would cross a mountpoint, and the mounted filesystem
242 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
243 * accepted as it stands and the mounted directory is
244 * returned. Otherwise the covered directory is returned.
245 * NOTE: this mountpoint crossing is not supported properly by all
246 * clients and is explicitly disallowed for NFSv3
249 nfsd_lookup(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, const char *name
,
250 unsigned int len
, struct svc_fh
*resfh
)
252 struct svc_export
*exp
;
253 struct dentry
*dentry
;
256 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_EXEC
);
259 err
= nfsd_lookup_dentry(rqstp
, fhp
, name
, len
, &exp
, &dentry
);
262 err
= check_nfsd_access(exp
, rqstp
);
266 * Note: we compose the file handle now, but as the
267 * dentry may be negative, it may need to be updated.
269 err
= fh_compose(resfh
, exp
, dentry
, fhp
);
270 if (!err
&& d_really_is_negative(dentry
))
279 * Commit metadata changes to stable storage.
282 commit_inode_metadata(struct inode
*inode
)
284 const struct export_operations
*export_ops
= inode
->i_sb
->s_export_op
;
286 if (export_ops
->commit_metadata
)
287 return export_ops
->commit_metadata(inode
);
288 return sync_inode_metadata(inode
, 1);
292 commit_metadata(struct svc_fh
*fhp
)
294 struct inode
*inode
= d_inode(fhp
->fh_dentry
);
296 if (!EX_ISSYNC(fhp
->fh_export
))
298 return commit_inode_metadata(inode
);
302 * Go over the attributes and take care of the small differences between
303 * NFS semantics and what Linux expects.
306 nfsd_sanitize_attrs(struct inode
*inode
, struct iattr
*iap
)
308 /* sanitize the mode change */
309 if (iap
->ia_valid
& ATTR_MODE
) {
310 iap
->ia_mode
&= S_IALLUGO
;
311 iap
->ia_mode
|= (inode
->i_mode
& ~S_IALLUGO
);
314 /* Revoke setuid/setgid on chown */
315 if (!S_ISDIR(inode
->i_mode
) &&
316 ((iap
->ia_valid
& ATTR_UID
) || (iap
->ia_valid
& ATTR_GID
))) {
317 iap
->ia_valid
|= ATTR_KILL_PRIV
;
318 if (iap
->ia_valid
& ATTR_MODE
) {
319 /* we're setting mode too, just clear the s*id bits */
320 iap
->ia_mode
&= ~S_ISUID
;
321 if (iap
->ia_mode
& S_IXGRP
)
322 iap
->ia_mode
&= ~S_ISGID
;
324 /* set ATTR_KILL_* bits and let VFS handle it */
325 iap
->ia_valid
|= (ATTR_KILL_SUID
| ATTR_KILL_SGID
);
331 nfsd_get_write_access(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
334 struct inode
*inode
= d_inode(fhp
->fh_dentry
);
336 if (iap
->ia_size
< inode
->i_size
) {
339 err
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
340 NFSD_MAY_TRUNC
| NFSD_MAY_OWNER_OVERRIDE
);
344 return nfserrno(get_write_access(inode
));
348 * Set various file attributes. After this call fhp needs an fh_put.
351 nfsd_setattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct iattr
*iap
,
352 int check_guard
, time64_t guardtime
)
354 struct dentry
*dentry
;
356 int accmode
= NFSD_MAY_SATTR
;
360 bool get_write_count
;
361 bool size_change
= (iap
->ia_valid
& ATTR_SIZE
);
363 if (iap
->ia_valid
& ATTR_SIZE
) {
364 accmode
|= NFSD_MAY_WRITE
|NFSD_MAY_OWNER_OVERRIDE
;
369 * If utimes(2) and friends are called with times not NULL, we should
370 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
371 * will return EACCES, when the caller's effective UID does not match
372 * the owner of the file, and the caller is not privileged. In this
373 * situation, we should return EPERM(notify_change will return this).
375 if (iap
->ia_valid
& (ATTR_ATIME
| ATTR_MTIME
)) {
376 accmode
|= NFSD_MAY_OWNER_OVERRIDE
;
377 if (!(iap
->ia_valid
& (ATTR_ATIME_SET
| ATTR_MTIME_SET
)))
378 accmode
|= NFSD_MAY_WRITE
;
381 /* Callers that do fh_verify should do the fh_want_write: */
382 get_write_count
= !fhp
->fh_dentry
;
385 err
= fh_verify(rqstp
, fhp
, ftype
, accmode
);
388 if (get_write_count
) {
389 host_err
= fh_want_write(fhp
);
394 dentry
= fhp
->fh_dentry
;
395 inode
= d_inode(dentry
);
397 /* Ignore any mode updates on symlinks */
398 if (S_ISLNK(inode
->i_mode
))
399 iap
->ia_valid
&= ~ATTR_MODE
;
404 nfsd_sanitize_attrs(inode
, iap
);
406 if (check_guard
&& guardtime
!= inode
->i_ctime
.tv_sec
)
407 return nfserr_notsync
;
410 * The size case is special, it changes the file in addition to the
411 * attributes, and file systems don't expect it to be mixed with
412 * "random" attribute changes. We thus split out the size change
413 * into a separate call to ->setattr, and do the rest as a separate
417 err
= nfsd_get_write_access(rqstp
, fhp
, iap
);
425 * RFC5661, Section 18.30.4:
426 * Changing the size of a file with SETATTR indirectly
427 * changes the time_modify and change attributes.
429 * (and similar for the older RFCs)
431 struct iattr size_attr
= {
432 .ia_valid
= ATTR_SIZE
| ATTR_CTIME
| ATTR_MTIME
,
433 .ia_size
= iap
->ia_size
,
436 host_err
= notify_change(&init_user_ns
, dentry
, &size_attr
, NULL
);
439 iap
->ia_valid
&= ~ATTR_SIZE
;
442 * Avoid the additional setattr call below if the only other
443 * attribute that the client sends is the mtime, as we update
444 * it as part of the size change above.
446 if ((iap
->ia_valid
& ~ATTR_MTIME
) == 0)
450 iap
->ia_valid
|= ATTR_CTIME
;
451 host_err
= notify_change(&init_user_ns
, dentry
, iap
, NULL
);
456 put_write_access(inode
);
459 host_err
= commit_metadata(fhp
);
460 return nfserrno(host_err
);
463 #if defined(CONFIG_NFSD_V4)
465 * NFS junction information is stored in an extended attribute.
467 #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
470 * nfsd4_is_junction - Test if an object could be an NFS junction
472 * @dentry: object to test
474 * Returns 1 if "dentry" appears to contain NFS junction information.
475 * Otherwise 0 is returned.
477 int nfsd4_is_junction(struct dentry
*dentry
)
479 struct inode
*inode
= d_inode(dentry
);
483 if (inode
->i_mode
& S_IXUGO
)
485 if (!(inode
->i_mode
& S_ISVTX
))
487 if (vfs_getxattr(&init_user_ns
, dentry
, NFSD_JUNCTION_XATTR_NAME
,
492 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
493 __be32
nfsd4_set_nfs4_label(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
494 struct xdr_netobj
*label
)
498 struct dentry
*dentry
;
500 error
= fh_verify(rqstp
, fhp
, 0 /* S_IFREG */, NFSD_MAY_SATTR
);
504 dentry
= fhp
->fh_dentry
;
506 inode_lock(d_inode(dentry
));
507 host_error
= security_inode_setsecctx(dentry
, label
->data
, label
->len
);
508 inode_unlock(d_inode(dentry
));
509 return nfserrno(host_error
);
512 __be32
nfsd4_set_nfs4_label(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
513 struct xdr_netobj
*label
)
515 return nfserr_notsupp
;
519 __be32
nfsd4_clone_file_range(struct nfsd_file
*nf_src
, u64 src_pos
,
520 struct nfsd_file
*nf_dst
, u64 dst_pos
, u64 count
, bool sync
)
522 struct file
*src
= nf_src
->nf_file
;
523 struct file
*dst
= nf_dst
->nf_file
;
527 down_write(&nf_dst
->nf_rwsem
);
528 cloned
= vfs_clone_file_range(src
, src_pos
, dst
, dst_pos
, count
, 0);
530 ret
= nfserrno(cloned
);
533 if (count
&& cloned
!= count
) {
534 ret
= nfserrno(-EINVAL
);
538 loff_t dst_end
= count
? dst_pos
+ count
- 1 : LLONG_MAX
;
539 int status
= vfs_fsync_range(dst
, dst_pos
, dst_end
, 0);
542 status
= commit_inode_metadata(file_inode(src
));
544 nfsd_reset_boot_verifier(net_generic(nf_dst
->nf_net
,
546 ret
= nfserrno(status
);
550 up_write(&nf_dst
->nf_rwsem
);
554 ssize_t
nfsd_copy_file_range(struct file
*src
, u64 src_pos
, struct file
*dst
,
555 u64 dst_pos
, u64 count
)
559 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
560 * thread and client rpc slot. The choice of 4MB is somewhat
561 * arbitrary. We might instead base this on r/wsize, or make it
562 * tunable, or use a time instead of a byte limit, or implement
563 * asynchronous copy. In theory a client could also recognize a
564 * limit like this and pipeline multiple COPY requests.
566 count
= min_t(u64
, count
, 1 << 22);
567 return vfs_copy_file_range(src
, src_pos
, dst
, dst_pos
, count
, 0);
570 __be32
nfsd4_vfs_fallocate(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
571 struct file
*file
, loff_t offset
, loff_t len
,
576 if (!S_ISREG(file_inode(file
)->i_mode
))
579 error
= vfs_fallocate(file
, flags
, offset
, len
);
581 error
= commit_metadata(fhp
);
583 return nfserrno(error
);
585 #endif /* defined(CONFIG_NFSD_V4) */
587 #ifdef CONFIG_NFSD_V3
589 * Check server access rights to a file system object
595 static struct accessmap nfs3_regaccess
[] = {
596 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
597 { NFS3_ACCESS_EXECUTE
, NFSD_MAY_EXEC
},
598 { NFS3_ACCESS_MODIFY
, NFSD_MAY_WRITE
|NFSD_MAY_TRUNC
},
599 { NFS3_ACCESS_EXTEND
, NFSD_MAY_WRITE
},
601 #ifdef CONFIG_NFSD_V4
602 { NFS4_ACCESS_XAREAD
, NFSD_MAY_READ
},
603 { NFS4_ACCESS_XAWRITE
, NFSD_MAY_WRITE
},
604 { NFS4_ACCESS_XALIST
, NFSD_MAY_READ
},
610 static struct accessmap nfs3_diraccess
[] = {
611 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
612 { NFS3_ACCESS_LOOKUP
, NFSD_MAY_EXEC
},
613 { NFS3_ACCESS_MODIFY
, NFSD_MAY_EXEC
|NFSD_MAY_WRITE
|NFSD_MAY_TRUNC
},
614 { NFS3_ACCESS_EXTEND
, NFSD_MAY_EXEC
|NFSD_MAY_WRITE
},
615 { NFS3_ACCESS_DELETE
, NFSD_MAY_REMOVE
},
617 #ifdef CONFIG_NFSD_V4
618 { NFS4_ACCESS_XAREAD
, NFSD_MAY_READ
},
619 { NFS4_ACCESS_XAWRITE
, NFSD_MAY_WRITE
},
620 { NFS4_ACCESS_XALIST
, NFSD_MAY_READ
},
626 static struct accessmap nfs3_anyaccess
[] = {
627 /* Some clients - Solaris 2.6 at least, make an access call
628 * to the server to check for access for things like /dev/null
629 * (which really, the server doesn't care about). So
630 * We provide simple access checking for them, looking
631 * mainly at mode bits, and we make sure to ignore read-only
634 { NFS3_ACCESS_READ
, NFSD_MAY_READ
},
635 { NFS3_ACCESS_EXECUTE
, NFSD_MAY_EXEC
},
636 { NFS3_ACCESS_MODIFY
, NFSD_MAY_WRITE
|NFSD_MAY_LOCAL_ACCESS
},
637 { NFS3_ACCESS_EXTEND
, NFSD_MAY_WRITE
|NFSD_MAY_LOCAL_ACCESS
},
643 nfsd_access(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, u32
*access
, u32
*supported
)
645 struct accessmap
*map
;
646 struct svc_export
*export
;
647 struct dentry
*dentry
;
648 u32 query
, result
= 0, sresult
= 0;
651 error
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_NOP
);
655 export
= fhp
->fh_export
;
656 dentry
= fhp
->fh_dentry
;
658 if (d_is_reg(dentry
))
659 map
= nfs3_regaccess
;
660 else if (d_is_dir(dentry
))
661 map
= nfs3_diraccess
;
663 map
= nfs3_anyaccess
;
667 for (; map
->access
; map
++) {
668 if (map
->access
& query
) {
671 sresult
|= map
->access
;
673 err2
= nfsd_permission(rqstp
, export
, dentry
, map
->how
);
676 result
|= map
->access
;
679 /* the following error codes just mean the access was not allowed,
680 * rather than an error occurred */
684 /* simply don't "or" in the access bit. */
694 *supported
= sresult
;
699 #endif /* CONFIG_NFSD_V3 */
701 int nfsd_open_break_lease(struct inode
*inode
, int access
)
705 if (access
& NFSD_MAY_NOT_BREAK_LEASE
)
707 mode
= (access
& NFSD_MAY_WRITE
) ? O_WRONLY
: O_RDONLY
;
708 return break_lease(inode
, mode
| O_NONBLOCK
);
712 * Open an existing file or directory.
713 * The may_flags argument indicates the type of open (read/write/lock)
714 * and additional flags.
715 * N.B. After this call fhp needs an fh_put
718 __nfsd_open(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, umode_t type
,
719 int may_flags
, struct file
**filp
)
724 int flags
= O_RDONLY
|O_LARGEFILE
;
728 path
.mnt
= fhp
->fh_export
->ex_path
.mnt
;
729 path
.dentry
= fhp
->fh_dentry
;
730 inode
= d_inode(path
.dentry
);
732 /* Disallow write access to files with the append-only bit set
733 * or any access when mandatory locking enabled
736 if (IS_APPEND(inode
) && (may_flags
& NFSD_MAY_WRITE
))
742 host_err
= nfsd_open_break_lease(inode
, may_flags
);
743 if (host_err
) /* NOMEM or WOULDBLOCK */
746 if (may_flags
& NFSD_MAY_WRITE
) {
747 if (may_flags
& NFSD_MAY_READ
)
748 flags
= O_RDWR
|O_LARGEFILE
;
750 flags
= O_WRONLY
|O_LARGEFILE
;
753 file
= dentry_open(&path
, flags
, current_cred());
755 host_err
= PTR_ERR(file
);
759 host_err
= ima_file_check(file
, may_flags
);
765 if (may_flags
& NFSD_MAY_64BIT_COOKIE
)
766 file
->f_mode
|= FMODE_64BITHASH
;
768 file
->f_mode
|= FMODE_32BITHASH
;
772 err
= nfserrno(host_err
);
778 nfsd_open(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, umode_t type
,
779 int may_flags
, struct file
**filp
)
783 validate_process_creds();
785 * If we get here, then the client has already done an "open",
786 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
787 * in case a chmod has now revoked permission.
789 * Arguably we should also allow the owner override for
790 * directories, but we never have and it doesn't seem to have
791 * caused anyone a problem. If we were to change this, note
792 * also that our filldir callbacks would need a variant of
793 * lookup_one_len that doesn't check permissions.
796 may_flags
|= NFSD_MAY_OWNER_OVERRIDE
;
797 err
= fh_verify(rqstp
, fhp
, type
, may_flags
);
799 err
= __nfsd_open(rqstp
, fhp
, type
, may_flags
, filp
);
800 validate_process_creds();
805 nfsd_open_verified(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, umode_t type
,
806 int may_flags
, struct file
**filp
)
810 validate_process_creds();
811 err
= __nfsd_open(rqstp
, fhp
, type
, may_flags
, filp
);
812 validate_process_creds();
817 * Grab and keep cached pages associated with a file in the svc_rqst
818 * so that they can be passed to the network sendmsg/sendpage routines
819 * directly. They will be released after the sending has completed.
822 nfsd_splice_actor(struct pipe_inode_info
*pipe
, struct pipe_buffer
*buf
,
823 struct splice_desc
*sd
)
825 struct svc_rqst
*rqstp
= sd
->u
.data
;
826 struct page
**pp
= rqstp
->rq_next_page
;
827 struct page
*page
= buf
->page
;
829 if (rqstp
->rq_res
.page_len
== 0) {
830 svc_rqst_replace_page(rqstp
, page
);
831 rqstp
->rq_res
.page_base
= buf
->offset
;
832 } else if (page
!= pp
[-1]) {
833 svc_rqst_replace_page(rqstp
, page
);
835 rqstp
->rq_res
.page_len
+= sd
->len
;
840 static int nfsd_direct_splice_actor(struct pipe_inode_info
*pipe
,
841 struct splice_desc
*sd
)
843 return __splice_from_pipe(pipe
, sd
, nfsd_splice_actor
);
846 static u32
nfsd_eof_on_read(struct file
*file
, loff_t offset
, ssize_t len
,
849 if (expected
!= 0 && len
== 0)
851 if (offset
+len
>= i_size_read(file_inode(file
)))
856 static __be32
nfsd_finish_read(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
857 struct file
*file
, loff_t offset
,
858 unsigned long *count
, u32
*eof
, ssize_t host_err
)
861 nfsd_stats_io_read_add(fhp
->fh_export
, host_err
);
862 *eof
= nfsd_eof_on_read(file
, offset
, host_err
, *count
);
864 fsnotify_access(file
);
865 trace_nfsd_read_io_done(rqstp
, fhp
, offset
, *count
);
868 trace_nfsd_read_err(rqstp
, fhp
, offset
, host_err
);
869 return nfserrno(host_err
);
873 __be32
nfsd_splice_read(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
874 struct file
*file
, loff_t offset
, unsigned long *count
,
877 struct splice_desc sd
= {
885 trace_nfsd_read_splice(rqstp
, fhp
, offset
, *count
);
886 rqstp
->rq_next_page
= rqstp
->rq_respages
+ 1;
887 host_err
= splice_direct_to_actor(file
, &sd
, nfsd_direct_splice_actor
);
888 return nfsd_finish_read(rqstp
, fhp
, file
, offset
, count
, eof
, host_err
);
891 __be32
nfsd_readv(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
892 struct file
*file
, loff_t offset
,
893 struct kvec
*vec
, int vlen
, unsigned long *count
,
896 struct iov_iter iter
;
897 loff_t ppos
= offset
;
900 trace_nfsd_read_vector(rqstp
, fhp
, offset
, *count
);
901 iov_iter_kvec(&iter
, READ
, vec
, vlen
, *count
);
902 host_err
= vfs_iter_read(file
, &iter
, &ppos
, 0);
903 return nfsd_finish_read(rqstp
, fhp
, file
, offset
, count
, eof
, host_err
);
907 * Gathered writes: If another process is currently writing to the file,
908 * there's a high chance this is another nfsd (triggered by a bulk write
909 * from a client's biod). Rather than syncing the file with each write
910 * request, we sleep for 10 msec.
912 * I don't know if this roughly approximates C. Juszak's idea of
913 * gathered writes, but it's a nice and simple solution (IMHO), and it
916 * Note: we do this only in the NFSv2 case, since v3 and higher have a
917 * better tool (separate unstable writes and commits) for solving this
920 static int wait_for_concurrent_writes(struct file
*file
)
922 struct inode
*inode
= file_inode(file
);
923 static ino_t last_ino
;
924 static dev_t last_dev
;
927 if (atomic_read(&inode
->i_writecount
) > 1
928 || (last_ino
== inode
->i_ino
&& last_dev
== inode
->i_sb
->s_dev
)) {
929 dprintk("nfsd: write defer %d\n", task_pid_nr(current
));
931 dprintk("nfsd: write resume %d\n", task_pid_nr(current
));
934 if (inode
->i_state
& I_DIRTY
) {
935 dprintk("nfsd: write sync %d\n", task_pid_nr(current
));
936 err
= vfs_fsync(file
, 0);
938 last_ino
= inode
->i_ino
;
939 last_dev
= inode
->i_sb
->s_dev
;
944 nfsd_vfs_write(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct nfsd_file
*nf
,
945 loff_t offset
, struct kvec
*vec
, int vlen
,
946 unsigned long *cnt
, int stable
,
949 struct file
*file
= nf
->nf_file
;
950 struct super_block
*sb
= file_inode(file
)->i_sb
;
951 struct svc_export
*exp
;
952 struct iov_iter iter
;
957 unsigned long exp_op_flags
= 0;
958 unsigned int pflags
= current
->flags
;
960 bool restore_flags
= false;
962 trace_nfsd_write_opened(rqstp
, fhp
, offset
, *cnt
);
965 exp_op_flags
= sb
->s_export_op
->flags
;
967 if (test_bit(RQ_LOCAL
, &rqstp
->rq_flags
) &&
968 !(exp_op_flags
& EXPORT_OP_REMOTE_FS
)) {
970 * We want throttling in balance_dirty_pages()
971 * and shrink_inactive_list() to only consider
972 * the backingdev we are writing to, so that nfs to
973 * localhost doesn't cause nfsd to lock up due to all
974 * the client's dirty pages or its congested queue.
976 current
->flags
|= PF_LOCAL_THROTTLE
;
977 restore_flags
= true;
980 exp
= fhp
->fh_export
;
981 use_wgather
= (rqstp
->rq_vers
== 2) && EX_WGATHER(exp
);
984 stable
= NFS_UNSTABLE
;
986 if (stable
&& !use_wgather
)
989 iov_iter_kvec(&iter
, WRITE
, vec
, vlen
, *cnt
);
990 if (flags
& RWF_SYNC
) {
991 down_write(&nf
->nf_rwsem
);
992 host_err
= vfs_iter_write(file
, &iter
, &pos
, flags
);
994 nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp
),
996 up_write(&nf
->nf_rwsem
);
998 down_read(&nf
->nf_rwsem
);
1000 nfsd_copy_boot_verifier(verf
,
1001 net_generic(SVC_NET(rqstp
),
1003 host_err
= vfs_iter_write(file
, &iter
, &pos
, flags
);
1004 up_read(&nf
->nf_rwsem
);
1007 nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp
),
1012 nfsd_stats_io_write_add(exp
, *cnt
);
1013 fsnotify_modify(file
);
1015 if (stable
&& use_wgather
) {
1016 host_err
= wait_for_concurrent_writes(file
);
1018 nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp
),
1023 if (host_err
>= 0) {
1024 trace_nfsd_write_io_done(rqstp
, fhp
, offset
, *cnt
);
1027 trace_nfsd_write_err(rqstp
, fhp
, offset
, host_err
);
1028 nfserr
= nfserrno(host_err
);
1031 current_restore_flags(pflags
, PF_LOCAL_THROTTLE
);
1036 * Read data from a file. count must contain the requested read count
1037 * on entry. On return, *count contains the number of bytes actually read.
1038 * N.B. After this call fhp needs an fh_put
1040 __be32
nfsd_read(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1041 loff_t offset
, struct kvec
*vec
, int vlen
, unsigned long *count
,
1044 struct nfsd_file
*nf
;
1048 trace_nfsd_read_start(rqstp
, fhp
, offset
, *count
);
1049 err
= nfsd_file_acquire(rqstp
, fhp
, NFSD_MAY_READ
, &nf
);
1054 if (file
->f_op
->splice_read
&& test_bit(RQ_SPLICE_OK
, &rqstp
->rq_flags
))
1055 err
= nfsd_splice_read(rqstp
, fhp
, file
, offset
, count
, eof
);
1057 err
= nfsd_readv(rqstp
, fhp
, file
, offset
, vec
, vlen
, count
, eof
);
1061 trace_nfsd_read_done(rqstp
, fhp
, offset
, *count
);
1067 * Write data to a file.
1068 * The stable flag requests synchronous writes.
1069 * N.B. After this call fhp needs an fh_put
1072 nfsd_write(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, loff_t offset
,
1073 struct kvec
*vec
, int vlen
, unsigned long *cnt
, int stable
,
1076 struct nfsd_file
*nf
;
1079 trace_nfsd_write_start(rqstp
, fhp
, offset
, *cnt
);
1081 err
= nfsd_file_acquire(rqstp
, fhp
, NFSD_MAY_WRITE
, &nf
);
1085 err
= nfsd_vfs_write(rqstp
, fhp
, nf
, offset
, vec
,
1086 vlen
, cnt
, stable
, verf
);
1089 trace_nfsd_write_done(rqstp
, fhp
, offset
, *cnt
);
1093 #ifdef CONFIG_NFSD_V3
1095 nfsd_filemap_write_and_wait_range(struct nfsd_file
*nf
, loff_t offset
,
1098 struct address_space
*mapping
= nf
->nf_file
->f_mapping
;
1099 int ret
= filemap_fdatawrite_range(mapping
, offset
, end
);
1103 filemap_fdatawait_range_keep_errors(mapping
, offset
, end
);
1108 * Commit all pending writes to stable storage.
1110 * Note: we only guarantee that data that lies within the range specified
1111 * by the 'offset' and 'count' parameters will be synced.
1113 * Unfortunately we cannot lock the file to make sure we return full WCC
1114 * data to the client, as locking happens lower down in the filesystem.
1117 nfsd_commit(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1118 loff_t offset
, unsigned long count
, __be32
*verf
)
1120 struct nfsd_file
*nf
;
1121 loff_t end
= LLONG_MAX
;
1122 __be32 err
= nfserr_inval
;
1127 end
= offset
+ (loff_t
)count
- 1;
1132 err
= nfsd_file_acquire(rqstp
, fhp
,
1133 NFSD_MAY_WRITE
|NFSD_MAY_NOT_BREAK_LEASE
, &nf
);
1136 if (EX_ISSYNC(fhp
->fh_export
)) {
1137 int err2
= nfsd_filemap_write_and_wait_range(nf
, offset
, end
);
1139 down_write(&nf
->nf_rwsem
);
1141 err2
= vfs_fsync_range(nf
->nf_file
, offset
, end
, 0);
1144 nfsd_copy_boot_verifier(verf
, net_generic(nf
->nf_net
,
1148 err
= nfserr_notsupp
;
1151 err
= nfserrno(err2
);
1152 nfsd_reset_boot_verifier(net_generic(nf
->nf_net
,
1155 up_write(&nf
->nf_rwsem
);
1157 nfsd_copy_boot_verifier(verf
, net_generic(nf
->nf_net
,
1164 #endif /* CONFIG_NFSD_V3 */
1167 nfsd_create_setattr(struct svc_rqst
*rqstp
, struct svc_fh
*resfhp
,
1171 * Mode has already been set earlier in create:
1173 iap
->ia_valid
&= ~ATTR_MODE
;
1175 * Setting uid/gid works only for root. Irix appears to
1176 * send along the gid on create when it tries to implement
1177 * setgid directories via NFS:
1179 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
))
1180 iap
->ia_valid
&= ~(ATTR_UID
|ATTR_GID
);
1182 return nfsd_setattr(rqstp
, resfhp
, iap
, 0, (time64_t
)0);
1183 /* Callers expect file metadata to be committed here */
1184 return nfserrno(commit_metadata(resfhp
));
1187 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1188 * setting size to 0 may fail for some specific file systems by the permission
1189 * checking which requires WRITE permission but the mode is 000.
1190 * we ignore the resizing(to 0) on the just new created file, since the size is
1191 * 0 after file created.
1193 * call this only after vfs_create() is called.
1196 nfsd_check_ignore_resizing(struct iattr
*iap
)
1198 if ((iap
->ia_valid
& ATTR_SIZE
) && (iap
->ia_size
== 0))
1199 iap
->ia_valid
&= ~ATTR_SIZE
;
1202 /* The parent directory should already be locked: */
1204 nfsd_create_locked(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1205 char *fname
, int flen
, struct iattr
*iap
,
1206 int type
, dev_t rdev
, struct svc_fh
*resfhp
)
1208 struct dentry
*dentry
, *dchild
;
1214 dentry
= fhp
->fh_dentry
;
1215 dirp
= d_inode(dentry
);
1217 dchild
= dget(resfhp
->fh_dentry
);
1218 if (!fhp
->fh_locked
) {
1219 WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1225 err
= nfsd_permission(rqstp
, fhp
->fh_export
, dentry
, NFSD_MAY_CREATE
);
1229 if (!(iap
->ia_valid
& ATTR_MODE
))
1231 iap
->ia_mode
= (iap
->ia_mode
& S_IALLUGO
) | type
;
1233 if (!IS_POSIXACL(dirp
))
1234 iap
->ia_mode
&= ~current_umask();
1240 host_err
= vfs_create(&init_user_ns
, dirp
, dchild
, iap
->ia_mode
, true);
1242 nfsd_check_ignore_resizing(iap
);
1245 host_err
= vfs_mkdir(&init_user_ns
, dirp
, dchild
, iap
->ia_mode
);
1246 if (!host_err
&& unlikely(d_unhashed(dchild
))) {
1248 d
= lookup_one_len(dchild
->d_name
.name
,
1250 dchild
->d_name
.len
);
1252 host_err
= PTR_ERR(d
);
1255 if (unlikely(d_is_negative(d
))) {
1257 err
= nfserr_serverfault
;
1260 dput(resfhp
->fh_dentry
);
1261 resfhp
->fh_dentry
= dget(d
);
1262 err
= fh_update(resfhp
);
1273 host_err
= vfs_mknod(&init_user_ns
, dirp
, dchild
,
1274 iap
->ia_mode
, rdev
);
1277 printk(KERN_WARNING
"nfsd: bad file type %o in nfsd_create\n",
1284 err
= nfsd_create_setattr(rqstp
, resfhp
, iap
);
1287 * nfsd_create_setattr already committed the child. Transactional
1288 * filesystems had a chance to commit changes for both parent and
1289 * child simultaneously making the following commit_metadata a
1292 err2
= nfserrno(commit_metadata(fhp
));
1296 * Update the file handle to get the new inode info.
1299 err
= fh_update(resfhp
);
1305 err
= nfserrno(host_err
);
1310 * Create a filesystem object (regular, directory, special).
1311 * Note that the parent directory is left locked.
1313 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1316 nfsd_create(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1317 char *fname
, int flen
, struct iattr
*iap
,
1318 int type
, dev_t rdev
, struct svc_fh
*resfhp
)
1320 struct dentry
*dentry
, *dchild
= NULL
;
1324 if (isdotent(fname
, flen
))
1325 return nfserr_exist
;
1327 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_NOP
);
1331 dentry
= fhp
->fh_dentry
;
1333 host_err
= fh_want_write(fhp
);
1335 return nfserrno(host_err
);
1337 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1338 dchild
= lookup_one_len(fname
, dentry
, flen
);
1339 host_err
= PTR_ERR(dchild
);
1341 return nfserrno(host_err
);
1342 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
, fhp
);
1344 * We unconditionally drop our ref to dchild as fh_compose will have
1345 * already grabbed its own ref for it.
1350 return nfsd_create_locked(rqstp
, fhp
, fname
, flen
, iap
, type
,
1354 #ifdef CONFIG_NFSD_V3
1357 * NFSv3 and NFSv4 version of nfsd_create
1360 do_nfsd_create(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1361 char *fname
, int flen
, struct iattr
*iap
,
1362 struct svc_fh
*resfhp
, int createmode
, u32
*verifier
,
1363 bool *truncp
, bool *created
)
1365 struct dentry
*dentry
, *dchild
= NULL
;
1369 __u32 v_mtime
=0, v_atime
=0;
1375 if (isdotent(fname
, flen
))
1377 if (!(iap
->ia_valid
& ATTR_MODE
))
1379 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_EXEC
);
1383 dentry
= fhp
->fh_dentry
;
1384 dirp
= d_inode(dentry
);
1386 host_err
= fh_want_write(fhp
);
1390 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1393 * Compose the response file handle.
1395 dchild
= lookup_one_len(fname
, dentry
, flen
);
1396 host_err
= PTR_ERR(dchild
);
1400 /* If file doesn't exist, check for permissions to create one */
1401 if (d_really_is_negative(dchild
)) {
1402 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1407 err
= fh_compose(resfhp
, fhp
->fh_export
, dchild
, fhp
);
1411 if (nfsd_create_is_exclusive(createmode
)) {
1412 /* solaris7 gets confused (bugid 4218508) if these have
1413 * the high bit set, so just clear the high bits. If this is
1414 * ever changed to use different attrs for storing the
1415 * verifier, then do_open_lookup() will also need to be fixed
1418 v_mtime
= verifier
[0]&0x7fffffff;
1419 v_atime
= verifier
[1]&0x7fffffff;
1422 if (d_really_is_positive(dchild
)) {
1425 switch (createmode
) {
1426 case NFS3_CREATE_UNCHECKED
:
1427 if (! d_is_reg(dchild
))
1430 /* in nfsv4, we need to treat this case a little
1431 * differently. we don't want to truncate the
1432 * file now; this would be wrong if the OPEN
1433 * fails for some other reason. furthermore,
1434 * if the size is nonzero, we should ignore it
1435 * according to spec!
1437 *truncp
= (iap
->ia_valid
& ATTR_SIZE
) && !iap
->ia_size
;
1440 iap
->ia_valid
&= ATTR_SIZE
;
1444 case NFS3_CREATE_EXCLUSIVE
:
1445 if ( d_inode(dchild
)->i_mtime
.tv_sec
== v_mtime
1446 && d_inode(dchild
)->i_atime
.tv_sec
== v_atime
1447 && d_inode(dchild
)->i_size
== 0 ) {
1453 case NFS4_CREATE_EXCLUSIVE4_1
:
1454 if ( d_inode(dchild
)->i_mtime
.tv_sec
== v_mtime
1455 && d_inode(dchild
)->i_atime
.tv_sec
== v_atime
1456 && d_inode(dchild
)->i_size
== 0 ) {
1462 case NFS3_CREATE_GUARDED
:
1469 if (!IS_POSIXACL(dirp
))
1470 iap
->ia_mode
&= ~current_umask();
1472 host_err
= vfs_create(&init_user_ns
, dirp
, dchild
, iap
->ia_mode
, true);
1480 nfsd_check_ignore_resizing(iap
);
1482 if (nfsd_create_is_exclusive(createmode
)) {
1483 /* Cram the verifier into atime/mtime */
1484 iap
->ia_valid
= ATTR_MTIME
|ATTR_ATIME
1485 | ATTR_MTIME_SET
|ATTR_ATIME_SET
;
1486 /* XXX someone who knows this better please fix it for nsec */
1487 iap
->ia_mtime
.tv_sec
= v_mtime
;
1488 iap
->ia_atime
.tv_sec
= v_atime
;
1489 iap
->ia_mtime
.tv_nsec
= 0;
1490 iap
->ia_atime
.tv_nsec
= 0;
1494 err
= nfsd_create_setattr(rqstp
, resfhp
, iap
);
1497 * nfsd_create_setattr already committed the child
1498 * (and possibly also the parent).
1501 err
= nfserrno(commit_metadata(fhp
));
1504 * Update the filehandle to get the new inode info.
1507 err
= fh_update(resfhp
);
1511 if (dchild
&& !IS_ERR(dchild
))
1517 err
= nfserrno(host_err
);
1520 #endif /* CONFIG_NFSD_V3 */
1523 * Read a symlink. On entry, *lenp must contain the maximum path length that
1524 * fits into the buffer. On return, it contains the true length.
1525 * N.B. After this call fhp needs an fh_put
1528 nfsd_readlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char *buf
, int *lenp
)
1533 DEFINE_DELAYED_CALL(done
);
1536 err
= fh_verify(rqstp
, fhp
, S_IFLNK
, NFSD_MAY_NOP
);
1540 path
.mnt
= fhp
->fh_export
->ex_path
.mnt
;
1541 path
.dentry
= fhp
->fh_dentry
;
1543 if (unlikely(!d_is_symlink(path
.dentry
)))
1544 return nfserr_inval
;
1548 link
= vfs_get_link(path
.dentry
, &done
);
1550 return nfserrno(PTR_ERR(link
));
1555 memcpy(buf
, link
, *lenp
);
1556 do_delayed_call(&done
);
1561 * Create a symlink and look up its inode
1562 * N.B. After this call _both_ fhp and resfhp need an fh_put
1565 nfsd_symlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
,
1566 char *fname
, int flen
,
1568 struct svc_fh
*resfhp
)
1570 struct dentry
*dentry
, *dnew
;
1575 if (!flen
|| path
[0] == '\0')
1578 if (isdotent(fname
, flen
))
1581 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1585 host_err
= fh_want_write(fhp
);
1590 dentry
= fhp
->fh_dentry
;
1591 dnew
= lookup_one_len(fname
, dentry
, flen
);
1592 host_err
= PTR_ERR(dnew
);
1596 host_err
= vfs_symlink(&init_user_ns
, d_inode(dentry
), dnew
, path
);
1597 err
= nfserrno(host_err
);
1600 err
= nfserrno(commit_metadata(fhp
));
1604 cerr
= fh_compose(resfhp
, fhp
->fh_export
, dnew
, fhp
);
1606 if (err
==0) err
= cerr
;
1611 err
= nfserrno(host_err
);
1617 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1620 nfsd_link(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
,
1621 char *name
, int len
, struct svc_fh
*tfhp
)
1623 struct dentry
*ddir
, *dnew
, *dold
;
1628 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1631 err
= fh_verify(rqstp
, tfhp
, 0, NFSD_MAY_NOP
);
1635 if (d_is_dir(tfhp
->fh_dentry
))
1641 if (isdotent(name
, len
))
1644 host_err
= fh_want_write(tfhp
);
1646 err
= nfserrno(host_err
);
1650 fh_lock_nested(ffhp
, I_MUTEX_PARENT
);
1651 ddir
= ffhp
->fh_dentry
;
1652 dirp
= d_inode(ddir
);
1654 dnew
= lookup_one_len(name
, ddir
, len
);
1655 host_err
= PTR_ERR(dnew
);
1659 dold
= tfhp
->fh_dentry
;
1662 if (d_really_is_negative(dold
))
1664 host_err
= vfs_link(dold
, &init_user_ns
, dirp
, dnew
, NULL
);
1667 err
= nfserrno(commit_metadata(ffhp
));
1669 err
= nfserrno(commit_metadata(tfhp
));
1671 if (host_err
== -EXDEV
&& rqstp
->rq_vers
== 2)
1674 err
= nfserrno(host_err
);
1680 fh_drop_write(tfhp
);
1685 err
= nfserrno(host_err
);
1690 nfsd_close_cached_files(struct dentry
*dentry
)
1692 struct inode
*inode
= d_inode(dentry
);
1694 if (inode
&& S_ISREG(inode
->i_mode
))
1695 nfsd_file_close_inode_sync(inode
);
1699 nfsd_has_cached_files(struct dentry
*dentry
)
1702 struct inode
*inode
= d_inode(dentry
);
1704 if (inode
&& S_ISREG(inode
->i_mode
))
1705 ret
= nfsd_file_is_cached(inode
);
1711 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1714 nfsd_rename(struct svc_rqst
*rqstp
, struct svc_fh
*ffhp
, char *fname
, int flen
,
1715 struct svc_fh
*tfhp
, char *tname
, int tlen
)
1717 struct dentry
*fdentry
, *tdentry
, *odentry
, *ndentry
, *trap
;
1718 struct inode
*fdir
, *tdir
;
1721 bool close_cached
= false;
1723 err
= fh_verify(rqstp
, ffhp
, S_IFDIR
, NFSD_MAY_REMOVE
);
1726 err
= fh_verify(rqstp
, tfhp
, S_IFDIR
, NFSD_MAY_CREATE
);
1730 fdentry
= ffhp
->fh_dentry
;
1731 fdir
= d_inode(fdentry
);
1733 tdentry
= tfhp
->fh_dentry
;
1734 tdir
= d_inode(tdentry
);
1737 if (!flen
|| isdotent(fname
, flen
) || !tlen
|| isdotent(tname
, tlen
))
1741 host_err
= fh_want_write(ffhp
);
1743 err
= nfserrno(host_err
);
1747 /* cannot use fh_lock as we need deadlock protective ordering
1748 * so do it by hand */
1749 trap
= lock_rename(tdentry
, fdentry
);
1750 ffhp
->fh_locked
= tfhp
->fh_locked
= true;
1754 odentry
= lookup_one_len(fname
, fdentry
, flen
);
1755 host_err
= PTR_ERR(odentry
);
1756 if (IS_ERR(odentry
))
1760 if (d_really_is_negative(odentry
))
1763 if (odentry
== trap
)
1766 ndentry
= lookup_one_len(tname
, tdentry
, tlen
);
1767 host_err
= PTR_ERR(ndentry
);
1768 if (IS_ERR(ndentry
))
1770 host_err
= -ENOTEMPTY
;
1771 if (ndentry
== trap
)
1775 if (ffhp
->fh_export
->ex_path
.mnt
!= tfhp
->fh_export
->ex_path
.mnt
)
1777 if (ffhp
->fh_export
->ex_path
.dentry
!= tfhp
->fh_export
->ex_path
.dentry
)
1780 if ((ndentry
->d_sb
->s_export_op
->flags
& EXPORT_OP_CLOSE_BEFORE_UNLINK
) &&
1781 nfsd_has_cached_files(ndentry
)) {
1782 close_cached
= true;
1785 struct renamedata rd
= {
1786 .old_mnt_userns
= &init_user_ns
,
1788 .old_dentry
= odentry
,
1789 .new_mnt_userns
= &init_user_ns
,
1791 .new_dentry
= ndentry
,
1793 host_err
= vfs_rename(&rd
);
1795 host_err
= commit_metadata(tfhp
);
1797 host_err
= commit_metadata(ffhp
);
1805 err
= nfserrno(host_err
);
1807 * We cannot rely on fh_unlock on the two filehandles,
1808 * as that would do the wrong thing if the two directories
1809 * were the same, so again we do it by hand.
1811 if (!close_cached
) {
1812 fill_post_wcc(ffhp
);
1813 fill_post_wcc(tfhp
);
1815 unlock_rename(tdentry
, fdentry
);
1816 ffhp
->fh_locked
= tfhp
->fh_locked
= false;
1817 fh_drop_write(ffhp
);
1820 * If the target dentry has cached open files, then we need to try to
1821 * close them prior to doing the rename. Flushing delayed fput
1822 * shouldn't be done with locks held however, so we delay it until this
1823 * point and then reattempt the whole shebang.
1826 close_cached
= false;
1827 nfsd_close_cached_files(ndentry
);
1836 * Unlink a file or directory
1837 * N.B. After this call fhp needs an fh_put
1840 nfsd_unlink(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, int type
,
1841 char *fname
, int flen
)
1843 struct dentry
*dentry
, *rdentry
;
1845 struct inode
*rinode
;
1850 if (!flen
|| isdotent(fname
, flen
))
1852 err
= fh_verify(rqstp
, fhp
, S_IFDIR
, NFSD_MAY_REMOVE
);
1856 host_err
= fh_want_write(fhp
);
1860 fh_lock_nested(fhp
, I_MUTEX_PARENT
);
1861 dentry
= fhp
->fh_dentry
;
1862 dirp
= d_inode(dentry
);
1864 rdentry
= lookup_one_len(fname
, dentry
, flen
);
1865 host_err
= PTR_ERR(rdentry
);
1866 if (IS_ERR(rdentry
))
1867 goto out_drop_write
;
1869 if (d_really_is_negative(rdentry
)) {
1872 goto out_drop_write
;
1874 rinode
= d_inode(rdentry
);
1878 type
= d_inode(rdentry
)->i_mode
& S_IFMT
;
1880 if (type
!= S_IFDIR
) {
1881 if (rdentry
->d_sb
->s_export_op
->flags
& EXPORT_OP_CLOSE_BEFORE_UNLINK
)
1882 nfsd_close_cached_files(rdentry
);
1883 host_err
= vfs_unlink(&init_user_ns
, dirp
, rdentry
, NULL
);
1885 host_err
= vfs_rmdir(&init_user_ns
, dirp
, rdentry
);
1890 host_err
= commit_metadata(fhp
);
1892 iput(rinode
); /* truncate the inode here */
1897 if (host_err
== -EBUSY
) {
1898 /* name is mounted-on. There is no perfect
1901 if (nfsd_v4client(rqstp
))
1902 err
= nfserr_file_open
;
1906 err
= nfserrno(host_err
);
1913 * We do this buffering because we must not call back into the file
1914 * system's ->lookup() method from the filldir callback. That may well
1915 * deadlock a number of file systems.
1917 * This is based heavily on the implementation of same in XFS.
1919 struct buffered_dirent
{
1923 unsigned int d_type
;
1927 struct readdir_data
{
1928 struct dir_context ctx
;
1934 static int nfsd_buffered_filldir(struct dir_context
*ctx
, const char *name
,
1935 int namlen
, loff_t offset
, u64 ino
,
1936 unsigned int d_type
)
1938 struct readdir_data
*buf
=
1939 container_of(ctx
, struct readdir_data
, ctx
);
1940 struct buffered_dirent
*de
= (void *)(buf
->dirent
+ buf
->used
);
1941 unsigned int reclen
;
1943 reclen
= ALIGN(sizeof(struct buffered_dirent
) + namlen
, sizeof(u64
));
1944 if (buf
->used
+ reclen
> PAGE_SIZE
) {
1949 de
->namlen
= namlen
;
1950 de
->offset
= offset
;
1952 de
->d_type
= d_type
;
1953 memcpy(de
->name
, name
, namlen
);
1954 buf
->used
+= reclen
;
1959 static __be32
nfsd_buffered_readdir(struct file
*file
, struct svc_fh
*fhp
,
1960 nfsd_filldir_t func
, struct readdir_cd
*cdp
,
1963 struct buffered_dirent
*de
;
1967 struct readdir_data buf
= {
1968 .ctx
.actor
= nfsd_buffered_filldir
,
1969 .dirent
= (void *)__get_free_page(GFP_KERNEL
)
1973 return nfserrno(-ENOMEM
);
1978 unsigned int reclen
;
1980 cdp
->err
= nfserr_eof
; /* will be cleared on successful read */
1984 host_err
= iterate_dir(file
, &buf
.ctx
);
1996 de
= (struct buffered_dirent
*)buf
.dirent
;
1998 offset
= de
->offset
;
2000 if (func(cdp
, de
->name
, de
->namlen
, de
->offset
,
2001 de
->ino
, de
->d_type
))
2004 if (cdp
->err
!= nfs_ok
)
2007 trace_nfsd_dirent(fhp
, de
->ino
, de
->name
, de
->namlen
);
2009 reclen
= ALIGN(sizeof(*de
) + de
->namlen
,
2012 de
= (struct buffered_dirent
*)((char *)de
+ reclen
);
2014 if (size
> 0) /* We bailed out early */
2017 offset
= vfs_llseek(file
, 0, SEEK_CUR
);
2020 free_page((unsigned long)(buf
.dirent
));
2023 return nfserrno(host_err
);
2030 * Read entries from a directory.
2031 * The NFSv3/4 verifier we ignore for now.
2034 nfsd_readdir(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, loff_t
*offsetp
,
2035 struct readdir_cd
*cdp
, nfsd_filldir_t func
)
2039 loff_t offset
= *offsetp
;
2040 int may_flags
= NFSD_MAY_READ
;
2042 /* NFSv2 only supports 32 bit cookies */
2043 if (rqstp
->rq_vers
> 2)
2044 may_flags
|= NFSD_MAY_64BIT_COOKIE
;
2046 err
= nfsd_open(rqstp
, fhp
, S_IFDIR
, may_flags
, &file
);
2050 offset
= vfs_llseek(file
, offset
, SEEK_SET
);
2052 err
= nfserrno((int)offset
);
2056 err
= nfsd_buffered_readdir(file
, fhp
, func
, cdp
, offsetp
);
2058 if (err
== nfserr_eof
|| err
== nfserr_toosmall
)
2059 err
= nfs_ok
; /* can still be found in ->err */
2067 * Get file system stats
2068 * N.B. After this call fhp needs an fh_put
2071 nfsd_statfs(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct kstatfs
*stat
, int access
)
2075 err
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_NOP
| access
);
2077 struct path path
= {
2078 .mnt
= fhp
->fh_export
->ex_path
.mnt
,
2079 .dentry
= fhp
->fh_dentry
,
2081 if (vfs_statfs(&path
, stat
))
2087 static int exp_rdonly(struct svc_rqst
*rqstp
, struct svc_export
*exp
)
2089 return nfsexp_flags(rqstp
, exp
) & NFSEXP_READONLY
;
2092 #ifdef CONFIG_NFSD_V4
2094 * Helper function to translate error numbers. In the case of xattr operations,
2095 * some error codes need to be translated outside of the standard translations.
2097 * ENODATA needs to be translated to nfserr_noxattr.
2098 * E2BIG to nfserr_xattr2big.
2100 * Additionally, vfs_listxattr can return -ERANGE. This means that the
2101 * file has too many extended attributes to retrieve inside an
2102 * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
2103 * filesystems will allow the adding of extended attributes until they hit
2104 * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
2105 * So, at that point, the attributes are present and valid, but can't
2106 * be retrieved using listxattr, since the upper level xattr code enforces
2107 * the XATTR_LIST_MAX limit.
2109 * This bug means that we need to deal with listxattr returning -ERANGE. The
2110 * best mapping is to return TOOSMALL.
2113 nfsd_xattr_errno(int err
)
2117 return nfserr_noxattr
;
2119 return nfserr_xattr2big
;
2121 return nfserr_toosmall
;
2123 return nfserrno(err
);
2127 * Retrieve the specified user extended attribute. To avoid always
2128 * having to allocate the maximum size (since we are not getting
2129 * a maximum size from the RPC), do a probe + alloc. Hold a reader
2130 * lock on i_rwsem to prevent the extended attribute from changing
2131 * size while we're doing this.
2134 nfsd_getxattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char *name
,
2135 void **bufp
, int *lenp
)
2140 struct inode
*inode
;
2141 struct dentry
*dentry
;
2143 err
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_READ
);
2148 dentry
= fhp
->fh_dentry
;
2149 inode
= d_inode(dentry
);
2151 inode_lock_shared(inode
);
2153 len
= vfs_getxattr(&init_user_ns
, dentry
, name
, NULL
, 0);
2156 * Zero-length attribute, just return.
2165 err
= nfsd_xattr_errno(len
);
2170 err
= nfserr_toosmall
;
2174 buf
= kvmalloc(len
, GFP_KERNEL
| GFP_NOFS
);
2176 err
= nfserr_jukebox
;
2180 len
= vfs_getxattr(&init_user_ns
, dentry
, name
, buf
, len
);
2184 err
= nfsd_xattr_errno(len
);
2191 inode_unlock_shared(inode
);
2197 * Retrieve the xattr names. Since we can't know how many are
2198 * user extended attributes, we must get all attributes here,
2199 * and have the XDR encode filter out the "user." ones.
2201 * While this could always just allocate an XATTR_LIST_MAX
2202 * buffer, that's a waste, so do a probe + allocate. To
2203 * avoid any changes between the probe and allocate, wrap
2204 * this in inode_lock.
2207 nfsd_listxattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char **bufp
,
2213 struct inode
*inode
;
2214 struct dentry
*dentry
;
2216 err
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_READ
);
2220 dentry
= fhp
->fh_dentry
;
2221 inode
= d_inode(dentry
);
2224 inode_lock_shared(inode
);
2226 len
= vfs_listxattr(dentry
, NULL
, 0);
2228 err
= nfsd_xattr_errno(len
);
2232 if (len
> XATTR_LIST_MAX
) {
2233 err
= nfserr_xattr2big
;
2238 * We're holding i_rwsem - use GFP_NOFS.
2240 buf
= kvmalloc(len
, GFP_KERNEL
| GFP_NOFS
);
2242 err
= nfserr_jukebox
;
2246 len
= vfs_listxattr(dentry
, buf
, len
);
2249 err
= nfsd_xattr_errno(len
);
2258 inode_unlock_shared(inode
);
2264 * Removexattr and setxattr need to call fh_lock to both lock the inode
2265 * and set the change attribute. Since the top-level vfs_removexattr
2266 * and vfs_setxattr calls already do their own inode_lock calls, call
2267 * the _locked variant. Pass in a NULL pointer for delegated_inode,
2268 * and let the client deal with NFS4ERR_DELAY (same as with e.g.
2269 * setattr and remove).
2272 nfsd_removexattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char *name
)
2277 err
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_WRITE
);
2281 ret
= fh_want_write(fhp
);
2283 return nfserrno(ret
);
2287 ret
= __vfs_removexattr_locked(&init_user_ns
, fhp
->fh_dentry
,
2293 return nfsd_xattr_errno(ret
);
2297 nfsd_setxattr(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, char *name
,
2298 void *buf
, u32 len
, u32 flags
)
2303 err
= fh_verify(rqstp
, fhp
, 0, NFSD_MAY_WRITE
);
2307 ret
= fh_want_write(fhp
);
2309 return nfserrno(ret
);
2312 ret
= __vfs_setxattr_locked(&init_user_ns
, fhp
->fh_dentry
, name
, buf
,
2318 return nfsd_xattr_errno(ret
);
2323 * Check for a user's access permissions to this inode.
2326 nfsd_permission(struct svc_rqst
*rqstp
, struct svc_export
*exp
,
2327 struct dentry
*dentry
, int acc
)
2329 struct inode
*inode
= d_inode(dentry
);
2332 if ((acc
& NFSD_MAY_MASK
) == NFSD_MAY_NOP
)
2335 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2337 (acc
& NFSD_MAY_READ
)? " read" : "",
2338 (acc
& NFSD_MAY_WRITE
)? " write" : "",
2339 (acc
& NFSD_MAY_EXEC
)? " exec" : "",
2340 (acc
& NFSD_MAY_SATTR
)? " sattr" : "",
2341 (acc
& NFSD_MAY_TRUNC
)? " trunc" : "",
2342 (acc
& NFSD_MAY_LOCK
)? " lock" : "",
2343 (acc
& NFSD_MAY_OWNER_OVERRIDE
)? " owneroverride" : "",
2345 IS_IMMUTABLE(inode
)? " immut" : "",
2346 IS_APPEND(inode
)? " append" : "",
2347 __mnt_is_readonly(exp
->ex_path
.mnt
)? " ro" : "");
2348 dprintk(" owner %d/%d user %d/%d\n",
2349 inode
->i_uid
, inode
->i_gid
, current_fsuid(), current_fsgid());
2352 /* Normally we reject any write/sattr etc access on a read-only file
2353 * system. But if it is IRIX doing check on write-access for a
2354 * device special file, we ignore rofs.
2356 if (!(acc
& NFSD_MAY_LOCAL_ACCESS
))
2357 if (acc
& (NFSD_MAY_WRITE
| NFSD_MAY_SATTR
| NFSD_MAY_TRUNC
)) {
2358 if (exp_rdonly(rqstp
, exp
) ||
2359 __mnt_is_readonly(exp
->ex_path
.mnt
))
2361 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode
))
2364 if ((acc
& NFSD_MAY_TRUNC
) && IS_APPEND(inode
))
2367 if (acc
& NFSD_MAY_LOCK
) {
2368 /* If we cannot rely on authentication in NLM requests,
2369 * just allow locks, otherwise require read permission, or
2372 if (exp
->ex_flags
& NFSEXP_NOAUTHNLM
)
2375 acc
= NFSD_MAY_READ
| NFSD_MAY_OWNER_OVERRIDE
;
2378 * The file owner always gets access permission for accesses that
2379 * would normally be checked at open time. This is to make
2380 * file access work even when the client has done a fchmod(fd, 0).
2382 * However, `cp foo bar' should fail nevertheless when bar is
2383 * readonly. A sensible way to do this might be to reject all
2384 * attempts to truncate a read-only file, because a creat() call
2385 * always implies file truncation.
2386 * ... but this isn't really fair. A process may reasonably call
2387 * ftruncate on an open file descriptor on a file with perm 000.
2388 * We must trust the client to do permission checking - using "ACCESS"
2391 if ((acc
& NFSD_MAY_OWNER_OVERRIDE
) &&
2392 uid_eq(inode
->i_uid
, current_fsuid()))
2395 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2396 err
= inode_permission(&init_user_ns
, inode
,
2397 acc
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
));
2399 /* Allow read access to binaries even when mode 111 */
2400 if (err
== -EACCES
&& S_ISREG(inode
->i_mode
) &&
2401 (acc
== (NFSD_MAY_READ
| NFSD_MAY_OWNER_OVERRIDE
) ||
2402 acc
== (NFSD_MAY_READ
| NFSD_MAY_READ_IF_EXEC
)))
2403 err
= inode_permission(&init_user_ns
, inode
, MAY_EXEC
);
2405 return err
? nfserrno(err
) : 0;