Skip to contentSkip to conversation

btrfs: drop premature generation setting in btrfs_init_new_buffer()

PATCH In review Sun YangKai 2 participants 4 messages Nov 20, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: drop premature generation setting in btrfs_init_new_buffer()

In btrfs_init_new_buffer(), we set the buffer's generation just for the check in btrfs_clear_buffer_dirty(). So just pass NULL to skip the check and we don't need to set the generation before erasing the buffer.

No functional change.

See commit messages for details.

Sun YangKai (2):
btrfs: add comment for btrfs_clear_buffer_dirty
btrfs: drop premature generation setting in btrfs_init_new_buffer()

 fs/btrfs/extent-tree.c |  6 ++----
 fs/btrfs/extent_io.c   | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)
Signature · 1 line
2.51.2
#1 lore raw

PATCH 1/2 btrfs: add comment for btrfs_clear_buffer_dirty

---
 fs/btrfs/extent_io.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/btrfs/extent_io.c +20 -0
index 23273d0e6f22..e80066fc0f02 100644
@@ -3678,6 +3678,26 @@ static void btree_clear_folio_dirty_tag(struct folio *folio)
 	xa_unlock_irq(&folio->mapping->i_pages);
 }
 
+/*
+ * btrfs_clear_buffer_dirty - Clear the dirty state of an extent buffer
+ * @trans:	Transaction handle, may be NULL.
+ *		If provided, the buffer must belong to the transaction
+ *		(checked via btrfs_header_generation). If the check failed,
+ *		the function returns immediately.
+ * @eb:		The extent buffer to clean.
+ *
+ * This function clears the dirty flag from @eb and updates all accounting
+ * that depends on it (per-CPU counter of dirty metadata bytes, folio dirty
+ * state, address-space tags).
+ *
+ * Special behaviour in zoned mode:
+ *   When the filesystem is zoned (btrfs_is_zoned) the buffer is *not*
+ *   immediately cleaned. Instead the EXTENT_BUFFER_ZONED_ZEROOUT flag is
+ *   set and the buffer remains conceptually dirty.  The physical zero-out
+ *   required by zoned devices is deferred until btree_csum_one_bio(), which
+ *   preserves write-ordering constraints without forcing callers to re-dirty
+ *   the buffer later.
+ */
 void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
 			      struct extent_buffer *eb)
 {
Signature · 1 line
2.51.2
#2 lore raw

PATCH 2/2 btrfs: drop premature generation setting in btrfs_init_new_buffer()

In reply to Sun YangKai

btrfs_init_new_buffer() sets the header generation only to satisfy btrfs_clear_buffer_dirty()'s transaction check, then immediately
cleared by memzero_extent_buffer(). This is introduced in
commit cbddcc4fa3443("btrfs: set generation before calling btrfs_clean_tree_block in btrfs_init_new_buffer").

However, after commit c4e54a6571168("btrfs: replace clearing extent buffer dirty bit with btrfs_clean_block") we can pass NULL instead of the transaction handle to
btrfs_clear_buffer_dirty() so that the generation of the buffer is not
checked. So we can remove the generation assignment that was done only
for that check.

No functional change, just removes a redundant write.

---
 fs/btrfs/extent-tree.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/btrfs/extent-tree.c +2 -4
index dc4ca98c3780..b22b0aaa99e4 100644
@@ -5082,9 +5082,6 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	    !test_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &root->state))
 		lockdep_owner = BTRFS_FS_TREE_OBJECTID;
 
-	/* btrfs_clear_buffer_dirty() accesses generation field. */
-	btrfs_set_header_generation(buf, trans->transid);
-
 	/*
 	 * This needs to stay, because we could allocate a freed block from an
 	 * old tree into a new tree, so we need to make sure this new block is
@@ -5093,7 +5090,8 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	btrfs_set_buffer_lockdep_class(lockdep_owner, buf, level);
 
 	btrfs_tree_lock_nested(buf, nest);
-	btrfs_clear_buffer_dirty(trans, buf);
+	/* We don't care buf's generation here. */
+	btrfs_clear_buffer_dirty(NULL, buf);
 	clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
 	clear_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &buf->bflags);
 
Signature · 1 line
2.51.2
Reviewed-by on the v1 series
#3 lore raw
In reply to Sun YangKai
Quoted 7 lines · Sun YangKai

In btrfs_init_new_buffer(), we set the buffer's generation just for the check in btrfs_clear_buffer_dirty(). So just pass NULL to skip the check and we don't need to set the generation before erasing the buffer.

No functional change.

See commit messages for details.

Looks good, thanks

Quoted 12 lines

Sun YangKai (2):
btrfs: add comment for btrfs_clear_buffer_dirty
btrfs: drop premature generation setting in btrfs_init_new_buffer()

 fs/btrfs/extent-tree.c |  6 ++----
 fs/btrfs/extent_io.c   | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)
Signature · 1 line
2.51.2