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 7/8] nilfs2: free virtual block numbers just before collecting DAT changes
Date: Fri, 27 Nov 2026 13:41:00 +0900 [thread overview]
Message-ID: <20261127044231.37630-8-konishi.ryusuke@gmail.com> (raw)
In-Reply-To: <20261127044231.37630-1-konishi.ryusuke@gmail.com>
During garbage collection, obsolete virtual block numbers were previously
freed from the DAT file at the beginning of the GC ioctl in
nilfs_ioctl_prepare_clean_segments(). Because this occurred before
collecting dirty blocks for the new log, those freed DAT entries could
theoretically be reallocated during log construction, making it
impossible to cancel the deallocation and recover consistent state if
construction is aborted.
Move the deallocation via nilfs_dat_freev() to the NILFS_ST_DAT stage
in nilfs_segctor_collect_blocks(), right before collecting dirty DAT
blocks, where freeing DAT entries cannot cause virtual block
reallocation. Also extend nilfs_palloc_freev() and nilfs_dat_freev()
to report the number of processed entries, and track the deallocation
state with NILFS_CF_VBKFREED so freed DAT entries are reliably restored
on abort.
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/alloc.c | 14 ++++++++++----
fs/nilfs2/alloc.h | 3 ++-
fs/nilfs2/dat.c | 15 +++++++++++----
fs/nilfs2/dat.h | 3 ++-
fs/nilfs2/ioctl.c | 36 +-----------------------------------
fs/nilfs2/segment.c | 29 +++++++++++++++++++++++++----
6 files changed, 51 insertions(+), 49 deletions(-)
diff --git a/fs/nilfs2/alloc.c b/fs/nilfs2/alloc.c
index 7b1cd2baefcf..bc67b211ac55 100644
--- a/fs/nilfs2/alloc.c
+++ b/fs/nilfs2/alloc.c
@@ -815,10 +815,12 @@ void nilfs_palloc_abort_free_entry(struct inode *inode,
* @inode: inode of metadata file using this allocator
* @entry_nrs: array of entry numbers to be deallocated
* @nitems: number of entries stored in @entry_nrs
+ * @ndone: place to store the number of process entries
*
* Return: 0 on success, or a negative error code on failure.
*/
-int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
+int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems,
+ size_t *ndone)
{
struct buffer_head *desc_bh, *bitmap_bh;
struct nilfs_palloc_group_desc *desc;
@@ -840,12 +842,12 @@ int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
if (ret < 0)
- return ret;
+ goto fail;
ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
&bitmap_bh);
if (ret < 0) {
brelse(desc_bh);
- return ret;
+ goto fail;
}
/* Get the first entry number of the group */
@@ -927,7 +929,11 @@ int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
ret, group, inode->i_ino);
}
}
- return 0;
+ ret = 0;
+fail:
+ if (ndone)
+ *ndone = i;
+ return ret;
}
void nilfs_palloc_setup_cache(struct inode *inode,
diff --git a/fs/nilfs2/alloc.h b/fs/nilfs2/alloc.h
index 046d876ea3e0..0028d74c694d 100644
--- a/fs/nilfs2/alloc.h
+++ b/fs/nilfs2/alloc.h
@@ -60,7 +60,8 @@ void nilfs_palloc_abort_alloc_entry(struct inode *, struct nilfs_palloc_req *);
void nilfs_palloc_commit_free_entry(struct inode *, struct nilfs_palloc_req *);
int nilfs_palloc_prepare_free_entry(struct inode *, struct nilfs_palloc_req *);
void nilfs_palloc_abort_free_entry(struct inode *, struct nilfs_palloc_req *);
-int nilfs_palloc_freev(struct inode *, __u64 *, size_t);
+int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems,
+ size_t *ndone);
#define nilfs_set_bit_atomic ext2_set_bit_atomic
#define nilfs_clear_bit_atomic ext2_clear_bit_atomic
diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
index 94d358d9d5b0..515f4243c7b5 100644
--- a/fs/nilfs2/dat.c
+++ b/fs/nilfs2/dat.c
@@ -300,6 +300,7 @@ int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
* @dat: DAT file inode
* @vblocknrs: array of virtual block numbers
* @nitems: number of virtual block numbers
+ * @ndone: location to store the number of freed virtual block numbers
*
* Description: nilfs_dat_freev() frees the virtual block numbers specified by
* @vblocknrs and @nitems.
@@ -310,7 +311,8 @@ int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
* * %-ENOENT - An unallocated management block was found.
* * %-ENOMEM - Insufficient memory available.
*/
-int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
+int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems,
+ size_t *ndone)
{
unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
struct buffer_head *entry_bh = NULL;
@@ -332,14 +334,14 @@ int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
&entry_bh);
if (unlikely(ret < 0)) {
if (ret != -ENOENT)
- return ret;
+ goto failed;
continue;
}
if (!buffer_nilfs_redirected(entry_bh)) {
ret = nilfs_mdt_freeze_buffer(dat, entry_bh);
if (unlikely(ret < 0)) {
brelse(entry_bh);
- return ret;
+ goto failed;
}
}
@@ -349,7 +351,12 @@ int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
}
brelse(entry_bh);
- return nilfs_palloc_freev(dat, vblocknrs, nitems);
+ return nilfs_palloc_freev(dat, vblocknrs, nitems, ndone);
+
+failed:
+ if (ndone)
+ *ndone = 0;
+ return ret;
}
/**
diff --git a/fs/nilfs2/dat.h b/fs/nilfs2/dat.h
index 8e1e52aa9cf7..9eb17ef2bb63 100644
--- a/fs/nilfs2/dat.h
+++ b/fs/nilfs2/dat.h
@@ -37,7 +37,8 @@ void nilfs_dat_abort_update(struct inode *, struct nilfs_palloc_req *,
struct nilfs_palloc_req *);
int nilfs_dat_mark_dirty(struct inode *, __u64);
-int nilfs_dat_freev(struct inode *, __u64 *, size_t);
+int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t items,
+ size_t *ndone);
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);
diff --git a/fs/nilfs2/ioctl.c b/fs/nilfs2/ioctl.c
index 686ac53498bc..a46dad335ee5 100644
--- a/fs/nilfs2/ioctl.c
+++ b/fs/nilfs2/ioctl.c
@@ -701,32 +701,6 @@ static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
return nmembs;
}
-/**
- * nilfs_ioctl_free_vblocknrs - free virtual block numbers
- * @nilfs: nilfs object
- * @argv: vector of arguments from userspace
- * @buf: array of virtual block numbers
- *
- * Description: nilfs_ioctl_free_vblocknrs() function frees
- * the virtual block numbers specified by @buf and @argv->v_nmembs.
- *
- * Return: Number of processed virtual block numbers on success, or one of the
- * following negative error codes on failure:
- * * %-EIO - I/O error.
- * * %-ENOENT - Unallocated virtual block number.
- * * %-ENOMEM - Insufficient memory available.
- */
-static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
- struct nilfs_argv *argv, void *buf)
-{
- size_t nmembs = argv->v_nmembs;
- int ret;
-
- ret = nilfs_dat_freev(nilfs->ns_dat, buf, nmembs);
-
- return (ret < 0) ? ret : nmembs;
-}
-
/**
* nilfs_ioctl_mark_blocks_dirty - mark blocks dirty
* @nilfs: nilfs object
@@ -809,15 +783,7 @@ int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
msg = "cannot delete checkpoints";
goto failed;
}
- ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
- if (ret < 0) {
- /*
- * can safely abort because DAT file is updated atomically
- * using a copy-on-write technique.
- */
- msg = "cannot delete virtual blocks from DAT file";
- goto failed;
- }
+
ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
if (ret < 0) {
/*
diff --git a/fs/nilfs2/segment.c b/fs/nilfs2/segment.c
index f3c12c14c750..b306f09c9497 100644
--- a/fs/nilfs2/segment.c
+++ b/fs/nilfs2/segment.c
@@ -107,8 +107,10 @@ static inline int nilfs_sc_cstage_get(struct nilfs_sc_info *sci)
/* State flags of collection */
#define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
#define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
-#define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
-#define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
+#define NILFS_CF_SUFREED 0x0004 /* segment usages have been freed */
+#define NILFS_CF_VBKFREED 0x0008 /* DAT entries have been freed */
+#define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED | \
+ NILFS_CF_VBKFREED)
/* Operations depending on the construction mode and file type */
struct nilfs_sc_operations {
@@ -1271,6 +1273,18 @@ static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
nilfs_sc_cstage_inc(sci);
fallthrough;
case NILFS_ST_DAT:
+ if (sci->sc_nfreevblks > 0) {
+ size_t ndone;
+
+ err = nilfs_dat_freev(nilfs->ns_dat, sci->sc_freevblks,
+ sci->sc_nfreevblks, &ndone);
+ if (unlikely(err)) {
+ nilfs_dat_cancel_freev(nilfs->ns_dat,
+ sci->sc_freevblks, ndone);
+ break;
+ }
+ sci->sc_stage.flags |= NILFS_CF_VBKFREED;
+ }
dat_stage:
err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
&nilfs_sc_dat_ops);
@@ -1563,6 +1577,12 @@ static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
nilfs_clear_logs(&sci->sc_segbufs);
+ if (sci->sc_stage.flags & NILFS_CF_VBKFREED) {
+ nilfs_dat_cancel_freev(nilfs->ns_dat,
+ sci->sc_freevblks, sci->sc_nfreevblks);
+ sci->sc_stage.flags &= ~NILFS_CF_VBKFREED;
+ }
+
if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
sci->sc_freesegs,
@@ -1909,11 +1929,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);
+
+ if (sci->sc_stage.flags & NILFS_CF_VBKFREED)
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);
--
2.53.0
next prev 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 ` [PATCH 6/8] nilfs2: restore removed DAT entries when aborting GC Ryusuke Konishi
2026-11-27 4:41 ` Ryusuke Konishi [this message]
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-8-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®