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 2/8] nilfs2: cancel the relocation of GC-targeted blocks if log writing fails
Date: Fri, 27 Nov 2026 13:40:55 +0900	[thread overview]
Message-ID: <20261127044231.37630-3-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.

When log writing fails, dirty metadata and data buffers for regular
inodes remain dirty in memory, so retrying the log write causes no
inconsistency.  However, for blocks relocated by garbage collection,
their physical block mappings in the DAT have already been updated to
the new locations during log writing.  If GC aborts on error and
discards its cached GC inodes without reverting these DAT mappings,
subsequent data reads or B-tree lookups will end up reading unwritten
block locations.

To allow safely bailing out of the GC loop on error, add a mechanism to
cancel the relocation of GC-targeted blocks and partially restore the
DAT when log writing fails in a GC context.  Specifically, introduce
nilfs_dat_undo_move() to restore original physical block numbers from
the saved buffers in the DAT shadow mapping, expose it through a new
bmap operation bop_undo_assign and nilfs_bmap_undo_assign() (set only
for GC inodes), and invoke nilfs_cancel_block_relocation() from
nilfs_segctor_abort_construction() when nilfs_doing_gc() is true.

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/bmap.h    | 23 ++++++++++++++++++
 fs/nilfs2/btree.c   |  8 +++++++
 fs/nilfs2/dat.c     | 58 +++++++++++++++++++++++++++++++++++++++++++++
 fs/nilfs2/dat.h     |  1 +
 fs/nilfs2/direct.c  |  1 +
 fs/nilfs2/segment.c | 35 +++++++++++++++++++++++++++
 6 files changed, 126 insertions(+)

diff --git a/fs/nilfs2/bmap.h b/fs/nilfs2/bmap.h
index a72f3c308a5d..6493217b1a7f 100644
--- a/fs/nilfs2/bmap.h
+++ b/fs/nilfs2/bmap.h
@@ -53,6 +53,7 @@ struct nilfs_bmap_stats {
  *                            mapping root
  * @bop_lookup_dirty_buffers: operation to collect dirty block buffers
  * @bop_assign:               disk block address assignment operation
+ * @bop_undo_assign:          undo disk block address assignment operation
  * @bop_mark:                 operation to mark in-use blocks as dirty for
  *                            relocation by GC
  * @bop_seek_key:             find valid block key operation
@@ -74,6 +75,8 @@ struct nilfs_bmap_operations {
 			  struct buffer_head **,
 			  sector_t,
 			  union nilfs_binfo *);
+	void (*bop_undo_assign)(struct nilfs_bmap *bmap,
+				struct buffer_head *bh);
 	int (*bop_mark)(struct nilfs_bmap *, __u64, int);
 
 	int (*bop_seek_key)(const struct nilfs_bmap *, __u64, __u64 *);
@@ -182,6 +185,26 @@ static inline int nilfs_bmap_lookup(struct nilfs_bmap *bmap, __u64 key,
 	return nilfs_bmap_lookup_at_level(bmap, key, 1, ptr);
 }
 
+/**
+ * nilfs_bmap_undo_assign - restore the block number to its original position
+ * @bmap:    bmap
+ * @bh:      buffer head for which the block number should be undone
+ *
+ * nilfs_bmap_undo_assign() undoes the block relocation performed during
+ * garbage collection for the buffer @bh and restores its original block
+ * position.  For regular bmaps where bop_undo_assign is %NULL, this
+ * function is a no-op.
+ */
+static inline void nilfs_bmap_undo_assign(struct nilfs_bmap *bmap,
+					struct buffer_head *bh)
+{
+	if (bmap->b_ops->bop_undo_assign) {
+		down_write(&bmap->b_sem);
+		bmap->b_ops->bop_undo_assign(bmap, bh);
+		up_write(&bmap->b_sem);
+	}
+}
+
 /*
  * Internal use only
  */
diff --git a/fs/nilfs2/btree.c b/fs/nilfs2/btree.c
index 6b8332e8c0db..8c12b78c0e9d 100644
--- a/fs/nilfs2/btree.c
+++ b/fs/nilfs2/btree.c
@@ -2378,6 +2378,12 @@ static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
 	return 0;
 }
 
