nfsd: fix file change detection in CB_GETATTR
Subsystems (3)
Chuck Lever
maintainerPATCH 1/2 nfsd: fix file change detection in CB_GETATTR
From: Scott Mayhew <[email protected]>
commit 304d81a2fbf2b454def4debcb38ea173911b72cd upstream.
RFC 8881, section 10.4.3 doesn't say anything about caching the file size in the delegation record, nor does it say anything about comparing a cached file size with the size reported by the client in the CB_GETATTR reply for the purpose of determining if the client holds modified data for the file.
What section 10.4.3 of RFC 8881 does say is that the server should compare the *current* file size with the size reported by the client holding the delegation in the CB_GETATTR reply, and if they differ to treat it as a modification regardless of the change attribute retrieved via the CB_GETATTR.
Doing otherwise would cause the server to believe the client holding the delegation has a modified version of the file, even if the client flushed the modifications to the server prior to the CB_GETATTR. This would have the added side effect of subsequent CB_GETATTRs causing updates to the mtime, ctime, and change attribute even if the client holding the delegation makes no further updates to the file.
Modify nfsd4_deleg_getattr_conflict() to obtain the current file size via i_size_read(). Retain the ncf_cur_fsize field, since it's a convenient way to return the file size back to nfsd4_encode_fattr4(), but don't use it for the purpose of detecting file changes. Remove the unnecessary initialization of ncf_cur_fsize in nfs4_open_delegation().
Also, if we recall the delegation (because the client didn't respond to the CB_GETATTR), then skip the logic that checks the nfs4_cb_fattr fields.
--- fs/nfsd/nfs4state.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
fs/nfsd/nfs4state.c +8 -5
@@ -6085,7 +6085,6 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, goto out_no_deleg; } open->op_delegate_type =NFS4_OPEN_DELEGATE_WRITE ; - dp->dl_cb_fattr.ncf_cur_fsize = stat.size; dp->dl_cb_fattr.ncf_initial_cinfo =nfsd4_change_attribute (&stat);trace_nfsd_deleg_write (&dp->dl_stid.sc_stateid); }else {
@@ -9039,11 +9038,15 @@ nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,if (status != nfserr_jukebox || !nfsd_wait_for_delegreturn (rqstp, inode)) goto out_status; + status = nfs_ok; + goto out_status; + } +if (!ncf->ncf_file_modified) { +if (ncf->ncf_initial_cinfo != ncf->ncf_cb_change) + ncf->ncf_file_modified = true; +else if (i_size_read (inode) != ncf->ncf_cb_fsize) + ncf->ncf_file_modified = true; } -if (!ncf->ncf_file_modified && - (ncf->ncf_initial_cinfo != ncf->ncf_cb_change || - ncf->ncf_cur_fsize != ncf->ncf_cb_fsize)) - ncf->ncf_file_modified = true;if (ncf->ncf_file_modified) {int err;
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH 2/2 nfsd: release layout stid on setlease failure
commit 30d55c8aabb261bc3f427d6b9aae7ef6206063f9 upstream.
nfs4_alloc_stid() publishes the new stid into cl->cl_stateids via idr_alloc_cyclic() under cl_lock before returning to nfsd4_alloc_layout_stateid(). When nfsd4_layout_setlease() then fails, the error path frees the layout stateid directly with kmem_cache_free() without ever calling idr_remove(), leaving the IDR slot pointing at freed slab memory. Any subsequent IDR walker (states_show, client teardown) dereferences the dangling pointer.
The correct teardown for an IDR-published stid is nfs4_put_stid(), which removes the IDR slot under cl_lock, dispatches sc_free (nfsd4_free_layout_stateid) to release ls->ls_file via nfsd4_close_layout(), and drops the nfs4_file reference in its tail.
Replace the manual nfsd_file_put + put_nfs4_file + kmem_cache_free cleanup with a single nfs4_put_stid(stp).
--- fs/nfsd/nfs4layouts.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
fs/nfsd/nfs4layouts.c +1 -3
@@ -256,9 +256,7 @@ nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,BUG_ON (!ls->ls_file);if (nfsd4_layout_setlease (ls)) { -nfsd_file_put (ls->ls_file); -put_nfs4_file (fp); -kmem_cache_free (nfs4_layout_stateid_cache, ls); +nfs4_put_stid (stp);return NULL ; }
Signature · 1 line
2.54.0
Chuck Lever
maintainerRe: [PATCH 6.12.y 2/2] nfsd: release layout stid on setlease failure
Please note: The correct Author for this patch is "Chris Mason <[email protected]>"
Quoted 49 lines · Chuck Lever
commit
30d55c8aabb261bc3f427d6b9aae7ef6206063f9upstream.
nfs4_alloc_stid()publishes the new stid intocl->cl_stateidsviaidr_alloc_cyclic()undercl_lockbefore returning tonfsd4_alloc_layout_stateid(). Whennfsd4_layout_setlease()then fails, the error path frees the layout stateid directly withkmem_cache_free()without ever callingidr_remove(), leaving the IDR slot pointing at freed slab memory. Any subsequent IDR walker (states_show, client teardown) dereferences the dangling pointer.The correct teardown for an IDR-published stid is
nfs4_put_stid(), which removes the IDR slot undercl_lock, dispatchessc_free(nfsd4_free_layout_stateid) to releasels->ls_filevianfsd4_close_layout(), and drops thenfs4_filereference in its tail.Replace the manual
nfsd_file_put+put_nfs4_file+kmem_cache_freecleanup with a singlenfs4_put_stid(stp).Fixes c5c707f96fc9 ("nfsd: implement pNFS layout recalls")Signed-off-by Chris Mason <[email protected]>Reviewed-by Jeff Layton <[email protected]>Signed-off-by Chuck Lever <[email protected]>Signed-off-by Chuck Lever <[email protected]>[ cel: no ls_fence_work in 6.12.y; dropped INIT_DELAYED_WORK hunk ]Signed-off-by Chuck Lever <[email protected]>--- fs/nfsd/nfs4layouts.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)fs/nfsd/nfs4layouts.c +1
-3@@ -256,9 +256,7 @@ nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,BUG_ON (!ls->ls_file);if (nfsd4_layout_setlease (ls)) { -nfsd_file_put (ls->ls_file); -put_nfs4_file (fp); -kmem_cache_free (nfs4_layout_stateid_cache, ls); +nfs4_put_stid (stp);return NULL ; }Signature · 2 lines
-- 2.54.0
Signature · 1 line
Chuck Lever
Quoted 1 line · Chuck Lever
commit
304d81a2fbf2b454def4debcb38ea173911b72cdupstream.
Queued the series for 6.12.y, with the author of 2/2 fixed up to Chris Mason per your follow-up. Thanks!
Signature · 2 lines
Thanks, Sasha