Re: [f2fs-dev] [RFC PATCH 3/3] f2fs-tools: introduce sb checksum

2018-08-07 Thread Chao Yu
On 2018/8/7 17:17, Junling Zheng wrote:
> This patch introduced superblock checksum.
> 
> Signed-off-by: Junling Zheng 
> ---
>  fsck/mount.c   | 52 ++
>  fsck/resize.c  | 11 ++
>  include/f2fs_fs.h  |  6 +-
>  mkfs/f2fs_format.c |  9 
>  4 files changed, 77 insertions(+), 1 deletion(-)
> 
> diff --git a/fsck/mount.c b/fsck/mount.c
> index 957f531..b29af3c 100644
> --- a/fsck/mount.c
> +++ b/fsck/mount.c
> @@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
>   DISP_u32(sb, node_ino);
>   DISP_u32(sb, meta_ino);
>   DISP_u32(sb, cp_payload);
> + DISP_u32(sb, crc);
>   DISP("%-.256s", sb, version);
>   printf("\n");
>  }
> @@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
>   if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
>   MSG(0, "%s", " lost_found");
>   }
> + if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
> + MSG(0, "%s", " sb_checksum");
> + }
>   MSG(0, "\n");
>   MSG(0, "Info: superblock encrypt level = %d, salt = ",
>   sb->encryption_level);
> @@ -602,6 +606,20 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
> u64 offset)
>  
>   if (sanity_check_area_boundary(sb, offset))
>   return -1;
> +
> + if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
> + if (CHKSUM_OFFSET_SB != get_sb(checksum_offset)) {
> + MSG(0, "\tInvalid SB CRC offset: %u\n",
> + get_sb(checksum_offset));
> + return -1;
> + }
> + if (f2fs_crc_valid(get_sb(crc), sb,
> + get_sb(checksum_offset))) {
> + MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
> + return -1;
> + }
> + }
> +
>   return 0;
>  }
>  
> @@ -609,6 +627,7 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
> block)
>  {
>   u64 offset;
>   char buf[F2FS_BLKSIZE];
> + u32 old_crc, new_crc;
>  
>   sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
>  
> @@ -646,6 +665,18 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
> block)
>  
>   memcpy(sbi->raw_super->version,
>   c.version, VERSION_LEN);
> + if (le32_to_cpu(sbi->raw_super->feature) &
> + F2FS_FEATURE_SB_CHKSUM) {
> + /* Recalculate CRC */
> + old_crc = le32_to_cpu(sbi->raw_super->crc);
> + new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC,
> + sbi->raw_super,
> + 
> CHKSUM_OFFSET_SB);
> + sbi->raw_super->crc = cpu_to_le32(new_crc);
> + MSG(0, "Info: update SB CRC successfully"
> + "(0x%x --> 0x%x)\n", old_crc, new_crc);
> + }

As most of these codes are copied one, how about introducing functions to avoid
redundant codes and improve readability:

verify_sb_checksum()
update_sb_checksum(, bool initialize/update)


> +
>   ret = dev_write(sbi->raw_super, offset,
>   sizeof(struct f2fs_super_block));
>   ASSERT(ret >= 0);
> @@ -2453,6 +2484,7 @@ static int check_sector_size(struct f2fs_super_block 
> *sb)
>   int index;
>   u_int32_t log_sectorsize, log_sectors_per_block;
>   u_int8_t *zero_buff;
> + u32 old_crc, new_crc;
>  
>   log_sectorsize = log_base_2(c.sector_size);
>   log_sectors_per_block = log_base_2(c.sectors_per_blk);
> @@ -2467,6 +2499,16 @@ static int check_sector_size(struct f2fs_super_block 
> *sb)
>   set_sb(log_sectorsize, log_sectorsize);
>   set_sb(log_sectors_per_block, log_sectors_per_block);
>  
> + if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
> + /* Recalculate CRC */
> + old_crc = get_sb(crc);
> + new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
> + CHKSUM_OFFSET_SB);
> + set_sb(crc, new_crc);
> + MSG(0, "Info: update SB CRC successfully"
> + "(0x%x --> 0x%x)\n", old_crc, new_crc);
> + }
> +
>   memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
>   DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
>   for (index = 0; index < 2; index++) {
> @@ -2485,6 +2527,7 @@ static int check_sector_size(struct f2fs_super_block 
> *sb)
>  static void tune_sb_features(struct f2fs_sb_info *sbi)
>  {
>   int sb_changed = 0;
> + u32 old_crc, new_crc;
>   struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
>  
>   

[f2fs-dev] [RFC PATCH 3/3] f2fs-tools: introduce sb checksum

2018-08-07 Thread Junling Zheng
This patch introduced superblock checksum.

Signed-off-by: Junling Zheng 
---
 fsck/mount.c   | 52 ++
 fsck/resize.c  | 11 ++
 include/f2fs_fs.h  |  6 +-
 mkfs/f2fs_format.c |  9 
 4 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 957f531..b29af3c 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
+   DISP_u32(sb, crc);
DISP("%-.256s", sb, version);
printf("\n");
 }
@@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
MSG(0, "%s", " lost_found");
}
+   if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
+   MSG(0, "%s", " sb_checksum");
+   }
MSG(0, "\n");
MSG(0, "Info: superblock encrypt level = %d, salt = ",
sb->encryption_level);
@@ -602,6 +606,20 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
 
