[PATCH 4/8] nilfs2: drop redirected buffer flag from DAT when clearing shadow map

From: Ryusuke Konishi

Date: Thu Nov 26 2026 - 23:42:55 EST


During garbage collection, the nilfs_redirected flag is set on buffer
heads in the primary mapping of the DAT inode when their blocks are
frozen into the shadow map. Normally, this flag is cleared when the
DAT blocks are written out, so it did not cause issues before.

However, since commit ce5a5ad1a833 ("nilfs2: fix infinite loop in
nilfs_clean_segments()"), if GC is aborted due to an error before the
DAT blocks are written, nilfs_mdt_clear_shadow_map() releases the frozen
buffers without clearing the nilfs_redirected flag from the corresponding
primary buffer heads. As a result, stale nilfs_redirected flags remain
on the DAT buffers, causing subsequent GC operations to skip freezing
those buffers into the shadow map.

Fix this by looking up the corresponding buffer head in the primary
mapping of @inode and clearing its nilfs_redirected flag inside
nilfs_release_frozen_buffers().

Fixes: ce5a5ad1a833 ("nilfs2: fix infinite loop in nilfs_clean_segments()")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>
---
fs/nilfs2/mdt.c | 41 ++++++++++++++++++++++++++++++++++-------
1 file changed, 34 insertions(+), 7 deletions(-)

diff --git a/fs/nilfs2/mdt.c b/fs/nilfs2/mdt.c
index 77dc2be2ce65..91b8455e7f94 100644
--- a/fs/nilfs2/mdt.c
+++ b/fs/nilfs2/mdt.c
@@ -602,16 +602,43 @@ nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
return bh_frozen;
}

-static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
+static void nilfs_release_frozen_buffers(struct inode *inode,
+ struct nilfs_shadow_map *shadow)
{
struct list_head *head = &shadow->frozen_buffers;
- struct buffer_head *bh;
+ unsigned int blkbits = inode->i_blkbits;

while (!list_empty(head)) {
- bh = list_first_entry(head, struct buffer_head,
- b_assoc_buffers);
- list_del_init(&bh->b_assoc_buffers);
- brelse(bh); /* drop ref-count to make it releasable */
+ struct buffer_head *bh_frozen, *bh;
+ struct folio *folio;
+ unsigned int n;
+
+ bh_frozen = list_first_entry(head, struct buffer_head,
+ b_assoc_buffers);
+
+ /*
+ * Clear the nilfs_redirected flag from the primary buffer head
+ * corresponding to bh_frozen to ensure that the flag does not
+ * remain on the non-shadow DAT blocks when a GC operation
+ * terminates due to an error.
+ */
+ folio = filemap_lock_folio(inode->i_mapping,
+ bh_frozen->b_folio->index);
+ if (!IS_ERR(folio)) {
+ bh = folio_buffers(folio);
+ if (bh) {
+ n = offset_in_folio(bh_frozen->b_folio,
+ bh_frozen->b_data) >> blkbits;
+ bh = get_nth_bh(bh, n);
+ clear_buffer_nilfs_redirected(bh);
+ brelse(bh);
+ }
+ folio_unlock(folio);
+ folio_put(folio);
+ }
+
+ list_del_init(&bh_frozen->b_assoc_buffers);
+ brelse(bh_frozen); /* drop ref-count to make it releasable */
}
}

@@ -653,7 +680,7 @@ void nilfs_mdt_clear_shadow_map(struct inode *inode)
struct inode *shadow_btnc_inode = NILFS_I(shadow->inode)->i_assoc_inode;

down_write(&mi->mi_sem);
- nilfs_release_frozen_buffers(shadow);
+ nilfs_release_frozen_buffers(inode, shadow);
truncate_inode_pages(shadow->inode->i_mapping, 0);
truncate_inode_pages(shadow_btnc_inode->i_mapping, 0);
up_write(&mi->mi_sem);
--
2.53.0