cifs: follow-on fixes after fscache_resize_cookie() consolidation
Subsystems (3)
v2 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.
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 v2 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
Re: [PATCH v2 1/4] cifs: use cifs_invalidate_cache() in cifs_do_truncate() for O_TRUNC
Quoted 10 lines · Frank Sorenson
cifs_do_truncate()is invoked fromcifs_open()withouti_rwsem, so it cannot usecifs_resize_file_locked()to perform a proper fscache cookie resize. Instead, addcifs_invalidate_cache()aftercifs_setsize().
cifs_invalidate_cache()callsfscache_invalidate(), which works without holdingi_rwsem: it unconditionally incrementsinval_counterand setsFSCACHE_COOKIE_NO_DATA_TO_READ, ensuring that stale cached data is not served once the cookie is later activated byfscache_use_cookie(). Truncation to zero leaves no valid cached data, making invalidation the correct semantic here.
What happens if there's a concurrent read or write in another thread? truncate(), buffered read/write and direct read/write() will play reasonably with each other through a combination of i_rwsem and the stuff in fs/netfs/locking.c.
But apart from that, I think that invalidating the cache should work. It may be slower, but since you're getting rid of all the data anyway...
David
Re: [PATCH v2 1/4] cifs: use cifs_invalidate_cache() in cifs_do_truncate() for O_TRUNC
Quoted 17 lines · David Howells
Quoted 10 lines · Frank Sorenson
cifs_do_truncate()is invoked fromcifs_open()withouti_rwsem, so it cannot usecifs_resize_file_locked()to perform a proper fscache cookie resize. Instead, addcifs_invalidate_cache()aftercifs_setsize().
cifs_invalidate_cache()callsfscache_invalidate(), which works without holdingi_rwsem: it unconditionally incrementsinval_counterand setsFSCACHE_COOKIE_NO_DATA_TO_READ, ensuring that stale cached data is not served once the cookie is later activated byfscache_use_cookie(). Truncation to zero leaves no valid cached data, making invalidation the correct semantic here.What happens if there's a concurrent read or write in another thread?
truncate(), buffered read/write and direct read/write() will play reasonably with each other through a combination ofi_rwsemand the stuff infs/netfs/locking.c.
Quoted 4 lines
But apart from that, I think that invalidating the cache should work. It may be slower, but since you're getting rid of all the data anyway...
David
The lockless call is pre-existing; this patch just adds the fscache_invalidate on top of it and doesn't worsen it, since it's safe to call without i_rwsem. Fixing the concurrent I/O issue properly is out of scope for this series.
Frank
Signature · 4 lines
Frank Sorenson [email protected] Principal Software Maintenance Engineer, filesystems Red Hat
Re: [PATCH v2 1/4] cifs: use cifs_invalidate_cache() in cifs_do_truncate() for O_TRUNC
LGTM. This is safe with or without the inode lock. The pre-existing O_TRUNC locking issue can be addressed separately.
Huiwen
Quoted 29 lines · Frank Sorenson
Quoted 17 lines · David Howells
Quoted 10 lines · Frank Sorenson
cifs_do_truncate()is invoked fromcifs_open()withouti_rwsem, so it cannot usecifs_resize_file_locked()to perform a proper fscache cookie resize. Instead, addcifs_invalidate_cache()aftercifs_setsize().
cifs_invalidate_cache()callsfscache_invalidate(), which works without holdingi_rwsem: it unconditionally incrementsinval_counterand setsFSCACHE_COOKIE_NO_DATA_TO_READ, ensuring that stale cached data is not served once the cookie is later activated byfscache_use_cookie(). Truncation to zero leaves no valid cached data, making invalidation the correct semantic here.What happens if there's a concurrent read or write in another thread?
truncate(), buffered read/write and direct read/write() will play reasonably with each other through a combination ofi_rwsemand the stuff infs/netfs/locking.c.Quoted 4 lines
But apart from that, I think that invalidating the cache should work. It may be slower, but since you're getting rid of all the data anyway...
David
The lockless call is pre-existing; this patch just adds the
fscache_invalidateon top of it and doesn't worsen it, since it's safe to call without i_rwsem. Fixing the concurrent I/O issue properly is out of scope for this series.Frank
PATCH v2 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,26 @@ 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
Re: [PATCH v2 2/4] cifs: add cifs_resize_file_locked() to guard fscache_resize_cookie() under i_rwsem
LGTM
Huiwen
Quoted 125 lines · Frank Sorenson
cifs_setsize()callsfscache_resize_cookie()without holdingi_rwsem. When the fscache cookie is active (FSCACHE_COOKIE_IS_CACHINGis set),fscache_resize_cookie()performs a real resize that requiresi_rwsemheld exclusively. If another file descriptor has the same inode open,fscache_use_cookie()was already called from thatcifs_open(), making the cookie active. In that case, callingcifs_setsize()fromcifs_do_truncate()(invoked fromcifs_open()withouti_rwsem) races against concurrent fscache I/O.Strip
fscache_resize_cookie()fromcifs_setsize(), making it a pure size/page-cache helper. Addcifs_resize_file_locked()for callers that already holdi_rwsem: it callsnetfs_resize_file()andcifs_setsize(), then temporarily activates the cookie withfscache_use_cookie()to perform the resize under the lock, then deactivates it withcifs_fscache_unuse_inode_cookie(). Usingfscache_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 ofsmb3_simple_falloc()to the new wrapper; those paths already holdi_rwsemvia VFS setattr,lock_two_nondirectories(), orcifs_fallocate()respectively.cifs_do_truncate()continues to callcifs_setsize()followed bycifs_invalidate_cache(), since it runs withouti_rwsem.Fixes fa724e235cfd ("cifs: add fscache_resize_cookie() to cifs_setsize()")Cc David Howells <[email protected]>Cc Paulo Alcantara <[email protected]>Signed-off-by Frank Sorenson <[email protected]>Reviewed-by Paulo Alcantara (Red Hat) <[email protected]>--- 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,26 @@ 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,
PATCH v2 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 v2 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