Avoid returning NFS4ERR_FILE_OPEN when not appropriate
Subsystems (2)
[email protected]
maintainerv3 cover Avoid returning NFS4ERR_FILE_OPEN when not appropriate
From: Chuck Lever <[email protected]>
This short series aims to prevent NFSD from returning NFS4ERR_FILE_OPEN when an NFSv4 LINK, RENAME, or REMOVE operation targets a directory. The only time the protocol spec permits a server to return FILE_OPEN is when the target of the operation is an object that is open and cannot be closed immediately to satisfy the request.
I would have preferred these fixes go into NFSv4-specific sections of NFSD, but the current structure of the code prevents doing that while maintaining operational efficiency. Plus, these small patches should be able to apply cleanly to LTS kernels.
We can defer deeper restructuring for later. For example, fh_verify() could be made to return an errno instead of a generic NFS status code; then the VFS utility functions in fs/nfsd/vfs.c could be made to do the same, making their callers responsible for the proper NFS version-specific translation of the errno into a status code.
This series has passed git regression and xfstests. I'm interested in review and comment about this approach, but please do test these if you have the ability to trigger -EBUSY easily.
NFSv4 OPEN is also affected, but because of its complexity will require careful audit (ie, a separate patch set). Please send a copy of the output of WARN_ONCE so we can see where to start digging in that area.
Changes since v2:
- Fix crash when renaming to a non-existent object
Changes since RFC:
- Address a minor code odor
- Clarify some code comments
Chuck Lever (4):
NFSD: nfsd_unlink() clobbers non-zero status returned from
fh_fill_pre_attrs()
NFSD: Never return NFS4ERR_FILE_OPEN when removing a directory
NFSD: Return NFS4ERR_FILE_OPEN only when renaming over an open file
NFSD: Return NFS4ERR_FILE_OPEN only when linking an open file
fs/nfsd/vfs.c | 105 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 26 deletions(-)
Signature · 1 line
2.47.0
[email protected]
maintainerPATCH v3 1/4 NFSD: nfsd_unlink() clobbers non-zero status returned from fh_fill_pre_attrs()
From: Chuck Lever <[email protected]>
If fh_fill_pre_attrs() returns a non-zero status, the error flow takes it through out_unlock, which then overwrites the returned status code with
err = nfserrno(host_err);
--- fs/nfsd/vfs.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
fs/nfsd/vfs.c +1 -3
@@ -2011,11 +2011,9 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,* error status. */ err = nfserr_file_open; - }else { - err =nfserrno (host_err); } out: -return err; +return err != nfs_ok ? err :nfserrno (host_err); out_unlock:inode_unlock (dirp); goto out_drop_write;
Signature · 1 line
2.47.0
[email protected]
maintainerPATCH v3 2/4 NFSD: Never return NFS4ERR_FILE_OPEN when removing a directory
In reply to [email protected]From: Chuck Lever <[email protected]>
RFC 8881 Section 18.25.4 paragraph 5 tells us that the server should return NFS4ERR_FILE_OPEN only if the target object is an opened file. This suggests that returning this status when removing a directory will confuse NFS clients.
This is a version-specific issue; nfsd_proc_remove/rmdir() and nfsd3_proc_remove/rmdir() already return nfserr_access as appropriate.
Unfortunately there is no quick way for nfsd4_remove() to determine whether the target object is a file or not, so the check is done in in nfsd_unlink() for now.
--- fs/nfsd/vfs.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
fs/nfsd/vfs.c +18 -6
@@ -1931,9 +1931,17 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,return err; } -/* -* Unlink a file or directory -* N.B. After this call fhp needs an fh_put +/** +* nfsd_unlink - remove a directory entry +* @rqstp: RPC transaction context +* @fhp: the file handle of the parent directory to be modified +* @type: enforced file type of the object to be removed +* @fname: the name of directory entry to be removed +* @flen: length of @fname in octets +* +* After this call fhp needs an fh_put. +* +* Returns a generic NFS status code in network byte-order. */ __be32 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
@@ -2007,10 +2015,14 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,fh_drop_write (fhp); out_nfserr:if (host_err == -EBUSY ) { -/* name is mounted-on. There is no perfect -* error status. +/* +* See RFC 8881 Section 18.25.4 para 4: NFSv4 REMOVE +* wants a status unique to the object type. */ - err = nfserr_file_open; +if (type !=S_IFDIR ) + err = nfserr_file_open; +else + err = nfserr_acces; } out:return err != nfs_ok ? err :nfserrno (host_err);
Signature · 1 line
2.47.0
[email protected]
maintainerPATCH v3 3/4 NFSD: Return NFS4ERR_FILE_OPEN only when renaming over an open file
In reply to [email protected]From: Chuck Lever <[email protected]>
RFC 8881 Section 18.26.4 paragraphs 1 - 3 tell us that RENAME should return NFS4ERR_FILE_OPEN only when the target object is a file that is currently open. If the target is a directory, some other status must be returned.
Generally I expect that a delegation recall will be triggered in some of these circumstances. In other cases, the VFS might return -EBUSY for other reasons, and NFSD has to ensure that errno does not leak to clients as a status code that is not permitted by spec.
There are some error flows where the target dentry hasn't been found yet. The default value for @type therefore is S_IFDIR to return an alternate status code in those cases.
--- fs/nfsd/vfs.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)
fs/nfsd/vfs.c +29 -4
@@ -1795,9 +1795,19 @@ nfsd_has_cached_files(struct dentry *dentry)return ret; } -/* -* Rename a file -* N.B. After this call _both_ ffhp and tfhp need an fh_put +/** +* nfsd_rename - rename a directory entry +* @rqstp: RPC transaction context +* @ffhp: the file handle of parent directory containing the entry to be renamed +* @fname: the filename of directory entry to be renamed +* @flen: the length of @fname in octets +* @tfhp: the file handle of parent directory to contain the renamed entry +* @tname: the filename of the new entry +* @tlen: the length of @tlen in octets +* +* After this call _both_ ffhp and tfhp need an fh_put. +* +* Returns a generic NFS status code in network byte-order. */ __be32 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
@@ -1805,6 +1815,7 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, {struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;struct inode *fdir, *tdir; +int type =S_IFDIR ;__be32 err;int host_err;bool close_cached = false;
@@ -1862,11 +1873,14 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, host_err = -EINVAL ;if (odentry == trap) goto out_dput_old; + type =d_inode (odentry)->i_mode &S_IFMT ; ndentry =lookup_one_len (tname, tdentry, tlen); host_err =PTR_ERR (ndentry);if (IS_ERR (ndentry)) goto out_dput_old; +if (d_inode (ndentry)) + type =d_inode (ndentry)->i_mode &S_IFMT ; host_err = -ENOTEMPTY ;if (ndentry == trap) goto out_dput_new;
@@ -1904,7 +1918,18 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, out_dput_old:dput (odentry); out_nfserr: - err =nfserrno (host_err); +if (host_err == -EBUSY ) { +/* +* See RFC 8881 Section 18.26.4 para 1-3: NFSv4 RENAME +* wants a status unique to the object type. +*/ +if (type !=S_IFDIR ) + err = nfserr_file_open; +else + err = nfserr_acces; + }else { + err =nfserrno (host_err); + }if (!close_cached) {fh_fill_post_attrs (ffhp);
Signature · 1 line
2.47.0
[email protected]
maintainerPATCH v3 4/4 NFSD: Return NFS4ERR_FILE_OPEN only when linking an open file
In reply to [email protected]From: Chuck Lever <[email protected]>
RFC 8881 Section 18.9.4 paragraphs 1 - 2 tell us that RENAME should return NFS4ERR_FILE_OPEN only when the target object is a file that is currently open. If the target is a directory, some other status must be returned.
The VFS is unlikely to return -EBUSY, but NFSD has to ensure that errno does not leak to clients as a status code that is not permitted by spec.
--- fs/nfsd/vfs.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-)
fs/nfsd/vfs.c +31 -13
@@ -1699,9 +1699,17 @@ nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,return err; } -/* -* Create a hardlink -* N.B. After this call _both_ ffhp and tfhp need an fh_put +/** +* nfsd_link - create a link +* @rqstp: RPC transaction context +* @ffhp: the file handle of the directory where the new link is to be created +* @name: the filename of the new link +* @len: the length of @name in octets +* @tfhp: the file handle of an existing file object +* +* After this call _both_ ffhp and tfhp need an fh_put. +* +* Returns a generic NFS status code in network byte-order. */ __be32 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
@@ -1709,6 +1717,7 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, {struct dentry *ddir, *dnew, *dold;struct inode *dirp; +int type;__be32 err;int host_err;
@@ -1728,11 +1737,11 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,if (isdotent (name, len)) goto out; + err = nfs_ok; + type =d_inode (tfhp->fh_dentry)->i_mode &S_IFMT ; host_err =fh_want_write (tfhp); -if (host_err) { - err =nfserrno (host_err); +if (host_err) goto out; - } ddir = ffhp->fh_dentry; dirp =d_inode (ddir);
@@ -1740,7 +1749,7 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, dnew =lookup_one_len (name, ddir, len);if (IS_ERR (dnew)) { - err =nfserrno (PTR_ERR (dnew)); + host_err =PTR_ERR (dnew); goto out_unlock; }
@@ -1756,17 +1765,26 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,fh_fill_post_attrs (ffhp);inode_unlock (dirp);if (!host_err) { - err =nfserrno (commit_metadata (ffhp)); -if (!err) - err =nfserrno (commit_metadata (tfhp)); - }else { - err =nfserrno (host_err); + host_err =commit_metadata (ffhp); +if (!host_err) + host_err =commit_metadata (tfhp); } +dput (dnew); out_drop_write:fh_drop_write (tfhp); +if (host_err == -EBUSY ) { +/* +* See RFC 8881 Section 18.9.4 para 1-2: NFSv4 LINK +* wants a status unique to the object type. +*/ +if (type !=S_IFDIR ) + err = nfserr_file_open; +else + err = nfserr_acces; + } out: -return err; +return err != nfs_ok ? err :nfserrno (host_err); out_dput:dput (dnew);
Signature · 1 line
2.47.0
Quoted 39 lines · On Sun, 2025-01-26 at 16:50 -0500, [email protected] wrote:
From: Chuck Lever <[email protected]>
This short series aims to prevent NFSD from returning
NFS4ERR_FILE_OPENwhen an NFSv4 LINK, RENAME, or REMOVE operation targets a directory. The only time the protocol spec permits a server to returnFILE_OPENis when the target of the operation is an object that is open and cannot be closed immediately to satisfy the request.I would have preferred these fixes go into NFSv4-specific sections of NFSD, but the current structure of the code prevents doing that while maintaining operational efficiency. Plus, these small patches should be able to apply cleanly to LTS kernels.
We can defer deeper restructuring for later. For example,
fh_verify()could be made to return an errno instead of a generic NFS status code; then the VFS utility functions infs/nfsd/vfs.ccould be made to do the same, making their callers responsible for the proper NFS version-specific translation of the errno into a status code.This series has passed git regression and xfstests. I'm interested in review and comment about this approach, but please do test these if you have the ability to trigger
-EBUSYeasily.NFSv4 OPEN is also affected, but because of its complexity will require careful audit (ie, a separate patch set). Please send a copy of the output of
WARN_ONCEso we can see where to start digging in that area.Changes since v2:
- Fix crash when renaming to a non-existent objectChanges since RFC:
- Address a minor code odor
- Clarify some code commentsChuck Lever (4):
NFSD:nfsd_unlink()clobbers non-zero status returned from
Quoted 8 lines
fh_fill_pre_attrs()NFSD: Never return
NFS4ERR_FILE_OPENwhen removing a directory
NFSD: ReturnNFS4ERR_FILE_OPENonly when renaming over an open file
NFSD: ReturnNFS4ERR_FILE_OPENonly when linking an open filefs/nfsd/vfs.c | 105 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 26 deletions(-)
This all looks pretty reasonable to me.