Commit 38fb6d0e authored by Icenowy Zheng's avatar Icenowy Zheng Committed by Jaegeuk Kim

f2fs: use EINVAL for superblock with invalid magic

The kernel mount_block_root() function expects -EACESS or -EINVAL for a
unmountable filesystem when trying to mount the root with different
filesystem types.

However, in 5.3-rc1 the behavior when F2FS code cannot find valid block
changed to return -EFSCORRUPTED(-EUCLEAN), and this error code makes
mount_block_root() fail when trying to probe F2FS.

When the magic number of the superblock mismatches, it has a high
probability that it's just not a F2FS. In this case return -EINVAL seems
to be a better result, and this return value can make mount_block_root()
probing work again.

Return -EINVAL when the superblock has magic mismatch, -EFSCORRUPTED in
other cases (the magic matches but the superblock cannot be recognized).

Fixes: 10f966bb ("f2fs: use generic EFSBADCRC/EFSCORRUPTED")
Signed-off-by: default avatarIcenowy Zheng <icenowy@aosc.io>
Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
parent 543b8c46
...@@ -2422,6 +2422,12 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2422,6 +2422,12 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
size_t crc_offset = 0; size_t crc_offset = 0;
__u32 crc = 0; __u32 crc = 0;
if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
return -EINVAL;
}
/* Check checksum_offset and crc in superblock */ /* Check checksum_offset and crc in superblock */
if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) { if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
crc_offset = le32_to_cpu(raw_super->checksum_offset); crc_offset = le32_to_cpu(raw_super->checksum_offset);
...@@ -2429,26 +2435,20 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2429,26 +2435,20 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
offsetof(struct f2fs_super_block, crc)) { offsetof(struct f2fs_super_block, crc)) {
f2fs_info(sbi, "Invalid SB checksum offset: %zu", f2fs_info(sbi, "Invalid SB checksum offset: %zu",
crc_offset); crc_offset);
return 1; return -EFSCORRUPTED;
} }
crc = le32_to_cpu(raw_super->crc); crc = le32_to_cpu(raw_super->crc);
if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) { if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
f2fs_info(sbi, "Invalid SB checksum value: %u", crc); f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
return 1; return -EFSCORRUPTED;
} }
} }
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
return 1;
}
/* Currently, support only 4KB page cache size */ /* Currently, support only 4KB page cache size */
if (F2FS_BLKSIZE != PAGE_SIZE) { if (F2FS_BLKSIZE != PAGE_SIZE) {
f2fs_info(sbi, "Invalid page_cache_size (%lu), supports only 4KB", f2fs_info(sbi, "Invalid page_cache_size (%lu), supports only 4KB",
PAGE_SIZE); PAGE_SIZE);
return 1; return -EFSCORRUPTED;
} }
/* Currently, support only 4KB block size */ /* Currently, support only 4KB block size */
...@@ -2456,14 +2456,14 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2456,14 +2456,14 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
if (blocksize != F2FS_BLKSIZE) { if (blocksize != F2FS_BLKSIZE) {
f2fs_info(sbi, "Invalid blocksize (%u), supports only 4KB", f2fs_info(sbi, "Invalid blocksize (%u), supports only 4KB",
blocksize); blocksize);
return 1; return -EFSCORRUPTED;
} }
/* check log blocks per segment */ /* check log blocks per segment */
if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) { if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
f2fs_info(sbi, "Invalid log blocks per segment (%u)", f2fs_info(sbi, "Invalid log blocks per segment (%u)",
le32_to_cpu(raw_super->log_blocks_per_seg)); le32_to_cpu(raw_super->log_blocks_per_seg));
return 1; return -EFSCORRUPTED;
} }
/* Currently, support 512/1024/2048/4096 bytes sector size */ /* Currently, support 512/1024/2048/4096 bytes sector size */
...@@ -2473,7 +2473,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2473,7 +2473,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
F2FS_MIN_LOG_SECTOR_SIZE) { F2FS_MIN_LOG_SECTOR_SIZE) {
f2fs_info(sbi, "Invalid log sectorsize (%u)", f2fs_info(sbi, "Invalid log sectorsize (%u)",
le32_to_cpu(raw_super->log_sectorsize)); le32_to_cpu(raw_super->log_sectorsize));
return 1; return -EFSCORRUPTED;
} }
if (le32_to_cpu(raw_super->log_sectors_per_block) + if (le32_to_cpu(raw_super->log_sectors_per_block) +
le32_to_cpu(raw_super->log_sectorsize) != le32_to_cpu(raw_super->log_sectorsize) !=
...@@ -2481,7 +2481,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2481,7 +2481,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)", f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
le32_to_cpu(raw_super->log_sectors_per_block), le32_to_cpu(raw_super->log_sectors_per_block),
le32_to_cpu(raw_super->log_sectorsize)); le32_to_cpu(raw_super->log_sectorsize));
return 1; return -EFSCORRUPTED;
} }
segment_count = le32_to_cpu(raw_super->segment_count); segment_count = le32_to_cpu(raw_super->segment_count);
...@@ -2495,7 +2495,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2495,7 +2495,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
if (segment_count > F2FS_MAX_SEGMENT || if (segment_count > F2FS_MAX_SEGMENT ||
segment_count < F2FS_MIN_SEGMENTS) { segment_count < F2FS_MIN_SEGMENTS) {
f2fs_info(sbi, "Invalid segment count (%u)", segment_count); f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
return 1; return -EFSCORRUPTED;
} }
if (total_sections > segment_count || if (total_sections > segment_count ||
...@@ -2503,25 +2503,25 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2503,25 +2503,25 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
segs_per_sec > segment_count || !segs_per_sec) { segs_per_sec > segment_count || !segs_per_sec) {
f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)", f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
segment_count, total_sections, segs_per_sec); segment_count, total_sections, segs_per_sec);
return 1; return -EFSCORRUPTED;
} }
if ((segment_count / segs_per_sec) < total_sections) { if ((segment_count / segs_per_sec) < total_sections) {
f2fs_info(sbi, "Small segment_count (%u < %u * %u)", f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
segment_count, segs_per_sec, total_sections); segment_count, segs_per_sec, total_sections);
return 1; return -EFSCORRUPTED;
} }
if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) { if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)", f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
segment_count, le64_to_cpu(raw_super->block_count)); segment_count, le64_to_cpu(raw_super->block_count));
return 1; return -EFSCORRUPTED;
} }
if (secs_per_zone > total_sections || !secs_per_zone) { if (secs_per_zone > total_sections || !secs_per_zone) {
f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)", f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
secs_per_zone, total_sections); secs_per_zone, total_sections);
return 1; return -EFSCORRUPTED;
} }
if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION || if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
raw_super->hot_ext_count > F2FS_MAX_EXTENSION || raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
...@@ -2531,7 +2531,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2531,7 +2531,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
le32_to_cpu(raw_super->extension_count), le32_to_cpu(raw_super->extension_count),
raw_super->hot_ext_count, raw_super->hot_ext_count,
F2FS_MAX_EXTENSION); F2FS_MAX_EXTENSION);
return 1; return -EFSCORRUPTED;
} }
if (le32_to_cpu(raw_super->cp_payload) > if (le32_to_cpu(raw_super->cp_payload) >
...@@ -2539,7 +2539,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2539,7 +2539,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
f2fs_info(sbi, "Insane cp_payload (%u > %u)", f2fs_info(sbi, "Insane cp_payload (%u > %u)",
le32_to_cpu(raw_super->cp_payload), le32_to_cpu(raw_super->cp_payload),
blocks_per_seg - F2FS_CP_PACKS); blocks_per_seg - F2FS_CP_PACKS);
return 1; return -EFSCORRUPTED;
} }
/* check reserved ino info */ /* check reserved ino info */
...@@ -2550,12 +2550,12 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, ...@@ -2550,12 +2550,12 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
le32_to_cpu(raw_super->node_ino), le32_to_cpu(raw_super->node_ino),
le32_to_cpu(raw_super->meta_ino), le32_to_cpu(raw_super->meta_ino),
le32_to_cpu(raw_super->root_ino)); le32_to_cpu(raw_super->root_ino));
return 1; return -EFSCORRUPTED;
} }
/* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */ /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
if (sanity_check_area_boundary(sbi, bh)) if (sanity_check_area_boundary(sbi, bh))
return 1; return -EFSCORRUPTED;
return 0; return 0;
} }
...@@ -2872,10 +2872,10 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi, ...@@ -2872,10 +2872,10 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
} }
/* sanity checking of raw super */ /* sanity checking of raw super */
if (sanity_check_raw_super(sbi, bh)) { err = sanity_check_raw_super(sbi, bh);
if (err) {
f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock", f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
block + 1); block + 1);
err = -EFSCORRUPTED;
brelse(bh); brelse(bh);
continue; continue;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment