Skip to content

Commit 6b8beca

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix to do sanity check on total_data_blocks
As Yanming reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215916 The kernel message is shown below: kernel BUG at fs/f2fs/segment.c:2560! Call Trace: allocate_segment_by_default+0x228/0x440 f2fs_allocate_data_block+0x13d1/0x31f0 do_write_page+0x18d/0x710 f2fs_outplace_write_data+0x151/0x250 f2fs_do_write_data_page+0xef9/0x1980 move_data_page+0x6af/0xbc0 do_garbage_collect+0x312f/0x46f0 f2fs_gc+0x6b0/0x3bc0 f2fs_balance_fs+0x921/0x2260 f2fs_write_single_data_page+0x16be/0x2370 f2fs_write_cache_pages+0x428/0xd00 f2fs_write_data_pages+0x96e/0xd50 do_writepages+0x168/0x550 __writeback_single_inode+0x9f/0x870 writeback_sb_inodes+0x47d/0xb20 __writeback_inodes_wb+0xb2/0x200 wb_writeback+0x4bd/0x660 wb_workfn+0x5f3/0xab0 process_one_work+0x79f/0x13e0 worker_thread+0x89/0xf60 kthread+0x26a/0x300 ret_from_fork+0x22/0x30 RIP: 0010:new_curseg+0xe8d/0x15f0 The root cause is: ckpt.valid_block_count is inconsistent with SIT table, stat info indicates filesystem has free blocks, but SIT table indicates filesystem has no free segment. So that during garbage colloection, it triggers panic when LFS allocator fails to find free segment. This patch tries to fix this issue by checking consistency in between ckpt.valid_block_count and block accounted from SIT. Cc: stable@vger.kernel.org Reported-by: Ming Yan <yanming@tju.edu.cn> Signed-off-by: Chao Yu <chao.yu@oppo.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent cfd66bb commit 6b8beca

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

fs/f2fs/f2fs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,8 @@ enum count_type {
11171117
*/
11181118
#define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
11191119
enum page_type {
1120-
DATA,
1121-
NODE,
1120+
DATA = 0,
1121+
NODE = 1, /* should not change this */
11221122
META,
11231123
NR_PAGE_TYPE,
11241124
META_FLUSH,

fs/f2fs/segment.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,7 +4461,7 @@ static int build_sit_entries(struct f2fs_sb_info *sbi)
44614461
unsigned int i, start, end;
44624462
unsigned int readed, start_blk = 0;
44634463
int err = 0;
4464-
block_t total_node_blocks = 0;
4464+
block_t sit_valid_blocks[2] = {0, 0};
44654465

44664466
do {
44674467
readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS,
@@ -4486,8 +4486,8 @@ static int build_sit_entries(struct f2fs_sb_info *sbi)
44864486
if (err)
44874487
return err;
44884488
seg_info_from_raw_sit(se, &sit);
4489-
if (IS_NODESEG(se->type))
4490-
total_node_blocks += se->valid_blocks;
4489+
4490+
sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks;
44914491

44924492
if (f2fs_block_unit_discard(sbi)) {
44934493
/* build discard map only one time */
@@ -4527,15 +4527,15 @@ static int build_sit_entries(struct f2fs_sb_info *sbi)
45274527
sit = sit_in_journal(journal, i);
45284528

45294529
old_valid_blocks = se->valid_blocks;
4530-
if (IS_NODESEG(se->type))
4531-
total_node_blocks -= old_valid_blocks;
4530+
4531+
sit_valid_blocks[SE_PAGETYPE(se)] -= old_valid_blocks;
45324532

45334533
err = check_block_count(sbi, start, &sit);
45344534
if (err)
45354535
break;
45364536
seg_info_from_raw_sit(se, &sit);
4537-
if (IS_NODESEG(se->type))
4538-
total_node_blocks += se->valid_blocks;
4537+
4538+
sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks;
45394539

45404540
if (f2fs_block_unit_discard(sbi)) {
45414541
if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
@@ -4557,13 +4557,24 @@ static int build_sit_entries(struct f2fs_sb_info *sbi)
45574557
}
45584558
up_read(&curseg->journal_rwsem);
45594559

4560-
if (!err && total_node_blocks != valid_node_count(sbi)) {
4560+
if (err)
4561+
return err;
4562+
4563+
if (sit_valid_blocks[NODE] != valid_node_count(sbi)) {
45614564
f2fs_err(sbi, "SIT is corrupted node# %u vs %u",
4562-
total_node_blocks, valid_node_count(sbi));
4563-
err = -EFSCORRUPTED;
4565+
sit_valid_blocks[NODE], valid_node_count(sbi));
4566+
return -EFSCORRUPTED;
45644567
}
45654568

4566-
return err;
4569+
if (sit_valid_blocks[DATA] + sit_valid_blocks[NODE] >
4570+
valid_user_blocks(sbi)) {
4571+
f2fs_err(sbi, "SIT is corrupted data# %u %u vs %u",
4572+
sit_valid_blocks[DATA], sit_valid_blocks[NODE],
4573+
valid_user_blocks(sbi));
4574+
return -EFSCORRUPTED;
4575+
}
4576+
4577+
return 0;
45674578
}
45684579

45694580
static void init_free_segmap(struct f2fs_sb_info *sbi)

fs/f2fs/segment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#define IS_DATASEG(t) ((t) <= CURSEG_COLD_DATA)
2626
#define IS_NODESEG(t) ((t) >= CURSEG_HOT_NODE && (t) <= CURSEG_COLD_NODE)
27+
#define SE_PAGETYPE(se) ((IS_NODESEG((se)->type) ? NODE : DATA))
2728

2829
static inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
2930
unsigned short seg_type)

0 commit comments

Comments
 (0)