Skip to contentSkip to conversation

f2fs-tools: fix potential deadloop

PATCH New Chao Yu 1 participant 10 messages Aug 9, 2019

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH 1/10 f2fs-tools: fix potential deadloop

In error path of build_sit_info(), start variable is unsigned int type, it should never be less than zero, fix it.

build_sit_info()
{
...

	unsigned int start;
...
free_validity_maps:
	for (--start ; start >= 0; --start)
		free(sit_i->sentries[start].cur_valid_map);

...
}

---
 fsck/mount.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fsck/mount.c +2 -1
index fd89b8e..f97c4ea 100644
@@ -1401,7 +1401,8 @@ int build_sit_info(struct f2fs_sb_info *sbi)
 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
 	struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
 	struct sit_info *sit_i;
-	unsigned int sit_segs, start;
+	unsigned int sit_segs;
+	int start;
 	char *src_bitmap, *dst_bitmap;
 	unsigned int bitmap_size;
 
Signature · 8 lines
2.18.0.rc1



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

PATCH 2/10 f2fs-tools: fix to avoid memory leak of sit_i->sentries

We missed to free sit_i->sentries in f2fs_do_umount(), fix it.

---
 fsck/mount.c | 1 +
 1 file changed, 1 insertion(+)
fsck/mount.c +1 -0
index f97c4ea..af7149e 100644
@@ -2917,6 +2917,7 @@ void f2fs_do_umount(struct f2fs_sb_info *sbi)
 		free(sit_i->sentries[i].cur_valid_map);
 
 	free(sit_i->sit_bitmap);
+	free(sit_i->sentries);
 	free(sm_i->sit_info);
 
 	/* free sm_info */
Signature · 8 lines
2.18.0.rc1



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

PATCH 3/10 f2fs-tools: allocate memory in batch in build_sit_info()

In reply to Chao Yu

Like we did in kernel, allocating memory in batch will be more efficient.

---
 fsck/f2fs.h  |  1 +
 fsck/mount.c | 28 ++++++++++++++++------------
 2 files changed, 17 insertions(+), 12 deletions(-)
fsck/f2fs.h +1 -0
index a52b5d4..6fc0bf3 100644
@@ -82,6 +82,7 @@ struct sit_info {
 	block_t sit_base_addr;          /* start block address of SIT area */
 	block_t sit_blocks;             /* # of blocks used by SIT area */
 	block_t written_valid_blocks;   /* # of valid blocks in main area */
+	unsigned char *bitmap;		/* all bitmaps pointer */
 	char *sit_bitmap;               /* SIT bitmap pointer */
 	unsigned int bitmap_size;       /* SIT bitmap size */
 
fsck/mount.c +16 -12
index af7149e..bed22d5 100644
@@ -1404,6 +1404,7 @@ int build_sit_info(struct f2fs_sb_info *sbi)
 	unsigned int sit_segs;
 	int start;
 	char *src_bitmap, *dst_bitmap;
+	unsigned char *bitmap;
 	unsigned int bitmap_size;
 
 	sit_i = malloc(sizeof(struct sit_info));
@@ -1420,13 +1421,19 @@ int build_sit_info(struct f2fs_sb_info *sbi)
 		goto free_sit_info;
 	}
 
+	bitmap_size = TOTAL_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE;
+
+	sit_i->bitmap = calloc(bitmap_size, 1);
+	if (!sit_i->bitmap) {
+		MSG(1, "\tError: Calloc failed for build_sit_info!!\n");
+		goto free_sentries;
+	}
+
+	bitmap = sit_i->bitmap;
+
 	for (start = 0; start < TOTAL_SEGS(sbi); start++) {
-		sit_i->sentries[start].cur_valid_map
-			= calloc(SIT_VBLOCK_MAP_SIZE, 1);
-		if (!sit_i->sentries[start].cur_valid_map) {
-			MSG(1, "\tError: Calloc failed for build_sit_info!!\n");
-			goto free_validity_maps;
-		}
+		sit_i->sentries[start].cur_valid_map = bitmap;
+		bitmap += SIT_VBLOCK_MAP_SIZE;
 	}
 
 	sit_segs = get_sb(segment_count_sit) >> 1;
@@ -1452,10 +1459,9 @@ int build_sit_info(struct f2fs_sb_info *sbi)
 	return 0;
 
 free_validity_maps:
-	for (--start ; start >= 0; --start)
-		free(sit_i->sentries[start].cur_valid_map);
+	free(sit_i->bitmap);
+free_sentries:
 	free(sit_i->sentries);
-
 free_sit_info:
 	free(sit_i);
 
@@ -2913,9 +2919,7 @@ void f2fs_do_umount(struct f2fs_sb_info *sbi)
 	free(sbi->nm_info);
 
 	/* free sit_info */
