diff mbox series

[f2fs-dev] f2fs-tools: avoid meaningless repetition in migrate_block

Message ID 20241211065545.4128914-1-weilongping@oppo.com (mailing list archive)
State Superseded
Headers show
Series [f2fs-dev] f2fs-tools: avoid meaningless repetition in migrate_block | expand

Commit Message

LongPing Wei Dec. 11, 2024, 6:55 a.m. UTC
1. Use the same buffer in the same call cycle of f2fs_defragment;
2. Pass se and offset as arguments to the migrate_block;

Signed-off-by: LongPing Wei <weilongping@oppo.com>
---
 fsck/defrag.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/fsck/defrag.c b/fsck/defrag.c
index 9889b70..7336f8f 100644
--- a/fsck/defrag.c
+++ b/fsck/defrag.c
@@ -9,28 +9,21 @@ 
  */
 #include "fsck.h"
 
-static int migrate_block(struct f2fs_sb_info *sbi, u64 from, u64 to)
+static int migrate_block(struct f2fs_sb_info *sbi, u64 from, u64 to,
+			 void *raw, struct seg_entry *se, u64 offset)
 {
-	void *raw = calloc(F2FS_BLKSIZE, 1);
-	struct seg_entry *se;
 	struct f2fs_summary sum;
-	u64 offset;
 	int ret, type;
 
-	ASSERT(raw != NULL);
-
 	/* read from */
 	ret = dev_read_block(raw, from);
 	ASSERT(ret >= 0);
 
-	/* get segment type */
-	se = get_seg_entry(sbi, GET_SEGNO(sbi, from));
 	/* write to */
 	ret = dev_write_block(raw, to, f2fs_io_type_to_rw_hint(se->type));
 	ASSERT(ret >= 0);
 
 	/* update sit bitmap & valid_blocks && se->type */
-	offset = OFFSET_IN_SEG(sbi, from);
 	type = se->type;
 	se->valid_blocks--;
 	f2fs_clear_bit(offset, (char *)se->cur_valid_map);
@@ -57,7 +50,6 @@  static int migrate_block(struct f2fs_sb_info *sbi, u64 from, u64 to)
 	DBG(1, "Migrate %s block %"PRIx64" -> %"PRIx64"\n",
 					IS_DATASEG(type) ? "data" : "node",
 					from, to);
-	free(raw);
 	return 0;
 }
 
@@ -65,6 +57,9 @@  int f2fs_defragment(struct f2fs_sb_info *sbi, u64 from, u64 len, u64 to, int lef
 {
 	struct seg_entry *se;
 	u64 idx, offset;
+	void *raw = calloc(F2FS_BLKSIZE, 1);
+
+	ASSERT(raw != NULL);
 
 	/* flush NAT/SIT journal entries */
 	flush_journal_entries(sbi);
@@ -83,7 +78,7 @@  int f2fs_defragment(struct f2fs_sb_info *sbi, u64 from, u64 len, u64 to, int lef
 			return -1;
 		}
 
-		if (migrate_block(sbi, idx, target)) {
+		if (migrate_block(sbi, idx, target, raw, se, offset)) {
 			ASSERT_MSG("Found inconsistency: please run FSCK");
 			return -1;
 		}
@@ -99,5 +94,6 @@  int f2fs_defragment(struct f2fs_sb_info *sbi, u64 from, u64 len, u64 to, int lef
 
 	write_checkpoint(sbi);
 
+	free(raw);
 	return 0;
 }