Skip to contentSkip to conversation

f2fs-tools: avoid meaningless repetition in migrate_block

PATCH New LongPing Wei via Linux-f2fs-devel 1 participant 1 message Dec 11, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 f2fs-tools: avoid meaningless repetition in migrate_block

1. Use the same buffer in the same call cycle of f2fs_defragment;
2. Pass se and offset as arguments to the migrate_block;

---
v2: avoid memory leak in v1

---
 fsck/defrag.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
fsck/defrag.c +14 -14
index 9889b70..791f435 100644
@@ -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;
 }
Signature · 8 lines
2.34.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel