cifs: follow-on fixes after fscache_resize_cookie() consolidation
Subsystems (3)
v3 cover cifs: follow-on fixes after fscache_resize_cookie() consolidation
After commit fa724e235cfd ("cifs: add fscache_resize_cookie() to cifs_setsize()") consolidated fscache cookie resizing into cifs_setsize(), two further fixes are needed and two dead-code blocks became removable.
Patch 1 fixes cifs_do_truncate() (O_TRUNC via cifs_open()). That path runs without i_rwsem, so it cannot use the locked resize path. cifs_invalidate_cache() is the correct alternative: it works without the lock and ensures stale cached data is not served once the cookie is later activated.
Patch 2 addresses a race identified during review: cifs_setsize() calls fscache_resize_cookie() without i_rwsem, but another concurrent open may already have the cookie active (IS_CACHING set), making fscache_resize_cookie() a real operation requiring the lock. The fix strips fscache_resize_cookie() from cifs_setsize() and introduces cifs_resize_file_locked(), which temporarily activates the cookie with fscache_use_cookie(), performs the resize under i_rwsem, then deactivates it. Callers that already hold i_rwsem (cifs_file_set_size, smb2_duplicate_extents, smb3_simple_falloc) switch to the wrapper; cifs_do_truncate() continues to use cifs_setsize() followed by cifs_invalidate_cache() as established in patch 1.
Patches 3 and 4 remove dead code. Patch 3 removes a caller-side truncate_setsize() + fscache_resize_cookie() block from cifs_remap_file_range() that became redundant once smb2_duplicate_extents() started performing the full size update via cifs_setsize() under the i_rwsem held by lock_two_nondirectories(). Patch 4 removes equivalent dead blocks from cifs_setattr_unix() and cifs_setattr_nounix(): since cifs_file_set_size() calls cifs_setsize() on success, i_size always equals attrs->ia_size on the success path, making the subsequent size-inequality blocks unreachable.
v3: Fixed broken patch
v2:
- Added patch 2 (cifs_resize_file_locked): based on review feedback (Huiwen He) that the fscache cookie is not guaranteed to be quiescent in cifs_do_truncate() - another concurrent open may already have it active. The fix strips fscache_resize_cookie() from cifs_setsize() and adds a locked wrapper for callers that hold i_rwsem.
- Patch 1 commit message updated: the original rationale (cookie is always quiescent in cifs_do_truncate) was incorrect; the correct reason is that i_rwsem cannot be held in that path.
- Patches 2-3 from v1 renumbered to 3-4 to place fixes before cleanups.
- Patches 3-4: add Reviewed-by from Huiwen He; otherwise unchanged from v1.
- All patches: add Reviewed-by from Paulo Alcantara.
Frank Sorenson (4):
cifs: use cifs_invalidate_cache() in cifs_do_truncate() for O_TRUNC
cifs: add cifs_resize_file_locked() to guard fscache_resize_cookie()
under i_rwsem
cifs: remove redundant size-update block in cifs_remap_file_range()
cifs: remove dead size-update blocks in cifs_setattr_unix/nounix
fs/smb/client/cifsfs.c | 6 +----- fs/smb/client/cifsfs.h | 1 + fs/smb/client/file.c | 1 + fs/smb/client/inode.c | 41 ++++++++++++++++++++++------------------- fs/smb/client/smb2ops.c | 9 +++------ 5 files changed, 28 insertions(+), 30 deletions(-)
Signature · 1 line
2.55.0
PATCH v3 1/4 cifs: use cifs_invalidate_cache() in cifs_do_truncate() for O_TRUNC
cifs_do_truncate() is invoked from cifs_open() without i_rwsem, so it cannot use cifs_resize_file_locked() to perform a proper fscache cookie resize. Instead, add cifs_invalidate_cache() after cifs_setsize().
cifs_invalidate_cache() calls fscache_invalidate(), which works without holding i_rwsem: it unconditionally increments inval_counter and sets FSCACHE_COOKIE_NO_DATA_TO_READ, ensuring that stale cached data is not served once the cookie is later activated by fscache_use_cookie(). Truncation to zero leaves no valid cached data, making invalidation the correct semantic here.
--- fs/smb/client/file.c | 1 + 1 file changed, 1 insertion(+)
fs/smb/client/file.c +1 -0
@@ -1016,6 +1016,7 @@ static int cifs_do_truncate(const unsigned int xid, struct dentry *dentry)if (!rc) {netfs_resize_file (&cinode->netfs,0 , true);cifs_setsize (inode,0 ); +cifs_invalidate_cache (inode,0 ); } }if (cfile)
Signature · 1 line
2.55.0
PATCH v3 2/4 cifs: add cifs_resize_file_locked() to guard fscache_resize_cookie() under i_rwsem
In reply to Frank Sorensoncifs_setsize() calls fscache_resize_cookie() without holding i_rwsem. When the fscache cookie is active (FSCACHE_COOKIE_IS_CACHING is set), fscache_resize_cookie() performs a real resize that requires i_rwsem held exclusively. If another file descriptor has the same inode open, fscache_use_cookie() was already called from that cifs_open(), making the cookie active. In that case, calling cifs_setsize() from cifs_do_truncate() (invoked from cifs_open() without i_rwsem) races against concurrent fscache I/O.
Strip fscache_resize_cookie() from cifs_setsize(), making it a pure size/page-cache helper. Add cifs_resize_file_locked() for callers that already hold i_rwsem: it calls netfs_resize_file() and cifs_setsize(), then temporarily activates the cookie with fscache_use_cookie() to perform the resize under the lock, then deactivates it with cifs_fscache_unuse_inode_cookie(). Using fscache_use_cookie() before the resize ensures correctness whether or not another fd already holds the cookie active.
Switch cifs_file_set_size(), smb2_duplicate_extents(), and both size- extension branches of smb3_simple_falloc() to the new wrapper; those paths already hold i_rwsem via VFS setattr, lock_two_nondirectories(), or cifs_fallocate() respectively. cifs_do_truncate() continues to call cifs_setsize() followed by cifs_invalidate_cache(), since it runs without i_rwsem.
--- fs/smb/client/cifsfs.h | 1 + fs/smb/client/inode.c | 24 +++++++++++++++++++----- fs/smb/client/smb2ops.c | 9 +++------ 3 files changed, 23 insertions(+), 11 deletions(-)
fs/smb/client/cifsfs.h +1 -0
@@ -147,6 +147,7 @@ ssize_t cifs_file_copychunk_range(unsigned int xid, struct file *src_file,long cifs_ioctl (struct file *filep,unsigned int command,unsigned long arg);void cifs_setsize (struct inode *inode,loff_t offset); +void cifs_resize_file_locked (struct inode *inode,loff_t offset);struct fs_context ;struct smb3_fs_context ;
fs/smb/client/inode.c +19 -5
@@ -3059,7 +3059,23 @@ void cifs_setsize(struct inode *inode, loff_t offset)inode_set_mtime_to_ts (inode,inode_set_ctime_current (inode));truncate_pagecache (inode, offset);netfs_wait_for_outstanding_io (inode); -fscache_resize_cookie (cifs_inode_cookie (inode), offset); +} + +void cifs_resize_file_locked (struct inode *inode,loff_t offset) +{ +struct fscache_cookie *cookie =cifs_inode_cookie (inode); + +lockdep_assert_held_write (&inode->i_rwsem); + +netfs_resize_file (netfs_inode (inode), offset, true); +cifs_setsize (inode, offset); + +if (!cookie) +return ; + +fscache_use_cookie (cookie, true); +fscache_resize_cookie (cookie, offset); +cifs_fscache_unuse_inode_cookie (inode, true); } int cifs_file_set_size(const unsigned int xid, struct dentry *dentry,
@@ -3125,10 +3144,8 @@ int cifs_file_set_size(const unsigned int xid, struct dentry *dentry,cifs_put_tlink (tlink); set_size_out: -if (rc ==0 ) { -netfs_resize_file (&cifsInode->netfs, size, true); -cifs_setsize (inode, size); - } +if (rc ==0 ) +cifs_resize_file_locked (inode, size);return rc; }
fs/smb/client/smb2ops.c +3 -6
@@ -2222,8 +2222,7 @@ smb2_duplicate_extents(const unsigned int xid, rc =smb2_set_file_size (xid, tcon, trgtfile, dest_off + len, false);if (rc) goto duplicate_extents_out; -netfs_resize_file (netfs_inode (inode), dest_off + len, true); -cifs_setsize (inode, dest_off + len); +cifs_resize_file_locked (inode, dest_off + len); } rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid, trgtfile->fid.volatile_fid,
@@ -3776,8 +3775,7 @@ static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon, } new_eof = off + len; -netfs_resize_file (&cifsi->netfs, new_eof, true); -cifs_setsize (inode, new_eof); +cifs_resize_file_locked (inode, new_eof); qrc = SMB2_query_info(xid, tcon, cfile->fid.persistent_fid,
@@ -3825,8 +3823,7 @@ static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,if (rc) goto out; -netfs_resize_file (&cifsi->netfs, new_eof, true); -cifs_setsize (inode, new_eof); +cifs_resize_file_locked (inode, new_eof); qrc = SMB2_query_info(xid, tcon, cfile->fid.persistent_fid,
Signature · 1 line
2.55.0
PATCH v3 3/4 cifs: remove redundant size-update block in cifs_remap_file_range()
In reply to Frank Sorensoncifs_remap_file_range() acquires i_rwsem on both inodes via lock_two_nondirectories() before calling smb2_duplicate_extents(). cifs_setsize() (called inside smb2_duplicate_extents() when the clone extends the file) therefore already runs under the lock, meaning the fscache_resize_cookie() added to cifs_setsize() by commit fa724e235cfd ("cifs: add fscache_resize_cookie() to cifs_setsize()") is correctly serialised for this path without further changes.
That same commit made the caller-side block:
if (rc == 0 && new_size > i_size) {
truncate_setsize(target_inode, new_size);
fscache_resize_cookie(cifs_inode_cookie(target_inode), new_size);
}redundant: smb2_duplicate_extents() already performs the full size update via cifs_setsize() when the operation extends the file. Remove the now-dead block.
--- fs/smb/client/cifsfs.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
fs/smb/client/cifsfs.c +1 -5
@@ -1464,11 +1464,7 @@ static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,if (target_tcon->ses->server->ops->duplicate_extents) { rc = target_tcon->ses->server->ops->duplicate_extents (xid, smb_file_src, smb_file_target, off, len, destoff); -if (rc ==0 && new_size > i_size) { -truncate_setsize (target_inode, new_size); -fscache_resize_cookie (cifs_inode_cookie (target_inode), - new_size); - }else if (rc == -EOPNOTSUPP ) { +if (rc == -EOPNOTSUPP ) {/* * copy_file_range syscall man page indicates EINVAL * is returned e.g when "fd_in and fd_out refer to the
Signature · 1 line
2.55.0
PATCH v3 4/4 cifs: remove dead size-update blocks in cifs_setattr_unix/nounix
In reply to Frank SorensonCommit 110fee6b9bb5 ("smb: client: fix missing timestamp updates with O_TRUNC") introduced cifs_file_set_size(), which calls netfs_resize_file() and cifs_setsize() on success. cifs_setsize() calls i_size_write(), updating i_size to the new value. The subsequent blocks in both cifs_setattr_unix() and cifs_setattr_nounix():
if ((attrs->ia_valid & ATTR_SIZE) &&
attrs->ia_size != i_size_read(inode)) {
truncate_setsize(inode, attrs->ia_size);
netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
}are therefore unreachable on the success path: attrs->ia_size == i_size_read(inode) always holds after cifs_file_set_size() succeeds. On the failure path, execution jumps to out/cifs_setattr_exit before reaching these blocks.
truncate_setsize() and netfs_resize_file() are redundant with what cifs_file_set_size() already did; fscache_resize_cookie() was moved there by commit fa724e235cfd ("cifs: add fscache_resize_cookie() to cifs_setsize()"). Remove both dead blocks.
--- fs/smb/client/inode.c | 14 -------------- 1 file changed, 14 deletions(-)
fs/smb/client/inode.c +0 -14
@@ -3306,13 +3306,6 @@ cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)if (rc) goto out; -if ((attrs->ia_valid &ATTR_SIZE ) && - attrs->ia_size !=i_size_read (inode)) { -truncate_setsize (inode, attrs->ia_size); -netfs_resize_file (&cifsInode->netfs, attrs->ia_size, true); -fscache_resize_cookie (cifs_inode_cookie (inode), attrs->ia_size); - } -setattr_copy (&nop_mnt_idmap, inode, attrs);mark_inode_dirty (inode);
@@ -3518,13 +3511,6 @@ cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)if (rc) goto cifs_setattr_exit; -if ((attrs->ia_valid &ATTR_SIZE ) && - attrs->ia_size !=i_size_read (inode)) { -truncate_setsize (inode, attrs->ia_size); -netfs_resize_file (&cifsInode->netfs, attrs->ia_size, true); -fscache_resize_cookie (cifs_inode_cookie (inode), attrs->ia_size); - } -setattr_copy (&nop_mnt_idmap, inode, attrs);mark_inode_dirty (inode);
Signature · 1 line
2.55.0
Reading through the Sashiko findings for this https://sashiko.dev/#/patchset/20260731171231.686427-1-sorenson%40redhat.com
These are all pre-existing, and not introduced by this patchset. I have fixes for several of them prepared:
* cifs_setsize() calls truncate_pagecache() without i_rwsem from cifs_do_truncate()
Real, pre-existing. cifs_do_truncate() has always been called from cifs_open() without the lock. This would likely require significant work to fix.
* Local inode truncated to 0 before network open; no rollback on network failure
Real, but pre-existing and unrelated; I have a patch that fixes exactly this.
* len underflow in cifs_remap_file_range() when off > i_size
Real, but pre-existing, unrelated to any of these hunks.
* smb2_duplicate_extents() leaves file extended if FSCTL fails
Real, but pre-existing; I have a patch that fixes exactly this
* pagecache_isize_extended() missing in cifs_setsize()
Real, but pre-existing; I have a patch that fixes exactly this
* Use-after-free in cifs_file_set_size() after cifsFileInfo_put()
This is real, but it's pre-existing and unrelated to this patchset. That said, I have a patch for exactly this.
Frank
Quoted 59 lines · Frank Sorenson
After commit
fa724e235cfd("cifs: addfscache_resize_cookie()tocifs_setsize()") consolidated fscache cookie resizing intocifs_setsize(), two further fixes are needed and two dead-code blocks became removable.Patch 1 fixes
cifs_do_truncate()(O_TRUNCviacifs_open()). That path runs withouti_rwsem, so it cannot use the locked resize path.cifs_invalidate_cache()is the correct alternative: it works without the lock and ensures stale cached data is not served once the cookie is later activated.Patch 2 addresses a race identified during review:
cifs_setsize()callsfscache_resize_cookie()withouti_rwsem, but another concurrent open may already have the cookie active (IS_CACHINGset), makingfscache_resize_cookie()a real operation requiring the lock. The fix stripsfscache_resize_cookie()fromcifs_setsize()and introducescifs_resize_file_locked(), which temporarily activates the cookie withfscache_use_cookie(), performs the resize underi_rwsem, then deactivates it. Callers that already holdi_rwsem(cifs_file_set_size,smb2_duplicate_extents,smb3_simple_falloc) switch to the wrapper;cifs_do_truncate()continues to usecifs_setsize()followed bycifs_invalidate_cache()as established in patch 1.Patches 3 and 4 remove dead code. Patch 3 removes a caller-side
truncate_setsize()+fscache_resize_cookie()block fromcifs_remap_file_range()that became redundant oncesmb2_duplicate_extents()started performing the full size update viacifs_setsize()under thei_rwsemheld bylock_two_nondirectories(). Patch 4 removes equivalent dead blocks fromcifs_setattr_unix()andcifs_setattr_nounix(): sincecifs_file_set_size()callscifs_setsize()on success,i_sizealways equalsattrs->ia_sizeon the success path, making the subsequent size-inequality blocks unreachable.v3: Fixed broken patch
v2:
- Added patch 2 (cifs_resize_file_locked): based on review feedback (Huiwen He) that the fscache cookie is not guaranteed to be quiescent incifs_do_truncate()- another concurrent open may already have it active. The fix stripsfscache_resize_cookie()fromcifs_setsize()and adds a locked wrapper for callers that holdi_rwsem.
- Patch 1 commit message updated: the original rationale (cookie is always quiescent incifs_do_truncate) was incorrect; the correct reason is thati_rwsemcannot be held in that path.
- Patches 2-3 from v1 renumbered to 3-4 to place fixes before cleanups.
- Patches 3-4: add Reviewed-by from Huiwen He; otherwise unchanged from v1.
- All patches: add Reviewed-by from Paulo Alcantara.Frank Sorenson (4):
cifs: usecifs_invalidate_cache()incifs_do_truncate()forO_TRUNC
cifs: addcifs_resize_file_locked()to guardfscache_resize_cookie()
underi_rwsem
cifs: remove redundant size-update block incifs_remap_file_range()
cifs: remove dead size-update blocks in cifs_setattr_unix/nounixfs/smb/client/cifsfs.c | 6 +----- fs/smb/client/cifsfs.h | 1 + fs/smb/client/file.c | 1 + fs/smb/client/inode.c | 41 ++++++++++++++++++++++------------------- fs/smb/client/smb2ops.c | 9 +++------ 5 files changed, 28 insertions(+), 30 deletions(-)
Namjae Jeon
maintainerQuoted 46 lines · Frank Sorenson
After commit
fa724e235cfd("cifs: addfscache_resize_cookie()tocifs_setsize()") consolidated fscache cookie resizing intocifs_setsize(), two further fixes are needed and two dead-code blocks became removable.Patch 1 fixes
cifs_do_truncate()(O_TRUNCviacifs_open()). That path runs withouti_rwsem, so it cannot use the locked resize path.cifs_invalidate_cache()is the correct alternative: it works without the lock and ensures stale cached data is not served once the cookie is later activated.Patch 2 addresses a race identified during review:
cifs_setsize()callsfscache_resize_cookie()withouti_rwsem, but another concurrent open may already have the cookie active (IS_CACHINGset), makingfscache_resize_cookie()a real operation requiring the lock. The fix stripsfscache_resize_cookie()fromcifs_setsize()and introducescifs_resize_file_locked(), which temporarily activates the cookie withfscache_use_cookie(), performs the resize underi_rwsem, then deactivates it. Callers that already holdi_rwsem(cifs_file_set_size,smb2_duplicate_extents,smb3_simple_falloc) switch to the wrapper;cifs_do_truncate()continues to usecifs_setsize()followed bycifs_invalidate_cache()as established in patch 1.Patches 3 and 4 remove dead code. Patch 3 removes a caller-side
truncate_setsize()+fscache_resize_cookie()block fromcifs_remap_file_range()that became redundant oncesmb2_duplicate_extents()started performing the full size update viacifs_setsize()under thei_rwsemheld bylock_two_nondirectories(). Patch 4 removes equivalent dead blocks fromcifs_setattr_unix()andcifs_setattr_nounix(): sincecifs_file_set_size()callscifs_setsize()on success,i_sizealways equalsattrs->ia_sizeon the success path, making the subsequent size-inequality blocks unreachable.v3: Fixed broken patch
v2:
- Added patch 2 (cifs_resize_file_locked): based on review feedback (Huiwen He) that the fscache cookie is not guaranteed to be quiescent incifs_do_truncate()- another concurrent open may already have it active. The fix stripsfscache_resize_cookie()fromcifs_setsize()and adds a locked wrapper for callers that holdi_rwsem.
- Patch 1 commit message updated: the original rationale (cookie is always quiescent incifs_do_truncate) was incorrect; the correct reason is thati_rwsemcannot be held in that path.
- Patches 2-3 from v1 renumbered to 3-4 to place fixes before cleanups.
- Patches 3-4: add Reviewed-by from Huiwen He; otherwise unchanged from v1.
- All patches: add Reviewed-by from Paulo Alcantara.
Quoted 6 lines
Frank Sorenson (4):
cifs: usecifs_invalidate_cache()incifs_do_truncate()forO_TRUNC
cifs: addcifs_resize_file_locked()to guardfscache_resize_cookie()
underi_rwsem
cifs: remove redundant size-update block incifs_remap_file_range()
cifs: remove dead size-update blocks in cifs_setattr_unix/nounix
I will apply this patch-set to #for-next.
Thanks!