[PATCH 0/8] nilfs2: support GC error rollback and avoid infinite retry hangs

From: Ryusuke Konishi

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


Hi Viacheslav,

Please apply this series for the next cycle.

This patch series rectifies an early design flaw in the garbage
collection mechanism itself and fundamentally resolves the task hang
issue seen in the GC ioctl (NILFS_IOCTL_CLEAN_SEGMENTS). To make GC
safely abortable upon errors, this series strengthens the error-recovery
and rollback logic for metadata updated during GC, specifically segment
usage information (sufile) and virtual block address mappings (DAT).

Historically, NILFS2 GC was designed around a point of no return: once
the log writer started, it looped until a full-format log (including a
checkpoint and a super root) was successfully written out. Because GC
runs exclusively while holding ns_segctor_sem for writing, any
persistent error during GC blocked the log writer thread and write
operations indefinitely, causing filesystem hangs.

An initial fix by Joshua Crofts (commit ce5a5ad1a833) took the right
direction by introducing an escape path when the filesystem
transitioned to read-only mode (-EROFS). However, two issues remained:

1. Cached GC inodes were leaked when GC exited on error, which Wang
Jianjian addressed in the first patch of this series.
2. Persistent errors that do not trigger read-only remounts (such as
-EIO or -ENOSPC) still caused infinite retry loops, as further
reported by Zheng Zheng.

Whenever GC aborts mid-way, however, an underlying issue arises:
bailing out of the GC loop leaves irreversible inconsistencies in
on-disk and in-memory metadata. While this was previously masked
because the escape path was only taken during emergency read-only
degradation, expanding error exits to general errors exposes this
problem, breaking subsequent GC runs and causing userland tools such
as "lssu -l" to fail permanently.

To build upon commit ce5a5ad1a833 and make error exits safe, this
series implements dedicated undo mechanisms for each metadata change
performed during GC before allowing nilfs_clean_segments() to bail
out on general errors:

- Patch 1 (by Wang Jianjian) fixes the GC inode leak on error paths.
- Patch 2 makes physical block location updates in the DAT for
GC-relocated blocks (other than DAT blocks themselves) reversible by
restoring them from the shadow mapping on error.
- Patch 3 makes the removal of GC target segment usage entries in
sufile reversible by saving and restoring them on abort.
- Patch 4 fixes an issue where stale nilfs_redirected flags remain on
primary DAT buffers when GC is aborted.
- Patches 5-6 make the deletion of obsolete virtual block entries from
the DAT during GC reversible by saving and restoring them on abort.
- Patch 7 defers this virtual block deletion to a safe stage so that the
freed entries cannot be reallocated while GC is in progress, which is
required now that GC can abort mid-way.
- Patch 8 updates nilfs_clean_segments() to bail out immediately on
errors, retaining only a minimal bounded retry loop for transient
cases.

Thanks,
Ryusuke Konishi


Ryusuke Konishi (7):
nilfs2: cancel the relocation of GC-targeted blocks if log writing
fails
nilfs2: restore segment usage from the save buffer when aborting GC
nilfs2: drop redirected buffer flag from DAT when clearing shadow map
nilfs2: save DAT entry blocks to shadow mapping before freeing
nilfs2: restore removed DAT entries when aborting GC
nilfs2: free virtual block numbers just before collecting DAT changes
nilfs2: avoid infinite retry loop on GC failure

Wang Jianjian (1):
nilfs2: fix leak of gc inodes on error

fs/nilfs2/alloc.c | 14 +++-
fs/nilfs2/alloc.h | 3 +-
fs/nilfs2/bmap.h | 23 ++++++
fs/nilfs2/btree.c | 8 ++
fs/nilfs2/dat.c | 181 +++++++++++++++++++++++++++++++++++++++++++-
fs/nilfs2/dat.h | 5 +-
fs/nilfs2/direct.c | 1 +
fs/nilfs2/ioctl.c | 36 +--------
fs/nilfs2/mdt.c | 41 ++++++++--
fs/nilfs2/segment.c | 108 +++++++++++++++++++++++---
fs/nilfs2/segment.h | 7 ++
fs/nilfs2/sufile.c | 27 ++++---
fs/nilfs2/sufile.h | 37 ++++-----
13 files changed, 404 insertions(+), 87 deletions(-)

--
2.53.0