+static void nilfs_btree_undo_assign_gc(struct nilfs_bmap *btree,
+				struct buffer_head *bh)
+{
+	nilfs_dat_undo_move(nilfs_bmap_get_dat(btree), bh->b_blocknr);
+}
+
 static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
 {
 	struct buffer_head *bh;
@@ -2423,6 +2429,7 @@ static const struct nilfs_bmap_operations nilfs_btree_ops = {
 	.bop_lookup_dirty_buffers =	nilfs_btree_lookup_dirty_buffers,
 
 	.bop_assign		=	nilfs_btree_assign,
+	.bop_undo_assign	=	NULL,
 	.bop_mark		=	nilfs_btree_mark,
 
 	.bop_seek_key		=	nilfs_btree_seek_key,
@@ -2445,6 +2452,7 @@ static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
 	.bop_lookup_dirty_buffers =	nilfs_btree_lookup_dirty_buffers,
 
 	.bop_assign		=	nilfs_btree_assign_gc,
+	.bop_undo_assign	=	nilfs_btree_undo_assign_gc,
 	.bop_mark		=	NULL,
 
 	.bop_seek_key		=	NULL,
diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
index 888dc1831c86..d2718944368b 100644
--- a/fs/nilfs2/dat.c
+++ b/fs/nilfs2/dat.c
@@ -379,6 +379,64 @@ int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
 	return 0;
 }
 
+/**
+ * nilfs_dat_undo_move - undo the change to the block number
+ * @dat:      DAT file inode
+ * @vblocknr: virtual block number
+ *
+ * nilfs_dat_undo_move() restores the original physical block number for
+ * @vblocknr from the frozen DAT entry buffer in the shadow mapping, if such
+ * a saved buffer exists and is marked uptodate; otherwise, it does nothing.
+ */
+void nilfs_dat_undo_move(struct inode *dat, __u64 vblocknr)
+{
+	struct buffer_head *entry_bh, *saved_bh;
+	int ret;
+
+	if (WARN_ON_ONCE(!nilfs_doing_gc()))
+		return;
+
+	ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
+	if (ret < 0) {
+		if (unlikely(ret != -ENOENT)) {
+			nilfs_warn(dat->i_sb,
+				"%s: error %d loading the entry block for vblocknr = %llu",
+				__func__, ret, (unsigned long long)vblocknr);
+		}
+		return;
+	}
+
+	if (!buffer_nilfs_redirected(entry_bh))
+		goto release_entry_bh;
+
+	saved_bh = nilfs_mdt_get_frozen_buffer(dat, entry_bh);
+	if (!saved_bh)
+		goto release_entry_bh;
+
+	if (likely(buffer_uptodate(saved_bh))) {
+		struct nilfs_dat_entry *entry, *saved_entry;
+		__le64 cur_blocknr, orig_blocknr;
+		size_t offset;
+
+		offset = nilfs_palloc_entry_offset(dat, vblocknr, entry_bh);
+		entry = kmap_local_folio(entry_bh->b_folio, offset);
+		saved_entry = kmap_local_folio(saved_bh->b_folio, offset);
+		cur_blocknr = entry->de_blocknr;
+		entry->de_blocknr = orig_blocknr = saved_entry->de_blocknr;
+		kunmap_local(saved_entry);
+		kunmap_local(entry);
+		if (cur_blocknr != orig_blocknr) {
+			mark_buffer_dirty(entry_bh);
+			nilfs_mdt_mark_dirty(dat);
+		}
+	}
+
+	brelse(saved_bh);
+
+release_entry_bh:
+	brelse(entry_bh);
+}
+
 /**
  * nilfs_dat_translate - translate a virtual block number to a block number
  * @dat: DAT file inode
diff --git a/fs/nilfs2/dat.h b/fs/nilfs2/dat.h
index 468c82d26183..4f67f31f928b 100644
--- a/fs/nilfs2/dat.h
+++ b/fs/nilfs2/dat.h
@@ -39,6 +39,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);
 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);
 
 int nilfs_dat_read(struct super_block *sb, size_t entry_size,
diff --git a/fs/nilfs2/direct.c b/fs/nilfs2/direct.c
index 64da677dc8d4..18a700db76ed 100644
--- a/fs/nilfs2/direct.c
+++ b/fs/nilfs2/direct.c
@@ -375,6 +375,7 @@ static const struct nilfs_bmap_operations nilfs_direct_ops = {
 	.bop_lookup_dirty_buffers	=	NULL,
 
 	.bop_assign		=	nilfs_direct_assign,
+	.bop_undo_assign	=	NULL,
 	.bop_mark		=	NULL,
 
 	.bop_seek_key		=	nilfs_direct_seek_key,
diff --git a/fs/nilfs2/segment.c b/fs/nilfs2/segment.c
index 4c2d51f6550c..24f8fa8cf864 100644
--- a/fs/nilfs2/segment.c
+++ b/fs/nilfs2/segment.c
@@ -1860,6 +1860,39 @@ static void nilfs_abort_logs(struct list_head *logs, int err)
 	nilfs_end_folio_io(fs_folio, err);
 }
 
+/**
+ * nilfs_cancel_block_relocation - cancel relocation of GC-targeted blocks
+ * @logs: list of segment buffers whose log write failed or was aborted
+ *
+ * nilfs_cancel_block_relocation() iterates through the payload buffers of
+ * each segment buffer in @logs (excluding the super root block) and calls
+ * nilfs_bmap_undo_assign() to revert the DAT mappings of blocks relocated
+ * by garbage collection.  This is necessary to prevent subsequent data reads
+ * or B-tree lookups from accessing unwritten block locations via the DAT
+ * after GC aborts on error and discards the cached GC inodes.
+ */
+static void nilfs_cancel_block_relocation(struct list_head *logs)
+{
+	struct nilfs_segment_buffer *segbuf;
+	struct buffer_head *bh;
+	struct inode *inode;
+
+	list_for_each_entry(segbuf, logs, sb_list) {
+		list_for_each_entry(bh, &segbuf->sb_payload_buffers,
+				    b_assoc_buffers) {
+			if (bh == segbuf->sb_super_root)
+				break;
+			inode = bh->b_folio->mapping->host;
+			/*
+			 * NILFS_I(inode)->i_bmap points to the primary inode's
+			 * bmap structure, whether the inode serves as a cache
+			 * for B-tree nodes or as the primary inode itself.
+			 */
+			nilfs_bmap_undo_assign(NILFS_I(inode)->i_bmap, bh);
+		}
+	}
+}
+
 static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
 					     struct the_nilfs *nilfs, int err)
 {
@@ -1874,6 +1907,8 @@ 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())
+		nilfs_cancel_block_relocation(&logs);
 	nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
 	nilfs_free_incomplete_logs(&logs, nilfs);
 
-- 
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 ` Ryusuke Konishi [this message]
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 ` [PATCH 6/8] nilfs2: restore removed DAT entries when aborting GC Ryusuke Konishi
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-3-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®