From: Huiwen He <huiwen.he@linux.dev>
To: linkinjeon@kernel.org, pc@manguebit.org,
ronniesahlberg@gmail.com, sprasad@microsoft.com, tom@talpey.com,
bharathsm@microsoft.com, senozhatsky@chromium.org,
dhowells@redhat.com, chenxiaosong@kylinos.cn
Cc: linux-cifs@vger.kernel.org
Subject: [PATCH v3 7/7] smb/client: invalidate fscache for fallocate range operations
Date: Sun, 23 Aug 2026 23:10:53 +0800 [thread overview]
Message-ID: <20260823151053.935889-8-huiwen.he@linux.dev> (raw)
In-Reply-To: <20260823151053.935889-1-huiwen.he@linux.dev>
From: Huiwen He <hehuiwen@kylinos.cn>
smb3_zero_range(), smb3_punch_hole(), smb3_insert_range(), and
smb3_collapse_range() modify file contents through server-side range
operations. These operations discard the affected page cache, but leave
the FS-Cache cookie valid, so a later read may return data cached before
the range operation.
Fix this by invalidating FS-Cache after outstanding I/O has completed
and before modifying the file on the server.
Run the following as root on a CIFS mount with fsc enabled and an active
CacheFiles backend:
bash -c '
MNT=/mnt/cifs
FILE="$MNT/repro"
# Generate four 1 MiB random blocks: [A][B][C][D].
dd if=/dev/urandom of=/tmp/src bs=1M count=4 status=none
# Expected contents after zeroing B: [A][zero][C][D].
cp /tmp/src /tmp/expected
dd if=/dev/zero of=/tmp/expected bs=1M seek=1 count=1 \
conv=notrunc status=none
cp /tmp/src "$FILE"
# Populate FS-Cache, then discard the page cache.
sync
echo 1 > /proc/sys/vm/drop_caches
cat "$FILE" > /dev/null
sync
echo 1 > /proc/sys/vm/drop_caches
fallocate --zero-range -o 1M -l 1M "$FILE"
if cmp -s /tmp/expected "$FILE"; then
echo "readback: OK"
else
echo "readback: STALE DATA"
fi
'
Before this change, the readback differs from /tmp/expected:
readback: STALE DATA
After this change, it matches:
readback: OK
Fixes: 30175628bf7f ("[SMB3] Enable fallocate -z support for SMB3 mounts")
Fixes: 31742c5a3317 ("enable fallocate punch hole ("fallocate -p") for SMB3")
Fixes: 5476b5dd82c8 ("cifs: add support for FALLOC_FL_COLLAPSE_RANGE")
Fixes: 7fe6fe95b936 ("cifs: add FALLOC_FL_INSERT_RANGE support")
Signed-off-by: Huiwen He <hehuiwen@kylinos.cn>
Suggested-by: Namjae Jeon <linkinjeon@kernel.org>
Reviewed-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
fs/smb/client/smb2ops.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 835bc342840d..60a40d6b0da3 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -3549,6 +3549,9 @@ static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
if (keep_size == false && !CIFS_CACHE_READ(cifsi))
goto zero_range_exit;
+ fscache_invalidate(cifs_inode_cookie(inode), NULL,
+ i_size_read(inode), 0);
+
rc = smb3_zero_data(file, tcon, offset, len, xid);
if (rc < 0)
goto zero_range_exit;
@@ -3618,6 +3621,8 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
*/
truncate_pagecache_range(inode, offset, offset + len - 1);
netfs_wait_for_outstanding_io(inode);
+ fscache_invalidate(cifs_inode_cookie(inode), NULL,
+ i_size_read(inode), 0);
cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
@@ -4037,6 +4042,7 @@ static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
* moving data on the server, so subsequent reads do not see stale data.
*/
truncate_pagecache_range(inode, round_down(off, PAGE_SIZE), -1);
+ fscache_invalidate(cifs_inode_cookie(inode), NULL, old_eof, 0);
spin_lock(&inode->i_lock);
netfs_write_zero_point(inode, old_eof);
@@ -4125,6 +4131,7 @@ static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
* moving data on the server, so subsequent reads do not see stale data.
*/
truncate_pagecache_range(inode, round_down(off, PAGE_SIZE), -1);
+ fscache_invalidate(cifs_inode_cookie(inode), NULL, old_eof, 0);
rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
cfile->fid.volatile_fid, cfile->pid, new_eof);
--
2.43.0
next prev parent reply other threads:[~2026-08-23 15:12 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-23 15:10 [PATCH v3 0/7] smb/client: fix fallocate range operation issues Huiwen He
2026-08-23 15:10 ` [PATCH v3 1/7] smb/client: validate new EOF for insert range Huiwen He
2026-08-27 2:07 ` Paulo Alcantara
2026-08-23 15:10 ` [PATCH v3 2/7] smb/client: validate new EOF for zero range Huiwen He
2026-08-27 2:08 ` Paulo Alcantara
2026-08-23 15:10 ` [PATCH v3 3/7] smb/client: mark file sparse before emulating insert range Huiwen He
2026-08-27 2:13 ` Paulo Alcantara
2026-08-27 3:18 ` hehuiwen
2026-08-23 15:10 ` [PATCH v3 4/7] smb/client: fix data corruption in emulated " Huiwen He
2026-08-27 2:19 ` Paulo Alcantara
2026-08-27 15:45 ` hehuiwen
2026-08-28 1:32 ` Paulo Alcantara
2026-08-23 15:10 ` [PATCH v3 5/7] smb/client: fix integer truncation in collapse range Huiwen He
2026-08-23 15:10 ` [PATCH v3 6/7] smb/client: fix stale page cache in insert/collapse range Huiwen He
2026-08-27 2:20 ` Paulo Alcantara
2026-08-23 15:10 ` Huiwen He [this message]
2026-08-27 2:20 ` [PATCH v3 7/7] smb/client: invalidate fscache for fallocate range operations Paulo Alcantara
2026-08-24 2:01 ` [PATCH v3 0/7] smb/client: fix fallocate range operation issues Namjae Jeon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260823151053.935889-8-huiwen.he@linux.dev \
--to=huiwen.he@linux.dev \
--cc=bharathsm@microsoft.com \
--cc=chenxiaosong@kylinos.cn \
--cc=dhowells@redhat.com \
--cc=linkinjeon@kernel.org \
--cc=linux-cifs@vger.kernel.org \
--cc=pc@manguebit.org \
--cc=ronniesahlberg@gmail.com \
--cc=senozhatsky@chromium.org \
--cc=sprasad@microsoft.com \
--cc=tom@talpey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox