diff mbox series

[f2fs-dev] f2fs-tools: synchronize nat journal with nat cache

Message ID 20231108172922.1325972-1-jaegeuk@kernel.org (mailing list archive)
State New
Headers show
Series [f2fs-dev] f2fs-tools: synchronize nat journal with nat cache | expand

Commit Message

Jaegeuk Kim Nov. 8, 2023, 5:29 p.m. UTC
From: Daeho Jeong <daehojeong@google.com>

Make nat journal and nat cache have the same content.

Signed-off-by: Daeho Jeong <daehojeong@google.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 fsck/fsck.c  |  1 -
 fsck/fsck.h  |  2 --
 fsck/mount.c | 54 ++++++++++++++++++++++++++++------------------------
 3 files changed, 29 insertions(+), 28 deletions(-)
diff mbox series

Patch

diff --git a/fsck/fsck.c b/fsck/fsck.c
index c97dceb406b3..55eddcaa21da 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -735,7 +735,6 @@  fix:
 		struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
 
 		FIX_MSG("Relink root inode, blkaddr: 0x%x", last_blkaddr);
-		update_nat_journal_blkaddr(sbi, root_ino, last_blkaddr);
 		update_nat_blkaddr(sbi, root_ino, root_ino, last_blkaddr);
 
 		if (f2fs_test_bit(root_ino, fsck->nat_area_bitmap))
diff --git a/fsck/fsck.h b/fsck/fsck.h
index c85a0741b257..f6f15e7781b8 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -205,8 +205,6 @@  extern void update_sum_entry(struct f2fs_sb_info *, block_t,
 				struct f2fs_summary *);
 extern void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
 extern void nullify_nat_entry(struct f2fs_sb_info *, u32);
-extern void update_nat_journal_blkaddr(struct f2fs_sb_info *sbi, u32 nid,
-					block_t blkaddr);
 extern void rewrite_sit_area_bitmap(struct f2fs_sb_info *);
 extern void build_nat_area_bitmap(struct f2fs_sb_info *);
 extern void build_sit_area_bitmap(struct f2fs_sb_info *);
diff --git a/fsck/mount.c b/fsck/mount.c
index 7871f2f48c81..72516f403734 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2479,10 +2479,25 @@  void update_data_blkaddr(struct f2fs_sb_info *sbi, nid_t nid,
 void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t ino,
 					nid_t nid, block_t newaddr)
 {
-	struct f2fs_nat_block *nat_block;
+	struct f2fs_nat_block *nat_block = NULL;
+	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
+	struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
+	struct f2fs_nat_entry *entry;
 	pgoff_t block_addr;
 	int entry_off;
-	int ret;
+	int ret, i;
+
+	for (i = 0; i < nats_in_cursum(journal); i++) {
+		if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
+			entry = &nat_in_journal(journal, i);
+			entry->block_addr = cpu_to_le32(newaddr);
+			if (ino)
+				entry->ino = cpu_to_le32(ino);
+			MSG(0, "update nat(nid:%d) blkaddr [0x%x] in journal\n",
+							nid, newaddr);
+			goto update_cache;
+		}
+	}
 
 	nat_block = (struct f2fs_nat_block *)calloc(F2FS_BLKSIZE, 1);
 	ASSERT(nat_block);
@@ -2493,15 +2508,19 @@  void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t ino,
 	ret = dev_read_block(nat_block, block_addr);
 	ASSERT(ret >= 0);
 
+	entry = &nat_block->entries[entry_off];
 	if (ino)
-		nat_block->entries[entry_off].ino = cpu_to_le32(ino);
-	nat_block->entries[entry_off].block_addr = cpu_to_le32(newaddr);
-	if (c.func == FSCK)
-		F2FS_FSCK(sbi)->entries[nid] = nat_block->entries[entry_off];
+		entry->ino = cpu_to_le32(ino);
+	entry->block_addr = cpu_to_le32(newaddr);
 
 	ret = dev_write_block(nat_block, block_addr);
 	ASSERT(ret >= 0);
-	free(nat_block);
+update_cache:
+	if (c.func == FSCK)
+		F2FS_FSCK(sbi)->entries[nid] = *entry;
+
+	if (nat_block)
+		free(nat_block);
 }
 
 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
@@ -3120,6 +3139,9 @@  void nullify_nat_entry(struct f2fs_sb_info *sbi, u32 nid)
 	int ret;
 	int i = 0;
 
+	if (c.func == FSCK)
+		F2FS_FSCK(sbi)->entries[nid].block_addr = 0;
+
 	/* check in journal */
 	for (i = 0; i < nats_in_cursum(journal); i++) {
 		if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
@@ -3153,24 +3175,6 @@  void nullify_nat_entry(struct f2fs_sb_info *sbi, u32 nid)
 	free(nat_block);
 }
 
-void update_nat_journal_blkaddr(struct f2fs_sb_info *sbi, u32 nid,
-					block_t blkaddr)
-{
-	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
-	struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
-	int i;
-
-	for (i = 0; i < nats_in_cursum(journal); i++) {
-		if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
-			nat_in_journal(journal, i).block_addr =
-						cpu_to_le32(blkaddr);
-			MSG(0, "update nat(nid:%d) blkaddr [0x%x] in journal\n",
-							nid, blkaddr);
-			return;
-		}
-	}
-}
-
 void duplicate_checkpoint(struct f2fs_sb_info *sbi)
 {
 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);