if (sanity_check_area_boundary(sb, offset))
return -1;
+
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
+   if (CHKSUM_OFFSET_SB != get_sb(checksum_offset)) {
+   MSG(0, "\tInvalid SB CRC offset: %u\n",
+   get_sb(checksum_offset));
+   return -1;
+   }
+   if (f2fs_crc_valid(get_sb(crc), sb,
+   get_sb(checksum_offset))) {
+   MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
+   return -1;
+   }
+   }
+
return 0;
 }
 
@@ -609,6 +627,7 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
block)
 {
u64 offset;
char buf[F2FS_BLKSIZE];
+   u32 old_crc, new_crc;
 
sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
 
@@ -646,6 +665,18 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
block)
 
memcpy(sbi->raw_super->version,
c.version, VERSION_LEN);
+   if (le32_to_cpu(sbi->raw_super->feature) &
+   F2FS_FEATURE_SB_CHKSUM) {
+   /* Recalculate CRC */
+   old_crc = le32_to_cpu(sbi->raw_super->crc);
+   new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC,
+   sbi->raw_super,
+   
CHKSUM_OFFSET_SB);
+   sbi->raw_super->crc = cpu_to_le32(new_crc);
+   MSG(0, "Info: update SB CRC successfully"
+   "(0x%x --> 0x%x)\n", old_crc, new_crc);
+   }
+
ret = dev_write(sbi->raw_super, offset,
sizeof(struct f2fs_super_block));
ASSERT(ret >= 0);
@@ -2453,6 +2484,7 @@ static int check_sector_size(struct f2fs_super_block *sb)
int index;
u_int32_t log_sectorsize, log_sectors_per_block;
u_int8_t *zero_buff;
+   u32 old_crc, new_crc;
 
log_sectorsize = log_base_2(c.sector_size);
log_sectors_per_block = log_base_2(c.sectors_per_blk);
@@ -2467,6 +2499,16 @@ static int check_sector_size(struct f2fs_super_block *sb)
set_sb(log_sectorsize, log_sectorsize);
set_sb(log_sectors_per_block, log_sectors_per_block);
 
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
+   /* Recalculate CRC */
+   old_crc = get_sb(crc);
+   new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
+   CHKSUM_OFFSET_SB);
+   set_sb(crc, new_crc);
+   MSG(0, "Info: update SB CRC successfully"
+   "(0x%x --> 0x%x)\n", old_crc, new_crc);
+   }
+
memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
for (index = 0; index < 2; index++) {
@@ -2485,6 +2527,7 @@ static int check_sector_size(struct f2fs_super_block *sb)
 static void tune_sb_features(struct f2fs_sb_info *sbi)
 {
int sb_changed = 0;
+   u32 old_crc, new_crc;
struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
 
if (!(sb->feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) &&
@@ -2499,6 +2542,15 @@ static void tune_sb_features(struct f2fs_sb_info *sbi)
if (!sb_changed)
return;
 
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
+   /* Recalculate CRC */
+   old_crc = get_sb(crc);
+