-	for (i = 0; i < TOTAL_SEGS(sbi); i++)
-		free(sit_i->sentries[i].cur_valid_map);
-
+	free(sit_i->bitmap);
 	free(sit_i->sit_bitmap);
 	free(sit_i->sentries);
 	free(sm_i->sit_info);
Signature · 8 lines
2.18.0.rc1



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

PATCH 4/10 fsck.f2fs: introduce current_sit_addr() for cleanup

In reply to Chao Yu

Just clean up, no logic change.

---
 fsck/mount.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
fsck/mount.c +13 -15
index bed22d5..eed27bf 100644
@@ -1700,13 +1700,12 @@ static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
 	ASSERT(segno <= end_segno);
 }
 
-void get_current_sit_page(struct f2fs_sb_info *sbi,
-			unsigned int segno, struct f2fs_sit_block *sit_blk)
+static inline block_t current_sit_addr(struct f2fs_sb_info *sbi,
+						unsigned int segno)
 {
 	struct sit_info *sit_i = SIT_I(sbi);
 	unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
 	block_t blk_addr = sit_i->sit_base_addr + offset;
-	int ret;
 
 	check_seg_range(sbi, segno);
 
@@ -1714,24 +1713,23 @@ void get_current_sit_page(struct f2fs_sb_info *sbi,
 	if (f2fs_test_bit(offset, sit_i->sit_bitmap))
 		blk_addr += sit_i->sit_blocks;
 
-	ret = dev_read_block(sit_blk, blk_addr);
-	ASSERT(ret >= 0);
+	return blk_addr;
 }
 
-void rewrite_current_sit_page(struct f2fs_sb_info *sbi,
+void get_current_sit_page(struct f2fs_sb_info *sbi,
 			unsigned int segno, struct f2fs_sit_block *sit_blk)
 {
-	struct sit_info *sit_i = SIT_I(sbi);
-	unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
-	block_t blk_addr = sit_i->sit_base_addr + offset;
-	int ret;
+	block_t blk_addr = current_sit_addr(sbi, segno);
 
-	/* calculate sit block address */
-	if (f2fs_test_bit(offset, sit_i->sit_bitmap))
-		blk_addr += sit_i->sit_blocks;
+	ASSERT(dev_read_block(sit_blk, blk_addr) >= 0);
+}
 
-	ret = dev_write_block(sit_blk, blk_addr);
-	ASSERT(ret >= 0);
+void rewrite_current_sit_page(struct f2fs_sb_info *sbi,
+			unsigned int segno, struct f2fs_sit_block *sit_blk)
+{
+	block_t blk_addr = current_sit_addr(sbi, segno);
+
+	ASSERT(dev_write_block(sit_blk, blk_addr) >= 0);
 }
 
 void check_block_count(struct f2fs_sb_info *sbi,
Signature · 8 lines
2.18.0.rc1



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

PATCH 5/10 f2fs-tools: introduce f2fs_ra_meta_pages()

In reply to Chao Yu

Introduce f2fs_ra_meta_pages() to readahead meta pages like we did in kernel.

---
 fsck/f2fs.h       |  28 +++++++++++
 fsck/fsck.c       |   2 +
 fsck/fsck.h       |   1 +
 fsck/mount.c      | 119 ++++++++++++++++++++++++++++++++++++++++++----
 include/f2fs_fs.h |   5 ++
 5 files changed, 147 insertions(+), 8 deletions(-)
fsck/f2fs.h +28 -0
index 6fc0bf3..52b81ff 100644
@@ -37,6 +37,20 @@ struct list_head {
 	struct list_head *next, *prev;
 };
 
+/*
+ * indicate meta/data type
+ */
+enum {
+	META_CP,
+	META_NAT,
+	META_SIT,
+	META_SSA,
+	META_MAX,
+	META_POR,
+};
+
+#define MAX_RA_BLOCKS	64
+
 enum {
 	NAT_BITMAP,
 	SIT_BITMAP
@@ -329,6 +343,13 @@ static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
 	((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) ||           \
 	 (t == CURSEG_WARM_NODE))
 
+#define MAIN_BLKADDR(sbi)						\
+	(SM_I(sbi) ? SM_I(sbi)->main_blkaddr :				\
+		le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
+#define SEG0_BLKADDR(sbi)						\
+	(SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr :				\
+		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
+
 #define GET_SUM_BLKADDR(sbi, segno)					\
 	((sbi->sm_info->ssa_blkaddr) + segno)
 
@@ -345,9 +366,16 @@ static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
 	GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr)
 #define GET_R2L_SEGNO(sbi, segno)	(segno + FREE_I_START_SEGNO(sbi))
 
+#define MAIN_SEGS(sbi)	(SM_I(sbi)->main_segments)
+#define TOTAL_BLKS(sbi)	(TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
+#define MAX_BLKADDR(sbi)	(SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
+
 #define START_BLOCK(sbi, segno)	(SM_I(sbi)->main_blkaddr +		\
 	((segno) << sbi->log_blocks_per_seg))
 
+#define SIT_BLK_CNT(sbi)						\
+	((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
+
 static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
 {
 	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
fsck/fsck.c +2 -0
index e835a22..d53317c 100644
@@ -1665,6 +1665,8 @@ int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
 	start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
 	orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);
 
+	f2fs_ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP);
+
 	orphan_blk = calloc(BLOCK_SZ, 1);
 	ASSERT(orphan_blk);
 
fsck/fsck.h +1 -0
index aabe4a3..f7ddf50 100644
@@ -180,6 +180,7 @@ extern int f2fs_set_sit_bitmap(struct f2fs_sb_info *, u32);
 extern void fsck_init(struct f2fs_sb_info *);
 extern int fsck_verify(struct f2fs_sb_info *);
 extern void fsck_free(struct f2fs_sb_info *);
+extern int f2fs_ra_meta_pages(struct f2fs_sb_info *, block_t, int, int);
 extern int f2fs_do_mount(struct f2fs_sb_info *);
 extern void f2fs_do_umount(struct f2fs_sb_info *);
 extern int f2fs_sparse_initialize_meta(struct f2fs_sb_info *);
fsck/mount.c +111 -8

145 lines not on this page · raw message

include/f2fs_fs.h +5 -0
index 2dda901..942c8dc 100644
@@ -1186,6 +1186,11 @@ extern int f2fs_finalize_device(void);
 extern int f2fs_fsync_device(void);
 
 extern int dev_read(void *, __u64, size_t);
+#ifdef POSIX_FADV_WILLNEED
+extern int dev_readahead(__u64, size_t);
+#else
+extern int dev_readahead(__u64, size_t UNUSED(len));
+#endif
 extern int dev_write(void *, __u64, size_t);
 extern int dev_write_block(void *, __u64);
 extern int dev_write_dump(void *, __u64, size_t);
Signature · 8 lines
2.18.0.rc1



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

PATCH 6/10 dump.f2fs: introduce start_bidx_of_node() for cleanup

In reply to Chao Yu

Just cleanup, no logic change, besides, it can be reused by latter patch.

---
 fsck/dump.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)
fsck/dump.c +23 -16
index 390361d..a6a1635 100644
@@ -527,11 +527,31 @@ static void dump_node_from_blkaddr(struct f2fs_sb_info *sbi, u32 blk_addr)
 	free(node_blk);
 }
 
+unsigned int start_bidx_of_node(unsigned int node_ofs,
+					struct f2fs_node *node_blk)
+{
+	unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
+	unsigned int bidx;
+
+	if (node_ofs == 0)
+		return 0;
+
+	if (node_ofs <= 2) {
+		bidx = node_ofs - 1;
+	} else if (node_ofs <= indirect_blks) {
+		int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
+		bidx = node_ofs - 2 - dec;
+	} else {
+		int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
+		bidx = node_ofs - 5 - dec;
+	}
+	return bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE(&node_blk->i);
+}
+
 static void dump_data_offset(u32 blk_addr, int ofs_in_node)
 {
 	struct f2fs_node *node_blk;
-	unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
-	unsigned int bidx = 0;
+	unsigned int bidx;
 	unsigned int node_ofs;
 	int ret;
 
@@ -543,20 +563,7 @@ static void dump_data_offset(u32 blk_addr, int ofs_in_node)
 
 	node_ofs = ofs_of_node(node_blk);
 
-	if (node_ofs == 0)
-		goto got_it;
-
-	if (node_ofs > 0 && node_ofs <= 2) {
-		bidx = node_ofs - 1;
-	} else if (node_ofs <= indirect_blks) {
-		int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
-		bidx = node_ofs - 2 - dec;
-	} else {
-		int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
-		bidx = node_ofs - 5 - dec;
-	}
-	bidx = bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE(&node_blk->i);
-got_it:
+	bidx = start_bidx_of_node(node_ofs, node_blk);
 	bidx +=  ofs_in_node;
 
 	setlocale(LC_ALL, "");
Signature · 8 lines
2.18.0.rc1



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

PATCH 7/10 fsck.f2fs: fix to set large section type during allocation

In reply to Chao Yu

During block allocation in large free section, we need to change all sub segments' type in it, otherwise, we will fail to allocate block in non-first segment due to mismatch seg-type.

---
 fsck/mount.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
fsck/mount.c +18 -1
index 46fe1be..ad20d19 100644
@@ -2393,6 +2393,20 @@ int relocate_curseg_offset(struct f2fs_sb_info *sbi, int type)
 	return 0;
 }
 
+void set_section_type(struct f2fs_sb_info *sbi, unsigned int segno, int type)
+{
+	struct seg_entry *se;
+	unsigned int i;
+
+	if (sbi->segs_per_sec == 1)
+		return;
+
+	for (i = 0; i < sbi->segs_per_sec; i++) {
+		se = get_seg_entry(sbi, segno + i);
+		se->type = type;
+	}
+}
+
 int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left, int type)
 {
 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
@@ -2436,8 +2450,11 @@ int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left, int type)
 				if (se2->valid_blocks)
 					break;
 			}
-			if (i == sbi->segs_per_sec)
+
+			if (i == sbi->segs_per_sec) {
+				set_section_type(sbi, segno, type);
 				return 0;
+			}
 		}
 
 		if (se->type == type &&
Signature · 8 lines
2.18.0.rc1



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

PATCH 8/10 f2fs-tools: advise to mount unclean image to replay journal

In reply to Chao Yu

For defrag, resize, sload tools, let's advise to mount unclean image to replay journal first in order to not lose any fsynced data.

---
 fsck/mount.c | 6 ++++++
 1 file changed, 6 insertions(+)
fsck/mount.c +6 -0
index ad20d19..6e950bd 100644
@@ -2969,6 +2969,12 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi)
 	}
 	cp = F2FS_CKPT(sbi);
 
