fsck.f2fs: add sanity check nat_bits
PATCH 2/2 resize.f2fs: fix wrong nat_bits migration
In reply to Jaegeuk KimWhen wring the last nat_bits in new checkpoint, we call write_nat_bits() to recover the nat_bits. But, we didn't update nm_i->nat_bitmap which we can get the up-to-date nat blocks. After sit migration, old checkpoint has no longer valid nat blocks, resulting in wrong free nid allocation at the following mount time.
Let's indicate new nat_blocks by adjusting nat_bitmap in nat migration.
--- fsck/resize.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fsck/resize.c +4 -1
@@ -339,8 +339,11 @@ static void migrate_nat(struct f2fs_sb_info *sbi, (seg_off << sbi->log_blocks_per_seg <<1 ) + (block_off & ((1 << sbi->log_blocks_per_seg) - 1))); -if (f2fs_test_bit (block_off, nm_i->nat_bitmap)) +/* move to set #0 */ +if (f2fs_test_bit (block_off, nm_i->nat_bitmap)) { block_addr += sbi->blocks_per_seg; +f2fs_clear_bit (block_off, nm_i->nat_bitmap); + } ret =dev_read_block (nat_block, block_addr);ASSERT (ret >=0 );
Signature · 6 lines
2.17.0.441.gb46fe60e1d-goog ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
PATCH 1/2 fsck.f2fs: add sanity check nat_bits
This patch adds to check nat_bits is valid or not. If not, it writes correct nat_bits.
--- fsck/mount.c | 95 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 74 insertions(+), 21 deletions(-)
fsck/mount.c +74 -21
@@ -1044,6 +1044,78 @@ void write_nat_bits(struct f2fs_sb_info *sbi,free (nat_bits); } +static int check_nat_bits (struct f2fs_sb_info *sbi, +struct f2fs_super_block *sb,struct f2fs_checkpoint *cp) +{ +struct f2fs_nm_info *nm_i =NM_I (sbi); +u_int32_t nat_blocks =get_sb (segment_count_nat) << + (get_sb (log_blocks_per_seg) -1 ); +u_int32_t nat_bits_bytes = nat_blocks >>3 ; +u_int32_t nat_bits_blocks =F2FS_BYTES_TO_BLK ((nat_bits_bytes <<1 ) + +8 +F2FS_BLKSIZE -1 ); +unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits; +u_int32_t i, j; +block_t blkaddr; +int err =0 ; + + nat_bits =calloc (F2FS_BLKSIZE , nat_bits_blocks); +ASSERT (nat_bits); + + full_nat_bits = nat_bits +8 ; + empty_nat_bits = full_nat_bits + nat_bits_bytes; + + blkaddr =get_sb (segment0_blkaddr) + (sbi->cur_cp << +get_sb (log_blocks_per_seg)) - nat_bits_blocks; + +for (i =0 ; i < nat_bits_blocks; i++) { +if (dev_read_block (nat_bits + i *F2FS_BLKSIZE , blkaddr + i)) +ASSERT_MSG ("\tError: read NAT bits to disk!!!\n" ); + } + +if (*(__le64 *)nat_bits !=get_cp_crc (cp)) { + err =-1 ; + goto out; + } + +for (i =0 ; i < nat_blocks; i++) { +u_int32_t start_nid = i *NAT_ENTRY_PER_BLOCK ; +u_int32_t valid =0 ; + +for (j =0 ; j <NAT_ENTRY_PER_BLOCK ; j++) { +if (f2fs_test_bit (start_nid + j, nm_i->nid_bitmap)) + valid++; + } +if (valid ==0 ) { +if (!test_bit_le (i, empty_nat_bits) || +test_bit_le (i, full_nat_bits)) { + err =-1 ; + goto out; + } + }else if (valid ==NAT_ENTRY_PER_BLOCK ) { +if (test_bit_le (i, empty_nat_bits) || + !test_bit_le (i, full_nat_bits)) { + err =-1 ; + goto out; + } + }else { +if (test_bit_le (i, empty_nat_bits) || +test_bit_le (i, full_nat_bits)) { + err =-1 ; + goto out; + } + } + } +out: +free (nat_bits); +if (!err) { +MSG (0 ,"Info: Checked valid nat_bits in checkpoint\n" ); + }else { + c.bug_on =1 ; +MSG (0 ,"Info: Corrupted valid nat_bits in checkpoint\n" ); + } +return err; +} +int init_node_manager (struct f2fs_sb_info *sbi) {struct f2fs_super_block *sb =F2FS_RAW_SUPER (sbi);
@@ -2394,28 +2466,9 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi) }/* Check nat_bits */ -if (c.func !=DUMP &&is_set_ckpt_flags (cp,CP_NAT_BITS_FLAG )) { -u_int32_t nat_bits_bytes, nat_bits_blocks; -__le64 *kaddr; -u_int32_t blk; - - blk =get_sb (cp_blkaddr) + (1 <<get_sb (log_blocks_per_seg)); -if (sbi->cur_cp ==2 ) - blk +=1 <<get_sb (log_blocks_per_seg); - - nat_bits_bytes =get_sb (segment_count_nat) <<5 ; - nat_bits_blocks =F2FS_BYTES_TO_BLK ((nat_bits_bytes <<1 ) +8 + -F2FS_BLKSIZE -1 ); - blk -= nat_bits_blocks; - - kaddr =malloc (PAGE_SIZE ); - ret =dev_read_block (kaddr, blk); -ASSERT (ret >=0 ); -if (*kaddr !=get_cp_crc (cp)) +if (c.func ==FSCK &&is_set_ckpt_flags (cp,CP_NAT_BITS_FLAG )) { +if (check_nat_bits (sbi, sb, cp) && c.fix_on)write_nat_bits (sbi, sb, cp, sbi->cur_cp); -else -MSG (0 ,"Info: Found valid nat_bits in checkpoint\n" ); -free (kaddr); }return 0 ; }
Signature · 6 lines
2.17.0.441.gb46fe60e1d-goog ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Quoted 66 lines · Jaegeuk Kim
This patch adds to check
nat_bitsis valid or not. If not, it writes correctnat_bits.Signed-off-by Jaegeuk Kim <[email protected]>--- fsck/mount.c | 95 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 74 insertions(+), 21 deletions(-)fsck/mount.c +50
-0@@ -1044,6 +1044,78 @@ void write_nat_bits(struct f2fs_sb_info *sbi,free (nat_bits); } +static int check_nat_bits (struct f2fs_sb_info *sbi, +struct f2fs_super_block *sb,struct f2fs_checkpoint *cp) +{ +struct f2fs_nm_info *nm_i =NM_I (sbi); +u_int32_t nat_blocks =get_sb (segment_count_nat) << + (get_sb (log_blocks_per_seg) -1 ); +u_int32_t nat_bits_bytes = nat_blocks >>3 ; +u_int32_t nat_bits_blocks =F2FS_BYTES_TO_BLK ((nat_bits_bytes <<1 ) + +8 +F2FS_BLKSIZE -1 ); +unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits; +u_int32_t i, j; +block_t blkaddr; +int err =0 ; + + nat_bits =calloc (F2FS_BLKSIZE , nat_bits_blocks); +ASSERT (nat_bits); + + full_nat_bits = nat_bits +8 ; + empty_nat_bits = full_nat_bits + nat_bits_bytes; + + blkaddr =get_sb (segment0_blkaddr) + (sbi->cur_cp << +get_sb (log_blocks_per_seg)) - nat_bits_blocks; + +for (i =0 ; i < nat_bits_blocks; i++) { +if (dev_read_block (nat_bits + i *F2FS_BLKSIZE , blkaddr + i)) +ASSERT_MSG ("\tError: read NAT bits to disk!!!\n" ); + } + +if (*(__le64 *)nat_bits !=get_cp_crc (cp)) { + err =-1 ; + goto out; + } + +for (i =0 ; i < nat_blocks; i++) { +u_int32_t start_nid = i *NAT_ENTRY_PER_BLOCK ; +u_int32_t valid =0 ; + +for (j =0 ; j <NAT_ENTRY_PER_BLOCK ; j++) { +if (f2fs_test_bit (start_nid + j, nm_i->nid_bitmap)) + valid++; + } +if (valid ==0 ) { +if (!test_bit_le (i, empty_nat_bits) || +test_bit_le (i, full_nat_bits)) { + err =-1 ; + goto out; + } + }else if (valid ==NAT_ENTRY_PER_BLOCK ) { +if (test_bit_le (i, empty_nat_bits) || + !test_bit_le (i, full_nat_bits)) {
fsck/mount.c void write_nat_bits(struct f2fs_sb_info *sbi, · line 1106
Quoted 10 lines
fsck/mount.c +10
-0+ err =-1 ; + goto out; + } + }else { +if (test_bit_le (i, empty_nat_bits) || +test_bit_le (i, full_nat_bits)) { + err =-1 ; + goto out; + } + }
Can clean up by replacing test_bit_le(i, ...) with added empty/full variables?
empty = test_bit_le(i, empty_nat_bits);
full = test_bit_le(i, full_nat_bits);
Anyway, it looks good to me.
Thanks,
Quoted 37 lines
fsck/mount.c +12
-17+ } +out: +free (nat_bits); +if (!err) { +MSG (0 ,"Info: Checked valid nat_bits in checkpoint\n" ); + }else { + c.bug_on =1 ; +MSG (0 ,"Info: Corrupted valid nat_bits in checkpoint\n" ); + } +return err; +} +int init_node_manager (struct f2fs_sb_info *sbi) {struct f2fs_super_block *sb =F2FS_RAW_SUPER (sbi);@@ -2394,28 +2466,9 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi) }/* Check nat_bits */ -if (c.func !=DUMP &&is_set_ckpt_flags (cp,CP_NAT_BITS_FLAG )) { -u_int32_t nat_bits_bytes, nat_bits_blocks; -__le64 *kaddr; -u_int32_t blk; - - blk =get_sb (cp_blkaddr) + (1 <<get_sb (log_blocks_per_seg)); -if (sbi->cur_cp ==2 ) - blk +=1 <<get_sb (log_blocks_per_seg); - - nat_bits_bytes =get_sb (segment_count_nat) <<5 ; - nat_bits_blocks =F2FS_BYTES_TO_BLK ((nat_bits_bytes <<1 ) +8 + -F2FS_BLKSIZE -1 ); - blk -= nat_bits_blocks; - - kaddr =malloc (PAGE_SIZE ); - ret =dev_read_block (kaddr, blk); -ASSERT (ret >=0 );
fsck/mount.c int f2fs_do_mount(struct f2fs_sb_info *sbi) · line 2474
Quoted 10 lines
fsck/mount.c +2
-4-if (*kaddr !=get_cp_crc (cp)) +if (c.func ==FSCK &&is_set_ckpt_flags (cp,CP_NAT_BITS_FLAG )) { +if (check_nat_bits (sbi, sb, cp) && c.fix_on)write_nat_bits (sbi, sb, cp, sbi->cur_cp); -else -MSG (0 ,"Info: Found valid nat_bits in checkpoint\n" ); -free (kaddr); }return 0 ; }
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Re: [PATCH 2/2] resize.f2fs: fix wrong nat_bits migration
In reply to Jaegeuk KimQuoted 10 lines · Jaegeuk Kim
When wring the last
nat_bitsin new checkpoint, we callwrite_nat_bits()to recover thenat_bits. But, we didn't updatenm_i->nat_bitmapwhich we can get the up-to-date nat blocks. After sit migration, old checkpoint has no longer valid nat blocks, resulting in wrong free nid allocation at the following mount time.Let's indicate new
nat_blocksby adjustingnat_bitmapin nat migration.Reported-by Lianjun Huang <[email protected]>Signed-off-by Jaegeuk Kim <[email protected]>
Thanks,
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot