mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ryusuke Konishi <konishi.ryusuke@gmail.com>
To: Viacheslav Dubeyko <slava@dubeyko.com>
Cc: Wang Jianjian <wangjianjian3@huawei.com>,
	linux-nilfs <linux-nilfs@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Zheng Zheng <fengaojigui88@gmail.com>,
	Joshua Crofts <joshua.crofts1@gmail.com>
Subject: [PATCH 6/8] nilfs2: restore removed DAT entries when aborting GC
Date: Fri, 27 Nov 2026 13:40:59 +0900	[thread overview]
Message-ID: <20261127044231.37630-7-konishi.ryusuke@gmail.com> (raw)
In-Reply-To: <20261127044231.37630-1-konishi.ryusuke@gmail.com>

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@vger.kernel.org
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
---
 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


  parent reply	other threads:[~2026-11-27  4:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  4:40 [PATCH 0/8] nilfs2: support GC error rollback and avoid infinite retry hangs Ryusuke Konishi
2026-11-27  4:40 ` [PATCH 1/8] nilfs2: fix leak of gc inodes on error Ryusuke Konishi
2026-11-27  4:40 ` [PATCH 2/8] nilfs2: cancel the relocation of GC-targeted blocks if log writing fails Ryusuke Konishi
2026-11-27  4:40 ` [PATCH 3/8] nilfs2: restore segment usage from the save buffer when aborting GC Ryusuke Konishi
2026-11-27  4:40 ` [PATCH 4/8] nilfs2: drop redirected buffer flag from DAT when clearing shadow map Ryusuke Konishi
2026-11-27  4:40 ` [PATCH 5/8] nilfs2: save DAT entry blocks to shadow mapping before freeing Ryusuke Konishi
2026-11-27  4:40 ` Ryusuke Konishi [this message]
2026-11-27  4:41 ` [PATCH 7/8] nilfs2: free virtual block numbers just before collecting DAT changes Ryusuke Konishi
2026-11-27  4:41 ` [PATCH 8/8] nilfs2: avoid infinite retry loop on GC failure Ryusuke Konishi
2026-11-27 18:13 ` [PATCH 0/8] nilfs2: support GC error rollback and avoid infinite retry hangs Viacheslav Dubeyko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127044231.37630-7-konishi.ryusuke@gmail.com \
    --to=konishi.ryusuke@gmail.com \
    --cc=fengaojigui88@gmail.com \
    --cc=joshua.crofts1@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nilfs@vger.kernel.org \
    --cc=slava@dubeyko.com \
    --cc=wangjianjian3@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®