fsck.f2fs: modify sit dump && add nat dump

modify sit dump && add nat dump like this:

dump_sit:

segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)

segno:      13  vblocks:512     seg_type:2      sit_pack:2

  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff
  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff
  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff
  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff  ff

dump_nat:

nid:    3       ino:    3       offset:    0    blkaddr:   2076417      pack:2
nid: 1042       ino: 1042       offset:    0    blkaddr:   2087156      pack:2
nid:57076       ino:57076       offset:    0    blkaddr:   2071232      pack:2
nid:57118       ino:57118       offset:    0    blkaddr:   2071233      pack:2
Signed-off-by: Yunlei He <heyunlei@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
This commit is contained in:
Yunlei He 2016-06-04 18:03:56 +08:00 committed by Jaegeuk Kim
parent 22aa244ad7
commit 4f105bbaba
3 changed files with 134 additions and 18 deletions

View File

@ -23,45 +23,151 @@ const char *seg_type_name[SEG_TYPE_MAX + 1] = {
"SEG_TYPE_NONE",
};
void nat_dump(struct f2fs_sb_info *sbi, int start_nat, int end_nat)
{
struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
struct f2fs_nm_info *nm_i = NM_I(sbi);
struct f2fs_nat_block *nat_block;
struct f2fs_node *node_block;
u32 nr_nat_blks, nid;
pgoff_t block_off;
pgoff_t block_addr;
char buf[BUF_SZ];
int seg_off;
int fd, ret, pack = 1;
unsigned int i;
nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
node_block = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
ASSERT(nat_block);
nr_nat_blks = get_sb(segment_count_nat) <<
(sbi->log_blocks_per_seg - 1);
fd = open("dump_nat", O_CREAT|O_WRONLY|O_TRUNC, 0666);
ASSERT(fd >= 0);
for (block_off = 0; block_off < nr_nat_blks; block_off++) {
seg_off = block_off >> sbi->log_blocks_per_seg;
block_addr = (pgoff_t)(nm_i->nat_blkaddr +
(seg_off << sbi->log_blocks_per_seg << 1) +
(block_off & ((1 << sbi->log_blocks_per_seg) - 1)));
if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) {
block_addr += sbi->blocks_per_seg;
pack = 2;
}
ret = dev_read_block(nat_block, block_addr);
ASSERT(ret >= 0);
nid = block_off * NAT_ENTRY_PER_BLOCK;
for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
struct f2fs_nat_entry raw_nat;
struct node_info ni;
ni.nid = nid + i;
if(nid + i == 0 || nid + i == 1 || nid + i == 2 )
continue;
if (lookup_nat_in_journal(sbi, nid + i,
&raw_nat) >= 0) {
node_info_from_raw_nat(&ni, &raw_nat);
ret = dev_read_block(node_block, ni.blk_addr);
ASSERT(ret >= 0);
if (ni.blk_addr != 0x0) {
memset(buf, 0, BUF_SZ);
snprintf(buf, BUF_SZ, "nid:%5u\tino:%5u\toffset:%5u"
"\tblkaddr:%10u\tpack:%d\n",ni.nid, ni.ino,
node_block->footer.flag >> OFFSET_BIT_SHIFT,
ni.blk_addr, pack);
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
}
} else {
node_info_from_raw_nat(&ni,
&nat_block->entries[i]);
if (ni.blk_addr == 0)
continue;
ret = dev_read_block(node_block, ni.blk_addr);
ASSERT(ret >= 0);
memset(buf, 0, BUF_SZ);
snprintf(buf, BUF_SZ, "nid:%5u\tino:%5u\toffset:%5u"
"\tblkaddr:%10u\tpack:%d\n",ni.nid,
ni.ino,node_block->footer.flag >>
OFFSET_BIT_SHIFT,ni.blk_addr, pack);
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
}
}
}
free(nat_block);
free(node_block);
close(fd);
}
void sit_dump(struct f2fs_sb_info *sbi, int start_sit, int end_sit)
{
struct seg_entry *se;
int segno;
struct sit_info *sit_i = SIT_I(sbi);
unsigned int segno;
char buf[BUF_SZ];
u32 free_segs = 0;;
u64 valid_blocks = 0;
int ret;
int fd;
int fd, i;
unsigned int offset;
fd = open("dump_sit", O_CREAT|O_WRONLY|O_TRUNC, 0666);
ASSERT(fd >= 0);
snprintf(buf, BUF_SZ, "segment_type(0:HD, 1:WD, 2:CD, "
"3:HN, 4:WN, 5:CN)\n");
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
for (segno = start_sit; segno < end_sit; segno++) {
se = get_seg_entry(sbi, segno);
offset = SIT_BLOCK_OFFSET(sit_i, segno);
i = f2fs_test_bit(offset, sit_i->sit_bitmap)?2:1;
memset(buf, 0, BUF_SZ);
snprintf(buf, BUF_SZ, "%5d %8d\n", segno, se->valid_blocks);
snprintf(buf, BUF_SZ, "\nsegno:%8u\tvblocks:%3u\tseg_type:%d\tsit_pack:%d\n\n",
segno, se->valid_blocks, se->type, i);
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
DBG(4, "SIT[0x%3x] : 0x%3x : %d\n", segno, se->valid_blocks, se->type);
if (se->valid_blocks == 0x0) {
free_segs++;
} else {
ASSERT(se->valid_blocks <= 512);
valid_blocks += se->valid_blocks;
for (i = 0; i < 64; i++) {
memset(buf, 0, BUF_SZ);
snprintf(buf, BUF_SZ, " %02x", *(se->cur_valid_map + i));
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
if((i+1) % 16 == 0) {
snprintf(buf, BUF_SZ, "\n");
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
}
}
}
}
memset(buf, 0, BUF_SZ);
snprintf(buf, BUF_SZ, "valid_segs:%d\t free_segs:%d\n",
SM_I(sbi)->main_segments - free_segs, free_segs);
snprintf(buf, BUF_SZ, "valid_blocks:[0x%" PRIx64 "]\tvalid_segs:%d\t free_segs:%d\n",
valid_blocks, SM_I(sbi)->main_segments - free_segs, free_segs);
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
close(fd);
DBG(1, "Blocks [0x%" PRIx64 "] Free Segs [0x%x]\n", valid_blocks, free_segs);
}
void ssa_dump(struct f2fs_sb_info *sbi, int start_ssa, int end_ssa)

