dump.c revision 7f35b548d4b0e3c8577ad7a09433e589a0ab3f2a
1/**
2 * dump.c
3 *
4 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 *             http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include "fsck.h"
12
13#define BUF_SZ	80
14
15void sit_dump(struct f2fs_sb_info *sbi, int start_sit, int end_sit)
16{
17	struct seg_entry *se;
18	int segno;
19	char buf[BUF_SZ];
20	u32 free_segs = 0;;
21	u64 valid_blocks = 0;
22	int ret;
23	int fd;
24
25	fd = open("dump_sit", O_CREAT|O_WRONLY|O_TRUNC, 0666);
26	ASSERT(fd >= 0);
27
28	for (segno = start_sit; segno < end_sit; segno++) {
29		se = get_seg_entry(sbi, segno);
30
31		memset(buf, 0, BUF_SZ);
32		snprintf(buf, BUF_SZ, "%5d %8d\n", segno, se->valid_blocks);
33
34		ret = write(fd, buf, strlen(buf));
35		ASSERT(ret >= 0);
36
37		DBG(4, "SIT[0x%3x] : 0x%x\n", segno, se->valid_blocks);
38		if (se->valid_blocks == 0x0) {
39			free_segs++;
40		} else {
41			ASSERT(se->valid_blocks <= 512);
42			valid_blocks += se->valid_blocks;
43		}
44	}
45
46	memset(buf, 0, BUF_SZ);
47	snprintf(buf, BUF_SZ, "valid_segs:%d\t free_segs:%d\n",
48			SM_I(sbi)->main_segments - free_segs, free_segs);
49	ret = write(fd, buf, strlen(buf));
50	ASSERT(ret >= 0);
51
52	close(fd);
53	DBG(1, "Blocks [0x%lx] Free Segs [0x%x]\n", valid_blocks, free_segs);
54}
55
56void ssa_dump(struct f2fs_sb_info *sbi, int start_ssa, int end_ssa)
57{
58	struct f2fs_summary_block sum_blk;
59	char buf[BUF_SZ];
60	int segno, i, ret;
61	int fd;
62
63	fd = open("dump_ssa", O_CREAT|O_WRONLY|O_TRUNC, 0666);
64	ASSERT(fd >= 0);
65
66	for (segno = start_ssa; segno < end_ssa; segno++) {
67		ret = get_sum_block(sbi, segno, &sum_blk);
68
69		memset(buf, 0, BUF_SZ);
70		switch (ret) {
71		case SEG_TYPE_CUR_NODE:
72			snprintf(buf, BUF_SZ, "\n\nsegno: %d, Current Node\n", segno);
73			break;
74		case SEG_TYPE_CUR_DATA:
75			snprintf(buf, BUF_SZ, "\n\nsegno: %d, Current Data\n", segno);
76			break;
77		case SEG_TYPE_NODE:
78			snprintf(buf, BUF_SZ, "\n\nsegno: %d, Node\n", segno);
79			break;
80		case SEG_TYPE_DATA:
81			snprintf(buf, BUF_SZ, "\n\nsegno: %d, Data\n", segno);
82			break;
83		}
84		ret = write(fd, buf, strlen(buf));
85		ASSERT(ret >= 0);
86
87		for (i = 0; i < ENTRIES_IN_SUM; i++) {
88			memset(buf, 0, BUF_SZ);
89			if (i % 10 == 0) {
90				buf[0] = '\n';
91				ret = write(fd, buf, strlen(buf));
92				ASSERT(ret >= 0);
93			}
94			snprintf(buf, BUF_SZ, " [%8x, %3d, %3x]",
95					le32_to_cpu(sum_blk.entries[i].nid),
96					le16_to_cpu(sum_blk.entries[i].ofs_in_node),
97					sum_blk.entries[i].version);
98			ret = write(fd, buf, strlen(buf));
99			ASSERT(ret >= 0);
100		}
101	}
102	close(fd);
103}
104
105int dump_node(struct f2fs_sb_info *sbi, nid_t nid)
106{
107	struct node_info ni;
108	struct f2fs_node *node_blk;
109	int ret;
110
111	ret = get_node_info(sbi, nid, &ni);
112	ASSERT(ret >= 0);
113
114	node_blk = calloc(BLOCK_SZ, 1);
115	dev_read_block(node_blk, ni.blk_addr);
116
117	DBG(1, "Node ID               [0x%x]\n", nid);
118	DBG(1, "nat_entry.block_addr  [0x%x]\n", ni.blk_addr);
119	DBG(1, "nat_entry.version     [0x%x]\n", ni.version);
120	DBG(1, "nat_entry.ino         [0x%x]\n", ni.ino);
121
122	if (ni.blk_addr == 0x0) {
123		MSG(0, "Invalid nat entry\n\n");
124	}
125
126	DBG(1, "node_blk.footer.ino [0x%x]\n", le32_to_cpu(node_blk->footer.ino));
127	DBG(1, "node_blk.footer.nid [0x%x]\n", le32_to_cpu(node_blk->footer.nid));
128
129	if (le32_to_cpu(node_blk->footer.ino) == ni.ino &&
130			le32_to_cpu(node_blk->footer.nid) == ni.nid) {
131		print_node_info(node_blk);
132	} else {
133		MSG(0, "Invalid node block\n\n");
134	}
135
136	free(node_blk);
137	return 0;
138}
139
140void dump_cleanup(struct f2fs_sb_info *sbi)
141{
142	f2fs_do_umount(sbi);
143}
144