resize.f2fs: fix to avoid zeroing main area blocks in migrate_ssa()
PATCH v2 1/2 resize.f2fs: fix to avoid zeroing main area blocks in migrate_ssa()
The number of SSA blocks after segment alignment is inconsistent with get_newsb(segment_count). This could result in some SSA blocks not being zeroed out or main area blocks being incorrectly zeroed out during SSA migration. Zeroing out main area blocks can corrupt the root inode, leading to file system corruption.
---
Changes in v2:
- Modify the commit message
--- fsck/resize.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
fsck/resize.c +19 -19
@@ -257,48 +257,48 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,struct f2fs_super_block *sb =F2FS_RAW_SUPER (sbi);block_t old_sum_blkaddr =get_sb (ssa_blkaddr);block_t new_sum_blkaddr =get_newsb (ssa_blkaddr); -unsigned int expand_segno =MAIN_SEGS (sbi) - offset; -unsigned int new_seg_total =get_newsb (segment_count); -int new_segno; +unsigned int expand_sum_blocks =MAIN_SEGS (sbi) - offset; +unsigned int new_sum_blocks =get_newsb (main_blkaddr) - new_sum_blkaddr; +int new_sum_blkoff;int ret =0 ;void *zero_block =calloc (F2FS_SUM_BLKSIZE ,1 );ASSERT (zero_block);if (offset && new_sum_blkaddr <= (old_sum_blkaddr + offset /SUMS_PER_BLOCK )) { - new_segno =0 ; -while (new_segno < new_seg_total) { -if (new_segno < expand_segno) -move_ssa (sbi, new_sb, offset++, new_segno); + new_sum_blkoff =0 ; +while (new_sum_blkoff < new_sum_blocks) { +if (new_sum_blkoff < expand_sum_blocks) +move_ssa (sbi, new_sb, offset++, new_sum_blkoff);else if (c.feature &F2FS_FEATURE_PACKED_SSA ) ret =dev_write_4k_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), -GET_SUM_NEW_BLKOFF (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), +GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );else ret =dev_write_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );ASSERT (ret >=0 ); - new_segno++; + new_sum_blkoff++; } }else { - new_segno = new_seg_total -1 ; + new_sum_blkoff = new_sum_blocks -1 ; offset =MAIN_SEGS (sbi) -1 ; -while (new_segno >=0 ) { -if (new_segno < expand_segno) -move_ssa (sbi, new_sb, offset--, new_segno); +while (new_sum_blkoff >=0 ) { +if (new_sum_blkoff < expand_sum_blocks) +move_ssa (sbi, new_sb, offset--, new_sum_blkoff);else if (c.feature &F2FS_FEATURE_PACKED_SSA ) ret =dev_write_4k_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), -GET_SUM_NEW_BLKOFF (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), +GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );else ret =dev_write_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );ASSERT (ret >=0 ); - new_segno--; + new_sum_blkoff--; } }
Signature · 8 lines
2.43.2 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
PATCH v2 2/2 resize.f2fs: add more logs in migrate_ssa()
Add logs for zeroing SSA blocks and add an ASSERT to prevent the issue of zeroing main area blocks from recurring in the future.
---
Changes in v2:
- Add commit message
--- fsck/resize.c | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-)
fsck/resize.c +30 -20
@@ -259,6 +259,7 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,block_t new_sum_blkaddr =get_newsb (ssa_blkaddr);unsigned int expand_sum_blocks =MAIN_SEGS (sbi) - offset;unsigned int new_sum_blocks =get_newsb (main_blkaddr) - new_sum_blkaddr; +block_t sum_blkaddr;int new_sum_blkoff;int ret =0 ;void *zero_block =calloc (F2FS_SUM_BLKSIZE ,1 );
@@ -268,17 +269,21 @@ static void migrate_ssa(struct f2fs_sb_info *sbi, offset / SUMS_PER_BLOCK)) { new_sum_blkoff =0 ;while (new_sum_blkoff < new_sum_blocks) { -if (new_sum_blkoff < expand_sum_blocks) +if (new_sum_blkoff < expand_sum_blocks) {move_ssa (sbi, new_sb, offset++, new_sum_blkoff); -else if (c.feature &F2FS_FEATURE_PACKED_SSA ) - ret =dev_write_4k_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), -GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff), -WRITE_LIFE_NONE ); -else - ret =dev_write_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), -WRITE_LIFE_NONE ); + }else { + sum_blkaddr =GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff); +if (c.feature &F2FS_FEATURE_PACKED_SSA ) + ret =dev_write_4k_block (zero_block, + sum_blkaddr, +GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff), +WRITE_LIFE_NONE ); +else + ret =dev_write_block (zero_block, + sum_blkaddr, +WRITE_LIFE_NONE ); +DBG (1 ,"Zero summary block: %x\n" , sum_blkaddr); + }ASSERT (ret >=0 ); new_sum_blkoff++; }
@@ -286,17 +291,22 @@ static void migrate_ssa(struct f2fs_sb_info *sbi, new_sum_blkoff = new_sum_blocks -1 ; offset =MAIN_SEGS (sbi) -1 ;while (new_sum_blkoff >=0 ) { -if (new_sum_blkoff < expand_sum_blocks) +if (new_sum_blkoff < expand_sum_blocks) {move_ssa (sbi, new_sb, offset--, new_sum_blkoff); -else if (c.feature &F2FS_FEATURE_PACKED_SSA ) - ret =dev_write_4k_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), -GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff), -WRITE_LIFE_NONE ); -else - ret =dev_write_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), -WRITE_LIFE_NONE ); + }else { + sum_blkaddr =GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff); +ASSERT (sum_blkaddr <get_newsb (main_blkaddr)); +if (c.feature &F2FS_FEATURE_PACKED_SSA ) + ret =dev_write_4k_block (zero_block, + sum_blkaddr, +GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff), +WRITE_LIFE_NONE ); +else + ret =dev_write_block (zero_block, + sum_blkaddr, +WRITE_LIFE_NONE ); +DBG (1 ,"Zero summary block: %x\n" , sum_blkaddr); + }ASSERT (ret >=0 ); new_sum_blkoff--; }
Signature · 8 lines
2.43.2 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 5 lines · Yangyang Zang
The number of SSA blocks after segment alignment is inconsistent with
get_newsb(segment_count). This could result in some SSA blocks not being zeroed out or main area blocks being incorrectly zeroed out during SSA migration. Zeroing out main area blocks can corrupt the root inode, leading to file system corruption.
Yangyang,
Could you please provide the testcase for this issue?
Quoted 15 lines
Fixes ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support")Signed-off-by Yangyang Zang <[email protected]>---
Changes in v2:
- Modify the commit message--- fsck/resize.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)fsck/resize.c +0
-0
fsck/resize.c static void migrate_ssa(struct f2fs_sb_info *sbi, · line 261
Quoted 8 lines
fsck/resize.c +2
-3@@ -257,48 +257,48 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,struct f2fs_super_block *sb =F2FS_RAW_SUPER (sbi);block_t old_sum_blkaddr =get_sb (ssa_blkaddr);block_t new_sum_blkaddr =get_newsb (ssa_blkaddr); -unsigned int expand_segno =MAIN_SEGS (sbi) - offset; -unsigned int new_seg_total =get_newsb (segment_count); -int new_segno; +unsigned int expand_sum_blocks =MAIN_SEGS (sbi) - offset; +unsigned int new_sum_blocks =get_newsb (main_blkaddr) - new_sum_blkaddr;
IIUC, the core change is above line, all other changes are cleanup, right? Can we split the fix and cleanup into two patches? I guess that will be better for review.
And, if this is a bug only when packed_ssa feature is on, do we need to check packed_ssa feature before updating new_sum_blocks?
Thanks,
Quoted 49 lines
fsck/resize.c +15
-14+int new_sum_blkoff;int ret =0 ;void *zero_block =calloc (F2FS_SUM_BLKSIZE ,1 );ASSERT (zero_block);if (offset && new_sum_blkaddr <= (old_sum_blkaddr + offset /SUMS_PER_BLOCK )) { - new_segno =0 ; -while (new_segno < new_seg_total) { -if (new_segno < expand_segno) -move_ssa (sbi, new_sb, offset++, new_segno); + new_sum_blkoff =0 ; +while (new_sum_blkoff < new_sum_blocks) { +if (new_sum_blkoff < expand_sum_blocks) +move_ssa (sbi, new_sb, offset++, new_sum_blkoff);else if (c.feature &F2FS_FEATURE_PACKED_SSA ) ret =dev_write_4k_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), -GET_SUM_NEW_BLKOFF (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), +GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );else ret =dev_write_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );ASSERT (ret >=0 ); - new_segno++; + new_sum_blkoff++; } }else { - new_segno = new_seg_total -1 ; + new_sum_blkoff = new_sum_blocks -1 ; offset =MAIN_SEGS (sbi) -1 ; -while (new_segno >=0 ) { -if (new_segno < expand_segno) -move_ssa (sbi, new_sb, offset--, new_segno); +while (new_sum_blkoff >=0 ) { +if (new_sum_blkoff < expand_sum_blocks) +move_ssa (sbi, new_sb, offset--, new_sum_blkoff);else if (c.feature &F2FS_FEATURE_PACKED_SSA ) ret =dev_write_4k_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), -GET_SUM_NEW_BLKOFF (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), +GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );else
fsck/resize.c static void migrate_ssa(struct f2fs_sb_info *sbi, · line 304
Quoted 10 lines
fsck/resize.c +2
-2ret =dev_write_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );ASSERT (ret >=0 ); - new_segno--; + new_sum_blkoff--; } }
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 9 lines · Chao Yu
Quoted 5 lines · Yangyang Zang
The number of SSA blocks after segment alignment is inconsistent with
get_newsb(segment_count). This could result in some SSA blocks not being zeroed out or main area blocks being incorrectly zeroed out during SSA migration. Zeroing out main area blocks can corrupt the root inode, leading to file system corruption.Yangyang,
Could you please provide the testcase for this issue?
This problem originated from our internal code upgrade to Android X beta 1.
After the upgrade, a large number of phones experienced file system corruption
and failed to boot. The first boot of the phone will perform a resize operation.
After the resize, the file system becomes corrupted, printing the following log:
[ 11.731278][ T1] F2FS-fs (sda35): invalid crc_offset: 0
[ 11.742722][ T1] F2FS-fs (sda35): inconsistent node block, node_type:1, nid:3, node_footer[nid:0,ino:0,ofs:0,cpver:0,blkaddr:0] [ 11.743019][ T1] F2FS-fs (sda35): Failed to read root inode
I will try to create a test case next.
Quoted 9 lines
Quoted 9 lines
Fixes ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support")Signed-off-by Yangyang Zang <[email protected]>---
Changes in v2:
- Modify the commit message--- fsck/resize.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
Quoted 14 lines
fsck/resize.c static void migrate_ssa(struct f2fs_sb_info *sbi, · line 261
Quoted 13 lines
fsck/resize.c +2
-3@@ -257,48 +257,48 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,struct f2fs_super_block *sb =F2FS_RAW_SUPER (sbi);block_t old_sum_blkaddr =get_sb (ssa_blkaddr);block_t new_sum_blkaddr =get_newsb (ssa_blkaddr); -unsigned int expand_segno =MAIN_SEGS (sbi) - offset; -unsigned int new_seg_total =get_newsb (segment_count); -int new_segno; +unsigned int expand_sum_blocks =MAIN_SEGS (sbi) - offset; +unsigned int new_sum_blocks =get_newsb (main_blkaddr) - new_sum_blkaddr;IIUC, the core change is above line, all other changes are cleanup, right?
Yes.
Quoted 2 lines
Can we split the fix and cleanup into two patches? I guess that will be better for review.
Yes, but I think that would make the first patch harder to understand.
The reason for this change is to make the code semantics more reasonable.
I think new_seg_total = get_newsb(main_blkaddr) - new_sum_blkaddr does not conform to
the original semantics of new_seg_total.
Even if it is changed to new_sum_blocks = get_newsb(main_blkaddr) - new_sum_blkaddr,
it is still inconsistent with the semantics of expand_segno and new_segno.
Quoted 2 lines
And, if this is a bug only when
packed_ssafeature is on, do we need to checkpacked_ssafeature before updatingnew_sum_blocks?
I think it's unnecessary. This bug can also be caused by not enabling the packed_ssa feature. Because
ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support")
changed the original SSA block zeroing logic, it does not distinguish whether the packed_ssa
feature is enabled or not.
Thanks,
Yangyang
Quoted 53 lines
Thanks,
Quoted 51 lines
fsck/resize.c +15
-15+int new_sum_blkoff;int ret =0 ;void *zero_block =calloc (F2FS_SUM_BLKSIZE ,1 );ASSERT (zero_block);if (offset && new_sum_blkaddr <= (old_sum_blkaddr + offset /SUMS_PER_BLOCK )) { - new_segno =0 ; -while (new_segno < new_seg_total) { -if (new_segno < expand_segno) -move_ssa (sbi, new_sb, offset++, new_segno); + new_sum_blkoff =0 ; +while (new_sum_blkoff < new_sum_blocks) { +if (new_sum_blkoff < expand_sum_blocks) +move_ssa (sbi, new_sb, offset++, new_sum_blkoff);else if (c.feature &F2FS_FEATURE_PACKED_SSA ) ret =dev_write_4k_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), -GET_SUM_NEW_BLKOFF (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), +GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );else ret =dev_write_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );ASSERT (ret >=0 ); - new_segno++; + new_sum_blkoff++; } }else { - new_segno = new_seg_total -1 ; + new_sum_blkoff = new_sum_blocks -1 ; offset =MAIN_SEGS (sbi) -1 ; -while (new_segno >=0 ) { -if (new_segno < expand_segno) -move_ssa (sbi, new_sb, offset--, new_segno); +while (new_sum_blkoff >=0 ) { +if (new_sum_blkoff < expand_sum_blocks) +move_ssa (sbi, new_sb, offset--, new_sum_blkoff);else if (c.feature &F2FS_FEATURE_PACKED_SSA ) ret =dev_write_4k_block (zero_block, -GET_SUM_NEW_BLKADDR (new_sb, new_segno), -GET_SUM_NEW_BLKOFF (new_sb, new_segno), +GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), +GET_SUM_NEW_BLKOFF (new_sb, new_sum_blkoff),WRITE_LIFE_NONE );else ret = dev_write_block(zero_block, - GET_SUM_NEW_BLKADDR(new_sb, new_segno),
Quoted 8 lines
fsck/resize.c static void migrate_ssa(struct f2fs_sb_info *sbi, · line 304
Quoted 8 lines
fsck/resize.c +2
-1+GET_SUM_NEW_BLKADDR (new_sb, new_sum_blkoff), WRITE_LIFE_NONE);ASSERT (ret >=0 ); - new_segno--; + new_sum_blkoff--; } }
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel