diff mbox series

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

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

Commit Message

LongPing Wei Dec. 11, 2024, 6:58 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>
---
v2: avoid memory leak in v1
---
 fsck/defrag.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/fsck/defrag.c b/fsck/defrag.c
index 9889b70..791f435 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,10 @@  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);
+	int ret = 0;
+
+	ASSERT(raw != NULL);
 
 	/* flush NAT/SIT journal entries */
 	flush_journal_entries(sbi);
@@ -80,12 +76,14 @@  int f2fs_defragment(struct f2fs_sb_info *sbi, u64 from, u64 len, u64 to, int lef
 
 		if (find_next_free_block(sbi, &target, left, se->type, false)) {
 			MSG(0, "Not enough space to migrate blocks");
-			return -1;
+			ret = -1;
+			goto out_err;
 		}
 
-		if (migrate_block(sbi, idx, target)) {
+		if (migrate_block(sbi, idx, target, raw, se, offset)) {
 			ASSERT_MSG("Found inconsistency: please run FSCK");
-			return -1;
+			ret = -1;
+			goto out_err;
 		}
 	}
 
@@ -99,5 +97,7 @@  int f2fs_defragment(struct f2fs_sb_info *sbi, u64 from, u64 len, u64 to, int lef
 
 	write_checkpoint(sbi);
 
-	return 0;
+out_err:
+	free(raw);
+	return ret;
 }