View File

@ -167,6 +167,8 @@ extern void print_raw_sb_info(struct f2fs_super_block *);
/* dump.c */
struct dump_option {
nid_t nid;
int start_nat;
int end_nat;
int start_sit;
int end_sit;
int start_ssa;
@ -174,6 +176,7 @@ struct dump_option {
int32_t blk_addr;
};
extern void nat_dump(struct f2fs_sb_info *, int, int);
extern void sit_dump(struct f2fs_sb_info *, int, int);
extern void ssa_dump(struct f2fs_sb_info *, int, int);
extern void dump_node(struct f2fs_sb_info *, nid_t);

View File

@ -38,6 +38,7 @@ void dump_usage()
MSG(0, "[options]:\n");
MSG(0, " -d debug level [default:0]\n");
MSG(0, " -i inode no (hex)\n");
MSG(0, " -n [NAT dump segno from #1~#2 (decimal), for all 0~-1]\n");
MSG(0, " -s [SIT dump segno from #1~#2 (decimal), for all 0~-1]\n");
MSG(0, " -a [SSA dump segno from #1~#2 (decimal), for all 0~-1]\n");
MSG(0, " -b blk_addr (in 4KB)\n");
@ -125,9 +126,11 @@ void f2fs_parse_options(int argc, char *argv[])
}
}
} else if (!strcmp("dump.f2fs", prog)) {
const char *option_string = "d:i:s:a:b:";
const char *option_string = "d:i:n:s:a:b:";
static struct dump_option dump_opt = {
.nid = 3, /* default root ino */
.nid = 0, /* default root ino */
.start_nat = -1,
.end_nat = -1,
.start_sit = -1,
.end_sit = -1,
.start_ssa = -1,
@ -153,6 +156,11 @@ void f2fs_parse_options(int argc, char *argv[])
ret = sscanf(optarg, "%x",
&dump_opt.nid);
break;
case 'n':
ret = sscanf(optarg, "%d~%d",
&dump_opt.start_nat,
&dump_opt.end_nat);
break;
case 's':
ret = sscanf(optarg, "%d~%d",
&dump_opt.start_sit,
@ -353,26 +361,25 @@ static void do_dump(struct f2fs_sb_info *sbi)
struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
u32 flag = le32_to_cpu(ckpt->ckpt_flags);
fsck_init(sbi);
if (opt->end_nat == -1)
opt->end_nat = NM_I(sbi)->max_nid;
if (opt->end_sit == -1)
opt->end_sit = SM_I(sbi)->main_segments;
if (opt->end_ssa == -1)
opt->end_ssa = SM_I(sbi)->main_segments;
if (opt->start_nat != -1)
nat_dump(sbi, opt->start_nat, opt->end_nat);
if (opt->start_sit != -1)
sit_dump(sbi, opt->start_sit, opt->end_sit);
if (opt->start_ssa != -1)
ssa_dump(sbi, opt->start_ssa, opt->end_ssa);
if (opt->blk_addr != -1) {
if (opt->blk_addr != -1)
dump_info_from_blkaddr(sbi, opt->blk_addr);
goto cleanup;
}
if (opt->nid)
dump_node(sbi, opt->nid);
print_cp_state(flag);
dump_node(sbi, opt->nid);
cleanup:
fsck_free(sbi);
}
static int do_defrag(struct f2fs_sb_info *sbi)