[PATCH 5/8] nilfs2: save DAT entry blocks to shadow mapping before freeing

From: Ryusuke Konishi

Date: Thu Nov 26 2026 - 23:43:02 EST


In preparation for restoring removed DAT entries when GC is aborted,
modify nilfs_dat_freev() to freeze the buffer heads of target DAT entry
blocks before freeing virtual block numbers, saving their original
contents in the shadow mapping.

Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>
---
fs/nilfs2/dat.c | 40 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
index d2718944368b..b4156945e433 100644
--- a/fs/nilfs2/dat.c
+++ b/fs/nilfs2/dat.c
@@ -11,6 +11,7 @@
#include <linux/buffer_head.h>
#include <linux/string.h>
#include <linux/errno.h>
+#include <linux/math64.h>
#include "nilfs.h"
#include "mdt.h"
#include "alloc.h"
@@ -306,11 +307,48 @@ int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
* Return: 0 on success, or one of the following negative error codes on
* failure:
* * %-EIO - I/O error (including metadata corruption).
- * * %-ENOENT - The virtual block number have not been allocated.
+ * * %-ENOENT - An unallocated management block was found.
* * %-ENOMEM - Insufficient memory available.
*/
int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
{
+ unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
+ struct buffer_head *entry_bh = NULL;
+ __u64 start_vblocknr = 0, end_vblocknr = 0;
+ u64 rem;
+ size_t i;
+ int ret;
+
+ /* Save the DAT entries to the shadow mapping before removal */
+ for (i = 0; i < nitems; i++) {
+ if (entry_bh && vblocknrs[i] >= start_vblocknr &&
+ vblocknrs[i] < end_vblocknr)
+ continue;
+
+ brelse(entry_bh);
+ entry_bh = NULL;
+
+ ret = nilfs_palloc_get_entry_block(dat, vblocknrs[i], 0,
+ &entry_bh);
+ if (unlikely(ret < 0)) {
+ if (ret != -ENOENT)
+ return ret;
+ continue;
+ }
+ if (!buffer_nilfs_redirected(entry_bh)) {
+ ret = nilfs_mdt_freeze_buffer(dat, entry_bh);
+ if (unlikely(ret < 0)) {
+ brelse(entry_bh);
+ return ret;
+ }
+ }
+
+ div64_u64_rem(vblocknrs[i], entries_per_block, &rem);
+ start_vblocknr = vblocknrs[i] - rem;
+ end_vblocknr = start_vblocknr + entries_per_block;
+ }
+ brelse(entry_bh);
+
return nilfs_palloc_freev(dat, vblocknrs, nitems);
}

--
2.53.0