From f505165a60d2f710f4f6f1ed7bf91671a82b984b Mon Sep 17 00:00:00 2001 From: nieben Date: Mon, 18 Mar 2024 11:14:08 +0800 Subject: [PATCH] f2fs_do_mount-dfx Signed-off-by: nieben --- fsck/BUILD.gn | 2 + fsck/mount.c | 9 ++++- tools/debug_tools/fsck_debug.c | 69 ++++++++++++++++++++++++++++++++++ tools/debug_tools/fsck_debug.h | 48 +++++++++++++++++++++++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 tools/debug_tools/fsck_debug.c create mode 100644 tools/debug_tools/fsck_debug.h diff --git a/fsck/BUILD.gn b/fsck/BUILD.gn index 4eaebab..9fed520 100644 --- a/fsck/BUILD.gn +++ b/fsck/BUILD.gn @@ -23,6 +23,7 @@ config("f2fs-defaults") { ohos_executable("fsck.f2fs") { configs = [ ":f2fs-defaults" ] sources = [ + "../tools/debug_tools/fsck_debug.c", "compress.c", "defrag.c", "dict.c", @@ -44,6 +45,7 @@ ohos_executable("fsck.f2fs") { include_dirs = [ ".", + "../tools/debug_tools", "//third_party/f2fs-tools", "//third_party/f2fs-tools/include", "//third_party/f2fs-tools/lib", diff --git a/fsck/mount.c b/fsck/mount.c index 4aaba61..96bc7da 100644 --- a/fsck/mount.c +++ b/fsck/mount.c @@ -9,6 +9,7 @@ * published by the Free Software Foundation. */ #include "fsck.h" +#include "fsck_debug.h" #include "node.h" #include "xattr.h" #include @@ -3594,8 +3595,10 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi) ret = validate_super_block(sbi, SB0_ADDR); if (ret) { ret = validate_super_block(sbi, SB1_ADDR); - if (ret) + if (ret) { + dump_sbi_info(sbi); return -1; + } } sb = F2FS_RAW_SUPER(sbi); @@ -3610,6 +3613,8 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi) ret = get_valid_checkpoint(sbi); if (ret) { ERR_MSG("Can't find valid checkpoint\n"); + dump_sbi_info(sbi); + print_ckpt_info(sbi); return -1; } @@ -3617,6 +3622,8 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi) if (sanity_check_ckpt(sbi)) { ERR_MSG("Checkpoint is polluted\n"); + dump_sbi_info(sbi); + print_ckpt_info(sbi); return -1; } cp = F2FS_CKPT(sbi); diff --git a/tools/debug_tools/fsck_debug.c b/tools/debug_tools/fsck_debug.c new file mode 100644 index 0000000..33ffb21 --- /dev/null +++ b/tools/debug_tools/fsck_debug.c @@ -0,0 +1,69 @@ +/** + * fsck_debug.c + * + * Copyright (C) 2024 Huawei Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ +#include "fsck_debug.h" + +void dump_sbi_info(struct f2fs_sb_info *sbi) +{ + if (sbi == NULL) { + MSG(0, "sbi is null\n"); + return; + } + + MSG(0, "\n"); + MSG(0, "+--------------------------------------------------------+\n"); + MSG(0, "| SBI |\n"); + MSG(0, "+--------------------------------------------------------+\n"); + MSG(0, "total_count %u\n", total_segments(sbi)); + MSG(0, "resvd_segs %u\n", reserved_segments(sbi)); + MSG(0, "overp_segs %u\n", overprov_segments(sbi)); + MSG(0, "valid_count %u\n", of_valid_block_count(sbi)); + MSG(0, "utilization %u\n", f2fs_utilization(sbi)); + MSG(0, "\n"); + hex_info_dump("f2fs_sb_info", sbi, + sizeof(struct f2fs_sb_info)); + MSG(0, "\n"); +} + +#define LINE_MAX_LEN 80 +#define LINE_MAX_INTS 16 +#define BATCH_INTS 8 +#define HEX_SHIFT_12 12 +#define HEX_SHIFT_8 8 +#define HEX_SHIFT_4 4 +#define HEX_MASK 0x0F +void hex_info_dump(const char *prompts, const unsigned char *buf, + unsigned int len) +{ + static const unsigned char hex_ascii[] = "0123456789abcdef"; + unsigned char line[LINE_MAX_LEN]; + unsigned int i, j, k, line_len; + unsigned int rest = len; + + MSG(0, "===HEX DUMP START: %.25s, len %u===\n", + prompts, len); + for (i = 0; i < len; i += LINE_MAX_INTS) { + line_len = rest > LINE_MAX_INTS ? LINE_MAX_INTS : rest; + k = 0; + line[k++] = hex_ascii[(i >> HEX_SHIFT_12) & HEX_MASK]; + line[k++] = hex_ascii[(i >> HEX_SHIFT_8) & HEX_MASK]; + line[k++] = hex_ascii[(i >> HEX_SHIFT_4) & HEX_MASK]; + line[k++] = hex_ascii[i & HEX_MASK]; + line[k++] = ':'; + for (j = 0; j < line_len; j++) { + j % BATCH_INTS == 0 ? line[k++] = ' ' : 1; + line[k++] = hex_ascii[(buf[i + j] >> HEX_SHIFT_4) & HEX_MASK]; + line[k++] = hex_ascii[(buf[i + j] & HEX_MASK)]; + } + line[k++] = '\0'; + rest -= line_len; + MSG(0, "%s\n", line); + } + MSG(0, "===HEX DUMP END===\n"); +} \ No newline at end of file diff --git a/tools/debug_tools/fsck_debug.h b/tools/debug_tools/fsck_debug.h new file mode 100644 index 0000000..603ef5d --- /dev/null +++ b/tools/debug_tools/fsck_debug.h @@ -0,0 +1,48 @@ +/** + * fsck_debug.h + * + * Copyright (C) 2024 Huawei Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ +#ifndef _FSCK_DEBUG_H_ +#define _FSCK_DEBUG_H_ + +#include "f2fs.h" + +void dump_sbi_info(struct f2fs_sb_info *); +void hex_info_dump(const char *, const unsigned char *, + unsigned int); + +static inline unsigned int total_segments(struct f2fs_sb_info *sbi) +{ + return sbi->blocks_per_seg == 0 ? + 0 : ((unsigned int)sbi->user_block_count) / + ((unsigned int)sbi->blocks_per_seg); +} + +static inline unsigned int reserved_segments(struct f2fs_sb_info *sbi) +{ + return sbi->sm_info == NULL ? 0 : sbi->sm_info->reserved_segments; +} + +static inline unsigned int overprov_segments(struct f2fs_sb_info *sbi) +{ + return sbi->sm_info == NULL ? 0 : sbi->sm_info->ovp_segments; +} + +static inline block_t of_valid_block_count(struct f2fs_sb_info *sbi) +{ + return sbi->total_valid_block_count; +} + +static inline unsigned int f2fs_utilization(struct f2fs_sb_info *sbi) +{ + /* valid block percentage of sbi */ + return sbi->user_block_count == 0 ? + 0 : (of_valid_block_count(sbi) * 100) / sbi->user_block_count; +} + +#endif /* _FSCK_DEBUG_H_ */ \ No newline at end of file