f2fs-tools: relocate chksum_offset for large_nat_bitmap feature
PATCH v2 1/1 f2fs-tools: relocate chksum_offset for large_nat_bitmap feature
For large_nat_bitmap feature, there is a design flaw:
Previous:
struct f2fs_checkpoint layout:
+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----|-------+ | ...... | | | | checksum_value |<-----+ | +--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+
Obviously, if nat_bitmap size + sit_bitmap size is larger than MAX_BITMAP_SIZE_IN_CKPT, nat_bitmap or sit_bitmap may overlap checkpoint checksum's position, once checkpoint() is triggered from kernel, nat or sit bitmap will be damaged by checksum field.
In order to fix this, let's relocate checksum_value's position to the head of sit_nat_version_bitmap as below, then nat/sit bitmap and chksum value update will become safe.
After:
struct f2fs_checkpoint layout:
+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----+ | ...... |<-------------+ | | | +--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+
Related report and discussion:
https://sourceforge.net/p/linux-f2fs/mailman/message/36642346/
In addition, we did below change as well:
- use MAX_BITMAP_SIZE_IN_CKPT to clean up codes.
- use get_cp(checksum_offset) instead of hard code CP_CHKSUM_OFFSET macro.
---
v2: detect deprecated layout with fsck.
fsck/f2fs.h | 6 +++++- fsck/fsck.c | 22 +++++++++++++++++++--- fsck/fsck.h | 1 + fsck/main.c | 2 ++ fsck/mount.c | 9 ++++++--- fsck/resize.c | 10 +++++----- mkfs/f2fs_format.c | 21 +++++++++++---------- 7 files changed, 49 insertions(+), 22 deletions(-)
fsck/f2fs.h +5 -1
@@ -272,7 +272,11 @@ static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)if (is_set_ckpt_flags (ckpt,CP_LARGE_NAT_BITMAP_FLAG )) { offset = (flag ==SIT_BITMAP ) ?le32_to_cpu (ckpt->nat_ver_bitmap_bytesize) :0 ; -return &ckpt->sit_nat_version_bitmap + offset; +/* +* if large_nat_bitmap feature is enabled, leave checksum +* protection for all nat/sit bitmaps. +*/ +return &ckpt->sit_nat_version_bitmap + offset +sizeof (__le32); }if (le32_to_cpu (F2FS_RAW_SUPER (sbi)->cp_payload) >0 ) {
fsck/fsck.c +19 -3
@@ -1917,6 +1917,19 @@ int fsck_chk_meta(struct f2fs_sb_info *sbi)return 0 ; } +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi) +{ +struct f2fs_checkpoint *cp =F2FS_CKPT (sbi); + +if (get_cp (ckpt_flags) &CP_LARGE_NAT_BITMAP_FLAG ) { +if (get_cp (checksum_offset) != +sizeof (struct f2fs_checkpoint ) -1 ) { +ASSERT_MSG ("Deprecated layout of large_nat_bitmap, " +"chksum_offset:%u" ,get_cp (checksum_offset)); + } + } +} +void fsck_init (struct f2fs_sb_info *sbi) {struct f2fs_fsck *fsck =F2FS_FSCK (sbi);
@@ -2038,8 +2051,10 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }if (flags &CP_UMOUNT_FLAG ) cp_blocks =8 ;
@@ -2058,8 +2073,9 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi)set_cp (valid_node_count, fsck->chk.valid_node_cnt);set_cp (valid_inode_count, fsck->chk.valid_inode_cnt); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )
fsck/fsck.h +1 -0
@@ -154,6 +154,7 @@ extern int fsck_chk_dentry_blk(struct f2fs_sb_info *, u32, struct child_info *, int, int);int fsck_chk_inline_dentries (struct f2fs_sb_info *,struct f2fs_node *,struct child_info *); +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi);int fsck_chk_meta (struct f2fs_sb_info *sbi);int fsck_chk_curseg_info (struct f2fs_sb_info *);int convert_encrypted_name (unsigned char *,u32 ,unsigned char *,int );
fsck/main.c +2 -0
@@ -616,6 +616,8 @@ static void do_fsck(struct f2fs_sb_info *sbi) c.fix_on =1 ; } +fsck_chk_checkpoint (sbi); +fsck_chk_quota_node (sbi);/* Traverse all block recursively from root inode */
fsck/mount.c +6 -3
@@ -2365,8 +2365,10 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }set_cp (free_segment_count,get_free_segments (sbi));set_cp (valid_block_count, sbi->total_valid_block_count);
@@ -2375,8 +2377,9 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags =update_nat_bits_flags (sb, cp, flags);set_cp (ckpt_flags, flags); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )
fsck/resize.c +5 -5
@@ -90,11 +90,11 @@ static int get_new_sb(struct f2fs_super_block *sb)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); }
@@ -520,8 +520,8 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi, (unsigned char *)cp); new_cp->checkpoint_ver =cpu_to_le64 (cp_ver +1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)new_cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)new_cp +get_cp (checksum_offset))) =cpu_to_le32 (crc);/* Write a new checkpoint in the other set */
mkfs/f2fs_format.c +11 -10
@@ -358,13 +358,11 @@ static int f2fs_prepare_super_block(void)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET - -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size = -CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); } max_nat_segments = (max_nat_bitmap_size *8 ) >> log_blks_per_seg;
@@ -708,10 +706,13 @@ static int f2fs_write_check_point_pack(void)set_cp (nat_ver_bitmap_bytesize, ((get_sb (segment_count_nat) /2 ) <<get_sb (log_blocks_per_seg)) /8 ); -set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +if (c.large_nat_bitmap) +set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +else +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); blk_size_bytes =1 <<get_sb (log_blocksize);
@@ -956,8 +957,8 @@ static int f2fs_write_check_point_pack(void)*/ cp->checkpoint_ver =0 ; - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); cp_seg_blk =get_sb (segment0_blkaddr) + c.blks_per_seg; DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",
Signature · 1 line
2.18.0.rc1
Quoted 30 lines · Chao Yu
For
large_nat_bitmapfeature, there is a design flaw:Previous:
struct
f2fs_checkpointlayout:+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----|-------+ | ...... | | | | checksum_value |<-----+ | +--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+Obviously, if
nat_bitmapsize +sit_bitmapsize is larger thanMAX_BITMAP_SIZE_IN_CKPT,nat_bitmaporsit_bitmapmay overlap checkpoint checksum's position, oncecheckpoint()is triggered from kernel, nat or sit bitmap will be damaged by checksum field.In order to fix this, let's relocate checksum_value's position to the head of
sit_nat_version_bitmapas below, then nat/sit bitmap and chksum value update will become safe.After:
Quoted 7 lines
struct
f2fs_checkpointlayout:+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----+
We can't write checksum in the middle of checkpoint for checksum data?
Quoted 2 lines
| ...... |<-------------+ | | |
I know it's very ugly tho, can we move large_bitmaps by 4 bytes?
Something like:
...
sit_nat_version_bitmap[0]
...
sit_nat_version_bitmap[N]checksum_value: 4B
sit_nat_version_bitmap[N+1]
...
sit_nat_version_bitmap[M]
Quoted 226 lines
+--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+Related report and discussion:
https://sourceforge.net/p/linux-f2fs/mailman/message/36642346/
In addition, we did below change as well:
- useMAX_BITMAP_SIZE_IN_CKPTto clean up codes.
- useget_cp(checksum_offset)instead of hard codeCP_CHKSUM_OFFSETmacro.Reported-by Park Ju Hyung <[email protected]>Signed-off-by Chao Yu <[email protected]>---
v2: detect deprecated layout with fsck.fsck/f2fs.h | 6 +++++- fsck/fsck.c | 22 +++++++++++++++++++--- fsck/fsck.h | 1 + fsck/main.c | 2 ++ fsck/mount.c | 9 ++++++--- fsck/resize.c | 10 +++++----- mkfs/f2fs_format.c | 21 +++++++++++---------- 7 files changed, 49 insertions(+), 22 deletions(-)fsck/f2fs.h +5
-1@@ -272,7 +272,11 @@ static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)if (is_set_ckpt_flags (ckpt,CP_LARGE_NAT_BITMAP_FLAG )) { offset = (flag ==SIT_BITMAP ) ?le32_to_cpu (ckpt->nat_ver_bitmap_bytesize) :0 ; -return &ckpt->sit_nat_version_bitmap + offset; +/* +* if large_nat_bitmap feature is enabled, leave checksum +* protection for all nat/sit bitmaps. +*/ +return &ckpt->sit_nat_version_bitmap + offset +sizeof (__le32); }if (le32_to_cpu (F2FS_RAW_SUPER (sbi)->cp_payload) >0 ) {fsck/fsck.c +19
-3@@ -1917,6 +1917,19 @@ int fsck_chk_meta(struct f2fs_sb_info *sbi)return 0 ; } +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi) +{ +struct f2fs_checkpoint *cp =F2FS_CKPT (sbi); + +if (get_cp (ckpt_flags) &CP_LARGE_NAT_BITMAP_FLAG ) { +if (get_cp (checksum_offset) != +sizeof (struct f2fs_checkpoint ) -1 ) { +ASSERT_MSG ("Deprecated layout of large_nat_bitmap, " +"chksum_offset:%u" ,get_cp (checksum_offset)); + } + } +} +void fsck_init (struct f2fs_sb_info *sbi) {struct f2fs_fsck *fsck =F2FS_FSCK (sbi);@@ -2038,8 +2051,10 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }if (flags &CP_UMOUNT_FLAG ) cp_blocks =8 ;@@ -2058,8 +2073,9 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi)set_cp (valid_node_count, fsck->chk.valid_node_cnt);set_cp (valid_inode_count, fsck->chk.valid_inode_cnt); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )fsck/fsck.h +1
-0@@ -154,6 +154,7 @@ extern int fsck_chk_dentry_blk(struct f2fs_sb_info *, u32, struct child_info *, int, int);int fsck_chk_inline_dentries (struct f2fs_sb_info *,struct f2fs_node *,struct child_info *); +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi);int fsck_chk_meta (struct f2fs_sb_info *sbi);int fsck_chk_curseg_info (struct f2fs_sb_info *);int convert_encrypted_name (unsigned char *,u32 ,unsigned char *,int );fsck/main.c +2
-0@@ -616,6 +616,8 @@ static void do_fsck(struct f2fs_sb_info *sbi) c.fix_on =1 ; } +fsck_chk_checkpoint (sbi); +fsck_chk_quota_node (sbi);/* Traverse all block recursively from root inode */ fsck/mount.c +6
-3@@ -2365,8 +2365,10 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }set_cp (free_segment_count,get_free_segments (sbi));set_cp (valid_block_count, sbi->total_valid_block_count);@@ -2375,8 +2377,9 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags =update_nat_bits_flags (sb, cp, flags);set_cp (ckpt_flags, flags); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )fsck/resize.c +5
-5@@ -90,11 +90,11 @@ static int get_new_sb(struct f2fs_super_block *sb)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); }@@ -520,8 +520,8 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi, (unsigned char *)cp); new_cp->checkpoint_ver =cpu_to_le64 (cp_ver +1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)new_cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)new_cp +get_cp (checksum_offset))) =cpu_to_le32 (crc);/* Write a new checkpoint in the other set */ mkfs/f2fs_format.c +11
-10@@ -358,13 +358,11 @@ static int f2fs_prepare_super_block(void)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET - -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size = -CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); } max_nat_segments = (max_nat_bitmap_size *8 ) >> log_blks_per_seg;@@ -708,10 +706,13 @@ static int f2fs_write_check_point_pack(void)set_cp (nat_ver_bitmap_bytesize, ((get_sb (segment_count_nat) /2 ) <<get_sb (log_blocks_per_seg)) /8 ); -set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +if (c.large_nat_bitmap) +set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +else +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); blk_size_bytes =1 <<get_sb (log_blocksize);@@ -956,8 +957,8 @@ static int f2fs_write_check_point_pack(void)*/ cp->checkpoint_ver =0 ; - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); cp_seg_blk =get_sb (segment0_blkaddr) + c.blks_per_seg; DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",Signature · 1 line
2.18.0.rc1
Quoted 39 lines · Jaegeuk Kim
Quoted 30 lines · Chao Yu
For
large_nat_bitmapfeature, there is a design flaw:Previous:
struct
f2fs_checkpointlayout:+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----|-------+ | ...... | | | | checksum_value |<-----+ | +--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+Obviously, if
nat_bitmapsize +sit_bitmapsize is larger thanMAX_BITMAP_SIZE_IN_CKPT,nat_bitmaporsit_bitmapmay overlap checkpoint checksum's position, oncecheckpoint()is triggered from kernel, nat or sit bitmap will be damaged by checksum field.In order to fix this, let's relocate checksum_value's position to the head of
sit_nat_version_bitmapas below, then nat/sit bitmap and chksum value update will become safe.After:
Quoted 7 lines
struct
f2fs_checkpointlayout:+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----+
Quoted 1 line
We can't write checksum in the middle of checkpoint for checksum data?
Jaegeuk,
inode.i_inode_checksum locates in the middle of inode layout, we calculate i_inode_checksum with superposition method, so there is no restriction in technique aspect, right?
f2fs_inode_chksum()
{
...
chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino, sizeof(ino)); chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen)); chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset); chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size); offset += cs_size; chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset, F2FS_BLKSIZE - offset); return chksum; }
Quoted 5 lines
Quoted 3 lines
| ...... |<-------------+ | | |I know it's very ugly tho, can we move
large_bitmapsby 4 bytes?
Have thought about this way... but... actually, as you said, the implementation will be ugly. So I choose the easy way.
If you insist, I can change to below implementation, any particular reason to use this implementation? To keep checksum locating in fixed position, or protect more cp park's data..?
Thanks,
Quoted 240 lines
Something like:
...
sit_nat_version_bitmap[0]
...
sit_nat_version_bitmap[N]checksum_value: 4B
sit_nat_version_bitmap[N+1]
...
sit_nat_version_bitmap[M]Quoted 226 lines
+--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+Related report and discussion:
https://sourceforge.net/p/linux-f2fs/mailman/message/36642346/
In addition, we did below change as well:
- useMAX_BITMAP_SIZE_IN_CKPTto clean up codes.
- useget_cp(checksum_offset)instead of hard codeCP_CHKSUM_OFFSETmacro.Reported-by Park Ju Hyung <[email protected]>Signed-off-by Chao Yu <[email protected]>---
v2: detect deprecated layout with fsck.fsck/f2fs.h | 6 +++++- fsck/fsck.c | 22 +++++++++++++++++++--- fsck/fsck.h | 1 + fsck/main.c | 2 ++ fsck/mount.c | 9 ++++++--- fsck/resize.c | 10 +++++----- mkfs/f2fs_format.c | 21 +++++++++++---------- 7 files changed, 49 insertions(+), 22 deletions(-)fsck/f2fs.h +5
-1@@ -272,7 +272,11 @@ static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)if (is_set_ckpt_flags (ckpt,CP_LARGE_NAT_BITMAP_FLAG )) { offset = (flag ==SIT_BITMAP ) ?le32_to_cpu (ckpt->nat_ver_bitmap_bytesize) :0 ; -return &ckpt->sit_nat_version_bitmap + offset; +/* +* if large_nat_bitmap feature is enabled, leave checksum +* protection for all nat/sit bitmaps. +*/ +return &ckpt->sit_nat_version_bitmap + offset +sizeof (__le32); }if (le32_to_cpu (F2FS_RAW_SUPER (sbi)->cp_payload) >0 ) {fsck/fsck.c +19
-3@@ -1917,6 +1917,19 @@ int fsck_chk_meta(struct f2fs_sb_info *sbi)return 0 ; } +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi) +{ +struct f2fs_checkpoint *cp =F2FS_CKPT (sbi); + +if (get_cp (ckpt_flags) &CP_LARGE_NAT_BITMAP_FLAG ) { +if (get_cp (checksum_offset) != +sizeof (struct f2fs_checkpoint ) -1 ) { +ASSERT_MSG ("Deprecated layout of large_nat_bitmap, " +"chksum_offset:%u" ,get_cp (checksum_offset)); + } + } +} +void fsck_init (struct f2fs_sb_info *sbi) {struct f2fs_fsck *fsck =F2FS_FSCK (sbi);@@ -2038,8 +2051,10 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }if (flags &CP_UMOUNT_FLAG ) cp_blocks =8 ;@@ -2058,8 +2073,9 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi)set_cp (valid_node_count, fsck->chk.valid_node_cnt);set_cp (valid_inode_count, fsck->chk.valid_inode_cnt); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )fsck/fsck.h +1
-0@@ -154,6 +154,7 @@ extern int fsck_chk_dentry_blk(struct f2fs_sb_info *, u32, struct child_info *, int, int);int fsck_chk_inline_dentries (struct f2fs_sb_info *,struct f2fs_node *,struct child_info *); +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi);int fsck_chk_meta (struct f2fs_sb_info *sbi);int fsck_chk_curseg_info (struct f2fs_sb_info *);int convert_encrypted_name (unsigned char *,u32 ,unsigned char *,int );fsck/main.c +2
-0@@ -616,6 +616,8 @@ static void do_fsck(struct f2fs_sb_info *sbi) c.fix_on =1 ; } +fsck_chk_checkpoint (sbi); +fsck_chk_quota_node (sbi);/* Traverse all block recursively from root inode */ fsck/mount.c +6
-3@@ -2365,8 +2365,10 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }set_cp (free_segment_count,get_free_segments (sbi));set_cp (valid_block_count, sbi->total_valid_block_count);@@ -2375,8 +2377,9 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags =update_nat_bits_flags (sb, cp, flags);set_cp (ckpt_flags, flags); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )fsck/resize.c +5
-5@@ -90,11 +90,11 @@ static int get_new_sb(struct f2fs_super_block *sb)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); }@@ -520,8 +520,8 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi, (unsigned char *)cp); new_cp->checkpoint_ver =cpu_to_le64 (cp_ver +1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)new_cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)new_cp +get_cp (checksum_offset))) =cpu_to_le32 (crc);/* Write a new checkpoint in the other set */ mkfs/f2fs_format.c +11
-10@@ -358,13 +358,11 @@ static int f2fs_prepare_super_block(void)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET - -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size = -CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); } max_nat_segments = (max_nat_bitmap_size *8 ) >> log_blks_per_seg;@@ -708,10 +706,13 @@ static int f2fs_write_check_point_pack(void)set_cp (nat_ver_bitmap_bytesize, ((get_sb (segment_count_nat) /2 ) <<get_sb (log_blocks_per_seg)) /8 ); -set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +if (c.large_nat_bitmap) +set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +else +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); blk_size_bytes =1 <<get_sb (log_blocksize);@@ -956,8 +957,8 @@ static int f2fs_write_check_point_pack(void)*/ cp->checkpoint_ver =0 ; - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); cp_seg_blk =get_sb (segment0_blkaddr) + c.blks_per_seg; DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",Signature · 1 line
2.18.0.rc1.
Quoted 67 lines · Chao Yu
Quoted 39 lines · Jaegeuk Kim
Quoted 30 lines · Chao Yu
For
large_nat_bitmapfeature, there is a design flaw:Previous:
struct
f2fs_checkpointlayout:+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----|-------+ | ...... | | | | checksum_value |<-----+ | +--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+Obviously, if
nat_bitmapsize +sit_bitmapsize is larger thanMAX_BITMAP_SIZE_IN_CKPT,nat_bitmaporsit_bitmapmay overlap checkpoint checksum's position, oncecheckpoint()is triggered from kernel, nat or sit bitmap will be damaged by checksum field.In order to fix this, let's relocate checksum_value's position to the head of
sit_nat_version_bitmapas below, then nat/sit bitmap and chksum value update will become safe.After:
Quoted 7 lines
struct
f2fs_checkpointlayout:+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----+Quoted 1 line
We can't write checksum in the middle of checkpoint for checksum data?
Jaegeuk,
inode.i_inode_checksum locates in the middle of inode layout, we calculate
i_inode_checksumwith superposition method, so there is no restriction in technique aspect, right?
f2fs_inode_chksum()
{
...chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino, sizeof(ino)); chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen)); chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset); chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size); offset += cs_size; chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset, F2FS_BLKSIZE - offset); return chksum; }Quoted 4 lines
Quoted 3 lines
| ...... |<-------------+ | | |
Quoted 8 lines
Quoted 1 line
I know it's very ugly tho, can we move
large_bitmapsby 4 bytes?Have thought about this way... but... actually, as you said, the implementation will be ugly. So I choose the easy way.
If you insist, I can change to below implementation, any particular reason to use this implementation? To keep checksum locating in fixed position, or protect more cp park's data..?
Okay, could you prepare a patch to allow checksum_value in any offset first?
Thanks,
Quoted 243 lines
Thanks,
Quoted 240 lines
Something like:
...
sit_nat_version_bitmap[0]
...
sit_nat_version_bitmap[N]checksum_value: 4B
sit_nat_version_bitmap[N+1]
...
sit_nat_version_bitmap[M]Quoted 226 lines
+--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+Related report and discussion:
https://sourceforge.net/p/linux-f2fs/mailman/message/36642346/
In addition, we did below change as well:
- useMAX_BITMAP_SIZE_IN_CKPTto clean up codes.
- useget_cp(checksum_offset)instead of hard codeCP_CHKSUM_OFFSETmacro.Reported-by Park Ju Hyung <[email protected]>Signed-off-by Chao Yu <[email protected]>---
v2: detect deprecated layout with fsck.fsck/f2fs.h | 6 +++++- fsck/fsck.c | 22 +++++++++++++++++++--- fsck/fsck.h | 1 + fsck/main.c | 2 ++ fsck/mount.c | 9 ++++++--- fsck/resize.c | 10 +++++----- mkfs/f2fs_format.c | 21 +++++++++++---------- 7 files changed, 49 insertions(+), 22 deletions(-)fsck/f2fs.h +5
-1@@ -272,7 +272,11 @@ static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)if (is_set_ckpt_flags (ckpt,CP_LARGE_NAT_BITMAP_FLAG )) { offset = (flag ==SIT_BITMAP ) ?le32_to_cpu (ckpt->nat_ver_bitmap_bytesize) :0 ; -return &ckpt->sit_nat_version_bitmap + offset; +/* +* if large_nat_bitmap feature is enabled, leave checksum +* protection for all nat/sit bitmaps. +*/ +return &ckpt->sit_nat_version_bitmap + offset +sizeof (__le32); }if (le32_to_cpu (F2FS_RAW_SUPER (sbi)->cp_payload) >0 ) {fsck/fsck.c +19
-3@@ -1917,6 +1917,19 @@ int fsck_chk_meta(struct f2fs_sb_info *sbi)return 0 ; } +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi) +{ +struct f2fs_checkpoint *cp =F2FS_CKPT (sbi); + +if (get_cp (ckpt_flags) &CP_LARGE_NAT_BITMAP_FLAG ) { +if (get_cp (checksum_offset) != +sizeof (struct f2fs_checkpoint ) -1 ) { +ASSERT_MSG ("Deprecated layout of large_nat_bitmap, " +"chksum_offset:%u" ,get_cp (checksum_offset)); + } + } +} +void fsck_init (struct f2fs_sb_info *sbi) {struct f2fs_fsck *fsck =F2FS_FSCK (sbi);@@ -2038,8 +2051,10 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }if (flags &CP_UMOUNT_FLAG ) cp_blocks =8 ;@@ -2058,8 +2073,9 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi)set_cp (valid_node_count, fsck->chk.valid_node_cnt);set_cp (valid_inode_count, fsck->chk.valid_inode_cnt); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )fsck/fsck.h +1
-0@@ -154,6 +154,7 @@ extern int fsck_chk_dentry_blk(struct f2fs_sb_info *, u32, struct child_info *, int, int);int fsck_chk_inline_dentries (struct f2fs_sb_info *,struct f2fs_node *,struct child_info *); +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi);int fsck_chk_meta (struct f2fs_sb_info *sbi);int fsck_chk_curseg_info (struct f2fs_sb_info *);int convert_encrypted_name (unsigned char *,u32 ,unsigned char *,int );fsck/main.c +2
-0@@ -616,6 +616,8 @@ static void do_fsck(struct f2fs_sb_info *sbi) c.fix_on =1 ; } +fsck_chk_checkpoint (sbi); +fsck_chk_quota_node (sbi);/* Traverse all block recursively from root inode */ fsck/mount.c +6
-3@@ -2365,8 +2365,10 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }set_cp (free_segment_count,get_free_segments (sbi));set_cp (valid_block_count, sbi->total_valid_block_count);@@ -2375,8 +2377,9 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags =update_nat_bits_flags (sb, cp, flags);set_cp (ckpt_flags, flags); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )fsck/resize.c +5
-5@@ -90,11 +90,11 @@ static int get_new_sb(struct f2fs_super_block *sb)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); }@@ -520,8 +520,8 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi, (unsigned char *)cp); new_cp->checkpoint_ver =cpu_to_le64 (cp_ver +1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)new_cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)new_cp +get_cp (checksum_offset))) =cpu_to_le32 (crc);/* Write a new checkpoint in the other set */ mkfs/f2fs_format.c +11
-10@@ -358,13 +358,11 @@ static int f2fs_prepare_super_block(void)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET - -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size = -CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); } max_nat_segments = (max_nat_bitmap_size *8 ) >> log_blks_per_seg;@@ -708,10 +706,13 @@ static int f2fs_write_check_point_pack(void)set_cp (nat_ver_bitmap_bytesize, ((get_sb (segment_count_nat) /2 ) <<get_sb (log_blocks_per_seg)) /8 ); -set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +if (c.large_nat_bitmap) +set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +else +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); blk_size_bytes =1 <<get_sb (log_blocksize);@@ -956,8 +957,8 @@ static int f2fs_write_check_point_pack(void)*/ cp->checkpoint_ver =0 ; - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); cp_seg_blk =get_sb (segment0_blkaddr) + c.blks_per_seg; DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",Signature · 1 line
2.18.0.rc1.
Quoted 70 lines · Jaegeuk Kim
Quoted 69 lines · Chao Yu
Quoted 39 lines · Jaegeuk Kim
Quoted 30 lines · Chao Yu
For
large_nat_bitmapfeature, there is a design flaw:Previous:
struct
f2fs_checkpointlayout:+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----|-------+ | ...... | | | | checksum_value |<-----+ | +--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+Obviously, if
nat_bitmapsize +sit_bitmapsize is larger thanMAX_BITMAP_SIZE_IN_CKPT,nat_bitmaporsit_bitmapmay overlap checkpoint checksum's position, oncecheckpoint()is triggered from kernel, nat or sit bitmap will be damaged by checksum field.In order to fix this, let's relocate checksum_value's position to the head of
sit_nat_version_bitmapas below, then nat/sit bitmap and chksum value update will become safe.After:
Quoted 7 lines
struct
f2fs_checkpointlayout:+--------------------------+ 0x0000 | checkpoint_ver | | ...... | | checksum_offset |------+ | ...... | | | sit_nat_version_bitmap[] |<-----+Quoted 1 line
We can't write checksum in the middle of checkpoint for checksum data?
Jaegeuk,
inode.i_inode_checksum locates in the middle of inode layout, we calculate
i_inode_checksumwith superposition method, so there is no restriction in technique aspect, right?
f2fs_inode_chksum()
{
...chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino, sizeof(ino)); chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen)); chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset); chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size); offset += cs_size; chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset, F2FS_BLKSIZE - offset); return chksum; }Quoted 5 lines
Quoted 3 lines
| ...... |<-------------+ | | |I know it's very ugly tho, can we move
large_bitmapsby 4 bytes?
Quoted 8 lines
Quoted 6 lines
Have thought about this way... but... actually, as you said, the implementation will be ugly. So I choose the easy way.
If you insist, I can change to below implementation, any particular reason to use this implementation? To keep checksum locating in fixed position, or protect more cp park's data..?
Okay, could you prepare a patch to allow
checksum_valuein any offset first?
No problem. :)
Thanks,
Quoted 248 lines
Thanks,
Quoted 243 lines
Thanks,
Quoted 240 lines
Something like:
...
sit_nat_version_bitmap[0]
...
sit_nat_version_bitmap[N]checksum_value: 4B
sit_nat_version_bitmap[N+1]
...
sit_nat_version_bitmap[M]Quoted 226 lines
+--------------------------+ 0x1000 | | | nat_bitmap + sit_bitmap | payload blocks | | | | | +--------------------------|<-------------+Related report and discussion:
https://sourceforge.net/p/linux-f2fs/mailman/message/36642346/
In addition, we did below change as well:
- useMAX_BITMAP_SIZE_IN_CKPTto clean up codes.
- useget_cp(checksum_offset)instead of hard codeCP_CHKSUM_OFFSETmacro.Reported-by Park Ju Hyung <[email protected]>Signed-off-by Chao Yu <[email protected]>---
v2: detect deprecated layout with fsck.fsck/f2fs.h | 6 +++++- fsck/fsck.c | 22 +++++++++++++++++++--- fsck/fsck.h | 1 + fsck/main.c | 2 ++ fsck/mount.c | 9 ++++++--- fsck/resize.c | 10 +++++----- mkfs/f2fs_format.c | 21 +++++++++++---------- 7 files changed, 49 insertions(+), 22 deletions(-)fsck/f2fs.h +5
-1@@ -272,7 +272,11 @@ static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)if (is_set_ckpt_flags (ckpt,CP_LARGE_NAT_BITMAP_FLAG )) { offset = (flag ==SIT_BITMAP ) ?le32_to_cpu (ckpt->nat_ver_bitmap_bytesize) :0 ; -return &ckpt->sit_nat_version_bitmap + offset; +/* +* if large_nat_bitmap feature is enabled, leave checksum +* protection for all nat/sit bitmaps. +*/ +return &ckpt->sit_nat_version_bitmap + offset +sizeof (__le32); }if (le32_to_cpu (F2FS_RAW_SUPER (sbi)->cp_payload) >0 ) {fsck/fsck.c +19
-3@@ -1917,6 +1917,19 @@ int fsck_chk_meta(struct f2fs_sb_info *sbi)return 0 ; } +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi) +{ +struct f2fs_checkpoint *cp =F2FS_CKPT (sbi); + +if (get_cp (ckpt_flags) &CP_LARGE_NAT_BITMAP_FLAG ) { +if (get_cp (checksum_offset) != +sizeof (struct f2fs_checkpoint ) -1 ) { +ASSERT_MSG ("Deprecated layout of large_nat_bitmap, " +"chksum_offset:%u" ,get_cp (checksum_offset)); + } + } +} +void fsck_init (struct f2fs_sb_info *sbi) {struct f2fs_fsck *fsck =F2FS_FSCK (sbi);@@ -2038,8 +2051,10 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }if (flags &CP_UMOUNT_FLAG ) cp_blocks =8 ;@@ -2058,8 +2073,9 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi)set_cp (valid_node_count, fsck->chk.valid_node_cnt);set_cp (valid_inode_count, fsck->chk.valid_inode_cnt); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )fsck/fsck.h +1
-0@@ -154,6 +154,7 @@ extern int fsck_chk_dentry_blk(struct f2fs_sb_info *, u32, struct child_info *, int, int);int fsck_chk_inline_dentries (struct f2fs_sb_info *,struct f2fs_node *,struct child_info *); +void fsck_chk_checkpoint (struct f2fs_sb_info *sbi);int fsck_chk_meta (struct f2fs_sb_info *sbi);int fsck_chk_curseg_info (struct f2fs_sb_info *);int convert_encrypted_name (unsigned char *,u32 ,unsigned char *,int );fsck/main.c +2
-0@@ -616,6 +616,8 @@ static void do_fsck(struct f2fs_sb_info *sbi) c.fix_on =1 ; } +fsck_chk_checkpoint (sbi); +fsck_chk_quota_node (sbi);/* Traverse all block recursively from root inode */ fsck/mount.c +6
-3@@ -2365,8 +2365,10 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags |=CP_TRIMMED_FLAG ;if (is_set_ckpt_flags (cp,CP_DISABLED_FLAG )) flags |=CP_DISABLED_FLAG ; -if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) +if (is_set_ckpt_flags (cp,CP_LARGE_NAT_BITMAP_FLAG )) { flags |=CP_LARGE_NAT_BITMAP_FLAG ; +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); + }set_cp (free_segment_count,get_free_segments (sbi));set_cp (valid_block_count, sbi->total_valid_block_count);@@ -2375,8 +2377,9 @@ void write_checkpoint(struct f2fs_sb_info *sbi) flags =update_nat_bits_flags (sb, cp, flags);set_cp (ckpt_flags, flags); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) =cpu_to_le32 (crc); + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) = +cpu_to_le32 (crc); cp_blk_no =get_sb (cp_blkaddr);if (sbi->cur_cp ==2 )fsck/resize.c +5
-5@@ -90,11 +90,11 @@ static int get_new_sb(struct f2fs_super_block *sb)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size =CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); }@@ -520,8 +520,8 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi, (unsigned char *)cp); new_cp->checkpoint_ver =cpu_to_le64 (cp_ver +1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)new_cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , new_cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)new_cp +get_cp (checksum_offset))) =cpu_to_le32 (crc);/* Write a new checkpoint in the other set */ mkfs/f2fs_format.c +11
-10@@ -358,13 +358,11 @@ static int f2fs_prepare_super_block(void)* It requires more pages for cp. */ if (max_sit_bitmap_size >MAX_SIT_BITMAP_SIZE_IN_CKPT ) { - max_nat_bitmap_size =CP_CHKSUM_OFFSET - -sizeof (struct f2fs_checkpoint ) +1 ; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT ;set_sb (cp_payload,F2FS_BLK_ALIGN (max_sit_bitmap_size)); }else { - max_nat_bitmap_size = -CP_CHKSUM_OFFSET -sizeof (struct f2fs_checkpoint ) +1 - - max_sit_bitmap_size; + max_nat_bitmap_size =MAX_BITMAP_SIZE_IN_CKPT - + max_sit_bitmap_size;set_sb (cp_payload,0 ); } max_nat_segments = (max_nat_bitmap_size *8 ) >> log_blks_per_seg;@@ -708,10 +706,13 @@ static int f2fs_write_check_point_pack(void)set_cp (nat_ver_bitmap_bytesize, ((get_sb (segment_count_nat) /2 ) <<get_sb (log_blocks_per_seg)) /8 ); -set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +if (c.large_nat_bitmap) +set_cp (checksum_offset,CP_CHKSUM_OFFSET ); +else +set_cp (checksum_offset,sizeof (struct f2fs_checkpoint ) -1 ); - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); blk_size_bytes =1 <<get_sb (log_blocksize);@@ -956,8 +957,8 @@ static int f2fs_write_check_point_pack(void)*/ cp->checkpoint_ver =0 ; - crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,CP_CHKSUM_OFFSET ); - *((__le32 *)((unsigned char *)cp +CP_CHKSUM_OFFSET )) = + crc =f2fs_cal_crc32 (F2FS_SUPER_MAGIC , cp,get_cp (checksum_offset)); + *((__le32 *)((unsigned char *)cp +get_cp (checksum_offset))) =cpu_to_le32 (crc); cp_seg_blk =get_sb (segment0_blkaddr) + c.blks_per_seg; DBG(1, "\tWriting cp page 1 of checkpoint pack 2, at offset 0x%08"PRIx64"\n",Signature · 1 line
2.18.0.rc1.
.