* [RFC PATCH v3 1/3] f2fs: support superblock checksum
@ 2018-08-08 10:27 Junling Zheng
2018-08-08 10:27 ` [RFC PATCH v2 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET Junling Zheng
` (3 more replies)
0 siblings, 4 replies; 12+ messages in thread
From: Junling Zheng @ 2018-08-08 10:27 UTC (permalink / raw)
To: jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
Now we support crc32 checksum for superblock.
Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
---
v2 -> v3:
- add sysfs entry for superblock checksum.
- move the crc checking to the beginning of sanity_check_raw_super().
- fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
sanity_check_raw_super().
v1 -> v2:
- fix to switch endian of crc.
fs/f2fs/f2fs.h | 2 ++
fs/f2fs/super.c | 29 +++++++++++++++++++++++++++++
fs/f2fs/sysfs.c | 7 +++++++
include/linux/f2fs_fs.h | 3 ++-
4 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
#define F2FS_FEATURE_INODE_CRTIME 0x0100
#define F2FS_FEATURE_LOST_FOUND 0x0200
#define F2FS_FEATURE_VERITY 0x0400 /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
#define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
#ifdef CONFIG_BLK_DEV_ZONED
static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..c3dbafa31613 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+ size_t crc_offset = 0;
+ __u32 crc = 0;
+
+ /* Check checksum_offset and crc in superblock */
+ if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+ crc_offset = le32_to_cpu(raw_super->checksum_offset);
+ if (crc_offset !=
+ offsetof(struct f2fs_super_block, crc)) {
+ f2fs_msg(sb, KERN_INFO,
+ "Invalid SB checksum offset: %lu",
+ crc_offset);
+ return 1;
+ }
+ crc = le32_to_cpu(raw_super->crc);
+ if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+ f2fs_msg(sb, KERN_INFO,
+ "Invalid SB checksum value: %u", crc);
+ return 1;
+ }
+ }
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
{
struct buffer_head *bh;
+ __u32 crc = 0;
int err;
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
return -EROFS;
}
+ /* we should update superblock crc here */
+ if (!recover &&
+ F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
+ crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+ offsetof(struct f2fs_super_block, crc));
+ F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+ }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+ if (f2fs_sb_has_sb_chksum(sb))
+ len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+ len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
}
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+ FEAT_SB_CHECKSUM,
};
static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+ case FEAT_SB_CHECKSUM:
return snprintf(buf, PAGE_SIZE, "supported\n");
}
return 0;
@@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
+F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
static struct attribute *f2fs_attrs[] = {
@@ -489,6 +495,7 @@ static struct attribute *f2fs_feat_attrs[] = {
ATTR_LIST(quota_ino),
ATTR_LIST(inode_crtime),
ATTR_LIST(lost_found),
+ ATTR_LIST(sb_checksum),
NULL,
};
diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
index f70f8ac9c4f4..aa4b586569c1 100644
--- a/include/linux/f2fs_fs.h
+++ b/include/linux/f2fs_fs.h
@@ -112,7 +112,8 @@ struct f2fs_super_block {
struct f2fs_device devs[MAX_DEVICES]; /* device list */
__le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
__u8 hot_ext_count; /* # of hot file extension */
- __u8 reserved[314]; /* valid reserved region */
+ __u8 reserved[310]; /* valid reserved region */
+ __le32 crc; /* checksum of superblock */
} __packed;
/*
--
2.18.0
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH v2 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET
2018-08-08 10:27 [RFC PATCH v3 1/3] f2fs: support superblock checksum Junling Zheng
@ 2018-08-08 10:27 ` Junling Zheng
2018-08-08 13:08 ` Chao Yu
2018-08-08 10:27 ` [RFC PATCH v2 3/3] f2fs-tools: introduce sb checksum Junling Zheng
` (2 subsequent siblings)
3 siblings, 1 reply; 12+ messages in thread
From: Junling Zheng @ 2018-08-08 10:27 UTC (permalink / raw)
To: jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
This patch renamed CHECKSUM_OFFSET to CP_CHKSUM_OFFSET.
Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
---
v1 -> v2:
- Rename CHKSUM_OFFSET_CP to CP_CHKSUM_OFFSET.
fsck/fsck.c | 4 ++--
fsck/mount.c | 4 ++--
fsck/resize.c | 8 ++++----
include/f2fs_fs.h | 6 +++---
mkfs/f2fs_format.c | 14 +++++++-------
5 files changed, 18 insertions(+), 18 deletions(-)
diff --git a/fsck/fsck.c b/fsck/fsck.c
index d550403..f080d3c 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2010,8 +2010,8 @@ 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, CHECKSUM_OFFSET);
- *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+ crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+ *((__le32 *)((unsigned char *)cp + CP_CHKSUM_OFFSET)) = cpu_to_le32(crc);
cp_blk_no = get_sb(cp_blkaddr);
if (sbi->cur_cp == 2)
diff --git a/fsck/mount.c b/fsck/mount.c
index 8fb4d59..58ef3e6 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2187,8 +2187,8 @@ 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, CHECKSUM_OFFSET);
- *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+ crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+ *((__le32 *)((unsigned char *)cp + CP_CHKSUM_OFFSET)) = cpu_to_le32(crc);
cp_blk_no = get_sb(cp_blkaddr);
if (sbi->cur_cp == 2)
diff --git a/fsck/resize.c b/fsck/resize.c
index fe8a61a..e9612b3 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,10 +90,10 @@ 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 = CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1;
+ max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
- max_nat_bitmap_size = CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1
+ max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1
- 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, CHECKSUM_OFFSET);
- *((__le32 *)((unsigned char *)new_cp + CHECKSUM_OFFSET)) =
+ crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CP_CHKSUM_OFFSET);
+ *((__le32 *)((unsigned char *)new_cp + CP_CHKSUM_OFFSET)) =
cpu_to_le32(crc);
/* Write a new checkpoint in the other set */
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 53fa002..e279b9f 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -278,7 +278,7 @@ static inline uint64_t bswap_64(uint64_t val)
#define PAGE_CACHE_SIZE 4096
#define BITS_PER_BYTE 8
#define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */
-#define CHECKSUM_OFFSET 4092
+#define CP_CHKSUM_OFFSET 4092
#define MAX_PATH_LEN 64
#define MAX_DEVICES 8
@@ -682,9 +682,9 @@ struct f2fs_checkpoint {
} __attribute__((packed));
#define MAX_SIT_BITMAP_SIZE_IN_CKPT \
- (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
+ (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
#define MAX_BITMAP_SIZE_IN_CKPT \
- (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
+ (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
/*
* For orphan inode management
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4b88d93..621126c 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -342,12 +342,12 @@ 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 = CHECKSUM_OFFSET -
+ max_nat_bitmap_size = CP_CHKSUM_OFFSET -
sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
max_nat_bitmap_size =
- CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1
+ CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1
- max_sit_bitmap_size;
set_sb(cp_payload, 0);
}
@@ -684,10 +684,10 @@ 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, CHECKSUM_OFFSET);
+ set_cp(checksum_offset, CP_CHKSUM_OFFSET);
- crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CHECKSUM_OFFSET);
- *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) =
+ crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+ *((__le32 *)((unsigned char *)cp + CP_CHKSUM_OFFSET)) =
cpu_to_le32(crc);
blk_size_bytes = 1 << get_sb(log_blocksize);
@@ -932,8 +932,8 @@ static int f2fs_write_check_point_pack(void)
*/
cp->checkpoint_ver = 0;
- crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CHECKSUM_OFFSET);
- *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) =
+ crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+ *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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",
--
2.18.0
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH v2 3/3] f2fs-tools: introduce sb checksum
2018-08-08 10:27 [RFC PATCH v3 1/3] f2fs: support superblock checksum Junling Zheng
2018-08-08 10:27 ` [RFC PATCH v2 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET Junling Zheng
@ 2018-08-08 10:27 ` Junling Zheng
2018-08-08 13:25 ` Chao Yu
2018-08-08 12:03 ` [RFC PATCH v3 1/3] f2fs: support superblock checksum Chao Yu
2018-08-09 0:20 ` [RFC PATCH v3 " Jaegeuk Kim
3 siblings, 1 reply; 12+ messages in thread
From: Junling Zheng @ 2018-08-08 10:27 UTC (permalink / raw)
To: jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
This patch introduced superblock checksum.
Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
---
v1 -> v2:
- Rename CHKSUM_OFFSET_SB to SB_CHKSUM_OFFSET.
- Introduce update_sb_chksum() to update superblock checksum.
fsck/fsck.h | 1 +
fsck/mount.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
fsck/resize.c | 3 +++
include/f2fs_fs.h | 6 +++++-
mkfs/f2fs_format.c | 9 +++++++++
5 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/fsck/fsck.h b/fsck/fsck.h
index 6042e68..2172cac 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -170,6 +170,7 @@ extern int fsck_verify(struct f2fs_sb_info *);
extern void fsck_free(struct f2fs_sb_info *);
extern int f2fs_do_mount(struct f2fs_sb_info *);
extern void f2fs_do_umount(struct f2fs_sb_info *);
+extern void update_sb_chksum(struct f2fs_super_block *);
extern void flush_journal_entries(struct f2fs_sb_info *);
extern void zero_journal_entries(struct f2fs_sb_info *);
diff --git a/fsck/mount.c b/fsck/mount.c
index 58ef3e6..1dfabba 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);
@@ -558,10 +562,29 @@ static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
return 0;
}
+static int verify_sb_chksum(struct f2fs_super_block *sb)
+{
+ if (SB_CHKSUM_OFFSET != 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;
+}
+
int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
{
unsigned int blocksize;
+ if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
+ verify_sb_chksum(sb))
+ return -1;
+
if (F2FS_SUPER_MAGIC != get_sb(magic))
return -1;
@@ -605,6 +628,18 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
return 0;
}
+void update_sb_chksum(struct f2fs_super_block *sb)
+{
+ u32 old_crc, new_crc;
+
+ old_crc = get_sb(crc);
+ new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
+ SB_CHKSUM_OFFSET);
+ set_sb(crc, new_crc);
+ MSG(0, "Info: update SB CRC successfully "
+ "(0x%x --> 0x%x)\n", old_crc, new_crc);
+}
+
int validate_super_block(struct f2fs_sb_info *sbi, int block)
{
u64 offset;
@@ -646,6 +681,10 @@ 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)
+ update_sb_chksum(sbi->raw_super);
+
ret = dev_write(sbi->raw_super, offset,
sizeof(struct f2fs_super_block));
ASSERT(ret >= 0);
@@ -2402,6 +2441,9 @@ 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)
+ update_sb_chksum(sb);
+
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++) {
@@ -2434,6 +2476,8 @@ static void tune_sb_features(struct f2fs_sb_info *sbi)
if (!sb_changed)
return;
+ if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)
+ update_sb_chksum(sb);
write_superblock(sb);
}
diff --git a/fsck/resize.c b/fsck/resize.c
index e9612b3..ca8c6f9 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -160,6 +160,9 @@ safe_resize:
(get_sb(segment_count_main) - 2));
return -1;
}
+ if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)
+ update_sb_chksum(sb);
+
return 0;
}
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index e279b9f..b6dfd29 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -279,6 +279,7 @@ static inline uint64_t bswap_64(uint64_t val)
#define BITS_PER_BYTE 8
#define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */
#define CP_CHKSUM_OFFSET 4092
+#define SB_CHKSUM_OFFSET 3068
#define MAX_PATH_LEN 64
#define MAX_DEVICES 8
@@ -579,6 +580,7 @@ enum {
#define F2FS_FEATURE_INODE_CRTIME 0x0100
#define F2FS_FEATURE_LOST_FOUND 0x0200
#define F2FS_FEATURE_VERITY 0x0400 /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
#define MAX_VOLUME_NAME 512
@@ -632,7 +634,8 @@ struct f2fs_super_block {
struct f2fs_device devs[MAX_DEVICES]; /* device list */
__le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
__u8 hot_ext_count; /* # of hot file extension */
- __u8 reserved[314]; /* valid reserved region */
+ __u8 reserved[310]; /* valid reserved region */
+ __le32 crc; /* checksum of superblock */
} __attribute__((packed));
/*
@@ -1337,6 +1340,7 @@ struct feature feature_table[] = { \
{ "inode_crtime", F2FS_FEATURE_INODE_CRTIME }, \
{ "lost_found", F2FS_FEATURE_LOST_FOUND }, \
{ "verity", F2FS_FEATURE_VERITY }, /* reserved */ \
+ { "sb_checksum", F2FS_FEATURE_SB_CHKSUM }, \
{ NULL, 0x0}, \
};
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 621126c..012f3a8 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -187,6 +187,7 @@ static int f2fs_prepare_super_block(void)
u_int32_t sit_bitmap_size, max_sit_bitmap_size;
u_int32_t max_nat_bitmap_size, max_nat_segments;
u_int32_t total_zones;
+ u_int32_t crc;
enum quota_type qtype;
int i;
@@ -504,6 +505,14 @@ static int f2fs_prepare_super_block(void)
sb->feature = c.feature;
+ if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
+ set_sb(checksum_offset, SB_CHKSUM_OFFSET);
+ /* calculate superblock checksum */
+ crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb, SB_CHKSUM_OFFSET);
+ set_sb(crc, crc);
+ MSG(0, "Info: set SB CRC successfully (0x%x)\n", crc);
+ }
+
return 0;
}
--
2.18.0
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [RFC PATCH v3 1/3] f2fs: support superblock checksum
2018-08-08 10:27 [RFC PATCH v3 1/3] f2fs: support superblock checksum Junling Zheng
2018-08-08 10:27 ` [RFC PATCH v2 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET Junling Zheng
2018-08-08 10:27 ` [RFC PATCH v2 3/3] f2fs-tools: introduce sb checksum Junling Zheng
@ 2018-08-08 12:03 ` Chao Yu
2018-08-08 12:21 ` Junling Zheng
2018-08-09 0:20 ` [RFC PATCH v3 " Jaegeuk Kim
3 siblings, 1 reply; 12+ messages in thread
From: Chao Yu @ 2018-08-08 12:03 UTC (permalink / raw)
To: Junling Zheng, jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
On 2018/8/8 18:27, Junling Zheng wrote:
> Now we support crc32 checksum for superblock.
>
> Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
> ---
> v2 -> v3:
> - add sysfs entry for superblock checksum.
> - move the crc checking to the beginning of sanity_check_raw_super().
> - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
> sanity_check_raw_super().
> v1 -> v2:
> - fix to switch endian of crc.
> fs/f2fs/f2fs.h | 2 ++
> fs/f2fs/super.c | 29 +++++++++++++++++++++++++++++
> fs/f2fs/sysfs.c | 7 +++++++
> include/linux/f2fs_fs.h | 3 ++-
> 4 files changed, 40 insertions(+), 1 deletion(-)
>
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 4525f4f82af0..d50d6efda96b 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -147,6 +147,7 @@ struct f2fs_mount_info {
> #define F2FS_FEATURE_INODE_CRTIME 0x0100
> #define F2FS_FEATURE_LOST_FOUND 0x0200
> #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
> +#define F2FS_FEATURE_SB_CHKSUM 0x0800
>
> #define F2FS_HAS_FEATURE(sb, mask) \
> ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
> @@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
> F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
> F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
> F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
> +F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
>
> #ifdef CONFIG_BLK_DEV_ZONED
> static inline int get_blkz_type(struct f2fs_sb_info *sbi,
> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
> index bd57be470e23..c3dbafa31613 100644
> --- a/fs/f2fs/super.c
> +++ b/fs/f2fs/super.c
> @@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
> (bh->b_data + F2FS_SUPER_OFFSET);
> struct super_block *sb = sbi->sb;
> unsigned int blocksize;
> + size_t crc_offset = 0;
> + __u32 crc = 0;
> +
> + /* Check checksum_offset and crc in superblock */
> + if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
> + crc_offset = le32_to_cpu(raw_super->checksum_offset);
> + if (crc_offset !=
> + offsetof(struct f2fs_super_block, crc)) {
> + f2fs_msg(sb, KERN_INFO,
> + "Invalid SB checksum offset: %lu",
warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument
4 has type ‘size_t’ [-Wformat=]
crc_offset);
Otherwise, it looks good to me, you can add in next version:
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Thanks,
> + crc_offset);
> + return 1;
> + }
> + crc = le32_to_cpu(raw_super->crc);
> + if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
> + f2fs_msg(sb, KERN_INFO,
> + "Invalid SB checksum value: %u", crc);
> + return 1;
> + }
> + }
>
> if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
> f2fs_msg(sb, KERN_INFO,
> @@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
> int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
> {
> struct buffer_head *bh;
> + __u32 crc = 0;
> int err;
>
> if ((recover && f2fs_readonly(sbi->sb)) ||
> @@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
> return -EROFS;
> }
>
> + /* we should update superblock crc here */
> + if (!recover &&
> + F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
> + crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
> + offsetof(struct f2fs_super_block, crc));
> + F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
> + }
> +
> /* write back-up superblock first */
> bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
> if (!bh)
> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
> index cd2e030e47b8..c86d91be6c48 100644
> --- a/fs/f2fs/sysfs.c
> +++ b/fs/f2fs/sysfs.c
> @@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
> if (f2fs_sb_has_lost_found(sb))
> len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
> len ? ", " : "", "lost_found");
> + if (f2fs_sb_has_sb_chksum(sb))
> + len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
> + len ? ", " : "", "sb_checksum");
> len += snprintf(buf + len, PAGE_SIZE - len, "\n");
> return len;
> }
> @@ -337,6 +340,7 @@ enum feat_id {
> FEAT_QUOTA_INO,
> FEAT_INODE_CRTIME,
> FEAT_LOST_FOUND,
> + FEAT_SB_CHECKSUM,
> };
>
> static ssize_t f2fs_feature_show(struct f2fs_attr *a,
> @@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
> case FEAT_QUOTA_INO:
> case FEAT_INODE_CRTIME:
> case FEAT_LOST_FOUND:
> + case FEAT_SB_CHECKSUM:
> return snprintf(buf, PAGE_SIZE, "supported\n");
> }
> return 0;
> @@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
> F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
> F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
> F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
> +F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
>
> #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
> static struct attribute *f2fs_attrs[] = {
> @@ -489,6 +495,7 @@ static struct attribute *f2fs_feat_attrs[] = {
> ATTR_LIST(quota_ino),
> ATTR_LIST(inode_crtime),
> ATTR_LIST(lost_found),
> + ATTR_LIST(sb_checksum),
> NULL,
> };
>
> diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
> index f70f8ac9c4f4..aa4b586569c1 100644
> --- a/include/linux/f2fs_fs.h
> +++ b/include/linux/f2fs_fs.h
> @@ -112,7 +112,8 @@ struct f2fs_super_block {
> struct f2fs_device devs[MAX_DEVICES]; /* device list */
> __le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
> __u8 hot_ext_count; /* # of hot file extension */
> - __u8 reserved[314]; /* valid reserved region */
> + __u8 reserved[310]; /* valid reserved region */
> + __le32 crc; /* checksum of superblock */
> } __packed;
>
> /*
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFC PATCH v3 1/3] f2fs: support superblock checksum
2018-08-08 12:03 ` [RFC PATCH v3 1/3] f2fs: support superblock checksum Chao Yu
@ 2018-08-08 12:21 ` Junling Zheng
2018-08-08 12:24 ` Chao Yu
2018-08-08 12:54 ` [RFC PATCH v4 " Junling Zheng
0 siblings, 2 replies; 12+ messages in thread
From: Junling Zheng @ 2018-08-08 12:21 UTC (permalink / raw)
To: Chao Yu, jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
On 2018/8/8 20:03, Chao Yu wrote:
> On 2018/8/8 18:27, Junling Zheng wrote:
>> Now we support crc32 checksum for superblock.
>>
>> Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
>> ---
>> v2 -> v3:
>> - add sysfs entry for superblock checksum.
>> - move the crc checking to the beginning of sanity_check_raw_super().
>> - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
>> sanity_check_raw_super().
>> v1 -> v2:
>> - fix to switch endian of crc.
>> fs/f2fs/f2fs.h | 2 ++
>> fs/f2fs/super.c | 29 +++++++++++++++++++++++++++++
>> fs/f2fs/sysfs.c | 7 +++++++
>> include/linux/f2fs_fs.h | 3 ++-
>> 4 files changed, 40 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
>> index 4525f4f82af0..d50d6efda96b 100644
>> --- a/fs/f2fs/f2fs.h
>> +++ b/fs/f2fs/f2fs.h
>> @@ -147,6 +147,7 @@ struct f2fs_mount_info {
>> #define F2FS_FEATURE_INODE_CRTIME 0x0100
>> #define F2FS_FEATURE_LOST_FOUND 0x0200
>> #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
>> +#define F2FS_FEATURE_SB_CHKSUM 0x0800
>>
>> #define F2FS_HAS_FEATURE(sb, mask) \
>> ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
>> @@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
>> F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
>> F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
>> F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
>> +F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
>>
>> #ifdef CONFIG_BLK_DEV_ZONED
>> static inline int get_blkz_type(struct f2fs_sb_info *sbi,
>> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
>> index bd57be470e23..c3dbafa31613 100644
>> --- a/fs/f2fs/super.c
>> +++ b/fs/f2fs/super.c
>> @@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
>> (bh->b_data + F2FS_SUPER_OFFSET);
>> struct super_block *sb = sbi->sb;
>> unsigned int blocksize;
>> + size_t crc_offset = 0;
>> + __u32 crc = 0;
>> +
>> + /* Check checksum_offset and crc in superblock */
>> + if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
>> + crc_offset = le32_to_cpu(raw_super->checksum_offset);
>> + if (crc_offset !=
>> + offsetof(struct f2fs_super_block, crc)) {
>> + f2fs_msg(sb, KERN_INFO,
>> + "Invalid SB checksum offset: %lu",
>
> warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument
> 4 has type ‘size_t’ [-Wformat=]
> crc_offset);
>
:( sorry, I'll send a v4 patch.
> Otherwise, it looks good to me, you can add in next version:
>
> Reviewed-by: Chao Yu <yuchao0@huawei.com>
>
> Thanks,
>
>> + crc_offset);
>> + return 1;
>> + }
>> + crc = le32_to_cpu(raw_super->crc);
>> + if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
>> + f2fs_msg(sb, KERN_INFO,
>> + "Invalid SB checksum value: %u", crc);
>> + return 1;
>> + }
>> + }
>>
>> if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
>> f2fs_msg(sb, KERN_INFO,
>> @@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
>> int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>> {
>> struct buffer_head *bh;
>> + __u32 crc = 0;
>> int err;
>>
>> if ((recover && f2fs_readonly(sbi->sb)) ||
>> @@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>> return -EROFS;
>> }
>>
>> + /* we should update superblock crc here */
>> + if (!recover &&
>> + F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
>> + crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
>> + offsetof(struct f2fs_super_block, crc));
>> + F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
>> + }
>> +
>> /* write back-up superblock first */
>> bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
>> if (!bh)
>> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
>> index cd2e030e47b8..c86d91be6c48 100644
>> --- a/fs/f2fs/sysfs.c
>> +++ b/fs/f2fs/sysfs.c
>> @@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
>> if (f2fs_sb_has_lost_found(sb))
>> len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
>> len ? ", " : "", "lost_found");
>> + if (f2fs_sb_has_sb_chksum(sb))
>> + len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
>> + len ? ", " : "", "sb_checksum");
>> len += snprintf(buf + len, PAGE_SIZE - len, "\n");
>> return len;
>> }
>> @@ -337,6 +340,7 @@ enum feat_id {
>> FEAT_QUOTA_INO,
>> FEAT_INODE_CRTIME,
>> FEAT_LOST_FOUND,
>> + FEAT_SB_CHECKSUM,
>> };
>>
>> static ssize_t f2fs_feature_show(struct f2fs_attr *a,
>> @@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
>> case FEAT_QUOTA_INO:
>> case FEAT_INODE_CRTIME:
>> case FEAT_LOST_FOUND:
>> + case FEAT_SB_CHECKSUM:
>> return snprintf(buf, PAGE_SIZE, "supported\n");
>> }
>> return 0;
>> @@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
>> F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
>> F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
>> F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
>> +F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
>>
>> #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
>> static struct attribute *f2fs_attrs[] = {
>> @@ -489,6 +495,7 @@ static struct attribute *f2fs_feat_attrs[] = {
>> ATTR_LIST(quota_ino),
>> ATTR_LIST(inode_crtime),
>> ATTR_LIST(lost_found),
>> + ATTR_LIST(sb_checksum),
>> NULL,
>> };
>>
>> diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
>> index f70f8ac9c4f4..aa4b586569c1 100644
>> --- a/include/linux/f2fs_fs.h
>> +++ b/include/linux/f2fs_fs.h
>> @@ -112,7 +112,8 @@ struct f2fs_super_block {
>> struct f2fs_device devs[MAX_DEVICES]; /* device list */
>> __le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
>> __u8 hot_ext_count; /* # of hot file extension */
>> - __u8 reserved[314]; /* valid reserved region */
>> + __u8 reserved[310]; /* valid reserved region */
>> + __le32 crc; /* checksum of superblock */
>> } __packed;
>>
>> /*
>>
>
> .
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFC PATCH v3 1/3] f2fs: support superblock checksum
2018-08-08 12:21 ` Junling Zheng
@ 2018-08-08 12:24 ` Chao Yu
2018-08-08 12:38 ` Junling Zheng
2018-08-08 12:54 ` [RFC PATCH v4 " Junling Zheng
1 sibling, 1 reply; 12+ messages in thread
From: Chao Yu @ 2018-08-08 12:24 UTC (permalink / raw)
To: Junling Zheng, jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
On 2018/8/8 20:21, Junling Zheng wrote:
> On 2018/8/8 20:03, Chao Yu wrote:
>> On 2018/8/8 18:27, Junling Zheng wrote:
>>> Now we support crc32 checksum for superblock.
>>>
>>> Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
>>> ---
>>> v2 -> v3:
>>> - add sysfs entry for superblock checksum.
>>> - move the crc checking to the beginning of sanity_check_raw_super().
>>> - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
>>> sanity_check_raw_super().
>>> v1 -> v2:
>>> - fix to switch endian of crc.
>>> fs/f2fs/f2fs.h | 2 ++
>>> fs/f2fs/super.c | 29 +++++++++++++++++++++++++++++
>>> fs/f2fs/sysfs.c | 7 +++++++
>>> include/linux/f2fs_fs.h | 3 ++-
>>> 4 files changed, 40 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
>>> index 4525f4f82af0..d50d6efda96b 100644
>>> --- a/fs/f2fs/f2fs.h
>>> +++ b/fs/f2fs/f2fs.h
>>> @@ -147,6 +147,7 @@ struct f2fs_mount_info {
>>> #define F2FS_FEATURE_INODE_CRTIME 0x0100
>>> #define F2FS_FEATURE_LOST_FOUND 0x0200
>>> #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
>>> +#define F2FS_FEATURE_SB_CHKSUM 0x0800
>>>
>>> #define F2FS_HAS_FEATURE(sb, mask) \
>>> ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
>>> @@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
>>> F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
>>> F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
>>> F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
>>> +F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
>>>
>>> #ifdef CONFIG_BLK_DEV_ZONED
>>> static inline int get_blkz_type(struct f2fs_sb_info *sbi,
>>> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
>>> index bd57be470e23..c3dbafa31613 100644
>>> --- a/fs/f2fs/super.c
>>> +++ b/fs/f2fs/super.c
>>> @@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
>>> (bh->b_data + F2FS_SUPER_OFFSET);
>>> struct super_block *sb = sbi->sb;
>>> unsigned int blocksize;
>>> + size_t crc_offset = 0;
>>> + __u32 crc = 0;
>>> +
>>> + /* Check checksum_offset and crc in superblock */
>>> + if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
>>> + crc_offset = le32_to_cpu(raw_super->checksum_offset);
>>> + if (crc_offset !=
>>> + offsetof(struct f2fs_super_block, crc)) {
>>> + f2fs_msg(sb, KERN_INFO,
>>> + "Invalid SB checksum offset: %lu",
>>
>> warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument
>> 4 has type ‘size_t’ [-Wformat=]
>> crc_offset);
>>
>
> :( sorry, I'll send a v4 patch.
>
>> Otherwise, it looks good to me, you can add in next version:
>>
>> Reviewed-by: Chao Yu <yuchao0@huawei.com>
>>
>> Thanks,
>>
>>> + crc_offset);
>>> + return 1;
>>> + }
>>> + crc = le32_to_cpu(raw_super->crc);
>>> + if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
>>> + f2fs_msg(sb, KERN_INFO,
>>> + "Invalid SB checksum value: %u", crc);
>>> + return 1;
>>> + }
>>> + }
>>>
>>> if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
>>> f2fs_msg(sb, KERN_INFO,
>>> @@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
>>> int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>>> {
>>> struct buffer_head *bh;
>>> + __u32 crc = 0;
>>> int err;
>>>
>>> if ((recover && f2fs_readonly(sbi->sb)) ||
>>> @@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>>> return -EROFS;
>>> }
>>>
>>> + /* we should update superblock crc here */
>>> + if (!recover &&
>>> + F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
Can we change to use f2fs_sb_has_sb_chksum()?
Thanks,
>>> + crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
>>> + offsetof(struct f2fs_super_block, crc));
>>> + F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
>>> + }
>>> +
>>> /* write back-up superblock first */
>>> bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
>>> if (!bh)
>>> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
>>> index cd2e030e47b8..c86d91be6c48 100644
>>> --- a/fs/f2fs/sysfs.c
>>> +++ b/fs/f2fs/sysfs.c
>>> @@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
>>> if (f2fs_sb_has_lost_found(sb))
>>> len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
>>> len ? ", " : "", "lost_found");
>>> + if (f2fs_sb_has_sb_chksum(sb))
>>> + len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
>>> + len ? ", " : "", "sb_checksum");
>>> len += snprintf(buf + len, PAGE_SIZE - len, "\n");
>>> return len;
>>> }
>>> @@ -337,6 +340,7 @@ enum feat_id {
>>> FEAT_QUOTA_INO,
>>> FEAT_INODE_CRTIME,
>>> FEAT_LOST_FOUND,
>>> + FEAT_SB_CHECKSUM,
>>> };
>>>
>>> static ssize_t f2fs_feature_show(struct f2fs_attr *a,
>>> @@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
>>> case FEAT_QUOTA_INO:
>>> case FEAT_INODE_CRTIME:
>>> case FEAT_LOST_FOUND:
>>> + case FEAT_SB_CHECKSUM:
>>> return snprintf(buf, PAGE_SIZE, "supported\n");
>>> }
>>> return 0;
>>> @@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
>>> F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
>>> F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
>>> F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
>>> +F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
>>>
>>> #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
>>> static struct attribute *f2fs_attrs[] = {
>>> @@ -489,6 +495,7 @@ static struct attribute *f2fs_feat_attrs[] = {
>>> ATTR_LIST(quota_ino),
>>> ATTR_LIST(inode_crtime),
>>> ATTR_LIST(lost_found),
>>> + ATTR_LIST(sb_checksum),
>>> NULL,
>>> };
>>>
>>> diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
>>> index f70f8ac9c4f4..aa4b586569c1 100644
>>> --- a/include/linux/f2fs_fs.h
>>> +++ b/include/linux/f2fs_fs.h
>>> @@ -112,7 +112,8 @@ struct f2fs_super_block {
>>> struct f2fs_device devs[MAX_DEVICES]; /* device list */
>>> __le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
>>> __u8 hot_ext_count; /* # of hot file extension */
>>> - __u8 reserved[314]; /* valid reserved region */
>>> + __u8 reserved[310]; /* valid reserved region */
>>> + __le32 crc; /* checksum of superblock */
>>> } __packed;
>>>
>>> /*
>>>
>>
>> .
>>
>
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFC PATCH v3 1/3] f2fs: support superblock checksum
2018-08-08 12:24 ` Chao Yu
@ 2018-08-08 12:38 ` Junling Zheng
0 siblings, 0 replies; 12+ messages in thread
From: Junling Zheng @ 2018-08-08 12:38 UTC (permalink / raw)
To: Chao Yu, jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
On 2018/8/8 20:24, Chao Yu wrote:
> On 2018/8/8 20:21, Junling Zheng wrote:
>> On 2018/8/8 20:03, Chao Yu wrote:
>>> On 2018/8/8 18:27, Junling Zheng wrote:
>>>> Now we support crc32 checksum for superblock.
>>>>
>>>> Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
>>>> ---
>>>> v2 -> v3:
>>>> - add sysfs entry for superblock checksum.
>>>> - move the crc checking to the beginning of sanity_check_raw_super().
>>>> - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
>>>> sanity_check_raw_super().
>>>> v1 -> v2:
>>>> - fix to switch endian of crc.
>>>> fs/f2fs/f2fs.h | 2 ++
>>>> fs/f2fs/super.c | 29 +++++++++++++++++++++++++++++
>>>> fs/f2fs/sysfs.c | 7 +++++++
>>>> include/linux/f2fs_fs.h | 3 ++-
>>>> 4 files changed, 40 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
>>>> index 4525f4f82af0..d50d6efda96b 100644
>>>> --- a/fs/f2fs/f2fs.h
>>>> +++ b/fs/f2fs/f2fs.h
>>>> @@ -147,6 +147,7 @@ struct f2fs_mount_info {
>>>> #define F2FS_FEATURE_INODE_CRTIME 0x0100
>>>> #define F2FS_FEATURE_LOST_FOUND 0x0200
>>>> #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
>>>> +#define F2FS_FEATURE_SB_CHKSUM 0x0800
>>>>
>>>> #define F2FS_HAS_FEATURE(sb, mask) \
>>>> ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
>>>> @@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
>>>> F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
>>>> F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
>>>> F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
>>>> +F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
>>>>
>>>> #ifdef CONFIG_BLK_DEV_ZONED
>>>> static inline int get_blkz_type(struct f2fs_sb_info *sbi,
>>>> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
>>>> index bd57be470e23..c3dbafa31613 100644
>>>> --- a/fs/f2fs/super.c
>>>> +++ b/fs/f2fs/super.c
>>>> @@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
>>>> (bh->b_data + F2FS_SUPER_OFFSET);
>>>> struct super_block *sb = sbi->sb;
>>>> unsigned int blocksize;
>>>> + size_t crc_offset = 0;
>>>> + __u32 crc = 0;
>>>> +
>>>> + /* Check checksum_offset and crc in superblock */
>>>> + if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
>>>> + crc_offset = le32_to_cpu(raw_super->checksum_offset);
>>>> + if (crc_offset !=
>>>> + offsetof(struct f2fs_super_block, crc)) {
>>>> + f2fs_msg(sb, KERN_INFO,
>>>> + "Invalid SB checksum offset: %lu",
>>>
>>> warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument
>>> 4 has type ‘size_t’ [-Wformat=]
>>> crc_offset);
>>>
>>
>> :( sorry, I'll send a v4 patch.
>>
>>> Otherwise, it looks good to me, you can add in next version:
>>>
>>> Reviewed-by: Chao Yu <yuchao0@huawei.com>
>>>
>>> Thanks,
>>>
>>>> + crc_offset);
>>>> + return 1;
>>>> + }
>>>> + crc = le32_to_cpu(raw_super->crc);
>>>> + if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
>>>> + f2fs_msg(sb, KERN_INFO,
>>>> + "Invalid SB checksum value: %u", crc);
>>>> + return 1;
>>>> + }
>>>> + }
>>>>
>>>> if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
>>>> f2fs_msg(sb, KERN_INFO,
>>>> @@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
>>>> int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>>>> {
>>>> struct buffer_head *bh;
>>>> + __u32 crc = 0;
>>>> int err;
>>>>
>>>> if ((recover && f2fs_readonly(sbi->sb)) ||
>>>> @@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>>>> return -EROFS;
>>>> }
>>>>
>>>> + /* we should update superblock crc here */
>>>> + if (!recover &&
>>>> + F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
>
> Can we change to use f2fs_sb_has_sb_chksum()?
>
Sure, that will be better :)
> Thanks,
>
>>>> + crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
>>>> + offsetof(struct f2fs_super_block, crc));
>>>> + F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
>>>> + }
>>>> +
>>>> /* write back-up superblock first */
>>>> bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
>>>> if (!bh)
>>>> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
>>>> index cd2e030e47b8..c86d91be6c48 100644
>>>> --- a/fs/f2fs/sysfs.c
>>>> +++ b/fs/f2fs/sysfs.c
>>>> @@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
>>>> if (f2fs_sb_has_lost_found(sb))
>>>> len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
>>>> len ? ", " : "", "lost_found");
>>>> + if (f2fs_sb_has_sb_chksum(sb))
>>>> + len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
>>>> + len ? ", " : "", "sb_checksum");
>>>> len += snprintf(buf + len, PAGE_SIZE - len, "\n");
>>>> return len;
>>>> }
>>>> @@ -337,6 +340,7 @@ enum feat_id {
>>>> FEAT_QUOTA_INO,
>>>> FEAT_INODE_CRTIME,
>>>> FEAT_LOST_FOUND,
>>>> + FEAT_SB_CHECKSUM,
>>>> };
>>>>
>>>> static ssize_t f2fs_feature_show(struct f2fs_attr *a,
>>>> @@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
>>>> case FEAT_QUOTA_INO:
>>>> case FEAT_INODE_CRTIME:
>>>> case FEAT_LOST_FOUND:
>>>> + case FEAT_SB_CHECKSUM:
>>>> return snprintf(buf, PAGE_SIZE, "supported\n");
>>>> }
>>>> return 0;
>>>> @@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
>>>> F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
>>>> F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
>>>> F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
>>>> +F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
>>>>
>>>> #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
>>>> static struct attribute *f2fs_attrs[] = {
>>>> @@ -489,6 +495,7 @@ static struct attribute *f2fs_feat_attrs[] = {
>>>> ATTR_LIST(quota_ino),
>>>> ATTR_LIST(inode_crtime),
>>>> ATTR_LIST(lost_found),
>>>> + ATTR_LIST(sb_checksum),
>>>> NULL,
>>>> };
>>>>
>>>> diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
>>>> index f70f8ac9c4f4..aa4b586569c1 100644
>>>> --- a/include/linux/f2fs_fs.h
>>>> +++ b/include/linux/f2fs_fs.h
>>>> @@ -112,7 +112,8 @@ struct f2fs_super_block {
>>>> struct f2fs_device devs[MAX_DEVICES]; /* device list */
>>>> __le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
>>>> __u8 hot_ext_count; /* # of hot file extension */
>>>> - __u8 reserved[314]; /* valid reserved region */
>>>> + __u8 reserved[310]; /* valid reserved region */
>>>> + __le32 crc; /* checksum of superblock */
>>>> } __packed;
>>>>
>>>> /*
>>>>
>>>
>>> .
>>>
>>
>>
>
> .
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply [flat|nested] 12+ messages in thread
* [RFC PATCH v4 1/3] f2fs: support superblock checksum
2018-08-08 12:21 ` Junling Zheng
2018-08-08 12:24 ` Chao Yu
@ 2018-08-08 12:54 ` Junling Zheng
2018-08-08 13:03 ` Chao Yu
1 sibling, 1 reply; 12+ messages in thread
From: Junling Zheng @ 2018-08-08 12:54 UTC (permalink / raw)
To: jaegeuk, yuchao0, chao; +Cc: miaoxie, linux-f2fs-devel
Now we support crc32 checksum for superblock.
Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
---
v3 -> v4:
- use '%zu' for size_t to fix compile warning.
- use f2fs_sb_has_sb_chksum() instead of F2FS_HAS_FEATURE().
v2 -> v3:
- add sysfs entry for superblock checksum.
- move the crc checking to the beginning of sanity_check_raw_super().
- fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
sanity_check_raw_super().
v1 -> v2:
- fix to switch endian of crc.
fs/f2fs/f2fs.h | 2 ++
fs/f2fs/super.c | 29 +++++++++++++++++++++++++++++
fs/f2fs/sysfs.c | 7 +++++++
include/linux/f2fs_fs.h | 3 ++-
4 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
#define F2FS_FEATURE_INODE_CRTIME 0x0100
#define F2FS_FEATURE_LOST_FOUND 0x0200
#define F2FS_FEATURE_VERITY 0x0400 /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
#define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
#ifdef CONFIG_BLK_DEV_ZONED
static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..3ffc336caea8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+ size_t crc_offset = 0;
+ __u32 crc = 0;
+
+ /* Check checksum_offset and crc in superblock */
+ if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+ crc_offset = le32_to_cpu(raw_super->checksum_offset);
+ if (crc_offset !=
+ offsetof(struct f2fs_super_block, crc)) {
+ f2fs_msg(sb, KERN_INFO,
+ "Invalid SB checksum offset: %zu",
+ crc_offset);
+ return 1;
+ }
+ crc = le32_to_cpu(raw_super->crc);
+ if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+ f2fs_msg(sb, KERN_INFO,
+ "Invalid SB checksum value: %u", crc);
+ return 1;
+ }
+ }
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
{
struct buffer_head *bh;
+ __u32 crc = 0;
int err;
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
return -EROFS;
}
+ /* we should update superblock crc here */
+ if (!recover &&
+ f2fs_sb_has_sb_chksum(sbi->sb)) {
+ crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+ offsetof(struct f2fs_super_block, crc));
+ F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+ }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+ if (f2fs_sb_has_sb_chksum(sb))
+ len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+ len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
}
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+ FEAT_SB_CHECKSUM,
};
static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+ case FEAT_SB_CHECKSUM:
return snprintf(buf, PAGE_SIZE, "supported\n");
}
return 0;
@@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
+F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
static struct attribute *f2fs_attrs[] = {
@@ -489,6 +495,7 @@ static struct attribute *f2fs_feat_attrs[] = {
ATTR_LIST(quota_ino),
ATTR_LIST(inode_crtime),
ATTR_LIST(lost_found),
+ ATTR_LIST(sb_checksum),
NULL,
};
diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
index f70f8ac9c4f4..aa4b586569c1 100644
--- a/include/linux/f2fs_fs.h
+++ b/include/linux/f2fs_fs.h
@@ -112,7 +112,8 @@ struct f2fs_super_block {
struct f2fs_device devs[MAX_DEVICES]; /* device list */
__le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
__u8 hot_ext_count; /* # of hot file extension */
- __u8 reserved[314]; /* valid reserved region */
+ __u8 reserved[310]; /* valid reserved region */
+ __le32 crc; /* checksum of superblock */
} __packed;
/*
--
2.18.0
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [RFC PATCH v4 1/3] f2fs: support superblock checksum
2018-08-08 12:54 ` [RFC PATCH v4 " Junling Zheng
@ 2018-08-08 13:03 ` Chao Yu
0 siblings, 0 replies; 12+ messages in thread
From: Chao Yu @ 2018-08-08 13:03 UTC (permalink / raw)
To: Junling Zheng, jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
On 2018/8/8 20:54, Junling Zheng wrote:
> Now we support crc32 checksum for superblock.
>
> Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Thanks,
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFC PATCH v2 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET
2018-08-08 10:27 ` [RFC PATCH v2 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET Junling Zheng
@ 2018-08-08 13:08 ` Chao Yu
0 siblings, 0 replies; 12+ messages in thread
From: Chao Yu @ 2018-08-08 13:08 UTC (permalink / raw)
To: Junling Zheng, jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
On 2018/8/8 18:27, Junling Zheng wrote:
> This patch renamed CHECKSUM_OFFSET to CP_CHKSUM_OFFSET.
>
> Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Thanks,
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFC PATCH v2 3/3] f2fs-tools: introduce sb checksum
2018-08-08 10:27 ` [RFC PATCH v2 3/3] f2fs-tools: introduce sb checksum Junling Zheng
@ 2018-08-08 13:25 ` Chao Yu
0 siblings, 0 replies; 12+ messages in thread
From: Chao Yu @ 2018-08-08 13:25 UTC (permalink / raw)
To: Junling Zheng, jaegeuk, yuchao0; +Cc: miaoxie, linux-f2fs-devel
On 2018/8/8 18:27, Junling Zheng wrote:
> This patch introduced superblock checksum.
It needs to update sb_chksum in sanity_check_area_boundary()?
To avoid missing updating sb_chksum, how about packaging a function to commit
superblock like what we did in kernel side, so that we can update chksum in
packaged function. IMO, we can add one other patch to do that before this one.
Thanks,
>
> Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
> ---
> v1 -> v2:
> - Rename CHKSUM_OFFSET_SB to SB_CHKSUM_OFFSET.
> - Introduce update_sb_chksum() to update superblock checksum.
> fsck/fsck.h | 1 +
> fsck/mount.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
> fsck/resize.c | 3 +++
> include/f2fs_fs.h | 6 +++++-
> mkfs/f2fs_format.c | 9 +++++++++
> 5 files changed, 62 insertions(+), 1 deletion(-)
>
> diff --git a/fsck/fsck.h b/fsck/fsck.h
> index 6042e68..2172cac 100644
> --- a/fsck/fsck.h
> +++ b/fsck/fsck.h
> @@ -170,6 +170,7 @@ extern int fsck_verify(struct f2fs_sb_info *);
> extern void fsck_free(struct f2fs_sb_info *);
> extern int f2fs_do_mount(struct f2fs_sb_info *);
> extern void f2fs_do_umount(struct f2fs_sb_info *);
> +extern void update_sb_chksum(struct f2fs_super_block *);
>
> extern void flush_journal_entries(struct f2fs_sb_info *);
> extern void zero_journal_entries(struct f2fs_sb_info *);
> diff --git a/fsck/mount.c b/fsck/mount.c
> index 58ef3e6..1dfabba 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);
> @@ -558,10 +562,29 @@ static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
> return 0;
> }
>
> +static int verify_sb_chksum(struct f2fs_super_block *sb)
> +{
> + if (SB_CHKSUM_OFFSET != 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;
> +}
> +
> int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
> {
> unsigned int blocksize;
>
> + if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
> + verify_sb_chksum(sb))
> + return -1;
> +
> if (F2FS_SUPER_MAGIC != get_sb(magic))
> return -1;
>
> @@ -605,6 +628,18 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
> return 0;
> }
>
> +void update_sb_chksum(struct f2fs_super_block *sb)
> +{
> + u32 old_crc, new_crc;
> +
> + old_crc = get_sb(crc);
> + new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
> + SB_CHKSUM_OFFSET);
> + set_sb(crc, new_crc);
> + MSG(0, "Info: update SB CRC successfully "
> + "(0x%x --> 0x%x)\n", old_crc, new_crc);
> +}
> +
> int validate_super_block(struct f2fs_sb_info *sbi, int block)
> {
> u64 offset;
> @@ -646,6 +681,10 @@ 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)
> + update_sb_chksum(sbi->raw_super);
> +
> ret = dev_write(sbi->raw_super, offset,
> sizeof(struct f2fs_super_block));
> ASSERT(ret >= 0);
> @@ -2402,6 +2441,9 @@ 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)
> + update_sb_chksum(sb);
> +
> 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++) {
> @@ -2434,6 +2476,8 @@ static void tune_sb_features(struct f2fs_sb_info *sbi)
> if (!sb_changed)
> return;
>
> + if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)
> + update_sb_chksum(sb);
> write_superblock(sb);
> }
>
> diff --git a/fsck/resize.c b/fsck/resize.c
> index e9612b3..ca8c6f9 100644
> --- a/fsck/resize.c
> +++ b/fsck/resize.c
> @@ -160,6 +160,9 @@ safe_resize:
> (get_sb(segment_count_main) - 2));
> return -1;
> }
> + if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)
> + update_sb_chksum(sb);
> +
> return 0;
> }
>
> diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
> index e279b9f..b6dfd29 100644
> --- a/include/f2fs_fs.h
> +++ b/include/f2fs_fs.h
> @@ -279,6 +279,7 @@ static inline uint64_t bswap_64(uint64_t val)
> #define BITS_PER_BYTE 8
> #define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */
> #define CP_CHKSUM_OFFSET 4092
> +#define SB_CHKSUM_OFFSET 3068
> #define MAX_PATH_LEN 64
> #define MAX_DEVICES 8
>
> @@ -579,6 +580,7 @@ enum {
> #define F2FS_FEATURE_INODE_CRTIME 0x0100
> #define F2FS_FEATURE_LOST_FOUND 0x0200
> #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
> +#define F2FS_FEATURE_SB_CHKSUM 0x0800
>
> #define MAX_VOLUME_NAME 512
>
> @@ -632,7 +634,8 @@ struct f2fs_super_block {
> struct f2fs_device devs[MAX_DEVICES]; /* device list */
> __le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
> __u8 hot_ext_count; /* # of hot file extension */
> - __u8 reserved[314]; /* valid reserved region */
> + __u8 reserved[310]; /* valid reserved region */
> + __le32 crc; /* checksum of superblock */
> } __attribute__((packed));
>
> /*
> @@ -1337,6 +1340,7 @@ struct feature feature_table[] = { \
> { "inode_crtime", F2FS_FEATURE_INODE_CRTIME }, \
> { "lost_found", F2FS_FEATURE_LOST_FOUND }, \
> { "verity", F2FS_FEATURE_VERITY }, /* reserved */ \
> + { "sb_checksum", F2FS_FEATURE_SB_CHKSUM }, \
> { NULL, 0x0}, \
> };
>
> diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
> index 621126c..012f3a8 100644
> --- a/mkfs/f2fs_format.c
> +++ b/mkfs/f2fs_format.c
> @@ -187,6 +187,7 @@ static int f2fs_prepare_super_block(void)
> u_int32_t sit_bitmap_size, max_sit_bitmap_size;
> u_int32_t max_nat_bitmap_size, max_nat_segments;
> u_int32_t total_zones;
> + u_int32_t crc;
> enum quota_type qtype;
> int i;
>
> @@ -504,6 +505,14 @@ static int f2fs_prepare_super_block(void)
>
> sb->feature = c.feature;
>
> + if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
> + set_sb(checksum_offset, SB_CHKSUM_OFFSET);
> + /* calculate superblock checksum */
> + crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb, SB_CHKSUM_OFFSET);
> + set_sb(crc, crc);
> + MSG(0, "Info: set SB CRC successfully (0x%x)\n", crc);
> + }
> +
> return 0;
> }
>
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFC PATCH v3 1/3] f2fs: support superblock checksum
2018-08-08 10:27 [RFC PATCH v3 1/3] f2fs: support superblock checksum Junling Zheng
` (2 preceding siblings ...)
2018-08-08 12:03 ` [RFC PATCH v3 1/3] f2fs: support superblock checksum Chao Yu
@ 2018-08-09 0:20 ` Jaegeuk Kim
3 siblings, 0 replies; 12+ messages in thread
From: Jaegeuk Kim @ 2018-08-09 0:20 UTC (permalink / raw)
To: Junling Zheng; +Cc: miaoxie, linux-f2fs-devel
Hi Junling,
Could you post this series once getting reviewed all later?
Thanks,
On 08/08, Junling Zheng wrote:
> Now we support crc32 checksum for superblock.
>
> Signed-off-by: Junling Zheng <zhengjunling@huawei.com>
> ---
> v2 -> v3:
> - add sysfs entry for superblock checksum.
> - move the crc checking to the beginning of sanity_check_raw_super().
> - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
> sanity_check_raw_super().
> v1 -> v2:
> - fix to switch endian of crc.
> fs/f2fs/f2fs.h | 2 ++
> fs/f2fs/super.c | 29 +++++++++++++++++++++++++++++
> fs/f2fs/sysfs.c | 7 +++++++
> include/linux/f2fs_fs.h | 3 ++-
> 4 files changed, 40 insertions(+), 1 deletion(-)
>
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 4525f4f82af0..d50d6efda96b 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -147,6 +147,7 @@ struct f2fs_mount_info {
> #define F2FS_FEATURE_INODE_CRTIME 0x0100
> #define F2FS_FEATURE_LOST_FOUND 0x0200
> #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
> +#define F2FS_FEATURE_SB_CHKSUM 0x0800
>
> #define F2FS_HAS_FEATURE(sb, mask) \
> ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
> @@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
> F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
> F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
> F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
> +F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
>
> #ifdef CONFIG_BLK_DEV_ZONED
> static inline int get_blkz_type(struct f2fs_sb_info *sbi,
> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
> index bd57be470e23..c3dbafa31613 100644
> --- a/fs/f2fs/super.c
> +++ b/fs/f2fs/super.c
> @@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
> (bh->b_data + F2FS_SUPER_OFFSET);
> struct super_block *sb = sbi->sb;
> unsigned int blocksize;
> + size_t crc_offset = 0;
> + __u32 crc = 0;
> +
> + /* Check checksum_offset and crc in superblock */
> + if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
> + crc_offset = le32_to_cpu(raw_super->checksum_offset);
> + if (crc_offset !=
> + offsetof(struct f2fs_super_block, crc)) {
> + f2fs_msg(sb, KERN_INFO,
> + "Invalid SB checksum offset: %lu",
> + crc_offset);
> + return 1;
> + }
> + crc = le32_to_cpu(raw_super->crc);
> + if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
> + f2fs_msg(sb, KERN_INFO,
> + "Invalid SB checksum value: %u", crc);
> + return 1;
> + }
> + }
>
> if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
> f2fs_msg(sb, KERN_INFO,
> @@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
> int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
> {
> struct buffer_head *bh;
> + __u32 crc = 0;
> int err;
>
> if ((recover && f2fs_readonly(sbi->sb)) ||
> @@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
> return -EROFS;
> }
>
> + /* we should update superblock crc here */
> + if (!recover &&
> + F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
> + crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
> + offsetof(struct f2fs_super_block, crc));
> + F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
> + }
> +
> /* write back-up superblock first */
> bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
> if (!bh)
> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
> index cd2e030e47b8..c86d91be6c48 100644
> --- a/fs/f2fs/sysfs.c
> +++ b/fs/f2fs/sysfs.c
> @@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
> if (f2fs_sb_has_lost_found(sb))
> len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
> len ? ", " : "", "lost_found");
> + if (f2fs_sb_has_sb_chksum(sb))
> + len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
> + len ? ", " : "", "sb_checksum");
> len += snprintf(buf + len, PAGE_SIZE - len, "\n");
> return len;
> }
> @@ -337,6 +340,7 @@ enum feat_id {
> FEAT_QUOTA_INO,
> FEAT_INODE_CRTIME,
> FEAT_LOST_FOUND,
> + FEAT_SB_CHECKSUM,
> };
>
> static ssize_t f2fs_feature_show(struct f2fs_attr *a,
> @@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
> case FEAT_QUOTA_INO:
> case FEAT_INODE_CRTIME:
> case FEAT_LOST_FOUND:
> + case FEAT_SB_CHECKSUM:
> return snprintf(buf, PAGE_SIZE, "supported\n");
> }
> return 0;
> @@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
> F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
> F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
> F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
> +F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
>
> #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
> static struct attribute *f2fs_attrs[] = {
> @@ -489,6 +495,7 @@ static struct attribute *f2fs_feat_attrs[] = {
> ATTR_LIST(quota_ino),
> ATTR_LIST(inode_crtime),
> ATTR_LIST(lost_found),
> + ATTR_LIST(sb_checksum),
> NULL,
> };
>
> diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
> index f70f8ac9c4f4..aa4b586569c1 100644
> --- a/include/linux/f2fs_fs.h
> +++ b/include/linux/f2fs_fs.h
> @@ -112,7 +112,8 @@ struct f2fs_super_block {
> struct f2fs_device devs[MAX_DEVICES]; /* device list */
> __le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
> __u8 hot_ext_count; /* # of hot file extension */
> - __u8 reserved[314]; /* valid reserved region */
> + __u8 reserved[310]; /* valid reserved region */
> + __le32 crc; /* checksum of superblock */
> } __packed;
>
> /*
> --
> 2.18.0
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2018-08-09 0:20 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-08 10:27 [RFC PATCH v3 1/3] f2fs: support superblock checksum Junling Zheng
2018-08-08 10:27 ` [RFC PATCH v2 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET Junling Zheng
2018-08-08 13:08 ` Chao Yu
2018-08-08 10:27 ` [RFC PATCH v2 3/3] f2fs-tools: introduce sb checksum Junling Zheng
2018-08-08 13:25 ` Chao Yu
2018-08-08 12:03 ` [RFC PATCH v3 1/3] f2fs: support superblock checksum Chao Yu
2018-08-08 12:21 ` Junling Zheng
2018-08-08 12:24 ` Chao Yu
2018-08-08 12:38 ` Junling Zheng
2018-08-08 12:54 ` [RFC PATCH v4 " Junling Zheng
2018-08-08 13:03 ` Chao Yu
2018-08-09 0:20 ` [RFC PATCH v3 " Jaegeuk Kim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).