Skip to contentSkip to conversation

smb: client: base punch-hole EOF decision on server_eof

PATCH RESEND Superseded Joseph Qi 2 participants 4 messages Nov 3 – Nov 4

Subsystems (3)
lore
Share & tools
lore

Joseph Qi

author
#0 lore raw

PATCH RESEND 1/1 smb: client: base punch-hole EOF decision on server_eof

smb3_punch_hole() decides whether to move the server EOF marker by comparing against netfs_inode(inode)->remote_i_size. Nothing on this base keeps that field up to date: netfs_inode_init() has no caller under fs/smb/, so remote_i_size is never even initialized, and the only writes to it are netfs_resize_file() from the clone and copy_file_range paths and this function itself. The writeback path tracks the server size in cifsi->server_eof through cifs_update_eof() instead, and cifs_fattr_to_inode() refreshes only server_eof as well.

Reading remote_i_size therefore compares the punch against whatever the slab object last held. With it at zero, punching the first 4k of a clean 8k file passes "end > remote_i_size && i_size > remote_i_size", issues SMB2_set_eof(4096) and truncates the server file in half, with nothing dirty left to write the tail back.

The flush added by commit 216e46958605 ("smb/client: flush dirty data before punching a hole") makes a second variant reachable. It writes out and cleans any page straddling the end of the punched range, including the valid data in it beyond the hole, and advances server_eof but not remote_i_size. When the punch end lies beyond that stale value, SMB2_set_eof() shrinks the server to the end of the hole and discards what the flush just wrote out. The page is clean, so a later fsync or close will not write it again and the data is silently lost.

Base the decision on CIFS_I(inode)->server_eof and keep both size fields in sync when the marker moves, so the server is never shrunk below data that has already been written out. Take inode->i_lock for the read and the update, as server_eof is documented to require, and only ever advance it, matching cifs_update_eof(): background writeback of folios outside the punched range is excluded by neither i_rwsem nor the invalidate lock, and can publish a larger EOF while SMB2_set_eof() is in flight.

Mainline does not need this change. cifs_alloc_inode() zeroes _remote_i_size there, cifs_fattr_to_inode() refreshes it, and the writeback path advances it through netfs_resize_file(), so the value cannot be stale after the flush.

Fixes 288d398df80c ("cifs: Fix FALLOC_FL_PUNCH_HOLE support")
---
 fs/smb/client/smb2ops.c | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)
fs/smb/client/smb2ops.c +30 -9
index 21a2b265ae7c4..2826a60f0d8ab 100644
@@ -3346,7 +3346,7 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
 	struct inode *inode = file_inode(file);
 	struct cifsFileInfo *cfile = file->private_data;
 	struct file_zero_data_information fsctl_buf;
-	unsigned long long end = offset + len, i_size, remote_i_size;
+	unsigned long long end = offset + len, i_size, server_eof;
 	long rc;
 	unsigned int xid;
 	__u8 set_sparse = 1;
@@ -3392,20 +3392,41 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
 	if (rc)
 		goto unlock;
 
-	/* If there's dirty data in the buffer that would extend the EOF if it
-	 * were written, then we need to move the EOF marker over to the lower
-	 * 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.
+	/*
+	 * If the tail of the file beyond the hole has not been written out
+	 * yet, the server EOF marker is still below both the end of the hole
+	 * and i_size.  Move it up to the lower of the two: when that is
+	 * i_size the file is already at its final size, otherwise the
+	 * remaining dirty tail is written later and extends it the rest of
+	 * the way.
+	 *
+	 * Decide on server_eof rather than the netfs remote_i_size, which
+	 * nothing here keeps up to date: the flush above advances only
+	 * server_eof, through cifs_update_eof(), so comparing against
+	 * remote_i_size would shrink the server below data that was just
+	 * written out.
 	 */
+	spin_lock(&inode->i_lock);
 	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) {
+	server_eof = CIFS_I(inode)->server_eof;
+	spin_unlock(&inode->i_lock);
+
+	if (end > server_eof && i_size > server_eof) {
 		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) {
+			/*
+			 * Writeback of folios outside the punched range may have
+			 * published a larger EOF while the request was in flight,
+			 * so only ever advance, as cifs_update_eof() does.
+			 */
+			spin_lock(&inode->i_lock);
+			if (extend_to > CIFS_I(inode)->server_eof)
+				CIFS_I(inode)->server_eof = extend_to;
 			netfs_inode(inode)->remote_i_size = extend_to;
+			spin_unlock(&inode->i_lock);
+		}
 	}
 
 unlock:
Signature · 1 line
2.39.3
maintainer
#1 lore raw
Quoted 3 lines

smb3_punch_hole() decides whether to move the server EOF marker by comparing against netfs_inode(inode)->remote_i_size. Nothing on this base keeps that field up to date

This is a stable-only fix, so I need an ack from a cifs maintainer (Steve French or Paulo Alcantara) on the list before I can queue it. Sashiko also flags a possible race with writeback and a stale server_eof in smb3_punch_hole(); please address that too.

Signature · 2 lines
Thanks,
Sasha
author
#2 lore raw

PATCH v2 1/1 smb: client: base punch-hole EOF decision on server_eof

In reply to Joseph Qi

smb3_punch_hole() decides whether to move the server EOF marker by comparing against netfs_inode(inode)->remote_i_size. Nothing on this base keeps that field up to date: netfs_inode_init() has no caller under fs/smb/, so remote_i_size is never even initialized, and the only writes to it are netfs_resize_file() from the clone and copy_file_range paths and this function itself. The writeback path tracks the server size in cifsi->server_eof through cifs_update_eof() instead, and cifs_fattr_to_inode() refreshes only server_eof as well.