+	if (c.func != FSCK && c.func != DUMP &&
+		!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG)) {
+		ERR_MSG("Mount unclean image to replay log first\n");
+		return -1;
+	}
+
 	print_ckpt_info(sbi);
 
 	if (c.quota_fix) {
Signature · 8 lines
2.18.0.rc1



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

PATCH 9/10 fsck.f2fs: fix to propagate error of write_dquots()

In reply to Chao Yu

Propagate correct error number from write_dquots() to quota_write_inode().

---
 fsck/mkquota.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fsck/mkquota.c +7 -3
index c1abbc4..84f9d3d 100644
@@ -42,10 +42,11 @@ static void print_dquot(const char *desc, struct dquot *dq)
 #define print_dquot(...)
 #endif
 
-static void write_dquots(dict_t *dict, struct quota_handle *qh)
+static int write_dquots(dict_t *dict, struct quota_handle *qh)
 {
 	dnode_t		*n;
 	struct dquot	*dq;
+	int retval = 0;
 
 	for (n = dict_first(dict); n; n = dict_next(dict, n)) {
 		dq = dnode_get(n);
@@ -53,10 +54,13 @@ static void write_dquots(dict_t *dict, struct quota_handle *qh)
 			print_dquot("write", dq);
 			dq->dq_h = qh;
 			update_grace_times(dq);
-			if (qh->qh_ops->commit_dquot(dq))
+			if (qh->qh_ops->commit_dquot(dq)) {
+				retval = -1;
 				break;
+			}
 		}
 	}
+	return retval;
 }
 
 errcode_t quota_write_inode(struct f2fs_sb_info *sbi, enum quota_type qtype)
