From: Mark Syms <mark.syms@citrix.com>
To: stable@vger.kernel.org
Cc: Mark Syms <mark.syms@citrix.com>,
linux-cifs@vger.kernel.org, Steve French <sfrench@samba.org>,
Paulo Alcantara <pc@manguebit.com>,
David Howells <dhowells@redhat.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Sasha Levin <sashal@kernel.org>
Subject: [PATCH 6.6.y] smb: client: don't truncate the file when punching a hole
Date: Fri, 4 Dec 2026 10:38:38 +0000 [thread overview]
Message-ID: <20261204103838.830548-1-mark.syms@citrix.com> (raw)
smb3_punch_hole() moves the server's EOF out to cover dirty data whose
size update the local hole punch dropped, and decides whether to from the
netfs remote_i_size. This tree's cifs does not maintain remote_i_size: it
is set at inode init and on copy/clone only, so for any file that has
grown since, the "extension" truncates the file to the end of the hole:
truncate -s 64M f; fallocate -p -o 1M -l 512k f -> 1572864 bytes
qemu passes guest discards through as punch holes, so a guest's first
discard cuts its image short and every block allocated above the hole is
lost.
Ask the server for its EOF instead, so the call can only grow the file.
Fixes: 288d398df80c ("cifs: Fix FALLOC_FL_PUNCH_HOLE support")
Assisted-by: Claude:claude-opus-5-5 checkpatch
Signed-off-by: Mark Syms <mark.syms@citrix.com>
---
This is a fix for 6.6.y only. Mainline is not affected: since the netfs
write conversion, remote_i_size is maintained on every write, so the
check in smb3_punch_hole() is correct there. 6.6 got the check from the
backport of 416871f4fb84 (6.6.49) without that maintenance, and every
6.6.y release since (checked through 6.6.163) truncates a file that has
grown since its inode was loaded when a hole is punched below its EOF.
It depends on query_server_eof() from 74f39fa28286 ("smb: client: only
require read lease for size extending ops"), which is in 6.6.163.
Found as qcow2 images on an SMB share being cut short by guest discards
passed through by qemu. Tested on 6.6.138 with the 6.6.158-163 smb
client fixes applied: the reproducer above keeps 67108864 bytes, and a
qemu-io discard/zero-range pattern test verifies clean.
fs/smb/client/smb2ops.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 84acf669f88a..20b4072314bd 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -3414,15 +3414,25 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
* of the high end of the hole and the proposed EOF. The problem is
* that we locally hole-punch the tail of the dirty data, the proposed
* EOF update will end up in the wrong place.
+ *
+ * Ask the server where its EOF is: this tree does not maintain the
+ * netfs remote_i_size, and a stale one turns this into a truncation
+ * of everything beyond the hole.
*/
+ rc = query_server_eof(xid, tcon, cfile, &remote_i_size);
+ if (rc)
+ goto unlock;
i_size = i_size_read(inode);
- remote_i_size = netfs_inode(inode)->remote_i_size;
if (end > remote_i_size && i_size > remote_i_size) {
unsigned long long extend_to = umin(end, i_size);
rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
cfile->fid.volatile_fid, cfile->pid, extend_to);
- if (rc >= 0)
+ if (rc >= 0) {
netfs_inode(inode)->remote_i_size = extend_to;
+ spin_lock(&inode->i_lock);
+ CIFS_I(inode)->server_eof = extend_to;
+ spin_unlock(&inode->i_lock);
+ }
}
unlock:
--
2.56.0
next reply other threads:[~2026-12-04 10:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 10:38 Mark Syms [this message]
2026-12-04 19:49 ` [PATCH 6.6.y] smb: client: don't truncate the file when punching a hole Sasha Levin
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=20261204103838.830548-1-mark.syms@citrix.com \
--to=mark.syms@citrix.com \
--cc=dhowells@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-cifs@vger.kernel.org \
--cc=pc@manguebit.com \
--cc=sashal@kernel.org \
--cc=sfrench@samba.org \
--cc=stable@vger.kernel.org \
/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