Reading remote_i_size therefore compares the punch against whatever the slab object last held. With it at zero, punching the first 4k of a clean 8k file passes "end > remote_i_size && i_size > remote_i_size", issues SMB2_set_eof(4096) and truncates the server file in half, with nothing dirty left to write the tail back.

The flush added by commit 216e46958605 ("smb/client: flush dirty data before punching a hole") makes a second variant reachable. It writes out and cleans any page straddling the end of the punched range, including the valid data in it beyond the hole, and advances server_eof but not remote_i_size. When the punch end lies beyond that stale value, SMB2_set_eof() shrinks the server to the end of the hole and discards what the flush just wrote out. The page is clean, so a later fsync or close will not write it again and the data is silently lost.

Base the decision on CIFS_I(inode)->server_eof and keep both size fields in sync when the marker moves, so the server is never shrunk below data that has already been written out.

The cached server_eof can in turn lag the server. Writeback of already-dirty folios beyond the punched range is excluded by neither i_rwsem nor the invalidate lock (cifs_writepages() takes neither), so it can complete while the decision or the SMB2_set_eof() request based on it is in flight, extending the server EOF beyond the value the request carries; the request then shrinks the server below data that writeback has just committed, and the local marker is left ahead of the server. When the marker needs to move, flush the whole file and re-read server_eof before deciding: writeback completions publish server_eof before ending folio writeback, so once filemap_write_and_wait() returns, the cached value reflects everything written out and no dirty folio remains that could race with the request. Take inode->i_lock for the reads and the update, as server_eof is documented to require, and only ever advance it, matching cifs_update_eof(): folios dirtied through an mmap can still be written back while the request is in flight.

Mainline does not need this change. cifs_alloc_inode() zeroes _remote_i_size there, cifs_fattr_to_inode() refreshes it, and the writeback path advances it through netfs_resize_file(), so the value cannot be stale after the flush.

Fixes 288d398df80c ("cifs: Fix FALLOC_FL_PUNCH_HOLE support")
---
 fs/smb/client/smb2ops.c | 75 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 62 insertions(+), 13 deletions(-)
fs/smb/client/smb2ops.c +62 -13
index 21a2b265ae7c4..df3499c8a2249 100644
@@ -3346,7 +3346,7 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
 	struct inode *inode = file_inode(file);
 	struct cifsFileInfo *cfile = file->private_data;
 	struct file_zero_data_information fsctl_buf;
-	unsigned long long end = offset + len, i_size, remote_i_size;
+	unsigned long long end = offset + len, i_size, server_eof;
 	long rc;
 	unsigned int xid;
 	__u8 set_sparse = 1;
@@ -3392,20 +3392,69 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
 	if (rc)
 		goto unlock;
 
-	/* If there's dirty data in the buffer that would extend the EOF if it
-	 * were written, then we need to move the EOF marker over to the lower
-	 * 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.
+	/*
+	 * If the tail of the file beyond the hole has not been written out
+	 * yet, the server EOF marker is still below both the end of the hole
+	 * and i_size.  Move it up to the lower of the two: when that is
+	 * i_size the file is already at its final size, otherwise the
+	 * remaining dirty tail is written later and extends it the rest of
+	 * the way.
+	 *
+	 * Decide on server_eof rather than the netfs remote_i_size, which
+	 * nothing here keeps up to date: the flush above advances only
+	 * server_eof, through cifs_update_eof(), so comparing against
+	 * remote_i_size would shrink the server below data that was just
+	 * written out.
 	 */
+	spin_lock(&inode->i_lock);
 	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)
-			netfs_inode(inode)->remote_i_size = extend_to;
+	server_eof = CIFS_I(inode)->server_eof;
+	spin_unlock(&inode->i_lock);
+
+	if (end > server_eof && i_size > server_eof) {
+		/*
+		 * The cached server_eof may still lag the server: writeback
+		 * of already-dirty folios beyond the punched range takes
+		 * neither i_rwsem nor the invalidate lock (see
+		 * cifs_writepages()), so it can extend the server EOF while
+		 * the decision or the request below is in flight, and the
+		 * request would then shrink the server below data that
+		 * writeback has just committed.  Flush the whole file and
+		 * re-read before deciding: writeback completions publish
+		 * server_eof before ending folio writeback, so once the
+		 * flush has returned the cached value reflects everything
+		 * that was written out, and no dirty folio is left that
+		 * could race with the request.
+		 */
+		rc = filemap_write_and_wait(inode->i_mapping);
+		if (rc < 0)
+			goto unlock;
+
+		spin_lock(&inode->i_lock);
+		i_size = i_size_read(inode);
+		server_eof = CIFS_I(inode)->server_eof;
+		spin_unlock(&inode->i_lock);
+
+		if (end > server_eof && i_size > server_eof) {
+			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) {
+				/*
+				 * Folios dirtied through an mmap while the
+				 * request was in flight can still be written
+				 * back and publish a larger EOF, so only ever
+				 * advance, as cifs_update_eof() does.
+				 */
+				spin_lock(&inode->i_lock);
+				if (extend_to > CIFS_I(inode)->server_eof)
+					CIFS_I(inode)->server_eof = extend_to;
+				netfs_inode(inode)->remote_i_size = extend_to;
+				spin_unlock(&inode->i_lock);
+			}
+		}
 	}
 
 unlock:
Signature · 1 line
2.39.3
maintainer
#3 lore raw
Quoted 2 lines · Joseph Qi

Reading remote_i_size therefore compares the punch against whatever the slab object last held.

This is a stable-only change with no upstream commit, so I need an Acked-by or Reviewed-by from Steve or Paulo before I can queue it. Steve, Paulo, could you review it?

Signature · 2 lines
Thanks,
Sasha