@@ -83,7 +87,7 @@ errcode_t quota_write_inode(struct f2fs_sb_info *sbi, enum quota_type qtype)
 		if (retval) {
 			log_debug("Cannot initialize io on quotafile");
 		} else {
-			write_dquots(dict, h);
+			retval = write_dquots(dict, h);
 			quota_file_close(sbi, h, 1);
 		}
 	}
Signature · 8 lines
2.18.0.rc1



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

PATCH 10/10 f2fs-tools: add missing newline symbol in log

In reply to Chao Yu

to show pretty log format.

---
 fsck/segment.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fsck/segment.c +4 -4
index 2d18358..b0c6889 100644
@@ -28,23 +28,23 @@ int reserve_new_block(struct f2fs_sb_info *sbi, block_t *to,
 	if (old_blkaddr == NULL_ADDR) {
 		if (c.func == FSCK) {
 			if (fsck->chk.valid_blk_cnt >= sbi->user_block_count) {
-				ERR_MSG("Not enough space");
+				ERR_MSG("Not enough space\n");
 				return -ENOSPC;
 			}
 			if (is_node && fsck->chk.valid_node_cnt >=
 					sbi->total_valid_node_count) {
-				ERR_MSG("Not enough space for node block");
+				ERR_MSG("Not enough space for node block\n");
 				return -ENOSPC;
 			}
 		} else {
 			if (sbi->total_valid_block_count >=
 						sbi->user_block_count) {
-				ERR_MSG("Not enough space");
+				ERR_MSG("Not enough space\n");
 				return -ENOSPC;
 			}
 			if (is_node && sbi->total_valid_node_count >=
 						sbi->total_node_count) {
-				ERR_MSG("Not enough space for node block");
+				ERR_MSG("Not enough space for node block\n");
 				return -ENOSPC;
 			}
 		}
Signature · 8 lines
2.18.0.rc1



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