[PATCH 6/8] nilfs2: restore removed DAT entries when aborting GC
From: Ryusuke Konishi
Date: Thu Nov 26 2026 - 23:43:10 EST
Since commit ce5a5ad1a833 ("nilfs2: fix infinite loop in
nilfs_clean_segments()"), an escape path was added to exit the GC loop
in nilfs_clean_segments() when GC fails. However, if GC is aborted
due to an error, obsolete virtual block numbers freed from the DAT
metadata file are left deleted, causing subsequent GC runs or userland
tools such as lssu -l (which relies on the DAT to evaluate live blocks)
to fail due to missing DAT entries.
Fix this by restoring the removed DAT entries from the shadow mapping
saved by nilfs_dat_freev() when log construction is aborted during GC.
Fixes: ce5a5ad1a833 ("nilfs2: fix infinite loop in nilfs_clean_segments()")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>
---
fs/nilfs2/dat.c | 72 +++++++++++++++++++++++++++++++++++++++++++++
fs/nilfs2/dat.h | 1 +
fs/nilfs2/segment.c | 10 ++++++-
fs/nilfs2/segment.h | 4 +++
4 files changed, 86 insertions(+), 1 deletion(-)
diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
index b4156945e433..94d358d9d5b0 100644
--- a/fs/nilfs2/dat.c
+++ b/fs/nilfs2/dat.c
@@ -352,6 +352,78 @@ int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
return nilfs_palloc_freev(dat, vblocknrs, nitems);
}
+/**
+ * nilfs_dat_cancel_freev - undo removal of DAT entries
+ * @dat: DAT file inode
+ * @vblocknrs: array of virtual block numbers
+ * @nitems: number of virtual block numbers
+ *
+ * nilfs_dat_cancel_freev() restores freed virtual block numbers specified by
+ * @vblocknrs and @nitems from the shadow mapping.
+ */
+void nilfs_dat_cancel_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
+{
+ unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
+ unsigned int entry_size = NILFS_MDT(dat)->mi_entry_size;
+ struct buffer_head *entry_bh = NULL, *saved_bh = NULL;
+ struct nilfs_palloc_req req;
+ __u64 start_vblocknr = 0, end_vblocknr = 0;
+ size_t i;
+
+ for (i = 0; i < nitems; i++) {
+ size_t offset;
+ int ret;
+
+ req.pr_entry_nr = vblocknrs[i];
+ ret = nilfs_palloc_prepare_alloc_entry(dat, &req, false);
+ if (ret < 0)
+ continue;
+
+ if (req.pr_entry_nr != vblocknrs[i])
+ goto abort_alloc_entry;
+
+ if (!saved_bh || vblocknrs[i] < start_vblocknr ||
+ vblocknrs[i] >= end_vblocknr) {
+ u64 rem;
+
+ brelse(saved_bh);
+ saved_bh = NULL;
+ brelse(entry_bh);
+ entry_bh = NULL;
+
+ ret = nilfs_dat_prepare_entry(dat, &req, true);
+ if (ret < 0)
+ goto abort_alloc_entry;
+
+ entry_bh = req.pr_entry_bh;
+
+ saved_bh = nilfs_mdt_get_frozen_buffer(dat, entry_bh);
+ if (!saved_bh) {
+ brelse(entry_bh);
+ entry_bh = NULL;
+ goto abort_alloc_entry;
+ }
+
+ div64_u64_rem(vblocknrs[i], entries_per_block, &rem);
+ start_vblocknr = vblocknrs[i] - rem;
+ end_vblocknr = start_vblocknr + entries_per_block;
+ }
+
+ offset = nilfs_palloc_entry_offset(dat, vblocknrs[i], entry_bh);
+ memcpy_folio(entry_bh->b_folio, offset, saved_bh->b_folio,
+ offset, entry_size);
+ mark_buffer_dirty(entry_bh);
+ nilfs_palloc_commit_alloc_entry(dat, &req);
+ continue;
+
+abort_alloc_entry:
+ nilfs_palloc_abort_alloc_entry(dat, &req);
+ }
+
+ brelse(saved_bh);
+ brelse(entry_bh);
+}
+
/**
* nilfs_dat_move - change a block number
* @dat: DAT file inode
diff --git a/fs/nilfs2/dat.h b/fs/nilfs2/dat.h
index 4f67f31f928b..8e1e52aa9cf7 100644
--- a/fs/nilfs2/dat.h
+++ b/fs/nilfs2/dat.h
@@ -38,6 +38,7 @@ void nilfs_dat_abort_update(struct inode *, struct nilfs_palloc_req *,
int nilfs_dat_mark_dirty(struct inode *, __u64);
int nilfs_dat_freev(struct inode *, __u64 *, size_t);
+void nilfs_dat_cancel_freev(struct inode *dat, __u64 *vblocknrs, size_t items);
int nilfs_dat_move(struct inode *, __u64, sector_t);
void nilfs_dat_undo_move(struct inode *dat, __u64 vblocknr);
ssize_t nilfs_dat_get_vinfo(struct inode *, void *, unsigned int, size_t);
diff --git a/fs/nilfs2/segment.c b/fs/nilfs2/segment.c
index 263f9ff00193..f3c12c14c750 100644
--- a/fs/nilfs2/segment.c
+++ b/fs/nilfs2/segment.c
@@ -1909,8 +1909,12 @@ static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
if (list_empty(&logs))
return; /* if the first segment buffer preparation failed */
- if (nilfs_doing_gc())
+ if (nilfs_doing_gc()) {
nilfs_cancel_block_relocation(&logs);
+ nilfs_dat_cancel_freev(nilfs->ns_dat, sci->sc_freevblks,
+ sci->sc_nfreevblks);
+ }
+
nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
nilfs_free_incomplete_logs(&logs, nilfs);
@@ -2605,6 +2609,8 @@ int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
sci->sc_freesegs = kbufs[4];
sci->sc_nfreesegs = argv[4].v_nmembs;
+ sci->sc_freevblks = kbufs[2];
+ sci->sc_nfreevblks = argv[2].v_nmembs;
sci->sc_subuf = subuf;
list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
@@ -2636,6 +2642,8 @@ int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
out_unlock:
sci->sc_subuf = NULL;
+ sci->sc_freevblks = NULL;
+ sci->sc_nfreevblks = 0;
sci->sc_freesegs = NULL;
sci->sc_nfreesegs = 0;
list_splice_tail_init(&sci->sc_gc_inodes, &nilfs->ns_gc_inodes);
diff --git a/fs/nilfs2/segment.h b/fs/nilfs2/segment.h
index e67afc14a82c..711c69e24575 100644
--- a/fs/nilfs2/segment.h
+++ b/fs/nilfs2/segment.h
@@ -83,7 +83,9 @@ struct nilfs_segsum_pointer {
* @sc_iput_queue: list of inodes for which iput should be done
* @sc_iput_work: work struct to defer iput call
* @sc_freesegs: array of segment numbers to be freed
+ * @sc_freevblks: array of virtual block numbers to be freed
* @sc_nfreesegs: number of segments on @sc_freesegs
+ * @sc_nfreevblks: number of virtual block numbers on @sc_freevblks
* @sc_subuf: buffer used to save segment entries
* @sc_dsync_inode: inode whose data pages are written for a sync operation
* @sc_dsync_start: start byte offset of data pages
@@ -129,7 +131,9 @@ struct nilfs_sc_info {
struct work_struct sc_iput_work;
__u64 *sc_freesegs;
+ __u64 *sc_freevblks;
size_t sc_nfreesegs;
+ size_t sc_nfreevblks;
void *sc_subuf;
struct nilfs_inode_info *sc_dsync_inode;
--
2.53.0